Received: by 10.213.65.68 with SMTP id h4csp175305imn; Mon, 12 Mar 2018 10:15:36 -0700 (PDT) X-Google-Smtp-Source: AG47ELtQSonptzNkL9Wwb4v/AxBT2VhgoxQLiimCB93vVU2OWWW+ScezOVLocVMyyeyjp90GQE96 X-Received: by 10.98.93.193 with SMTP id n62mr8716736pfj.83.1520874936008; Mon, 12 Mar 2018 10:15:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520874935; cv=none; d=google.com; s=arc-20160816; b=Qzs7bktP0aP0bTpQNPa2jdRl+lSUqyuBs0fCK0dfJcG+gvoYs3p5yMdDo2CA7ovgYU YGkSGAFHEI76C90VFdQluy5DY4F0CHVpdolFI5+xd8KJkouzqH+rZQwA6d57s/le/A5i JCEYeSAcF5tfxUpBsaLvrCKhD6yQVhrWWFMndpo5CaBQzvBnTJskcZvZ3kMQugGSGKTK 7wsJg4V/bMWZ4IfXSrfxw0LSB72AlIR0eol5BQR33O6r84gLy8zcUCeKBrvneO5r9DJH MpnFq1u3+L7Pj9N7jVcSWjoN370GZKl+0Wgltsf4RL5fenAvNG4ucgsi7/5uQsuVFWMm VS8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=3nfFFOZrB/phSxB3hbC/PFQOud77vF4yxB0le/beYuw=; b=hGv7CASWEMTouTqIo4SRjzL2G8aZ7GTGxIaTTVW29XDfrlG0uXYVPRwy+kgn7Xed3x JulSjiellVR+v3ZCMD/oobcmLZnWtg5SwTxFgJ2zeShE206iOXaNiWFPgRVbtHH2SMDj 7uvFKCTWA3z/Y+fw9NllI/4uwKgO39+0AIq3Q/Qd3cm5hWNdtZMNV8hBAkmqD2lc73Rp Xg1dA9vN8ek3hfG70x9oSd4LM8J2Ppn86L9ZXXAgsctLY9ANml3HSKygzmtpE5nXhNrZ m30GdTmro/6NIFRsmOUyo9oyCz9GdhlsZ7KbC/IAxMKY4Te6ttBdJ/uQAYmxKoCmRgML 9VHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r7-v6si6283711ple.514.2018.03.12.10.15.19; Mon, 12 Mar 2018 10:15:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932609AbeCLROD (ORCPT + 99 others); Mon, 12 Mar 2018 13:14:03 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:40874 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932548AbeCLRNh (ORCPT ); Mon, 12 Mar 2018 13:13:37 -0400 Received: by mail-wm0-f67.google.com with SMTP id t6so17665451wmt.5 for ; Mon, 12 Mar 2018 10:13:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3nfFFOZrB/phSxB3hbC/PFQOud77vF4yxB0le/beYuw=; b=fCNj27qbxv5kO3FUKLXC+riOlb0jqTn/xh+gVbJKSZQx2oRPfV2ZqFdvpwgB6DJnXo +thkAe3hBX6KXQqGyxbctcuf5KCADPfZQ2VEfUBV5drR4LEVwabkRKRO3tqGpGG2k6Vo HQtPdINHB5CSL2Bvq+KpghwuwDlFaS9ykM10rZwLulQPAkl4z5xu/m7vsyQ7M0q8XKP0 NXsrqq2oSbISpbTxZF45B7oq8yn9TEi+ifM4y7v7Xi8gBfjpAbWNtKqRBgvXlwIkJKen yxGzFm9zWFN9qY4wR/K8ypBJxi4lm2U1R++MhXjaot1DgzBU2LMkCdJK7zh3WjRxg7hx pkqw== X-Gm-Message-State: AElRT7GKHLs4eqartOHYVzcY2YjO9uojb0hkpHvjnAvWcpWAalIMExt9 TUCmXwX4M/OJM7ybvnokt0BmIjuj X-Received: by 10.28.87.211 with SMTP id l202mr5764334wmb.32.1520874816452; Mon, 12 Mar 2018 10:13:36 -0700 (PDT) Received: from localhost.localdomain (u-086-c252.eap.uni-tuebingen.de. [134.2.86.252]) by smtp.gmail.com with ESMTPSA id x17sm10954864wrg.32.2018.03.12.10.13.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Mar 2018 10:13:35 -0700 (PDT) From: Christian Brauner To: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, ebiederm@xmission.com, torvalds@linux-foundation.org Cc: Christian Brauner Subject: [PATCH 2/3 v3] devpts: resolve devpts bind-mounts Date: Mon, 12 Mar 2018 18:13:29 +0100 Message-Id: <20180312171330.32054-3-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180312171330.32054-1-christian.brauner@ubuntu.com> References: <20180312171330.32054-1-christian.brauner@ubuntu.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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//fd/ 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//fd/ 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 -> / Signed-off-by: Christian Brauner Suggested-by: Eric Biederman Suggested-by: Linus Torvalds --- 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c index d3ddbb888874..b31362c6c548 100644 --- a/fs/devpts/inode.c +++ b/fs/devpts/inode.c @@ -154,27 +154,35 @@ static int devpts_ptmx_path(struct path *path) struct vfsmount *devpts_mntget(struct file *filp, struct pts_fs_info *fsi) { + bool unwind; 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; + unwind = (DEVPTS_SB(path.mnt->mnt_sb) == fsi) && + (path.mnt->mnt_root == fsi->ptmx_dentry); + /* Walk upward while the start point is a bind mount of + * a single file. + */ + while (path.mnt->mnt_root == path.dentry && unwind) + if (follow_up(&path) == 0) + break; + if ((path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC) || unwind) 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; } -- 2.15.1