lsm: consolidate buffer size handling into lsm_fill_user_ctx()

While we have a lsm_fill_user_ctx() helper function designed to make
life easier for LSMs which return lsm_ctx structs to userspace, we
didn't include all of the buffer length safety checks and buffer
padding adjustments in the helper.  This led to code duplication
across the different LSMs and the possibility for mistakes across the
different LSM subsystems.  In order to reduce code duplication and
decrease the chances of silly mistakes, we're consolidating all of
this code into the lsm_fill_user_ctx() helper.

The buffer padding is also modified from a fixed 8-byte alignment to
an alignment that matches the word length of the machine
(BITS_PER_LONG / 8).

Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Paul Moore 2023-10-24 14:44:00 -04:00
parent fdcf699b60
commit d7cf3412a9
5 changed files with 67 additions and 77 deletions

View File

@ -492,8 +492,8 @@ int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen);
int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen); int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen);
int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen); int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen);
int security_locked_down(enum lockdown_reason what); int security_locked_down(enum lockdown_reason what);
int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, size_t *uctx_len,
size_t context_size, u64 id, u64 flags); void *val, size_t val_len, u64 id, u64 flags);
#else /* CONFIG_SECURITY */ #else /* CONFIG_SECURITY */
static inline int call_blocking_lsm_notifier(enum lsm_event event, void *data) static inline int call_blocking_lsm_notifier(enum lsm_event event, void *data)
@ -1424,8 +1424,9 @@ static inline int security_locked_down(enum lockdown_reason what)
{ {
return 0; return 0;
} }
static inline int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, static inline int lsm_fill_user_ctx(struct lsm_ctx __user *uctx,
size_t context_size, u64 id, u64 flags) size_t *uctx_len, void *val, size_t val_len,
u64 id, u64 flags)
{ {
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }

View File

@ -782,7 +782,6 @@ static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
int error = -ENOENT; int error = -ENOENT;
struct aa_task_ctx *ctx = task_ctx(current); struct aa_task_ctx *ctx = task_ctx(current);
struct aa_label *label = NULL; struct aa_label *label = NULL;
size_t total_len = 0;
char *value; char *value;
switch (attr) { switch (attr) {
@ -804,22 +803,14 @@ static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
if (label) { if (label) {
error = aa_getprocattr(label, &value, false); error = aa_getprocattr(label, &value, false);
if (error > 0) { if (error > 0)
total_len = ALIGN(struct_size(lx, ctx, error), 8); error = lsm_fill_user_ctx(lx, size, value, error,
if (total_len > *size) LSM_ID_APPARMOR, 0);
error = -E2BIG;
else if (lx)
error = lsm_fill_user_ctx(lx, value, error,
LSM_ID_APPARMOR, 0);
else
error = 1;
}
kfree(value); kfree(value);
} }
aa_put_label(label); aa_put_label(label);
*size = total_len;
if (error < 0) if (error < 0)
return error; return error;
return 1; return 1;

View File

@ -772,42 +772,49 @@ static int lsm_superblock_alloc(struct super_block *sb)
/** /**
* lsm_fill_user_ctx - Fill a user space lsm_ctx structure * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
* @ctx: an LSM context to be filled * @uctx: a userspace LSM context to be filled
* @context: the new context value * @uctx_len: available uctx size (input), used uctx size (output)
* @context_size: the size of the new context value * @val: the new LSM context value
* @val_len: the size of the new LSM context value
* @id: LSM id * @id: LSM id
* @flags: LSM defined flags * @flags: LSM defined flags
* *
* Fill all of the fields in a user space lsm_ctx structure. * Fill all of the fields in a userspace lsm_ctx structure.
* Caller is assumed to have verified that @ctx has enough space
* for @context.
* *
* Returns 0 on success, -EFAULT on a copyout error, -ENOMEM * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
* if memory can't be allocated. * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
*/ */
int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, size_t *uctx_len,
size_t context_size, u64 id, u64 flags) void *val, size_t val_len,
u64 id, u64 flags)
{ {
struct lsm_ctx *lctx; struct lsm_ctx *nctx = NULL;
size_t locallen = struct_size(lctx, ctx, context_size); size_t nctx_len;
int rc = 0; int rc = 0;
lctx = kzalloc(locallen, GFP_KERNEL); nctx_len = ALIGN(struct_size(nctx, ctx, val_len), BITS_PER_LONG / 8);
if (lctx == NULL) if (nctx_len > *uctx_len) {
return -ENOMEM; rc = -E2BIG;
goto out;
}
lctx->id = id; nctx = kzalloc(nctx_len, GFP_KERNEL);
lctx->flags = flags; if (nctx == NULL) {
lctx->ctx_len = context_size; rc = -ENOMEM;
lctx->len = locallen; goto out;
}
nctx->id = id;
nctx->flags = flags;
nctx->len = nctx_len;
nctx->ctx_len = val_len;
memcpy(nctx->ctx, val, val_len);
memcpy(lctx->ctx, context, context_size); if (copy_to_user(uctx, nctx, nctx_len))
if (copy_to_user(ctx, lctx, locallen))
rc = -EFAULT; rc = -EFAULT;
kfree(lctx); out:
kfree(nctx);
*uctx_len = nctx_len;
return rc; return rc;
} }

View File

@ -6486,30 +6486,32 @@ abort_change:
return error; return error;
} }
/**
* selinux_getselfattr - Get SELinux current task attributes
* @attr: the requested attribute
* @ctx: buffer to receive the result
* @size: buffer size (input), buffer size used (output)
* @flags: unused
*
* Fill the passed user space @ctx with the details of the requested
* attribute.
*
* Returns the number of attributes on success, an error code otherwise.
* There will only ever be one attribute.
*/
static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
size_t *size, u32 flags) size_t *size, u32 flags)
{ {
char *value; int rc;
size_t total_len; char *val;
int len; int val_len;
int rc = 0;
len = selinux_lsm_getattr(attr, current, &value); val_len = selinux_lsm_getattr(attr, current, &val);
if (len < 0) if (val_len < 0)
return len; return val_len;
rc = lsm_fill_user_ctx(ctx, size, val, val_len, LSM_ID_SELINUX, 0);
total_len = ALIGN(struct_size(ctx, ctx, len), 8); kfree(val);
return (!rc ? 1 : rc);
if (total_len > *size)
rc = -E2BIG;
else if (ctx)
rc = lsm_fill_user_ctx(ctx, value, len, LSM_ID_SELINUX, 0);
kfree(value);
*size = total_len;
if (rc < 0)
return rc;
return 1;
} }
static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx, static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx,

View File

@ -3642,28 +3642,17 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
size_t *size, u32 flags) size_t *size, u32 flags)
{ {
struct smack_known *skp = smk_of_current();
int total;
int slen;
int rc; int rc;
struct smack_known *skp;
if (attr != LSM_ATTR_CURRENT) if (attr != LSM_ATTR_CURRENT)
return -EOPNOTSUPP; return -EOPNOTSUPP;
slen = strlen(skp->smk_known) + 1; skp = smk_of_current();
total = ALIGN(slen + sizeof(*ctx), 8); rc = lsm_fill_user_ctx(ctx, size,
if (total > *size) skp->smk_known, strlen(skp->smk_known) + 1,
rc = -E2BIG; LSM_ID_SMACK, 0);
else if (ctx) return (!rc ? 1 : rc);
rc = lsm_fill_user_ctx(ctx, skp->smk_known, slen, LSM_ID_SMACK,
0);
else
rc = 1;
*size = total;
if (rc >= 0)
return 1;
return rc;
} }
/** /**