Commit 6bcdfd2c authored by Roberto Sassu's avatar Roberto Sassu Committed by Paul Moore

security: Allow all LSMs to provide xattrs for inode_init_security hook

Currently, the LSM infrastructure supports only one LSM providing an xattr
and EVM calculating the HMAC on that xattr, plus other inode metadata.

Allow all LSMs to provide one or multiple xattrs, by extending the security
blob reservation mechanism. Introduce the new lbs_xattr_count field of the
lsm_blob_sizes structure, so that each LSM can specify how many xattrs it
needs, and the LSM infrastructure knows how many xattr slots it should
allocate.

Modify the inode_init_security hook definition, by passing the full
xattr array allocated in security_inode_init_security(), and the current
number of xattr slots in that array filled by LSMs. The first parameter
would allow EVM to access and calculate the HMAC on xattrs supplied by
other LSMs, the second to not leave gaps in the xattr array, when an LSM
requested but did not provide xattrs (e.g. if it is not initialized).

Introduce lsm_get_xattr_slot(), which LSMs can call as many times as the
number specified in the lbs_xattr_count field of the lsm_blob_sizes
structure. During each call, lsm_get_xattr_slot() increments the number of
filled xattrs, so that at the next invocation it returns the next xattr
slot to fill.

Cleanup security_inode_init_security(). Unify the !initxattrs and
initxattrs case by simply not allocating the new_xattrs array in the
former. Update the documentation to reflect the changes, and fix the
description of the xattr name, as it is not allocated anymore.

Adapt both SELinux and Smack to use the new definition of the
inode_init_security hook, and to call lsm_get_xattr_slot() to obtain and
fill the reserved slots in the xattr array.

Move the xattr->name assignment after the xattr->value one, so that it is
done only in case of successful memory allocation.

