xref: /openbmc/linux/security/security.c (revision 63c1845b)
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_ops: 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_ops: 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  * @olddb: source superblock
1411  * @newdb: 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  * @flags: flags
2129  *
2130  * Check permission before setting the extended attributes.
2131  *
2132  * Return: Returns 0 if permission is granted.
2133  */
2134 int security_inode_setxattr(struct mnt_idmap *idmap,
2135 			    struct dentry *dentry, const char *name,
2136 			    const void *value, size_t size, int flags)
2137 {
2138 	int ret;
2139 
2140 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2141 		return 0;
2142 	/*
2143 	 * SELinux and Smack integrate the cap call,
2144 	 * so assume that all LSMs supplying this call do so.
2145 	 */
2146 	ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
2147 			    size, flags);
2148 
2149 	if (ret == 1)
2150 		ret = cap_inode_setxattr(dentry, name, value, size, flags);
2151 	if (ret)
2152 		return ret;
2153 	ret = ima_inode_setxattr(dentry, name, value, size);
2154 	if (ret)
2155 		return ret;
2156 	return evm_inode_setxattr(idmap, dentry, name, value, size);
2157 }
2158 
2159 /**
2160  * security_inode_set_acl() - Check if setting posix acls is allowed
2161  * @idmap: idmap of the mount
2162  * @dentry: file
2163  * @acl_name: acl name
2164  * @kacl: acl struct
2165  *
2166  * Check permission before setting posix acls, the posix acls in @kacl are
2167  * identified by @acl_name.
2168  *
2169  * Return: Returns 0 if permission is granted.
2170  */
2171 int security_inode_set_acl(struct mnt_idmap *idmap,
2172 			   struct dentry *dentry, const char *acl_name,
2173 			   struct posix_acl *kacl)
2174 {
2175 	int ret;
2176 
2177 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2178 		return 0;
2179 	ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
2180 			    kacl);
2181 	if (ret)
2182 		return ret;
2183 	ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
2184 	if (ret)
2185 		return ret;
2186 	return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
2187 }
2188 
2189 /**
2190  * security_inode_get_acl() - Check if reading posix acls is allowed
2191  * @idmap: idmap of the mount
2192  * @dentry: file
2193  * @acl_name: acl name
2194  *
2195  * Check permission before getting osix acls, the posix acls are identified by
2196  * @acl_name.
2197  *
2198  * Return: Returns 0 if permission is granted.
2199  */
2200 int security_inode_get_acl(struct mnt_idmap *idmap,
2201 			   struct dentry *dentry, const char *acl_name)
2202 {
2203 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2204 		return 0;
2205 	return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
2206 }
2207 
2208 /**
2209  * security_inode_remove_acl() - Check if removing a posix acl is allowed
2210  * @idmap: idmap of the mount
2211  * @dentry: file
2212  * @acl_name: acl name
2213  *
2214  * Check permission before removing posix acls, the posix acls are identified
2215  * by @acl_name.
2216  *
2217  * Return: Returns 0 if permission is granted.
2218  */
2219 int security_inode_remove_acl(struct mnt_idmap *idmap,
2220 			      struct dentry *dentry, const char *acl_name)
2221 {
2222 	int ret;
2223 
2224 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2225 		return 0;
2226 	ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
2227 	if (ret)
2228 		return ret;
2229 	ret = ima_inode_remove_acl(idmap, dentry, acl_name);
2230 	if (ret)
2231 		return ret;
2232 	return evm_inode_remove_acl(idmap, dentry, acl_name);
2233 }
2234 
2235 /**
2236  * security_inode_post_setxattr() - Update the inode after a setxattr operation
2237  * @dentry: file
2238  * @name: xattr name
2239  * @value: xattr value
2240  * @size: xattr value size
2241  * @flags: flags
2242  *
2243  * Update inode security field after successful setxattr operation.
2244  */
2245 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2246 				  const void *value, size_t size, int flags)
2247 {
2248 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2249 		return;
2250 	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
2251 	evm_inode_post_setxattr(dentry, name, value, size);
2252 }
2253 
2254 /**
2255  * security_inode_getxattr() - Check if xattr access is allowed
2256  * @dentry: file
2257  * @name: xattr name
2258  *
2259  * Check permission before obtaining the extended attributes identified by
2260  * @name for @dentry.
2261  *
2262  * Return: Returns 0 if permission is granted.
2263  */
2264 int security_inode_getxattr(struct dentry *dentry, const char *name)
2265 {
2266 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2267 		return 0;
2268 	return call_int_hook(inode_getxattr, 0, dentry, name);
2269 }
2270 
2271 /**
2272  * security_inode_listxattr() - Check if listing xattrs is allowed
2273  * @dentry: file
2274  *
2275  * Check permission before obtaining the list of extended attribute names for
2276  * @dentry.
2277  *
2278  * Return: Returns 0 if permission is granted.
2279  */
2280 int security_inode_listxattr(struct dentry *dentry)
2281 {
2282 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2283 		return 0;
2284 	return call_int_hook(inode_listxattr, 0, dentry);
2285 }
2286 
2287 /**
2288  * security_inode_removexattr() - Check if removing an xattr is allowed
2289  * @idmap: idmap of the mount
2290  * @dentry: file
2291  * @name: xattr name
2292  *
2293  * Check permission before removing the extended attribute identified by @name
2294  * for @dentry.
2295  *
2296  * Return: Returns 0 if permission is granted.
2297  */
2298 int security_inode_removexattr(struct mnt_idmap *idmap,
2299 			       struct dentry *dentry, const char *name)
2300 {
2301 	int ret;
2302 
2303 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2304 		return 0;
2305 	/*
2306 	 * SELinux and Smack integrate the cap call,
2307 	 * so assume that all LSMs supplying this call do so.
2308 	 */
2309 	ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
2310 	if (ret == 1)
2311 		ret = cap_inode_removexattr(idmap, dentry, name);
2312 	if (ret)
2313 		return ret;
2314 	ret = ima_inode_removexattr(dentry, name);
2315 	if (ret)
2316 		return ret;
2317 	return evm_inode_removexattr(idmap, dentry, name);
2318 }
2319 
2320 /**
2321  * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2322  * @dentry: associated dentry
2323  *
2324  * Called when an inode has been changed to determine if
2325  * security_inode_killpriv() should be called.
2326  *
2327  * Return: Return <0 on error to abort the inode change operation, return 0 if
2328  *         security_inode_killpriv() does not need to be called, return >0 if
2329  *         security_inode_killpriv() does need to be called.
2330  */
2331 int security_inode_need_killpriv(struct dentry *dentry)
2332 {
2333 	return call_int_hook(inode_need_killpriv, 0, dentry);
2334 }
2335 
2336 /**
2337  * security_inode_killpriv() - The setuid bit is removed, update LSM state
2338  * @idmap: idmap of the mount
2339  * @dentry: associated dentry
2340  *
2341  * The @dentry's setuid bit is being removed.  Remove similar security labels.
2342  * Called with the dentry->d_inode->i_mutex held.
2343  *
2344  * Return: Return 0 on success.  If error is returned, then the operation
2345  *         causing setuid bit removal is failed.
2346  */
2347 int security_inode_killpriv(struct mnt_idmap *idmap,
2348 			    struct dentry *dentry)
2349 {
2350 	return call_int_hook(inode_killpriv, 0, idmap, dentry);
2351 }
2352 
2353 /**
2354  * security_inode_getsecurity() - Get the xattr security label of an inode
2355  * @idmap: idmap of the mount
2356  * @inode: inode
2357  * @name: xattr name
2358  * @buffer: security label buffer
2359  * @alloc: allocation flag
2360  *
2361  * Retrieve a copy of the extended attribute representation of the security
2362  * label associated with @name for @inode via @buffer.  Note that @name is the
2363  * remainder of the attribute name after the security prefix has been removed.
2364  * @alloc is used to specify if the call should return a value via the buffer
2365  * or just the value length.
2366  *
2367  * Return: Returns size of buffer on success.
2368  */
2369 int security_inode_getsecurity(struct mnt_idmap *idmap,
2370 			       struct inode *inode, const char *name,
2371 			       void **buffer, bool alloc)
2372 {
2373 	struct security_hook_list *hp;
2374 	int rc;
2375 
2376 	if (unlikely(IS_PRIVATE(inode)))
2377 		return LSM_RET_DEFAULT(inode_getsecurity);
2378 	/*
2379 	 * Only one module will provide an attribute with a given name.
2380 	 */
2381 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
2382 		rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer,
2383 						alloc);
2384 		if (rc != LSM_RET_DEFAULT(inode_getsecurity))
2385 			return rc;
2386 	}
2387 	return LSM_RET_DEFAULT(inode_getsecurity);
2388 }
2389 
2390 /**
2391  * security_inode_setsecurity() - Set the xattr security label of an inode
2392  * @inode: inode
2393  * @name: xattr name
2394  * @value: security label
2395  * @size: length of security label
2396  * @flags: flags
2397  *
2398  * Set the security label associated with @name for @inode from the extended
2399  * attribute value @value.  @size indicates the size of the @value in bytes.
2400  * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2401  * remainder of the attribute name after the security. prefix has been removed.
2402  *
2403  * Return: Returns 0 on success.
2404  */
2405 int security_inode_setsecurity(struct inode *inode, const char *name,
2406 			       const void *value, size_t size, int flags)
2407 {
2408 	struct security_hook_list *hp;
2409 	int rc;
2410 
2411 	if (unlikely(IS_PRIVATE(inode)))
2412 		return LSM_RET_DEFAULT(inode_setsecurity);
2413 	/*
2414 	 * Only one module will provide an attribute with a given name.
2415 	 */
2416 	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
2417 		rc = hp->hook.inode_setsecurity(inode, name, value, size,
2418 						flags);
2419 		if (rc != LSM_RET_DEFAULT(inode_setsecurity))
2420 			return rc;
2421 	}
2422 	return LSM_RET_DEFAULT(inode_setsecurity);
2423 }
2424 
2425 /**
2426  * security_inode_listsecurity() - List the xattr security label names
2427  * @inode: inode
2428  * @buffer: buffer
2429  * @buffer_size: size of buffer
2430  *
2431  * Copy the extended attribute names for the security labels associated with
2432  * @inode into @buffer.  The maximum size of @buffer is specified by
2433  * @buffer_size.  @buffer may be NULL to request the size of the buffer
2434  * required.
2435  *
2436  * Return: Returns number of bytes used/required on success.
2437  */
2438 int security_inode_listsecurity(struct inode *inode,
2439 				char *buffer, size_t buffer_size)
2440 {
2441 	if (unlikely(IS_PRIVATE(inode)))
2442 		return 0;
2443 	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
2444 }
2445 EXPORT_SYMBOL(security_inode_listsecurity);
2446 
2447 /**
2448  * security_inode_getsecid() - Get an inode's secid
2449  * @inode: inode
2450  * @secid: secid to return
2451  *
2452  * Get the secid associated with the node.  In case of failure, @secid will be
2453  * set to zero.
2454  */
2455 void security_inode_getsecid(struct inode *inode, u32 *secid)
2456 {
2457 	call_void_hook(inode_getsecid, inode, secid);
2458 }
2459 
2460 /**
2461  * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2462  * @src: union dentry of copy-up file
2463  * @new: newly created creds
2464  *
2465  * A file is about to be copied up from lower layer to upper layer of overlay
2466  * filesystem. Security module can prepare a set of new creds and modify as
2467  * need be and return new creds. Caller will switch to new creds temporarily to
2468  * create new file and release newly allocated creds.
2469  *
2470  * Return: Returns 0 on success or a negative error code on error.
2471  */
2472 int security_inode_copy_up(struct dentry *src, struct cred **new)
2473 {
2474 	return call_int_hook(inode_copy_up, 0, src, new);
2475 }
2476 EXPORT_SYMBOL(security_inode_copy_up);
2477 
2478 /**
2479  * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2480  * @name: xattr name
2481  *
2482  * Filter the xattrs being copied up when a unioned file is copied up from a
2483  * lower layer to the union/overlay layer.   The caller is responsible for
2484  * reading and writing the xattrs, this hook is merely a filter.
2485  *
2486  * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2487  *         if the security module does not know about attribute, or a negative
2488  *         error code to abort the copy up.
2489  */
2490 int security_inode_copy_up_xattr(const char *name)
2491 {
2492 	struct security_hook_list *hp;
2493 	int rc;
2494 
2495 	/*
2496 	 * The implementation can return 0 (accept the xattr), 1 (discard the
2497 	 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2498 	 * any other error code incase of an error.
2499 	 */
2500 	hlist_for_each_entry(hp,
2501 			     &security_hook_heads.inode_copy_up_xattr, list) {
2502 		rc = hp->hook.inode_copy_up_xattr(name);
2503 		if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2504 			return rc;
2505 	}
2506 
2507 	return LSM_RET_DEFAULT(inode_copy_up_xattr);
2508 }
2509 EXPORT_SYMBOL(security_inode_copy_up_xattr);
2510 
2511 /**
2512  * security_kernfs_init_security() - Init LSM context for a kernfs node
2513  * @kn_dir: parent kernfs node
2514  * @kn: the kernfs node to initialize
2515  *
2516  * Initialize the security context of a newly created kernfs node based on its
2517  * own and its parent's attributes.
2518  *
2519  * Return: Returns 0 if permission is granted.
2520  */
2521 int security_kernfs_init_security(struct kernfs_node *kn_dir,
2522 				  struct kernfs_node *kn)
2523 {
2524 	return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2525 }
2526 
2527 /**
2528  * security_file_permission() - Check file permissions
2529  * @file: file
2530  * @mask: requested permissions
2531  *
2532  * Check file permissions before accessing an open file.  This hook is called
2533  * by various operations that read or write files.  A security module can use
2534  * this hook to perform additional checking on these operations, e.g. to
2535  * revalidate permissions on use to support privilege bracketing or policy
2536  * changes.  Notice that this hook is used when the actual read/write
2537  * operations are performed, whereas the inode_security_ops hook is called when
2538  * a file is opened (as well as many other operations).  Although this hook can
2539  * be used to revalidate permissions for various system call operations that
2540  * read or write files, it does not address the revalidation of permissions for
2541  * memory-mapped files.  Security modules must handle this separately if they
2542  * need such revalidation.
2543  *
2544  * Return: Returns 0 if permission is granted.
2545  */
2546 int security_file_permission(struct file *file, int mask)
2547 {
2548 	int ret;
2549 
2550 	ret = call_int_hook(file_permission, 0, file, mask);
2551 	if (ret)
2552 		return ret;
2553 
2554 	return fsnotify_perm(file, mask);
2555 }
2556 
2557 /**
2558  * security_file_alloc() - Allocate and init a file's LSM blob
2559  * @file: the file
2560  *
2561  * Allocate and attach a security structure to the file->f_security field.  The
2562  * security field is initialized to NULL when the structure is first created.
2563  *
2564  * Return: Return 0 if the hook is successful and permission is granted.
2565  */
2566 int security_file_alloc(struct file *file)
2567 {
2568 	int rc = lsm_file_alloc(file);
2569 
2570 	if (rc)
2571 		return rc;
2572 	rc = call_int_hook(file_alloc_security, 0, file);
2573 	if (unlikely(rc))
2574 		security_file_free(file);
2575 	return rc;
2576 }
2577 
2578 /**
2579  * security_file_free() - Free a file's LSM blob
2580  * @file: the file
2581  *
2582  * Deallocate and free any security structures stored in file->f_security.
2583  */
2584 void security_file_free(struct file *file)
2585 {
2586 	void *blob;
2587 
2588 	call_void_hook(file_free_security, file);
2589 
2590 	blob = file->f_security;
2591 	if (blob) {
2592 		file->f_security = NULL;
2593 		kmem_cache_free(lsm_file_cache, blob);
2594 	}
2595 }
2596 
2597 /**
2598  * security_file_ioctl() - Check if an ioctl is allowed
2599  * @file: associated file
2600  * @cmd: ioctl cmd
2601  * @arg: ioctl arguments
2602  *
2603  * Check permission for an ioctl operation on @file.  Note that @arg sometimes
2604  * represents a user space pointer; in other cases, it may be a simple integer
2605  * value.  When @arg represents a user space pointer, it should never be used
2606  * by the security module.
2607  *
2608  * Return: Returns 0 if permission is granted.
2609  */
2610 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2611 {
2612 	return call_int_hook(file_ioctl, 0, file, cmd, arg);
2613 }
2614 EXPORT_SYMBOL_GPL(security_file_ioctl);
2615 
2616 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
2617 {
2618 	/*
2619 	 * Does we have PROT_READ and does the application expect
2620 	 * it to imply PROT_EXEC?  If not, nothing to talk about...
2621 	 */
2622 	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2623 		return prot;
2624 	if (!(current->personality & READ_IMPLIES_EXEC))
2625 		return prot;
2626 	/*
2627 	 * if that's an anonymous mapping, let it.
2628 	 */
2629 	if (!file)
2630 		return prot | PROT_EXEC;
2631 	/*
2632 	 * ditto if it's not on noexec mount, except that on !MMU we need
2633 	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
2634 	 */
2635 	if (!path_noexec(&file->f_path)) {
2636 #ifndef CONFIG_MMU
2637 		if (file->f_op->mmap_capabilities) {
2638 			unsigned caps = file->f_op->mmap_capabilities(file);
2639 			if (!(caps & NOMMU_MAP_EXEC))
2640 				return prot;
2641 		}
2642 #endif
2643 		return prot | PROT_EXEC;
2644 	}
2645 	/* anything on noexec mount won't get PROT_EXEC */
2646 	return prot;
2647 }
2648 
2649 /**
2650  * security_mmap_file() - Check if mmap'ing a file is allowed
2651  * @file: file
2652  * @prot: protection applied by the kernel
2653  * @flags: flags
2654  *
2655  * Check permissions for a mmap operation.  The @file may be NULL, e.g. if
2656  * mapping anonymous memory.
2657  *
2658  * Return: Returns 0 if permission is granted.
2659  */
2660 int security_mmap_file(struct file *file, unsigned long prot,
2661 		       unsigned long flags)
2662 {
2663 	unsigned long prot_adj = mmap_prot(file, prot);
2664 	int ret;
2665 
2666 	ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
2667 	if (ret)
2668 		return ret;
2669 	return ima_file_mmap(file, prot, prot_adj, flags);
2670 }
2671 
2672 /**
2673  * security_mmap_addr() - Check if mmap'ing an address is allowed
2674  * @addr: address
2675  *
2676  * Check permissions for a mmap operation at @addr.
2677  *
2678  * Return: Returns 0 if permission is granted.
2679  */
2680 int security_mmap_addr(unsigned long addr)
2681 {
2682 	return call_int_hook(mmap_addr, 0, addr);
2683 }
2684 
2685 /**
2686  * security_file_mprotect() - Check if changing memory protections is allowed
2687  * @vma: memory region
2688  * @reqprot: application requested protection
2689  * @prog: protection applied by the kernel
2690  *
2691  * Check permissions before changing memory access permissions.
2692  *
2693  * Return: Returns 0 if permission is granted.
2694  */
2695 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2696 			   unsigned long prot)
2697 {
2698 	int ret;
2699 
2700 	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
2701 	if (ret)
2702 		return ret;
2703 	return ima_file_mprotect(vma, prot);
2704 }
2705 
2706 /**
2707  * security_file_lock() - Check if a file lock is allowed
2708  * @file: file
2709  * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2710  *
2711  * Check permission before performing file locking operations.  Note the hook
2712  * mediates both flock and fcntl style locks.
2713  *
2714  * Return: Returns 0 if permission is granted.
2715  */
2716 int security_file_lock(struct file *file, unsigned int cmd)
2717 {
2718 	return call_int_hook(file_lock, 0, file, cmd);
2719 }
2720 
2721 /**
2722  * security_file_fcntl() - Check if fcntl() op is allowed
2723  * @file: file
2724  * @cmd: fnctl command
2725  * @arg: command argument
2726  *
2727  * Check permission before allowing the file operation specified by @cmd from
2728  * being performed on the file @file.  Note that @arg sometimes represents a
2729  * user space pointer; in other cases, it may be a simple integer value.  When
2730  * @arg represents a user space pointer, it should never be used by the
2731  * security module.
2732  *
2733  * Return: Returns 0 if permission is granted.
2734  */
2735 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2736 {
2737 	return call_int_hook(file_fcntl, 0, file, cmd, arg);
2738 }
2739 
2740 /**
2741  * security_file_set_fowner() - Set the file owner info in the LSM blob
2742  * @file: the file
2743  *
2744  * Save owner security information (typically from current->security) in
2745  * file->f_security for later use by the send_sigiotask hook.
2746  *
2747  * Return: Returns 0 on success.
2748  */
2749 void security_file_set_fowner(struct file *file)
2750 {
2751 	call_void_hook(file_set_fowner, file);
2752 }
2753 
2754 /**
2755  * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2756  * @tsk: target task
2757  * @fown: signal sender
2758  * @sig: signal to be sent, SIGIO is sent if 0
2759  *
2760  * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2761  * process @tsk.  Note that this hook is sometimes called from interrupt.  Note
2762  * that the fown_struct, @fown, is never outside the context of a struct file,
2763  * so the file structure (and associated security information) can always be
2764  * obtained: container_of(fown, struct file, f_owner).
2765  *
2766  * Return: Returns 0 if permission is granted.
2767  */
2768 int security_file_send_sigiotask(struct task_struct *tsk,
2769 				 struct fown_struct *fown, int sig)
2770 {
2771 	return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
2772 }
2773 
2774 /**
2775  * security_file_receive() - Check is receiving a file via IPC is allowed
2776  * @file: file being received
2777  *
2778  * This hook allows security modules to control the ability of a process to
2779  * receive an open file descriptor via socket IPC.
2780  *
2781  * Return: Returns 0 if permission is granted.
2782  */
2783 int security_file_receive(struct file *file)
2784 {
2785 	return call_int_hook(file_receive, 0, file);
2786 }
2787 
2788 /**
2789  * security_file_open() - Save open() time state for late use by the LSM
2790  * @file:
2791  *
2792  * Save open-time permission checking state for later use upon file_permission,
2793  * and recheck access if anything has changed since inode_permission.
2794  *
2795  * Return: Returns 0 if permission is granted.
2796  */
2797 int security_file_open(struct file *file)
2798 {
2799 	int ret;
2800 
2801 	ret = call_int_hook(file_open, 0, file);
2802 	if (ret)
2803 		return ret;
2804 
2805 	return fsnotify_perm(file, MAY_OPEN);
2806 }
2807 
2808 /**
2809  * security_file_truncate() - Check if truncating a file is allowed
2810  * @file: file
2811  *
2812  * Check permission before truncating a file, i.e. using ftruncate.  Note that
2813  * truncation permission may also be checked based on the path, using the
2814  * @path_truncate hook.
2815  *
2816  * Return: Returns 0 if permission is granted.
2817  */
2818 int security_file_truncate(struct file *file)
2819 {
2820 	return call_int_hook(file_truncate, 0, file);
2821 }
2822 
2823 /**
2824  * security_task_alloc() - Allocate a task's LSM blob
2825  * @task: the task
2826  * @clone_flags: flags indicating what is being shared
2827  *
2828  * Handle allocation of task-related resources.
2829  *
2830  * Return: Returns a zero on success, negative values on failure.
2831  */
2832 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2833 {
2834 	int rc = lsm_task_alloc(task);
2835 
2836 	if (rc)
2837 		return rc;
2838 	rc = call_int_hook(task_alloc, 0, task, clone_flags);
2839 	if (unlikely(rc))
2840 		security_task_free(task);
2841 	return rc;
2842 }
2843 
2844 /**
2845  * security_task_free() - Free a task's LSM blob and related resources
2846  * @task: task
2847  *
2848  * Handle release of task-related resources.  Note that this can be called from
2849  * interrupt context.
2850  */
2851 void security_task_free(struct task_struct *task)
2852 {
2853 	call_void_hook(task_free, task);
2854 
2855 	kfree(task->security);
2856 	task->security = NULL;
2857 }
2858 
2859 /**
2860  * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
2861  * @cred: credentials
2862  * @gfp: gfp flags
2863  *
2864  * Only allocate sufficient memory and attach to @cred such that
2865  * cred_transfer() will not get ENOMEM.
2866  *
2867  * Return: Returns 0 on success, negative values on failure.
2868  */
2869 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2870 {
2871 	int rc = lsm_cred_alloc(cred, gfp);
2872 
2873 	if (rc)
2874 		return rc;
2875 
2876 	rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
2877 	if (unlikely(rc))
2878 		security_cred_free(cred);
2879 	return rc;
2880 }
2881 
2882 /**
2883  * security_cred_free() - Free the cred's LSM blob and associated resources
2884  * @cred: credentials
2885  *
2886  * Deallocate and clear the cred->security field in a set of credentials.
2887  */
2888 void security_cred_free(struct cred *cred)
2889 {
2890 	/*
2891 	 * There is a failure case in prepare_creds() that
2892 	 * may result in a call here with ->security being NULL.
2893 	 */
2894 	if (unlikely(cred->security == NULL))
2895 		return;
2896 
2897 	call_void_hook(cred_free, cred);
2898 
2899 	kfree(cred->security);
2900 	cred->security = NULL;
2901 }
2902 
2903 /**
2904  * security_prepare_creds() - Prepare a new set of credentials
2905  * @new: new credentials
2906  * @old: original credentials
2907  * @gfp: gfp flags
2908  *
2909  * Prepare a new set of credentials by copying the data from the old set.
2910  *
2911  * Return: Returns 0 on success, negative values on failure.
2912  */
2913 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
2914 {
2915 	int rc = lsm_cred_alloc(new, gfp);
2916 
2917 	if (rc)
2918 		return rc;
2919 
2920 	rc = call_int_hook(cred_prepare, 0, new, old, gfp);
2921 	if (unlikely(rc))
2922 		security_cred_free(new);
2923 	return rc;
2924 }
2925 
2926 /**
2927  * security_transfer_creds() - Transfer creds
2928  * @new: target credentials
2929  * @old: original credentials
2930  *
2931  * Transfer data from original creds to new creds.
2932  */
2933 void security_transfer_creds(struct cred *new, const struct cred *old)
2934 {
2935 	call_void_hook(cred_transfer, new, old);
2936 }
2937 
2938 /**
2939  * security_cred_getsecid() - Get the secid from a set of credentials
2940  * @c: credentials
2941  * @secid: secid value
2942  *
2943  * Retrieve the security identifier of the cred structure @c.  In case of
2944  * failure, @secid will be set to zero.
2945  */
2946 void security_cred_getsecid(const struct cred *c, u32 *secid)
2947 {
2948 	*secid = 0;
2949 	call_void_hook(cred_getsecid, c, secid);
2950 }
2951 EXPORT_SYMBOL(security_cred_getsecid);
2952 
2953 /**
2954  * security_kernel_act_as() - Set the kernel credentials to act as secid
2955  * @new: credentials
2956  * @secid: secid
2957  *
2958  * Set the credentials for a kernel service to act as (subjective context).
2959  * The current task must be the one that nominated @secid.
2960  *
2961  * Return: Returns 0 if successful.
2962  */
2963 int security_kernel_act_as(struct cred *new, u32 secid)
2964 {
2965 	return call_int_hook(kernel_act_as, 0, new, secid);
2966 }
2967 
2968 /**
2969  * security_kernel_create_files_as() - Set file creation context using an inode
2970  * @new: target credentials
2971  * @inode: reference inode
2972  *
2973  * Set the file creation context in a set of credentials to be the same as the
2974  * objective context of the specified inode.  The current task must be the one
2975  * that nominated @inode.
2976  *
2977  * Return: Returns 0 if successful.
2978  */
2979 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
2980 {
2981 	return call_int_hook(kernel_create_files_as, 0, new, inode);
2982 }
2983 
2984 /**
2985  * security_kernel_module_request() - Check is loading a module is allowed
2986  * @kmod_name: module name
2987  *
2988  * Ability to trigger the kernel to automatically upcall to userspace for
2989  * userspace to load a kernel module with the given name.
2990  *
2991  * Return: Returns 0 if successful.
2992  */
2993 int security_kernel_module_request(char *kmod_name)
2994 {
2995 	int ret;
2996 
2997 	ret = call_int_hook(kernel_module_request, 0, kmod_name);
2998 	if (ret)
2999 		return ret;
3000 	return integrity_kernel_module_request(kmod_name);
3001 }
3002 
3003 /**
3004  * security_kernel_read_file() - Read a file specified by userspace
3005  * @file: file
3006  * @id: file identifier
3007  * @contents: trust if security_kernel_post_read_file() will be called
3008  *
3009  * Read a file specified by userspace.
3010  *
3011  * Return: Returns 0 if permission is granted.
3012  */
3013 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
3014 			      bool contents)
3015 {
3016 	int ret;
3017 
3018 	ret = call_int_hook(kernel_read_file, 0, file, id, contents);
3019 	if (ret)
3020 		return ret;
3021 	return ima_read_file(file, id, contents);
3022 }
3023 EXPORT_SYMBOL_GPL(security_kernel_read_file);
3024 
3025 /**
3026  * security_kernel_post_read_file() - Read a file specified by userspace
3027  * @file: file
3028  * @buf: file contents
3029  * @size: size of file contents
3030  * @id: file identifier
3031  *
3032  * Read a file specified by userspace.  This must be paired with a prior call
3033  * to security_kernel_read_file() call that indicated this hook would also be
3034  * called, see security_kernel_read_file() for more information.
3035  *
3036  * Return: Returns 0 if permission is granted.
3037  */
3038 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3039 				   enum kernel_read_file_id id)
3040 {
3041 	int ret;
3042 
3043 	ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
3044 	if (ret)
3045 		return ret;
3046 	return ima_post_read_file(file, buf, size, id);
3047 }
3048 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3049 
3050 /**
3051  * security_kernel_load_data() - Load data provided by userspace
3052  * @id: data identifier
3053  * @contents: true if security_kernel_post_load_data() will be called
3054  *
3055  * Load data provided by userspace.
3056  *
3057  * Return: Returns 0 if permission is granted.
3058  */
3059 int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
3060 {
3061 	int ret;
3062 
3063 	ret = call_int_hook(kernel_load_data, 0, id, contents);
3064 	if (ret)
3065 		return ret;
3066 	return ima_load_data(id, contents);
3067 }
3068 EXPORT_SYMBOL_GPL(security_kernel_load_data);
3069 
3070 /**
3071  * security_kernel_post_load_data() - Load userspace data from a non-file source
3072  * @buf: data
3073  * @size: size of data
3074  * @id: data identifier
3075  * @description: text description of data, specific to the id value
3076  *
3077  * Load data provided by a non-file source (usually userspace buffer).  This
3078  * must be paired with a prior security_kernel_load_data() call that indicated
3079  * this hook would also be called, see security_kernel_load_data() for more
3080  * information.
3081  *
3082  * Return: Returns 0 if permission is granted.
3083  */
3084 int security_kernel_post_load_data(char *buf, loff_t size,
3085 				   enum kernel_load_data_id id,
3086 				   char *description)
3087 {
3088 	int ret;
3089 
3090 	ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
3091 			    description);
3092 	if (ret)
3093 		return ret;
3094 	return ima_post_load_data(buf, size, id, description);
3095 }
3096 EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3097 
3098 /**
3099  * security_task_fix_setuid() - Update LSM with new user id attributes
3100  * @new: updated credentials
3101  * @old: credentials being replaced
3102  * @flags: LSM_SETID_* flag values
3103  *
3104  * Update the module's state after setting one or more of the user identity
3105  * attributes of the current process.  The @flags parameter indicates which of
3106  * the set*uid system calls invoked this hook.  If @new is the set of
3107  * credentials that will be installed.  Modifications should be made to this
3108  * rather than to @current->cred.
3109  *
3110  * Return: Returns 0 on success.
3111  */
3112 int security_task_fix_setuid(struct cred *new, const struct cred *old,
3113 			     int flags)
3114 {
3115 	return call_int_hook(task_fix_setuid, 0, new, old, flags);
3116 }
3117 
3118 /**
3119  * security_task_fix_setgid() - Update LSM with new group id attributes
3120  * @new: updated credentials
3121  * @old: credentials being replaced
3122  * @flags: LSM_SETID_* flag value
3123  *
3124  * Update the module's state after setting one or more of the group identity
3125  * attributes of the current process.  The @flags parameter indicates which of
3126  * the set*gid system calls invoked this hook.  @new is the set of credentials
3127  * that will be installed.  Modifications should be made to this rather than to
3128  * @current->cred.
3129  *
3130  * Return: Returns 0 on success.
3131  */
3132 int security_task_fix_setgid(struct cred *new, const struct cred *old,
3133 			     int flags)
3134 {
3135 	return call_int_hook(task_fix_setgid, 0, new, old, flags);
3136 }
3137 
3138 /**
3139  * security_task_fix_setgroups() - Update LSM with new supplementary groups
3140  * @new: updated credentials
3141  * @old: credentials being replaced
3142  *
3143  * Update the module's state after setting the supplementary group identity
3144  * attributes of the current process.  @new is the set of credentials that will
3145  * be installed.  Modifications should be made to this rather than to
3146  * @current->cred.
3147  *
3148  * Return: Returns 0 on success.
3149  */
3150 int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3151 {
3152 	return call_int_hook(task_fix_setgroups, 0, new, old);
3153 }
3154 
3155 /**
3156  * security_task_setpgid() - Check if setting the pgid is allowed
3157  * @p: task being modified
3158  * @pgid: new pgid
3159  *
3160  * Check permission before setting the process group identifier of the process
3161  * @p to @pgid.
3162  *
3163  * Return: Returns 0 if permission is granted.
3164  */
3165 int security_task_setpgid(struct task_struct *p, pid_t pgid)
3166 {
3167 	return call_int_hook(task_setpgid, 0, p, pgid);
3168 }
3169 
3170 /**
3171  * security_task_getpgid() - Check if getting the pgid is allowed
3172  * @p: task
3173  *
3174  * Check permission before getting the process group identifier of the process
3175  * @p.
3176  *
3177  * Return: Returns 0 if permission is granted.
3178  */
3179 int security_task_getpgid(struct task_struct *p)
3180 {
3181 	return call_int_hook(task_getpgid, 0, p);
3182 }
3183 
3184 /**
3185  * security_task_getsid() - Check if getting the session id is allowed
3186  * @p: task
3187  *
3188  * Check permission before getting the session identifier of the process @p.
3189  *
3190  * Return: Returns 0 if permission is granted.
3191  */
3192 int security_task_getsid(struct task_struct *p)
3193 {
3194 	return call_int_hook(task_getsid, 0, p);
3195 }
3196 
3197 /**
3198  * security_current_getsecid_subj() - Get the current task's subjective secid
3199  * @secid: secid value
3200  *
3201  * Retrieve the subjective security identifier of the current task and return
3202  * it in @secid.  In case of failure, @secid will be set to zero.
3203  */
3204 void security_current_getsecid_subj(u32 *secid)
3205 {
3206 	*secid = 0;
3207 	call_void_hook(current_getsecid_subj, secid);
3208 }
3209 EXPORT_SYMBOL(security_current_getsecid_subj);
3210 
3211 /**
3212  * security_task_getsecid_obj() - Get a task's objective secid
3213  * @p: target task
3214  * @secid: secid value
3215  *
3216  * Retrieve the objective security identifier of the task_struct in @p and
3217  * return it in @secid. In case of failure, @secid will be set to zero.
3218  */
3219 void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3220 {
3221 	*secid = 0;
3222 	call_void_hook(task_getsecid_obj, p, secid);
3223 }
3224 EXPORT_SYMBOL(security_task_getsecid_obj);
3225 
3226 /**
3227  * security_task_setnice() - Check if setting a task's nice value is allowed
3228  * @p: target task
3229  * @nice: nice value
3230  *
3231  * Check permission before setting the nice value of @p to @nice.
3232  *
3233  * Return: Returns 0 if permission is granted.
3234  */
3235 int security_task_setnice(struct task_struct *p, int nice)
3236 {
3237 	return call_int_hook(task_setnice, 0, p, nice);
3238 }
3239 
3240 /**
3241  * security_task_setioprio() - Check if setting a task's ioprio is allowed
3242  * @p: target task
3243  * @ioprio: ioprio value
3244  *
3245  * Check permission before setting the ioprio value of @p to @ioprio.
3246  *
3247  * Return: Returns 0 if permission is granted.
3248  */
3249 int security_task_setioprio(struct task_struct *p, int ioprio)
3250 {
3251 	return call_int_hook(task_setioprio, 0, p, ioprio);
3252 }
3253 
3254 /**
3255  * security_task_getioprio() - Check if getting a task's ioprio is allowed
3256  * @p: task
3257  *
3258  * Check permission before getting the ioprio value of @p.
3259  *
3260  * Return: Returns 0 if permission is granted.
3261  */
3262 int security_task_getioprio(struct task_struct *p)
3263 {
3264 	return call_int_hook(task_getioprio, 0, p);
3265 }
3266 
3267 /**
3268  * security_task_prlimit() - Check if get/setting resources limits is allowed
3269  * @cred: current task credentials
3270  * @tcred: target task credentials
3271  * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3272  *
3273  * Check permission before getting and/or setting the resource limits of
3274  * another task.
3275  *
3276  * Return: Returns 0 if permission is granted.
3277  */
3278 int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3279 			  unsigned int flags)
3280 {
3281 	return call_int_hook(task_prlimit, 0, cred, tcred, flags);
3282 }
3283 
3284 /**
3285  * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3286  * @p: target task's group leader
3287  * @resource: resource whose limit is being set
3288  * @new_rlim: new resource limit
3289  *
3290  * Check permission before setting the resource limits of process @p for
3291  * @resource to @new_rlim.  The old resource limit values can be examined by
3292  * dereferencing (p->signal->rlim + resource).
3293  *
3294  * Return: Returns 0 if permission is granted.
3295  */
3296 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3297 			    struct rlimit *new_rlim)
3298 {
3299 	return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
3300 }
3301 
3302 /**
3303  * security_task_setscheduler() - Check if setting sched policy/param is allowed
3304  * @p: target task
3305  *
3306  * Check permission before setting scheduling policy and/or parameters of
3307  * process @p.
3308  *
3309  * Return: Returns 0 if permission is granted.
3310  */
3311 int security_task_setscheduler(struct task_struct *p)
3312 {
3313 	return call_int_hook(task_setscheduler, 0, p);
3314 }
3315 
3316 /**
3317  * security_task_getscheduler() - Check if getting scheduling info is allowed
3318  * @p: target task
3319  *
3320  * Check permission before obtaining scheduling information for process @p.
3321  *
3322  * Return: Returns 0 if permission is granted.
3323  */
3324 int security_task_getscheduler(struct task_struct *p)
3325 {
3326 	return call_int_hook(task_getscheduler, 0, p);
3327 }
3328 
3329 /**
3330  * security_task_movememory() - Check if moving memory is allowed
3331  * @p: task
3332  *
3333  * Check permission before moving memory owned by process @p.
3334  *
3335  * Return: Returns 0 if permission is granted.
3336  */
3337 int security_task_movememory(struct task_struct *p)
3338 {
3339 	return call_int_hook(task_movememory, 0, p);
3340 }
3341 
3342 /**
3343  * security_task_kill() - Check if sending a signal is allowed
3344  * @p: target process
3345  * @info: signal information
3346  * @sig: signal value
3347  * @cred: credentials of the signal sender, NULL if @current
3348  *
3349  * Check permission before sending signal @sig to @p.  @info can be NULL, the
3350  * constant 1, or a pointer to a kernel_siginfo structure.  If @info is 1 or
3351  * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3352  * the kernel and should typically be permitted.  SIGIO signals are handled
3353  * separately by the send_sigiotask hook in file_security_ops.
3354  *
3355  * Return: Returns 0 if permission is granted.
3356  */
3357 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3358 		       int sig, const struct cred *cred)
3359 {
3360 	return call_int_hook(task_kill, 0, p, info, sig, cred);
3361 }
3362 
3363 /**
3364  * security_task_prctl() - Check if a prctl op is allowed
3365  * @option: operation
3366  * @arg2: argument
3367  * @arg3: argument
3368  * @arg4: argument
3369  * @arg5: argument
3370  *
3371  * Check permission before performing a process control operation on the
3372  * current process.
3373  *
3374  * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3375  *         to cause prctl() to return immediately with that value.
3376  */
3377 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3378 			unsigned long arg4, unsigned long arg5)
3379 {
3380 	int thisrc;
3381 	int rc = LSM_RET_DEFAULT(task_prctl);
3382 	struct security_hook_list *hp;
3383 
3384 	hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
3385 		thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
3386 		if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3387 			rc = thisrc;
3388 			if (thisrc != 0)
3389 				break;
3390 		}
3391 	}
3392 	return rc;
3393 }
3394 
3395 /**
3396  * security_task_to_inode() - Set the security attributes of a task's inode
3397  * @p: task
3398  * @inode: inode
3399  *
3400  * Set the security attributes for an inode based on an associated task's
3401  * security attributes, e.g. for /proc/pid inodes.
3402  */
3403 void security_task_to_inode(struct task_struct *p, struct inode *inode)
3404 {
3405 	call_void_hook(task_to_inode, p, inode);
3406 }
3407 
3408 /**
3409  * security_create_user_ns() - Check if creating a new userns is allowed
3410  * @cred: prepared creds
3411  *
3412  * Check permission prior to creating a new user namespace.
3413  *
3414  * Return: Returns 0 if successful, otherwise < 0 error code.
3415  */
3416 int security_create_user_ns(const struct cred *cred)
3417 {
3418 	return call_int_hook(userns_create, 0, cred);
3419 }
3420 
3421 /**
3422  * security_ipc_permission() - Check if sysv ipc access is allowed
3423  * @ipcp: ipc permission structure
3424  * @flags: requested permissions
3425  *
3426  * Check permissions for access to IPC.
3427  *
3428  * Return: Returns 0 if permission is granted.
3429  */
3430 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3431 {
3432 	return call_int_hook(ipc_permission, 0, ipcp, flag);
3433 }
3434 
3435 /**
3436  * security_ipc_getsecid() - Get the sysv ipc object's secid
3437  * @ipcp: ipc permission structure
3438  * @secid: secid pointer
3439  *
3440  * Get the secid associated with the ipc object.  In case of failure, @secid
3441  * will be set to zero.
3442  */
3443 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3444 {
3445 	*secid = 0;
3446 	call_void_hook(ipc_getsecid, ipcp, secid);
3447 }
3448 
3449 /**
3450  * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3451  * @msg: message structure
3452  *
3453  * Allocate and attach a security structure to the msg->security field.  The
3454  * security field is initialized to NULL when the structure is first created.
3455  *
3456  * Return: Return 0 if operation was successful and permission is granted.
3457  */
3458 int security_msg_msg_alloc(struct msg_msg *msg)
3459 {
3460 	int rc = lsm_msg_msg_alloc(msg);
3461 
3462 	if (unlikely(rc))
3463 		return rc;
3464 	rc = call_int_hook(msg_msg_alloc_security, 0, msg);
3465 	if (unlikely(rc))
3466 		security_msg_msg_free(msg);
3467 	return rc;
3468 }
3469 
3470 /**
3471  * security_msg_msg_free() - Free a sysv ipc message LSM blob
3472  * @msg: message structure
3473  *
3474  * Deallocate the security structure for this message.
3475  */
3476 void security_msg_msg_free(struct msg_msg *msg)
3477 {
3478 	call_void_hook(msg_msg_free_security, msg);
3479 	kfree(msg->security);
3480 	msg->security = NULL;
3481 }
3482 
3483 /**
3484  * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3485  * @msq: sysv ipc permission structure
3486  *
3487  * Allocate and attach a security structure to @msg. The security field is
3488  * initialized to NULL when the structure is first created.
3489  *
3490  * Return: Returns 0 if operation was successful and permission is granted.
3491  */
3492 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
3493 {
3494 	int rc = lsm_ipc_alloc(msq);
3495 
3496 	if (unlikely(rc))
3497 		return rc;
3498 	rc = call_int_hook(msg_queue_alloc_security, 0, msq);
3499 	if (unlikely(rc))
3500 		security_msg_queue_free(msq);
3501 	return rc;
3502 }
3503 
3504 /**
3505  * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3506  * @msq: sysv ipc permission structure
3507  *
3508  * Deallocate security field @perm->security for the message queue.
3509  */
3510 void security_msg_queue_free(struct kern_ipc_perm *msq)
3511 {
3512 	call_void_hook(msg_queue_free_security, msq);
3513 	kfree(msq->security);
3514 	msq->security = NULL;
3515 }
3516 
3517 /**
3518  * security_msg_queue_associate() - Check if a msg queue operation is allowed
3519  * @msq: sysv ipc permission structure
3520  * @msqflg: operation flags
3521  *
3522  * Check permission when a message queue is requested through the msgget system
3523  * call. This hook is only called when returning the message queue identifier
3524  * for an existing message queue, not when a new message queue is created.
3525  *
3526  * Return: Return 0 if permission is granted.
3527  */
3528 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
3529 {
3530 	return call_int_hook(msg_queue_associate, 0, msq, msqflg);
3531 }
3532 
3533 /**
3534  * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3535  * @msq: sysv ipc permission structure
3536  * @cmd: operation
3537  *
3538  * Check permission when a message control operation specified by @cmd is to be
3539  * performed on the message queue with permissions.
3540  *
3541  * Return: Returns 0 if permission is granted.
3542  */
3543 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
3544 {
3545 	return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
3546 }
3547 
3548 /**
3549  * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3550  * @msq: sysv ipc permission structure
3551  * @msg: message
3552  * @msqflg: operation flags
3553  *
3554  * Check permission before a message, @msg, is enqueued on the message queue
3555  * with permissions specified in @msq.
3556  *
3557  * Return: Returns 0 if permission is granted.
3558  */
3559 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
3560 			      struct msg_msg *msg, int msqflg)
3561 {
3562 	return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
3563 }
3564 
3565 /**
3566  * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3567  * @msq: sysv ipc permission structure
3568  * @msg: message
3569  * @target: target task
3570  * @type: type of message requested
3571  * @mode: operation flags
3572  *
3573  * Check permission before a message, @msg, is removed from the message	queue.
3574  * The @target task structure contains a pointer to the process that will be
3575  * receiving the message (not equal to the current process when inline receives
3576  * are being performed).
3577  *
3578  * Return: Returns 0 if permission is granted.
3579  */
3580 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
3581 			      struct task_struct *target, long type, int mode)
3582 {
3583 	return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
3584 }
3585 
3586 /**
3587  * security_shm_alloc() - Allocate a sysv shm LSM blob
3588  * @shp: sysv ipc permission structure
3589  *
3590  * Allocate and attach a security structure to the @shp security field.  The
3591  * security field is initialized to NULL when the structure is first created.
3592  *
3593  * Return: Returns 0 if operation was successful and permission is granted.
3594  */
3595 int security_shm_alloc(struct kern_ipc_perm *shp)
3596 {
3597 	int rc = lsm_ipc_alloc(shp);
3598 
3599 	if (unlikely(rc))
3600 		return rc;
3601 	rc = call_int_hook(shm_alloc_security, 0, shp);
3602 	if (unlikely(rc))
3603 		security_shm_free(shp);
3604 	return rc;
3605 }
3606 
3607 /**
3608  * security_shm_free() - Free a sysv shm LSM blob
3609  * @shp: sysv ipc permission structure
3610  *
3611  * Deallocate the security structure @perm->security for the memory segment.
3612  */
3613 void security_shm_free(struct kern_ipc_perm *shp)
3614 {
3615 	call_void_hook(shm_free_security, shp);
3616 	kfree(shp->security);
3617 	shp->security = NULL;
3618 }
3619 
3620 /**
3621  * security_shm_associate() - Check if a sysv shm operation is allowed
3622  * @shp: sysv ipc permission structure
3623  * @shmflg: operation flags
3624  *
3625  * Check permission when a shared memory region is requested through the shmget
3626  * system call. This hook is only called when returning the shared memory
3627  * region identifier for an existing region, not when a new shared memory
3628  * region is created.
3629  *
3630  * Return: Returns 0 if permission is granted.
3631  */
3632 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
3633 {
3634 	return call_int_hook(shm_associate, 0, shp, shmflg);
3635 }
3636 
3637 /**
3638  * security_shm_shmctl() - Check if a sysv shm operation is allowed
3639  * @shp: sysv ipc permission structure
3640  * @cmd: operation
3641  *
3642  * Check permission when a shared memory control operation specified by @cmd is
3643  * to be performed on the shared memory region with permissions in @shp.
3644  *
3645  * Return: Return 0 if permission is granted.
3646  */
3647 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
3648 {
3649 	return call_int_hook(shm_shmctl, 0, shp, cmd);
3650 }
3651 
3652 /**
3653  * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3654  * @shp: sysv ipc permission structure
3655  * @shmaddr: address of memory region to attach
3656  * @shmflg: operation flags
3657  *
3658  * Check permissions prior to allowing the shmat system call to attach the
3659  * shared memory segment with permissions @shp to the data segment of the
3660  * calling process. The attaching address is specified by @shmaddr.
3661  *
3662  * Return: Returns 0 if permission is granted.
3663  */
3664 int security_shm_shmat(struct kern_ipc_perm *shp,
3665 		       char __user *shmaddr, int shmflg)
3666 {
3667 	return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
3668 }
3669 
3670 /**
3671  * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3672  * @sma: sysv ipc permission structure
3673  *
3674  * Allocate and attach a security structure to the @sma security field. The
3675  * security field is initialized to NULL when the structure is first created.
3676  *
3677  * Return: Returns 0 if operation was successful and permission is granted.
3678  */
3679 int security_sem_alloc(struct kern_ipc_perm *sma)
3680 {
3681 	int rc = lsm_ipc_alloc(sma);
3682 
3683 	if (unlikely(rc))
3684 		return rc;
3685 	rc = call_int_hook(sem_alloc_security, 0, sma);
3686 	if (unlikely(rc))
3687 		security_sem_free(sma);
3688 	return rc;
3689 }
3690 
3691 /**
3692  * security_sem_free() - Free a sysv semaphore LSM blob
3693  * @sma: sysv ipc permission structure
3694  *
3695  * Deallocate security structure @sma->security for the semaphore.
3696  */
3697 void security_sem_free(struct kern_ipc_perm *sma)
3698 {
3699 	call_void_hook(sem_free_security, sma);
3700 	kfree(sma->security);
3701 	sma->security = NULL;
3702 }
3703 
3704 /**
3705  * security_sem_associate() - Check if a sysv semaphore operation is allowed
3706  * @sma: sysv ipc permission structure
3707  * @semflg: operation flags
3708  *
3709  * Check permission when a semaphore is requested through the semget system
3710  * call. This hook is only called when returning the semaphore identifier for
3711  * an existing semaphore, not when a new one must be created.
3712  *
3713  * Return: Returns 0 if permission is granted.
3714  */
3715 int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
3716 {
3717 	return call_int_hook(sem_associate, 0, sma, semflg);
3718 }
3719 
3720 /**
3721  * security_sem_ctl() - Check if a sysv semaphore operation is allowed
3722  * @sma: sysv ipc permission structure
3723  * @cmd: operation
3724  *
3725  * Check permission when a semaphore operation specified by @cmd is to be
3726  * performed on the semaphore.
3727  *
3728  * Return: Returns 0 if permission is granted.
3729  */
3730 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
3731 {
3732 	return call_int_hook(sem_semctl, 0, sma, cmd);
3733 }
3734 
3735 /**
3736  * security_sem_semop() - Check if a sysv semaphore operation is allowed
3737  * @sma: sysv ipc permission structure
3738  * @sops: operations to perform
3739  * @nsops: number of operations
3740  * @alter: flag indicating changes will be made
3741  *
3742  * Check permissions before performing operations on members of the semaphore
3743  * set. If the @alter flag is nonzero, the semaphore set may be modified.
3744  *
3745  * Return: Returns 0 if permission is granted.
3746  */
3747 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
3748 		       unsigned nsops, int alter)
3749 {
3750 	return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
3751 }
3752 
3753 /**
3754  * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3755  * @dentry: dentry
3756  * @inode: inode
3757  *
3758  * Fill in @inode security information for a @dentry if allowed.
3759  */
3760 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3761 {
3762 	if (unlikely(inode && IS_PRIVATE(inode)))
3763 		return;
3764 	call_void_hook(d_instantiate, dentry, inode);
3765 }
3766 EXPORT_SYMBOL(security_d_instantiate);
3767 
3768 /**
3769  * security_getprocattr() - Read an attribute for a task
3770  * @p: the task
3771  * @lsm: LSM name
3772  * @name: attribute name
3773  * @value: attribute value
3774  *
3775  * Read attribute @name for task @p and store it into @value if allowed.
3776  *
3777  * Return: Returns the length of @value on success, a negative value otherwise.
3778  */
3779 int security_getprocattr(struct task_struct *p, const char *lsm,
3780 			 const char *name, char **value)
3781 {
3782 	struct security_hook_list *hp;
3783 
3784 	hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
3785 		if (lsm != NULL && strcmp(lsm, hp->lsm))
3786 			continue;
3787 		return hp->hook.getprocattr(p, name, value);
3788 	}
3789 	return LSM_RET_DEFAULT(getprocattr);
3790 }
3791 
3792 /**
3793  * security_setprocattr() - Set an attribute for a task
3794  * @lsm: LSM name
3795  * @name: attribute name
3796  * @value: attribute value
3797  * @size: attribute value size
3798  *
3799  * Write (set) the current task's attribute @name to @value, size @size if
3800  * allowed.
3801  *
3802  * Return: Returns bytes written on success, a negative value otherwise.
3803  */
3804 int security_setprocattr(const char *lsm, const char *name, void *value,
3805 			 size_t size)
3806 {
3807 	struct security_hook_list *hp;
3808 
3809 	hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
3810 		if (lsm != NULL && strcmp(lsm, hp->lsm))
3811 			continue;
3812 		return hp->hook.setprocattr(name, value, size);
3813 	}
3814 	return LSM_RET_DEFAULT(setprocattr);
3815 }
3816 
3817 /**
3818  * security_netlink_send() - Save info and check if netlink sending is allowed
3819  * @sk: sending socket
3820  * @skb: netlink message
3821  *
3822  * Save security information for a netlink message so that permission checking
3823  * can be performed when the message is processed.  The security information
3824  * can be saved using the eff_cap field of the netlink_skb_parms structure.
3825  * Also may be used to provide fine grained control over message transmission.
3826  *
3827  * Return: Returns 0 if the information was successfully saved and message is
3828  *         allowed to be transmitted.
3829  */
3830 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
3831 {
3832 	return call_int_hook(netlink_send, 0, sk, skb);
3833 }
3834 
3835 /**
3836  * security_ismaclabel() - Check is the named attribute is a MAC label
3837  * @name: full extended attribute name
3838  *
3839  * Check if the extended attribute specified by @name represents a MAC label.
3840  *
3841  * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
3842  */
3843 int security_ismaclabel(const char *name)
3844 {
3845 	return call_int_hook(ismaclabel, 0, name);
3846 }
3847 EXPORT_SYMBOL(security_ismaclabel);
3848 
3849 /**
3850  * security_secid_to_secctx() - Convert a secid to a secctx
3851  * @secid: secid
3852  * @secdata: secctx
3853  * @seclen: secctx length
3854  *
3855  * Convert secid to security context.  If @secdata is NULL the length of the
3856  * result will be returned in @seclen, but no @secdata will be returned.  This
3857  * does mean that the length could change between calls to check the length and
3858  * the next call which actually allocates and returns the @secdata.
3859  *
3860  * Return: Return 0 on success, error on failure.
3861  */
3862 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3863 {
3864 	struct security_hook_list *hp;
3865 	int rc;
3866 
3867 	/*
3868 	 * Currently, only one LSM can implement secid_to_secctx (i.e this
3869 	 * LSM hook is not "stackable").
3870 	 */
3871 	hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
3872 		rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
3873 		if (rc != LSM_RET_DEFAULT(secid_to_secctx))
3874 			return rc;
3875 	}
3876 
3877 	return LSM_RET_DEFAULT(secid_to_secctx);
3878 }
3879 EXPORT_SYMBOL(security_secid_to_secctx);
3880 
3881 /**
3882  * security_secctx_to_secid() - Convert a secctx to a secid
3883  * @secdata: secctx
3884  * @seclen: length of secctx
3885  * @secid: secid
3886  *
3887  * Convert security context to secid.
3888  *
3889  * Return: Returns 0 on success, error on failure.
3890  */
3891 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3892 {
3893 	*secid = 0;
3894 	return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
3895 }
3896 EXPORT_SYMBOL(security_secctx_to_secid);
3897 
3898 /**
3899  * security_release_secctx() - Free a secctx buffer
3900  * @secdata: secctx
3901  * @seclen: length of secctx
3902  *
3903  * Release the security context.
3904  */
3905 void security_release_secctx(char *secdata, u32 seclen)
3906 {
3907 	call_void_hook(release_secctx, secdata, seclen);
3908 }
3909 EXPORT_SYMBOL(security_release_secctx);
3910 
3911 /**
3912  * security_inode_invalidate_secctx() - Invalidate an inode's security label
3913  * @inode: inode
3914  *
3915  * Notify the security module that it must revalidate the security context of
3916  * an inode.
3917  */
3918 void security_inode_invalidate_secctx(struct inode *inode)
3919 {
3920 	call_void_hook(inode_invalidate_secctx, inode);
3921 }
3922 EXPORT_SYMBOL(security_inode_invalidate_secctx);
3923 
3924 /**
3925  * security_inode_notifysecctx() - Nofify the LSM of an inode's security label
3926  * @inode: inode
3927  * @ctx: secctx
3928  * @ctxlen: length of secctx
3929  *
3930  * Notify the security module of what the security context of an inode should
3931  * be.  Initializes the incore security context managed by the security module
3932  * for this inode.  Example usage: NFS client invokes this hook to initialize
3933  * the security context in its incore inode to the value provided by the server
3934  * for the file when the server returned the file's attributes to the client.
3935  * Must be called with inode->i_mutex locked.
3936  *
3937  * Return: Returns 0 on success, error on failure.
3938  */
3939 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3940 {
3941 	return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
3942 }
3943 EXPORT_SYMBOL(security_inode_notifysecctx);
3944 
3945 /**
3946  * security_inode_setsecctx() - Change the security label of an inode
3947  * @dentry: inode
3948  * @ctx: secctx
3949  * @ctxlen: length of secctx
3950  *
3951  * Change the security context of an inode.  Updates the incore security
3952  * context managed by the security module and invokes the fs code as needed
3953  * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
3954  * context.  Example usage: NFS server invokes this hook to change the security
3955  * context in its incore inode and on the backing filesystem to a value
3956  * provided by the client on a SETATTR operation.  Must be called with
3957  * inode->i_mutex locked.
3958  *
3959  * Return: Returns 0 on success, error on failure.
3960  */
3961 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3962 {
3963 	return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
3964 }
3965 EXPORT_SYMBOL(security_inode_setsecctx);
3966 
3967 /**
3968  * security_inode_getsecctx() - Get the security label of an inode
3969  * @inode: inode
3970  * @ctx: secctx
3971  * @ctxlen: length of secctx
3972  *
3973  * On success, returns 0 and fills out @ctx and @ctxlen with the security
3974  * context for the given @inode.
3975  *
3976  * Return: Returns 0 on success, error on failure.
3977  */
3978 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3979 {
3980 	return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
3981 }
3982 EXPORT_SYMBOL(security_inode_getsecctx);
3983 
3984 #ifdef CONFIG_WATCH_QUEUE
3985 /**
3986  * security_post_notification() - Check if a watch notification can be posted
3987  * @w_cred: credentials of the task that set the watch
3988  * @cred: credentials of the task which triggered the watch
3989  * @n: the notification
3990  *
3991  * Check to see if a watch notification can be posted to a particular queue.
3992  *
3993  * Return: Returns 0 if permission is granted.
3994  */
3995 int security_post_notification(const struct cred *w_cred,
3996 			       const struct cred *cred,
3997 			       struct watch_notification *n)
3998 {
3999 	return call_int_hook(post_notification, 0, w_cred, cred, n);
4000 }
4001 #endif /* CONFIG_WATCH_QUEUE */
4002 
4003 #ifdef CONFIG_KEY_NOTIFICATIONS
4004 /**
4005  * security_watch_key() - Check if a task is allowed to watch for key events
4006  * @key: the key to watch
4007  *
4008  * Check to see if a process is allowed to watch for event notifications from
4009  * a key or keyring.
4010  *
4011  * Return: Returns 0 if permission is granted.
4012  */
4013 int security_watch_key(struct key *key)
4014 {
4015 	return call_int_hook(watch_key, 0, key);
4016 }
4017 #endif /* CONFIG_KEY_NOTIFICATIONS */
4018 
4019 #ifdef CONFIG_SECURITY_NETWORK
4020 /**
4021  * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
4022  * @sock: originating sock
4023  * @other: peer sock
4024  * @newsk: new sock
4025  *
4026  * Check permissions before establishing a Unix domain stream connection
4027  * between @sock and @other.
4028  *
4029  * The @unix_stream_connect and @unix_may_send hooks were necessary because
4030  * Linux provides an alternative to the conventional file name space for Unix
4031  * domain sockets.  Whereas binding and connecting to sockets in the file name
4032  * space is mediated by the typical file permissions (and caught by the mknod
4033  * and permission hooks in inode_security_ops), binding and connecting to
4034  * sockets in the abstract name space is completely unmediated.  Sufficient
4035  * control of Unix domain sockets in the abstract name space isn't possible
4036  * using only the socket layer hooks, since we need to know the actual target
4037  * socket, which is not looked up until we are inside the af_unix code.
4038  *
4039  * Return: Returns 0 if permission is granted.
4040  */
4041 int security_unix_stream_connect(struct sock *sock, struct sock *other,
4042 				 struct sock *newsk)
4043 {
4044 	return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
4045 }
4046 EXPORT_SYMBOL(security_unix_stream_connect);
4047 
4048 /**
4049  * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
4050  * @sock: originating sock
4051  * @other: peer sock
4052  *
4053  * Check permissions before connecting or sending datagrams from @sock to
4054  * @other.
4055  *
4056  * The @unix_stream_connect and @unix_may_send hooks were necessary because
4057  * Linux provides an alternative to the conventional file name space for Unix
4058  * domain sockets.  Whereas binding and connecting to sockets in the file name
4059  * space is mediated by the typical file permissions (and caught by the mknod
4060  * and permission hooks in inode_security_ops), binding and connecting to
4061  * sockets in the abstract name space is completely unmediated.  Sufficient
4062  * control of Unix domain sockets in the abstract name space isn't possible
4063  * using only the socket layer hooks, since we need to know the actual target
4064  * socket, which is not looked up until we are inside the af_unix code.
4065  *
4066  * Return: Returns 0 if permission is granted.
4067  */
4068 int security_unix_may_send(struct socket *sock,  struct socket *other)
4069 {
4070 	return call_int_hook(unix_may_send, 0, sock, other);
4071 }
4072 EXPORT_SYMBOL(security_unix_may_send);
4073 
4074 /**
4075  * security_socket_create() - Check if creating a new socket is allowed
4076  * @family: protocol family
4077  * @type: communications type
4078  * @protocol: requested protocol
4079  * @kern: set to 1 if a kernel socket is requested
4080  *
4081  * Check permissions prior to creating a new socket.
4082  *
4083  * Return: Returns 0 if permission is granted.
4084  */
4085 int security_socket_create(int family, int type, int protocol, int kern)
4086 {
4087 	return call_int_hook(socket_create, 0, family, type, protocol, kern);
4088 }
4089 
4090 /**
4091  * security_socket_create() - Initialize a newly created socket
4092  * @sock: socket
4093  * @family: protocol family
4094  * @type: communications type
4095  * @protocol: requested protocol
4096  * @kern: set to 1 if a kernel socket is requested
4097  *
4098  * This hook allows a module to update or allocate a per-socket security
4099  * structure. Note that the security field was not added directly to the socket
4100  * structure, but rather, the socket security information is stored in the
4101  * associated inode.  Typically, the inode alloc_security hook will allocate
4102  * and attach security information to SOCK_INODE(sock)->i_security.  This hook
4103  * may be used to update the SOCK_INODE(sock)->i_security field with additional
4104  * information that wasn't available when the inode was allocated.
4105  *
4106  * Return: Returns 0 if permission is granted.
4107  */
4108 int security_socket_post_create(struct socket *sock, int family,
4109 				int type, int protocol, int kern)
4110 {
4111 	return call_int_hook(socket_post_create, 0, sock, family, type,
4112 			     protocol, kern);
4113 }
4114 
4115 /**
4116  * security_socket_socketpair() - Check if creating a socketpair is allowed
4117  * @socka: first socket
4118  * @sockb: second socket
4119  *
4120  * Check permissions before creating a fresh pair of sockets.
4121  *
4122  * Return: Returns 0 if permission is granted and the connection was
4123  *         established.
4124  */
4125 int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4126 {
4127 	return call_int_hook(socket_socketpair, 0, socka, sockb);
4128 }
4129 EXPORT_SYMBOL(security_socket_socketpair);
4130 
4131 /**
4132  * security_socket_bind() - Check if a socket bind operation is allowed
4133  * @sock: socket
4134  * @address: requested bind address
4135  * @addrlen: length of address
4136  *
4137  * Check permission before socket protocol layer bind operation is performed
4138  * and the socket @sock is bound to the address specified in the @address
4139  * parameter.
4140  *
4141  * Return: Returns 0 if permission is granted.
4142  */
4143 int security_socket_bind(struct socket *sock,
4144 			 struct sockaddr *address, int addrlen)
4145 {
4146 	return call_int_hook(socket_bind, 0, sock, address, addrlen);
4147 }
4148 
4149 /**
4150  * security_socket_connect() - Check if a socket connect operation is allowed
4151  * @sock: socket
4152  * @address: address of remote connection point
4153  * @addrlen: length of address
4154  *
4155  * Check permission before socket protocol layer connect operation attempts to
4156  * connect socket @sock to a remote address, @address.
4157  *
4158  * Return: Returns 0 if permission is granted.
4159  */
4160 int security_socket_connect(struct socket *sock,
4161 			    struct sockaddr *address, int addrlen)
4162 {
4163 	return call_int_hook(socket_connect, 0, sock, address, addrlen);
4164 }
4165 
4166 /**
4167  * security_socket_listen() - Check if a socket is allowed to listen
4168  * @sock: socket
4169  * @backlog: connection queue size
4170  *
4171  * Check permission before socket protocol layer listen operation.
4172  *
4173  * Return: Returns 0 if permission is granted.
4174  */
4175 int security_socket_listen(struct socket *sock, int backlog)
4176 {
4177 	return call_int_hook(socket_listen, 0, sock, backlog);
4178 }
4179 
4180 /**
4181  * security_socket_accept() - Check if a socket is allowed to accept connections
4182  * @sock: listening socket
4183  * @newsock: newly creation connection socket
4184  *
4185  * Check permission before accepting a new connection.  Note that the new
4186  * socket, @newsock, has been created and some information copied to it, but
4187  * the accept operation has not actually been performed.
4188  *
4189  * Return: Returns 0 if permission is granted.
4190  */
4191 int security_socket_accept(struct socket *sock, struct socket *newsock)
4192 {
4193 	return call_int_hook(socket_accept, 0, sock, newsock);
4194 }
4195 
4196 /**
4197  * security_socket_sendmsg() - Check is sending a message is allowed
4198  * @sock: sending socket
4199  * @msg: message to send
4200  * @size: size of message
4201  *
4202  * Check permission before transmitting a message to another socket.
4203  *
4204  * Return: Returns 0 if permission is granted.
4205  */
4206 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
4207 {
4208 	return call_int_hook(socket_sendmsg, 0, sock, msg, size);
4209 }
4210 
4211 /**
4212  * security_socket_recvmsg() - Check if receiving a message is allowed
4213  * @sock: receiving socket
4214  * @msg: message to receive
4215  * @size: size of message
4216  * @flags: operational flags
4217  *
4218  * Check permission before receiving a message from a socket.
4219  *
4220  * Return: Returns 0 if permission is granted.
4221  */
4222 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4223 			    int size, int flags)
4224 {
4225 	return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
4226 }
4227 
4228 /**
4229  * security_socket_getsockname() - Check if reading the socket addr is allowed
4230  * @sock: socket
4231  *
4232  * Check permission before reading the local address (name) of the socket
4233  * object.
4234  *
4235  * Return: Returns 0 if permission is granted.
4236  */
4237 int security_socket_getsockname(struct socket *sock)
4238 {
4239 	return call_int_hook(socket_getsockname, 0, sock);
4240 }
4241 
4242 /**
4243  * security_socket_getpeername() - Check if reading the peer's addr is allowed
4244  * @sock: socket
4245  *
4246  * Check permission before the remote address (name) of a socket object.
4247  *
4248  * Return: Returns 0 if permission is granted.
4249  */
4250 int security_socket_getpeername(struct socket *sock)
4251 {
4252 	return call_int_hook(socket_getpeername, 0, sock);
4253 }
4254 
4255 /**
4256  * security_socket_getsockopt() - Check if reading a socket option is allowed
4257  * @sock: socket
4258  * @level: option's protocol level
4259  * @optname: option name
4260  *
4261  * Check permissions before retrieving the options associated with socket
4262  * @sock.
4263  *
4264  * Return: Returns 0 if permission is granted.
4265  */
4266 int security_socket_getsockopt(struct socket *sock, int level, int optname)
4267 {
4268 	return call_int_hook(socket_getsockopt, 0, sock, level, optname);
4269 }
4270 
4271 /**
4272  * security_socket_setsockopt() - Check if setting a socket option is allowed
4273  * @sock: socket
4274  * @level: option's protocol level
4275  * @optname: option name
4276  *
4277  * Check permissions before setting the options associated with socket @sock.
4278  *
4279  * Return: Returns 0 if permission is granted.
4280  */
4281 int security_socket_setsockopt(struct socket *sock, int level, int optname)
4282 {
4283 	return call_int_hook(socket_setsockopt, 0, sock, level, optname);
4284 }
4285 
4286 /**
4287  * security_socket_shutdown() - Checks if shutting down the socket is allowed
4288  * @sock: socket
4289  * @how: flag indicating how sends and receives are handled
4290  *
4291  * Checks permission before all or part of a connection on the socket @sock is
4292  * shut down.
4293  *
4294  * Return: Returns 0 if permission is granted.
4295  */
4296 int security_socket_shutdown(struct socket *sock, int how)
4297 {
4298 	return call_int_hook(socket_shutdown, 0, sock, how);
4299 }
4300 
4301 /**
4302  * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4303  * @sk: destination sock
4304  * @skb: incoming packet
4305  *
4306  * Check permissions on incoming network packets.  This hook is distinct from
4307  * Netfilter's IP input hooks since it is the first time that the incoming
4308  * sk_buff @skb has been associated with a particular socket, @sk.  Must not
4309  * sleep inside this hook because some callers hold spinlocks.
4310  *
4311  * Return: Returns 0 if permission is granted.
4312  */
4313 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4314 {
4315 	return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
4316 }
4317 EXPORT_SYMBOL(security_sock_rcv_skb);
4318 
4319 /**
4320  * security_socket_getpeersec_stream() - Get the remote peer label
4321  * @sock: socket
4322  * @optval: destination buffer
4323  * @optlen: size of peer label copied into the buffer
4324  * @len: maximum size of the destination buffer
4325  *
4326  * This hook allows the security module to provide peer socket security state
4327  * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4328  * For tcp sockets this can be meaningful if the socket is associated with an
4329  * ipsec SA.
4330  *
4331  * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4332  *         values.
4333  */
4334 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4335 				      sockptr_t optlen, unsigned int len)
4336 {
4337 	return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
4338 			     optval, optlen, len);
4339 }
4340 
4341 /**
4342  * security_socket_getpeersec_dgram() - Get the remote peer label
4343  * @sock: socket
4344  * @skb: datagram packet
4345  * @secid: remote peer label secid
4346  *
4347  * This hook allows the security module to provide peer socket security state
4348  * for udp sockets on a per-packet basis to userspace via getsockopt
4349  * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4350  * option via getsockopt. It can then retrieve the security state returned by
4351  * this hook for a packet via the SCM_SECURITY ancillary message type.
4352  *
4353  * Return: Returns 0 on success, error on failure.
4354  */
4355 int security_socket_getpeersec_dgram(struct socket *sock,
4356 				     struct sk_buff *skb, u32 *secid)
4357 {
4358 	return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
4359 			     skb, secid);
4360 }
4361 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4362 
4363 /**
4364  * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4365  * @sk: sock
4366  * @family: protocol family
4367  * @priotity: gfp flags
4368  *
4369  * Allocate and attach a security structure to the sk->sk_security field, which
4370  * is used to copy security attributes between local stream sockets.
4371  *
4372  * Return: Returns 0 on success, error on failure.
4373  */
4374 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4375 {
4376 	return call_int_hook(sk_alloc_security, 0, sk, family, priority);
4377 }
4378 
4379 /**
4380  * security_sk_free() - Free the sock's LSM blob
4381  * @sk: sock
4382  *
4383  * Deallocate security structure.
4384  */
4385 void security_sk_free(struct sock *sk)
4386 {
4387 	call_void_hook(sk_free_security, sk);
4388 }
4389 
4390 /**
4391  * security_sk_clone() - Clone a sock's LSM state
4392  * @sk: original sock
4393  * @newsk: target sock
4394  *
4395  * Clone/copy security structure.
4396  */
4397 void security_sk_clone(const struct sock *sk, struct sock *newsk)
4398 {
4399 	call_void_hook(sk_clone_security, sk, newsk);
4400 }
4401 EXPORT_SYMBOL(security_sk_clone);
4402 
4403 void security_sk_classify_flow(struct sock *sk, struct flowi_common *flic)
4404 {
4405 	call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
4406 }
4407 EXPORT_SYMBOL(security_sk_classify_flow);
4408 
4409 /**
4410  * security_req_classify_flow() - Set a flow's secid based on request_sock
4411  * @req: request_sock
4412  * @flic: target flow
4413  *
4414  * Sets @flic's secid to @req's secid.
4415  */
4416 void security_req_classify_flow(const struct request_sock *req,
4417 				struct flowi_common *flic)
4418 {
4419 	call_void_hook(req_classify_flow, req, flic);
4420 }
4421 EXPORT_SYMBOL(security_req_classify_flow);
4422 
4423 /**
4424  * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4425  * @sk: sock being grafted
4426  * @sock: target socket
4427  *
4428  * Sets @sock's inode secid to @sk's secid and update @sk with any necessary
4429  * LSM state from @sock.
4430  */
4431 void security_sock_graft(struct sock *sk, struct socket *parent)
4432 {
4433 	call_void_hook(sock_graft, sk, parent);
4434 }
4435 EXPORT_SYMBOL(security_sock_graft);
4436 
4437 /**
4438  * security_inet_conn_request() - Set request_sock state using incoming connect
4439  * @sk: parent listening sock
4440  * @skb: incoming connection
4441  * @req: new request_sock
4442  *
4443  * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4444  *
4445  * Return: Returns 0 if permission is granted.
4446  */
4447 int security_inet_conn_request(const struct sock *sk,
4448 			       struct sk_buff *skb, struct request_sock *req)
4449 {
4450 	return call_int_hook(inet_conn_request, 0, sk, skb, req);
4451 }
4452 EXPORT_SYMBOL(security_inet_conn_request);
4453 
4454 /**
4455  * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4456  * @newsk: new sock
4457  * @req: connection request_sock
4458  *
4459  * Set that LSM state of @sock using the LSM state from @req.
4460  */
4461 void security_inet_csk_clone(struct sock *newsk,
4462 			     const struct request_sock *req)
4463 {
4464 	call_void_hook(inet_csk_clone, newsk, req);
4465 }
4466 
4467 /**
4468  * security_inet_conn_established() - Update sock's LSM state with connection
4469  * @sk: sock
4470  * @skb: connection packet
4471  *
4472  * Update @sock's LSM state to represent a new connection from @skb.
4473  */
4474 void security_inet_conn_established(struct sock *sk,
4475 				    struct sk_buff *skb)
4476 {
4477 	call_void_hook(inet_conn_established, sk, skb);
4478 }
4479 EXPORT_SYMBOL(security_inet_conn_established);
4480 
4481 /**
4482  * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4483  * @secid: new secmark value
4484  *
4485  * Check if the process should be allowed to relabel packets to @secid.
4486  *
4487  * Return: Returns 0 if permission is granted.
4488  */
4489 int security_secmark_relabel_packet(u32 secid)
4490 {
4491 	return call_int_hook(secmark_relabel_packet, 0, secid);
4492 }
4493 EXPORT_SYMBOL(security_secmark_relabel_packet);
4494 
4495 /**
4496  * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4497  *
4498  * Tells the LSM to increment the number of secmark labeling rules loaded.
4499  */
4500 void security_secmark_refcount_inc(void)
4501 {
4502 	call_void_hook(secmark_refcount_inc);
4503 }
4504 EXPORT_SYMBOL(security_secmark_refcount_inc);
4505 
4506 /**
4507  * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4508  *
4509  * Tells the LSM to decrement the number of secmark labeling rules loaded.
4510  */
4511 void security_secmark_refcount_dec(void)
4512 {
4513 	call_void_hook(secmark_refcount_dec);
4514 }
4515 EXPORT_SYMBOL(security_secmark_refcount_dec);
4516 
4517 /**
4518  * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4519  * @security: pointer to the LSM blob
4520  *
4521  * This hook allows a module to allocate a security structure for a TUN	device,
4522  * returning the pointer in @security.
4523  *
4524  * Return: Returns a zero on success, negative values on failure.
4525  */
4526 int security_tun_dev_alloc_security(void **security)
4527 {
4528 	return call_int_hook(tun_dev_alloc_security, 0, security);
4529 }
4530 EXPORT_SYMBOL(security_tun_dev_alloc_security);
4531 
4532 /**
4533  * security_tun_dev_free_security() - Free a TUN device LSM blob
4534  * @security: LSM blob
4535  *
4536  * This hook allows a module to free the security structure for a TUN device.
4537  */
4538 void security_tun_dev_free_security(void *security)
4539 {
4540 	call_void_hook(tun_dev_free_security, security);
4541 }
4542 EXPORT_SYMBOL(security_tun_dev_free_security);
4543 
4544 /**
4545  * security_tun_dev_create() - Check if creating a TUN device is allowed
4546  *
4547  * Check permissions prior to creating a new TUN device.
4548  *
4549  * Return: Returns 0 if permission is granted.
4550  */
4551 int security_tun_dev_create(void)
4552 {
4553 	return call_int_hook(tun_dev_create, 0);
4554 }
4555 EXPORT_SYMBOL(security_tun_dev_create);
4556 
4557 /**
4558  * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4559  * @security: TUN device LSM blob
4560  *
4561  * Check permissions prior to attaching to a TUN device queue.
4562  *
4563  * Return: Returns 0 if permission is granted.
4564  */
4565 int security_tun_dev_attach_queue(void *security)
4566 {
4567 	return call_int_hook(tun_dev_attach_queue, 0, security);
4568 }
4569 EXPORT_SYMBOL(security_tun_dev_attach_queue);
4570 
4571 /**
4572  * security_tun_dev_attach() - Update TUN device LSM state on attach
4573  * @sk: associated sock
4574  * @security: TUN device LSM blob
4575  *
4576  * This hook can be used by the module to update any security state associated
4577  * with the TUN device's sock structure.
4578  *
4579  * Return: Returns 0 if permission is granted.
4580  */
4581 int security_tun_dev_attach(struct sock *sk, void *security)
4582 {
4583 	return call_int_hook(tun_dev_attach, 0, sk, security);
4584 }
4585 EXPORT_SYMBOL(security_tun_dev_attach);
4586 
4587 /**
4588  * security_tun_dev_open() - Update TUN device LSM state on open
4589  * @security: TUN device LSM blob
4590  *
4591  * This hook can be used by the module to update any security state associated
4592  * with the TUN device's security structure.
4593  *
4594  * Return: Returns 0 if permission is granted.
4595  */
4596 int security_tun_dev_open(void *security)
4597 {
4598 	return call_int_hook(tun_dev_open, 0, security);
4599 }
4600 EXPORT_SYMBOL(security_tun_dev_open);
4601 
4602 /**
4603  * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4604  * @asoc: SCTP association
4605  * @skb: packet requesting the association
4606  *
4607  * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4608  *
4609  * Return: Returns 0 on success, error on failure.
4610  */
4611 int security_sctp_assoc_request(struct sctp_association *asoc,
4612 				struct sk_buff *skb)
4613 {
4614 	return call_int_hook(sctp_assoc_request, 0, asoc, skb);
4615 }
4616 EXPORT_SYMBOL(security_sctp_assoc_request);
4617 
4618 /**
4619  * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4620  * @sk: socket
4621  * @optname: SCTP option to validate
4622  * @address: list of IP addresses to validate
4623  * @addrlen: length of the address list
4624  *
4625  * Validiate permissions required for each address associated with sock	@sk.
4626  * Depending on @optname, the addresses will be treated as either a connect or
4627  * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4628  * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4629  *
4630  * Return: Returns 0 on success, error on failure.
4631  */
4632 int security_sctp_bind_connect(struct sock *sk, int optname,
4633 			       struct sockaddr *address, int addrlen)
4634 {
4635 	return call_int_hook(sctp_bind_connect, 0, sk, optname,
4636 			     address, addrlen);
4637 }
4638 EXPORT_SYMBOL(security_sctp_bind_connect);
4639 
4640 /**
4641  * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4642  * @asoc: SCTP association
4643  * @sk: original sock
4644  * @newsk: target sock
4645  *
4646  * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4647  * socket) or when a socket is 'peeled off' e.g userspace calls
4648  * sctp_peeloff(3).
4649  */
4650 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
4651 			    struct sock *newsk)
4652 {
4653 	call_void_hook(sctp_sk_clone, asoc, sk, newsk);
4654 }
4655 EXPORT_SYMBOL(security_sctp_sk_clone);
4656 
4657 /**
4658  * security_sctp_assoc_established() - Update LSM state when assoc established
4659  * @asoc: SCTP association
4660  * @skb: packet establishing the association
4661  *
4662  * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4663  * security module.
4664  *
4665  * Return: Returns 0 if permission is granted.
4666  */
4667 int security_sctp_assoc_established(struct sctp_association *asoc,
4668 				    struct sk_buff *skb)
4669 {
4670 	return call_int_hook(sctp_assoc_established, 0, asoc, skb);
4671 }
4672 EXPORT_SYMBOL(security_sctp_assoc_established);
4673 
4674 #endif	/* CONFIG_SECURITY_NETWORK */
4675 
4676 #ifdef CONFIG_SECURITY_INFINIBAND
4677 /**
4678  * security_ib_pkey_access() - Check if access to an IB pkey is allowed
4679  * @sec: LSM blob
4680  * @subnet_prefix: subnet prefix of the port
4681  * @pkey: IB pkey
4682  *
4683  * Check permission to access a pkey when modifing a QP.
4684  *
4685  * Return: Returns 0 if permission is granted.
4686  */
4687 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
4688 {
4689 	return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
4690 }
4691 EXPORT_SYMBOL(security_ib_pkey_access);
4692 
4693 /**
4694  * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
4695  * @sec: LSM blob
4696  * @dev_name: IB device name
4697  * @port_num: port number
4698  *
4699  * Check permissions to send and receive SMPs on a end port.
4700  *
4701  * Return: Returns 0 if permission is granted.
4702  */
4703 int security_ib_endport_manage_subnet(void *sec,
4704 				      const char *dev_name, u8 port_num)
4705 {
4706 	return call_int_hook(ib_endport_manage_subnet, 0, sec,
4707 			     dev_name, port_num);
4708 }
4709 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
4710 
4711 /**
4712  * security_ib_alloc_security() - Allocate an Infiniband LSM blob
4713  * @sec: LSM blob
4714  *
4715  * Allocate a security structure for Infiniband objects.
4716  *
4717  * Return: Returns 0 on success, non-zero on failure.
4718  */
4719 int security_ib_alloc_security(void **sec)
4720 {
4721 	return call_int_hook(ib_alloc_security, 0, sec);
4722 }
4723 EXPORT_SYMBOL(security_ib_alloc_security);
4724 
4725 /**
4726  * security_ib_free_security() - Free an Infiniband LSM blob
4727  * @sec: LSM blob
4728  *
4729  * Deallocate an Infiniband security structure.
4730  */
4731 void security_ib_free_security(void *sec)
4732 {
4733 	call_void_hook(ib_free_security, sec);
4734 }
4735 EXPORT_SYMBOL(security_ib_free_security);
4736 #endif	/* CONFIG_SECURITY_INFINIBAND */
4737 
4738 #ifdef CONFIG_SECURITY_NETWORK_XFRM
4739 /**
4740  * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
4741  * @ctxp: xfrm security context being added to the SPD
4742  * @sec_ctx: security label provided by userspace
4743  * @gfp: gfp flags
4744  *
4745  * Allocate a security structure to the xp->security field; the security field
4746  * is initialized to NULL when the xfrm_policy is allocated.
4747  *
4748  * Return:  Return 0 if operation was successful.
4749  */
4750 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
4751 			       struct xfrm_user_sec_ctx *sec_ctx,
4752 			       gfp_t gfp)
4753 {
4754 	return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
4755 }
4756 EXPORT_SYMBOL(security_xfrm_policy_alloc);
4757 
4758 /**
4759  * security_xfrm_policy_clone() - Clone xfrm policy LSM state
4760  * @old_ctx: xfrm security context
4761  * @new_ctxp: target xfrm security context
4762  *
4763  * Allocate a security structure in new_ctxp that contains the information from
4764  * the old_ctx structure.
4765  *
4766  * Return: Return 0 if operation was successful.
4767  */
4768 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
4769 			       struct xfrm_sec_ctx **new_ctxp)
4770 {
4771 	return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
4772 }
4773 
4774 /**
4775  * security_xfrm_policy_free() - Free a xfrm security context
4776  * @ctx: xfrm security context
4777  *
4778  * Free LSM resources associated with @ctx.
4779  */
4780 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
4781 {
4782 	call_void_hook(xfrm_policy_free_security, ctx);
4783 }
4784 EXPORT_SYMBOL(security_xfrm_policy_free);
4785 
4786 /**
4787  * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
4788  * @ctx: xfrm security context
4789  *
4790  * Authorize deletion of a SPD entry.
4791  *
4792  * Return: Returns 0 if permission is granted.
4793  */
4794 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
4795 {
4796 	return call_int_hook(xfrm_policy_delete_security, 0, ctx);
4797 }
4798 
4799 /**
4800  * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
4801  * @x: xfrm state being added to the SAD
4802  * @sec_ctx: security label provided by userspace
4803  *
4804  * Allocate a security structure to the @x->security field; the security field
4805  * is initialized to NULL when the xfrm_state is allocated. Set the context to
4806  * correspond to @sec_ctx.
4807  *
4808  * Return: Return 0 if operation was successful.
4809  */
4810 int security_xfrm_state_alloc(struct xfrm_state *x,
4811 			      struct xfrm_user_sec_ctx *sec_ctx)
4812 {
4813 	return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
4814 }
4815 EXPORT_SYMBOL(security_xfrm_state_alloc);
4816 
4817 /**
4818  * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
4819  * @x: xfrm state being added to the SAD
4820  * @polsec: associated policy's security context
4821  * @secid: secid from the flow
4822  *
4823  * Allocate a security structure to the x->security field; the security field
4824  * is initialized to NULL when the xfrm_state is allocated.  Set the context to
4825  * correspond to secid.
4826  *
4827  * Return: Returns 0 if operation was successful.
4828  */
4829 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
4830 				      struct xfrm_sec_ctx *polsec, u32 secid)
4831 {
4832 	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
4833 }
4834 
4835 /**
4836  * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
4837  * @x: xfrm state
4838  *
4839  * Authorize deletion of x->security.
4840  *
4841  * Return: Returns 0 if permission is granted.
4842  */
4843 int security_xfrm_state_delete(struct xfrm_state *x)
4844 {
4845 	return call_int_hook(xfrm_state_delete_security, 0, x);
4846 }
4847 EXPORT_SYMBOL(security_xfrm_state_delete);
4848 
4849 /**
4850  * security_xfrm_state_free() - Free a xfrm state
4851  * @x: xfrm state
4852  *
4853  * Deallocate x->security.
4854  */
4855 void security_xfrm_state_free(struct xfrm_state *x)
4856 {
4857 	call_void_hook(xfrm_state_free_security, x);
4858 }
4859 
4860 /**
4861  * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
4862  * @ctx: target xfrm security context
4863  * @fl_secid: flow secid used to authorize access
4864  *
4865  * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
4866  * packet.  The hook is called when selecting either a per-socket policy or a
4867  * generic xfrm policy.
4868  *
4869  * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
4870  *         other errors.
4871  */
4872 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
4873 {
4874 	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
4875 }
4876 
4877 /**
4878  * security_xfrm_state_pol_flow_match() - Check for a xfrm match
4879  * @x: xfrm state to match
4880  * @xp xfrm policy to check for a match
4881  * @flic: flow to check for a match.
4882  *
4883  * Check @xp and @flic for a match with @x.
4884  *
4885  * Return: Returns 1 if there is a match.
4886  */
4887 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
4888 				       struct xfrm_policy *xp,
4889 				       const struct flowi_common *flic)
4890 {
4891 	struct security_hook_list *hp;
4892 	int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
4893 
4894 	/*
4895 	 * Since this function is expected to return 0 or 1, the judgment
4896 	 * becomes difficult if multiple LSMs supply this call. Fortunately,
4897 	 * we can use the first LSM's judgment because currently only SELinux
4898 	 * supplies this call.
4899 	 *
4900 	 * For speed optimization, we explicitly break the loop rather than
4901 	 * using the macro
4902 	 */
4903 	hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
4904 			     list) {
4905 		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
4906 		break;
4907 	}
4908 	return rc;
4909 }
4910 
4911 /**
4912  * security_xfrm_decode_session() - Determine the xfrm secid for a packet
4913  * @skb: xfrm packet
4914  * @secid: secid
4915  *
4916  * Decode the packet in @skb and return the security label in @secid.
4917  *
4918  * Return: Return 0 if all xfrms used have the same secid.
4919  */
4920 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
4921 {
4922 	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
4923 }
4924 
4925 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
4926 {
4927 	int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
4928 			       0);
4929 
4930 	BUG_ON(rc);
4931 }
4932 EXPORT_SYMBOL(security_skb_classify_flow);
4933 #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
4934 
4935 #ifdef CONFIG_KEYS
4936 /**
4937  * security_key_alloc() - Allocate and initialize a kernel key LSM blob
4938  * @key: key
4939  * @cred: credentials
4940  * @flags: allocation flags
4941  *
4942  * Permit allocation of a key and assign security data. Note that key does not
4943  * have a serial number assigned at this point.
4944  *
4945  * Return: Return 0 if permission is granted, -ve error otherwise.
4946  */
4947 int security_key_alloc(struct key *key, const struct cred *cred,
4948 		       unsigned long flags)
4949 {
4950 	return call_int_hook(key_alloc, 0, key, cred, flags);
4951 }
4952 
4953 /**
4954  * security_key_free() - Free a kernel key LSM blob
4955  * @key: key
4956  *
4957  * Notification of destruction; free security data.
4958  */
4959 void security_key_free(struct key *key)
4960 {
4961 	call_void_hook(key_free, key);
4962 }
4963 
4964 /**
4965  * security_key_permission() - Check if a kernel key operation is allowed
4966  * @key_ref: key reference
4967  * @cred: credentials of actor requesting access
4968  * @need_perm: requested permissions
4969  *
4970  * See whether a specific operational right is granted to a process on a key.
4971  *
4972  * Return: Return 0 if permission is granted, -ve error otherwise.
4973  */
4974 int security_key_permission(key_ref_t key_ref, const struct cred *cred,
4975 			    enum key_need_perm need_perm)
4976 {
4977 	return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
4978 }
4979 
4980 /**
4981  * security_key_getsecurity() - Get the key's security label
4982  * @key: key
4983  * @buffer: security label buffer
4984  *
4985  * Get a textual representation of the security context attached to a key for
4986  * the purposes of honouring KEYCTL_GETSECURITY.  This function allocates the
4987  * storage for the NUL-terminated string and the caller should free it.
4988  *
4989  * Return: Returns the length of @buffer (including terminating NUL) or -ve if
4990  *         an error occurs.  May also return 0 (and a NULL buffer pointer) if
4991  *         there is no security label assigned to the key.
4992  */
4993 int security_key_getsecurity(struct key *key, char **_buffer)
4994 {
4995 	*_buffer = NULL;
4996 	return call_int_hook(key_getsecurity, 0, key, _buffer);
4997 }
4998 #endif	/* CONFIG_KEYS */
4999 
5000 #ifdef CONFIG_AUDIT
5001 /**
5002  * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5003  * @field: audit action
5004  * @op: rule operator
5005  * @rulestr: rule context
5006  * @lsmrule: receive buffer for audit rule struct
5007  *
5008  * Allocate and initialize an LSM audit rule structure.
5009  *
5010  * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5011  *         an invalid rule.
5012  */
5013 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
5014 {
5015 	return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
5016 }
5017 
5018 /**
5019  * security_audit_rule_known() - Check if an audit rule contains LSM fields
5020  * @krule: audit rule
5021  *
5022  * Specifies whether given @krule contains any fields related to the current
5023  * LSM.
5024  *
5025  * Return: Returns 1 in case of relation found, 0 otherwise.
5026  */
5027 int security_audit_rule_known(struct audit_krule *krule)
5028 {
5029 	return call_int_hook(audit_rule_known, 0, krule);
5030 }
5031 
5032 /**
5033  * security_audit_rule_free() - Free an LSM audit rule struct
5034  * @lsmrule: audit rule struct
5035  *
5036  * Deallocate the LSM audit rule structure previously allocated by
5037  * audit_rule_init().
5038  */
5039 void security_audit_rule_free(void *lsmrule)
5040 {
5041 	call_void_hook(audit_rule_free, lsmrule);
5042 }
5043 
5044 /**
5045  * security_audit_rule_match() - Check if a label matches an audit rule
5046  * @secid: security label
5047  * @field: LSM audit field
5048  * @op: matching operator
5049  * @lsmrule: audit rule
5050  *
5051  * Determine if given @secid matches a rule previously approved by
5052  * security_audit_rule_known().
5053  *
5054  * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5055  *         failure.
5056  */
5057 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
5058 {
5059 	return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
5060 }
5061 #endif /* CONFIG_AUDIT */
5062 
5063 #ifdef CONFIG_BPF_SYSCALL
5064 /**
5065  * security_bpf() - Check if the bpf syscall operation is allowed
5066  * @cmd: command
5067  * @attr: bpf attribute
5068  * @size: size
5069  *
5070  * Do a initial check for all bpf syscalls after the attribute is copied into
5071  * the kernel. The actual security module can implement their own rules to
5072  * check the specific cmd they need.
5073  *
5074  * Return: Returns 0 if permission is granted.
5075  */
5076 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5077 {
5078 	return call_int_hook(bpf, 0, cmd, attr, size);
5079 }
5080 
5081 /**
5082  * security_bpf_map() - Check if access to a bpf map is allowed
5083  * @map: bpf map
5084  * @fmode: mode
5085  *
5086  * Do a check when the kernel generates and returns a file descriptor for eBPF
5087  * maps.
5088  *
5089  * Return: Returns 0 if permission is granted.
5090  */
5091 int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5092 {
5093 	return call_int_hook(bpf_map, 0, map, fmode);
5094 }
5095 
5096 /**
5097  * security_bpf_prog() - Check if access to a bpf program is allowed
5098  * @prog: bpf program
5099  *
5100  * Do a check when the kernel generates and returns a file descriptor for eBPF
5101  * programs.
5102  *
5103  * Return: Returns 0 if permission is granted.
5104  */
5105 int security_bpf_prog(struct bpf_prog *prog)
5106 {
5107 	return call_int_hook(bpf_prog, 0, prog);
5108 }
5109 
5110 /**
5111  * security_bpf_map_alloc() - Allocate a bpf map LSM blob
5112  * @map: bpf map
5113  *
5114  * Initialize the security field inside bpf map.
5115  *
5116  * Return: Returns 0 on success, error on failure.
5117  */
5118 int security_bpf_map_alloc(struct bpf_map *map)
5119 {
5120 	return call_int_hook(bpf_map_alloc_security, 0, map);
5121 }
5122 
5123 /**
5124  * security_bpf_prog_alloc() - Allocate a bpf program LSM blob
5125  * @aux: bpf program aux info struct
5126  *
5127  * Initialize the security field inside bpf program.
5128  *
5129  * Return: Returns 0 on success, error on failure.
5130  */
5131 int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
5132 {
5133 	return call_int_hook(bpf_prog_alloc_security, 0, aux);
5134 }
5135 
5136 /**
5137  * security_bpf_map_free() - Free a bpf map's LSM blob
5138  * @map: bpf map
5139  *
5140  * Clean up the security information stored inside bpf map.
5141  */
5142 void security_bpf_map_free(struct bpf_map *map)
5143 {
5144 	call_void_hook(bpf_map_free_security, map);
5145 }
5146 
5147 /**
5148  * security_bpf_prog_free() - Free a bpf program's LSM blob
5149  * @aux: bpf program aux info struct
5150  *
5151  * Clean up the security information stored inside bpf prog.
5152  */
5153 void security_bpf_prog_free(struct bpf_prog_aux *aux)
5154 {
5155 	call_void_hook(bpf_prog_free_security, aux);
5156 }
5157 #endif /* CONFIG_BPF_SYSCALL */
5158 
5159 /**
5160  * security_locked_down() - Check if a kernel feature is allowed
5161  * @what: requested kernel feature
5162  *
5163  * Determine whether a kernel feature that potentially enables arbitrary code
5164  * execution in kernel space should be permitted.
5165  *
5166  * Return: Returns 0 if permission is granted.
5167  */
5168 int security_locked_down(enum lockdown_reason what)
5169 {
5170 	return call_int_hook(locked_down, 0, what);
5171 }
5172 EXPORT_SYMBOL(security_locked_down);
5173 
5174 #ifdef CONFIG_PERF_EVENTS
5175 /**
5176  * security_perf_event_open() - Check if a perf event open is allowed
5177  * @attr: perf event attribute
5178  * @type: type of event
5179  *
5180  * Check whether the @type of perf_event_open syscall is allowed.
5181  *
5182  * Return: Returns 0 if permission is granted.
5183  */
5184 int security_perf_event_open(struct perf_event_attr *attr, int type)
5185 {
5186 	return call_int_hook(perf_event_open, 0, attr, type);
5187 }
5188 
5189 /**
5190  * security_perf_event_alloc() - Allocate a perf event LSM blob
5191  * @event: perf event
5192  *
5193  * Allocate and save perf_event security info.
5194  *
5195  * Return: Returns 0 on success, error on failure.
5196  */
5197 int security_perf_event_alloc(struct perf_event *event)
5198 {
5199 	return call_int_hook(perf_event_alloc, 0, event);
5200 }
5201 
5202 /**
5203  * security_perf_event_free() - Free a perf event LSM blob
5204  * @event: perf event
5205  *
5206  * Release (free) perf_event security info.
5207  */
5208 void security_perf_event_free(struct perf_event *event)
5209 {
5210 	call_void_hook(perf_event_free, event);
5211 }
5212 
5213 /**
5214  * security_perf_event_read() - Check if reading a perf event label is allowed
5215  * @event: perf event
5216  *
5217  * Read perf_event security info if allowed.
5218  *
5219  * Return: Returns 0 if permission is granted.
5220  */
5221 int security_perf_event_read(struct perf_event *event)
5222 {
5223 	return call_int_hook(perf_event_read, 0, event);
5224 }
5225 
5226 /**
5227  * security_perf_event_write() - Check if writing a perf event label is allowed
5228  * @event: perf event
5229  *
5230  * Write perf_event security info if allowed.
5231  *
5232  * Return: Returns 0 if permission is granted.
5233  */
5234 int security_perf_event_write(struct perf_event *event)
5235 {
5236 	return call_int_hook(perf_event_write, 0, event);
5237 }
5238 #endif /* CONFIG_PERF_EVENTS */
5239 
5240 #ifdef CONFIG_IO_URING
5241 /**
5242  * security_uring_override_creds() - Check if overriding creds is allowed
5243  * @new: new credentials
5244  *
5245  * Check if the current task, executing an io_uring operation, is allowed to
5246  * override it's credentials with @new.
5247  *
5248  * Return: Returns 0 if permission is granted.
5249  */
5250 int security_uring_override_creds(const struct cred *new)
5251 {
5252 	return call_int_hook(uring_override_creds, 0, new);
5253 }
5254 
5255 /**
5256  * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
5257  *
5258  * Check whether the current task is allowed to spawn a io_uring polling thread
5259  * (IORING_SETUP_SQPOLL).
5260  *
5261  * Return: Returns 0 if permission is granted.
5262  */
5263 int security_uring_sqpoll(void)
5264 {
5265 	return call_int_hook(uring_sqpoll, 0);
5266 }
5267 
5268 /**
5269  * security_uring_cmd() - Check if a io_uring passthrough command is allowed
5270  * @ioucmd: command
5271  *
5272  * Check whether the file_operations uring_cmd is allowed to run.
5273  *
5274  * Return: Returns 0 if permission is granted.
5275  */
5276 int security_uring_cmd(struct io_uring_cmd *ioucmd)
5277 {
5278 	return call_int_hook(uring_cmd, 0, ioucmd);
5279 }
5280 #endif /* CONFIG_IO_URING */
5281