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