Skip to contentSkip to conversation

devpts: handle bind-mounts

PATCH Superseded Christian Brauner 3 participants 7 messages Mar 13, 2018 Kselftest

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover devpts: handle bind-mounts

Hey everyone,

This is the fourth iteration of this patch. Relevant changes are. ChangeLog v3->v4:
* small logical simplifications
* add test that bind-mounts of /dev/pts/ptmx to locations that do not resolve to a valid slave pty path under the originating devpts mount fail
ChangeLog v2->v3:
* rewritten commit message to thoroughly analyse the problem for posterity in Subject: [PATCH 2/3 v3] devpts: resolve devpts bind-mounts and in this cover letter.
* extended selftests to test for correct handling of /dev/pts/ptmx bind-mounts to /dev/ptmx and non-standard devpts mounts such as mount -t devpts devpts /mnt

Most libcs will still look at /dev/ptmx when opening the master fd of a pty device. When /dev/ptmx is a bind-mount of /dev/pts/ptmx and the TIOCGPTPEER ioctl() is used to safely retrieve a file descriptor for the slave side of the pty based on the master fd, the /proc/self/fd/{0,1,2} symlinks will point to /.

When the kernel tries to look up the root mount of the dentry for the slave file descriptor it will detect that the dentry is escaping its bind-mount since the root mount of the dentry is /dev/pts where the devpts is mounted but the root mount of /dev/ptmx is /dev.

Having bind-mounts of /dev/pts/ptmx to /dev/ptmx not working correctly is a regression. In addition, it is also a fairly common scenario in containers employing user namespaces.

To handle bind-mounts of /dev/pts/ptmx to /dev/ptmx correctly we need to walk up the bind-mounts for /dev/ptmx in devpts_mntget(). Since the contents of /proc/<pid>/fd/<nr> symlinks attached to the slave side of a file descriptor will always point to a path under the devpts mount we need to try and ensure that the kernel doesn't falsely get the impression that a pty slave file descriptor retrieved via TIOCGPTPEER based on a pty master file descriptor opened via a bind-mount of the ptmx device escapes its bind-mount. To clarify in pseudo code:

* bind-mount /dev/pts/ptmx to /dev/ptmx
* master = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_CLOEXEC);
* slave = ioctl(master, TIOCGPTPEER, O_RDWR | O_NOCTTY | O_CLOEXEC);

would cause the kernel to think that slave is escaping its bind-mount. The reason is that while the devpts mounted at /dev/pts has devtmpfs mounted at /dev as its parent mount:

21 -- -- / /dev
-- 21 -- / /dev/pts

they are on different devices

-- -- 0:6 / /dev
-- -- 0:20 / /dev/pts

which has the consequence that the pathname of the directory which forms the root of the /dev/pts mount is /. So if we bind-mount /dev/pts/ptmx to /dev/ptmx we will end up on the same device as the devtmpfs mount at /dev/pts

-- -- 0:20 /ptmx /dev/ptmx

Without the bind-mount resolution patch here the kernel will now perform the bind-mount escape check directly on /dev/ptmx. When it hits devpts_ptmx_path() calls pts_path() which in turn calls path_parent_directory(). While one would expect that path_parent_directory() *should* yield /dev it will yield / since /dev and /dev/pts are on different devices. This will cause path_pts() to fail finding a "pts" directory since there is none under /. Thus, the kernel detects that /dev/ptmx is escaping its bind-mount and will set /proc/<pid>/fd/<nr> to /.

This patch changes the logic to do bind-mount resolution and after the bind-mount has been resolved (i.e. we have traced it back to the devpts mount) we can safely perform devpts_ptmx_path() and check whether we find a "pts" directory in the parent directory of the devpts mount. Since path_parent_directory() will now correctly yield /dev as parent directory for the devpts mount at /dev/pts.

However, we can only perform devpts_ptmx_path() devpts_mntget() if we either did resolve a bind-mount or did not find a suitable devpts filesystem. The reason is that we want and need to support non-standard mountpoints for the devpts filesystem. If we call devpts_ptmx_path() although we did already find a devpts filesystem and did not resolve bind-mounts we will fail on devpts mounts such as:

mount -t devpts devpts /mnt

where no "pts" directory will be under /. So change the logic to account for this.

