xref: /openbmc/linux/security/security.c (revision 55e85320)
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 /**
783  * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
784  * @mgr: task credentials of current binder process
785  *
786  * Check whether @mgr is allowed to be the binder context manager.
787  *
788  * Return: Return 0 if permission is granted.
789  */
790 int security_binder_set_context_mgr(const struct cred *mgr)
791 {
792 	return call_int_hook(binder_set_context_mgr, 0, mgr);
793 }
794 
795 /**
796  * security_binder_transaction() - Check if a binder transaction is allowed
797  * @from: sending process
798  * @to: receiving process
799  *
800  * Check whether @from is allowed to invoke a binder transaction call to @to.
801  *
802  * Return: Returns 0 if permission is granted.
803  */
804 int security_binder_transaction(const struct cred *from,
805 				const struct cred *to)
806 {
807 	return call_int_hook(binder_transaction, 0, from, to);
808 }
809 
810 /**
811  * security_binder_transfer_binder() - Check if a binder transfer is allowed
812  * @from: sending process
813  * @to: receiving process
814  *
815  * Check whether @from is allowed to transfer a binder reference to @to.
816  *
817  * Return: Returns 0 if permission is granted.
818  */
819 int security_binder_transfer_binder(const struct cred *from,
820 				    const struct cred *to)
821 {
822 	return call_int_hook(binder_transfer_binder, 0, from, to);
823 }
824 
825 /**
826  * security_binder_transfer_file() - Check if a binder file xfer is allowed
827  * @from: sending process
828  * @to: receiving process
829  * @file: file being transferred
830  *
831  * Check whether @from is allowed to transfer @file to @to.
832  *
833  * Return: Returns 0 if permission is granted.
834  */
835 int security_binder_transfer_file(const struct cred *from,
836 				  const struct cred *to, struct file *file)
837 {
838 	return call_int_hook(binder_transfer_file, 0, from, to, file);
839 }
840 
841 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
842 {
843 	return call_int_hook(ptrace_access_check, 0, child, mode);
844 }
845 
846 int security_ptrace_traceme(struct task_struct *parent)
847 {
848 	return call_int_hook(ptrace_traceme, 0, parent);
849 }
850 
851 int security_capget(struct task_struct *target,
852 		     kernel_cap_t *effective,
853 		     kernel_cap_t *inheritable,
854 		     kernel_cap_t *permitted)
855 {
856 	return call_int_hook(capget, 0, target,
857 				effective, inheritable, permitted);
858 }
859 
860 int security_capset(struct cred *new, const struct cred *old,
861 		    const kernel_cap_t *effective,
862 		    const kernel_cap_t *inheritable,
863 		    const kernel_cap_t *permitted)
864 {
865 	return call_int_hook(capset, 0, new, old,
866 				effective, inheritable, permitted);
867 }
868 
869 int security_capable(const struct cred *cred,
870 		     struct user_namespace *ns,
871 		     int cap,
872 		     unsigned int opts)
873 {
874 	return call_int_hook(capable, 0, cred, ns, cap, opts);
875 }
876 
877 int security_quotactl(int cmds, int type, int id, struct super_block *sb)
878 {
879 	return call_int_hook(quotactl, 0, cmds, type, id, sb);
880 }
881 
882 int security_quota_on(struct dentry *dentry)
883 {
884 	return call_int_hook(quota_on, 0, dentry);
885 }
886 
887 int security_syslog(int type)
888 {
889 	return call_int_hook(syslog, 0, type);
890 }
891 
892 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
893 {
894 	return call_int_hook(settime, 0, ts, tz);
895 }
896 
897 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
898 {
899 	struct security_hook_list *hp;
900 	int cap_sys_admin = 1;
901 	int rc;
902 
903 	/*
904 	 * The module will respond with a positive value if
905 	 * it thinks the __vm_enough_memory() call should be
906 	 * made with the cap_sys_admin set. If all of the modules
907 	 * agree that it should be set it will. If any module
908 	 * thinks it should not be set it won't.
909 	 */
910 	hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
911 		rc = hp->hook.vm_enough_memory(mm, pages);
912 		if (rc <= 0) {
913 			cap_sys_admin = 0;
914 			break;
915 		}
916 	}
917 	return __vm_enough_memory(mm, pages, cap_sys_admin);
918 }
919 
920 /**
921  * security_bprm_creds_for_exec() - Prepare the credentials for exec()
922  * @bprm: binary program information
923  *
924  * If the setup in prepare_exec_creds did not setup @bprm->cred->security
925  * properly for executing @bprm->file, update the LSM's portion of
926  * @bprm->cred->security to be what commit_creds needs to install for the new
927  * program.  This hook may also optionally check permissions (e.g. for
928  * transitions between security domains).  The hook must set @bprm->secureexec
929  * to 1 if AT_SECURE should be set to request libc enable secure mode.  @bprm
930  * contains the linux_binprm structure.
931  *
932  * Return: Returns 0 if the hook is successful and permission is granted.
933  */
934 int security_bprm_creds_for_exec(struct linux_binprm *bprm)
935 {
936 	return call_int_hook(bprm_creds_for_exec, 0, bprm);
937 }
938 
939 /**
940  * security_bprm_creds_from_file() - Update linux_binprm creds based on file
941  * @bprm: binary program information
942  * @file: associated file
943  *
944  * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
945  * exec, update @bprm->cred to reflect that change. This is called after
946  * finding the binary that will be executed without an interpreter.  This
947  * ensures that the credentials will not be derived from a script that the
948  * binary will need to reopen, which when reopend may end up being a completely
949  * different file.  This hook may also optionally check permissions (e.g. for
950  * transitions between security domains).  The hook must set @bprm->secureexec
951  * to 1 if AT_SECURE should be set to request libc enable secure mode.  The
952  * hook must add to @bprm->per_clear any personality flags that should be
953  * cleared from current->personality.  @bprm contains the linux_binprm
954  * structure.
955  *
956  * Return: Returns 0 if the hook is successful and permission is granted.
957  */
958 int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
959 {
960 	return call_int_hook(bprm_creds_from_file, 0, bprm, file);
961 }
962 
963 /**
964  * security_bprm_check() - Mediate binary handler search
965  * @bprm: binary program information
966  *
967  * This hook mediates the point when a search for a binary handler will begin.
968  * It allows a check against the @bprm->cred->security value which was set in
969  * the preceding creds_for_exec call.  The argv list and envp list are reliably
970  * available in @bprm.  This hook may be called multiple times during a single
971  * execve.  @bprm contains the linux_binprm structure.
972  *
973  * Return: Returns 0 if the hook is successful and permission is granted.
974  */
975 int security_bprm_check(struct linux_binprm *bprm)
976 {
977 	int ret;
978 
979 	ret = call_int_hook(bprm_check_security, 0, bprm);
980 	if (ret)
981 		return ret;
982 	return ima_bprm_check(bprm);
983 }
984 
985 /**
986  * security_bprm_committing_creds() - Install creds for a process during exec()
987  * @bprm: binary program information
988  *
989  * Prepare to install the new security attributes of a process being
990  * transformed by an execve operation, based on the old credentials pointed to
991  * by @current->cred and the information set in @bprm->cred by the
992  * bprm_creds_for_exec hook.  @bprm points to the linux_binprm structure.  This
993  * hook is a good place to perform state changes on the process such as closing
994  * open file descriptors to which access will no longer be granted when the
995  * attributes are changed.  This is called immediately before commit_creds().
996  */
997 void security_bprm_committing_creds(struct linux_binprm *bprm)
998 {
999 	call_void_hook(bprm_committing_creds, bprm);
1000 }
1001 
1002 /**
1003  * security_bprm_committed_creds() - Tidy up after cred install during exec()
1004  * @bprm: binary program information
1005  *
1006  * Tidy up after the installation of the new security attributes of a process
1007  * being transformed by an execve operation.  The new credentials have, by this
1008  * point, been set to @current->cred.  @bprm points to the linux_binprm
1009  * structure.  This hook is a good place to perform state changes on the
1010  * process such as clearing out non-inheritable signal state.  This is called
1011  * immediately after commit_creds().
1012  */
1013 void security_bprm_committed_creds(struct linux_binprm *bprm)
1014 {
1015 	call_void_hook(bprm_committed_creds, bprm);
1016 }
1017 
1018 /**
1019  * security_fs_context_dup() - Duplicate a fs_context LSM blob
1020  * @fc: destination filesystem context
1021  * @src_fc: source filesystem context
1022  *
1023  * Allocate and attach a security structure to sc->security.  This pointer is
1024  * initialised to NULL by the caller.  @fc indicates the new filesystem context.
1025  * @src_fc indicates the original filesystem context.
1026  *
1027  * Return: Returns 0 on success or a negative error code on failure.
1028  */
1029 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1030 {
1031 	return call_int_hook(fs_context_dup, 0, fc, src_fc);
1032 }
1033 
1034 /**
1035  * security_fs_context_parse_param() - Configure a filesystem context
1036  * @fc: filesystem context
1037  * @param: filesystem parameter
1038  *
1039  * Userspace provided a parameter to configure a superblock.  The LSM can
1040  * consume the parameter or return it to the caller for use elsewhere.
1041  *
1042  * Return: If the parameter is used by the LSM it should return 0, if it is
1043  *         returned to the caller -ENOPARAM is returned, otherwise a negative
1044  *         error code is returned.
1045  */
1046 int security_fs_context_parse_param(struct fs_context *fc,
1047 				    struct fs_parameter *param)
1048 {
1049 	struct security_hook_list *hp;
1050 	int trc;
1051 	int rc = -ENOPARAM;
1052 
1053 	hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1054 			     list) {
1055 		trc = hp->hook.fs_context_parse_param(fc, param);
1056 		if (trc == 0)
1057 			rc = 0;
1058 		else if (trc != -ENOPARAM)
1059 			return trc;
1060 	}
1061 	return rc;
1062 }
1063 
1064 /**
1065  * security_sb_alloc() - Allocate a super_block LSM blob
1066  * @sb: filesystem superblock
1067  *
1068  * Allocate and attach a security structure to the sb->s_security field.  The
1069  * s_security field is initialized to NULL when the structure is allocated.
1070  * @sb contains the super_block structure to be modified.
1071  *
1072  * Return: Returns 0 if operation was successful.
1073  */
1074 int security_sb_alloc(struct super_block *sb)
1075 {
1076 	int rc = lsm_superblock_alloc(sb);
1077 
1078 	if (unlikely(rc))
1079 		return rc;
1080 	rc = call_int_hook(sb_alloc_security, 0, sb);
1081 	if (unlikely(rc))
1082 		security_sb_free(sb);
1083 	return rc;
1084 }
1085 
1086 /**
1087  * security_sb_delete() - Release super_block LSM associated objects
1088  * @sb: filesystem superblock
1089  *
1090  * Release objects tied to a superblock (e.g. inodes).  @sb contains the
1091  * super_block structure being released.
1092  */
1093 void security_sb_delete(struct super_block *sb)
1094 {
1095 	call_void_hook(sb_delete, sb);
1096 }
1097 
1098 /**
1099  * security_sb_free() - Free a super_block LSM blob
1100  * @sb: filesystem superblock
1101  *
1102  * Deallocate and clear the sb->s_security field.  @sb contains the super_block
1103  * structure to be modified.
1104  */
1105 void security_sb_free(struct super_block *sb)
1106 {
1107 	call_void_hook(sb_free_security, sb);
1108 	kfree(sb->s_security);
1109 	sb->s_security = NULL;
1110 }
1111 
1112 /**
1113  * security_free_mnt_opts() - Free memory associated with mount options
1114  * @mnt_ops: LSM processed mount options
1115  *
1116  * Free memory associated with @mnt_ops.
1117  */
1118 void security_free_mnt_opts(void **mnt_opts)
1119 {
1120 	if (!*mnt_opts)
1121 		return;
1122 	call_void_hook(sb_free_mnt_opts, *mnt_opts);
1123 	*mnt_opts = NULL;
1124 }
1125 EXPORT_SYMBOL(security_free_mnt_opts);
1126 
1127 /**
1128  * security_sb_eat_lsm_opts() - Consume LSM mount options
1129  * @options: mount options
1130  * @mnt_ops: LSM processed mount options
1131  *
1132  * Eat (scan @options) and save them in @mnt_opts.
1133  *
1134  * Return: Returns 0 on success, negative values on failure.
1135  */
1136 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1137 {
1138 	return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
1139 }
1140 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1141 
1142 /**
1143  * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1144  * @sb: filesystem superblock
1145  * @mnt_opts: new mount options
1146  *
1147  * Determine if the new mount options in @mnt_opts are allowed given the
1148  * existing mounted filesystem at @sb.  @sb superblock being compared.
1149  *
1150  * Return: Returns 0 if options are compatible.
1151  */
1152 int security_sb_mnt_opts_compat(struct super_block *sb,
1153 				void *mnt_opts)
1154 {
1155 	return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
1156 }
1157 EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1158 
1159 /**
1160  * security_sb_remount() - Verify no incompatible mount changes during remount
1161  * @sb: filesystem superblock
1162  * @mnt_opts: (re)mount options
1163  *
1164  * Extracts security system specific mount options and verifies no changes are
1165  * being made to those options.
1166  *
1167  * Return: Returns 0 if permission is granted.
1168  */
1169 int security_sb_remount(struct super_block *sb,
1170 			void *mnt_opts)
1171 {
1172 	return call_int_hook(sb_remount, 0, sb, mnt_opts);
1173 }
1174 EXPORT_SYMBOL(security_sb_remount);
1175 
1176 /**
1177  * security_sb_kern_mount() - Check if a kernel mount is allowed
1178  * @sb: filesystem superblock
1179  *
1180  * Mount this @sb if allowed by permissions.
1181  *
1182  * Return: Returns 0 if permission is granted.
1183  */
1184 int security_sb_kern_mount(struct super_block *sb)
1185 {
1186 	return call_int_hook(sb_kern_mount, 0, sb);
1187 }
1188 
1189 /**
1190  * security_sb_show_options() - Output the mount options for a superblock
1191  * @m: output file
1192  * @sb: filesystem superblock
1193  *
1194  * Show (print on @m) mount options for this @sb.
1195  *
1196  * Return: Returns 0 on success, negative values on failure.
1197  */
1198 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1199 {
1200 	return call_int_hook(sb_show_options, 0, m, sb);
1201 }
1202 
1203 /**
1204  * security_sb_statfs() - Check if accessing fs stats is allowed
1205  * @dentry: superblock handle
1206  *
1207  * Check permission before obtaining filesystem statistics for the @mnt
1208  * mountpoint.  @dentry is a handle on the superblock for the filesystem.
1209  *
1210  * Return: Returns 0 if permission is granted.
1211  */
1212 int security_sb_statfs(struct dentry *dentry)
1213 {
1214 	return call_int_hook(sb_statfs, 0, dentry);
1215 }
1216 
1217 /**
1218  * security_sb_mount() - Check permission for mounting a filesystem
1219  * @dev_name: filesystem backing device
1220  * @path: mount point
1221  * @type: filesystem type
1222  * @flags: mount flags
1223  * @data: filesystem specific data
1224  *
1225  * Check permission before an object specified by @dev_name is mounted on the
1226  * mount point named by @nd.  For an ordinary mount, @dev_name identifies a
1227  * device if the file system type requires a device.  For a remount
1228  * (@flags & MS_REMOUNT), @dev_name is irrelevant.  For a loopback/bind mount
1229  * (@flags & MS_BIND), @dev_name identifies the	pathname of the object being
1230  * mounted.
1231  *
1232  * Return: Returns 0 if permission is granted.
1233  */
1234 int security_sb_mount(const char *dev_name, const struct path *path,
1235                        const char *type, unsigned long flags, void *data)
1236 {
1237 	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
1238 }
1239 
1240 /**
1241  * security_sb_umount() - Check permission for unmounting a filesystem
1242  * @mnt: mounted filesystem
1243  * @flags: unmount flags
1244  *
1245  * Check permission before the @mnt file system is unmounted.
1246  *
1247  * Return: Returns 0 if permission is granted.
1248  */
1249 int security_sb_umount(struct vfsmount *mnt, int flags)
1250 {
1251 	return call_int_hook(sb_umount, 0, mnt, flags);
1252 }
1253 
1254 /**
1255  * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1256  * @old_path: new location for current rootfs
1257  * @new_path: location of the new rootfs
1258  *
1259  * Check permission before pivoting the root filesystem.
1260  *
1261  * Return: Returns 0 if permission is granted.
1262  */
1263 int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
1264 {
1265 	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
1266 }
1267 
1268 /**
1269  * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1270  * @sb: filesystem superblock
1271  * @mnt_opts: binary mount options
1272  * @kern_flags: kernel flags (in)
1273  * @set_kern_flags: kernel flags (out)
1274  *
1275  * Set the security relevant mount options used for a superblock.
1276  *
1277  * Return: Returns 0 on success, error on failure.
1278  */
1279 int security_sb_set_mnt_opts(struct super_block *sb,
1280 				void *mnt_opts,
1281 				unsigned long kern_flags,
1282 				unsigned long *set_kern_flags)
1283 {
1284 	return call_int_hook(sb_set_mnt_opts,
1285 				mnt_opts ? -EOPNOTSUPP : 0, sb,
1286 				mnt_opts, kern_flags, set_kern_flags);
1287 }
1288 EXPORT_SYMBOL(security_sb_set_mnt_opts);
1289 
1290 /**
1291  * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1292  * @olddb: source superblock
1293  * @newdb: destination superblock
1294  * @kern_flags: kernel flags (in)
1295  * @set_kern_flags: kernel flags (out)
1296  *
1297  * Copy all security options from a given superblock to another.
1298  *
1299  * Return: Returns 0 on success, error on failure.
1300  */
1301 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
1302 				struct super_block *newsb,
1303 				unsigned long kern_flags,
1304 				unsigned long *set_kern_flags)
1305 {
1306 	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
1307 				kern_flags, set_kern_flags);
1308 }
1309 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1310 
1311 /**
1312  * security_move_mount() - Check permissions for moving a mount
1313  * @from_path: source mount point
1314  * @to_path: destination mount point
1315  *
1316  * Check permission before a mount is moved.
1317  *
1318  * Return: Returns 0 if permission is granted.
1319  */
1320 int security_move_mount(const struct path *from_path, const struct path *to_path)
1321 {
1322 	return call_int_hook(move_mount, 0, from_path, to_path);
1323 }
1324 
1325 /**
1326  * security_path_notify() - Check if setting a watch is allowed
1327  * @path: file path
1328  * @mask: event mask
1329  * @obj_type: file path type
1330  *
1331  * Check permissions before setting a watch on events as defined by @mask, on
1332  * an object at @path, whose type is defined by @obj_type.
1333  *
1334  * Return: Returns 0 if permission is granted.
1335  */
1336 int security_path_notify(const struct path *path, u64 mask,
1337 				unsigned int obj_type)
1338 {
1339 	return call_int_hook(path_notify, 0, path, mask, obj_type);
1340 }
1341 
1342 /**
1343  * security_inode_alloc() - Allocate an inode LSM blob
1344  * @inode: the inode
1345  *
1346  * Allocate and attach a security structure to @inode->i_security.  The
1347  * i_security field is initialized to NULL when the inode structure is
1348  * allocated.
1349  *
1350  * Return: Return 0 if operation was successful.
1351  */
1352 int security_inode_alloc(struct inode *inode)
1353 {
1354 	int rc = lsm_inode_alloc(inode);
1355 
1356 	if (unlikely(rc))
1357 		return rc;
1358 	rc = call_int_hook(inode_alloc_security, 0, inode);
1359 	if (unlikely(rc))
1360 		security_inode_free(inode);
1361 	return rc;
1362 }
1363 
1364 static void inode_free_by_rcu(struct rcu_head *head)
1365 {
1366 	/*
1367 	 * The rcu head is at the start of the inode blob
1368 	 */
1369 	kmem_cache_free(lsm_inode_cache, head);
1370 }
1371 
1372 /**
1373  * security_inode_free() - Free an inode's LSM blob
1374  * @inode: the inode
1375  *
1376  * Deallocate the inode security structure and set @inode->i_security to NULL.
1377  */
1378 void security_inode_free(struct inode *inode)
1379 {
1380 	integrity_inode_free(inode);
1381 	call_void_hook(inode_free_security, inode);
1382 	/*
1383 	 * The inode may still be referenced in a path walk and
1384 	 * a call to security_inode_permission() can be made
1385 	 * after inode_free_security() is called. Ideally, the VFS
1386 	 * wouldn't do this, but fixing that is a much harder
1387 	 * job. For now, simply free the i_security via RCU, and
1388 	 * leave the current inode->i_security pointer intact.
1389 	 * The inode will be freed after the RCU grace period too.
1390 	 */
1391 	if (inode->i_security)
1392 		call_rcu((struct rcu_head *)inode->i_security,
1393 				inode_free_by_rcu);
1394 }
1395 
1396 /**
1397  * security_dentry_init_security() - 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  * @xattr_name: name of the security/LSM xattr
1402  * @ctx: pointer to the resulting LSM context
1403  * @ctxlen: length of @ctx
1404  *
1405  * Compute a context for a dentry as the inode is not yet available since NFSv4
1406  * has no label backed by an EA anyway.  It is important to note that
1407  * @xattr_name does not need to be free'd by the caller, it is a static string.
1408  *
1409  * Return: Returns 0 on success, negative values on failure.
1410  */
1411 int security_dentry_init_security(struct dentry *dentry, int mode,
1412 				  const struct qstr *name,
1413 				  const char **xattr_name, void **ctx,
1414 				  u32 *ctxlen)
1415 {
1416 	struct security_hook_list *hp;
1417 	int rc;
1418 
1419 	/*
1420 	 * Only one module will provide a security context.
1421 	 */
1422 	hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security, list) {
1423 		rc = hp->hook.dentry_init_security(dentry, mode, name,
1424 						   xattr_name, ctx, ctxlen);
1425 		if (rc != LSM_RET_DEFAULT(dentry_init_security))
1426 			return rc;
1427 	}
1428 	return LSM_RET_DEFAULT(dentry_init_security);
1429 }
1430 EXPORT_SYMBOL(security_dentry_init_security);
1431 
1432 /**
1433  * security_dentry_create_files_as() - Perform dentry initialization
1434  * @dentry: the dentry to initialize
1435  * @mode: mode used to determine resource type
1436  * @name: name of the last path component
1437  * @old: creds to use for LSM context calculations
1438  * @new: creds to modify
1439  *
1440  * Compute a context for a dentry as the inode is not yet available and set
1441  * that context in passed in creds so that new files are created using that
1442  * context. Context is calculated using the passed in creds and not the creds
1443  * of the caller.
1444  *
1445  * Return: Returns 0 on success, error on failure.
1446  */
1447 int security_dentry_create_files_as(struct dentry *dentry, int mode,
1448 				    struct qstr *name,
1449 				    const struct cred *old, struct cred *new)
1450 {
1451 	return call_int_hook(dentry_create_files_as, 0, dentry, mode,
1452 				name, old, new);
1453 }
1454 EXPORT_SYMBOL(security_dentry_create_files_as);
1455 
1456 /**
1457  * security_inode_init_security() - Initialize an inode's LSM context
1458  * @inode: the inode
1459  * @dir: parent directory
1460  * @qstr: last component of the pathname
1461  * @initxattrs: callback function to write xattrs
1462  * @fs_data: filesystem specific data
1463  *
1464  * Obtain the security attribute name suffix and value to set on a newly
1465  * created inode and set up the incore security field for the new inode.  This
1466  * hook is called by the fs code as part of the inode creation transaction and
1467  * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1468  * hooks called by the VFS.  The hook function is expected to allocate the name
1469  * and value via kmalloc, with the caller being responsible for calling kfree
1470  * after using them.  If the security module does not use security attributes
1471  * or does not wish to put a security attribute on this particular inode, then
1472  * it should return -EOPNOTSUPP to skip this processing.
1473  *
1474  * Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is
1475  * needed, or -ENOMEM on memory allocation failure.
1476  */
1477 int security_inode_init_security(struct inode *inode, struct inode *dir,
1478 				 const struct qstr *qstr,
1479 				 const initxattrs initxattrs, void *fs_data)
1480 {
1481 	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
1482 	struct xattr *lsm_xattr, *evm_xattr, *xattr;
1483 	int ret;
1484 
1485 	if (unlikely(IS_PRIVATE(inode)))
1486 		return 0;
1487 
1488 	if (!initxattrs)
1489 		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
1490 				     dir, qstr, NULL, NULL, NULL);
1491 	memset(new_xattrs, 0, sizeof(new_xattrs));
1492 	lsm_xattr = new_xattrs;
1493 	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
1494 						&lsm_xattr->name,
1495 						&lsm_xattr->value,
1496 						&lsm_xattr->value_len);
1497 	if (ret)
1498 		goto out;
1499 
1500 	evm_xattr = lsm_xattr + 1;
1501 	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1502 	if (ret)
1503 		goto out;
1504 	ret = initxattrs(inode, new_xattrs, fs_data);
1505 out:
1506 	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
1507 		kfree(xattr->value);
1508 	return (ret == -EOPNOTSUPP) ? 0 : ret;
1509 }
1510 EXPORT_SYMBOL(security_inode_init_security);
1511 
1512 /**
1513  * security_inode_init_security_anon() - Initialize an anonymous inode
1514  * @inode: the inode
1515  * @name: the anonymous inode class
1516  * @context_inode: an optional related inode
1517  *
1518  * Set up the incore security field for the new anonymous inode and return
1519  * whether the inode creation is permitted by the security module or not.
1520  *
1521  * Return: Returns 0 on success, -EACCES if the security module denies the
1522  * creation of this inode, or another -errno upon other errors.
1523  */
1524 int security_inode_init_security_anon(struct inode *inode,
1525 				      const struct qstr *name,
1526 				      const struct inode *context_inode)
1527 {
1528 	return call_int_hook(inode_init_security_anon, 0, inode, name,
1529 			     context_inode);
1530 }
1531 
1532 int security_old_inode_init_security(struct inode *inode, struct inode *dir,
1533 				     const struct qstr *qstr, const char **name,
1534 				     void **value, size_t *len)
1535 {
1536 	if (unlikely(IS_PRIVATE(inode)))
1537 		return -EOPNOTSUPP;
1538 	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1539 			     qstr, name, value, len);
1540 }
1541 EXPORT_SYMBOL(security_old_inode_init_security);
1542 
1543 #ifdef CONFIG_SECURITY_PATH
1544 /**
1545  * security_path_mknod() - Check if creating a special file is allowed
1546  * @dir: parent directory
1547  * @dentry: new file
1548  * @mode: new file mode
1549  * @dev: device number
1550  *
1551  * Check permissions when creating a file. Note that this hook is called even
1552  * if mknod operation is being done for a regular file.
1553  *
1554  * Return: Returns 0 if permission is granted.
1555  */
1556 int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
1557 			unsigned int dev)
1558 {
1559 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1560 		return 0;
1561 	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1562 }
1563 EXPORT_SYMBOL(security_path_mknod);
1564 
1565 /**
1566  * security_path_mkdir() - Check if creating a new directory is allowed
1567  * @dir: parent directory
1568  * @dentry: new directory
1569  * @mode: new directory mode
1570  *
1571  * Check permissions to create a new directory in the existing directory.
1572  *
1573  * Return: Returns 0 if permission is granted.
1574  */
1575 int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
1576 {
1577 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1578 		return 0;
1579 	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1580 }
1581 EXPORT_SYMBOL(security_path_mkdir);
1582 
1583 /**
1584  * security_path_rmdir() - Check if removing a directory is allowed
1585  * @dir: parent directory
1586  * @dentry: directory to remove
1587  *
1588  * Check the permission to remove a directory.
1589  *
1590  * Return: Returns 0 if permission is granted.
1591  */
1592 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1593 {
1594 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1595 		return 0;
1596 	return call_int_hook(path_rmdir, 0, dir, dentry);
1597 }
1598 
1599 /**
1600  * security_path_unlink() - Check if removing a hard link is allowed
1601  * @dir: parent directory
1602  * @dentry: file
1603  *
1604  * Check the permission to remove a hard link to a file.
1605  *
1606  * Return: Returns 0 if permission is granted.
1607  */
1608 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1609 {
1610 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1611 		return 0;
1612 	return call_int_hook(path_unlink, 0, dir, dentry);
1613 }
1614 EXPORT_SYMBOL(security_path_unlink);
1615 
1616 /**
1617  * security_path_symlink() - Check if creating a symbolic link is allowed
1618  * @dir: parent directory
1619  * @dentry: symbolic link
1620  * @old_name: file pathname
1621  *
1622  * Check the permission to create a symbolic link to a file.
1623  *
1624  * Return: Returns 0 if permission is granted.
1625  */
1626 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1627 			  const char *old_name)
1628 {
1629 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1630 		return 0;
1631 	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1632 }
1633 
1634 /**
1635  * security_path_link - Check if creating a hard link is allowed
1636  * @old_dentry: existing file
1637  * @new_dir: new parent directory
1638  * @new_dentry: new link
1639  *
1640  * Check permission before creating a new hard link to a file.
1641  *
1642  * Return: Returns 0 if permission is granted.
1643  */
1644 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1645 		       struct dentry *new_dentry)
1646 {
1647 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1648 		return 0;
1649 	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1650 }
1651 
1652 /**
1653  * security_path_rename() - Check if renaming a file is allowed
1654  * @old_dir: parent directory of the old file
1655  * @old_dentry: the old file
1656  * @new_dir: parent directory of the new file
1657  * @new_dentry: the new file
1658  * @flags: flags
1659  *
1660  * Check for permission to rename a file or directory.
1661  *
1662  * Return: Returns 0 if permission is granted.
1663  */
1664 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1665 			 const struct path *new_dir, struct dentry *new_dentry,
1666 			 unsigned int flags)
1667 {
1668 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1669 		     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1670 		return 0;
1671 
1672 	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1673 				new_dentry, flags);
1674 }
1675 EXPORT_SYMBOL(security_path_rename);
1676 
1677 /**
1678  * security_path_truncate() - Check if truncating a file is allowed
1679  * @path: file
1680  *
1681  * Check permission before truncating the file indicated by path.  Note that
1682  * truncation permissions may also be checked based on already opened files,
1683  * using the security_file_truncate() hook.
1684  *
1685  * Return: Returns 0 if permission is granted.
1686  */
1687 int security_path_truncate(const struct path *path)
1688 {
1689 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1690 		return 0;
1691 	return call_int_hook(path_truncate, 0, path);
1692 }
1693 
1694 /**
1695  * security_path_chmod() - Check if changing the file's mode is allowed
1696  * @path: file
1697  * @mode: new mode
1698  *
1699  * Check for permission to change a mode of the file @path. The new mode is
1700  * specified in @mode which is a bitmask of constants from
1701  * <include/uapi/linux/stat.h>.
1702  *
1703  * Return: Returns 0 if permission is granted.
1704  */
1705 int security_path_chmod(const struct path *path, umode_t mode)
1706 {
1707 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1708 		return 0;
1709 	return call_int_hook(path_chmod, 0, path, mode);
1710 }
1711 
1712 /**
1713  * security_path_chown() - Check if changing the file's owner/group is allowed
1714  * @path: file
1715  * @uid: file owner
1716  * @gid: file group
1717  *
1718  * Check for permission to change owner/group of a file or directory.
1719  *
1720  * Return: Returns 0 if permission is granted.
1721  */
1722 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1723 {
1724 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1725 		return 0;
1726 	return call_int_hook(path_chown, 0, path, uid, gid);
1727 }
1728 
1729 /**
1730  * security_path_chroot() - Check if changing the root directory is allowed
1731  * @path: directory
1732  *
1733  * Check for permission to change root directory.
1734  *
1735  * Return: Returns 0 if permission is granted.
1736  */
1737 int security_path_chroot(const struct path *path)
1738 {
1739 	return call_int_hook(path_chroot, 0, path);
1740 }
1741 #endif
1742 
1743 /**
1744  * security_inode_create() - Check if creating a file is allowed
1745  * @dir: the parent directory
1746  * @dentry: the file being created
1747  * @mode: requested file mode
1748  *
1749  * Check permission to create a regular file.
1750  *
1751  * Return: Returns 0 if permission is granted.
1752  */
1753 int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1754 {
1755 	if (unlikely(IS_PRIVATE(dir)))
1756 		return 0;
1757 	return call_int_hook(inode_create, 0, dir, dentry, mode);
1758 }
1759 EXPORT_SYMBOL_GPL(security_inode_create);
1760 
1761 /**
1762  * security_inode_link() - Check if creating a hard link is allowed
1763  * @old_dentry: existing file
1764  * @dir: new parent directory
1765  * @new_dentry: new link
1766  *
1767  * Check permission before creating a new hard link to a file.
1768  *
1769  * Return: Returns 0 if permission is granted.
1770  */
1771 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1772 			 struct dentry *new_dentry)
1773 {
1774 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1775 		return 0;
1776 	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
1777 }
1778 
1779 /**
1780  * security_inode_unlink() - Check if removing a hard link is allowed
1781  * @dir: parent directory
1782  * @dentry: file
1783  *
1784  * Check the permission to remove a hard link to a file.
1785  *
1786  * Return: Returns 0 if permission is granted.
1787  */
1788 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1789 {
1790 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1791 		return 0;
1792 	return call_int_hook(inode_unlink, 0, dir, dentry);
1793 }
1794 
1795 /**
1796  * security_inode_symlink() Check if creating a symbolic link is allowed
1797  * @dir: parent directory
1798  * @dentry: symbolic link
1799  * @old_name: existing filename
1800  *
1801  * Check the permission to create a symbolic link to a file.
1802  *
1803  * Return: Returns 0 if permission is granted.
1804  */
1805 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1806 			    const char *old_name)
1807 {
1808 	if (unlikely(IS_PRIVATE(dir)))
1809 		return 0;
1810 	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
1811 }
1812 
1813 /**
1814  * security_inode_mkdir() - Check if creation a new director is allowed
1815  * @dir: parent directory
1816  * @dentry: new directory
1817  * @mode: new directory mode
1818  *
1819  * Check permissions to create a new directory in the existing directory
1820  * associated with inode structure @dir.
1821  *
1822  * Return: Returns 0 if permission is granted.
1823  */
1824 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1825 {
1826 	if (unlikely(IS_PRIVATE(dir)))
1827 		return 0;
1828 	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
1829 }
1830 EXPORT_SYMBOL_GPL(security_inode_mkdir);
1831 
1832 /**
1833  * security_inode_rmdir() - Check if removing a directory is allowed
1834  * @dir: parent directory
1835  * @dentry: directory to be removed
1836  *
1837  * Check the permission to remove a directory.
1838  *
1839  * Return: Returns 0 if permission is granted.
1840  */
1841 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1842 {
1843 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1844 		return 0;
1845 	return call_int_hook(inode_rmdir, 0, dir, dentry);
1846 }
1847 
1848 /**
1849  * security_inode_mknod() - Check if creating a special file is allowed
1850  * @dir: parent directory
1851  * @dentry: new file
1852  * @mode: new file mode
1853  * @dev: device number
1854  *
1855  * Check permissions when creating a special file (or a socket or a fifo file
1856  * created via the mknod system call).  Note that if mknod operation is being
1857  * done for a regular file, then the create hook will be called and not this
1858  * hook.
1859  *
1860  * Return: Returns 0 if permission is granted.
1861  */
1862 int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1863 {
1864 	if (unlikely(IS_PRIVATE(dir)))
1865 		return 0;
1866 	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
1867 }
1868 
1869 /**
1870  * security_inode_rename() - Check if renaming a file is allowed
1871  * @old_dir: parent directory of the old file
1872  * @old_dentry: the old file
1873  * @new_dir: parent directory of the new file
1874  * @new_dentry: the new file
1875  * @flags: flags
1876  *
1877  * Check for permission to rename a file or directory.
1878  *
1879  * Return: Returns 0 if permission is granted.
1880  */
1881 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
1882 			   struct inode *new_dir, struct dentry *new_dentry,
1883 			   unsigned int flags)
1884 {
1885         if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1886             (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1887 		return 0;
1888 
1889 	if (flags & RENAME_EXCHANGE) {
1890 		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
1891 						     old_dir, old_dentry);
1892 		if (err)
1893 			return err;
1894 	}
1895 
1896 	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
1897 					   new_dir, new_dentry);
1898 }
1899 
1900 /**
1901  * security_inode_readlink() - Check if reading a symbolic link is allowed
1902  * @dentry: link
1903  *
1904  * Check the permission to read the symbolic link.
1905  *
1906  * Return: Returns 0 if permission is granted.
1907  */
1908 int security_inode_readlink(struct dentry *dentry)
1909 {
1910 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1911 		return 0;
1912 	return call_int_hook(inode_readlink, 0, dentry);
1913 }
1914 
1915 /**
1916  * security_inode_follow_link() - Check if following a symbolic link is allowed
1917  * @dentry: link dentry
1918  * @inode: link inode
1919  * @rcu: true if in RCU-walk mode
1920  *
1921  * Check permission to follow a symbolic link when looking up a pathname.  If
1922  * @rcu is true, @inode is not stable.
1923  *
1924  * Return: Returns 0 if permission is granted.
1925  */
1926 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1927 			       bool rcu)
1928 {
1929 	if (unlikely(IS_PRIVATE(inode)))
1930 		return 0;
1931 	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
1932 }
1933 
1934 /**
1935  * security_inode_permission() - Check if accessing an inode is allowed
1936  * @inode: inode
1937  * @mask: access mask
1938  *
1939  * Check permission before accessing an inode.  This hook is called by the
1940  * existing Linux permission function, so a security module can use it to
1941  * provide additional checking for existing Linux permission checks.  Notice
1942  * that this hook is called when a file is opened (as well as many other
1943  * operations), whereas the file_security_ops permission hook is called when
1944  * the actual read/write operations are performed.
1945  *
1946  * Return: Returns 0 if permission is granted.
1947  */
1948 int security_inode_permission(struct inode *inode, int mask)
1949 {
1950 	if (unlikely(IS_PRIVATE(inode)))
1951 		return 0;
1952 	return call_int_hook(inode_permission, 0, inode, mask);
1953 }
1954 
1955 /**
1956  * security_inode_setattr() - Check if setting file attributes is allowed
1957  * @idmap: idmap of the mount
1958  * @dentry: file
1959  * @attr: new attributes
1960  *
1961  * Check permission before setting file attributes.  Note that the kernel call
1962  * to notify_change is performed from several locations, whenever file
1963  * attributes change (such as when a file is truncated, chown/chmod operations,
1964  * transferring disk quotas, etc).
1965  *
1966  * Return: Returns 0 if permission is granted.
1967  */
1968 int security_inode_setattr(struct mnt_idmap *idmap,
1969 			   struct dentry *dentry, struct iattr *attr)
1970 {
1971 	int ret;
1972 
1973 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1974 		return 0;
1975 	ret = call_int_hook(inode_setattr, 0, dentry, attr);
1976 	if (ret)
1977 		return ret;
1978 	return evm_inode_setattr(idmap, dentry, attr);
1979 }
1980 EXPORT_SYMBOL_GPL(security_inode_setattr);
1981 
1982 /**
1983  * security_inode_getattr() - Check if getting file attributes is allowed
1984  * @path: file
1985  *
1986  * Check permission before obtaining file attributes.
1987  *
1988  * Return: Returns 0 if permission is granted.
1989  */
1990 int security_inode_getattr(const struct path *path)
1991 {
1992 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1993 		return 0;
1994 	return call_int_hook(inode_getattr, 0, path);
1995 }
1996 
1997 /**
1998  * security_inode_setxattr() - Check if setting file xattrs is allowed
1999  * @idmap: idmap of the mount
2000  * @dentry: file
2001  * @name: xattr name
2002  * @value: xattr value
2003  * @flags: flags
2004  *
2005  * Check permission before setting the extended attributes.
2006  *
2007  * Return: Returns 0 if permission is granted.
2008  */
2009 int security_inode_setxattr(struct mnt_idmap *idmap,
2010 			    struct dentry *dentry, const char *name,
2011 			    const void *value, size_t size, int flags)
2012 {
2013 	int ret;
2014 
2015 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2016 		return 0;
2017 	/*
2018 	 * SELinux and Smack integrate the cap call,
2019 	 * so assume that all LSMs supplying this call do so.
2020 	 */
2021 	ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
2022 			    size, flags);
2023 
2024 	if (ret == 1)
2025 		ret = cap_inode_setxattr(dentry, name, value, size, flags);
2026 	if (ret)
2027 		return ret;
2028 	ret = ima_inode_setxattr(dentry, name, value, size);
2029 	if (ret)
2030 		return ret;
2031 	return evm_inode_setxattr(idmap, dentry, name, value, size);
2032 }
2033 
2034 /**
2035  * security_inode_set_acl() - Check if setting posix acls is allowed
2036  * @idmap: idmap of the mount
2037  * @dentry: file
2038  * @acl_name: acl name
2039  * @kacl: acl struct
2040  *
2041  * Check permission before setting posix acls, the posix acls in @kacl are
2042  * identified by @acl_name.
2043  *
2044  * Return: Returns 0 if permission is granted.
2045  */
2046 int security_inode_set_acl(struct mnt_idmap *idmap,
2047 			   struct dentry *dentry, const char *acl_name,
2048 			   struct posix_acl *kacl)
2049 {
2050 	int ret;
2051 
2052 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2053 		return 0;
2054 	ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
2055 			    kacl);
2056 	if (ret)
2057 		return ret;
2058 	ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
2059 	if (ret)
2060 		return ret;
2061 	return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
2062 }
2063 
2064 /**
2065  * security_inode_get_acl() - Check if reading posix acls is allowed
2066  * @idmap: idmap of the mount
2067  * @dentry: file
2068  * @acl_name: acl name
2069  *
2070  * Check permission before getting osix acls, the posix acls are identified by
2071  * @acl_name.
2072  *
2073  * Return: Returns 0 if permission is granted.
2074  */
2075 int security_inode_get_acl(struct mnt_idmap *idmap,
2076 			   struct dentry *dentry, const char *acl_name)
2077 {
2078 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2079 		return 0;
2080 	return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
2081 }
2082 
2083 /**
2084  * security_inode_remove_acl() - Check if removing a posix acl is allowed
2085  * @idmap: idmap of the mount
2086  * @dentry: file
2087  * @acl_name: acl name
2088  *
2089  * Check permission before removing posix acls, the posix acls are identified
2090  * by @acl_name.
2091  *
2092  * Return: Returns 0 if permission is granted.
2093  */
2094 int security_inode_remove_acl(struct mnt_idmap *idmap,
2095 			      struct dentry *dentry, const char *acl_name)
2096 {
2097 	int ret;
2098 
2099 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2100 		return 0;
2101 	ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
2102 	if (ret)
2103 		return ret;
2104 	ret = ima_inode_remove_acl(idmap, dentry, acl_name);
2105 	if (ret)
2106 		return ret;
2107 	return evm_inode_remove_acl(idmap, dentry, acl_name);
2108 }
2109 
2110 /**
2111  * security_inode_post_setxattr() - Update the inode after a setxattr operation
2112  * @dentry: file
2113  * @name: xattr name
2114  * @value: xattr value
2115  * @size: xattr value size
2116  * @flags: flags
2117  *
2118  * Update inode security field after successful setxattr operation.
2119  */
2120 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2121 				  const void *value, size_t size, int flags)
2122 {
2123 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2124 		return;
2125 	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2126 	evm_inode_post_setxattr(dentry, name, value, size);
2127 }
2128 
2129 /**
2130  * security_inode_getxattr() - Check if xattr access is allowed
2131  * @dentry: file
2132  * @name: xattr name
2133  *
2134  * Check permission before obtaining the extended attributes identified by
2135  * @name for @dentry.
2136  *
2137  * Return: Returns 0 if permission is granted.
2138  */
2139 int security_inode_getxattr(struct dentry *dentry, const char *name)
2140 {
2141 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2142 		return 0;
2143 	return call_int_hook(inode_getxattr, 0, dentry, name);
2144 }
2145 
2146 /**
2147  * security_inode_listxattr() - Check if listing xattrs is allowed
2148  * @dentry: file
2149  *
2150  * Check permission before obtaining the list of extended attribute names for
2151  * @dentry.
2152  *
2153  * Return: Returns 0 if permission is granted.
2154  */
2155 int security_inode_listxattr(struct dentry *dentry)
2156 {
2157 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2158 		return 0;
2159 	return call_int_hook(inode_listxattr, 0, dentry);
2160 }
2161 
2162 /**
2163  * security_inode_removexattr() - Check if removing an xattr is allowed
2164  * @idmap: idmap of the mount
2165  * @dentry: file
2166  * @name: xattr name
2167  *
2168  * Check permission before removing the extended attribute identified by @name
2169  * for @dentry.
2170  *
2171  * Return: Returns 0 if permission is granted.
2172  */
2173 int security_inode_removexattr(struct mnt_idmap *idmap,
2174 			       struct dentry *dentry, const char *name)
2175 {
2176 	int ret;
2177 
2178 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2179 		return 0;
2180 	/*
2181 	 * SELinux and Smack integrate the cap call,
2182 	 * so assume that all LSMs supplying this call do so.
2183 	 */
2184 	ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
2185 	if (ret == 1)
2186 		ret = cap_inode_removexattr(idmap, dentry, name);
2187 	if (ret)
2188 		return ret;
2189 	ret = ima_inode_removexattr(dentry, name);
2190 	if (ret)
2191 		return ret;
2192 	return evm_inode_removexattr(idmap, dentry, name);
2193 }
2194 
2195 /**
2196  * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2197  * @dentry: associated dentry
2198  *
2199  * Called when an inode has been changed to determine if
2200  * security_inode_killpriv() should be called.
2201  *
2202  * Return: Return <0 on error to abort the inode change operation, return 0 if
2203  *         security_inode_killpriv() does not need to be called, return >0 if
2204  *         security_inode_killpriv() does need to be called.
2205  */
2206 int security_inode_need_killpriv(struct dentry *dentry)
2207 {
2208 	return call_int_hook(inode_need_killpriv, 0, dentry);
2209 }
2210 
2211 /**
2212  * security_inode_killpriv() - The setuid bit is removed, update LSM state
2213  * @idmap: idmap of the mount
2214  * @dentry: associated dentry
2215  *
2216  * The @dentry's setuid bit is being removed.  Remove similar security labels.
2217  * Called with the dentry->d_inode->i_mutex held.
2218  *
2219  * Return: Return 0 on success.  If error is returned, then the operation
2220  *         causing setuid bit removal is failed.
2221  */
2222 int security_inode_killpriv(struct mnt_idmap *idmap,
2223 			    struct dentry *dentry)
2224 {
2225 	return call_int_hook(inode_killpriv, 0, idmap, dentry);
2226 }
2227 
2228 /**
2229  * security_inode_getsecurity() - Get the xattr security label of an inode
2230  * @idmap: idmap of the mount
2231  * @inode: inode
2232  * @name: xattr name
2233  * @buffer: security label buffer
2234  * @alloc: allocation flag
2235  *
2236  * Retrieve a copy of the extended attribute representation of the security
2237  * label associated with @name for @inode via @buffer.  Note that @name is the
2238  * remainder of the attribute name after the security prefix has been removed.
2239  * @alloc is used to specify if the call should return a value via the buffer
2240  * or just the value length.
2241  *
2242  * Return: Returns size of buffer on success.
2243  */
2244 int security_inode_getsecurity(struct mnt_idmap *idmap,
2245 			       struct inode *inode, const char *name,
2246 			       void **buffer, bool alloc)
2247 {
2248 	struct security_hook_list *hp;
2249 	int rc;
2250 
2251 	if (unlikely(IS_PRIVATE(inode)))
2252 		return LSM_RET_DEFAULT(inode_getsecurity);
2253 	/*
2254 	 * Only one module will provide an attribute with a given name.
2255 	 */
2256 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
2257 		rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer, alloc);
2258 		if (rc != LSM_RET_DEFAULT(inode_getsecurity))
2259 			return rc;
2260 	}
2261 	return LSM_RET_DEFAULT(inode_getsecurity);
2262 }
2263 
2264 /**
2265  * security_inode_setsecurity() - Set the xattr security label of an inode
2266  * @inode: inode
2267  * @name: xattr name
2268  * @value: security label
2269  * @size: length of security label
2270  * @flags: flags
2271  *
2272  * Set the security label associated with @name for @inode from the extended
2273  * attribute value @value.  @size indicates the size of the @value in bytes.
2274  * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2275  * remainder of the attribute name after the security. prefix has been removed.
2276  *
2277  * Return: Returns 0 on success.
2278  */
2279 int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
2280 {
2281 	struct security_hook_list *hp;
2282 	int rc;
2283 
2284 	if (unlikely(IS_PRIVATE(inode)))
2285 		return LSM_RET_DEFAULT(inode_setsecurity);
2286 	/*
2287 	 * Only one module will provide an attribute with a given name.
2288 	 */
2289 	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
2290 		rc = hp->hook.inode_setsecurity(inode, name, value, size,
2291 								flags);
2292 		if (rc != LSM_RET_DEFAULT(inode_setsecurity))
2293 			return rc;
2294 	}
2295 	return LSM_RET_DEFAULT(inode_setsecurity);
2296 }
2297 
2298 /**
2299  * security_inode_listsecurity() - List the xattr security label names
2300  * @inode: inode
2301  * @buffer: buffer
2302  * @buffer_size: size of buffer
2303  *
2304  * Copy the extended attribute names for the security labels associated with
2305  * @inode into @buffer.  The maximum size of @buffer is specified by
2306  * @buffer_size.  @buffer may be NULL to request the size of the buffer
2307  * required.
2308  *
2309  * Return: Returns number of bytes used/required on success.
2310  */
2311 int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2312 {
2313 	if (unlikely(IS_PRIVATE(inode)))
2314 		return 0;
2315 	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
2316 }
2317 EXPORT_SYMBOL(security_inode_listsecurity);
2318 
2319 /**
2320  * security_inode_getsecid() - Get an inode's secid
2321  * @inode: inode
2322  * @secid: secid to return
2323  *
2324  * Get the secid associated with the node.  In case of failure, @secid will be
2325  * set to zero.
2326  */
2327 void security_inode_getsecid(struct inode *inode, u32 *secid)
2328 {
2329 	call_void_hook(inode_getsecid, inode, secid);
2330 }
2331 
2332 /**
2333  * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2334  * @src: union dentry of copy-up file
2335  * @new: newly created creds
2336  *
2337  * A file is about to be copied up from lower layer to upper layer of overlay
2338  * filesystem. Security module can prepare a set of new creds and modify as
2339  * need be and return new creds. Caller will switch to new creds temporarily to
2340  * create new file and release newly allocated creds.
2341  *
2342  * Return: Returns 0 on success or a negative error code on error.
2343  */
2344 int security_inode_copy_up(struct dentry *src, struct cred **new)
2345 {
2346 	return call_int_hook(inode_copy_up, 0, src, new);
2347 }
2348 EXPORT_SYMBOL(security_inode_copy_up);
2349 
2350 /**
2351  * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2352  * @name: xattr name
2353  *
2354  * Filter the xattrs being copied up when a unioned file is copied up from a
2355  * lower layer to the union/overlay layer.   The caller is responsible for
2356  * reading and writing the xattrs, this hook is merely a filter.
2357  *
2358  * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2359  *         if the security module does not know about attribute, or a negative
2360  *         error code to abort the copy up.
2361  */
2362 int security_inode_copy_up_xattr(const char *name)
2363 {
2364 	struct security_hook_list *hp;
2365 	int rc;
2366 
2367 	/*
2368 	 * The implementation can return 0 (accept the xattr), 1 (discard the
2369 	 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2370 	 * any other error code incase of an error.
2371 	 */
2372 	hlist_for_each_entry(hp,
2373 		&security_hook_heads.inode_copy_up_xattr, list) {
2374 		rc = hp->hook.inode_copy_up_xattr(name);
2375 		if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2376 			return rc;
2377 	}
2378 
2379 	return LSM_RET_DEFAULT(inode_copy_up_xattr);
2380 }
2381 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2382 
2383 /**
2384  * security_kernfs_init_security() - Init LSM context for a kernfs node
2385  * @kn_dir: parent kernfs node
2386  * @kn: the kernfs node to initialize
2387  *
2388  * Initialize the security context of a newly created kernfs node based on its
2389  * own and its parent's attributes.
2390  *
2391  * Return: Returns 0 if permission is granted.
2392  */
2393 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2394 				  struct kernfs_node *kn)
2395 {
2396 	return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2397 }
2398 
2399 /**
2400  * security_file_permission() - Check file permissions
2401  * @file: file
2402  * @mask: requested permissions
2403  *
2404  * Check file permissions before accessing an open file.  This hook is called
2405  * by various operations that read or write files.  A security module can use
2406  * this hook to perform additional checking on these operations, e.g. to
2407  * revalidate permissions on use to support privilege bracketing or policy
2408  * changes.  Notice that this hook is used when the actual read/write
2409  * operations are performed, whereas the inode_security_ops hook is called when
2410  * a file is opened (as well as many other operations).  Although this hook can
2411  * be used to revalidate permissions for various system call operations that
2412  * read or write files, it does not address the revalidation of permissions for
2413  * memory-mapped files.  Security modules must handle this separately if they
2414  * need such revalidation.
2415  *
2416  * Return: Returns 0 if permission is granted.
2417  */
2418 int security_file_permission(struct file *file, int mask)
2419 {
2420 	int ret;
2421 
2422 	ret = call_int_hook(file_permission, 0, file, mask);
2423 	if (ret)
2424 		return ret;
2425 
2426 	return fsnotify_perm(file, mask);
2427 }
2428 
2429 /**
2430  * security_file_alloc() - Allocate and init a file's LSM blob
2431  * @file: the file
2432  *
2433  * Allocate and attach a security structure to the file->f_security field.  The
2434  * security field is initialized to NULL when the structure is first created.
2435  *
2436  * Return: Return 0 if the hook is successful and permission is granted.
2437  */
2438 int security_file_alloc(struct file *file)
2439 {
2440 	int rc = lsm_file_alloc(file);
2441 
2442 	if (rc)
2443 		return rc;
2444 	rc = call_int_hook(file_alloc_security, 0, file);
2445 	if (unlikely(rc))
2446 		security_file_free(file);
2447 	return rc;
2448 }
2449 
2450 /**
2451  * security_file_free() - Free a file's LSM blob
2452  * @file: the file
2453  *
2454  * Deallocate and free any security structures stored in file->f_security.
2455  */
2456 void security_file_free(struct file *file)
2457 {
2458 	void *blob;
2459 
2460 	call_void_hook(file_free_security, file);
2461 
2462 	blob = file->f_security;
2463 	if (blob) {
2464 		file->f_security = NULL;
2465 		kmem_cache_free(lsm_file_cache, blob);
2466 	}
2467 }
2468 
2469 /**
2470  * security_file_ioctl() - Check if an ioctl is allowed
2471  * @file: associated file
2472  * @cmd: ioctl cmd
2473  * @arg: ioctl arguments
2474  *
2475  * Check permission for an ioctl operation on @file.  Note that @arg sometimes
2476  * represents a user space pointer; in other cases, it may be a simple integer
2477  * value.  When @arg represents a user space pointer, it should never be used
2478  * by the security module.
2479  *
2480  * Return: Returns 0 if permission is granted.
2481  */
2482 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2483 {
2484 	return call_int_hook(file_ioctl, 0, file, cmd, arg);
2485 }
2486 EXPORT_SYMBOL_GPL(security_file_ioctl);
2487 
2488 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2489 {
2490 	/*
2491 	 * Does we have PROT_READ and does the application expect
2492 	 * it to imply PROT_EXEC?  If not, nothing to talk about...
2493 	 */
2494 	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2495 		return prot;
2496 	if (!(current->personality & READ_IMPLIES_EXEC))
2497 		return prot;
2498 	/*
2499 	 * if that's an anonymous mapping, let it.
2500 	 */
2501 	if (!file)
2502 		return prot | PROT_EXEC;
2503 	/*
2504 	 * ditto if it's not on noexec mount, except that on !MMU we need
2505 	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2506 	 */
2507 	if (!path_noexec(&file->f_path)) {
2508 #ifndef CONFIG_MMU
2509 		if (file->f_op->mmap_capabilities) {
2510 			unsigned caps = file->f_op->mmap_capabilities(file);
2511 			if (!(caps & NOMMU_MAP_EXEC))
2512 				return prot;
2513 		}
2514 #endif
2515 		return prot | PROT_EXEC;
2516 	}
2517 	/* anything on noexec mount won't get PROT_EXEC */
2518 	return prot;
2519 }
2520 
2521 /**
2522  * security_mmap_file() - Check if mmap'ing a file is allowed
2523  * @file: file
2524  * @prot: protection applied by the kernel
2525  * @flags: flags
2526  *
2527  * Check permissions for a mmap operation.  The @file may be NULL, e.g. if
2528  * mapping anonymous memory.
2529  *
2530  * Return: Returns 0 if permission is granted.
2531  */
2532 int security_mmap_file(struct file *file, unsigned long prot,
2533 			unsigned long flags)
2534 {
2535 	unsigned long prot_adj = mmap_prot(file, prot);
2536 	int ret;
2537 
2538 	ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
2539 	if (ret)
2540 		return ret;
2541 	return ima_file_mmap(file, prot, prot_adj, flags);
2542 }
2543 
2544 /**
2545  * security_mmap_addr() - Check if mmap'ing an address is allowed
2546  * @addr: address
2547  *
2548  * Check permissions for a mmap operation at @addr.
2549  *
2550  * Return: Returns 0 if permission is granted.
2551  */
2552 int security_mmap_addr(unsigned long addr)
2553 {
2554 	return call_int_hook(mmap_addr, 0, addr);
2555 }
2556 
2557 /**
2558  * security_file_mprotect() - Check if changing memory protections is allowed
2559  * @vma: memory region
2560  * @reqprot: application requested protection
2561  * @prog: protection applied by the kernel
2562  *
2563  * Check permissions before changing memory access permissions.
2564  *
2565  * Return: Returns 0 if permission is granted.
2566  */
2567 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2568 			    unsigned long prot)
2569 {
2570 	int ret;
2571 
2572 	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
2573 	if (ret)
2574 		return ret;
2575 	return ima_file_mprotect(vma, prot);
2576 }
2577 
2578 /**
2579  * security_file_lock() - Check if a file lock is allowed
2580  * @file: file
2581  * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2582  *
2583  * Check permission before performing file locking operations.  Note the hook
2584  * mediates both flock and fcntl style locks.
2585  *
2586  * Return: Returns 0 if permission is granted.
2587  */
2588 int security_file_lock(struct file *file, unsigned int cmd)
2589 {
2590 	return call_int_hook(file_lock, 0, file, cmd);
2591 }
2592 
2593 /**
2594  * security_file_fcntl() - Check if fcntl() op is allowed
2595  * @file: file
2596  * @cmd: fnctl command
2597  * @arg: command argument
2598  *
2599  * Check permission before allowing the file operation specified by @cmd from
2600  * being performed on the file @file.  Note that @arg sometimes represents a
2601  * user space pointer; in other cases, it may be a simple integer value.  When
2602  * @arg represents a user space pointer, it should never be used by the
2603  * security module.
2604  *
2605  * Return: Returns 0 if permission is granted.
2606  */
2607 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2608 {
2609 	return call_int_hook(file_fcntl, 0, file, cmd, arg);
2610 }
2611 
2612 /**
2613  * security_file_set_fowner() - Set the file owner info in the LSM blob
2614  * @file: the file
2615  *
2616  * Save owner security information (typically from current->security) in
2617  * file->f_security for later use by the send_sigiotask hook.
2618  *
2619  * Return: Returns 0 on success.
2620  */
2621 void security_file_set_fowner(struct file *file)
2622 {
2623 	call_void_hook(file_set_fowner, file);
2624 }
2625 
2626 /**
2627  * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2628  * @tsk: target task
2629  * @fown: signal sender
2630  * @sig: signal to be sent, SIGIO is sent if 0
2631  *
2632  * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2633  * process @tsk.  Note that this hook is sometimes called from interrupt.  Note
2634  * that the fown_struct, @fown, is never outside the context of a struct file,
2635  * so the file structure (and associated security information) can always be
2636  * obtained: container_of(fown, struct file, f_owner).
2637  *
2638  * Return: Returns 0 if permission is granted.
2639  */
2640 int security_file_send_sigiotask(struct task_struct *tsk,
2641 				  struct fown_struct *fown, int sig)
2642 {
2643 	return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
2644 }
2645 
2646 /**
2647  * security_file_receive() - Check is receiving a file via IPC is allowed
2648  * @file: file being received
2649  *
2650  * This hook allows security modules to control the ability of a process to
2651  * receive an open file descriptor via socket IPC.
2652  *
2653  * Return: Returns 0 if permission is granted.
2654  */
2655 int security_file_receive(struct file *file)
2656 {
2657 	return call_int_hook(file_receive, 0, file);
2658 }
2659 
2660 /**
2661  * security_file_open() - Save open() time state for late use by the LSM
2662  * @file:
2663  *
2664  * Save open-time permission checking state for later use upon file_permission,
2665  * and recheck access if anything has changed since inode_permission.
2666  *
2667  * Return: Returns 0 if permission is granted.
2668  */
2669 int security_file_open(struct file *file)
2670 {
2671 	int ret;
2672 
2673 	ret = call_int_hook(file_open, 0, file);
2674 	if (ret)
2675 		return ret;
2676 
2677 	return fsnotify_perm(file, MAY_OPEN);
2678 }
2679 
2680 /**
2681  * security_file_truncate() - Check if truncating a file is allowed
2682  * @file: file
2683  *
2684  * Check permission before truncating a file, i.e. using ftruncate.  Note that
2685  * truncation permission may also be checked based on the path, using the
2686  * @path_truncate hook.
2687  *
2688  * Return: Returns 0 if permission is granted.
2689  */
2690 int security_file_truncate(struct file *file)
2691 {
2692 	return call_int_hook(file_truncate, 0, file);
2693 }
2694 
2695 /**
2696  * security_task_alloc() - Allocate a task's LSM blob
2697  * @task: the task
2698  * @clone_flags: flags indicating what is being shared
2699  *
2700  * Handle allocation of task-related resources.
2701  *
2702  * Return: Returns a zero on success, negative values on failure.
2703  */
2704 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2705 {
2706 	int rc = lsm_task_alloc(task);
2707 
2708 	if (rc)
2709 		return rc;
2710 	rc = call_int_hook(task_alloc, 0, task, clone_flags);
2711 	if (unlikely(rc))
2712 		security_task_free(task);
2713 	return rc;
2714 }
2715 
2716 /**
2717  * security_task_free() - Free a task's LSM blob and related resources
2718  * @task: task
2719  *
2720  * Handle release of task-related resources.  Note that this can be called from
2721  * interrupt context.
2722  */
2723 void security_task_free(struct task_struct *task)
2724 {
2725 	call_void_hook(task_free, task);
2726 
2727 	kfree(task->security);
2728 	task->security = NULL;
2729 }
2730 
2731 /**
2732  * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
2733  * @cred: credentials
2734  * @gfp: gfp flags
2735  *
2736  * Only allocate sufficient memory and attach to @cred such that
2737  * cred_transfer() will not get ENOMEM.
2738  *
2739  * Return: Returns 0 on success, negative values on failure.
2740  */
2741 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2742 {
2743 	int rc = lsm_cred_alloc(cred, gfp);
2744 
2745 	if (rc)
2746 		return rc;
2747 
2748 	rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
2749 	if (unlikely(rc))
2750 		security_cred_free(cred);
2751 	return rc;
2752 }
2753 
2754 /**
2755  * security_cred_free() - Free the cred's LSM blob and associated resources
2756  * @cred: credentials
2757  *
2758  * Deallocate and clear the cred->security field in a set of credentials.
2759  */
2760 void security_cred_free(struct cred *cred)
2761 {
2762 	/*
2763 	 * There is a failure case in prepare_creds() that
2764 	 * may result in a call here with ->security being NULL.
2765 	 */
2766 	if (unlikely(cred->security == NULL))
2767 		return;
2768 
2769 	call_void_hook(cred_free, cred);
2770 
2771 	kfree(cred->security);
2772 	cred->security = NULL;
2773 }
2774 
2775 /**
2776  * security_prepare_creds() - Prepare a new set of credentials
2777  * @new: new credentials
2778  * @old: original credentials
2779  * @gfp: gfp flags
2780  *
2781  * Prepare a new set of credentials by copying the data from the old set.
2782  *
2783  * Return: Returns 0 on success, negative values on failure.
2784  */
2785 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
2786 {
2787 	int rc = lsm_cred_alloc(new, gfp);
2788 
2789 	if (rc)
2790 		return rc;
2791 
2792 	rc = call_int_hook(cred_prepare, 0, new, old, gfp);
2793 	if (unlikely(rc))
2794 		security_cred_free(new);
2795 	return rc;
2796 }
2797 
2798 /**
2799  * security_transfer_creds() - Transfer creds
2800  * @new: target credentials
2801  * @old: original credentials
2802  *
2803  * Transfer data from original creds to new creds.
2804  */
2805 void security_transfer_creds(struct cred *new, const struct cred *old)
2806 {
2807 	call_void_hook(cred_transfer, new, old);
2808 }
2809 
2810 /**
2811  * security_cred_getsecid() - Get the secid from a set of credentials
2812  * @c: credentials
2813  * @secid: secid value
2814  *
2815  * Retrieve the security identifier of the cred structure @c.  In case of
2816  * failure, @secid will be set to zero.
2817  */
2818 void security_cred_getsecid(const struct cred *c, u32 *secid)
2819 {
2820 	*secid = 0;
2821 	call_void_hook(cred_getsecid, c, secid);
2822 }
2823 EXPORT_SYMBOL(security_cred_getsecid);
2824 
2825 /**
2826  * security_kernel_act_as() - Set the kernel credentials to act as secid
2827  * @new: credentials
2828  * @secid: secid
2829  *
2830  * Set the credentials for a kernel service to act as (subjective context).
2831  * The current task must be the one that nominated @secid.
2832  *
2833  * Return: Returns 0 if successful.
2834  */
2835 int security_kernel_act_as(struct cred *new, u32 secid)
2836 {
2837 	return call_int_hook(kernel_act_as, 0, new, secid);
2838 }
2839 
2840 /**
2841  * security_kernel_create_files_as() - Set file creation context using an inode
2842  * @new: target credentials
2843  * @inode: reference inode
2844  *
2845  * Set the file creation context in a set of credentials to be the same as the
2846  * objective context of the specified inode.  The current task must be the one
2847  * that nominated @inode.
2848  *
2849  * Return: Returns 0 if successful.
2850  */
2851 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
2852 {
2853 	return call_int_hook(kernel_create_files_as, 0, new, inode);
2854 }
2855 
2856 /**
2857  * security_kernel_module_request() - Check is loading a module is allowed
2858  * @kmod_name: module name
2859  *
2860  * Ability to trigger the kernel to automatically upcall to userspace for
2861  * userspace to load a kernel module with the given name.
2862  *
2863  * Return: Returns 0 if successful.
2864  */
2865 int security_kernel_module_request(char *kmod_name)
2866 {
2867 	int ret;
2868 
2869 	ret = call_int_hook(kernel_module_request, 0, kmod_name);
2870 	if (ret)
2871 		return ret;
2872 	return integrity_kernel_module_request(kmod_name);
2873 }
2874 
2875 /**
2876  * security_kernel_read_file() - Read a file specified by userspace
2877  * @file: file
2878  * @id: file identifier
2879  * @contents: trust if security_kernel_post_read_file() will be called
2880  *
2881  * Read a file specified by userspace.
2882  *
2883  * Return: Returns 0 if permission is granted.
2884  */
2885 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
2886 			      bool contents)
2887 {
2888 	int ret;
2889 
2890 	ret = call_int_hook(kernel_read_file, 0, file, id, contents);
2891 	if (ret)
2892 		return ret;
2893 	return ima_read_file(file, id, contents);
2894 }
2895 EXPORT_SYMBOL_GPL(security_kernel_read_file);
2896 
2897 /**
2898  * security_kernel_post_read_file() - Read a file specified by userspace
2899  * @file: file
2900  * @buf: file contents
2901  * @size: size of file contents
2902  * @id: file identifier
2903  *
2904  * Read a file specified by userspace.  This must be paired with a prior call
2905  * to security_kernel_read_file() call that indicated this hook would also be
2906  * called, see security_kernel_read_file() for more information.
2907  *
2908  * Return: Returns 0 if permission is granted.
2909  */
2910 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
2911 				   enum kernel_read_file_id id)
2912 {
2913 	int ret;
2914 
2915 	ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
2916 	if (ret)
2917 		return ret;
2918 	return ima_post_read_file(file, buf, size, id);
2919 }
2920 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
2921 
2922 /**
2923  * security_kernel_load_data() - Load data provided by userspace
2924  * @id: data identifier
2925  * @contents: true if security_kernel_post_load_data() will be called
2926  *
2927  * Load data provided by userspace.
2928  *
2929  * Return: Returns 0 if permission is granted.
2930  */
2931 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
2932 {
2933 	int ret;
2934 
2935 	ret = call_int_hook(kernel_load_data, 0, id, contents);
2936 	if (ret)
2937 		return ret;
2938 	return ima_load_data(id, contents);
2939 }
2940 EXPORT_SYMBOL_GPL(security_kernel_load_data);
2941 
2942 /**
2943  * security_kernel_post_load_data() - Load userspace data from a non-file source
2944  * @buf: data
2945  * @size: size of data
2946  * @id: data identifier
2947  * @description: text description of data, specific to the id value
2948  *
2949  * Load data provided by a non-file source (usually userspace buffer).  This
2950  * must be paired with a prior security_kernel_load_data() call that indicated
2951  * this hook would also be called, see security_kernel_load_data() for more
2952  * information.
2953  *
2954  * Return: Returns 0 if permission is granted.
2955  */
2956 int security_kernel_post_load_data(char *buf, loff_t size,
2957 				   enum kernel_load_data_id id,
2958 				   char *description)
2959 {
2960 	int ret;
2961 
2962 	ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
2963 			    description);
2964 	if (ret)
2965 		return ret;
2966 	return ima_post_load_data(buf, size, id, description);
2967 }
2968 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
2969 
2970 /**
2971  * security_task_fix_setuid() - Update LSM with new user id attributes
2972  * @new: updated credentials
2973  * @old: credentials being replaced
2974  * @flags: LSM_SETID_* flag values
2975  *
2976  * Update the module's state after setting one or more of the user identity
2977  * attributes of the current process.  The @flags parameter indicates which of
2978  * the set*uid system calls invoked this hook.  If @new is the set of
2979  * credentials that will be installed.  Modifications should be made to this
2980  * rather than to @current->cred.
2981  *
2982  * Return: Returns 0 on success.
2983  */
2984 int security_task_fix_setuid(struct cred *new, const struct cred *old,
2985 			     int flags)
2986 {
2987 	return call_int_hook(task_fix_setuid, 0, new, old, flags);
2988 }
2989 
2990 /**
2991  * security_task_fix_setgid() - Update LSM with new group id attributes
2992  * @new: updated credentials
2993  * @old: credentials being replaced
2994  * @flags: LSM_SETID_* flag value
2995  *
2996  * Update the module's state after setting one or more of the group identity
2997  * attributes of the current process.  The @flags parameter indicates which of
2998  * the set*gid system calls invoked this hook.  @new is the set of credentials
2999  * that will be installed.  Modifications should be made to this rather than to
3000  * @current->cred.
3001  *
3002  * Return: Returns 0 on success.
3003  */
3004 int security_task_fix_setgid(struct cred *new, const struct cred *old,
3005 				 int flags)
3006 {
3007 	return call_int_hook(task_fix_setgid, 0, new, old, flags);
3008 }
3009 
3010 /**
3011  * security_task_fix_setgroups() - Update LSM with new supplementary groups
3012  * @new: updated credentials
3013  * @old: credentials being replaced
3014  *
3015  * Update the module's state after setting the supplementary group identity
3016  * attributes of the current process.  @new is the set of credentials that will
3017  * be installed.  Modifications should be made to this rather than to
3018  * @current->cred.
3019  *
3020  * Return: Returns 0 on success.
3021  */
3022 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3023 {
3024 	return call_int_hook(task_fix_setgroups, 0, new, old);
3025 }
3026 
3027 /**
3028  * security_task_setpgid() - Check if setting the pgid is allowed
3029  * @p: task being modified
3030  * @pgid: new pgid
3031  *
3032  * Check permission before setting the process group identifier of the process
3033  * @p to @pgid.
3034  *
3035  * Return: Returns 0 if permission is granted.
3036  */
3037 int security_task_setpgid(struct task_struct *p, pid_t pgid)
3038 {
3039 	return call_int_hook(task_setpgid, 0, p, pgid);
3040 }
3041 
3042 /**
3043  * security_task_getpgid() - Check if getting the pgid is allowed
3044  * @p: task
3045  *
3046  * Check permission before getting the process group identifier of the process
3047  * @p.
3048  *
3049  * Return: Returns 0 if permission is granted.
3050  */
3051 int security_task_getpgid(struct task_struct *p)
3052 {
3053 	return call_int_hook(task_getpgid, 0, p);
3054 }
3055 
3056 /**
3057  * security_task_getsid() - Check if getting the session id is allowed
3058  * @p: task
3059  *
3060  * Check permission before getting the session identifier of the process @p.
3061  *
3062  * Return: Returns 0 if permission is granted.
3063  */
3064 int security_task_getsid(struct task_struct *p)
3065 {
3066 	return call_int_hook(task_getsid, 0, p);
3067 }
3068 
3069 /**
3070  * security_current_getsecid_subj() - Get the current task's subjective secid
3071  * @secid: secid value
3072  *
3073  * Retrieve the subjective security identifier of the current task and return
3074  * it in @secid.  In case of failure, @secid will be set to zero.
3075  */
3076 void security_current_getsecid_subj(u32 *secid)
3077 {
3078 	*secid = 0;
3079 	call_void_hook(current_getsecid_subj, secid);
3080 }
3081 EXPORT_SYMBOL(security_current_getsecid_subj);
3082 
3083 /**
3084  * security_task_getsecid_obj() - Get a task's objective secid
3085  * @p: target task
3086  * @secid: secid value
3087  *
3088  * Retrieve the objective security identifier of the task_struct in @p and
3089  * return it in @secid. In case of failure, @secid will be set to zero.
3090  */
3091 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3092 {
3093 	*secid = 0;
3094 	call_void_hook(task_getsecid_obj, p, secid);
3095 }
3096 EXPORT_SYMBOL(security_task_getsecid_obj);
3097 
3098 /**
3099  * security_task_setnice() - Check if setting a task's nice value is allowed
3100  * @p: target task
3101  * @nice: nice value
3102  *
3103  * Check permission before setting the nice value of @p to @nice.
3104  *
3105  * Return: Returns 0 if permission is granted.
3106  */
3107 int security_task_setnice(struct task_struct *p, int nice)
3108 {
3109 	return call_int_hook(task_setnice, 0, p, nice);
3110 }
3111 
3112 /**
3113  * security_task_setioprio() - Check if setting a task's ioprio is allowed
3114  * @p: target task
3115  * @ioprio: ioprio value
3116  *
3117  * Check permission before setting the ioprio value of @p to @ioprio.
3118  *
3119  * Return: Returns 0 if permission is granted.
3120  */
3121 int security_task_setioprio(struct task_struct *p, int ioprio)
3122 {
3123 	return call_int_hook(task_setioprio, 0, p, ioprio);
3124 }
3125 
3126 /**
3127  * security_task_getioprio() - Check if getting a task's ioprio is allowed
3128  * @p: task
3129  *
3130  * Check permission before getting the ioprio value of @p.
3131  *
3132  * Return: Returns 0 if permission is granted.
3133  */
3134 int security_task_getioprio(struct task_struct *p)
3135 {
3136 	return call_int_hook(task_getioprio, 0, p);
3137 }
3138 
3139 /**
3140  * security_task_prlimit() - Check if get/setting resources limits is allowed
3141  * @cred: current task credentials
3142  * @tcred: target task credentials
3143  * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3144  *
3145  * Check permission before getting and/or setting the resource limits of
3146  * another task.
3147  *
3148  * Return: Returns 0 if permission is granted.
3149  */
3150 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3151 			  unsigned int flags)
3152 {
3153 	return call_int_hook(task_prlimit, 0, cred, tcred, flags);
3154 }
3155 
3156 /**
3157  * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3158  * @p: target task's group leader
3159  * @resource: resource whose limit is being set
3160  * @new_rlim: new resource limit
3161  *
3162  * Check permission before setting the resource limits of process @p for
3163  * @resource to @new_rlim.  The old resource limit values can be examined by
3164  * dereferencing (p->signal->rlim + resource).
3165  *
3166  * Return: Returns 0 if permission is granted.
3167  */
3168 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3169 		struct rlimit *new_rlim)
3170 {
3171 	return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
3172 }
3173 
3174 /**
3175  * security_task_setscheduler() - Check if setting sched policy/param is allowed
3176  * @p: target task
3177  *
3178  * Check permission before setting scheduling policy and/or parameters of
3179  * process @p.
3180  *
3181  * Return: Returns 0 if permission is granted.
3182  */
3183 int security_task_setscheduler(struct task_struct *p)
3184 {
3185 	return call_int_hook(task_setscheduler, 0, p);
3186 }
3187 
3188 /**
3189  * security_task_getscheduler() - Check if getting scheduling info is allowed
3190  * @p: target task
3191  *
3192  * Check permission before obtaining scheduling information for process @p.
3193  *
3194  * Return: Returns 0 if permission is granted.
3195  */
3196 int security_task_getscheduler(struct task_struct *p)
3197 {
3198 	return call_int_hook(task_getscheduler, 0, p);
3199 }
3200 
3201 /**
3202  * security_task_movememory() - Check if moving memory is allowed
3203  * @p: task
3204  *
3205  * Check permission before moving memory owned by process @p.
3206  *
3207  * Return: Returns 0 if permission is granted.
3208  */
3209 int security_task_movememory(struct task_struct *p)
3210 {
3211 	return call_int_hook(task_movememory, 0, p);
3212 }
3213 
3214 /**
3215  * security_task_kill() - Check if sending a signal is allowed
3216  * @p: target process
3217  * @info: signal information
3218  * @sig: signal value
3219  * @cred: credentials of the signal sender, NULL if @current
3220  *
3221  * Check permission before sending signal @sig to @p.  @info can be NULL, the
3222  * constant 1, or a pointer to a kernel_siginfo structure.  If @info is 1 or
3223  * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3224  * the kernel and should typically be permitted.  SIGIO signals are handled
3225  * separately by the send_sigiotask hook in file_security_ops.
3226  *
3227  * Return: Returns 0 if permission is granted.
3228  */
3229 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3230 			int sig, const struct cred *cred)
3231 {
3232 	return call_int_hook(task_kill, 0, p, info, sig, cred);
3233 }
3234 
3235 /**
3236  * security_task_prctl() - Check if a prctl op is allowed
3237  * @option: operation
3238  * @arg2: argument
3239  * @arg3: argument
3240  * @arg4: argument
3241  * @arg5: argument
3242  *
3243  * Check permission before performing a process control operation on the
3244  * current process.
3245  *
3246  * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3247  *         to cause prctl() to return immediately with that value.
3248  */
3249 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3250 			 unsigned long arg4, unsigned long arg5)
3251 {
3252 	int thisrc;
3253 	int rc = LSM_RET_DEFAULT(task_prctl);
3254 	struct security_hook_list *hp;
3255 
3256 	hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
3257 		thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
3258 		if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3259 			rc = thisrc;
3260 			if (thisrc != 0)
3261 				break;
3262 		}
3263 	}
3264 	return rc;
3265 }
3266 
3267 /**
3268  * security_task_to_inode() - Set the security attributes of a task's inode
3269  * @p: task
3270  * @inode: inode
3271  *
3272  * Set the security attributes for an inode based on an associated task's
3273  * security attributes, e.g. for /proc/pid inodes.
3274  */
3275 void security_task_to_inode(struct task_struct *p, struct inode *inode)
3276 {
3277 	call_void_hook(task_to_inode, p, inode);
3278 }
3279 
3280 /**
3281  * security_create_user_ns() - Check if creating a new userns is allowed
3282  * @cred: prepared creds
3283  *
3284  * Check permission prior to creating a new user namespace.
3285  *
3286  * Return: Returns 0 if successful, otherwise < 0 error code.
3287  */
3288 int security_create_user_ns(const struct cred *cred)
3289 {
3290 	return call_int_hook(userns_create, 0, cred);
3291 }
3292 
3293 /**
3294  * security_ipc_permission() - Check if sysv ipc access is allowed
3295  * @ipcp: ipc permission structure
3296  * @flags: requested permissions
3297  *
3298  * Check permissions for access to IPC.
3299  *
3300  * Return: Returns 0 if permission is granted.
3301  */
3302 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3303 {
3304 	return call_int_hook(ipc_permission, 0, ipcp, flag);
3305 }
3306 
3307 /**
3308  * security_ipc_getsecid() - Get the sysv ipc object's secid
3309  * @ipcp: ipc permission structure
3310  * @secid: secid pointer
3311  *
3312  * Get the secid associated with the ipc object.  In case of failure, @secid
3313  * will be set to zero.
3314  */
3315 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3316 {
3317 	*secid = 0;
3318 	call_void_hook(ipc_getsecid, ipcp, secid);
3319 }
3320 
3321 /**
3322  * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3323  * @msg: message structure
3324  *
3325  * Allocate and attach a security structure to the msg->security field.  The
3326  * security field is initialized to NULL when the structure is first created.
3327  *
3328  * Return: Return 0 if operation was successful and permission is granted.
3329  */
3330 int security_msg_msg_alloc(struct msg_msg *msg)
3331 {
3332 	int rc = lsm_msg_msg_alloc(msg);
3333 
3334 	if (unlikely(rc))
3335 		return rc;
3336 	rc = call_int_hook(msg_msg_alloc_security, 0, msg);
3337 	if (unlikely(rc))
3338 		security_msg_msg_free(msg);
3339 	return rc;
3340 }
3341 
3342 /**
3343  * security_msg_msg_free() - Free a sysv ipc message LSM blob
3344  * @msg: message structure
3345  *
3346  * Deallocate the security structure for this message.
3347  */
3348 void security_msg_msg_free(struct msg_msg *msg)
3349 {
3350 	call_void_hook(msg_msg_free_security, msg);
3351 	kfree(msg->security);
3352 	msg->security = NULL;
3353 }
3354 
3355 /**
3356  * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3357  * @msq: sysv ipc permission structure
3358  *
3359  * Allocate and attach a security structure to @msg. The security field is
3360  * initialized to NULL when the structure is first created.
3361  *
3362  * Return: Returns 0 if operation was successful and permission is granted.
3363  */
3364 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
3365 {
3366 	int rc = lsm_ipc_alloc(msq);
3367 
3368 	if (unlikely(rc))
3369 		return rc;
3370 	rc = call_int_hook(msg_queue_alloc_security, 0, msq);
3371 	if (unlikely(rc))
3372 		security_msg_queue_free(msq);
3373 	return rc;
3374 }
3375 
3376 /**
3377  * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3378  * @msq: sysv ipc permission structure
3379  *
3380  * Deallocate security field @perm->security for the message queue.
3381  */
3382 void security_msg_queue_free(struct kern_ipc_perm *msq)
3383 {
3384 	call_void_hook(msg_queue_free_security, msq);
3385 	kfree(msq->security);
3386 	msq->security = NULL;
3387 }
3388 
3389 /**
3390  * security_msg_queue_associate() - Check if a msg queue operation is allowed
3391  * @msq: sysv ipc permission structure
3392  * @msqflg: operation flags
3393  *
3394  * Check permission when a message queue is requested through the msgget system
3395  * call. This hook is only called when returning the message queue identifier
3396  * for an existing message queue, not when a new message queue is created.
3397  *
3398  * Return: Return 0 if permission is granted.
3399  */
3400 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
3401 {
3402 	return call_int_hook(msg_queue_associate, 0, msq, msqflg);
3403 }
3404 
3405 /**
3406  * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3407  * @msq: sysv ipc permission structure
3408  * @cmd: operation
3409  *
3410  * Check permission when a message control operation specified by @cmd is to be
3411  * performed on the message queue with permissions.
3412  *
3413  * Return: Returns 0 if permission is granted.
3414  */
3415 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
3416 {
3417 	return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
3418 }
3419 
3420 /**
3421  * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3422  * @msq: sysv ipc permission structure
3423  * @msg: message
3424  * @msqflg: operation flags
3425  *
3426  * Check permission before a message, @msg, is enqueued on the message queue
3427  * with permissions specified in @msq.
3428  *
3429  * Return: Returns 0 if permission is granted.
3430  */
3431 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
3432 			       struct msg_msg *msg, int msqflg)
3433 {
3434 	return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
3435 }
3436 
3437 /**
3438  * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3439  * @msq: sysv ipc permission structure
3440  * @msg: message
3441  * @target: target task
3442  * @type: type of message requested
3443  * @mode: operation flags
3444  *
3445  * Check permission before a message, @msg, is removed from the message	queue.
3446  * The @target task structure contains a pointer to the process that will be
3447  * receiving the message (not equal to the current process when inline receives
3448  * are being performed).
3449  *
3450  * Return: Returns 0 if permission is granted.
3451  */
3452 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
3453 			       struct task_struct *target, long type, int mode)
3454 {
3455 	return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
3456 }
3457 
3458 /**
3459  * security_shm_alloc() - Allocate a sysv shm LSM blob
3460  * @shp: sysv ipc permission structure
3461  *
3462  * Allocate and attach a security structure to the @shp security field.  The
3463  * security field is initialized to NULL when the structure is first created.
3464  *
3465  * Return: Returns 0 if operation was successful and permission is granted.
3466  */
3467 int security_shm_alloc(struct kern_ipc_perm *shp)
3468 {
3469 	int rc = lsm_ipc_alloc(shp);
3470 
3471 	if (unlikely(rc))
3472 		return rc;
3473 	rc = call_int_hook(shm_alloc_security, 0, shp);
3474 	if (unlikely(rc))
3475 		security_shm_free(shp);
3476 	return rc;
3477 }
3478 
3479 /**
3480  * security_shm_free() - Free a sysv shm LSM blob
3481  * @shp: sysv ipc permission structure
3482  *
3483  * Deallocate the security structure @perm->security for the memory segment.
3484  */
3485 void security_shm_free(struct kern_ipc_perm *shp)
3486 {
3487 	call_void_hook(shm_free_security, shp);
3488 	kfree(shp->security);
3489 	shp->security = NULL;
3490 }
3491 
3492 /**
3493  * security_shm_associate() - Check if a sysv shm operation is allowed
3494  * @shp: sysv ipc permission structure
3495  * @shmflg: operation flags
3496  *
3497  * Check permission when a shared memory region is requested through the shmget
3498  * system call. This hook is only called when returning the shared memory
3499  * region identifier for an existing region, not when a new shared memory
3500  * region is created.
3501  *
3502  * Return: Returns 0 if permission is granted.
3503  */
3504 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
3505 {
3506 	return call_int_hook(shm_associate, 0, shp, shmflg);
3507 }
3508 
3509 /**
3510  * security_shm_shmctl() - Check if a sysv shm operation is allowed
3511  * @shp: sysv ipc permission structure
3512  * @cmd: operation
3513  *
3514  * Check permission when a shared memory control operation specified by @cmd is
3515  * to be performed on the shared memory region with permissions in @shp.
3516  *
3517  * Return: Return 0 if permission is granted.
3518  */
3519 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
3520 {
3521 	return call_int_hook(shm_shmctl, 0, shp, cmd);
3522 }
3523 
3524 /**
3525  * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3526  * @shp: sysv ipc permission structure
3527  * @shmaddr: address of memory region to attach
3528  * @shmflg: operation flags
3529  *
3530  * Check permissions prior to allowing the shmat system call to attach the
3531  * shared memory segment with permissions @shp to the data segment of the
3532  * calling process. The attaching address is specified by @shmaddr.
3533  *
3534  * Return: Returns 0 if permission is granted.
3535  */
3536 int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
3537 {
3538 	return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
3539 }
3540 
3541 /**
3542  * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3543  * @sma: sysv ipc permission structure
3544  *
3545  * Allocate and attach a security structure to the @sma security field. The
3546  * security field is initialized to NULL when the structure is first created.
3547  *
3548  * Return: Returns 0 if operation was successful and permission is granted.
3549  */
3550 int security_sem_alloc(struct kern_ipc_perm *sma)
3551 {
3552 	int rc = lsm_ipc_alloc(sma);
3553 
3554 	if (unlikely(rc))
3555 		return rc;
3556 	rc = call_int_hook(sem_alloc_security, 0, sma);
3557 	if (unlikely(rc))
3558 		security_sem_free(sma);
3559 	return rc;
3560 }
3561 
3562 /**
3563  * security_sem_free() - Free a sysv semaphore LSM blob
3564  * @sma: sysv ipc permission structure
3565  *
3566  * Deallocate security structure @sma->security for the semaphore.
3567  */
3568 void security_sem_free(struct kern_ipc_perm *sma)
3569 {
3570 	call_void_hook(sem_free_security, sma);
3571 	kfree(sma->security);
3572 	sma->security = NULL;
3573 }
3574 
3575 /**
3576  * security_sem_associate() - Check if a sysv semaphore operation is allowed
3577  * @sma: sysv ipc permission structure
3578  * @semflg: operation flags
3579  *
3580  * Check permission when a semaphore is requested through the semget system
3581  * call. This hook is only called when returning the semaphore identifier for
3582  * an existing semaphore, not when a new one must be created.
3583  *
3584  * Return: Returns 0 if permission is granted.
3585  */
3586 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
3587 {
3588 	return call_int_hook(sem_associate, 0, sma, semflg);
3589 }
3590 
3591 /**
3592  * security_sem_ctl() - Check if a sysv semaphore operation is allowed
3593  * @sma: sysv ipc permission structure
3594  * @cmd: operation
3595  *
3596  * Check permission when a semaphore operation specified by @cmd is to be
3597  * performed on the semaphore.
3598  *
3599  * Return: Returns 0 if permission is granted.
3600  */
3601 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
3602 {
3603 	return call_int_hook(sem_semctl, 0, sma, cmd);
3604 }
3605 
3606 /**
3607  * security_sem_semop() - Check if a sysv semaphore operation is allowed
3608  * @sma: sysv ipc permission structure
3609  * @sops: operations to perform
3610  * @nsops: number of operations
3611  * @alter: flag indicating changes will be made
3612  *
3613  * Check permissions before performing operations on members of the semaphore
3614  * set. If the @alter flag is nonzero, the semaphore set may be modified.
3615  *
3616  * Return: Returns 0 if permission is granted.
3617  */
3618 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
3619 			unsigned nsops, int alter)
3620 {
3621 	return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
3622 }
3623 
3624 /**
3625  * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3626  * @dentry: dentry
3627  * @inode: inode
3628  *
3629  * Fill in @inode security information for a @dentry if allowed.
3630  */
3631 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3632 {
3633 	if (unlikely(inode && IS_PRIVATE(inode)))
3634 		return;
3635 	call_void_hook(d_instantiate, dentry, inode);
3636 }
3637 EXPORT_SYMBOL(security_d_instantiate);
3638 
3639 /**
3640  * security_getprocattr() - Read an attribute for a task
3641  * @p: the task
3642  * @lsm: LSM name
3643  * @name: attribute name
3644  * @value: attribute value
3645  *
3646  * Read attribute @name for task @p and store it into @value if allowed.
3647  *
3648  * Return: Returns the length of @value on success, a negative value otherwise.
3649  */
3650 int security_getprocattr(struct task_struct *p, const char *lsm,
3651 			 const char *name, char **value)
3652 {
3653 	struct security_hook_list *hp;
3654 
3655 	hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
3656 		if (lsm != NULL && strcmp(lsm, hp->lsm))
3657 			continue;
3658 		return hp->hook.getprocattr(p, name, value);
3659 	}
3660 	return LSM_RET_DEFAULT(getprocattr);
3661 }
3662 
3663 /**
3664  * security_setprocattr() - Set an attribute for a task
3665  * @lsm: LSM name
3666  * @name: attribute name
3667  * @value: attribute value
3668  * @size: attribute value size
3669  *
3670  * Write (set) the current task's attribute @name to @value, size @size if
3671  * allowed.
3672  *
3673  * Return: Returns bytes written on success, a negative value otherwise.
3674  */
3675 int security_setprocattr(const char *lsm, const char *name, void *value,
3676 			 size_t size)
3677 {
3678 	struct security_hook_list *hp;
3679 
3680 	hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
3681 		if (lsm != NULL && strcmp(lsm, hp->lsm))
3682 			continue;
3683 		return hp->hook.setprocattr(name, value, size);
3684 	}
3685 	return LSM_RET_DEFAULT(setprocattr);
3686 }
3687 
3688 /**
3689  * security_netlink_send() - Save info and check if netlink sending is allowed
3690  * @sk: sending socket
3691  * @skb: netlink message
3692  *
3693  * Save security information for a netlink message so that permission checking
3694  * can be performed when the message is processed.  The security information
3695  * can be saved using the eff_cap field of the netlink_skb_parms structure.
3696  * Also may be used to provide fine grained control over message transmission.
3697  *
3698  * Return: Returns 0 if the information was successfully saved and message is
3699  *         allowed to be transmitted.
3700  */
3701 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
3702 {
3703 	return call_int_hook(netlink_send, 0, sk, skb);
3704 }
3705 
3706 int security_ismaclabel(const char *name)
3707 {
3708 	return call_int_hook(ismaclabel, 0, name);
3709 }
3710 EXPORT_SYMBOL(security_ismaclabel);
3711 
3712 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3713 {
3714 	struct security_hook_list *hp;
3715 	int rc;
3716 
3717 	/*
3718 	 * Currently, only one LSM can implement secid_to_secctx (i.e this
3719 	 * LSM hook is not "stackable").
3720 	 */
3721 	hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
3722 		rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
3723 		if (rc != LSM_RET_DEFAULT(secid_to_secctx))
3724 			return rc;
3725 	}
3726 
3727 	return LSM_RET_DEFAULT(secid_to_secctx);
3728 }
3729 EXPORT_SYMBOL(security_secid_to_secctx);
3730 
3731 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3732 {
3733 	*secid = 0;
3734 	return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
3735 }
3736 EXPORT_SYMBOL(security_secctx_to_secid);
3737 
3738 void security_release_secctx(char *secdata, u32 seclen)
3739 {
3740 	call_void_hook(release_secctx, secdata, seclen);
3741 }
3742 EXPORT_SYMBOL(security_release_secctx);
3743 
3744 void security_inode_invalidate_secctx(struct inode *inode)
3745 {
3746 	call_void_hook(inode_invalidate_secctx, inode);
3747 }
3748 EXPORT_SYMBOL(security_inode_invalidate_secctx);
3749 
3750 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3751 {
3752 	return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
3753 }
3754 EXPORT_SYMBOL(security_inode_notifysecctx);
3755 
3756 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3757 {
3758 	return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
3759 }
3760 EXPORT_SYMBOL(security_inode_setsecctx);
3761 
3762 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3763 {
3764 	return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
3765 }
3766 EXPORT_SYMBOL(security_inode_getsecctx);
3767 
3768 #ifdef CONFIG_WATCH_QUEUE
3769 int security_post_notification(const struct cred *w_cred,
3770 			       const struct cred *cred,
3771 			       struct watch_notification *n)
3772 {
3773 	return call_int_hook(post_notification, 0, w_cred, cred, n);
3774 }
3775 #endif /* CONFIG_WATCH_QUEUE */
3776 
3777 #ifdef CONFIG_KEY_NOTIFICATIONS
3778 int security_watch_key(struct key *key)
3779 {
3780 	return call_int_hook(watch_key, 0, key);
3781 }
3782 #endif
3783 
3784 #ifdef CONFIG_SECURITY_NETWORK
3785 /**
3786  * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
3787  * @sock: originating sock
3788  * @other: peer sock
3789  * @newsk: new sock
3790  *
3791  * Check permissions before establishing a Unix domain stream connection
3792  * between @sock and @other.
3793  *
3794  * The @unix_stream_connect and @unix_may_send hooks were necessary because
3795  * Linux provides an alternative to the conventional file name space for Unix
3796  * domain sockets.  Whereas binding and connecting to sockets in the file name
3797  * space is mediated by the typical file permissions (and caught by the mknod
3798  * and permission hooks in inode_security_ops), binding and connecting to
3799  * sockets in the abstract name space is completely unmediated.  Sufficient
3800  * control of Unix domain sockets in the abstract name space isn't possible
3801  * using only the socket layer hooks, since we need to know the actual target
3802  * socket, which is not looked up until we are inside the af_unix code.
3803  *
3804  * Return: Returns 0 if permission is granted.
3805  */
3806 int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
3807 {
3808 	return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
3809 }
3810 EXPORT_SYMBOL(security_unix_stream_connect);
3811 
3812 /**
3813  * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
3814  * @sock: originating sock
3815  * @other: peer sock
3816  *
3817  * Check permissions before connecting or sending datagrams from @sock to
3818  * @other.
3819  *
3820  * The @unix_stream_connect and @unix_may_send hooks were necessary because
3821  * Linux provides an alternative to the conventional file name space for Unix
3822  * domain sockets.  Whereas binding and connecting to sockets in the file name
3823  * space is mediated by the typical file permissions (and caught by the mknod
3824  * and permission hooks in inode_security_ops), binding and connecting to
3825  * sockets in the abstract name space is completely unmediated.  Sufficient
3826  * control of Unix domain sockets in the abstract name space isn't possible
3827  * using only the socket layer hooks, since we need to know the actual target
3828  * socket, which is not looked up until we are inside the af_unix code.
3829  *
3830  * Return: Returns 0 if permission is granted.
3831  */
3832 int security_unix_may_send(struct socket *sock,  struct socket *other)
3833 {
3834 	return call_int_hook(unix_may_send, 0, sock, other);
3835 }
3836 EXPORT_SYMBOL(security_unix_may_send);
3837 
3838 /**
3839  * security_socket_create() - Check if creating a new socket is allowed
3840  * @family: protocol family
3841  * @type: communications type
3842  * @protocol: requested protocol
3843  * @kern: set to 1 if a kernel socket is requested
3844  *
3845  * Check permissions prior to creating a new socket.
3846  *
3847  * Return: Returns 0 if permission is granted.
3848  */
3849 int security_socket_create(int family, int type, int protocol, int kern)
3850 {
3851 	return call_int_hook(socket_create, 0, family, type, protocol, kern);
3852 }
3853 
3854 /**
3855  * security_socket_create() - Initialize a newly created socket
3856  * @sock: socket
3857  * @family: protocol family
3858  * @type: communications type
3859  * @protocol: requested protocol
3860  * @kern: set to 1 if a kernel socket is requested
3861  *
3862  * This hook allows a module to update or allocate a per-socket security
3863  * structure. Note that the security field was not added directly to the socket
3864  * structure, but rather, the socket security information is stored in the
3865  * associated inode.  Typically, the inode alloc_security hook will allocate
3866  * and attach security information to SOCK_INODE(sock)->i_security.  This hook
3867  * may be used to update the SOCK_INODE(sock)->i_security field with additional
3868  * information that wasn't available when the inode was allocated.
3869  *
3870  * Return: Returns 0 if permission is granted.
3871  */
3872 int security_socket_post_create(struct socket *sock, int family,
3873 				int type, int protocol, int kern)
3874 {
3875 	return call_int_hook(socket_post_create, 0, sock, family, type,
3876 						protocol, kern);
3877 }
3878 
3879 /**
3880  * security_socket_socketpair() - Check if creating a socketpair is allowed
3881  * @socka: first socket
3882  * @sockb: second socket
3883  *
3884  * Check permissions before creating a fresh pair of sockets.
3885  *
3886  * Return: Returns 0 if permission is granted and the connection was
3887  *         established.
3888  */
3889 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
3890 {
3891 	return call_int_hook(socket_socketpair, 0, socka, sockb);
3892 }
3893 EXPORT_SYMBOL(security_socket_socketpair);
3894 
3895 /**
3896  * security_socket_bind() - Check if a socket bind operation is allowed
3897  * @sock: socket
3898  * @address: requested bind address
3899  * @addrlen: length of address
3900  *
3901  * Check permission before socket protocol layer bind operation is performed
3902  * and the socket @sock is bound to the address specified in the @address
3903  * parameter.
3904  *
3905  * Return: Returns 0 if permission is granted.
3906  */
3907 int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3908 {
3909 	return call_int_hook(socket_bind, 0, sock, address, addrlen);
3910 }
3911 
3912 /**
3913  * security_socket_connect() - Check if a socket connect operation is allowed
3914  * @sock: socket
3915  * @address: address of remote connection point
3916  * @addrlen: length of address
3917  *
3918  * Check permission before socket protocol layer connect operation attempts to
3919  * connect socket @sock to a remote address, @address.
3920  *
3921  * Return: Returns 0 if permission is granted.
3922  */
3923 int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3924 {
3925 	return call_int_hook(socket_connect, 0, sock, address, addrlen);
3926 }
3927 
3928 /**
3929  * security_socket_listen() - Check if a socket is allowed to listen
3930  * @sock: socket
3931  * @backlog: connection queue size
3932  *
3933  * Check permission before socket protocol layer listen operation.
3934  *
3935  * Return: Returns 0 if permission is granted.
3936  */
3937 int security_socket_listen(struct socket *sock, int backlog)
3938 {
3939 	return call_int_hook(socket_listen, 0, sock, backlog);
3940 }
3941 
3942 /**
3943  * security_socket_accept() - Check if a socket is allowed to accept connections
3944  * @sock: listening socket
3945  * @newsock: newly creation connection socket
3946  *
3947  * Check permission before accepting a new connection.  Note that the new
3948  * socket, @newsock, has been created and some information copied to it, but
3949  * the accept operation has not actually been performed.
3950  *
3951  * Return: Returns 0 if permission is granted.
3952  */
3953 int security_socket_accept(struct socket *sock, struct socket *newsock)
3954 {
3955 	return call_int_hook(socket_accept, 0, sock, newsock);
3956 }
3957 
3958 /**
3959  * security_socket_sendmsg() - Check is sending a message is allowed
3960  * @sock: sending socket
3961  * @msg: message to send
3962  * @size: size of message
3963  *
3964  * Check permission before transmitting a message to another socket.
3965  *
3966  * Return: Returns 0 if permission is granted.
3967  */
3968 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
3969 {
3970 	return call_int_hook(socket_sendmsg, 0, sock, msg, size);
3971 }
3972 
3973 /**
3974  * security_socket_recvmsg() - Check if receiving a message is allowed
3975  * @sock: receiving socket
3976  * @msg: message to receive
3977  * @size: size of message
3978  * @flags: operational flags
3979  *
3980  * Check permission before receiving a message from a socket.
3981  *
3982  * Return: Returns 0 if permission is granted.
3983  */
3984 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
3985 			    int size, int flags)
3986 {
3987 	return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
3988 }
3989 
3990 /**
3991  * security_socket_getsockname() - Check if reading the socket addr is allowed
3992  * @sock: socket
3993  *
3994  * Check permission before reading the local address (name) of the socket
3995  * object.
3996  *
3997  * Return: Returns 0 if permission is granted.
3998  */
3999 int security_socket_getsockname(struct socket *sock)
4000 {
4001 	return call_int_hook(socket_getsockname, 0, sock);
4002 }
4003 
4004 /**
4005  * security_socket_getpeername() - Check if reading the peer's addr is allowed
4006  * @sock: socket
4007  *
4008  * Check permission before the remote address (name) of a socket object.
4009  *
4010  * Return: Returns 0 if permission is granted.
4011  */
4012 int security_socket_getpeername(struct socket *sock)
4013 {
4014 	return call_int_hook(socket_getpeername, 0, sock);
4015 }
4016 
4017 /**
4018  * security_socket_getsockopt() - Check if reading a socket option is allowed
4019  * @sock: socket
4020  * @level: option's protocol level
4021  * @optname: option name
4022  *
4023  * Check permissions before retrieving the options associated with socket
4024  * @sock.
4025  *
4026  * Return: Returns 0 if permission is granted.
4027  */
4028 int security_socket_getsockopt(struct socket *sock, int level, int optname)
4029 {
4030 	return call_int_hook(socket_getsockopt, 0, sock, level, optname);
4031 }
4032 
4033 /**
4034  * security_socket_setsockopt() - Check if setting a socket option is allowed
4035  * @sock: socket
4036  * @level: option's protocol level
4037  * @optname: option name
4038  *
4039  * Check permissions before setting the options associated with socket @sock.
4040  *
4041  * Return: Returns 0 if permission is granted.
4042  */
4043 int security_socket_setsockopt(struct socket *sock, int level, int optname)
4044 {
4045 	return call_int_hook(socket_setsockopt, 0, sock, level, optname);
4046 }
4047 
4048 /**
4049  * security_socket_shutdown() - Checks if shutting down the socket is allowed
4050  * @sock: socket
4051  * @how: flag indicating how sends and receives are handled
4052  *
4053  * Checks permission before all or part of a connection on the socket @sock is
4054  * shut down.
4055  *
4056  * Return: Returns 0 if permission is granted.
4057  */
4058 int security_socket_shutdown(struct socket *sock, int how)
4059 {
4060 	return call_int_hook(socket_shutdown, 0, sock, how);
4061 }
4062 
4063 /**
4064  * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4065  * @sk: destination sock
4066  * @skb: incoming packet
4067  *
4068  * Check permissions on incoming network packets.  This hook is distinct from
4069  * Netfilter's IP input hooks since it is the first time that the incoming
4070  * sk_buff @skb has been associated with a particular socket, @sk.  Must not
4071  * sleep inside this hook because some callers hold spinlocks.
4072  *
4073  * Return: Returns 0 if permission is granted.
4074  */
4075 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4076 {
4077 	return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
4078 }
4079 EXPORT_SYMBOL(security_sock_rcv_skb);
4080 
4081 /**
4082  * security_socket_getpeersec_stream() - Get the remote peer label
4083  * @sock: socket
4084  * @optval: destination buffer
4085  * @optlen: size of peer label copied into the buffer
4086  * @len: maximum size of the destination buffer
4087  *
4088  * This hook allows the security module to provide peer socket security state
4089  * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4090  * For tcp sockets this can be meaningful if the socket is associated with an
4091  * ipsec SA.
4092  *
4093  * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4094  *         values.
4095  */
4096 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4097 				      sockptr_t optlen, unsigned int len)
4098 {
4099 	return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
4100 			     optval, optlen, len);
4101 }
4102 
4103 /**
4104  * security_socket_getpeersec_dgram() - Get the remote peer label
4105  * @sock: socket
4106  * @skb: datagram packet
4107  * @secid: remote peer label secid
4108  *
4109  * This hook allows the security module to provide peer socket security state
4110  * for udp sockets on a per-packet basis to userspace via getsockopt
4111  * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4112  * option via getsockopt. It can then retrieve the security state returned by
4113  * this hook for a packet via the SCM_SECURITY ancillary message type.
4114  *
4115  * Return: Returns 0 on success, error on failure.
4116  */
4117 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4118 {
4119 	return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
4120 			     skb, secid);
4121 }
4122 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4123 
4124 /**
4125  * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4126  * @sk: sock
4127  * @family: protocol family
4128  * @priotity: gfp flags
4129  *
4130  * Allocate and attach a security structure to the sk->sk_security field, which
4131  * is used to copy security attributes between local stream sockets.
4132  *
4133  * Return: Returns 0 on success, error on failure.
4134  */
4135 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4136 {
4137 	return call_int_hook(sk_alloc_security, 0, sk, family, priority);
4138 }
4139 
4140 /**
4141  * security_sk_free() - Free the sock's LSM blob
4142  * @sk: sock
4143  *
4144  * Deallocate security structure.
4145  */
4146 void security_sk_free(struct sock *sk)
4147 {
4148 	call_void_hook(sk_free_security, sk);
4149 }
4150 
4151 /**
4152  * security_sk_clone() - Clone a sock's LSM state
4153  * @sk: original sock
4154  * @newsk: target sock
4155  *
4156  * Clone/copy security structure.
4157  */
4158 void security_sk_clone(const struct sock *sk, struct sock *newsk)
4159 {
4160 	call_void_hook(sk_clone_security, sk, newsk);
4161 }
4162 EXPORT_SYMBOL(security_sk_clone);
4163 
4164 void security_sk_classify_flow(struct sock *sk, struct flowi_common *flic)
4165 {
4166 	call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
4167 }
4168 EXPORT_SYMBOL(security_sk_classify_flow);
4169 
4170 /**
4171  * security_req_classify_flow() - Set a flow's secid based on request_sock
4172  * @req: request_sock
4173  * @flic: target flow
4174  *
4175  * Sets @flic's secid to @req's secid.
4176  */
4177 void security_req_classify_flow(const struct request_sock *req,
4178 				struct flowi_common *flic)
4179 {
4180 	call_void_hook(req_classify_flow, req, flic);
4181 }
4182 EXPORT_SYMBOL(security_req_classify_flow);
4183 
4184 /**
4185  * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4186  * @sk: sock being grafted
4187  * @sock: target socket
4188  *
4189  * Sets @sock's inode secid to @sk's secid and update @sk with any necessary
4190  * LSM state from @sock.
4191  */
4192 void security_sock_graft(struct sock *sk, struct socket *parent)
4193 {
4194 	call_void_hook(sock_graft, sk, parent);
4195 }
4196 EXPORT_SYMBOL(security_sock_graft);
4197 
4198 /**
4199  * security_inet_conn_request() - Set request_sock state using incoming connect
4200  * @sk: parent listening sock
4201  * @skb: incoming connection
4202  * @req: new request_sock
4203  *
4204  * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4205  *
4206  * Return: Returns 0 if permission is granted.
4207  */
4208 int security_inet_conn_request(const struct sock *sk,
4209 			struct sk_buff *skb, struct request_sock *req)
4210 {
4211 	return call_int_hook(inet_conn_request, 0, sk, skb, req);
4212 }
4213 EXPORT_SYMBOL(security_inet_conn_request);
4214 
4215 /**
4216  * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4217  * @newsk: new sock
4218  * @req: connection request_sock
4219  *
4220  * Set that LSM state of @sock using the LSM state from @req.
4221  */
4222 void security_inet_csk_clone(struct sock *newsk,
4223 			const struct request_sock *req)
4224 {
4225 	call_void_hook(inet_csk_clone, newsk, req);
4226 }
4227 
4228 /**
4229  * security_inet_conn_established() - Update sock's LSM state with connection
4230  * @sk: sock
4231  * @skb: connection packet
4232  *
4233  * Update @sock's LSM state to represent a new connection from @skb.
4234  */
4235 void security_inet_conn_established(struct sock *sk,
4236 			struct sk_buff *skb)
4237 {
4238 	call_void_hook(inet_conn_established, sk, skb);
4239 }
4240 EXPORT_SYMBOL(security_inet_conn_established);
4241 
4242 /**
4243  * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4244  * @secid: new secmark value
4245  *
4246  * Check if the process should be allowed to relabel packets to @secid.
4247  *
4248  * Return: Returns 0 if permission is granted.
4249  */
4250 int security_secmark_relabel_packet(u32 secid)
4251 {
4252 	return call_int_hook(secmark_relabel_packet, 0, secid);
4253 }
4254 EXPORT_SYMBOL(security_secmark_relabel_packet);
4255 
4256 /**
4257  * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4258  *
4259  * Tells the LSM to increment the number of secmark labeling rules loaded.
4260  */
4261 void security_secmark_refcount_inc(void)
4262 {
4263 	call_void_hook(secmark_refcount_inc);
4264 }
4265 EXPORT_SYMBOL(security_secmark_refcount_inc);
4266 
4267 /**
4268  * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4269  *
4270  * Tells the LSM to decrement the number of secmark labeling rules loaded.
4271  */
4272 void security_secmark_refcount_dec(void)
4273 {
4274 	call_void_hook(secmark_refcount_dec);
4275 }
4276 EXPORT_SYMBOL(security_secmark_refcount_dec);
4277 
4278 /**
4279  * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4280  * @security: pointer to the LSM blob
4281  *
4282  * This hook allows a module to allocate a security structure for a TUN	device,
4283  * returning the pointer in @security.
4284  *
4285  * Return: Returns a zero on success, negative values on failure.
4286  */
4287 int security_tun_dev_alloc_security(void **security)
4288 {
4289 	return call_int_hook(tun_dev_alloc_security, 0, security);
4290 }
4291 EXPORT_SYMBOL(security_tun_dev_alloc_security);
4292 
4293 /**
4294  * security_tun_dev_free_security() - Free a TUN device LSM blob
4295  * @security: LSM blob
4296  *
4297  * This hook allows a module to free the security structure for a TUN device.
4298  */
4299 void security_tun_dev_free_security(void *security)
4300 {
4301 	call_void_hook(tun_dev_free_security, security);
4302 }
4303 EXPORT_SYMBOL(security_tun_dev_free_security);
4304 
4305 /**
4306  * security_tun_dev_create() - Check if creating a TUN device is allowed
4307  *
4308  * Check permissions prior to creating a new TUN device.
4309  *
4310  * Return: Returns 0 if permission is granted.
4311  */
4312 int security_tun_dev_create(void)
4313 {
4314 	return call_int_hook(tun_dev_create, 0);
4315 }
4316 EXPORT_SYMBOL(security_tun_dev_create);
4317 
4318 /**
4319  * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4320  * @security: TUN device LSM blob
4321  *
4322  * Check permissions prior to attaching to a TUN device queue.
4323  *
4324  * Return: Returns 0 if permission is granted.
4325  */
4326 int security_tun_dev_attach_queue(void *security)
4327 {
4328 	return call_int_hook(tun_dev_attach_queue, 0, security);
4329 }
4330 EXPORT_SYMBOL(security_tun_dev_attach_queue);
4331 
4332 /**
4333  * security_tun_dev_attach() - Update TUN device LSM state on attach
4334  * @sk: associated sock
4335  * @security: TUN device LSM blob
4336  *
4337  * This hook can be used by the module to update any security state associated
4338  * with the TUN device's sock structure.
4339  *
4340  * Return: Returns 0 if permission is granted.
4341  */
4342 int security_tun_dev_attach(struct sock *sk, void *security)
4343 {
4344 	return call_int_hook(tun_dev_attach, 0, sk, security);
4345 }
4346 EXPORT_SYMBOL(security_tun_dev_attach);
4347 
4348 /**
4349  * security_tun_dev_open() - Update TUN device LSM state on open
4350  * @security: TUN device LSM blob
4351  *
4352  * This hook can be used by the module to update any security state associated
4353  * with the TUN device's security structure.
4354  *
4355  * Return: Returns 0 if permission is granted.
4356  */
4357 int security_tun_dev_open(void *security)
4358 {
4359 	return call_int_hook(tun_dev_open, 0, security);
4360 }
4361 EXPORT_SYMBOL(security_tun_dev_open);
4362 
4363 /**
4364  * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4365  * @asoc: SCTP association
4366  * @skb: packet requesting the association
4367  *
4368  * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4369  *
4370  * Return: Returns 0 on success, error on failure.
4371  */
4372 int security_sctp_assoc_request(struct sctp_association *asoc, struct sk_buff *skb)
4373 {
4374 	return call_int_hook(sctp_assoc_request, 0, asoc, skb);
4375 }
4376 EXPORT_SYMBOL(security_sctp_assoc_request);
4377 
4378 /**
4379  * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4380  * @sk: socket
4381  * @optname: SCTP option to validate
4382  * @address: list of IP addresses to validate
4383  * @addrlen: length of the address list
4384  *
4385  * Validiate permissions required for each address associated with sock	@sk.
4386  * Depending on @optname, the addresses will be treated as either a connect or
4387  * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4388  * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4389  *
4390  * Return: Returns 0 on success, error on failure.
4391  */
4392 int security_sctp_bind_connect(struct sock *sk, int optname,
4393 			       struct sockaddr *address, int addrlen)
4394 {
4395 	return call_int_hook(sctp_bind_connect, 0, sk, optname,
4396 			     address, addrlen);
4397 }
4398 EXPORT_SYMBOL(security_sctp_bind_connect);
4399 
4400 /**
4401  * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4402  * @asoc: SCTP association
4403  * @sk: original sock
4404  * @newsk: target sock
4405  *
4406  * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4407  * socket) or when a socket is 'peeled off' e.g userspace calls
4408  * sctp_peeloff(3).
4409  */
4410 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
4411 			    struct sock *newsk)
4412 {
4413 	call_void_hook(sctp_sk_clone, asoc, sk, newsk);
4414 }
4415 EXPORT_SYMBOL(security_sctp_sk_clone);
4416 
4417 /**
4418  * security_sctp_assoc_established() - Update LSM state when assoc established
4419  * @asoc: SCTP association
4420  * @skb: packet establishing the association
4421  *
4422  * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4423  * security module.
4424  *
4425  * Return: Returns 0 if permission is granted.
4426  */
4427 int security_sctp_assoc_established(struct sctp_association *asoc,
4428 				    struct sk_buff *skb)
4429 {
4430 	return call_int_hook(sctp_assoc_established, 0, asoc, skb);
4431 }
4432 EXPORT_SYMBOL(security_sctp_assoc_established);
4433 
4434 #endif	/* CONFIG_SECURITY_NETWORK */
4435 
4436 #ifdef CONFIG_SECURITY_INFINIBAND
4437 
4438 /**
4439  * security_ib_pkey_access() - Check if access to an IB pkey is allowed
4440  * @sec: LSM blob
4441  * @subnet_prefix: subnet prefix of the port
4442  * @pkey: IB pkey
4443  *
4444  * Check permission to access a pkey when modifing a QP.
4445  *
4446  * Return: Returns 0 if permission is granted.
4447  */
4448 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
4449 {
4450 	return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
4451 }
4452 EXPORT_SYMBOL(security_ib_pkey_access);
4453 
4454 /**
4455  * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
4456  * @sec: LSM blob
4457  * @dev_name: IB device name
4458  * @port_num: port number
4459  *
4460  * Check permissions to send and receive SMPs on a end port.
4461  *
4462  * Return: Returns 0 if permission is granted.
4463  */
4464 int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
4465 {
4466 	return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
4467 }
4468 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
4469 
4470 /**
4471  * security_ib_alloc_security() - Allocate an Infiniband LSM blob
4472  * @sec: LSM blob
4473  *
4474  * Allocate a security structure for Infiniband objects.
4475  *
4476  * Return: Returns 0 on success, non-zero on failure.
4477  */
4478 int security_ib_alloc_security(void **sec)
4479 {
4480 	return call_int_hook(ib_alloc_security, 0, sec);
4481 }
4482 EXPORT_SYMBOL(security_ib_alloc_security);
4483 
4484 /**
4485  * security_ib_free_security() - Free an Infiniband LSM blob
4486  * @sec: LSM blob
4487  *
4488  * Deallocate an Infiniband security structure.
4489  */
4490 void security_ib_free_security(void *sec)
4491 {
4492 	call_void_hook(ib_free_security, sec);
4493 }
4494 EXPORT_SYMBOL(security_ib_free_security);
4495 #endif	/* CONFIG_SECURITY_INFINIBAND */
4496 
4497 #ifdef CONFIG_SECURITY_NETWORK_XFRM
4498 
4499 /**
4500  * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
4501  * @ctxp: xfrm security context being added to the SPD
4502  * @sec_ctx: security label provided by userspace
4503  * @gfp: gfp flags
4504  *
4505  * Allocate a security structure to the xp->security field; the security field
4506  * is initialized to NULL when the xfrm_policy is allocated.
4507  *
4508  * Return:  Return 0 if operation was successful.
4509  */
4510 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
4511 			       struct xfrm_user_sec_ctx *sec_ctx,
4512 			       gfp_t gfp)
4513 {
4514 	return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
4515 }
4516 EXPORT_SYMBOL(security_xfrm_policy_alloc);
4517 
4518 /**
4519  * security_xfrm_policy_clone() - Clone xfrm policy LSM state
4520  * @old_ctx: xfrm security context
4521  * @new_ctxp: target xfrm security context
4522  *
4523  * Allocate a security structure in new_ctxp that contains the information from
4524  * the old_ctx structure.
4525  *
4526  * Return: Return 0 if operation was successful.
4527  */
4528 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
4529 			      struct xfrm_sec_ctx **new_ctxp)
4530 {
4531 	return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
4532 }
4533 
4534 /**
4535  * security_xfrm_policy_free() - Free a xfrm security context
4536  * @ctx: xfrm security context
4537  *
4538  * Free LSM resources associated with @ctx.
4539  */
4540 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
4541 {
4542 	call_void_hook(xfrm_policy_free_security, ctx);
4543 }
4544 EXPORT_SYMBOL(security_xfrm_policy_free);
4545 
4546 /**
4547  * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
4548  * @ctx: xfrm security context
4549  *
4550  * Authorize deletion of a SPD entry.
4551  *
4552  * Return: Returns 0 if permission is granted.
4553  */
4554 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
4555 {
4556 	return call_int_hook(xfrm_policy_delete_security, 0, ctx);
4557 }
4558 
4559 /**
4560  * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
4561  * @x: xfrm state being added to the SAD
4562  * @sec_ctx: security label provided by userspace
4563  *
4564  * Allocate a security structure to the @x->security field; the security field
4565  * is initialized to NULL when the xfrm_state is allocated. Set the context to
4566  * correspond to @sec_ctx.
4567  *
4568  * Return: Return 0 if operation was successful.
4569  */
4570 int security_xfrm_state_alloc(struct xfrm_state *x,
4571 			      struct xfrm_user_sec_ctx *sec_ctx)
4572 {
4573 	return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
4574 }
4575 EXPORT_SYMBOL(security_xfrm_state_alloc);
4576 
4577 /**
4578  * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
4579  * @x: xfrm state being added to the SAD
4580  * @polsec: associated policy's security context
4581  * @secid: secid from the flow
4582  *
4583  * Allocate a security structure to the x->security field; the security field
4584  * is initialized to NULL when the xfrm_state is allocated.  Set the context to
4585  * correspond to secid.
4586  *
4587  * Return: Returns 0 if operation was successful.
4588  */
4589 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
4590 				      struct xfrm_sec_ctx *polsec, u32 secid)
4591 {
4592 	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
4593 }
4594 
4595 /**
4596  * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
4597  * @x: xfrm state
4598  *
4599  * Authorize deletion of x->security.
4600  *
4601  * Return: Returns 0 if permission is granted.
4602  */
4603 int security_xfrm_state_delete(struct xfrm_state *x)
4604 {
4605 	return call_int_hook(xfrm_state_delete_security, 0, x);
4606 }
4607 EXPORT_SYMBOL(security_xfrm_state_delete);
4608 
4609 /**
4610  * security_xfrm_state_free() - Free a xfrm state
4611  * @x: xfrm state
4612  *
4613  * Deallocate x->security.
4614  */
4615 void security_xfrm_state_free(struct xfrm_state *x)
4616 {
4617 	call_void_hook(xfrm_state_free_security, x);
4618 }
4619 
4620 /**
4621  * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
4622  * @ctx: target xfrm security context
4623  * @fl_secid: flow secid used to authorize access
4624  *
4625  * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
4626  * packet.  The hook is called when selecting either a per-socket policy or a
4627  * generic xfrm policy.
4628  *
4629  * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
4630  *         other errors.
4631  */
4632 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
4633 {
4634 	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
4635 }
4636 
4637 /**
4638  * security_xfrm_state_pol_flow_match() - Check for a xfrm match
4639  * @x: xfrm state to match
4640  * @xp xfrm policy to check for a match
4641  * @flic: flow to check for a match.
4642  *
4643  * Check @xp and @flic for a match with @x.
4644  *
4645  * Return: Returns 1 if there is a match.
4646  */
4647 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
4648 				       struct xfrm_policy *xp,
4649 				       const struct flowi_common *flic)
4650 {
4651 	struct security_hook_list *hp;
4652 	int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
4653 
4654 	/*
4655 	 * Since this function is expected to return 0 or 1, the judgment
4656 	 * becomes difficult if multiple LSMs supply this call. Fortunately,
4657 	 * we can use the first LSM's judgment because currently only SELinux
4658 	 * supplies this call.
4659 	 *
4660 	 * For speed optimization, we explicitly break the loop rather than
4661 	 * using the macro
4662 	 */
4663 	hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
4664 				list) {
4665 		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
4666 		break;
4667 	}
4668 	return rc;
4669 }
4670 
4671 /**
4672  * security_xfrm_decode_session() - Determine the xfrm secid for a packet
4673  * @skb: xfrm packet
4674  * @secid: secid
4675  *
4676  * Decode the packet in @skb and return the security label in @secid.
4677  *
4678  * Return: Return 0 if all xfrms used have the same secid.
4679  */
4680 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
4681 {
4682 	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
4683 }
4684 
4685 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
4686 {
4687 	int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
4688 				0);
4689 
4690 	BUG_ON(rc);
4691 }
4692 EXPORT_SYMBOL(security_skb_classify_flow);
4693 
4694 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
4695 
4696 #ifdef CONFIG_KEYS
4697 
4698 /**
4699  * security_key_alloc() - Allocate and initialize a kernel key LSM blob
4700  * @key: key
4701  * @cred: credentials
4702  * @flags: allocation flags
4703  *
4704  * Permit allocation of a key and assign security data. Note that key does not
4705  * have a serial number assigned at this point.
4706  *
4707  * Return: Return 0 if permission is granted, -ve error otherwise.
4708  */
4709 int security_key_alloc(struct key *key, const struct cred *cred,
4710 		       unsigned long flags)
4711 {
4712 	return call_int_hook(key_alloc, 0, key, cred, flags);
4713 }
4714 
4715 /**
4716  * security_key_free() - Free a kernel key LSM blob
4717  * @key: key
4718  *
4719  * Notification of destruction; free security data.
4720  */
4721 void security_key_free(struct key *key)
4722 {
4723 	call_void_hook(key_free, key);
4724 }
4725 
4726 /**
4727  * security_key_permission() - Check if a kernel key operation is allowed
4728  * @key_ref: key reference
4729  * @cred: credentials of actor requesting access
4730  * @need_perm: requested permissions
4731  *
4732  * See whether a specific operational right is granted to a process on a key.
4733  *
4734  * Return: Return 0 if permission is granted, -ve error otherwise.
4735  */
4736 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
4737 			    enum key_need_perm need_perm)
4738 {
4739 	return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
4740 }
4741 
4742 /**
4743  * security_key_getsecurity() - Get the key's security label
4744  * @key: key
4745  * @buffer: security label buffer
4746  *
4747  * Get a textual representation of the security context attached to a key for
4748  * the purposes of honouring KEYCTL_GETSECURITY.  This function allocates the
4749  * storage for the NUL-terminated string and the caller should free it.
4750  *
4751  * Return: Returns the length of @buffer (including terminating NUL) or -ve if
4752  *         an error occurs.  May also return 0 (and a NULL buffer pointer) if
4753  *         there is no security label assigned to the key.
4754  */
4755 int security_key_getsecurity(struct key *key, char **_buffer)
4756 {
4757 	*_buffer = NULL;
4758 	return call_int_hook(key_getsecurity, 0, key, _buffer);
4759 }
4760 
4761 #endif	/* CONFIG_KEYS */
4762 
4763 #ifdef CONFIG_AUDIT
4764 
4765 /**
4766  * security_audit_rule_init() - Allocate and init an LSM audit rule struct
4767  * @field: audit action
4768  * @op: rule operator
4769  * @rulestr: rule context
4770  * @lsmrule: receive buffer for audit rule struct
4771  *
4772  * Allocate and initialize an LSM audit rule structure.
4773  *
4774  * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
4775  *         an invalid rule.
4776  */
4777 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
4778 {
4779 	return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
4780 }
4781 
4782 /**
4783  * security_audit_rule_known() - Check if an audit rule contains LSM fields
4784  * @krule: audit rule
4785  *
4786  * Specifies whether given @krule contains any fields related to the current
4787  * LSM.
4788  *
4789  * Return: Returns 1 in case of relation found, 0 otherwise.
4790  */
4791 int security_audit_rule_known(struct audit_krule *krule)
4792 {
4793 	return call_int_hook(audit_rule_known, 0, krule);
4794 }
4795 
4796 /**
4797  * security_audit_rule_free() - Free an LSM audit rule struct
4798  * @lsmrule: audit rule struct
4799  *
4800  * Deallocate the LSM audit rule structure previously allocated by
4801  * audit_rule_init().
4802  */
4803 void security_audit_rule_free(void *lsmrule)
4804 {
4805 	call_void_hook(audit_rule_free, lsmrule);
4806 }
4807 
4808 /**
4809  * security_audit_rule_match() - Check if a label matches an audit rule
4810  * @secid: security label
4811  * @field: LSM audit field
4812  * @op: matching operator
4813  * @lsmrule: audit rule
4814  *
4815  * Determine if given @secid matches a rule previously approved by
4816  * security_audit_rule_known().
4817  *
4818  * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
4819  *         failure.
4820  */
4821 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
4822 {
4823 	return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
4824 }
4825 #endif /* CONFIG_AUDIT */
4826 
4827 #ifdef CONFIG_BPF_SYSCALL
4828 /**
4829  * security_bpf() - Check if the bpf syscall operation is allowed
4830  * @cmd: command
4831  * @attr: bpf attribute
4832  * @size: size
4833  *
4834  * Do a initial check for all bpf syscalls after the attribute is copied into
4835  * the kernel. The actual security module can implement their own rules to
4836  * check the specific cmd they need.
4837  *
4838  * Return: Returns 0 if permission is granted.
4839  */
4840 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
4841 {
4842 	return call_int_hook(bpf, 0, cmd, attr, size);
4843 }
4844 
4845 /**
4846  * security_bpf_map() - Check if access to a bpf map is allowed
4847  * @map: bpf map
4848  * @fmode: mode
4849  *
4850  * Do a check when the kernel generates and returns a file descriptor for eBPF
4851  * maps.
4852  *
4853  * Return: Returns 0 if permission is granted.
4854  */
4855 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
4856 {
4857 	return call_int_hook(bpf_map, 0, map, fmode);
4858 }
4859 
4860 /**
4861  * security_bpf_prog() - Check if access to a bpf program is allowed
4862  * @prog: bpf program
4863  *
4864  * Do a check when the kernel generates and returns a file descriptor for eBPF
4865  * programs.
4866  *
4867  * Return: Returns 0 if permission is granted.
4868  */
4869 int security_bpf_prog(struct bpf_prog *prog)
4870 {
4871 	return call_int_hook(bpf_prog, 0, prog);
4872 }
4873 
4874 /**
4875  * security_bpf_map_alloc() - Allocate a bpf map LSM blob
4876  * @map: bpf map
4877  *
4878  * Initialize the security field inside bpf map.
4879  *
4880  * Return: Returns 0 on success, error on failure.
4881  */
4882 int security_bpf_map_alloc(struct bpf_map *map)
4883 {
4884 	return call_int_hook(bpf_map_alloc_security, 0, map);
4885 }
4886 
4887 /**
4888  * security_bpf_prog_alloc() - Allocate a bpf program LSM blob
4889  * @aux: bpf program aux info struct
4890  *
4891  * Initialize the security field inside bpf program.
4892  *
4893  * Return: Returns 0 on success, error on failure.
4894  */
4895 int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
4896 {
4897 	return call_int_hook(bpf_prog_alloc_security, 0, aux);
4898 }
4899 
4900 /**
4901  * security_bpf_map_free() - Free a bpf map's LSM blob
4902  * @map: bpf map
4903  *
4904  * Clean up the security information stored inside bpf map.
4905  */
4906 void security_bpf_map_free(struct bpf_map *map)
4907 {
4908 	call_void_hook(bpf_map_free_security, map);
4909 }
4910 
4911 /**
4912  * security_bpf_prog_free() - Free a bpf program's LSM blob
4913  * @aux: bpf program aux info struct
4914  *
4915  * Clean up the security information stored inside bpf prog.
4916  */
4917 void security_bpf_prog_free(struct bpf_prog_aux *aux)
4918 {
4919 	call_void_hook(bpf_prog_free_security, aux);
4920 }
4921 #endif /* CONFIG_BPF_SYSCALL */
4922 
4923 int security_locked_down(enum lockdown_reason what)
4924 {
4925 	return call_int_hook(locked_down, 0, what);
4926 }
4927 EXPORT_SYMBOL(security_locked_down);
4928 
4929 #ifdef CONFIG_PERF_EVENTS
4930 int security_perf_event_open(struct perf_event_attr *attr, int type)
4931 {
4932 	return call_int_hook(perf_event_open, 0, attr, type);
4933 }
4934 
4935 int security_perf_event_alloc(struct perf_event *event)
4936 {
4937 	return call_int_hook(perf_event_alloc, 0, event);
4938 }
4939 
4940 void security_perf_event_free(struct perf_event *event)
4941 {
4942 	call_void_hook(perf_event_free, event);
4943 }
4944 
4945 int security_perf_event_read(struct perf_event *event)
4946 {
4947 	return call_int_hook(perf_event_read, 0, event);
4948 }
4949 
4950 int security_perf_event_write(struct perf_event *event)
4951 {
4952 	return call_int_hook(perf_event_write, 0, event);
4953 }
4954 #endif /* CONFIG_PERF_EVENTS */
4955 
4956 #ifdef CONFIG_IO_URING
4957 int security_uring_override_creds(const struct cred *new)
4958 {
4959 	return call_int_hook(uring_override_creds, 0, new);
4960 }
4961 
4962 int security_uring_sqpoll(void)
4963 {
4964 	return call_int_hook(uring_sqpoll, 0);
4965 }
4966 int security_uring_cmd(struct io_uring_cmd *ioucmd)
4967 {
4968 	return call_int_hook(uring_cmd, 0, ioucmd);
4969 }
4970 #endif /* CONFIG_IO_URING */
4971