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