inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/namei.h>
  18. #include <linux/slab.h>
  19. #include <linux/mount.h>
  20. #include <linux/tty.h>
  21. #include <linux/mutex.h>
  22. #include <linux/magic.h>
  23. #include <linux/idr.h>
  24. #include <linux/devpts_fs.h>
  25. #include <linux/parser.h>
  26. #include <linux/fsnotify.h>
  27. #include <linux/seq_file.h>
  28. #define DEVPTS_DEFAULT_MODE 0600
  29. /*
  30. * ptmx is a new node in /dev/pts and will be unused in legacy (single-
  31. * instance) mode. To prevent surprises in user space, set permissions of
  32. * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
  33. * permissions.
  34. */
  35. #define DEVPTS_DEFAULT_PTMX_MODE 0000
  36. #define PTMX_MINOR 2
  37. /*
  38. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  39. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  40. */
  41. static int pty_limit = NR_UNIX98_PTY_DEFAULT;
  42. static int pty_reserve = NR_UNIX98_PTY_RESERVE;
  43. static int pty_limit_min;
  44. static int pty_limit_max = INT_MAX;
  45. static atomic_t pty_count = ATOMIC_INIT(0);
  46. static struct ctl_table pty_table[] = {
  47. {
  48. .procname = "max",
  49. .maxlen = sizeof(int),
  50. .mode = 0644,
  51. .data = &pty_limit,
  52. .proc_handler = proc_dointvec_minmax,
  53. .extra1 = &pty_limit_min,
  54. .extra2 = &pty_limit_max,
  55. }, {
  56. .procname = "reserve",
  57. .maxlen = sizeof(int),
  58. .mode = 0644,
  59. .data = &pty_reserve,
  60. .proc_handler = proc_dointvec_minmax,
  61. .extra1 = &pty_limit_min,
  62. .extra2 = &pty_limit_max,
  63. }, {
  64. .procname = "nr",
  65. .maxlen = sizeof(int),
  66. .mode = 0444,
  67. .data = &pty_count,
  68. .proc_handler = proc_dointvec,
  69. },
  70. {}
  71. };
  72. static struct ctl_table pty_kern_table[] = {
  73. {
  74. .procname = "pty",
  75. .mode = 0555,
  76. .child = pty_table,
  77. },
  78. {}
  79. };
  80. static struct ctl_table pty_root_table[] = {
  81. {
  82. .procname = "kernel",
  83. .mode = 0555,
  84. .child = pty_kern_table,
  85. },
  86. {}
  87. };
  88. struct pts_mount_opts {
  89. int setuid;
  90. int setgid;
  91. kuid_t uid;
  92. kgid_t gid;
  93. umode_t mode;
  94. umode_t ptmxmode;
  95. int reserve;
  96. int max;
  97. };
  98. enum {
  99. Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance, Opt_max,
  100. Opt_err
  101. };
  102. static const match_table_t tokens = {
  103. {Opt_uid, "uid=%u"},
  104. {Opt_gid, "gid=%u"},
  105. {Opt_mode, "mode=%o"},
  106. {Opt_ptmxmode, "ptmxmode=%o"},
  107. {Opt_newinstance, "newinstance"},
  108. {Opt_max, "max=%d"},
  109. {Opt_err, NULL}
  110. };
  111. struct pts_fs_info {
  112. struct ida allocated_ptys;
  113. struct pts_mount_opts mount_opts;
  114. struct super_block *sb;
  115. struct dentry *ptmx_dentry;
  116. };
  117. static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
  118. {
  119. return sb->s_fs_info;
  120. }
  121. static int devpts_ptmx_path(struct path *path)
  122. {
  123. struct super_block *sb;
  124. int err;
  125. /* Is a devpts filesystem at "pts" in the same directory? */
  126. err = path_pts(path);
  127. if (err)
  128. return err;
  129. /* Is the path the root of a devpts filesystem? */
  130. sb = path->mnt->mnt_sb;
  131. if ((sb->s_magic != DEVPTS_SUPER_MAGIC) ||
  132. (path->mnt->mnt_root != sb->s_root))
  133. return -ENODEV;
  134. return 0;
  135. }
  136. /*
  137. * Try to find a suitable devpts filesystem. We support the following
  138. * scenarios:
  139. * - The ptmx device node is located in the same directory as the devpts
  140. * mount where the pts device nodes are located.
  141. * This is e.g. the case when calling open on the /dev/pts/ptmx device
  142. * node when the devpts filesystem is mounted at /dev/pts.
  143. * - The ptmx device node is located outside the devpts filesystem mount
  144. * where the pts device nodes are located. For example, the ptmx device
  145. * is a symlink, separate device node, or bind-mount.
  146. * A supported scenario is bind-mounting /dev/pts/ptmx to /dev/ptmx and
  147. * then calling open on /dev/ptmx. In this case a suitable pts
  148. * subdirectory can be found in the common parent directory /dev of the
  149. * devpts mount and the ptmx bind-mount, after resolving the /dev/ptmx
  150. * bind-mount.
  151. * If no suitable pts subdirectory can be found this function will fail.
  152. * This is e.g. the case when bind-mounting /dev/pts/ptmx to /ptmx.
  153. */
  154. struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi)
  155. {
  156. struct path path;
  157. int err = 0;
  158. path = filp->f_path;
  159. path_get(&path);
  160. /* Walk upward while the start point is a bind mount of
  161. * a single file.
  162. */
  163. while (path.mnt->mnt_root == path.dentry)
  164. if (follow_up(&path) == 0)
  165. break;
  166. /* devpts_ptmx_path() finds a devpts fs or returns an error. */
  167. if ((path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) ||
  168. (DEVPTS_SB(path.mnt->mnt_sb) != fsi))
  169. err = devpts_ptmx_path(&path);
  170. dput(path.dentry);
  171. if (!err) {
  172. if (DEVPTS_SB(path.mnt->mnt_sb) == fsi)
  173. return path.mnt;
  174. err = -ENODEV;
  175. }
  176. mntput(path.mnt);
  177. return ERR_PTR(err);
  178. }
  179. struct pts_fs_info *devpts_acquire(struct file *filp)
  180. {
  181. struct pts_fs_info *result;
  182. struct path path;
  183. struct super_block *sb;
  184. path = filp->f_path;
  185. path_get(&path);
  186. /* Has the devpts filesystem already been found? */
  187. if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
  188. int err;
  189. err = devpts_ptmx_path(&path);
  190. if (err) {
  191. result = ERR_PTR(err);
  192. goto out;
  193. }
  194. }
  195. /*
  196. * pty code needs to hold extra references in case of last /dev/tty close
  197. */
  198. sb = path.mnt->mnt_sb;
  199. atomic_inc(&sb->s_active);
  200. result = DEVPTS_SB(sb);
  201. out:
  202. path_put(&path);
  203. return result;
  204. }
  205. void devpts_release(struct pts_fs_info *fsi)
  206. {
  207. deactivate_super(fsi->sb);
  208. }
  209. #define PARSE_MOUNT 0
  210. #define PARSE_REMOUNT 1
  211. /*
  212. * parse_mount_options():
  213. * Set @opts to mount options specified in @data. If an option is not
  214. * specified in @data, set it to its default value.
  215. *
  216. * Note: @data may be NULL (in which case all options are set to default).
  217. */
  218. static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
  219. {
  220. char *p;
  221. kuid_t uid;
  222. kgid_t gid;
  223. opts->setuid = 0;
  224. opts->setgid = 0;
  225. opts->uid = GLOBAL_ROOT_UID;
  226. opts->gid = GLOBAL_ROOT_GID;
  227. opts->mode = DEVPTS_DEFAULT_MODE;
  228. opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  229. opts->max = NR_UNIX98_PTY_MAX;
  230. /* Only allow instances mounted from the initial mount
  231. * namespace to tap the reserve pool of ptys.
  232. */
  233. if (op == PARSE_MOUNT)
  234. opts->reserve =
  235. (current->nsproxy->mnt_ns == init_task.nsproxy->mnt_ns);
  236. while ((p = strsep(&data, ",")) != NULL) {
  237. substring_t args[MAX_OPT_ARGS];
  238. int token;
  239. int option;
  240. if (!*p)
  241. continue;
  242. token = match_token(p, tokens, args);
  243. switch (token) {
  244. case Opt_uid:
  245. if (match_int(&args[0], &option))
  246. return -EINVAL;
  247. uid = make_kuid(current_user_ns(), option);
  248. if (!uid_valid(uid))
  249. return -EINVAL;
  250. opts->uid = uid;
  251. opts->setuid = 1;
  252. break;
  253. case Opt_gid:
  254. if (match_int(&args[0], &option))
  255. return -EINVAL;
  256. gid = make_kgid(current_user_ns(), option);
  257. if (!gid_valid(gid))
  258. return -EINVAL;
  259. opts->gid = gid;
  260. opts->setgid = 1;
  261. break;
  262. case Opt_mode:
  263. if (match_octal(&args[0], &option))
  264. return -EINVAL;
  265. opts->mode = option & S_IALLUGO;
  266. break;
  267. case Opt_ptmxmode:
  268. if (match_octal(&args[0], &option))
  269. return -EINVAL;
  270. opts->ptmxmode = option & S_IALLUGO;
  271. break;
  272. case Opt_newinstance:
  273. break;
  274. case Opt_max:
  275. if (match_int(&args[0], &option) ||
  276. option < 0 || option > NR_UNIX98_PTY_MAX)
  277. return -EINVAL;
  278. opts->max = option;
  279. break;
  280. default:
  281. pr_err("called with bogus options\n");
  282. return -EINVAL;
  283. }
  284. }
  285. return 0;
  286. }
  287. static int mknod_ptmx(struct super_block *sb)
  288. {
  289. int mode;
  290. int rc = -ENOMEM;
  291. struct dentry *dentry;
  292. struct inode *inode;
  293. struct dentry *root = sb->s_root;
  294. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  295. struct pts_mount_opts *opts = &fsi->mount_opts;
  296. kuid_t ptmx_uid = current_fsuid();
  297. kgid_t ptmx_gid = current_fsgid();
  298. inode_lock(d_inode(root));
  299. /* If we have already created ptmx node, return */
  300. if (fsi->ptmx_dentry) {
  301. rc = 0;
  302. goto out;
  303. }
  304. dentry = d_alloc_name(root, "ptmx");
  305. if (!dentry) {
  306. pr_err("Unable to alloc dentry for ptmx node\n");
  307. goto out;
  308. }
  309. /*
  310. * Create a new 'ptmx' node in this mount of devpts.
  311. */
  312. inode = new_inode(sb);
  313. if (!inode) {
  314. pr_err("Unable to alloc inode for ptmx node\n");
  315. dput(dentry);
  316. goto out;
  317. }
  318. inode->i_ino = 2;
  319. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  320. mode = S_IFCHR|opts->ptmxmode;
  321. init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
  322. inode->i_uid = ptmx_uid;
  323. inode->i_gid = ptmx_gid;
  324. d_add(dentry, inode);
  325. fsi->ptmx_dentry = dentry;
  326. rc = 0;
  327. out:
  328. inode_unlock(d_inode(root));
  329. return rc;
  330. }
  331. static void update_ptmx_mode(struct pts_fs_info *fsi)
  332. {
  333. struct inode *inode;
  334. if (fsi->ptmx_dentry) {
  335. inode = d_inode(fsi->ptmx_dentry);
  336. inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
  337. }
  338. }
  339. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  340. {
  341. int err;
  342. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  343. struct pts_mount_opts *opts = &fsi->mount_opts;
  344. err = parse_mount_options(data, PARSE_REMOUNT, opts);
  345. /*
  346. * parse_mount_options() restores options to default values
  347. * before parsing and may have changed ptmxmode. So, update the
  348. * mode in the inode too. Bogus options don't fail the remount,
  349. * so do this even on error return.
  350. */
  351. update_ptmx_mode(fsi);
  352. return err;
  353. }
  354. static int devpts_show_options(struct seq_file *seq, struct dentry *root)
  355. {
  356. struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
  357. struct pts_mount_opts *opts = &fsi->mount_opts;
  358. if (opts->setuid)
  359. seq_printf(seq, ",uid=%u",
  360. from_kuid_munged(&init_user_ns, opts->uid));
  361. if (opts->setgid)
  362. seq_printf(seq, ",gid=%u",
  363. from_kgid_munged(&init_user_ns, opts->gid));
  364. seq_printf(seq, ",mode=%03o", opts->mode);
  365. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  366. if (opts->max < NR_UNIX98_PTY_MAX)
  367. seq_printf(seq, ",max=%d", opts->max);
  368. return 0;
  369. }
  370. static const struct super_operations devpts_sops = {
  371. .statfs = simple_statfs,
  372. .remount_fs = devpts_remount,
  373. .show_options = devpts_show_options,
  374. };
  375. static void *new_pts_fs_info(struct super_block *sb)
  376. {
  377. struct pts_fs_info *fsi;
  378. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  379. if (!fsi)
  380. return NULL;
  381. ida_init(&fsi->allocated_ptys);
  382. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  383. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  384. fsi->sb = sb;
  385. return fsi;
  386. }
  387. static int
  388. devpts_fill_super(struct super_block *s, void *data, int silent)
  389. {
  390. struct inode *inode;
  391. int error;
  392. s->s_iflags &= ~SB_I_NODEV;
  393. s->s_blocksize = 1024;
  394. s->s_blocksize_bits = 10;
  395. s->s_magic = DEVPTS_SUPER_MAGIC;
  396. s->s_op = &devpts_sops;
  397. s->s_d_op = &simple_dentry_operations;
  398. s->s_time_gran = 1;
  399. error = -ENOMEM;
  400. s->s_fs_info = new_pts_fs_info(s);
  401. if (!s->s_fs_info)
  402. goto fail;
  403. error = parse_mount_options(data, PARSE_MOUNT, &DEVPTS_SB(s)->mount_opts);
  404. if (error)
  405. goto fail;
  406. error = -ENOMEM;
  407. inode = new_inode(s);
  408. if (!inode)
  409. goto fail;
  410. inode->i_ino = 1;
  411. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  412. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  413. inode->i_op = &simple_dir_inode_operations;
  414. inode->i_fop = &simple_dir_operations;
  415. set_nlink(inode, 2);
  416. s->s_root = d_make_root(inode);
  417. if (!s->s_root) {
  418. pr_err("get root dentry failed\n");
  419. goto fail;
  420. }
  421. error = mknod_ptmx(s);
  422. if (error)
  423. goto fail_dput;
  424. return 0;
  425. fail_dput:
  426. dput(s->s_root);
  427. s->s_root = NULL;
  428. fail:
  429. return error;
  430. }
  431. /*
  432. * devpts_mount()
  433. *
  434. * Mount a new (private) instance of devpts. PTYs created in this
  435. * instance are independent of the PTYs in other devpts instances.
  436. */
  437. static struct dentry *devpts_mount(struct file_system_type *fs_type,
  438. int flags, const char *dev_name, void *data)
  439. {
  440. return mount_nodev(fs_type, flags, data, devpts_fill_super);
  441. }
  442. static void devpts_kill_sb(struct super_block *sb)
  443. {
  444. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  445. if (fsi)
  446. ida_destroy(&fsi->allocated_ptys);
  447. kfree(fsi);
  448. kill_litter_super(sb);
  449. }
  450. static struct file_system_type devpts_fs_type = {
  451. .name = "devpts",
  452. .mount = devpts_mount,
  453. .kill_sb = devpts_kill_sb,
  454. .fs_flags = FS_USERNS_MOUNT,
  455. };
  456. /*
  457. * The normal naming convention is simply /dev/pts/<number>; this conforms
  458. * to the System V naming convention
  459. */
  460. int devpts_new_index(struct pts_fs_info *fsi)
  461. {
  462. int index = -ENOSPC;
  463. if (atomic_inc_return(&pty_count) >= (pty_limit -
  464. (fsi->mount_opts.reserve ? 0 : pty_reserve)))
  465. goto out;
  466. index = ida_alloc_max(&fsi->allocated_ptys, fsi->mount_opts.max - 1,
  467. GFP_KERNEL);
  468. out:
  469. if (index < 0)
  470. atomic_dec(&pty_count);
  471. return index;
  472. }
  473. void devpts_kill_index(struct pts_fs_info *fsi, int idx)
  474. {
  475. ida_free(&fsi->allocated_ptys, idx);
  476. atomic_dec(&pty_count);
  477. }
  478. /**
  479. * devpts_pty_new -- create a new inode in /dev/pts/
  480. * @ptmx_inode: inode of the master
  481. * @device: major+minor of the node to be created
  482. * @index: used as a name of the node
  483. * @priv: what's given back by devpts_get_priv
  484. *
  485. * The created inode is returned. Remove it from /dev/pts/ by devpts_pty_kill.
  486. */
  487. struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv)
  488. {
  489. struct dentry *dentry;
  490. struct super_block *sb = fsi->sb;
  491. struct inode *inode;
  492. struct dentry *root;
  493. struct pts_mount_opts *opts;
  494. char s[12];
  495. root = sb->s_root;
  496. opts = &fsi->mount_opts;
  497. inode = new_inode(sb);
  498. if (!inode)
  499. return ERR_PTR(-ENOMEM);
  500. inode->i_ino = index + 3;
  501. inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
  502. inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
  503. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  504. init_special_inode(inode, S_IFCHR|opts->mode, MKDEV(UNIX98_PTY_SLAVE_MAJOR, index));
  505. sprintf(s, "%d", index);
  506. dentry = d_alloc_name(root, s);
  507. if (dentry) {
  508. dentry->d_fsdata = priv;
  509. d_add(dentry, inode);
  510. fsnotify_create(d_inode(root), dentry);
  511. } else {
  512. iput(inode);
  513. dentry = ERR_PTR(-ENOMEM);
  514. }
  515. return dentry;
  516. }
  517. /**
  518. * devpts_get_priv -- get private data for a slave
  519. * @pts_inode: inode of the slave
  520. *
  521. * Returns whatever was passed as priv in devpts_pty_new for a given inode.
  522. */
  523. void *devpts_get_priv(struct dentry *dentry)
  524. {
  525. if (dentry->d_sb->s_magic != DEVPTS_SUPER_MAGIC)
  526. return NULL;
  527. return dentry->d_fsdata;
  528. }
  529. /**
  530. * devpts_pty_kill -- remove inode form /dev/pts/
  531. * @inode: inode of the slave to be removed
  532. *
  533. * This is an inverse operation of devpts_pty_new.
  534. */
  535. void devpts_pty_kill(struct dentry *dentry)
  536. {
  537. WARN_ON_ONCE(dentry->d_sb->s_magic != DEVPTS_SUPER_MAGIC);
  538. dentry->d_fsdata = NULL;
  539. drop_nlink(dentry->d_inode);
  540. d_delete(dentry);
  541. dput(dentry); /* d_alloc_name() in devpts_pty_new() */
  542. }
  543. static int __init init_devpts_fs(void)
  544. {
  545. int err = register_filesystem(&devpts_fs_type);
  546. if (!err) {
  547. register_sysctl_table(pty_root_table);
  548. }
  549. return err;
  550. }
  551. module_init(init_devpts_fs)