Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp254948pxx; Thu, 29 Oct 2020 01:36:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxus3ir+3VXouiqxDFINlJllpEQWETsvlpnr2MyF8xk2vQzwVL42wpqqHGXOVqBsiSEBOHR X-Received: by 2002:aa7:dcc2:: with SMTP id w2mr2803593edu.121.1603960582401; Thu, 29 Oct 2020 01:36:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603960582; cv=none; d=google.com; s=arc-20160816; b=xOfQTHEDcEa0odPYDUbonO/tvDLJ6Hok8CGuKJc/QlyNsbZHUOTihaDVYAvdoN+rzJ sfgMF6q6Rs831Gn0jgiB8qyLjYei68d7bkCSRgyrkrFqAs0928Ys+dfIGbbSJg6/OXUI tM6anR4Kg4Oa4tmPu21+imuAaNvx8LQvx5VVIsH5nFaOQlkfnDdmuXTfv7xPRbJcmSL0 BJGjYlzUQmZp0/0oERBRn2PFqPhBHVZNcoUDMhRMUGvfCtMUS1tsrYLdAY9WBhNa65Pb 1FNpNqEnahhnBgJ0hzy+l+E7k8rBCmldz/1aUJ2zbXFYFtaGv80FCLKG2WM3hYwijpBo Gtlg== 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=QhDAEUqXNZnsagMT6+sm2dUvIPTzXfqjokTZ+S1Bqp0=; b=eiJzFwLOLvWHHepaw0BjvGggJzh+h4tvSPj5MbJiEYPRm9Kcq4oEzJd9F1qNNrYAM/ 6+uXkxQaNQ1OB0i6h2kcpqE2arzExQwitZ548d8cL2bEiRnx2rL31n5pFgNY1Jec6UOE yhgA9vcb/+8suEUMaZuEuUYT687sTTPtXRvEP4dimRgq0tsfrmwxeGTOpeB8aZSrPDIF aKgRrvIuKPY3h/firpppuz7WCIRsVeAEE+N30kh43+FuvnIS96DbzwNqHcjlPrvolQMd o1CHy1I+inwTUyDohi3RblSEAB056NxTdvNVw8EqnegnRazjYCEIjeOt+8ONWU+CrcCO yGGA== 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 k15si1440122edr.208.2020.10.29.01.35.58; Thu, 29 Oct 2020 01:36:22 -0700 (PDT) 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 S1725787AbgJ2Axg (ORCPT + 99 others); Wed, 28 Oct 2020 20:53:36 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:60626 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727944AbgJ2Af2 (ORCPT ); Wed, 28 Oct 2020 20:35:28 -0400 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 1kXvuU-0008Ep-1v; Thu, 29 Oct 2020 00:35:22 +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 , Amir Goldstein , Miklos Szeredi , Theodore Tso , Alban Crequy , Tycho Andersen , David Howells , James Bottomley , Jann Horn , Seth Forshee , =?UTF-8?q?St=C3=A9phane=20Graber?= , Aleksa Sarai , Lennart Poettering , "Eric W. Biederman" , smbarber@chromium.org, Phil Estes , Serge Hallyn , Kees Cook , Todd Kjos , Jonathan Corbet , containers@lists.linux-foundation.org, linux-security-module@vger.kernel.org, linux-api@vger.kernel.org, linux-ext4@vger.kernel.org, linux-unionfs@vger.kernel.org, linux-audit@redhat.com, linux-integrity@vger.kernel.org, selinux@vger.kernel.org, Christian Brauner Subject: [PATCH 06/34] fs: add id translation helpers Date: Thu, 29 Oct 2020 01:32:24 +0100 Message-Id: <20201029003252.2128653-7-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.29.0 In-Reply-To: <20201029003252.2128653-1-christian.brauner@ubuntu.com> References: <20201029003252.2128653-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 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. Signed-off-by: Christian Brauner --- include/linux/fs.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 8314cd351673..8a891b80d0b4 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -1574,6 +1575,80 @@ 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 *to, kuid_t kuid) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return make_kuid(to, __kuid_val(kuid)); +#else + return kuid; +#endif +} + +static inline kgid_t kgid_into_mnt(struct user_namespace *to, kgid_t kgid) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return make_kgid(to, __kgid_val(kgid)); +#else + return kgid; +#endif +} + +static inline kuid_t i_uid_into_mnt(struct user_namespace *to, + const struct inode *inode) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return kuid_into_mnt(to, inode->i_uid); +#else + return inode->i_uid; +#endif +} + +static inline kgid_t i_gid_into_mnt(struct user_namespace *to, + const struct inode *inode) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return kgid_into_mnt(to, inode->i_gid); +#else + return inode->i_gid; +#endif +} + +static inline kuid_t kuid_from_mnt(struct user_namespace *to, kuid_t kuid) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return KUIDT_INIT(from_kuid(to, kuid)); +#else + return kuid; +#endif +} + +static inline kgid_t kgid_from_mnt(struct user_namespace *to, kgid_t kgid) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return KGIDT_INIT(from_kgid(to, kgid)); +#else + return kgid; +#endif +} + +static inline kuid_t fsuid_into_mnt(struct user_namespace *to) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return kuid_from_mnt(to, current_fsuid()); +#else + return current_fsuid(); +#endif +} + +static inline kgid_t fsgid_into_mnt(struct user_namespace *to) +{ +#ifdef CONFIG_IDMAP_MOUNTS + return kgid_from_mnt(to, current_fsgid()); +#else + return current_fsgid(); +#endif +} + extern struct timespec64 current_time(struct inode *inode); /* -- 2.29.0