xref: /openbmc/linux/security/security.c (revision 9348944b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Security plug functions
4  *
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>
10  */
11 
12 #define pr_fmt(fmt) "LSM: " fmt
13 
14 #include <linux/bpf.h>
15 #include <linux/capability.h>
16 #include <linux/dcache.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/kernel_read_file.h>
21 #include <linux/lsm_hooks.h>
22 #include <linux/integrity.h>
23 #include <linux/ima.h>
24 #include <linux/evm.h>
25 #include <linux/fsnotify.h>
26 #include <linux/mman.h>
27 #include <linux/mount.h>
28 #include <linux/personality.h>
29 #include <linux/backing-dev.h>
30 #include <linux/string.h>
31 #include <linux/msg.h>
32 #include <net/flow.h>
33 
34 #define MAX_LSM_EVM_XATTR	2
35 
36 /* How many LSMs were built into the kernel? */
37 #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
38 
39 /*
40  * These are descriptions of the reasons that can be passed to the
41  * security_locked_down() LSM hook. Placing this array here allows
42  * all security modules to use the same descriptions for auditing
43  * purposes.
44  */
45 const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
46 	[LOCKDOWN_NONE] = "none",
47 	[LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
48 	[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
49 	[LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
50 	[LOCKDOWN_KEXEC] = "kexec of unsigned images",
51 	[LOCKDOWN_HIBERNATION] = "hibernation",
52 	[LOCKDOWN_PCI_ACCESS] = "direct PCI access",
53 	[LOCKDOWN_IOPORT] = "raw io port access",
54 	[LOCKDOWN_MSR] = "raw MSR access",
55 	[LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
56 	[LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
57 	[LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
58 	[LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
59 	[LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
60 	[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
61 	[LOCKDOWN_DEBUGFS] = "debugfs access",
62 	[LOCKDOWN_XMON_WR] = "xmon write access",
63 	[LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
64 	[LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
65 	[LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
66 	[LOCKDOWN_INTEGRITY_MAX] = "integrity",
67 	[LOCKDOWN_KCORE] = "/proc/kcore access",
68 	[LOCKDOWN_KPROBES] = "use of kprobes",
69 	[LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
70 	[LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
71 	[LOCKDOWN_PERF] = "unsafe use of perf",
72 	[LOCKDOWN_TRACEFS] = "use of tracefs",
73 	[LOCKDOWN_XMON_RW] = "xmon read and write access",
74 	[LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
75 	[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
76 };
77 
78 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
79 static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
80 
81 static struct kmem_cache *lsm_file_cache;
82 static struct kmem_cache *lsm_inode_cache;
83 
84 char *lsm_names;
85 static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
86 
87 /* Boot-time LSM user choice */
88 static __initdata const char *chosen_lsm_order;
89 static __initdata const char *chosen_major_lsm;
90 
91 static __initconst const char * const builtin_lsm_order = CONFIG_LSM;
92 
93 /* Ordered list of LSMs to initialize. */
94 static __initdata struct lsm_info **ordered_lsms;
95 static __initdata struct lsm_info *exclusive;
96 
97 static __initdata bool debug;
98 #define init_debug(...)						\
99 	do {							\
100 		if (debug)					\
101 			pr_info(__VA_ARGS__);			\
102 	} while (0)
103 
104 static bool __init is_enabled(struct lsm_info *lsm)
105 {
106 	if (!lsm->enabled)
107 		return false;
108 
109 	return *lsm->enabled;
110 }
111 
112 /* Mark an LSM's enabled flag. */
113 static int lsm_enabled_true __initdata = 1;
114 static int lsm_enabled_false __initdata = 0;
115 static void __init set_enabled(struct lsm_info *lsm, bool enabled)
116 {
117 	/*
118 	 * When an LSM hasn't configured an enable variable, we can use
119 	 * a hard-coded location for storing the default enabled state.
120 	 */
121 	if (!lsm->enabled) {
122 		if (enabled)
123 			lsm->enabled = &lsm_enabled_true;
124 		else
125 			lsm->enabled = &lsm_enabled_false;
126 	} else if (lsm->enabled == &lsm_enabled_true) {
127 		if (!enabled)
128 			lsm->enabled = &lsm_enabled_false;
129 	} else if (lsm->enabled == &lsm_enabled_false) {
130 		if (enabled)
131 			lsm->enabled = &lsm_enabled_true;
132 	} else {
133 		*lsm->enabled = enabled;
134 	}
135 }
136 
137 /* Is an LSM already listed in the ordered LSMs list? */
138 static bool __init exists_ordered_lsm(struct lsm_info *lsm)
139 {
140 	struct lsm_info **check;
141 
142 	for (check = ordered_lsms; *check; check++)
143 		if (*check == lsm)
144 			return true;
145 
146 	return false;
147 }
148 
149 /* Append an LSM to the list of ordered LSMs to initialize. */
150 static int last_lsm __initdata;
151 static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
152 {
153 	/* Ignore duplicate selections. */
154 	if (exists_ordered_lsm(lsm))
155 		return;
156 
157 	if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
158 		return;
159 
160 	/* Enable this LSM, if it is not already set. */
161 	if (!lsm->enabled)
162 		lsm->enabled = &lsm_enabled_true;
163 	ordered_lsms[last_lsm++] = lsm;
164 
165 	init_debug("%s ordered: %s (%s)\n", from, lsm->name,
166 		   is_enabled(lsm) ? "enabled" : "disabled");
167 }
168 
169 /* Is an LSM allowed to be initialized? */
170 static bool __init lsm_allowed(struct lsm_info *lsm)
171 {
172 	/* Skip if the LSM is disabled. */
173 	if (!is_enabled(lsm))
174 		return false;
175 
176 	/* Not allowed if another exclusive LSM already initialized. */
177 	if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
178 		init_debug("exclusive disabled: %s\n", lsm->name);
179 		return false;
180 	}
181 
182 	return true;
183 }
184 
185 static void __init lsm_set_blob_size(int *need, int *lbs)
186 {
187 	int offset;
188 
189 	if (*need <= 0)
190 		return;
191 
192 	offset = ALIGN(*lbs, sizeof(void *));
193 	*lbs = offset + *need;
194 	*need = offset;
195 }
196 
197 static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
198 {
199 	if (!needed)
200 		return;
201 
202 	lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
203 	lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
204 	/*
205 	 * The inode blob gets an rcu_head in addition to
206 	 * what the modules might need.
207 	 */
208 	if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
209 		blob_sizes.lbs_inode = sizeof(struct rcu_head);
210 	lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
211 	lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
212 	lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
213 	lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
214 	lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
215 }
216 
217 /* Prepare LSM for initialization. */
218 static void __init prepare_lsm(struct lsm_info *lsm)
219 {
220 	int enabled = lsm_allowed(lsm);
221 
222 	/* Record enablement (to handle any following exclusive LSMs). */
223 	set_enabled(lsm, enabled);
224 
225 	/* If enabled, do pre-initialization work. */
226 	if (enabled) {
227 		if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
228 			exclusive = lsm;
229 			init_debug("exclusive chosen:   %s\n", lsm->name);
230 		}
231 
232 		lsm_set_blob_sizes(lsm->blobs);
233 	}
234 }
235 
236 /* Initialize a given LSM, if it is enabled. */
237 static void __init initialize_lsm(struct lsm_info *lsm)
238 {
239 	if (is_enabled(lsm)) {
240 		int ret;
241 
242 		init_debug("initializing %s\n", lsm->name);
243 		ret = lsm->init();
244 		WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
245 	}
246 }
247 
248 /* Populate ordered LSMs list from comma-separated LSM name list. */
249 static void __init ordered_lsm_parse(const char *order, const char *origin)
250 {
251 	struct lsm_info *lsm;
252 	char *sep, *name, *next;
253 
254 	/* LSM_ORDER_FIRST is always first. */
255 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
256 		if (lsm->order == LSM_ORDER_FIRST)
257 			append_ordered_lsm(lsm, "  first");
258 	}
259 
260 	/* Process "security=", if given. */
261 	if (chosen_major_lsm) {
262 		struct lsm_info *major;
263 
264 		/*
265 		 * To match the original "security=" behavior, this
266 		 * explicitly does NOT fallback to another Legacy Major
267 		 * if the selected one was separately disabled: disable
268 		 * all non-matching Legacy Major LSMs.
269 		 */
270 		for (major = __start_lsm_info; major < __end_lsm_info;
271 		     major++) {
272 			if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
273 			    strcmp(major->name, chosen_major_lsm) != 0) {
274 				set_enabled(major, false);
275 				init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
276 					   chosen_major_lsm, major->name);
277 			}
278 		}
279 	}
280 
281 	sep = kstrdup(order, GFP_KERNEL);
282 	next = sep;
283 	/* Walk the list, looking for matching LSMs. */
284 	while ((name = strsep(&next, ",")) != NULL) {
285 		bool found = false;
286 
287 		for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
288 			if (lsm->order == LSM_ORDER_MUTABLE &&
289 			    strcmp(lsm->name, name) == 0) {
290 				append_ordered_lsm(lsm, origin);
291 				found = true;
292 			}
293 		}
294 
295 		if (!found)
296 			init_debug("%s ignored: %s (not built into kernel)\n",
297 				   origin, name);
298 	}
299 
300 	/* Process "security=", if given. */
301 	if (chosen_major_lsm) {
302 		for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
303 			if (exists_ordered_lsm(lsm))
304 				continue;
305 			if (strcmp(lsm->name, chosen_major_lsm) == 0)
306 				append_ordered_lsm(lsm, "security=");
307 		}
308 	}
309 
310 	/* Disable all LSMs not in the ordered list. */
311 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
312 		if (exists_ordered_lsm(lsm))
313 			continue;
314 		set_enabled(lsm, false);
315 		init_debug("%s skipped: %s (not in requested order)\n",
316 			   origin, lsm->name);
317 	}
318 
319 	kfree(sep);
320 }
321 
322 static void __init lsm_early_cred(struct cred *cred);
323 static void __init lsm_early_task(struct task_struct *task);
324 
325 static int lsm_append(const char *new, char **result);
326 
327 static void __init report_lsm_order(void)
328 {
329 	struct lsm_info **lsm, *early;
330 	int first = 0;
331 
332 	pr_info("initializing lsm=");
333 
334 	/* Report each enabled LSM name, comma separated. */
335 	for (early = __start_early_lsm_info; early < __end_early_lsm_info; early++)
336 		if (is_enabled(early))
337 			pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
338 	for (lsm = ordered_lsms; *lsm; lsm++)
339 		if (is_enabled(*lsm))
340 			pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
341 
342 	pr_cont("\n");
343 }
344 
345 static void __init ordered_lsm_init(void)
346 {
347 	struct lsm_info **lsm;
348 
349 	ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
350 				GFP_KERNEL);
351 
352 	if (chosen_lsm_order) {
353 		if (chosen_major_lsm) {
354 			pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
355 				chosen_major_lsm, chosen_lsm_order);
356 			chosen_major_lsm = NULL;
357 		}
358 		ordered_lsm_parse(chosen_lsm_order, "cmdline");
359 	} else
360 		ordered_lsm_parse(builtin_lsm_order, "builtin");
361 
362 	for (lsm = ordered_lsms; *lsm; lsm++)
363 		prepare_lsm(*lsm);
364 
365 	report_lsm_order();
366 
367 	init_debug("cred blob size       = %d\n", blob_sizes.lbs_cred);
368 	init_debug("file blob size       = %d\n", blob_sizes.lbs_file);
369 	init_debug("inode blob size      = %d\n", blob_sizes.lbs_inode);
370 	init_debug("ipc blob size        = %d\n", blob_sizes.lbs_ipc);
371 	init_debug("msg_msg blob size    = %d\n", blob_sizes.lbs_msg_msg);
372 	init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
373 	init_debug("task blob size       = %d\n", blob_sizes.lbs_task);
374 
375 	/*
376 	 * Create any kmem_caches needed for blobs
377 	 */
378 	if (blob_sizes.lbs_file)
379 		lsm_file_cache = kmem_cache_create("lsm_file_cache",
380 						   blob_sizes.lbs_file, 0,
381 						   SLAB_PANIC, NULL);
382 	if (blob_sizes.lbs_inode)
383 		lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
384 						    blob_sizes.lbs_inode, 0,
385 						    SLAB_PANIC, NULL);
386 
387 	lsm_early_cred((struct cred *) current->cred);
388 	lsm_early_task(current);
389 	for (lsm = ordered_lsms; *lsm; lsm++)
390 		initialize_lsm(*lsm);
391 
392 	kfree(ordered_lsms);
393 }
394 
395 int __init early_security_init(void)
396 {
397 	struct lsm_info *lsm;
398 
399 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
400 	INIT_HLIST_HEAD(&security_hook_heads.NAME);
401 #include "linux/lsm_hook_defs.h"
402 #undef LSM_HOOK
403 
404 	for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
405 		if (!lsm->enabled)
406 			lsm->enabled = &lsm_enabled_true;
407 		prepare_lsm(lsm);
408 		initialize_lsm(lsm);
409 	}
410 
411 	return 0;
412 }
413 
414 /**
415  * security_init - initializes the security framework
416  *
417  * This should be called early in the kernel initialization sequence.
418  */
419 int __init security_init(void)
420 {
421 	struct lsm_info *lsm;
422 
423 	init_debug("legacy security=%s\n", chosen_major_lsm ?: " *unspecified*");
424 	init_debug("  CONFIG_LSM=%s\n", builtin_lsm_order);
425 	init_debug("boot arg lsm=%s\n", chosen_lsm_order ?: " *unspecified*");
426 
427 	/*
428 	 * Append the names of the early LSM modules now that kmalloc() is
429 	 * available
430 	 */
431 	for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
432 		init_debug("  early started: %s (%s)\n", lsm->name,
433 			   is_enabled(lsm) ? "enabled" : "disabled");
434 		if (lsm->enabled)
435 			lsm_append(lsm->name, &lsm_names);
436 	}
437 
438 	/* Load LSMs in specified order. */
439 	ordered_lsm_init();
440 
441 	return 0;
442 }
443 
444 /* Save user chosen LSM */
445 static int __init choose_major_lsm(char *str)
446 {
447 	chosen_major_lsm = str;
448 	return 1;
449 }
450 __setup("security=", choose_major_lsm);
451 
452 /* Explicitly choose LSM initialization order. */
453 static int __init choose_lsm_order(char *str)
454 {
455 	chosen_lsm_order = str;
456 	return 1;
457 }
458 __setup("lsm=", choose_lsm_order);
459 
460 /* Enable LSM order debugging. */
461 static int __init enable_debug(char *str)
462 {
463 	debug = true;
464 	return 1;
465 }
466 __setup("lsm.debug", enable_debug);
467 
468 static bool match_last_lsm(const char *list, const char *lsm)
469 {
470 	const char *last;
471 
472 	if (WARN_ON(!list || !lsm))
473 		return false;
474 	last = strrchr(list, ',');
475 	if (last)
476 		/* Pass the comma, strcmp() will check for '\0' */
477 		last++;
478 	else
479 		last = list;
480 	return !strcmp(last, lsm);
481 }
482 
483 static int lsm_append(const char *new, char **result)
484 {
485 	char *cp;
486 
487 	if (*result == NULL) {
488 		*result = kstrdup(new, GFP_KERNEL);
489 		if (*result == NULL)
490 			return -ENOMEM;
491 	} else {
492 		/* Check if it is the last registered name */
493 		if (match_last_lsm(*result, new))
494 			return 0;
495 		cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
496 		if (cp == NULL)
497 			return -ENOMEM;
498 		kfree(*result);
499 		*result = cp;
500 	}
501 	return 0;
502 }
503 
504 /**
505  * security_add_hooks - Add a modules hooks to the hook lists.
506  * @hooks: the hooks to add
507  * @count: the number of hooks to add
508  * @lsm: the name of the security module
509  *
510  * Each LSM has to register its hooks with the infrastructure.
511  */
512 void __init security_add_hooks(struct security_hook_list *hooks, int count,
513 				const char *lsm)
514 {
515 	int i;
516 
517 	for (i = 0; i < count; i++) {
518 		hooks[i].lsm = lsm;
519 		hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
520 	}
521 
522 	/*
523 	 * Don't try to append during early_security_init(), we'll come back
524 	 * and fix this up afterwards.
525 	 */
526 	if (slab_is_available()) {
527 		if (lsm_append(lsm, &lsm_names) < 0)
528 			panic("%s - Cannot get early memory.\n", __func__);
529 	}
530 }
531 
532 int call_blocking_lsm_notifier(enum lsm_event event, void *data)
533 {
534 	return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
535 					    event, data);
536 }
537 EXPORT_SYMBOL(call_blocking_lsm_notifier);
538 
539 int register_blocking_lsm_notifier(struct notifier_block *nb)
540 {
541 	return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
542 						nb);
543 }
544 EXPORT_SYMBOL(register_blocking_lsm_notifier);
545 
546 int unregister_blocking_lsm_notifier(struct notifier_block *nb)
547 {
548 	return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
549 						  nb);
550 }
551 EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
552 
553 /**
554  * lsm_cred_alloc - allocate a composite cred blob
555  * @cred: the cred that needs a blob
556  * @gfp: allocation type
557  *
558  * Allocate the cred blob for all the modules
559  *
560  * Returns 0, or -ENOMEM if memory can't be allocated.
561  */
562 static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
563 {
564 	if (blob_sizes.lbs_cred == 0) {
565 		cred->security = NULL;
566 		return 0;
567 	}
568 
569 	cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
570 	if (cred->security == NULL)
571 		return -ENOMEM;
572 	return 0;
573 }
574 
575 /**
576  * lsm_early_cred - during initialization allocate a composite cred blob
577  * @cred: the cred that needs a blob
578  *
579  * Allocate the cred blob for all the modules
580  */
581 static void __init lsm_early_cred(struct cred *cred)
582 {
583 	int rc = lsm_cred_alloc(cred, GFP_KERNEL);
584 
585 	if (rc)
586 		panic("%s: Early cred alloc failed.\n", __func__);
587 }
588 
589 /**
590  * lsm_file_alloc - allocate a composite file blob
591  * @file: the file that needs a blob
592  *
593  * Allocate the file blob for all the modules
594  *
595  * Returns 0, or -ENOMEM if memory can't be allocated.
596  */
597 static int lsm_file_alloc(struct file *file)
598 {
599 	if (!lsm_file_cache) {
600 		file->f_security = NULL;
601 		return 0;
602 	}
603 
604 	file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
605 	if (file->f_security == NULL)
606 		return -ENOMEM;
607 	return 0;
608 }
609 
610 /**
611  * lsm_inode_alloc - allocate a composite inode blob
612  * @inode: the inode that needs a blob
613  *
614  * Allocate the inode blob for all the modules
615  *
616  * Returns 0, or -ENOMEM if memory can't be allocated.
617  */
618 int lsm_inode_alloc(struct inode *inode)
619 {
620 	if (!lsm_inode_cache) {
621 		inode->i_security = NULL;
622 		return 0;
623 	}
624 
625 	inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
626 	if (inode->i_security == NULL)
627 		return -ENOMEM;
628 	return 0;
629 }
630 
631 /**
632  * lsm_task_alloc - allocate a composite task blob
633  * @task: the task that needs a blob
634  *
635  * Allocate the task blob for all the modules
636  *
637  * Returns 0, or -ENOMEM if memory can't be allocated.
638  */
639 static int lsm_task_alloc(struct task_struct *task)
640 {
641 	if (blob_sizes.lbs_task == 0) {
642 		task->security = NULL;
643 		return 0;
644 	}
645 
646 	task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
647 	if (task->security == NULL)
648 		return -ENOMEM;
649 	return 0;
650 }
651 
652 /**
653  * lsm_ipc_alloc - allocate a composite ipc blob
654  * @kip: the ipc that needs a blob
655  *
656  * Allocate the ipc blob for all the modules
657  *
658  * Returns 0, or -ENOMEM if memory can't be allocated.
659  */
660 static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
661 {
662 	if (blob_sizes.lbs_ipc == 0) {
663 		kip->security = NULL;
664 		return 0;
665 	}
666 
667 	kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
668 	if (kip->security == NULL)
669 		return -ENOMEM;
670 	return 0;
671 }
672 
673 /**
674  * lsm_msg_msg_alloc - allocate a composite msg_msg blob
675  * @mp: the msg_msg that needs a blob
676  *
677  * Allocate the ipc blob for all the modules
678  *
679  * Returns 0, or -ENOMEM if memory can't be allocated.
680  */
681 static int lsm_msg_msg_alloc(struct msg_msg *mp)
682 {
683 	if (blob_sizes.lbs_msg_msg == 0) {
684 		mp->security = NULL;
685 		return 0;
686 	}
687 
688 	mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
689 	if (mp->security == NULL)
690 		return -ENOMEM;
691 	return 0;
692 }
693 
694 /**
695  * lsm_early_task - during initialization allocate a composite task blob
696  * @task: the task that needs a blob
697  *
698  * Allocate the task blob for all the modules
699  */
700 static void __init lsm_early_task(struct task_struct *task)
701 {
702 	int rc = lsm_task_alloc(task);
703 
704 	if (rc)
705 		panic("%s: Early task alloc failed.\n", __func__);
706 }
707 
708 /**
709  * lsm_superblock_alloc - allocate a composite superblock blob
710  * @sb: the superblock that needs a blob
711  *
712  * Allocate the superblock blob for all the modules
713  *
714  * Returns 0, or -ENOMEM if memory can't be allocated.
715  */
716 static int lsm_superblock_alloc(struct super_block *sb)
717 {
718 	if (blob_sizes.lbs_superblock == 0) {
719 		sb->s_security = NULL;
720 		return 0;
721 	}
722 
723 	sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
724 	if (sb->s_security == NULL)
725 		return -ENOMEM;
726 	return 0;
727 }
728 
729 /*
730  * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
731  * can be accessed with:
732  *
733  *	LSM_RET_DEFAULT(<hook_name>)
734  *
735  * The macros below define static constants for the default value of each
736  * LSM hook.
737  */
738 #define LSM_RET_DEFAULT(NAME) (NAME##_default)
739 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
740 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
741 	static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
742 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
743 	DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
744 
745 #include <linux/lsm_hook_defs.h>
746 #undef LSM_HOOK
747 
748 /*
749  * Hook list operation macros.
750  *
751  * call_void_hook:
752  *	This is a hook that does not return a value.
753  *
754  * call_int_hook:
755  *	This is a hook that returns a value.
756  */
757 
758 #define call_void_hook(FUNC, ...)				\
759 	do {							\
760 		struct security_hook_list *P;			\
761 								\
762 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
763 			P->hook.FUNC(__VA_ARGS__);		\
764 	} while (0)
765 
766 #define call_int_hook(FUNC, IRC, ...) ({			\
767 	int RC = IRC;						\
768 	do {							\
769 		struct security_hook_list *P;			\
770 								\
771 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
772 			RC = P->hook.FUNC(__VA_ARGS__);		\
773 			if (RC != 0)				\
774 				break;				\
775 		}						\
776 	} while (0);						\
777 	RC;							\
778 })
779 
780 /* Security operations */
781 
782 int security_binder_set_context_mgr(const struct cred *mgr)
783 {
784 	return call_int_hook(binder_set_context_mgr, 0, mgr);
785 }
786 
787 int security_binder_transaction(const struct cred *from,
788 				const struct cred *to)
789 {
790 	return call_int_hook(binder_transaction, 0, from, to);
791 }
792 
793 int security_binder_transfer_binder(const struct cred *from,
794 				    const struct cred *to)
795 {
796 	return call_int_hook(binder_transfer_binder, 0, from, to);
797 }
798 
799 int security_binder_transfer_file(const struct cred *from,
800 				  const struct cred *to, struct file *file)
801 {
802 	return call_int_hook(binder_transfer_file, 0, from, to, file);
803 }
804 
805 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
806 {
807 	return call_int_hook(ptrace_access_check, 0, child, mode);
808 }
809 
810 int security_ptrace_traceme(struct task_struct *parent)
811 {
812 	return call_int_hook(ptrace_traceme, 0, parent);
813 }
814 
815 int security_capget(struct task_struct *target,
816 		     kernel_cap_t *effective,
817 		     kernel_cap_t *inheritable,
818 		     kernel_cap_t *permitted)
819 {
820 	return call_int_hook(capget, 0, target,
821 				effective, inheritable, permitted);
822 }
823 
824 int security_capset(struct cred *new, const struct cred *old,
825 		    const kernel_cap_t *effective,
826 		    const kernel_cap_t *inheritable,
827 		    const kernel_cap_t *permitted)
828 {
829 	return call_int_hook(capset, 0, new, old,
830 				effective, inheritable, permitted);
831 }
832 
833 int security_capable(const struct cred *cred,
834 		     struct user_namespace *ns,
835 		     int cap,
836 		     unsigned int opts)
837 {
838 	return call_int_hook(capable, 0, cred, ns, cap, opts);
839 }
840 
841 int security_quotactl(int cmds, int type, int id, struct super_block *sb)
842 {
843 	return call_int_hook(quotactl, 0, cmds, type, id, sb);
844 }
845 
846 int security_quota_on(struct dentry *dentry)
847 {
848 	return call_int_hook(quota_on, 0, dentry);
849 }
850 
851 int security_syslog(int type)
852 {
853 	return call_int_hook(syslog, 0, type);
854 }
855 
856 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
857 {
858 	return call_int_hook(settime, 0, ts, tz);
859 }
860 
861 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
862 {
863 	struct security_hook_list *hp;
864 	int cap_sys_admin = 1;
865 	int rc;
866 
867 	/*
868 	 * The module will respond with a positive value if
869 	 * it thinks the __vm_enough_memory() call should be
870 	 * made with the cap_sys_admin set. If all of the modules
871 	 * agree that it should be set it will. If any module
872 	 * thinks it should not be set it won't.
873 	 */
874 	hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
875 		rc = hp->hook.vm_enough_memory(mm, pages);
876 		if (rc <= 0) {
877 			cap_sys_admin = 0;
878 			break;
879 		}
880 	}
881 	return __vm_enough_memory(mm, pages, cap_sys_admin);
882 }
883 
884 /**
885  * security_bprm_creds_for_exec() - Prepare the credentials for exec()
886  * @bprm: binary program information
887  *
888  * If the setup in prepare_exec_creds did not setup @bprm->cred->security
889  * properly for executing @bprm->file, update the LSM's portion of
890  * @bprm->cred->security to be what commit_creds needs to install for the new
891  * program.  This hook may also optionally check permissions (e.g. for
892  * transitions between security domains).  The hook must set @bprm->secureexec
893  * to 1 if AT_SECURE should be set to request libc enable secure mode.  @bprm
894  * contains the linux_binprm structure.
895  *
896  * Return: Returns 0 if the hook is successful and permission is granted.
897  */
898 int security_bprm_creds_for_exec(struct linux_binprm *bprm)
899 {
900 	return call_int_hook(bprm_creds_for_exec, 0, bprm);
901 }
902 
903 /**
904  * security_bprm_creds_from_file() - Update linux_binprm creds based on file
905  * @bprm: binary program information
906  * @file: associated file
907  *
908  * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
909  * exec, update @bprm->cred to reflect that change. This is called after
910  * finding the binary that will be executed without an interpreter.  This
911  * ensures that the credentials will not be derived from a script that the
912  * binary will need to reopen, which when reopend may end up being a completely
913  * different file.  This hook may also optionally check permissions (e.g. for
914  * transitions between security domains).  The hook must set @bprm->secureexec
915  * to 1 if AT_SECURE should be set to request libc enable secure mode.  The
916  * hook must add to @bprm->per_clear any personality flags that should be
917  * cleared from current->personality.  @bprm contains the linux_binprm
918  * structure.
919  *
920  * Return: Returns 0 if the hook is successful and permission is granted.
921  */
922 int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
923 {
924 	return call_int_hook(bprm_creds_from_file, 0, bprm, file);
925 }
926 
927 /**
928  * security_bprm_check() - Mediate binary handler search
929  * @bprm: binary program information
930  *
931  * This hook mediates the point when a search for a binary handler will begin.
932  * It allows a check against the @bprm->cred->security value which was set in
933  * the preceding creds_for_exec call.  The argv list and envp list are reliably
934  * available in @bprm.  This hook may be called multiple times during a single
935  * execve.  @bprm contains the linux_binprm structure.
936  *
937  * Return: Returns 0 if the hook is successful and permission is granted.
938  */
939 int security_bprm_check(struct linux_binprm *bprm)
940 {
941 	int ret;
942 
943 	ret = call_int_hook(bprm_check_security, 0, bprm);
944 	if (ret)
945 		return ret;
946 	return ima_bprm_check(bprm);
947 }
948 
949 /**
950  * security_bprm_committing_creds() - Install creds for a process during exec()
951  * @bprm: binary program information
952  *
953  * Prepare to install the new security attributes of a process being
954  * transformed by an execve operation, based on the old credentials pointed to
955  * by @current->cred and the information set in @bprm->cred by the
956  * bprm_creds_for_exec hook.  @bprm points to the linux_binprm structure.  This
957  * hook is a good place to perform state changes on the process such as closing
958  * open file descriptors to which access will no longer be granted when the
959  * attributes are changed.  This is called immediately before commit_creds().
960  */
961 void security_bprm_committing_creds(struct linux_binprm *bprm)
962 {
963 	call_void_hook(bprm_committing_creds, bprm);
964 }
965 
966 /**
967  * security_bprm_committed_creds() - Tidy up after cred install during exec()
968  * @bprm: binary program information
969  *
970  * Tidy up after the installation of the new security attributes of a process
971  * being transformed by an execve operation.  The new credentials have, by this
972  * point, been set to @current->cred.  @bprm points to the linux_binprm
973  * structure.  This hook is a good place to perform state changes on the
974  * process such as clearing out non-inheritable signal state.  This is called
975  * immediately after commit_creds().
976  */
977 void security_bprm_committed_creds(struct linux_binprm *bprm)
978 {
979 	call_void_hook(bprm_committed_creds, bprm);
980 }
981 
982 /**
983  * security_fs_context_dup() - Duplicate a fs_context LSM blob
984  * @fc: destination filesystem context
985  * @src_fc: source filesystem context
986  *
987  * Allocate and attach a security structure to sc->security.  This pointer is
988  * initialised to NULL by the caller.  @fc indicates the new filesystem context.
989  * @src_fc indicates the original filesystem context.
990  *
991  * Return: Returns 0 on success or a negative error code on failure.
992  */
993 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
994 {
995 	return call_int_hook(fs_context_dup, 0, fc, src_fc);
996 }
997 
998 /**
999  * security_fs_context_parse_param() - Configure a filesystem context
1000  * @fc: filesystem context
1001  * @param: filesystem parameter
1002  *
1003  * Userspace provided a parameter to configure a superblock.  The LSM can
1004  * consume the parameter or return it to the caller for use elsewhere.
1005  *
1006  * Return: If the parameter is used by the LSM it should return 0, if it is
1007  *         returned to the caller -ENOPARAM is returned, otherwise a negative
1008  *         error code is returned.
1009  */
1010 int security_fs_context_parse_param(struct fs_context *fc,
1011 				    struct fs_parameter *param)
1012 {
1013 	struct security_hook_list *hp;
1014 	int trc;
1015 	int rc = -ENOPARAM;
1016 
1017 	hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1018 			     list) {
1019 		trc = hp->hook.fs_context_parse_param(fc, param);
1020 		if (trc == 0)
1021 			rc = 0;
1022 		else if (trc != -ENOPARAM)
1023 			return trc;
1024 	}
1025 	return rc;
1026 }
1027 
1028 /**
1029  * security_sb_alloc() - Allocate a super_block LSM blob
1030  * @sb: filesystem superblock
1031  *
1032  * Allocate and attach a security structure to the sb->s_security field.  The
1033  * s_security field is initialized to NULL when the structure is allocated.
1034  * @sb contains the super_block structure to be modified.
1035  *
1036  * Return: Returns 0 if operation was successful.
1037  */
1038 int security_sb_alloc(struct super_block *sb)
1039 {
1040 	int rc = lsm_superblock_alloc(sb);
1041 
1042 	if (unlikely(rc))
1043 		return rc;
1044 	rc = call_int_hook(sb_alloc_security, 0, sb);
1045 	if (unlikely(rc))
1046 		security_sb_free(sb);
1047 	return rc;
1048 }
1049 
1050 /**
1051  * security_sb_delete() - Release super_block LSM associated objects
1052  * @sb: filesystem superblock
1053  *
1054  * Release objects tied to a superblock (e.g. inodes).  @sb contains the
1055  * super_block structure being released.
1056  */
1057 void security_sb_delete(struct super_block *sb)
1058 {
1059 	call_void_hook(sb_delete, sb);
1060 }
1061 
1062 /**
1063  * security_sb_free() - Free a super_block LSM blob
1064  * @sb: filesystem superblock
1065  *
1066  * Deallocate and clear the sb->s_security field.  @sb contains the super_block
1067  * structure to be modified.
1068  */
1069 void security_sb_free(struct super_block *sb)
1070 {
1071 	call_void_hook(sb_free_security, sb);
1072 	kfree(sb->s_security);
1073 	sb->s_security = NULL;
1074 }
1075 
1076 /**
1077  * security_free_mnt_opts() - Free memory associated with mount options
1078  * @mnt_ops: LSM processed mount options
1079  *
1080  * Free memory associated with @mnt_ops.
1081  */
1082 void security_free_mnt_opts(void **mnt_opts)
1083 {
1084 	if (!*mnt_opts)
1085 		return;
1086 	call_void_hook(sb_free_mnt_opts, *mnt_opts);
1087 	*mnt_opts = NULL;
1088 }
1089 EXPORT_SYMBOL(security_free_mnt_opts);
1090 
1091 /**
1092  * security_sb_eat_lsm_opts() - Consume LSM mount options
1093  * @options: mount options
1094  * @mnt_ops: LSM processed mount options
1095  *
1096  * Eat (scan @options) and save them in @mnt_opts.
1097  *
1098  * Return: Returns 0 on success, negative values on failure.
1099  */
1100 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1101 {
1102 	return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
1103 }
1104 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1105 
1106 /**
1107  * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1108  * @sb: filesystem superblock
1109  * @mnt_opts: new mount options
1110  *
1111  * Determine if the new mount options in @mnt_opts are allowed given the
1112  * existing mounted filesystem at @sb.  @sb superblock being compared.
1113  *
1114  * Return: Returns 0 if options are compatible.
1115  */
1116 int security_sb_mnt_opts_compat(struct super_block *sb,
1117 				void *mnt_opts)
1118 {
1119 	return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
1120 }
1121 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1122 
1123 /**
1124  * security_sb_remount() - Verify no incompatible mount changes during remount
1125  * @sb: filesystem superblock
1126  * @mnt_opts: (re)mount options
1127  *
1128  * Extracts security system specific mount options and verifies no changes are
1129  * being made to those options.
1130  *
1131  * Return: Returns 0 if permission is granted.
1132  */
1133 int security_sb_remount(struct super_block *sb,
1134 			void *mnt_opts)
1135 {
1136 	return call_int_hook(sb_remount, 0, sb, mnt_opts);
1137 }
1138 EXPORT_SYMBOL(security_sb_remount);
1139 
1140 /**
1141  * security_sb_kern_mount() - Check if a kernel mount is allowed
1142  * @sb: filesystem superblock
1143  *
1144  * Mount this @sb if allowed by permissions.
1145  *
1146  * Return: Returns 0 if permission is granted.
1147  */
1148 int security_sb_kern_mount(struct super_block *sb)
1149 {
1150 	return call_int_hook(sb_kern_mount, 0, sb);
1151 }
1152 
1153 /**
1154  * security_sb_show_options() - Output the mount options for a superblock
1155  * @m: output file
1156  * @sb: filesystem superblock
1157  *
1158  * Show (print on @m) mount options for this @sb.
1159  *
1160  * Return: Returns 0 on success, negative values on failure.
1161  */
1162 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1163 {
1164 	return call_int_hook(sb_show_options, 0, m, sb);
1165 }
1166 
1167 /**
1168  * security_sb_statfs() - Check if accessing fs stats is allowed
1169  * @dentry: superblock handle
1170  *
1171  * Check permission before obtaining filesystem statistics for the @mnt
1172  * mountpoint.  @dentry is a handle on the superblock for the filesystem.
1173  *
1174  * Return: Returns 0 if permission is granted.
1175  */
1176 int security_sb_statfs(struct dentry *dentry)
1177 {
1178 	return call_int_hook(sb_statfs, 0, dentry);
1179 }
1180 
1181 /**
1182  * security_sb_mount() - Check permission for mounting a filesystem
1183  * @dev_name: filesystem backing device
1184  * @path: mount point
1185  * @type: filesystem type
1186  * @flags: mount flags
1187  * @data: filesystem specific data
1188  *
1189  * Check permission before an object specified by @dev_name is mounted on the
1190  * mount point named by @nd.  For an ordinary mount, @dev_name identifies a
1191  * device if the file system type requires a device.  For a remount
1192  * (@flags & MS_REMOUNT), @dev_name is irrelevant.  For a loopback/bind mount
1193  * (@flags & MS_BIND), @dev_name identifies the	pathname of the object being
1194  * mounted.
1195  *
1196  * Return: Returns 0 if permission is granted.
1197  */
1198 int security_sb_mount(const char *dev_name, const struct path *path,
1199                        const char *type, unsigned long flags, void *data)
1200 {
1201 	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
1202 }
1203 
1204 /**
1205  * security_sb_umount() - Check permission for unmounting a filesystem
1206  * @mnt: mounted filesystem
1207  * @flags: unmount flags
1208  *
1209  * Check permission before the @mnt file system is unmounted.
1210  *
1211  * Return: Returns 0 if permission is granted.
1212  */
1213 int security_sb_umount(struct vfsmount *mnt, int flags)
1214 {
1215 	return call_int_hook(sb_umount, 0, mnt, flags);
1216 }
1217 
1218 /**
1219  * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1220  * @old_path: new location for current rootfs
1221  * @new_path: location of the new rootfs
1222  *
1223  * Check permission before pivoting the root filesystem.
1224  *
1225  * Return: Returns 0 if permission is granted.
1226  */
1227 int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
1228 {
1229 	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
1230 }
1231 
1232 /**
1233  * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1234  * @sb: filesystem superblock
1235  * @mnt_opts: binary mount options
1236  * @kern_flags: kernel flags (in)
1237  * @set_kern_flags: kernel flags (out)
1238  *
1239  * Set the security relevant mount options used for a superblock.
1240  *
1241  * Return: Returns 0 on success, error on failure.
1242  */
1243 int security_sb_set_mnt_opts(struct super_block *sb,
1244 				void *mnt_opts,
1245 				unsigned long kern_flags,
1246 				unsigned long *set_kern_flags)
1247 {
1248 	return call_int_hook(sb_set_mnt_opts,
1249 				mnt_opts ? -EOPNOTSUPP : 0, sb,
1250 				mnt_opts, kern_flags, set_kern_flags);
1251 }
1252 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1253 
1254 /**
1255  * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1256  * @olddb: source superblock
1257  * @newdb: destination superblock
1258  * @kern_flags: kernel flags (in)
1259  * @set_kern_flags: kernel flags (out)
1260  *
1261  * Copy all security options from a given superblock to another.
1262  *
1263  * Return: Returns 0 on success, error on failure.
1264  */
1265 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1266 				struct super_block *newsb,
1267 				unsigned long kern_flags,
1268 				unsigned long *set_kern_flags)
1269 {
1270 	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
1271 				kern_flags, set_kern_flags);
1272 }
1273 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1274 
1275 /**
1276  * security_move_mount() - Check permissions for moving a mount
1277  * @from_path: source mount point
1278  * @to_path: destination mount point
1279  *
1280  * Check permission before a mount is moved.
1281  *
1282  * Return: Returns 0 if permission is granted.
1283  */
1284 int security_move_mount(const struct path *from_path, const struct path *to_path)
1285 {
1286 	return call_int_hook(move_mount, 0, from_path, to_path);
1287 }
1288 
1289 /**
1290  * security_path_notify() - Check if setting a watch is allowed
1291  * @path: file path
1292  * @mask: event mask
1293  * @obj_type: file path type
1294  *
1295  * Check permissions before setting a watch on events as defined by @mask, on
1296  * an object at @path, whose type is defined by @obj_type.
1297  *
1298  * Return: Returns 0 if permission is granted.
1299  */
1300 int security_path_notify(const struct path *path, u64 mask,
1301 				unsigned int obj_type)
1302 {
1303 	return call_int_hook(path_notify, 0, path, mask, obj_type);
1304 }
1305 
1306 /**
1307  * security_inode_alloc() - Allocate an inode LSM blob
1308  * @inode: the inode
1309  *
1310  * Allocate and attach a security structure to @inode->i_security.  The
1311  * i_security field is initialized to NULL when the inode structure is
1312  * allocated.
1313  *
1314  * Return: Return 0 if operation was successful.
1315  */
1316 int security_inode_alloc(struct inode *inode)
1317 {
1318 	int rc = lsm_inode_alloc(inode);
1319 
1320 	if (unlikely(rc))
1321 		return rc;
1322 	rc = call_int_hook(inode_alloc_security, 0, inode);
1323 	if (unlikely(rc))
1324 		security_inode_free(inode);
1325 	return rc;
1326 }
1327 
1328 static void inode_free_by_rcu(struct rcu_head *head)
1329 {
1330 	/*
1331 	 * The rcu head is at the start of the inode blob
1332 	 */
1333 	kmem_cache_free(lsm_inode_cache, head);
1334 }
1335 
1336 /**
1337  * security_inode_free() - Free an inode's LSM blob
1338  * @inode: the inode
1339  *
1340  * Deallocate the inode security structure and set @inode->i_security to NULL.
1341  */
1342 void security_inode_free(struct inode *inode)
1343 {
1344 	integrity_inode_free(inode);
1345 	call_void_hook(inode_free_security, inode);
1346 	/*
1347 	 * The inode may still be referenced in a path walk and
1348 	 * a call to security_inode_permission() can be made
1349 	 * after inode_free_security() is called. Ideally, the VFS
1350 	 * wouldn't do this, but fixing that is a much harder
1351 	 * job. For now, simply free the i_security via RCU, and
1352 	 * leave the current inode->i_security pointer intact.
1353 	 * The inode will be freed after the RCU grace period too.
1354 	 */
1355 	if (inode->i_security)
1356 		call_rcu((struct rcu_head *)inode->i_security,
1357 				inode_free_by_rcu);
1358 }
1359 
1360 /**
1361  * security_dentry_init_security() - Perform dentry initialization
1362  * @dentry: the dentry to initialize
1363  * @mode: mode used to determine resource type
1364  * @name: name of the last path component
1365  * @xattr_name: name of the security/LSM xattr
1366  * @ctx: pointer to the resulting LSM context
1367  * @ctxlen: length of @ctx
1368  *
1369  * Compute a context for a dentry as the inode is not yet available since NFSv4
1370  * has no label backed by an EA anyway.  It is important to note that
1371  * @xattr_name does not need to be free'd by the caller, it is a static string.
1372  *
1373  * Return: Returns 0 on success, negative values on failure.
1374  */
1375 int security_dentry_init_security(struct dentry *dentry, int mode,
1376 				  const struct qstr *name,
1377 				  const char **xattr_name, void **ctx,
1378 				  u32 *ctxlen)
1379 {
1380 	struct security_hook_list *hp;
1381 	int rc;
1382 
1383 	/*
1384 	 * Only one module will provide a security context.
1385 	 */
1386 	hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security, list) {
1387 		rc = hp->hook.dentry_init_security(dentry, mode, name,
1388 						   xattr_name, ctx, ctxlen);
1389 		if (rc != LSM_RET_DEFAULT(dentry_init_security))
1390 			return rc;
1391 	}
1392 	return LSM_RET_DEFAULT(dentry_init_security);
1393 }
1394 EXPORT_SYMBOL(security_dentry_init_security);
1395 
1396 /**
1397  * security_dentry_create_files_as() - Perform dentry initialization
1398  * @dentry: the dentry to initialize
1399  * @mode: mode used to determine resource type
1400  * @name: name of the last path component
1401  * @old: creds to use for LSM context calculations
1402  * @new: creds to modify
1403  *
1404  * Compute a context for a dentry as the inode is not yet available and set
1405  * that context in passed in creds so that new files are created using that
1406  * context. Context is calculated using the passed in creds and not the creds
1407  * of the caller.
1408  *
1409  * Return: Returns 0 on success, error on failure.
1410  */
1411 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1412 				    struct qstr *name,
1413 				    const struct cred *old, struct cred *new)
1414 {
1415 	return call_int_hook(dentry_create_files_as, 0, dentry, mode,
1416 				name, old, new);
1417 }
1418 EXPORT_SYMBOL(security_dentry_create_files_as);
1419 
1420 /**
1421  * security_inode_init_security() - Initialize an inode's LSM context
1422  * @inode: the inode
1423  * @dir: parent directory
1424  * @qstr: last component of the pathname
1425  * @initxattrs: callback function to write xattrs
1426  * @fs_data: filesystem specific data
1427  *
1428  * Obtain the security attribute name suffix and value to set on a newly
1429  * created inode and set up the incore security field for the new inode.  This
1430  * hook is called by the fs code as part of the inode creation transaction and
1431  * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1432  * hooks called by the VFS.  The hook function is expected to allocate the name
1433  * and value via kmalloc, with the caller being responsible for calling kfree
1434  * after using them.  If the security module does not use security attributes
1435  * or does not wish to put a security attribute on this particular inode, then
1436  * it should return -EOPNOTSUPP to skip this processing.
1437  *
1438  * Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is
1439  * needed, or -ENOMEM on memory allocation failure.
1440  */
1441 int security_inode_init_security(struct inode *inode, struct inode *dir,
1442 				 const struct qstr *qstr,
1443 				 const initxattrs initxattrs, void *fs_data)
1444 {
1445 	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
1446 	struct xattr *lsm_xattr, *evm_xattr, *xattr;
1447 	int ret;
1448 
1449 	if (unlikely(IS_PRIVATE(inode)))
1450 		return 0;
1451 
1452 	if (!initxattrs)
1453 		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
1454 				     dir, qstr, NULL, NULL, NULL);
1455 	memset(new_xattrs, 0, sizeof(new_xattrs));
1456 	lsm_xattr = new_xattrs;
1457 	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
1458 						&lsm_xattr->name,
1459 						&lsm_xattr->value,
1460 						&lsm_xattr->value_len);
1461 	if (ret)
1462 		goto out;
1463 
1464 	evm_xattr = lsm_xattr + 1;
1465 	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1466 	if (ret)
1467 		goto out;
1468 	ret = initxattrs(inode, new_xattrs, fs_data);
1469 out:
1470 	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
1471 		kfree(xattr->value);
1472 	return (ret == -EOPNOTSUPP) ? 0 : ret;
1473 }
1474 EXPORT_SYMBOL(security_inode_init_security);
1475 
1476 /**
1477  * security_inode_init_security_anon() - Initialize an anonymous inode
1478  * @inode: the inode
1479  * @name: the anonymous inode class
1480  * @context_inode: an optional related inode
1481  *
1482  * Set up the incore security field for the new anonymous inode and return
1483  * whether the inode creation is permitted by the security module or not.
1484  *
1485  * Return: Returns 0 on success, -EACCES if the security module denies the
1486  * creation of this inode, or another -errno upon other errors.
1487  */
1488 int security_inode_init_security_anon(struct inode *inode,
1489 				      const struct qstr *name,
1490 				      const struct inode *context_inode)
1491 {
1492 	return call_int_hook(inode_init_security_anon, 0, inode, name,
1493 			     context_inode);
1494 }
1495 
1496 int security_old_inode_init_security(struct inode *inode, struct inode *dir,
1497 				     const struct qstr *qstr, const char **name,
1498 				     void **value, size_t *len)
1499 {
1500 	if (unlikely(IS_PRIVATE(inode)))
1501 		return -EOPNOTSUPP;
1502 	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1503 			     qstr, name, value, len);
1504 }
1505 EXPORT_SYMBOL(security_old_inode_init_security);
1506 
1507 #ifdef CONFIG_SECURITY_PATH
1508 /**
1509  * security_path_mknod() - Check if creating a special file is allowed
1510  * @dir: parent directory
1511  * @dentry: new file
1512  * @mode: new file mode
1513  * @dev: device number
1514  *
1515  * Check permissions when creating a file. Note that this hook is called even
1516  * if mknod operation is being done for a regular file.
1517  *
1518  * Return: Returns 0 if permission is granted.
1519  */
1520 int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
1521 			unsigned int dev)
1522 {
1523 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1524 		return 0;
1525 	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1526 }
1527 EXPORT_SYMBOL(security_path_mknod);
1528 
1529 /**
1530  * security_path_mkdir() - Check if creating a new directory is allowed
1531  * @dir: parent directory
1532  * @dentry: new directory
1533  * @mode: new directory mode
1534  *
1535  * Check permissions to create a new directory in the existing directory.
1536  *
1537  * Return: Returns 0 if permission is granted.
1538  */
1539 int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
1540 {
1541 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1542 		return 0;
1543 	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1544 }
1545 EXPORT_SYMBOL(security_path_mkdir);
1546 
1547 /**
1548  * security_path_rmdir() - Check if removing a directory is allowed
1549  * @dir: parent directory
1550  * @dentry: directory to remove
1551  *
1552  * Check the permission to remove a directory.
1553  *
1554  * Return: Returns 0 if permission is granted.
1555  */
1556 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1557 {
1558 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1559 		return 0;
1560 	return call_int_hook(path_rmdir, 0, dir, dentry);
1561 }
1562 
1563 /**
1564  * security_path_unlink() - Check if removing a hard link is allowed
1565  * @dir: parent directory
1566  * @dentry: file
1567  *
1568  * Check the permission to remove a hard link to a file.
1569  *
1570  * Return: Returns 0 if permission is granted.
1571  */
1572 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1573 {
1574 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1575 		return 0;
1576 	return call_int_hook(path_unlink, 0, dir, dentry);
1577 }
1578 EXPORT_SYMBOL(security_path_unlink);
1579 
1580 /**
1581  * security_path_symlink() - Check if creating a symbolic link is allowed
1582  * @dir: parent directory
1583  * @dentry: symbolic link
1584  * @old_name: file pathname
1585  *
1586  * Check the permission to create a symbolic link to a file.
1587  *
1588  * Return: Returns 0 if permission is granted.
1589  */
1590 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1591 			  const char *old_name)
1592 {
1593 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1594 		return 0;
1595 	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1596 }
1597 
1598 /**
1599  * security_path_link - Check if creating a hard link is allowed
1600  * @old_dentry: existing file
1601  * @new_dir: new parent directory
1602  * @new_dentry: new link
1603  *
1604  * Check permission before creating a new hard link to a file.
1605  *
1606  * Return: Returns 0 if permission is granted.
1607  */
1608 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1609 		       struct dentry *new_dentry)
1610 {
1611 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1612 		return 0;
1613 	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1614 }
1615 
1616 /**
1617  * security_path_rename() - Check if renaming a file is allowed
1618  * @old_dir: parent directory of the old file
1619  * @old_dentry: the old file
1620  * @new_dir: parent directory of the new file
1621  * @new_dentry: the new file
1622  * @flags: flags
1623  *
1624  * Check for permission to rename a file or directory.
1625  *
1626  * Return: Returns 0 if permission is granted.
1627  */
1628 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1629 			 const struct path *new_dir, struct dentry *new_dentry,
1630 			 unsigned int flags)
1631 {
1632 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1633 		     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1634 		return 0;
1635 
1636 	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1637 				new_dentry, flags);
1638 }
1639 EXPORT_SYMBOL(security_path_rename);
1640 
1641 /**
1642  * security_path_truncate() - Check if truncating a file is allowed
1643  * @path: file
1644  *
1645  * Check permission before truncating the file indicated by path.  Note that
1646  * truncation permissions may also be checked based on already opened files,
1647  * using the security_file_truncate() hook.
1648  *
1649  * Return: Returns 0 if permission is granted.
1650  */
1651 int security_path_truncate(const struct path *path)
1652 {
1653 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1654 		return 0;
1655 	return call_int_hook(path_truncate, 0, path);
1656 }
1657 
1658 /**
1659  * security_path_chmod() - Check if changing the file's mode is allowed
1660  * @path: file
1661  * @mode: new mode
1662  *
1663  * Check for permission to change a mode of the file @path. The new mode is
1664  * specified in @mode which is a bitmask of constants from
1665  * <include/uapi/linux/stat.h>.
1666  *
1667  * Return: Returns 0 if permission is granted.
1668  */
1669 int security_path_chmod(const struct path *path, umode_t mode)
1670 {
1671 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1672 		return 0;
1673 	return call_int_hook(path_chmod, 0, path, mode);
1674 }
1675 
1676 /**
1677  * security_path_chown() - Check if changing the file's owner/group is allowed
1678  * @path: file
1679  * @uid: file owner
1680  * @gid: file group
1681  *
1682  * Check for permission to change owner/group of a file or directory.
1683  *
1684  * Return: Returns 0 if permission is granted.
1685  */
1686 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1687 {
1688 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1689 		return 0;
1690 	return call_int_hook(path_chown, 0, path, uid, gid);
1691 }
1692 
1693 /**
1694  * security_path_chroot() - Check if changing the root directory is allowed
1695  * @path: directory
1696  *
1697  * Check for permission to change root directory.
1698  *
1699  * Return: Returns 0 if permission is granted.
1700  */
1701 int security_path_chroot(const struct path *path)
1702 {
1703 	return call_int_hook(path_chroot, 0, path);
1704 }
1705 #endif
1706 
1707 /**
1708  * security_inode_create() - Check if creating a file is allowed
1709  * @dir: the parent directory
1710  * @dentry: the file being created
1711  * @mode: requested file mode
1712  *
1713  * Check permission to create a regular file.
1714  *
1715  * Return: Returns 0 if permission is granted.
1716  */
1717 int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1718 {
1719 	if (unlikely(IS_PRIVATE(dir)))
1720 		return 0;
1721 	return call_int_hook(inode_create, 0, dir, dentry, mode);
1722 }
1723 EXPORT_SYMBOL_GPL(security_inode_create);
1724 
1725 /**
1726  * security_inode_link() - Check if creating a hard link is allowed
1727  * @old_dentry: existing file
1728  * @dir: new parent directory
1729  * @new_dentry: new link
1730  *
1731  * Check permission before creating a new hard link to a file.
1732  *
1733  * Return: Returns 0 if permission is granted.
1734  */
1735 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1736 			 struct dentry *new_dentry)
1737 {
1738 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1739 		return 0;
1740 	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
1741 }
1742 
1743 /**
1744  * security_inode_unlink() - Check if removing a hard link is allowed
1745  * @dir: parent directory
1746  * @dentry: file
1747  *
1748  * Check the permission to remove a hard link to a file.
1749  *
1750  * Return: Returns 0 if permission is granted.
1751  */
1752 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1753 {
1754 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1755 		return 0;
1756 	return call_int_hook(inode_unlink, 0, dir, dentry);
1757 }
1758 
1759 /**
1760  * security_inode_symlink() Check if creating a symbolic link is allowed
1761  * @dir: parent directory
1762  * @dentry: symbolic link
1763  * @old_name: existing filename
1764  *
1765  * Check the permission to create a symbolic link to a file.
1766  *
1767  * Return: Returns 0 if permission is granted.
1768  */
1769 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1770 			    const char *old_name)
1771 {
1772 	if (unlikely(IS_PRIVATE(dir)))
1773 		return 0;
1774 	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
1775 }
1776 
1777 /**
1778  * security_inode_mkdir() - Check if creation a new director is allowed
1779  * @dir: parent directory
1780  * @dentry: new directory
1781  * @mode: new directory mode
1782  *
1783  * Check permissions to create a new directory in the existing directory
1784  * associated with inode structure @dir.
1785  *
1786  * Return: Returns 0 if permission is granted.
1787  */
1788 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1789 {
1790 	if (unlikely(IS_PRIVATE(dir)))
1791 		return 0;
1792 	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
1793 }
1794 EXPORT_SYMBOL_GPL(security_inode_mkdir);
1795 
1796 /**
1797  * security_inode_rmdir() - Check if removing a directory is allowed
1798  * @dir: parent directory
1799  * @dentry: directory to be removed
1800  *
1801  * Check the permission to remove a directory.
1802  *
1803  * Return: Returns 0 if permission is granted.
1804  */
1805 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1806 {
1807 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1808 		return 0;
1809 	return call_int_hook(inode_rmdir, 0, dir, dentry);
1810 }
1811 
1812 /**
1813  * security_inode_mknod() - Check if creating a special file is allowed
1814  * @dir: parent directory
1815  * @dentry: new file
1816  * @mode: new file mode
1817  * @dev: device number
1818  *
1819  * Check permissions when creating a special file (or a socket or a fifo file
1820  * created via the mknod system call).  Note that if mknod operation is being
1821  * done for a regular file, then the create hook will be called and not this
1822  * hook.
1823  *
1824  * Return: Returns 0 if permission is granted.
1825  */
1826 int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1827 {
1828 	if (unlikely(IS_PRIVATE(dir)))
1829 		return 0;
1830 	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
1831 }
1832 
1833 /**
1834  * security_inode_rename() - Check if renaming a file is allowed
1835  * @old_dir: parent directory of the old file
1836  * @old_dentry: the old file
1837  * @new_dir: parent directory of the new file
1838  * @new_dentry: the new file
1839  * @flags: flags
1840  *
1841  * Check for permission to rename a file or directory.
1842  *
1843  * Return: Returns 0 if permission is granted.
1844  */
1845 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
1846 			   struct inode *new_dir, struct dentry *new_dentry,
1847 			   unsigned int flags)
1848 {
1849         if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1850             (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1851 		return 0;
1852 
1853 	if (flags & RENAME_EXCHANGE) {
1854 		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
1855 						     old_dir, old_dentry);
1856 		if (err)
1857 			return err;
1858 	}
1859 
1860 	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
1861 					   new_dir, new_dentry);
1862 }
1863 
1864 /**
1865  * security_inode_readlink() - Check if reading a symbolic link is allowed
1866  * @dentry: link
1867  *
1868  * Check the permission to read the symbolic link.
1869  *
1870  * Return: Returns 0 if permission is granted.
1871  */
1872 int security_inode_readlink(struct dentry *dentry)
1873 {
1874 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1875 		return 0;
1876 	return call_int_hook(inode_readlink, 0, dentry);
1877 }
1878 
1879 /**
1880  * security_inode_follow_link() - Check if following a symbolic link is allowed
1881  * @dentry: link dentry
1882  * @inode: link inode
1883  * @rcu: true if in RCU-walk mode
1884  *
1885  * Check permission to follow a symbolic link when looking up a pathname.  If
1886  * @rcu is true, @inode is not stable.
1887  *
1888  * Return: Returns 0 if permission is granted.
1889  */
1890 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1891 			       bool rcu)
1892 {
1893 	if (unlikely(IS_PRIVATE(inode)))
1894 		return 0;
1895 	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
1896 }
1897 
1898 /**
1899  * security_inode_permission() - Check if accessing an inode is allowed
1900  * @inode: inode
1901  * @mask: access mask
1902  *
1903  * Check permission before accessing an inode.  This hook is called by the
1904  * existing Linux permission function, so a security module can use it to
1905  * provide additional checking for existing Linux permission checks.  Notice
1906  * that this hook is called when a file is opened (as well as many other
1907  * operations), whereas the file_security_ops permission hook is called when
1908  * the actual read/write operations are performed.
1909  *
1910  * Return: Returns 0 if permission is granted.
1911  */
1912 int security_inode_permission(struct inode *inode, int mask)
1913 {
1914 	if (unlikely(IS_PRIVATE(inode)))
1915 		return 0;
1916 	return call_int_hook(inode_permission, 0, inode, mask);
1917 }
1918 
1919 /**
1920  * security_inode_setattr() - Check if setting file attributes is allowed
1921  * @idmap: idmap of the mount
1922  * @dentry: file
1923  * @attr: new attributes
1924  *
1925  * Check permission before setting file attributes.  Note that the kernel call
1926  * to notify_change is performed from several locations, whenever file
1927  * attributes change (such as when a file is truncated, chown/chmod operations,
1928  * transferring disk quotas, etc).
1929  *
1930  * Return: Returns 0 if permission is granted.
1931  */
1932 int security_inode_setattr(struct mnt_idmap *idmap,
1933 			   struct dentry *dentry, struct iattr *attr)
1934 {
1935 	int ret;
1936 
1937 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1938 		return 0;
1939 	ret = call_int_hook(inode_setattr, 0, dentry, attr);
1940 	if (ret)
1941 		return ret;
1942 	return evm_inode_setattr(idmap, dentry, attr);
1943 }
1944 EXPORT_SYMBOL_GPL(security_inode_setattr);
1945 
1946 /**
1947  * security_inode_getattr() - Check if getting file attributes is allowed
1948  * @path: file
1949  *
1950  * Check permission before obtaining file attributes.
1951  *
1952  * Return: Returns 0 if permission is granted.
1953  */
1954 int security_inode_getattr(const struct path *path)
1955 {
1956 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1957 		return 0;
1958 	return call_int_hook(inode_getattr, 0, path);
1959 }
1960 
1961 /**
1962  * security_inode_setxattr() - Check if setting file xattrs is allowed
1963  * @idmap: idmap of the mount
1964  * @dentry: file
1965  * @name: xattr name
1966  * @value: xattr value
1967  * @flags: flags
1968  *
1969  * Check permission before setting the extended attributes.
1970  *
1971  * Return: Returns 0 if permission is granted.
1972  */
1973 int security_inode_setxattr(struct mnt_idmap *idmap,
1974 			    struct dentry *dentry, const char *name,
1975 			    const void *value, size_t size, int flags)
1976 {
1977 	int ret;
1978 
1979 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1980 		return 0;
1981 	/*
1982 	 * SELinux and Smack integrate the cap call,
1983 	 * so assume that all LSMs supplying this call do so.
1984 	 */
1985 	ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
1986 			    size, flags);
1987 
1988 	if (ret == 1)
1989 		ret = cap_inode_setxattr(dentry, name, value, size, flags);
1990 	if (ret)
1991 		return ret;
1992 	ret = ima_inode_setxattr(dentry, name, value, size);
1993 	if (ret)
1994 		return ret;
1995 	return evm_inode_setxattr(idmap, dentry, name, value, size);
1996 }
1997 
1998 /**
1999  * security_inode_set_acl() - Check if setting posix acls is allowed
2000  * @idmap: idmap of the mount
2001  * @dentry: file
2002  * @acl_name: acl name
2003  * @kacl: acl struct
2004  *
2005  * Check permission before setting posix acls, the posix acls in @kacl are
2006  * identified by @acl_name.
2007  *
2008  * Return: Returns 0 if permission is granted.
2009  */
2010 int security_inode_set_acl(struct mnt_idmap *idmap,
2011 			   struct dentry *dentry, const char *acl_name,
2012 			   struct posix_acl *kacl)
2013 {
2014 	int ret;
2015 
2016 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2017 		return 0;
2018 	ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
2019 			    kacl);
2020 	if (ret)
2021 		return ret;
2022 	ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
2023 	if (ret)
2024 		return ret;
2025 	return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
2026 }
2027 
2028 /**
2029  * security_inode_get_acl() - Check if reading posix acls is allowed
2030  * @idmap: idmap of the mount
2031  * @dentry: file
2032  * @acl_name: acl name
2033  *
2034  * Check permission before getting osix acls, the posix acls are identified by
2035  * @acl_name.
2036  *
2037  * Return: Returns 0 if permission is granted.
2038  */
2039 int security_inode_get_acl(struct mnt_idmap *idmap,
2040 			   struct dentry *dentry, const char *acl_name)
2041 {
2042 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2043 		return 0;
2044 	return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
2045 }
2046 
2047 /**
2048  * security_inode_remove_acl() - Check if removing a posix acl is allowed
2049  * @idmap: idmap of the mount
2050  * @dentry: file
2051  * @acl_name: acl name
2052  *
2053  * Check permission before removing posix acls, the posix acls are identified
2054  * by @acl_name.
2055  *
2056  * Return: Returns 0 if permission is granted.
2057  */
2058 int security_inode_remove_acl(struct mnt_idmap *idmap,
2059 			      struct dentry *dentry, const char *acl_name)
2060 {
2061 	int ret;
2062 
2063 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2064 		return 0;
2065 	ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
2066 	if (ret)
2067 		return ret;
2068 	ret = ima_inode_remove_acl(idmap, dentry, acl_name);
2069 	if (ret)
2070 		return ret;
2071 	return evm_inode_remove_acl(idmap, dentry, acl_name);
2072 }
2073 
2074 /**
2075  * security_inode_post_setxattr() - Update the inode after a setxattr operation
2076  * @dentry: file
2077  * @name: xattr name
2078  * @value: xattr value
2079  * @size: xattr value size
2080  * @flags: flags
2081  *
2082  * Update inode security field after successful setxattr operation.
2083  */
2084 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2085 				  const void *value, size_t size, int flags)
2086 {
2087 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2088 		return;
2089 	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2090 	evm_inode_post_setxattr(dentry, name, value, size);
2091 }
2092 
2093 /**
2094  * security_inode_getxattr() - Check if xattr access is allowed
2095  * @dentry: file
2096  * @name: xattr name
2097  *
2098  * Check permission before obtaining the extended attributes identified by
2099  * @name for @dentry.
2100  *
2101  * Return: Returns 0 if permission is granted.
2102  */
2103 int security_inode_getxattr(struct dentry *dentry, const char *name)
2104 {
2105 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2106 		return 0;
2107 	return call_int_hook(inode_getxattr, 0, dentry, name);
2108 }
2109 
2110 /**
2111  * security_inode_listxattr() - Check if listing xattrs is allowed
2112  * @dentry: file
2113  *
2114  * Check permission before obtaining the list of extended attribute names for
2115  * @dentry.
2116  *
2117  * Return: Returns 0 if permission is granted.
2118  */
2119 int security_inode_listxattr(struct dentry *dentry)
2120 {
2121 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2122 		return 0;
2123 	return call_int_hook(inode_listxattr, 0, dentry);
2124 }
2125 
2126 /**
2127  * security_inode_removexattr() - Check if removing an xattr is allowed
2128  * @idmap: idmap of the mount
2129  * @dentry: file
2130  * @name: xattr name
2131  *
2132  * Check permission before removing the extended attribute identified by @name
2133  * for @dentry.
2134  *
2135  * Return: Returns 0 if permission is granted.
2136  */
2137 int security_inode_removexattr(struct mnt_idmap *idmap,
2138 			       struct dentry *dentry, const char *name)
2139 {
2140 	int ret;
2141 
2142 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2143 		return 0;
2144 	/*
2145 	 * SELinux and Smack integrate the cap call,
2146 	 * so assume that all LSMs supplying this call do so.
2147 	 */
2148 	ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
2149 	if (ret == 1)
2150 		ret = cap_inode_removexattr(idmap, dentry, name);
2151 	if (ret)
2152 		return ret;
2153 	ret = ima_inode_removexattr(dentry, name);
2154 	if (ret)
2155 		return ret;
2156 	return evm_inode_removexattr(idmap, dentry, name);
2157 }
2158 
2159 /**
2160  * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2161  * @dentry: associated dentry
2162  *
2163  * Called when an inode has been changed to determine if
2164  * security_inode_killpriv() should be called.
2165  *
2166  * Return: Return <0 on error to abort the inode change operation, return 0 if
2167  *         security_inode_killpriv() does not need to be called, return >0 if
2168  *         security_inode_killpriv() does need to be called.
2169  */
2170 int security_inode_need_killpriv(struct dentry *dentry)
2171 {
2172 	return call_int_hook(inode_need_killpriv, 0, dentry);
2173 }
2174 
2175 /**
2176  * security_inode_killpriv() - The setuid bit is removed, update LSM state
2177  * @idmap: idmap of the mount
2178  * @dentry: associated dentry
2179  *
2180  * The @dentry's setuid bit is being removed.  Remove similar security labels.
2181  * Called with the dentry->d_inode->i_mutex held.
2182  *
2183  * Return: Return 0 on success.  If error is returned, then the operation
2184  *         causing setuid bit removal is failed.
2185  */
2186 int security_inode_killpriv(struct mnt_idmap *idmap,
2187 			    struct dentry *dentry)
2188 {
2189 	return call_int_hook(inode_killpriv, 0, idmap, dentry);
2190 }
2191 
2192 /**
2193  * security_inode_getsecurity() - Get the xattr security label of an inode
2194  * @idmap: idmap of the mount
2195  * @inode: inode
2196  * @name: xattr name
2197  * @buffer: security label buffer
2198  * @alloc: allocation flag
2199  *
2200  * Retrieve a copy of the extended attribute representation of the security
2201  * label associated with @name for @inode via @buffer.  Note that @name is the
2202  * remainder of the attribute name after the security prefix has been removed.
2203  * @alloc is used to specify if the call should return a value via the buffer
2204  * or just the value length.
2205  *
2206  * Return: Returns size of buffer on success.
2207  */
2208 int security_inode_getsecurity(struct mnt_idmap *idmap,
2209 			       struct inode *inode, const char *name,
2210 			       void **buffer, bool alloc)
2211 {
2212 	struct security_hook_list *hp;
2213 	int rc;
2214 
2215 	if (unlikely(IS_PRIVATE(inode)))
2216 		return LSM_RET_DEFAULT(inode_getsecurity);
2217 	/*
2218 	 * Only one module will provide an attribute with a given name.
2219 	 */
2220 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
2221 		rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer, alloc);
2222 		if (rc != LSM_RET_DEFAULT(inode_getsecurity))
2223 			return rc;
2224 	}
2225 	return LSM_RET_DEFAULT(inode_getsecurity);
2226 }
2227 
2228 /**
2229  * security_inode_setsecurity() - Set the xattr security label of an inode
2230  * @inode: inode
2231  * @name: xattr name
2232  * @value: security label
2233  * @size: length of security label
2234  * @flags: flags
2235  *
2236  * Set the security label associated with @name for @inode from the extended
2237  * attribute value @value.  @size indicates the size of the @value in bytes.
2238  * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2239  * remainder of the attribute name after the security. prefix has been removed.
2240  *
2241  * Return: Returns 0 on success.
2242  */
2243 int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
2244 {
2245 	struct security_hook_list *hp;
2246 	int rc;
2247 
2248 	if (unlikely(IS_PRIVATE(inode)))
2249 		return LSM_RET_DEFAULT(inode_setsecurity);
2250 	/*
2251 	 * Only one module will provide an attribute with a given name.
2252 	 */
2253 	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
2254 		rc = hp->hook.inode_setsecurity(inode, name, value, size,
2255 								flags);
2256 		if (rc != LSM_RET_DEFAULT(inode_setsecurity))
2257 			return rc;
2258 	}
2259 	return LSM_RET_DEFAULT(inode_setsecurity);
2260 }
2261 
2262 /**
2263  * security_inode_listsecurity() - List the xattr security label names
2264  * @inode: inode
2265  * @buffer: buffer
2266  * @buffer_size: size of buffer
2267  *
2268  * Copy the extended attribute names for the security labels associated with
2269  * @inode into @buffer.  The maximum size of @buffer is specified by
2270  * @buffer_size.  @buffer may be NULL to request the size of the buffer
2271  * required.
2272  *
2273  * Return: Returns number of bytes used/required on success.
2274  */
2275 int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2276 {
2277 	if (unlikely(IS_PRIVATE(inode)))
2278 		return 0;
2279 	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
2280 }
2281 EXPORT_SYMBOL(security_inode_listsecurity);
2282 
2283 /**
2284  * security_inode_getsecid() - Get an inode's secid
2285  * @inode: inode
2286  * @secid: secid to return
2287  *
2288  * Get the secid associated with the node.  In case of failure, @secid will be
2289  * set to zero.
2290  */
2291 void security_inode_getsecid(struct inode *inode, u32 *secid)
2292 {
2293 	call_void_hook(inode_getsecid, inode, secid);
2294 }
2295 
2296 /**
2297  * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2298  * @src: union dentry of copy-up file
2299  * @new: newly created creds
2300  *
2301  * A file is about to be copied up from lower layer to upper layer of overlay
2302  * filesystem. Security module can prepare a set of new creds and modify as
2303  * need be and return new creds. Caller will switch to new creds temporarily to
2304  * create new file and release newly allocated creds.
2305  *
2306  * Return: Returns 0 on success or a negative error code on error.
2307  */
2308 int security_inode_copy_up(struct dentry *src, struct cred **new)
2309 {
2310 	return call_int_hook(inode_copy_up, 0, src, new);
2311 }
2312 EXPORT_SYMBOL(security_inode_copy_up);
2313 
2314 /**
2315  * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2316  * @name: xattr name
2317  *
2318  * Filter the xattrs being copied up when a unioned file is copied up from a
2319  * lower layer to the union/overlay layer.   The caller is responsible for
2320  * reading and writing the xattrs, this hook is merely a filter.
2321  *
2322  * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2323  *         if the security module does not know about attribute, or a negative
2324  *         error code to abort the copy up.
2325  */
2326 int security_inode_copy_up_xattr(const char *name)
2327 {
2328 	struct security_hook_list *hp;
2329 	int rc;
2330 
2331 	/*
2332 	 * The implementation can return 0 (accept the xattr), 1 (discard the
2333 	 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2334 	 * any other error code incase of an error.
2335 	 */
2336 	hlist_for_each_entry(hp,
2337 		&security_hook_heads.inode_copy_up_xattr, list) {
2338 		rc = hp->hook.inode_copy_up_xattr(name);
2339 		if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2340 			return rc;
2341 	}
2342 
2343 	return LSM_RET_DEFAULT(inode_copy_up_xattr);
2344 }
2345 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2346 
2347 /**
2348  * security_kernfs_init_security() - Init LSM context for a kernfs node
2349  * @kn_dir: parent kernfs node
2350  * @kn: the kernfs node to initialize
2351  *
2352  * Initialize the security context of a newly created kernfs node based on its
2353  * own and its parent's attributes.
2354  *
2355  * Return: Returns 0 if permission is granted.
2356  */
2357 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2358 				  struct kernfs_node *kn)
2359 {
2360 	return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2361 }
2362 
2363 int security_file_permission(struct file *file, int mask)
2364 {
2365 	int ret;
2366 
2367 	ret = call_int_hook(file_permission, 0, file, mask);
2368 	if (ret)
2369 		return ret;
2370 
2371 	return fsnotify_perm(file, mask);
2372 }
2373 
2374 int security_file_alloc(struct file *file)
2375 {
2376 	int rc = lsm_file_alloc(file);
2377 
2378 	if (rc)
2379 		return rc;
2380 	rc = call_int_hook(file_alloc_security, 0, file);
2381 	if (unlikely(rc))
2382 		security_file_free(file);
2383 	return rc;
2384 }
2385 
2386 void security_file_free(struct file *file)
2387 {
2388 	void *blob;
2389 
2390 	call_void_hook(file_free_security, file);
2391 
2392 	blob = file->f_security;
2393 	if (blob) {
2394 		file->f_security = NULL;
2395 		kmem_cache_free(lsm_file_cache, blob);
2396 	}
2397 }
2398 
2399 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2400 {
2401 	return call_int_hook(file_ioctl, 0, file, cmd, arg);
2402 }
2403 EXPORT_SYMBOL_GPL(security_file_ioctl);
2404 
2405 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2406 {
2407 	/*
2408 	 * Does we have PROT_READ and does the application expect
2409 	 * it to imply PROT_EXEC?  If not, nothing to talk about...
2410 	 */
2411 	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2412 		return prot;
2413 	if (!(current->personality & READ_IMPLIES_EXEC))
2414 		return prot;
2415 	/*
2416 	 * if that's an anonymous mapping, let it.
2417 	 */
2418 	if (!file)
2419 		return prot | PROT_EXEC;
2420 	/*
2421 	 * ditto if it's not on noexec mount, except that on !MMU we need
2422 	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2423 	 */
2424 	if (!path_noexec(&file->f_path)) {
2425 #ifndef CONFIG_MMU
2426 		if (file->f_op->mmap_capabilities) {
2427 			unsigned caps = file->f_op->mmap_capabilities(file);
2428 			if (!(caps & NOMMU_MAP_EXEC))
2429 				return prot;
2430 		}
2431 #endif
2432 		return prot | PROT_EXEC;
2433 	}
2434 	/* anything on noexec mount won't get PROT_EXEC */
2435 	return prot;
2436 }
2437 
2438 int security_mmap_file(struct file *file, unsigned long prot,
2439 			unsigned long flags)
2440 {
2441 	unsigned long prot_adj = mmap_prot(file, prot);
2442 	int ret;
2443 
2444 	ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
2445 	if (ret)
2446 		return ret;
2447 	return ima_file_mmap(file, prot, prot_adj, flags);
2448 }
2449 
2450 int security_mmap_addr(unsigned long addr)
2451 {
2452 	return call_int_hook(mmap_addr, 0, addr);
2453 }
2454 
2455 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2456 			    unsigned long prot)
2457 {
2458 	int ret;
2459 
2460 	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
2461 	if (ret)
2462 		return ret;
2463 	return ima_file_mprotect(vma, prot);
2464 }
2465 
2466 int security_file_lock(struct file *file, unsigned int cmd)
2467 {
2468 	return call_int_hook(file_lock, 0, file, cmd);
2469 }
2470 
2471 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2472 {
2473 	return call_int_hook(file_fcntl, 0, file, cmd, arg);
2474 }
2475 
2476 void security_file_set_fowner(struct file *file)
2477 {
2478 	call_void_hook(file_set_fowner, file);
2479 }
2480 
2481 int security_file_send_sigiotask(struct task_struct *tsk,
2482 				  struct fown_struct *fown, int sig)
2483 {
2484 	return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
2485 }
2486 
2487 int security_file_receive(struct file *file)
2488 {
2489 	return call_int_hook(file_receive, 0, file);
2490 }
2491 
2492 int security_file_open(struct file *file)
2493 {
2494 	int ret;
2495 
2496 	ret = call_int_hook(file_open, 0, file);
2497 	if (ret)
2498 		return ret;
2499 
2500 	return fsnotify_perm(file, MAY_OPEN);
2501 }
2502 
2503 int security_file_truncate(struct file *file)
2504 {
2505 	return call_int_hook(file_truncate, 0, file);
2506 }
2507 
2508 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2509 {
2510 	int rc = lsm_task_alloc(task);
2511 
2512 	if (rc)
2513 		return rc;
2514 	rc = call_int_hook(task_alloc, 0, task, clone_flags);
2515 	if (unlikely(rc))
2516 		security_task_free(task);
2517 	return rc;
2518 }
2519 
2520 void security_task_free(struct task_struct *task)
2521 {
2522 	call_void_hook(task_free, task);
2523 
2524 	kfree(task->security);
2525 	task->security = NULL;
2526 }
2527 
2528 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2529 {
2530 	int rc = lsm_cred_alloc(cred, gfp);
2531 
2532 	if (rc)
2533 		return rc;
2534 
2535 	rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
2536 	if (unlikely(rc))
2537 		security_cred_free(cred);
2538 	return rc;
2539 }
2540 
2541 void security_cred_free(struct cred *cred)
2542 {
2543 	/*
2544 	 * There is a failure case in prepare_creds() that
2545 	 * may result in a call here with ->security being NULL.
2546 	 */
2547 	if (unlikely(cred->security == NULL))
2548 		return;
2549 
2550 	call_void_hook(cred_free, cred);
2551 
2552 	kfree(cred->security);
2553 	cred->security = NULL;
2554 }
2555 
2556 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
2557 {
2558 	int rc = lsm_cred_alloc(new, gfp);
2559 
2560 	if (rc)
2561 		return rc;
2562 
2563 	rc = call_int_hook(cred_prepare, 0, new, old, gfp);
2564 	if (unlikely(rc))
2565 		security_cred_free(new);
2566 	return rc;
2567 }
2568 
2569 void security_transfer_creds(struct cred *new, const struct cred *old)
2570 {
2571 	call_void_hook(cred_transfer, new, old);
2572 }
2573 
2574 void security_cred_getsecid(const struct cred *c, u32 *secid)
2575 {
2576 	*secid = 0;
2577 	call_void_hook(cred_getsecid, c, secid);
2578 }
2579 EXPORT_SYMBOL(security_cred_getsecid);
2580 
2581 int security_kernel_act_as(struct cred *new, u32 secid)
2582 {
2583 	return call_int_hook(kernel_act_as, 0, new, secid);
2584 }
2585 
2586 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
2587 {
2588 	return call_int_hook(kernel_create_files_as, 0, new, inode);
2589 }
2590 
2591 int security_kernel_module_request(char *kmod_name)
2592 {
2593 	int ret;
2594 
2595 	ret = call_int_hook(kernel_module_request, 0, kmod_name);
2596 	if (ret)
2597 		return ret;
2598 	return integrity_kernel_module_request(kmod_name);
2599 }
2600 
2601 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
2602 			      bool contents)
2603 {
2604 	int ret;
2605 
2606 	ret = call_int_hook(kernel_read_file, 0, file, id, contents);
2607 	if (ret)
2608 		return ret;
2609 	return ima_read_file(file, id, contents);
2610 }
2611 EXPORT_SYMBOL_GPL(security_kernel_read_file);
2612 
2613 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
2614 				   enum kernel_read_file_id id)
2615 {
2616 	int ret;
2617 
2618 	ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
2619 	if (ret)
2620 		return ret;
2621 	return ima_post_read_file(file, buf, size, id);
2622 }
2623 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
2624 
2625 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
2626 {
2627 	int ret;
2628 
2629 	ret = call_int_hook(kernel_load_data, 0, id, contents);
2630 	if (ret)
2631 		return ret;
2632 	return ima_load_data(id, contents);
2633 }
2634 EXPORT_SYMBOL_GPL(security_kernel_load_data);
2635 
2636 int security_kernel_post_load_data(char *buf, loff_t size,
2637 				   enum kernel_load_data_id id,
2638 				   char *description)
2639 {
2640 	int ret;
2641 
2642 	ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
2643 			    description);
2644 	if (ret)
2645 		return ret;
2646 	return ima_post_load_data(buf, size, id, description);
2647 }
2648 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
2649 
2650 int security_task_fix_setuid(struct cred *new, const struct cred *old,
2651 			     int flags)
2652 {
2653 	return call_int_hook(task_fix_setuid, 0, new, old, flags);
2654 }
2655 
2656 int security_task_fix_setgid(struct cred *new, const struct cred *old,
2657 				 int flags)
2658 {
2659 	return call_int_hook(task_fix_setgid, 0, new, old, flags);
2660 }
2661 
2662 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
2663 {
2664 	return call_int_hook(task_fix_setgroups, 0, new, old);
2665 }
2666 
2667 int security_task_setpgid(struct task_struct *p, pid_t pgid)
2668 {
2669 	return call_int_hook(task_setpgid, 0, p, pgid);
2670 }
2671 
2672 int security_task_getpgid(struct task_struct *p)
2673 {
2674 	return call_int_hook(task_getpgid, 0, p);
2675 }
2676 
2677 int security_task_getsid(struct task_struct *p)
2678 {
2679 	return call_int_hook(task_getsid, 0, p);
2680 }
2681 
2682 void security_current_getsecid_subj(u32 *secid)
2683 {
2684 	*secid = 0;
2685 	call_void_hook(current_getsecid_subj, secid);
2686 }
2687 EXPORT_SYMBOL(security_current_getsecid_subj);
2688 
2689 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
2690 {
2691 	*secid = 0;
2692 	call_void_hook(task_getsecid_obj, p, secid);
2693 }
2694 EXPORT_SYMBOL(security_task_getsecid_obj);
2695 
2696 int security_task_setnice(struct task_struct *p, int nice)
2697 {
2698 	return call_int_hook(task_setnice, 0, p, nice);
2699 }
2700 
2701 int security_task_setioprio(struct task_struct *p, int ioprio)
2702 {
2703 	return call_int_hook(task_setioprio, 0, p, ioprio);
2704 }
2705 
2706 int security_task_getioprio(struct task_struct *p)
2707 {
2708 	return call_int_hook(task_getioprio, 0, p);
2709 }
2710 
2711 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
2712 			  unsigned int flags)
2713 {
2714 	return call_int_hook(task_prlimit, 0, cred, tcred, flags);
2715 }
2716 
2717 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
2718 		struct rlimit *new_rlim)
2719 {
2720 	return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
2721 }
2722 
2723 int security_task_setscheduler(struct task_struct *p)
2724 {
2725 	return call_int_hook(task_setscheduler, 0, p);
2726 }
2727 
2728 int security_task_getscheduler(struct task_struct *p)
2729 {
2730 	return call_int_hook(task_getscheduler, 0, p);
2731 }
2732 
2733 int security_task_movememory(struct task_struct *p)
2734 {
2735 	return call_int_hook(task_movememory, 0, p);
2736 }
2737 
2738 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2739 			int sig, const struct cred *cred)
2740 {
2741 	return call_int_hook(task_kill, 0, p, info, sig, cred);
2742 }
2743 
2744 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
2745 			 unsigned long arg4, unsigned long arg5)
2746 {
2747 	int thisrc;
2748 	int rc = LSM_RET_DEFAULT(task_prctl);
2749 	struct security_hook_list *hp;
2750 
2751 	hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
2752 		thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
2753 		if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
2754 			rc = thisrc;
2755 			if (thisrc != 0)
2756 				break;
2757 		}
2758 	}
2759 	return rc;
2760 }
2761 
2762 void security_task_to_inode(struct task_struct *p, struct inode *inode)
2763 {
2764 	call_void_hook(task_to_inode, p, inode);
2765 }
2766 
2767 int security_create_user_ns(const struct cred *cred)
2768 {
2769 	return call_int_hook(userns_create, 0, cred);
2770 }
2771 
2772 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
2773 {
2774 	return call_int_hook(ipc_permission, 0, ipcp, flag);
2775 }
2776 
2777 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
2778 {
2779 	*secid = 0;
2780 	call_void_hook(ipc_getsecid, ipcp, secid);
2781 }
2782 
2783 int security_msg_msg_alloc(struct msg_msg *msg)
2784 {
2785 	int rc = lsm_msg_msg_alloc(msg);
2786 
2787 	if (unlikely(rc))
2788 		return rc;
2789 	rc = call_int_hook(msg_msg_alloc_security, 0, msg);
2790 	if (unlikely(rc))
2791 		security_msg_msg_free(msg);
2792 	return rc;
2793 }
2794 
2795 void security_msg_msg_free(struct msg_msg *msg)
2796 {
2797 	call_void_hook(msg_msg_free_security, msg);
2798 	kfree(msg->security);
2799 	msg->security = NULL;
2800 }
2801 
2802 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
2803 {
2804 	int rc = lsm_ipc_alloc(msq);
2805 
2806 	if (unlikely(rc))
2807 		return rc;
2808 	rc = call_int_hook(msg_queue_alloc_security, 0, msq);
2809 	if (unlikely(rc))
2810 		security_msg_queue_free(msq);
2811 	return rc;
2812 }
2813 
2814 void security_msg_queue_free(struct kern_ipc_perm *msq)
2815 {
2816 	call_void_hook(msg_queue_free_security, msq);
2817 	kfree(msq->security);
2818 	msq->security = NULL;
2819 }
2820 
2821 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
2822 {
2823 	return call_int_hook(msg_queue_associate, 0, msq, msqflg);
2824 }
2825 
2826 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
2827 {
2828 	return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
2829 }
2830 
2831 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
2832 			       struct msg_msg *msg, int msqflg)
2833 {
2834 	return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
2835 }
2836 
2837 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
2838 			       struct task_struct *target, long type, int mode)
2839 {
2840 	return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
2841 }
2842 
2843 int security_shm_alloc(struct kern_ipc_perm *shp)
2844 {
2845 	int rc = lsm_ipc_alloc(shp);
2846 
2847 	if (unlikely(rc))
2848 		return rc;
2849 	rc = call_int_hook(shm_alloc_security, 0, shp);
2850 	if (unlikely(rc))
2851 		security_shm_free(shp);
2852 	return rc;
2853 }
2854 
2855 void security_shm_free(struct kern_ipc_perm *shp)
2856 {
2857 	call_void_hook(shm_free_security, shp);
2858 	kfree(shp->security);
2859 	shp->security = NULL;
2860 }
2861 
2862 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
2863 {
2864 	return call_int_hook(shm_associate, 0, shp, shmflg);
2865 }
2866 
2867 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
2868 {
2869 	return call_int_hook(shm_shmctl, 0, shp, cmd);
2870 }
2871 
2872 int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
2873 {
2874 	return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
2875 }
2876 
2877 int security_sem_alloc(struct kern_ipc_perm *sma)
2878 {
2879 	int rc = lsm_ipc_alloc(sma);
2880 
2881 	if (unlikely(rc))
2882 		return rc;
2883 	rc = call_int_hook(sem_alloc_security, 0, sma);
2884 	if (unlikely(rc))
2885 		security_sem_free(sma);
2886 	return rc;
2887 }
2888 
2889 void security_sem_free(struct kern_ipc_perm *sma)
2890 {
2891 	call_void_hook(sem_free_security, sma);
2892 	kfree(sma->security);
2893 	sma->security = NULL;
2894 }
2895 
2896 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
2897 {
2898 	return call_int_hook(sem_associate, 0, sma, semflg);
2899 }
2900 
2901 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
2902 {
2903 	return call_int_hook(sem_semctl, 0, sma, cmd);
2904 }
2905 
2906 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
2907 			unsigned nsops, int alter)
2908 {
2909 	return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
2910 }
2911 
2912 /**
2913  * security_d_instantiate() - Populate an inode's LSM state based on a dentry
2914  * @dentry: dentry
2915  * @inode: inode
2916  *
2917  * Fill in @inode security information for a @dentry if allowed.
2918  */
2919 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
2920 {
2921 	if (unlikely(inode && IS_PRIVATE(inode)))
2922 		return;
2923 	call_void_hook(d_instantiate, dentry, inode);
2924 }
2925 EXPORT_SYMBOL(security_d_instantiate);
2926 
2927 /**
2928  * security_getprocattr() - Read an attribute for a task
2929  * @p: the task
2930  * @lsm: LSM name
2931  * @name: attribute name
2932  * @value: attribute value
2933  *
2934  * Read attribute @name for task @p and store it into @value if allowed.
2935  *
2936  * Return: Returns the length of @value on success, a negative value otherwise.
2937  */
2938 int security_getprocattr(struct task_struct *p, const char *lsm,
2939 			 const char *name, char **value)
2940 {
2941 	struct security_hook_list *hp;
2942 
2943 	hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
2944 		if (lsm != NULL && strcmp(lsm, hp->lsm))
2945 			continue;
2946 		return hp->hook.getprocattr(p, name, value);
2947 	}
2948 	return LSM_RET_DEFAULT(getprocattr);
2949 }
2950 
2951 /**
2952  * security_setprocattr() - Set an attribute for a task
2953  * @lsm: LSM name
2954  * @name: attribute name
2955  * @value: attribute value
2956  * @size: attribute value size
2957  *
2958  * Write (set) the current task's attribute @name to @value, size @size if
2959  * allowed.
2960  *
2961  * Return: Returns bytes written on success, a negative value otherwise.
2962  */
2963 int security_setprocattr(const char *lsm, const char *name, void *value,
2964 			 size_t size)
2965 {
2966 	struct security_hook_list *hp;
2967 
2968 	hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
2969 		if (lsm != NULL && strcmp(lsm, hp->lsm))
2970 			continue;
2971 		return hp->hook.setprocattr(name, value, size);
2972 	}
2973 	return LSM_RET_DEFAULT(setprocattr);
2974 }
2975 
2976 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
2977 {
2978 	return call_int_hook(netlink_send, 0, sk, skb);
2979 }
2980 
2981 int security_ismaclabel(const char *name)
2982 {
2983 	return call_int_hook(ismaclabel, 0, name);
2984 }
2985 EXPORT_SYMBOL(security_ismaclabel);
2986 
2987 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2988 {
2989 	struct security_hook_list *hp;
2990 	int rc;
2991 
2992 	/*
2993 	 * Currently, only one LSM can implement secid_to_secctx (i.e this
2994 	 * LSM hook is not "stackable").
2995 	 */
2996 	hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
2997 		rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
2998 		if (rc != LSM_RET_DEFAULT(secid_to_secctx))
2999 			return rc;
3000 	}
3001 
3002 	return LSM_RET_DEFAULT(secid_to_secctx);
3003 }
3004 EXPORT_SYMBOL(security_secid_to_secctx);
3005 
3006 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3007 {
3008 	*secid = 0;
3009 	return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
3010 }
3011 EXPORT_SYMBOL(security_secctx_to_secid);
3012 
3013 void security_release_secctx(char *secdata, u32 seclen)
3014 {
3015 	call_void_hook(release_secctx, secdata, seclen);
3016 }
3017 EXPORT_SYMBOL(security_release_secctx);
3018 
3019 void security_inode_invalidate_secctx(struct inode *inode)
3020 {
3021 	call_void_hook(inode_invalidate_secctx, inode);
3022 }
3023 EXPORT_SYMBOL(security_inode_invalidate_secctx);
3024 
3025 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3026 {
3027 	return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
3028 }
3029 EXPORT_SYMBOL(security_inode_notifysecctx);
3030 
3031 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3032 {
3033 	return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
3034 }
3035 EXPORT_SYMBOL(security_inode_setsecctx);
3036 
3037 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3038 {
3039 	return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
3040 }
3041 EXPORT_SYMBOL(security_inode_getsecctx);
3042 
3043 #ifdef CONFIG_WATCH_QUEUE
3044 int security_post_notification(const struct cred *w_cred,
3045 			       const struct cred *cred,
3046 			       struct watch_notification *n)
3047 {
3048 	return call_int_hook(post_notification, 0, w_cred, cred, n);
3049 }
3050 #endif /* CONFIG_WATCH_QUEUE */
3051 
3052 #ifdef CONFIG_KEY_NOTIFICATIONS
3053 int security_watch_key(struct key *key)
3054 {
3055 	return call_int_hook(watch_key, 0, key);
3056 }
3057 #endif
3058 
3059 #ifdef CONFIG_SECURITY_NETWORK
3060 
3061 int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
3062 {
3063 	return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
3064 }
3065 EXPORT_SYMBOL(security_unix_stream_connect);
3066 
3067 int security_unix_may_send(struct socket *sock,  struct socket *other)
3068 {
3069 	return call_int_hook(unix_may_send, 0, sock, other);
3070 }
3071 EXPORT_SYMBOL(security_unix_may_send);
3072 
3073 int security_socket_create(int family, int type, int protocol, int kern)
3074 {
3075 	return call_int_hook(socket_create, 0, family, type, protocol, kern);
3076 }
3077 
3078 int security_socket_post_create(struct socket *sock, int family,
3079 				int type, int protocol, int kern)
3080 {
3081 	return call_int_hook(socket_post_create, 0, sock, family, type,
3082 						protocol, kern);
3083 }
3084 
3085 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
3086 {
3087 	return call_int_hook(socket_socketpair, 0, socka, sockb);
3088 }
3089 EXPORT_SYMBOL(security_socket_socketpair);
3090 
3091 int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3092 {
3093 	return call_int_hook(socket_bind, 0, sock, address, addrlen);
3094 }
3095 
3096 int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3097 {
3098 	return call_int_hook(socket_connect, 0, sock, address, addrlen);
3099 }
3100 
3101 int security_socket_listen(struct socket *sock, int backlog)
3102 {
3103 	return call_int_hook(socket_listen, 0, sock, backlog);
3104 }
3105 
3106 int security_socket_accept(struct socket *sock, struct socket *newsock)
3107 {
3108 	return call_int_hook(socket_accept, 0, sock, newsock);
3109 }
3110 
3111 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
3112 {
3113 	return call_int_hook(socket_sendmsg, 0, sock, msg, size);
3114 }
3115 
3116 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
3117 			    int size, int flags)
3118 {
3119 	return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
3120 }
3121 
3122 int security_socket_getsockname(struct socket *sock)
3123 {
3124 	return call_int_hook(socket_getsockname, 0, sock);
3125 }
3126 
3127 int security_socket_getpeername(struct socket *sock)
3128 {
3129 	return call_int_hook(socket_getpeername, 0, sock);
3130 }
3131 
3132 int security_socket_getsockopt(struct socket *sock, int level, int optname)
3133 {
3134 	return call_int_hook(socket_getsockopt, 0, sock, level, optname);
3135 }
3136 
3137 int security_socket_setsockopt(struct socket *sock, int level, int optname)
3138 {
3139 	return call_int_hook(socket_setsockopt, 0, sock, level, optname);
3140 }
3141 
3142 int security_socket_shutdown(struct socket *sock, int how)
3143 {
3144 	return call_int_hook(socket_shutdown, 0, sock, how);
3145 }
3146 
3147 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3148 {
3149 	return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
3150 }
3151 EXPORT_SYMBOL(security_sock_rcv_skb);
3152 
3153 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
3154 				      sockptr_t optlen, unsigned int len)
3155 {
3156 	return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
3157 			     optval, optlen, len);
3158 }
3159 
3160 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
3161 {
3162 	return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
3163 			     skb, secid);
3164 }
3165 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
3166 
3167 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
3168 {
3169 	return call_int_hook(sk_alloc_security, 0, sk, family, priority);
3170 }
3171 
3172 void security_sk_free(struct sock *sk)
3173 {
3174 	call_void_hook(sk_free_security, sk);
3175 }
3176 
3177 void security_sk_clone(const struct sock *sk, struct sock *newsk)
3178 {
3179 	call_void_hook(sk_clone_security, sk, newsk);
3180 }
3181 EXPORT_SYMBOL(security_sk_clone);
3182 
3183 void security_sk_classify_flow(struct sock *sk, struct flowi_common *flic)
3184 {
3185 	call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
3186 }
3187 EXPORT_SYMBOL(security_sk_classify_flow);
3188 
3189 void security_req_classify_flow(const struct request_sock *req,
3190 				struct flowi_common *flic)
3191 {
3192 	call_void_hook(req_classify_flow, req, flic);
3193 }
3194 EXPORT_SYMBOL(security_req_classify_flow);
3195 
3196 void security_sock_graft(struct sock *sk, struct socket *parent)
3197 {
3198 	call_void_hook(sock_graft, sk, parent);
3199 }
3200 EXPORT_SYMBOL(security_sock_graft);
3201 
3202 int security_inet_conn_request(const struct sock *sk,
3203 			struct sk_buff *skb, struct request_sock *req)
3204 {
3205 	return call_int_hook(inet_conn_request, 0, sk, skb, req);
3206 }
3207 EXPORT_SYMBOL(security_inet_conn_request);
3208 
3209 void security_inet_csk_clone(struct sock *newsk,
3210 			const struct request_sock *req)
3211 {
3212 	call_void_hook(inet_csk_clone, newsk, req);
3213 }
3214 
3215 void security_inet_conn_established(struct sock *sk,
3216 			struct sk_buff *skb)
3217 {
3218 	call_void_hook(inet_conn_established, sk, skb);
3219 }
3220 EXPORT_SYMBOL(security_inet_conn_established);
3221 
3222 int security_secmark_relabel_packet(u32 secid)
3223 {
3224 	return call_int_hook(secmark_relabel_packet, 0, secid);
3225 }
3226 EXPORT_SYMBOL(security_secmark_relabel_packet);
3227 
3228 void security_secmark_refcount_inc(void)
3229 {
3230 	call_void_hook(secmark_refcount_inc);
3231 }
3232 EXPORT_SYMBOL(security_secmark_refcount_inc);
3233 
3234 void security_secmark_refcount_dec(void)
3235 {
3236 	call_void_hook(secmark_refcount_dec);
3237 }
3238 EXPORT_SYMBOL(security_secmark_refcount_dec);
3239 
3240 int security_tun_dev_alloc_security(void **security)
3241 {
3242 	return call_int_hook(tun_dev_alloc_security, 0, security);
3243 }
3244 EXPORT_SYMBOL(security_tun_dev_alloc_security);
3245 
3246 void security_tun_dev_free_security(void *security)
3247 {
3248 	call_void_hook(tun_dev_free_security, security);
3249 }
3250 EXPORT_SYMBOL(security_tun_dev_free_security);
3251 
3252 int security_tun_dev_create(void)
3253 {
3254 	return call_int_hook(tun_dev_create, 0);
3255 }
3256 EXPORT_SYMBOL(security_tun_dev_create);
3257 
3258 int security_tun_dev_attach_queue(void *security)
3259 {
3260 	return call_int_hook(tun_dev_attach_queue, 0, security);
3261 }
3262 EXPORT_SYMBOL(security_tun_dev_attach_queue);
3263 
3264 int security_tun_dev_attach(struct sock *sk, void *security)
3265 {
3266 	return call_int_hook(tun_dev_attach, 0, sk, security);
3267 }
3268 EXPORT_SYMBOL(security_tun_dev_attach);
3269 
3270 int security_tun_dev_open(void *security)
3271 {
3272 	return call_int_hook(tun_dev_open, 0, security);
3273 }
3274 EXPORT_SYMBOL(security_tun_dev_open);
3275 
3276 int security_sctp_assoc_request(struct sctp_association *asoc, struct sk_buff *skb)
3277 {
3278 	return call_int_hook(sctp_assoc_request, 0, asoc, skb);
3279 }
3280 EXPORT_SYMBOL(security_sctp_assoc_request);
3281 
3282 int security_sctp_bind_connect(struct sock *sk, int optname,
3283 			       struct sockaddr *address, int addrlen)
3284 {
3285 	return call_int_hook(sctp_bind_connect, 0, sk, optname,
3286 			     address, addrlen);
3287 }
3288 EXPORT_SYMBOL(security_sctp_bind_connect);
3289 
3290 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
3291 			    struct sock *newsk)
3292 {
3293 	call_void_hook(sctp_sk_clone, asoc, sk, newsk);
3294 }
3295 EXPORT_SYMBOL(security_sctp_sk_clone);
3296 
3297 int security_sctp_assoc_established(struct sctp_association *asoc,
3298 				    struct sk_buff *skb)
3299 {
3300 	return call_int_hook(sctp_assoc_established, 0, asoc, skb);
3301 }
3302 EXPORT_SYMBOL(security_sctp_assoc_established);
3303 
3304 #endif	/* CONFIG_SECURITY_NETWORK */
3305 
3306 #ifdef CONFIG_SECURITY_INFINIBAND
3307 
3308 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
3309 {
3310 	return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
3311 }
3312 EXPORT_SYMBOL(security_ib_pkey_access);
3313 
3314 int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
3315 {
3316 	return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
3317 }
3318 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
3319 
3320 int security_ib_alloc_security(void **sec)
3321 {
3322 	return call_int_hook(ib_alloc_security, 0, sec);
3323 }
3324 EXPORT_SYMBOL(security_ib_alloc_security);
3325 
3326 void security_ib_free_security(void *sec)
3327 {
3328 	call_void_hook(ib_free_security, sec);
3329 }
3330 EXPORT_SYMBOL(security_ib_free_security);
3331 #endif	/* CONFIG_SECURITY_INFINIBAND */
3332 
3333 #ifdef CONFIG_SECURITY_NETWORK_XFRM
3334 
3335 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
3336 			       struct xfrm_user_sec_ctx *sec_ctx,
3337 			       gfp_t gfp)
3338 {
3339 	return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
3340 }
3341 EXPORT_SYMBOL(security_xfrm_policy_alloc);
3342 
3343 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
3344 			      struct xfrm_sec_ctx **new_ctxp)
3345 {
3346 	return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
3347 }
3348 
3349 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
3350 {
3351 	call_void_hook(xfrm_policy_free_security, ctx);
3352 }
3353 EXPORT_SYMBOL(security_xfrm_policy_free);
3354 
3355 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
3356 {
3357 	return call_int_hook(xfrm_policy_delete_security, 0, ctx);
3358 }
3359 
3360 int security_xfrm_state_alloc(struct xfrm_state *x,
3361 			      struct xfrm_user_sec_ctx *sec_ctx)
3362 {
3363 	return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
3364 }
3365 EXPORT_SYMBOL(security_xfrm_state_alloc);
3366 
3367 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
3368 				      struct xfrm_sec_ctx *polsec, u32 secid)
3369 {
3370 	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
3371 }
3372 
3373 int security_xfrm_state_delete(struct xfrm_state *x)
3374 {
3375 	return call_int_hook(xfrm_state_delete_security, 0, x);
3376 }
3377 EXPORT_SYMBOL(security_xfrm_state_delete);
3378 
3379 void security_xfrm_state_free(struct xfrm_state *x)
3380 {
3381 	call_void_hook(xfrm_state_free_security, x);
3382 }
3383 
3384 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
3385 {
3386 	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
3387 }
3388 
3389 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
3390 				       struct xfrm_policy *xp,
3391 				       const struct flowi_common *flic)
3392 {
3393 	struct security_hook_list *hp;
3394 	int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
3395 
3396 	/*
3397 	 * Since this function is expected to return 0 or 1, the judgment
3398 	 * becomes difficult if multiple LSMs supply this call. Fortunately,
3399 	 * we can use the first LSM's judgment because currently only SELinux
3400 	 * supplies this call.
3401 	 *
3402 	 * For speed optimization, we explicitly break the loop rather than
3403 	 * using the macro
3404 	 */
3405 	hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
3406 				list) {
3407 		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
3408 		break;
3409 	}
3410 	return rc;
3411 }
3412 
3413 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
3414 {
3415 	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
3416 }
3417 
3418 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
3419 {
3420 	int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
3421 				0);
3422 
3423 	BUG_ON(rc);
3424 }
3425 EXPORT_SYMBOL(security_skb_classify_flow);
3426 
3427 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
3428 
3429 #ifdef CONFIG_KEYS
3430 
3431 int security_key_alloc(struct key *key, const struct cred *cred,
3432 		       unsigned long flags)
3433 {
3434 	return call_int_hook(key_alloc, 0, key, cred, flags);
3435 }
3436 
3437 void security_key_free(struct key *key)
3438 {
3439 	call_void_hook(key_free, key);
3440 }
3441 
3442 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
3443 			    enum key_need_perm need_perm)
3444 {
3445 	return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
3446 }
3447 
3448 int security_key_getsecurity(struct key *key, char **_buffer)
3449 {
3450 	*_buffer = NULL;
3451 	return call_int_hook(key_getsecurity, 0, key, _buffer);
3452 }
3453 
3454 #endif	/* CONFIG_KEYS */
3455 
3456 #ifdef CONFIG_AUDIT
3457 
3458 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
3459 {
3460 	return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
3461 }
3462 
3463 int security_audit_rule_known(struct audit_krule *krule)
3464 {
3465 	return call_int_hook(audit_rule_known, 0, krule);
3466 }
3467 
3468 void security_audit_rule_free(void *lsmrule)
3469 {
3470 	call_void_hook(audit_rule_free, lsmrule);
3471 }
3472 
3473 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
3474 {
3475 	return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
3476 }
3477 #endif /* CONFIG_AUDIT */
3478 
3479 #ifdef CONFIG_BPF_SYSCALL
3480 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
3481 {
3482 	return call_int_hook(bpf, 0, cmd, attr, size);
3483 }
3484 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
3485 {
3486 	return call_int_hook(bpf_map, 0, map, fmode);
3487 }
3488 int security_bpf_prog(struct bpf_prog *prog)
3489 {
3490 	return call_int_hook(bpf_prog, 0, prog);
3491 }
3492 int security_bpf_map_alloc(struct bpf_map *map)
3493 {
3494 	return call_int_hook(bpf_map_alloc_security, 0, map);
3495 }
3496 int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
3497 {
3498 	return call_int_hook(bpf_prog_alloc_security, 0, aux);
3499 }
3500 void security_bpf_map_free(struct bpf_map *map)
3501 {
3502 	call_void_hook(bpf_map_free_security, map);
3503 }
3504 void security_bpf_prog_free(struct bpf_prog_aux *aux)
3505 {
3506 	call_void_hook(bpf_prog_free_security, aux);
3507 }
3508 #endif /* CONFIG_BPF_SYSCALL */
3509 
3510 int security_locked_down(enum lockdown_reason what)
3511 {
3512 	return call_int_hook(locked_down, 0, what);
3513 }
3514 EXPORT_SYMBOL(security_locked_down);
3515 
3516 #ifdef CONFIG_PERF_EVENTS
3517 int security_perf_event_open(struct perf_event_attr *attr, int type)
3518 {
3519 	return call_int_hook(perf_event_open, 0, attr, type);
3520 }
3521 
3522 int security_perf_event_alloc(struct perf_event *event)
3523 {
3524 	return call_int_hook(perf_event_alloc, 0, event);
3525 }
3526 
3527 void security_perf_event_free(struct perf_event *event)
3528 {
3529 	call_void_hook(perf_event_free, event);
3530 }
3531 
3532 int security_perf_event_read(struct perf_event *event)
3533 {
3534 	return call_int_hook(perf_event_read, 0, event);
3535 }
3536 
3537 int security_perf_event_write(struct perf_event *event)
3538 {
3539 	return call_int_hook(perf_event_write, 0, event);
3540 }
3541 #endif /* CONFIG_PERF_EVENTS */
3542 
3543 #ifdef CONFIG_IO_URING
3544 int security_uring_override_creds(const struct cred *new)
3545 {
3546 	return call_int_hook(uring_override_creds, 0, new);
3547 }
3548 
3549 int security_uring_sqpoll(void)
3550 {
3551 	return call_int_hook(uring_sqpoll, 0);
3552 }
3553 int security_uring_cmd(struct io_uring_cmd *ioucmd)
3554 {
3555 	return call_int_hook(uring_cmd, 0, ioucmd);
3556 }
3557 #endif /* CONFIG_IO_URING */
3558