From patchwork Tue Nov 23 11:42:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 12633983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79F9DC43219 for ; Tue, 23 Nov 2021 11:42:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236408AbhKWLqD (ORCPT ); Tue, 23 Nov 2021 06:46:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:37396 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236409AbhKWLqB (ORCPT ); Tue, 23 Nov 2021 06:46:01 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id BD45561027; Tue, 23 Nov 2021 11:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1637667773; bh=3HVKuOoJpQQVgeGCOA77s6WuBmYiiIXMZ2nXvsHh1VM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OHl26BhWYlBb5JX+OL8HYxgzM/t5wxwB5IRf7vjkgY5w7sM3fQBXrxJvjAXCkZ9u4 G/N4kVwoleu/ldUZdSaoV7gyD4kHwYFJNggnw70ckqG+Df9wUx1pUNajGvNVRVU4y8 1SUaNG0B7eu4ZRwYKrBFmtPHbMHGO7xksaBkLMrwHnWcxvCR+YBy0EYl8ZnpV+Enao hTZBK+CIq/Y+4NmnsWz7c8HbLTbk1MebgQyhoWVw8wb9d14B4IydCs5juo2ChJcSDR KWHz9buhGHw6Ne++2Utr00QUCtez5PFlIoALoulWijXdqF3v4M8HB9Lx/Tasyfzvn0 VVGeNZ9U1iBcA== From: Christian Brauner To: Christoph Hellwig Cc: Seth Forshee , Al Viro , linux-fsdevel@vger.kernel.org, Christian Brauner Subject: [PATCH 03/10] fs: tweak fsuidgid_has_mapping() Date: Tue, 23 Nov 2021 12:42:20 +0100 Message-Id: <20211123114227.3124056-4-brauner@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211123114227.3124056-1-brauner@kernel.org> References: <20211123114227.3124056-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1615; h=from:subject; bh=BiM/wSu7WnhoUMqf8ScJVSixomvTuvNa162ansl70Wk=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMSTOuVxys+22a/XZWXNP2z/y1Foz6/Rm6ymFjr/505y/3rpq velcckcpC4MYF4OsmCKLQ7tJuNxynorNRpkaMHNYmUCGMHBxCsBE8v8w/LNns633MJ+pcfSGbkXjmd QlV33t/oSsbOYNqAhdl7CsVYKR4Z7W3W93Y+4IeDT3rV5e8/jNR/aza5NEDvz/mObXeIblJxsA X-Developer-Key: i=christian.brauner@ubuntu.com; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org From: Christian Brauner If the caller's fs{g,u}id aren't mapped in the mount's idmapping we can return early and skip the check whether the mapped fs{g,u}id also have a mapping in the filesystem's idmapping. If the fs{g,u}id aren't mapped in the mount's idmapping they consequently can't be mapped in the filesystem's idmapping. So there's no point in checking that. Cc: Seth Forshee Cc: Christoph Hellwig Cc: Al Viro CC: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Amir Goldstein --- include/linux/fs.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/linux/fs.h b/include/linux/fs.h index eb69e8b035fa..161b5936094e 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1695,10 +1695,18 @@ static inline void inode_fsgid_set(struct inode *inode, static inline bool fsuidgid_has_mapping(struct super_block *sb, struct user_namespace *mnt_userns) { - struct user_namespace *s_user_ns = sb->s_user_ns; + struct user_namespace *fs_userns = sb->s_user_ns; + kuid_t kuid; + kgid_t kgid; - return kuid_has_mapping(s_user_ns, mapped_fsuid(mnt_userns)) && - kgid_has_mapping(s_user_ns, mapped_fsgid(mnt_userns)); + kuid = mapped_fsuid(mnt_userns); + if (!uid_valid(kuid)) + return false; + kgid = mapped_fsgid(mnt_userns); + if (!gid_valid(kgid)) + return false; + return kuid_has_mapping(fs_userns, kuid) && + kgid_has_mapping(fs_userns, kgid); } extern struct timespec64 current_time(struct inode *inode);