inode.c 15 KB

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