Here's a little reproducer that presupposes a libc that uses TIOCGPTPEER in its openpty() implementation:

unshare --mount
mount --bind /dev/pts/ptmx /dev/ptmx
chmod 666 /dev/ptmx
script
ls -al /proc/self/fd/0

with output:

lrwx------ 1 chb chb 64 Mar 7 16:41 /proc/self/fd/0 -> /

Christian Brauner (3):
devpts: hoist out check for DEVPTS_SUPER_MAGIC
devpts: resolve devpts bind-mounts
selftests: add devpts selftests

 fs/devpts/inode.c                                |  33 ++-
 tools/testing/selftests/Makefile                 |   1 +
 tools/testing/selftests/filesystems/.gitignore   |   1 +
 tools/testing/selftests/filesystems/Makefile     |   2 +-
 tools/testing/selftests/filesystems/devpts_pts.c | 313 +++++++++++++++++++++++
 5 files changed, 338 insertions(+), 12 deletions(-)
 create mode 100644 tools/testing/selftests/filesystems/devpts_pts.c
Signature · 1 line
2.15.1
#1 lore raw

PATCH v4 1/3 devpts: hoist out check for DEVPTS_SUPER_MAGIC

Hoist the check whether we have already found a suitable devpts filesystem out of devpts_ptmx_path() in preparation for the devpts bind-mount resolution patch. This is a non-functional change.

---
ChangeLog v3->v4:
* patch unchanged
ChangeLog v2->v3:
* patch unchanged
ChangeLog v1->v2:
* patch added
ChangeLog v0->v1:
* patch not present

---
 fs/devpts/inode.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
fs/devpts/inode.c +20 -15
index e31d6ed3ec32..d3ddbb888874 100644
@@ -138,10 +138,6 @@ static int devpts_ptmx_path(struct path *path)
 	struct super_block *sb;
 	int err;
 
-	/* Has the devpts filesystem already been found? */
-	if (path->mnt->mnt_sb->s_magic == DEVPTS_SUPER_MAGIC)
-		return 0;
-
 	/* Is a devpts filesystem at "pts" in the same directory? */
 	err = path_pts(path);
 	if (err)
