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