1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Security plug functions
5 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
6 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
8 * Copyright (C) 2016 Mellanox Technologies
9 * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com>
12 #define pr_fmt(fmt) "LSM: " fmt
14 #include <linux/bpf.h>
15 #include <linux/capability.h>
16 #include <linux/dcache.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/kernel_read_file.h>
21 #include <linux/lsm_hooks.h>
22 #include <linux/integrity.h>
23 #include <linux/ima.h>
24 #include <linux/evm.h>
25 #include <linux/fsnotify.h>
26 #include <linux/mman.h>
27 #include <linux/mount.h>
28 #include <linux/personality.h>
29 #include <linux/backing-dev.h>
30 #include <linux/string.h>
31 #include <linux/msg.h>
32 #include <linux/overflow.h>
35 /* How many LSMs were built into the kernel? */
36 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
39 * How many LSMs are built into the kernel as determined at
40 * build time. Used to determine fixed array sizes.
41 * The capability module is accounted for by CONFIG_SECURITY
43 #define LSM_CONFIG_COUNT ( \
44 (IS_ENABLED(CONFIG_SECURITY) ? 1 : 0) + \
45 (IS_ENABLED(CONFIG_SECURITY_SELINUX) ? 1 : 0) + \
46 (IS_ENABLED(CONFIG_SECURITY_SMACK) ? 1 : 0) + \
47 (IS_ENABLED(CONFIG_SECURITY_TOMOYO) ? 1 : 0) + \
48 (IS_ENABLED(CONFIG_SECURITY_APPARMOR) ? 1 : 0) + \
49 (IS_ENABLED(CONFIG_SECURITY_YAMA) ? 1 : 0) + \
50 (IS_ENABLED(CONFIG_SECURITY_LOADPIN) ? 1 : 0) + \
51 (IS_ENABLED(CONFIG_SECURITY_SAFESETID) ? 1 : 0) + \
52 (IS_ENABLED(CONFIG_SECURITY_LOCKDOWN_LSM) ? 1 : 0) + \
53 (IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \
54 (IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0))
57 * These are descriptions of the reasons that can be passed to the
58 * security_locked_down() LSM hook. Placing this array here allows
59 * all security modules to use the same descriptions for auditing
62 const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
63 [LOCKDOWN_NONE] = "none",
64 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
65 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
66 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
67 [LOCKDOWN_KEXEC] = "kexec of unsigned images",
68 [LOCKDOWN_HIBERNATION] = "hibernation",
69 [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
70 [LOCKDOWN_IOPORT] = "raw io port access",
71 [LOCKDOWN_MSR] = "raw MSR access",
72 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
73 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
74 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
75 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
76 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
77 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
78 [LOCKDOWN_DEBUGFS] = "debugfs access",
79 [LOCKDOWN_XMON_WR] = "xmon write access",
80 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
81 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
82 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
83 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
84 [LOCKDOWN_KCORE] = "/proc/kcore access",
85 [LOCKDOWN_KPROBES] = "use of kprobes",
86 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
87 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
88 [LOCKDOWN_PERF] = "unsafe use of perf",
89 [LOCKDOWN_TRACEFS] = "use of tracefs",
90 [LOCKDOWN_XMON_RW] = "xmon read and write access",
91 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
92 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
95 struct security_hook_heads security_hook_heads __ro_after_init;
96 static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
98 static struct kmem_cache *lsm_file_cache;
99 static struct kmem_cache *lsm_inode_cache;
102 static struct lsm_blob_sizes blob_sizes __ro_after_init;
104 /* Boot-time LSM user choice */
105 static __initdata const char *chosen_lsm_order;
106 static __initdata const char *chosen_major_lsm;
108 static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
110 /* Ordered list of LSMs to initialize. */
111 static __initdata struct lsm_info **ordered_lsms;
112 static __initdata struct lsm_info *exclusive;
114 static __initdata bool debug;
115 #define init_debug(...) \
118 pr_info(__VA_ARGS__); \
121 static bool __init is_enabled(struct lsm_info *lsm)
126 return *lsm->enabled;
129 /* Mark an LSM's enabled flag. */
130 static int lsm_enabled_true __initdata = 1;
131 static int lsm_enabled_false __initdata = 0;
132 static void __init set_enabled(struct lsm_info *lsm, bool enabled)
135 * When an LSM hasn't configured an enable variable, we can use
136 * a hard-coded location for storing the default enabled state.
140 lsm->enabled = &lsm_enabled_true;
142 lsm->enabled = &lsm_enabled_false;
143 } else if (lsm->enabled == &lsm_enabled_true) {
145 lsm->enabled = &lsm_enabled_false;
146 } else if (lsm->enabled == &lsm_enabled_false) {
148 lsm->enabled = &lsm_enabled_true;
150 *lsm->enabled = enabled;
154 /* Is an LSM already listed in the ordered LSMs list? */
155 static bool __init exists_ordered_lsm(struct lsm_info *lsm)
157 struct lsm_info **check;
159 for (check = ordered_lsms; *check; check++)
166 /* Append an LSM to the list of ordered LSMs to initialize. */
167 static int last_lsm __initdata;
168 static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
170 /* Ignore duplicate selections. */
171 if (exists_ordered_lsm(lsm))
174 if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
177 /* Enable this LSM, if it is not already set. */
179 lsm->enabled = &lsm_enabled_true;
180 ordered_lsms[last_lsm++] = lsm;
182 init_debug("%s ordered: %s (%s)\n", from, lsm->name,
183 is_enabled(lsm) ? "enabled" : "disabled");
186 /* Is an LSM allowed to be initialized? */
187 static bool __init lsm_allowed(struct lsm_info *lsm)
189 /* Skip if the LSM is disabled. */
190 if (!is_enabled(lsm))
193 /* Not allowed if another exclusive LSM already initialized. */
194 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
195 init_debug("exclusive disabled: %s\n", lsm->name);
202 static void __init lsm_set_blob_size(int *need, int *lbs)
209 offset = ALIGN(*lbs, sizeof(void *));
210 *lbs = offset + *need;
214 static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
219 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
220 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
222 * The inode blob gets an rcu_head in addition to
223 * what the modules might need.
225 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
226 blob_sizes.lbs_inode = sizeof(struct rcu_head);
227 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
228 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
229 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
230 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
231 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
232 lsm_set_blob_size(&needed->lbs_xattr_count,
233 &blob_sizes.lbs_xattr_count);
236 /* Prepare LSM for initialization. */
237 static void __init prepare_lsm(struct lsm_info *lsm)
239 int enabled = lsm_allowed(lsm);
241 /* Record enablement (to handle any following exclusive LSMs). */
242 set_enabled(lsm, enabled);
244 /* If enabled, do pre-initialization work. */
246 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
248 init_debug("exclusive chosen: %s\n", lsm->name);
251 lsm_set_blob_sizes(lsm->blobs);
255 /* Initialize a given LSM, if it is enabled. */
256 static void __init initialize_lsm(struct lsm_info *lsm)
258 if (is_enabled(lsm)) {
261 init_debug("initializing %s\n", lsm->name);
263 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
268 * Current index to use while initializing the lsm id list.
270 u32 lsm_active_cnt __ro_after_init;
271 const struct lsm_id *lsm_idlist[LSM_CONFIG_COUNT];
273 /* Populate ordered LSMs list from comma-separated LSM name list. */
274 static void __init ordered_lsm_parse(const char *order, const char *origin)
276 struct lsm_info *lsm;
277 char *sep, *name, *next;
279 /* LSM_ORDER_FIRST is always first. */
280 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
281 if (lsm->order == LSM_ORDER_FIRST)
282 append_ordered_lsm(lsm, " first");
285 /* Process "security=", if given. */
286 if (chosen_major_lsm) {
287 struct lsm_info *major;
290 * To match the original "security=" behavior, this
291 * explicitly does NOT fallback to another Legacy Major
292 * if the selected one was separately disabled: disable
293 * all non-matching Legacy Major LSMs.
295 for (major = __start_lsm_info; major < __end_lsm_info;
297 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
298 strcmp(major->name, chosen_major_lsm) != 0) {
299 set_enabled(major, false);
300 init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
301 chosen_major_lsm, major->name);
306 sep = kstrdup(order, GFP_KERNEL);
308 /* Walk the list, looking for matching LSMs. */
309 while ((name = strsep(&next, ",")) != NULL) {
312 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
313 if (strcmp(lsm->name, name) == 0) {
314 if (lsm->order == LSM_ORDER_MUTABLE)
315 append_ordered_lsm(lsm, origin);
321 init_debug("%s ignored: %s (not built into kernel)\n",
325 /* Process "security=", if given. */
326 if (chosen_major_lsm) {
327 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
328 if (exists_ordered_lsm(lsm))
330 if (strcmp(lsm->name, chosen_major_lsm) == 0)
331 append_ordered_lsm(lsm, "security=");
335 /* LSM_ORDER_LAST is always last. */
336 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
337 if (lsm->order == LSM_ORDER_LAST)
338 append_ordered_lsm(lsm, " last");
341 /* Disable all LSMs not in the ordered list. */
342 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
343 if (exists_ordered_lsm(lsm))
345 set_enabled(lsm, false);
346 init_debug("%s skipped: %s (not in requested order)\n",
353 static void __init lsm_early_cred(struct cred *cred);
354 static void __init lsm_early_task(struct task_struct *task);
356 static int lsm_append(const char *new, char **result);
358 static void __init report_lsm_order(void)
360 struct lsm_info **lsm, *early;
363 pr_info("initializing lsm=");
365 /* Report each enabled LSM name, comma separated. */
366 for (early = __start_early_lsm_info;
367 early < __end_early_lsm_info; early++)
368 if (is_enabled(early))
369 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
370 for (lsm = ordered_lsms; *lsm; lsm++)
371 if (is_enabled(*lsm))
372 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
377 static void __init ordered_lsm_init(void)
379 struct lsm_info **lsm;
381 ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
384 if (chosen_lsm_order) {
385 if (chosen_major_lsm) {
386 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
387 chosen_major_lsm, chosen_lsm_order);
388 chosen_major_lsm = NULL;
390 ordered_lsm_parse(chosen_lsm_order, "cmdline");
392 ordered_lsm_parse(builtin_lsm_order, "builtin");
394 for (lsm = ordered_lsms; *lsm; lsm++)
399 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
400 init_debug("file blob size = %d\n", blob_sizes.lbs_file);
401 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
402 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
403 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
404 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
405 init_debug("task blob size = %d\n", blob_sizes.lbs_task);
406 init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
409 * Create any kmem_caches needed for blobs
411 if (blob_sizes.lbs_file)
412 lsm_file_cache = kmem_cache_create("lsm_file_cache",
413 blob_sizes.lbs_file, 0,
415 if (blob_sizes.lbs_inode)
416 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
417 blob_sizes.lbs_inode, 0,
420 lsm_early_cred((struct cred *) current->cred);
421 lsm_early_task(current);
422 for (lsm = ordered_lsms; *lsm; lsm++)
423 initialize_lsm(*lsm);
428 int __init early_security_init(void)
430 struct lsm_info *lsm;
432 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
433 INIT_HLIST_HEAD(&security_hook_heads.NAME);
434 #include "linux/lsm_hook_defs.h"
437 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
439 lsm->enabled = &lsm_enabled_true;
448 * security_init - initializes the security framework
450 * This should be called early in the kernel initialization sequence.
452 int __init security_init(void)
454 struct lsm_info *lsm;
456 init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
457 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
458 init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
461 * Append the names of the early LSM modules now that kmalloc() is
464 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
465 init_debug(" early started: %s (%s)\n", lsm->name,
466 is_enabled(lsm) ? "enabled" : "disabled");
468 lsm_append(lsm->name, &lsm_names);
471 /* Load LSMs in specified order. */
477 /* Save user chosen LSM */
478 static int __init choose_major_lsm(char *str)
480 chosen_major_lsm = str;
483 __setup("security=", choose_major_lsm);
485 /* Explicitly choose LSM initialization order. */
486 static int __init choose_lsm_order(char *str)
488 chosen_lsm_order = str;
491 __setup("lsm=", choose_lsm_order);
493 /* Enable LSM order debugging. */
494 static int __init enable_debug(char *str)
499 __setup("lsm.debug", enable_debug);
501 static bool match_last_lsm(const char *list, const char *lsm)
505 if (WARN_ON(!list || !lsm))
507 last = strrchr(list, ',');
509 /* Pass the comma, strcmp() will check for '\0' */
513 return !strcmp(last, lsm);
516 static int lsm_append(const char *new, char **result)
520 if (*result == NULL) {
521 *result = kstrdup(new, GFP_KERNEL);
525 /* Check if it is the last registered name */
526 if (match_last_lsm(*result, new))
528 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
538 * security_add_hooks - Add a modules hooks to the hook lists.
539 * @hooks: the hooks to add
540 * @count: the number of hooks to add
541 * @lsmid: the identification information for the security module
543 * Each LSM has to register its hooks with the infrastructure.
545 void __init security_add_hooks(struct security_hook_list *hooks, int count,
546 const struct lsm_id *lsmid)
551 * A security module may call security_add_hooks() more
552 * than once during initialization, and LSM initialization
553 * is serialized. Landlock is one such case.
554 * Look at the previous entry, if there is one, for duplication.
556 if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
557 if (lsm_active_cnt >= LSM_CONFIG_COUNT)
558 panic("%s Too many LSMs registered.\n", __func__);
559 lsm_idlist[lsm_active_cnt++] = lsmid;
562 for (i = 0; i < count; i++) {
563 hooks[i].lsmid = lsmid;
564 hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
568 * Don't try to append during early_security_init(), we'll come back
569 * and fix this up afterwards.
571 if (slab_is_available()) {
572 if (lsm_append(lsmid->name, &lsm_names) < 0)
573 panic("%s - Cannot get early memory.\n", __func__);
577 int call_blocking_lsm_notifier(enum lsm_event event, void *data)
579 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
582 EXPORT_SYMBOL(call_blocking_lsm_notifier);
584 int register_blocking_lsm_notifier(struct notifier_block *nb)
586 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
589 EXPORT_SYMBOL(register_blocking_lsm_notifier);
591 int unregister_blocking_lsm_notifier(struct notifier_block *nb)
593 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
596 EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
599 * lsm_cred_alloc - allocate a composite cred blob
600 * @cred: the cred that needs a blob
601 * @gfp: allocation type
603 * Allocate the cred blob for all the modules
605 * Returns 0, or -ENOMEM if memory can't be allocated.
607 static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
609 if (blob_sizes.lbs_cred == 0) {
610 cred->security = NULL;
614 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
615 if (cred->security == NULL)
621 * lsm_early_cred - during initialization allocate a composite cred blob
622 * @cred: the cred that needs a blob
624 * Allocate the cred blob for all the modules
626 static void __init lsm_early_cred(struct cred *cred)
628 int rc = lsm_cred_alloc(cred, GFP_KERNEL);
631 panic("%s: Early cred alloc failed.\n", __func__);
635 * lsm_file_alloc - allocate a composite file blob
636 * @file: the file that needs a blob
638 * Allocate the file blob for all the modules
640 * Returns 0, or -ENOMEM if memory can't be allocated.
642 static int lsm_file_alloc(struct file *file)
644 if (!lsm_file_cache) {
645 file->f_security = NULL;
649 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
650 if (file->f_security == NULL)
656 * lsm_inode_alloc - allocate a composite inode blob
657 * @inode: the inode that needs a blob
659 * Allocate the inode blob for all the modules
661 * Returns 0, or -ENOMEM if memory can't be allocated.
663 int lsm_inode_alloc(struct inode *inode)
665 if (!lsm_inode_cache) {
666 inode->i_security = NULL;
670 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
671 if (inode->i_security == NULL)
677 * lsm_task_alloc - allocate a composite task blob
678 * @task: the task that needs a blob
680 * Allocate the task blob for all the modules
682 * Returns 0, or -ENOMEM if memory can't be allocated.
684 static int lsm_task_alloc(struct task_struct *task)
686 if (blob_sizes.lbs_task == 0) {
687 task->security = NULL;
691 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
692 if (task->security == NULL)
698 * lsm_ipc_alloc - allocate a composite ipc blob
699 * @kip: the ipc that needs a blob
701 * Allocate the ipc blob for all the modules
703 * Returns 0, or -ENOMEM if memory can't be allocated.
705 static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
707 if (blob_sizes.lbs_ipc == 0) {
708 kip->security = NULL;
712 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
713 if (kip->security == NULL)
719 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
720 * @mp: the msg_msg that needs a blob
722 * Allocate the ipc blob for all the modules
724 * Returns 0, or -ENOMEM if memory can't be allocated.
726 static int lsm_msg_msg_alloc(struct msg_msg *mp)
728 if (blob_sizes.lbs_msg_msg == 0) {
733 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
734 if (mp->security == NULL)
740 * lsm_early_task - during initialization allocate a composite task blob
741 * @task: the task that needs a blob
743 * Allocate the task blob for all the modules
745 static void __init lsm_early_task(struct task_struct *task)
747 int rc = lsm_task_alloc(task);
750 panic("%s: Early task alloc failed.\n", __func__);
754 * lsm_superblock_alloc - allocate a composite superblock blob
755 * @sb: the superblock that needs a blob
757 * Allocate the superblock blob for all the modules
759 * Returns 0, or -ENOMEM if memory can't be allocated.
761 static int lsm_superblock_alloc(struct super_block *sb)
763 if (blob_sizes.lbs_superblock == 0) {
764 sb->s_security = NULL;
768 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
769 if (sb->s_security == NULL)
775 * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
776 * @uctx: a userspace LSM context to be filled
777 * @uctx_len: available uctx size (input), used uctx size (output)
778 * @val: the new LSM context value
779 * @val_len: the size of the new LSM context value
781 * @flags: LSM defined flags
783 * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL
784 * simply calculate the required size to output via @utc_len and return
787 * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
788 * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
790 int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
791 void *val, size_t val_len,
794 struct lsm_ctx *nctx = NULL;
798 nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
799 if (nctx_len > *uctx_len) {
804 /* no buffer - return success/0 and set @uctx_len to the req size */
808 nctx = kzalloc(nctx_len, GFP_KERNEL);
815 nctx->len = nctx_len;
816 nctx->ctx_len = val_len;
817 memcpy(nctx->ctx, val, val_len);
819 if (copy_to_user(uctx, nctx, nctx_len))
824 *uctx_len = nctx_len;
829 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
830 * can be accessed with:
832 * LSM_RET_DEFAULT(<hook_name>)
834 * The macros below define static constants for the default value of each
837 #define LSM_RET_DEFAULT(NAME) (NAME##_default)
838 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
839 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
840 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
841 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
842 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
844 #include <linux/lsm_hook_defs.h>
848 * Hook list operation macros.
851 * This is a hook that does not return a value.
854 * This is a hook that returns a value.
857 #define call_void_hook(FUNC, ...) \
859 struct security_hook_list *P; \
861 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
862 P->hook.FUNC(__VA_ARGS__); \
865 #define call_int_hook(FUNC, IRC, ...) ({ \
868 struct security_hook_list *P; \
870 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
871 RC = P->hook.FUNC(__VA_ARGS__); \
879 /* Security operations */
882 * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
883 * @mgr: task credentials of current binder process
885 * Check whether @mgr is allowed to be the binder context manager.
887 * Return: Return 0 if permission is granted.
889 int security_binder_set_context_mgr(const struct cred *mgr)
891 return call_int_hook(binder_set_context_mgr, 0, mgr);
895 * security_binder_transaction() - Check if a binder transaction is allowed
896 * @from: sending process
897 * @to: receiving process
899 * Check whether @from is allowed to invoke a binder transaction call to @to.
901 * Return: Returns 0 if permission is granted.
903 int security_binder_transaction(const struct cred *from,
904 const struct cred *to)
906 return call_int_hook(binder_transaction, 0, from, to);
910 * security_binder_transfer_binder() - Check if a binder transfer is allowed
911 * @from: sending process
912 * @to: receiving process
914 * Check whether @from is allowed to transfer a binder reference to @to.
916 * Return: Returns 0 if permission is granted.
918 int security_binder_transfer_binder(const struct cred *from,
919 const struct cred *to)
921 return call_int_hook(binder_transfer_binder, 0, from, to);
925 * security_binder_transfer_file() - Check if a binder file xfer is allowed
926 * @from: sending process
927 * @to: receiving process
928 * @file: file being transferred
930 * Check whether @from is allowed to transfer @file to @to.
932 * Return: Returns 0 if permission is granted.
934 int security_binder_transfer_file(const struct cred *from,
935 const struct cred *to, const struct file *file)
937 return call_int_hook(binder_transfer_file, 0, from, to, file);
941 * security_ptrace_access_check() - Check if tracing is allowed
942 * @child: target process
943 * @mode: PTRACE_MODE flags
945 * Check permission before allowing the current process to trace the @child
946 * process. Security modules may also want to perform a process tracing check
947 * during an execve in the set_security or apply_creds hooks of tracing check
948 * during an execve in the bprm_set_creds hook of binprm_security_ops if the
949 * process is being traced and its security attributes would be changed by the
952 * Return: Returns 0 if permission is granted.
954 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
956 return call_int_hook(ptrace_access_check, 0, child, mode);
960 * security_ptrace_traceme() - Check if tracing is allowed
961 * @parent: tracing process
963 * Check that the @parent process has sufficient permission to trace the
964 * current process before allowing the current process to present itself to the
965 * @parent process for tracing.
967 * Return: Returns 0 if permission is granted.
969 int security_ptrace_traceme(struct task_struct *parent)
971 return call_int_hook(ptrace_traceme, 0, parent);
975 * security_capget() - Get the capability sets for a process
976 * @target: target process
977 * @effective: effective capability set
978 * @inheritable: inheritable capability set
979 * @permitted: permitted capability set
981 * Get the @effective, @inheritable, and @permitted capability sets for the
982 * @target process. The hook may also perform permission checking to determine
983 * if the current process is allowed to see the capability sets of the @target
986 * Return: Returns 0 if the capability sets were successfully obtained.
988 int security_capget(const struct task_struct *target,
989 kernel_cap_t *effective,
990 kernel_cap_t *inheritable,
991 kernel_cap_t *permitted)
993 return call_int_hook(capget, 0, target,
994 effective, inheritable, permitted);
998 * security_capset() - Set the capability sets for a process
999 * @new: new credentials for the target process
1000 * @old: current credentials of the target process
1001 * @effective: effective capability set
1002 * @inheritable: inheritable capability set
1003 * @permitted: permitted capability set
1005 * Set the @effective, @inheritable, and @permitted capability sets for the
1008 * Return: Returns 0 and update @new if permission is granted.
1010 int security_capset(struct cred *new, const struct cred *old,
1011 const kernel_cap_t *effective,
1012 const kernel_cap_t *inheritable,
1013 const kernel_cap_t *permitted)
1015 return call_int_hook(capset, 0, new, old,
1016 effective, inheritable, permitted);
1020 * security_capable() - Check if a process has the necessary capability
1021 * @cred: credentials to examine
1022 * @ns: user namespace
1023 * @cap: capability requested
1024 * @opts: capability check options
1026 * Check whether the @tsk process has the @cap capability in the indicated
1027 * credentials. @cap contains the capability <include/linux/capability.h>.
1028 * @opts contains options for the capable check <include/linux/security.h>.
1030 * Return: Returns 0 if the capability is granted.
1032 int security_capable(const struct cred *cred,
1033 struct user_namespace *ns,
1037 return call_int_hook(capable, 0, cred, ns, cap, opts);
1041 * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
1047 * Check whether the quotactl syscall is allowed for this @sb.
1049 * Return: Returns 0 if permission is granted.
1051 int security_quotactl(int cmds, int type, int id, const struct super_block *sb)
1053 return call_int_hook(quotactl, 0, cmds, type, id, sb);
1057 * security_quota_on() - Check if QUOTAON is allowed for a dentry
1060 * Check whether QUOTAON is allowed for @dentry.
1062 * Return: Returns 0 if permission is granted.
1064 int security_quota_on(struct dentry *dentry)
1066 return call_int_hook(quota_on, 0, dentry);
1070 * security_syslog() - Check if accessing the kernel message ring is allowed
1071 * @type: SYSLOG_ACTION_* type
1073 * Check permission before accessing the kernel message ring or changing
1074 * logging to the console. See the syslog(2) manual page for an explanation of
1077 * Return: Return 0 if permission is granted.
1079 int security_syslog(int type)
1081 return call_int_hook(syslog, 0, type);
1085 * security_settime64() - Check if changing the system time is allowed
1089 * Check permission to change the system time, struct timespec64 is defined in
1090 * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
1092 * Return: Returns 0 if permission is granted.
1094 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
1096 return call_int_hook(settime, 0, ts, tz);
1100 * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
1102 * @pages: number of pages
1104 * Check permissions for allocating a new virtual mapping. If all LSMs return
1105 * a positive value, __vm_enough_memory() will be called with cap_sys_admin
1106 * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
1107 * called with cap_sys_admin cleared.
1109 * Return: Returns 0 if permission is granted by the LSM infrastructure to the
1112 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
1114 struct security_hook_list *hp;
1115 int cap_sys_admin = 1;
1119 * The module will respond with a positive value if
1120 * it thinks the __vm_enough_memory() call should be
1121 * made with the cap_sys_admin set. If all of the modules
1122 * agree that it should be set it will. If any module
1123 * thinks it should not be set it won't.
1125 hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
1126 rc = hp->hook.vm_enough_memory(mm, pages);
1132 return __vm_enough_memory(mm, pages, cap_sys_admin);
1136 * security_bprm_creds_for_exec() - Prepare the credentials for exec()
1137 * @bprm: binary program information
1139 * If the setup in prepare_exec_creds did not setup @bprm->cred->security
1140 * properly for executing @bprm->file, update the LSM's portion of
1141 * @bprm->cred->security to be what commit_creds needs to install for the new
1142 * program. This hook may also optionally check permissions (e.g. for
1143 * transitions between security domains). The hook must set @bprm->secureexec
1144 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
1145 * contains the linux_binprm structure.
1147 * Return: Returns 0 if the hook is successful and permission is granted.
1149 int security_bprm_creds_for_exec(struct linux_binprm *bprm)
1151 return call_int_hook(bprm_creds_for_exec, 0, bprm);
1155 * security_bprm_creds_from_file() - Update linux_binprm creds based on file
1156 * @bprm: binary program information
1157 * @file: associated file
1159 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
1160 * exec, update @bprm->cred to reflect that change. This is called after
1161 * finding the binary that will be executed without an interpreter. This
1162 * ensures that the credentials will not be derived from a script that the
1163 * binary will need to reopen, which when reopend may end up being a completely
1164 * different file. This hook may also optionally check permissions (e.g. for
1165 * transitions between security domains). The hook must set @bprm->secureexec
1166 * to 1 if AT_SECURE should be set to request libc enable secure mode. The
1167 * hook must add to @bprm->per_clear any personality flags that should be
1168 * cleared from current->personality. @bprm contains the linux_binprm
1171 * Return: Returns 0 if the hook is successful and permission is granted.
1173 int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file)
1175 return call_int_hook(bprm_creds_from_file, 0, bprm, file);
1179 * security_bprm_check() - Mediate binary handler search
1180 * @bprm: binary program information
1182 * This hook mediates the point when a search for a binary handler will begin.
1183 * It allows a check against the @bprm->cred->security value which was set in
1184 * the preceding creds_for_exec call. The argv list and envp list are reliably
1185 * available in @bprm. This hook may be called multiple times during a single
1186 * execve. @bprm contains the linux_binprm structure.
1188 * Return: Returns 0 if the hook is successful and permission is granted.
1190 int security_bprm_check(struct linux_binprm *bprm)
1194 ret = call_int_hook(bprm_check_security, 0, bprm);
1197 return ima_bprm_check(bprm);
1201 * security_bprm_committing_creds() - Install creds for a process during exec()
1202 * @bprm: binary program information
1204 * Prepare to install the new security attributes of a process being
1205 * transformed by an execve operation, based on the old credentials pointed to
1206 * by @current->cred and the information set in @bprm->cred by the
1207 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
1208 * hook is a good place to perform state changes on the process such as closing
1209 * open file descriptors to which access will no longer be granted when the
1210 * attributes are changed. This is called immediately before commit_creds().
1212 void security_bprm_committing_creds(const struct linux_binprm *bprm)
1214 call_void_hook(bprm_committing_creds, bprm);
1218 * security_bprm_committed_creds() - Tidy up after cred install during exec()
1219 * @bprm: binary program information
1221 * Tidy up after the installation of the new security attributes of a process
1222 * being transformed by an execve operation. The new credentials have, by this
1223 * point, been set to @current->cred. @bprm points to the linux_binprm
1224 * structure. This hook is a good place to perform state changes on the
1225 * process such as clearing out non-inheritable signal state. This is called
1226 * immediately after commit_creds().
1228 void security_bprm_committed_creds(const struct linux_binprm *bprm)
1230 call_void_hook(bprm_committed_creds, bprm);
1234 * security_fs_context_submount() - Initialise fc->security
1235 * @fc: new filesystem context
1236 * @reference: dentry reference for submount/remount
1238 * Fill out the ->security field for a new fs_context.
1240 * Return: Returns 0 on success or negative error code on failure.
1242 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
1244 return call_int_hook(fs_context_submount, 0, fc, reference);
1248 * security_fs_context_dup() - Duplicate a fs_context LSM blob
1249 * @fc: destination filesystem context
1250 * @src_fc: source filesystem context
1252 * Allocate and attach a security structure to sc->security. This pointer is
1253 * initialised to NULL by the caller. @fc indicates the new filesystem context.
1254 * @src_fc indicates the original filesystem context.
1256 * Return: Returns 0 on success or a negative error code on failure.
1258 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1260 return call_int_hook(fs_context_dup, 0, fc, src_fc);
1264 * security_fs_context_parse_param() - Configure a filesystem context
1265 * @fc: filesystem context
1266 * @param: filesystem parameter
1268 * Userspace provided a parameter to configure a superblock. The LSM can
1269 * consume the parameter or return it to the caller for use elsewhere.
1271 * Return: If the parameter is used by the LSM it should return 0, if it is
1272 * returned to the caller -ENOPARAM is returned, otherwise a negative
1273 * error code is returned.
1275 int security_fs_context_parse_param(struct fs_context *fc,
1276 struct fs_parameter *param)
1278 struct security_hook_list *hp;
1282 hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1284 trc = hp->hook.fs_context_parse_param(fc, param);
1287 else if (trc != -ENOPARAM)
1294 * security_sb_alloc() - Allocate a super_block LSM blob
1295 * @sb: filesystem superblock
1297 * Allocate and attach a security structure to the sb->s_security field. The
1298 * s_security field is initialized to NULL when the structure is allocated.
1299 * @sb contains the super_block structure to be modified.
1301 * Return: Returns 0 if operation was successful.
1303 int security_sb_alloc(struct super_block *sb)
1305 int rc = lsm_superblock_alloc(sb);
1309 rc = call_int_hook(sb_alloc_security, 0, sb);
1311 security_sb_free(sb);
1316 * security_sb_delete() - Release super_block LSM associated objects
1317 * @sb: filesystem superblock
1319 * Release objects tied to a superblock (e.g. inodes). @sb contains the
1320 * super_block structure being released.
1322 void security_sb_delete(struct super_block *sb)
1324 call_void_hook(sb_delete, sb);
1328 * security_sb_free() - Free a super_block LSM blob
1329 * @sb: filesystem superblock
1331 * Deallocate and clear the sb->s_security field. @sb contains the super_block
1332 * structure to be modified.
1334 void security_sb_free(struct super_block *sb)
1336 call_void_hook(sb_free_security, sb);
1337 kfree(sb->s_security);
1338 sb->s_security = NULL;
1342 * security_free_mnt_opts() - Free memory associated with mount options
1343 * @mnt_opts: LSM processed mount options
1345 * Free memory associated with @mnt_ops.
1347 void security_free_mnt_opts(void **mnt_opts)
1351 call_void_hook(sb_free_mnt_opts, *mnt_opts);
1354 EXPORT_SYMBOL(security_free_mnt_opts);
1357 * security_sb_eat_lsm_opts() - Consume LSM mount options
1358 * @options: mount options
1359 * @mnt_opts: LSM processed mount options
1361 * Eat (scan @options) and save them in @mnt_opts.
1363 * Return: Returns 0 on success, negative values on failure.
1365 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1367 return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
1369 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1372 * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1373 * @sb: filesystem superblock
1374 * @mnt_opts: new mount options
1376 * Determine if the new mount options in @mnt_opts are allowed given the
1377 * existing mounted filesystem at @sb. @sb superblock being compared.
1379 * Return: Returns 0 if options are compatible.
1381 int security_sb_mnt_opts_compat(struct super_block *sb,
1384 return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
1386 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1389 * security_sb_remount() - Verify no incompatible mount changes during remount
1390 * @sb: filesystem superblock
1391 * @mnt_opts: (re)mount options
1393 * Extracts security system specific mount options and verifies no changes are
1394 * being made to those options.
1396 * Return: Returns 0 if permission is granted.
1398 int security_sb_remount(struct super_block *sb,
1401 return call_int_hook(sb_remount, 0, sb, mnt_opts);
1403 EXPORT_SYMBOL(security_sb_remount);
1406 * security_sb_kern_mount() - Check if a kernel mount is allowed
1407 * @sb: filesystem superblock
1409 * Mount this @sb if allowed by permissions.
1411 * Return: Returns 0 if permission is granted.
1413 int security_sb_kern_mount(const struct super_block *sb)
1415 return call_int_hook(sb_kern_mount, 0, sb);
1419 * security_sb_show_options() - Output the mount options for a superblock
1421 * @sb: filesystem superblock
1423 * Show (print on @m) mount options for this @sb.
1425 * Return: Returns 0 on success, negative values on failure.
1427 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1429 return call_int_hook(sb_show_options, 0, m, sb);
1433 * security_sb_statfs() - Check if accessing fs stats is allowed
1434 * @dentry: superblock handle
1436 * Check permission before obtaining filesystem statistics for the @mnt
1437 * mountpoint. @dentry is a handle on the superblock for the filesystem.
1439 * Return: Returns 0 if permission is granted.
1441 int security_sb_statfs(struct dentry *dentry)
1443 return call_int_hook(sb_statfs, 0, dentry);
1447 * security_sb_mount() - Check permission for mounting a filesystem
1448 * @dev_name: filesystem backing device
1449 * @path: mount point
1450 * @type: filesystem type
1451 * @flags: mount flags
1452 * @data: filesystem specific data
1454 * Check permission before an object specified by @dev_name is mounted on the
1455 * mount point named by @nd. For an ordinary mount, @dev_name identifies a
1456 * device if the file system type requires a device. For a remount
1457 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
1458 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
1461 * Return: Returns 0 if permission is granted.
1463 int security_sb_mount(const char *dev_name, const struct path *path,
1464 const char *type, unsigned long flags, void *data)
1466 return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
1470 * security_sb_umount() - Check permission for unmounting a filesystem
1471 * @mnt: mounted filesystem
1472 * @flags: unmount flags
1474 * Check permission before the @mnt file system is unmounted.
1476 * Return: Returns 0 if permission is granted.
1478 int security_sb_umount(struct vfsmount *mnt, int flags)
1480 return call_int_hook(sb_umount, 0, mnt, flags);
1484 * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1485 * @old_path: new location for current rootfs
1486 * @new_path: location of the new rootfs
1488 * Check permission before pivoting the root filesystem.
1490 * Return: Returns 0 if permission is granted.
1492 int security_sb_pivotroot(const struct path *old_path,
1493 const struct path *new_path)
1495 return call_int_hook(sb_pivotroot, 0, old_path, new_path);
1499 * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1500 * @sb: filesystem superblock
1501 * @mnt_opts: binary mount options
1502 * @kern_flags: kernel flags (in)
1503 * @set_kern_flags: kernel flags (out)
1505 * Set the security relevant mount options used for a superblock.
1507 * Return: Returns 0 on success, error on failure.
1509 int security_sb_set_mnt_opts(struct super_block *sb,
1511 unsigned long kern_flags,
1512 unsigned long *set_kern_flags)
1514 return call_int_hook(sb_set_mnt_opts,
1515 mnt_opts ? -EOPNOTSUPP : 0, sb,
1516 mnt_opts, kern_flags, set_kern_flags);
1518 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1521 * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1522 * @oldsb: source superblock
1523 * @newsb: destination superblock
1524 * @kern_flags: kernel flags (in)
1525 * @set_kern_flags: kernel flags (out)
1527 * Copy all security options from a given superblock to another.
1529 * Return: Returns 0 on success, error on failure.
1531 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1532 struct super_block *newsb,
1533 unsigned long kern_flags,
1534 unsigned long *set_kern_flags)
1536 return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
1537 kern_flags, set_kern_flags);
1539 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1542 * security_move_mount() - Check permissions for moving a mount
1543 * @from_path: source mount point
1544 * @to_path: destination mount point
1546 * Check permission before a mount is moved.
1548 * Return: Returns 0 if permission is granted.
1550 int security_move_mount(const struct path *from_path,
1551 const struct path *to_path)
1553 return call_int_hook(move_mount, 0, from_path, to_path);
1557 * security_path_notify() - Check if setting a watch is allowed
1560 * @obj_type: file path type
1562 * Check permissions before setting a watch on events as defined by @mask, on
1563 * an object at @path, whose type is defined by @obj_type.
1565 * Return: Returns 0 if permission is granted.
1567 int security_path_notify(const struct path *path, u64 mask,
1568 unsigned int obj_type)
1570 return call_int_hook(path_notify, 0, path, mask, obj_type);
1574 * security_inode_alloc() - Allocate an inode LSM blob
1577 * Allocate and attach a security structure to @inode->i_security. The
1578 * i_security field is initialized to NULL when the inode structure is
1581 * Return: Return 0 if operation was successful.
1583 int security_inode_alloc(struct inode *inode)
1585 int rc = lsm_inode_alloc(inode);
1589 rc = call_int_hook(inode_alloc_security, 0, inode);
1591 security_inode_free(inode);
1595 static void inode_free_by_rcu(struct rcu_head *head)
1598 * The rcu head is at the start of the inode blob
1600 kmem_cache_free(lsm_inode_cache, head);
1604 * security_inode_free() - Free an inode's LSM blob
1607 * Deallocate the inode security structure and set @inode->i_security to NULL.
1609 void security_inode_free(struct inode *inode)
1611 integrity_inode_free(inode);
1612 call_void_hook(inode_free_security, inode);
1614 * The inode may still be referenced in a path walk and
1615 * a call to security_inode_permission() can be made
1616 * after inode_free_security() is called. Ideally, the VFS
1617 * wouldn't do this, but fixing that is a much harder
1618 * job. For now, simply free the i_security via RCU, and
1619 * leave the current inode->i_security pointer intact.
1620 * The inode will be freed after the RCU grace period too.
1622 if (inode->i_security)
1623 call_rcu((struct rcu_head *)inode->i_security,
1628 * security_dentry_init_security() - Perform dentry initialization
1629 * @dentry: the dentry to initialize
1630 * @mode: mode used to determine resource type
1631 * @name: name of the last path component
1632 * @xattr_name: name of the security/LSM xattr
1633 * @ctx: pointer to the resulting LSM context
1634 * @ctxlen: length of @ctx
1636 * Compute a context for a dentry as the inode is not yet available since NFSv4
1637 * has no label backed by an EA anyway. It is important to note that
1638 * @xattr_name does not need to be free'd by the caller, it is a static string.
1640 * Return: Returns 0 on success, negative values on failure.
1642 int security_dentry_init_security(struct dentry *dentry, int mode,
1643 const struct qstr *name,
1644 const char **xattr_name, void **ctx,
1647 struct security_hook_list *hp;
1651 * Only one module will provide a security context.
1653 hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security,
1655 rc = hp->hook.dentry_init_security(dentry, mode, name,
1656 xattr_name, ctx, ctxlen);
1657 if (rc != LSM_RET_DEFAULT(dentry_init_security))
1660 return LSM_RET_DEFAULT(dentry_init_security);
1662 EXPORT_SYMBOL(security_dentry_init_security);
1665 * security_dentry_create_files_as() - Perform dentry initialization
1666 * @dentry: the dentry to initialize
1667 * @mode: mode used to determine resource type
1668 * @name: name of the last path component
1669 * @old: creds to use for LSM context calculations
1670 * @new: creds to modify
1672 * Compute a context for a dentry as the inode is not yet available and set
1673 * that context in passed in creds so that new files are created using that
1674 * context. Context is calculated using the passed in creds and not the creds
1677 * Return: Returns 0 on success, error on failure.
1679 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1681 const struct cred *old, struct cred *new)
1683 return call_int_hook(dentry_create_files_as, 0, dentry, mode,
1686 EXPORT_SYMBOL(security_dentry_create_files_as);
1689 * security_inode_init_security() - Initialize an inode's LSM context
1691 * @dir: parent directory
1692 * @qstr: last component of the pathname
1693 * @initxattrs: callback function to write xattrs
1694 * @fs_data: filesystem specific data
1696 * Obtain the security attribute name suffix and value to set on a newly
1697 * created inode and set up the incore security field for the new inode. This
1698 * hook is called by the fs code as part of the inode creation transaction and
1699 * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1700 * hooks called by the VFS.
1702 * The hook function is expected to populate the xattrs array, by calling
1703 * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
1704 * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
1705 * slot, the hook function should set ->name to the attribute name suffix
1706 * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
1707 * to the attribute value, to set ->value_len to the length of the value. If
1708 * the security module does not use security attributes or does not wish to put
1709 * a security attribute on this particular inode, then it should return
1710 * -EOPNOTSUPP to skip this processing.
1712 * Return: Returns 0 if the LSM successfully initialized all of the inode
1713 * security attributes that are required, negative values otherwise.
1715 int security_inode_init_security(struct inode *inode, struct inode *dir,
1716 const struct qstr *qstr,
1717 const initxattrs initxattrs, void *fs_data)
1719 struct security_hook_list *hp;
1720 struct xattr *new_xattrs = NULL;
1721 int ret = -EOPNOTSUPP, xattr_count = 0;
1723 if (unlikely(IS_PRIVATE(inode)))
1726 if (!blob_sizes.lbs_xattr_count)
1730 /* Allocate +1 for EVM and +1 as terminator. */
1731 new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 2,
1732 sizeof(*new_xattrs), GFP_NOFS);
1737 hlist_for_each_entry(hp, &security_hook_heads.inode_init_security,
1739 ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs,
1741 if (ret && ret != -EOPNOTSUPP)
1744 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
1745 * means that the LSM is not willing to provide an xattr, not
1746 * that it wants to signal an error. Thus, continue to invoke
1747 * the remaining LSMs.
1751 /* If initxattrs() is NULL, xattr_count is zero, skip the call. */
1755 ret = evm_inode_init_security(inode, dir, qstr, new_xattrs,
1759 ret = initxattrs(inode, new_xattrs, fs_data);
1761 for (; xattr_count > 0; xattr_count--)
1762 kfree(new_xattrs[xattr_count - 1].value);
1764 return (ret == -EOPNOTSUPP) ? 0 : ret;
1766 EXPORT_SYMBOL(security_inode_init_security);
1769 * security_inode_init_security_anon() - Initialize an anonymous inode
1771 * @name: the anonymous inode class
1772 * @context_inode: an optional related inode
1774 * Set up the incore security field for the new anonymous inode and return
1775 * whether the inode creation is permitted by the security module or not.
1777 * Return: Returns 0 on success, -EACCES if the security module denies the
1778 * creation of this inode, or another -errno upon other errors.
1780 int security_inode_init_security_anon(struct inode *inode,
1781 const struct qstr *name,
1782 const struct inode *context_inode)
1784 return call_int_hook(inode_init_security_anon, 0, inode, name,
1788 #ifdef CONFIG_SECURITY_PATH
1790 * security_path_mknod() - Check if creating a special file is allowed
1791 * @dir: parent directory
1793 * @mode: new file mode
1794 * @dev: device number
1796 * Check permissions when creating a file. Note that this hook is called even
1797 * if mknod operation is being done for a regular file.
1799 * Return: Returns 0 if permission is granted.
1801 int security_path_mknod(const struct path *dir, struct dentry *dentry,
1802 umode_t mode, unsigned int dev)
1804 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1806 return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1808 EXPORT_SYMBOL(security_path_mknod);
1811 * security_path_mkdir() - Check if creating a new directory is allowed
1812 * @dir: parent directory
1813 * @dentry: new directory
1814 * @mode: new directory mode
1816 * Check permissions to create a new directory in the existing directory.
1818 * Return: Returns 0 if permission is granted.
1820 int security_path_mkdir(const struct path *dir, struct dentry *dentry,
1823 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1825 return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1827 EXPORT_SYMBOL(security_path_mkdir);
1830 * security_path_rmdir() - Check if removing a directory is allowed
1831 * @dir: parent directory
1832 * @dentry: directory to remove
1834 * Check the permission to remove a directory.
1836 * Return: Returns 0 if permission is granted.
1838 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1840 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1842 return call_int_hook(path_rmdir, 0, dir, dentry);
1846 * security_path_unlink() - Check if removing a hard link is allowed
1847 * @dir: parent directory
1850 * Check the permission to remove a hard link to a file.
1852 * Return: Returns 0 if permission is granted.
1854 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1856 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1858 return call_int_hook(path_unlink, 0, dir, dentry);
1860 EXPORT_SYMBOL(security_path_unlink);
1863 * security_path_symlink() - Check if creating a symbolic link is allowed
1864 * @dir: parent directory
1865 * @dentry: symbolic link
1866 * @old_name: file pathname
1868 * Check the permission to create a symbolic link to a file.
1870 * Return: Returns 0 if permission is granted.
1872 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1873 const char *old_name)
1875 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1877 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1881 * security_path_link - Check if creating a hard link is allowed
1882 * @old_dentry: existing file
1883 * @new_dir: new parent directory
1884 * @new_dentry: new link
1886 * Check permission before creating a new hard link to a file.
1888 * Return: Returns 0 if permission is granted.
1890 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1891 struct dentry *new_dentry)
1893 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1895 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1899 * security_path_rename() - Check if renaming a file is allowed
1900 * @old_dir: parent directory of the old file
1901 * @old_dentry: the old file
1902 * @new_dir: parent directory of the new file
1903 * @new_dentry: the new file
1906 * Check for permission to rename a file or directory.
1908 * Return: Returns 0 if permission is granted.
1910 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1911 const struct path *new_dir, struct dentry *new_dentry,
1914 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1915 (d_is_positive(new_dentry) &&
1916 IS_PRIVATE(d_backing_inode(new_dentry)))))
1919 return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1922 EXPORT_SYMBOL(security_path_rename);
1925 * security_path_truncate() - Check if truncating a file is allowed
1928 * Check permission before truncating the file indicated by path. Note that
1929 * truncation permissions may also be checked based on already opened files,
1930 * using the security_file_truncate() hook.
1932 * Return: Returns 0 if permission is granted.
1934 int security_path_truncate(const struct path *path)
1936 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1938 return call_int_hook(path_truncate, 0, path);
1942 * security_path_chmod() - Check if changing the file's mode is allowed
1946 * Check for permission to change a mode of the file @path. The new mode is
1947 * specified in @mode which is a bitmask of constants from
1948 * <include/uapi/linux/stat.h>.
1950 * Return: Returns 0 if permission is granted.
1952 int security_path_chmod(const struct path *path, umode_t mode)
1954 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1956 return call_int_hook(path_chmod, 0, path, mode);
1960 * security_path_chown() - Check if changing the file's owner/group is allowed
1965 * Check for permission to change owner/group of a file or directory.
1967 * Return: Returns 0 if permission is granted.
1969 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1971 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1973 return call_int_hook(path_chown, 0, path, uid, gid);
1977 * security_path_chroot() - Check if changing the root directory is allowed
1980 * Check for permission to change root directory.
1982 * Return: Returns 0 if permission is granted.
1984 int security_path_chroot(const struct path *path)
1986 return call_int_hook(path_chroot, 0, path);
1988 #endif /* CONFIG_SECURITY_PATH */
1991 * security_inode_create() - Check if creating a file is allowed
1992 * @dir: the parent directory
1993 * @dentry: the file being created
1994 * @mode: requested file mode
1996 * Check permission to create a regular file.
1998 * Return: Returns 0 if permission is granted.
2000 int security_inode_create(struct inode *dir, struct dentry *dentry,
2003 if (unlikely(IS_PRIVATE(dir)))
2005 return call_int_hook(inode_create, 0, dir, dentry, mode);
2007 EXPORT_SYMBOL_GPL(security_inode_create);
2010 * security_inode_link() - Check if creating a hard link is allowed
2011 * @old_dentry: existing file
2012 * @dir: new parent directory
2013 * @new_dentry: new link
2015 * Check permission before creating a new hard link to a file.
2017 * Return: Returns 0 if permission is granted.
2019 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
2020 struct dentry *new_dentry)
2022 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
2024 return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
2028 * security_inode_unlink() - Check if removing a hard link is allowed
2029 * @dir: parent directory
2032 * Check the permission to remove a hard link to a file.
2034 * Return: Returns 0 if permission is granted.
2036 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
2038 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2040 return call_int_hook(inode_unlink, 0, dir, dentry);
2044 * security_inode_symlink() - Check if creating a symbolic link is allowed
2045 * @dir: parent directory
2046 * @dentry: symbolic link
2047 * @old_name: existing filename
2049 * Check the permission to create a symbolic link to a file.
2051 * Return: Returns 0 if permission is granted.
2053 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
2054 const char *old_name)
2056 if (unlikely(IS_PRIVATE(dir)))
2058 return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
2062 * security_inode_mkdir() - Check if creation a new director is allowed
2063 * @dir: parent directory
2064 * @dentry: new directory
2065 * @mode: new directory mode
2067 * Check permissions to create a new directory in the existing directory
2068 * associated with inode structure @dir.
2070 * Return: Returns 0 if permission is granted.
2072 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2074 if (unlikely(IS_PRIVATE(dir)))
2076 return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
2078 EXPORT_SYMBOL_GPL(security_inode_mkdir);
2081 * security_inode_rmdir() - Check if removing a directory is allowed
2082 * @dir: parent directory
2083 * @dentry: directory to be removed
2085 * Check the permission to remove a directory.
2087 * Return: Returns 0 if permission is granted.
2089 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
2091 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2093 return call_int_hook(inode_rmdir, 0, dir, dentry);
2097 * security_inode_mknod() - Check if creating a special file is allowed
2098 * @dir: parent directory
2100 * @mode: new file mode
2101 * @dev: device number
2103 * Check permissions when creating a special file (or a socket or a fifo file
2104 * created via the mknod system call). Note that if mknod operation is being
2105 * done for a regular file, then the create hook will be called and not this
2108 * Return: Returns 0 if permission is granted.
2110 int security_inode_mknod(struct inode *dir, struct dentry *dentry,
2111 umode_t mode, dev_t dev)
2113 if (unlikely(IS_PRIVATE(dir)))
2115 return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
2119 * security_inode_rename() - Check if renaming a file is allowed
2120 * @old_dir: parent directory of the old file
2121 * @old_dentry: the old file
2122 * @new_dir: parent directory of the new file
2123 * @new_dentry: the new file
2126 * Check for permission to rename a file or directory.
2128 * Return: Returns 0 if permission is granted.
2130 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
2131 struct inode *new_dir, struct dentry *new_dentry,
2134 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
2135 (d_is_positive(new_dentry) &&
2136 IS_PRIVATE(d_backing_inode(new_dentry)))))
2139 if (flags & RENAME_EXCHANGE) {
2140 int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
2141 old_dir, old_dentry);
2146 return call_int_hook(inode_rename, 0, old_dir, old_dentry,
2147 new_dir, new_dentry);
2151 * security_inode_readlink() - Check if reading a symbolic link is allowed
2154 * Check the permission to read the symbolic link.
2156 * Return: Returns 0 if permission is granted.
2158 int security_inode_readlink(struct dentry *dentry)
2160 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2162 return call_int_hook(inode_readlink, 0, dentry);
2166 * security_inode_follow_link() - Check if following a symbolic link is allowed
2167 * @dentry: link dentry
2168 * @inode: link inode
2169 * @rcu: true if in RCU-walk mode
2171 * Check permission to follow a symbolic link when looking up a pathname. If
2172 * @rcu is true, @inode is not stable.
2174 * Return: Returns 0 if permission is granted.
2176 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
2179 if (unlikely(IS_PRIVATE(inode)))
2181 return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
2185 * security_inode_permission() - Check if accessing an inode is allowed
2187 * @mask: access mask
2189 * Check permission before accessing an inode. This hook is called by the
2190 * existing Linux permission function, so a security module can use it to
2191 * provide additional checking for existing Linux permission checks. Notice
2192 * that this hook is called when a file is opened (as well as many other
2193 * operations), whereas the file_security_ops permission hook is called when
2194 * the actual read/write operations are performed.
2196 * Return: Returns 0 if permission is granted.
2198 int security_inode_permission(struct inode *inode, int mask)
2200 if (unlikely(IS_PRIVATE(inode)))
2202 return call_int_hook(inode_permission, 0, inode, mask);
2206 * security_inode_setattr() - Check if setting file attributes is allowed
2207 * @idmap: idmap of the mount
2209 * @attr: new attributes
2211 * Check permission before setting file attributes. Note that the kernel call
2212 * to notify_change is performed from several locations, whenever file
2213 * attributes change (such as when a file is truncated, chown/chmod operations,
2214 * transferring disk quotas, etc).
2216 * Return: Returns 0 if permission is granted.
2218 int security_inode_setattr(struct mnt_idmap *idmap,
2219 struct dentry *dentry, struct iattr *attr)
2223 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2225 ret = call_int_hook(inode_setattr, 0, dentry, attr);
2228 return evm_inode_setattr(idmap, dentry, attr);
2230 EXPORT_SYMBOL_GPL(security_inode_setattr);
2233 * security_inode_getattr() - Check if getting file attributes is allowed
2236 * Check permission before obtaining file attributes.
2238 * Return: Returns 0 if permission is granted.
2240 int security_inode_getattr(const struct path *path)
2242 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2244 return call_int_hook(inode_getattr, 0, path);
2248 * security_inode_setxattr() - Check if setting file xattrs is allowed
2249 * @idmap: idmap of the mount
2252 * @value: xattr value
2253 * @size: size of xattr value
2256 * Check permission before setting the extended attributes.
2258 * Return: Returns 0 if permission is granted.
2260 int security_inode_setxattr(struct mnt_idmap *idmap,
2261 struct dentry *dentry, const char *name,
2262 const void *value, size_t size, int flags)
2266 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2269 * SELinux and Smack integrate the cap call,
2270 * so assume that all LSMs supplying this call do so.
2272 ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
2276 ret = cap_inode_setxattr(dentry, name, value, size, flags);
2279 ret = ima_inode_setxattr(dentry, name, value, size);
2282 return evm_inode_setxattr(idmap, dentry, name, value, size);
2286 * security_inode_set_acl() - Check if setting posix acls is allowed
2287 * @idmap: idmap of the mount
2289 * @acl_name: acl name
2292 * Check permission before setting posix acls, the posix acls in @kacl are
2293 * identified by @acl_name.
2295 * Return: Returns 0 if permission is granted.
2297 int security_inode_set_acl(struct mnt_idmap *idmap,
2298 struct dentry *dentry, const char *acl_name,
2299 struct posix_acl *kacl)
2303 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2305 ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
2309 ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
2312 return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
2316 * security_inode_get_acl() - Check if reading posix acls is allowed
2317 * @idmap: idmap of the mount
2319 * @acl_name: acl name
2321 * Check permission before getting osix acls, the posix acls are identified by
2324 * Return: Returns 0 if permission is granted.
2326 int security_inode_get_acl(struct mnt_idmap *idmap,
2327 struct dentry *dentry, const char *acl_name)
2329 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2331 return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
2335 * security_inode_remove_acl() - Check if removing a posix acl is allowed
2336 * @idmap: idmap of the mount
2338 * @acl_name: acl name
2340 * Check permission before removing posix acls, the posix acls are identified
2343 * Return: Returns 0 if permission is granted.
2345 int security_inode_remove_acl(struct mnt_idmap *idmap,
2346 struct dentry *dentry, const char *acl_name)
2350 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2352 ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
2355 ret = ima_inode_remove_acl(idmap, dentry, acl_name);
2358 return evm_inode_remove_acl(idmap, dentry, acl_name);
2362 * security_inode_post_setxattr() - Update the inode after a setxattr operation
2365 * @value: xattr value
2366 * @size: xattr value size
2369 * Update inode security field after successful setxattr operation.
2371 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2372 const void *value, size_t size, int flags)
2374 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2376 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2377 evm_inode_post_setxattr(dentry, name, value, size);
2381 * security_inode_getxattr() - Check if xattr access is allowed
2385 * Check permission before obtaining the extended attributes identified by
2386 * @name for @dentry.
2388 * Return: Returns 0 if permission is granted.
2390 int security_inode_getxattr(struct dentry *dentry, const char *name)
2392 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2394 return call_int_hook(inode_getxattr, 0, dentry, name);
2398 * security_inode_listxattr() - Check if listing xattrs is allowed
2401 * Check permission before obtaining the list of extended attribute names for
2404 * Return: Returns 0 if permission is granted.
2406 int security_inode_listxattr(struct dentry *dentry)
2408 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2410 return call_int_hook(inode_listxattr, 0, dentry);
2414 * security_inode_removexattr() - Check if removing an xattr is allowed
2415 * @idmap: idmap of the mount
2419 * Check permission before removing the extended attribute identified by @name
2422 * Return: Returns 0 if permission is granted.
2424 int security_inode_removexattr(struct mnt_idmap *idmap,
2425 struct dentry *dentry, const char *name)
2429 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2432 * SELinux and Smack integrate the cap call,
2433 * so assume that all LSMs supplying this call do so.
2435 ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
2437 ret = cap_inode_removexattr(idmap, dentry, name);
2440 ret = ima_inode_removexattr(dentry, name);
2443 return evm_inode_removexattr(idmap, dentry, name);
2447 * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2448 * @dentry: associated dentry
2450 * Called when an inode has been changed to determine if
2451 * security_inode_killpriv() should be called.
2453 * Return: Return <0 on error to abort the inode change operation, return 0 if
2454 * security_inode_killpriv() does not need to be called, return >0 if
2455 * security_inode_killpriv() does need to be called.
2457 int security_inode_need_killpriv(struct dentry *dentry)
2459 return call_int_hook(inode_need_killpriv, 0, dentry);
2463 * security_inode_killpriv() - The setuid bit is removed, update LSM state
2464 * @idmap: idmap of the mount
2465 * @dentry: associated dentry
2467 * The @dentry's setuid bit is being removed. Remove similar security labels.
2468 * Called with the dentry->d_inode->i_mutex held.
2470 * Return: Return 0 on success. If error is returned, then the operation
2471 * causing setuid bit removal is failed.
2473 int security_inode_killpriv(struct mnt_idmap *idmap,
2474 struct dentry *dentry)
2476 return call_int_hook(inode_killpriv, 0, idmap, dentry);
2480 * security_inode_getsecurity() - Get the xattr security label of an inode
2481 * @idmap: idmap of the mount
2484 * @buffer: security label buffer
2485 * @alloc: allocation flag
2487 * Retrieve a copy of the extended attribute representation of the security
2488 * label associated with @name for @inode via @buffer. Note that @name is the
2489 * remainder of the attribute name after the security prefix has been removed.
2490 * @alloc is used to specify if the call should return a value via the buffer
2491 * or just the value length.
2493 * Return: Returns size of buffer on success.
2495 int security_inode_getsecurity(struct mnt_idmap *idmap,
2496 struct inode *inode, const char *name,
2497 void **buffer, bool alloc)
2499 struct security_hook_list *hp;
2502 if (unlikely(IS_PRIVATE(inode)))
2503 return LSM_RET_DEFAULT(inode_getsecurity);
2505 * Only one module will provide an attribute with a given name.
2507 hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
2508 rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer,
2510 if (rc != LSM_RET_DEFAULT(inode_getsecurity))
2513 return LSM_RET_DEFAULT(inode_getsecurity);
2517 * security_inode_setsecurity() - Set the xattr security label of an inode
2520 * @value: security label
2521 * @size: length of security label
2524 * Set the security label associated with @name for @inode from the extended
2525 * attribute value @value. @size indicates the size of the @value in bytes.
2526 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2527 * remainder of the attribute name after the security. prefix has been removed.
2529 * Return: Returns 0 on success.
2531 int security_inode_setsecurity(struct inode *inode, const char *name,
2532 const void *value, size_t size, int flags)
2534 struct security_hook_list *hp;
2537 if (unlikely(IS_PRIVATE(inode)))
2538 return LSM_RET_DEFAULT(inode_setsecurity);
2540 * Only one module will provide an attribute with a given name.
2542 hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
2543 rc = hp->hook.inode_setsecurity(inode, name, value, size,
2545 if (rc != LSM_RET_DEFAULT(inode_setsecurity))
2548 return LSM_RET_DEFAULT(inode_setsecurity);
2552 * security_inode_listsecurity() - List the xattr security label names
2555 * @buffer_size: size of buffer
2557 * Copy the extended attribute names for the security labels associated with
2558 * @inode into @buffer. The maximum size of @buffer is specified by
2559 * @buffer_size. @buffer may be NULL to request the size of the buffer
2562 * Return: Returns number of bytes used/required on success.
2564 int security_inode_listsecurity(struct inode *inode,
2565 char *buffer, size_t buffer_size)
2567 if (unlikely(IS_PRIVATE(inode)))
2569 return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
2571 EXPORT_SYMBOL(security_inode_listsecurity);
2574 * security_inode_getsecid() - Get an inode's secid
2576 * @secid: secid to return
2578 * Get the secid associated with the node. In case of failure, @secid will be
2581 void security_inode_getsecid(struct inode *inode, u32 *secid)
2583 call_void_hook(inode_getsecid, inode, secid);
2587 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2588 * @src: union dentry of copy-up file
2589 * @new: newly created creds
2591 * A file is about to be copied up from lower layer to upper layer of overlay
2592 * filesystem. Security module can prepare a set of new creds and modify as
2593 * need be and return new creds. Caller will switch to new creds temporarily to
2594 * create new file and release newly allocated creds.
2596 * Return: Returns 0 on success or a negative error code on error.
2598 int security_inode_copy_up(struct dentry *src, struct cred **new)
2600 return call_int_hook(inode_copy_up, 0, src, new);
2602 EXPORT_SYMBOL(security_inode_copy_up);
2605 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2608 * Filter the xattrs being copied up when a unioned file is copied up from a
2609 * lower layer to the union/overlay layer. The caller is responsible for
2610 * reading and writing the xattrs, this hook is merely a filter.
2612 * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2613 * if the security module does not know about attribute, or a negative
2614 * error code to abort the copy up.
2616 int security_inode_copy_up_xattr(const char *name)
2618 struct security_hook_list *hp;
2622 * The implementation can return 0 (accept the xattr), 1 (discard the
2623 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2624 * any other error code in case of an error.
2626 hlist_for_each_entry(hp,
2627 &security_hook_heads.inode_copy_up_xattr, list) {
2628 rc = hp->hook.inode_copy_up_xattr(name);
2629 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2633 return evm_inode_copy_up_xattr(name);
2635 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2638 * security_kernfs_init_security() - Init LSM context for a kernfs node
2639 * @kn_dir: parent kernfs node
2640 * @kn: the kernfs node to initialize
2642 * Initialize the security context of a newly created kernfs node based on its
2643 * own and its parent's attributes.
2645 * Return: Returns 0 if permission is granted.
2647 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2648 struct kernfs_node *kn)
2650 return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2654 * security_file_permission() - Check file permissions
2656 * @mask: requested permissions
2658 * Check file permissions before accessing an open file. This hook is called
2659 * by various operations that read or write files. A security module can use
2660 * this hook to perform additional checking on these operations, e.g. to
2661 * revalidate permissions on use to support privilege bracketing or policy
2662 * changes. Notice that this hook is used when the actual read/write
2663 * operations are performed, whereas the inode_security_ops hook is called when
2664 * a file is opened (as well as many other operations). Although this hook can
2665 * be used to revalidate permissions for various system call operations that
2666 * read or write files, it does not address the revalidation of permissions for
2667 * memory-mapped files. Security modules must handle this separately if they
2668 * need such revalidation.
2670 * Return: Returns 0 if permission is granted.
2672 int security_file_permission(struct file *file, int mask)
2674 return call_int_hook(file_permission, 0, file, mask);
2678 * security_file_alloc() - Allocate and init a file's LSM blob
2681 * Allocate and attach a security structure to the file->f_security field. The
2682 * security field is initialized to NULL when the structure is first created.
2684 * Return: Return 0 if the hook is successful and permission is granted.
2686 int security_file_alloc(struct file *file)
2688 int rc = lsm_file_alloc(file);
2692 rc = call_int_hook(file_alloc_security, 0, file);
2694 security_file_free(file);
2699 * security_file_free() - Free a file's LSM blob
2702 * Deallocate and free any security structures stored in file->f_security.
2704 void security_file_free(struct file *file)
2708 call_void_hook(file_free_security, file);
2710 blob = file->f_security;
2712 file->f_security = NULL;
2713 kmem_cache_free(lsm_file_cache, blob);
2718 * security_file_ioctl() - Check if an ioctl is allowed
2719 * @file: associated file
2721 * @arg: ioctl arguments
2723 * Check permission for an ioctl operation on @file. Note that @arg sometimes
2724 * represents a user space pointer; in other cases, it may be a simple integer
2725 * value. When @arg represents a user space pointer, it should never be used
2726 * by the security module.
2728 * Return: Returns 0 if permission is granted.
2730 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2732 return call_int_hook(file_ioctl, 0, file, cmd, arg);
2734 EXPORT_SYMBOL_GPL(security_file_ioctl);
2737 * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
2738 * @file: associated file
2740 * @arg: ioctl arguments
2742 * Compat version of security_file_ioctl() that correctly handles 32-bit
2743 * processes running on 64-bit kernels.
2745 * Return: Returns 0 if permission is granted.
2747 int security_file_ioctl_compat(struct file *file, unsigned int cmd,
2750 return call_int_hook(file_ioctl_compat, 0, file, cmd, arg);
2752 EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
2754 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2757 * Does we have PROT_READ and does the application expect
2758 * it to imply PROT_EXEC? If not, nothing to talk about...
2760 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2762 if (!(current->personality & READ_IMPLIES_EXEC))
2765 * if that's an anonymous mapping, let it.
2768 return prot | PROT_EXEC;
2770 * ditto if it's not on noexec mount, except that on !MMU we need
2771 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2773 if (!path_noexec(&file->f_path)) {
2775 if (file->f_op->mmap_capabilities) {
2776 unsigned caps = file->f_op->mmap_capabilities(file);
2777 if (!(caps & NOMMU_MAP_EXEC))
2781 return prot | PROT_EXEC;
2783 /* anything on noexec mount won't get PROT_EXEC */
2788 * security_mmap_file() - Check if mmap'ing a file is allowed
2790 * @prot: protection applied by the kernel
2793 * Check permissions for a mmap operation. The @file may be NULL, e.g. if
2794 * mapping anonymous memory.
2796 * Return: Returns 0 if permission is granted.
2798 int security_mmap_file(struct file *file, unsigned long prot,
2799 unsigned long flags)
2801 unsigned long prot_adj = mmap_prot(file, prot);
2804 ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
2807 return ima_file_mmap(file, prot, prot_adj, flags);
2811 * security_mmap_addr() - Check if mmap'ing an address is allowed
2814 * Check permissions for a mmap operation at @addr.
2816 * Return: Returns 0 if permission is granted.
2818 int security_mmap_addr(unsigned long addr)
2820 return call_int_hook(mmap_addr, 0, addr);
2824 * security_file_mprotect() - Check if changing memory protections is allowed
2825 * @vma: memory region
2826 * @reqprot: application requested protection
2827 * @prot: protection applied by the kernel
2829 * Check permissions before changing memory access permissions.
2831 * Return: Returns 0 if permission is granted.
2833 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2838 ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
2841 return ima_file_mprotect(vma, prot);
2845 * security_file_lock() - Check if a file lock is allowed
2847 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2849 * Check permission before performing file locking operations. Note the hook
2850 * mediates both flock and fcntl style locks.
2852 * Return: Returns 0 if permission is granted.
2854 int security_file_lock(struct file *file, unsigned int cmd)
2856 return call_int_hook(file_lock, 0, file, cmd);
2860 * security_file_fcntl() - Check if fcntl() op is allowed
2862 * @cmd: fcntl command
2863 * @arg: command argument
2865 * Check permission before allowing the file operation specified by @cmd from
2866 * being performed on the file @file. Note that @arg sometimes represents a
2867 * user space pointer; in other cases, it may be a simple integer value. When
2868 * @arg represents a user space pointer, it should never be used by the
2871 * Return: Returns 0 if permission is granted.
2873 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2875 return call_int_hook(file_fcntl, 0, file, cmd, arg);
2879 * security_file_set_fowner() - Set the file owner info in the LSM blob
2882 * Save owner security information (typically from current->security) in
2883 * file->f_security for later use by the send_sigiotask hook.
2885 * Return: Returns 0 on success.
2887 void security_file_set_fowner(struct file *file)
2889 call_void_hook(file_set_fowner, file);
2893 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2895 * @fown: signal sender
2896 * @sig: signal to be sent, SIGIO is sent if 0
2898 * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2899 * process @tsk. Note that this hook is sometimes called from interrupt. Note
2900 * that the fown_struct, @fown, is never outside the context of a struct file,
2901 * so the file structure (and associated security information) can always be
2902 * obtained: container_of(fown, struct file, f_owner).
2904 * Return: Returns 0 if permission is granted.
2906 int security_file_send_sigiotask(struct task_struct *tsk,
2907 struct fown_struct *fown, int sig)
2909 return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
2913 * security_file_receive() - Check is receiving a file via IPC is allowed
2914 * @file: file being received
2916 * This hook allows security modules to control the ability of a process to
2917 * receive an open file descriptor via socket IPC.
2919 * Return: Returns 0 if permission is granted.
2921 int security_file_receive(struct file *file)
2923 return call_int_hook(file_receive, 0, file);
2927 * security_file_open() - Save open() time state for late use by the LSM
2930 * Save open-time permission checking state for later use upon file_permission,
2931 * and recheck access if anything has changed since inode_permission.
2933 * Return: Returns 0 if permission is granted.
2935 int security_file_open(struct file *file)
2939 ret = call_int_hook(file_open, 0, file);
2943 return fsnotify_open_perm(file);
2947 * security_file_truncate() - Check if truncating a file is allowed
2950 * Check permission before truncating a file, i.e. using ftruncate. Note that
2951 * truncation permission may also be checked based on the path, using the
2952 * @path_truncate hook.
2954 * Return: Returns 0 if permission is granted.
2956 int security_file_truncate(struct file *file)
2958 return call_int_hook(file_truncate, 0, file);
2962 * security_task_alloc() - Allocate a task's LSM blob
2964 * @clone_flags: flags indicating what is being shared
2966 * Handle allocation of task-related resources.
2968 * Return: Returns a zero on success, negative values on failure.
2970 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2972 int rc = lsm_task_alloc(task);
2976 rc = call_int_hook(task_alloc, 0, task, clone_flags);
2978 security_task_free(task);
2983 * security_task_free() - Free a task's LSM blob and related resources
2986 * Handle release of task-related resources. Note that this can be called from
2987 * interrupt context.
2989 void security_task_free(struct task_struct *task)
2991 call_void_hook(task_free, task);
2993 kfree(task->security);
2994 task->security = NULL;
2998 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
2999 * @cred: credentials
3002 * Only allocate sufficient memory and attach to @cred such that
3003 * cred_transfer() will not get ENOMEM.
3005 * Return: Returns 0 on success, negative values on failure.
3007 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3009 int rc = lsm_cred_alloc(cred, gfp);
3014 rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
3016 security_cred_free(cred);
3021 * security_cred_free() - Free the cred's LSM blob and associated resources
3022 * @cred: credentials
3024 * Deallocate and clear the cred->security field in a set of credentials.
3026 void security_cred_free(struct cred *cred)
3029 * There is a failure case in prepare_creds() that
3030 * may result in a call here with ->security being NULL.
3032 if (unlikely(cred->security == NULL))
3035 call_void_hook(cred_free, cred);
3037 kfree(cred->security);
3038 cred->security = NULL;
3042 * security_prepare_creds() - Prepare a new set of credentials
3043 * @new: new credentials
3044 * @old: original credentials
3047 * Prepare a new set of credentials by copying the data from the old set.
3049 * Return: Returns 0 on success, negative values on failure.
3051 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
3053 int rc = lsm_cred_alloc(new, gfp);
3058 rc = call_int_hook(cred_prepare, 0, new, old, gfp);
3060 security_cred_free(new);
3065 * security_transfer_creds() - Transfer creds
3066 * @new: target credentials
3067 * @old: original credentials
3069 * Transfer data from original creds to new creds.
3071 void security_transfer_creds(struct cred *new, const struct cred *old)
3073 call_void_hook(cred_transfer, new, old);
3077 * security_cred_getsecid() - Get the secid from a set of credentials
3079 * @secid: secid value
3081 * Retrieve the security identifier of the cred structure @c. In case of
3082 * failure, @secid will be set to zero.
3084 void security_cred_getsecid(const struct cred *c, u32 *secid)
3087 call_void_hook(cred_getsecid, c, secid);
3089 EXPORT_SYMBOL(security_cred_getsecid);
3092 * security_kernel_act_as() - Set the kernel credentials to act as secid
3096 * Set the credentials for a kernel service to act as (subjective context).
3097 * The current task must be the one that nominated @secid.
3099 * Return: Returns 0 if successful.
3101 int security_kernel_act_as(struct cred *new, u32 secid)
3103 return call_int_hook(kernel_act_as, 0, new, secid);
3107 * security_kernel_create_files_as() - Set file creation context using an inode
3108 * @new: target credentials
3109 * @inode: reference inode
3111 * Set the file creation context in a set of credentials to be the same as the
3112 * objective context of the specified inode. The current task must be the one
3113 * that nominated @inode.
3115 * Return: Returns 0 if successful.
3117 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
3119 return call_int_hook(kernel_create_files_as, 0, new, inode);
3123 * security_kernel_module_request() - Check is loading a module is allowed
3124 * @kmod_name: module name
3126 * Ability to trigger the kernel to automatically upcall to userspace for
3127 * userspace to load a kernel module with the given name.
3129 * Return: Returns 0 if successful.
3131 int security_kernel_module_request(char *kmod_name)
3135 ret = call_int_hook(kernel_module_request, 0, kmod_name);
3138 return integrity_kernel_module_request(kmod_name);
3142 * security_kernel_read_file() - Read a file specified by userspace
3144 * @id: file identifier
3145 * @contents: trust if security_kernel_post_read_file() will be called
3147 * Read a file specified by userspace.
3149 * Return: Returns 0 if permission is granted.
3151 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
3156 ret = call_int_hook(kernel_read_file, 0, file, id, contents);
3159 return ima_read_file(file, id, contents);
3161 EXPORT_SYMBOL_GPL(security_kernel_read_file);
3164 * security_kernel_post_read_file() - Read a file specified by userspace
3166 * @buf: file contents
3167 * @size: size of file contents
3168 * @id: file identifier
3170 * Read a file specified by userspace. This must be paired with a prior call
3171 * to security_kernel_read_file() call that indicated this hook would also be
3172 * called, see security_kernel_read_file() for more information.
3174 * Return: Returns 0 if permission is granted.
3176 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3177 enum kernel_read_file_id id)
3181 ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
3184 return ima_post_read_file(file, buf, size, id);
3186 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3189 * security_kernel_load_data() - Load data provided by userspace
3190 * @id: data identifier
3191 * @contents: true if security_kernel_post_load_data() will be called
3193 * Load data provided by userspace.
3195 * Return: Returns 0 if permission is granted.
3197 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
3201 ret = call_int_hook(kernel_load_data, 0, id, contents);
3204 return ima_load_data(id, contents);
3206 EXPORT_SYMBOL_GPL(security_kernel_load_data);
3209 * security_kernel_post_load_data() - Load userspace data from a non-file source
3211 * @size: size of data
3212 * @id: data identifier
3213 * @description: text description of data, specific to the id value
3215 * Load data provided by a non-file source (usually userspace buffer). This
3216 * must be paired with a prior security_kernel_load_data() call that indicated
3217 * this hook would also be called, see security_kernel_load_data() for more
3220 * Return: Returns 0 if permission is granted.
3222 int security_kernel_post_load_data(char *buf, loff_t size,
3223 enum kernel_load_data_id id,
3228 ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
3232 return ima_post_load_data(buf, size, id, description);
3234 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3237 * security_task_fix_setuid() - Update LSM with new user id attributes
3238 * @new: updated credentials
3239 * @old: credentials being replaced
3240 * @flags: LSM_SETID_* flag values
3242 * Update the module's state after setting one or more of the user identity
3243 * attributes of the current process. The @flags parameter indicates which of
3244 * the set*uid system calls invoked this hook. If @new is the set of
3245 * credentials that will be installed. Modifications should be made to this
3246 * rather than to @current->cred.
3248 * Return: Returns 0 on success.
3250 int security_task_fix_setuid(struct cred *new, const struct cred *old,
3253 return call_int_hook(task_fix_setuid, 0, new, old, flags);
3257 * security_task_fix_setgid() - Update LSM with new group id attributes
3258 * @new: updated credentials
3259 * @old: credentials being replaced
3260 * @flags: LSM_SETID_* flag value
3262 * Update the module's state after setting one or more of the group identity
3263 * attributes of the current process. The @flags parameter indicates which of
3264 * the set*gid system calls invoked this hook. @new is the set of credentials
3265 * that will be installed. Modifications should be made to this rather than to
3268 * Return: Returns 0 on success.
3270 int security_task_fix_setgid(struct cred *new, const struct cred *old,
3273 return call_int_hook(task_fix_setgid, 0, new, old, flags);
3277 * security_task_fix_setgroups() - Update LSM with new supplementary groups
3278 * @new: updated credentials
3279 * @old: credentials being replaced
3281 * Update the module's state after setting the supplementary group identity
3282 * attributes of the current process. @new is the set of credentials that will
3283 * be installed. Modifications should be made to this rather than to
3286 * Return: Returns 0 on success.
3288 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3290 return call_int_hook(task_fix_setgroups, 0, new, old);
3294 * security_task_setpgid() - Check if setting the pgid is allowed
3295 * @p: task being modified
3298 * Check permission before setting the process group identifier of the process
3301 * Return: Returns 0 if permission is granted.
3303 int security_task_setpgid(struct task_struct *p, pid_t pgid)
3305 return call_int_hook(task_setpgid, 0, p, pgid);
3309 * security_task_getpgid() - Check if getting the pgid is allowed
3312 * Check permission before getting the process group identifier of the process
3315 * Return: Returns 0 if permission is granted.
3317 int security_task_getpgid(struct task_struct *p)
3319 return call_int_hook(task_getpgid, 0, p);
3323 * security_task_getsid() - Check if getting the session id is allowed
3326 * Check permission before getting the session identifier of the process @p.
3328 * Return: Returns 0 if permission is granted.
3330 int security_task_getsid(struct task_struct *p)
3332 return call_int_hook(task_getsid, 0, p);
3336 * security_current_getsecid_subj() - Get the current task's subjective secid
3337 * @secid: secid value
3339 * Retrieve the subjective security identifier of the current task and return
3340 * it in @secid. In case of failure, @secid will be set to zero.
3342 void security_current_getsecid_subj(u32 *secid)
3345 call_void_hook(current_getsecid_subj, secid);
3347 EXPORT_SYMBOL(security_current_getsecid_subj);
3350 * security_task_getsecid_obj() - Get a task's objective secid
3352 * @secid: secid value
3354 * Retrieve the objective security identifier of the task_struct in @p and
3355 * return it in @secid. In case of failure, @secid will be set to zero.
3357 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3360 call_void_hook(task_getsecid_obj, p, secid);
3362 EXPORT_SYMBOL(security_task_getsecid_obj);
3365 * security_task_setnice() - Check if setting a task's nice value is allowed
3369 * Check permission before setting the nice value of @p to @nice.
3371 * Return: Returns 0 if permission is granted.
3373 int security_task_setnice(struct task_struct *p, int nice)
3375 return call_int_hook(task_setnice, 0, p, nice);
3379 * security_task_setioprio() - Check if setting a task's ioprio is allowed
3381 * @ioprio: ioprio value
3383 * Check permission before setting the ioprio value of @p to @ioprio.
3385 * Return: Returns 0 if permission is granted.
3387 int security_task_setioprio(struct task_struct *p, int ioprio)
3389 return call_int_hook(task_setioprio, 0, p, ioprio);
3393 * security_task_getioprio() - Check if getting a task's ioprio is allowed
3396 * Check permission before getting the ioprio value of @p.
3398 * Return: Returns 0 if permission is granted.
3400 int security_task_getioprio(struct task_struct *p)
3402 return call_int_hook(task_getioprio, 0, p);
3406 * security_task_prlimit() - Check if get/setting resources limits is allowed
3407 * @cred: current task credentials
3408 * @tcred: target task credentials
3409 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3411 * Check permission before getting and/or setting the resource limits of
3414 * Return: Returns 0 if permission is granted.
3416 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3419 return call_int_hook(task_prlimit, 0, cred, tcred, flags);
3423 * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3424 * @p: target task's group leader
3425 * @resource: resource whose limit is being set
3426 * @new_rlim: new resource limit
3428 * Check permission before setting the resource limits of process @p for
3429 * @resource to @new_rlim. The old resource limit values can be examined by
3430 * dereferencing (p->signal->rlim + resource).
3432 * Return: Returns 0 if permission is granted.
3434 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3435 struct rlimit *new_rlim)
3437 return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
3441 * security_task_setscheduler() - Check if setting sched policy/param is allowed
3444 * Check permission before setting scheduling policy and/or parameters of
3447 * Return: Returns 0 if permission is granted.
3449 int security_task_setscheduler(struct task_struct *p)
3451 return call_int_hook(task_setscheduler, 0, p);
3455 * security_task_getscheduler() - Check if getting scheduling info is allowed
3458 * Check permission before obtaining scheduling information for process @p.
3460 * Return: Returns 0 if permission is granted.
3462 int security_task_getscheduler(struct task_struct *p)
3464 return call_int_hook(task_getscheduler, 0, p);
3468 * security_task_movememory() - Check if moving memory is allowed
3471 * Check permission before moving memory owned by process @p.
3473 * Return: Returns 0 if permission is granted.
3475 int security_task_movememory(struct task_struct *p)
3477 return call_int_hook(task_movememory, 0, p);
3481 * security_task_kill() - Check if sending a signal is allowed
3482 * @p: target process
3483 * @info: signal information
3484 * @sig: signal value
3485 * @cred: credentials of the signal sender, NULL if @current
3487 * Check permission before sending signal @sig to @p. @info can be NULL, the
3488 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
3489 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3490 * the kernel and should typically be permitted. SIGIO signals are handled
3491 * separately by the send_sigiotask hook in file_security_ops.
3493 * Return: Returns 0 if permission is granted.
3495 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3496 int sig, const struct cred *cred)
3498 return call_int_hook(task_kill, 0, p, info, sig, cred);
3502 * security_task_prctl() - Check if a prctl op is allowed
3503 * @option: operation
3509 * Check permission before performing a process control operation on the
3512 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3513 * to cause prctl() to return immediately with that value.
3515 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3516 unsigned long arg4, unsigned long arg5)
3519 int rc = LSM_RET_DEFAULT(task_prctl);
3520 struct security_hook_list *hp;
3522 hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
3523 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
3524 if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3534 * security_task_to_inode() - Set the security attributes of a task's inode
3538 * Set the security attributes for an inode based on an associated task's
3539 * security attributes, e.g. for /proc/pid inodes.
3541 void security_task_to_inode(struct task_struct *p, struct inode *inode)
3543 call_void_hook(task_to_inode, p, inode);
3547 * security_create_user_ns() - Check if creating a new userns is allowed
3548 * @cred: prepared creds
3550 * Check permission prior to creating a new user namespace.
3552 * Return: Returns 0 if successful, otherwise < 0 error code.
3554 int security_create_user_ns(const struct cred *cred)
3556 return call_int_hook(userns_create, 0, cred);
3560 * security_ipc_permission() - Check if sysv ipc access is allowed
3561 * @ipcp: ipc permission structure
3562 * @flag: requested permissions
3564 * Check permissions for access to IPC.
3566 * Return: Returns 0 if permission is granted.
3568 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3570 return call_int_hook(ipc_permission, 0, ipcp, flag);
3574 * security_ipc_getsecid() - Get the sysv ipc object's secid
3575 * @ipcp: ipc permission structure
3576 * @secid: secid pointer
3578 * Get the secid associated with the ipc object. In case of failure, @secid
3579 * will be set to zero.
3581 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3584 call_void_hook(ipc_getsecid, ipcp, secid);
3588 * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3589 * @msg: message structure
3591 * Allocate and attach a security structure to the msg->security field. The
3592 * security field is initialized to NULL when the structure is first created.
3594 * Return: Return 0 if operation was successful and permission is granted.
3596 int security_msg_msg_alloc(struct msg_msg *msg)
3598 int rc = lsm_msg_msg_alloc(msg);
3602 rc = call_int_hook(msg_msg_alloc_security, 0, msg);
3604 security_msg_msg_free(msg);
3609 * security_msg_msg_free() - Free a sysv ipc message LSM blob
3610 * @msg: message structure
3612 * Deallocate the security structure for this message.
3614 void security_msg_msg_free(struct msg_msg *msg)
3616 call_void_hook(msg_msg_free_security, msg);
3617 kfree(msg->security);
3618 msg->security = NULL;
3622 * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3623 * @msq: sysv ipc permission structure
3625 * Allocate and attach a security structure to @msg. The security field is
3626 * initialized to NULL when the structure is first created.
3628 * Return: Returns 0 if operation was successful and permission is granted.
3630 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
3632 int rc = lsm_ipc_alloc(msq);
3636 rc = call_int_hook(msg_queue_alloc_security, 0, msq);
3638 security_msg_queue_free(msq);
3643 * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3644 * @msq: sysv ipc permission structure
3646 * Deallocate security field @perm->security for the message queue.
3648 void security_msg_queue_free(struct kern_ipc_perm *msq)
3650 call_void_hook(msg_queue_free_security, msq);
3651 kfree(msq->security);
3652 msq->security = NULL;
3656 * security_msg_queue_associate() - Check if a msg queue operation is allowed
3657 * @msq: sysv ipc permission structure
3658 * @msqflg: operation flags
3660 * Check permission when a message queue is requested through the msgget system
3661 * call. This hook is only called when returning the message queue identifier
3662 * for an existing message queue, not when a new message queue is created.
3664 * Return: Return 0 if permission is granted.
3666 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
3668 return call_int_hook(msg_queue_associate, 0, msq, msqflg);
3672 * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3673 * @msq: sysv ipc permission structure
3676 * Check permission when a message control operation specified by @cmd is to be
3677 * performed on the message queue with permissions.
3679 * Return: Returns 0 if permission is granted.
3681 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
3683 return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
3687 * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3688 * @msq: sysv ipc permission structure
3690 * @msqflg: operation flags
3692 * Check permission before a message, @msg, is enqueued on the message queue
3693 * with permissions specified in @msq.
3695 * Return: Returns 0 if permission is granted.
3697 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
3698 struct msg_msg *msg, int msqflg)
3700 return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
3704 * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3705 * @msq: sysv ipc permission structure
3707 * @target: target task
3708 * @type: type of message requested
3709 * @mode: operation flags
3711 * Check permission before a message, @msg, is removed from the message queue.
3712 * The @target task structure contains a pointer to the process that will be
3713 * receiving the message (not equal to the current process when inline receives
3714 * are being performed).
3716 * Return: Returns 0 if permission is granted.
3718 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
3719 struct task_struct *target, long type, int mode)
3721 return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
3725 * security_shm_alloc() - Allocate a sysv shm LSM blob
3726 * @shp: sysv ipc permission structure
3728 * Allocate and attach a security structure to the @shp security field. The
3729 * security field is initialized to NULL when the structure is first created.
3731 * Return: Returns 0 if operation was successful and permission is granted.
3733 int security_shm_alloc(struct kern_ipc_perm *shp)
3735 int rc = lsm_ipc_alloc(shp);
3739 rc = call_int_hook(shm_alloc_security, 0, shp);
3741 security_shm_free(shp);
3746 * security_shm_free() - Free a sysv shm LSM blob
3747 * @shp: sysv ipc permission structure
3749 * Deallocate the security structure @perm->security for the memory segment.
3751 void security_shm_free(struct kern_ipc_perm *shp)
3753 call_void_hook(shm_free_security, shp);
3754 kfree(shp->security);
3755 shp->security = NULL;
3759 * security_shm_associate() - Check if a sysv shm operation is allowed
3760 * @shp: sysv ipc permission structure
3761 * @shmflg: operation flags
3763 * Check permission when a shared memory region is requested through the shmget
3764 * system call. This hook is only called when returning the shared memory
3765 * region identifier for an existing region, not when a new shared memory
3766 * region is created.
3768 * Return: Returns 0 if permission is granted.
3770 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
3772 return call_int_hook(shm_associate, 0, shp, shmflg);
3776 * security_shm_shmctl() - Check if a sysv shm operation is allowed
3777 * @shp: sysv ipc permission structure
3780 * Check permission when a shared memory control operation specified by @cmd is
3781 * to be performed on the shared memory region with permissions in @shp.
3783 * Return: Return 0 if permission is granted.
3785 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
3787 return call_int_hook(shm_shmctl, 0, shp, cmd);
3791 * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3792 * @shp: sysv ipc permission structure
3793 * @shmaddr: address of memory region to attach
3794 * @shmflg: operation flags
3796 * Check permissions prior to allowing the shmat system call to attach the
3797 * shared memory segment with permissions @shp to the data segment of the
3798 * calling process. The attaching address is specified by @shmaddr.
3800 * Return: Returns 0 if permission is granted.
3802 int security_shm_shmat(struct kern_ipc_perm *shp,
3803 char __user *shmaddr, int shmflg)
3805 return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
3809 * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3810 * @sma: sysv ipc permission structure
3812 * Allocate and attach a security structure to the @sma security field. The
3813 * security field is initialized to NULL when the structure is first created.
3815 * Return: Returns 0 if operation was successful and permission is granted.
3817 int security_sem_alloc(struct kern_ipc_perm *sma)
3819 int rc = lsm_ipc_alloc(sma);
3823 rc = call_int_hook(sem_alloc_security, 0, sma);
3825 security_sem_free(sma);
3830 * security_sem_free() - Free a sysv semaphore LSM blob
3831 * @sma: sysv ipc permission structure
3833 * Deallocate security structure @sma->security for the semaphore.
3835 void security_sem_free(struct kern_ipc_perm *sma)
3837 call_void_hook(sem_free_security, sma);
3838 kfree(sma->security);
3839 sma->security = NULL;
3843 * security_sem_associate() - Check if a sysv semaphore operation is allowed
3844 * @sma: sysv ipc permission structure
3845 * @semflg: operation flags
3847 * Check permission when a semaphore is requested through the semget system
3848 * call. This hook is only called when returning the semaphore identifier for
3849 * an existing semaphore, not when a new one must be created.
3851 * Return: Returns 0 if permission is granted.
3853 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
3855 return call_int_hook(sem_associate, 0, sma, semflg);
3859 * security_sem_semctl() - Check if a sysv semaphore operation is allowed
3860 * @sma: sysv ipc permission structure
3863 * Check permission when a semaphore operation specified by @cmd is to be
3864 * performed on the semaphore.
3866 * Return: Returns 0 if permission is granted.
3868 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
3870 return call_int_hook(sem_semctl, 0, sma, cmd);
3874 * security_sem_semop() - Check if a sysv semaphore operation is allowed
3875 * @sma: sysv ipc permission structure
3876 * @sops: operations to perform
3877 * @nsops: number of operations
3878 * @alter: flag indicating changes will be made
3880 * Check permissions before performing operations on members of the semaphore
3881 * set. If the @alter flag is nonzero, the semaphore set may be modified.
3883 * Return: Returns 0 if permission is granted.
3885 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
3886 unsigned nsops, int alter)
3888 return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
3892 * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3896 * Fill in @inode security information for a @dentry if allowed.
3898 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3900 if (unlikely(inode && IS_PRIVATE(inode)))
3902 call_void_hook(d_instantiate, dentry, inode);
3904 EXPORT_SYMBOL(security_d_instantiate);
3907 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
3911 * security_getselfattr - Read an LSM attribute of the current process.
3912 * @attr: which attribute to return
3913 * @uctx: the user-space destination for the information, or NULL
3914 * @size: pointer to the size of space available to receive the data
3915 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
3916 * attributes associated with the LSM identified in the passed @ctx be
3919 * A NULL value for @uctx can be used to get both the number of attributes
3920 * and the size of the data.
3922 * Returns the number of attributes found on success, negative value
3923 * on error. @size is reset to the total size of the data.
3924 * If @size is insufficient to contain the data -E2BIG is returned.
3926 int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
3927 u32 __user *size, u32 flags)
3929 struct security_hook_list *hp;
3930 struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, };
3931 u8 __user *base = (u8 __user *)uctx;
3935 bool toobig = false;
3936 bool single = false;
3940 if (attr == LSM_ATTR_UNDEF)
3944 if (get_user(left, size))
3949 * Only flag supported is LSM_FLAG_SINGLE
3951 if (flags != LSM_FLAG_SINGLE || !uctx)
3953 if (copy_from_user(&lctx, uctx, sizeof(lctx)))
3956 * If the LSM ID isn't specified it is an error.
3958 if (lctx.id == LSM_ID_UNDEF)
3964 * In the usual case gather all the data from the LSMs.
3965 * In the single case only get the data from the LSM specified.
3967 hlist_for_each_entry(hp, &security_hook_heads.getselfattr, list) {
3968 if (single && lctx.id != hp->lsmid->id)
3972 uctx = (struct lsm_ctx __user *)(base + total);
3973 rc = hp->hook.getselfattr(attr, uctx, &entrysize, flags);
3974 if (rc == -EOPNOTSUPP) {
3992 if (put_user(total, size))
3997 return LSM_RET_DEFAULT(getselfattr);
4002 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
4006 * security_setselfattr - Set an LSM attribute on the current process.
4007 * @attr: which attribute to set
4008 * @uctx: the user-space source for the information
4009 * @size: the size of the data
4010 * @flags: reserved for future use, must be 0
4012 * Set an LSM attribute for the current process. The LSM, attribute
4013 * and new value are included in @uctx.
4015 * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT
4016 * if the user buffer is inaccessible, E2BIG if size is too big, or an
4017 * LSM specific failure.
4019 int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
4020 u32 size, u32 flags)
4022 struct security_hook_list *hp;
4023 struct lsm_ctx *lctx;
4024 int rc = LSM_RET_DEFAULT(setselfattr);
4029 if (size < sizeof(*lctx))
4031 if (size > PAGE_SIZE)
4034 lctx = memdup_user(uctx, size);
4036 return PTR_ERR(lctx);
4038 if (size < lctx->len ||
4039 check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) ||
4040 lctx->len < required_len) {
4045 hlist_for_each_entry(hp, &security_hook_heads.setselfattr, list)
4046 if ((hp->lsmid->id) == lctx->id) {
4047 rc = hp->hook.setselfattr(attr, lctx, size, flags);
4057 * security_getprocattr() - Read an attribute for a task
4059 * @lsmid: LSM identification
4060 * @name: attribute name
4061 * @value: attribute value
4063 * Read attribute @name for task @p and store it into @value if allowed.
4065 * Return: Returns the length of @value on success, a negative value otherwise.
4067 int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
4070 struct security_hook_list *hp;
4072 hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
4073 if (lsmid != 0 && lsmid != hp->lsmid->id)
4075 return hp->hook.getprocattr(p, name, value);
4077 return LSM_RET_DEFAULT(getprocattr);
4081 * security_setprocattr() - Set an attribute for a task
4082 * @lsmid: LSM identification
4083 * @name: attribute name
4084 * @value: attribute value
4085 * @size: attribute value size
4087 * Write (set) the current task's attribute @name to @value, size @size if
4090 * Return: Returns bytes written on success, a negative value otherwise.
4092 int security_setprocattr(int lsmid, const char *name, void *value, size_t size)
4094 struct security_hook_list *hp;
4096 hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
4097 if (lsmid != 0 && lsmid != hp->lsmid->id)
4099 return hp->hook.setprocattr(name, value, size);
4101 return LSM_RET_DEFAULT(setprocattr);
4105 * security_netlink_send() - Save info and check if netlink sending is allowed
4106 * @sk: sending socket
4107 * @skb: netlink message
4109 * Save security information for a netlink message so that permission checking
4110 * can be performed when the message is processed. The security information
4111 * can be saved using the eff_cap field of the netlink_skb_parms structure.
4112 * Also may be used to provide fine grained control over message transmission.
4114 * Return: Returns 0 if the information was successfully saved and message is
4115 * allowed to be transmitted.
4117 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
4119 return call_int_hook(netlink_send, 0, sk, skb);
4123 * security_ismaclabel() - Check is the named attribute is a MAC label
4124 * @name: full extended attribute name
4126 * Check if the extended attribute specified by @name represents a MAC label.
4128 * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
4130 int security_ismaclabel(const char *name)
4132 return call_int_hook(ismaclabel, 0, name);
4134 EXPORT_SYMBOL(security_ismaclabel);
4137 * security_secid_to_secctx() - Convert a secid to a secctx
4140 * @seclen: secctx length
4142 * Convert secid to security context. If @secdata is NULL the length of the
4143 * result will be returned in @seclen, but no @secdata will be returned. This
4144 * does mean that the length could change between calls to check the length and
4145 * the next call which actually allocates and returns the @secdata.
4147 * Return: Return 0 on success, error on failure.
4149 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4151 struct security_hook_list *hp;
4155 * Currently, only one LSM can implement secid_to_secctx (i.e this
4156 * LSM hook is not "stackable").
4158 hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
4159 rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
4160 if (rc != LSM_RET_DEFAULT(secid_to_secctx))
4164 return LSM_RET_DEFAULT(secid_to_secctx);
4166 EXPORT_SYMBOL(security_secid_to_secctx);
4169 * security_secctx_to_secid() - Convert a secctx to a secid
4171 * @seclen: length of secctx
4174 * Convert security context to secid.
4176 * Return: Returns 0 on success, error on failure.
4178 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4181 return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
4183 EXPORT_SYMBOL(security_secctx_to_secid);
4186 * security_release_secctx() - Free a secctx buffer
4188 * @seclen: length of secctx
4190 * Release the security context.
4192 void security_release_secctx(char *secdata, u32 seclen)
4194 call_void_hook(release_secctx, secdata, seclen);
4196 EXPORT_SYMBOL(security_release_secctx);
4199 * security_inode_invalidate_secctx() - Invalidate an inode's security label
4202 * Notify the security module that it must revalidate the security context of
4205 void security_inode_invalidate_secctx(struct inode *inode)
4207 call_void_hook(inode_invalidate_secctx, inode);
4209 EXPORT_SYMBOL(security_inode_invalidate_secctx);
4212 * security_inode_notifysecctx() - Notify the LSM of an inode's security label
4215 * @ctxlen: length of secctx
4217 * Notify the security module of what the security context of an inode should
4218 * be. Initializes the incore security context managed by the security module
4219 * for this inode. Example usage: NFS client invokes this hook to initialize
4220 * the security context in its incore inode to the value provided by the server
4221 * for the file when the server returned the file's attributes to the client.
4222 * Must be called with inode->i_mutex locked.
4224 * Return: Returns 0 on success, error on failure.
4226 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4228 return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
4230 EXPORT_SYMBOL(security_inode_notifysecctx);
4233 * security_inode_setsecctx() - Change the security label of an inode
4236 * @ctxlen: length of secctx
4238 * Change the security context of an inode. Updates the incore security
4239 * context managed by the security module and invokes the fs code as needed
4240 * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
4241 * context. Example usage: NFS server invokes this hook to change the security
4242 * context in its incore inode and on the backing filesystem to a value
4243 * provided by the client on a SETATTR operation. Must be called with
4244 * inode->i_mutex locked.
4246 * Return: Returns 0 on success, error on failure.
4248 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4250 return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
4252 EXPORT_SYMBOL(security_inode_setsecctx);
4255 * security_inode_getsecctx() - Get the security label of an inode
4258 * @ctxlen: length of secctx
4260 * On success, returns 0 and fills out @ctx and @ctxlen with the security
4261 * context for the given @inode.
4263 * Return: Returns 0 on success, error on failure.
4265 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4267 struct security_hook_list *hp;
4271 * Only one module will provide a security context.
4273 hlist_for_each_entry(hp, &security_hook_heads.inode_getsecctx, list) {
4274 rc = hp->hook.inode_getsecctx(inode, ctx, ctxlen);
4275 if (rc != LSM_RET_DEFAULT(inode_getsecctx))
4279 return LSM_RET_DEFAULT(inode_getsecctx);
4281 EXPORT_SYMBOL(security_inode_getsecctx);
4283 #ifdef CONFIG_WATCH_QUEUE
4285 * security_post_notification() - Check if a watch notification can be posted
4286 * @w_cred: credentials of the task that set the watch
4287 * @cred: credentials of the task which triggered the watch
4288 * @n: the notification
4290 * Check to see if a watch notification can be posted to a particular queue.
4292 * Return: Returns 0 if permission is granted.
4294 int security_post_notification(const struct cred *w_cred,
4295 const struct cred *cred,
4296 struct watch_notification *n)
4298 return call_int_hook(post_notification, 0, w_cred, cred, n);
4300 #endif /* CONFIG_WATCH_QUEUE */
4302 #ifdef CONFIG_KEY_NOTIFICATIONS
4304 * security_watch_key() - Check if a task is allowed to watch for key events
4305 * @key: the key to watch
4307 * Check to see if a process is allowed to watch for event notifications from
4310 * Return: Returns 0 if permission is granted.
4312 int security_watch_key(struct key *key)
4314 return call_int_hook(watch_key, 0, key);
4316 #endif /* CONFIG_KEY_NOTIFICATIONS */
4318 #ifdef CONFIG_SECURITY_NETWORK
4320 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
4321 * @sock: originating sock
4325 * Check permissions before establishing a Unix domain stream connection
4326 * between @sock and @other.
4328 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4329 * Linux provides an alternative to the conventional file name space for Unix
4330 * domain sockets. Whereas binding and connecting to sockets in the file name
4331 * space is mediated by the typical file permissions (and caught by the mknod
4332 * and permission hooks in inode_security_ops), binding and connecting to
4333 * sockets in the abstract name space is completely unmediated. Sufficient
4334 * control of Unix domain sockets in the abstract name space isn't possible
4335 * using only the socket layer hooks, since we need to know the actual target
4336 * socket, which is not looked up until we are inside the af_unix code.
4338 * Return: Returns 0 if permission is granted.
4340 int security_unix_stream_connect(struct sock *sock, struct sock *other,
4343 return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
4345 EXPORT_SYMBOL(security_unix_stream_connect);
4348 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
4349 * @sock: originating sock
4352 * Check permissions before connecting or sending datagrams from @sock to
4355 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4356 * Linux provides an alternative to the conventional file name space for Unix
4357 * domain sockets. Whereas binding and connecting to sockets in the file name
4358 * space is mediated by the typical file permissions (and caught by the mknod
4359 * and permission hooks in inode_security_ops), binding and connecting to
4360 * sockets in the abstract name space is completely unmediated. Sufficient
4361 * control of Unix domain sockets in the abstract name space isn't possible
4362 * using only the socket layer hooks, since we need to know the actual target
4363 * socket, which is not looked up until we are inside the af_unix code.
4365 * Return: Returns 0 if permission is granted.
4367 int security_unix_may_send(struct socket *sock, struct socket *other)
4369 return call_int_hook(unix_may_send, 0, sock, other);
4371 EXPORT_SYMBOL(security_unix_may_send);
4374 * security_socket_create() - Check if creating a new socket is allowed
4375 * @family: protocol family
4376 * @type: communications type
4377 * @protocol: requested protocol
4378 * @kern: set to 1 if a kernel socket is requested
4380 * Check permissions prior to creating a new socket.
4382 * Return: Returns 0 if permission is granted.
4384 int security_socket_create(int family, int type, int protocol, int kern)
4386 return call_int_hook(socket_create, 0, family, type, protocol, kern);
4390 * security_socket_post_create() - Initialize a newly created socket
4392 * @family: protocol family
4393 * @type: communications type
4394 * @protocol: requested protocol
4395 * @kern: set to 1 if a kernel socket is requested
4397 * This hook allows a module to update or allocate a per-socket security
4398 * structure. Note that the security field was not added directly to the socket
4399 * structure, but rather, the socket security information is stored in the
4400 * associated inode. Typically, the inode alloc_security hook will allocate
4401 * and attach security information to SOCK_INODE(sock)->i_security. This hook
4402 * may be used to update the SOCK_INODE(sock)->i_security field with additional
4403 * information that wasn't available when the inode was allocated.
4405 * Return: Returns 0 if permission is granted.
4407 int security_socket_post_create(struct socket *sock, int family,
4408 int type, int protocol, int kern)
4410 return call_int_hook(socket_post_create, 0, sock, family, type,
4415 * security_socket_socketpair() - Check if creating a socketpair is allowed
4416 * @socka: first socket
4417 * @sockb: second socket
4419 * Check permissions before creating a fresh pair of sockets.
4421 * Return: Returns 0 if permission is granted and the connection was
4424 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4426 return call_int_hook(socket_socketpair, 0, socka, sockb);
4428 EXPORT_SYMBOL(security_socket_socketpair);
4431 * security_socket_bind() - Check if a socket bind operation is allowed
4433 * @address: requested bind address
4434 * @addrlen: length of address
4436 * Check permission before socket protocol layer bind operation is performed
4437 * and the socket @sock is bound to the address specified in the @address
4440 * Return: Returns 0 if permission is granted.
4442 int security_socket_bind(struct socket *sock,
4443 struct sockaddr *address, int addrlen)
4445 return call_int_hook(socket_bind, 0, sock, address, addrlen);
4449 * security_socket_connect() - Check if a socket connect operation is allowed
4451 * @address: address of remote connection point
4452 * @addrlen: length of address
4454 * Check permission before socket protocol layer connect operation attempts to
4455 * connect socket @sock to a remote address, @address.
4457 * Return: Returns 0 if permission is granted.
4459 int security_socket_connect(struct socket *sock,
4460 struct sockaddr *address, int addrlen)
4462 return call_int_hook(socket_connect, 0, sock, address, addrlen);
4466 * security_socket_listen() - Check if a socket is allowed to listen
4468 * @backlog: connection queue size
4470 * Check permission before socket protocol layer listen operation.
4472 * Return: Returns 0 if permission is granted.
4474 int security_socket_listen(struct socket *sock, int backlog)
4476 return call_int_hook(socket_listen, 0, sock, backlog);
4480 * security_socket_accept() - Check if a socket is allowed to accept connections
4481 * @sock: listening socket
4482 * @newsock: newly creation connection socket
4484 * Check permission before accepting a new connection. Note that the new
4485 * socket, @newsock, has been created and some information copied to it, but
4486 * the accept operation has not actually been performed.
4488 * Return: Returns 0 if permission is granted.
4490 int security_socket_accept(struct socket *sock, struct socket *newsock)
4492 return call_int_hook(socket_accept, 0, sock, newsock);
4496 * security_socket_sendmsg() - Check is sending a message is allowed
4497 * @sock: sending socket
4498 * @msg: message to send
4499 * @size: size of message
4501 * Check permission before transmitting a message to another socket.
4503 * Return: Returns 0 if permission is granted.
4505 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
4507 return call_int_hook(socket_sendmsg, 0, sock, msg, size);
4511 * security_socket_recvmsg() - Check if receiving a message is allowed
4512 * @sock: receiving socket
4513 * @msg: message to receive
4514 * @size: size of message
4515 * @flags: operational flags
4517 * Check permission before receiving a message from a socket.
4519 * Return: Returns 0 if permission is granted.
4521 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4522 int size, int flags)
4524 return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
4528 * security_socket_getsockname() - Check if reading the socket addr is allowed
4531 * Check permission before reading the local address (name) of the socket
4534 * Return: Returns 0 if permission is granted.
4536 int security_socket_getsockname(struct socket *sock)
4538 return call_int_hook(socket_getsockname, 0, sock);
4542 * security_socket_getpeername() - Check if reading the peer's addr is allowed
4545 * Check permission before the remote address (name) of a socket object.
4547 * Return: Returns 0 if permission is granted.
4549 int security_socket_getpeername(struct socket *sock)
4551 return call_int_hook(socket_getpeername, 0, sock);
4555 * security_socket_getsockopt() - Check if reading a socket option is allowed
4557 * @level: option's protocol level
4558 * @optname: option name
4560 * Check permissions before retrieving the options associated with socket
4563 * Return: Returns 0 if permission is granted.
4565 int security_socket_getsockopt(struct socket *sock, int level, int optname)
4567 return call_int_hook(socket_getsockopt, 0, sock, level, optname);
4571 * security_socket_setsockopt() - Check if setting a socket option is allowed
4573 * @level: option's protocol level
4574 * @optname: option name
4576 * Check permissions before setting the options associated with socket @sock.
4578 * Return: Returns 0 if permission is granted.
4580 int security_socket_setsockopt(struct socket *sock, int level, int optname)
4582 return call_int_hook(socket_setsockopt, 0, sock, level, optname);
4586 * security_socket_shutdown() - Checks if shutting down the socket is allowed
4588 * @how: flag indicating how sends and receives are handled
4590 * Checks permission before all or part of a connection on the socket @sock is
4593 * Return: Returns 0 if permission is granted.
4595 int security_socket_shutdown(struct socket *sock, int how)
4597 return call_int_hook(socket_shutdown, 0, sock, how);
4601 * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4602 * @sk: destination sock
4603 * @skb: incoming packet
4605 * Check permissions on incoming network packets. This hook is distinct from
4606 * Netfilter's IP input hooks since it is the first time that the incoming
4607 * sk_buff @skb has been associated with a particular socket, @sk. Must not
4608 * sleep inside this hook because some callers hold spinlocks.
4610 * Return: Returns 0 if permission is granted.
4612 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4614 return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
4616 EXPORT_SYMBOL(security_sock_rcv_skb);
4619 * security_socket_getpeersec_stream() - Get the remote peer label
4621 * @optval: destination buffer
4622 * @optlen: size of peer label copied into the buffer
4623 * @len: maximum size of the destination buffer
4625 * This hook allows the security module to provide peer socket security state
4626 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4627 * For tcp sockets this can be meaningful if the socket is associated with an
4630 * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4633 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4634 sockptr_t optlen, unsigned int len)
4636 struct security_hook_list *hp;
4640 * Only one module will provide a security context.
4642 hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_stream,
4644 rc = hp->hook.socket_getpeersec_stream(sock, optval, optlen,
4646 if (rc != LSM_RET_DEFAULT(socket_getpeersec_stream))
4649 return LSM_RET_DEFAULT(socket_getpeersec_stream);
4653 * security_socket_getpeersec_dgram() - Get the remote peer label
4655 * @skb: datagram packet
4656 * @secid: remote peer label secid
4658 * This hook allows the security module to provide peer socket security state
4659 * for udp sockets on a per-packet basis to userspace via getsockopt
4660 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4661 * option via getsockopt. It can then retrieve the security state returned by
4662 * this hook for a packet via the SCM_SECURITY ancillary message type.
4664 * Return: Returns 0 on success, error on failure.
4666 int security_socket_getpeersec_dgram(struct socket *sock,
4667 struct sk_buff *skb, u32 *secid)
4669 struct security_hook_list *hp;
4673 * Only one module will provide a security context.
4675 hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_dgram,
4677 rc = hp->hook.socket_getpeersec_dgram(sock, skb, secid);
4678 if (rc != LSM_RET_DEFAULT(socket_getpeersec_dgram))
4681 return LSM_RET_DEFAULT(socket_getpeersec_dgram);
4683 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4686 * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4688 * @family: protocol family
4689 * @priority: gfp flags
4691 * Allocate and attach a security structure to the sk->sk_security field, which
4692 * is used to copy security attributes between local stream sockets.
4694 * Return: Returns 0 on success, error on failure.
4696 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4698 return call_int_hook(sk_alloc_security, 0, sk, family, priority);
4702 * security_sk_free() - Free the sock's LSM blob
4705 * Deallocate security structure.
4707 void security_sk_free(struct sock *sk)
4709 call_void_hook(sk_free_security, sk);
4713 * security_sk_clone() - Clone a sock's LSM state
4714 * @sk: original sock
4715 * @newsk: target sock
4717 * Clone/copy security structure.
4719 void security_sk_clone(const struct sock *sk, struct sock *newsk)
4721 call_void_hook(sk_clone_security, sk, newsk);
4723 EXPORT_SYMBOL(security_sk_clone);
4726 * security_sk_classify_flow() - Set a flow's secid based on socket
4727 * @sk: original socket
4728 * @flic: target flow
4730 * Set the target flow's secid to socket's secid.
4732 void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
4734 call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
4736 EXPORT_SYMBOL(security_sk_classify_flow);
4739 * security_req_classify_flow() - Set a flow's secid based on request_sock
4740 * @req: request_sock
4741 * @flic: target flow
4743 * Sets @flic's secid to @req's secid.
4745 void security_req_classify_flow(const struct request_sock *req,
4746 struct flowi_common *flic)
4748 call_void_hook(req_classify_flow, req, flic);
4750 EXPORT_SYMBOL(security_req_classify_flow);
4753 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4754 * @sk: sock being grafted
4755 * @parent: target parent socket
4757 * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
4758 * LSM state from @parent.
4760 void security_sock_graft(struct sock *sk, struct socket *parent)
4762 call_void_hook(sock_graft, sk, parent);
4764 EXPORT_SYMBOL(security_sock_graft);
4767 * security_inet_conn_request() - Set request_sock state using incoming connect
4768 * @sk: parent listening sock
4769 * @skb: incoming connection
4770 * @req: new request_sock
4772 * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4774 * Return: Returns 0 if permission is granted.
4776 int security_inet_conn_request(const struct sock *sk,
4777 struct sk_buff *skb, struct request_sock *req)
4779 return call_int_hook(inet_conn_request, 0, sk, skb, req);
4781 EXPORT_SYMBOL(security_inet_conn_request);
4784 * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4786 * @req: connection request_sock
4788 * Set that LSM state of @sock using the LSM state from @req.
4790 void security_inet_csk_clone(struct sock *newsk,
4791 const struct request_sock *req)
4793 call_void_hook(inet_csk_clone, newsk, req);
4797 * security_inet_conn_established() - Update sock's LSM state with connection
4799 * @skb: connection packet
4801 * Update @sock's LSM state to represent a new connection from @skb.
4803 void security_inet_conn_established(struct sock *sk,
4804 struct sk_buff *skb)
4806 call_void_hook(inet_conn_established, sk, skb);
4808 EXPORT_SYMBOL(security_inet_conn_established);
4811 * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4812 * @secid: new secmark value
4814 * Check if the process should be allowed to relabel packets to @secid.
4816 * Return: Returns 0 if permission is granted.
4818 int security_secmark_relabel_packet(u32 secid)
4820 return call_int_hook(secmark_relabel_packet, 0, secid);
4822 EXPORT_SYMBOL(security_secmark_relabel_packet);
4825 * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4827 * Tells the LSM to increment the number of secmark labeling rules loaded.
4829 void security_secmark_refcount_inc(void)
4831 call_void_hook(secmark_refcount_inc);
4833 EXPORT_SYMBOL(security_secmark_refcount_inc);
4836 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4838 * Tells the LSM to decrement the number of secmark labeling rules loaded.
4840 void security_secmark_refcount_dec(void)
4842 call_void_hook(secmark_refcount_dec);
4844 EXPORT_SYMBOL(security_secmark_refcount_dec);
4847 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4848 * @security: pointer to the LSM blob
4850 * This hook allows a module to allocate a security structure for a TUN device,
4851 * returning the pointer in @security.
4853 * Return: Returns a zero on success, negative values on failure.
4855 int security_tun_dev_alloc_security(void **security)
4857 return call_int_hook(tun_dev_alloc_security, 0, security);
4859 EXPORT_SYMBOL(security_tun_dev_alloc_security);
4862 * security_tun_dev_free_security() - Free a TUN device LSM blob
4863 * @security: LSM blob
4865 * This hook allows a module to free the security structure for a TUN device.
4867 void security_tun_dev_free_security(void *security)
4869 call_void_hook(tun_dev_free_security, security);
4871 EXPORT_SYMBOL(security_tun_dev_free_security);
4874 * security_tun_dev_create() - Check if creating a TUN device is allowed
4876 * Check permissions prior to creating a new TUN device.
4878 * Return: Returns 0 if permission is granted.
4880 int security_tun_dev_create(void)
4882 return call_int_hook(tun_dev_create, 0);
4884 EXPORT_SYMBOL(security_tun_dev_create);
4887 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4888 * @security: TUN device LSM blob
4890 * Check permissions prior to attaching to a TUN device queue.
4892 * Return: Returns 0 if permission is granted.
4894 int security_tun_dev_attach_queue(void *security)
4896 return call_int_hook(tun_dev_attach_queue, 0, security);
4898 EXPORT_SYMBOL(security_tun_dev_attach_queue);
4901 * security_tun_dev_attach() - Update TUN device LSM state on attach
4902 * @sk: associated sock
4903 * @security: TUN device LSM blob
4905 * This hook can be used by the module to update any security state associated
4906 * with the TUN device's sock structure.
4908 * Return: Returns 0 if permission is granted.
4910 int security_tun_dev_attach(struct sock *sk, void *security)
4912 return call_int_hook(tun_dev_attach, 0, sk, security);
4914 EXPORT_SYMBOL(security_tun_dev_attach);
4917 * security_tun_dev_open() - Update TUN device LSM state on open
4918 * @security: TUN device LSM blob
4920 * This hook can be used by the module to update any security state associated
4921 * with the TUN device's security structure.
4923 * Return: Returns 0 if permission is granted.
4925 int security_tun_dev_open(void *security)
4927 return call_int_hook(tun_dev_open, 0, security);
4929 EXPORT_SYMBOL(security_tun_dev_open);
4932 * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4933 * @asoc: SCTP association
4934 * @skb: packet requesting the association
4936 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4938 * Return: Returns 0 on success, error on failure.
4940 int security_sctp_assoc_request(struct sctp_association *asoc,
4941 struct sk_buff *skb)
4943 return call_int_hook(sctp_assoc_request, 0, asoc, skb);
4945 EXPORT_SYMBOL(security_sctp_assoc_request);
4948 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4950 * @optname: SCTP option to validate
4951 * @address: list of IP addresses to validate
4952 * @addrlen: length of the address list
4954 * Validiate permissions required for each address associated with sock @sk.
4955 * Depending on @optname, the addresses will be treated as either a connect or
4956 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4957 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4959 * Return: Returns 0 on success, error on failure.
4961 int security_sctp_bind_connect(struct sock *sk, int optname,
4962 struct sockaddr *address, int addrlen)
4964 return call_int_hook(sctp_bind_connect, 0, sk, optname,
4967 EXPORT_SYMBOL(security_sctp_bind_connect);
4970 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4971 * @asoc: SCTP association
4972 * @sk: original sock
4973 * @newsk: target sock
4975 * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4976 * socket) or when a socket is 'peeled off' e.g userspace calls
4979 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
4982 call_void_hook(sctp_sk_clone, asoc, sk, newsk);
4984 EXPORT_SYMBOL(security_sctp_sk_clone);
4987 * security_sctp_assoc_established() - Update LSM state when assoc established
4988 * @asoc: SCTP association
4989 * @skb: packet establishing the association
4991 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4994 * Return: Returns 0 if permission is granted.
4996 int security_sctp_assoc_established(struct sctp_association *asoc,
4997 struct sk_buff *skb)
4999 return call_int_hook(sctp_assoc_established, 0, asoc, skb);
5001 EXPORT_SYMBOL(security_sctp_assoc_established);
5004 * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
5005 * @sk: the owning MPTCP socket
5006 * @ssk: the new subflow
5008 * Update the labeling for the given MPTCP subflow, to match the one of the
5009 * owning MPTCP socket. This hook has to be called after the socket creation and
5010 * initialization via the security_socket_create() and
5011 * security_socket_post_create() LSM hooks.
5013 * Return: Returns 0 on success or a negative error code on failure.
5015 int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
5017 return call_int_hook(mptcp_add_subflow, 0, sk, ssk);
5020 #endif /* CONFIG_SECURITY_NETWORK */
5022 #ifdef CONFIG_SECURITY_INFINIBAND
5024 * security_ib_pkey_access() - Check if access to an IB pkey is allowed
5026 * @subnet_prefix: subnet prefix of the port
5029 * Check permission to access a pkey when modifying a QP.
5031 * Return: Returns 0 if permission is granted.
5033 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
5035 return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
5037 EXPORT_SYMBOL(security_ib_pkey_access);
5040 * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
5042 * @dev_name: IB device name
5043 * @port_num: port number
5045 * Check permissions to send and receive SMPs on a end port.
5047 * Return: Returns 0 if permission is granted.
5049 int security_ib_endport_manage_subnet(void *sec,
5050 const char *dev_name, u8 port_num)
5052 return call_int_hook(ib_endport_manage_subnet, 0, sec,
5053 dev_name, port_num);
5055 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
5058 * security_ib_alloc_security() - Allocate an Infiniband LSM blob
5061 * Allocate a security structure for Infiniband objects.
5063 * Return: Returns 0 on success, non-zero on failure.
5065 int security_ib_alloc_security(void **sec)
5067 return call_int_hook(ib_alloc_security, 0, sec);
5069 EXPORT_SYMBOL(security_ib_alloc_security);
5072 * security_ib_free_security() - Free an Infiniband LSM blob
5075 * Deallocate an Infiniband security structure.
5077 void security_ib_free_security(void *sec)
5079 call_void_hook(ib_free_security, sec);
5081 EXPORT_SYMBOL(security_ib_free_security);
5082 #endif /* CONFIG_SECURITY_INFINIBAND */
5084 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5086 * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
5087 * @ctxp: xfrm security context being added to the SPD
5088 * @sec_ctx: security label provided by userspace
5091 * Allocate a security structure to the xp->security field; the security field
5092 * is initialized to NULL when the xfrm_policy is allocated.
5094 * Return: Return 0 if operation was successful.
5096 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
5097 struct xfrm_user_sec_ctx *sec_ctx,
5100 return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
5102 EXPORT_SYMBOL(security_xfrm_policy_alloc);
5105 * security_xfrm_policy_clone() - Clone xfrm policy LSM state
5106 * @old_ctx: xfrm security context
5107 * @new_ctxp: target xfrm security context
5109 * Allocate a security structure in new_ctxp that contains the information from
5110 * the old_ctx structure.
5112 * Return: Return 0 if operation was successful.
5114 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
5115 struct xfrm_sec_ctx **new_ctxp)
5117 return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
5121 * security_xfrm_policy_free() - Free a xfrm security context
5122 * @ctx: xfrm security context
5124 * Free LSM resources associated with @ctx.
5126 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
5128 call_void_hook(xfrm_policy_free_security, ctx);
5130 EXPORT_SYMBOL(security_xfrm_policy_free);
5133 * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
5134 * @ctx: xfrm security context
5136 * Authorize deletion of a SPD entry.
5138 * Return: Returns 0 if permission is granted.
5140 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
5142 return call_int_hook(xfrm_policy_delete_security, 0, ctx);
5146 * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
5147 * @x: xfrm state being added to the SAD
5148 * @sec_ctx: security label provided by userspace
5150 * Allocate a security structure to the @x->security field; the security field
5151 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5152 * correspond to @sec_ctx.
5154 * Return: Return 0 if operation was successful.
5156 int security_xfrm_state_alloc(struct xfrm_state *x,
5157 struct xfrm_user_sec_ctx *sec_ctx)
5159 return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
5161 EXPORT_SYMBOL(security_xfrm_state_alloc);
5164 * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
5165 * @x: xfrm state being added to the SAD
5166 * @polsec: associated policy's security context
5167 * @secid: secid from the flow
5169 * Allocate a security structure to the x->security field; the security field
5170 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5171 * correspond to secid.
5173 * Return: Returns 0 if operation was successful.
5175 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
5176 struct xfrm_sec_ctx *polsec, u32 secid)
5178 return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
5182 * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
5185 * Authorize deletion of x->security.
5187 * Return: Returns 0 if permission is granted.
5189 int security_xfrm_state_delete(struct xfrm_state *x)
5191 return call_int_hook(xfrm_state_delete_security, 0, x);
5193 EXPORT_SYMBOL(security_xfrm_state_delete);
5196 * security_xfrm_state_free() - Free a xfrm state
5199 * Deallocate x->security.
5201 void security_xfrm_state_free(struct xfrm_state *x)
5203 call_void_hook(xfrm_state_free_security, x);
5207 * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
5208 * @ctx: target xfrm security context
5209 * @fl_secid: flow secid used to authorize access
5211 * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
5212 * packet. The hook is called when selecting either a per-socket policy or a
5213 * generic xfrm policy.
5215 * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
5218 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
5220 return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
5224 * security_xfrm_state_pol_flow_match() - Check for a xfrm match
5225 * @x: xfrm state to match
5226 * @xp: xfrm policy to check for a match
5227 * @flic: flow to check for a match.
5229 * Check @xp and @flic for a match with @x.
5231 * Return: Returns 1 if there is a match.
5233 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
5234 struct xfrm_policy *xp,
5235 const struct flowi_common *flic)
5237 struct security_hook_list *hp;
5238 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
5241 * Since this function is expected to return 0 or 1, the judgment
5242 * becomes difficult if multiple LSMs supply this call. Fortunately,
5243 * we can use the first LSM's judgment because currently only SELinux
5244 * supplies this call.
5246 * For speed optimization, we explicitly break the loop rather than
5249 hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
5251 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
5258 * security_xfrm_decode_session() - Determine the xfrm secid for a packet
5262 * Decode the packet in @skb and return the security label in @secid.
5264 * Return: Return 0 if all xfrms used have the same secid.
5266 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
5268 return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
5271 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
5273 int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
5278 EXPORT_SYMBOL(security_skb_classify_flow);
5279 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
5283 * security_key_alloc() - Allocate and initialize a kernel key LSM blob
5285 * @cred: credentials
5286 * @flags: allocation flags
5288 * Permit allocation of a key and assign security data. Note that key does not
5289 * have a serial number assigned at this point.
5291 * Return: Return 0 if permission is granted, -ve error otherwise.
5293 int security_key_alloc(struct key *key, const struct cred *cred,
5294 unsigned long flags)
5296 return call_int_hook(key_alloc, 0, key, cred, flags);
5300 * security_key_free() - Free a kernel key LSM blob
5303 * Notification of destruction; free security data.
5305 void security_key_free(struct key *key)
5307 call_void_hook(key_free, key);
5311 * security_key_permission() - Check if a kernel key operation is allowed
5312 * @key_ref: key reference
5313 * @cred: credentials of actor requesting access
5314 * @need_perm: requested permissions
5316 * See whether a specific operational right is granted to a process on a key.
5318 * Return: Return 0 if permission is granted, -ve error otherwise.
5320 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
5321 enum key_need_perm need_perm)
5323 return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
5327 * security_key_getsecurity() - Get the key's security label
5329 * @buffer: security label buffer
5331 * Get a textual representation of the security context attached to a key for
5332 * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the
5333 * storage for the NUL-terminated string and the caller should free it.
5335 * Return: Returns the length of @buffer (including terminating NUL) or -ve if
5336 * an error occurs. May also return 0 (and a NULL buffer pointer) if
5337 * there is no security label assigned to the key.
5339 int security_key_getsecurity(struct key *key, char **buffer)
5342 return call_int_hook(key_getsecurity, 0, key, buffer);
5344 #endif /* CONFIG_KEYS */
5348 * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5349 * @field: audit action
5350 * @op: rule operator
5351 * @rulestr: rule context
5352 * @lsmrule: receive buffer for audit rule struct
5354 * Allocate and initialize an LSM audit rule structure.
5356 * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5359 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
5361 return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
5365 * security_audit_rule_known() - Check if an audit rule contains LSM fields
5366 * @krule: audit rule
5368 * Specifies whether given @krule contains any fields related to the current
5371 * Return: Returns 1 in case of relation found, 0 otherwise.
5373 int security_audit_rule_known(struct audit_krule *krule)
5375 return call_int_hook(audit_rule_known, 0, krule);
5379 * security_audit_rule_free() - Free an LSM audit rule struct
5380 * @lsmrule: audit rule struct
5382 * Deallocate the LSM audit rule structure previously allocated by
5383 * audit_rule_init().
5385 void security_audit_rule_free(void *lsmrule)
5387 call_void_hook(audit_rule_free, lsmrule);
5391 * security_audit_rule_match() - Check if a label matches an audit rule
5392 * @secid: security label
5393 * @field: LSM audit field
5394 * @op: matching operator
5395 * @lsmrule: audit rule
5397 * Determine if given @secid matches a rule previously approved by
5398 * security_audit_rule_known().
5400 * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5403 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
5405 return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
5407 #endif /* CONFIG_AUDIT */
5409 #ifdef CONFIG_BPF_SYSCALL
5411 * security_bpf() - Check if the bpf syscall operation is allowed
5413 * @attr: bpf attribute
5416 * Do a initial check for all bpf syscalls after the attribute is copied into
5417 * the kernel. The actual security module can implement their own rules to
5418 * check the specific cmd they need.
5420 * Return: Returns 0 if permission is granted.
5422 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5424 return call_int_hook(bpf, 0, cmd, attr, size);
5428 * security_bpf_map() - Check if access to a bpf map is allowed
5432 * Do a check when the kernel generates and returns a file descriptor for eBPF
5435 * Return: Returns 0 if permission is granted.
5437 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5439 return call_int_hook(bpf_map, 0, map, fmode);
5443 * security_bpf_prog() - Check if access to a bpf program is allowed
5444 * @prog: bpf program
5446 * Do a check when the kernel generates and returns a file descriptor for eBPF
5449 * Return: Returns 0 if permission is granted.
5451 int security_bpf_prog(struct bpf_prog *prog)
5453 return call_int_hook(bpf_prog, 0, prog);
5457 * security_bpf_map_alloc() - Allocate a bpf map LSM blob
5460 * Initialize the security field inside bpf map.
5462 * Return: Returns 0 on success, error on failure.
5464 int security_bpf_map_alloc(struct bpf_map *map)
5466 return call_int_hook(bpf_map_alloc_security, 0, map);
5470 * security_bpf_prog_alloc() - Allocate a bpf program LSM blob
5471 * @aux: bpf program aux info struct
5473 * Initialize the security field inside bpf program.
5475 * Return: Returns 0 on success, error on failure.
5477 int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
5479 return call_int_hook(bpf_prog_alloc_security, 0, aux);
5483 * security_bpf_map_free() - Free a bpf map's LSM blob
5486 * Clean up the security information stored inside bpf map.
5488 void security_bpf_map_free(struct bpf_map *map)
5490 call_void_hook(bpf_map_free_security, map);
5494 * security_bpf_prog_free() - Free a bpf program's LSM blob
5495 * @aux: bpf program aux info struct
5497 * Clean up the security information stored inside bpf prog.
5499 void security_bpf_prog_free(struct bpf_prog_aux *aux)
5501 call_void_hook(bpf_prog_free_security, aux);
5503 #endif /* CONFIG_BPF_SYSCALL */
5506 * security_locked_down() - Check if a kernel feature is allowed
5507 * @what: requested kernel feature
5509 * Determine whether a kernel feature that potentially enables arbitrary code
5510 * execution in kernel space should be permitted.
5512 * Return: Returns 0 if permission is granted.
5514 int security_locked_down(enum lockdown_reason what)
5516 return call_int_hook(locked_down, 0, what);
5518 EXPORT_SYMBOL(security_locked_down);
5520 #ifdef CONFIG_PERF_EVENTS
5522 * security_perf_event_open() - Check if a perf event open is allowed
5523 * @attr: perf event attribute
5524 * @type: type of event
5526 * Check whether the @type of perf_event_open syscall is allowed.
5528 * Return: Returns 0 if permission is granted.
5530 int security_perf_event_open(struct perf_event_attr *attr, int type)
5532 return call_int_hook(perf_event_open, 0, attr, type);
5536 * security_perf_event_alloc() - Allocate a perf event LSM blob
5537 * @event: perf event
5539 * Allocate and save perf_event security info.
5541 * Return: Returns 0 on success, error on failure.
5543 int security_perf_event_alloc(struct perf_event *event)
5545 return call_int_hook(perf_event_alloc, 0, event);
5549 * security_perf_event_free() - Free a perf event LSM blob
5550 * @event: perf event
5552 * Release (free) perf_event security info.
5554 void security_perf_event_free(struct perf_event *event)
5556 call_void_hook(perf_event_free, event);
5560 * security_perf_event_read() - Check if reading a perf event label is allowed
5561 * @event: perf event
5563 * Read perf_event security info if allowed.
5565 * Return: Returns 0 if permission is granted.
5567 int security_perf_event_read(struct perf_event *event)
5569 return call_int_hook(perf_event_read, 0, event);
5573 * security_perf_event_write() - Check if writing a perf event label is allowed
5574 * @event: perf event
5576 * Write perf_event security info if allowed.
5578 * Return: Returns 0 if permission is granted.
5580 int security_perf_event_write(struct perf_event *event)
5582 return call_int_hook(perf_event_write, 0, event);
5584 #endif /* CONFIG_PERF_EVENTS */
5586 #ifdef CONFIG_IO_URING
5588 * security_uring_override_creds() - Check if overriding creds is allowed
5589 * @new: new credentials
5591 * Check if the current task, executing an io_uring operation, is allowed to
5592 * override it's credentials with @new.
5594 * Return: Returns 0 if permission is granted.
5596 int security_uring_override_creds(const struct cred *new)
5598 return call_int_hook(uring_override_creds, 0, new);
5602 * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
5604 * Check whether the current task is allowed to spawn a io_uring polling thread
5605 * (IORING_SETUP_SQPOLL).
5607 * Return: Returns 0 if permission is granted.
5609 int security_uring_sqpoll(void)
5611 return call_int_hook(uring_sqpoll, 0);
5615 * security_uring_cmd() - Check if a io_uring passthrough command is allowed
5618 * Check whether the file_operations uring_cmd is allowed to run.
5620 * Return: Returns 0 if permission is granted.
5622 int security_uring_cmd(struct io_uring_cmd *ioucmd)
5624 return call_int_hook(uring_cmd, 0, ioucmd);
5626 #endif /* CONFIG_IO_URING */