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