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