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