Finally, change the default return value of the inode_init_security hook
from zero to -EOPNOTSUPP, so that BPF LSM correctly follows the hook
conventions.
Reported-by: default avatarNicolas Bouchinet <nicolas.bouchinet@clip-os.org>
Link: https://lore.kernel.org/linux-integrity/Y1FTSIo+1x+4X0LS@archlinux/Signed-off-by: default avatarRoberto Sassu <roberto.sassu@huawei.com>
Acked-by: default avatarCasey Schaufler <casey@schaufler-ca.com>
[PM: minor comment and variable tweaks, approved by RS]
Signed-off-by: default avatarPaul Moore <paul@paul-moore.com>
parent ff72942c
...@@ -111,9 +111,9 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask, ...@@ -111,9 +111,9 @@ LSM_HOOK(int, 0, path_notify, const struct path *path, u64 mask,
unsigned int obj_type) unsigned int obj_type)
LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode) LSM_HOOK(int, 0, inode_alloc_security, struct inode *inode)
LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_free_security, struct inode *inode)
LSM_HOOK(int, 0, inode_init_security, struct inode *inode, LSM_HOOK(int, -EOPNOTSUPP, inode_init_security, struct inode *inode,
struct inode *dir, const struct qstr *qstr, const char **name, struct inode *dir, const struct qstr *qstr, struct xattr *xattrs,
void **value, size_t *len) int *xattr_count)
LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode, LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode,
const struct qstr *name, const struct inode *context_inode) const struct qstr *name, const struct inode *context_inode)
LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry, LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry,
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <linux/security.h> #include <linux/security.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/rculist.h> #include <linux/rculist.h>
#include <linux/xattr.h>
union security_list_options { union security_list_options {
#define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__); #define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__);
...@@ -63,8 +64,27 @@ struct lsm_blob_sizes { ...@@ -63,8 +64,27 @@ struct lsm_blob_sizes {
int lbs_ipc; int lbs_ipc;
int lbs_msg_msg; int lbs_msg_msg;
int lbs_task; int lbs_task;
int lbs_xattr_count; /* number of xattr slots in new_xattrs array */
}; };
/**
* lsm_get_xattr_slot - Return the next available slot and increment the index
* @xattrs: array storing LSM-provided xattrs
* @xattr_count: number of already stored xattrs (updated)
*
* Retrieve the first available slot in the @xattrs array to fill with an xattr,
* and increment @xattr_count.
*
* Return: The slot to fill in @xattrs if non-NULL, NULL otherwise.
*/
static inline struct xattr *lsm_get_xattr_slot(struct xattr *xattrs,
int *xattr_count)
{
if (unlikely(!xattrs))
return NULL;
return &xattrs[(*xattr_count)++];
}
/* /*
* LSM_RET_VOID is used as the default value in LSM_HOOK definitions for void * LSM_RET_VOID is used as the default value in LSM_HOOK definitions for void
* LSM hooks (in include/linux/lsm_hook_defs.h). * LSM hooks (in include/linux/lsm_hook_defs.h).
......
...@@ -31,8 +31,6 @@ ...@@ -31,8 +31,6 @@
#include <linux/msg.h> #include <linux/msg.h>
#include <net/flow.h> #include <net/flow.h>
#define MAX_LSM_EVM_XATTR 2
/* How many LSMs were built into the kernel? */ /* How many LSMs were built into the kernel? */
#define LSM_COUNT (__end_lsm_info - __start_lsm_info) #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
...@@ -212,6 +210,8 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed) ...@@ -212,6 +210,8 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock); lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
lsm_set_blob_size(&needed->lbs_xattr_count,
&blob_sizes.lbs_xattr_count);
} }
/* Prepare LSM for initialization. */ /* Prepare LSM for initialization. */
...@@ -378,6 +378,7 @@ static void __init ordered_lsm_init(void) ...@@ -378,6 +378,7 @@ static void __init ordered_lsm_init(void)
init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock); init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
init_debug("task blob size = %d\n", blob_sizes.lbs_task); init_debug("task blob size = %d\n", blob_sizes.lbs_task);
init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
/* /*
* Create any kmem_caches needed for blobs * Create any kmem_caches needed for blobs
...@@ -1591,11 +1592,17 @@ EXPORT_SYMBOL(security_dentry_create_files_as); ...@@ -1591,11 +1592,17 @@ EXPORT_SYMBOL(security_dentry_create_files_as);
* created inode and set up the incore security field for the new inode. This * created inode and set up the incore security field for the new inode. This
* hook is called by the fs code as part of the inode creation transaction and * hook is called by the fs code as part of the inode creation transaction and
* provides for atomic labeling of the inode, unlike the post_create/mkdir/... * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
* hooks called by the VFS. The hook function is expected to allocate the name * hooks called by the VFS.
* and value via kmalloc, with the caller being responsible for calling kfree *
* after using them. If the security module does not use security attributes * The hook function is expected to populate the xattrs array, by calling
* or does not wish to put a security attribute on this particular inode, then * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
* it should return -EOPNOTSUPP to skip this processing. * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
* slot, the hook function should set ->name to the attribute name suffix
* (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
* to the attribute value, to set ->value_len to the length of the value. If
* the security module does not use security attributes or does not wish to put
* a security attribute on this particular inode, then it should return
* -EOPNOTSUPP to skip this processing.
* *
* Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is * Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is
* needed, or -ENOMEM on memory allocation failure. * needed, or -ENOMEM on memory allocation failure.
...@@ -1604,33 +1611,51 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, ...@@ -1604,33 +1611,51 @@ int security_inode_init_security(struct inode *inode, struct inode *dir,
const struct qstr *qstr, const struct qstr *qstr,
const initxattrs initxattrs, void *fs_data) const initxattrs initxattrs, void *fs_data)
{ {
struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1]; struct security_hook_list *hp;
struct xattr *lsm_xattr, *evm_xattr, *xattr; struct xattr *new_xattrs = NULL;
int ret; int ret = -EOPNOTSUPP, xattr_count = 0;
if (unlikely(IS_PRIVATE(inode))) if (unlikely(IS_PRIVATE(inode)))
return 0; return 0;
if (!initxattrs) if (!blob_sizes.lbs_xattr_count)
return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, return 0;
dir, qstr, NULL, NULL, NULL);
memset(new_xattrs, 0, sizeof(new_xattrs)); if (initxattrs) {
lsm_xattr = new_xattrs; /* Allocate +1 for EVM and +1 as terminator. */
ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr, new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 2,
&lsm_xattr->name, sizeof(*new_xattrs), GFP_NOFS);
&lsm_xattr->value, if (!new_xattrs)
&lsm_xattr->value_len); return -ENOMEM;
if (ret) }
hlist_for_each_entry(hp, &security_hook_heads.inode_init_security,
list) {
ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs,
&xattr_count);
if (ret && ret != -EOPNOTSUPP)
goto out;
/*
* As documented in lsm_hooks.h, -EOPNOTSUPP in this context
* means that the LSM is not willing to provide an xattr, not
* that it wants to signal an error. Thus, continue to invoke
* the remaining LSMs.
*/
}
/* If initxattrs() is NULL, xattr_count is zero, skip the call. */
if (!xattr_count)
goto out; goto out;
evm_xattr = lsm_xattr + 1; ret = evm_inode_init_security(inode, new_xattrs,
ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr); &new_xattrs[xattr_count]);
if (ret) if (ret)
goto out; goto out;
ret = initxattrs(inode, new_xattrs, fs_data); ret = initxattrs(inode, new_xattrs, fs_data);
out: out:
for (xattr = new_xattrs; xattr->value != NULL; xattr++) for (; xattr_count > 0; xattr_count--)
kfree(xattr->value); kfree(new_xattrs[xattr_count - 1].value);
kfree(new_xattrs);
return (ret == -EOPNOTSUPP) ? 0 : ret; return (ret == -EOPNOTSUPP) ? 0 : ret;
} }
EXPORT_SYMBOL(security_inode_init_security); EXPORT_SYMBOL(security_inode_init_security);
......
...@@ -104,6 +104,8 @@ ...@@ -104,6 +104,8 @@
#include "audit.h" #include "audit.h"
#include "avc_ss.h" #include "avc_ss.h"
#define SELINUX_INODE_INIT_XATTRS 1
struct selinux_state selinux_state; struct selinux_state selinux_state;
/* SECMARK reference count */ /* SECMARK reference count */
...@@ -2847,11 +2849,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, ...@@ -2847,11 +2849,11 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
static int selinux_inode_init_security(struct inode *inode, struct inode *dir, static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
const struct qstr *qstr, const struct qstr *qstr,
const char **name, struct xattr *xattrs, int *xattr_count)
void **value, size_t *len)
{ {
const struct task_security_struct *tsec = selinux_cred(current_cred()); const struct task_security_struct *tsec = selinux_cred(current_cred());
struct superblock_security_struct *sbsec; struct superblock_security_struct *sbsec;
struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
u32 newsid, clen; u32 newsid, clen;
int rc; int rc;
char *context; char *context;
...@@ -2878,16 +2880,14 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, ...@@ -2878,16 +2880,14 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
!(sbsec->flags & SBLABEL_MNT)) !(sbsec->flags & SBLABEL_MNT))
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (name) if (xattr) {
*name = XATTR_SELINUX_SUFFIX;
if (value && len) {
rc = security_sid_to_context_force(newsid, rc = security_sid_to_context_force(newsid,
&context, &clen); &context, &clen);
if (rc) if (rc)
return rc; return rc;
*value = context; xattr->value = context;
*len = clen; xattr->value_len = clen;
xattr->name = XATTR_SELINUX_SUFFIX;
} }
return 0; return 0;
...@@ -6815,6 +6815,7 @@ struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = { ...@@ -6815,6 +6815,7 @@ struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
.lbs_ipc = sizeof(struct ipc_security_struct), .lbs_ipc = sizeof(struct ipc_security_struct),
.lbs_msg_msg = sizeof(struct msg_security_struct), .lbs_msg_msg = sizeof(struct msg_security_struct),
.lbs_superblock = sizeof(struct superblock_security_struct), .lbs_superblock = sizeof(struct superblock_security_struct),
.lbs_xattr_count = SELINUX_INODE_INIT_XATTRS,
}; };
#ifdef CONFIG_PERF_EVENTS #ifdef CONFIG_PERF_EVENTS
......
...@@ -52,6 +52,8 @@ ...@@ -52,6 +52,8 @@
#define SMK_RECEIVING 1 #define SMK_RECEIVING 1
#define SMK_SENDING 2 #define SMK_SENDING 2
#define SMACK_INODE_INIT_XATTRS 1
#ifdef SMACK_IPV6_PORT_LABELING #ifdef SMACK_IPV6_PORT_LABELING
static DEFINE_MUTEX(smack_ipv6_lock); static DEFINE_MUTEX(smack_ipv6_lock);
static LIST_HEAD(smk_ipv6_port_list); static LIST_HEAD(smk_ipv6_port_list);
...@@ -923,27 +925,24 @@ static int smack_inode_alloc_security(struct inode *inode) ...@@ -923,27 +925,24 @@ static int smack_inode_alloc_security(struct inode *inode)
* @inode: the newly created inode * @inode: the newly created inode
* @dir: containing directory object * @dir: containing directory object
* @qstr: unused * @qstr: unused
* @name: where to put the attribute name * @xattrs: where to put the attributes
* @value: where to put the attribute value * @xattr_count: current number of LSM-provided xattrs (updated)
* @len: where to put the length of the attribute
* *
* Returns 0 if it all works out, -ENOMEM if there's no memory * Returns 0 if it all works out, -ENOMEM if there's no memory
*/ */
static int smack_inode_init_security(struct inode *inode, struct inode *dir, static int smack_inode_init_security(struct inode *inode, struct inode *dir,
const struct qstr *qstr, const char **name, const struct qstr *qstr,
void **value, size_t *len) struct xattr *xattrs, int *xattr_count)
{ {
struct task_smack *tsp = smack_cred(current_cred()); struct task_smack *tsp = smack_cred(current_cred());
struct inode_smack *issp = smack_inode(inode); struct inode_smack *issp = smack_inode(inode);
struct smack_known *skp = smk_of_task(tsp); struct smack_known *skp = smk_of_task(tsp);
struct smack_known *isp = smk_of_inode(inode); struct smack_known *isp = smk_of_inode(inode);
struct smack_known *dsp = smk_of_inode(dir); struct smack_known *dsp = smk_of_inode(dir);
struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
int may; int may;
if (name) if (xattr) {
*name = XATTR_SMACK_SUFFIX;
if (value && len) {
/* /*
* If equal, transmuting already occurred in * If equal, transmuting already occurred in
* smack_dentry_create_files_as(). No need to check again. * smack_dentry_create_files_as(). No need to check again.
...@@ -975,11 +974,12 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir, ...@@ -975,11 +974,12 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir,
issp->smk_flags |= SMK_INODE_CHANGED; issp->smk_flags |= SMK_INODE_CHANGED;
} }
*value = kstrdup(isp->smk_known, GFP_NOFS); xattr->value = kstrdup(isp->smk_known, GFP_NOFS);
if (*value == NULL) if (!xattr->value)
return -ENOMEM; return -ENOMEM;
*len = strlen(isp->smk_known); xattr->value_len = strlen(isp->smk_known);
xattr->name = XATTR_SMACK_SUFFIX;
} }
return 0; return 0;
...@@ -4869,6 +4869,7 @@ struct lsm_blob_sizes smack_blob_sizes __ro_after_init = { ...@@ -4869,6 +4869,7 @@ struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
.lbs_ipc = sizeof(struct smack_known *), .lbs_ipc = sizeof(struct smack_known *),
.lbs_msg_msg = sizeof(struct smack_known *), .lbs_msg_msg = sizeof(struct smack_known *),
.lbs_superblock = sizeof(struct superblock_smack), .lbs_superblock = sizeof(struct superblock_smack),
.lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
}; };
static struct security_hook_list smack_hooks[] __ro_after_init = { static struct security_hook_list smack_hooks[] __ro_after_init = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment