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