[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240711111908.3817636-3-xukuohai@huaweicloud.com>
Date: Thu, 11 Jul 2024 19:18:50 +0800
From: Xu Kuohai <xukuohai@...weicloud.com>
To: bpf@...r.kernel.org,
netdev@...r.kernel.org,
linux-security-module@...r.kernel.org,
linux-kselftest@...r.kernel.org,
linux-integrity@...r.kernel.org,
apparmor@...ts.ubuntu.com,
selinux@...r.kernel.org
Cc: Alexei Starovoitov <ast@...nel.org>,
Andrii Nakryiko <andrii@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>,
Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...gle.com>,
Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>,
Matt Bobrowski <mattbobrowski@...gle.com>,
Brendan Jackman <jackmanb@...omium.org>,
Paul Moore <paul@...l-moore.com>,
James Morris <jmorris@...ei.org>,
"Serge E . Hallyn" <serge@...lyn.com>,
Khadija Kamran <kamrankhadijadj@...il.com>,
Casey Schaufler <casey@...aufler-ca.com>,
Ondrej Mosnacek <omosnace@...hat.com>,
Kees Cook <keescook@...omium.org>,
John Johansen <john.johansen@...onical.com>,
Lukas Bulwahn <lukas.bulwahn@...il.com>,
Roberto Sassu <roberto.sassu@...wei.com>,
Shung-Hsi Yu <shung-hsi.yu@...e.com>,
Edward Cree <ecree.xilinx@...il.com>,
Alexander Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>,
Trond Myklebust <trond.myklebust@...merspace.com>,
Anna Schumaker <anna@...nel.org>,
Eric Dumazet <edumazet@...gle.com>,
Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>,
Stephen Smalley <stephen.smalley.work@...il.com>
Subject: [PATCH bpf-next v4 02/20] lsm: Refactor return value of LSM hook inode_need_killpriv
From: Xu Kuohai <xukuohai@...wei.com>
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 <xukuohai@...wei.com>
---
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;
}
/**
--
2.30.2
Powered by blists - more mailing lists