diff options
author | Ondrej Mosnacek <omosnace@redhat.com> | 2019-02-22 09:57:17 -0500 |
---|---|---|
committer | Paul Moore <paul@paul-moore.com> | 2019-03-20 22:07:45 -0400 |
commit | ec882da5cda911e799b8a5ede94d099fdc0c656b (patch) | |
tree | b7a09e2138b42635095aa4c43022fd53d526ab77 /security | |
parent | b230d5aba2d1a7b0636408889a75bf9eae6b8bc7 (diff) |
selinux: implement the kernfs_init_security hook
The hook applies the same logic as selinux_determine_inode_label(), with
the exception of the super_block handling, which will be enforced on the
actual inodes later by other hooks.
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
[PM: minor merge fixes]
Signed-off-by: Paul Moore <paul@paul-moore.com>
Diffstat (limited to 'security')
-rw-r--r-- | security/selinux/hooks.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 085409b36794..ab4b049daf17 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c | |||
@@ -89,6 +89,8 @@ | |||
89 | #include <linux/msg.h> | 89 | #include <linux/msg.h> |
90 | #include <linux/shm.h> | 90 | #include <linux/shm.h> |
91 | #include <linux/bpf.h> | 91 | #include <linux/bpf.h> |
92 | #include <linux/kernfs.h> | ||
93 | #include <linux/stringhash.h> /* for hashlen_string() */ | ||
92 | #include <uapi/linux/mount.h> | 94 | #include <uapi/linux/mount.h> |
93 | 95 | ||
94 | #include "avc.h" | 96 | #include "avc.h" |
@@ -3382,6 +3384,68 @@ static int selinux_inode_copy_up_xattr(const char *name) | |||
3382 | return -EOPNOTSUPP; | 3384 | return -EOPNOTSUPP; |
3383 | } | 3385 | } |
3384 | 3386 | ||
3387 | /* kernfs node operations */ | ||
3388 | |||
3389 | int selinux_kernfs_init_security(struct kernfs_node *kn_dir, | ||
3390 | struct kernfs_node *kn) | ||
3391 | { | ||
3392 | const struct task_security_struct *tsec = current_security(); | ||
3393 | u32 parent_sid, newsid, clen; | ||
3394 | int rc; | ||
3395 | char *context; | ||
3396 | |||
3397 | rc = kernfs_security_xattr_get(kn_dir, XATTR_SELINUX_SUFFIX, NULL, 0); | ||
3398 | if (rc == -ENODATA) | ||
3399 | return 0; | ||
3400 | else if (rc < 0) | ||
3401 | return rc; | ||
3402 | |||
3403 | clen = (u32)rc; | ||
3404 | context = kmalloc(clen, GFP_KERNEL); | ||
3405 | if (!context) | ||
3406 | return -ENOMEM; | ||
3407 | |||
3408 | rc = kernfs_security_xattr_get(kn_dir, XATTR_SELINUX_SUFFIX, context, | ||
3409 | clen); | ||
3410 | if (rc < 0) { | ||
3411 | kfree(context); | ||
3412 | return rc; | ||
3413 | } | ||
3414 | |||
3415 | rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid, | ||
3416 | GFP_KERNEL); | ||
3417 | kfree(context); | ||
3418 | if (rc) | ||
3419 | return rc; | ||
3420 | |||
3421 | if (tsec->create_sid) { | ||
3422 | newsid = tsec->create_sid; | ||
3423 | } else { | ||
3424 | u16 secclass = inode_mode_to_security_class(kn->mode); | ||
3425 | struct qstr q; | ||
3426 | |||
3427 | q.name = kn->name; | ||
3428 | q.hash_len = hashlen_string(kn_dir, kn->name); | ||
3429 | |||
3430 | rc = security_transition_sid(&selinux_state, tsec->sid, | ||
3431 | parent_sid, secclass, &q, | ||
3432 | &newsid); | ||
3433 | if (rc) | ||
3434 | return rc; | ||
3435 | } | ||
3436 | |||
3437 | rc = security_sid_to_context_force(&selinux_state, newsid, | ||
3438 | &context, &clen); | ||
3439 | if (rc) | ||
3440 | return rc; | ||
3441 | |||
3442 | rc = kernfs_security_xattr_set(kn, XATTR_SELINUX_SUFFIX, context, clen, | ||
3443 | XATTR_CREATE); | ||
3444 | kfree(context); | ||
3445 | return rc; | ||
3446 | } | ||
3447 | |||
3448 | |||
3385 | /* file security operations */ | 3449 | /* file security operations */ |
3386 | 3450 | ||
3387 | static int selinux_revalidate_file_permission(struct file *file, int mask) | 3451 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
@@ -6730,6 +6794,8 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { | |||
6730 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), | 6794 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), |
6731 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), | 6795 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), |
6732 | 6796 | ||
6797 | LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), | ||
6798 | |||
6733 | LSM_HOOK_INIT(file_permission, selinux_file_permission), | 6799 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
6734 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), | 6800 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
6735 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), | 6801 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |