Skip to content

Commit eedf265

Browse files
ebiedermtorvalds
authored andcommitted
devpts: Make each mount of devpts an independent filesystem.
The /dev/ptmx device node is changed to lookup the directory entry "pts" in the same directory as the /dev/ptmx device node was opened in. If there is a "pts" entry and that entry is a devpts filesystem /dev/ptmx uses that filesystem. Otherwise the open of /dev/ptmx fails. The DEVPTS_MULTIPLE_INSTANCES configuration option is removed, so that userspace can now safely depend on each mount of devpts creating a new instance of the filesystem. Each mount of devpts is now a separate and equal filesystem. Reserved ttys are now available to all instances of devpts where the mounter is in the initial mount namespace. A new vfs helper path_pts is introduced that finds a directory entry named "pts" in the directory of the passed in path, and changes the passed in path to point to it. The helper path_pts uses a function path_parent_directory that was factored out of follow_dotdot. In the implementation of devpts: - devpts_mnt is killed as it is no longer meaningful if all mounts of devpts are equal. - pts_sb_from_inode is replaced by just inode->i_sb as all cached inodes in the tty layer are now from the devpts filesystem. - devpts_add_ref is rolled into the new function devpts_ptmx. And the unnecessary inode hold is removed. - devpts_del_ref is renamed devpts_release and reduced to just a deacrivate_super. - The newinstance mount option continues to be accepted but is now ignored. In devpts_fs.h definitions for when !CONFIG_UNIX98_PTYS are removed as they are never used. Documentation/filesystems/devices.txt is updated to describe the current situation. This has been verified to work properly on openwrt-15.05, centos5, centos6, centos7, debian-6.0.2, debian-7.9, debian-8.2, ubuntu-14.04.3, ubuntu-15.10, fedora23, magia-5, mint-17.3, opensuse-42.1, slackware-14.1, gentoo-20151225 (13.0?), archlinux-2015-12-01. With the caveat that on centos6 and on slackware-14.1 that there wind up being two instances of the devpts filesystem mounted on /dev/pts, the lower copy does not end up getting used. Signed-off-by: "Eric W. Biederman" <[email protected]> Cc: Greg KH <[email protected]> Cc: Peter Hurley <[email protected]> Cc: Peter Anvin <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Al Viro <[email protected]> Cc: Serge Hallyn <[email protected]> Cc: Willy Tarreau <[email protected]> Cc: Aurelien Jarno <[email protected]> Cc: One Thousand Gnomes <[email protected]> Cc: Jann Horn <[email protected]> Cc: Jiri Slaby <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Konstantin Khlebnikov <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 049ec1b commit eedf265

File tree

7 files changed

+126
-296
lines changed

7 files changed

+126
-296
lines changed

Documentation/filesystems/devpts.txt

Lines changed: 15 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,26 @@
1+
Each mount of the devpts filesystem is now distinct such that ptys
2+
and their indicies allocated in one mount are independent from ptys
3+
and their indicies in all other mounts.
14

2-
To support containers, we now allow multiple instances of devpts filesystem,
3-
such that indices of ptys allocated in one instance are independent of indices
4-
allocated in other instances of devpts.
5+
All mounts of the devpts filesystem now create a /dev/pts/ptmx node
6+
with permissions 0000.
57

6-
To preserve backward compatibility, this support for multiple instances is
7-
enabled only if:
8+
To retain backwards compatibility the a ptmx device node (aka any node
9+
created with "mknod name c 5 2") when opened will look for an instance
10+
of devpts under the name "pts" in the same directory as the ptmx device
11+
node.
812

