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