Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp26667pxb; Tue, 12 Jan 2021 18:56:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJx3B+tfdlvoS7W4Sbu7R9DTbHwxbEL4BblSZBDzRBMkTQYxygTUeHI1urmcu4Aywmm4mT/q X-Received: by 2002:a05:6402:490:: with SMTP id k16mr56908edv.71.1610506612492; Tue, 12 Jan 2021 18:56:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610506612; cv=none; d=google.com; s=arc-20160816; b=fOinLMPjlDS2CSdGEI8uJvi4eCM6R/1okQFMKu1fVZYZmJUODA16ZGsyyoHm7ShTBg VfydC0WReNMDpvzD1lv+Megl+LHRgf1tDFraHE+vbX5EE8OV95B+EmrdiS5Sr8N1HVzw 5XYQfWd2a0VmRpEh2Geg60fDsJ6q67BQy4Q+IkJeE9X5nCOMIjCWOm26I5NOLxXFjOzT N/xIoippRbsgKNCQtJs1loP9lrJ7nty53mQpaOCPUCIKNt6t7uvVOkbE/2drAG5UHvNz nXAuNzhn77gX6P2BCCj+6O3NGEPnHakv8EZ8qob0f5FvO8I4pdm26VD2KvOB3Bxf1sIK x++Q== 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=6lDOt8NGUnQDbdB8QCCwUj46Zo2cF3foDYw8sdDBS7o=; b=b5s5GUqjOlfwS6EdxBEB+BGjaioOJk87/uypRNLROtIBPIxYqLZ9C8pLcC87yF5kDn jOaxOF++LJCIAm5EuRG/DPt5v+br4xzv6Jcs9dzM6PpPosfAjrTqZNyMhCmZRzDJq5Pu w+RPH2khLn3dUr8O9Q9jROa+R2448eZpfOLW+Wa/QRbQXk9WZGBXdky3BAmeAfmZ++fl NOBVGmQ0C4PZzN95ZWS3ENxqN/AQ6mwZkY39XZTZuDsDFSKnhpZis9eCVZ1+MKzJuRMS 9Z0RiO1dccwRdbbfaDtrc2KkxmDFK8kNmksk2Z9bKlxxkaXo6OX5IYICkzuh1SYPxHSc GTyg== 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 i3si297305ejk.273.2021.01.12.18.56.29; Tue, 12 Jan 2021 18:56:52 -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 S2393555AbhALWEV (ORCPT + 99 others); Tue, 12 Jan 2021 17:04:21 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:42988 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392274AbhALWDq (ORCPT ); Tue, 12 Jan 2021 17:03:46 -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 1kzRkk-0003bd-Vx; Tue, 12 Jan 2021 22:03:03 +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 , Christoph Hellwig Subject: [PATCH v5 08/42] fs: add id translation helpers Date: Tue, 12 Jan 2021 23:00:50 +0100 Message-Id: <20210112220124.837960-9-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210112220124.837960-1-christian.brauner@ubuntu.com> References: <20210112220124.837960-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; i=DPHLY9J1WgTtTJIZfW+M0ys29DrUndhBq8NbMQ4LhTc=; m=f8Rp5sG3By3IoZf9oxFawhK5ciuhIUWlpZXQnI5KhyM=; p=FdjzGfRkpgFbW6faYox4tn7jKSccYWBah7wp94uJi2M=; g=d24687f3801a2d5b9a8c966b0220bebc4a38950b X-Patch-Sig: m=pgp; i=christian.brauner@ubuntu.com; s=0x0x91C61BC06578DCA2; b=iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCX/4YtQAKCRCRxhvAZXjcoms5AP9UD8K jKRQgl1LwNzDLENvBEn2KJkcp0Z2leCRTfRmUMgD/ZWnrxZqJq8G8S8UIcj5YGta0jcX3KaWk4MJ4 XSta0wo= Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Add simple helpers to make it easy to map kuids into and from idmapped mounts. We provide simple wrappers that filesystems can use to e.g. initialize inodes similar to i_{uid,gid}_read() and i_{uid,gid}_write(). Accessing an inode through an idmapped mount will require the inode to be mapped according to the mount's user namespace. If the fsids are used to compare against inodes or to initialize inodes they are required to be shifted from the mount's user namespace. Passing the initial user namespace to these helpers makes them a nop and so any non-idmapped paths will not be impacted. Cc: David Howells Cc: Al Viro Cc: linux-fsdevel@vger.kernel.org Reviewed-by: Christoph Hellwig Signed-off-by: Christian Brauner --- /* v2 */ - Christoph Hellwig : - Get rid of the ifdefs and the config option that hid idmapped mounts. /* v3 */ unchanged /* v4 */ - Serge Hallyn : - Use "mnt_userns" to refer to a vfsmount's userns everywhere to make terminology consistent. /* v5 */ base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837 --- include/linux/fs.h | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index fd47deea7c17..1da4c21fb588 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -1572,6 +1573,52 @@ static inline void i_gid_write(struct inode *inode, gid_t gid) inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid); } +static inline kuid_t kuid_into_mnt(struct user_namespace *mnt_userns, + kuid_t kuid) +{ + return make_kuid(mnt_userns, __kuid_val(kuid)); +} + +static inline kgid_t kgid_into_mnt(struct user_namespace *mnt_userns, + kgid_t kgid) +{ + return make_kgid(mnt_userns, __kgid_val(kgid)); +} + +static inline kuid_t i_uid_into_mnt(struct user_namespace *mnt_userns, + const struct inode *inode) +{ + return kuid_into_mnt(mnt_userns, inode->i_uid); +} + +static inline kgid_t i_gid_into_mnt(struct user_namespace *mnt_userns, + const struct inode *inode) +{ + return kgid_into_mnt(mnt_userns, inode->i_gid); +} + +static inline kuid_t kuid_from_mnt(struct user_namespace *mnt_userns, + kuid_t kuid) +{ + return KUIDT_INIT(from_kuid(mnt_userns, kuid)); +} + +static inline kgid_t kgid_from_mnt(struct user_namespace *mnt_userns, + kgid_t kgid) +{ + return KGIDT_INIT(from_kgid(mnt_userns, kgid)); +} + +static inline kuid_t fsuid_into_mnt(struct user_namespace *mnt_userns) +{ + return kuid_from_mnt(mnt_userns, current_fsuid()); +} + +static inline kgid_t fsgid_into_mnt(struct user_namespace *mnt_userns) +{ + return kgid_from_mnt(mnt_userns, current_fsgid()); +} + extern struct timespec64 current_time(struct inode *inode); /* -- 2.30.0