@@ -159,17 +155,22 @@ static int devpts_ptmx_path(struct path *path)
 struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi)
 {
 	struct path path;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	dput(path.dentry);
-	if (err) {
-		mntput(path.mnt);
-		return ERR_PTR(err);
+	/* Has the devpts filesystem already been found? */
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		dput(path.dentry);
+		if (err) {
+			mntput(path.mnt);
+			return ERR_PTR(err);
+		}
 	}
+
 	if (DEVPTS_SB(path.mnt->mnt_sb) != fsi) {
 		mntput(path.mnt);
 		return ERR_PTR(-ENODEV);
@@ -182,15 +183,19 @@ struct pts_fs_info *devpts_acquire(struct file *filp)
 	struct pts_fs_info *result;
 	struct path path;
 	struct super_block *sb;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	if (err) {
-		result = ERR_PTR(err);
-		goto out;
+	/* Has the devpts filesystem already been found? */
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		if (err) {
+			result = ERR_PTR(err);
+			goto out;
+		}
 	}
 
 	/*
Signature · 1 line
2.15.1
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH 1/3 v4] devpts: hoist out check for DEVPTS_SUPER_MAGIC

Quoted 5 lines · Christian Brauner

Hoist the check whether we have already found a suitable devpts filesystem out of devpts_ptmx_path() in preparation for the devpts bind-mount resolution patch. This is a non-functional change.

Quoted 82 lines

---
ChangeLog v3->v4:
* patch unchanged
ChangeLog v2->v3:
* patch unchanged
ChangeLog v1->v2:
* patch added
ChangeLog v0->v1:
* patch not present

---
 fs/devpts/inode.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
fs/devpts/inode.c +20 -15
index e31d6ed3ec32..d3ddbb888874 100644
@@ -138,10 +138,6 @@ static int devpts_ptmx_path(struct path *path)
 	struct super_block *sb;
 	int err;
 
-	/* Has the devpts filesystem already been found? */
-	if (path->mnt->mnt_sb->s_magic == DEVPTS_SUPER_MAGIC)
-		return 0;
-
 	/* Is a devpts filesystem at "pts" in the same directory? */
 	err = path_pts(path);
 	if (err)
@@ -159,17 +155,22 @@ static int devpts_ptmx_path(struct path *path)
 struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi)
 {
 	struct path path;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	dput(path.dentry);
-	if (err) {
-		mntput(path.mnt);
-		return ERR_PTR(err);
+	/* Has the devpts filesystem already been found? */
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		dput(path.dentry);
+		if (err) {
+			mntput(path.mnt);
+			return ERR_PTR(err);
+		}
 	}
+
 	if (DEVPTS_SB(path.mnt->mnt_sb) != fsi) {
 		mntput(path.mnt);
 		return ERR_PTR(-ENODEV);
@@ -182,15 +183,19 @@ struct pts_fs_info *devpts_acquire(struct file *filp)
 	struct pts_fs_info *result;
 	struct path path;
 	struct super_block *sb;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	if (err) {
-		result = ERR_PTR(err);
-		goto out;
+	/* Has the devpts filesystem already been found? */
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		if (err) {
+			result = ERR_PTR(err);
+			goto out;
+		}
 	}
 
 	/*
#3 lore raw

Re: [PATCH 1/3 v4] devpts: hoist out check for DEVPTS_SUPER_MAGIC

In reply to Christian Brauner
Quoted 3 lines · Christian Brauner

Hoist the check whether we have already found a suitable devpts filesystem out of devpts_ptmx_path() in preparation for the devpts bind-mount resolution patch. This is a non-functional change.

Sigh. Scratch my review-by.

Quoted 45 lines

---
ChangeLog v3->v4:
* patch unchanged
ChangeLog v2->v3:
* patch unchanged
ChangeLog v1->v2:
* patch added
ChangeLog v0->v1:
* patch not present

---
 fs/devpts/inode.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
fs/devpts/inode.c +1 -10
index e31d6ed3ec32..d3ddbb888874 100644
@@ -138,10 +138,6 @@ static int devpts_ptmx_path(struct path *path)
 	struct super_block *sb;
 	int err;
 
-	/* Has the devpts filesystem already been found? */
-	if (path->mnt->mnt_sb->s_magic == DEVPTS_SUPER_MAGIC)
-		return 0;
-
 	/* Is a devpts filesystem at "pts" in the same directory? */
 	err = path_pts(path);
 	if (err)
@@ -159,17 +155,22 @@ static int devpts_ptmx_path(struct path *path)
 struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi)
 {
 	struct path path;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	dput(path.dentry);
-	if (err) {
-		mntput(path.mnt);
-		return ERR_PTR(err);
+	/* Has the devpts filesystem already been found? */

fs/devpts/inode.c static int devpts_ptmx_path(struct path *path) · line 172

Quoted 10 lines
fs/devpts/inode.c +9 -0
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		dput(path.dentry);
+		if (err) {
+			mntput(path.mnt);
+			return ERR_PTR(err);
+		}
 	}

Indenting the dput results in a dentry leak. You catch this in your next patch but this is still wrong in this one.

Quoted 18 lines
fs/devpts/inode.c +2 -5
 	if (DEVPTS_SB(path.mnt->mnt_sb) != fsi) {
 		mntput(path.mnt);
 		return ERR_PTR(-ENODEV);
@@ -182,15 +183,19 @@ struct pts_fs_info *devpts_acquire(struct file *filp)
 	struct pts_fs_info *result;
 	struct path path;
 	struct super_block *sb;
-	int err;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	err = devpts_ptmx_path(&path);
-	if (err) {
-		result = ERR_PTR(err);
-		goto out;
+	/* Has the devpts filesystem already been found? */
+	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {

fs/devpts/inode.c struct pts_fs_info *devpts_acquire(struct file *filp) · line 201

Quoted 10 lines
fs/devpts/inode.c +7 -0
+		int err;
+
+		err = devpts_ptmx_path(&path);
+		if (err) {
+			result = ERR_PTR(err);
+			goto out;
+		}
 	}
 
 	/*

Eric

#4 lore raw

PATCH v4 2/3 devpts: resolve devpts bind-mounts

In reply to Christian Brauner

Most libcs will still look at /dev/ptmx when opening the master fd of a pty device. When /dev/ptmx is a bind-mount of /dev/pts/ptmx and the TIOCGPTPEER ioctl() is used to safely retrieve a file descriptor for the slave side of the pty based on the master fd, the /proc/self/fd/{0,1,2} symlinks will point to /.

When the kernel tries to look up the root mount of the dentry for the slave file descriptor it will detect that the dentry is escaping its bind-mount since the root mount of the dentry is /dev/pts where the devpts is mounted but the root mount of /dev/ptmx is /dev.

Having bind-mounts of /dev/pts/ptmx to /dev/ptmx not working correctly is a regression. In addition, it is also a fairly common scenario in containers employing user namespaces.

To handle bind-mounts of /dev/pts/ptmx to /dev/ptmx correctly we need to walk up the bind-mounts for /dev/ptmx in devpts_mntget(). Since the contents of /proc/<pid>/fd/<nr> symlinks attached to the slave side of a file descriptor will always point to a path under the devpts mount we need to try and ensure that the kernel doesn't falsely get the impression that a pty slave file descriptor retrieved via TIOCGPTPEER based on a pty master file descriptor opened via a bind-mount of the ptmx device escapes its bind-mount. To clarify in pseudo code:

* bind-mount /dev/pts/ptmx to /dev/ptmx
* master = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_CLOEXEC);
* slave = ioctl(master, TIOCGPTPEER, O_RDWR | O_NOCTTY | O_CLOEXEC);

would cause the kernel to think that slave is escaping its bind-mount. The reason is that while the devpts mounted at /dev/pts has devtmpfs mounted at /dev as its parent mount:

21 -- -- / /dev
-- 21 -- / /dev/pts

they are on different devices

-- -- 0:6 / /dev
-- -- 0:20 / /dev/pts

which has the consequence that the pathname of the directory which forms the root of the /dev/pts mount is /. So if we bind-mount /dev/pts/ptmx to /dev/ptmx we will end up on the same device as the devtmpfs mount at /dev/pts

-- -- 0:20 /ptmx /dev/ptmx

Without the bind-mount resolution patch here the kernel will now perform the bind-mount escape check directly on /dev/ptmx. When it hits devpts_ptmx_path() calls pts_path() which in turn calls path_parent_directory(). While one would expect that path_parent_directory() *should* yield /dev it will yield / since /dev and /dev/pts are on different devices. This will cause path_pts() to fail finding a "pts" directory since there is none under /. Thus, the kernel detects that /dev/ptmx is escaping its bind-mount and will set /proc/<pid>/fd/<nr> to /.

This patch changes the logic to do bind-mount resolution and after the bind-mount has been resolved (i.e. we have traced it back to the devpts mount) we can safely perform devpts_ptmx_path() and check whether we find a "pts" directory in the parent directory of the devpts mount. Since path_parent_directory() will now correctly yield /dev as parent directory for the devpts mount at /dev/pts.

However, we can only perform devpts_ptmx_path() devpts_mntget() if we either did resolve a bind-mount or did not find a suitable devpts filesystem. The reason is that we want and need to support non-standard mountpoints for the devpts filesystem. If we call devpts_ptmx_path() although we did already find a devpts filesystem and did not resolve bind-mounts we will fail on devpts mounts such as:

mount -t devpts devpts /mnt

where no "pts" directory will be under /. So change the logic to account for this.

Here's a little reproducer that presupposes a libc that uses TIOCGPTPEER in its openpty() implementation:

unshare --mount
mount --bind /dev/pts/ptmx /dev/ptmx
chmod 666 /dev/ptmx
script
ls -al /proc/self/fd/0

with output:

lrwx------ 1 chb chb 64 Mar 7 16:41 /proc/self/fd/0 -> /

---
ChangeLog v3->v4:
* simplify if condition
ChangeLog v2->v3:
* rework logic to account for non-standard devpts mounts such as mount -t devpts devpts /mnt
ChangeLog v1->v2:
* move removal of if (path->mnt->mnt_sb->s_magic == DEVPTS_SUPER_MAGIC) condition to separate patch with non-functional changes
ChangeLog v0->v1:
* remove

        /* Has the devpts filesystem already been found? */
        if (path->mnt->mnt_sb->s_magic == DEVPTS_SUPER_MAGIC)
	                return 0
  from devpts_ptmx_path()
* check superblock after devpts_ptmx_path() returned
---
 fs/devpts/inode.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/devpts/inode.c +14 -8
index d3ddbb888874..4a94f0d2d4c8 100644
@@ -155,26 +155,32 @@ static int devpts_ptmx_path(struct path *path)
 struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi)
 {
 	struct path path;
+	int err = 0;
 
 	path = filp->f_path;
 	path_get(&path);
 
-	/* Has the devpts filesystem already been found? */
-	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) {
-		int err;
+	/* Walk upward while the start point is a bind mount of
+	 * a single file.
+	 */
+	while (path.mnt->mnt_root == path.dentry)
+		if (follow_up(&path) == 0)
+			break;
 
+	if ((path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) ||
+	    (DEVPTS_SB(path.mnt->mnt_sb) != fsi))
 		err = devpts_ptmx_path(&path);
-		dput(path.dentry);
-		if (err) {
-			mntput(path.mnt);
-			return ERR_PTR(err);
-		}
+	dput(path.dentry);
+	if (err) {
+		mntput(path.mnt);
+		return ERR_PTR(err);
 	}
 
 	if (DEVPTS_SB(path.mnt->mnt_sb) != fsi) {
 		mntput(path.mnt);
 		return ERR_PTR(-ENODEV);
 	}
+
 	return path.mnt;
 }
 
Signature · 1 line
2.15.1
#5 lore raw

Re: [PATCH 2/3 v4] devpts: resolve devpts bind-mounts

Quoted 3 lines · Christian Brauner

ChangeLog v3->v4:
* simplify if condition

Ok, I definitely prefer this simpler version.

We could simplify it even more, though. That end could be just

        ...
        dput(path.dentry);
        if (!err) {
                if (DEVPTS_SB(path.mnt->mnt_sb) == fsi)
                        return path.mnt;
                err = -ENODEV;
        }

        mntput(path.mnt);
        return ERR_PTR(err);

instead.

And it might be worth adding a comment that devpts_ptmx_path() always returns a DEVPTS filesystem of an error. I already wrote out "that's not right, you can't do DEVPTS_SB() without checking s_magic", but devpts_ptmx_path() will have done that already.

Linus

#6 lore raw

PATCH v4 3/3 selftests: add devpts selftests

In reply to Christian Brauner

This adds tests to check:
- bind-mounts from /dev/pts/ptmx to /dev/ptmx work
- non-standard mounts of devpts work
- bind-mounts of /dev/pts/ptmx to locations that do not resolve to a valid slave pty path under the originating devpts mount fail

---
ChangeLog v3->v4:
* patch unchanged
ChangeLog v2->v3:
* extend test for non-standard devpts mounts such as mount -t devpts e devpts /mnt
ChangeLog v1->v2:
* patch added
ChangeLog v0->v1:
* patch not present

---
 tools/testing/selftests/Makefile                 |   1 +
 tools/testing/selftests/filesystems/.gitignore   |   1 +
 tools/testing/selftests/filesystems/Makefile     |   2 +-
 tools/testing/selftests/filesystems/devpts_pts.c | 313 +++++++++++++++++++++++
 4 files changed, 316 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/filesystems/devpts_pts.c
tools/testing/selftests/Makefile +1 -0
index 7442dfb73b7f..dbda89c9d9b9 100644
@@ -7,6 +7,7 @@ TARGETS += cpufreq
 TARGETS += cpu-hotplug
 TARGETS += efivarfs
 TARGETS += exec
+TARGETS += filesystems
 TARGETS += firmware
 TARGETS += ftrace
 TARGETS += futex
tools/testing/selftests/filesystems/.gitignore +1 -0
index 31d6e426b6d4..8449cf6716ce 100644
@@ -1 +1,2 @@
 dnotify_test
+devpts_pts
tools/testing/selftests/filesystems/Makefile +1 -1
index 13a73bf725b5..4e6d09fb166f 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-TEST_PROGS := dnotify_test
+TEST_PROGS := dnotify_test devpts_pts
 all: $(TEST_PROGS)
 
 include ../lib.mk
tools/testing/selftests/filesystems/devpts_pts.c new file +313 -0

313 lines not on this page · raw message

Signature · 1 line
2.15.1