inode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/devpts/inode.c
  4. *
  5. * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/sched.h>
  16. #include <linux/namei.h>
  17. #include <linux/slab.h>
  18. #include <linux/mount.h>
  19. #include <linux/tty.h>
  20. #include <linux/mutex.h>
  21. #include <linux/magic.h>
  22. #include <linux/idr.h>
  23. #include <linux/devpts_fs.h>
  24. #include <linux/parser.h>
  25. #include <linux/fsnotify.h>
  26. #include <linux/seq_file.h>
  27. #define DEVPTS_DEFAULT_MODE 0600
  28. /*
  29. * ptmx is a new node in /dev/pts and will be unused in legacy (single-
  30. * instance) mode. To prevent surprises in user space, set permissions of
  31. * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
  32. * permissions.
  33. */
  34. #define DEVPTS_DEFAULT_PTMX_MODE 0000
  35. #define PTMX_MINOR 2
  36. /*
  37. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  38. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  39. */
  40. static int pty_limit = NR_UNIX98_PTY_DEFAULT;
  41. static int pty_reserve = NR_UNIX98_PTY_RESERVE;
  42. static int pty_limit_min;
  43. static int pty_limit_max = INT_MAX;
  44. static int pty_count;
  45. static struct ctl_table pty_table[] = {
  46. {
  47. .procname = "max",
  48. .maxlen = sizeof(int),
  49. .mode = 0644,
  50. .data = &pty_limit,
  51. .proc_handler = proc_dointvec_minmax,
  52. .extra1 = &pty_limit_min,
  53. .extra2 = &pty_limit_max,
  54. }, {
  55. .procname = "reserve",
  56. .maxlen = sizeof(int),
  57. .mode = 0644,
  58. .data = &pty_reserve,
  59. .proc_handler = proc_dointvec_minmax,
  60. .extra1 = &pty_limit_min,
  61. .extra2 = &pty_limit_max,
  62. }, {
  63. .procname = "nr",
  64. .maxlen = sizeof(int),
  65. .mode = 0444,
  66. .data = &pty_count,
  67. .proc_handler = proc_dointvec,
  68. },
  69. {}
  70. };
  71. static struct ctl_table pty_kern_table[] = {
  72. {
  73. .procname = "pty",
  74. .mode = 0555,
  75. .child = pty_table,
  76. },
  77. {}
  78. };
  79. static struct ctl_table pty_root_table[] = {
  80. {
  81. .procname = "kernel",
  82. .mode = 0555,
  83. .child = pty_kern_table,
  84. },
  85. {}
  86. };
  87. static DEFINE_MUTEX(allocated_ptys_lock);
  88. static struct vfsmount *devpts_mnt;
  89. struct pts_mount_opts {
  90. int setuid;
  91. int setgid;
  92. kuid_t uid;
  93. kgid_t gid;
  94. umode_t mode;
  95. umode_t ptmxmode;
  96. int newinstance;
  97. int max;
  98. };
  99. enum {
  100. Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance, Opt_max,
  101. Opt_err
  102. };
  103. static const match_table_t tokens = {
  104. {Opt_uid, "uid=%u"},
  105. {Opt_gid, "gid=%u"},
  106. {Opt_mode, "mode=%o"},
  107. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  108. {Opt_ptmxmode, "ptmxmode=%o"},
  109. {Opt_newinstance, "newinstance"},
  110. {Opt_max, "max=%d"},
  111. #endif
  112. {Opt_err, NULL}
  113. };
  114. struct pts_fs_info {
  115. struct ida allocated_ptys;
  116. struct pts_mount_opts mount_opts;
  117. struct dentry *ptmx_dentry;
  118. };
  119. static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
  120. {
  121. return sb->s_fs_info;
  122. }
  123. static inline struct super_block *pts_sb_from_inode(struct inode *inode)
  124. {
  125. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  126. if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  127. return inode->i_sb;
  128. #endif
  129. return devpts_mnt->mnt_sb;
  130. }
  131. #define PARSE_MOUNT 0
  132. #define PARSE_REMOUNT 1
  133. /*
  134. * parse_mount_options():
  135. * Set @opts to mount options specified in @data. If an option is not
  136. * specified in @data, set it to its default value. The exception is
  137. * 'newinstance' option which can only be set/cleared on a mount (i.e.
  138. * cannot be changed during remount).
  139. *
  140. * Note: @data may be NULL (in which case all options are set to default).
  141. */
  142. static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
  143. {
  144. char *p;
  145. kuid_t uid;
  146. kgid_t gid;
  147. opts->setuid = 0;
  148. opts->setgid = 0;
  149. opts->uid = GLOBAL_ROOT_UID;
  150. opts->gid = GLOBAL_ROOT_GID;
  151. opts->mode = DEVPTS_DEFAULT_MODE;
  152. opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  153. opts->max = NR_UNIX98_PTY_MAX;
  154. /* newinstance makes sense only on initial mount */
  155. if (op == PARSE_MOUNT)
  156. opts->newinstance = 0;
  157. while ((p = strsep(&data, ",")) != NULL) {
  158. substring_t args[MAX_OPT_ARGS];
  159. int token;
  160. int option;
  161. if (!*p)
  162. continue;
  163. token = match_token(p, tokens, args);
  164. switch (token) {
  165. case Opt_uid:
  166. if (match_int(&args[0], &option))
  167. return -EINVAL;
  168. uid = make_kuid(current_user_ns(), option);
  169. if (!uid_valid(uid))
  170. return -EINVAL;
  171. opts->uid = uid;
  172. opts->setuid = 1;
  173. break;
  174. case Opt_gid:
  175. if (match_int(&args[0], &option))
  176. return -EINVAL;
  177. gid = make_kgid(current_user_ns(), option);
  178. if (!gid_valid(gid))
  179. return -EINVAL;
  180. opts->gid = gid;
  181. opts->setgid = 1;
  182. break;
  183. case Opt_mode:
  184. if (match_octal(&args[0], &option))
  185. return -EINVAL;
  186. opts->mode = option & S_IALLUGO;
  187. break;
  188. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  189. case Opt_ptmxmode:
  190. if (match_octal(&args[0], &option))
  191. return -EINVAL;
  192. opts->ptmxmode = option & S_IALLUGO;
  193. break;
  194. case Opt_newinstance:
  195. /* newinstance makes sense only on initial mount */
  196. if (op == PARSE_MOUNT)
  197. opts->newinstance = 1;
  198. break;
  199. case Opt_max:
  200. if (match_int(&args[0], &option) ||
  201. option < 0 || option > NR_UNIX98_PTY_MAX)
  202. return -EINVAL;
  203. opts->max = option;
  204. break;
  205. #endif
  206. default:
  207. printk(KERN_ERR "devpts: called with bogus options\n");
  208. return -EINVAL;
  209. }
  210. }
  211. return 0;
  212. }
  213. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  214. static int mknod_ptmx(struct super_block *sb)
  215. {
  216. int mode;
  217. int rc = -ENOMEM;
  218. struct dentry *dentry;
  219. struct inode *inode;
  220. struct dentry *root = sb->s_root;
  221. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  222. struct pts_mount_opts *opts = &fsi->mount_opts;
  223. mutex_lock(&root->d_inode->i_mutex);
  224. /* If we have already created ptmx node, return */
  225. if (fsi->ptmx_dentry) {
  226. rc = 0;
  227. goto out;
  228. }
  229. dentry = d_alloc_name(root, "ptmx");
  230. if (!dentry) {
  231. printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
  232. goto out;
  233. }
  234. /*
  235. * Create a new 'ptmx' node in this mount of devpts.
  236. */
  237. inode = new_inode(sb);
  238. if (!inode) {
  239. printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
  240. dput(dentry);
  241. goto out;
  242. }
  243. inode->i_ino = 2;
  244. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  245. mode = S_IFCHR|opts->ptmxmode;
  246. init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
  247. d_add(dentry, inode);
  248. fsi->ptmx_dentry = dentry;
  249. rc = 0;
  250. out:
  251. mutex_unlock(&root->d_inode->i_mutex);
  252. return rc;
  253. }
  254. static void update_ptmx_mode(struct pts_fs_info *fsi)
  255. {
  256. struct inode *inode;
  257. if (fsi->ptmx_dentry) {
  258. inode = fsi->ptmx_dentry->d_inode;
  259. inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
  260. }
  261. }
  262. #else
  263. static inline void update_ptmx_mode(struct pts_fs_info *fsi)
  264. {
  265. return;
  266. }
  267. #endif
  268. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  269. {
  270. int err;
  271. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  272. struct pts_mount_opts *opts = &fsi->mount_opts;
  273. err = parse_mount_options(data, PARSE_REMOUNT, opts);
  274. /*
  275. * parse_mount_options() restores options to default values
  276. * before parsing and may have changed ptmxmode. So, update the
  277. * mode in the inode too. Bogus options don't fail the remount,
  278. * so do this even on error return.
  279. */
  280. update_ptmx_mode(fsi);
  281. return err;
  282. }
  283. static int devpts_show_options(struct seq_file *seq, struct dentry *root)
  284. {
  285. struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
  286. struct pts_mount_opts *opts = &fsi->mount_opts;
  287. if (opts->setuid)
  288. seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, opts->uid));
  289. if (opts->setgid)
  290. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, opts->gid));
  291. seq_printf(seq, ",mode=%03o", opts->mode);
  292. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  293. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  294. if (opts->max < NR_UNIX98_PTY_MAX)
  295. seq_printf(seq, ",max=%d", opts->max);
  296. #endif
  297. return 0;
  298. }
  299. static const struct super_operations devpts_sops = {
  300. .statfs = simple_statfs,
  301. .remount_fs = devpts_remount,
  302. .show_options = devpts_show_options,
  303. };
  304. static void *new_pts_fs_info(void)
  305. {
  306. struct pts_fs_info *fsi;
  307. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  308. if (!fsi)
  309. return NULL;
  310. ida_init(&fsi->allocated_ptys);
  311. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  312. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  313. return fsi;
  314. }
  315. static int
  316. devpts_fill_super(struct super_block *s, void *data, int silent)
  317. {
  318. struct inode *inode;
  319. s->s_blocksize = 1024;
  320. s->s_blocksize_bits = 10;
  321. s->s_magic = DEVPTS_SUPER_MAGIC;
  322. s->s_op = &devpts_sops;
  323. s->s_time_gran = 1;
  324. s->s_fs_info = new_pts_fs_info();
  325. if (!s->s_fs_info)
  326. goto fail;
  327. inode = new_inode(s);
  328. if (!inode)
  329. goto fail;
  330. inode->i_ino = 1;
  331. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  332. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  333. inode->i_op = &simple_dir_inode_operations;
  334. inode->i_fop = &simple_dir_operations;
  335. set_nlink(inode, 2);
  336. s->s_root = d_make_root(inode);
  337. if (s->s_root)
  338. return 0;
  339. printk(KERN_ERR "devpts: get root dentry failed\n");
  340. fail:
  341. return -ENOMEM;
  342. }
  343. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  344. static int compare_init_pts_sb(struct super_block *s, void *p)
  345. {
  346. if (devpts_mnt)
  347. return devpts_mnt->mnt_sb == s;
  348. return 0;
  349. }
  350. /*
  351. * devpts_mount()
  352. *
  353. * If the '-o newinstance' mount option was specified, mount a new
  354. * (private) instance of devpts. PTYs created in this instance are
  355. * independent of the PTYs in other devpts instances.
  356. *
  357. * If the '-o newinstance' option was not specified, mount/remount the
  358. * initial kernel mount of devpts. This type of mount gives the
  359. * legacy, single-instance semantics.
  360. *
  361. * The 'newinstance' option is needed to support multiple namespace
  362. * semantics in devpts while preserving backward compatibility of the
  363. * current 'single-namespace' semantics. i.e all mounts of devpts
  364. * without the 'newinstance' mount option should bind to the initial
  365. * kernel mount, like mount_single().
  366. *
  367. * Mounts with 'newinstance' option create a new, private namespace.
  368. *
  369. * NOTE:
  370. *
  371. * For single-mount semantics, devpts cannot use mount_single(),
  372. * because mount_single()/sget() find and use the super-block from
  373. * the most recent mount of devpts. But that recent mount may be a
  374. * 'newinstance' mount and mount_single() would pick the newinstance
  375. * super-block instead of the initial super-block.
  376. */
  377. static struct dentry *devpts_mount(struct file_system_type *fs_type,
  378. int flags, const char *dev_name, void *data)
  379. {
  380. int error;
  381. struct pts_mount_opts opts;
  382. struct super_block *s;
  383. error = parse_mount_options(data, PARSE_MOUNT, &opts);
  384. if (error)
  385. return ERR_PTR(error);
  386. if (opts.newinstance)
  387. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  388. else
  389. s = sget(fs_type, compare_init_pts_sb, set_anon_super, flags,
  390. NULL);
  391. if (IS_ERR(s))
  392. return ERR_CAST(s);
  393. if (!s->s_root) {
  394. error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  395. if (error)
  396. goto out_undo_sget;
  397. s->s_flags |= MS_ACTIVE;
  398. }
  399. memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
  400. error = mknod_ptmx(s);
  401. if (error)
  402. goto out_undo_sget;
  403. return dget(s->s_root);
  404. out_undo_sget:
  405. deactivate_locked_super(s);
  406. return ERR_PTR(error);
  407. }
  408. #else
  409. /*
  410. * This supports only the legacy single-instance semantics (no
  411. * multiple-instance semantics)
  412. */
  413. static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
  414. const char *dev_name, void *data)
  415. {
  416. return mount_single(fs_type, flags, data, devpts_fill_super);
  417. }
  418. #endif
  419. static void devpts_kill_sb(struct super_block *sb)
  420. {
  421. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  422. kfree(fsi);
  423. kill_litter_super(sb);
  424. }
  425. static struct file_system_type devpts_fs_type = {
  426. .name = "devpts",
  427. .mount = devpts_mount,
  428. .kill_sb = devpts_kill_sb,
  429. };
  430. /*
  431. * The normal naming convention is simply /dev/pts/<number>; this conforms
  432. * to the System V naming convention
  433. */
  434. int devpts_new_index(struct inode *ptmx_inode)
  435. {
  436. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  437. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  438. int index;
  439. int ida_ret;
  440. retry:
  441. if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
  442. return -ENOMEM;
  443. mutex_lock(&allocated_ptys_lock);
  444. if (pty_count >= pty_limit -
  445. (fsi->mount_opts.newinstance ? pty_reserve : 0)) {
  446. mutex_unlock(&allocated_ptys_lock);
  447. return -ENOSPC;
  448. }
  449. ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
  450. if (ida_ret < 0) {
  451. mutex_unlock(&allocated_ptys_lock);
  452. if (ida_ret == -EAGAIN)
  453. goto retry;
  454. return -EIO;
  455. }
  456. if (index >= fsi->mount_opts.max) {
  457. ida_remove(&fsi->allocated_ptys, index);
  458. mutex_unlock(&allocated_ptys_lock);
  459. return -ENOSPC;
  460. }
  461. pty_count++;
  462. mutex_unlock(&allocated_ptys_lock);
  463. return index;
  464. }
  465. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  466. {
  467. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  468. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  469. mutex_lock(&allocated_ptys_lock);
  470. ida_remove(&fsi->allocated_ptys, idx);
  471. pty_count--;
  472. mutex_unlock(&allocated_ptys_lock);
  473. }
  474. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  475. {
  476. /* tty layer puts index from devpts_new_index() in here */
  477. int number = tty->index;
  478. struct tty_driver *driver = tty->driver;
  479. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  480. struct dentry *dentry;
  481. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  482. struct inode *inode = new_inode(sb);
  483. struct dentry *root = sb->s_root;
  484. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  485. struct pts_mount_opts *opts = &fsi->mount_opts;
  486. int ret = 0;
  487. char s[12];
  488. /* We're supposed to be given the slave end of a pty */
  489. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  490. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  491. if (!inode)
  492. return -ENOMEM;
  493. inode->i_ino = number + 3;
  494. inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
  495. inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
  496. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  497. init_special_inode(inode, S_IFCHR|opts->mode, device);
  498. inode->i_private = tty;
  499. tty->driver_data = inode;
  500. sprintf(s, "%d", number);
  501. mutex_lock(&root->d_inode->i_mutex);
  502. dentry = d_alloc_name(root, s);
  503. if (dentry) {
  504. d_add(dentry, inode);
  505. fsnotify_create(root->d_inode, dentry);
  506. } else {
  507. iput(inode);
  508. ret = -ENOMEM;
  509. }
  510. mutex_unlock(&root->d_inode->i_mutex);
  511. return ret;
  512. }
  513. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  514. {
  515. struct dentry *dentry;
  516. struct tty_struct *tty;
  517. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  518. /* Ensure dentry has not been deleted by devpts_pty_kill() */
  519. dentry = d_find_alias(pts_inode);
  520. if (!dentry)
  521. return NULL;
  522. tty = NULL;
  523. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  524. tty = (struct tty_struct *)pts_inode->i_private;
  525. dput(dentry);
  526. return tty;
  527. }
  528. void devpts_pty_kill(struct tty_struct *tty)
  529. {
  530. struct inode *inode = tty->driver_data;
  531. struct super_block *sb = pts_sb_from_inode(inode);
  532. struct dentry *root = sb->s_root;
  533. struct dentry *dentry;
  534. BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  535. mutex_lock(&root->d_inode->i_mutex);
  536. dentry = d_find_alias(inode);
  537. drop_nlink(inode);
  538. d_delete(dentry);
  539. dput(dentry); /* d_alloc_name() in devpts_pty_new() */
  540. dput(dentry); /* d_find_alias above */
  541. mutex_unlock(&root->d_inode->i_mutex);
  542. }
  543. static int __init init_devpts_fs(void)
  544. {
  545. int err = register_filesystem(&devpts_fs_type);
  546. struct ctl_table_header *table;
  547. if (!err) {
  548. table = register_sysctl_table(pty_root_table);
  549. devpts_mnt = kern_mount(&devpts_fs_type);
  550. if (IS_ERR(devpts_mnt)) {
  551. err = PTR_ERR(devpts_mnt);
  552. unregister_filesystem(&devpts_fs_type);
  553. unregister_sysctl_table(table);
  554. }
  555. }
  556. return err;
  557. }
  558. module_init(init_devpts_fs)