1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Security plug functions
8 * Copyright (C) 2016 Mellanox Technologies
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/mman.h>
23 #include <linux/mount.h>
24 #include <linux/personality.h>
25 #include <linux/backing-dev.h>
26 #include <linux/string.h>
27 #include <linux/xattr.h>
28 #include <linux/msg.h>
29 #include <linux/overflow.h>
30 #include <linux/perf_event.h>
35 #define SECURITY_HOOK_ACTIVE_KEY(HOOK, IDX) security_hook_active_##HOOK##_##IDX
38 * Identifier for the LSM static calls.
39 * HOOK is an LSM hook as defined in linux/lsm_hookdefs.h
40 * IDX is the index of the static call. 0 <= NUM < MAX_LSM_COUNT
42 #define LSM_STATIC_CALL(HOOK, IDX) lsm_static_call_##HOOK##_##IDX
45 * Call the macro M for each LSM hook MAX_LSM_COUNT times.
47 #define LSM_LOOP_UNROLL(M, ...) \
49 UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__) \
52 #define LSM_DEFINE_UNROLL(M, ...) UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__)
55 * These are descriptions of the reasons that can be passed to the
56 * security_locked_down() LSM hook. Placing this array here allows
57 * all security modules to use the same descriptions for auditing
60 const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
61 [LOCKDOWN_NONE] = "none",
62 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
63 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
64 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
65 [LOCKDOWN_KEXEC] = "kexec of unsigned images",
66 [LOCKDOWN_HIBERNATION] = "hibernation",
67 [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
68 [LOCKDOWN_IOPORT] = "raw io port access",
69 [LOCKDOWN_MSR] = "raw MSR access",
70 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
71 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
72 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
73 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
74 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
75 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
76 [LOCKDOWN_DEBUGFS] = "debugfs access",
77 [LOCKDOWN_XMON_WR] = "xmon write access",
78 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
79 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
80 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
81 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
82 [LOCKDOWN_KCORE] = "/proc/kcore access",
83 [LOCKDOWN_KPROBES] = "use of kprobes",
84 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
85 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
86 [LOCKDOWN_PERF] = "unsafe use of perf",
87 [LOCKDOWN_TRACEFS] = "use of tracefs",
88 [LOCKDOWN_XMON_RW] = "xmon read and write access",
89 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
90 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
93 static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
95 static struct kmem_cache *lsm_file_cache;
96 static struct kmem_cache *lsm_inode_cache;
99 static struct lsm_blob_sizes blob_sizes __ro_after_init;
101 /* Boot-time LSM user choice */
102 static __initdata const char *chosen_lsm_order;
103 static __initdata const char *chosen_major_lsm;
105 static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
107 /* Ordered list of LSMs to initialize. */
108 static __initdata struct lsm_info *ordered_lsms[MAX_LSM_COUNT + 1];
109 static __initdata struct lsm_info *exclusive;
111 #ifdef CONFIG_HAVE_STATIC_CALL
112 #define LSM_HOOK_TRAMP(NAME, NUM) \
113 &STATIC_CALL_TRAMP(LSM_STATIC_CALL(NAME, NUM))
115 #define LSM_HOOK_TRAMP(NAME, NUM) NULL
119 * Define static calls and static keys for each LSM hook.
121 #define DEFINE_LSM_STATIC_CALL(NUM, NAME, RET, ...) \
122 DEFINE_STATIC_CALL_NULL(LSM_STATIC_CALL(NAME, NUM), \
123 *((RET(*)(__VA_ARGS__))NULL)); \
124 DEFINE_STATIC_KEY_FALSE(SECURITY_HOOK_ACTIVE_KEY(NAME, NUM));
126 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
127 LSM_DEFINE_UNROLL(DEFINE_LSM_STATIC_CALL, NAME, RET, __VA_ARGS__)
128 #include <linux/lsm_hook_defs.h>
130 #undef DEFINE_LSM_STATIC_CALL
133 * Initialise a table of static calls for each LSM hook.
134 * DEFINE_STATIC_CALL_NULL invocation above generates a key (STATIC_CALL_KEY)
135 * and a trampoline (STATIC_CALL_TRAMP) which are used to call
136 * __static_call_update when updating the static call.
138 * The static calls table is used by early LSMs, some architectures can fault on
139 * unaligned accesses and the fault handling code may not be ready by then.
140 * Thus, the static calls table should be aligned to avoid any unhandled faults
143 struct lsm_static_calls_table
144 static_calls_table __ro_after_init __aligned(sizeof(u64)) = {
145 #define INIT_LSM_STATIC_CALL(NUM, NAME) \
146 (struct lsm_static_call) { \
147 .key = &STATIC_CALL_KEY(LSM_STATIC_CALL(NAME, NUM)), \
148 .trampoline = LSM_HOOK_TRAMP(NAME, NUM), \
149 .active = &SECURITY_HOOK_ACTIVE_KEY(NAME, NUM), \
151 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
153 LSM_DEFINE_UNROLL(INIT_LSM_STATIC_CALL, NAME) \
155 #include <linux/lsm_hook_defs.h>
157 #undef INIT_LSM_STATIC_CALL
160 static __initdata bool debug;
161 #define init_debug(...) \
164 pr_info(__VA_ARGS__); \
167 static bool __init is_enabled(struct lsm_info *lsm)
172 return *lsm->enabled;
175 /* Mark an LSM's enabled flag. */
176 static int lsm_enabled_true __initdata = 1;
177 static int lsm_enabled_false __initdata = 0;
178 static void __init set_enabled(struct lsm_info *lsm, bool enabled)
181 * When an LSM hasn't configured an enable variable, we can use
182 * a hard-coded location for storing the default enabled state.
186 lsm->enabled = &lsm_enabled_true;
188 lsm->enabled = &lsm_enabled_false;
189 } else if (lsm->enabled == &lsm_enabled_true) {
191 lsm->enabled = &lsm_enabled_false;
192 } else if (lsm->enabled == &lsm_enabled_false) {
194 lsm->enabled = &lsm_enabled_true;
196 *lsm->enabled = enabled;
200 /* Is an LSM already listed in the ordered LSMs list? */
201 static bool __init exists_ordered_lsm(struct lsm_info *lsm)
203 struct lsm_info **check;
205 for (check = ordered_lsms; *check; check++)
212 /* Append an LSM to the list of ordered LSMs to initialize. */
213 static int last_lsm __initdata;
214 static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
216 /* Ignore duplicate selections. */
217 if (exists_ordered_lsm(lsm))
220 if (WARN(last_lsm == MAX_LSM_COUNT, "%s: out of LSM static calls!?\n", from))
223 /* Enable this LSM, if it is not already set. */
225 lsm->enabled = &lsm_enabled_true;
226 ordered_lsms[last_lsm++] = lsm;
228 init_debug("%s ordered: %s (%s)\n", from, lsm->name,
229 is_enabled(lsm) ? "enabled" : "disabled");
232 /* Is an LSM allowed to be initialized? */
233 static bool __init lsm_allowed(struct lsm_info *lsm)
235 /* Skip if the LSM is disabled. */
236 if (!is_enabled(lsm))
239 /* Not allowed if another exclusive LSM already initialized. */
240 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
241 init_debug("exclusive disabled: %s\n", lsm->name);
248 static void __init lsm_set_blob_size(int *need, int *lbs)
255 offset = ALIGN(*lbs, sizeof(void *));
256 *lbs = offset + *need;
260 static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
265 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
266 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
267 lsm_set_blob_size(&needed->lbs_ib, &blob_sizes.lbs_ib);
269 * The inode blob gets an rcu_head in addition to
270 * what the modules might need.
272 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
273 blob_sizes.lbs_inode = sizeof(struct rcu_head);
274 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
275 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
276 lsm_set_blob_size(&needed->lbs_key, &blob_sizes.lbs_key);
277 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
278 lsm_set_blob_size(&needed->lbs_perf_event, &blob_sizes.lbs_perf_event);
279 lsm_set_blob_size(&needed->lbs_sock, &blob_sizes.lbs_sock);
280 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
281 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
282 lsm_set_blob_size(&needed->lbs_tun_dev, &blob_sizes.lbs_tun_dev);
283 lsm_set_blob_size(&needed->lbs_xattr_count,
284 &blob_sizes.lbs_xattr_count);
285 lsm_set_blob_size(&needed->lbs_bdev, &blob_sizes.lbs_bdev);
288 /* Prepare LSM for initialization. */
289 static void __init prepare_lsm(struct lsm_info *lsm)
291 int enabled = lsm_allowed(lsm);
293 /* Record enablement (to handle any following exclusive LSMs). */
294 set_enabled(lsm, enabled);
296 /* If enabled, do pre-initialization work. */
298 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
300 init_debug("exclusive chosen: %s\n", lsm->name);
303 lsm_set_blob_sizes(lsm->blobs);
307 /* Initialize a given LSM, if it is enabled. */
308 static void __init initialize_lsm(struct lsm_info *lsm)
310 if (is_enabled(lsm)) {
313 init_debug("initializing %s\n", lsm->name);
315 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
320 * Current index to use while initializing the lsm id list.
322 u32 lsm_active_cnt __ro_after_init;
323 const struct lsm_id *lsm_idlist[MAX_LSM_COUNT];
325 /* Populate ordered LSMs list from comma-separated LSM name list. */
326 static void __init ordered_lsm_parse(const char *order, const char *origin)
328 struct lsm_info *lsm;
329 char *sep, *name, *next;
331 /* LSM_ORDER_FIRST is always first. */
332 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
333 if (lsm->order == LSM_ORDER_FIRST)
334 append_ordered_lsm(lsm, " first");
337 /* Process "security=", if given. */
338 if (chosen_major_lsm) {
339 struct lsm_info *major;
342 * To match the original "security=" behavior, this
343 * explicitly does NOT fallback to another Legacy Major
344 * if the selected one was separately disabled: disable
345 * all non-matching Legacy Major LSMs.
347 for (major = __start_lsm_info; major < __end_lsm_info;
349 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
350 strcmp(major->name, chosen_major_lsm) != 0) {
351 set_enabled(major, false);
352 init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
353 chosen_major_lsm, major->name);
358 sep = kstrdup(order, GFP_KERNEL);
360 /* Walk the list, looking for matching LSMs. */
361 while ((name = strsep(&next, ",")) != NULL) {
364 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
365 if (strcmp(lsm->name, name) == 0) {
366 if (lsm->order == LSM_ORDER_MUTABLE)
367 append_ordered_lsm(lsm, origin);
373 init_debug("%s ignored: %s (not built into kernel)\n",
377 /* Process "security=", if given. */
378 if (chosen_major_lsm) {
379 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
380 if (exists_ordered_lsm(lsm))
382 if (strcmp(lsm->name, chosen_major_lsm) == 0)
383 append_ordered_lsm(lsm, "security=");
387 /* LSM_ORDER_LAST is always last. */
388 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
389 if (lsm->order == LSM_ORDER_LAST)
390 append_ordered_lsm(lsm, " last");
393 /* Disable all LSMs not in the ordered list. */
394 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
395 if (exists_ordered_lsm(lsm))
397 set_enabled(lsm, false);
398 init_debug("%s skipped: %s (not in requested order)\n",
405 static void __init lsm_static_call_init(struct security_hook_list *hl)
407 struct lsm_static_call *scall = hl->scalls;
410 for (i = 0; i < MAX_LSM_COUNT; i++) {
411 /* Update the first static call that is not used yet */
413 __static_call_update(scall->key, scall->trampoline,
414 hl->hook.lsm_func_addr);
416 static_branch_enable(scall->active);
421 panic("%s - Ran out of static slots.\n", __func__);
424 static void __init lsm_early_cred(struct cred *cred);
425 static void __init lsm_early_task(struct task_struct *task);
427 static int lsm_append(const char *new, char **result);
429 static void __init report_lsm_order(void)
431 struct lsm_info **lsm, *early;
434 pr_info("initializing lsm=");
436 /* Report each enabled LSM name, comma separated. */
437 for (early = __start_early_lsm_info;
438 early < __end_early_lsm_info; early++)
439 if (is_enabled(early))
440 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
441 for (lsm = ordered_lsms; *lsm; lsm++)
442 if (is_enabled(*lsm))
443 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
448 static void __init ordered_lsm_init(void)
450 struct lsm_info **lsm;
452 if (chosen_lsm_order) {
453 if (chosen_major_lsm) {
454 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
455 chosen_major_lsm, chosen_lsm_order);
456 chosen_major_lsm = NULL;
458 ordered_lsm_parse(chosen_lsm_order, "cmdline");
460 ordered_lsm_parse(builtin_lsm_order, "builtin");
462 for (lsm = ordered_lsms; *lsm; lsm++)
467 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
468 init_debug("file blob size = %d\n", blob_sizes.lbs_file);
469 init_debug("ib blob size = %d\n", blob_sizes.lbs_ib);
470 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
471 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
473 init_debug("key blob size = %d\n", blob_sizes.lbs_key);
474 #endif /* CONFIG_KEYS */
475 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
476 init_debug("sock blob size = %d\n", blob_sizes.lbs_sock);
477 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
478 init_debug("perf event blob size = %d\n", blob_sizes.lbs_perf_event);
479 init_debug("task blob size = %d\n", blob_sizes.lbs_task);
480 init_debug("tun device blob size = %d\n", blob_sizes.lbs_tun_dev);
481 init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
482 init_debug("bdev blob size = %d\n", blob_sizes.lbs_bdev);
485 * Create any kmem_caches needed for blobs
487 if (blob_sizes.lbs_file)
488 lsm_file_cache = kmem_cache_create("lsm_file_cache",
489 blob_sizes.lbs_file, 0,
491 if (blob_sizes.lbs_inode)
492 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
493 blob_sizes.lbs_inode, 0,
496 lsm_early_cred((struct cred *) current->cred);
497 lsm_early_task(current);
498 for (lsm = ordered_lsms; *lsm; lsm++)
499 initialize_lsm(*lsm);
502 int __init early_security_init(void)
504 struct lsm_info *lsm;
506 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
508 lsm->enabled = &lsm_enabled_true;
517 * security_init - initializes the security framework
519 * This should be called early in the kernel initialization sequence.
521 int __init security_init(void)
523 struct lsm_info *lsm;
525 init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
526 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
527 init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
530 * Append the names of the early LSM modules now that kmalloc() is
533 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
534 init_debug(" early started: %s (%s)\n", lsm->name,
535 is_enabled(lsm) ? "enabled" : "disabled");
537 lsm_append(lsm->name, &lsm_names);
540 /* Load LSMs in specified order. */
546 /* Save user chosen LSM */
547 static int __init choose_major_lsm(char *str)
549 chosen_major_lsm = str;
552 __setup("security=", choose_major_lsm);
554 /* Explicitly choose LSM initialization order. */
555 static int __init choose_lsm_order(char *str)
557 chosen_lsm_order = str;
560 __setup("lsm=", choose_lsm_order);
562 /* Enable LSM order debugging. */
563 static int __init enable_debug(char *str)
568 __setup("lsm.debug", enable_debug);
570 static bool match_last_lsm(const char *list, const char *lsm)
574 if (WARN_ON(!list || !lsm))
576 last = strrchr(list, ',');
578 /* Pass the comma, strcmp() will check for '\0' */
582 return !strcmp(last, lsm);
585 static int lsm_append(const char *new, char **result)
589 if (*result == NULL) {
590 *result = kstrdup(new, GFP_KERNEL);
594 /* Check if it is the last registered name */
595 if (match_last_lsm(*result, new))
597 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
607 * security_add_hooks - Add a modules hooks to the hook lists.
608 * @hooks: the hooks to add
609 * @count: the number of hooks to add
610 * @lsmid: the identification information for the security module
612 * Each LSM has to register its hooks with the infrastructure.
614 void __init security_add_hooks(struct security_hook_list *hooks, int count,
615 const struct lsm_id *lsmid)
620 * A security module may call security_add_hooks() more
621 * than once during initialization, and LSM initialization
622 * is serialized. Landlock is one such case.
623 * Look at the previous entry, if there is one, for duplication.
625 if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
626 if (lsm_active_cnt >= MAX_LSM_COUNT)
627 panic("%s Too many LSMs registered.\n", __func__);
628 lsm_idlist[lsm_active_cnt++] = lsmid;
631 for (i = 0; i < count; i++) {
632 hooks[i].lsmid = lsmid;
633 lsm_static_call_init(&hooks[i]);
637 * Don't try to append during early_security_init(), we'll come back
638 * and fix this up afterwards.
640 if (slab_is_available()) {
641 if (lsm_append(lsmid->name, &lsm_names) < 0)
642 panic("%s - Cannot get early memory.\n", __func__);
646 int call_blocking_lsm_notifier(enum lsm_event event, void *data)
648 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
651 EXPORT_SYMBOL(call_blocking_lsm_notifier);
653 int register_blocking_lsm_notifier(struct notifier_block *nb)
655 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
658 EXPORT_SYMBOL(register_blocking_lsm_notifier);
660 int unregister_blocking_lsm_notifier(struct notifier_block *nb)
662 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
665 EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
668 * lsm_blob_alloc - allocate a composite blob
669 * @dest: the destination for the blob
670 * @size: the size of the blob
671 * @gfp: allocation type
673 * Allocate a blob for all the modules
675 * Returns 0, or -ENOMEM if memory can't be allocated.
677 static int lsm_blob_alloc(void **dest, size_t size, gfp_t gfp)
684 *dest = kzalloc(size, gfp);
691 * lsm_cred_alloc - allocate a composite cred blob
692 * @cred: the cred that needs a blob
693 * @gfp: allocation type
695 * Allocate the cred blob for all the modules
697 * Returns 0, or -ENOMEM if memory can't be allocated.
699 static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
701 return lsm_blob_alloc(&cred->security, blob_sizes.lbs_cred, gfp);
705 * lsm_early_cred - during initialization allocate a composite cred blob
706 * @cred: the cred that needs a blob
708 * Allocate the cred blob for all the modules
710 static void __init lsm_early_cred(struct cred *cred)
712 int rc = lsm_cred_alloc(cred, GFP_KERNEL);
715 panic("%s: Early cred alloc failed.\n", __func__);
719 * lsm_file_alloc - allocate a composite file blob
720 * @file: the file that needs a blob
722 * Allocate the file blob for all the modules
724 * Returns 0, or -ENOMEM if memory can't be allocated.
726 static int lsm_file_alloc(struct file *file)
728 if (!lsm_file_cache) {
729 file->f_security = NULL;
733 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
734 if (file->f_security == NULL)
740 * lsm_inode_alloc - allocate a composite inode blob
741 * @inode: the inode that needs a blob
742 * @gfp: allocation flags
744 * Allocate the inode blob for all the modules
746 * Returns 0, or -ENOMEM if memory can't be allocated.
748 static int lsm_inode_alloc(struct inode *inode, gfp_t gfp)
750 if (!lsm_inode_cache) {
751 inode->i_security = NULL;
755 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, gfp);
756 if (inode->i_security == NULL)
762 * lsm_task_alloc - allocate a composite task blob
763 * @task: the task that needs a blob
765 * Allocate the task blob for all the modules
767 * Returns 0, or -ENOMEM if memory can't be allocated.
769 static int lsm_task_alloc(struct task_struct *task)
771 return lsm_blob_alloc(&task->security, blob_sizes.lbs_task, GFP_KERNEL);
775 * lsm_ipc_alloc - allocate a composite ipc blob
776 * @kip: the ipc that needs a blob
778 * Allocate the ipc blob for all the modules
780 * Returns 0, or -ENOMEM if memory can't be allocated.
782 static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
784 return lsm_blob_alloc(&kip->security, blob_sizes.lbs_ipc, GFP_KERNEL);
789 * lsm_key_alloc - allocate a composite key blob
790 * @key: the key that needs a blob
792 * Allocate the key blob for all the modules
794 * Returns 0, or -ENOMEM if memory can't be allocated.
796 static int lsm_key_alloc(struct key *key)
798 return lsm_blob_alloc(&key->security, blob_sizes.lbs_key, GFP_KERNEL);
800 #endif /* CONFIG_KEYS */
803 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
804 * @mp: the msg_msg that needs a blob
806 * Allocate the ipc blob for all the modules
808 * Returns 0, or -ENOMEM if memory can't be allocated.
810 static int lsm_msg_msg_alloc(struct msg_msg *mp)
812 return lsm_blob_alloc(&mp->security, blob_sizes.lbs_msg_msg,
817 * lsm_bdev_alloc - allocate a composite block_device blob
818 * @bdev: the block_device that needs a blob
820 * Allocate the block_device blob for all the modules
822 * Returns 0, or -ENOMEM if memory can't be allocated.
824 static int lsm_bdev_alloc(struct block_device *bdev)
826 if (blob_sizes.lbs_bdev == 0) {
827 bdev->bd_security = NULL;
831 bdev->bd_security = kzalloc(blob_sizes.lbs_bdev, GFP_KERNEL);
832 if (!bdev->bd_security)
839 * lsm_early_task - during initialization allocate a composite task blob
840 * @task: the task that needs a blob
842 * Allocate the task blob for all the modules
844 static void __init lsm_early_task(struct task_struct *task)
846 int rc = lsm_task_alloc(task);
849 panic("%s: Early task alloc failed.\n", __func__);
853 * lsm_superblock_alloc - allocate a composite superblock blob
854 * @sb: the superblock that needs a blob
856 * Allocate the superblock blob for all the modules
858 * Returns 0, or -ENOMEM if memory can't be allocated.
860 static int lsm_superblock_alloc(struct super_block *sb)
862 return lsm_blob_alloc(&sb->s_security, blob_sizes.lbs_superblock,
867 * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
868 * @uctx: a userspace LSM context to be filled
869 * @uctx_len: available uctx size (input), used uctx size (output)
870 * @val: the new LSM context value
871 * @val_len: the size of the new LSM context value
873 * @flags: LSM defined flags
875 * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL
876 * simply calculate the required size to output via @utc_len and return
879 * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
880 * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
882 int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
883 void *val, size_t val_len,
886 struct lsm_ctx *nctx = NULL;
890 nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
891 if (nctx_len > *uctx_len) {
896 /* no buffer - return success/0 and set @uctx_len to the req size */
900 nctx = kzalloc(nctx_len, GFP_KERNEL);
907 nctx->len = nctx_len;
908 nctx->ctx_len = val_len;
909 memcpy(nctx->ctx, val, val_len);
911 if (copy_to_user(uctx, nctx, nctx_len))
916 *uctx_len = nctx_len;
921 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
922 * can be accessed with:
924 * LSM_RET_DEFAULT(<hook_name>)
926 * The macros below define static constants for the default value of each
929 #define LSM_RET_DEFAULT(NAME) (NAME##_default)
930 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
931 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
932 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
933 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
934 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
936 #include <linux/lsm_hook_defs.h>
940 * Hook list operation macros.
943 * This is a hook that does not return a value.
946 * This is a hook that returns a value.
948 #define __CALL_STATIC_VOID(NUM, HOOK, ...) \
950 if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \
951 static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \
955 #define call_void_hook(HOOK, ...) \
957 LSM_LOOP_UNROLL(__CALL_STATIC_VOID, HOOK, __VA_ARGS__); \
961 #define __CALL_STATIC_INT(NUM, R, HOOK, LABEL, ...) \
963 if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \
964 R = static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \
965 if (R != LSM_RET_DEFAULT(HOOK)) \
970 #define call_int_hook(HOOK, ...) \
973 int RC = LSM_RET_DEFAULT(HOOK); \
975 LSM_LOOP_UNROLL(__CALL_STATIC_INT, RC, HOOK, OUT, __VA_ARGS__); \
980 #define lsm_for_each_hook(scall, NAME) \
981 for (scall = static_calls_table.NAME; \
982 scall - static_calls_table.NAME < MAX_LSM_COUNT; scall++) \
983 if (static_key_enabled(&scall->active->key))
985 /* Security operations */
988 * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
989 * @mgr: task credentials of current binder process
991 * Check whether @mgr is allowed to be the binder context manager.
993 * Return: Return 0 if permission is granted.
995 int security_binder_set_context_mgr(const struct cred *mgr)
997 return call_int_hook(binder_set_context_mgr, mgr);
1001 * security_binder_transaction() - Check if a binder transaction is allowed
1002 * @from: sending process
1003 * @to: receiving process
1005 * Check whether @from is allowed to invoke a binder transaction call to @to.
1007 * Return: Returns 0 if permission is granted.
1009 int security_binder_transaction(const struct cred *from,
1010 const struct cred *to)
1012 return call_int_hook(binder_transaction, from, to);
1016 * security_binder_transfer_binder() - Check if a binder transfer is allowed
1017 * @from: sending process
1018 * @to: receiving process
1020 * Check whether @from is allowed to transfer a binder reference to @to.
1022 * Return: Returns 0 if permission is granted.
1024 int security_binder_transfer_binder(const struct cred *from,
1025 const struct cred *to)
1027 return call_int_hook(binder_transfer_binder, from, to);
1031 * security_binder_transfer_file() - Check if a binder file xfer is allowed
1032 * @from: sending process
1033 * @to: receiving process
1034 * @file: file being transferred
1036 * Check whether @from is allowed to transfer @file to @to.
1038 * Return: Returns 0 if permission is granted.
1040 int security_binder_transfer_file(const struct cred *from,
1041 const struct cred *to, const struct file *file)
1043 return call_int_hook(binder_transfer_file, from, to, file);
1047 * security_ptrace_access_check() - Check if tracing is allowed
1048 * @child: target process
1049 * @mode: PTRACE_MODE flags
1051 * Check permission before allowing the current process to trace the @child
1052 * process. Security modules may also want to perform a process tracing check
1053 * during an execve in the set_security or apply_creds hooks of tracing check
1054 * during an execve in the bprm_set_creds hook of binprm_security_ops if the
1055 * process is being traced and its security attributes would be changed by the
1058 * Return: Returns 0 if permission is granted.
1060 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
1062 return call_int_hook(ptrace_access_check, child, mode);
1066 * security_ptrace_traceme() - Check if tracing is allowed
1067 * @parent: tracing process
1069 * Check that the @parent process has sufficient permission to trace the
1070 * current process before allowing the current process to present itself to the
1071 * @parent process for tracing.
1073 * Return: Returns 0 if permission is granted.
1075 int security_ptrace_traceme(struct task_struct *parent)
1077 return call_int_hook(ptrace_traceme, parent);
1081 * security_capget() - Get the capability sets for a process
1082 * @target: target process
1083 * @effective: effective capability set
1084 * @inheritable: inheritable capability set
1085 * @permitted: permitted capability set
1087 * Get the @effective, @inheritable, and @permitted capability sets for the
1088 * @target process. The hook may also perform permission checking to determine
1089 * if the current process is allowed to see the capability sets of the @target
1092 * Return: Returns 0 if the capability sets were successfully obtained.
1094 int security_capget(const struct task_struct *target,
1095 kernel_cap_t *effective,
1096 kernel_cap_t *inheritable,
1097 kernel_cap_t *permitted)
1099 return call_int_hook(capget, target, effective, inheritable, permitted);
1103 * security_capset() - Set the capability sets for a process
1104 * @new: new credentials for the target process
1105 * @old: current credentials of the target process
1106 * @effective: effective capability set
1107 * @inheritable: inheritable capability set
1108 * @permitted: permitted capability set
1110 * Set the @effective, @inheritable, and @permitted capability sets for the
1113 * Return: Returns 0 and update @new if permission is granted.
1115 int security_capset(struct cred *new, const struct cred *old,
1116 const kernel_cap_t *effective,
1117 const kernel_cap_t *inheritable,
1118 const kernel_cap_t *permitted)
1120 return call_int_hook(capset, new, old, effective, inheritable,
1125 * security_capable() - Check if a process has the necessary capability
1126 * @cred: credentials to examine
1127 * @ns: user namespace
1128 * @cap: capability requested
1129 * @opts: capability check options
1131 * Check whether the @tsk process has the @cap capability in the indicated
1132 * credentials. @cap contains the capability <include/linux/capability.h>.
1133 * @opts contains options for the capable check <include/linux/security.h>.
1135 * Return: Returns 0 if the capability is granted.
1137 int security_capable(const struct cred *cred,
1138 struct user_namespace *ns,
1142 return call_int_hook(capable, cred, ns, cap, opts);
1146 * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
1152 * Check whether the quotactl syscall is allowed for this @sb.
1154 * Return: Returns 0 if permission is granted.
1156 int security_quotactl(int cmds, int type, int id, const struct super_block *sb)
1158 return call_int_hook(quotactl, cmds, type, id, sb);
1162 * security_quota_on() - Check if QUOTAON is allowed for a dentry
1165 * Check whether QUOTAON is allowed for @dentry.
1167 * Return: Returns 0 if permission is granted.
1169 int security_quota_on(struct dentry *dentry)
1171 return call_int_hook(quota_on, dentry);
1175 * security_syslog() - Check if accessing the kernel message ring is allowed
1176 * @type: SYSLOG_ACTION_* type
1178 * Check permission before accessing the kernel message ring or changing
1179 * logging to the console. See the syslog(2) manual page for an explanation of
1182 * Return: Return 0 if permission is granted.
1184 int security_syslog(int type)
1186 return call_int_hook(syslog, type);
1190 * security_settime64() - Check if changing the system time is allowed
1194 * Check permission to change the system time, struct timespec64 is defined in
1195 * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
1197 * Return: Returns 0 if permission is granted.
1199 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
1201 return call_int_hook(settime, ts, tz);
1205 * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
1207 * @pages: number of pages
1209 * Check permissions for allocating a new virtual mapping. If all LSMs return
1210 * a positive value, __vm_enough_memory() will be called with cap_sys_admin
1211 * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
1212 * called with cap_sys_admin cleared.
1214 * Return: Returns 0 if permission is granted by the LSM infrastructure to the
1217 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
1219 struct lsm_static_call *scall;
1220 int cap_sys_admin = 1;
1224 * The module will respond with 0 if it thinks the __vm_enough_memory()
1225 * call should be made with the cap_sys_admin set. If all of the modules
1226 * agree that it should be set it will. If any module thinks it should
1227 * not be set it won't.
1229 lsm_for_each_hook(scall, vm_enough_memory) {
1230 rc = scall->hl->hook.vm_enough_memory(mm, pages);
1236 return __vm_enough_memory(mm, pages, cap_sys_admin);
1240 * security_bprm_creds_for_exec() - Prepare the credentials for exec()
1241 * @bprm: binary program information
1243 * If the setup in prepare_exec_creds did not setup @bprm->cred->security
1244 * properly for executing @bprm->file, update the LSM's portion of
1245 * @bprm->cred->security to be what commit_creds needs to install for the new
1246 * program. This hook may also optionally check permissions (e.g. for
1247 * transitions between security domains). The hook must set @bprm->secureexec
1248 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
1249 * contains the linux_binprm structure.
1251 * If execveat(2) is called with the AT_EXECVE_CHECK flag, bprm->is_check is
1252 * set. The result must be the same as without this flag even if the execution
1253 * will never really happen and @bprm will always be dropped.
1255 * This hook must not change current->cred, only @bprm->cred.
1257 * Return: Returns 0 if the hook is successful and permission is granted.
1259 int security_bprm_creds_for_exec(struct linux_binprm *bprm)
1261 return call_int_hook(bprm_creds_for_exec, bprm);
1265 * security_bprm_creds_from_file() - Update linux_binprm creds based on file
1266 * @bprm: binary program information
1267 * @file: associated file
1269 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
1270 * exec, update @bprm->cred to reflect that change. This is called after
1271 * finding the binary that will be executed without an interpreter. This
1272 * ensures that the credentials will not be derived from a script that the
1273 * binary will need to reopen, which when reopend may end up being a completely
1274 * different file. This hook may also optionally check permissions (e.g. for
1275 * transitions between security domains). The hook must set @bprm->secureexec
1276 * to 1 if AT_SECURE should be set to request libc enable secure mode. The
1277 * hook must add to @bprm->per_clear any personality flags that should be
1278 * cleared from current->personality. @bprm contains the linux_binprm
1281 * Return: Returns 0 if the hook is successful and permission is granted.
1283 int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file)
1285 return call_int_hook(bprm_creds_from_file, bprm, file);
1289 * security_bprm_check() - Mediate binary handler search
1290 * @bprm: binary program information
1292 * This hook mediates the point when a search for a binary handler will begin.
1293 * It allows a check against the @bprm->cred->security value which was set in
1294 * the preceding creds_for_exec call. The argv list and envp list are reliably
1295 * available in @bprm. This hook may be called multiple times during a single
1296 * execve. @bprm contains the linux_binprm structure.
1298 * Return: Returns 0 if the hook is successful and permission is granted.
1300 int security_bprm_check(struct linux_binprm *bprm)
1302 return call_int_hook(bprm_check_security, bprm);
1306 * security_bprm_committing_creds() - Install creds for a process during exec()
1307 * @bprm: binary program information
1309 * Prepare to install the new security attributes of a process being
1310 * transformed by an execve operation, based on the old credentials pointed to
1311 * by @current->cred and the information set in @bprm->cred by the
1312 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
1313 * hook is a good place to perform state changes on the process such as closing
1314 * open file descriptors to which access will no longer be granted when the
1315 * attributes are changed. This is called immediately before commit_creds().
1317 void security_bprm_committing_creds(const struct linux_binprm *bprm)
1319 call_void_hook(bprm_committing_creds, bprm);
1323 * security_bprm_committed_creds() - Tidy up after cred install during exec()
1324 * @bprm: binary program information
1326 * Tidy up after the installation of the new security attributes of a process
1327 * being transformed by an execve operation. The new credentials have, by this
1328 * point, been set to @current->cred. @bprm points to the linux_binprm
1329 * structure. This hook is a good place to perform state changes on the
1330 * process such as clearing out non-inheritable signal state. This is called
1331 * immediately after commit_creds().
1333 void security_bprm_committed_creds(const struct linux_binprm *bprm)
1335 call_void_hook(bprm_committed_creds, bprm);
1339 * security_fs_context_submount() - Initialise fc->security
1340 * @fc: new filesystem context
1341 * @reference: dentry reference for submount/remount
1343 * Fill out the ->security field for a new fs_context.
1345 * Return: Returns 0 on success or negative error code on failure.
1347 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
1349 return call_int_hook(fs_context_submount, fc, reference);
1353 * security_fs_context_dup() - Duplicate a fs_context LSM blob
1354 * @fc: destination filesystem context
1355 * @src_fc: source filesystem context
1357 * Allocate and attach a security structure to sc->security. This pointer is
1358 * initialised to NULL by the caller. @fc indicates the new filesystem context.
1359 * @src_fc indicates the original filesystem context.
1361 * Return: Returns 0 on success or a negative error code on failure.
1363 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1365 return call_int_hook(fs_context_dup, fc, src_fc);
1369 * security_fs_context_parse_param() - Configure a filesystem context
1370 * @fc: filesystem context
1371 * @param: filesystem parameter
1373 * Userspace provided a parameter to configure a superblock. The LSM can
1374 * consume the parameter or return it to the caller for use elsewhere.
1376 * Return: If the parameter is used by the LSM it should return 0, if it is
1377 * returned to the caller -ENOPARAM is returned, otherwise a negative
1378 * error code is returned.
1380 int security_fs_context_parse_param(struct fs_context *fc,
1381 struct fs_parameter *param)
1383 struct lsm_static_call *scall;
1387 lsm_for_each_hook(scall, fs_context_parse_param) {
1388 trc = scall->hl->hook.fs_context_parse_param(fc, param);
1391 else if (trc != -ENOPARAM)
1398 * security_sb_alloc() - Allocate a super_block LSM blob
1399 * @sb: filesystem superblock
1401 * Allocate and attach a security structure to the sb->s_security field. The
1402 * s_security field is initialized to NULL when the structure is allocated.
1403 * @sb contains the super_block structure to be modified.
1405 * Return: Returns 0 if operation was successful.
1407 int security_sb_alloc(struct super_block *sb)
1409 int rc = lsm_superblock_alloc(sb);
1413 rc = call_int_hook(sb_alloc_security, sb);
1415 security_sb_free(sb);
1420 * security_sb_delete() - Release super_block LSM associated objects
1421 * @sb: filesystem superblock
1423 * Release objects tied to a superblock (e.g. inodes). @sb contains the
1424 * super_block structure being released.
1426 void security_sb_delete(struct super_block *sb)
1428 call_void_hook(sb_delete, sb);
1432 * security_sb_free() - Free a super_block LSM blob
1433 * @sb: filesystem superblock
1435 * Deallocate and clear the sb->s_security field. @sb contains the super_block
1436 * structure to be modified.
1438 void security_sb_free(struct super_block *sb)
1440 call_void_hook(sb_free_security, sb);
1441 kfree(sb->s_security);
1442 sb->s_security = NULL;
1446 * security_free_mnt_opts() - Free memory associated with mount options
1447 * @mnt_opts: LSM processed mount options
1449 * Free memory associated with @mnt_ops.
1451 void security_free_mnt_opts(void **mnt_opts)
1455 call_void_hook(sb_free_mnt_opts, *mnt_opts);
1458 EXPORT_SYMBOL(security_free_mnt_opts);
1461 * security_sb_eat_lsm_opts() - Consume LSM mount options
1462 * @options: mount options
1463 * @mnt_opts: LSM processed mount options
1465 * Eat (scan @options) and save them in @mnt_opts.
1467 * Return: Returns 0 on success, negative values on failure.
1469 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1471 return call_int_hook(sb_eat_lsm_opts, options, mnt_opts);
1473 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1476 * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1477 * @sb: filesystem superblock
1478 * @mnt_opts: new mount options
1480 * Determine if the new mount options in @mnt_opts are allowed given the
1481 * existing mounted filesystem at @sb. @sb superblock being compared.
1483 * Return: Returns 0 if options are compatible.
1485 int security_sb_mnt_opts_compat(struct super_block *sb,
1488 return call_int_hook(sb_mnt_opts_compat, sb, mnt_opts);
1490 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1493 * security_sb_remount() - Verify no incompatible mount changes during remount
1494 * @sb: filesystem superblock
1495 * @mnt_opts: (re)mount options
1497 * Extracts security system specific mount options and verifies no changes are
1498 * being made to those options.
1500 * Return: Returns 0 if permission is granted.
1502 int security_sb_remount(struct super_block *sb,
1505 return call_int_hook(sb_remount, sb, mnt_opts);
1507 EXPORT_SYMBOL(security_sb_remount);
1510 * security_sb_kern_mount() - Check if a kernel mount is allowed
1511 * @sb: filesystem superblock
1513 * Mount this @sb if allowed by permissions.
1515 * Return: Returns 0 if permission is granted.
1517 int security_sb_kern_mount(const struct super_block *sb)
1519 return call_int_hook(sb_kern_mount, sb);
1523 * security_sb_show_options() - Output the mount options for a superblock
1525 * @sb: filesystem superblock
1527 * Show (print on @m) mount options for this @sb.
1529 * Return: Returns 0 on success, negative values on failure.
1531 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1533 return call_int_hook(sb_show_options, m, sb);
1537 * security_sb_statfs() - Check if accessing fs stats is allowed
1538 * @dentry: superblock handle
1540 * Check permission before obtaining filesystem statistics for the @mnt
1541 * mountpoint. @dentry is a handle on the superblock for the filesystem.
1543 * Return: Returns 0 if permission is granted.
1545 int security_sb_statfs(struct dentry *dentry)
1547 return call_int_hook(sb_statfs, dentry);
1551 * security_sb_mount() - Check permission for mounting a filesystem
1552 * @dev_name: filesystem backing device
1553 * @path: mount point
1554 * @type: filesystem type
1555 * @flags: mount flags
1556 * @data: filesystem specific data
1558 * Check permission before an object specified by @dev_name is mounted on the
1559 * mount point named by @nd. For an ordinary mount, @dev_name identifies a
1560 * device if the file system type requires a device. For a remount
1561 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
1562 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
1565 * Return: Returns 0 if permission is granted.
1567 int security_sb_mount(const char *dev_name, const struct path *path,
1568 const char *type, unsigned long flags, void *data)
1570 return call_int_hook(sb_mount, dev_name, path, type, flags, data);
1574 * security_sb_umount() - Check permission for unmounting a filesystem
1575 * @mnt: mounted filesystem
1576 * @flags: unmount flags
1578 * Check permission before the @mnt file system is unmounted.
1580 * Return: Returns 0 if permission is granted.
1582 int security_sb_umount(struct vfsmount *mnt, int flags)
1584 return call_int_hook(sb_umount, mnt, flags);
1588 * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1589 * @old_path: new location for current rootfs
1590 * @new_path: location of the new rootfs
1592 * Check permission before pivoting the root filesystem.
1594 * Return: Returns 0 if permission is granted.
1596 int security_sb_pivotroot(const struct path *old_path,
1597 const struct path *new_path)
1599 return call_int_hook(sb_pivotroot, old_path, new_path);
1603 * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1604 * @sb: filesystem superblock
1605 * @mnt_opts: binary mount options
1606 * @kern_flags: kernel flags (in)
1607 * @set_kern_flags: kernel flags (out)
1609 * Set the security relevant mount options used for a superblock.
1611 * Return: Returns 0 on success, error on failure.
1613 int security_sb_set_mnt_opts(struct super_block *sb,
1615 unsigned long kern_flags,
1616 unsigned long *set_kern_flags)
1618 struct lsm_static_call *scall;
1619 int rc = mnt_opts ? -EOPNOTSUPP : LSM_RET_DEFAULT(sb_set_mnt_opts);
1621 lsm_for_each_hook(scall, sb_set_mnt_opts) {
1622 rc = scall->hl->hook.sb_set_mnt_opts(sb, mnt_opts, kern_flags,
1624 if (rc != LSM_RET_DEFAULT(sb_set_mnt_opts))
1629 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1632 * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1633 * @oldsb: source superblock
1634 * @newsb: destination superblock
1635 * @kern_flags: kernel flags (in)
1636 * @set_kern_flags: kernel flags (out)
1638 * Copy all security options from a given superblock to another.
1640 * Return: Returns 0 on success, error on failure.
1642 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1643 struct super_block *newsb,
1644 unsigned long kern_flags,
1645 unsigned long *set_kern_flags)
1647 return call_int_hook(sb_clone_mnt_opts, oldsb, newsb,
1648 kern_flags, set_kern_flags);
1650 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1653 * security_move_mount() - Check permissions for moving a mount
1654 * @from_path: source mount point
1655 * @to_path: destination mount point
1657 * Check permission before a mount is moved.
1659 * Return: Returns 0 if permission is granted.
1661 int security_move_mount(const struct path *from_path,
1662 const struct path *to_path)
1664 return call_int_hook(move_mount, from_path, to_path);
1668 * security_path_notify() - Check if setting a watch is allowed
1671 * @obj_type: file path type
1673 * Check permissions before setting a watch on events as defined by @mask, on
1674 * an object at @path, whose type is defined by @obj_type.
1676 * Return: Returns 0 if permission is granted.
1678 int security_path_notify(const struct path *path, u64 mask,
1679 unsigned int obj_type)
1681 return call_int_hook(path_notify, path, mask, obj_type);
1685 * security_inode_alloc() - Allocate an inode LSM blob
1687 * @gfp: allocation flags
1689 * Allocate and attach a security structure to @inode->i_security. The
1690 * i_security field is initialized to NULL when the inode structure is
1693 * Return: Return 0 if operation was successful.
1695 int security_inode_alloc(struct inode *inode, gfp_t gfp)
1697 int rc = lsm_inode_alloc(inode, gfp);
1701 rc = call_int_hook(inode_alloc_security, inode);
1703 security_inode_free(inode);
1707 static void inode_free_by_rcu(struct rcu_head *head)
1709 /* The rcu head is at the start of the inode blob */
1710 call_void_hook(inode_free_security_rcu, head);
1711 kmem_cache_free(lsm_inode_cache, head);
1715 * security_inode_free() - Free an inode's LSM blob
1718 * Release any LSM resources associated with @inode, although due to the
1719 * inode's RCU protections it is possible that the resources will not be
1720 * fully released until after the current RCU grace period has elapsed.
1722 * It is important for LSMs to note that despite being present in a call to
1723 * security_inode_free(), @inode may still be referenced in a VFS path walk
1724 * and calls to security_inode_permission() may be made during, or after,
1725 * a call to security_inode_free(). For this reason the inode->i_security
1726 * field is released via a call_rcu() callback and any LSMs which need to
1727 * retain inode state for use in security_inode_permission() should only
1728 * release that state in the inode_free_security_rcu() LSM hook callback.
1730 void security_inode_free(struct inode *inode)
1732 call_void_hook(inode_free_security, inode);
1733 if (!inode->i_security)
1735 call_rcu((struct rcu_head *)inode->i_security, inode_free_by_rcu);
1739 * security_dentry_init_security() - Perform dentry initialization
1740 * @dentry: the dentry to initialize
1741 * @mode: mode used to determine resource type
1742 * @name: name of the last path component
1743 * @xattr_name: name of the security/LSM xattr
1744 * @lsmctx: pointer to the resulting LSM context
1746 * Compute a context for a dentry as the inode is not yet available since NFSv4
1747 * has no label backed by an EA anyway. It is important to note that
1748 * @xattr_name does not need to be free'd by the caller, it is a static string.
1750 * Return: Returns 0 on success, negative values on failure.
1752 int security_dentry_init_security(struct dentry *dentry, int mode,
1753 const struct qstr *name,
1754 const char **xattr_name,
1755 struct lsm_context *lsmctx)
1757 return call_int_hook(dentry_init_security, dentry, mode, name,
1758 xattr_name, lsmctx);
1760 EXPORT_SYMBOL(security_dentry_init_security);
1763 * security_dentry_create_files_as() - Perform dentry initialization
1764 * @dentry: the dentry to initialize
1765 * @mode: mode used to determine resource type
1766 * @name: name of the last path component
1767 * @old: creds to use for LSM context calculations
1768 * @new: creds to modify
1770 * Compute a context for a dentry as the inode is not yet available and set
1771 * that context in passed in creds so that new files are created using that
1772 * context. Context is calculated using the passed in creds and not the creds
1775 * Return: Returns 0 on success, error on failure.
1777 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1779 const struct cred *old, struct cred *new)
1781 return call_int_hook(dentry_create_files_as, dentry, mode,
1784 EXPORT_SYMBOL(security_dentry_create_files_as);
1787 * security_inode_init_security() - Initialize an inode's LSM context
1789 * @dir: parent directory
1790 * @qstr: last component of the pathname
1791 * @initxattrs: callback function to write xattrs
1792 * @fs_data: filesystem specific data
1794 * Obtain the security attribute name suffix and value to set on a newly
1795 * created inode and set up the incore security field for the new inode. This
1796 * hook is called by the fs code as part of the inode creation transaction and
1797 * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1798 * hooks called by the VFS.
1800 * The hook function is expected to populate the xattrs array, by calling
1801 * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
1802 * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
1803 * slot, the hook function should set ->name to the attribute name suffix
1804 * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
1805 * to the attribute value, to set ->value_len to the length of the value. If
1806 * the security module does not use security attributes or does not wish to put
1807 * a security attribute on this particular inode, then it should return
1808 * -EOPNOTSUPP to skip this processing.
1810 * Return: Returns 0 if the LSM successfully initialized all of the inode
1811 * security attributes that are required, negative values otherwise.
1813 int security_inode_init_security(struct inode *inode, struct inode *dir,
1814 const struct qstr *qstr,
1815 const initxattrs initxattrs, void *fs_data)
1817 struct lsm_static_call *scall;
1818 struct xattr *new_xattrs = NULL;
1819 int ret = -EOPNOTSUPP, xattr_count = 0;
1821 if (unlikely(IS_PRIVATE(inode)))
1824 if (!blob_sizes.lbs_xattr_count)
1828 /* Allocate +1 as terminator. */
1829 new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 1,
1830 sizeof(*new_xattrs), GFP_NOFS);
1835 lsm_for_each_hook(scall, inode_init_security) {
1836 ret = scall->hl->hook.inode_init_security(inode, dir, qstr, new_xattrs,
1838 if (ret && ret != -EOPNOTSUPP)
1841 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
1842 * means that the LSM is not willing to provide an xattr, not
1843 * that it wants to signal an error. Thus, continue to invoke
1844 * the remaining LSMs.
1848 /* If initxattrs() is NULL, xattr_count is zero, skip the call. */
1852 ret = initxattrs(inode, new_xattrs, fs_data);
1854 for (; xattr_count > 0; xattr_count--)
1855 kfree(new_xattrs[xattr_count - 1].value);
1857 return (ret == -EOPNOTSUPP) ? 0 : ret;
1859 EXPORT_SYMBOL(security_inode_init_security);
1862 * security_inode_init_security_anon() - Initialize an anonymous inode
1864 * @name: the anonymous inode class
1865 * @context_inode: an optional related inode
1867 * Set up the incore security field for the new anonymous inode and return
1868 * whether the inode creation is permitted by the security module or not.
1870 * Return: Returns 0 on success, -EACCES if the security module denies the
1871 * creation of this inode, or another -errno upon other errors.
1873 int security_inode_init_security_anon(struct inode *inode,
1874 const struct qstr *name,
1875 const struct inode *context_inode)
1877 return call_int_hook(inode_init_security_anon, inode, name,
1881 #ifdef CONFIG_SECURITY_PATH
1883 * security_path_mknod() - Check if creating a special file is allowed
1884 * @dir: parent directory
1886 * @mode: new file mode
1887 * @dev: device number
1889 * Check permissions when creating a file. Note that this hook is called even
1890 * if mknod operation is being done for a regular file.
1892 * Return: Returns 0 if permission is granted.
1894 int security_path_mknod(const struct path *dir, struct dentry *dentry,
1895 umode_t mode, unsigned int dev)
1897 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1899 return call_int_hook(path_mknod, dir, dentry, mode, dev);
1901 EXPORT_SYMBOL(security_path_mknod);
1904 * security_path_post_mknod() - Update inode security after reg file creation
1905 * @idmap: idmap of the mount
1908 * Update inode security field after a regular file has been created.
1910 void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
1912 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1914 call_void_hook(path_post_mknod, idmap, dentry);
1918 * security_path_mkdir() - Check if creating a new directory is allowed
1919 * @dir: parent directory
1920 * @dentry: new directory
1921 * @mode: new directory mode
1923 * Check permissions to create a new directory in the existing directory.
1925 * Return: Returns 0 if permission is granted.
1927 int security_path_mkdir(const struct path *dir, struct dentry *dentry,
1930 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1932 return call_int_hook(path_mkdir, dir, dentry, mode);
1934 EXPORT_SYMBOL(security_path_mkdir);
1937 * security_path_rmdir() - Check if removing a directory is allowed
1938 * @dir: parent directory
1939 * @dentry: directory to remove
1941 * Check the permission to remove a directory.
1943 * Return: Returns 0 if permission is granted.
1945 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1947 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1949 return call_int_hook(path_rmdir, dir, dentry);
1953 * security_path_unlink() - Check if removing a hard link is allowed
1954 * @dir: parent directory
1957 * Check the permission to remove a hard link to a file.
1959 * Return: Returns 0 if permission is granted.
1961 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1963 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1965 return call_int_hook(path_unlink, dir, dentry);
1967 EXPORT_SYMBOL(security_path_unlink);
1970 * security_path_symlink() - Check if creating a symbolic link is allowed
1971 * @dir: parent directory
1972 * @dentry: symbolic link
1973 * @old_name: file pathname
1975 * Check the permission to create a symbolic link to a file.
1977 * Return: Returns 0 if permission is granted.
1979 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1980 const char *old_name)
1982 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1984 return call_int_hook(path_symlink, dir, dentry, old_name);
1988 * security_path_link - Check if creating a hard link is allowed
1989 * @old_dentry: existing file
1990 * @new_dir: new parent directory
1991 * @new_dentry: new link
1993 * Check permission before creating a new hard link to a file.
1995 * Return: Returns 0 if permission is granted.
1997 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1998 struct dentry *new_dentry)
2000 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
2002 return call_int_hook(path_link, old_dentry, new_dir, new_dentry);
2006 * security_path_rename() - Check if renaming a file is allowed
2007 * @old_dir: parent directory of the old file
2008 * @old_dentry: the old file
2009 * @new_dir: parent directory of the new file
2010 * @new_dentry: the new file
2013 * Check for permission to rename a file or directory.
2015 * Return: Returns 0 if permission is granted.
2017 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
2018 const struct path *new_dir, struct dentry *new_dentry,
2021 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
2022 (d_is_positive(new_dentry) &&
2023 IS_PRIVATE(d_backing_inode(new_dentry)))))
2026 return call_int_hook(path_rename, old_dir, old_dentry, new_dir,
2029 EXPORT_SYMBOL(security_path_rename);
2032 * security_path_truncate() - Check if truncating a file is allowed
2035 * Check permission before truncating the file indicated by path. Note that
2036 * truncation permissions may also be checked based on already opened files,
2037 * using the security_file_truncate() hook.
2039 * Return: Returns 0 if permission is granted.
2041 int security_path_truncate(const struct path *path)
2043 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2045 return call_int_hook(path_truncate, path);
2049 * security_path_chmod() - Check if changing the file's mode is allowed
2053 * Check for permission to change a mode of the file @path. The new mode is
2054 * specified in @mode which is a bitmask of constants from
2055 * <include/uapi/linux/stat.h>.
2057 * Return: Returns 0 if permission is granted.
2059 int security_path_chmod(const struct path *path, umode_t mode)
2061 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2063 return call_int_hook(path_chmod, path, mode);
2067 * security_path_chown() - Check if changing the file's owner/group is allowed
2072 * Check for permission to change owner/group of a file or directory.
2074 * Return: Returns 0 if permission is granted.
2076 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
2078 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2080 return call_int_hook(path_chown, path, uid, gid);
2084 * security_path_chroot() - Check if changing the root directory is allowed
2087 * Check for permission to change root directory.
2089 * Return: Returns 0 if permission is granted.
2091 int security_path_chroot(const struct path *path)
2093 return call_int_hook(path_chroot, path);
2095 #endif /* CONFIG_SECURITY_PATH */
2098 * security_inode_create() - Check if creating a file is allowed
2099 * @dir: the parent directory
2100 * @dentry: the file being created
2101 * @mode: requested file mode
2103 * Check permission to create a regular file.
2105 * Return: Returns 0 if permission is granted.
2107 int security_inode_create(struct inode *dir, struct dentry *dentry,
2110 if (unlikely(IS_PRIVATE(dir)))
2112 return call_int_hook(inode_create, dir, dentry, mode);
2114 EXPORT_SYMBOL_GPL(security_inode_create);
2117 * security_inode_post_create_tmpfile() - Update inode security of new tmpfile
2118 * @idmap: idmap of the mount
2119 * @inode: inode of the new tmpfile
2121 * Update inode security data after a tmpfile has been created.
2123 void security_inode_post_create_tmpfile(struct mnt_idmap *idmap,
2124 struct inode *inode)
2126 if (unlikely(IS_PRIVATE(inode)))
2128 call_void_hook(inode_post_create_tmpfile, idmap, inode);
2132 * security_inode_link() - Check if creating a hard link is allowed
2133 * @old_dentry: existing file
2134 * @dir: new parent directory
2135 * @new_dentry: new link
2137 * Check permission before creating a new hard link to a file.
2139 * Return: Returns 0 if permission is granted.
2141 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
2142 struct dentry *new_dentry)
2144 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
2146 return call_int_hook(inode_link, old_dentry, dir, new_dentry);
2150 * security_inode_unlink() - Check if removing a hard link is allowed
2151 * @dir: parent directory
2154 * Check the permission to remove a hard link to a file.
2156 * Return: Returns 0 if permission is granted.
2158 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
2160 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2162 return call_int_hook(inode_unlink, dir, dentry);
2166 * security_inode_symlink() - Check if creating a symbolic link is allowed
2167 * @dir: parent directory
2168 * @dentry: symbolic link
2169 * @old_name: existing filename
2171 * Check the permission to create a symbolic link to a file.
2173 * Return: Returns 0 if permission is granted.
2175 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
2176 const char *old_name)
2178 if (unlikely(IS_PRIVATE(dir)))
2180 return call_int_hook(inode_symlink, dir, dentry, old_name);
2184 * security_inode_mkdir() - Check if creation a new director is allowed
2185 * @dir: parent directory
2186 * @dentry: new directory
2187 * @mode: new directory mode
2189 * Check permissions to create a new directory in the existing directory
2190 * associated with inode structure @dir.
2192 * Return: Returns 0 if permission is granted.
2194 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2196 if (unlikely(IS_PRIVATE(dir)))
2198 return call_int_hook(inode_mkdir, dir, dentry, mode);
2200 EXPORT_SYMBOL_GPL(security_inode_mkdir);
2203 * security_inode_rmdir() - Check if removing a directory is allowed
2204 * @dir: parent directory
2205 * @dentry: directory to be removed
2207 * Check the permission to remove a directory.
2209 * Return: Returns 0 if permission is granted.
2211 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
2213 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2215 return call_int_hook(inode_rmdir, dir, dentry);
2219 * security_inode_mknod() - Check if creating a special file is allowed
2220 * @dir: parent directory
2222 * @mode: new file mode
2223 * @dev: device number
2225 * Check permissions when creating a special file (or a socket or a fifo file
2226 * created via the mknod system call). Note that if mknod operation is being
2227 * done for a regular file, then the create hook will be called and not this
2230 * Return: Returns 0 if permission is granted.
2232 int security_inode_mknod(struct inode *dir, struct dentry *dentry,
2233 umode_t mode, dev_t dev)
2235 if (unlikely(IS_PRIVATE(dir)))
2237 return call_int_hook(inode_mknod, dir, dentry, mode, dev);
2241 * security_inode_rename() - Check if renaming a file is allowed
2242 * @old_dir: parent directory of the old file
2243 * @old_dentry: the old file
2244 * @new_dir: parent directory of the new file
2245 * @new_dentry: the new file
2248 * Check for permission to rename a file or directory.
2250 * Return: Returns 0 if permission is granted.
2252 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
2253 struct inode *new_dir, struct dentry *new_dentry,
2256 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
2257 (d_is_positive(new_dentry) &&
2258 IS_PRIVATE(d_backing_inode(new_dentry)))))
2261 if (flags & RENAME_EXCHANGE) {
2262 int err = call_int_hook(inode_rename, new_dir, new_dentry,
2263 old_dir, old_dentry);
2268 return call_int_hook(inode_rename, old_dir, old_dentry,
2269 new_dir, new_dentry);
2273 * security_inode_readlink() - Check if reading a symbolic link is allowed
2276 * Check the permission to read the symbolic link.
2278 * Return: Returns 0 if permission is granted.
2280 int security_inode_readlink(struct dentry *dentry)
2282 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2284 return call_int_hook(inode_readlink, dentry);
2288 * security_inode_follow_link() - Check if following a symbolic link is allowed
2289 * @dentry: link dentry
2290 * @inode: link inode
2291 * @rcu: true if in RCU-walk mode
2293 * Check permission to follow a symbolic link when looking up a pathname. If
2294 * @rcu is true, @inode is not stable.
2296 * Return: Returns 0 if permission is granted.
2298 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
2301 if (unlikely(IS_PRIVATE(inode)))
2303 return call_int_hook(inode_follow_link, dentry, inode, rcu);
2307 * security_inode_permission() - Check if accessing an inode is allowed
2309 * @mask: access mask
2311 * Check permission before accessing an inode. This hook is called by the
2312 * existing Linux permission function, so a security module can use it to
2313 * provide additional checking for existing Linux permission checks. Notice
2314 * that this hook is called when a file is opened (as well as many other
2315 * operations), whereas the file_security_ops permission hook is called when
2316 * the actual read/write operations are performed.
2318 * Return: Returns 0 if permission is granted.
2320 int security_inode_permission(struct inode *inode, int mask)
2322 if (unlikely(IS_PRIVATE(inode)))
2324 return call_int_hook(inode_permission, inode, mask);
2328 * security_inode_setattr() - Check if setting file attributes is allowed
2329 * @idmap: idmap of the mount
2331 * @attr: new attributes
2333 * Check permission before setting file attributes. Note that the kernel call
2334 * to notify_change is performed from several locations, whenever file
2335 * attributes change (such as when a file is truncated, chown/chmod operations,
2336 * transferring disk quotas, etc).
2338 * Return: Returns 0 if permission is granted.
2340 int security_inode_setattr(struct mnt_idmap *idmap,
2341 struct dentry *dentry, struct iattr *attr)
2343 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2345 return call_int_hook(inode_setattr, idmap, dentry, attr);
2347 EXPORT_SYMBOL_GPL(security_inode_setattr);
2350 * security_inode_post_setattr() - Update the inode after a setattr operation
2351 * @idmap: idmap of the mount
2353 * @ia_valid: file attributes set
2355 * Update inode security field after successful setting file attributes.
2357 void security_inode_post_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2360 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2362 call_void_hook(inode_post_setattr, idmap, dentry, ia_valid);
2366 * security_inode_getattr() - Check if getting file attributes is allowed
2369 * Check permission before obtaining file attributes.
2371 * Return: Returns 0 if permission is granted.
2373 int security_inode_getattr(const struct path *path)
2375 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2377 return call_int_hook(inode_getattr, path);
2381 * security_inode_setxattr() - Check if setting file xattrs is allowed
2382 * @idmap: idmap of the mount
2385 * @value: xattr value
2386 * @size: size of xattr value
2389 * This hook performs the desired permission checks before setting the extended
2390 * attributes (xattrs) on @dentry. It is important to note that we have some
2391 * additional logic before the main LSM implementation calls to detect if we
2392 * need to perform an additional capability check at the LSM layer.
2394 * Normally we enforce a capability check prior to executing the various LSM
2395 * hook implementations, but if a LSM wants to avoid this capability check,
2396 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
2397 * xattrs that it wants to avoid the capability check, leaving the LSM fully
2398 * responsible for enforcing the access control for the specific xattr. If all
2399 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
2400 * or return a 0 (the default return value), the capability check is still
2401 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
2402 * check is performed.
2404 * Return: Returns 0 if permission is granted.
2406 int security_inode_setxattr(struct mnt_idmap *idmap,
2407 struct dentry *dentry, const char *name,
2408 const void *value, size_t size, int flags)
2412 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2415 /* enforce the capability checks at the lsm layer, if needed */
2416 if (!call_int_hook(inode_xattr_skipcap, name)) {
2417 rc = cap_inode_setxattr(dentry, name, value, size, flags);
2422 return call_int_hook(inode_setxattr, idmap, dentry, name, value, size,
2427 * security_inode_set_acl() - Check if setting posix acls is allowed
2428 * @idmap: idmap of the mount
2430 * @acl_name: acl name
2433 * Check permission before setting posix acls, the posix acls in @kacl are
2434 * identified by @acl_name.
2436 * Return: Returns 0 if permission is granted.
2438 int security_inode_set_acl(struct mnt_idmap *idmap,
2439 struct dentry *dentry, const char *acl_name,
2440 struct posix_acl *kacl)
2442 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2444 return call_int_hook(inode_set_acl, idmap, dentry, acl_name, kacl);
2448 * security_inode_post_set_acl() - Update inode security from posix acls set
2450 * @acl_name: acl name
2453 * Update inode security data after successfully setting posix acls on @dentry.
2454 * The posix acls in @kacl are identified by @acl_name.
2456 void security_inode_post_set_acl(struct dentry *dentry, const char *acl_name,
2457 struct posix_acl *kacl)
2459 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2461 call_void_hook(inode_post_set_acl, dentry, acl_name, kacl);
2465 * security_inode_get_acl() - Check if reading posix acls is allowed
2466 * @idmap: idmap of the mount
2468 * @acl_name: acl name
2470 * Check permission before getting osix acls, the posix acls are identified by
2473 * Return: Returns 0 if permission is granted.
2475 int security_inode_get_acl(struct mnt_idmap *idmap,
2476 struct dentry *dentry, const char *acl_name)
2478 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2480 return call_int_hook(inode_get_acl, idmap, dentry, acl_name);
2484 * security_inode_remove_acl() - Check if removing a posix acl is allowed
2485 * @idmap: idmap of the mount
2487 * @acl_name: acl name
2489 * Check permission before removing posix acls, the posix acls are identified
2492 * Return: Returns 0 if permission is granted.
2494 int security_inode_remove_acl(struct mnt_idmap *idmap,
2495 struct dentry *dentry, const char *acl_name)
2497 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2499 return call_int_hook(inode_remove_acl, idmap, dentry, acl_name);
2503 * security_inode_post_remove_acl() - Update inode security after rm posix acls
2504 * @idmap: idmap of the mount
2506 * @acl_name: acl name
2508 * Update inode security data after successfully removing posix acls on
2509 * @dentry in @idmap. The posix acls are identified by @acl_name.
2511 void security_inode_post_remove_acl(struct mnt_idmap *idmap,
2512 struct dentry *dentry, const char *acl_name)
2514 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2516 call_void_hook(inode_post_remove_acl, idmap, dentry, acl_name);
2520 * security_inode_post_setxattr() - Update the inode after a setxattr operation
2523 * @value: xattr value
2524 * @size: xattr value size
2527 * Update inode security field after successful setxattr operation.
2529 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2530 const void *value, size_t size, int flags)
2532 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2534 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2538 * security_inode_getxattr() - Check if xattr access is allowed
2542 * Check permission before obtaining the extended attributes identified by
2543 * @name for @dentry.
2545 * Return: Returns 0 if permission is granted.
2547 int security_inode_getxattr(struct dentry *dentry, const char *name)
2549 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2551 return call_int_hook(inode_getxattr, dentry, name);
2555 * security_inode_listxattr() - Check if listing xattrs is allowed
2558 * Check permission before obtaining the list of extended attribute names for
2561 * Return: Returns 0 if permission is granted.
2563 int security_inode_listxattr(struct dentry *dentry)
2565 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2567 return call_int_hook(inode_listxattr, dentry);
2571 * security_inode_removexattr() - Check if removing an xattr is allowed
2572 * @idmap: idmap of the mount
2576 * This hook performs the desired permission checks before setting the extended
2577 * attributes (xattrs) on @dentry. It is important to note that we have some
2578 * additional logic before the main LSM implementation calls to detect if we
2579 * need to perform an additional capability check at the LSM layer.
2581 * Normally we enforce a capability check prior to executing the various LSM
2582 * hook implementations, but if a LSM wants to avoid this capability check,
2583 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
2584 * xattrs that it wants to avoid the capability check, leaving the LSM fully
2585 * responsible for enforcing the access control for the specific xattr. If all
2586 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
2587 * or return a 0 (the default return value), the capability check is still
2588 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
2589 * check is performed.
2591 * Return: Returns 0 if permission is granted.
2593 int security_inode_removexattr(struct mnt_idmap *idmap,
2594 struct dentry *dentry, const char *name)
2598 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2601 /* enforce the capability checks at the lsm layer, if needed */
2602 if (!call_int_hook(inode_xattr_skipcap, name)) {
2603 rc = cap_inode_removexattr(idmap, dentry, name);
2608 return call_int_hook(inode_removexattr, idmap, dentry, name);
2612 * security_inode_post_removexattr() - Update the inode after a removexattr op
2616 * Update the inode after a successful removexattr operation.
2618 void security_inode_post_removexattr(struct dentry *dentry, const char *name)
2620 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2622 call_void_hook(inode_post_removexattr, dentry, name);
2626 * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2627 * @dentry: associated dentry
2629 * Called when an inode has been changed to determine if
2630 * security_inode_killpriv() should be called.
2632 * Return: Return <0 on error to abort the inode change operation, return 0 if
2633 * security_inode_killpriv() does not need to be called, return >0 if
2634 * security_inode_killpriv() does need to be called.
2636 int security_inode_need_killpriv(struct dentry *dentry)
2638 return call_int_hook(inode_need_killpriv, dentry);
2642 * security_inode_killpriv() - The setuid bit is removed, update LSM state
2643 * @idmap: idmap of the mount
2644 * @dentry: associated dentry
2646 * The @dentry's setuid bit is being removed. Remove similar security labels.
2647 * Called with the dentry->d_inode->i_mutex held.
2649 * Return: Return 0 on success. If error is returned, then the operation
2650 * causing setuid bit removal is failed.
2652 int security_inode_killpriv(struct mnt_idmap *idmap,
2653 struct dentry *dentry)
2655 return call_int_hook(inode_killpriv, idmap, dentry);
2659 * security_inode_getsecurity() - Get the xattr security label of an inode
2660 * @idmap: idmap of the mount
2663 * @buffer: security label buffer
2664 * @alloc: allocation flag
2666 * Retrieve a copy of the extended attribute representation of the security
2667 * label associated with @name for @inode via @buffer. Note that @name is the
2668 * remainder of the attribute name after the security prefix has been removed.
2669 * @alloc is used to specify if the call should return a value via the buffer
2670 * or just the value length.
2672 * Return: Returns size of buffer on success.
2674 int security_inode_getsecurity(struct mnt_idmap *idmap,
2675 struct inode *inode, const char *name,
2676 void **buffer, bool alloc)
2678 if (unlikely(IS_PRIVATE(inode)))
2679 return LSM_RET_DEFAULT(inode_getsecurity);
2681 return call_int_hook(inode_getsecurity, idmap, inode, name, buffer,
2686 * security_inode_setsecurity() - Set the xattr security label of an inode
2689 * @value: security label
2690 * @size: length of security label
2693 * Set the security label associated with @name for @inode from the extended
2694 * attribute value @value. @size indicates the size of the @value in bytes.
2695 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2696 * remainder of the attribute name after the security. prefix has been removed.
2698 * Return: Returns 0 on success.
2700 int security_inode_setsecurity(struct inode *inode, const char *name,
2701 const void *value, size_t size, int flags)
2703 if (unlikely(IS_PRIVATE(inode)))
2704 return LSM_RET_DEFAULT(inode_setsecurity);
2706 return call_int_hook(inode_setsecurity, inode, name, value, size,
2711 * security_inode_listsecurity() - List the xattr security label names
2714 * @buffer_size: size of buffer
2716 * Copy the extended attribute names for the security labels associated with
2717 * @inode into @buffer. The maximum size of @buffer is specified by
2718 * @buffer_size. @buffer may be NULL to request the size of the buffer
2721 * Return: Returns number of bytes used/required on success.
2723 int security_inode_listsecurity(struct inode *inode,
2724 char *buffer, size_t buffer_size)
2726 if (unlikely(IS_PRIVATE(inode)))
2728 return call_int_hook(inode_listsecurity, inode, buffer, buffer_size);
2730 EXPORT_SYMBOL(security_inode_listsecurity);
2733 * security_inode_getlsmprop() - Get an inode's LSM data
2735 * @prop: lsm specific information to return
2737 * Get the lsm specific information associated with the node.
2739 void security_inode_getlsmprop(struct inode *inode, struct lsm_prop *prop)
2741 call_void_hook(inode_getlsmprop, inode, prop);
2745 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2746 * @src: union dentry of copy-up file
2747 * @new: newly created creds
2749 * A file is about to be copied up from lower layer to upper layer of overlay
2750 * filesystem. Security module can prepare a set of new creds and modify as
2751 * need be and return new creds. Caller will switch to new creds temporarily to
2752 * create new file and release newly allocated creds.
2754 * Return: Returns 0 on success or a negative error code on error.
2756 int security_inode_copy_up(struct dentry *src, struct cred **new)
2758 return call_int_hook(inode_copy_up, src, new);
2760 EXPORT_SYMBOL(security_inode_copy_up);
2763 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2764 * @src: union dentry of copy-up file
2767 * Filter the xattrs being copied up when a unioned file is copied up from a
2768 * lower layer to the union/overlay layer. The caller is responsible for
2769 * reading and writing the xattrs, this hook is merely a filter.
2771 * Return: Returns 0 to accept the xattr, -ECANCELED to discard the xattr,
2772 * -EOPNOTSUPP if the security module does not know about attribute,
2773 * or a negative error code to abort the copy up.
2775 int security_inode_copy_up_xattr(struct dentry *src, const char *name)
2779 rc = call_int_hook(inode_copy_up_xattr, src, name);
2780 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2783 return LSM_RET_DEFAULT(inode_copy_up_xattr);
2785 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2788 * security_inode_setintegrity() - Set the inode's integrity data
2790 * @type: type of integrity, e.g. hash digest, signature, etc
2791 * @value: the integrity value
2792 * @size: size of the integrity value
2794 * Register a verified integrity measurement of a inode with LSMs.
2795 * LSMs should free the previously saved data if @value is NULL.
2797 * Return: Returns 0 on success, negative values on failure.
2799 int security_inode_setintegrity(const struct inode *inode,
2800 enum lsm_integrity_type type, const void *value,
2803 return call_int_hook(inode_setintegrity, inode, type, value, size);
2805 EXPORT_SYMBOL(security_inode_setintegrity);
2808 * security_kernfs_init_security() - Init LSM context for a kernfs node
2809 * @kn_dir: parent kernfs node
2810 * @kn: the kernfs node to initialize
2812 * Initialize the security context of a newly created kernfs node based on its
2813 * own and its parent's attributes.
2815 * Return: Returns 0 if permission is granted.
2817 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2818 struct kernfs_node *kn)
2820 return call_int_hook(kernfs_init_security, kn_dir, kn);
2824 * security_file_permission() - Check file permissions
2826 * @mask: requested permissions
2828 * Check file permissions before accessing an open file. This hook is called
2829 * by various operations that read or write files. A security module can use
2830 * this hook to perform additional checking on these operations, e.g. to
2831 * revalidate permissions on use to support privilege bracketing or policy
2832 * changes. Notice that this hook is used when the actual read/write
2833 * operations are performed, whereas the inode_security_ops hook is called when
2834 * a file is opened (as well as many other operations). Although this hook can
2835 * be used to revalidate permissions for various system call operations that
2836 * read or write files, it does not address the revalidation of permissions for
2837 * memory-mapped files. Security modules must handle this separately if they
2838 * need such revalidation.
2840 * Return: Returns 0 if permission is granted.
2842 int security_file_permission(struct file *file, int mask)
2844 return call_int_hook(file_permission, file, mask);
2848 * security_file_alloc() - Allocate and init a file's LSM blob
2851 * Allocate and attach a security structure to the file->f_security field. The
2852 * security field is initialized to NULL when the structure is first created.
2854 * Return: Return 0 if the hook is successful and permission is granted.
2856 int security_file_alloc(struct file *file)
2858 int rc = lsm_file_alloc(file);
2862 rc = call_int_hook(file_alloc_security, file);
2864 security_file_free(file);
2869 * security_file_release() - Perform actions before releasing the file ref
2872 * Perform actions before releasing the last reference to a file.
2874 void security_file_release(struct file *file)
2876 call_void_hook(file_release, file);
2880 * security_file_free() - Free a file's LSM blob
2883 * Deallocate and free any security structures stored in file->f_security.
2885 void security_file_free(struct file *file)
2889 call_void_hook(file_free_security, file);
2891 blob = file->f_security;
2893 file->f_security = NULL;
2894 kmem_cache_free(lsm_file_cache, blob);
2899 * security_file_ioctl() - Check if an ioctl is allowed
2900 * @file: associated file
2902 * @arg: ioctl arguments
2904 * Check permission for an ioctl operation on @file. Note that @arg sometimes
2905 * represents a user space pointer; in other cases, it may be a simple integer
2906 * value. When @arg represents a user space pointer, it should never be used
2907 * by the security module.
2909 * Return: Returns 0 if permission is granted.
2911 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2913 return call_int_hook(file_ioctl, file, cmd, arg);
2915 EXPORT_SYMBOL_GPL(security_file_ioctl);
2918 * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
2919 * @file: associated file
2921 * @arg: ioctl arguments
2923 * Compat version of security_file_ioctl() that correctly handles 32-bit
2924 * processes running on 64-bit kernels.
2926 * Return: Returns 0 if permission is granted.
2928 int security_file_ioctl_compat(struct file *file, unsigned int cmd,
2931 return call_int_hook(file_ioctl_compat, file, cmd, arg);
2933 EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
2935 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2938 * Does we have PROT_READ and does the application expect
2939 * it to imply PROT_EXEC? If not, nothing to talk about...
2941 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2943 if (!(current->personality & READ_IMPLIES_EXEC))
2946 * if that's an anonymous mapping, let it.
2949 return prot | PROT_EXEC;
2951 * ditto if it's not on noexec mount, except that on !MMU we need
2952 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2954 if (!path_noexec(&file->f_path)) {
2956 if (file->f_op->mmap_capabilities) {
2957 unsigned caps = file->f_op->mmap_capabilities(file);
2958 if (!(caps & NOMMU_MAP_EXEC))
2962 return prot | PROT_EXEC;
2964 /* anything on noexec mount won't get PROT_EXEC */
2969 * security_mmap_file() - Check if mmap'ing a file is allowed
2971 * @prot: protection applied by the kernel
2974 * Check permissions for a mmap operation. The @file may be NULL, e.g. if
2975 * mapping anonymous memory.
2977 * Return: Returns 0 if permission is granted.
2979 int security_mmap_file(struct file *file, unsigned long prot,
2980 unsigned long flags)
2982 return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot),
2987 * security_mmap_addr() - Check if mmap'ing an address is allowed
2990 * Check permissions for a mmap operation at @addr.
2992 * Return: Returns 0 if permission is granted.
2994 int security_mmap_addr(unsigned long addr)
2996 return call_int_hook(mmap_addr, addr);
3000 * security_file_mprotect() - Check if changing memory protections is allowed
3001 * @vma: memory region
3002 * @reqprot: application requested protection
3003 * @prot: protection applied by the kernel
3005 * Check permissions before changing memory access permissions.
3007 * Return: Returns 0 if permission is granted.
3009 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
3012 return call_int_hook(file_mprotect, vma, reqprot, prot);
3016 * security_file_lock() - Check if a file lock is allowed
3018 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
3020 * Check permission before performing file locking operations. Note the hook
3021 * mediates both flock and fcntl style locks.
3023 * Return: Returns 0 if permission is granted.
3025 int security_file_lock(struct file *file, unsigned int cmd)
3027 return call_int_hook(file_lock, file, cmd);
3031 * security_file_fcntl() - Check if fcntl() op is allowed
3033 * @cmd: fcntl command
3034 * @arg: command argument
3036 * Check permission before allowing the file operation specified by @cmd from
3037 * being performed on the file @file. Note that @arg sometimes represents a
3038 * user space pointer; in other cases, it may be a simple integer value. When
3039 * @arg represents a user space pointer, it should never be used by the
3042 * Return: Returns 0 if permission is granted.
3044 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
3046 return call_int_hook(file_fcntl, file, cmd, arg);
3050 * security_file_set_fowner() - Set the file owner info in the LSM blob
3053 * Save owner security information (typically from current->security) in
3054 * file->f_security for later use by the send_sigiotask hook.
3056 * This hook is called with file->f_owner.lock held.
3058 * Return: Returns 0 on success.
3060 void security_file_set_fowner(struct file *file)
3062 call_void_hook(file_set_fowner, file);
3066 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
3068 * @fown: signal sender
3069 * @sig: signal to be sent, SIGIO is sent if 0
3071 * Check permission for the file owner @fown to send SIGIO or SIGURG to the
3072 * process @tsk. Note that this hook is sometimes called from interrupt. Note
3073 * that the fown_struct, @fown, is never outside the context of a struct file,
3074 * so the file structure (and associated security information) can always be
3075 * obtained: container_of(fown, struct file, f_owner).
3077 * Return: Returns 0 if permission is granted.
3079 int security_file_send_sigiotask(struct task_struct *tsk,
3080 struct fown_struct *fown, int sig)
3082 return call_int_hook(file_send_sigiotask, tsk, fown, sig);
3086 * security_file_receive() - Check if receiving a file via IPC is allowed
3087 * @file: file being received
3089 * This hook allows security modules to control the ability of a process to
3090 * receive an open file descriptor via socket IPC.
3092 * Return: Returns 0 if permission is granted.
3094 int security_file_receive(struct file *file)
3096 return call_int_hook(file_receive, file);
3100 * security_file_open() - Save open() time state for late use by the LSM
3103 * Save open-time permission checking state for later use upon file_permission,
3104 * and recheck access if anything has changed since inode_permission.
3106 * We can check if a file is opened for execution (e.g. execve(2) call), either
3107 * directly or indirectly (e.g. ELF's ld.so) by checking file->f_flags &
3110 * Return: Returns 0 if permission is granted.
3112 int security_file_open(struct file *file)
3114 return call_int_hook(file_open, file);
3118 * security_file_post_open() - Evaluate a file after it has been opened
3120 * @mask: access mask
3122 * Evaluate an opened file and the access mask requested with open(). The hook
3123 * is useful for LSMs that require the file content to be available in order to
3126 * Return: Returns 0 if permission is granted.
3128 int security_file_post_open(struct file *file, int mask)
3130 return call_int_hook(file_post_open, file, mask);
3132 EXPORT_SYMBOL_GPL(security_file_post_open);
3135 * security_file_truncate() - Check if truncating a file is allowed
3138 * Check permission before truncating a file, i.e. using ftruncate. Note that
3139 * truncation permission may also be checked based on the path, using the
3140 * @path_truncate hook.
3142 * Return: Returns 0 if permission is granted.
3144 int security_file_truncate(struct file *file)
3146 return call_int_hook(file_truncate, file);
3150 * security_task_alloc() - Allocate a task's LSM blob
3152 * @clone_flags: flags indicating what is being shared
3154 * Handle allocation of task-related resources.
3156 * Return: Returns a zero on success, negative values on failure.
3158 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
3160 int rc = lsm_task_alloc(task);
3164 rc = call_int_hook(task_alloc, task, clone_flags);
3166 security_task_free(task);
3171 * security_task_free() - Free a task's LSM blob and related resources
3174 * Handle release of task-related resources. Note that this can be called from
3175 * interrupt context.
3177 void security_task_free(struct task_struct *task)
3179 call_void_hook(task_free, task);
3181 kfree(task->security);
3182 task->security = NULL;
3186 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
3187 * @cred: credentials
3190 * Only allocate sufficient memory and attach to @cred such that
3191 * cred_transfer() will not get ENOMEM.
3193 * Return: Returns 0 on success, negative values on failure.
3195 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3197 int rc = lsm_cred_alloc(cred, gfp);
3202 rc = call_int_hook(cred_alloc_blank, cred, gfp);
3204 security_cred_free(cred);
3209 * security_cred_free() - Free the cred's LSM blob and associated resources
3210 * @cred: credentials
3212 * Deallocate and clear the cred->security field in a set of credentials.
3214 void security_cred_free(struct cred *cred)
3217 * There is a failure case in prepare_creds() that
3218 * may result in a call here with ->security being NULL.
3220 if (unlikely(cred->security == NULL))
3223 call_void_hook(cred_free, cred);
3225 kfree(cred->security);
3226 cred->security = NULL;
3230 * security_prepare_creds() - Prepare a new set of credentials
3231 * @new: new credentials
3232 * @old: original credentials
3235 * Prepare a new set of credentials by copying the data from the old set.
3237 * Return: Returns 0 on success, negative values on failure.
3239 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
3241 int rc = lsm_cred_alloc(new, gfp);
3246 rc = call_int_hook(cred_prepare, new, old, gfp);
3248 security_cred_free(new);
3253 * security_transfer_creds() - Transfer creds
3254 * @new: target credentials
3255 * @old: original credentials
3257 * Transfer data from original creds to new creds.
3259 void security_transfer_creds(struct cred *new, const struct cred *old)
3261 call_void_hook(cred_transfer, new, old);
3265 * security_cred_getsecid() - Get the secid from a set of credentials
3267 * @secid: secid value
3269 * Retrieve the security identifier of the cred structure @c. In case of
3270 * failure, @secid will be set to zero.
3272 void security_cred_getsecid(const struct cred *c, u32 *secid)
3275 call_void_hook(cred_getsecid, c, secid);
3277 EXPORT_SYMBOL(security_cred_getsecid);
3280 * security_cred_getlsmprop() - Get the LSM data from a set of credentials
3282 * @prop: destination for the LSM data
3284 * Retrieve the security data of the cred structure @c. In case of
3285 * failure, @prop will be cleared.
3287 void security_cred_getlsmprop(const struct cred *c, struct lsm_prop *prop)
3290 call_void_hook(cred_getlsmprop, c, prop);
3292 EXPORT_SYMBOL(security_cred_getlsmprop);
3295 * security_kernel_act_as() - Set the kernel credentials to act as secid
3299 * Set the credentials for a kernel service to act as (subjective context).
3300 * The current task must be the one that nominated @secid.
3302 * Return: Returns 0 if successful.
3304 int security_kernel_act_as(struct cred *new, u32 secid)
3306 return call_int_hook(kernel_act_as, new, secid);
3310 * security_kernel_create_files_as() - Set file creation context using an inode
3311 * @new: target credentials
3312 * @inode: reference inode
3314 * Set the file creation context in a set of credentials to be the same as the
3315 * objective context of the specified inode. The current task must be the one
3316 * that nominated @inode.
3318 * Return: Returns 0 if successful.
3320 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
3322 return call_int_hook(kernel_create_files_as, new, inode);
3326 * security_kernel_module_request() - Check if loading a module is allowed
3327 * @kmod_name: module name
3329 * Ability to trigger the kernel to automatically upcall to userspace for
3330 * userspace to load a kernel module with the given name.
3332 * Return: Returns 0 if successful.
3334 int security_kernel_module_request(char *kmod_name)
3336 return call_int_hook(kernel_module_request, kmod_name);
3340 * security_kernel_read_file() - Read a file specified by userspace
3342 * @id: file identifier
3343 * @contents: trust if security_kernel_post_read_file() will be called
3345 * Read a file specified by userspace.
3347 * Return: Returns 0 if permission is granted.
3349 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
3352 return call_int_hook(kernel_read_file, file, id, contents);
3354 EXPORT_SYMBOL_GPL(security_kernel_read_file);
3357 * security_kernel_post_read_file() - Read a file specified by userspace
3359 * @buf: file contents
3360 * @size: size of file contents
3361 * @id: file identifier
3363 * Read a file specified by userspace. This must be paired with a prior call
3364 * to security_kernel_read_file() call that indicated this hook would also be
3365 * called, see security_kernel_read_file() for more information.
3367 * Return: Returns 0 if permission is granted.
3369 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3370 enum kernel_read_file_id id)
3372 return call_int_hook(kernel_post_read_file, file, buf, size, id);
3374 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3377 * security_kernel_load_data() - Load data provided by userspace
3378 * @id: data identifier
3379 * @contents: true if security_kernel_post_load_data() will be called
3381 * Load data provided by userspace.
3383 * Return: Returns 0 if permission is granted.
3385 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
3387 return call_int_hook(kernel_load_data, id, contents);
3389 EXPORT_SYMBOL_GPL(security_kernel_load_data);
3392 * security_kernel_post_load_data() - Load userspace data from a non-file source
3394 * @size: size of data
3395 * @id: data identifier
3396 * @description: text description of data, specific to the id value
3398 * Load data provided by a non-file source (usually userspace buffer). This
3399 * must be paired with a prior security_kernel_load_data() call that indicated
3400 * this hook would also be called, see security_kernel_load_data() for more
3403 * Return: Returns 0 if permission is granted.
3405 int security_kernel_post_load_data(char *buf, loff_t size,
3406 enum kernel_load_data_id id,
3409 return call_int_hook(kernel_post_load_data, buf, size, id, description);
3411 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3414 * security_task_fix_setuid() - Update LSM with new user id attributes
3415 * @new: updated credentials
3416 * @old: credentials being replaced
3417 * @flags: LSM_SETID_* flag values
3419 * Update the module's state after setting one or more of the user identity
3420 * attributes of the current process. The @flags parameter indicates which of
3421 * the set*uid system calls invoked this hook. If @new is the set of
3422 * credentials that will be installed. Modifications should be made to this
3423 * rather than to @current->cred.
3425 * Return: Returns 0 on success.
3427 int security_task_fix_setuid(struct cred *new, const struct cred *old,
3430 return call_int_hook(task_fix_setuid, new, old, flags);
3434 * security_task_fix_setgid() - Update LSM with new group id attributes
3435 * @new: updated credentials
3436 * @old: credentials being replaced
3437 * @flags: LSM_SETID_* flag value
3439 * Update the module's state after setting one or more of the group identity
3440 * attributes of the current process. The @flags parameter indicates which of
3441 * the set*gid system calls invoked this hook. @new is the set of credentials
3442 * that will be installed. Modifications should be made to this rather than to
3445 * Return: Returns 0 on success.
3447 int security_task_fix_setgid(struct cred *new, const struct cred *old,
3450 return call_int_hook(task_fix_setgid, new, old, flags);
3454 * security_task_fix_setgroups() - Update LSM with new supplementary groups
3455 * @new: updated credentials
3456 * @old: credentials being replaced
3458 * Update the module's state after setting the supplementary group identity
3459 * attributes of the current process. @new is the set of credentials that will
3460 * be installed. Modifications should be made to this rather than to
3463 * Return: Returns 0 on success.
3465 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3467 return call_int_hook(task_fix_setgroups, new, old);
3471 * security_task_setpgid() - Check if setting the pgid is allowed
3472 * @p: task being modified
3475 * Check permission before setting the process group identifier of the process
3478 * Return: Returns 0 if permission is granted.
3480 int security_task_setpgid(struct task_struct *p, pid_t pgid)
3482 return call_int_hook(task_setpgid, p, pgid);
3486 * security_task_getpgid() - Check if getting the pgid is allowed
3489 * Check permission before getting the process group identifier of the process
3492 * Return: Returns 0 if permission is granted.
3494 int security_task_getpgid(struct task_struct *p)
3496 return call_int_hook(task_getpgid, p);
3500 * security_task_getsid() - Check if getting the session id is allowed
3503 * Check permission before getting the session identifier of the process @p.
3505 * Return: Returns 0 if permission is granted.
3507 int security_task_getsid(struct task_struct *p)
3509 return call_int_hook(task_getsid, p);
3513 * security_current_getlsmprop_subj() - Current task's subjective LSM data
3514 * @prop: lsm specific information
3516 * Retrieve the subjective security identifier of the current task and return
3519 void security_current_getlsmprop_subj(struct lsm_prop *prop)
3522 call_void_hook(current_getlsmprop_subj, prop);
3524 EXPORT_SYMBOL(security_current_getlsmprop_subj);
3527 * security_task_getlsmprop_obj() - Get a task's objective LSM data
3529 * @prop: lsm specific information
3531 * Retrieve the objective security identifier of the task_struct in @p and
3532 * return it in @prop.
3534 void security_task_getlsmprop_obj(struct task_struct *p, struct lsm_prop *prop)
3537 call_void_hook(task_getlsmprop_obj, p, prop);
3539 EXPORT_SYMBOL(security_task_getlsmprop_obj);
3542 * security_task_setnice() - Check if setting a task's nice value is allowed
3546 * Check permission before setting the nice value of @p to @nice.
3548 * Return: Returns 0 if permission is granted.
3550 int security_task_setnice(struct task_struct *p, int nice)
3552 return call_int_hook(task_setnice, p, nice);
3556 * security_task_setioprio() - Check if setting a task's ioprio is allowed
3558 * @ioprio: ioprio value
3560 * Check permission before setting the ioprio value of @p to @ioprio.
3562 * Return: Returns 0 if permission is granted.
3564 int security_task_setioprio(struct task_struct *p, int ioprio)
3566 return call_int_hook(task_setioprio, p, ioprio);
3570 * security_task_getioprio() - Check if getting a task's ioprio is allowed
3573 * Check permission before getting the ioprio value of @p.
3575 * Return: Returns 0 if permission is granted.
3577 int security_task_getioprio(struct task_struct *p)
3579 return call_int_hook(task_getioprio, p);
3583 * security_task_prlimit() - Check if get/setting resources limits is allowed
3584 * @cred: current task credentials
3585 * @tcred: target task credentials
3586 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3588 * Check permission before getting and/or setting the resource limits of
3591 * Return: Returns 0 if permission is granted.
3593 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3596 return call_int_hook(task_prlimit, cred, tcred, flags);
3600 * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3601 * @p: target task's group leader
3602 * @resource: resource whose limit is being set
3603 * @new_rlim: new resource limit
3605 * Check permission before setting the resource limits of process @p for
3606 * @resource to @new_rlim. The old resource limit values can be examined by
3607 * dereferencing (p->signal->rlim + resource).
3609 * Return: Returns 0 if permission is granted.
3611 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3612 struct rlimit *new_rlim)
3614 return call_int_hook(task_setrlimit, p, resource, new_rlim);
3618 * security_task_setscheduler() - Check if setting sched policy/param is allowed
3621 * Check permission before setting scheduling policy and/or parameters of
3624 * Return: Returns 0 if permission is granted.
3626 int security_task_setscheduler(struct task_struct *p)
3628 return call_int_hook(task_setscheduler, p);
3632 * security_task_getscheduler() - Check if getting scheduling info is allowed
3635 * Check permission before obtaining scheduling information for process @p.
3637 * Return: Returns 0 if permission is granted.
3639 int security_task_getscheduler(struct task_struct *p)
3641 return call_int_hook(task_getscheduler, p);
3645 * security_task_movememory() - Check if moving memory is allowed
3648 * Check permission before moving memory owned by process @p.
3650 * Return: Returns 0 if permission is granted.
3652 int security_task_movememory(struct task_struct *p)
3654 return call_int_hook(task_movememory, p);
3658 * security_task_kill() - Check if sending a signal is allowed
3659 * @p: target process
3660 * @info: signal information
3661 * @sig: signal value
3662 * @cred: credentials of the signal sender, NULL if @current
3664 * Check permission before sending signal @sig to @p. @info can be NULL, the
3665 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
3666 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3667 * the kernel and should typically be permitted. SIGIO signals are handled
3668 * separately by the send_sigiotask hook in file_security_ops.
3670 * Return: Returns 0 if permission is granted.
3672 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3673 int sig, const struct cred *cred)
3675 return call_int_hook(task_kill, p, info, sig, cred);
3679 * security_task_prctl() - Check if a prctl op is allowed
3680 * @option: operation
3686 * Check permission before performing a process control operation on the
3689 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3690 * to cause prctl() to return immediately with that value.
3692 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3693 unsigned long arg4, unsigned long arg5)
3696 int rc = LSM_RET_DEFAULT(task_prctl);
3697 struct lsm_static_call *scall;
3699 lsm_for_each_hook(scall, task_prctl) {
3700 thisrc = scall->hl->hook.task_prctl(option, arg2, arg3, arg4, arg5);
3701 if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3711 * security_task_to_inode() - Set the security attributes of a task's inode
3715 * Set the security attributes for an inode based on an associated task's
3716 * security attributes, e.g. for /proc/pid inodes.
3718 void security_task_to_inode(struct task_struct *p, struct inode *inode)
3720 call_void_hook(task_to_inode, p, inode);
3724 * security_create_user_ns() - Check if creating a new userns is allowed
3725 * @cred: prepared creds
3727 * Check permission prior to creating a new user namespace.
3729 * Return: Returns 0 if successful, otherwise < 0 error code.
3731 int security_create_user_ns(const struct cred *cred)
3733 return call_int_hook(userns_create, cred);
3737 * security_ipc_permission() - Check if sysv ipc access is allowed
3738 * @ipcp: ipc permission structure
3739 * @flag: requested permissions
3741 * Check permissions for access to IPC.
3743 * Return: Returns 0 if permission is granted.
3745 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3747 return call_int_hook(ipc_permission, ipcp, flag);
3751 * security_ipc_getlsmprop() - Get the sysv ipc object LSM data
3752 * @ipcp: ipc permission structure
3753 * @prop: pointer to lsm information
3755 * Get the lsm information associated with the ipc object.
3758 void security_ipc_getlsmprop(struct kern_ipc_perm *ipcp, struct lsm_prop *prop)
3761 call_void_hook(ipc_getlsmprop, ipcp, prop);
3765 * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3766 * @msg: message structure
3768 * Allocate and attach a security structure to the msg->security field. The
3769 * security field is initialized to NULL when the structure is first created.
3771 * Return: Return 0 if operation was successful and permission is granted.
3773 int security_msg_msg_alloc(struct msg_msg *msg)
3775 int rc = lsm_msg_msg_alloc(msg);
3779 rc = call_int_hook(msg_msg_alloc_security, msg);
3781 security_msg_msg_free(msg);
3786 * security_msg_msg_free() - Free a sysv ipc message LSM blob
3787 * @msg: message structure
3789 * Deallocate the security structure for this message.
3791 void security_msg_msg_free(struct msg_msg *msg)
3793 call_void_hook(msg_msg_free_security, msg);
3794 kfree(msg->security);
3795 msg->security = NULL;
3799 * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3800 * @msq: sysv ipc permission structure
3802 * Allocate and attach a security structure to @msg. The security field is
3803 * initialized to NULL when the structure is first created.
3805 * Return: Returns 0 if operation was successful and permission is granted.
3807 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
3809 int rc = lsm_ipc_alloc(msq);
3813 rc = call_int_hook(msg_queue_alloc_security, msq);
3815 security_msg_queue_free(msq);
3820 * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3821 * @msq: sysv ipc permission structure
3823 * Deallocate security field @perm->security for the message queue.
3825 void security_msg_queue_free(struct kern_ipc_perm *msq)
3827 call_void_hook(msg_queue_free_security, msq);
3828 kfree(msq->security);
3829 msq->security = NULL;
3833 * security_msg_queue_associate() - Check if a msg queue operation is allowed
3834 * @msq: sysv ipc permission structure
3835 * @msqflg: operation flags
3837 * Check permission when a message queue is requested through the msgget system
3838 * call. This hook is only called when returning the message queue identifier
3839 * for an existing message queue, not when a new message queue is created.
3841 * Return: Return 0 if permission is granted.
3843 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
3845 return call_int_hook(msg_queue_associate, msq, msqflg);
3849 * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3850 * @msq: sysv ipc permission structure
3853 * Check permission when a message control operation specified by @cmd is to be
3854 * performed on the message queue with permissions.
3856 * Return: Returns 0 if permission is granted.
3858 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
3860 return call_int_hook(msg_queue_msgctl, msq, cmd);
3864 * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3865 * @msq: sysv ipc permission structure
3867 * @msqflg: operation flags
3869 * Check permission before a message, @msg, is enqueued on the message queue
3870 * with permissions specified in @msq.
3872 * Return: Returns 0 if permission is granted.
3874 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
3875 struct msg_msg *msg, int msqflg)
3877 return call_int_hook(msg_queue_msgsnd, msq, msg, msqflg);
3881 * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3882 * @msq: sysv ipc permission structure
3884 * @target: target task
3885 * @type: type of message requested
3886 * @mode: operation flags
3888 * Check permission before a message, @msg, is removed from the message queue.
3889 * The @target task structure contains a pointer to the process that will be
3890 * receiving the message (not equal to the current process when inline receives
3891 * are being performed).
3893 * Return: Returns 0 if permission is granted.
3895 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
3896 struct task_struct *target, long type, int mode)
3898 return call_int_hook(msg_queue_msgrcv, msq, msg, target, type, mode);
3902 * security_shm_alloc() - Allocate a sysv shm LSM blob
3903 * @shp: sysv ipc permission structure
3905 * Allocate and attach a security structure to the @shp security field. The
3906 * security field is initialized to NULL when the structure is first created.
3908 * Return: Returns 0 if operation was successful and permission is granted.
3910 int security_shm_alloc(struct kern_ipc_perm *shp)
3912 int rc = lsm_ipc_alloc(shp);
3916 rc = call_int_hook(shm_alloc_security, shp);
3918 security_shm_free(shp);
3923 * security_shm_free() - Free a sysv shm LSM blob
3924 * @shp: sysv ipc permission structure
3926 * Deallocate the security structure @perm->security for the memory segment.
3928 void security_shm_free(struct kern_ipc_perm *shp)
3930 call_void_hook(shm_free_security, shp);
3931 kfree(shp->security);
3932 shp->security = NULL;
3936 * security_shm_associate() - Check if a sysv shm operation is allowed
3937 * @shp: sysv ipc permission structure
3938 * @shmflg: operation flags
3940 * Check permission when a shared memory region is requested through the shmget
3941 * system call. This hook is only called when returning the shared memory
3942 * region identifier for an existing region, not when a new shared memory
3943 * region is created.
3945 * Return: Returns 0 if permission is granted.
3947 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
3949 return call_int_hook(shm_associate, shp, shmflg);
3953 * security_shm_shmctl() - Check if a sysv shm operation is allowed
3954 * @shp: sysv ipc permission structure
3957 * Check permission when a shared memory control operation specified by @cmd is
3958 * to be performed on the shared memory region with permissions in @shp.
3960 * Return: Return 0 if permission is granted.
3962 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
3964 return call_int_hook(shm_shmctl, shp, cmd);
3968 * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3969 * @shp: sysv ipc permission structure
3970 * @shmaddr: address of memory region to attach
3971 * @shmflg: operation flags
3973 * Check permissions prior to allowing the shmat system call to attach the
3974 * shared memory segment with permissions @shp to the data segment of the
3975 * calling process. The attaching address is specified by @shmaddr.
3977 * Return: Returns 0 if permission is granted.
3979 int security_shm_shmat(struct kern_ipc_perm *shp,
3980 char __user *shmaddr, int shmflg)
3982 return call_int_hook(shm_shmat, shp, shmaddr, shmflg);
3986 * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3987 * @sma: sysv ipc permission structure
3989 * Allocate and attach a security structure to the @sma security field. The
3990 * security field is initialized to NULL when the structure is first created.
3992 * Return: Returns 0 if operation was successful and permission is granted.
3994 int security_sem_alloc(struct kern_ipc_perm *sma)
3996 int rc = lsm_ipc_alloc(sma);
4000 rc = call_int_hook(sem_alloc_security, sma);
4002 security_sem_free(sma);
4007 * security_sem_free() - Free a sysv semaphore LSM blob
4008 * @sma: sysv ipc permission structure
4010 * Deallocate security structure @sma->security for the semaphore.
4012 void security_sem_free(struct kern_ipc_perm *sma)
4014 call_void_hook(sem_free_security, sma);
4015 kfree(sma->security);
4016 sma->security = NULL;
4020 * security_sem_associate() - Check if a sysv semaphore operation is allowed
4021 * @sma: sysv ipc permission structure
4022 * @semflg: operation flags
4024 * Check permission when a semaphore is requested through the semget system
4025 * call. This hook is only called when returning the semaphore identifier for
4026 * an existing semaphore, not when a new one must be created.
4028 * Return: Returns 0 if permission is granted.
4030 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
4032 return call_int_hook(sem_associate, sma, semflg);
4036 * security_sem_semctl() - Check if a sysv semaphore operation is allowed
4037 * @sma: sysv ipc permission structure
4040 * Check permission when a semaphore operation specified by @cmd is to be
4041 * performed on the semaphore.
4043 * Return: Returns 0 if permission is granted.
4045 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
4047 return call_int_hook(sem_semctl, sma, cmd);
4051 * security_sem_semop() - Check if a sysv semaphore operation is allowed
4052 * @sma: sysv ipc permission structure
4053 * @sops: operations to perform
4054 * @nsops: number of operations
4055 * @alter: flag indicating changes will be made
4057 * Check permissions before performing operations on members of the semaphore
4058 * set. If the @alter flag is nonzero, the semaphore set may be modified.
4060 * Return: Returns 0 if permission is granted.
4062 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
4063 unsigned nsops, int alter)
4065 return call_int_hook(sem_semop, sma, sops, nsops, alter);
4069 * security_d_instantiate() - Populate an inode's LSM state based on a dentry
4073 * Fill in @inode security information for a @dentry if allowed.
4075 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
4077 if (unlikely(inode && IS_PRIVATE(inode)))
4079 call_void_hook(d_instantiate, dentry, inode);
4081 EXPORT_SYMBOL(security_d_instantiate);
4084 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
4088 * security_getselfattr - Read an LSM attribute of the current process.
4089 * @attr: which attribute to return
4090 * @uctx: the user-space destination for the information, or NULL
4091 * @size: pointer to the size of space available to receive the data
4092 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
4093 * attributes associated with the LSM identified in the passed @ctx be
4096 * A NULL value for @uctx can be used to get both the number of attributes
4097 * and the size of the data.
4099 * Returns the number of attributes found on success, negative value
4100 * on error. @size is reset to the total size of the data.
4101 * If @size is insufficient to contain the data -E2BIG is returned.
4103 int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
4104 u32 __user *size, u32 flags)
4106 struct lsm_static_call *scall;
4107 struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, };
4108 u8 __user *base = (u8 __user *)uctx;
4112 bool toobig = false;
4113 bool single = false;
4117 if (attr == LSM_ATTR_UNDEF)
4121 if (get_user(left, size))
4126 * Only flag supported is LSM_FLAG_SINGLE
4128 if (flags != LSM_FLAG_SINGLE || !uctx)
4130 if (copy_from_user(&lctx, uctx, sizeof(lctx)))
4133 * If the LSM ID isn't specified it is an error.
4135 if (lctx.id == LSM_ID_UNDEF)
4141 * In the usual case gather all the data from the LSMs.
4142 * In the single case only get the data from the LSM specified.
4144 lsm_for_each_hook(scall, getselfattr) {
4145 if (single && lctx.id != scall->hl->lsmid->id)
4149 uctx = (struct lsm_ctx __user *)(base + total);
4150 rc = scall->hl->hook.getselfattr(attr, uctx, &entrysize, flags);
4151 if (rc == -EOPNOTSUPP)
4167 if (put_user(total, size))
4172 return LSM_RET_DEFAULT(getselfattr);
4177 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
4181 * security_setselfattr - Set an LSM attribute on the current process.
4182 * @attr: which attribute to set
4183 * @uctx: the user-space source for the information
4184 * @size: the size of the data
4185 * @flags: reserved for future use, must be 0
4187 * Set an LSM attribute for the current process. The LSM, attribute
4188 * and new value are included in @uctx.
4190 * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT
4191 * if the user buffer is inaccessible, E2BIG if size is too big, or an
4192 * LSM specific failure.
4194 int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
4195 u32 size, u32 flags)
4197 struct lsm_static_call *scall;
4198 struct lsm_ctx *lctx;
4199 int rc = LSM_RET_DEFAULT(setselfattr);
4204 if (size < sizeof(*lctx))
4206 if (size > PAGE_SIZE)
4209 lctx = memdup_user(uctx, size);
4211 return PTR_ERR(lctx);
4213 if (size < lctx->len ||
4214 check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) ||
4215 lctx->len < required_len) {
4220 lsm_for_each_hook(scall, setselfattr)
4221 if ((scall->hl->lsmid->id) == lctx->id) {
4222 rc = scall->hl->hook.setselfattr(attr, lctx, size, flags);
4232 * security_getprocattr() - Read an attribute for a task
4234 * @lsmid: LSM identification
4235 * @name: attribute name
4236 * @value: attribute value
4238 * Read attribute @name for task @p and store it into @value if allowed.
4240 * Return: Returns the length of @value on success, a negative value otherwise.
4242 int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
4245 struct lsm_static_call *scall;
4247 lsm_for_each_hook(scall, getprocattr) {
4248 if (lsmid != 0 && lsmid != scall->hl->lsmid->id)
4250 return scall->hl->hook.getprocattr(p, name, value);
4252 return LSM_RET_DEFAULT(getprocattr);
4256 * security_setprocattr() - Set an attribute for a task
4257 * @lsmid: LSM identification
4258 * @name: attribute name
4259 * @value: attribute value
4260 * @size: attribute value size
4262 * Write (set) the current task's attribute @name to @value, size @size if
4265 * Return: Returns bytes written on success, a negative value otherwise.
4267 int security_setprocattr(int lsmid, const char *name, void *value, size_t size)
4269 struct lsm_static_call *scall;
4271 lsm_for_each_hook(scall, setprocattr) {
4272 if (lsmid != 0 && lsmid != scall->hl->lsmid->id)
4274 return scall->hl->hook.setprocattr(name, value, size);
4276 return LSM_RET_DEFAULT(setprocattr);
4280 * security_netlink_send() - Save info and check if netlink sending is allowed
4281 * @sk: sending socket
4282 * @skb: netlink message
4284 * Save security information for a netlink message so that permission checking
4285 * can be performed when the message is processed. The security information
4286 * can be saved using the eff_cap field of the netlink_skb_parms structure.
4287 * Also may be used to provide fine grained control over message transmission.
4289 * Return: Returns 0 if the information was successfully saved and message is
4290 * allowed to be transmitted.
4292 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
4294 return call_int_hook(netlink_send, sk, skb);
4298 * security_ismaclabel() - Check if the named attribute is a MAC label
4299 * @name: full extended attribute name
4301 * Check if the extended attribute specified by @name represents a MAC label.
4303 * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
4305 int security_ismaclabel(const char *name)
4307 return call_int_hook(ismaclabel, name);
4309 EXPORT_SYMBOL(security_ismaclabel);
4312 * security_secid_to_secctx() - Convert a secid to a secctx
4314 * @cp: the LSM context
4316 * Convert secid to security context. If @cp is NULL the length of the
4317 * result will be returned, but no data will be returned. This
4318 * does mean that the length could change between calls to check the length and
4319 * the next call which actually allocates and returns the data.
4321 * Return: Return length of data on success, error on failure.
4323 int security_secid_to_secctx(u32 secid, struct lsm_context *cp)
4325 return call_int_hook(secid_to_secctx, secid, cp);
4327 EXPORT_SYMBOL(security_secid_to_secctx);
4330 * security_lsmprop_to_secctx() - Convert a lsm_prop to a secctx
4331 * @prop: lsm specific information
4332 * @cp: the LSM context
4334 * Convert a @prop entry to security context. If @cp is NULL the
4335 * length of the result will be returned. This does mean that the
4336 * length could change between calls to check the length and the
4337 * next call which actually allocates and returns the @cp.
4339 * Return: Return length of data on success, error on failure.
4341 int security_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp)
4343 return call_int_hook(lsmprop_to_secctx, prop, cp);
4345 EXPORT_SYMBOL(security_lsmprop_to_secctx);
4348 * security_secctx_to_secid() - Convert a secctx to a secid
4350 * @seclen: length of secctx
4353 * Convert security context to secid.
4355 * Return: Returns 0 on success, error on failure.
4357 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4360 return call_int_hook(secctx_to_secid, secdata, seclen, secid);
4362 EXPORT_SYMBOL(security_secctx_to_secid);
4365 * security_release_secctx() - Free a secctx buffer
4366 * @cp: the security context
4368 * Release the security context.
4370 void security_release_secctx(struct lsm_context *cp)
4372 call_void_hook(release_secctx, cp);
4373 memset(cp, 0, sizeof(*cp));
4375 EXPORT_SYMBOL(security_release_secctx);
4378 * security_inode_invalidate_secctx() - Invalidate an inode's security label
4381 * Notify the security module that it must revalidate the security context of
4384 void security_inode_invalidate_secctx(struct inode *inode)
4386 call_void_hook(inode_invalidate_secctx, inode);
4388 EXPORT_SYMBOL(security_inode_invalidate_secctx);
4391 * security_inode_notifysecctx() - Notify the LSM of an inode's security label
4394 * @ctxlen: length of secctx
4396 * Notify the security module of what the security context of an inode should
4397 * be. Initializes the incore security context managed by the security module
4398 * for this inode. Example usage: NFS client invokes this hook to initialize
4399 * the security context in its incore inode to the value provided by the server
4400 * for the file when the server returned the file's attributes to the client.
4401 * Must be called with inode->i_mutex locked.
4403 * Return: Returns 0 on success, error on failure.
4405 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4407 return call_int_hook(inode_notifysecctx, inode, ctx, ctxlen);
4409 EXPORT_SYMBOL(security_inode_notifysecctx);
4412 * security_inode_setsecctx() - Change the security label of an inode
4415 * @ctxlen: length of secctx
4417 * Change the security context of an inode. Updates the incore security
4418 * context managed by the security module and invokes the fs code as needed
4419 * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
4420 * context. Example usage: NFS server invokes this hook to change the security
4421 * context in its incore inode and on the backing filesystem to a value
4422 * provided by the client on a SETATTR operation. Must be called with
4423 * inode->i_mutex locked.
4425 * Return: Returns 0 on success, error on failure.
4427 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4429 return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen);
4431 EXPORT_SYMBOL(security_inode_setsecctx);
4434 * security_inode_getsecctx() - Get the security label of an inode
4436 * @cp: security context
4438 * On success, returns 0 and fills out @cp with the security context
4439 * for the given @inode.
4441 * Return: Returns 0 on success, error on failure.
4443 int security_inode_getsecctx(struct inode *inode, struct lsm_context *cp)
4445 memset(cp, 0, sizeof(*cp));
4446 return call_int_hook(inode_getsecctx, inode, cp);
4448 EXPORT_SYMBOL(security_inode_getsecctx);
4450 #ifdef CONFIG_WATCH_QUEUE
4452 * security_post_notification() - Check if a watch notification can be posted
4453 * @w_cred: credentials of the task that set the watch
4454 * @cred: credentials of the task which triggered the watch
4455 * @n: the notification
4457 * Check to see if a watch notification can be posted to a particular queue.
4459 * Return: Returns 0 if permission is granted.
4461 int security_post_notification(const struct cred *w_cred,
4462 const struct cred *cred,
4463 struct watch_notification *n)
4465 return call_int_hook(post_notification, w_cred, cred, n);
4467 #endif /* CONFIG_WATCH_QUEUE */
4469 #ifdef CONFIG_KEY_NOTIFICATIONS
4471 * security_watch_key() - Check if a task is allowed to watch for key events
4472 * @key: the key to watch
4474 * Check to see if a process is allowed to watch for event notifications from
4477 * Return: Returns 0 if permission is granted.
4479 int security_watch_key(struct key *key)
4481 return call_int_hook(watch_key, key);
4483 #endif /* CONFIG_KEY_NOTIFICATIONS */
4485 #ifdef CONFIG_SECURITY_NETWORK
4487 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
4488 * @sock: originating sock
4492 * Check permissions before establishing a Unix domain stream connection
4493 * between @sock and @other.
4495 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4496 * Linux provides an alternative to the conventional file name space for Unix
4497 * domain sockets. Whereas binding and connecting to sockets in the file name
4498 * space is mediated by the typical file permissions (and caught by the mknod
4499 * and permission hooks in inode_security_ops), binding and connecting to
4500 * sockets in the abstract name space is completely unmediated. Sufficient
4501 * control of Unix domain sockets in the abstract name space isn't possible
4502 * using only the socket layer hooks, since we need to know the actual target
4503 * socket, which is not looked up until we are inside the af_unix code.
4505 * Return: Returns 0 if permission is granted.
4507 int security_unix_stream_connect(struct sock *sock, struct sock *other,
4510 return call_int_hook(unix_stream_connect, sock, other, newsk);
4512 EXPORT_SYMBOL(security_unix_stream_connect);
4515 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
4516 * @sock: originating sock
4519 * Check permissions before connecting or sending datagrams from @sock to
4522 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4523 * Linux provides an alternative to the conventional file name space for Unix
4524 * domain sockets. Whereas binding and connecting to sockets in the file name
4525 * space is mediated by the typical file permissions (and caught by the mknod
4526 * and permission hooks in inode_security_ops), binding and connecting to
4527 * sockets in the abstract name space is completely unmediated. Sufficient
4528 * control of Unix domain sockets in the abstract name space isn't possible
4529 * using only the socket layer hooks, since we need to know the actual target
4530 * socket, which is not looked up until we are inside the af_unix code.
4532 * Return: Returns 0 if permission is granted.
4534 int security_unix_may_send(struct socket *sock, struct socket *other)
4536 return call_int_hook(unix_may_send, sock, other);
4538 EXPORT_SYMBOL(security_unix_may_send);
4541 * security_socket_create() - Check if creating a new socket is allowed
4542 * @family: protocol family
4543 * @type: communications type
4544 * @protocol: requested protocol
4545 * @kern: set to 1 if a kernel socket is requested
4547 * Check permissions prior to creating a new socket.
4549 * Return: Returns 0 if permission is granted.
4551 int security_socket_create(int family, int type, int protocol, int kern)
4553 return call_int_hook(socket_create, family, type, protocol, kern);
4557 * security_socket_post_create() - Initialize a newly created socket
4559 * @family: protocol family
4560 * @type: communications type
4561 * @protocol: requested protocol
4562 * @kern: set to 1 if a kernel socket is requested
4564 * This hook allows a module to update or allocate a per-socket security
4565 * structure. Note that the security field was not added directly to the socket
4566 * structure, but rather, the socket security information is stored in the
4567 * associated inode. Typically, the inode alloc_security hook will allocate
4568 * and attach security information to SOCK_INODE(sock)->i_security. This hook
4569 * may be used to update the SOCK_INODE(sock)->i_security field with additional
4570 * information that wasn't available when the inode was allocated.
4572 * Return: Returns 0 if permission is granted.
4574 int security_socket_post_create(struct socket *sock, int family,
4575 int type, int protocol, int kern)
4577 return call_int_hook(socket_post_create, sock, family, type,
4582 * security_socket_socketpair() - Check if creating a socketpair is allowed
4583 * @socka: first socket
4584 * @sockb: second socket
4586 * Check permissions before creating a fresh pair of sockets.
4588 * Return: Returns 0 if permission is granted and the connection was
4591 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4593 return call_int_hook(socket_socketpair, socka, sockb);
4595 EXPORT_SYMBOL(security_socket_socketpair);
4598 * security_socket_bind() - Check if a socket bind operation is allowed
4600 * @address: requested bind address
4601 * @addrlen: length of address
4603 * Check permission before socket protocol layer bind operation is performed
4604 * and the socket @sock is bound to the address specified in the @address
4607 * Return: Returns 0 if permission is granted.
4609 int security_socket_bind(struct socket *sock,
4610 struct sockaddr *address, int addrlen)
4612 return call_int_hook(socket_bind, sock, address, addrlen);
4616 * security_socket_connect() - Check if a socket connect operation is allowed
4618 * @address: address of remote connection point
4619 * @addrlen: length of address
4621 * Check permission before socket protocol layer connect operation attempts to
4622 * connect socket @sock to a remote address, @address.
4624 * Return: Returns 0 if permission is granted.
4626 int security_socket_connect(struct socket *sock,
4627 struct sockaddr *address, int addrlen)
4629 return call_int_hook(socket_connect, sock, address, addrlen);
4633 * security_socket_listen() - Check if a socket is allowed to listen
4635 * @backlog: connection queue size
4637 * Check permission before socket protocol layer listen operation.
4639 * Return: Returns 0 if permission is granted.
4641 int security_socket_listen(struct socket *sock, int backlog)
4643 return call_int_hook(socket_listen, sock, backlog);
4647 * security_socket_accept() - Check if a socket is allowed to accept connections
4648 * @sock: listening socket
4649 * @newsock: newly creation connection socket
4651 * Check permission before accepting a new connection. Note that the new
4652 * socket, @newsock, has been created and some information copied to it, but
4653 * the accept operation has not actually been performed.
4655 * Return: Returns 0 if permission is granted.
4657 int security_socket_accept(struct socket *sock, struct socket *newsock)
4659 return call_int_hook(socket_accept, sock, newsock);
4663 * security_socket_sendmsg() - Check if sending a message is allowed
4664 * @sock: sending socket
4665 * @msg: message to send
4666 * @size: size of message
4668 * Check permission before transmitting a message to another socket.
4670 * Return: Returns 0 if permission is granted.
4672 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
4674 return call_int_hook(socket_sendmsg, sock, msg, size);
4678 * security_socket_recvmsg() - Check if receiving a message is allowed
4679 * @sock: receiving socket
4680 * @msg: message to receive
4681 * @size: size of message
4682 * @flags: operational flags
4684 * Check permission before receiving a message from a socket.
4686 * Return: Returns 0 if permission is granted.
4688 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4689 int size, int flags)
4691 return call_int_hook(socket_recvmsg, sock, msg, size, flags);
4695 * security_socket_getsockname() - Check if reading the socket addr is allowed
4698 * Check permission before reading the local address (name) of the socket
4701 * Return: Returns 0 if permission is granted.
4703 int security_socket_getsockname(struct socket *sock)
4705 return call_int_hook(socket_getsockname, sock);
4709 * security_socket_getpeername() - Check if reading the peer's addr is allowed
4712 * Check permission before the remote address (name) of a socket object.
4714 * Return: Returns 0 if permission is granted.
4716 int security_socket_getpeername(struct socket *sock)
4718 return call_int_hook(socket_getpeername, sock);
4722 * security_socket_getsockopt() - Check if reading a socket option is allowed
4724 * @level: option's protocol level
4725 * @optname: option name
4727 * Check permissions before retrieving the options associated with socket
4730 * Return: Returns 0 if permission is granted.
4732 int security_socket_getsockopt(struct socket *sock, int level, int optname)
4734 return call_int_hook(socket_getsockopt, sock, level, optname);
4738 * security_socket_setsockopt() - Check if setting a socket option is allowed
4740 * @level: option's protocol level
4741 * @optname: option name
4743 * Check permissions before setting the options associated with socket @sock.
4745 * Return: Returns 0 if permission is granted.
4747 int security_socket_setsockopt(struct socket *sock, int level, int optname)
4749 return call_int_hook(socket_setsockopt, sock, level, optname);
4753 * security_socket_shutdown() - Checks if shutting down the socket is allowed
4755 * @how: flag indicating how sends and receives are handled
4757 * Checks permission before all or part of a connection on the socket @sock is
4760 * Return: Returns 0 if permission is granted.
4762 int security_socket_shutdown(struct socket *sock, int how)
4764 return call_int_hook(socket_shutdown, sock, how);
4768 * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4769 * @sk: destination sock
4770 * @skb: incoming packet
4772 * Check permissions on incoming network packets. This hook is distinct from
4773 * Netfilter's IP input hooks since it is the first time that the incoming
4774 * sk_buff @skb has been associated with a particular socket, @sk. Must not
4775 * sleep inside this hook because some callers hold spinlocks.
4777 * Return: Returns 0 if permission is granted.
4779 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4781 return call_int_hook(socket_sock_rcv_skb, sk, skb);
4783 EXPORT_SYMBOL(security_sock_rcv_skb);
4786 * security_socket_getpeersec_stream() - Get the remote peer label
4788 * @optval: destination buffer
4789 * @optlen: size of peer label copied into the buffer
4790 * @len: maximum size of the destination buffer
4792 * This hook allows the security module to provide peer socket security state
4793 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4794 * For tcp sockets this can be meaningful if the socket is associated with an
4797 * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4800 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4801 sockptr_t optlen, unsigned int len)
4803 return call_int_hook(socket_getpeersec_stream, sock, optval, optlen,
4808 * security_socket_getpeersec_dgram() - Get the remote peer label
4810 * @skb: datagram packet
4811 * @secid: remote peer label secid
4813 * This hook allows the security module to provide peer socket security state
4814 * for udp sockets on a per-packet basis to userspace via getsockopt
4815 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4816 * option via getsockopt. It can then retrieve the security state returned by
4817 * this hook for a packet via the SCM_SECURITY ancillary message type.
4819 * Return: Returns 0 on success, error on failure.
4821 int security_socket_getpeersec_dgram(struct socket *sock,
4822 struct sk_buff *skb, u32 *secid)
4824 return call_int_hook(socket_getpeersec_dgram, sock, skb, secid);
4826 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4829 * lsm_sock_alloc - allocate a composite sock blob
4830 * @sock: the sock that needs a blob
4831 * @gfp: allocation mode
4833 * Allocate the sock blob for all the modules
4835 * Returns 0, or -ENOMEM if memory can't be allocated.
4837 static int lsm_sock_alloc(struct sock *sock, gfp_t gfp)
4839 return lsm_blob_alloc(&sock->sk_security, blob_sizes.lbs_sock, gfp);
4843 * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4845 * @family: protocol family
4846 * @priority: gfp flags
4848 * Allocate and attach a security structure to the sk->sk_security field, which
4849 * is used to copy security attributes between local stream sockets.
4851 * Return: Returns 0 on success, error on failure.
4853 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4855 int rc = lsm_sock_alloc(sk, priority);
4859 rc = call_int_hook(sk_alloc_security, sk, family, priority);
4861 security_sk_free(sk);
4866 * security_sk_free() - Free the sock's LSM blob
4869 * Deallocate security structure.
4871 void security_sk_free(struct sock *sk)
4873 call_void_hook(sk_free_security, sk);
4874 kfree(sk->sk_security);
4875 sk->sk_security = NULL;
4879 * security_sk_clone() - Clone a sock's LSM state
4880 * @sk: original sock
4881 * @newsk: target sock
4883 * Clone/copy security structure.
4885 void security_sk_clone(const struct sock *sk, struct sock *newsk)
4887 call_void_hook(sk_clone_security, sk, newsk);
4889 EXPORT_SYMBOL(security_sk_clone);
4892 * security_sk_classify_flow() - Set a flow's secid based on socket
4893 * @sk: original socket
4894 * @flic: target flow
4896 * Set the target flow's secid to socket's secid.
4898 void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
4900 call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
4902 EXPORT_SYMBOL(security_sk_classify_flow);
4905 * security_req_classify_flow() - Set a flow's secid based on request_sock
4906 * @req: request_sock
4907 * @flic: target flow
4909 * Sets @flic's secid to @req's secid.
4911 void security_req_classify_flow(const struct request_sock *req,
4912 struct flowi_common *flic)
4914 call_void_hook(req_classify_flow, req, flic);
4916 EXPORT_SYMBOL(security_req_classify_flow);
4919 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4920 * @sk: sock being grafted
4921 * @parent: target parent socket
4923 * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
4924 * LSM state from @parent.
4926 void security_sock_graft(struct sock *sk, struct socket *parent)
4928 call_void_hook(sock_graft, sk, parent);
4930 EXPORT_SYMBOL(security_sock_graft);
4933 * security_inet_conn_request() - Set request_sock state using incoming connect
4934 * @sk: parent listening sock
4935 * @skb: incoming connection
4936 * @req: new request_sock
4938 * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4940 * Return: Returns 0 if permission is granted.
4942 int security_inet_conn_request(const struct sock *sk,
4943 struct sk_buff *skb, struct request_sock *req)
4945 return call_int_hook(inet_conn_request, sk, skb, req);
4947 EXPORT_SYMBOL(security_inet_conn_request);
4950 * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4952 * @req: connection request_sock
4954 * Set that LSM state of @sock using the LSM state from @req.
4956 void security_inet_csk_clone(struct sock *newsk,
4957 const struct request_sock *req)
4959 call_void_hook(inet_csk_clone, newsk, req);
4963 * security_inet_conn_established() - Update sock's LSM state with connection
4965 * @skb: connection packet
4967 * Update @sock's LSM state to represent a new connection from @skb.
4969 void security_inet_conn_established(struct sock *sk,
4970 struct sk_buff *skb)
4972 call_void_hook(inet_conn_established, sk, skb);
4974 EXPORT_SYMBOL(security_inet_conn_established);
4977 * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4978 * @secid: new secmark value
4980 * Check if the process should be allowed to relabel packets to @secid.
4982 * Return: Returns 0 if permission is granted.
4984 int security_secmark_relabel_packet(u32 secid)
4986 return call_int_hook(secmark_relabel_packet, secid);
4988 EXPORT_SYMBOL(security_secmark_relabel_packet);
4991 * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4993 * Tells the LSM to increment the number of secmark labeling rules loaded.
4995 void security_secmark_refcount_inc(void)
4997 call_void_hook(secmark_refcount_inc);
4999 EXPORT_SYMBOL(security_secmark_refcount_inc);
5002 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
5004 * Tells the LSM to decrement the number of secmark labeling rules loaded.
5006 void security_secmark_refcount_dec(void)
5008 call_void_hook(secmark_refcount_dec);
5010 EXPORT_SYMBOL(security_secmark_refcount_dec);
5013 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
5014 * @security: pointer to the LSM blob
5016 * This hook allows a module to allocate a security structure for a TUN device,
5017 * returning the pointer in @security.
5019 * Return: Returns a zero on success, negative values on failure.
5021 int security_tun_dev_alloc_security(void **security)
5025 rc = lsm_blob_alloc(security, blob_sizes.lbs_tun_dev, GFP_KERNEL);
5029 rc = call_int_hook(tun_dev_alloc_security, *security);
5036 EXPORT_SYMBOL(security_tun_dev_alloc_security);
5039 * security_tun_dev_free_security() - Free a TUN device LSM blob
5040 * @security: LSM blob
5042 * This hook allows a module to free the security structure for a TUN device.
5044 void security_tun_dev_free_security(void *security)
5048 EXPORT_SYMBOL(security_tun_dev_free_security);
5051 * security_tun_dev_create() - Check if creating a TUN device is allowed
5053 * Check permissions prior to creating a new TUN device.
5055 * Return: Returns 0 if permission is granted.
5057 int security_tun_dev_create(void)
5059 return call_int_hook(tun_dev_create);
5061 EXPORT_SYMBOL(security_tun_dev_create);
5064 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
5065 * @security: TUN device LSM blob
5067 * Check permissions prior to attaching to a TUN device queue.
5069 * Return: Returns 0 if permission is granted.
5071 int security_tun_dev_attach_queue(void *security)
5073 return call_int_hook(tun_dev_attach_queue, security);
5075 EXPORT_SYMBOL(security_tun_dev_attach_queue);
5078 * security_tun_dev_attach() - Update TUN device LSM state on attach
5079 * @sk: associated sock
5080 * @security: TUN device LSM blob
5082 * This hook can be used by the module to update any security state associated
5083 * with the TUN device's sock structure.
5085 * Return: Returns 0 if permission is granted.
5087 int security_tun_dev_attach(struct sock *sk, void *security)
5089 return call_int_hook(tun_dev_attach, sk, security);
5091 EXPORT_SYMBOL(security_tun_dev_attach);
5094 * security_tun_dev_open() - Update TUN device LSM state on open
5095 * @security: TUN device LSM blob
5097 * This hook can be used by the module to update any security state associated
5098 * with the TUN device's security structure.
5100 * Return: Returns 0 if permission is granted.
5102 int security_tun_dev_open(void *security)
5104 return call_int_hook(tun_dev_open, security);
5106 EXPORT_SYMBOL(security_tun_dev_open);
5109 * security_sctp_assoc_request() - Update the LSM on a SCTP association req
5110 * @asoc: SCTP association
5111 * @skb: packet requesting the association
5113 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
5115 * Return: Returns 0 on success, error on failure.
5117 int security_sctp_assoc_request(struct sctp_association *asoc,
5118 struct sk_buff *skb)
5120 return call_int_hook(sctp_assoc_request, asoc, skb);
5122 EXPORT_SYMBOL(security_sctp_assoc_request);
5125 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
5127 * @optname: SCTP option to validate
5128 * @address: list of IP addresses to validate
5129 * @addrlen: length of the address list
5131 * Validiate permissions required for each address associated with sock @sk.
5132 * Depending on @optname, the addresses will be treated as either a connect or
5133 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
5134 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
5136 * Return: Returns 0 on success, error on failure.
5138 int security_sctp_bind_connect(struct sock *sk, int optname,
5139 struct sockaddr *address, int addrlen)
5141 return call_int_hook(sctp_bind_connect, sk, optname, address, addrlen);
5143 EXPORT_SYMBOL(security_sctp_bind_connect);
5146 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
5147 * @asoc: SCTP association
5148 * @sk: original sock
5149 * @newsk: target sock
5151 * Called whenever a new socket is created by accept(2) (i.e. a TCP style
5152 * socket) or when a socket is 'peeled off' e.g userspace calls
5155 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
5158 call_void_hook(sctp_sk_clone, asoc, sk, newsk);
5160 EXPORT_SYMBOL(security_sctp_sk_clone);
5163 * security_sctp_assoc_established() - Update LSM state when assoc established
5164 * @asoc: SCTP association
5165 * @skb: packet establishing the association
5167 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
5170 * Return: Returns 0 if permission is granted.
5172 int security_sctp_assoc_established(struct sctp_association *asoc,
5173 struct sk_buff *skb)
5175 return call_int_hook(sctp_assoc_established, asoc, skb);
5177 EXPORT_SYMBOL(security_sctp_assoc_established);
5180 * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
5181 * @sk: the owning MPTCP socket
5182 * @ssk: the new subflow
5184 * Update the labeling for the given MPTCP subflow, to match the one of the
5185 * owning MPTCP socket. This hook has to be called after the socket creation and
5186 * initialization via the security_socket_create() and
5187 * security_socket_post_create() LSM hooks.
5189 * Return: Returns 0 on success or a negative error code on failure.
5191 int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
5193 return call_int_hook(mptcp_add_subflow, sk, ssk);
5196 #endif /* CONFIG_SECURITY_NETWORK */
5198 #ifdef CONFIG_SECURITY_INFINIBAND
5200 * security_ib_pkey_access() - Check if access to an IB pkey is allowed
5202 * @subnet_prefix: subnet prefix of the port
5205 * Check permission to access a pkey when modifying a QP.
5207 * Return: Returns 0 if permission is granted.
5209 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
5211 return call_int_hook(ib_pkey_access, sec, subnet_prefix, pkey);
5213 EXPORT_SYMBOL(security_ib_pkey_access);
5216 * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
5218 * @dev_name: IB device name
5219 * @port_num: port number
5221 * Check permissions to send and receive SMPs on a end port.
5223 * Return: Returns 0 if permission is granted.
5225 int security_ib_endport_manage_subnet(void *sec,
5226 const char *dev_name, u8 port_num)
5228 return call_int_hook(ib_endport_manage_subnet, sec, dev_name, port_num);
5230 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
5233 * security_ib_alloc_security() - Allocate an Infiniband LSM blob
5236 * Allocate a security structure for Infiniband objects.
5238 * Return: Returns 0 on success, non-zero on failure.
5240 int security_ib_alloc_security(void **sec)
5244 rc = lsm_blob_alloc(sec, blob_sizes.lbs_ib, GFP_KERNEL);
5248 rc = call_int_hook(ib_alloc_security, *sec);
5255 EXPORT_SYMBOL(security_ib_alloc_security);
5258 * security_ib_free_security() - Free an Infiniband LSM blob
5261 * Deallocate an Infiniband security structure.
5263 void security_ib_free_security(void *sec)
5267 EXPORT_SYMBOL(security_ib_free_security);
5268 #endif /* CONFIG_SECURITY_INFINIBAND */
5270 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5272 * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
5273 * @ctxp: xfrm security context being added to the SPD
5274 * @sec_ctx: security label provided by userspace
5277 * Allocate a security structure to the xp->security field; the security field
5278 * is initialized to NULL when the xfrm_policy is allocated.
5280 * Return: Return 0 if operation was successful.
5282 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
5283 struct xfrm_user_sec_ctx *sec_ctx,
5286 return call_int_hook(xfrm_policy_alloc_security, ctxp, sec_ctx, gfp);
5288 EXPORT_SYMBOL(security_xfrm_policy_alloc);
5291 * security_xfrm_policy_clone() - Clone xfrm policy LSM state
5292 * @old_ctx: xfrm security context
5293 * @new_ctxp: target xfrm security context
5295 * Allocate a security structure in new_ctxp that contains the information from
5296 * the old_ctx structure.
5298 * Return: Return 0 if operation was successful.
5300 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
5301 struct xfrm_sec_ctx **new_ctxp)
5303 return call_int_hook(xfrm_policy_clone_security, old_ctx, new_ctxp);
5307 * security_xfrm_policy_free() - Free a xfrm security context
5308 * @ctx: xfrm security context
5310 * Free LSM resources associated with @ctx.
5312 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
5314 call_void_hook(xfrm_policy_free_security, ctx);
5316 EXPORT_SYMBOL(security_xfrm_policy_free);
5319 * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
5320 * @ctx: xfrm security context
5322 * Authorize deletion of a SPD entry.
5324 * Return: Returns 0 if permission is granted.
5326 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
5328 return call_int_hook(xfrm_policy_delete_security, ctx);
5332 * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
5333 * @x: xfrm state being added to the SAD
5334 * @sec_ctx: security label provided by userspace
5336 * Allocate a security structure to the @x->security field; the security field
5337 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5338 * correspond to @sec_ctx.
5340 * Return: Return 0 if operation was successful.
5342 int security_xfrm_state_alloc(struct xfrm_state *x,
5343 struct xfrm_user_sec_ctx *sec_ctx)
5345 return call_int_hook(xfrm_state_alloc, x, sec_ctx);
5347 EXPORT_SYMBOL(security_xfrm_state_alloc);
5350 * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
5351 * @x: xfrm state being added to the SAD
5352 * @polsec: associated policy's security context
5353 * @secid: secid from the flow
5355 * Allocate a security structure to the x->security field; the security field
5356 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5357 * correspond to secid.
5359 * Return: Returns 0 if operation was successful.
5361 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
5362 struct xfrm_sec_ctx *polsec, u32 secid)
5364 return call_int_hook(xfrm_state_alloc_acquire, x, polsec, secid);
5368 * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
5371 * Authorize deletion of x->security.
5373 * Return: Returns 0 if permission is granted.
5375 int security_xfrm_state_delete(struct xfrm_state *x)
5377 return call_int_hook(xfrm_state_delete_security, x);
5379 EXPORT_SYMBOL(security_xfrm_state_delete);
5382 * security_xfrm_state_free() - Free a xfrm state
5385 * Deallocate x->security.
5387 void security_xfrm_state_free(struct xfrm_state *x)
5389 call_void_hook(xfrm_state_free_security, x);
5393 * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
5394 * @ctx: target xfrm security context
5395 * @fl_secid: flow secid used to authorize access
5397 * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
5398 * packet. The hook is called when selecting either a per-socket policy or a
5399 * generic xfrm policy.
5401 * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
5404 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
5406 return call_int_hook(xfrm_policy_lookup, ctx, fl_secid);
5410 * security_xfrm_state_pol_flow_match() - Check for a xfrm match
5411 * @x: xfrm state to match
5412 * @xp: xfrm policy to check for a match
5413 * @flic: flow to check for a match.
5415 * Check @xp and @flic for a match with @x.
5417 * Return: Returns 1 if there is a match.
5419 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
5420 struct xfrm_policy *xp,
5421 const struct flowi_common *flic)
5423 struct lsm_static_call *scall;
5424 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
5427 * Since this function is expected to return 0 or 1, the judgment
5428 * becomes difficult if multiple LSMs supply this call. Fortunately,
5429 * we can use the first LSM's judgment because currently only SELinux
5430 * supplies this call.
5432 * For speed optimization, we explicitly break the loop rather than
5435 lsm_for_each_hook(scall, xfrm_state_pol_flow_match) {
5436 rc = scall->hl->hook.xfrm_state_pol_flow_match(x, xp, flic);
5443 * security_xfrm_decode_session() - Determine the xfrm secid for a packet
5447 * Decode the packet in @skb and return the security label in @secid.
5449 * Return: Return 0 if all xfrms used have the same secid.
5451 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
5453 return call_int_hook(xfrm_decode_session, skb, secid, 1);
5456 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
5458 int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
5463 EXPORT_SYMBOL(security_skb_classify_flow);
5464 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
5468 * security_key_alloc() - Allocate and initialize a kernel key LSM blob
5470 * @cred: credentials
5471 * @flags: allocation flags
5473 * Permit allocation of a key and assign security data. Note that key does not
5474 * have a serial number assigned at this point.
5476 * Return: Return 0 if permission is granted, -ve error otherwise.
5478 int security_key_alloc(struct key *key, const struct cred *cred,
5479 unsigned long flags)
5481 int rc = lsm_key_alloc(key);
5485 rc = call_int_hook(key_alloc, key, cred, flags);
5487 security_key_free(key);
5492 * security_key_free() - Free a kernel key LSM blob
5495 * Notification of destruction; free security data.
5497 void security_key_free(struct key *key)
5499 kfree(key->security);
5500 key->security = NULL;
5504 * security_key_permission() - Check if a kernel key operation is allowed
5505 * @key_ref: key reference
5506 * @cred: credentials of actor requesting access
5507 * @need_perm: requested permissions
5509 * See whether a specific operational right is granted to a process on a key.
5511 * Return: Return 0 if permission is granted, -ve error otherwise.
5513 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
5514 enum key_need_perm need_perm)
5516 return call_int_hook(key_permission, key_ref, cred, need_perm);
5520 * security_key_getsecurity() - Get the key's security label
5522 * @buffer: security label buffer
5524 * Get a textual representation of the security context attached to a key for
5525 * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the
5526 * storage for the NUL-terminated string and the caller should free it.
5528 * Return: Returns the length of @buffer (including terminating NUL) or -ve if
5529 * an error occurs. May also return 0 (and a NULL buffer pointer) if
5530 * there is no security label assigned to the key.
5532 int security_key_getsecurity(struct key *key, char **buffer)
5535 return call_int_hook(key_getsecurity, key, buffer);
5539 * security_key_post_create_or_update() - Notification of key create or update
5540 * @keyring: keyring to which the key is linked to
5541 * @key: created or updated key
5542 * @payload: data used to instantiate or update the key
5543 * @payload_len: length of payload
5545 * @create: flag indicating whether the key was created or updated
5547 * Notify the caller of a key creation or update.
5549 void security_key_post_create_or_update(struct key *keyring, struct key *key,
5550 const void *payload, size_t payload_len,
5551 unsigned long flags, bool create)
5553 call_void_hook(key_post_create_or_update, keyring, key, payload,
5554 payload_len, flags, create);
5556 #endif /* CONFIG_KEYS */
5560 * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5561 * @field: audit action
5562 * @op: rule operator
5563 * @rulestr: rule context
5564 * @lsmrule: receive buffer for audit rule struct
5565 * @gfp: GFP flag used for kmalloc
5567 * Allocate and initialize an LSM audit rule structure.
5569 * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5572 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
5575 return call_int_hook(audit_rule_init, field, op, rulestr, lsmrule, gfp);
5579 * security_audit_rule_known() - Check if an audit rule contains LSM fields
5580 * @krule: audit rule
5582 * Specifies whether given @krule contains any fields related to the current
5585 * Return: Returns 1 in case of relation found, 0 otherwise.
5587 int security_audit_rule_known(struct audit_krule *krule)
5589 return call_int_hook(audit_rule_known, krule);
5593 * security_audit_rule_free() - Free an LSM audit rule struct
5594 * @lsmrule: audit rule struct
5596 * Deallocate the LSM audit rule structure previously allocated by
5597 * audit_rule_init().
5599 void security_audit_rule_free(void *lsmrule)
5601 call_void_hook(audit_rule_free, lsmrule);
5605 * security_audit_rule_match() - Check if a label matches an audit rule
5606 * @prop: security label
5607 * @field: LSM audit field
5608 * @op: matching operator
5609 * @lsmrule: audit rule
5611 * Determine if given @secid matches a rule previously approved by
5612 * security_audit_rule_known().
5614 * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5617 int security_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
5620 return call_int_hook(audit_rule_match, prop, field, op, lsmrule);
5622 #endif /* CONFIG_AUDIT */
5624 #ifdef CONFIG_BPF_SYSCALL
5626 * security_bpf() - Check if the bpf syscall operation is allowed
5628 * @attr: bpf attribute
5631 * Do a initial check for all bpf syscalls after the attribute is copied into
5632 * the kernel. The actual security module can implement their own rules to
5633 * check the specific cmd they need.
5635 * Return: Returns 0 if permission is granted.
5637 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5639 return call_int_hook(bpf, cmd, attr, size);
5643 * security_bpf_map() - Check if access to a bpf map is allowed
5647 * Do a check when the kernel generates and returns a file descriptor for eBPF
5650 * Return: Returns 0 if permission is granted.
5652 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5654 return call_int_hook(bpf_map, map, fmode);
5658 * security_bpf_prog() - Check if access to a bpf program is allowed
5659 * @prog: bpf program
5661 * Do a check when the kernel generates and returns a file descriptor for eBPF
5664 * Return: Returns 0 if permission is granted.
5666 int security_bpf_prog(struct bpf_prog *prog)
5668 return call_int_hook(bpf_prog, prog);
5672 * security_bpf_map_create() - Check if BPF map creation is allowed
5673 * @map: BPF map object
5674 * @attr: BPF syscall attributes used to create BPF map
5675 * @token: BPF token used to grant user access
5677 * Do a check when the kernel creates a new BPF map. This is also the
5678 * point where LSM blob is allocated for LSMs that need them.
5680 * Return: Returns 0 on success, error on failure.
5682 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
5683 struct bpf_token *token)
5685 return call_int_hook(bpf_map_create, map, attr, token);
5689 * security_bpf_prog_load() - Check if loading of BPF program is allowed
5690 * @prog: BPF program object
5691 * @attr: BPF syscall attributes used to create BPF program
5692 * @token: BPF token used to grant user access to BPF subsystem
5694 * Perform an access control check when the kernel loads a BPF program and
5695 * allocates associated BPF program object. This hook is also responsible for
5696 * allocating any required LSM state for the BPF program.
5698 * Return: Returns 0 on success, error on failure.
5700 int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5701 struct bpf_token *token)
5703 return call_int_hook(bpf_prog_load, prog, attr, token);
5707 * security_bpf_token_create() - Check if creating of BPF token is allowed
5708 * @token: BPF token object
5709 * @attr: BPF syscall attributes used to create BPF token
5710 * @path: path pointing to BPF FS mount point from which BPF token is created
5712 * Do a check when the kernel instantiates a new BPF token object from BPF FS
5713 * instance. This is also the point where LSM blob can be allocated for LSMs.
5715 * Return: Returns 0 on success, error on failure.
5717 int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
5718 const struct path *path)
5720 return call_int_hook(bpf_token_create, token, attr, path);
5724 * security_bpf_token_cmd() - Check if BPF token is allowed to delegate
5725 * requested BPF syscall command
5726 * @token: BPF token object
5727 * @cmd: BPF syscall command requested to be delegated by BPF token
5729 * Do a check when the kernel decides whether provided BPF token should allow
5730 * delegation of requested BPF syscall command.
5732 * Return: Returns 0 on success, error on failure.
5734 int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
5736 return call_int_hook(bpf_token_cmd, token, cmd);
5740 * security_bpf_token_capable() - Check if BPF token is allowed to delegate
5741 * requested BPF-related capability
5742 * @token: BPF token object
5743 * @cap: capabilities requested to be delegated by BPF token
5745 * Do a check when the kernel decides whether provided BPF token should allow
5746 * delegation of requested BPF-related capabilities.
5748 * Return: Returns 0 on success, error on failure.
5750 int security_bpf_token_capable(const struct bpf_token *token, int cap)
5752 return call_int_hook(bpf_token_capable, token, cap);
5756 * security_bpf_map_free() - Free a bpf map's LSM blob
5759 * Clean up the security information stored inside bpf map.
5761 void security_bpf_map_free(struct bpf_map *map)
5763 call_void_hook(bpf_map_free, map);
5767 * security_bpf_prog_free() - Free a BPF program's LSM blob
5768 * @prog: BPF program struct
5770 * Clean up the security information stored inside BPF program.
5772 void security_bpf_prog_free(struct bpf_prog *prog)
5774 call_void_hook(bpf_prog_free, prog);
5778 * security_bpf_token_free() - Free a BPF token's LSM blob
5779 * @token: BPF token struct
5781 * Clean up the security information stored inside BPF token.
5783 void security_bpf_token_free(struct bpf_token *token)
5785 call_void_hook(bpf_token_free, token);
5787 #endif /* CONFIG_BPF_SYSCALL */
5790 * security_locked_down() - Check if a kernel feature is allowed
5791 * @what: requested kernel feature
5793 * Determine whether a kernel feature that potentially enables arbitrary code
5794 * execution in kernel space should be permitted.
5796 * Return: Returns 0 if permission is granted.
5798 int security_locked_down(enum lockdown_reason what)
5800 return call_int_hook(locked_down, what);
5802 EXPORT_SYMBOL(security_locked_down);
5805 * security_bdev_alloc() - Allocate a block device LSM blob
5806 * @bdev: block device
5808 * Allocate and attach a security structure to @bdev->bd_security. The
5809 * security field is initialized to NULL when the bdev structure is
5812 * Return: Return 0 if operation was successful.
5814 int security_bdev_alloc(struct block_device *bdev)
5818 rc = lsm_bdev_alloc(bdev);
5822 rc = call_int_hook(bdev_alloc_security, bdev);
5824 security_bdev_free(bdev);
5828 EXPORT_SYMBOL(security_bdev_alloc);
5831 * security_bdev_free() - Free a block device's LSM blob
5832 * @bdev: block device
5834 * Deallocate the bdev security structure and set @bdev->bd_security to NULL.
5836 void security_bdev_free(struct block_device *bdev)
5838 if (!bdev->bd_security)
5841 call_void_hook(bdev_free_security, bdev);
5843 kfree(bdev->bd_security);
5844 bdev->bd_security = NULL;
5846 EXPORT_SYMBOL(security_bdev_free);
5849 * security_bdev_setintegrity() - Set the device's integrity data
5850 * @bdev: block device
5851 * @type: type of integrity, e.g. hash digest, signature, etc
5852 * @value: the integrity value
5853 * @size: size of the integrity value
5855 * Register a verified integrity measurement of a bdev with LSMs.
5856 * LSMs should free the previously saved data if @value is NULL.
5857 * Please note that the new hook should be invoked every time the security
5858 * information is updated to keep these data current. For example, in dm-verity,
5859 * if the mapping table is reloaded and configured to use a different dm-verity
5860 * target with a new roothash and signing information, the previously stored
5861 * data in the LSM blob will become obsolete. It is crucial to re-invoke the
5862 * hook to refresh these data and ensure they are up to date. This necessity
5863 * arises from the design of device-mapper, where a device-mapper device is
5864 * first created, and then targets are subsequently loaded into it. These
5865 * targets can be modified multiple times during the device's lifetime.
5866 * Therefore, while the LSM blob is allocated during the creation of the block
5867 * device, its actual contents are not initialized at this stage and can change
5868 * substantially over time. This includes alterations from data that the LSMs
5869 * 'trusts' to those they do not, making it essential to handle these changes
5870 * correctly. Failure to address this dynamic aspect could potentially allow
5871 * for bypassing LSM checks.
5873 * Return: Returns 0 on success, negative values on failure.
5875 int security_bdev_setintegrity(struct block_device *bdev,
5876 enum lsm_integrity_type type, const void *value,
5879 return call_int_hook(bdev_setintegrity, bdev, type, value, size);
5881 EXPORT_SYMBOL(security_bdev_setintegrity);
5883 #ifdef CONFIG_PERF_EVENTS
5885 * security_perf_event_open() - Check if a perf event open is allowed
5886 * @attr: perf event attribute
5887 * @type: type of event
5889 * Check whether the @type of perf_event_open syscall is allowed.
5891 * Return: Returns 0 if permission is granted.
5893 int security_perf_event_open(struct perf_event_attr *attr, int type)
5895 return call_int_hook(perf_event_open, attr, type);
5899 * security_perf_event_alloc() - Allocate a perf event LSM blob
5900 * @event: perf event
5902 * Allocate and save perf_event security info.
5904 * Return: Returns 0 on success, error on failure.
5906 int security_perf_event_alloc(struct perf_event *event)
5910 rc = lsm_blob_alloc(&event->security, blob_sizes.lbs_perf_event,
5915 rc = call_int_hook(perf_event_alloc, event);
5917 kfree(event->security);
5918 event->security = NULL;
5924 * security_perf_event_free() - Free a perf event LSM blob
5925 * @event: perf event
5927 * Release (free) perf_event security info.
5929 void security_perf_event_free(struct perf_event *event)
5931 kfree(event->security);
5932 event->security = NULL;
5936 * security_perf_event_read() - Check if reading a perf event label is allowed
5937 * @event: perf event
5939 * Read perf_event security info if allowed.
5941 * Return: Returns 0 if permission is granted.
5943 int security_perf_event_read(struct perf_event *event)
5945 return call_int_hook(perf_event_read, event);
5949 * security_perf_event_write() - Check if writing a perf event label is allowed
5950 * @event: perf event
5952 * Write perf_event security info if allowed.
5954 * Return: Returns 0 if permission is granted.
5956 int security_perf_event_write(struct perf_event *event)
5958 return call_int_hook(perf_event_write, event);
5960 #endif /* CONFIG_PERF_EVENTS */
5962 #ifdef CONFIG_IO_URING
5964 * security_uring_override_creds() - Check if overriding creds is allowed
5965 * @new: new credentials
5967 * Check if the current task, executing an io_uring operation, is allowed to
5968 * override it's credentials with @new.
5970 * Return: Returns 0 if permission is granted.
5972 int security_uring_override_creds(const struct cred *new)
5974 return call_int_hook(uring_override_creds, new);
5978 * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
5980 * Check whether the current task is allowed to spawn a io_uring polling thread
5981 * (IORING_SETUP_SQPOLL).
5983 * Return: Returns 0 if permission is granted.
5985 int security_uring_sqpoll(void)
5987 return call_int_hook(uring_sqpoll);
5991 * security_uring_cmd() - Check if a io_uring passthrough command is allowed
5994 * Check whether the file_operations uring_cmd is allowed to run.
5996 * Return: Returns 0 if permission is granted.
5998 int security_uring_cmd(struct io_uring_cmd *ioucmd)
6000 return call_int_hook(uring_cmd, ioucmd);
6002 #endif /* CONFIG_IO_URING */
6005 * security_initramfs_populated() - Notify LSMs that initramfs has been loaded
6007 * Tells the LSMs the initramfs has been unpacked into the rootfs.
6009 void security_initramfs_populated(void)
6011 call_void_hook(initramfs_populated);