Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp304400pxb; Thu, 21 Jan 2021 07:32:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpItf9/gh9L4Nlzbs85BXBzTba4KZTO+ZW1oyprHbFENif45x4V1a201ervxiiv2cR0nJ4 X-Received: by 2002:a05:6402:1751:: with SMTP id v17mr11922836edx.289.1611243137803; Thu, 21 Jan 2021 07:32:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611243137; cv=none; d=google.com; s=arc-20160816; b=YUcW9L7ATYEsZ3CbH/Y8RnO9k4oa+cct4ov37u0k6on6OPAizFtjyuchgkcZ2WAnjY 78q3sfBc7YzXF1MNb5vmZT+x/kLkRjWNMViE1+eJhQDCvrLVCbXAdVY3NwEiTGugMVcq T97EvYiR4z02eeGvo3gF1wPJeZJ5TXoAJbr563WVoneURc5gN/kxvKskJ2urvEskrXE1 WG7bHEYyi/n5sDiV9piySDXCDnWh6LO3BcoB55dq1G9TsJASgCMwc+TM3X49At8BtlwG SW96vJB9Ujl+I8xLeezgHL9oOfR0mriY3KchKB80tBjIg6v0FYQZUYff3H8T/8jPIPLF HXaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=0elGaZur9bIFvCSmY37sxNE0eG5XTWtho8YE6oWcakQ=; b=CWX2lisYHgeG3SUtRanKJ9pYD/j4l1uogaKnp2/QU0PcmJNf95bRA5bDcpVvjfiTYY s4vfPMz3xJtUkzKL9JiQNa0ilnhTWEtdUldviwFPOmnfccVrhuToZGIbwUU3fmpnMb5U qpXVIil7r5KUZPOZ67LIAiRFwrPBfuAtR/Ef3Y51AVc/r9SGO9v6FAQ8XqGaw6dmRkjH CsAq7DYEuHXsSMMiWd9ff8Mmt0RMpswycD1E9gPtMFVhPmIexuotXSLUOY6SAMDZhp0C T3Ua5hL/jWRuSnPSRQI2kHLRTTTjAgBx1/dKeQip6Tt8olbSwAaMo7+fY+g4zu/yHIo0 6gCA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x43si2402681ede.248.2021.01.21.07.31.53; Thu, 21 Jan 2021 07:32:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731793AbhAUPa2 (ORCPT + 99 others); Thu, 21 Jan 2021 10:30:28 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:54090 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731136AbhAUNWN (ORCPT ); Thu, 21 Jan 2021 08:22:13 -0500 Received: from ip5f5af0a0.dynamic.kabel-deutschland.de ([95.90.240.160] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l2Ztt-0005g7-Cs; Thu, 21 Jan 2021 13:21:25 +0000 From: Christian Brauner To: Alexander Viro , Christoph Hellwig , linux-fsdevel@vger.kernel.org Cc: John Johansen , James Morris , Mimi Zohar , Dmitry Kasatkin , Stephen Smalley , Casey Schaufler , Arnd Bergmann , Andreas Dilger , OGAWA Hirofumi , Geoffrey Thomas , Mrunal Patel , Josh Triplett , Andy Lutomirski , Theodore Tso , Alban Crequy , Tycho Andersen , David Howells , James Bottomley , Seth Forshee , =?UTF-8?q?St=C3=A9phane=20Graber?= , Linus Torvalds , Aleksa Sarai , Lennart Poettering , "Eric W. Biederman" , smbarber@chromium.org, Phil Estes , Serge Hallyn , Kees Cook , Todd Kjos , Paul Moore , Jonathan Corbet , containers@lists.linux-foundation.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, linux-integrity@vger.kernel.org, selinux@vger.kernel.org, Christian Brauner Subject: [PATCH v6 16/40] open: handle idmapped mounts Date: Thu, 21 Jan 2021 14:19:35 +0100 Message-Id: <20210121131959.646623-17-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210121131959.646623-1-christian.brauner@ubuntu.com> References: <20210121131959.646623-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; i=YKSd0FdGGLgCSTeDw7b6sTvRU/kXPfMpmc0z0c+ceJs=; m=QtXKH7l01QTmlgDZWGGLs41QsIHJYr2I07NJQrbQhUc=; p=UQ8ZL9iZHePP4j74jgLUJqMxCUpzv40jcAqgWmdc+iM=; g=fc42bb0317f59f78c12f1f5b6f750e685efc9c8e X-Patch-Sig: m=pgp; i=christian.brauner@ubuntu.com; s=0x0x91C61BC06578DCA2; b=iHQEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCYAl9pAAKCRCRxhvAZXjcom58APiYXnz MLA9PdyDTNWHJ1hzxQwmhv0RHo0AradNsIDslAQDIvEnNGlH+yVfr0r/waOULLGeiGRFi0nzVv0+4 7M8YDg== Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org For core file operations such as changing directories or chrooting, determining file access, changing mode or ownership the vfs will verify that the caller is privileged over the inode. Extend the various helpers to handle idmapped mounts. If the inode is accessed through an idmapped mount map it into the mount's user namespace. Afterwards the permissions checks are identical to non-idmapped mounts. When changing file ownership we need to map the uid and gid from the mount's user namespace. If the initial user namespace is passed nothing changes so non-idmapped mounts will see identical behavior as before. Link: https://lore.kernel.org/r/20210112220124.837960-24-christian.brauner@ubuntu.com Cc: Christoph Hellwig Cc: David Howells Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner --- /* v2 */ unchanged /* v3 */ - David Howells : - Remove mnt_idmapped() check after removing mnt_idmapped() helper in earlier patches. /* v4 */ - Serge Hallyn : - Use "mnt_userns" to refer to a vfsmount's userns everywhere to make terminology consistent. /* v5 */ unchanged base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837 - Christoph Hellwig : - Use new file_mnt_user_ns() helper. /* v6 */ base-commit: 19c329f6808995b142b3966301f217c831e7cf31 - Christoph Hellwig : - Make use of new path_permission() helper. --- fs/open.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/fs/open.c b/fs/open.c index 8b3f3eb652d0..4ec3979d0466 100644 --- a/fs/open.c +++ b/fs/open.c @@ -438,7 +438,7 @@ static long do_faccessat(int dfd, const char __user *filename, int mode, int fla goto out_path_release; } - res = inode_permission(&init_user_ns, inode, mode | MAY_ACCESS); + res = inode_permission(mnt_user_ns(path.mnt), inode, mode | MAY_ACCESS); /* SuS v2 requires we report a read only fs too */ if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) goto out_path_release; @@ -582,8 +582,8 @@ int chmod_common(const struct path *path, umode_t mode) goto out_unlock; newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; - error = notify_change(&init_user_ns, path->dentry, &newattrs, - &delegated_inode); + error = notify_change(mnt_user_ns(path->mnt), path->dentry, + &newattrs, &delegated_inode); out_unlock: inode_unlock(inode); if (delegated_inode) { @@ -644,6 +644,7 @@ SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) int chown_common(const struct path *path, uid_t user, gid_t group) { + struct user_namespace *mnt_userns; struct inode *inode = path->dentry->d_inode; struct inode *delegated_inode = NULL; int error; @@ -654,6 +655,10 @@ int chown_common(const struct path *path, uid_t user, gid_t group) uid = make_kuid(current_user_ns(), user); gid = make_kgid(current_user_ns(), group); + mnt_userns = mnt_user_ns(path->mnt); + uid = kuid_from_mnt(mnt_userns, uid); + gid = kgid_from_mnt(mnt_userns, gid); + retry_deleg: newattrs.ia_valid = ATTR_CTIME; if (user != (uid_t) -1) { @@ -674,7 +679,7 @@ int chown_common(const struct path *path, uid_t user, gid_t group) inode_lock(inode); error = security_path_chown(path, uid, gid); if (!error) - error = notify_change(&init_user_ns, path->dentry, &newattrs, + error = notify_change(mnt_userns, path->dentry, &newattrs, &delegated_inode); inode_unlock(inode); if (delegated_inode) { -- 2.30.0