inode.c 14 KB

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