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