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