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