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