9-
- CONFIG_DEVPTS_MULTIPLE_INSTANCES=y, and
10-
- '-o newinstance' mount option is specified while mounting devpts
11-
12-
IOW, devpts now supports both single-instance and multi-instance semantics.
13-
14-
If CONFIG_DEVPTS_MULTIPLE_INSTANCES=n, there is no change in behavior and
15-
this referred to as the "legacy" mode. In this mode, the new mount options
16-
(-o newinstance and -o ptmxmode) will be ignored with a 'bogus option' message
17-
on console.
18-
19-
If CONFIG_DEVPTS_MULTIPLE_INSTANCES=y and devpts is mounted without the
20-
'newinstance' option (as in current start-up scripts) the new mount binds
21-
to the initial kernel mount of devpts. This mode is referred to as the
22-
'single-instance' mode and the current, single-instance semantics are
23-
preserved, i.e PTYs are common across the system.
24-
25-
The only difference between this single-instance mode and the legacy mode
26-
is the presence of new, '/dev/pts/ptmx' node with permissions 0000, which
27-
can safely be ignored.
28-
29-
If CONFIG_DEVPTS_MULTIPLE_INSTANCES=y and 'newinstance' option is specified,
30-
the mount is considered to be in the multi-instance mode and a new instance
31-
of the devpts fs is created. Any ptys created in this instance are independent
32-
of ptys in other instances of devpts. Like in the single-instance mode, the
33-
/dev/pts/ptmx node is present. To effectively use the multi-instance mode,
34-
open of /dev/ptmx must be a redirected to '/dev/pts/ptmx' using a symlink or
35-
bind-mount.
36-
37-
Eg: A container startup script could do the following:
38-
39-
$ chmod 0666 /dev/pts/ptmx
40-
$ rm /dev/ptmx
41-
$ ln -s pts/ptmx /dev/ptmx
42-
$ ns_exec -cm /bin/bash
43-
44-
# We are now in new container
45-
46-
$ umount /dev/pts
47-
$ mount -t devpts -o newinstance lxcpts /dev/pts
48-
$ sshd -p 1234
49-
50-
where 'ns_exec -cm /bin/bash' calls clone() with CLONE_NEWNS flag and execs
51-
/bin/bash in the child process. A pty created by the sshd is not visible in
52-
the original mount of /dev/pts.
13+
As an option instead of placing a /dev/ptmx device node at /dev/ptmx
14+
it is possible to place a symlink to /dev/pts/ptmx at /dev/ptmx or
15+
to bind mount /dev/ptx/ptmx to /dev/ptmx. If you opt for using
16+
the devpts filesystem in this manner devpts should be mounted with
17+
the ptmxmode=0666, or chmod 0666 /dev/pts/ptmx should be called.
5318

5419
Total count of pty pairs in all instances is limited by sysctls:
5520
kernel.pty.max = 4096 - global limit
56-
kernel.pty.reserve = 1024 - reserve for initial instance
21+
kernel.pty.reserve = 1024 - reserved for filesystems mounted from the initial mount namespace
5722
kernel.pty.nr - current count of ptys
5823

5924
Per-instance limit could be set by adding mount option "max=<count>".
6025
This feature was added in kernel 3.4 together with sysctl kernel.pty.reserve.
6126
In kernels older than 3.4 sysctl kernel.pty.max works as per-instance limit.
62-
63-
User-space changes
64-
------------------
65-
66-
In multi-instance mode (i.e '-o newinstance' mount option is specified at least
67-
once), following user-space issues should be noted.
68-
69-
1. If -o newinstance mount option is never used, /dev/pts/ptmx can be ignored
70-
and no change is needed to system-startup scripts.
71-
72-
2. To effectively use multi-instance mode (i.e -o newinstance is specified)
73-
administrators or startup scripts should "redirect" open of /dev/ptmx to
74-
/dev/pts/ptmx using either a bind mount or symlink.
75-
76-
$ mount -t devpts -o newinstance devpts /dev/pts
77-
78-
followed by either
79-
80-
$ rm /dev/ptmx
81-
$ ln -s pts/ptmx /dev/ptmx
82-
$ chmod 666 /dev/pts/ptmx
83-
or
84-
$ mount -o bind /dev/pts/ptmx /dev/ptmx
85-
86-
3. The '/dev/ptmx -> pts/ptmx' symlink is the preferred method since it
87-
enables better error-reporting and treats both single-instance and
88-
multi-instance mounts similarly.
89-
90-
But this method requires that system-startup scripts set the mode of
91-
/dev/pts/ptmx correctly (default mode is 0000). The scripts can set the
92-
mode by, either
93-
94-
- adding ptmxmode mount option to devpts entry in /etc/fstab, or
95-
- using 'chmod 0666 /dev/pts/ptmx'
96-
97-
4. If multi-instance mode mount is needed for containers, but the system
98-
startup scripts have not yet been updated, container-startup scripts
99-
should bind mount /dev/ptmx to /dev/pts/ptmx to avoid breaking single-
100-
instance mounts.
101-
102-
Or, in general, container-startup scripts should use:
103-
104-
mount -t devpts -o newinstance -o ptmxmode=0666 devpts /dev/pts
105-
if [ ! -L /dev/ptmx ]; then
106-
mount -o bind /dev/pts/ptmx /dev/ptmx
107-
fi
108-
109-
When all devpts mounts are multi-instance, /dev/ptmx can permanently be
110-
a symlink to pts/ptmx and the bind mount can be ignored.
111-
112-
5. A multi-instance mount that is not accompanied by the /dev/ptmx to
113-
/dev/pts/ptmx redirection would result in an unusable/unreachable pty.
114-
115-
mount -t devpts -o newinstance lxcpts /dev/pts
116-
117-
immediately followed by:
118-
119-
open("/dev/ptmx")
120-
121-
would create a pty, say /dev/pts/7, in the initial kernel mount.
122-
But /dev/pts/7 would be invisible in the new mount.
123-
124-
6. The permissions for /dev/pts/ptmx node should be specified when mounting
125-
/dev/pts, using the '-o ptmxmode=%o' mount option (default is 0000).
126-
127-
mount -t devpts -o newinstance -o ptmxmode=0644 devpts /dev/pts
128-
129-
The permissions can be later be changed as usual with 'chmod'.
130-
131-
chmod 666 /dev/pts/ptmx
132-
133-
7. A mount of devpts without the 'newinstance' option results in binding to
134-
initial kernel mount. This behavior while preserving legacy semantics,
135-
does not provide strict isolation in a container environment. i.e by
136-
mounting devpts without the 'newinstance' option, a container could
137-
get visibility into the 'host' or root container's devpts.
138-
139-
To workaround this and have strict isolation, all mounts of devpts,
140-
including the mount in the root container, should use the newinstance
141-
option.

