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