Merge tag '6.11-rc-smb-client-fixes-part2' of git://git.samba.org/sfrench/cifs-2.6
[linux-stable.git] / security / security.c
blob8cee5b6c6e6d53ceb213feab8b0bd53bcc886c7b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Security plug functions
5 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
6 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
8 * Copyright (C) 2016 Mellanox Technologies
9 * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com>
12 #define pr_fmt(fmt) "LSM: " fmt
14 #include <linux/bpf.h>
15 #include <linux/capability.h>
16 #include <linux/dcache.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/kernel_read_file.h>
21 #include <linux/lsm_hooks.h>
22 #include <linux/fsnotify.h>
23 #include <linux/mman.h>
24 #include <linux/mount.h>
25 #include <linux/personality.h>
26 #include <linux/backing-dev.h>
27 #include <linux/string.h>
28 #include <linux/xattr.h>
29 #include <linux/msg.h>
30 #include <linux/overflow.h>
31 #include <net/flow.h>
33 /* How many LSMs were built into the kernel? */
34 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
37 * How many LSMs are built into the kernel as determined at
38 * build time. Used to determine fixed array sizes.
39 * The capability module is accounted for by CONFIG_SECURITY
41 #define LSM_CONFIG_COUNT ( \
42 (IS_ENABLED(CONFIG_SECURITY) ? 1 : 0) + \
43 (IS_ENABLED(CONFIG_SECURITY_SELINUX) ? 1 : 0) + \
44 (IS_ENABLED(CONFIG_SECURITY_SMACK) ? 1 : 0) + \
45 (IS_ENABLED(CONFIG_SECURITY_TOMOYO) ? 1 : 0) + \
46 (IS_ENABLED(CONFIG_SECURITY_APPARMOR) ? 1 : 0) + \
47 (IS_ENABLED(CONFIG_SECURITY_YAMA) ? 1 : 0) + \
48 (IS_ENABLED(CONFIG_SECURITY_LOADPIN) ? 1 : 0) + \
49 (IS_ENABLED(CONFIG_SECURITY_SAFESETID) ? 1 : 0) + \
50 (IS_ENABLED(CONFIG_SECURITY_LOCKDOWN_LSM) ? 1 : 0) + \
51 (IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \
52 (IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0) + \
53 (IS_ENABLED(CONFIG_IMA) ? 1 : 0) + \
54 (IS_ENABLED(CONFIG_EVM) ? 1 : 0))
57 * These are descriptions of the reasons that can be passed to the
58 * security_locked_down() LSM hook. Placing this array here allows
59 * all security modules to use the same descriptions for auditing
60 * purposes.
62 const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
63 [LOCKDOWN_NONE] = "none",
64 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
65 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
66 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
67 [LOCKDOWN_KEXEC] = "kexec of unsigned images",
68 [LOCKDOWN_HIBERNATION] = "hibernation",
69 [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
70 [LOCKDOWN_IOPORT] = "raw io port access",
71 [LOCKDOWN_MSR] = "raw MSR access",
72 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
73 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
74 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
75 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
76 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
77 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
78 [LOCKDOWN_DEBUGFS] = "debugfs access",
79 [LOCKDOWN_XMON_WR] = "xmon write access",
80 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
81 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
82 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
83 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
84 [LOCKDOWN_KCORE] = "/proc/kcore access",
85 [LOCKDOWN_KPROBES] = "use of kprobes",
86 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
87 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
88 [LOCKDOWN_PERF] = "unsafe use of perf",
89 [LOCKDOWN_TRACEFS] = "use of tracefs",
90 [LOCKDOWN_XMON_RW] = "xmon read and write access",
91 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
92 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
95 struct security_hook_heads security_hook_heads __ro_after_init;
96 static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
98 static struct kmem_cache *lsm_file_cache;
99 static struct kmem_cache *lsm_inode_cache;
101 char *lsm_names;
102 static struct lsm_blob_sizes blob_sizes __ro_after_init;
104 /* Boot-time LSM user choice */
105 static __initdata const char *chosen_lsm_order;
106 static __initdata const char *chosen_major_lsm;
108 static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
110 /* Ordered list of LSMs to initialize. */
111 static __initdata struct lsm_info **ordered_lsms;
112 static __initdata struct lsm_info *exclusive;
114 static __initdata bool debug;
115 #define init_debug(...) \
116 do { \
117 if (debug) \
118 pr_info(__VA_ARGS__); \
119 } while (0)
121 static bool __init is_enabled(struct lsm_info *lsm)
123 if (!lsm->enabled)
124 return false;
126 return *lsm->enabled;
129 /* Mark an LSM's enabled flag. */
130 static int lsm_enabled_true __initdata = 1;
131 static int lsm_enabled_false __initdata = 0;
132 static void __init set_enabled(struct lsm_info *lsm, bool enabled)
135 * When an LSM hasn't configured an enable variable, we can use
136 * a hard-coded location for storing the default enabled state.
138 if (!lsm->enabled) {
139 if (enabled)
140 lsm->enabled = &lsm_enabled_true;
141 else
142 lsm->enabled = &lsm_enabled_false;
143 } else if (lsm->enabled == &lsm_enabled_true) {
144 if (!enabled)
145 lsm->enabled = &lsm_enabled_false;
146 } else if (lsm->enabled == &lsm_enabled_false) {
147 if (enabled)
148 lsm->enabled = &lsm_enabled_true;
149 } else {
150 *lsm->enabled = enabled;
154 /* Is an LSM already listed in the ordered LSMs list? */
155 static bool __init exists_ordered_lsm(struct lsm_info *lsm)
157 struct lsm_info **check;
159 for (check = ordered_lsms; *check; check++)
160 if (*check == lsm)
161 return true;
163 return false;
166 /* Append an LSM to the list of ordered LSMs to initialize. */
167 static int last_lsm __initdata;
168 static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
170 /* Ignore duplicate selections. */
171 if (exists_ordered_lsm(lsm))
172 return;
174 if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
175 return;
177 /* Enable this LSM, if it is not already set. */
178 if (!lsm->enabled)
179 lsm->enabled = &lsm_enabled_true;
180 ordered_lsms[last_lsm++] = lsm;
182 init_debug("%s ordered: %s (%s)\n", from, lsm->name,
183 is_enabled(lsm) ? "enabled" : "disabled");
186 /* Is an LSM allowed to be initialized? */
187 static bool __init lsm_allowed(struct lsm_info *lsm)
189 /* Skip if the LSM is disabled. */
190 if (!is_enabled(lsm))
191 return false;
193 /* Not allowed if another exclusive LSM already initialized. */
194 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
195 init_debug("exclusive disabled: %s\n", lsm->name);
196 return false;
199 return true;
202 static void __init lsm_set_blob_size(int *need, int *lbs)
204 int offset;
206 if (*need <= 0)
207 return;
209 offset = ALIGN(*lbs, sizeof(void *));
210 *lbs = offset + *need;
211 *need = offset;
214 static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
216 if (!needed)
217 return;
219 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
220 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
222 * The inode blob gets an rcu_head in addition to
223 * what the modules might need.
225 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
226 blob_sizes.lbs_inode = sizeof(struct rcu_head);
227 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
228 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
229 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
230 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
231 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
232 lsm_set_blob_size(&needed->lbs_xattr_count,
233 &blob_sizes.lbs_xattr_count);
236 /* Prepare LSM for initialization. */
237 static void __init prepare_lsm(struct lsm_info *lsm)
239 int enabled = lsm_allowed(lsm);
241 /* Record enablement (to handle any following exclusive LSMs). */
242 set_enabled(lsm, enabled);
244 /* If enabled, do pre-initialization work. */
245 if (enabled) {
246 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
247 exclusive = lsm;
248 init_debug("exclusive chosen: %s\n", lsm->name);
251 lsm_set_blob_sizes(lsm->blobs);
255 /* Initialize a given LSM, if it is enabled. */
256 static void __init initialize_lsm(struct lsm_info *lsm)
258 if (is_enabled(lsm)) {
259 int ret;
261 init_debug("initializing %s\n", lsm->name);
262 ret = lsm->init();
263 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
268 * Current index to use while initializing the lsm id list.
270 u32 lsm_active_cnt __ro_after_init;
271 const struct lsm_id *lsm_idlist[LSM_CONFIG_COUNT];
273 /* Populate ordered LSMs list from comma-separated LSM name list. */
274 static void __init ordered_lsm_parse(const char *order, const char *origin)
276 struct lsm_info *lsm;
277 char *sep, *name, *next;
279 /* LSM_ORDER_FIRST is always first. */
280 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
281 if (lsm->order == LSM_ORDER_FIRST)
282 append_ordered_lsm(lsm, " first");
285 /* Process "security=", if given. */
286 if (chosen_major_lsm) {
287 struct lsm_info *major;
290 * To match the original "security=" behavior, this
291 * explicitly does NOT fallback to another Legacy Major
292 * if the selected one was separately disabled: disable
293 * all non-matching Legacy Major LSMs.
295 for (major = __start_lsm_info; major < __end_lsm_info;
296 major++) {
297 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
298 strcmp(major->name, chosen_major_lsm) != 0) {
299 set_enabled(major, false);
300 init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
301 chosen_major_lsm, major->name);
306 sep = kstrdup(order, GFP_KERNEL);
307 next = sep;
308 /* Walk the list, looking for matching LSMs. */
309 while ((name = strsep(&next, ",")) != NULL) {
310 bool found = false;
312 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
313 if (strcmp(lsm->name, name) == 0) {
314 if (lsm->order == LSM_ORDER_MUTABLE)
315 append_ordered_lsm(lsm, origin);
316 found = true;
320 if (!found)
321 init_debug("%s ignored: %s (not built into kernel)\n",
322 origin, name);
325 /* Process "security=", if given. */
326 if (chosen_major_lsm) {
327 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
328 if (exists_ordered_lsm(lsm))
329 continue;
330 if (strcmp(lsm->name, chosen_major_lsm) == 0)
331 append_ordered_lsm(lsm, "security=");
335 /* LSM_ORDER_LAST is always last. */
336 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
337 if (lsm->order == LSM_ORDER_LAST)
338 append_ordered_lsm(lsm, " last");
341 /* Disable all LSMs not in the ordered list. */
342 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
343 if (exists_ordered_lsm(lsm))
344 continue;
345 set_enabled(lsm, false);
346 init_debug("%s skipped: %s (not in requested order)\n",
347 origin, lsm->name);
350 kfree(sep);
353 static void __init lsm_early_cred(struct cred *cred);
354 static void __init lsm_early_task(struct task_struct *task);
356 static int lsm_append(const char *new, char **result);
358 static void __init report_lsm_order(void)
360 struct lsm_info **lsm, *early;
361 int first = 0;
363 pr_info("initializing lsm=");
365 /* Report each enabled LSM name, comma separated. */
366 for (early = __start_early_lsm_info;
367 early < __end_early_lsm_info; early++)
368 if (is_enabled(early))
369 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
370 for (lsm = ordered_lsms; *lsm; lsm++)
371 if (is_enabled(*lsm))
372 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
374 pr_cont("\n");
377 static void __init ordered_lsm_init(void)
379 struct lsm_info **lsm;
381 ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
382 GFP_KERNEL);
384 if (chosen_lsm_order) {
385 if (chosen_major_lsm) {
386 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
387 chosen_major_lsm, chosen_lsm_order);
388 chosen_major_lsm = NULL;
390 ordered_lsm_parse(chosen_lsm_order, "cmdline");
391 } else
392 ordered_lsm_parse(builtin_lsm_order, "builtin");
394 for (lsm = ordered_lsms; *lsm; lsm++)
395 prepare_lsm(*lsm);
397 report_lsm_order();
399 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
400 init_debug("file blob size = %d\n", blob_sizes.lbs_file);
401 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
402 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
403 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
404 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
405 init_debug("task blob size = %d\n", blob_sizes.lbs_task);
406 init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
409 * Create any kmem_caches needed for blobs
411 if (blob_sizes.lbs_file)
412 lsm_file_cache = kmem_cache_create("lsm_file_cache",
413 blob_sizes.lbs_file, 0,
414 SLAB_PANIC, NULL);
415 if (blob_sizes.lbs_inode)
416 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
417 blob_sizes.lbs_inode, 0,
418 SLAB_PANIC, NULL);
420 lsm_early_cred((struct cred *) current->cred);
421 lsm_early_task(current);
422 for (lsm = ordered_lsms; *lsm; lsm++)
423 initialize_lsm(*lsm);
425 kfree(ordered_lsms);
428 int __init early_security_init(void)
430 struct lsm_info *lsm;
432 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
433 INIT_HLIST_HEAD(&security_hook_heads.NAME);
434 #include "linux/lsm_hook_defs.h"
435 #undef LSM_HOOK
437 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
438 if (!lsm->enabled)
439 lsm->enabled = &lsm_enabled_true;
440 prepare_lsm(lsm);
441 initialize_lsm(lsm);
444 return 0;
448 * security_init - initializes the security framework
450 * This should be called early in the kernel initialization sequence.
452 int __init security_init(void)
454 struct lsm_info *lsm;
456 init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
457 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
458 init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
461 * Append the names of the early LSM modules now that kmalloc() is
462 * available
464 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
465 init_debug(" early started: %s (%s)\n", lsm->name,
466 is_enabled(lsm) ? "enabled" : "disabled");
467 if (lsm->enabled)
468 lsm_append(lsm->name, &lsm_names);
471 /* Load LSMs in specified order. */
472 ordered_lsm_init();
474 return 0;
477 /* Save user chosen LSM */
478 static int __init choose_major_lsm(char *str)
480 chosen_major_lsm = str;
481 return 1;
483 __setup("security=", choose_major_lsm);
485 /* Explicitly choose LSM initialization order. */
486 static int __init choose_lsm_order(char *str)
488 chosen_lsm_order = str;
489 return 1;
491 __setup("lsm=", choose_lsm_order);
493 /* Enable LSM order debugging. */
494 static int __init enable_debug(char *str)
496 debug = true;
497 return 1;
499 __setup("lsm.debug", enable_debug);
501 static bool match_last_lsm(const char *list, const char *lsm)
503 const char *last;
505 if (WARN_ON(!list || !lsm))
506 return false;
507 last = strrchr(list, ',');
508 if (last)
509 /* Pass the comma, strcmp() will check for '\0' */
510 last++;
511 else
512 last = list;
513 return !strcmp(last, lsm);
516 static int lsm_append(const char *new, char **result)
518 char *cp;
520 if (*result == NULL) {
521 *result = kstrdup(new, GFP_KERNEL);
522 if (*result == NULL)
523 return -ENOMEM;
524 } else {
525 /* Check if it is the last registered name */
526 if (match_last_lsm(*result, new))
527 return 0;
528 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
529 if (cp == NULL)
530 return -ENOMEM;
531 kfree(*result);
532 *result = cp;
534 return 0;
538 * security_add_hooks - Add a modules hooks to the hook lists.
539 * @hooks: the hooks to add
540 * @count: the number of hooks to add
541 * @lsmid: the identification information for the security module
543 * Each LSM has to register its hooks with the infrastructure.
545 void __init security_add_hooks(struct security_hook_list *hooks, int count,
546 const struct lsm_id *lsmid)
548 int i;
551 * A security module may call security_add_hooks() more
552 * than once during initialization, and LSM initialization
553 * is serialized. Landlock is one such case.
554 * Look at the previous entry, if there is one, for duplication.
556 if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
557 if (lsm_active_cnt >= LSM_CONFIG_COUNT)
558 panic("%s Too many LSMs registered.\n", __func__);
559 lsm_idlist[lsm_active_cnt++] = lsmid;
562 for (i = 0; i < count; i++) {
563 hooks[i].lsmid = lsmid;
564 hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
568 * Don't try to append during early_security_init(), we'll come back
569 * and fix this up afterwards.
571 if (slab_is_available()) {
572 if (lsm_append(lsmid->name, &lsm_names) < 0)
573 panic("%s - Cannot get early memory.\n", __func__);
577 int call_blocking_lsm_notifier(enum lsm_event event, void *data)
579 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
580 event, data);
582 EXPORT_SYMBOL(call_blocking_lsm_notifier);
584 int register_blocking_lsm_notifier(struct notifier_block *nb)
586 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
587 nb);
589 EXPORT_SYMBOL(register_blocking_lsm_notifier);
591 int unregister_blocking_lsm_notifier(struct notifier_block *nb)
593 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
594 nb);
596 EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
599 * lsm_cred_alloc - allocate a composite cred blob
600 * @cred: the cred that needs a blob
601 * @gfp: allocation type
603 * Allocate the cred blob for all the modules
605 * Returns 0, or -ENOMEM if memory can't be allocated.
607 static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
609 if (blob_sizes.lbs_cred == 0) {
610 cred->security = NULL;
611 return 0;
614 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
615 if (cred->security == NULL)
616 return -ENOMEM;
617 return 0;
621 * lsm_early_cred - during initialization allocate a composite cred blob
622 * @cred: the cred that needs a blob
624 * Allocate the cred blob for all the modules
626 static void __init lsm_early_cred(struct cred *cred)
628 int rc = lsm_cred_alloc(cred, GFP_KERNEL);
630 if (rc)
631 panic("%s: Early cred alloc failed.\n", __func__);
635 * lsm_file_alloc - allocate a composite file blob
636 * @file: the file that needs a blob
638 * Allocate the file blob for all the modules
640 * Returns 0, or -ENOMEM if memory can't be allocated.
642 static int lsm_file_alloc(struct file *file)
644 if (!lsm_file_cache) {
645 file->f_security = NULL;
646 return 0;
649 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
650 if (file->f_security == NULL)
651 return -ENOMEM;
652 return 0;
656 * lsm_inode_alloc - allocate a composite inode blob
657 * @inode: the inode that needs a blob
659 * Allocate the inode blob for all the modules
661 * Returns 0, or -ENOMEM if memory can't be allocated.
663 int lsm_inode_alloc(struct inode *inode)
665 if (!lsm_inode_cache) {
666 inode->i_security = NULL;
667 return 0;
670 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
671 if (inode->i_security == NULL)
672 return -ENOMEM;
673 return 0;
677 * lsm_task_alloc - allocate a composite task blob
678 * @task: the task that needs a blob
680 * Allocate the task blob for all the modules
682 * Returns 0, or -ENOMEM if memory can't be allocated.
684 static int lsm_task_alloc(struct task_struct *task)
686 if (blob_sizes.lbs_task == 0) {
687 task->security = NULL;
688 return 0;
691 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
692 if (task->security == NULL)
693 return -ENOMEM;
694 return 0;
698 * lsm_ipc_alloc - allocate a composite ipc blob
699 * @kip: the ipc that needs a blob
701 * Allocate the ipc blob for all the modules
703 * Returns 0, or -ENOMEM if memory can't be allocated.
705 static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
707 if (blob_sizes.lbs_ipc == 0) {
708 kip->security = NULL;
709 return 0;
712 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
713 if (kip->security == NULL)
714 return -ENOMEM;
715 return 0;
719 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
720 * @mp: the msg_msg that needs a blob
722 * Allocate the ipc blob for all the modules
724 * Returns 0, or -ENOMEM if memory can't be allocated.
726 static int lsm_msg_msg_alloc(struct msg_msg *mp)
728 if (blob_sizes.lbs_msg_msg == 0) {
729 mp->security = NULL;
730 return 0;
733 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
734 if (mp->security == NULL)
735 return -ENOMEM;
736 return 0;
740 * lsm_early_task - during initialization allocate a composite task blob
741 * @task: the task that needs a blob
743 * Allocate the task blob for all the modules
745 static void __init lsm_early_task(struct task_struct *task)
747 int rc = lsm_task_alloc(task);
749 if (rc)
750 panic("%s: Early task alloc failed.\n", __func__);
754 * lsm_superblock_alloc - allocate a composite superblock blob
755 * @sb: the superblock that needs a blob
757 * Allocate the superblock blob for all the modules
759 * Returns 0, or -ENOMEM if memory can't be allocated.
761 static int lsm_superblock_alloc(struct super_block *sb)
763 if (blob_sizes.lbs_superblock == 0) {
764 sb->s_security = NULL;
765 return 0;
768 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
769 if (sb->s_security == NULL)
770 return -ENOMEM;
771 return 0;
775 * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
776 * @uctx: a userspace LSM context to be filled
777 * @uctx_len: available uctx size (input), used uctx size (output)
778 * @val: the new LSM context value
779 * @val_len: the size of the new LSM context value
780 * @id: LSM id
781 * @flags: LSM defined flags
783 * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL
784 * simply calculate the required size to output via @utc_len and return
785 * success.
787 * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
788 * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
790 int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
791 void *val, size_t val_len,
792 u64 id, u64 flags)
794 struct lsm_ctx *nctx = NULL;
795 size_t nctx_len;
796 int rc = 0;
798 nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
799 if (nctx_len > *uctx_len) {
800 rc = -E2BIG;
801 goto out;
804 /* no buffer - return success/0 and set @uctx_len to the req size */
805 if (!uctx)
806 goto out;
808 nctx = kzalloc(nctx_len, GFP_KERNEL);
809 if (nctx == NULL) {
810 rc = -ENOMEM;
811 goto out;
813 nctx->id = id;
814 nctx->flags = flags;
815 nctx->len = nctx_len;
816 nctx->ctx_len = val_len;
817 memcpy(nctx->ctx, val, val_len);
819 if (copy_to_user(uctx, nctx, nctx_len))
820 rc = -EFAULT;
822 out:
823 kfree(nctx);
824 *uctx_len = nctx_len;
825 return rc;
829 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
830 * can be accessed with:
832 * LSM_RET_DEFAULT(<hook_name>)
834 * The macros below define static constants for the default value of each
835 * LSM hook.
837 #define LSM_RET_DEFAULT(NAME) (NAME##_default)
838 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
839 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
840 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
841 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
842 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
844 #include <linux/lsm_hook_defs.h>
845 #undef LSM_HOOK
848 * Hook list operation macros.
850 * call_void_hook:
851 * This is a hook that does not return a value.
853 * call_int_hook:
854 * This is a hook that returns a value.
857 #define call_void_hook(FUNC, ...) \
858 do { \
859 struct security_hook_list *P; \
861 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
862 P->hook.FUNC(__VA_ARGS__); \
863 } while (0)
865 #define call_int_hook(FUNC, ...) ({ \
866 int RC = LSM_RET_DEFAULT(FUNC); \
867 do { \
868 struct security_hook_list *P; \
870 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
871 RC = P->hook.FUNC(__VA_ARGS__); \
872 if (RC != LSM_RET_DEFAULT(FUNC)) \
873 break; \
875 } while (0); \
876 RC; \
879 /* Security operations */
882 * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
883 * @mgr: task credentials of current binder process
885 * Check whether @mgr is allowed to be the binder context manager.
887 * Return: Return 0 if permission is granted.
889 int security_binder_set_context_mgr(const struct cred *mgr)
891 return call_int_hook(binder_set_context_mgr, mgr);
895 * security_binder_transaction() - Check if a binder transaction is allowed
896 * @from: sending process
897 * @to: receiving process
899 * Check whether @from is allowed to invoke a binder transaction call to @to.
901 * Return: Returns 0 if permission is granted.
903 int security_binder_transaction(const struct cred *from,
904 const struct cred *to)
906 return call_int_hook(binder_transaction, from, to);
910 * security_binder_transfer_binder() - Check if a binder transfer is allowed
911 * @from: sending process
912 * @to: receiving process
914 * Check whether @from is allowed to transfer a binder reference to @to.
916 * Return: Returns 0 if permission is granted.
918 int security_binder_transfer_binder(const struct cred *from,
919 const struct cred *to)
921 return call_int_hook(binder_transfer_binder, from, to);
925 * security_binder_transfer_file() - Check if a binder file xfer is allowed
926 * @from: sending process
927 * @to: receiving process
928 * @file: file being transferred
930 * Check whether @from is allowed to transfer @file to @to.
932 * Return: Returns 0 if permission is granted.
934 int security_binder_transfer_file(const struct cred *from,
935 const struct cred *to, const struct file *file)
937 return call_int_hook(binder_transfer_file, from, to, file);
941 * security_ptrace_access_check() - Check if tracing is allowed
942 * @child: target process
943 * @mode: PTRACE_MODE flags
945 * Check permission before allowing the current process to trace the @child
946 * process. Security modules may also want to perform a process tracing check
947 * during an execve in the set_security or apply_creds hooks of tracing check
948 * during an execve in the bprm_set_creds hook of binprm_security_ops if the
949 * process is being traced and its security attributes would be changed by the
950 * execve.
952 * Return: Returns 0 if permission is granted.
954 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
956 return call_int_hook(ptrace_access_check, child, mode);
960 * security_ptrace_traceme() - Check if tracing is allowed
961 * @parent: tracing process
963 * Check that the @parent process has sufficient permission to trace the
964 * current process before allowing the current process to present itself to the
965 * @parent process for tracing.
967 * Return: Returns 0 if permission is granted.
969 int security_ptrace_traceme(struct task_struct *parent)
971 return call_int_hook(ptrace_traceme, parent);
975 * security_capget() - Get the capability sets for a process
976 * @target: target process
977 * @effective: effective capability set
978 * @inheritable: inheritable capability set
979 * @permitted: permitted capability set
981 * Get the @effective, @inheritable, and @permitted capability sets for the
982 * @target process. The hook may also perform permission checking to determine
983 * if the current process is allowed to see the capability sets of the @target
984 * process.
986 * Return: Returns 0 if the capability sets were successfully obtained.
988 int security_capget(const struct task_struct *target,
989 kernel_cap_t *effective,
990 kernel_cap_t *inheritable,
991 kernel_cap_t *permitted)
993 return call_int_hook(capget, target, effective, inheritable, permitted);
997 * security_capset() - Set the capability sets for a process
998 * @new: new credentials for the target process
999 * @old: current credentials of the target process
1000 * @effective: effective capability set
1001 * @inheritable: inheritable capability set
1002 * @permitted: permitted capability set
1004 * Set the @effective, @inheritable, and @permitted capability sets for the
1005 * current process.
1007 * Return: Returns 0 and update @new if permission is granted.
1009 int security_capset(struct cred *new, const struct cred *old,
1010 const kernel_cap_t *effective,
1011 const kernel_cap_t *inheritable,
1012 const kernel_cap_t *permitted)
1014 return call_int_hook(capset, new, old, effective, inheritable,
1015 permitted);
1019 * security_capable() - Check if a process has the necessary capability
1020 * @cred: credentials to examine
1021 * @ns: user namespace
1022 * @cap: capability requested
1023 * @opts: capability check options
1025 * Check whether the @tsk process has the @cap capability in the indicated
1026 * credentials. @cap contains the capability <include/linux/capability.h>.
1027 * @opts contains options for the capable check <include/linux/security.h>.
1029 * Return: Returns 0 if the capability is granted.
1031 int security_capable(const struct cred *cred,
1032 struct user_namespace *ns,
1033 int cap,
1034 unsigned int opts)
1036 return call_int_hook(capable, cred, ns, cap, opts);
1040 * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
1041 * @cmds: commands
1042 * @type: type
1043 * @id: id
1044 * @sb: filesystem
1046 * Check whether the quotactl syscall is allowed for this @sb.
1048 * Return: Returns 0 if permission is granted.
1050 int security_quotactl(int cmds, int type, int id, const struct super_block *sb)
1052 return call_int_hook(quotactl, cmds, type, id, sb);
1056 * security_quota_on() - Check if QUOTAON is allowed for a dentry
1057 * @dentry: dentry
1059 * Check whether QUOTAON is allowed for @dentry.
1061 * Return: Returns 0 if permission is granted.
1063 int security_quota_on(struct dentry *dentry)
1065 return call_int_hook(quota_on, dentry);
1069 * security_syslog() - Check if accessing the kernel message ring is allowed
1070 * @type: SYSLOG_ACTION_* type
1072 * Check permission before accessing the kernel message ring or changing
1073 * logging to the console. See the syslog(2) manual page for an explanation of
1074 * the @type values.
1076 * Return: Return 0 if permission is granted.
1078 int security_syslog(int type)
1080 return call_int_hook(syslog, type);
1084 * security_settime64() - Check if changing the system time is allowed
1085 * @ts: new time
1086 * @tz: timezone
1088 * Check permission to change the system time, struct timespec64 is defined in
1089 * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
1091 * Return: Returns 0 if permission is granted.
1093 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
1095 return call_int_hook(settime, ts, tz);
1099 * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
1100 * @mm: mm struct
1101 * @pages: number of pages
1103 * Check permissions for allocating a new virtual mapping. If all LSMs return
1104 * a positive value, __vm_enough_memory() will be called with cap_sys_admin
1105 * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
1106 * called with cap_sys_admin cleared.
1108 * Return: Returns 0 if permission is granted by the LSM infrastructure to the
1109 * caller.
1111 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
1113 struct security_hook_list *hp;
1114 int cap_sys_admin = 1;
1115 int rc;
1118 * The module will respond with a positive value if
1119 * it thinks the __vm_enough_memory() call should be
1120 * made with the cap_sys_admin set. If all of the modules
1121 * agree that it should be set it will. If any module
1122 * thinks it should not be set it won't.
1124 hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
1125 rc = hp->hook.vm_enough_memory(mm, pages);
1126 if (rc <= 0) {
1127 cap_sys_admin = 0;
1128 break;
1131 return __vm_enough_memory(mm, pages, cap_sys_admin);
1135 * security_bprm_creds_for_exec() - Prepare the credentials for exec()
1136 * @bprm: binary program information
1138 * If the setup in prepare_exec_creds did not setup @bprm->cred->security
1139 * properly for executing @bprm->file, update the LSM's portion of
1140 * @bprm->cred->security to be what commit_creds needs to install for the new
1141 * program. This hook may also optionally check permissions (e.g. for
1142 * transitions between security domains). The hook must set @bprm->secureexec
1143 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
1144 * contains the linux_binprm structure.
1146 * Return: Returns 0 if the hook is successful and permission is granted.
1148 int security_bprm_creds_for_exec(struct linux_binprm *bprm)
1150 return call_int_hook(bprm_creds_for_exec, bprm);
1154 * security_bprm_creds_from_file() - Update linux_binprm creds based on file
1155 * @bprm: binary program information
1156 * @file: associated file
1158 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
1159 * exec, update @bprm->cred to reflect that change. This is called after
1160 * finding the binary that will be executed without an interpreter. This
1161 * ensures that the credentials will not be derived from a script that the
1162 * binary will need to reopen, which when reopend may end up being a completely
1163 * different file. This hook may also optionally check permissions (e.g. for
1164 * transitions between security domains). The hook must set @bprm->secureexec
1165 * to 1 if AT_SECURE should be set to request libc enable secure mode. The
1166 * hook must add to @bprm->per_clear any personality flags that should be
1167 * cleared from current->personality. @bprm contains the linux_binprm
1168 * structure.
1170 * Return: Returns 0 if the hook is successful and permission is granted.
1172 int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file)
1174 return call_int_hook(bprm_creds_from_file, bprm, file);
1178 * security_bprm_check() - Mediate binary handler search
1179 * @bprm: binary program information
1181 * This hook mediates the point when a search for a binary handler will begin.
1182 * It allows a check against the @bprm->cred->security value which was set in
1183 * the preceding creds_for_exec call. The argv list and envp list are reliably
1184 * available in @bprm. This hook may be called multiple times during a single
1185 * execve. @bprm contains the linux_binprm structure.
1187 * Return: Returns 0 if the hook is successful and permission is granted.
1189 int security_bprm_check(struct linux_binprm *bprm)
1191 return call_int_hook(bprm_check_security, bprm);
1195 * security_bprm_committing_creds() - Install creds for a process during exec()
1196 * @bprm: binary program information
1198 * Prepare to install the new security attributes of a process being
1199 * transformed by an execve operation, based on the old credentials pointed to
1200 * by @current->cred and the information set in @bprm->cred by the
1201 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
1202 * hook is a good place to perform state changes on the process such as closing
1203 * open file descriptors to which access will no longer be granted when the
1204 * attributes are changed. This is called immediately before commit_creds().
1206 void security_bprm_committing_creds(const struct linux_binprm *bprm)
1208 call_void_hook(bprm_committing_creds, bprm);
1212 * security_bprm_committed_creds() - Tidy up after cred install during exec()
1213 * @bprm: binary program information
1215 * Tidy up after the installation of the new security attributes of a process
1216 * being transformed by an execve operation. The new credentials have, by this
1217 * point, been set to @current->cred. @bprm points to the linux_binprm
1218 * structure. This hook is a good place to perform state changes on the
1219 * process such as clearing out non-inheritable signal state. This is called
1220 * immediately after commit_creds().
1222 void security_bprm_committed_creds(const struct linux_binprm *bprm)
1224 call_void_hook(bprm_committed_creds, bprm);
1228 * security_fs_context_submount() - Initialise fc->security
1229 * @fc: new filesystem context
1230 * @reference: dentry reference for submount/remount
1232 * Fill out the ->security field for a new fs_context.
1234 * Return: Returns 0 on success or negative error code on failure.
1236 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
1238 return call_int_hook(fs_context_submount, fc, reference);
1242 * security_fs_context_dup() - Duplicate a fs_context LSM blob
1243 * @fc: destination filesystem context
1244 * @src_fc: source filesystem context
1246 * Allocate and attach a security structure to sc->security. This pointer is
1247 * initialised to NULL by the caller. @fc indicates the new filesystem context.
1248 * @src_fc indicates the original filesystem context.
1250 * Return: Returns 0 on success or a negative error code on failure.
1252 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1254 return call_int_hook(fs_context_dup, fc, src_fc);
1258 * security_fs_context_parse_param() - Configure a filesystem context
1259 * @fc: filesystem context
1260 * @param: filesystem parameter
1262 * Userspace provided a parameter to configure a superblock. The LSM can
1263 * consume the parameter or return it to the caller for use elsewhere.
1265 * Return: If the parameter is used by the LSM it should return 0, if it is
1266 * returned to the caller -ENOPARAM is returned, otherwise a negative
1267 * error code is returned.
1269 int security_fs_context_parse_param(struct fs_context *fc,
1270 struct fs_parameter *param)
1272 struct security_hook_list *hp;
1273 int trc;
1274 int rc = -ENOPARAM;
1276 hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1277 list) {
1278 trc = hp->hook.fs_context_parse_param(fc, param);
1279 if (trc == 0)
1280 rc = 0;
1281 else if (trc != -ENOPARAM)
1282 return trc;
1284 return rc;
1288 * security_sb_alloc() - Allocate a super_block LSM blob
1289 * @sb: filesystem superblock
1291 * Allocate and attach a security structure to the sb->s_security field. The
1292 * s_security field is initialized to NULL when the structure is allocated.
1293 * @sb contains the super_block structure to be modified.
1295 * Return: Returns 0 if operation was successful.
1297 int security_sb_alloc(struct super_block *sb)
1299 int rc = lsm_superblock_alloc(sb);
1301 if (unlikely(rc))
1302 return rc;
1303 rc = call_int_hook(sb_alloc_security, sb);
1304 if (unlikely(rc))
1305 security_sb_free(sb);
1306 return rc;
1310 * security_sb_delete() - Release super_block LSM associated objects
1311 * @sb: filesystem superblock
1313 * Release objects tied to a superblock (e.g. inodes). @sb contains the
1314 * super_block structure being released.
1316 void security_sb_delete(struct super_block *sb)
1318 call_void_hook(sb_delete, sb);
1322 * security_sb_free() - Free a super_block LSM blob
1323 * @sb: filesystem superblock
1325 * Deallocate and clear the sb->s_security field. @sb contains the super_block
1326 * structure to be modified.
1328 void security_sb_free(struct super_block *sb)
1330 call_void_hook(sb_free_security, sb);
1331 kfree(sb->s_security);
1332 sb->s_security = NULL;
1336 * security_free_mnt_opts() - Free memory associated with mount options
1337 * @mnt_opts: LSM processed mount options
1339 * Free memory associated with @mnt_ops.
1341 void security_free_mnt_opts(void **mnt_opts)
1343 if (!*mnt_opts)
1344 return;
1345 call_void_hook(sb_free_mnt_opts, *mnt_opts);
1346 *mnt_opts = NULL;
1348 EXPORT_SYMBOL(security_free_mnt_opts);
1351 * security_sb_eat_lsm_opts() - Consume LSM mount options
1352 * @options: mount options
1353 * @mnt_opts: LSM processed mount options
1355 * Eat (scan @options) and save them in @mnt_opts.
1357 * Return: Returns 0 on success, negative values on failure.
1359 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1361 return call_int_hook(sb_eat_lsm_opts, options, mnt_opts);
1363 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1366 * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1367 * @sb: filesystem superblock
1368 * @mnt_opts: new mount options
1370 * Determine if the new mount options in @mnt_opts are allowed given the
1371 * existing mounted filesystem at @sb. @sb superblock being compared.
1373 * Return: Returns 0 if options are compatible.
1375 int security_sb_mnt_opts_compat(struct super_block *sb,
1376 void *mnt_opts)
1378 return call_int_hook(sb_mnt_opts_compat, sb, mnt_opts);
1380 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1383 * security_sb_remount() - Verify no incompatible mount changes during remount
1384 * @sb: filesystem superblock
1385 * @mnt_opts: (re)mount options
1387 * Extracts security system specific mount options and verifies no changes are
1388 * being made to those options.
1390 * Return: Returns 0 if permission is granted.
1392 int security_sb_remount(struct super_block *sb,
1393 void *mnt_opts)
1395 return call_int_hook(sb_remount, sb, mnt_opts);
1397 EXPORT_SYMBOL(security_sb_remount);
1400 * security_sb_kern_mount() - Check if a kernel mount is allowed
1401 * @sb: filesystem superblock
1403 * Mount this @sb if allowed by permissions.
1405 * Return: Returns 0 if permission is granted.
1407 int security_sb_kern_mount(const struct super_block *sb)
1409 return call_int_hook(sb_kern_mount, sb);
1413 * security_sb_show_options() - Output the mount options for a superblock
1414 * @m: output file
1415 * @sb: filesystem superblock
1417 * Show (print on @m) mount options for this @sb.
1419 * Return: Returns 0 on success, negative values on failure.
1421 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1423 return call_int_hook(sb_show_options, m, sb);
1427 * security_sb_statfs() - Check if accessing fs stats is allowed
1428 * @dentry: superblock handle
1430 * Check permission before obtaining filesystem statistics for the @mnt
1431 * mountpoint. @dentry is a handle on the superblock for the filesystem.
1433 * Return: Returns 0 if permission is granted.
1435 int security_sb_statfs(struct dentry *dentry)
1437 return call_int_hook(sb_statfs, dentry);
1441 * security_sb_mount() - Check permission for mounting a filesystem
1442 * @dev_name: filesystem backing device
1443 * @path: mount point
1444 * @type: filesystem type
1445 * @flags: mount flags
1446 * @data: filesystem specific data
1448 * Check permission before an object specified by @dev_name is mounted on the
1449 * mount point named by @nd. For an ordinary mount, @dev_name identifies a
1450 * device if the file system type requires a device. For a remount
1451 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
1452 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
1453 * mounted.
1455 * Return: Returns 0 if permission is granted.
1457 int security_sb_mount(const char *dev_name, const struct path *path,
1458 const char *type, unsigned long flags, void *data)
1460 return call_int_hook(sb_mount, dev_name, path, type, flags, data);
1464 * security_sb_umount() - Check permission for unmounting a filesystem
1465 * @mnt: mounted filesystem
1466 * @flags: unmount flags
1468 * Check permission before the @mnt file system is unmounted.
1470 * Return: Returns 0 if permission is granted.
1472 int security_sb_umount(struct vfsmount *mnt, int flags)
1474 return call_int_hook(sb_umount, mnt, flags);
1478 * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1479 * @old_path: new location for current rootfs
1480 * @new_path: location of the new rootfs
1482 * Check permission before pivoting the root filesystem.
1484 * Return: Returns 0 if permission is granted.
1486 int security_sb_pivotroot(const struct path *old_path,
1487 const struct path *new_path)
1489 return call_int_hook(sb_pivotroot, old_path, new_path);
1493 * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1494 * @sb: filesystem superblock
1495 * @mnt_opts: binary mount options
1496 * @kern_flags: kernel flags (in)
1497 * @set_kern_flags: kernel flags (out)
1499 * Set the security relevant mount options used for a superblock.
1501 * Return: Returns 0 on success, error on failure.
1503 int security_sb_set_mnt_opts(struct super_block *sb,
1504 void *mnt_opts,
1505 unsigned long kern_flags,
1506 unsigned long *set_kern_flags)
1508 struct security_hook_list *hp;
1509 int rc = mnt_opts ? -EOPNOTSUPP : LSM_RET_DEFAULT(sb_set_mnt_opts);
1511 hlist_for_each_entry(hp, &security_hook_heads.sb_set_mnt_opts,
1512 list) {
1513 rc = hp->hook.sb_set_mnt_opts(sb, mnt_opts, kern_flags,
1514 set_kern_flags);
1515 if (rc != LSM_RET_DEFAULT(sb_set_mnt_opts))
1516 break;
1518 return rc;
1520 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1523 * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1524 * @oldsb: source superblock
1525 * @newsb: destination superblock
1526 * @kern_flags: kernel flags (in)
1527 * @set_kern_flags: kernel flags (out)
1529 * Copy all security options from a given superblock to another.
1531 * Return: Returns 0 on success, error on failure.
1533 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1534 struct super_block *newsb,
1535 unsigned long kern_flags,
1536 unsigned long *set_kern_flags)
1538 return call_int_hook(sb_clone_mnt_opts, oldsb, newsb,
1539 kern_flags, set_kern_flags);
1541 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1544 * security_move_mount() - Check permissions for moving a mount
1545 * @from_path: source mount point
1546 * @to_path: destination mount point
1548 * Check permission before a mount is moved.
1550 * Return: Returns 0 if permission is granted.
1552 int security_move_mount(const struct path *from_path,
1553 const struct path *to_path)
1555 return call_int_hook(move_mount, from_path, to_path);
1559 * security_path_notify() - Check if setting a watch is allowed
1560 * @path: file path
1561 * @mask: event mask
1562 * @obj_type: file path type
1564 * Check permissions before setting a watch on events as defined by @mask, on
1565 * an object at @path, whose type is defined by @obj_type.
1567 * Return: Returns 0 if permission is granted.
1569 int security_path_notify(const struct path *path, u64 mask,
1570 unsigned int obj_type)
1572 return call_int_hook(path_notify, path, mask, obj_type);
1576 * security_inode_alloc() - Allocate an inode LSM blob
1577 * @inode: the inode
1579 * Allocate and attach a security structure to @inode->i_security. The
1580 * i_security field is initialized to NULL when the inode structure is
1581 * allocated.
1583 * Return: Return 0 if operation was successful.
1585 int security_inode_alloc(struct inode *inode)
1587 int rc = lsm_inode_alloc(inode);
1589 if (unlikely(rc))
1590 return rc;
1591 rc = call_int_hook(inode_alloc_security, inode);
1592 if (unlikely(rc))
1593 security_inode_free(inode);
1594 return rc;
1597 static void inode_free_by_rcu(struct rcu_head *head)
1600 * The rcu head is at the start of the inode blob
1602 kmem_cache_free(lsm_inode_cache, head);
1606 * security_inode_free() - Free an inode's LSM blob
1607 * @inode: the inode
1609 * Deallocate the inode security structure and set @inode->i_security to NULL.
1611 void security_inode_free(struct inode *inode)
1613 call_void_hook(inode_free_security, inode);
1615 * The inode may still be referenced in a path walk and
1616 * a call to security_inode_permission() can be made
1617 * after inode_free_security() is called. Ideally, the VFS
1618 * wouldn't do this, but fixing that is a much harder
1619 * job. For now, simply free the i_security via RCU, and
1620 * leave the current inode->i_security pointer intact.
1621 * The inode will be freed after the RCU grace period too.
1623 if (inode->i_security)
1624 call_rcu((struct rcu_head *)inode->i_security,
1625 inode_free_by_rcu);
1629 * security_dentry_init_security() - Perform dentry initialization
1630 * @dentry: the dentry to initialize
1631 * @mode: mode used to determine resource type
1632 * @name: name of the last path component
1633 * @xattr_name: name of the security/LSM xattr
1634 * @ctx: pointer to the resulting LSM context
1635 * @ctxlen: length of @ctx
1637 * Compute a context for a dentry as the inode is not yet available since NFSv4
1638 * has no label backed by an EA anyway. It is important to note that
1639 * @xattr_name does not need to be free'd by the caller, it is a static string.
1641 * Return: Returns 0 on success, negative values on failure.
1643 int security_dentry_init_security(struct dentry *dentry, int mode,
1644 const struct qstr *name,
1645 const char **xattr_name, void **ctx,
1646 u32 *ctxlen)
1648 return call_int_hook(dentry_init_security, dentry, mode, name,
1649 xattr_name, ctx, ctxlen);
1651 EXPORT_SYMBOL(security_dentry_init_security);
1654 * security_dentry_create_files_as() - Perform dentry initialization
1655 * @dentry: the dentry to initialize
1656 * @mode: mode used to determine resource type
1657 * @name: name of the last path component
1658 * @old: creds to use for LSM context calculations
1659 * @new: creds to modify
1661 * Compute a context for a dentry as the inode is not yet available and set
1662 * that context in passed in creds so that new files are created using that
1663 * context. Context is calculated using the passed in creds and not the creds
1664 * of the caller.
1666 * Return: Returns 0 on success, error on failure.
1668 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1669 struct qstr *name,
1670 const struct cred *old, struct cred *new)
1672 return call_int_hook(dentry_create_files_as, dentry, mode,
1673 name, old, new);
1675 EXPORT_SYMBOL(security_dentry_create_files_as);
1678 * security_inode_init_security() - Initialize an inode's LSM context
1679 * @inode: the inode
1680 * @dir: parent directory
1681 * @qstr: last component of the pathname
1682 * @initxattrs: callback function to write xattrs
1683 * @fs_data: filesystem specific data
1685 * Obtain the security attribute name suffix and value to set on a newly
1686 * created inode and set up the incore security field for the new inode. This
1687 * hook is called by the fs code as part of the inode creation transaction and
1688 * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1689 * hooks called by the VFS.
1691 * The hook function is expected to populate the xattrs array, by calling
1692 * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
1693 * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
1694 * slot, the hook function should set ->name to the attribute name suffix
1695 * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
1696 * to the attribute value, to set ->value_len to the length of the value. If
1697 * the security module does not use security attributes or does not wish to put
1698 * a security attribute on this particular inode, then it should return
1699 * -EOPNOTSUPP to skip this processing.
1701 * Return: Returns 0 if the LSM successfully initialized all of the inode
1702 * security attributes that are required, negative values otherwise.
1704 int security_inode_init_security(struct inode *inode, struct inode *dir,
1705 const struct qstr *qstr,
1706 const initxattrs initxattrs, void *fs_data)
1708 struct security_hook_list *hp;
1709 struct xattr *new_xattrs = NULL;
1710 int ret = -EOPNOTSUPP, xattr_count = 0;
1712 if (unlikely(IS_PRIVATE(inode)))
1713 return 0;
1715 if (!blob_sizes.lbs_xattr_count)
1716 return 0;
1718 if (initxattrs) {
1719 /* Allocate +1 as terminator. */
1720 new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 1,
1721 sizeof(*new_xattrs), GFP_NOFS);
1722 if (!new_xattrs)
1723 return -ENOMEM;
1726 hlist_for_each_entry(hp, &security_hook_heads.inode_init_security,
1727 list) {
1728 ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs,
1729 &xattr_count);
1730 if (ret && ret != -EOPNOTSUPP)
1731 goto out;
1733 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
1734 * means that the LSM is not willing to provide an xattr, not
1735 * that it wants to signal an error. Thus, continue to invoke
1736 * the remaining LSMs.
1740 /* If initxattrs() is NULL, xattr_count is zero, skip the call. */
1741 if (!xattr_count)
1742 goto out;
1744 ret = initxattrs(inode, new_xattrs, fs_data);
1745 out:
1746 for (; xattr_count > 0; xattr_count--)
1747 kfree(new_xattrs[xattr_count - 1].value);
1748 kfree(new_xattrs);
1749 return (ret == -EOPNOTSUPP) ? 0 : ret;
1751 EXPORT_SYMBOL(security_inode_init_security);
1754 * security_inode_init_security_anon() - Initialize an anonymous inode
1755 * @inode: the inode
1756 * @name: the anonymous inode class
1757 * @context_inode: an optional related inode
1759 * Set up the incore security field for the new anonymous inode and return
1760 * whether the inode creation is permitted by the security module or not.
1762 * Return: Returns 0 on success, -EACCES if the security module denies the
1763 * creation of this inode, or another -errno upon other errors.
1765 int security_inode_init_security_anon(struct inode *inode,
1766 const struct qstr *name,
1767 const struct inode *context_inode)
1769 return call_int_hook(inode_init_security_anon, inode, name,
1770 context_inode);
1773 #ifdef CONFIG_SECURITY_PATH
1775 * security_path_mknod() - Check if creating a special file is allowed
1776 * @dir: parent directory
1777 * @dentry: new file
1778 * @mode: new file mode
1779 * @dev: device number
1781 * Check permissions when creating a file. Note that this hook is called even
1782 * if mknod operation is being done for a regular file.
1784 * Return: Returns 0 if permission is granted.
1786 int security_path_mknod(const struct path *dir, struct dentry *dentry,
1787 umode_t mode, unsigned int dev)
1789 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1790 return 0;
1791 return call_int_hook(path_mknod, dir, dentry, mode, dev);
1793 EXPORT_SYMBOL(security_path_mknod);
1796 * security_path_post_mknod() - Update inode security after reg file creation
1797 * @idmap: idmap of the mount
1798 * @dentry: new file
1800 * Update inode security field after a regular file has been created.
1802 void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
1804 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1805 return;
1806 call_void_hook(path_post_mknod, idmap, dentry);
1810 * security_path_mkdir() - Check if creating a new directory is allowed
1811 * @dir: parent directory
1812 * @dentry: new directory
1813 * @mode: new directory mode
1815 * Check permissions to create a new directory in the existing directory.
1817 * Return: Returns 0 if permission is granted.
1819 int security_path_mkdir(const struct path *dir, struct dentry *dentry,
1820 umode_t mode)
1822 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1823 return 0;
1824 return call_int_hook(path_mkdir, dir, dentry, mode);
1826 EXPORT_SYMBOL(security_path_mkdir);
1829 * security_path_rmdir() - Check if removing a directory is allowed
1830 * @dir: parent directory
1831 * @dentry: directory to remove
1833 * Check the permission to remove a directory.
1835 * Return: Returns 0 if permission is granted.
1837 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1839 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1840 return 0;
1841 return call_int_hook(path_rmdir, dir, dentry);
1845 * security_path_unlink() - Check if removing a hard link is allowed
1846 * @dir: parent directory
1847 * @dentry: file
1849 * Check the permission to remove a hard link to a file.
1851 * Return: Returns 0 if permission is granted.
1853 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1855 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1856 return 0;
1857 return call_int_hook(path_unlink, dir, dentry);
1859 EXPORT_SYMBOL(security_path_unlink);
1862 * security_path_symlink() - Check if creating a symbolic link is allowed
1863 * @dir: parent directory
1864 * @dentry: symbolic link
1865 * @old_name: file pathname
1867 * Check the permission to create a symbolic link to a file.
1869 * Return: Returns 0 if permission is granted.
1871 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1872 const char *old_name)
1874 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1875 return 0;
1876 return call_int_hook(path_symlink, dir, dentry, old_name);
1880 * security_path_link - Check if creating a hard link is allowed
1881 * @old_dentry: existing file
1882 * @new_dir: new parent directory
1883 * @new_dentry: new link
1885 * Check permission before creating a new hard link to a file.
1887 * Return: Returns 0 if permission is granted.
1889 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1890 struct dentry *new_dentry)
1892 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1893 return 0;
1894 return call_int_hook(path_link, old_dentry, new_dir, new_dentry);
1898 * security_path_rename() - Check if renaming a file is allowed
1899 * @old_dir: parent directory of the old file
1900 * @old_dentry: the old file
1901 * @new_dir: parent directory of the new file
1902 * @new_dentry: the new file
1903 * @flags: flags
1905 * Check for permission to rename a file or directory.
1907 * Return: Returns 0 if permission is granted.
1909 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1910 const struct path *new_dir, struct dentry *new_dentry,
1911 unsigned int flags)
1913 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1914 (d_is_positive(new_dentry) &&
1915 IS_PRIVATE(d_backing_inode(new_dentry)))))
1916 return 0;
1918 return call_int_hook(path_rename, old_dir, old_dentry, new_dir,
1919 new_dentry, flags);
1921 EXPORT_SYMBOL(security_path_rename);
1924 * security_path_truncate() - Check if truncating a file is allowed
1925 * @path: file
1927 * Check permission before truncating the file indicated by path. Note that
1928 * truncation permissions may also be checked based on already opened files,
1929 * using the security_file_truncate() hook.
1931 * Return: Returns 0 if permission is granted.
1933 int security_path_truncate(const struct path *path)
1935 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1936 return 0;
1937 return call_int_hook(path_truncate, path);
1941 * security_path_chmod() - Check if changing the file's mode is allowed
1942 * @path: file
1943 * @mode: new mode
1945 * Check for permission to change a mode of the file @path. The new mode is
1946 * specified in @mode which is a bitmask of constants from
1947 * <include/uapi/linux/stat.h>.
1949 * Return: Returns 0 if permission is granted.
1951 int security_path_chmod(const struct path *path, umode_t mode)
1953 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1954 return 0;
1955 return call_int_hook(path_chmod, path, mode);
1959 * security_path_chown() - Check if changing the file's owner/group is allowed
1960 * @path: file
1961 * @uid: file owner
1962 * @gid: file group
1964 * Check for permission to change owner/group of a file or directory.
1966 * Return: Returns 0 if permission is granted.
1968 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1970 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1971 return 0;
1972 return call_int_hook(path_chown, path, uid, gid);
1976 * security_path_chroot() - Check if changing the root directory is allowed
1977 * @path: directory
1979 * Check for permission to change root directory.
1981 * Return: Returns 0 if permission is granted.
1983 int security_path_chroot(const struct path *path)
1985 return call_int_hook(path_chroot, path);
1987 #endif /* CONFIG_SECURITY_PATH */
1990 * security_inode_create() - Check if creating a file is allowed
1991 * @dir: the parent directory
1992 * @dentry: the file being created
1993 * @mode: requested file mode
1995 * Check permission to create a regular file.
1997 * Return: Returns 0 if permission is granted.
1999 int security_inode_create(struct inode *dir, struct dentry *dentry,
2000 umode_t mode)
2002 if (unlikely(IS_PRIVATE(dir)))
2003 return 0;
2004 return call_int_hook(inode_create, dir, dentry, mode);
2006 EXPORT_SYMBOL_GPL(security_inode_create);
2009 * security_inode_post_create_tmpfile() - Update inode security of new tmpfile
2010 * @idmap: idmap of the mount
2011 * @inode: inode of the new tmpfile
2013 * Update inode security data after a tmpfile has been created.
2015 void security_inode_post_create_tmpfile(struct mnt_idmap *idmap,
2016 struct inode *inode)
2018 if (unlikely(IS_PRIVATE(inode)))
2019 return;
2020 call_void_hook(inode_post_create_tmpfile, idmap, inode);
2024 * security_inode_link() - Check if creating a hard link is allowed
2025 * @old_dentry: existing file
2026 * @dir: new parent directory
2027 * @new_dentry: new link
2029 * Check permission before creating a new hard link to a file.
2031 * Return: Returns 0 if permission is granted.
2033 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
2034 struct dentry *new_dentry)
2036 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
2037 return 0;
2038 return call_int_hook(inode_link, old_dentry, dir, new_dentry);
2042 * security_inode_unlink() - Check if removing a hard link is allowed
2043 * @dir: parent directory
2044 * @dentry: file
2046 * Check the permission to remove a hard link to a file.
2048 * Return: Returns 0 if permission is granted.
2050 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
2052 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2053 return 0;
2054 return call_int_hook(inode_unlink, dir, dentry);
2058 * security_inode_symlink() - Check if creating a symbolic link is allowed
2059 * @dir: parent directory
2060 * @dentry: symbolic link
2061 * @old_name: existing filename
2063 * Check the permission to create a symbolic link to a file.
2065 * Return: Returns 0 if permission is granted.
2067 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
2068 const char *old_name)
2070 if (unlikely(IS_PRIVATE(dir)))
2071 return 0;
2072 return call_int_hook(inode_symlink, dir, dentry, old_name);
2076 * security_inode_mkdir() - Check if creation a new director is allowed
2077 * @dir: parent directory
2078 * @dentry: new directory
2079 * @mode: new directory mode
2081 * Check permissions to create a new directory in the existing directory
2082 * associated with inode structure @dir.
2084 * Return: Returns 0 if permission is granted.
2086 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2088 if (unlikely(IS_PRIVATE(dir)))
2089 return 0;
2090 return call_int_hook(inode_mkdir, dir, dentry, mode);
2092 EXPORT_SYMBOL_GPL(security_inode_mkdir);
2095 * security_inode_rmdir() - Check if removing a directory is allowed
2096 * @dir: parent directory
2097 * @dentry: directory to be removed
2099 * Check the permission to remove a directory.
2101 * Return: Returns 0 if permission is granted.
2103 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
2105 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2106 return 0;
2107 return call_int_hook(inode_rmdir, dir, dentry);
2111 * security_inode_mknod() - Check if creating a special file is allowed
2112 * @dir: parent directory
2113 * @dentry: new file
2114 * @mode: new file mode
2115 * @dev: device number
2117 * Check permissions when creating a special file (or a socket or a fifo file
2118 * created via the mknod system call). Note that if mknod operation is being
2119 * done for a regular file, then the create hook will be called and not this
2120 * hook.
2122 * Return: Returns 0 if permission is granted.
2124 int security_inode_mknod(struct inode *dir, struct dentry *dentry,
2125 umode_t mode, dev_t dev)
2127 if (unlikely(IS_PRIVATE(dir)))
2128 return 0;
2129 return call_int_hook(inode_mknod, dir, dentry, mode, dev);
2133 * security_inode_rename() - Check if renaming a file is allowed
2134 * @old_dir: parent directory of the old file
2135 * @old_dentry: the old file
2136 * @new_dir: parent directory of the new file
2137 * @new_dentry: the new file
2138 * @flags: flags
2140 * Check for permission to rename a file or directory.
2142 * Return: Returns 0 if permission is granted.
2144 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
2145 struct inode *new_dir, struct dentry *new_dentry,
2146 unsigned int flags)
2148 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
2149 (d_is_positive(new_dentry) &&
2150 IS_PRIVATE(d_backing_inode(new_dentry)))))
2151 return 0;
2153 if (flags & RENAME_EXCHANGE) {
2154 int err = call_int_hook(inode_rename, new_dir, new_dentry,
2155 old_dir, old_dentry);
2156 if (err)
2157 return err;
2160 return call_int_hook(inode_rename, old_dir, old_dentry,
2161 new_dir, new_dentry);
2165 * security_inode_readlink() - Check if reading a symbolic link is allowed
2166 * @dentry: link
2168 * Check the permission to read the symbolic link.
2170 * Return: Returns 0 if permission is granted.
2172 int security_inode_readlink(struct dentry *dentry)
2174 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2175 return 0;
2176 return call_int_hook(inode_readlink, dentry);
2180 * security_inode_follow_link() - Check if following a symbolic link is allowed
2181 * @dentry: link dentry
2182 * @inode: link inode
2183 * @rcu: true if in RCU-walk mode
2185 * Check permission to follow a symbolic link when looking up a pathname. If
2186 * @rcu is true, @inode is not stable.
2188 * Return: Returns 0 if permission is granted.
2190 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
2191 bool rcu)
2193 if (unlikely(IS_PRIVATE(inode)))
2194 return 0;
2195 return call_int_hook(inode_follow_link, dentry, inode, rcu);
2199 * security_inode_permission() - Check if accessing an inode is allowed
2200 * @inode: inode
2201 * @mask: access mask
2203 * Check permission before accessing an inode. This hook is called by the
2204 * existing Linux permission function, so a security module can use it to
2205 * provide additional checking for existing Linux permission checks. Notice
2206 * that this hook is called when a file is opened (as well as many other
2207 * operations), whereas the file_security_ops permission hook is called when
2208 * the actual read/write operations are performed.
2210 * Return: Returns 0 if permission is granted.
2212 int security_inode_permission(struct inode *inode, int mask)
2214 if (unlikely(IS_PRIVATE(inode)))
2215 return 0;
2216 return call_int_hook(inode_permission, inode, mask);
2220 * security_inode_setattr() - Check if setting file attributes is allowed
2221 * @idmap: idmap of the mount
2222 * @dentry: file
2223 * @attr: new attributes
2225 * Check permission before setting file attributes. Note that the kernel call
2226 * to notify_change is performed from several locations, whenever file
2227 * attributes change (such as when a file is truncated, chown/chmod operations,
2228 * transferring disk quotas, etc).
2230 * Return: Returns 0 if permission is granted.
2232 int security_inode_setattr(struct mnt_idmap *idmap,
2233 struct dentry *dentry, struct iattr *attr)
2235 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2236 return 0;
2237 return call_int_hook(inode_setattr, idmap, dentry, attr);
2239 EXPORT_SYMBOL_GPL(security_inode_setattr);
2242 * security_inode_post_setattr() - Update the inode after a setattr operation
2243 * @idmap: idmap of the mount
2244 * @dentry: file
2245 * @ia_valid: file attributes set
2247 * Update inode security field after successful setting file attributes.
2249 void security_inode_post_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2250 int ia_valid)
2252 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2253 return;
2254 call_void_hook(inode_post_setattr, idmap, dentry, ia_valid);
2258 * security_inode_getattr() - Check if getting file attributes is allowed
2259 * @path: file
2261 * Check permission before obtaining file attributes.
2263 * Return: Returns 0 if permission is granted.
2265 int security_inode_getattr(const struct path *path)
2267 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
2268 return 0;
2269 return call_int_hook(inode_getattr, path);
2273 * security_inode_setxattr() - Check if setting file xattrs is allowed
2274 * @idmap: idmap of the mount
2275 * @dentry: file
2276 * @name: xattr name
2277 * @value: xattr value
2278 * @size: size of xattr value
2279 * @flags: flags
2281 * This hook performs the desired permission checks before setting the extended
2282 * attributes (xattrs) on @dentry. It is important to note that we have some
2283 * additional logic before the main LSM implementation calls to detect if we
2284 * need to perform an additional capability check at the LSM layer.
2286 * Normally we enforce a capability check prior to executing the various LSM
2287 * hook implementations, but if a LSM wants to avoid this capability check,
2288 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
2289 * xattrs that it wants to avoid the capability check, leaving the LSM fully
2290 * responsible for enforcing the access control for the specific xattr. If all
2291 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
2292 * or return a 0 (the default return value), the capability check is still
2293 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
2294 * check is performed.
2296 * Return: Returns 0 if permission is granted.
2298 int security_inode_setxattr(struct mnt_idmap *idmap,
2299 struct dentry *dentry, const char *name,
2300 const void *value, size_t size, int flags)
2302 int rc;
2304 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2305 return 0;
2307 /* enforce the capability checks at the lsm layer, if needed */
2308 if (!call_int_hook(inode_xattr_skipcap, name)) {
2309 rc = cap_inode_setxattr(dentry, name, value, size, flags);
2310 if (rc)
2311 return rc;
2314 return call_int_hook(inode_setxattr, idmap, dentry, name, value, size,
2315 flags);
2319 * security_inode_set_acl() - Check if setting posix acls is allowed
2320 * @idmap: idmap of the mount
2321 * @dentry: file
2322 * @acl_name: acl name
2323 * @kacl: acl struct
2325 * Check permission before setting posix acls, the posix acls in @kacl are
2326 * identified by @acl_name.
2328 * Return: Returns 0 if permission is granted.
2330 int security_inode_set_acl(struct mnt_idmap *idmap,
2331 struct dentry *dentry, const char *acl_name,
2332 struct posix_acl *kacl)
2334 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2335 return 0;
2336 return call_int_hook(inode_set_acl, idmap, dentry, acl_name, kacl);
2340 * security_inode_post_set_acl() - Update inode security from posix acls set
2341 * @dentry: file
2342 * @acl_name: acl name
2343 * @kacl: acl struct
2345 * Update inode security data after successfully setting posix acls on @dentry.
2346 * The posix acls in @kacl are identified by @acl_name.
2348 void security_inode_post_set_acl(struct dentry *dentry, const char *acl_name,
2349 struct posix_acl *kacl)
2351 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2352 return;
2353 call_void_hook(inode_post_set_acl, dentry, acl_name, kacl);
2357 * security_inode_get_acl() - Check if reading posix acls is allowed
2358 * @idmap: idmap of the mount
2359 * @dentry: file
2360 * @acl_name: acl name
2362 * Check permission before getting osix acls, the posix acls are identified by
2363 * @acl_name.
2365 * Return: Returns 0 if permission is granted.
2367 int security_inode_get_acl(struct mnt_idmap *idmap,
2368 struct dentry *dentry, const char *acl_name)
2370 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2371 return 0;
2372 return call_int_hook(inode_get_acl, idmap, dentry, acl_name);
2376 * security_inode_remove_acl() - Check if removing a posix acl is allowed
2377 * @idmap: idmap of the mount
2378 * @dentry: file
2379 * @acl_name: acl name
2381 * Check permission before removing posix acls, the posix acls are identified
2382 * by @acl_name.
2384 * Return: Returns 0 if permission is granted.
2386 int security_inode_remove_acl(struct mnt_idmap *idmap,
2387 struct dentry *dentry, const char *acl_name)
2389 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2390 return 0;
2391 return call_int_hook(inode_remove_acl, idmap, dentry, acl_name);
2395 * security_inode_post_remove_acl() - Update inode security after rm posix acls
2396 * @idmap: idmap of the mount
2397 * @dentry: file
2398 * @acl_name: acl name
2400 * Update inode security data after successfully removing posix acls on
2401 * @dentry in @idmap. The posix acls are identified by @acl_name.
2403 void security_inode_post_remove_acl(struct mnt_idmap *idmap,
2404 struct dentry *dentry, const char *acl_name)
2406 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2407 return;
2408 call_void_hook(inode_post_remove_acl, idmap, dentry, acl_name);
2412 * security_inode_post_setxattr() - Update the inode after a setxattr operation
2413 * @dentry: file
2414 * @name: xattr name
2415 * @value: xattr value
2416 * @size: xattr value size
2417 * @flags: flags
2419 * Update inode security field after successful setxattr operation.
2421 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2422 const void *value, size_t size, int flags)
2424 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2425 return;
2426 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2430 * security_inode_getxattr() - Check if xattr access is allowed
2431 * @dentry: file
2432 * @name: xattr name
2434 * Check permission before obtaining the extended attributes identified by
2435 * @name for @dentry.
2437 * Return: Returns 0 if permission is granted.
2439 int security_inode_getxattr(struct dentry *dentry, const char *name)
2441 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2442 return 0;
2443 return call_int_hook(inode_getxattr, dentry, name);
2447 * security_inode_listxattr() - Check if listing xattrs is allowed
2448 * @dentry: file
2450 * Check permission before obtaining the list of extended attribute names for
2451 * @dentry.
2453 * Return: Returns 0 if permission is granted.
2455 int security_inode_listxattr(struct dentry *dentry)
2457 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2458 return 0;
2459 return call_int_hook(inode_listxattr, dentry);
2463 * security_inode_removexattr() - Check if removing an xattr is allowed
2464 * @idmap: idmap of the mount
2465 * @dentry: file
2466 * @name: xattr name
2468 * This hook performs the desired permission checks before setting the extended
2469 * attributes (xattrs) on @dentry. It is important to note that we have some
2470 * additional logic before the main LSM implementation calls to detect if we
2471 * need to perform an additional capability check at the LSM layer.
2473 * Normally we enforce a capability check prior to executing the various LSM
2474 * hook implementations, but if a LSM wants to avoid this capability check,
2475 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for
2476 * xattrs that it wants to avoid the capability check, leaving the LSM fully
2477 * responsible for enforcing the access control for the specific xattr. If all
2478 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook,
2479 * or return a 0 (the default return value), the capability check is still
2480 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability
2481 * check is performed.
2483 * Return: Returns 0 if permission is granted.
2485 int security_inode_removexattr(struct mnt_idmap *idmap,
2486 struct dentry *dentry, const char *name)
2488 int rc;
2490 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2491 return 0;
2493 /* enforce the capability checks at the lsm layer, if needed */
2494 if (!call_int_hook(inode_xattr_skipcap, name)) {
2495 rc = cap_inode_removexattr(idmap, dentry, name);
2496 if (rc)
2497 return rc;
2500 return call_int_hook(inode_removexattr, idmap, dentry, name);
2504 * security_inode_post_removexattr() - Update the inode after a removexattr op
2505 * @dentry: file
2506 * @name: xattr name
2508 * Update the inode after a successful removexattr operation.
2510 void security_inode_post_removexattr(struct dentry *dentry, const char *name)
2512 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2513 return;
2514 call_void_hook(inode_post_removexattr, dentry, name);
2518 * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2519 * @dentry: associated dentry
2521 * Called when an inode has been changed to determine if
2522 * security_inode_killpriv() should be called.
2524 * Return: Return <0 on error to abort the inode change operation, return 0 if
2525 * security_inode_killpriv() does not need to be called, return >0 if
2526 * security_inode_killpriv() does need to be called.
2528 int security_inode_need_killpriv(struct dentry *dentry)
2530 return call_int_hook(inode_need_killpriv, dentry);
2534 * security_inode_killpriv() - The setuid bit is removed, update LSM state
2535 * @idmap: idmap of the mount
2536 * @dentry: associated dentry
2538 * The @dentry's setuid bit is being removed. Remove similar security labels.
2539 * Called with the dentry->d_inode->i_mutex held.
2541 * Return: Return 0 on success. If error is returned, then the operation
2542 * causing setuid bit removal is failed.
2544 int security_inode_killpriv(struct mnt_idmap *idmap,
2545 struct dentry *dentry)
2547 return call_int_hook(inode_killpriv, idmap, dentry);
2551 * security_inode_getsecurity() - Get the xattr security label of an inode
2552 * @idmap: idmap of the mount
2553 * @inode: inode
2554 * @name: xattr name
2555 * @buffer: security label buffer
2556 * @alloc: allocation flag
2558 * Retrieve a copy of the extended attribute representation of the security
2559 * label associated with @name for @inode via @buffer. Note that @name is the
2560 * remainder of the attribute name after the security prefix has been removed.
2561 * @alloc is used to specify if the call should return a value via the buffer
2562 * or just the value length.
2564 * Return: Returns size of buffer on success.
2566 int security_inode_getsecurity(struct mnt_idmap *idmap,
2567 struct inode *inode, const char *name,
2568 void **buffer, bool alloc)
2570 if (unlikely(IS_PRIVATE(inode)))
2571 return LSM_RET_DEFAULT(inode_getsecurity);
2573 return call_int_hook(inode_getsecurity, idmap, inode, name, buffer,
2574 alloc);
2578 * security_inode_setsecurity() - Set the xattr security label of an inode
2579 * @inode: inode
2580 * @name: xattr name
2581 * @value: security label
2582 * @size: length of security label
2583 * @flags: flags
2585 * Set the security label associated with @name for @inode from the extended
2586 * attribute value @value. @size indicates the size of the @value in bytes.
2587 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2588 * remainder of the attribute name after the security. prefix has been removed.
2590 * Return: Returns 0 on success.
2592 int security_inode_setsecurity(struct inode *inode, const char *name,
2593 const void *value, size_t size, int flags)
2595 if (unlikely(IS_PRIVATE(inode)))
2596 return LSM_RET_DEFAULT(inode_setsecurity);
2598 return call_int_hook(inode_setsecurity, inode, name, value, size,
2599 flags);
2603 * security_inode_listsecurity() - List the xattr security label names
2604 * @inode: inode
2605 * @buffer: buffer
2606 * @buffer_size: size of buffer
2608 * Copy the extended attribute names for the security labels associated with
2609 * @inode into @buffer. The maximum size of @buffer is specified by
2610 * @buffer_size. @buffer may be NULL to request the size of the buffer
2611 * required.
2613 * Return: Returns number of bytes used/required on success.
2615 int security_inode_listsecurity(struct inode *inode,
2616 char *buffer, size_t buffer_size)
2618 if (unlikely(IS_PRIVATE(inode)))
2619 return 0;
2620 return call_int_hook(inode_listsecurity, inode, buffer, buffer_size);
2622 EXPORT_SYMBOL(security_inode_listsecurity);
2625 * security_inode_getsecid() - Get an inode's secid
2626 * @inode: inode
2627 * @secid: secid to return
2629 * Get the secid associated with the node. In case of failure, @secid will be
2630 * set to zero.
2632 void security_inode_getsecid(struct inode *inode, u32 *secid)
2634 call_void_hook(inode_getsecid, inode, secid);
2638 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2639 * @src: union dentry of copy-up file
2640 * @new: newly created creds
2642 * A file is about to be copied up from lower layer to upper layer of overlay
2643 * filesystem. Security module can prepare a set of new creds and modify as
2644 * need be and return new creds. Caller will switch to new creds temporarily to
2645 * create new file and release newly allocated creds.
2647 * Return: Returns 0 on success or a negative error code on error.
2649 int security_inode_copy_up(struct dentry *src, struct cred **new)
2651 return call_int_hook(inode_copy_up, src, new);
2653 EXPORT_SYMBOL(security_inode_copy_up);
2656 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2657 * @src: union dentry of copy-up file
2658 * @name: xattr name
2660 * Filter the xattrs being copied up when a unioned file is copied up from a
2661 * lower layer to the union/overlay layer. The caller is responsible for
2662 * reading and writing the xattrs, this hook is merely a filter.
2664 * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2665 * if the security module does not know about attribute, or a negative
2666 * error code to abort the copy up.
2668 int security_inode_copy_up_xattr(struct dentry *src, const char *name)
2670 int rc;
2673 * The implementation can return 0 (accept the xattr), 1 (discard the
2674 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2675 * any other error code in case of an error.
2677 rc = call_int_hook(inode_copy_up_xattr, src, name);
2678 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2679 return rc;
2681 return LSM_RET_DEFAULT(inode_copy_up_xattr);
2683 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2686 * security_kernfs_init_security() - Init LSM context for a kernfs node
2687 * @kn_dir: parent kernfs node
2688 * @kn: the kernfs node to initialize
2690 * Initialize the security context of a newly created kernfs node based on its
2691 * own and its parent's attributes.
2693 * Return: Returns 0 if permission is granted.
2695 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2696 struct kernfs_node *kn)
2698 return call_int_hook(kernfs_init_security, kn_dir, kn);
2702 * security_file_permission() - Check file permissions
2703 * @file: file
2704 * @mask: requested permissions
2706 * Check file permissions before accessing an open file. This hook is called
2707 * by various operations that read or write files. A security module can use
2708 * this hook to perform additional checking on these operations, e.g. to
2709 * revalidate permissions on use to support privilege bracketing or policy
2710 * changes. Notice that this hook is used when the actual read/write
2711 * operations are performed, whereas the inode_security_ops hook is called when
2712 * a file is opened (as well as many other operations). Although this hook can
2713 * be used to revalidate permissions for various system call operations that
2714 * read or write files, it does not address the revalidation of permissions for
2715 * memory-mapped files. Security modules must handle this separately if they
2716 * need such revalidation.
2718 * Return: Returns 0 if permission is granted.
2720 int security_file_permission(struct file *file, int mask)
2722 return call_int_hook(file_permission, file, mask);
2726 * security_file_alloc() - Allocate and init a file's LSM blob
2727 * @file: the file
2729 * Allocate and attach a security structure to the file->f_security field. The
2730 * security field is initialized to NULL when the structure is first created.
2732 * Return: Return 0 if the hook is successful and permission is granted.
2734 int security_file_alloc(struct file *file)
2736 int rc = lsm_file_alloc(file);
2738 if (rc)
2739 return rc;
2740 rc = call_int_hook(file_alloc_security, file);
2741 if (unlikely(rc))
2742 security_file_free(file);
2743 return rc;
2747 * security_file_release() - Perform actions before releasing the file ref
2748 * @file: the file
2750 * Perform actions before releasing the last reference to a file.
2752 void security_file_release(struct file *file)
2754 call_void_hook(file_release, file);
2758 * security_file_free() - Free a file's LSM blob
2759 * @file: the file
2761 * Deallocate and free any security structures stored in file->f_security.
2763 void security_file_free(struct file *file)
2765 void *blob;
2767 call_void_hook(file_free_security, file);
2769 blob = file->f_security;
2770 if (blob) {
2771 file->f_security = NULL;
2772 kmem_cache_free(lsm_file_cache, blob);
2777 * security_file_ioctl() - Check if an ioctl is allowed
2778 * @file: associated file
2779 * @cmd: ioctl cmd
2780 * @arg: ioctl arguments
2782 * Check permission for an ioctl operation on @file. Note that @arg sometimes
2783 * represents a user space pointer; in other cases, it may be a simple integer
2784 * value. When @arg represents a user space pointer, it should never be used
2785 * by the security module.
2787 * Return: Returns 0 if permission is granted.
2789 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2791 return call_int_hook(file_ioctl, file, cmd, arg);
2793 EXPORT_SYMBOL_GPL(security_file_ioctl);
2796 * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
2797 * @file: associated file
2798 * @cmd: ioctl cmd
2799 * @arg: ioctl arguments
2801 * Compat version of security_file_ioctl() that correctly handles 32-bit
2802 * processes running on 64-bit kernels.
2804 * Return: Returns 0 if permission is granted.
2806 int security_file_ioctl_compat(struct file *file, unsigned int cmd,
2807 unsigned long arg)
2809 return call_int_hook(file_ioctl_compat, file, cmd, arg);
2811 EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
2813 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2816 * Does we have PROT_READ and does the application expect
2817 * it to imply PROT_EXEC? If not, nothing to talk about...
2819 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2820 return prot;
2821 if (!(current->personality & READ_IMPLIES_EXEC))
2822 return prot;
2824 * if that's an anonymous mapping, let it.
2826 if (!file)
2827 return prot | PROT_EXEC;
2829 * ditto if it's not on noexec mount, except that on !MMU we need
2830 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2832 if (!path_noexec(&file->f_path)) {
2833 #ifndef CONFIG_MMU
2834 if (file->f_op->mmap_capabilities) {
2835 unsigned caps = file->f_op->mmap_capabilities(file);
2836 if (!(caps & NOMMU_MAP_EXEC))
2837 return prot;
2839 #endif
2840 return prot | PROT_EXEC;
2842 /* anything on noexec mount won't get PROT_EXEC */
2843 return prot;
2847 * security_mmap_file() - Check if mmap'ing a file is allowed
2848 * @file: file
2849 * @prot: protection applied by the kernel
2850 * @flags: flags
2852 * Check permissions for a mmap operation. The @file may be NULL, e.g. if
2853 * mapping anonymous memory.
2855 * Return: Returns 0 if permission is granted.
2857 int security_mmap_file(struct file *file, unsigned long prot,
2858 unsigned long flags)
2860 return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot),
2861 flags);
2865 * security_mmap_addr() - Check if mmap'ing an address is allowed
2866 * @addr: address
2868 * Check permissions for a mmap operation at @addr.
2870 * Return: Returns 0 if permission is granted.
2872 int security_mmap_addr(unsigned long addr)
2874 return call_int_hook(mmap_addr, addr);
2878 * security_file_mprotect() - Check if changing memory protections is allowed
2879 * @vma: memory region
2880 * @reqprot: application requested protection
2881 * @prot: protection applied by the kernel
2883 * Check permissions before changing memory access permissions.
2885 * Return: Returns 0 if permission is granted.
2887 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2888 unsigned long prot)
2890 return call_int_hook(file_mprotect, vma, reqprot, prot);
2894 * security_file_lock() - Check if a file lock is allowed
2895 * @file: file
2896 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2898 * Check permission before performing file locking operations. Note the hook
2899 * mediates both flock and fcntl style locks.
2901 * Return: Returns 0 if permission is granted.
2903 int security_file_lock(struct file *file, unsigned int cmd)
2905 return call_int_hook(file_lock, file, cmd);
2909 * security_file_fcntl() - Check if fcntl() op is allowed
2910 * @file: file
2911 * @cmd: fcntl command
2912 * @arg: command argument
2914 * Check permission before allowing the file operation specified by @cmd from
2915 * being performed on the file @file. Note that @arg sometimes represents a
2916 * user space pointer; in other cases, it may be a simple integer value. When
2917 * @arg represents a user space pointer, it should never be used by the
2918 * security module.
2920 * Return: Returns 0 if permission is granted.
2922 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2924 return call_int_hook(file_fcntl, file, cmd, arg);
2928 * security_file_set_fowner() - Set the file owner info in the LSM blob
2929 * @file: the file
2931 * Save owner security information (typically from current->security) in
2932 * file->f_security for later use by the send_sigiotask hook.
2934 * Return: Returns 0 on success.
2936 void security_file_set_fowner(struct file *file)
2938 call_void_hook(file_set_fowner, file);
2942 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2943 * @tsk: target task
2944 * @fown: signal sender
2945 * @sig: signal to be sent, SIGIO is sent if 0
2947 * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2948 * process @tsk. Note that this hook is sometimes called from interrupt. Note
2949 * that the fown_struct, @fown, is never outside the context of a struct file,
2950 * so the file structure (and associated security information) can always be
2951 * obtained: container_of(fown, struct file, f_owner).
2953 * Return: Returns 0 if permission is granted.
2955 int security_file_send_sigiotask(struct task_struct *tsk,
2956 struct fown_struct *fown, int sig)
2958 return call_int_hook(file_send_sigiotask, tsk, fown, sig);
2962 * security_file_receive() - Check if receiving a file via IPC is allowed
2963 * @file: file being received
2965 * This hook allows security modules to control the ability of a process to
2966 * receive an open file descriptor via socket IPC.
2968 * Return: Returns 0 if permission is granted.
2970 int security_file_receive(struct file *file)
2972 return call_int_hook(file_receive, file);
2976 * security_file_open() - Save open() time state for late use by the LSM
2977 * @file:
2979 * Save open-time permission checking state for later use upon file_permission,
2980 * and recheck access if anything has changed since inode_permission.
2982 * Return: Returns 0 if permission is granted.
2984 int security_file_open(struct file *file)
2986 int ret;
2988 ret = call_int_hook(file_open, file);
2989 if (ret)
2990 return ret;
2992 return fsnotify_open_perm(file);
2996 * security_file_post_open() - Evaluate a file after it has been opened
2997 * @file: the file
2998 * @mask: access mask
3000 * Evaluate an opened file and the access mask requested with open(). The hook
3001 * is useful for LSMs that require the file content to be available in order to
3002 * make decisions.
3004 * Return: Returns 0 if permission is granted.
3006 int security_file_post_open(struct file *file, int mask)
3008 return call_int_hook(file_post_open, file, mask);
3010 EXPORT_SYMBOL_GPL(security_file_post_open);
3013 * security_file_truncate() - Check if truncating a file is allowed
3014 * @file: file
3016 * Check permission before truncating a file, i.e. using ftruncate. Note that
3017 * truncation permission may also be checked based on the path, using the
3018 * @path_truncate hook.
3020 * Return: Returns 0 if permission is granted.
3022 int security_file_truncate(struct file *file)
3024 return call_int_hook(file_truncate, file);
3028 * security_task_alloc() - Allocate a task's LSM blob
3029 * @task: the task
3030 * @clone_flags: flags indicating what is being shared
3032 * Handle allocation of task-related resources.
3034 * Return: Returns a zero on success, negative values on failure.
3036 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
3038 int rc = lsm_task_alloc(task);
3040 if (rc)
3041 return rc;
3042 rc = call_int_hook(task_alloc, task, clone_flags);
3043 if (unlikely(rc))
3044 security_task_free(task);
3045 return rc;
3049 * security_task_free() - Free a task's LSM blob and related resources
3050 * @task: task
3052 * Handle release of task-related resources. Note that this can be called from
3053 * interrupt context.
3055 void security_task_free(struct task_struct *task)
3057 call_void_hook(task_free, task);
3059 kfree(task->security);
3060 task->security = NULL;
3064 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
3065 * @cred: credentials
3066 * @gfp: gfp flags
3068 * Only allocate sufficient memory and attach to @cred such that
3069 * cred_transfer() will not get ENOMEM.
3071 * Return: Returns 0 on success, negative values on failure.
3073 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3075 int rc = lsm_cred_alloc(cred, gfp);
3077 if (rc)
3078 return rc;
3080 rc = call_int_hook(cred_alloc_blank, cred, gfp);
3081 if (unlikely(rc))
3082 security_cred_free(cred);
3083 return rc;
3087 * security_cred_free() - Free the cred's LSM blob and associated resources
3088 * @cred: credentials
3090 * Deallocate and clear the cred->security field in a set of credentials.
3092 void security_cred_free(struct cred *cred)
3095 * There is a failure case in prepare_creds() that
3096 * may result in a call here with ->security being NULL.
3098 if (unlikely(cred->security == NULL))
3099 return;
3101 call_void_hook(cred_free, cred);
3103 kfree(cred->security);
3104 cred->security = NULL;
3108 * security_prepare_creds() - Prepare a new set of credentials
3109 * @new: new credentials
3110 * @old: original credentials
3111 * @gfp: gfp flags
3113 * Prepare a new set of credentials by copying the data from the old set.
3115 * Return: Returns 0 on success, negative values on failure.
3117 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
3119 int rc = lsm_cred_alloc(new, gfp);
3121 if (rc)
3122 return rc;
3124 rc = call_int_hook(cred_prepare, new, old, gfp);
3125 if (unlikely(rc))
3126 security_cred_free(new);
3127 return rc;
3131 * security_transfer_creds() - Transfer creds
3132 * @new: target credentials
3133 * @old: original credentials
3135 * Transfer data from original creds to new creds.
3137 void security_transfer_creds(struct cred *new, const struct cred *old)
3139 call_void_hook(cred_transfer, new, old);
3143 * security_cred_getsecid() - Get the secid from a set of credentials
3144 * @c: credentials
3145 * @secid: secid value
3147 * Retrieve the security identifier of the cred structure @c. In case of
3148 * failure, @secid will be set to zero.
3150 void security_cred_getsecid(const struct cred *c, u32 *secid)
3152 *secid = 0;
3153 call_void_hook(cred_getsecid, c, secid);
3155 EXPORT_SYMBOL(security_cred_getsecid);
3158 * security_kernel_act_as() - Set the kernel credentials to act as secid
3159 * @new: credentials
3160 * @secid: secid
3162 * Set the credentials for a kernel service to act as (subjective context).
3163 * The current task must be the one that nominated @secid.
3165 * Return: Returns 0 if successful.
3167 int security_kernel_act_as(struct cred *new, u32 secid)
3169 return call_int_hook(kernel_act_as, new, secid);
3173 * security_kernel_create_files_as() - Set file creation context using an inode
3174 * @new: target credentials
3175 * @inode: reference inode
3177 * Set the file creation context in a set of credentials to be the same as the
3178 * objective context of the specified inode. The current task must be the one
3179 * that nominated @inode.
3181 * Return: Returns 0 if successful.
3183 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
3185 return call_int_hook(kernel_create_files_as, new, inode);
3189 * security_kernel_module_request() - Check if loading a module is allowed
3190 * @kmod_name: module name
3192 * Ability to trigger the kernel to automatically upcall to userspace for
3193 * userspace to load a kernel module with the given name.
3195 * Return: Returns 0 if successful.
3197 int security_kernel_module_request(char *kmod_name)
3199 return call_int_hook(kernel_module_request, kmod_name);
3203 * security_kernel_read_file() - Read a file specified by userspace
3204 * @file: file
3205 * @id: file identifier
3206 * @contents: trust if security_kernel_post_read_file() will be called
3208 * Read a file specified by userspace.
3210 * Return: Returns 0 if permission is granted.
3212 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
3213 bool contents)
3215 return call_int_hook(kernel_read_file, file, id, contents);
3217 EXPORT_SYMBOL_GPL(security_kernel_read_file);
3220 * security_kernel_post_read_file() - Read a file specified by userspace
3221 * @file: file
3222 * @buf: file contents
3223 * @size: size of file contents
3224 * @id: file identifier
3226 * Read a file specified by userspace. This must be paired with a prior call
3227 * to security_kernel_read_file() call that indicated this hook would also be
3228 * called, see security_kernel_read_file() for more information.
3230 * Return: Returns 0 if permission is granted.
3232 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3233 enum kernel_read_file_id id)
3235 return call_int_hook(kernel_post_read_file, file, buf, size, id);
3237 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3240 * security_kernel_load_data() - Load data provided by userspace
3241 * @id: data identifier
3242 * @contents: true if security_kernel_post_load_data() will be called
3244 * Load data provided by userspace.
3246 * Return: Returns 0 if permission is granted.
3248 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
3250 return call_int_hook(kernel_load_data, id, contents);
3252 EXPORT_SYMBOL_GPL(security_kernel_load_data);
3255 * security_kernel_post_load_data() - Load userspace data from a non-file source
3256 * @buf: data
3257 * @size: size of data
3258 * @id: data identifier
3259 * @description: text description of data, specific to the id value
3261 * Load data provided by a non-file source (usually userspace buffer). This
3262 * must be paired with a prior security_kernel_load_data() call that indicated
3263 * this hook would also be called, see security_kernel_load_data() for more
3264 * information.
3266 * Return: Returns 0 if permission is granted.
3268 int security_kernel_post_load_data(char *buf, loff_t size,
3269 enum kernel_load_data_id id,
3270 char *description)
3272 return call_int_hook(kernel_post_load_data, buf, size, id, description);
3274 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3277 * security_task_fix_setuid() - Update LSM with new user id attributes
3278 * @new: updated credentials
3279 * @old: credentials being replaced
3280 * @flags: LSM_SETID_* flag values
3282 * Update the module's state after setting one or more of the user identity
3283 * attributes of the current process. The @flags parameter indicates which of
3284 * the set*uid system calls invoked this hook. If @new is the set of
3285 * credentials that will be installed. Modifications should be made to this
3286 * rather than to @current->cred.
3288 * Return: Returns 0 on success.
3290 int security_task_fix_setuid(struct cred *new, const struct cred *old,
3291 int flags)
3293 return call_int_hook(task_fix_setuid, new, old, flags);
3297 * security_task_fix_setgid() - Update LSM with new group id attributes
3298 * @new: updated credentials
3299 * @old: credentials being replaced
3300 * @flags: LSM_SETID_* flag value
3302 * Update the module's state after setting one or more of the group identity
3303 * attributes of the current process. The @flags parameter indicates which of
3304 * the set*gid system calls invoked this hook. @new is the set of credentials
3305 * that will be installed. Modifications should be made to this rather than to
3306 * @current->cred.
3308 * Return: Returns 0 on success.
3310 int security_task_fix_setgid(struct cred *new, const struct cred *old,
3311 int flags)
3313 return call_int_hook(task_fix_setgid, new, old, flags);
3317 * security_task_fix_setgroups() - Update LSM with new supplementary groups
3318 * @new: updated credentials
3319 * @old: credentials being replaced
3321 * Update the module's state after setting the supplementary group identity
3322 * attributes of the current process. @new is the set of credentials that will
3323 * be installed. Modifications should be made to this rather than to
3324 * @current->cred.
3326 * Return: Returns 0 on success.
3328 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3330 return call_int_hook(task_fix_setgroups, new, old);
3334 * security_task_setpgid() - Check if setting the pgid is allowed
3335 * @p: task being modified
3336 * @pgid: new pgid
3338 * Check permission before setting the process group identifier of the process
3339 * @p to @pgid.
3341 * Return: Returns 0 if permission is granted.
3343 int security_task_setpgid(struct task_struct *p, pid_t pgid)
3345 return call_int_hook(task_setpgid, p, pgid);
3349 * security_task_getpgid() - Check if getting the pgid is allowed
3350 * @p: task
3352 * Check permission before getting the process group identifier of the process
3353 * @p.
3355 * Return: Returns 0 if permission is granted.
3357 int security_task_getpgid(struct task_struct *p)
3359 return call_int_hook(task_getpgid, p);
3363 * security_task_getsid() - Check if getting the session id is allowed
3364 * @p: task
3366 * Check permission before getting the session identifier of the process @p.
3368 * Return: Returns 0 if permission is granted.
3370 int security_task_getsid(struct task_struct *p)
3372 return call_int_hook(task_getsid, p);
3376 * security_current_getsecid_subj() - Get the current task's subjective secid
3377 * @secid: secid value
3379 * Retrieve the subjective security identifier of the current task and return
3380 * it in @secid. In case of failure, @secid will be set to zero.
3382 void security_current_getsecid_subj(u32 *secid)
3384 *secid = 0;
3385 call_void_hook(current_getsecid_subj, secid);
3387 EXPORT_SYMBOL(security_current_getsecid_subj);
3390 * security_task_getsecid_obj() - Get a task's objective secid
3391 * @p: target task
3392 * @secid: secid value
3394 * Retrieve the objective security identifier of the task_struct in @p and
3395 * return it in @secid. In case of failure, @secid will be set to zero.
3397 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3399 *secid = 0;
3400 call_void_hook(task_getsecid_obj, p, secid);
3402 EXPORT_SYMBOL(security_task_getsecid_obj);
3405 * security_task_setnice() - Check if setting a task's nice value is allowed
3406 * @p: target task
3407 * @nice: nice value
3409 * Check permission before setting the nice value of @p to @nice.
3411 * Return: Returns 0 if permission is granted.
3413 int security_task_setnice(struct task_struct *p, int nice)
3415 return call_int_hook(task_setnice, p, nice);
3419 * security_task_setioprio() - Check if setting a task's ioprio is allowed
3420 * @p: target task
3421 * @ioprio: ioprio value
3423 * Check permission before setting the ioprio value of @p to @ioprio.
3425 * Return: Returns 0 if permission is granted.
3427 int security_task_setioprio(struct task_struct *p, int ioprio)
3429 return call_int_hook(task_setioprio, p, ioprio);
3433 * security_task_getioprio() - Check if getting a task's ioprio is allowed
3434 * @p: task
3436 * Check permission before getting the ioprio value of @p.
3438 * Return: Returns 0 if permission is granted.
3440 int security_task_getioprio(struct task_struct *p)
3442 return call_int_hook(task_getioprio, p);
3446 * security_task_prlimit() - Check if get/setting resources limits is allowed
3447 * @cred: current task credentials
3448 * @tcred: target task credentials
3449 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3451 * Check permission before getting and/or setting the resource limits of
3452 * another task.
3454 * Return: Returns 0 if permission is granted.
3456 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3457 unsigned int flags)
3459 return call_int_hook(task_prlimit, cred, tcred, flags);
3463 * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3464 * @p: target task's group leader
3465 * @resource: resource whose limit is being set
3466 * @new_rlim: new resource limit
3468 * Check permission before setting the resource limits of process @p for
3469 * @resource to @new_rlim. The old resource limit values can be examined by
3470 * dereferencing (p->signal->rlim + resource).
3472 * Return: Returns 0 if permission is granted.
3474 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3475 struct rlimit *new_rlim)
3477 return call_int_hook(task_setrlimit, p, resource, new_rlim);
3481 * security_task_setscheduler() - Check if setting sched policy/param is allowed
3482 * @p: target task
3484 * Check permission before setting scheduling policy and/or parameters of
3485 * process @p.
3487 * Return: Returns 0 if permission is granted.
3489 int security_task_setscheduler(struct task_struct *p)
3491 return call_int_hook(task_setscheduler, p);
3495 * security_task_getscheduler() - Check if getting scheduling info is allowed
3496 * @p: target task
3498 * Check permission before obtaining scheduling information for process @p.
3500 * Return: Returns 0 if permission is granted.
3502 int security_task_getscheduler(struct task_struct *p)
3504 return call_int_hook(task_getscheduler, p);
3508 * security_task_movememory() - Check if moving memory is allowed
3509 * @p: task
3511 * Check permission before moving memory owned by process @p.
3513 * Return: Returns 0 if permission is granted.
3515 int security_task_movememory(struct task_struct *p)
3517 return call_int_hook(task_movememory, p);
3521 * security_task_kill() - Check if sending a signal is allowed
3522 * @p: target process
3523 * @info: signal information
3524 * @sig: signal value
3525 * @cred: credentials of the signal sender, NULL if @current
3527 * Check permission before sending signal @sig to @p. @info can be NULL, the
3528 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
3529 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3530 * the kernel and should typically be permitted. SIGIO signals are handled
3531 * separately by the send_sigiotask hook in file_security_ops.
3533 * Return: Returns 0 if permission is granted.
3535 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3536 int sig, const struct cred *cred)
3538 return call_int_hook(task_kill, p, info, sig, cred);
3542 * security_task_prctl() - Check if a prctl op is allowed
3543 * @option: operation
3544 * @arg2: argument
3545 * @arg3: argument
3546 * @arg4: argument
3547 * @arg5: argument
3549 * Check permission before performing a process control operation on the
3550 * current process.
3552 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3553 * to cause prctl() to return immediately with that value.
3555 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3556 unsigned long arg4, unsigned long arg5)
3558 int thisrc;
3559 int rc = LSM_RET_DEFAULT(task_prctl);
3560 struct security_hook_list *hp;
3562 hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
3563 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
3564 if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3565 rc = thisrc;
3566 if (thisrc != 0)
3567 break;
3570 return rc;
3574 * security_task_to_inode() - Set the security attributes of a task's inode
3575 * @p: task
3576 * @inode: inode
3578 * Set the security attributes for an inode based on an associated task's
3579 * security attributes, e.g. for /proc/pid inodes.
3581 void security_task_to_inode(struct task_struct *p, struct inode *inode)
3583 call_void_hook(task_to_inode, p, inode);
3587 * security_create_user_ns() - Check if creating a new userns is allowed
3588 * @cred: prepared creds
3590 * Check permission prior to creating a new user namespace.
3592 * Return: Returns 0 if successful, otherwise < 0 error code.
3594 int security_create_user_ns(const struct cred *cred)
3596 return call_int_hook(userns_create, cred);
3600 * security_ipc_permission() - Check if sysv ipc access is allowed
3601 * @ipcp: ipc permission structure
3602 * @flag: requested permissions
3604 * Check permissions for access to IPC.
3606 * Return: Returns 0 if permission is granted.
3608 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3610 return call_int_hook(ipc_permission, ipcp, flag);
3614 * security_ipc_getsecid() - Get the sysv ipc object's secid
3615 * @ipcp: ipc permission structure
3616 * @secid: secid pointer
3618 * Get the secid associated with the ipc object. In case of failure, @secid
3619 * will be set to zero.
3621 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3623 *secid = 0;
3624 call_void_hook(ipc_getsecid, ipcp, secid);
3628 * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3629 * @msg: message structure
3631 * Allocate and attach a security structure to the msg->security field. The
3632 * security field is initialized to NULL when the structure is first created.
3634 * Return: Return 0 if operation was successful and permission is granted.
3636 int security_msg_msg_alloc(struct msg_msg *msg)
3638 int rc = lsm_msg_msg_alloc(msg);
3640 if (unlikely(rc))
3641 return rc;
3642 rc = call_int_hook(msg_msg_alloc_security, msg);
3643 if (unlikely(rc))
3644 security_msg_msg_free(msg);
3645 return rc;
3649 * security_msg_msg_free() - Free a sysv ipc message LSM blob
3650 * @msg: message structure
3652 * Deallocate the security structure for this message.
3654 void security_msg_msg_free(struct msg_msg *msg)
3656 call_void_hook(msg_msg_free_security, msg);
3657 kfree(msg->security);
3658 msg->security = NULL;
3662 * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3663 * @msq: sysv ipc permission structure
3665 * Allocate and attach a security structure to @msg. The security field is
3666 * initialized to NULL when the structure is first created.
3668 * Return: Returns 0 if operation was successful and permission is granted.
3670 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
3672 int rc = lsm_ipc_alloc(msq);
3674 if (unlikely(rc))
3675 return rc;
3676 rc = call_int_hook(msg_queue_alloc_security, msq);
3677 if (unlikely(rc))
3678 security_msg_queue_free(msq);
3679 return rc;
3683 * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3684 * @msq: sysv ipc permission structure
3686 * Deallocate security field @perm->security for the message queue.
3688 void security_msg_queue_free(struct kern_ipc_perm *msq)
3690 call_void_hook(msg_queue_free_security, msq);
3691 kfree(msq->security);
3692 msq->security = NULL;
3696 * security_msg_queue_associate() - Check if a msg queue operation is allowed
3697 * @msq: sysv ipc permission structure
3698 * @msqflg: operation flags
3700 * Check permission when a message queue is requested through the msgget system
3701 * call. This hook is only called when returning the message queue identifier
3702 * for an existing message queue, not when a new message queue is created.
3704 * Return: Return 0 if permission is granted.
3706 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
3708 return call_int_hook(msg_queue_associate, msq, msqflg);
3712 * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3713 * @msq: sysv ipc permission structure
3714 * @cmd: operation
3716 * Check permission when a message control operation specified by @cmd is to be
3717 * performed on the message queue with permissions.
3719 * Return: Returns 0 if permission is granted.
3721 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
3723 return call_int_hook(msg_queue_msgctl, msq, cmd);
3727 * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3728 * @msq: sysv ipc permission structure
3729 * @msg: message
3730 * @msqflg: operation flags
3732 * Check permission before a message, @msg, is enqueued on the message queue
3733 * with permissions specified in @msq.
3735 * Return: Returns 0 if permission is granted.
3737 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
3738 struct msg_msg *msg, int msqflg)
3740 return call_int_hook(msg_queue_msgsnd, msq, msg, msqflg);
3744 * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3745 * @msq: sysv ipc permission structure
3746 * @msg: message
3747 * @target: target task
3748 * @type: type of message requested
3749 * @mode: operation flags
3751 * Check permission before a message, @msg, is removed from the message queue.
3752 * The @target task structure contains a pointer to the process that will be
3753 * receiving the message (not equal to the current process when inline receives
3754 * are being performed).
3756 * Return: Returns 0 if permission is granted.
3758 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
3759 struct task_struct *target, long type, int mode)
3761 return call_int_hook(msg_queue_msgrcv, msq, msg, target, type, mode);
3765 * security_shm_alloc() - Allocate a sysv shm LSM blob
3766 * @shp: sysv ipc permission structure
3768 * Allocate and attach a security structure to the @shp security field. The
3769 * security field is initialized to NULL when the structure is first created.
3771 * Return: Returns 0 if operation was successful and permission is granted.
3773 int security_shm_alloc(struct kern_ipc_perm *shp)
3775 int rc = lsm_ipc_alloc(shp);
3777 if (unlikely(rc))
3778 return rc;
3779 rc = call_int_hook(shm_alloc_security, shp);
3780 if (unlikely(rc))
3781 security_shm_free(shp);
3782 return rc;
3786 * security_shm_free() - Free a sysv shm LSM blob
3787 * @shp: sysv ipc permission structure
3789 * Deallocate the security structure @perm->security for the memory segment.
3791 void security_shm_free(struct kern_ipc_perm *shp)
3793 call_void_hook(shm_free_security, shp);
3794 kfree(shp->security);
3795 shp->security = NULL;
3799 * security_shm_associate() - Check if a sysv shm operation is allowed
3800 * @shp: sysv ipc permission structure
3801 * @shmflg: operation flags
3803 * Check permission when a shared memory region is requested through the shmget
3804 * system call. This hook is only called when returning the shared memory
3805 * region identifier for an existing region, not when a new shared memory
3806 * region is created.
3808 * Return: Returns 0 if permission is granted.
3810 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
3812 return call_int_hook(shm_associate, shp, shmflg);
3816 * security_shm_shmctl() - Check if a sysv shm operation is allowed
3817 * @shp: sysv ipc permission structure
3818 * @cmd: operation
3820 * Check permission when a shared memory control operation specified by @cmd is
3821 * to be performed on the shared memory region with permissions in @shp.
3823 * Return: Return 0 if permission is granted.
3825 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
3827 return call_int_hook(shm_shmctl, shp, cmd);
3831 * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3832 * @shp: sysv ipc permission structure
3833 * @shmaddr: address of memory region to attach
3834 * @shmflg: operation flags
3836 * Check permissions prior to allowing the shmat system call to attach the
3837 * shared memory segment with permissions @shp to the data segment of the
3838 * calling process. The attaching address is specified by @shmaddr.
3840 * Return: Returns 0 if permission is granted.
3842 int security_shm_shmat(struct kern_ipc_perm *shp,
3843 char __user *shmaddr, int shmflg)
3845 return call_int_hook(shm_shmat, shp, shmaddr, shmflg);
3849 * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3850 * @sma: sysv ipc permission structure
3852 * Allocate and attach a security structure to the @sma security field. The
3853 * security field is initialized to NULL when the structure is first created.
3855 * Return: Returns 0 if operation was successful and permission is granted.
3857 int security_sem_alloc(struct kern_ipc_perm *sma)
3859 int rc = lsm_ipc_alloc(sma);
3861 if (unlikely(rc))
3862 return rc;
3863 rc = call_int_hook(sem_alloc_security, sma);
3864 if (unlikely(rc))
3865 security_sem_free(sma);
3866 return rc;
3870 * security_sem_free() - Free a sysv semaphore LSM blob
3871 * @sma: sysv ipc permission structure
3873 * Deallocate security structure @sma->security for the semaphore.
3875 void security_sem_free(struct kern_ipc_perm *sma)
3877 call_void_hook(sem_free_security, sma);
3878 kfree(sma->security);
3879 sma->security = NULL;
3883 * security_sem_associate() - Check if a sysv semaphore operation is allowed
3884 * @sma: sysv ipc permission structure
3885 * @semflg: operation flags
3887 * Check permission when a semaphore is requested through the semget system
3888 * call. This hook is only called when returning the semaphore identifier for
3889 * an existing semaphore, not when a new one must be created.
3891 * Return: Returns 0 if permission is granted.
3893 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
3895 return call_int_hook(sem_associate, sma, semflg);
3899 * security_sem_semctl() - Check if a sysv semaphore operation is allowed
3900 * @sma: sysv ipc permission structure
3901 * @cmd: operation
3903 * Check permission when a semaphore operation specified by @cmd is to be
3904 * performed on the semaphore.
3906 * Return: Returns 0 if permission is granted.
3908 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
3910 return call_int_hook(sem_semctl, sma, cmd);
3914 * security_sem_semop() - Check if a sysv semaphore operation is allowed
3915 * @sma: sysv ipc permission structure
3916 * @sops: operations to perform
3917 * @nsops: number of operations
3918 * @alter: flag indicating changes will be made
3920 * Check permissions before performing operations on members of the semaphore
3921 * set. If the @alter flag is nonzero, the semaphore set may be modified.
3923 * Return: Returns 0 if permission is granted.
3925 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
3926 unsigned nsops, int alter)
3928 return call_int_hook(sem_semop, sma, sops, nsops, alter);
3932 * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3933 * @dentry: dentry
3934 * @inode: inode
3936 * Fill in @inode security information for a @dentry if allowed.
3938 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3940 if (unlikely(inode && IS_PRIVATE(inode)))
3941 return;
3942 call_void_hook(d_instantiate, dentry, inode);
3944 EXPORT_SYMBOL(security_d_instantiate);
3947 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
3951 * security_getselfattr - Read an LSM attribute of the current process.
3952 * @attr: which attribute to return
3953 * @uctx: the user-space destination for the information, or NULL
3954 * @size: pointer to the size of space available to receive the data
3955 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
3956 * attributes associated with the LSM identified in the passed @ctx be
3957 * reported.
3959 * A NULL value for @uctx can be used to get both the number of attributes
3960 * and the size of the data.
3962 * Returns the number of attributes found on success, negative value
3963 * on error. @size is reset to the total size of the data.
3964 * If @size is insufficient to contain the data -E2BIG is returned.
3966 int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
3967 u32 __user *size, u32 flags)
3969 struct security_hook_list *hp;
3970 struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, };
3971 u8 __user *base = (u8 __user *)uctx;
3972 u32 entrysize;
3973 u32 total = 0;
3974 u32 left;
3975 bool toobig = false;
3976 bool single = false;
3977 int count = 0;
3978 int rc;
3980 if (attr == LSM_ATTR_UNDEF)
3981 return -EINVAL;
3982 if (size == NULL)
3983 return -EINVAL;
3984 if (get_user(left, size))
3985 return -EFAULT;
3987 if (flags) {
3989 * Only flag supported is LSM_FLAG_SINGLE
3991 if (flags != LSM_FLAG_SINGLE || !uctx)
3992 return -EINVAL;
3993 if (copy_from_user(&lctx, uctx, sizeof(lctx)))
3994 return -EFAULT;
3996 * If the LSM ID isn't specified it is an error.
3998 if (lctx.id == LSM_ID_UNDEF)
3999 return -EINVAL;
4000 single = true;
4004 * In the usual case gather all the data from the LSMs.
4005 * In the single case only get the data from the LSM specified.
4007 hlist_for_each_entry(hp, &security_hook_heads.getselfattr, list) {
4008 if (single && lctx.id != hp->lsmid->id)
4009 continue;
4010 entrysize = left;
4011 if (base)
4012 uctx = (struct lsm_ctx __user *)(base + total);
4013 rc = hp->hook.getselfattr(attr, uctx, &entrysize, flags);
4014 if (rc == -EOPNOTSUPP) {
4015 rc = 0;
4016 continue;
4018 if (rc == -E2BIG) {
4019 rc = 0;
4020 left = 0;
4021 toobig = true;
4022 } else if (rc < 0)
4023 return rc;
4024 else
4025 left -= entrysize;
4027 total += entrysize;
4028 count += rc;
4029 if (single)
4030 break;
4032 if (put_user(total, size))
4033 return -EFAULT;
4034 if (toobig)
4035 return -E2BIG;
4036 if (count == 0)
4037 return LSM_RET_DEFAULT(getselfattr);
4038 return count;
4042 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
4046 * security_setselfattr - Set an LSM attribute on the current process.
4047 * @attr: which attribute to set
4048 * @uctx: the user-space source for the information
4049 * @size: the size of the data
4050 * @flags: reserved for future use, must be 0
4052 * Set an LSM attribute for the current process. The LSM, attribute
4053 * and new value are included in @uctx.
4055 * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT
4056 * if the user buffer is inaccessible, E2BIG if size is too big, or an
4057 * LSM specific failure.
4059 int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
4060 u32 size, u32 flags)
4062 struct security_hook_list *hp;
4063 struct lsm_ctx *lctx;
4064 int rc = LSM_RET_DEFAULT(setselfattr);
4065 u64 required_len;
4067 if (flags)
4068 return -EINVAL;
4069 if (size < sizeof(*lctx))
4070 return -EINVAL;
4071 if (size > PAGE_SIZE)
4072 return -E2BIG;
4074 lctx = memdup_user(uctx, size);
4075 if (IS_ERR(lctx))
4076 return PTR_ERR(lctx);
4078 if (size < lctx->len ||
4079 check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) ||
4080 lctx->len < required_len) {
4081 rc = -EINVAL;
4082 goto free_out;
4085 hlist_for_each_entry(hp, &security_hook_heads.setselfattr, list)
4086 if ((hp->lsmid->id) == lctx->id) {
4087 rc = hp->hook.setselfattr(attr, lctx, size, flags);
4088 break;
4091 free_out:
4092 kfree(lctx);
4093 return rc;
4097 * security_getprocattr() - Read an attribute for a task
4098 * @p: the task
4099 * @lsmid: LSM identification
4100 * @name: attribute name
4101 * @value: attribute value
4103 * Read attribute @name for task @p and store it into @value if allowed.
4105 * Return: Returns the length of @value on success, a negative value otherwise.
4107 int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
4108 char **value)
4110 struct security_hook_list *hp;
4112 hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
4113 if (lsmid != 0 && lsmid != hp->lsmid->id)
4114 continue;
4115 return hp->hook.getprocattr(p, name, value);
4117 return LSM_RET_DEFAULT(getprocattr);
4121 * security_setprocattr() - Set an attribute for a task
4122 * @lsmid: LSM identification
4123 * @name: attribute name
4124 * @value: attribute value
4125 * @size: attribute value size
4127 * Write (set) the current task's attribute @name to @value, size @size if
4128 * allowed.
4130 * Return: Returns bytes written on success, a negative value otherwise.
4132 int security_setprocattr(int lsmid, const char *name, void *value, size_t size)
4134 struct security_hook_list *hp;
4136 hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
4137 if (lsmid != 0 && lsmid != hp->lsmid->id)
4138 continue;
4139 return hp->hook.setprocattr(name, value, size);
4141 return LSM_RET_DEFAULT(setprocattr);
4145 * security_netlink_send() - Save info and check if netlink sending is allowed
4146 * @sk: sending socket
4147 * @skb: netlink message
4149 * Save security information for a netlink message so that permission checking
4150 * can be performed when the message is processed. The security information
4151 * can be saved using the eff_cap field of the netlink_skb_parms structure.
4152 * Also may be used to provide fine grained control over message transmission.
4154 * Return: Returns 0 if the information was successfully saved and message is
4155 * allowed to be transmitted.
4157 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
4159 return call_int_hook(netlink_send, sk, skb);
4163 * security_ismaclabel() - Check if the named attribute is a MAC label
4164 * @name: full extended attribute name
4166 * Check if the extended attribute specified by @name represents a MAC label.
4168 * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
4170 int security_ismaclabel(const char *name)
4172 return call_int_hook(ismaclabel, name);
4174 EXPORT_SYMBOL(security_ismaclabel);
4177 * security_secid_to_secctx() - Convert a secid to a secctx
4178 * @secid: secid
4179 * @secdata: secctx
4180 * @seclen: secctx length
4182 * Convert secid to security context. If @secdata is NULL the length of the
4183 * result will be returned in @seclen, but no @secdata will be returned. This
4184 * does mean that the length could change between calls to check the length and
4185 * the next call which actually allocates and returns the @secdata.
4187 * Return: Return 0 on success, error on failure.
4189 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4191 return call_int_hook(secid_to_secctx, secid, secdata, seclen);
4193 EXPORT_SYMBOL(security_secid_to_secctx);
4196 * security_secctx_to_secid() - Convert a secctx to a secid
4197 * @secdata: secctx
4198 * @seclen: length of secctx
4199 * @secid: secid
4201 * Convert security context to secid.
4203 * Return: Returns 0 on success, error on failure.
4205 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4207 *secid = 0;
4208 return call_int_hook(secctx_to_secid, secdata, seclen, secid);
4210 EXPORT_SYMBOL(security_secctx_to_secid);
4213 * security_release_secctx() - Free a secctx buffer
4214 * @secdata: secctx
4215 * @seclen: length of secctx
4217 * Release the security context.
4219 void security_release_secctx(char *secdata, u32 seclen)
4221 call_void_hook(release_secctx, secdata, seclen);
4223 EXPORT_SYMBOL(security_release_secctx);
4226 * security_inode_invalidate_secctx() - Invalidate an inode's security label
4227 * @inode: inode
4229 * Notify the security module that it must revalidate the security context of
4230 * an inode.
4232 void security_inode_invalidate_secctx(struct inode *inode)
4234 call_void_hook(inode_invalidate_secctx, inode);
4236 EXPORT_SYMBOL(security_inode_invalidate_secctx);
4239 * security_inode_notifysecctx() - Notify the LSM of an inode's security label
4240 * @inode: inode
4241 * @ctx: secctx
4242 * @ctxlen: length of secctx
4244 * Notify the security module of what the security context of an inode should
4245 * be. Initializes the incore security context managed by the security module
4246 * for this inode. Example usage: NFS client invokes this hook to initialize
4247 * the security context in its incore inode to the value provided by the server
4248 * for the file when the server returned the file's attributes to the client.
4249 * Must be called with inode->i_mutex locked.
4251 * Return: Returns 0 on success, error on failure.
4253 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4255 return call_int_hook(inode_notifysecctx, inode, ctx, ctxlen);
4257 EXPORT_SYMBOL(security_inode_notifysecctx);
4260 * security_inode_setsecctx() - Change the security label of an inode
4261 * @dentry: inode
4262 * @ctx: secctx
4263 * @ctxlen: length of secctx
4265 * Change the security context of an inode. Updates the incore security
4266 * context managed by the security module and invokes the fs code as needed
4267 * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
4268 * context. Example usage: NFS server invokes this hook to change the security
4269 * context in its incore inode and on the backing filesystem to a value
4270 * provided by the client on a SETATTR operation. Must be called with
4271 * inode->i_mutex locked.
4273 * Return: Returns 0 on success, error on failure.
4275 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4277 return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen);
4279 EXPORT_SYMBOL(security_inode_setsecctx);
4282 * security_inode_getsecctx() - Get the security label of an inode
4283 * @inode: inode
4284 * @ctx: secctx
4285 * @ctxlen: length of secctx
4287 * On success, returns 0 and fills out @ctx and @ctxlen with the security
4288 * context for the given @inode.
4290 * Return: Returns 0 on success, error on failure.
4292 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4294 return call_int_hook(inode_getsecctx, inode, ctx, ctxlen);
4296 EXPORT_SYMBOL(security_inode_getsecctx);
4298 #ifdef CONFIG_WATCH_QUEUE
4300 * security_post_notification() - Check if a watch notification can be posted
4301 * @w_cred: credentials of the task that set the watch
4302 * @cred: credentials of the task which triggered the watch
4303 * @n: the notification
4305 * Check to see if a watch notification can be posted to a particular queue.
4307 * Return: Returns 0 if permission is granted.
4309 int security_post_notification(const struct cred *w_cred,
4310 const struct cred *cred,
4311 struct watch_notification *n)
4313 return call_int_hook(post_notification, w_cred, cred, n);
4315 #endif /* CONFIG_WATCH_QUEUE */
4317 #ifdef CONFIG_KEY_NOTIFICATIONS
4319 * security_watch_key() - Check if a task is allowed to watch for key events
4320 * @key: the key to watch
4322 * Check to see if a process is allowed to watch for event notifications from
4323 * a key or keyring.
4325 * Return: Returns 0 if permission is granted.
4327 int security_watch_key(struct key *key)
4329 return call_int_hook(watch_key, key);
4331 #endif /* CONFIG_KEY_NOTIFICATIONS */
4333 #ifdef CONFIG_SECURITY_NETWORK
4335 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
4336 * @sock: originating sock
4337 * @other: peer sock
4338 * @newsk: new sock
4340 * Check permissions before establishing a Unix domain stream connection
4341 * between @sock and @other.
4343 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4344 * Linux provides an alternative to the conventional file name space for Unix
4345 * domain sockets. Whereas binding and connecting to sockets in the file name
4346 * space is mediated by the typical file permissions (and caught by the mknod
4347 * and permission hooks in inode_security_ops), binding and connecting to
4348 * sockets in the abstract name space is completely unmediated. Sufficient
4349 * control of Unix domain sockets in the abstract name space isn't possible
4350 * using only the socket layer hooks, since we need to know the actual target
4351 * socket, which is not looked up until we are inside the af_unix code.
4353 * Return: Returns 0 if permission is granted.
4355 int security_unix_stream_connect(struct sock *sock, struct sock *other,
4356 struct sock *newsk)
4358 return call_int_hook(unix_stream_connect, sock, other, newsk);
4360 EXPORT_SYMBOL(security_unix_stream_connect);
4363 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
4364 * @sock: originating sock
4365 * @other: peer sock
4367 * Check permissions before connecting or sending datagrams from @sock to
4368 * @other.
4370 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4371 * Linux provides an alternative to the conventional file name space for Unix
4372 * domain sockets. Whereas binding and connecting to sockets in the file name
4373 * space is mediated by the typical file permissions (and caught by the mknod
4374 * and permission hooks in inode_security_ops), binding and connecting to
4375 * sockets in the abstract name space is completely unmediated. Sufficient
4376 * control of Unix domain sockets in the abstract name space isn't possible
4377 * using only the socket layer hooks, since we need to know the actual target
4378 * socket, which is not looked up until we are inside the af_unix code.
4380 * Return: Returns 0 if permission is granted.
4382 int security_unix_may_send(struct socket *sock, struct socket *other)
4384 return call_int_hook(unix_may_send, sock, other);
4386 EXPORT_SYMBOL(security_unix_may_send);
4389 * security_socket_create() - Check if creating a new socket is allowed
4390 * @family: protocol family
4391 * @type: communications type
4392 * @protocol: requested protocol
4393 * @kern: set to 1 if a kernel socket is requested
4395 * Check permissions prior to creating a new socket.
4397 * Return: Returns 0 if permission is granted.
4399 int security_socket_create(int family, int type, int protocol, int kern)
4401 return call_int_hook(socket_create, family, type, protocol, kern);
4405 * security_socket_post_create() - Initialize a newly created socket
4406 * @sock: socket
4407 * @family: protocol family
4408 * @type: communications type
4409 * @protocol: requested protocol
4410 * @kern: set to 1 if a kernel socket is requested
4412 * This hook allows a module to update or allocate a per-socket security
4413 * structure. Note that the security field was not added directly to the socket
4414 * structure, but rather, the socket security information is stored in the
4415 * associated inode. Typically, the inode alloc_security hook will allocate
4416 * and attach security information to SOCK_INODE(sock)->i_security. This hook
4417 * may be used to update the SOCK_INODE(sock)->i_security field with additional
4418 * information that wasn't available when the inode was allocated.
4420 * Return: Returns 0 if permission is granted.
4422 int security_socket_post_create(struct socket *sock, int family,
4423 int type, int protocol, int kern)
4425 return call_int_hook(socket_post_create, sock, family, type,
4426 protocol, kern);
4430 * security_socket_socketpair() - Check if creating a socketpair is allowed
4431 * @socka: first socket
4432 * @sockb: second socket
4434 * Check permissions before creating a fresh pair of sockets.
4436 * Return: Returns 0 if permission is granted and the connection was
4437 * established.
4439 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4441 return call_int_hook(socket_socketpair, socka, sockb);
4443 EXPORT_SYMBOL(security_socket_socketpair);
4446 * security_socket_bind() - Check if a socket bind operation is allowed
4447 * @sock: socket
4448 * @address: requested bind address
4449 * @addrlen: length of address
4451 * Check permission before socket protocol layer bind operation is performed
4452 * and the socket @sock is bound to the address specified in the @address
4453 * parameter.
4455 * Return: Returns 0 if permission is granted.
4457 int security_socket_bind(struct socket *sock,
4458 struct sockaddr *address, int addrlen)
4460 return call_int_hook(socket_bind, sock, address, addrlen);
4464 * security_socket_connect() - Check if a socket connect operation is allowed
4465 * @sock: socket
4466 * @address: address of remote connection point
4467 * @addrlen: length of address
4469 * Check permission before socket protocol layer connect operation attempts to
4470 * connect socket @sock to a remote address, @address.
4472 * Return: Returns 0 if permission is granted.
4474 int security_socket_connect(struct socket *sock,
4475 struct sockaddr *address, int addrlen)
4477 return call_int_hook(socket_connect, sock, address, addrlen);
4481 * security_socket_listen() - Check if a socket is allowed to listen
4482 * @sock: socket
4483 * @backlog: connection queue size
4485 * Check permission before socket protocol layer listen operation.
4487 * Return: Returns 0 if permission is granted.
4489 int security_socket_listen(struct socket *sock, int backlog)
4491 return call_int_hook(socket_listen, sock, backlog);
4495 * security_socket_accept() - Check if a socket is allowed to accept connections
4496 * @sock: listening socket
4497 * @newsock: newly creation connection socket
4499 * Check permission before accepting a new connection. Note that the new
4500 * socket, @newsock, has been created and some information copied to it, but
4501 * the accept operation has not actually been performed.
4503 * Return: Returns 0 if permission is granted.
4505 int security_socket_accept(struct socket *sock, struct socket *newsock)
4507 return call_int_hook(socket_accept, sock, newsock);
4511 * security_socket_sendmsg() - Check if sending a message is allowed
4512 * @sock: sending socket
4513 * @msg: message to send
4514 * @size: size of message
4516 * Check permission before transmitting a message to another socket.
4518 * Return: Returns 0 if permission is granted.
4520 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
4522 return call_int_hook(socket_sendmsg, sock, msg, size);
4526 * security_socket_recvmsg() - Check if receiving a message is allowed
4527 * @sock: receiving socket
4528 * @msg: message to receive
4529 * @size: size of message
4530 * @flags: operational flags
4532 * Check permission before receiving a message from a socket.
4534 * Return: Returns 0 if permission is granted.
4536 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4537 int size, int flags)
4539 return call_int_hook(socket_recvmsg, sock, msg, size, flags);
4543 * security_socket_getsockname() - Check if reading the socket addr is allowed
4544 * @sock: socket
4546 * Check permission before reading the local address (name) of the socket
4547 * object.
4549 * Return: Returns 0 if permission is granted.
4551 int security_socket_getsockname(struct socket *sock)
4553 return call_int_hook(socket_getsockname, sock);
4557 * security_socket_getpeername() - Check if reading the peer's addr is allowed
4558 * @sock: socket
4560 * Check permission before the remote address (name) of a socket object.
4562 * Return: Returns 0 if permission is granted.
4564 int security_socket_getpeername(struct socket *sock)
4566 return call_int_hook(socket_getpeername, sock);
4570 * security_socket_getsockopt() - Check if reading a socket option is allowed
4571 * @sock: socket
4572 * @level: option's protocol level
4573 * @optname: option name
4575 * Check permissions before retrieving the options associated with socket
4576 * @sock.
4578 * Return: Returns 0 if permission is granted.
4580 int security_socket_getsockopt(struct socket *sock, int level, int optname)
4582 return call_int_hook(socket_getsockopt, sock, level, optname);
4586 * security_socket_setsockopt() - Check if setting a socket option is allowed
4587 * @sock: socket
4588 * @level: option's protocol level
4589 * @optname: option name
4591 * Check permissions before setting the options associated with socket @sock.
4593 * Return: Returns 0 if permission is granted.
4595 int security_socket_setsockopt(struct socket *sock, int level, int optname)
4597 return call_int_hook(socket_setsockopt, sock, level, optname);
4601 * security_socket_shutdown() - Checks if shutting down the socket is allowed
4602 * @sock: socket
4603 * @how: flag indicating how sends and receives are handled
4605 * Checks permission before all or part of a connection on the socket @sock is
4606 * shut down.
4608 * Return: Returns 0 if permission is granted.
4610 int security_socket_shutdown(struct socket *sock, int how)
4612 return call_int_hook(socket_shutdown, sock, how);
4616 * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4617 * @sk: destination sock
4618 * @skb: incoming packet
4620 * Check permissions on incoming network packets. This hook is distinct from
4621 * Netfilter's IP input hooks since it is the first time that the incoming
4622 * sk_buff @skb has been associated with a particular socket, @sk. Must not
4623 * sleep inside this hook because some callers hold spinlocks.
4625 * Return: Returns 0 if permission is granted.
4627 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4629 return call_int_hook(socket_sock_rcv_skb, sk, skb);
4631 EXPORT_SYMBOL(security_sock_rcv_skb);
4634 * security_socket_getpeersec_stream() - Get the remote peer label
4635 * @sock: socket
4636 * @optval: destination buffer
4637 * @optlen: size of peer label copied into the buffer
4638 * @len: maximum size of the destination buffer
4640 * This hook allows the security module to provide peer socket security state
4641 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4642 * For tcp sockets this can be meaningful if the socket is associated with an
4643 * ipsec SA.
4645 * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4646 * values.
4648 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4649 sockptr_t optlen, unsigned int len)
4651 return call_int_hook(socket_getpeersec_stream, sock, optval, optlen,
4652 len);
4656 * security_socket_getpeersec_dgram() - Get the remote peer label
4657 * @sock: socket
4658 * @skb: datagram packet
4659 * @secid: remote peer label secid
4661 * This hook allows the security module to provide peer socket security state
4662 * for udp sockets on a per-packet basis to userspace via getsockopt
4663 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4664 * option via getsockopt. It can then retrieve the security state returned by
4665 * this hook for a packet via the SCM_SECURITY ancillary message type.
4667 * Return: Returns 0 on success, error on failure.
4669 int security_socket_getpeersec_dgram(struct socket *sock,
4670 struct sk_buff *skb, u32 *secid)
4672 return call_int_hook(socket_getpeersec_dgram, sock, skb, secid);
4674 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4677 * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4678 * @sk: sock
4679 * @family: protocol family
4680 * @priority: gfp flags
4682 * Allocate and attach a security structure to the sk->sk_security field, which
4683 * is used to copy security attributes between local stream sockets.
4685 * Return: Returns 0 on success, error on failure.
4687 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4689 return call_int_hook(sk_alloc_security, sk, family, priority);
4693 * security_sk_free() - Free the sock's LSM blob
4694 * @sk: sock
4696 * Deallocate security structure.
4698 void security_sk_free(struct sock *sk)
4700 call_void_hook(sk_free_security, sk);
4704 * security_sk_clone() - Clone a sock's LSM state
4705 * @sk: original sock
4706 * @newsk: target sock
4708 * Clone/copy security structure.
4710 void security_sk_clone(const struct sock *sk, struct sock *newsk)
4712 call_void_hook(sk_clone_security, sk, newsk);
4714 EXPORT_SYMBOL(security_sk_clone);
4717 * security_sk_classify_flow() - Set a flow's secid based on socket
4718 * @sk: original socket
4719 * @flic: target flow
4721 * Set the target flow's secid to socket's secid.
4723 void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
4725 call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
4727 EXPORT_SYMBOL(security_sk_classify_flow);
4730 * security_req_classify_flow() - Set a flow's secid based on request_sock
4731 * @req: request_sock
4732 * @flic: target flow
4734 * Sets @flic's secid to @req's secid.
4736 void security_req_classify_flow(const struct request_sock *req,
4737 struct flowi_common *flic)
4739 call_void_hook(req_classify_flow, req, flic);
4741 EXPORT_SYMBOL(security_req_classify_flow);
4744 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4745 * @sk: sock being grafted
4746 * @parent: target parent socket
4748 * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
4749 * LSM state from @parent.
4751 void security_sock_graft(struct sock *sk, struct socket *parent)
4753 call_void_hook(sock_graft, sk, parent);
4755 EXPORT_SYMBOL(security_sock_graft);
4758 * security_inet_conn_request() - Set request_sock state using incoming connect
4759 * @sk: parent listening sock
4760 * @skb: incoming connection
4761 * @req: new request_sock
4763 * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4765 * Return: Returns 0 if permission is granted.
4767 int security_inet_conn_request(const struct sock *sk,
4768 struct sk_buff *skb, struct request_sock *req)
4770 return call_int_hook(inet_conn_request, sk, skb, req);
4772 EXPORT_SYMBOL(security_inet_conn_request);
4775 * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4776 * @newsk: new sock
4777 * @req: connection request_sock
4779 * Set that LSM state of @sock using the LSM state from @req.
4781 void security_inet_csk_clone(struct sock *newsk,
4782 const struct request_sock *req)
4784 call_void_hook(inet_csk_clone, newsk, req);
4788 * security_inet_conn_established() - Update sock's LSM state with connection
4789 * @sk: sock
4790 * @skb: connection packet
4792 * Update @sock's LSM state to represent a new connection from @skb.
4794 void security_inet_conn_established(struct sock *sk,
4795 struct sk_buff *skb)
4797 call_void_hook(inet_conn_established, sk, skb);
4799 EXPORT_SYMBOL(security_inet_conn_established);
4802 * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4803 * @secid: new secmark value
4805 * Check if the process should be allowed to relabel packets to @secid.
4807 * Return: Returns 0 if permission is granted.
4809 int security_secmark_relabel_packet(u32 secid)
4811 return call_int_hook(secmark_relabel_packet, secid);
4813 EXPORT_SYMBOL(security_secmark_relabel_packet);
4816 * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4818 * Tells the LSM to increment the number of secmark labeling rules loaded.
4820 void security_secmark_refcount_inc(void)
4822 call_void_hook(secmark_refcount_inc);
4824 EXPORT_SYMBOL(security_secmark_refcount_inc);
4827 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4829 * Tells the LSM to decrement the number of secmark labeling rules loaded.
4831 void security_secmark_refcount_dec(void)
4833 call_void_hook(secmark_refcount_dec);
4835 EXPORT_SYMBOL(security_secmark_refcount_dec);
4838 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4839 * @security: pointer to the LSM blob
4841 * This hook allows a module to allocate a security structure for a TUN device,
4842 * returning the pointer in @security.
4844 * Return: Returns a zero on success, negative values on failure.
4846 int security_tun_dev_alloc_security(void **security)
4848 return call_int_hook(tun_dev_alloc_security, security);
4850 EXPORT_SYMBOL(security_tun_dev_alloc_security);
4853 * security_tun_dev_free_security() - Free a TUN device LSM blob
4854 * @security: LSM blob
4856 * This hook allows a module to free the security structure for a TUN device.
4858 void security_tun_dev_free_security(void *security)
4860 call_void_hook(tun_dev_free_security, security);
4862 EXPORT_SYMBOL(security_tun_dev_free_security);
4865 * security_tun_dev_create() - Check if creating a TUN device is allowed
4867 * Check permissions prior to creating a new TUN device.
4869 * Return: Returns 0 if permission is granted.
4871 int security_tun_dev_create(void)
4873 return call_int_hook(tun_dev_create);
4875 EXPORT_SYMBOL(security_tun_dev_create);
4878 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4879 * @security: TUN device LSM blob
4881 * Check permissions prior to attaching to a TUN device queue.
4883 * Return: Returns 0 if permission is granted.
4885 int security_tun_dev_attach_queue(void *security)
4887 return call_int_hook(tun_dev_attach_queue, security);
4889 EXPORT_SYMBOL(security_tun_dev_attach_queue);
4892 * security_tun_dev_attach() - Update TUN device LSM state on attach
4893 * @sk: associated sock
4894 * @security: TUN device LSM blob
4896 * This hook can be used by the module to update any security state associated
4897 * with the TUN device's sock structure.
4899 * Return: Returns 0 if permission is granted.
4901 int security_tun_dev_attach(struct sock *sk, void *security)
4903 return call_int_hook(tun_dev_attach, sk, security);
4905 EXPORT_SYMBOL(security_tun_dev_attach);
4908 * security_tun_dev_open() - Update TUN device LSM state on open
4909 * @security: TUN device LSM blob
4911 * This hook can be used by the module to update any security state associated
4912 * with the TUN device's security structure.
4914 * Return: Returns 0 if permission is granted.
4916 int security_tun_dev_open(void *security)
4918 return call_int_hook(tun_dev_open, security);
4920 EXPORT_SYMBOL(security_tun_dev_open);
4923 * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4924 * @asoc: SCTP association
4925 * @skb: packet requesting the association
4927 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4929 * Return: Returns 0 on success, error on failure.
4931 int security_sctp_assoc_request(struct sctp_association *asoc,
4932 struct sk_buff *skb)
4934 return call_int_hook(sctp_assoc_request, asoc, skb);
4936 EXPORT_SYMBOL(security_sctp_assoc_request);
4939 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4940 * @sk: socket
4941 * @optname: SCTP option to validate
4942 * @address: list of IP addresses to validate
4943 * @addrlen: length of the address list
4945 * Validiate permissions required for each address associated with sock @sk.
4946 * Depending on @optname, the addresses will be treated as either a connect or
4947 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4948 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4950 * Return: Returns 0 on success, error on failure.
4952 int security_sctp_bind_connect(struct sock *sk, int optname,
4953 struct sockaddr *address, int addrlen)
4955 return call_int_hook(sctp_bind_connect, sk, optname, address, addrlen);
4957 EXPORT_SYMBOL(security_sctp_bind_connect);
4960 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4961 * @asoc: SCTP association
4962 * @sk: original sock
4963 * @newsk: target sock
4965 * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4966 * socket) or when a socket is 'peeled off' e.g userspace calls
4967 * sctp_peeloff(3).
4969 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
4970 struct sock *newsk)
4972 call_void_hook(sctp_sk_clone, asoc, sk, newsk);
4974 EXPORT_SYMBOL(security_sctp_sk_clone);
4977 * security_sctp_assoc_established() - Update LSM state when assoc established
4978 * @asoc: SCTP association
4979 * @skb: packet establishing the association
4981 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4982 * security module.
4984 * Return: Returns 0 if permission is granted.
4986 int security_sctp_assoc_established(struct sctp_association *asoc,
4987 struct sk_buff *skb)
4989 return call_int_hook(sctp_assoc_established, asoc, skb);
4991 EXPORT_SYMBOL(security_sctp_assoc_established);
4994 * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
4995 * @sk: the owning MPTCP socket
4996 * @ssk: the new subflow
4998 * Update the labeling for the given MPTCP subflow, to match the one of the
4999 * owning MPTCP socket. This hook has to be called after the socket creation and
5000 * initialization via the security_socket_create() and
5001 * security_socket_post_create() LSM hooks.
5003 * Return: Returns 0 on success or a negative error code on failure.
5005 int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
5007 return call_int_hook(mptcp_add_subflow, sk, ssk);
5010 #endif /* CONFIG_SECURITY_NETWORK */
5012 #ifdef CONFIG_SECURITY_INFINIBAND
5014 * security_ib_pkey_access() - Check if access to an IB pkey is allowed
5015 * @sec: LSM blob
5016 * @subnet_prefix: subnet prefix of the port
5017 * @pkey: IB pkey
5019 * Check permission to access a pkey when modifying a QP.
5021 * Return: Returns 0 if permission is granted.
5023 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
5025 return call_int_hook(ib_pkey_access, sec, subnet_prefix, pkey);
5027 EXPORT_SYMBOL(security_ib_pkey_access);
5030 * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
5031 * @sec: LSM blob
5032 * @dev_name: IB device name
5033 * @port_num: port number
5035 * Check permissions to send and receive SMPs on a end port.
5037 * Return: Returns 0 if permission is granted.
5039 int security_ib_endport_manage_subnet(void *sec,
5040 const char *dev_name, u8 port_num)
5042 return call_int_hook(ib_endport_manage_subnet, sec, dev_name, port_num);
5044 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
5047 * security_ib_alloc_security() - Allocate an Infiniband LSM blob
5048 * @sec: LSM blob
5050 * Allocate a security structure for Infiniband objects.
5052 * Return: Returns 0 on success, non-zero on failure.
5054 int security_ib_alloc_security(void **sec)
5056 return call_int_hook(ib_alloc_security, sec);
5058 EXPORT_SYMBOL(security_ib_alloc_security);
5061 * security_ib_free_security() - Free an Infiniband LSM blob
5062 * @sec: LSM blob
5064 * Deallocate an Infiniband security structure.
5066 void security_ib_free_security(void *sec)
5068 call_void_hook(ib_free_security, sec);
5070 EXPORT_SYMBOL(security_ib_free_security);
5071 #endif /* CONFIG_SECURITY_INFINIBAND */
5073 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5075 * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
5076 * @ctxp: xfrm security context being added to the SPD
5077 * @sec_ctx: security label provided by userspace
5078 * @gfp: gfp flags
5080 * Allocate a security structure to the xp->security field; the security field
5081 * is initialized to NULL when the xfrm_policy is allocated.
5083 * Return: Return 0 if operation was successful.
5085 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
5086 struct xfrm_user_sec_ctx *sec_ctx,
5087 gfp_t gfp)
5089 return call_int_hook(xfrm_policy_alloc_security, ctxp, sec_ctx, gfp);
5091 EXPORT_SYMBOL(security_xfrm_policy_alloc);
5094 * security_xfrm_policy_clone() - Clone xfrm policy LSM state
5095 * @old_ctx: xfrm security context
5096 * @new_ctxp: target xfrm security context
5098 * Allocate a security structure in new_ctxp that contains the information from
5099 * the old_ctx structure.
5101 * Return: Return 0 if operation was successful.
5103 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
5104 struct xfrm_sec_ctx **new_ctxp)
5106 return call_int_hook(xfrm_policy_clone_security, old_ctx, new_ctxp);
5110 * security_xfrm_policy_free() - Free a xfrm security context
5111 * @ctx: xfrm security context
5113 * Free LSM resources associated with @ctx.
5115 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
5117 call_void_hook(xfrm_policy_free_security, ctx);
5119 EXPORT_SYMBOL(security_xfrm_policy_free);
5122 * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
5123 * @ctx: xfrm security context
5125 * Authorize deletion of a SPD entry.
5127 * Return: Returns 0 if permission is granted.
5129 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
5131 return call_int_hook(xfrm_policy_delete_security, ctx);
5135 * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
5136 * @x: xfrm state being added to the SAD
5137 * @sec_ctx: security label provided by userspace
5139 * Allocate a security structure to the @x->security field; the security field
5140 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5141 * correspond to @sec_ctx.
5143 * Return: Return 0 if operation was successful.
5145 int security_xfrm_state_alloc(struct xfrm_state *x,
5146 struct xfrm_user_sec_ctx *sec_ctx)
5148 return call_int_hook(xfrm_state_alloc, x, sec_ctx);
5150 EXPORT_SYMBOL(security_xfrm_state_alloc);
5153 * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
5154 * @x: xfrm state being added to the SAD
5155 * @polsec: associated policy's security context
5156 * @secid: secid from the flow
5158 * Allocate a security structure to the x->security field; the security field
5159 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5160 * correspond to secid.
5162 * Return: Returns 0 if operation was successful.
5164 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
5165 struct xfrm_sec_ctx *polsec, u32 secid)
5167 return call_int_hook(xfrm_state_alloc_acquire, x, polsec, secid);
5171 * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
5172 * @x: xfrm state
5174 * Authorize deletion of x->security.
5176 * Return: Returns 0 if permission is granted.
5178 int security_xfrm_state_delete(struct xfrm_state *x)
5180 return call_int_hook(xfrm_state_delete_security, x);
5182 EXPORT_SYMBOL(security_xfrm_state_delete);
5185 * security_xfrm_state_free() - Free a xfrm state
5186 * @x: xfrm state
5188 * Deallocate x->security.
5190 void security_xfrm_state_free(struct xfrm_state *x)
5192 call_void_hook(xfrm_state_free_security, x);
5196 * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
5197 * @ctx: target xfrm security context
5198 * @fl_secid: flow secid used to authorize access
5200 * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
5201 * packet. The hook is called when selecting either a per-socket policy or a
5202 * generic xfrm policy.
5204 * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
5205 * other errors.
5207 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
5209 return call_int_hook(xfrm_policy_lookup, ctx, fl_secid);
5213 * security_xfrm_state_pol_flow_match() - Check for a xfrm match
5214 * @x: xfrm state to match
5215 * @xp: xfrm policy to check for a match
5216 * @flic: flow to check for a match.
5218 * Check @xp and @flic for a match with @x.
5220 * Return: Returns 1 if there is a match.
5222 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
5223 struct xfrm_policy *xp,
5224 const struct flowi_common *flic)
5226 struct security_hook_list *hp;
5227 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
5230 * Since this function is expected to return 0 or 1, the judgment
5231 * becomes difficult if multiple LSMs supply this call. Fortunately,
5232 * we can use the first LSM's judgment because currently only SELinux
5233 * supplies this call.
5235 * For speed optimization, we explicitly break the loop rather than
5236 * using the macro
5238 hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
5239 list) {
5240 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
5241 break;
5243 return rc;
5247 * security_xfrm_decode_session() - Determine the xfrm secid for a packet
5248 * @skb: xfrm packet
5249 * @secid: secid
5251 * Decode the packet in @skb and return the security label in @secid.
5253 * Return: Return 0 if all xfrms used have the same secid.
5255 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
5257 return call_int_hook(xfrm_decode_session, skb, secid, 1);
5260 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
5262 int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
5265 BUG_ON(rc);
5267 EXPORT_SYMBOL(security_skb_classify_flow);
5268 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
5270 #ifdef CONFIG_KEYS
5272 * security_key_alloc() - Allocate and initialize a kernel key LSM blob
5273 * @key: key
5274 * @cred: credentials
5275 * @flags: allocation flags
5277 * Permit allocation of a key and assign security data. Note that key does not
5278 * have a serial number assigned at this point.
5280 * Return: Return 0 if permission is granted, -ve error otherwise.
5282 int security_key_alloc(struct key *key, const struct cred *cred,
5283 unsigned long flags)
5285 return call_int_hook(key_alloc, key, cred, flags);
5289 * security_key_free() - Free a kernel key LSM blob
5290 * @key: key
5292 * Notification of destruction; free security data.
5294 void security_key_free(struct key *key)
5296 call_void_hook(key_free, key);
5300 * security_key_permission() - Check if a kernel key operation is allowed
5301 * @key_ref: key reference
5302 * @cred: credentials of actor requesting access
5303 * @need_perm: requested permissions
5305 * See whether a specific operational right is granted to a process on a key.
5307 * Return: Return 0 if permission is granted, -ve error otherwise.
5309 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
5310 enum key_need_perm need_perm)
5312 return call_int_hook(key_permission, key_ref, cred, need_perm);
5316 * security_key_getsecurity() - Get the key's security label
5317 * @key: key
5318 * @buffer: security label buffer
5320 * Get a textual representation of the security context attached to a key for
5321 * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the
5322 * storage for the NUL-terminated string and the caller should free it.
5324 * Return: Returns the length of @buffer (including terminating NUL) or -ve if
5325 * an error occurs. May also return 0 (and a NULL buffer pointer) if
5326 * there is no security label assigned to the key.
5328 int security_key_getsecurity(struct key *key, char **buffer)
5330 *buffer = NULL;
5331 return call_int_hook(key_getsecurity, key, buffer);
5335 * security_key_post_create_or_update() - Notification of key create or update
5336 * @keyring: keyring to which the key is linked to
5337 * @key: created or updated key
5338 * @payload: data used to instantiate or update the key
5339 * @payload_len: length of payload
5340 * @flags: key flags
5341 * @create: flag indicating whether the key was created or updated
5343 * Notify the caller of a key creation or update.
5345 void security_key_post_create_or_update(struct key *keyring, struct key *key,
5346 const void *payload, size_t payload_len,
5347 unsigned long flags, bool create)
5349 call_void_hook(key_post_create_or_update, keyring, key, payload,
5350 payload_len, flags, create);
5352 #endif /* CONFIG_KEYS */
5354 #ifdef CONFIG_AUDIT
5356 * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5357 * @field: audit action
5358 * @op: rule operator
5359 * @rulestr: rule context
5360 * @lsmrule: receive buffer for audit rule struct
5361 * @gfp: GFP flag used for kmalloc
5363 * Allocate and initialize an LSM audit rule structure.
5365 * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5366 * an invalid rule.
5368 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
5369 gfp_t gfp)
5371 return call_int_hook(audit_rule_init, field, op, rulestr, lsmrule, gfp);
5375 * security_audit_rule_known() - Check if an audit rule contains LSM fields
5376 * @krule: audit rule
5378 * Specifies whether given @krule contains any fields related to the current
5379 * LSM.
5381 * Return: Returns 1 in case of relation found, 0 otherwise.
5383 int security_audit_rule_known(struct audit_krule *krule)
5385 return call_int_hook(audit_rule_known, krule);
5389 * security_audit_rule_free() - Free an LSM audit rule struct
5390 * @lsmrule: audit rule struct
5392 * Deallocate the LSM audit rule structure previously allocated by
5393 * audit_rule_init().
5395 void security_audit_rule_free(void *lsmrule)
5397 call_void_hook(audit_rule_free, lsmrule);
5401 * security_audit_rule_match() - Check if a label matches an audit rule
5402 * @secid: security label
5403 * @field: LSM audit field
5404 * @op: matching operator
5405 * @lsmrule: audit rule
5407 * Determine if given @secid matches a rule previously approved by
5408 * security_audit_rule_known().
5410 * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5411 * failure.
5413 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
5415 return call_int_hook(audit_rule_match, secid, field, op, lsmrule);
5417 #endif /* CONFIG_AUDIT */
5419 #ifdef CONFIG_BPF_SYSCALL
5421 * security_bpf() - Check if the bpf syscall operation is allowed
5422 * @cmd: command
5423 * @attr: bpf attribute
5424 * @size: size
5426 * Do a initial check for all bpf syscalls after the attribute is copied into
5427 * the kernel. The actual security module can implement their own rules to
5428 * check the specific cmd they need.
5430 * Return: Returns 0 if permission is granted.
5432 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5434 return call_int_hook(bpf, cmd, attr, size);
5438 * security_bpf_map() - Check if access to a bpf map is allowed
5439 * @map: bpf map
5440 * @fmode: mode
5442 * Do a check when the kernel generates and returns a file descriptor for eBPF
5443 * maps.
5445 * Return: Returns 0 if permission is granted.
5447 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5449 return call_int_hook(bpf_map, map, fmode);
5453 * security_bpf_prog() - Check if access to a bpf program is allowed
5454 * @prog: bpf program
5456 * Do a check when the kernel generates and returns a file descriptor for eBPF
5457 * programs.
5459 * Return: Returns 0 if permission is granted.
5461 int security_bpf_prog(struct bpf_prog *prog)
5463 return call_int_hook(bpf_prog, prog);
5467 * security_bpf_map_create() - Check if BPF map creation is allowed
5468 * @map: BPF map object
5469 * @attr: BPF syscall attributes used to create BPF map
5470 * @token: BPF token used to grant user access
5472 * Do a check when the kernel creates a new BPF map. This is also the
5473 * point where LSM blob is allocated for LSMs that need them.
5475 * Return: Returns 0 on success, error on failure.
5477 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
5478 struct bpf_token *token)
5480 return call_int_hook(bpf_map_create, map, attr, token);
5484 * security_bpf_prog_load() - Check if loading of BPF program is allowed
5485 * @prog: BPF program object
5486 * @attr: BPF syscall attributes used to create BPF program
5487 * @token: BPF token used to grant user access to BPF subsystem
5489 * Perform an access control check when the kernel loads a BPF program and
5490 * allocates associated BPF program object. This hook is also responsible for
5491 * allocating any required LSM state for the BPF program.
5493 * Return: Returns 0 on success, error on failure.
5495 int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5496 struct bpf_token *token)
5498 return call_int_hook(bpf_prog_load, prog, attr, token);
5502 * security_bpf_token_create() - Check if creating of BPF token is allowed
5503 * @token: BPF token object
5504 * @attr: BPF syscall attributes used to create BPF token
5505 * @path: path pointing to BPF FS mount point from which BPF token is created
5507 * Do a check when the kernel instantiates a new BPF token object from BPF FS
5508 * instance. This is also the point where LSM blob can be allocated for LSMs.
5510 * Return: Returns 0 on success, error on failure.
5512 int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
5513 struct path *path)
5515 return call_int_hook(bpf_token_create, token, attr, path);
5519 * security_bpf_token_cmd() - Check if BPF token is allowed to delegate
5520 * requested BPF syscall command
5521 * @token: BPF token object
5522 * @cmd: BPF syscall command requested to be delegated by BPF token
5524 * Do a check when the kernel decides whether provided BPF token should allow
5525 * delegation of requested BPF syscall command.
5527 * Return: Returns 0 on success, error on failure.
5529 int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
5531 return call_int_hook(bpf_token_cmd, token, cmd);
5535 * security_bpf_token_capable() - Check if BPF token is allowed to delegate
5536 * requested BPF-related capability
5537 * @token: BPF token object
5538 * @cap: capabilities requested to be delegated by BPF token
5540 * Do a check when the kernel decides whether provided BPF token should allow
5541 * delegation of requested BPF-related capabilities.
5543 * Return: Returns 0 on success, error on failure.
5545 int security_bpf_token_capable(const struct bpf_token *token, int cap)
5547 return call_int_hook(bpf_token_capable, token, cap);
5551 * security_bpf_map_free() - Free a bpf map's LSM blob
5552 * @map: bpf map
5554 * Clean up the security information stored inside bpf map.
5556 void security_bpf_map_free(struct bpf_map *map)
5558 call_void_hook(bpf_map_free, map);
5562 * security_bpf_prog_free() - Free a BPF program's LSM blob
5563 * @prog: BPF program struct
5565 * Clean up the security information stored inside BPF program.
5567 void security_bpf_prog_free(struct bpf_prog *prog)
5569 call_void_hook(bpf_prog_free, prog);
5573 * security_bpf_token_free() - Free a BPF token's LSM blob
5574 * @token: BPF token struct
5576 * Clean up the security information stored inside BPF token.
5578 void security_bpf_token_free(struct bpf_token *token)
5580 call_void_hook(bpf_token_free, token);
5582 #endif /* CONFIG_BPF_SYSCALL */
5585 * security_locked_down() - Check if a kernel feature is allowed
5586 * @what: requested kernel feature
5588 * Determine whether a kernel feature that potentially enables arbitrary code
5589 * execution in kernel space should be permitted.
5591 * Return: Returns 0 if permission is granted.
5593 int security_locked_down(enum lockdown_reason what)
5595 return call_int_hook(locked_down, what);
5597 EXPORT_SYMBOL(security_locked_down);
5599 #ifdef CONFIG_PERF_EVENTS
5601 * security_perf_event_open() - Check if a perf event open is allowed
5602 * @attr: perf event attribute
5603 * @type: type of event
5605 * Check whether the @type of perf_event_open syscall is allowed.
5607 * Return: Returns 0 if permission is granted.
5609 int security_perf_event_open(struct perf_event_attr *attr, int type)
5611 return call_int_hook(perf_event_open, attr, type);
5615 * security_perf_event_alloc() - Allocate a perf event LSM blob
5616 * @event: perf event
5618 * Allocate and save perf_event security info.
5620 * Return: Returns 0 on success, error on failure.
5622 int security_perf_event_alloc(struct perf_event *event)
5624 return call_int_hook(perf_event_alloc, event);
5628 * security_perf_event_free() - Free a perf event LSM blob
5629 * @event: perf event
5631 * Release (free) perf_event security info.
5633 void security_perf_event_free(struct perf_event *event)
5635 call_void_hook(perf_event_free, event);
5639 * security_perf_event_read() - Check if reading a perf event label is allowed
5640 * @event: perf event
5642 * Read perf_event security info if allowed.
5644 * Return: Returns 0 if permission is granted.
5646 int security_perf_event_read(struct perf_event *event)
5648 return call_int_hook(perf_event_read, event);
5652 * security_perf_event_write() - Check if writing a perf event label is allowed
5653 * @event: perf event
5655 * Write perf_event security info if allowed.
5657 * Return: Returns 0 if permission is granted.
5659 int security_perf_event_write(struct perf_event *event)
5661 return call_int_hook(perf_event_write, event);
5663 #endif /* CONFIG_PERF_EVENTS */
5665 #ifdef CONFIG_IO_URING
5667 * security_uring_override_creds() - Check if overriding creds is allowed
5668 * @new: new credentials
5670 * Check if the current task, executing an io_uring operation, is allowed to
5671 * override it's credentials with @new.
5673 * Return: Returns 0 if permission is granted.
5675 int security_uring_override_creds(const struct cred *new)
5677 return call_int_hook(uring_override_creds, new);
5681 * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
5683 * Check whether the current task is allowed to spawn a io_uring polling thread
5684 * (IORING_SETUP_SQPOLL).
5686 * Return: Returns 0 if permission is granted.
5688 int security_uring_sqpoll(void)
5690 return call_int_hook(uring_sqpoll);
5694 * security_uring_cmd() - Check if a io_uring passthrough command is allowed
5695 * @ioucmd: command
5697 * Check whether the file_operations uring_cmd is allowed to run.
5699 * Return: Returns 0 if permission is granted.
5701 int security_uring_cmd(struct io_uring_cmd *ioucmd)
5703 return call_int_hook(uring_cmd, ioucmd);
5705 #endif /* CONFIG_IO_URING */