drivers/tty/Kconfig

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,6 @@ config UNIX98_PTYS
120120
All modern Linux systems use the Unix98 ptys. Say Y unless
121121
you're on an embedded system and want to conserve memory.
122122

123-
config DEVPTS_MULTIPLE_INSTANCES
124-
bool "Support multiple instances of devpts"
125-
depends on UNIX98_PTYS
126-
default n
127-
---help---
128-
Enable support for multiple instances of devpts filesystem.
129-
If you want to have isolated PTY namespaces (eg: in containers),
130-
say Y here. Otherwise, say N. If enabled, each mount of devpts
131-
filesystem with the '-o newinstance' option will create an
132-
independent PTY namespace.
133-
134123
config LEGACY_PTYS
135124
bool "Legacy (BSD) PTY support"
136125
default y

drivers/tty/pty.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
668668
else
669669
fsi = tty->link->driver_data;
670670
devpts_kill_index(fsi, tty->index);
671-
devpts_put_ref(fsi);
671+
devpts_release(fsi);
672672
}
673673

674674
static const struct tty_operations ptm_unix98_ops = {
@@ -733,10 +733,11 @@ static int ptmx_open(struct inode *inode, struct file *filp)
733733
if (retval)
734734
return retval;
735735

736-
fsi = devpts_get_ref(inode, filp);
737-
retval = -ENODEV;
738-
if (!fsi)
736+
fsi = devpts_acquire(filp);
737+
if (IS_ERR(fsi)) {
738+
retval = PTR_ERR(fsi);
739739
goto out_free_file;
740+
}
740741

741742
/* find a device that is not in use. */
742743
mutex_lock(&devpts_mutex);
@@ -745,7 +746,7 @@ static int ptmx_open(struct inode *inode, struct file *filp)
745746

746747
retval = index;
747748
if (index < 0)
748-
goto out_put_ref;
749+
goto out_put_fsi;
749750

750751

751752
mutex_lock(&tty_mutex);
@@ -789,8 +790,8 @@ static int ptmx_open(struct inode *inode, struct file *filp)
789790
return retval;
790791
out:
791792
devpts_kill_index(fsi, index);
792-
out_put_ref:
793-
devpts_put_ref(fsi);
793+
out_put_fsi:
794+
devpts_release(fsi);
794795
out_free_file:
795796
tty_free_file(filp);
796797
return retval;

0 commit comments

Comments
 (0)