xref: /openbmc/linux/security/security.c (revision 36819f18)
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 int security_sb_alloc(struct super_block *sb)
1029 {
1030 	int rc = lsm_superblock_alloc(sb);
1031 
1032 	if (unlikely(rc))
1033 		return rc;
1034 	rc = call_int_hook(sb_alloc_security, 0, sb);
1035 	if (unlikely(rc))
1036 		security_sb_free(sb);
1037 	return rc;
1038 }
1039 
1040 void security_sb_delete(struct super_block *sb)
1041 {
1042 	call_void_hook(sb_delete, sb);
1043 }
1044 
1045 void security_sb_free(struct super_block *sb)
1046 {
1047 	call_void_hook(sb_free_security, sb);
1048 	kfree(sb->s_security);
1049 	sb->s_security = NULL;
1050 }
1051 
1052 void security_free_mnt_opts(void **mnt_opts)
1053 {
1054 	if (!*mnt_opts)
1055 		return;
1056 	call_void_hook(sb_free_mnt_opts, *mnt_opts);
1057 	*mnt_opts = NULL;
1058 }
1059 EXPORT_SYMBOL(security_free_mnt_opts);
1060 
1061 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1062 {
1063 	return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
1064 }
1065 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1066 
1067 int security_sb_mnt_opts_compat(struct super_block *sb,
1068 				void *mnt_opts)
1069 {
1070 	return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
1071 }
1072 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1073 
1074 int security_sb_remount(struct super_block *sb,
1075 			void *mnt_opts)
1076 {
1077 	return call_int_hook(sb_remount, 0, sb, mnt_opts);
1078 }
1079 EXPORT_SYMBOL(security_sb_remount);
1080 
1081 int security_sb_kern_mount(struct super_block *sb)
1082 {
1083 	return call_int_hook(sb_kern_mount, 0, sb);
1084 }
1085 
1086 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1087 {
1088 	return call_int_hook(sb_show_options, 0, m, sb);
1089 }
1090 
1091 int security_sb_statfs(struct dentry *dentry)
1092 {
1093 	return call_int_hook(sb_statfs, 0, dentry);
1094 }
1095 
1096 int security_sb_mount(const char *dev_name, const struct path *path,
1097                        const char *type, unsigned long flags, void *data)
1098 {
1099 	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
1100 }
1101 
1102 int security_sb_umount(struct vfsmount *mnt, int flags)
1103 {
1104 	return call_int_hook(sb_umount, 0, mnt, flags);
1105 }
1106 
1107 int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
1108 {
1109 	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
1110 }
1111 
1112 int security_sb_set_mnt_opts(struct super_block *sb,
1113 				void *mnt_opts,
1114 				unsigned long kern_flags,
1115 				unsigned long *set_kern_flags)
1116 {
1117 	return call_int_hook(sb_set_mnt_opts,
1118 				mnt_opts ? -EOPNOTSUPP : 0, sb,
1119 				mnt_opts, kern_flags, set_kern_flags);
1120 }
1121 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1122 
1123 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1124 				struct super_block *newsb,
1125 				unsigned long kern_flags,
1126 				unsigned long *set_kern_flags)
1127 {
1128 	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
1129 				kern_flags, set_kern_flags);
1130 }
1131 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1132 
1133 int security_move_mount(const struct path *from_path, const struct path *to_path)
1134 {
1135 	return call_int_hook(move_mount, 0, from_path, to_path);
1136 }
1137 
1138 int security_path_notify(const struct path *path, u64 mask,
1139 				unsigned int obj_type)
1140 {
1141 	return call_int_hook(path_notify, 0, path, mask, obj_type);
1142 }
1143 
1144 int security_inode_alloc(struct inode *inode)
1145 {
1146 	int rc = lsm_inode_alloc(inode);
1147 
1148 	if (unlikely(rc))
1149 		return rc;
1150 	rc = call_int_hook(inode_alloc_security, 0, inode);
1151 	if (unlikely(rc))
1152 		security_inode_free(inode);
1153 	return rc;
1154 }
1155 
1156 static void inode_free_by_rcu(struct rcu_head *head)
1157 {
1158 	/*
1159 	 * The rcu head is at the start of the inode blob
1160 	 */
1161 	kmem_cache_free(lsm_inode_cache, head);
1162 }
1163 
1164 void security_inode_free(struct inode *inode)
1165 {
1166 	integrity_inode_free(inode);
1167 	call_void_hook(inode_free_security, inode);
1168 	/*
1169 	 * The inode may still be referenced in a path walk and
1170 	 * a call to security_inode_permission() can be made
1171 	 * after inode_free_security() is called. Ideally, the VFS
1172 	 * wouldn't do this, but fixing that is a much harder
1173 	 * job. For now, simply free the i_security via RCU, and
1174 	 * leave the current inode->i_security pointer intact.
1175 	 * The inode will be freed after the RCU grace period too.
1176 	 */
1177 	if (inode->i_security)
1178 		call_rcu((struct rcu_head *)inode->i_security,
1179 				inode_free_by_rcu);
1180 }
1181 
1182 int security_dentry_init_security(struct dentry *dentry, int mode,
1183 				  const struct qstr *name,
1184 				  const char **xattr_name, void **ctx,
1185 				  u32 *ctxlen)
1186 {
1187 	struct security_hook_list *hp;
1188 	int rc;
1189 
1190 	/*
1191 	 * Only one module will provide a security context.
1192 	 */
1193 	hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security, list) {
1194 		rc = hp->hook.dentry_init_security(dentry, mode, name,
1195 						   xattr_name, ctx, ctxlen);
1196 		if (rc != LSM_RET_DEFAULT(dentry_init_security))
1197 			return rc;
1198 	}
1199 	return LSM_RET_DEFAULT(dentry_init_security);
1200 }
1201 EXPORT_SYMBOL(security_dentry_init_security);
1202 
1203 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1204 				    struct qstr *name,
1205 				    const struct cred *old, struct cred *new)
1206 {
1207 	return call_int_hook(dentry_create_files_as, 0, dentry, mode,
1208 				name, old, new);
1209 }
1210 EXPORT_SYMBOL(security_dentry_create_files_as);
1211 
1212 int security_inode_init_security(struct inode *inode, struct inode *dir,
1213 				 const struct qstr *qstr,
1214 				 const initxattrs initxattrs, void *fs_data)
1215 {
1216 	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
1217 	struct xattr *lsm_xattr, *evm_xattr, *xattr;
1218 	int ret;
1219 
1220 	if (unlikely(IS_PRIVATE(inode)))
1221 		return 0;
1222 
1223 	if (!initxattrs)
1224 		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
1225 				     dir, qstr, NULL, NULL, NULL);
1226 	memset(new_xattrs, 0, sizeof(new_xattrs));
1227 	lsm_xattr = new_xattrs;
1228 	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
1229 						&lsm_xattr->name,
1230 						&lsm_xattr->value,
1231 						&lsm_xattr->value_len);
1232 	if (ret)
1233 		goto out;
1234 
1235 	evm_xattr = lsm_xattr + 1;
1236 	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1237 	if (ret)
1238 		goto out;
1239 	ret = initxattrs(inode, new_xattrs, fs_data);
1240 out:
1241 	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
1242 		kfree(xattr->value);
1243 	return (ret == -EOPNOTSUPP) ? 0 : ret;
1244 }
1245 EXPORT_SYMBOL(security_inode_init_security);
1246 
1247 int security_inode_init_security_anon(struct inode *inode,
1248 				      const struct qstr *name,
1249 				      const struct inode *context_inode)
1250 {
1251 	return call_int_hook(inode_init_security_anon, 0, inode, name,
1252 			     context_inode);
1253 }
1254 
1255 int security_old_inode_init_security(struct inode *inode, struct inode *dir,
1256 				     const struct qstr *qstr, const char **name,
1257 				     void **value, size_t *len)
1258 {
1259 	if (unlikely(IS_PRIVATE(inode)))
1260 		return -EOPNOTSUPP;
1261 	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1262 			     qstr, name, value, len);
1263 }
1264 EXPORT_SYMBOL(security_old_inode_init_security);
1265 
1266 #ifdef CONFIG_SECURITY_PATH
1267 int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
1268 			unsigned int dev)
1269 {
1270 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1271 		return 0;
1272 	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1273 }
1274 EXPORT_SYMBOL(security_path_mknod);
1275 
1276 int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
1277 {
1278 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1279 		return 0;
1280 	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1281 }
1282 EXPORT_SYMBOL(security_path_mkdir);
1283 
1284 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1285 {
1286 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1287 		return 0;
1288 	return call_int_hook(path_rmdir, 0, dir, dentry);
1289 }
1290 
1291 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1292 {
1293 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1294 		return 0;
1295 	return call_int_hook(path_unlink, 0, dir, dentry);
1296 }
1297 EXPORT_SYMBOL(security_path_unlink);
1298 
1299 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1300 			  const char *old_name)
1301 {
1302 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1303 		return 0;
1304 	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1305 }
1306 
1307 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1308 		       struct dentry *new_dentry)
1309 {
1310 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1311 		return 0;
1312 	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1313 }
1314 
1315 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1316 			 const struct path *new_dir, struct dentry *new_dentry,
1317 			 unsigned int flags)
1318 {
1319 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1320 		     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1321 		return 0;
1322 
1323 	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1324 				new_dentry, flags);
1325 }
1326 EXPORT_SYMBOL(security_path_rename);
1327 
1328 int security_path_truncate(const struct path *path)
1329 {
1330 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1331 		return 0;
1332 	return call_int_hook(path_truncate, 0, path);
1333 }
1334 
1335 int security_path_chmod(const struct path *path, umode_t mode)
1336 {
1337 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1338 		return 0;
1339 	return call_int_hook(path_chmod, 0, path, mode);
1340 }
1341 
1342 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1343 {
1344 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1345 		return 0;
1346 	return call_int_hook(path_chown, 0, path, uid, gid);
1347 }
1348 
1349 int security_path_chroot(const struct path *path)
1350 {
1351 	return call_int_hook(path_chroot, 0, path);
1352 }
1353 #endif
1354 
1355 int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1356 {
1357 	if (unlikely(IS_PRIVATE(dir)))
1358 		return 0;
1359 	return call_int_hook(inode_create, 0, dir, dentry, mode);
1360 }
1361 EXPORT_SYMBOL_GPL(security_inode_create);
1362 
1363 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1364 			 struct dentry *new_dentry)
1365 {
1366 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1367 		return 0;
1368 	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
1369 }
1370 
1371 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1372 {
1373 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1374 		return 0;
1375 	return call_int_hook(inode_unlink, 0, dir, dentry);
1376 }
1377 
1378 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1379 			    const char *old_name)
1380 {
1381 	if (unlikely(IS_PRIVATE(dir)))
1382 		return 0;
1383 	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
1384 }
1385 
1386 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1387 {
1388 	if (unlikely(IS_PRIVATE(dir)))
1389 		return 0;
1390 	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
1391 }
1392 EXPORT_SYMBOL_GPL(security_inode_mkdir);
1393 
1394 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1395 {
1396 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1397 		return 0;
1398 	return call_int_hook(inode_rmdir, 0, dir, dentry);
1399 }
1400 
1401 int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1402 {
1403 	if (unlikely(IS_PRIVATE(dir)))
1404 		return 0;
1405 	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
1406 }
1407 
1408 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
1409 			   struct inode *new_dir, struct dentry *new_dentry,
1410 			   unsigned int flags)
1411 {
1412         if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1413             (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1414 		return 0;
1415 
1416 	if (flags & RENAME_EXCHANGE) {
1417 		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
1418 						     old_dir, old_dentry);
1419 		if (err)
1420 			return err;
1421 	}
1422 
1423 	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
1424 					   new_dir, new_dentry);
1425 }
1426 
1427 int security_inode_readlink(struct dentry *dentry)
1428 {
1429 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1430 		return 0;
1431 	return call_int_hook(inode_readlink, 0, dentry);
1432 }
1433 
1434 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1435 			       bool rcu)
1436 {
1437 	if (unlikely(IS_PRIVATE(inode)))
1438 		return 0;
1439 	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
1440 }
1441 
1442 int security_inode_permission(struct inode *inode, int mask)
1443 {
1444 	if (unlikely(IS_PRIVATE(inode)))
1445 		return 0;
1446 	return call_int_hook(inode_permission, 0, inode, mask);
1447 }
1448 
1449 int security_inode_setattr(struct mnt_idmap *idmap,
1450 			   struct dentry *dentry, struct iattr *attr)
1451 {
1452 	int ret;
1453 
1454 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1455 		return 0;
1456 	ret = call_int_hook(inode_setattr, 0, dentry, attr);
1457 	if (ret)
1458 		return ret;
1459 	return evm_inode_setattr(idmap, dentry, attr);
1460 }
1461 EXPORT_SYMBOL_GPL(security_inode_setattr);
1462 
1463 int security_inode_getattr(const struct path *path)
1464 {
1465 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1466 		return 0;
1467 	return call_int_hook(inode_getattr, 0, path);
1468 }
1469 
1470 int security_inode_setxattr(struct mnt_idmap *idmap,
1471 			    struct dentry *dentry, const char *name,
1472 			    const void *value, size_t size, int flags)
1473 {
1474 	int ret;
1475 
1476 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1477 		return 0;
1478 	/*
1479 	 * SELinux and Smack integrate the cap call,
1480 	 * so assume that all LSMs supplying this call do so.
1481 	 */
1482 	ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
1483 			    size, flags);
1484 
1485 	if (ret == 1)
1486 		ret = cap_inode_setxattr(dentry, name, value, size, flags);
1487 	if (ret)
1488 		return ret;
1489 	ret = ima_inode_setxattr(dentry, name, value, size);
1490 	if (ret)
1491 		return ret;
1492 	return evm_inode_setxattr(idmap, dentry, name, value, size);
1493 }
1494 
1495 int security_inode_set_acl(struct mnt_idmap *idmap,
1496 			   struct dentry *dentry, const char *acl_name,
1497 			   struct posix_acl *kacl)
1498 {
1499 	int ret;
1500 
1501 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1502 		return 0;
1503 	ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
1504 			    kacl);
1505 	if (ret)
1506 		return ret;
1507 	ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
1508 	if (ret)
1509 		return ret;
1510 	return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
1511 }
1512 
1513 int security_inode_get_acl(struct mnt_idmap *idmap,
1514 			   struct dentry *dentry, const char *acl_name)
1515 {
1516 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1517 		return 0;
1518 	return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
1519 }
1520 
1521 int security_inode_remove_acl(struct mnt_idmap *idmap,
1522 			      struct dentry *dentry, const char *acl_name)
1523 {
1524 	int ret;
1525 
1526 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1527 		return 0;
1528 	ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
1529 	if (ret)
1530 		return ret;
1531 	ret = ima_inode_remove_acl(idmap, dentry, acl_name);
1532 	if (ret)
1533 		return ret;
1534 	return evm_inode_remove_acl(idmap, dentry, acl_name);
1535 }
1536 
1537 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
1538 				  const void *value, size_t size, int flags)
1539 {
1540 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1541 		return;
1542 	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
1543 	evm_inode_post_setxattr(dentry, name, value, size);
1544 }
1545 
1546 int security_inode_getxattr(struct dentry *dentry, const char *name)
1547 {
1548 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1549 		return 0;
1550 	return call_int_hook(inode_getxattr, 0, dentry, name);
1551 }
1552 
1553 int security_inode_listxattr(struct dentry *dentry)
1554 {
1555 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1556 		return 0;
1557 	return call_int_hook(inode_listxattr, 0, dentry);
1558 }
1559 
1560 int security_inode_removexattr(struct mnt_idmap *idmap,
1561 			       struct dentry *dentry, const char *name)
1562 {
1563 	int ret;
1564 
1565 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1566 		return 0;
1567 	/*
1568 	 * SELinux and Smack integrate the cap call,
1569 	 * so assume that all LSMs supplying this call do so.
1570 	 */
1571 	ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
1572 	if (ret == 1)
1573 		ret = cap_inode_removexattr(idmap, dentry, name);
1574 	if (ret)
1575 		return ret;
1576 	ret = ima_inode_removexattr(dentry, name);
1577 	if (ret)
1578 		return ret;
1579 	return evm_inode_removexattr(idmap, dentry, name);
1580 }
1581 
1582 int security_inode_need_killpriv(struct dentry *dentry)
1583 {
1584 	return call_int_hook(inode_need_killpriv, 0, dentry);
1585 }
1586 
1587 int security_inode_killpriv(struct mnt_idmap *idmap,
1588 			    struct dentry *dentry)
1589 {
1590 	return call_int_hook(inode_killpriv, 0, idmap, dentry);
1591 }
1592 
1593 int security_inode_getsecurity(struct mnt_idmap *idmap,
1594 			       struct inode *inode, const char *name,
1595 			       void **buffer, bool alloc)
1596 {
1597 	struct security_hook_list *hp;
1598 	int rc;
1599 
1600 	if (unlikely(IS_PRIVATE(inode)))
1601 		return LSM_RET_DEFAULT(inode_getsecurity);
1602 	/*
1603 	 * Only one module will provide an attribute with a given name.
1604 	 */
1605 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
1606 		rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer, alloc);
1607 		if (rc != LSM_RET_DEFAULT(inode_getsecurity))
1608 			return rc;
1609 	}
1610 	return LSM_RET_DEFAULT(inode_getsecurity);
1611 }
1612 
1613 int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
1614 {
1615 	struct security_hook_list *hp;
1616 	int rc;
1617 
1618 	if (unlikely(IS_PRIVATE(inode)))
1619 		return LSM_RET_DEFAULT(inode_setsecurity);
1620 	/*
1621 	 * Only one module will provide an attribute with a given name.
1622 	 */
1623 	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
1624 		rc = hp->hook.inode_setsecurity(inode, name, value, size,
1625 								flags);
1626 		if (rc != LSM_RET_DEFAULT(inode_setsecurity))
1627 			return rc;
1628 	}
1629 	return LSM_RET_DEFAULT(inode_setsecurity);
1630 }
1631 
1632 int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
1633 {
1634 	if (unlikely(IS_PRIVATE(inode)))
1635 		return 0;
1636 	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
1637 }
1638 EXPORT_SYMBOL(security_inode_listsecurity);
1639 
1640 void security_inode_getsecid(struct inode *inode, u32 *secid)
1641 {
1642 	call_void_hook(inode_getsecid, inode, secid);
1643 }
1644 
1645 int security_inode_copy_up(struct dentry *src, struct cred **new)
1646 {
1647 	return call_int_hook(inode_copy_up, 0, src, new);
1648 }
1649 EXPORT_SYMBOL(security_inode_copy_up);
1650 
1651 int security_inode_copy_up_xattr(const char *name)
1652 {
1653 	struct security_hook_list *hp;
1654 	int rc;
1655 
1656 	/*
1657 	 * The implementation can return 0 (accept the xattr), 1 (discard the
1658 	 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
1659 	 * any other error code incase of an error.
1660 	 */
1661 	hlist_for_each_entry(hp,
1662 		&security_hook_heads.inode_copy_up_xattr, list) {
1663 		rc = hp->hook.inode_copy_up_xattr(name);
1664 		if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
1665 			return rc;
1666 	}
1667 
1668 	return LSM_RET_DEFAULT(inode_copy_up_xattr);
1669 }
1670 EXPORT_SYMBOL(security_inode_copy_up_xattr);
1671 
1672 int security_kernfs_init_security(struct kernfs_node *kn_dir,
1673 				  struct kernfs_node *kn)
1674 {
1675 	return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
1676 }
1677 
1678 int security_file_permission(struct file *file, int mask)
1679 {
1680 	int ret;
1681 
1682 	ret = call_int_hook(file_permission, 0, file, mask);
1683 	if (ret)
1684 		return ret;
1685 
1686 	return fsnotify_perm(file, mask);
1687 }
1688 
1689 int security_file_alloc(struct file *file)
1690 {
1691 	int rc = lsm_file_alloc(file);
1692 
1693 	if (rc)
1694 		return rc;
1695 	rc = call_int_hook(file_alloc_security, 0, file);
1696 	if (unlikely(rc))
1697 		security_file_free(file);
1698 	return rc;
1699 }
1700 
1701 void security_file_free(struct file *file)
1702 {
1703 	void *blob;
1704 
1705 	call_void_hook(file_free_security, file);
1706 
1707 	blob = file->f_security;
1708 	if (blob) {
1709 		file->f_security = NULL;
1710 		kmem_cache_free(lsm_file_cache, blob);
1711 	}
1712 }
1713 
1714 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1715 {
1716 	return call_int_hook(file_ioctl, 0, file, cmd, arg);
1717 }
1718 EXPORT_SYMBOL_GPL(security_file_ioctl);
1719 
1720 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
1721 {
1722 	/*
1723 	 * Does we have PROT_READ and does the application expect
1724 	 * it to imply PROT_EXEC?  If not, nothing to talk about...
1725 	 */
1726 	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
1727 		return prot;
1728 	if (!(current->personality & READ_IMPLIES_EXEC))
1729 		return prot;
1730 	/*
1731 	 * if that's an anonymous mapping, let it.
1732 	 */
1733 	if (!file)
1734 		return prot | PROT_EXEC;
1735 	/*
1736 	 * ditto if it's not on noexec mount, except that on !MMU we need
1737 	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
1738 	 */
1739 	if (!path_noexec(&file->f_path)) {
1740 #ifndef CONFIG_MMU
1741 		if (file->f_op->mmap_capabilities) {
1742 			unsigned caps = file->f_op->mmap_capabilities(file);
1743 			if (!(caps & NOMMU_MAP_EXEC))
1744 				return prot;
1745 		}
1746 #endif
1747 		return prot | PROT_EXEC;
1748 	}
1749 	/* anything on noexec mount won't get PROT_EXEC */
1750 	return prot;
1751 }
1752 
1753 int security_mmap_file(struct file *file, unsigned long prot,
1754 			unsigned long flags)
1755 {
1756 	unsigned long prot_adj = mmap_prot(file, prot);
1757 	int ret;
1758 
1759 	ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
1760 	if (ret)
1761 		return ret;
1762 	return ima_file_mmap(file, prot, prot_adj, flags);
1763 }
1764 
1765 int security_mmap_addr(unsigned long addr)
1766 {
1767 	return call_int_hook(mmap_addr, 0, addr);
1768 }
1769 
1770 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
1771 			    unsigned long prot)
1772 {
1773 	int ret;
1774 
1775 	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
1776 	if (ret)
1777 		return ret;
1778 	return ima_file_mprotect(vma, prot);
1779 }
1780 
1781 int security_file_lock(struct file *file, unsigned int cmd)
1782 {
1783 	return call_int_hook(file_lock, 0, file, cmd);
1784 }
1785 
1786 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1787 {
1788 	return call_int_hook(file_fcntl, 0, file, cmd, arg);
1789 }
1790 
1791 void security_file_set_fowner(struct file *file)
1792 {
1793 	call_void_hook(file_set_fowner, file);
1794 }
1795 
1796 int security_file_send_sigiotask(struct task_struct *tsk,
1797 				  struct fown_struct *fown, int sig)
1798 {
1799 	return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
1800 }
1801 
1802 int security_file_receive(struct file *file)
1803 {
1804 	return call_int_hook(file_receive, 0, file);
1805 }
1806 
1807 int security_file_open(struct file *file)
1808 {
1809 	int ret;
1810 
1811 	ret = call_int_hook(file_open, 0, file);
1812 	if (ret)
1813 		return ret;
1814 
1815 	return fsnotify_perm(file, MAY_OPEN);
1816 }
1817 
1818 int security_file_truncate(struct file *file)
1819 {
1820 	return call_int_hook(file_truncate, 0, file);
1821 }
1822 
1823 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
1824 {
1825 	int rc = lsm_task_alloc(task);
1826 
1827 	if (rc)
1828 		return rc;
1829 	rc = call_int_hook(task_alloc, 0, task, clone_flags);
1830 	if (unlikely(rc))
1831 		security_task_free(task);
1832 	return rc;
1833 }
1834 
1835 void security_task_free(struct task_struct *task)
1836 {
1837 	call_void_hook(task_free, task);
1838 
1839 	kfree(task->security);
1840 	task->security = NULL;
1841 }
1842 
1843 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1844 {
1845 	int rc = lsm_cred_alloc(cred, gfp);
1846 
1847 	if (rc)
1848 		return rc;
1849 
1850 	rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
1851 	if (unlikely(rc))
1852 		security_cred_free(cred);
1853 	return rc;
1854 }
1855 
1856 void security_cred_free(struct cred *cred)
1857 {
1858 	/*
1859 	 * There is a failure case in prepare_creds() that
1860 	 * may result in a call here with ->security being NULL.
1861 	 */
1862 	if (unlikely(cred->security == NULL))
1863 		return;
1864 
1865 	call_void_hook(cred_free, cred);
1866 
1867 	kfree(cred->security);
1868 	cred->security = NULL;
1869 }
1870 
1871 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
1872 {
1873 	int rc = lsm_cred_alloc(new, gfp);
1874 
1875 	if (rc)
1876 		return rc;
1877 
1878 	rc = call_int_hook(cred_prepare, 0, new, old, gfp);
1879 	if (unlikely(rc))
1880 		security_cred_free(new);
1881 	return rc;
1882 }
1883 
1884 void security_transfer_creds(struct cred *new, const struct cred *old)
1885 {
1886 	call_void_hook(cred_transfer, new, old);
1887 }
1888 
1889 void security_cred_getsecid(const struct cred *c, u32 *secid)
1890 {
1891 	*secid = 0;
1892 	call_void_hook(cred_getsecid, c, secid);
1893 }
1894 EXPORT_SYMBOL(security_cred_getsecid);
1895 
1896 int security_kernel_act_as(struct cred *new, u32 secid)
1897 {
1898 	return call_int_hook(kernel_act_as, 0, new, secid);
1899 }
1900 
1901 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
1902 {
1903 	return call_int_hook(kernel_create_files_as, 0, new, inode);
1904 }
1905 
1906 int security_kernel_module_request(char *kmod_name)
1907 {
1908 	int ret;
1909 
1910 	ret = call_int_hook(kernel_module_request, 0, kmod_name);
1911 	if (ret)
1912 		return ret;
1913 	return integrity_kernel_module_request(kmod_name);
1914 }
1915 
1916 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
1917 			      bool contents)
1918 {
1919 	int ret;
1920 
1921 	ret = call_int_hook(kernel_read_file, 0, file, id, contents);
1922 	if (ret)
1923 		return ret;
1924 	return ima_read_file(file, id, contents);
1925 }
1926 EXPORT_SYMBOL_GPL(security_kernel_read_file);
1927 
1928 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
1929 				   enum kernel_read_file_id id)
1930 {
1931 	int ret;
1932 
1933 	ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
1934 	if (ret)
1935 		return ret;
1936 	return ima_post_read_file(file, buf, size, id);
1937 }
1938 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
1939 
1940 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
1941 {
1942 	int ret;
1943 
1944 	ret = call_int_hook(kernel_load_data, 0, id, contents);
1945 	if (ret)
1946 		return ret;
1947 	return ima_load_data(id, contents);
1948 }
1949 EXPORT_SYMBOL_GPL(security_kernel_load_data);
1950 
1951 int security_kernel_post_load_data(char *buf, loff_t size,
1952 				   enum kernel_load_data_id id,
1953 				   char *description)
1954 {
1955 	int ret;
1956 
1957 	ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
1958 			    description);
1959 	if (ret)
1960 		return ret;
1961 	return ima_post_load_data(buf, size, id, description);
1962 }
1963 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
1964 
1965 int security_task_fix_setuid(struct cred *new, const struct cred *old,
1966 			     int flags)
1967 {
1968 	return call_int_hook(task_fix_setuid, 0, new, old, flags);
1969 }
1970 
1971 int security_task_fix_setgid(struct cred *new, const struct cred *old,
1972 				 int flags)
1973 {
1974 	return call_int_hook(task_fix_setgid, 0, new, old, flags);
1975 }
1976 
1977 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
1978 {
1979 	return call_int_hook(task_fix_setgroups, 0, new, old);
1980 }
1981 
1982 int security_task_setpgid(struct task_struct *p, pid_t pgid)
1983 {
1984 	return call_int_hook(task_setpgid, 0, p, pgid);
1985 }
1986 
1987 int security_task_getpgid(struct task_struct *p)
1988 {
1989 	return call_int_hook(task_getpgid, 0, p);
1990 }
1991 
1992 int security_task_getsid(struct task_struct *p)
1993 {
1994 	return call_int_hook(task_getsid, 0, p);
1995 }
1996 
1997 void security_current_getsecid_subj(u32 *secid)
1998 {
1999 	*secid = 0;
2000 	call_void_hook(current_getsecid_subj, secid);
2001 }
2002 EXPORT_SYMBOL(security_current_getsecid_subj);
2003 
2004 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
2005 {
2006 	*secid = 0;
2007 	call_void_hook(task_getsecid_obj, p, secid);
2008 }
2009 EXPORT_SYMBOL(security_task_getsecid_obj);
2010 
2011 int security_task_setnice(struct task_struct *p, int nice)
2012 {
2013 	return call_int_hook(task_setnice, 0, p, nice);
2014 }
2015 
2016 int security_task_setioprio(struct task_struct *p, int ioprio)
2017 {
2018 	return call_int_hook(task_setioprio, 0, p, ioprio);
2019 }
2020 
2021 int security_task_getioprio(struct task_struct *p)
2022 {
2023 	return call_int_hook(task_getioprio, 0, p);
2024 }
2025 
2026 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
2027 			  unsigned int flags)
2028 {
2029 	return call_int_hook(task_prlimit, 0, cred, tcred, flags);
2030 }
2031 
2032 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
2033 		struct rlimit *new_rlim)
2034 {
2035 	return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
2036 }
2037 
2038 int security_task_setscheduler(struct task_struct *p)
2039 {
2040 	return call_int_hook(task_setscheduler, 0, p);
2041 }
2042 
2043 int security_task_getscheduler(struct task_struct *p)
2044 {
2045 	return call_int_hook(task_getscheduler, 0, p);
2046 }
2047 
2048 int security_task_movememory(struct task_struct *p)
2049 {
2050 	return call_int_hook(task_movememory, 0, p);
2051 }
2052 
2053 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2054 			int sig, const struct cred *cred)
2055 {
2056 	return call_int_hook(task_kill, 0, p, info, sig, cred);
2057 }
2058 
2059 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
2060 			 unsigned long arg4, unsigned long arg5)
2061 {
2062 	int thisrc;
2063 	int rc = LSM_RET_DEFAULT(task_prctl);
2064 	struct security_hook_list *hp;
2065 
2066 	hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
2067 		thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
2068 		if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
2069 			rc = thisrc;
2070 			if (thisrc != 0)
2071 				break;
2072 		}
2073 	}
2074 	return rc;
2075 }
2076 
2077 void security_task_to_inode(struct task_struct *p, struct inode *inode)
2078 {
2079 	call_void_hook(task_to_inode, p, inode);
2080 }
2081 
2082 int security_create_user_ns(const struct cred *cred)
2083 {
2084 	return call_int_hook(userns_create, 0, cred);
2085 }
2086 
2087 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
2088 {
2089 	return call_int_hook(ipc_permission, 0, ipcp, flag);
2090 }
2091 
2092 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
2093 {
2094 	*secid = 0;
2095 	call_void_hook(ipc_getsecid, ipcp, secid);
2096 }
2097 
2098 int security_msg_msg_alloc(struct msg_msg *msg)
2099 {
2100 	int rc = lsm_msg_msg_alloc(msg);
2101 
2102 	if (unlikely(rc))
2103 		return rc;
2104 	rc = call_int_hook(msg_msg_alloc_security, 0, msg);
2105 	if (unlikely(rc))
2106 		security_msg_msg_free(msg);
2107 	return rc;
2108 }
2109 
2110 void security_msg_msg_free(struct msg_msg *msg)
2111 {
2112 	call_void_hook(msg_msg_free_security, msg);
2113 	kfree(msg->security);
2114 	msg->security = NULL;
2115 }
2116 
2117 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
2118 {
2119 	int rc = lsm_ipc_alloc(msq);
2120 
2121 	if (unlikely(rc))
2122 		return rc;
2123 	rc = call_int_hook(msg_queue_alloc_security, 0, msq);
2124 	if (unlikely(rc))
2125 		security_msg_queue_free(msq);
2126 	return rc;
2127 }
2128 
2129 void security_msg_queue_free(struct kern_ipc_perm *msq)
2130 {
2131 	call_void_hook(msg_queue_free_security, msq);
2132 	kfree(msq->security);
2133 	msq->security = NULL;
2134 }
2135 
2136 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
2137 {
2138 	return call_int_hook(msg_queue_associate, 0, msq, msqflg);
2139 }
2140 
2141 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
2142 {
2143 	return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
2144 }
2145 
2146 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
2147 			       struct msg_msg *msg, int msqflg)
2148 {
2149 	return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
2150 }
2151 
2152 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
2153 			       struct task_struct *target, long type, int mode)
2154 {
2155 	return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
2156 }
2157 
2158 int security_shm_alloc(struct kern_ipc_perm *shp)
2159 {
2160 	int rc = lsm_ipc_alloc(shp);
2161 
2162 	if (unlikely(rc))
2163 		return rc;
2164 	rc = call_int_hook(shm_alloc_security, 0, shp);
2165 	if (unlikely(rc))
2166 		security_shm_free(shp);
2167 	return rc;
2168 }
2169 
2170 void security_shm_free(struct kern_ipc_perm *shp)
2171 {
2172 	call_void_hook(shm_free_security, shp);
2173 	kfree(shp->security);
2174 	shp->security = NULL;
2175 }
2176 
2177 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
2178 {
2179 	return call_int_hook(shm_associate, 0, shp, shmflg);
2180 }
2181 
2182 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
2183 {
2184 	return call_int_hook(shm_shmctl, 0, shp, cmd);
2185 }
2186 
2187 int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
2188 {
2189 	return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
2190 }
2191 
2192 int security_sem_alloc(struct kern_ipc_perm *sma)
2193 {
2194 	int rc = lsm_ipc_alloc(sma);
2195 
2196 	if (unlikely(rc))
2197 		return rc;
2198 	rc = call_int_hook(sem_alloc_security, 0, sma);
2199 	if (unlikely(rc))
2200 		security_sem_free(sma);
2201 	return rc;
2202 }
2203 
2204 void security_sem_free(struct kern_ipc_perm *sma)
2205 {
2206 	call_void_hook(sem_free_security, sma);
2207 	kfree(sma->security);
2208 	sma->security = NULL;
2209 }
2210 
2211 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
2212 {
2213 	return call_int_hook(sem_associate, 0, sma, semflg);
2214 }
2215 
2216 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
2217 {
2218 	return call_int_hook(sem_semctl, 0, sma, cmd);
2219 }
2220 
2221 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
2222 			unsigned nsops, int alter)
2223 {
2224 	return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
2225 }
2226 
2227 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
2228 {
2229 	if (unlikely(inode && IS_PRIVATE(inode)))
2230 		return;
2231 	call_void_hook(d_instantiate, dentry, inode);
2232 }
2233 EXPORT_SYMBOL(security_d_instantiate);
2234 
2235 int security_getprocattr(struct task_struct *p, const char *lsm,
2236 			 const char *name, char **value)
2237 {
2238 	struct security_hook_list *hp;
2239 
2240 	hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
2241 		if (lsm != NULL && strcmp(lsm, hp->lsm))
2242 			continue;
2243 		return hp->hook.getprocattr(p, name, value);
2244 	}
2245 	return LSM_RET_DEFAULT(getprocattr);
2246 }
2247 
2248 int security_setprocattr(const char *lsm, const char *name, void *value,
2249 			 size_t size)
2250 {
2251 	struct security_hook_list *hp;
2252 
2253 	hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
2254 		if (lsm != NULL && strcmp(lsm, hp->lsm))
2255 			continue;
2256 		return hp->hook.setprocattr(name, value, size);
2257 	}
2258 	return LSM_RET_DEFAULT(setprocattr);
2259 }
2260 
2261 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
2262 {
2263 	return call_int_hook(netlink_send, 0, sk, skb);
2264 }
2265 
2266 int security_ismaclabel(const char *name)
2267 {
2268 	return call_int_hook(ismaclabel, 0, name);
2269 }
2270 EXPORT_SYMBOL(security_ismaclabel);
2271 
2272 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2273 {
2274 	struct security_hook_list *hp;
2275 	int rc;
2276 
2277 	/*
2278 	 * Currently, only one LSM can implement secid_to_secctx (i.e this
2279 	 * LSM hook is not "stackable").
2280 	 */
2281 	hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
2282 		rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
2283 		if (rc != LSM_RET_DEFAULT(secid_to_secctx))
2284 			return rc;
2285 	}
2286 
2287 	return LSM_RET_DEFAULT(secid_to_secctx);
2288 }
2289 EXPORT_SYMBOL(security_secid_to_secctx);
2290 
2291 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2292 {
2293 	*secid = 0;
2294 	return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
2295 }
2296 EXPORT_SYMBOL(security_secctx_to_secid);
2297 
2298 void security_release_secctx(char *secdata, u32 seclen)
2299 {
2300 	call_void_hook(release_secctx, secdata, seclen);
2301 }
2302 EXPORT_SYMBOL(security_release_secctx);
2303 
2304 void security_inode_invalidate_secctx(struct inode *inode)
2305 {
2306 	call_void_hook(inode_invalidate_secctx, inode);
2307 }
2308 EXPORT_SYMBOL(security_inode_invalidate_secctx);
2309 
2310 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
2311 {
2312 	return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
2313 }
2314 EXPORT_SYMBOL(security_inode_notifysecctx);
2315 
2316 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
2317 {
2318 	return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
2319 }
2320 EXPORT_SYMBOL(security_inode_setsecctx);
2321 
2322 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
2323 {
2324 	return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
2325 }
2326 EXPORT_SYMBOL(security_inode_getsecctx);
2327 
2328 #ifdef CONFIG_WATCH_QUEUE
2329 int security_post_notification(const struct cred *w_cred,
2330 			       const struct cred *cred,
2331 			       struct watch_notification *n)
2332 {
2333 	return call_int_hook(post_notification, 0, w_cred, cred, n);
2334 }
2335 #endif /* CONFIG_WATCH_QUEUE */
2336 
2337 #ifdef CONFIG_KEY_NOTIFICATIONS
2338 int security_watch_key(struct key *key)
2339 {
2340 	return call_int_hook(watch_key, 0, key);
2341 }
2342 #endif
2343 
2344 #ifdef CONFIG_SECURITY_NETWORK
2345 
2346 int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
2347 {
2348 	return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
2349 }
2350 EXPORT_SYMBOL(security_unix_stream_connect);
2351 
2352 int security_unix_may_send(struct socket *sock,  struct socket *other)
2353 {
2354 	return call_int_hook(unix_may_send, 0, sock, other);
2355 }
2356 EXPORT_SYMBOL(security_unix_may_send);
2357 
2358 int security_socket_create(int family, int type, int protocol, int kern)
2359 {
2360 	return call_int_hook(socket_create, 0, family, type, protocol, kern);
2361 }
2362 
2363 int security_socket_post_create(struct socket *sock, int family,
2364 				int type, int protocol, int kern)
2365 {
2366 	return call_int_hook(socket_post_create, 0, sock, family, type,
2367 						protocol, kern);
2368 }
2369 
2370 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
2371 {
2372 	return call_int_hook(socket_socketpair, 0, socka, sockb);
2373 }
2374 EXPORT_SYMBOL(security_socket_socketpair);
2375 
2376 int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
2377 {
2378 	return call_int_hook(socket_bind, 0, sock, address, addrlen);
2379 }
2380 
2381 int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
2382 {
2383 	return call_int_hook(socket_connect, 0, sock, address, addrlen);
2384 }
2385 
2386 int security_socket_listen(struct socket *sock, int backlog)
2387 {
2388 	return call_int_hook(socket_listen, 0, sock, backlog);
2389 }
2390 
2391 int security_socket_accept(struct socket *sock, struct socket *newsock)
2392 {
2393 	return call_int_hook(socket_accept, 0, sock, newsock);
2394 }
2395 
2396 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
2397 {
2398 	return call_int_hook(socket_sendmsg, 0, sock, msg, size);
2399 }
2400 
2401 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
2402 			    int size, int flags)
2403 {
2404 	return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
2405 }
2406 
2407 int security_socket_getsockname(struct socket *sock)
2408 {
2409 	return call_int_hook(socket_getsockname, 0, sock);
2410 }
2411 
2412 int security_socket_getpeername(struct socket *sock)
2413 {
2414 	return call_int_hook(socket_getpeername, 0, sock);
2415 }
2416 
2417 int security_socket_getsockopt(struct socket *sock, int level, int optname)
2418 {
2419 	return call_int_hook(socket_getsockopt, 0, sock, level, optname);
2420 }
2421 
2422 int security_socket_setsockopt(struct socket *sock, int level, int optname)
2423 {
2424 	return call_int_hook(socket_setsockopt, 0, sock, level, optname);
2425 }
2426 
2427 int security_socket_shutdown(struct socket *sock, int how)
2428 {
2429 	return call_int_hook(socket_shutdown, 0, sock, how);
2430 }
2431 
2432 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2433 {
2434 	return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
2435 }
2436 EXPORT_SYMBOL(security_sock_rcv_skb);
2437 
2438 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
2439 				      sockptr_t optlen, unsigned int len)
2440 {
2441 	return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
2442 			     optval, optlen, len);
2443 }
2444 
2445 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
2446 {
2447 	return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
2448 			     skb, secid);
2449 }
2450 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
2451 
2452 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
2453 {
2454 	return call_int_hook(sk_alloc_security, 0, sk, family, priority);
2455 }
2456 
2457 void security_sk_free(struct sock *sk)
2458 {
2459 	call_void_hook(sk_free_security, sk);
2460 }
2461 
2462 void security_sk_clone(const struct sock *sk, struct sock *newsk)
2463 {
2464 	call_void_hook(sk_clone_security, sk, newsk);
2465 }
2466 EXPORT_SYMBOL(security_sk_clone);
2467 
2468 void security_sk_classify_flow(struct sock *sk, struct flowi_common *flic)
2469 {
2470 	call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
2471 }
2472 EXPORT_SYMBOL(security_sk_classify_flow);
2473 
2474 void security_req_classify_flow(const struct request_sock *req,
2475 				struct flowi_common *flic)
2476 {
2477 	call_void_hook(req_classify_flow, req, flic);
2478 }
2479 EXPORT_SYMBOL(security_req_classify_flow);
2480 
2481 void security_sock_graft(struct sock *sk, struct socket *parent)
2482 {
2483 	call_void_hook(sock_graft, sk, parent);
2484 }
2485 EXPORT_SYMBOL(security_sock_graft);
2486 
2487 int security_inet_conn_request(const struct sock *sk,
2488 			struct sk_buff *skb, struct request_sock *req)
2489 {
2490 	return call_int_hook(inet_conn_request, 0, sk, skb, req);
2491 }
2492 EXPORT_SYMBOL(security_inet_conn_request);
2493 
2494 void security_inet_csk_clone(struct sock *newsk,
2495 			const struct request_sock *req)
2496 {
2497 	call_void_hook(inet_csk_clone, newsk, req);
2498 }
2499 
2500 void security_inet_conn_established(struct sock *sk,
2501 			struct sk_buff *skb)
2502 {
2503 	call_void_hook(inet_conn_established, sk, skb);
2504 }
2505 EXPORT_SYMBOL(security_inet_conn_established);
2506 
2507 int security_secmark_relabel_packet(u32 secid)
2508 {
2509 	return call_int_hook(secmark_relabel_packet, 0, secid);
2510 }
2511 EXPORT_SYMBOL(security_secmark_relabel_packet);
2512 
2513 void security_secmark_refcount_inc(void)
2514 {
2515 	call_void_hook(secmark_refcount_inc);
2516 }
2517 EXPORT_SYMBOL(security_secmark_refcount_inc);
2518 
2519 void security_secmark_refcount_dec(void)
2520 {
2521 	call_void_hook(secmark_refcount_dec);
2522 }
2523 EXPORT_SYMBOL(security_secmark_refcount_dec);
2524 
2525 int security_tun_dev_alloc_security(void **security)
2526 {
2527 	return call_int_hook(tun_dev_alloc_security, 0, security);
2528 }
2529 EXPORT_SYMBOL(security_tun_dev_alloc_security);
2530 
2531 void security_tun_dev_free_security(void *security)
2532 {
2533 	call_void_hook(tun_dev_free_security, security);
2534 }
2535 EXPORT_SYMBOL(security_tun_dev_free_security);
2536 
2537 int security_tun_dev_create(void)
2538 {
2539 	return call_int_hook(tun_dev_create, 0);
2540 }
2541 EXPORT_SYMBOL(security_tun_dev_create);
2542 
2543 int security_tun_dev_attach_queue(void *security)
2544 {
2545 	return call_int_hook(tun_dev_attach_queue, 0, security);
2546 }
2547 EXPORT_SYMBOL(security_tun_dev_attach_queue);
2548 
2549 int security_tun_dev_attach(struct sock *sk, void *security)
2550 {
2551 	return call_int_hook(tun_dev_attach, 0, sk, security);
2552 }
2553 EXPORT_SYMBOL(security_tun_dev_attach);
2554 
2555 int security_tun_dev_open(void *security)
2556 {
2557 	return call_int_hook(tun_dev_open, 0, security);
2558 }
2559 EXPORT_SYMBOL(security_tun_dev_open);
2560 
2561 int security_sctp_assoc_request(struct sctp_association *asoc, struct sk_buff *skb)
2562 {
2563 	return call_int_hook(sctp_assoc_request, 0, asoc, skb);
2564 }
2565 EXPORT_SYMBOL(security_sctp_assoc_request);
2566 
2567 int security_sctp_bind_connect(struct sock *sk, int optname,
2568 			       struct sockaddr *address, int addrlen)
2569 {
2570 	return call_int_hook(sctp_bind_connect, 0, sk, optname,
2571 			     address, addrlen);
2572 }
2573 EXPORT_SYMBOL(security_sctp_bind_connect);
2574 
2575 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
2576 			    struct sock *newsk)
2577 {
2578 	call_void_hook(sctp_sk_clone, asoc, sk, newsk);
2579 }
2580 EXPORT_SYMBOL(security_sctp_sk_clone);
2581 
2582 int security_sctp_assoc_established(struct sctp_association *asoc,
2583 				    struct sk_buff *skb)
2584 {
2585 	return call_int_hook(sctp_assoc_established, 0, asoc, skb);
2586 }
2587 EXPORT_SYMBOL(security_sctp_assoc_established);
2588 
2589 #endif	/* CONFIG_SECURITY_NETWORK */
2590 
2591 #ifdef CONFIG_SECURITY_INFINIBAND
2592 
2593 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
2594 {
2595 	return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
2596 }
2597 EXPORT_SYMBOL(security_ib_pkey_access);
2598 
2599 int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
2600 {
2601 	return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
2602 }
2603 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
2604 
2605 int security_ib_alloc_security(void **sec)
2606 {
2607 	return call_int_hook(ib_alloc_security, 0, sec);
2608 }
2609 EXPORT_SYMBOL(security_ib_alloc_security);
2610 
2611 void security_ib_free_security(void *sec)
2612 {
2613 	call_void_hook(ib_free_security, sec);
2614 }
2615 EXPORT_SYMBOL(security_ib_free_security);
2616 #endif	/* CONFIG_SECURITY_INFINIBAND */
2617 
2618 #ifdef CONFIG_SECURITY_NETWORK_XFRM
2619 
2620 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
2621 			       struct xfrm_user_sec_ctx *sec_ctx,
2622 			       gfp_t gfp)
2623 {
2624 	return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
2625 }
2626 EXPORT_SYMBOL(security_xfrm_policy_alloc);
2627 
2628 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
2629 			      struct xfrm_sec_ctx **new_ctxp)
2630 {
2631 	return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
2632 }
2633 
2634 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
2635 {
2636 	call_void_hook(xfrm_policy_free_security, ctx);
2637 }
2638 EXPORT_SYMBOL(security_xfrm_policy_free);
2639 
2640 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
2641 {
2642 	return call_int_hook(xfrm_policy_delete_security, 0, ctx);
2643 }
2644 
2645 int security_xfrm_state_alloc(struct xfrm_state *x,
2646 			      struct xfrm_user_sec_ctx *sec_ctx)
2647 {
2648 	return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
2649 }
2650 EXPORT_SYMBOL(security_xfrm_state_alloc);
2651 
2652 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
2653 				      struct xfrm_sec_ctx *polsec, u32 secid)
2654 {
2655 	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
2656 }
2657 
2658 int security_xfrm_state_delete(struct xfrm_state *x)
2659 {
2660 	return call_int_hook(xfrm_state_delete_security, 0, x);
2661 }
2662 EXPORT_SYMBOL(security_xfrm_state_delete);
2663 
2664 void security_xfrm_state_free(struct xfrm_state *x)
2665 {
2666 	call_void_hook(xfrm_state_free_security, x);
2667 }
2668 
2669 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
2670 {
2671 	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
2672 }
2673 
2674 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
2675 				       struct xfrm_policy *xp,
2676 				       const struct flowi_common *flic)
2677 {
2678 	struct security_hook_list *hp;
2679 	int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
2680 
2681 	/*
2682 	 * Since this function is expected to return 0 or 1, the judgment
2683 	 * becomes difficult if multiple LSMs supply this call. Fortunately,
2684 	 * we can use the first LSM's judgment because currently only SELinux
2685 	 * supplies this call.
2686 	 *
2687 	 * For speed optimization, we explicitly break the loop rather than
2688 	 * using the macro
2689 	 */
2690 	hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
2691 				list) {
2692 		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
2693 		break;
2694 	}
2695 	return rc;
2696 }
2697 
2698 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
2699 {
2700 	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
2701 }
2702 
2703 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
2704 {
2705 	int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
2706 				0);
2707 
2708 	BUG_ON(rc);
2709 }
2710 EXPORT_SYMBOL(security_skb_classify_flow);
2711 
2712 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
2713 
2714 #ifdef CONFIG_KEYS
2715 
2716 int security_key_alloc(struct key *key, const struct cred *cred,
2717 		       unsigned long flags)
2718 {
2719 	return call_int_hook(key_alloc, 0, key, cred, flags);
2720 }
2721 
2722 void security_key_free(struct key *key)
2723 {
2724 	call_void_hook(key_free, key);
2725 }
2726 
2727 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
2728 			    enum key_need_perm need_perm)
2729 {
2730 	return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
2731 }
2732 
2733 int security_key_getsecurity(struct key *key, char **_buffer)
2734 {
2735 	*_buffer = NULL;
2736 	return call_int_hook(key_getsecurity, 0, key, _buffer);
2737 }
2738 
2739 #endif	/* CONFIG_KEYS */
2740 
2741 #ifdef CONFIG_AUDIT
2742 
2743 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2744 {
2745 	return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
2746 }
2747 
2748 int security_audit_rule_known(struct audit_krule *krule)
2749 {
2750 	return call_int_hook(audit_rule_known, 0, krule);
2751 }
2752 
2753 void security_audit_rule_free(void *lsmrule)
2754 {
2755 	call_void_hook(audit_rule_free, lsmrule);
2756 }
2757 
2758 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
2759 {
2760 	return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
2761 }
2762 #endif /* CONFIG_AUDIT */
2763 
2764 #ifdef CONFIG_BPF_SYSCALL
2765 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
2766 {
2767 	return call_int_hook(bpf, 0, cmd, attr, size);
2768 }
2769 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
2770 {
2771 	return call_int_hook(bpf_map, 0, map, fmode);
2772 }
2773 int security_bpf_prog(struct bpf_prog *prog)
2774 {
2775 	return call_int_hook(bpf_prog, 0, prog);
2776 }
2777 int security_bpf_map_alloc(struct bpf_map *map)
2778 {
2779 	return call_int_hook(bpf_map_alloc_security, 0, map);
2780 }
2781 int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
2782 {
2783 	return call_int_hook(bpf_prog_alloc_security, 0, aux);
2784 }
2785 void security_bpf_map_free(struct bpf_map *map)
2786 {
2787 	call_void_hook(bpf_map_free_security, map);
2788 }
2789 void security_bpf_prog_free(struct bpf_prog_aux *aux)
2790 {
2791 	call_void_hook(bpf_prog_free_security, aux);
2792 }
2793 #endif /* CONFIG_BPF_SYSCALL */
2794 
2795 int security_locked_down(enum lockdown_reason what)
2796 {
2797 	return call_int_hook(locked_down, 0, what);
2798 }
2799 EXPORT_SYMBOL(security_locked_down);
2800 
2801 #ifdef CONFIG_PERF_EVENTS
2802 int security_perf_event_open(struct perf_event_attr *attr, int type)
2803 {
2804 	return call_int_hook(perf_event_open, 0, attr, type);
2805 }
2806 
2807 int security_perf_event_alloc(struct perf_event *event)
2808 {
2809 	return call_int_hook(perf_event_alloc, 0, event);
2810 }
2811 
2812 void security_perf_event_free(struct perf_event *event)
2813 {
2814 	call_void_hook(perf_event_free, event);
2815 }
2816 
2817 int security_perf_event_read(struct perf_event *event)
2818 {
2819 	return call_int_hook(perf_event_read, 0, event);
2820 }
2821 
2822 int security_perf_event_write(struct perf_event *event)
2823 {
2824 	return call_int_hook(perf_event_write, 0, event);
2825 }
2826 #endif /* CONFIG_PERF_EVENTS */
2827 
2828 #ifdef CONFIG_IO_URING
2829 int security_uring_override_creds(const struct cred *new)
2830 {
2831 	return call_int_hook(uring_override_creds, 0, new);
2832 }
2833 
2834 int security_uring_sqpoll(void)
2835 {
2836 	return call_int_hook(uring_sqpoll, 0);
2837 }
2838 int security_uring_cmd(struct io_uring_cmd *ioucmd)
2839 {
2840 	return call_int_hook(uring_cmd, 0, ioucmd);
2841 }
2842 #endif /* CONFIG_IO_URING */
2843