// SPDX-License-Identifier: GPL-2.0-only1/*2* System calls implementing the Linux Security Module API.3*4* Copyright (C) 2022 Casey Schaufler <[email protected]>5* Copyright (C) 2022 Intel Corporation6*/78#include <asm/current.h>9#include <linux/compiler_types.h>10#include <linux/err.h>11#include <linux/errno.h>12#include <linux/security.h>13#include <linux/stddef.h>14#include <linux/syscalls.h>15#include <linux/types.h>16#include <linux/lsm_hooks.h>17#include <uapi/linux/lsm.h>1819/**20* lsm_name_to_attr - map an LSM attribute name to its ID21* @name: name of the attribute22*23* Returns the LSM attribute value associated with @name, or 0 if24* there is no mapping.25*/26u64 lsm_name_to_attr(const char *name)27{28if (!strcmp(name, "current"))29return LSM_ATTR_CURRENT;30if (!strcmp(name, "exec"))31return LSM_ATTR_EXEC;32if (!strcmp(name, "fscreate"))33return LSM_ATTR_FSCREATE;34if (!strcmp(name, "keycreate"))35return LSM_ATTR_KEYCREATE;36if (!strcmp(name, "prev"))37return LSM_ATTR_PREV;38if (!strcmp(name, "sockcreate"))39return LSM_ATTR_SOCKCREATE;40return LSM_ATTR_UNDEF;41}4243/**44* sys_lsm_set_self_attr - Set current task's security module attribute45* @attr: which attribute to set46* @ctx: the LSM contexts47* @size: size of @ctx48* @flags: reserved for future use49*50* Sets the calling task's LSM context. On success this function51* returns 0. If the attribute specified cannot be set a negative52* value indicating the reason for the error is returned.53*/54SYSCALL_DEFINE4(lsm_set_self_attr, unsigned int, attr, struct lsm_ctx __user *,55ctx, u32, size, u32, flags)56{57return security_setselfattr(attr, ctx, size, flags);58}5960/**61* sys_lsm_get_self_attr - Return current task's security module attributes62* @attr: which attribute to return63* @ctx: the user-space destination for the information, or NULL64* @size: pointer to the size of space available to receive the data65* @flags: special handling options. LSM_FLAG_SINGLE indicates that only66* attributes associated with the LSM identified in the passed @ctx be67* reported.68*69* Returns the calling task's LSM contexts. On success this70* function returns the number of @ctx array elements. This value71* may be zero if there are no LSM contexts assigned. If @size is72* insufficient to contain the return data -E2BIG is returned and73* @size is set to the minimum required size. In all other cases74* a negative value indicating the error is returned.75*/76SYSCALL_DEFINE4(lsm_get_self_attr, unsigned int, attr, struct lsm_ctx __user *,77ctx, u32 __user *, size, u32, flags)78{79return security_getselfattr(attr, ctx, size, flags);80}8182/**83* sys_lsm_list_modules - Return a list of the active security modules84* @ids: the LSM module ids85* @size: pointer to size of @ids, updated on return86* @flags: reserved for future use, must be zero87*88* Returns a list of the active LSM ids. On success this function89* returns the number of @ids array elements. This value may be zero90* if there are no LSMs active. If @size is insufficient to contain91* the return data -E2BIG is returned and @size is set to the minimum92* required size. In all other cases a negative value indicating the93* error is returned.94*/95SYSCALL_DEFINE3(lsm_list_modules, u64 __user *, ids, u32 __user *, size,96u32, flags)97{98u32 total_size = lsm_active_cnt * sizeof(*ids);99u32 usize;100int i;101102if (flags)103return -EINVAL;104105if (get_user(usize, size))106return -EFAULT;107108if (put_user(total_size, size) != 0)109return -EFAULT;110111if (usize < total_size)112return -E2BIG;113114for (i = 0; i < lsm_active_cnt; i++)115if (put_user(lsm_idlist[i]->id, ids++))116return -EFAULT;117118return lsm_active_cnt;119}120121122