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