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