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