inode.c 15 KB

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