inode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 root_uid;
  247. kgid_t root_gid;
  248. root_uid = make_kuid(current_user_ns(), 0);
  249. root_gid = make_kgid(current_user_ns(), 0);
  250. if (!uid_valid(root_uid) || !gid_valid(root_gid))
  251. return -EINVAL;
  252. inode_lock(d_inode(root));
  253. /* If we have already created ptmx node, return */
  254. if (fsi->ptmx_dentry) {
  255. rc = 0;
  256. goto out;
  257. }
  258. dentry = d_alloc_name(root, "ptmx");
  259. if (!dentry) {
  260. pr_err("Unable to alloc dentry for ptmx node\n");
  261. goto out;
  262. }
  263. /*
  264. * Create a new 'ptmx' node in this mount of devpts.
  265. */
  266. inode = new_inode(sb);
  267. if (!inode) {
  268. pr_err("Unable to alloc inode for ptmx node\n");
  269. dput(dentry);
  270. goto out;
  271. }
  272. inode->i_ino = 2;
  273. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  274. mode = S_IFCHR|opts->ptmxmode;
  275. init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
  276. inode->i_uid = root_uid;
  277. inode->i_gid = root_gid;
  278. d_add(dentry, inode);
  279. fsi->ptmx_dentry = dentry;
  280. rc = 0;
  281. out:
  282. inode_unlock(d_inode(root));
  283. return rc;
  284. }
  285. static void update_ptmx_mode(struct pts_fs_info *fsi)
  286. {
  287. struct inode *inode;
  288. if (fsi->ptmx_dentry) {
  289. inode = d_inode(fsi->ptmx_dentry);
  290. inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
  291. }
  292. }
  293. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  294. {
  295. int err;
  296. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  297. struct pts_mount_opts *opts = &fsi->mount_opts;
  298. sync_filesystem(sb);
  299. err = parse_mount_options(data, PARSE_REMOUNT, opts);
  300. /*
  301. * parse_mount_options() restores options to default values
  302. * before parsing and may have changed ptmxmode. So, update the
  303. * mode in the inode too. Bogus options don't fail the remount,
  304. * so do this even on error return.
  305. */
  306. update_ptmx_mode(fsi);
  307. return err;
  308. }
  309. static int devpts_show_options(struct seq_file *seq, struct dentry *root)
  310. {
  311. struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
  312. struct pts_mount_opts *opts = &fsi->mount_opts;
  313. if (opts->setuid)
  314. seq_printf(seq, ",uid=%u",
  315. from_kuid_munged(&init_user_ns, opts->uid));
  316. if (opts->setgid)
  317. seq_printf(seq, ",gid=%u",
  318. from_kgid_munged(&init_user_ns, opts->gid));
  319. seq_printf(seq, ",mode=%03o", opts->mode);
  320. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  321. if (opts->max < NR_UNIX98_PTY_MAX)
  322. seq_printf(seq, ",max=%d", opts->max);
  323. return 0;
  324. }
  325. static const struct super_operations devpts_sops = {
  326. .statfs = simple_statfs,
  327. .remount_fs = devpts_remount,
  328. .show_options = devpts_show_options,
  329. };
  330. static void *new_pts_fs_info(struct super_block *sb)
  331. {
  332. struct pts_fs_info *fsi;
  333. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  334. if (!fsi)
  335. return NULL;
  336. ida_init(&fsi->allocated_ptys);
  337. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  338. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  339. fsi->sb = sb;
  340. return fsi;
  341. }
  342. static int
  343. devpts_fill_super(struct super_block *s, void *data, int silent)
  344. {
  345. struct inode *inode;
  346. s->s_iflags &= ~SB_I_NODEV;
  347. s->s_blocksize = 1024;
  348. s->s_blocksize_bits = 10;
  349. s->s_magic = DEVPTS_SUPER_MAGIC;
  350. s->s_op = &devpts_sops;
  351. s->s_time_gran = 1;
  352. s->s_fs_info = new_pts_fs_info(s);
  353. if (!s->s_fs_info)
  354. goto fail;
  355. inode = new_inode(s);
  356. if (!inode)
  357. goto fail;
  358. inode->i_ino = 1;
  359. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  360. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  361. inode->i_op = &simple_dir_inode_operations;
  362. inode->i_fop = &simple_dir_operations;
  363. set_nlink(inode, 2);
  364. s->s_root = d_make_root(inode);
  365. if (s->s_root)
  366. return 0;
  367. pr_err("get root dentry failed\n");
  368. fail:
  369. return -ENOMEM;
  370. }
  371. /*
  372. * devpts_mount()
  373. *
  374. * Mount a new (private) instance of devpts. PTYs created in this
  375. * instance are independent of the PTYs in other devpts instances.
  376. */
  377. static struct dentry *devpts_mount(struct file_system_type *fs_type,
  378. int flags, const char *dev_name, void *data)
  379. {
  380. int error;
  381. struct pts_mount_opts opts;
  382. struct super_block *s;
  383. error = parse_mount_options(data, PARSE_MOUNT, &opts);
  384. if (error)
  385. return ERR_PTR(error);
  386. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  387. if (IS_ERR(s))
  388. return ERR_CAST(s);
  389. if (!s->s_root) {
  390. error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  391. if (error)
  392. goto out_undo_sget;
  393. s->s_flags |= MS_ACTIVE;
  394. }
  395. memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
  396. error = mknod_ptmx(s);
  397. if (error)
  398. goto out_undo_sget;
  399. return dget(s->s_root);
  400. out_undo_sget:
  401. deactivate_locked_super(s);
  402. return ERR_PTR(error);
  403. }
  404. static void devpts_kill_sb(struct super_block *sb)
  405. {
  406. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  407. ida_destroy(&fsi->allocated_ptys);
  408. kfree(fsi);
  409. kill_litter_super(sb);
  410. }
  411. static struct file_system_type devpts_fs_type = {
  412. .name = "devpts",
  413. .mount = devpts_mount,
  414. .kill_sb = devpts_kill_sb,
  415. .fs_flags = FS_USERNS_MOUNT,
  416. };
  417. /*
  418. * The normal naming convention is simply /dev/pts/<number>; this conforms
  419. * to the System V naming convention
  420. */
  421. int devpts_new_index(struct pts_fs_info *fsi)
  422. {
  423. int index;
  424. int ida_ret;
  425. retry:
  426. if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
  427. return -ENOMEM;
  428. mutex_lock(&allocated_ptys_lock);
  429. if (pty_count >= (pty_limit -
  430. (fsi->mount_opts.reserve ? 0 : pty_reserve))) {
  431. mutex_unlock(&allocated_ptys_lock);
  432. return -ENOSPC;
  433. }
  434. ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
  435. if (ida_ret < 0) {
  436. mutex_unlock(&allocated_ptys_lock);
  437. if (ida_ret == -EAGAIN)
  438. goto retry;
  439. return -EIO;
  440. }
  441. if (index >= fsi->mount_opts.max) {
  442. ida_remove(&fsi->allocated_ptys, index);
  443. mutex_unlock(&allocated_ptys_lock);
  444. return -ENOSPC;
  445. }
  446. pty_count++;
  447. mutex_unlock(&allocated_ptys_lock);
  448. return index;
  449. }
  450. void devpts_kill_index(struct pts_fs_info *fsi, int idx)
  451. {
  452. mutex_lock(&allocated_ptys_lock);
  453. ida_remove(&fsi->allocated_ptys, idx);
  454. pty_count--;
  455. mutex_unlock(&allocated_ptys_lock);
  456. }
  457. /**
  458. * devpts_pty_new -- create a new inode in /dev/pts/
  459. * @ptmx_inode: inode of the master
  460. * @device: major+minor of the node to be created
  461. * @index: used as a name of the node
  462. * @priv: what's given back by devpts_get_priv
  463. *
  464. * The created inode is returned. Remove it from /dev/pts/ by devpts_pty_kill.
  465. */
  466. struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv)
  467. {
  468. struct dentry *dentry;
  469. struct super_block *sb = fsi->sb;
  470. struct inode *inode;
  471. struct dentry *root;
  472. struct pts_mount_opts *opts;
  473. char s[12];
  474. root = sb->s_root;
  475. opts = &fsi->mount_opts;
  476. inode = new_inode(sb);
  477. if (!inode)
  478. return ERR_PTR(-ENOMEM);
  479. inode->i_ino = index + 3;
  480. inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
  481. inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
  482. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  483. init_special_inode(inode, S_IFCHR|opts->mode, MKDEV(UNIX98_PTY_SLAVE_MAJOR, index));
  484. sprintf(s, "%d", index);
  485. dentry = d_alloc_name(root, s);
  486. if (dentry) {
  487. dentry->d_fsdata = priv;
  488. d_add(dentry, inode);
  489. fsnotify_create(d_inode(root), dentry);
  490. } else {
  491. iput(inode);
  492. dentry = ERR_PTR(-ENOMEM);
  493. }
  494. return dentry;
  495. }
  496. /**
  497. * devpts_get_priv -- get private data for a slave
  498. * @pts_inode: inode of the slave
  499. *
  500. * Returns whatever was passed as priv in devpts_pty_new for a given inode.
  501. */
  502. void *devpts_get_priv(struct dentry *dentry)
  503. {
  504. WARN_ON_ONCE(dentry->d_sb->s_magic != DEVPTS_SUPER_MAGIC);
  505. return dentry->d_fsdata;
  506. }
  507. /**
  508. * devpts_pty_kill -- remove inode form /dev/pts/
  509. * @inode: inode of the slave to be removed
  510. *
  511. * This is an inverse operation of devpts_pty_new.
  512. */
  513. void devpts_pty_kill(struct dentry *dentry)
  514. {
  515. WARN_ON_ONCE(dentry->d_sb->s_magic != DEVPTS_SUPER_MAGIC);
  516. dentry->d_fsdata = NULL;
  517. drop_nlink(dentry->d_inode);
  518. d_delete(dentry);
  519. dput(dentry); /* d_alloc_name() in devpts_pty_new() */
  520. }
  521. static int __init init_devpts_fs(void)
  522. {
  523. int err = register_filesystem(&devpts_fs_type);
  524. if (!err) {
  525. register_sysctl_table(pty_root_table);
  526. }
  527. return err;
  528. }
  529. module_init(init_devpts_fs)