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