2 * Security plug functions
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 * Copyright (C) 2016 Mellanox Technologies
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/capability.h>
16 #include <linux/dcache.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/lsm_hooks.h>
21 #include <linux/integrity.h>
22 #include <linux/ima.h>
23 #include <linux/evm.h>
24 #include <linux/fsnotify.h>
25 #include <linux/mman.h>
26 #include <linux/mount.h>
27 #include <linux/personality.h>
28 #include <linux/backing-dev.h>
29 #include <linux/string.h>
30 #include <linux/prctl.h>
33 #define MAX_LSM_EVM_XATTR 2
35 /* Maximum number of letters for an LSM name string */
36 #define SECURITY_NAME_MAX 10
37 #define SECURITY_CHOSEN_NAMES_MAX (SECURITY_NAME_MAX * LSM_MAX_MAJOR)
38 #define MODULE_STACK "(stacking)"
40 struct security_hook_heads security_hook_heads __lsm_ro_after_init
;
41 static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain
);
46 * If stacking is enabled the task blob will always
47 * include an indicator of what security module data
48 * should be displayed. This is set with PR_SET_DISPLAY_LSM.
50 static struct lsm_blob_sizes blob_sizes
= {
51 #ifdef CONFIG_SECURITY_STACKING
52 .lbs_task
= SECURITY_NAME_MAX
+ 6,
56 /* Boot-time LSM user choice */
57 static __initdata
char chosen_lsms
[SECURITY_CHOSEN_NAMES_MAX
+ 1] =
58 #ifdef CONFIG_SECURITY_STACKING
61 CONFIG_DEFAULT_SECURITY
;
63 static __initdata
char chosen_display_lsm
[SECURITY_NAME_MAX
+ 1]
64 #ifdef CONFIG_SECURITY_STACKING
65 = CONFIG_SECURITY_DEFAULT_DISPLAY_NAME
68 static char default_display_lsm
[SECURITY_NAME_MAX
+ 1];
70 static void __init
do_security_initcalls(void)
73 call
= __security_initcall_start
;
74 while (call
< __security_initcall_end
) {
81 * security_init - initializes the security framework
83 * This should be called early in the kernel initialization sequence.
85 int __init
security_init(void)
88 struct list_head
*list
= (struct list_head
*) &security_hook_heads
;
90 for (i
= 0; i
< sizeof(security_hook_heads
) / sizeof(struct list_head
);
92 INIT_LIST_HEAD(&list
[i
]);
93 pr_info("Security Framework initialized\n");
96 * Load minor LSMs, with the capability module always first.
98 capability_add_hooks();
103 * The first call to a module specific init function
104 * updates the blob size requirements.
106 do_security_initcalls();
109 * The second call to a module specific init function
110 * adds hooks to the hook lists and does any other early
111 * initializations required.
113 do_security_initcalls();
115 #ifdef CONFIG_SECURITY_LSM_DEBUG
116 pr_info("LSM: cred blob size = %d\n", blob_sizes
.lbs_cred
);
117 pr_info("LSM: file blob size = %d\n", blob_sizes
.lbs_file
);
118 pr_info("LSM: task blob size = %d\n", blob_sizes
.lbs_task
);
124 /* Save user chosen LSM */
125 static int __init
choose_lsm(char *str
)
127 strncpy(chosen_lsms
, str
, SECURITY_CHOSEN_NAMES_MAX
);
128 pr_info("LSM: command line set '%s' security module(s).\n",
132 __setup("security=", choose_lsm
);
134 static int __init
choose_display_lsm(char *str
)
136 strncpy(chosen_display_lsm
, str
, SECURITY_NAME_MAX
);
137 pr_info("LSM: command line set default display lsm %s'\n",
141 __setup("security.display=", choose_display_lsm
);
143 static bool match_last_lsm(const char *list
, const char *lsm
)
147 if (WARN_ON(!list
|| !lsm
))
149 last
= strrchr(list
, ',');
151 /* Pass the comma, strcmp() will check for '\0' */
155 return !strcmp(last
, lsm
);
158 static int lsm_append(char *new, char **result
)
162 if (*result
== NULL
) {
163 *result
= kstrdup(new, GFP_KERNEL
);
165 /* Check if it is the last registered name */
166 if (match_last_lsm(*result
, new))
168 cp
= kasprintf(GFP_KERNEL
, "%s,%s", *result
, new);
178 * security_module_enable - Load given security module on boot ?
179 * @module: the name of the module
180 * @stacked: indicates that the module wants to be stacked
182 * Each LSM must pass this method before registering its own operations
183 * to avoid security registration races. This method may also be used
184 * to check if your LSM is currently loaded during kernel initialization.
190 * - The passed LSM is the one chosen by user at boot time,
191 * - or the passed LSM is configured as the default and the user did not
192 * choose an alternate LSM at boot time.
194 * Otherwise, return false.
196 #ifdef CONFIG_SECURITY_STACKING
197 static bool __init
cmp_lsms(const char *lsm
)
199 const char *str
= chosen_lsms
;
201 int len
= strlen(lsm
);
203 if (len
> SECURITY_NAME_MAX
) {
204 pr_info("LSM: security module name '%s' exceeds limit\n", lsm
);
207 for (split
= strchr(str
, ','); split
; split
= strchr(str
, ',')) {
208 if ((len
== split
- str
) && !strncmp(lsm
, str
, split
- str
))
212 if ((len
== strlen(str
)) && !strncmp(lsm
, str
, strlen(str
)))
218 bool __init
security_module_enable(const char *lsm
, const bool stacked
)
220 #ifdef CONFIG_SECURITY_STACKING
222 * Module defined on the command line security=XXXX
224 if (strcmp(chosen_lsms
, MODULE_STACK
)) {
226 /* set to first LSM registered and then override */
227 if (!*default_display_lsm
)
228 strcpy(default_display_lsm
, lsm
);
229 else if (*chosen_display_lsm
&& !strcmp(chosen_display_lsm
, lsm
)) {
230 strcpy(default_display_lsm
, lsm
);
231 pr_info("LSM: default display lsm '%s'\n", default_display_lsm
);
238 * Module configured as stacked.
240 if (stacked
&& !*default_display_lsm
)
241 strcpy(default_display_lsm
, lsm
);
242 else if (stacked
&& *chosen_display_lsm
&& !strcmp(chosen_display_lsm
, lsm
)) {
243 strcpy(default_display_lsm
, lsm
);
244 pr_info("LSM: default display lsm '%s'\n", default_display_lsm
);
249 if (strcmp(lsm
, chosen_lsms
) == 0) {
250 strcpy(default_display_lsm
, lsm
);
258 * Keep the order of major modules for mapping secids.
260 static int lsm_next_major
;
263 * security_add_hooks - Add a modules hooks to the hook lists.
264 * @hooks: the hooks to add
265 * @count: the number of hooks to add
266 * @lsm: the name of the security module
268 * Each LSM has to register its hooks with the infrastructure.
270 void __init
security_add_hooks(struct security_hook_list
*hooks
, int count
,
274 int lsm_index
= lsm_next_major
++;
276 #ifdef CONFIG_SECURITY_LSM_DEBUG
277 pr_info("LSM: Security module %s gets index %d\n", lsm
, lsm_index
);
279 for (i
= 0; i
< count
; i
++) {
281 hooks
[i
].lsm_index
= lsm_index
;
282 list_add_tail_rcu(&hooks
[i
].list
, hooks
[i
].head
);
284 if (lsm_append(lsm
, &lsm_names
) < 0)
285 panic("%s - Cannot get early memory.\n", __func__
);
288 int call_lsm_notifier(enum lsm_event event
, void *data
)
290 return atomic_notifier_call_chain(&lsm_notifier_chain
, event
, data
);
292 EXPORT_SYMBOL(call_lsm_notifier
);
294 int register_lsm_notifier(struct notifier_block
*nb
)
296 return atomic_notifier_chain_register(&lsm_notifier_chain
, nb
);
298 EXPORT_SYMBOL(register_lsm_notifier
);
300 int unregister_lsm_notifier(struct notifier_block
*nb
)
302 return atomic_notifier_chain_unregister(&lsm_notifier_chain
, nb
);
304 EXPORT_SYMBOL(unregister_lsm_notifier
);
307 * lsm_cred_alloc - allocate a composite cred blob
308 * @cred: the cred that needs a blob
309 * @gfp: allocation type
311 * Allocate the cred blob for all the modules
313 * Returns 0, or -ENOMEM if memory can't be allocated.
315 int lsm_cred_alloc(struct cred
*cred
, gfp_t gfp
)
317 #ifdef CONFIG_SECURITY_LSM_DEBUG
319 pr_info("%s: Inbound cred blob is not NULL.\n", __func__
);
321 if (blob_sizes
.lbs_cred
== 0)
324 cred
->security
= kzalloc(blob_sizes
.lbs_cred
, gfp
);
325 if (cred
->security
== NULL
)
331 * lsm_early_cred - during initialization allocate a composite cred blob
332 * @cred: the cred that needs a blob
334 * Allocate the cred blob for all the modules if it's not already there
336 void lsm_early_cred(struct cred
*cred
)
341 panic("%s: NULL cred.\n", __func__
);
342 if (cred
->security
!= NULL
)
344 rc
= lsm_cred_alloc(cred
, GFP_KERNEL
);
346 panic("%s: Early cred alloc failed.\n", __func__
);
349 static void __init
lsm_set_size(int *need
, int *lbs
)
361 * security_add_blobs - Report blob sizes
362 * @needed: the size of blobs needed by the module
364 * Each LSM has to register its blobs with the infrastructure.
365 * The "needed" data tells the infrastructure how much memory
366 * the module requires for each of its blobs. On return the
367 * structure is filled with the offset that module should use
368 * from the blob pointer.
370 void __init
security_add_blobs(struct lsm_blob_sizes
*needed
)
372 lsm_set_size(&needed
->lbs_cred
, &blob_sizes
.lbs_cred
);
373 lsm_set_size(&needed
->lbs_file
, &blob_sizes
.lbs_file
);
374 lsm_set_size(&needed
->lbs_task
, &blob_sizes
.lbs_task
);
378 * lsm_file_alloc - allocate a composite file blob
379 * @file: the file that needs a blob
381 * Allocate the file blob for all the modules
383 * Returns 0, or -ENOMEM if memory can't be allocated.
385 int lsm_file_alloc(struct file
*file
)
387 #ifdef CONFIG_SECURITY_LSM_DEBUG
388 if (file
->f_security
)
389 pr_info("%s: Inbound file blob is not NULL.\n", __func__
);
391 if (blob_sizes
.lbs_file
== 0)
394 file
->f_security
= kzalloc(blob_sizes
.lbs_file
, GFP_KERNEL
);
395 if (file
->f_security
== NULL
)
400 #ifdef CONFIG_SECURITY_STACKING
401 static inline char *lsm_of_task(struct task_struct
*task
)
403 #ifdef CONFIG_SECURITY_LSM_DEBUG
404 if (task
->security
== NULL
)
405 pr_info("%s: task has no lsm name.\n", __func__
);
407 return task
->security
;
411 #ifdef CONFIG_SECURITY_STACKING
418 * lsm_parse_context - break a compound "context" into module data
419 * @cxt: the initial data, which will be modified
420 * @vlist: an array to receive the results
422 * Returns the number of entries, or -EINVAL if the cxt is unworkable.
424 static int lsm_parse_context(char *cxt
, struct lsm_value
*vlist
)
432 for (i
= 0; i
< LSM_MAX_MAJOR
; i
++) {
433 data
= strstr(lsm
, "='");
438 cp
= strchr(data
, '\'');
443 vlist
[i
].data
= data
;
456 #endif /* CONFIG_SECURITY_STACKING */
459 * lsm_task_alloc - allocate a composite task blob
460 * @task: the task that needs a blob
462 * Allocate the task blob for all the modules
464 * Returns 0, or -ENOMEM if memory can't be allocated.
466 int lsm_task_alloc(struct task_struct
*task
)
468 #ifdef CONFIG_SECURITY_LSM_DEBUG
470 pr_info("%s: Inbound task blob is not NULL.\n", __func__
);
472 if (blob_sizes
.lbs_task
== 0)
475 task
->security
= kzalloc(blob_sizes
.lbs_task
, GFP_KERNEL
);
476 if (task
->security
== NULL
)
479 /* inherit current display lsm */
480 #ifdef CONFIG_SECURITY_STACKING
481 if (current
->security
)
482 strcpy(task
->security
, lsm_of_task(current
));
484 strcpy(task
->security
, default_display_lsm
);
490 * Hook list operation macros.
493 * This is a hook that does not return a value.
496 * This is a hook that returns a value.
499 #define call_void_hook(FUNC, ...) \
501 struct security_hook_list *P; \
503 list_for_each_entry(P, &security_hook_heads.FUNC, list) \
504 P->hook.FUNC(__VA_ARGS__); \
507 #define call_int_hook(FUNC, IRC, ...) ({ \
510 struct security_hook_list *P; \
512 list_for_each_entry(P, &security_hook_heads.FUNC, list) { \
513 RC = P->hook.FUNC(__VA_ARGS__); \
521 /* Security operations */
523 int security_binder_set_context_mgr(struct task_struct
*mgr
)
525 return call_int_hook(binder_set_context_mgr
, 0, mgr
);
528 int security_binder_transaction(struct task_struct
*from
,
529 struct task_struct
*to
)
531 return call_int_hook(binder_transaction
, 0, from
, to
);
534 int security_binder_transfer_binder(struct task_struct
*from
,
535 struct task_struct
*to
)
537 return call_int_hook(binder_transfer_binder
, 0, from
, to
);
540 int security_binder_transfer_file(struct task_struct
*from
,
541 struct task_struct
*to
, struct file
*file
)
543 return call_int_hook(binder_transfer_file
, 0, from
, to
, file
);
546 int security_ptrace_access_check(struct task_struct
*child
, unsigned int mode
)
548 return call_int_hook(ptrace_access_check
, 0, child
, mode
);
551 int security_ptrace_traceme(struct task_struct
*parent
)
553 return call_int_hook(ptrace_traceme
, 0, parent
);
556 int security_capget(struct task_struct
*target
,
557 kernel_cap_t
*effective
,
558 kernel_cap_t
*inheritable
,
559 kernel_cap_t
*permitted
)
561 return call_int_hook(capget
, 0, target
,
562 effective
, inheritable
, permitted
);
565 int security_capset(struct cred
*new, const struct cred
*old
,
566 const kernel_cap_t
*effective
,
567 const kernel_cap_t
*inheritable
,
568 const kernel_cap_t
*permitted
)
570 return call_int_hook(capset
, 0, new, old
,
571 effective
, inheritable
, permitted
);
574 int security_capable(const struct cred
*cred
, struct user_namespace
*ns
,
577 return call_int_hook(capable
, 0, cred
, ns
, cap
, SECURITY_CAP_AUDIT
);
580 int security_capable_noaudit(const struct cred
*cred
, struct user_namespace
*ns
,
583 return call_int_hook(capable
, 0, cred
, ns
, cap
, SECURITY_CAP_NOAUDIT
);
586 int security_quotactl(int cmds
, int type
, int id
, struct super_block
*sb
)
588 return call_int_hook(quotactl
, 0, cmds
, type
, id
, sb
);
591 int security_quota_on(struct dentry
*dentry
)
593 return call_int_hook(quota_on
, 0, dentry
);
596 int security_syslog(int type
)
598 return call_int_hook(syslog
, 0, type
);
601 int security_settime64(const struct timespec64
*ts
, const struct timezone
*tz
)
603 return call_int_hook(settime
, 0, ts
, tz
);
606 int security_vm_enough_memory_mm(struct mm_struct
*mm
, long pages
)
608 struct security_hook_list
*hp
;
609 int cap_sys_admin
= 1;
613 * The module will respond with a positive value if
614 * it thinks the __vm_enough_memory() call should be
615 * made with the cap_sys_admin set. If all of the modules
616 * agree that it should be set it will. If any module
617 * thinks it should not be set it won't.
619 list_for_each_entry(hp
, &security_hook_heads
.vm_enough_memory
, list
) {
620 rc
= hp
->hook
.vm_enough_memory(mm
, pages
);
626 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);
629 int security_bprm_set_creds(struct linux_binprm
*bprm
)
631 return call_int_hook(bprm_set_creds
, 0, bprm
);
634 int security_bprm_check(struct linux_binprm
*bprm
)
638 ret
= call_int_hook(bprm_check_security
, 0, bprm
);
641 return ima_bprm_check(bprm
);
644 void security_bprm_committing_creds(struct linux_binprm
*bprm
)
646 call_void_hook(bprm_committing_creds
, bprm
);
649 void security_bprm_committed_creds(struct linux_binprm
*bprm
)
651 call_void_hook(bprm_committed_creds
, bprm
);
654 int security_bprm_secureexec(struct linux_binprm
*bprm
)
656 return call_int_hook(bprm_secureexec
, 0, bprm
);
659 int security_sb_alloc(struct super_block
*sb
)
661 return call_int_hook(sb_alloc_security
, 0, sb
);
664 void security_sb_free(struct super_block
*sb
)
666 call_void_hook(sb_free_security
, sb
);
669 int security_sb_copy_data(char *orig
, char *copy
)
671 return call_int_hook(sb_copy_data
, 0, orig
, copy
);
673 EXPORT_SYMBOL(security_sb_copy_data
);
675 int security_sb_remount(struct super_block
*sb
, void *data
)
677 return call_int_hook(sb_remount
, 0, sb
, data
);
680 int security_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
682 return call_int_hook(sb_kern_mount
, 0, sb
, flags
, data
);
685 int security_sb_show_options(struct seq_file
*m
, struct super_block
*sb
)
687 return call_int_hook(sb_show_options
, 0, m
, sb
);
690 int security_sb_statfs(struct dentry
*dentry
)
692 return call_int_hook(sb_statfs
, 0, dentry
);
695 int security_sb_mount(const char *dev_name
, const struct path
*path
,
696 const char *type
, unsigned long flags
, void *data
)
698 return call_int_hook(sb_mount
, 0, dev_name
, path
, type
, flags
, data
);
701 int security_sb_umount(struct vfsmount
*mnt
, int flags
)
703 return call_int_hook(sb_umount
, 0, mnt
, flags
);
706 int security_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
708 return call_int_hook(sb_pivotroot
, 0, old_path
, new_path
);
711 int security_sb_set_mnt_opts(struct super_block
*sb
,
712 struct security_mnt_opts
*opts
,
713 unsigned long kern_flags
,
714 unsigned long *set_kern_flags
)
716 return call_int_hook(sb_set_mnt_opts
,
717 opts
->num_mnt_opts
? -EOPNOTSUPP
: 0, sb
,
718 opts
, kern_flags
, set_kern_flags
);
720 EXPORT_SYMBOL(security_sb_set_mnt_opts
);
722 int security_sb_clone_mnt_opts(const struct super_block
*oldsb
,
723 struct super_block
*newsb
,
724 unsigned long kern_flags
,
725 unsigned long *set_kern_flags
)
727 return call_int_hook(sb_clone_mnt_opts
, 0, oldsb
, newsb
,
728 kern_flags
, set_kern_flags
);
730 EXPORT_SYMBOL(security_sb_clone_mnt_opts
);
732 int security_sb_parse_opts_str(char *options
, struct security_mnt_opts
*opts
)
734 return call_int_hook(sb_parse_opts_str
, 0, options
, opts
);
736 EXPORT_SYMBOL(security_sb_parse_opts_str
);
738 int security_inode_alloc(struct inode
*inode
)
740 inode
->i_security
= NULL
;
741 return call_int_hook(inode_alloc_security
, 0, inode
);
744 void security_inode_free(struct inode
*inode
)
746 integrity_inode_free(inode
);
747 call_void_hook(inode_free_security
, inode
);
750 int security_dentry_init_security(struct dentry
*dentry
, int mode
,
751 const struct qstr
*name
, void **ctx
,
754 return call_int_hook(dentry_init_security
, -EOPNOTSUPP
, dentry
, mode
,
757 EXPORT_SYMBOL(security_dentry_init_security
);
759 int security_dentry_create_files_as(struct dentry
*dentry
, int mode
,
761 const struct cred
*old
, struct cred
*new)
763 return call_int_hook(dentry_create_files_as
, 0, dentry
, mode
,
766 EXPORT_SYMBOL(security_dentry_create_files_as
);
768 int security_inode_init_security(struct inode
*inode
, struct inode
*dir
,
769 const struct qstr
*qstr
,
770 const initxattrs initxattrs
, void *fs_data
)
772 struct xattr new_xattrs
[MAX_LSM_EVM_XATTR
+ 1];
773 struct xattr
*lsm_xattr
, *evm_xattr
, *xattr
;
776 if (unlikely(IS_PRIVATE(inode
)))
780 return call_int_hook(inode_init_security
, -EOPNOTSUPP
, inode
,
781 dir
, qstr
, NULL
, NULL
, NULL
);
782 memset(new_xattrs
, 0, sizeof(new_xattrs
));
783 lsm_xattr
= new_xattrs
;
784 ret
= call_int_hook(inode_init_security
, -EOPNOTSUPP
, inode
, dir
, qstr
,
787 &lsm_xattr
->value_len
);
791 evm_xattr
= lsm_xattr
+ 1;
792 ret
= evm_inode_init_security(inode
, lsm_xattr
, evm_xattr
);
795 ret
= initxattrs(inode
, new_xattrs
, fs_data
);
797 for (xattr
= new_xattrs
; xattr
->value
!= NULL
; xattr
++)
799 return (ret
== -EOPNOTSUPP
) ? 0 : ret
;
801 EXPORT_SYMBOL(security_inode_init_security
);
803 int security_old_inode_init_security(struct inode
*inode
, struct inode
*dir
,
804 const struct qstr
*qstr
, const char **name
,
805 void **value
, size_t *len
)
807 if (unlikely(IS_PRIVATE(inode
)))
809 return call_int_hook(inode_init_security
, -EOPNOTSUPP
, inode
, dir
,
810 qstr
, name
, value
, len
);
812 EXPORT_SYMBOL(security_old_inode_init_security
);
814 #ifdef CONFIG_SECURITY_PATH
815 int security_path_mknod(const struct path
*dir
, struct dentry
*dentry
, umode_t mode
,
818 if (unlikely(IS_PRIVATE(d_backing_inode(dir
->dentry
))))
820 return call_int_hook(path_mknod
, 0, dir
, dentry
, mode
, dev
);
822 EXPORT_SYMBOL(security_path_mknod
);
824 int security_path_mkdir(const struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
826 if (unlikely(IS_PRIVATE(d_backing_inode(dir
->dentry
))))
828 return call_int_hook(path_mkdir
, 0, dir
, dentry
, mode
);
830 EXPORT_SYMBOL(security_path_mkdir
);
832 int security_path_rmdir(const struct path
*dir
, struct dentry
*dentry
)
834 if (unlikely(IS_PRIVATE(d_backing_inode(dir
->dentry
))))
836 return call_int_hook(path_rmdir
, 0, dir
, dentry
);
838 EXPORT_SYMBOL_GPL(security_path_rmdir
);
840 int security_path_unlink(const struct path
*dir
, struct dentry
*dentry
)
842 if (unlikely(IS_PRIVATE(d_backing_inode(dir
->dentry
))))
844 return call_int_hook(path_unlink
, 0, dir
, dentry
);
846 EXPORT_SYMBOL(security_path_unlink
);
848 int security_path_symlink(const struct path
*dir
, struct dentry
*dentry
,
849 const char *old_name
)
851 if (unlikely(IS_PRIVATE(d_backing_inode(dir
->dentry
))))
853 return call_int_hook(path_symlink
, 0, dir
, dentry
, old_name
);
855 EXPORT_SYMBOL_GPL(security_path_symlink
);
857 int security_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
858 struct dentry
*new_dentry
)
860 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry
))))
862 return call_int_hook(path_link
, 0, old_dentry
, new_dir
, new_dentry
);
864 EXPORT_SYMBOL_GPL(security_path_link
);
866 int security_path_rename(const struct path
*old_dir
, struct dentry
*old_dentry
,
867 const struct path
*new_dir
, struct dentry
*new_dentry
,
870 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry
)) ||
871 (d_is_positive(new_dentry
) && IS_PRIVATE(d_backing_inode(new_dentry
)))))
874 if (flags
& RENAME_EXCHANGE
) {
875 int err
= call_int_hook(path_rename
, 0, new_dir
, new_dentry
,
876 old_dir
, old_dentry
);
881 return call_int_hook(path_rename
, 0, old_dir
, old_dentry
, new_dir
,
884 EXPORT_SYMBOL(security_path_rename
);
886 int security_path_truncate(const struct path
*path
)
888 if (unlikely(IS_PRIVATE(d_backing_inode(path
->dentry
))))
890 return call_int_hook(path_truncate
, 0, path
);
892 EXPORT_SYMBOL_GPL(security_path_truncate
);
894 int security_path_chmod(const struct path
*path
, umode_t mode
)
896 if (unlikely(IS_PRIVATE(d_backing_inode(path
->dentry
))))
898 return call_int_hook(path_chmod
, 0, path
, mode
);
900 EXPORT_SYMBOL_GPL(security_path_chmod
);
902 int security_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
904 if (unlikely(IS_PRIVATE(d_backing_inode(path
->dentry
))))
906 return call_int_hook(path_chown
, 0, path
, uid
, gid
);
908 EXPORT_SYMBOL_GPL(security_path_chown
);
910 int security_path_chroot(const struct path
*path
)
912 return call_int_hook(path_chroot
, 0, path
);
916 int security_inode_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
918 if (unlikely(IS_PRIVATE(dir
)))
920 return call_int_hook(inode_create
, 0, dir
, dentry
, mode
);
922 EXPORT_SYMBOL_GPL(security_inode_create
);
924 int security_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
925 struct dentry
*new_dentry
)
927 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry
))))
929 return call_int_hook(inode_link
, 0, old_dentry
, dir
, new_dentry
);
932 int security_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
934 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
936 return call_int_hook(inode_unlink
, 0, dir
, dentry
);
939 int security_inode_symlink(struct inode
*dir
, struct dentry
*dentry
,
940 const char *old_name
)
942 if (unlikely(IS_PRIVATE(dir
)))
944 return call_int_hook(inode_symlink
, 0, dir
, dentry
, old_name
);
947 int security_inode_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
949 if (unlikely(IS_PRIVATE(dir
)))
951 return call_int_hook(inode_mkdir
, 0, dir
, dentry
, mode
);
953 EXPORT_SYMBOL_GPL(security_inode_mkdir
);
955 int security_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
957 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
959 return call_int_hook(inode_rmdir
, 0, dir
, dentry
);
962 int security_inode_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
964 if (unlikely(IS_PRIVATE(dir
)))
966 return call_int_hook(inode_mknod
, 0, dir
, dentry
, mode
, dev
);
969 int security_inode_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
970 struct inode
*new_dir
, struct dentry
*new_dentry
,
973 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry
)) ||
974 (d_is_positive(new_dentry
) && IS_PRIVATE(d_backing_inode(new_dentry
)))))
977 if (flags
& RENAME_EXCHANGE
) {
978 int err
= call_int_hook(inode_rename
, 0, new_dir
, new_dentry
,
979 old_dir
, old_dentry
);
984 return call_int_hook(inode_rename
, 0, old_dir
, old_dentry
,
985 new_dir
, new_dentry
);
988 int security_inode_readlink(struct dentry
*dentry
)
990 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
992 return call_int_hook(inode_readlink
, 0, dentry
);
994 EXPORT_SYMBOL_GPL(security_inode_readlink
);
996 int security_inode_follow_link(struct dentry
*dentry
, struct inode
*inode
,
999 if (unlikely(IS_PRIVATE(inode
)))
1001 return call_int_hook(inode_follow_link
, 0, dentry
, inode
, rcu
);
1004 int security_inode_permission(struct inode
*inode
, int mask
)
1006 if (unlikely(IS_PRIVATE(inode
)))
1008 return call_int_hook(inode_permission
, 0, inode
, mask
);
1010 EXPORT_SYMBOL_GPL(security_inode_permission
);
1012 int security_inode_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1016 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1018 ret
= call_int_hook(inode_setattr
, 0, dentry
, attr
);
1021 return evm_inode_setattr(dentry
, attr
);
1023 EXPORT_SYMBOL_GPL(security_inode_setattr
);
1025 int security_inode_getattr(const struct path
*path
)
1027 if (unlikely(IS_PRIVATE(d_backing_inode(path
->dentry
))))
1029 return call_int_hook(inode_getattr
, 0, path
);
1032 int security_inode_setxattr(struct dentry
*dentry
, const char *name
,
1033 const void *value
, size_t size
, int flags
)
1037 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1040 * SELinux and Smack integrate the cap call,
1041 * so assume that all LSMs supplying this call do so.
1043 ret
= call_int_hook(inode_setxattr
, 1, dentry
, name
, value
, size
,
1047 ret
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1050 ret
= ima_inode_setxattr(dentry
, name
, value
, size
);
1053 return evm_inode_setxattr(dentry
, name
, value
, size
);
1056 void security_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1057 const void *value
, size_t size
, int flags
)
1059 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1061 call_void_hook(inode_post_setxattr
, dentry
, name
, value
, size
, flags
);
1062 evm_inode_post_setxattr(dentry
, name
, value
, size
);
1065 int security_inode_getxattr(struct dentry
*dentry
, const char *name
)
1067 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1069 return call_int_hook(inode_getxattr
, 0, dentry
, name
);
1072 int security_inode_listxattr(struct dentry
*dentry
)
1074 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1076 return call_int_hook(inode_listxattr
, 0, dentry
);
1079 int security_inode_removexattr(struct dentry
*dentry
, const char *name
)
1083 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
1086 * SELinux and Smack integrate the cap call,
1087 * so assume that all LSMs supplying this call do so.
1089 ret
= call_int_hook(inode_removexattr
, 1, dentry
, name
);
1091 ret
= cap_inode_removexattr(dentry
, name
);
1094 ret
= ima_inode_removexattr(dentry
, name
);
1097 return evm_inode_removexattr(dentry
, name
);
1100 int security_inode_need_killpriv(struct dentry
*dentry
)
1102 return call_int_hook(inode_need_killpriv
, 0, dentry
);
1105 int security_inode_killpriv(struct dentry
*dentry
)
1107 return call_int_hook(inode_killpriv
, 0, dentry
);
1110 int security_inode_getsecurity(struct inode
*inode
, const char *name
, void **buffer
, bool alloc
)
1112 struct security_hook_list
*hp
;
1115 if (unlikely(IS_PRIVATE(inode
)))
1118 * Only one module will provide an attribute with a given name.
1120 list_for_each_entry(hp
, &security_hook_heads
.inode_getsecurity
, list
) {
1121 rc
= hp
->hook
.inode_getsecurity(inode
, name
, buffer
, alloc
);
1122 if (rc
!= -EOPNOTSUPP
)
1128 int security_inode_setsecurity(struct inode
*inode
, const char *name
, const void *value
, size_t size
, int flags
)
1130 struct security_hook_list
*hp
;
1133 if (unlikely(IS_PRIVATE(inode
)))
1136 * Only one module will provide an attribute with a given name.
1138 list_for_each_entry(hp
, &security_hook_heads
.inode_setsecurity
, list
) {
1139 rc
= hp
->hook
.inode_setsecurity(inode
, name
, value
, size
,
1141 if (rc
!= -EOPNOTSUPP
)
1147 int security_inode_listsecurity(struct inode
*inode
, char *buffer
, size_t buffer_size
)
1149 if (unlikely(IS_PRIVATE(inode
)))
1151 return call_int_hook(inode_listsecurity
, 0, inode
, buffer
, buffer_size
);
1153 EXPORT_SYMBOL(security_inode_listsecurity
);
1155 void security_inode_getsecid(struct inode
*inode
, u32
*secid
)
1157 call_void_hook(inode_getsecid
, inode
, secid
);
1160 int security_inode_copy_up(struct dentry
*src
, struct cred
**new)
1162 return call_int_hook(inode_copy_up
, 0, src
, new);
1164 EXPORT_SYMBOL(security_inode_copy_up
);
1166 int security_inode_copy_up_xattr(const char *name
)
1168 return call_int_hook(inode_copy_up_xattr
, -EOPNOTSUPP
, name
);
1170 EXPORT_SYMBOL(security_inode_copy_up_xattr
);
1172 int security_file_permission(struct file
*file
, int mask
)
1176 ret
= call_int_hook(file_permission
, 0, file
, mask
);
1180 return fsnotify_perm(file
, mask
);
1182 EXPORT_SYMBOL_GPL(security_file_permission
);
1184 int security_file_alloc(struct file
*file
)
1186 int rc
= lsm_file_alloc(file
);
1190 return call_int_hook(file_alloc_security
, 0, file
);
1193 void security_file_free(struct file
*file
)
1195 call_void_hook(file_free_security
, file
);
1197 kfree(file
->f_security
);
1198 file
->f_security
= NULL
;
1201 int security_file_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1203 return call_int_hook(file_ioctl
, 0, file
, cmd
, arg
);
1206 static inline unsigned long mmap_prot(struct file
*file
, unsigned long prot
)
1209 * Does we have PROT_READ and does the application expect
1210 * it to imply PROT_EXEC? If not, nothing to talk about...
1212 if ((prot
& (PROT_READ
| PROT_EXEC
)) != PROT_READ
)
1214 if (!(current
->personality
& READ_IMPLIES_EXEC
))
1217 * if that's an anonymous mapping, let it.
1220 return prot
| PROT_EXEC
;
1222 * ditto if it's not on noexec mount, except that on !MMU we need
1223 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
1225 if (!path_noexec(&file
->f_path
)) {
1227 if (file
->f_op
->mmap_capabilities
) {
1228 unsigned caps
= file
->f_op
->mmap_capabilities(file
);
1229 if (!(caps
& NOMMU_MAP_EXEC
))
1233 return prot
| PROT_EXEC
;
1235 /* anything on noexec mount won't get PROT_EXEC */
1239 int security_mmap_file(struct file
*file
, unsigned long prot
,
1240 unsigned long flags
)
1243 ret
= call_int_hook(mmap_file
, 0, file
, prot
,
1244 mmap_prot(file
, prot
), flags
);
1247 return ima_file_mmap(file
, prot
);
1249 EXPORT_SYMBOL_GPL(security_mmap_file
);
1251 int security_mmap_addr(unsigned long addr
)
1253 return call_int_hook(mmap_addr
, 0, addr
);
1256 int security_file_mprotect(struct vm_area_struct
*vma
, unsigned long reqprot
,
1259 return call_int_hook(file_mprotect
, 0, vma
, reqprot
, prot
);
1262 int security_file_lock(struct file
*file
, unsigned int cmd
)
1264 return call_int_hook(file_lock
, 0, file
, cmd
);
1267 int security_file_fcntl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1269 return call_int_hook(file_fcntl
, 0, file
, cmd
, arg
);
1272 void security_file_set_fowner(struct file
*file
)
1274 call_void_hook(file_set_fowner
, file
);
1277 int security_file_send_sigiotask(struct task_struct
*tsk
,
1278 struct fown_struct
*fown
, int sig
)
1280 return call_int_hook(file_send_sigiotask
, 0, tsk
, fown
, sig
);
1283 int security_file_receive(struct file
*file
)
1285 return call_int_hook(file_receive
, 0, file
);
1288 int security_file_open(struct file
*file
, const struct cred
*cred
)
1292 ret
= call_int_hook(file_open
, 0, file
, cred
);
1296 return fsnotify_perm(file
, MAY_OPEN
);
1299 int security_task_create(unsigned long clone_flags
)
1301 return call_int_hook(task_create
, 0, clone_flags
);
1304 int security_task_alloc(struct task_struct
*task
, unsigned long clone_flags
)
1306 int rc
= lsm_task_alloc(task
);
1310 return call_int_hook(task_alloc
, 0, task
, clone_flags
);
1313 void security_task_free(struct task_struct
*task
)
1315 call_void_hook(task_free
, task
);
1317 kfree(task
->security
);
1318 task
->security
= NULL
;
1321 int security_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1323 int rc
= lsm_cred_alloc(cred
, gfp
);
1328 return call_int_hook(cred_alloc_blank
, 0, cred
, gfp
);
1331 void security_cred_free(struct cred
*cred
)
1333 call_void_hook(cred_free
, cred
);
1335 kfree(cred
->security
);
1336 cred
->security
= NULL
;
1339 int security_prepare_creds(struct cred
*new, const struct cred
*old
, gfp_t gfp
)
1341 int rc
= lsm_cred_alloc(new, gfp
);
1346 return call_int_hook(cred_prepare
, 0, new, old
, gfp
);
1349 void security_transfer_creds(struct cred
*new, const struct cred
*old
)
1351 call_void_hook(cred_transfer
, new, old
);
1354 int security_kernel_act_as(struct cred
*new, u32 secid
)
1356 return call_int_hook(kernel_act_as
, 0, new, secid
);
1359 int security_kernel_create_files_as(struct cred
*new, struct inode
*inode
)
1361 return call_int_hook(kernel_create_files_as
, 0, new, inode
);
1364 int security_kernel_module_request(char *kmod_name
)
1366 return call_int_hook(kernel_module_request
, 0, kmod_name
);
1369 int security_kernel_read_file(struct file
*file
, enum kernel_read_file_id id
)
1373 ret
= call_int_hook(kernel_read_file
, 0, file
, id
);
1376 return ima_read_file(file
, id
);
1378 EXPORT_SYMBOL_GPL(security_kernel_read_file
);
1380 int security_kernel_post_read_file(struct file
*file
, char *buf
, loff_t size
,
1381 enum kernel_read_file_id id
)
1385 ret
= call_int_hook(kernel_post_read_file
, 0, file
, buf
, size
, id
);
1388 return ima_post_read_file(file
, buf
, size
, id
);
1390 EXPORT_SYMBOL_GPL(security_kernel_post_read_file
);
1392 int security_task_fix_setuid(struct cred
*new, const struct cred
*old
,
1395 return call_int_hook(task_fix_setuid
, 0, new, old
, flags
);
1398 int security_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1400 return call_int_hook(task_setpgid
, 0, p
, pgid
);
1403 int security_task_getpgid(struct task_struct
*p
)
1405 return call_int_hook(task_getpgid
, 0, p
);
1408 int security_task_getsid(struct task_struct
*p
)
1410 return call_int_hook(task_getsid
, 0, p
);
1413 void security_task_getsecid(struct task_struct
*p
, u32
*secid
)
1416 call_void_hook(task_getsecid
, p
, secid
);
1418 EXPORT_SYMBOL(security_task_getsecid
);
1420 int security_task_setnice(struct task_struct
*p
, int nice
)
1422 return call_int_hook(task_setnice
, 0, p
, nice
);
1425 int security_task_setioprio(struct task_struct
*p
, int ioprio
)
1427 return call_int_hook(task_setioprio
, 0, p
, ioprio
);
1430 int security_task_getioprio(struct task_struct
*p
)
1432 return call_int_hook(task_getioprio
, 0, p
);
1435 int security_task_prlimit(const struct cred
*cred
, const struct cred
*tcred
,
1438 return call_int_hook(task_prlimit
, 0, cred
, tcred
, flags
);
1441 int security_task_setrlimit(struct task_struct
*p
, unsigned int resource
,
1442 struct rlimit
*new_rlim
)
1444 return call_int_hook(task_setrlimit
, 0, p
, resource
, new_rlim
);
1447 int security_task_setscheduler(struct task_struct
*p
)
1449 return call_int_hook(task_setscheduler
, 0, p
);
1452 int security_task_getscheduler(struct task_struct
*p
)
1454 return call_int_hook(task_getscheduler
, 0, p
);
1457 int security_task_movememory(struct task_struct
*p
)
1459 return call_int_hook(task_movememory
, 0, p
);
1462 int security_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1465 return call_int_hook(task_kill
, 0, p
, info
, sig
, secid
);
1468 #ifdef CONFIG_SECURITY_STACKING
1469 static char *nolsm
= "-default";
1472 static bool is_registered_lsm(const char *str
, size_t size
)
1474 struct security_hook_list
*hp
;
1476 list_for_each_entry(hp
, &security_hook_heads
.getprocattr
, list
) {
1477 if (size
== strlen(hp
->lsm
) && !strncmp(str
, hp
->lsm
, size
))
1484 static bool set_lsm_of_current(const char *str
, size_t size
)
1486 char *lsm
= lsm_of_task(current
);
1488 if (is_registered_lsm(str
, size
)) {
1489 strncpy(lsm
, str
, size
);
1491 } else if (size
== NOLSMLEN
&& !strncmp(str
, nolsm
, size
)) {
1499 static int lsm_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1500 unsigned long arg4
, unsigned long arg5
)
1502 char *lsm
= lsm_of_task(current
);
1503 char buffer
[SECURITY_NAME_MAX
+ 1];
1504 __user
char *optval
= (__user
char *)arg2
;
1505 __user
int *optlen
= (__user
int *)arg3
;
1510 case PR_GET_DISPLAY_LSM
:
1512 if (lsm
[0] == '\0') {
1516 dlen
= strlen(lsm
) + 1;
1519 if (copy_to_user(optval
, lsm
, dlen
))
1521 if (put_user(dlen
, optlen
))
1524 case PR_SET_DISPLAY_LSM
:
1526 if (len
> SECURITY_NAME_MAX
)
1528 if (copy_from_user(buffer
, optval
, len
))
1531 /* verify the requested LSM is registered */
1532 if (!set_lsm_of_current(buffer
, len
))
1542 int security_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1543 unsigned long arg4
, unsigned long arg5
)
1547 struct security_hook_list
*hp
;
1549 #ifdef CONFIG_SECURITY_STACKING
1550 rc
= lsm_task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1555 list_for_each_entry(hp
, &security_hook_heads
.task_prctl
, list
) {
1556 thisrc
= hp
->hook
.task_prctl(option
, arg2
, arg3
, arg4
, arg5
);
1557 if (thisrc
!= -ENOSYS
) {
1566 void security_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
1568 call_void_hook(task_to_inode
, p
, inode
);
1571 int security_ipc_permission(struct kern_ipc_perm
*ipcp
, short flag
)
1573 return call_int_hook(ipc_permission
, 0, ipcp
, flag
);
1576 void security_ipc_getsecid(struct kern_ipc_perm
*ipcp
, u32
*secid
)
1579 call_void_hook(ipc_getsecid
, ipcp
, secid
);
1582 int security_msg_msg_alloc(struct msg_msg
*msg
)
1584 return call_int_hook(msg_msg_alloc_security
, 0, msg
);
1587 void security_msg_msg_free(struct msg_msg
*msg
)
1589 call_void_hook(msg_msg_free_security
, msg
);
1592 int security_msg_queue_alloc(struct msg_queue
*msq
)
1594 return call_int_hook(msg_queue_alloc_security
, 0, msq
);
1597 void security_msg_queue_free(struct msg_queue
*msq
)
1599 call_void_hook(msg_queue_free_security
, msq
);
1602 int security_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
1604 return call_int_hook(msg_queue_associate
, 0, msq
, msqflg
);
1607 int security_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
1609 return call_int_hook(msg_queue_msgctl
, 0, msq
, cmd
);
1612 int security_msg_queue_msgsnd(struct msg_queue
*msq
,
1613 struct msg_msg
*msg
, int msqflg
)
1615 return call_int_hook(msg_queue_msgsnd
, 0, msq
, msg
, msqflg
);
1618 int security_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
1619 struct task_struct
*target
, long type
, int mode
)
1621 return call_int_hook(msg_queue_msgrcv
, 0, msq
, msg
, target
, type
, mode
);
1624 int security_shm_alloc(struct shmid_kernel
*shp
)
1626 return call_int_hook(shm_alloc_security
, 0, shp
);
1629 void security_shm_free(struct shmid_kernel
*shp
)
1631 call_void_hook(shm_free_security
, shp
);
1634 int security_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
1636 return call_int_hook(shm_associate
, 0, shp
, shmflg
);
1639 int security_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
1641 return call_int_hook(shm_shmctl
, 0, shp
, cmd
);
1644 int security_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
, int shmflg
)
1646 return call_int_hook(shm_shmat
, 0, shp
, shmaddr
, shmflg
);
1649 int security_sem_alloc(struct sem_array
*sma
)
1651 return call_int_hook(sem_alloc_security
, 0, sma
);
1654 void security_sem_free(struct sem_array
*sma
)
1656 call_void_hook(sem_free_security
, sma
);
1659 int security_sem_associate(struct sem_array
*sma
, int semflg
)
1661 return call_int_hook(sem_associate
, 0, sma
, semflg
);
1664 int security_sem_semctl(struct sem_array
*sma
, int cmd
)
1666 return call_int_hook(sem_semctl
, 0, sma
, cmd
);
1669 int security_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
1670 unsigned nsops
, int alter
)
1672 return call_int_hook(sem_semop
, 0, sma
, sops
, nsops
, alter
);
1675 void security_d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
1677 if (unlikely(inode
&& IS_PRIVATE(inode
)))
1679 call_void_hook(d_instantiate
, dentry
, inode
);
1681 EXPORT_SYMBOL(security_d_instantiate
);
1683 int security_getprocattr(struct task_struct
*p
, const char *lsm
, char *name
,
1686 #ifdef CONFIG_SECURITY_STACKING
1687 char *speclsm
= lsm_of_task(p
);
1689 struct security_hook_list
*hp
;
1696 * "context" requires work here in addition to what
1697 * the modules provide.
1699 if (strcmp(name
, "context") == 0) {
1702 list_for_each_entry(hp
,
1703 &security_hook_heads
.getprocattr
, list
) {
1704 if (lsm
!= NULL
&& strcmp(lsm
, hp
->lsm
))
1706 trc
= hp
->hook
.getprocattr(p
, "context", &vp
);
1714 if (*value
== NULL
) {
1717 cp
= kasprintf(GFP_KERNEL
, "%s,%s", *value
, vp
);
1729 return strlen(*value
);
1731 } else if (strcmp(name
, "display_lsm") == 0) {
1732 *value
= kstrdup(current
->security
, GFP_KERNEL
);
1735 return strlen(*value
);
1738 list_for_each_entry(hp
, &security_hook_heads
.getprocattr
, list
) {
1739 if (lsm
!= NULL
&& strcmp(lsm
, hp
->lsm
))
1741 #ifdef CONFIG_SECURITY_STACKING
1742 if (!lsm
&& speclsm
&& speclsm
[0] && strcmp(speclsm
, hp
->lsm
))
1745 rc
= hp
->hook
.getprocattr(p
, name
, value
);
1752 int security_setprocattr(const char *lsm
, const char *name
, void *value
,
1755 #ifdef CONFIG_SECURITY_STACKING
1756 char *speclsm
= lsm_of_task(current
);
1757 struct lsm_value
*lsm_value
= NULL
;
1762 struct security_hook_list
*hp
;
1771 * If lsm is NULL look at all the modules to find one
1772 * that processes name. If lsm is not NULL only look at
1775 * "context" is handled directly here.
1777 if (strcmp(name
, "context") == 0) {
1779 temp
= kmemdup(value
, size
+ 1, GFP_KERNEL
);
1784 cp
= strrchr(temp
, '\'');
1789 #ifdef CONFIG_SECURITY_STACKING
1790 lsm_value
= kzalloc(sizeof(*lsm_value
) * LSM_MAX_MAJOR
,
1797 count
= lsm_parse_context(temp
, lsm_value
);
1801 for (count
--; count
>= 0; count
--) {
1802 list_for_each_entry(hp
,
1803 &security_hook_heads
.setprocattr
, list
) {
1805 if (lsm
&& strcmp(lsm
, hp
->lsm
))
1807 if (!strcmp(hp
->lsm
, lsm_value
[count
].lsm
)) {
1808 rc
= hp
->hook
.setprocattr("context",
1809 lsm_value
[count
].data
,
1810 strlen(lsm_value
[count
].data
));
1814 if (rc
< 0 || (lsm
&& rc
>0))
1817 #else /* CONFIG_SECURITY_STACKING */
1818 cp
= strstr(temp
, "='");
1822 tvalue
= strchr(cp
+ 2, '\'');
1825 list_for_each_entry(hp
, &security_hook_heads
.setprocattr
,
1827 if (lsm
== NULL
|| !strcmp(lsm
, hp
->lsm
)) {
1828 rc
= hp
->hook
.setprocattr(name
, tvalue
, size
);
1832 #endif /* CONFIG_SECURITY_STACKING */
1835 #ifdef CONFIG_SECURITY_STACKING
1841 } else if (strcmp(name
, "display_lsm") == 0) {
1842 #ifdef CONFIG_SECURITY_STACKING
1843 if (set_lsm_of_current(value
, size
))
1849 list_for_each_entry(hp
, &security_hook_heads
.setprocattr
, list
) {
1850 if (lsm
&& strcmp(lsm
, hp
->lsm
))
1852 #ifdef CONFIG_SECURITY_STACKING
1853 if (!lsm
&& speclsm
&& speclsm
[0] && strcmp(speclsm
, hp
->lsm
))
1856 rc
= hp
->hook
.setprocattr(name
, value
, size
);
1863 int security_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
1865 return call_int_hook(netlink_send
, 0, sk
, skb
);
1868 int security_ismaclabel(const char *name
)
1870 return call_int_hook(ismaclabel
, 0, name
);
1872 EXPORT_SYMBOL(security_ismaclabel
);
1874 int security_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
1876 return call_int_hook(secid_to_secctx
, -EOPNOTSUPP
, secid
, secdata
,
1879 EXPORT_SYMBOL(security_secid_to_secctx
);
1881 int security_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
1884 return call_int_hook(secctx_to_secid
, 0, secdata
, seclen
, secid
);
1886 EXPORT_SYMBOL(security_secctx_to_secid
);
1888 void security_release_secctx(char *secdata
, u32 seclen
)
1890 #ifdef CONFIG_SECURITY_STACKING
1891 char *speclsm
= lsm_of_task(current
);
1893 struct security_hook_list
*hp
;
1895 list_for_each_entry(hp
, &security_hook_heads
.release_secctx
, list
) {
1896 #ifdef CONFIG_SECURITY_STACKING
1897 if (speclsm
[0] && strcmp(hp
->lsm
, speclsm
))
1900 hp
->hook
.release_secctx(secdata
, seclen
);
1904 EXPORT_SYMBOL(security_release_secctx
);
1906 void security_inode_invalidate_secctx(struct inode
*inode
)
1908 call_void_hook(inode_invalidate_secctx
, inode
);
1910 EXPORT_SYMBOL(security_inode_invalidate_secctx
);
1912 int security_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
1914 return call_int_hook(inode_notifysecctx
, 0, inode
, ctx
, ctxlen
);
1916 EXPORT_SYMBOL(security_inode_notifysecctx
);
1918 int security_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
1920 return call_int_hook(inode_setsecctx
, 0, dentry
, ctx
, ctxlen
);
1922 EXPORT_SYMBOL(security_inode_setsecctx
);
1924 int security_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
1926 return call_int_hook(inode_getsecctx
, -EOPNOTSUPP
, inode
, ctx
, ctxlen
);
1928 EXPORT_SYMBOL(security_inode_getsecctx
);
1930 #ifdef CONFIG_SECURITY_NETWORK
1932 int security_unix_stream_connect(struct sock
*sock
, struct sock
*other
, struct sock
*newsk
)
1934 return call_int_hook(unix_stream_connect
, 0, sock
, other
, newsk
);
1936 EXPORT_SYMBOL(security_unix_stream_connect
);
1938 int security_unix_may_send(struct socket
*sock
, struct socket
*other
)
1940 return call_int_hook(unix_may_send
, 0, sock
, other
);
1942 EXPORT_SYMBOL(security_unix_may_send
);
1944 int security_socket_create(int family
, int type
, int protocol
, int kern
)
1946 return call_int_hook(socket_create
, 0, family
, type
, protocol
, kern
);
1949 int security_socket_post_create(struct socket
*sock
, int family
,
1950 int type
, int protocol
, int kern
)
1952 return call_int_hook(socket_post_create
, 0, sock
, family
, type
,
1956 int security_socket_bind(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
1958 return call_int_hook(socket_bind
, 0, sock
, address
, addrlen
);
1961 int security_socket_connect(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
1963 return call_int_hook(socket_connect
, 0, sock
, address
, addrlen
);
1966 int security_socket_listen(struct socket
*sock
, int backlog
)
1968 return call_int_hook(socket_listen
, 0, sock
, backlog
);
1971 int security_socket_accept(struct socket
*sock
, struct socket
*newsock
)
1973 return call_int_hook(socket_accept
, 0, sock
, newsock
);
1976 int security_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int size
)
1978 return call_int_hook(socket_sendmsg
, 0, sock
, msg
, size
);
1981 int security_socket_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
1982 int size
, int flags
)
1984 return call_int_hook(socket_recvmsg
, 0, sock
, msg
, size
, flags
);
1987 int security_socket_getsockname(struct socket
*sock
)
1989 return call_int_hook(socket_getsockname
, 0, sock
);
1992 int security_socket_getpeername(struct socket
*sock
)
1994 return call_int_hook(socket_getpeername
, 0, sock
);
1997 int security_socket_getsockopt(struct socket
*sock
, int level
, int optname
)
1999 return call_int_hook(socket_getsockopt
, 0, sock
, level
, optname
);
2002 int security_socket_setsockopt(struct socket
*sock
, int level
, int optname
)
2004 return call_int_hook(socket_setsockopt
, 0, sock
, level
, optname
);
2007 int security_socket_shutdown(struct socket
*sock
, int how
)
2009 return call_int_hook(socket_shutdown
, 0, sock
, how
);
2012 int security_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
2014 return call_int_hook(socket_sock_rcv_skb
, 0, sk
, skb
);
2016 EXPORT_SYMBOL(security_sock_rcv_skb
);
2018 int security_socket_getpeersec_stream(struct socket
*sock
, char __user
*optval
,
2019 int __user
*optlen
, unsigned len
)
2021 #ifdef CONFIG_SECURITY_STACKING
2022 struct security_hook_list
*hp
;
2023 char *lsm
= lsm_of_task(current
);
2025 list_for_each_entry(hp
, &security_hook_heads
.socket_getpeersec_stream
,
2027 if (!lsm
|| !lsm
[0] || !strcmp(lsm
, hp
->lsm
))
2028 return hp
->hook
.socket_getpeersec_stream(sock
, optval
,
2031 return -ENOPROTOOPT
;
2033 return call_int_hook(socket_getpeersec_stream
, -ENOPROTOOPT
, sock
,
2034 optval
, optlen
, len
);
2038 int security_socket_getpeersec_dgram(struct socket
*sock
, struct sk_buff
*skb
, u32
*secid
)
2040 return call_int_hook(socket_getpeersec_dgram
, -ENOPROTOOPT
, sock
,
2043 EXPORT_SYMBOL(security_socket_getpeersec_dgram
);
2045 int security_sk_alloc(struct sock
*sk
, int family
, gfp_t priority
)
2047 return call_int_hook(sk_alloc_security
, 0, sk
, family
, priority
);
2050 void security_sk_free(struct sock
*sk
)
2052 call_void_hook(sk_free_security
, sk
);
2055 void security_sk_clone(const struct sock
*sk
, struct sock
*newsk
)
2057 call_void_hook(sk_clone_security
, sk
, newsk
);
2059 EXPORT_SYMBOL(security_sk_clone
);
2061 void security_sk_classify_flow(struct sock
*sk
, struct flowi
*fl
)
2063 call_void_hook(sk_getsecid
, sk
, &fl
->flowi_secid
);
2065 EXPORT_SYMBOL(security_sk_classify_flow
);
2067 void security_req_classify_flow(const struct request_sock
*req
, struct flowi
*fl
)
2069 call_void_hook(req_classify_flow
, req
, fl
);
2071 EXPORT_SYMBOL(security_req_classify_flow
);
2073 void security_sock_graft(struct sock
*sk
, struct socket
*parent
)
2075 call_void_hook(sock_graft
, sk
, parent
);
2077 EXPORT_SYMBOL(security_sock_graft
);
2079 int security_inet_conn_request(struct sock
*sk
,
2080 struct sk_buff
*skb
, struct request_sock
*req
)
2082 return call_int_hook(inet_conn_request
, 0, sk
, skb
, req
);
2084 EXPORT_SYMBOL(security_inet_conn_request
);
2086 void security_inet_csk_clone(struct sock
*newsk
,
2087 const struct request_sock
*req
)
2089 call_void_hook(inet_csk_clone
, newsk
, req
);
2092 void security_inet_conn_established(struct sock
*sk
,
2093 struct sk_buff
*skb
)
2095 call_void_hook(inet_conn_established
, sk
, skb
);
2098 int security_secmark_relabel_packet(u32 secid
)
2100 return call_int_hook(secmark_relabel_packet
, 0, secid
);
2102 EXPORT_SYMBOL(security_secmark_relabel_packet
);
2104 void security_secmark_refcount_inc(void)
2106 call_void_hook(secmark_refcount_inc
);
2108 EXPORT_SYMBOL(security_secmark_refcount_inc
);
2110 void security_secmark_refcount_dec(void)
2112 call_void_hook(secmark_refcount_dec
);
2114 EXPORT_SYMBOL(security_secmark_refcount_dec
);
2116 int security_tun_dev_alloc_security(void **security
)
2118 return call_int_hook(tun_dev_alloc_security
, 0, security
);
2120 EXPORT_SYMBOL(security_tun_dev_alloc_security
);
2122 void security_tun_dev_free_security(void *security
)
2124 call_void_hook(tun_dev_free_security
, security
);
2126 EXPORT_SYMBOL(security_tun_dev_free_security
);
2128 int security_tun_dev_create(void)
2130 return call_int_hook(tun_dev_create
, 0);
2132 EXPORT_SYMBOL(security_tun_dev_create
);
2134 int security_tun_dev_attach_queue(void *security
)
2136 return call_int_hook(tun_dev_attach_queue
, 0, security
);
2138 EXPORT_SYMBOL(security_tun_dev_attach_queue
);
2140 int security_tun_dev_attach(struct sock
*sk
, void *security
)
2142 return call_int_hook(tun_dev_attach
, 0, sk
, security
);
2144 EXPORT_SYMBOL(security_tun_dev_attach
);
2146 int security_tun_dev_open(void *security
)
2148 return call_int_hook(tun_dev_open
, 0, security
);
2150 EXPORT_SYMBOL(security_tun_dev_open
);
2152 #endif /* CONFIG_SECURITY_NETWORK */
2154 #ifdef CONFIG_SECURITY_INFINIBAND
2156 int security_ib_pkey_access(void *sec
, u64 subnet_prefix
, u16 pkey
)
2158 return call_int_hook(ib_pkey_access
, 0, sec
, subnet_prefix
, pkey
);
2160 EXPORT_SYMBOL(security_ib_pkey_access
);
2162 int security_ib_endport_manage_subnet(void *sec
, const char *dev_name
, u8 port_num
)
2164 return call_int_hook(ib_endport_manage_subnet
, 0, sec
, dev_name
, port_num
);
2166 EXPORT_SYMBOL(security_ib_endport_manage_subnet
);
2168 int security_ib_alloc_security(void **sec
)
2170 return call_int_hook(ib_alloc_security
, 0, sec
);
2172 EXPORT_SYMBOL(security_ib_alloc_security
);
2174 void security_ib_free_security(void *sec
)
2176 call_void_hook(ib_free_security
, sec
);
2178 EXPORT_SYMBOL(security_ib_free_security
);
2179 #endif /* CONFIG_SECURITY_INFINIBAND */
2181 #ifdef CONFIG_SECURITY_NETWORK_XFRM
2183 int security_xfrm_policy_alloc(struct xfrm_sec_ctx
**ctxp
,
2184 struct xfrm_user_sec_ctx
*sec_ctx
,
2187 return call_int_hook(xfrm_policy_alloc_security
, 0, ctxp
, sec_ctx
, gfp
);
2189 EXPORT_SYMBOL(security_xfrm_policy_alloc
);
2191 int security_xfrm_policy_clone(struct xfrm_sec_ctx
*old_ctx
,
2192 struct xfrm_sec_ctx
**new_ctxp
)
2194 return call_int_hook(xfrm_policy_clone_security
, 0, old_ctx
, new_ctxp
);
2197 void security_xfrm_policy_free(struct xfrm_sec_ctx
*ctx
)
2199 call_void_hook(xfrm_policy_free_security
, ctx
);
2201 EXPORT_SYMBOL(security_xfrm_policy_free
);
2203 int security_xfrm_policy_delete(struct xfrm_sec_ctx
*ctx
)
2205 return call_int_hook(xfrm_policy_delete_security
, 0, ctx
);
2208 int security_xfrm_state_alloc(struct xfrm_state
*x
,
2209 struct xfrm_user_sec_ctx
*sec_ctx
)
2211 return call_int_hook(xfrm_state_alloc
, 0, x
, sec_ctx
);
2213 EXPORT_SYMBOL(security_xfrm_state_alloc
);
2215 int security_xfrm_state_alloc_acquire(struct xfrm_state
*x
,
2216 struct xfrm_sec_ctx
*polsec
, u32 secid
)
2218 return call_int_hook(xfrm_state_alloc_acquire
, 0, x
, polsec
, secid
);
2221 int security_xfrm_state_delete(struct xfrm_state
*x
)
2223 return call_int_hook(xfrm_state_delete_security
, 0, x
);
2225 EXPORT_SYMBOL(security_xfrm_state_delete
);
2227 void security_xfrm_state_free(struct xfrm_state
*x
)
2229 call_void_hook(xfrm_state_free_security
, x
);
2232 int security_xfrm_policy_lookup(struct xfrm_sec_ctx
*ctx
, u32 fl_secid
, u8 dir
)
2234 return call_int_hook(xfrm_policy_lookup
, 0, ctx
, fl_secid
, dir
);
2237 int security_xfrm_state_pol_flow_match(struct xfrm_state
*x
,
2238 struct xfrm_policy
*xp
,
2239 const struct flowi
*fl
)
2241 struct security_hook_list
*hp
;
2245 * Since this function is expected to return 0 or 1, the judgment
2246 * becomes difficult if multiple LSMs supply this call. Fortunately,
2247 * we can use the first LSM's judgment because currently only SELinux
2248 * supplies this call.
2250 * For speed optimization, we explicitly break the loop rather than
2253 list_for_each_entry(hp
, &security_hook_heads
.xfrm_state_pol_flow_match
,
2255 rc
= hp
->hook
.xfrm_state_pol_flow_match(x
, xp
, fl
);
2261 int security_xfrm_decode_session(struct sk_buff
*skb
, u32
*secid
)
2263 return call_int_hook(xfrm_decode_session
, 0, skb
, secid
, 1);
2266 void security_skb_classify_flow(struct sk_buff
*skb
, struct flowi
*fl
)
2268 int rc
= call_int_hook(xfrm_decode_session
, 0, skb
, &fl
->flowi_secid
,
2273 EXPORT_SYMBOL(security_skb_classify_flow
);
2275 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
2279 int security_key_alloc(struct key
*key
, const struct cred
*cred
,
2280 unsigned long flags
)
2282 return call_int_hook(key_alloc
, 0, key
, cred
, flags
);
2285 void security_key_free(struct key
*key
)
2287 call_void_hook(key_free
, key
);
2290 int security_key_permission(key_ref_t key_ref
,
2291 const struct cred
*cred
, unsigned perm
)
2293 return call_int_hook(key_permission
, 0, key_ref
, cred
, perm
);
2296 int security_key_getsecurity(struct key
*key
, char **_buffer
)
2299 return call_int_hook(key_getsecurity
, 0, key
, _buffer
);
2302 #endif /* CONFIG_KEYS */
2306 int security_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **lsmrule
)
2308 return call_int_hook(audit_rule_init
, 0, field
, op
, rulestr
, lsmrule
);
2311 int security_audit_rule_known(struct audit_krule
*krule
)
2313 return call_int_hook(audit_rule_known
, 0, krule
);
2316 void security_audit_rule_free(void *lsmrule
)
2318 call_void_hook(audit_rule_free
, lsmrule
);
2321 int security_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *lsmrule
,
2322 struct audit_context
*actx
)
2324 return call_int_hook(audit_rule_match
, 0, secid
, field
, op
, lsmrule
,
2327 #endif /* CONFIG_AUDIT */