From mboxrd@z Thu Jan 1 00:00:00 1970 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 E0555C433FE for ; Mon, 17 Oct 2022 15:09:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231225AbiJQPJ2 (ORCPT ); Mon, 17 Oct 2022 11:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231218AbiJQPJH (ORCPT ); Mon, 17 Oct 2022 11:09:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02BC56BCE3; Mon, 17 Oct 2022 08:09:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id DFE35B81900; Mon, 17 Oct 2022 15:08:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0EBE0C433C1; Mon, 17 Oct 2022 15:08:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666019309; bh=H0sVkbSycyaOfG0xPI3PgvpkEWuDz5eNIYY8UbPIwyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a7iH048/TtkRJl6V9+iffQDvPlnqOwbwqJwffn/DjMCKoRjOxpz4CPcUwdUGpQiJY Zh3TEnEPhfnR6If3He2qVokfu7wFBb6w4kGA54KQNQkFL8UPVnALwtYiGTKVZEYDJZ F+Bngn3wy3ZQWgBE4v+dT/1HKS6ec99TC2fXq6CtUzkgxSW5rJqDyqlCovwinWVKCW qe+I7VrB7zDR25bdbJ7ZrLFBGPfkAlwg3jWWW8VWbhft8nf/5B5EkWtuRcl7qOfrwy TJVk0JmPFsDDNGwHgreilxRie7wmjBwwCsBGv+KDwW5yhOqGE69tYLvF/9CyJHWVto uCcf1mWSeu0BA== From: Christian Brauner To: Amir Goldstein , Miklos Szeredi , "Darrick J . Wong" Cc: Christian Brauner , Al Viro , Christoph Hellwig , Seth Forshee , Yang Xu , Filipe Manana , linux-unionfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v4 2/6] fs: move should_remove_suid() Date: Mon, 17 Oct 2022 17:06:35 +0200 Message-Id: <20221017150640.112577-3-brauner@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221017150640.112577-1-brauner@kernel.org> References: <20221017150640.112577-1-brauner@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2799; i=brauner@kernel.org; h=from:subject; bh=H0sVkbSycyaOfG0xPI3PgvpkEWuDz5eNIYY8UbPIwyE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMST75ufM3jxJcmkXX9NxW91mi4JMG7u6X7O51Asm8WSGy+ia Oad3lLIwiHExyIopsji0m4TLLeep2GyUqQEzh5UJZAgDF6cATOT5EkaGB845fAGnnC5cqLZZGqijOe XV+fM3ZZifeSV1PpW/mRSqx/A/qq3EiaciuDz8g6Vg5IMUceeiKu01vSdeqzjeTtH5WckNAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Move the helper from inode.c to attr.c. This keeps the the core of the set{g,u}id stripping logic in one place when we add follow-up changes. It is the better place anyway, since should_remove_suid() returns ATTR_KILL_S{G,U}ID flags. Reviewed-by: Amir Goldstein Signed-off-by: Christian Brauner (Microsoft) --- Notes: /* v2 */ patch not present /* v3 */ patch not present /* v4 */ patch added Amir Goldstein : - Make the move of should_remove_suid() a separate patch. fs/attr.c | 29 +++++++++++++++++++++++++++++ fs/inode.c | 29 ----------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index b1162fca84a2..e508b3caae76 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -20,6 +20,35 @@ #include "internal.h" +/* + * The logic we want is + * + * if suid or (sgid and xgrp) + * remove privs + */ +int should_remove_suid(struct dentry *dentry) +{ + umode_t mode = d_inode(dentry)->i_mode; + int kill = 0; + + /* suid always must be killed */ + if (unlikely(mode & S_ISUID)) + kill = ATTR_KILL_SUID; + + /* + * sgid without any exec bits is just a mandatory locking mark; leave + * it alone. If some exec bits are set, it's a real sgid; kill it. + */ + if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) + kill |= ATTR_KILL_SGID; + + if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) + return kill; + + return 0; +} +EXPORT_SYMBOL(should_remove_suid); + /** * chown_ok - verify permissions to chown inode * @mnt_userns: user namespace of the mount @inode was found from diff --git a/fs/inode.c b/fs/inode.c index 55299b710c45..6df2b7c936c2 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -1948,35 +1948,6 @@ void touch_atime(const struct path *path) } EXPORT_SYMBOL(touch_atime); -/* - * The logic we want is - * - * if suid or (sgid and xgrp) - * remove privs - */ -int should_remove_suid(struct dentry *dentry) -{ - umode_t mode = d_inode(dentry)->i_mode; - int kill = 0; - - /* suid always must be killed */ - if (unlikely(mode & S_ISUID)) - kill = ATTR_KILL_SUID; - - /* - * sgid without any exec bits is just a mandatory locking mark; leave - * it alone. If some exec bits are set, it's a real sgid; kill it. - */ - if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) - kill |= ATTR_KILL_SGID; - - if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode))) - return kill; - - return 0; -} -EXPORT_SYMBOL(should_remove_suid); - /* * Return mask of changes for notify_change() that need to be done as a * response to write or truncate. Return 0 if nothing has to be changed. -- 2.34.1