From patchwork Thu Jul 11 11:18:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Kuohai X-Patchwork-Id: 13730409 X-Patchwork-Delegate: bpf@iogearbox.net Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40AA415CD61; Thu, 11 Jul 2024 11:14:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696442; cv=none; b=G88jRs4+rb9jeqcGiYBB9mo8hRzeAutvXE0HFDXfp1Wi/VBOTnycuT55eOQs+sAvXxagjRw+g3ajDBLAj8ffrTEZ+RuDfMPSLR5FxrT47plL5aO2ApoYb/p+iBKaUM8Z0dSaUo+A4w2xTrLXiRzcqvwNv718ELBhMDimyEI6/Og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696442; c=relaxed/simple; bh=Hg9ese+3RioISfNTgvRm6HlDHVGrAICdXCleHqiZp3o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dOJCi2z6Za/FQgW/GlftM/LQjonui5CnOdYy1muaDE7uNY0XkCUrERhDU8DQ+CkVq5LZizjZNHVgksvCGyZktMkD2k/8C34OuBoQnkXlWx//5UriXmyfQty2PtAQyY+J69jfFCqbJbXScJZUXWnM8cok0H6N9a1yJnimpeN9JbI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4WKXCq6g8rz4f3m6w; Thu, 11 Jul 2024 19:13:43 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id CA1321A0572; Thu, 11 Jul 2024 19:13:56 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP4 (Coremail) with SMTP id gCh0CgCHjPVxvo9mulQgBw--.25300S4; Thu, 11 Jul 2024 19:13:56 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, netdev@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-integrity@vger.kernel.org, apparmor@lists.ubuntu.com, selinux@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Matt Bobrowski , Brendan Jackman , Paul Moore , James Morris , "Serge E . Hallyn" , Khadija Kamran , Casey Schaufler , Ondrej Mosnacek , Kees Cook , John Johansen , Lukas Bulwahn , Roberto Sassu , Shung-Hsi Yu , Edward Cree , Alexander Viro , Christian Brauner , Trond Myklebust , Anna Schumaker , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Stephen Smalley Subject: [PATCH bpf-next v4 02/20] lsm: Refactor return value of LSM hook inode_need_killpriv Date: Thu, 11 Jul 2024 19:18:50 +0800 Message-Id: <20240711111908.3817636-3-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240711111908.3817636-1-xukuohai@huaweicloud.com> References: <20240711111908.3817636-1-xukuohai@huaweicloud.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: gCh0Cg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14v26r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7IU1sa9DUUUUU== X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-Patchwork-Delegate: bpf@iogearbox.net From: Xu Kuohai To be consistent with most LSM hooks, convert the return value of hook inode_need_killpriv to 0 or a negative error code. Before: - Both hook inode_need_killpriv and func security_inode_need_killpriv return > 0 if security_inode_killpriv is required, 0 if not, and < 0 to abort the operation. After: - Both hook inode_need_killpriv and func security_inode_need_killpriv return 0 on success and a negative error code on failure. On success, hook inode_need_killpriv sets output param @need to true if security_inode_killpriv is required, and false if not. When @need is true, func security_inode_need_killpriv sets ATTR_KILL_PRIV flag in @attr; when false, it clears the flag. On failure, @need and @attr remains unchanged. Signed-off-by: Xu Kuohai --- fs/attr.c | 5 ++--- fs/inode.c | 4 +--- include/linux/lsm_hook_defs.h | 2 +- include/linux/security.h | 20 ++++++++++++++++---- security/commoncap.c | 12 ++++++++---- security/security.c | 29 ++++++++++++++++++++++++----- 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index 960a310581eb..aaadc721c982 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -427,11 +427,10 @@ int notify_change(struct mnt_idmap *idmap, struct dentry *dentry, attr->ia_mtime = timestamp_truncate(attr->ia_mtime, inode); if (ia_valid & ATTR_KILL_PRIV) { - error = security_inode_need_killpriv(dentry); + error = security_inode_need_killpriv(dentry, &ia_valid); if (error < 0) return error; - if (error == 0) - ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV; + attr->ia_valid = ia_valid; } /* diff --git a/fs/inode.c b/fs/inode.c index 3a41f83a4ba5..cd335dc3a3bc 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2012,11 +2012,9 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap, return 0; mask = setattr_should_drop_suidgid(idmap, inode); - ret = security_inode_need_killpriv(dentry); + ret = security_inode_need_killpriv(dentry, &mask); if (ret < 0) return ret; - if (ret) - mask |= ATTR_KILL_PRIV; return mask; } diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index e6e6f8473955..964849de424b 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -165,7 +165,7 @@ LSM_HOOK(int, 0, inode_remove_acl, struct mnt_idmap *idmap, struct dentry *dentry, const char *acl_name) LSM_HOOK(void, LSM_RET_VOID, inode_post_remove_acl, struct mnt_idmap *idmap, struct dentry *dentry, const char *acl_name) -LSM_HOOK(int, 0, inode_need_killpriv, struct dentry *dentry) +LSM_HOOK(int, 0, inode_need_killpriv, struct dentry *dentry, bool *need) LSM_HOOK(int, 0, inode_killpriv, struct mnt_idmap *idmap, struct dentry *dentry) LSM_HOOK(int, -EOPNOTSUPP, inode_getsecurity, struct mnt_idmap *idmap, diff --git a/include/linux/security.h b/include/linux/security.h index 454f96307cb9..1614ef5b2dd2 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -161,7 +161,7 @@ int cap_inode_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); int cap_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry, const char *name); -int cap_inode_need_killpriv(struct dentry *dentry); +int cap_inode_need_killpriv(struct dentry *dentry, bool *need); int cap_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry); int cap_inode_getsecurity(struct mnt_idmap *idmap, struct inode *inode, const char *name, void **buffer, @@ -389,7 +389,7 @@ int security_inode_listxattr(struct dentry *dentry); int security_inode_removexattr(struct mnt_idmap *idmap, struct dentry *dentry, const char *name); void security_inode_post_removexattr(struct dentry *dentry, const char *name); -int security_inode_need_killpriv(struct dentry *dentry); +int security_inode_need_killpriv(struct dentry *dentry, int *attr); int security_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry); int security_inode_getsecurity(struct mnt_idmap *idmap, struct inode *inode, const char *name, @@ -971,9 +971,21 @@ static inline void security_inode_post_removexattr(struct dentry *dentry, const char *name) { } -static inline int security_inode_need_killpriv(struct dentry *dentry) +static inline int security_inode_need_killpriv(struct dentry *dentry, int *attr) { - return cap_inode_need_killpriv(dentry); + int rc; + bool need = false; + + rc = cap_inode_need_killpriv(dentry, &need); + if (rc < 0) + return rc; + + if (need) + *attr |= ATTR_KILL_PRIV; + else + *attr &= ~ATTR_KILL_PRIV; + + return 0; } static inline int security_inode_killpriv(struct mnt_idmap *idmap, diff --git a/security/commoncap.c b/security/commoncap.c index cefad323a0b1..17d6188d22cf 100644 --- a/security/commoncap.c +++ b/security/commoncap.c @@ -286,21 +286,25 @@ int cap_capset(struct cred *new, /** * cap_inode_need_killpriv - Determine if inode change affects privileges * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV + * @need: If inode_killpriv() is needed * * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV * affects the security markings on that inode, and if it is, should * inode_killpriv() be invoked or the change rejected. * - * Return: 1 if security.capability has a value, meaning inode_killpriv() - * is required, 0 otherwise, meaning inode_killpriv() is not required. + * Return: Always returns 0. If security.capability has a value, meaning + * inode_killpriv() is required, @need is set to true. */ -int cap_inode_need_killpriv(struct dentry *dentry) +int cap_inode_need_killpriv(struct dentry *dentry, bool *need) { struct inode *inode = d_backing_inode(dentry); int error; error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0); - return error > 0; + if (error > 0) + *need = true; + + return 0; } /** diff --git a/security/security.c b/security/security.c index 3475f0cab3da..a4abcd86eb36 100644 --- a/security/security.c +++ b/security/security.c @@ -2490,17 +2490,36 @@ void security_inode_post_removexattr(struct dentry *dentry, const char *name) /** * security_inode_need_killpriv() - Check if security_inode_killpriv() required * @dentry: associated dentry + * @attr: attribute flags * * Called when an inode has been changed to determine if * security_inode_killpriv() should be called. * - * Return: Return <0 on error to abort the inode change operation, return 0 if - * security_inode_killpriv() does not need to be called, return >0 if - * security_inode_killpriv() does need to be called. + * Return: Return 0 on success, negative error code on failure. + * On success, set ATTR_KILL_PRIV flag in @attr when @need is true, + * clears it when false. */ -int security_inode_need_killpriv(struct dentry *dentry) +int security_inode_need_killpriv(struct dentry *dentry, int *attr) { - return call_int_hook(inode_need_killpriv, dentry); + int rc; + bool need = false; + struct security_hook_list *hp; + + hlist_for_each_entry(hp, &security_hook_heads.inode_need_killpriv, + list) { + rc = hp->hook.inode_need_killpriv(dentry, &need); + if (rc < 0) + return rc; + if (need) + break; + } + + if (need) + *attr |= ATTR_KILL_PRIV; + else + *attr &= ~ATTR_KILL_PRIV; + + return 0; } /**