inode.c 13 KB

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