// 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#include "lsm.h"2021/**22* lsm_name_to_attr - map an LSM attribute name to its ID23* @name: name of the attribute24*25* Returns the LSM attribute value associated with @name, or 0 if26* there is no mapping.27*/28u64 lsm_name_to_attr(const char *name)29{30if (!strcmp(name, "current"))31return LSM_ATTR_CURRENT;32if (!strcmp(name, "exec"))33return LSM_ATTR_EXEC;34if (!strcmp(name, "fscreate"))35return LSM_ATTR_FSCREATE;36if (!strcmp(name, "keycreate"))37return LSM_ATTR_KEYCREATE;38if (!strcmp(name, "prev"))39return LSM_ATTR_PREV;40if (!strcmp(name, "sockcreate"))41return LSM_ATTR_SOCKCREATE;42return LSM_ATTR_UNDEF;43}4445/**46* sys_lsm_set_self_attr - Set current task's security module attribute47* @attr: which attribute to set48* @ctx: the LSM contexts49* @size: size of @ctx50* @flags: reserved for future use51*52* Sets the calling task's LSM context. On success this function53* returns 0. If the attribute specified cannot be set a negative54* value indicating the reason for the error is returned.55*/56SYSCALL_DEFINE4(lsm_set_self_attr, unsigned int, attr, struct lsm_ctx __user *,57ctx, u32, size, u32, flags)58{59return security_setselfattr(attr, ctx, size, flags);60}6162/**63* sys_lsm_get_self_attr - Return current task's security module attributes64* @attr: which attribute to return65* @ctx: the user-space destination for the information, or NULL66* @size: pointer to the size of space available to receive the data67* @flags: special handling options. LSM_FLAG_SINGLE indicates that only68* attributes associated with the LSM identified in the passed @ctx be69* reported.70*71* Returns the calling task's LSM contexts. On success this72* function returns the number of @ctx array elements. This value73* may be zero if there are no LSM contexts assigned. If @size is74* insufficient to contain the return data -E2BIG is returned and75* @size is set to the minimum required size. In all other cases76* a negative value indicating the error is returned.77*/78SYSCALL_DEFINE4(lsm_get_self_attr, unsigned int, attr, struct lsm_ctx __user *,79ctx, u32 __user *, size, u32, flags)80{81return security_getselfattr(attr, ctx, size, flags);82}8384/**85* sys_lsm_list_modules - Return a list of the active security modules86* @ids: the LSM module ids87* @size: pointer to size of @ids, updated on return88* @flags: reserved for future use, must be zero89*90* Returns a list of the active LSM ids. On success this function91* returns the number of @ids array elements. This value may be zero92* if there are no LSMs active. If @size is insufficient to contain93* the return data -E2BIG is returned and @size is set to the minimum94* required size. In all other cases a negative value indicating the95* error is returned.96*/97SYSCALL_DEFINE3(lsm_list_modules, u64 __user *, ids, u32 __user *, size,98u32, flags)99{100u32 total_size = lsm_active_cnt * sizeof(*ids);101u32 usize;102int i;103104if (flags)105return -EINVAL;106107if (get_user(usize, size))108return -EFAULT;109110if (put_user(total_size, size) != 0)111return -EFAULT;112113if (usize < total_size)114return -E2BIG;115116for (i = 0; i < lsm_active_cnt; i++)117if (put_user(lsm_idlist[i]->id, ids++))118return -EFAULT;119120return lsm_active_cnt;121}122123124