LSM: Create lsm_list_modules system call
[linux-block.git] / security / lsm_syscalls.c
CommitLineData
a04a1198
CS
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * System calls implementing the Linux Security Module API.
4 *
5 * Copyright (C) 2022 Casey Schaufler <casey@schaufler-ca.com>
6 * Copyright (C) 2022 Intel Corporation
7 */
8
9#include <asm/current.h>
10#include <linux/compiler_types.h>
11#include <linux/err.h>
12#include <linux/errno.h>
13#include <linux/security.h>
14#include <linux/stddef.h>
15#include <linux/syscalls.h>
16#include <linux/types.h>
17#include <linux/lsm_hooks.h>
18#include <uapi/linux/lsm.h>
19
20/**
21 * sys_lsm_set_self_attr - Set current task's security module attribute
22 * @attr: which attribute to set
23 * @ctx: the LSM contexts
24 * @size: size of @ctx
25 * @flags: reserved for future use
26 *
27 * Sets the calling task's LSM context. On success this function
28 * returns 0. If the attribute specified cannot be set a negative
29 * value indicating the reason for the error is returned.
30 */
31SYSCALL_DEFINE4(lsm_set_self_attr, unsigned int, attr, struct lsm_ctx __user *,
32 ctx, size_t, size, u32, flags)
33{
34 return security_setselfattr(attr, ctx, size, flags);
35}
36
37/**
38 * sys_lsm_get_self_attr - Return current task's security module attributes
39 * @attr: which attribute to return
40 * @ctx: the user-space destination for the information, or NULL
41 * @size: pointer to the size of space available to receive the data
42 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
43 * attributes associated with the LSM identified in the passed @ctx be
44 * reported.
45 *
46 * Returns the calling task's LSM contexts. On success this
47 * function returns the number of @ctx array elements. This value
48 * may be zero if there are no LSM contexts assigned. If @size is
49 * insufficient to contain the return data -E2BIG is returned and
50 * @size is set to the minimum required size. In all other cases
51 * a negative value indicating the error is returned.
52 */
53SYSCALL_DEFINE4(lsm_get_self_attr, unsigned int, attr, struct lsm_ctx __user *,
54 ctx, size_t __user *, size, u32, flags)
55{
56 return security_getselfattr(attr, ctx, size, flags);
57}
ad4aff9e
CS
58
59/**
60 * sys_lsm_list_modules - Return a list of the active security modules
61 * @ids: the LSM module ids
62 * @size: pointer to size of @ids, updated on return
63 * @flags: reserved for future use, must be zero
64 *
65 * Returns a list of the active LSM ids. On success this function
66 * returns the number of @ids array elements. This value may be zero
67 * if there are no LSMs active. If @size is insufficient to contain
68 * the return data -E2BIG is returned and @size is set to the minimum
69 * required size. In all other cases a negative value indicating the
70 * error is returned.
71 */
72SYSCALL_DEFINE3(lsm_list_modules, u64 __user *, ids, size_t __user *, size,
73 u32, flags)
74{
75 size_t total_size = lsm_active_cnt * sizeof(*ids);
76 size_t usize;
77 int i;
78
79 if (flags)
80 return -EINVAL;
81
82 if (get_user(usize, size))
83 return -EFAULT;
84
85 if (put_user(total_size, size) != 0)
86 return -EFAULT;
87
88 if (usize < total_size)
89 return -E2BIG;
90
91 for (i = 0; i < lsm_active_cnt; i++)
92 if (put_user(lsm_idlist[i]->id, ids++))
93 return -EFAULT;
94
95 return lsm_active_cnt;
96}