2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
46 #define TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
54 LIST_HEAD(smk_ipv6_port_list
);
55 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
56 static struct kmem_cache
*smack_inode_cache
;
59 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
60 static void smk_bu_mode(int mode
, char *s
)
70 if (mode
& MAY_APPEND
)
72 if (mode
& MAY_TRANSMUTE
)
82 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
83 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
84 struct smack_known
*oskp
, int mode
, int rc
)
86 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
91 smk_bu_mode(mode
, acc
);
92 pr_info("Smack Bringup: (%s %s %s) %s\n",
93 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
97 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
100 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
101 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
104 struct task_smack
*tsp
= current_security();
105 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
110 smk_bu_mode(mode
, acc
);
111 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
112 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
113 acc
, current
->comm
, note
);
117 #define smk_bu_current(note, oskp, mode, RC) (RC)
120 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
121 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
123 struct task_smack
*tsp
= current_security();
124 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
125 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
130 smk_bu_mode(mode
, acc
);
131 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
132 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
133 current
->comm
, otp
->comm
);
137 #define smk_bu_task(otp, mode, RC) (RC)
140 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
141 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
143 struct task_smack
*tsp
= current_security();
144 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
149 smk_bu_mode(mode
, acc
);
150 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
151 tsp
->smk_task
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
152 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
156 #define smk_bu_inode(inode, mode, RC) (RC)
159 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
160 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
162 struct task_smack
*tsp
= current_security();
163 struct smack_known
*sskp
= tsp
->smk_task
;
164 struct inode
*inode
= file_inode(file
);
165 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
170 smk_bu_mode(mode
, acc
);
171 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
172 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
173 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
178 #define smk_bu_file(file, mode, RC) (RC)
181 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
182 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
185 struct task_smack
*tsp
= cred
->security
;
186 struct smack_known
*sskp
= tsp
->smk_task
;
187 struct inode
*inode
= file
->f_inode
;
188 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
193 smk_bu_mode(mode
, acc
);
194 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
195 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
196 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
201 #define smk_bu_credfile(cred, file, mode, RC) (RC)
205 * smk_fetch - Fetch the smack label from a file.
206 * @name: type of the label (attribute)
207 * @ip: a pointer to the inode
208 * @dp: a pointer to the dentry
210 * Returns a pointer to the master list entry for the Smack label
211 * or NULL if there was no label to fetch.
213 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
218 struct smack_known
*skp
= NULL
;
220 if (ip
->i_op
->getxattr
== NULL
)
223 buffer
= kzalloc(SMK_LONGLABEL
, GFP_KERNEL
);
227 rc
= ip
->i_op
->getxattr(dp
, name
, buffer
, SMK_LONGLABEL
);
229 skp
= smk_import_entry(buffer
, rc
);
237 * new_inode_smack - allocate an inode security blob
238 * @skp: a pointer to the Smack label entry to use in the blob
240 * Returns the new blob or NULL if there's no memory available
242 struct inode_smack
*new_inode_smack(struct smack_known
*skp
)
244 struct inode_smack
*isp
;
246 isp
= kmem_cache_zalloc(smack_inode_cache
, GFP_NOFS
);
250 isp
->smk_inode
= skp
;
252 mutex_init(&isp
->smk_lock
);
258 * new_task_smack - allocate a task security blob
259 * @task: a pointer to the Smack label for the running task
260 * @forked: a pointer to the Smack label for the forked task
261 * @gfp: type of the memory for the allocation
263 * Returns the new blob or NULL if there's no memory available
265 static struct task_smack
*new_task_smack(struct smack_known
*task
,
266 struct smack_known
*forked
, gfp_t gfp
)
268 struct task_smack
*tsp
;
270 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
274 tsp
->smk_task
= task
;
275 tsp
->smk_forked
= forked
;
276 INIT_LIST_HEAD(&tsp
->smk_rules
);
277 mutex_init(&tsp
->smk_rules_lock
);
283 * smk_copy_rules - copy a rule set
284 * @nhead: new rules header pointer
285 * @ohead: old rules header pointer
286 * @gfp: type of the memory for the allocation
288 * Returns 0 on success, -ENOMEM on error
290 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
293 struct smack_rule
*nrp
;
294 struct smack_rule
*orp
;
297 INIT_LIST_HEAD(nhead
);
299 list_for_each_entry_rcu(orp
, ohead
, list
) {
300 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
306 list_add_rcu(&nrp
->list
, nhead
);
312 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
313 * @mode - input mode in form of PTRACE_MODE_*
315 * Returns a converted MAY_* mode usable by smack rules
317 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
320 case PTRACE_MODE_READ
:
322 case PTRACE_MODE_ATTACH
:
323 return MAY_READWRITE
;
330 * smk_ptrace_rule_check - helper for ptrace access
331 * @tracer: tracer process
332 * @tracee_known: label entry of the process that's about to be traced
333 * @mode: ptrace attachment mode (PTRACE_MODE_*)
334 * @func: name of the function that called us, used for audit
336 * Returns 0 on access granted, -error on error
338 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
339 struct smack_known
*tracee_known
,
340 unsigned int mode
, const char *func
)
343 struct smk_audit_info ad
, *saip
= NULL
;
344 struct task_smack
*tsp
;
345 struct smack_known
*tracer_known
;
347 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
348 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
349 smk_ad_setfield_u_tsk(&ad
, tracer
);
354 tsp
= __task_cred(tracer
)->security
;
355 tracer_known
= smk_of_task(tsp
);
357 if ((mode
& PTRACE_MODE_ATTACH
) &&
358 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
359 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
360 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
362 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
364 else if (capable(CAP_SYS_PTRACE
))
370 smack_log(tracer_known
->smk_known
,
371 tracee_known
->smk_known
,
378 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
379 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
387 * We he, that is fun!
391 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
392 * @ctp: child task pointer
393 * @mode: ptrace attachment mode (PTRACE_MODE_*)
395 * Returns 0 if access is OK, an error code otherwise
397 * Do the capability checks.
399 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
402 struct smack_known
*skp
;
404 rc
= cap_ptrace_access_check(ctp
, mode
);
408 skp
= smk_of_task_struct(ctp
);
410 rc
= smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
415 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
416 * @ptp: parent task pointer
418 * Returns 0 if access is OK, an error code otherwise
420 * Do the capability checks, and require PTRACE_MODE_ATTACH.
422 static int smack_ptrace_traceme(struct task_struct
*ptp
)
425 struct smack_known
*skp
;
427 rc
= cap_ptrace_traceme(ptp
);
431 skp
= smk_of_task(current_security());
433 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
438 * smack_syslog - Smack approval on syslog
439 * @type: message type
441 * Returns 0 on success, error code otherwise.
443 static int smack_syslog(int typefrom_file
)
446 struct smack_known
*skp
= smk_of_current();
448 if (smack_privileged(CAP_MAC_OVERRIDE
))
451 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
463 * smack_sb_alloc_security - allocate a superblock blob
464 * @sb: the superblock getting the blob
466 * Returns 0 on success or -ENOMEM on error.
468 static int smack_sb_alloc_security(struct super_block
*sb
)
470 struct superblock_smack
*sbsp
;
472 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
477 sbsp
->smk_root
= &smack_known_floor
;
478 sbsp
->smk_default
= &smack_known_floor
;
479 sbsp
->smk_floor
= &smack_known_floor
;
480 sbsp
->smk_hat
= &smack_known_hat
;
482 * smk_initialized will be zero from kzalloc.
484 sb
->s_security
= sbsp
;
490 * smack_sb_free_security - free a superblock blob
491 * @sb: the superblock getting the blob
494 static void smack_sb_free_security(struct super_block
*sb
)
496 kfree(sb
->s_security
);
497 sb
->s_security
= NULL
;
501 * smack_sb_copy_data - copy mount options data for processing
502 * @orig: where to start
503 * @smackopts: mount options string
505 * Returns 0 on success or -ENOMEM on error.
507 * Copy the Smack specific mount options out of the mount
510 static int smack_sb_copy_data(char *orig
, char *smackopts
)
512 char *cp
, *commap
, *otheropts
, *dp
;
514 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
515 if (otheropts
== NULL
)
518 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
519 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
521 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
523 else if (strstr(cp
, SMK_FSHAT
) == cp
)
525 else if (strstr(cp
, SMK_FSROOT
) == cp
)
527 else if (strstr(cp
, SMK_FSTRANS
) == cp
)
532 commap
= strchr(cp
, ',');
541 strcpy(orig
, otheropts
);
542 free_page((unsigned long)otheropts
);
548 * smack_sb_kern_mount - Smack specific mount processing
549 * @sb: the file system superblock
550 * @flags: the mount flags
551 * @data: the smack mount options
553 * Returns 0 on success, an error code on failure
555 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
557 struct dentry
*root
= sb
->s_root
;
558 struct inode
*inode
= root
->d_inode
;
559 struct superblock_smack
*sp
= sb
->s_security
;
560 struct inode_smack
*isp
;
561 struct smack_known
*skp
;
567 if (sp
->smk_initialized
)
570 sp
->smk_initialized
= 1;
572 for (op
= data
; op
!= NULL
; op
= commap
) {
573 commap
= strchr(op
, ',');
577 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
578 op
+= strlen(SMK_FSHAT
);
579 skp
= smk_import_entry(op
, 0);
584 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
585 op
+= strlen(SMK_FSFLOOR
);
586 skp
= smk_import_entry(op
, 0);
591 } else if (strncmp(op
, SMK_FSDEFAULT
,
592 strlen(SMK_FSDEFAULT
)) == 0) {
593 op
+= strlen(SMK_FSDEFAULT
);
594 skp
= smk_import_entry(op
, 0);
596 sp
->smk_default
= skp
;
599 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
600 op
+= strlen(SMK_FSROOT
);
601 skp
= smk_import_entry(op
, 0);
606 } else if (strncmp(op
, SMK_FSTRANS
, strlen(SMK_FSTRANS
)) == 0) {
607 op
+= strlen(SMK_FSTRANS
);
608 skp
= smk_import_entry(op
, 0);
617 if (!smack_privileged(CAP_MAC_ADMIN
)) {
619 * Unprivileged mounts don't get to specify Smack values.
624 * Unprivileged mounts get root and default from the caller.
626 skp
= smk_of_current();
628 sp
->smk_default
= skp
;
631 * Initialize the root inode.
633 isp
= inode
->i_security
;
635 isp
= new_inode_smack(sp
->smk_root
);
638 inode
->i_security
= isp
;
640 isp
->smk_inode
= sp
->smk_root
;
643 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
649 * smack_sb_statfs - Smack check on statfs
650 * @dentry: identifies the file system in question
652 * Returns 0 if current can read the floor of the filesystem,
653 * and error code otherwise
655 static int smack_sb_statfs(struct dentry
*dentry
)
657 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
659 struct smk_audit_info ad
;
661 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
662 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
664 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
665 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
674 * smack_bprm_set_creds - set creds for exec
675 * @bprm: the exec information
677 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
679 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
681 struct inode
*inode
= file_inode(bprm
->file
);
682 struct task_smack
*bsp
= bprm
->cred
->security
;
683 struct inode_smack
*isp
;
686 rc
= cap_bprm_set_creds(bprm
);
690 if (bprm
->cred_prepared
)
693 isp
= inode
->i_security
;
694 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
697 if (bprm
->unsafe
& (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
698 struct task_struct
*tracer
;
702 tracer
= ptrace_parent(current
);
703 if (likely(tracer
!= NULL
))
704 rc
= smk_ptrace_rule_check(tracer
,
712 } else if (bprm
->unsafe
)
715 bsp
->smk_task
= isp
->smk_task
;
716 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
722 * smack_bprm_committing_creds - Prepare to install the new credentials
725 * @bprm: binprm for exec
727 static void smack_bprm_committing_creds(struct linux_binprm
*bprm
)
729 struct task_smack
*bsp
= bprm
->cred
->security
;
731 if (bsp
->smk_task
!= bsp
->smk_forked
)
732 current
->pdeath_signal
= 0;
736 * smack_bprm_secureexec - Return the decision to use secureexec.
737 * @bprm: binprm for exec
739 * Returns 0 on success.
741 static int smack_bprm_secureexec(struct linux_binprm
*bprm
)
743 struct task_smack
*tsp
= current_security();
744 int ret
= cap_bprm_secureexec(bprm
);
746 if (!ret
&& (tsp
->smk_task
!= tsp
->smk_forked
))
757 * smack_inode_alloc_security - allocate an inode blob
758 * @inode: the inode in need of a blob
760 * Returns 0 if it gets a blob, -ENOMEM otherwise
762 static int smack_inode_alloc_security(struct inode
*inode
)
764 struct smack_known
*skp
= smk_of_current();
766 inode
->i_security
= new_inode_smack(skp
);
767 if (inode
->i_security
== NULL
)
773 * smack_inode_free_security - free an inode blob
774 * @inode: the inode with a blob
776 * Clears the blob pointer in inode
778 static void smack_inode_free_security(struct inode
*inode
)
780 kmem_cache_free(smack_inode_cache
, inode
->i_security
);
781 inode
->i_security
= NULL
;
785 * smack_inode_init_security - copy out the smack from an inode
786 * @inode: the newly created inode
787 * @dir: containing directory object
789 * @name: where to put the attribute name
790 * @value: where to put the attribute value
791 * @len: where to put the length of the attribute
793 * Returns 0 if it all works out, -ENOMEM if there's no memory
795 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
796 const struct qstr
*qstr
, const char **name
,
797 void **value
, size_t *len
)
799 struct inode_smack
*issp
= inode
->i_security
;
800 struct smack_known
*skp
= smk_of_current();
801 struct smack_known
*isp
= smk_of_inode(inode
);
802 struct smack_known
*dsp
= smk_of_inode(dir
);
806 *name
= XATTR_SMACK_SUFFIX
;
810 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
815 * If the access rule allows transmutation and
816 * the directory requests transmutation then
817 * by all means transmute.
818 * Mark the inode as changed.
820 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
821 smk_inode_transmutable(dir
)) {
823 issp
->smk_flags
|= SMK_INODE_CHANGED
;
826 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
830 *len
= strlen(isp
->smk_known
);
837 * smack_inode_link - Smack check on link
838 * @old_dentry: the existing object
840 * @new_dentry: the new object
842 * Returns 0 if access is permitted, an error code otherwise
844 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
845 struct dentry
*new_dentry
)
847 struct smack_known
*isp
;
848 struct smk_audit_info ad
;
851 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
852 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
854 isp
= smk_of_inode(old_dentry
->d_inode
);
855 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
856 rc
= smk_bu_inode(old_dentry
->d_inode
, MAY_WRITE
, rc
);
858 if (rc
== 0 && d_is_positive(new_dentry
)) {
859 isp
= smk_of_inode(new_dentry
->d_inode
);
860 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
861 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
862 rc
= smk_bu_inode(new_dentry
->d_inode
, MAY_WRITE
, rc
);
869 * smack_inode_unlink - Smack check on inode deletion
870 * @dir: containing directory object
871 * @dentry: file to unlink
873 * Returns 0 if current can write the containing directory
874 * and the object, error code otherwise
876 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
878 struct inode
*ip
= dentry
->d_inode
;
879 struct smk_audit_info ad
;
882 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
883 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
886 * You need write access to the thing you're unlinking
888 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
889 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
892 * You also need write access to the containing directory
894 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
895 smk_ad_setfield_u_fs_inode(&ad
, dir
);
896 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
897 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
903 * smack_inode_rmdir - Smack check on directory deletion
904 * @dir: containing directory object
905 * @dentry: directory to unlink
907 * Returns 0 if current can write the containing directory
908 * and the directory, error code otherwise
910 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
912 struct smk_audit_info ad
;
915 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
916 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
919 * You need write access to the thing you're removing
921 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
922 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
925 * You also need write access to the containing directory
927 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
928 smk_ad_setfield_u_fs_inode(&ad
, dir
);
929 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
930 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
937 * smack_inode_rename - Smack check on rename
939 * @old_dentry: the old object
941 * @new_dentry: the new object
943 * Read and write access is required on both the old and
946 * Returns 0 if access is permitted, an error code otherwise
948 static int smack_inode_rename(struct inode
*old_inode
,
949 struct dentry
*old_dentry
,
950 struct inode
*new_inode
,
951 struct dentry
*new_dentry
)
954 struct smack_known
*isp
;
955 struct smk_audit_info ad
;
957 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
958 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
960 isp
= smk_of_inode(old_dentry
->d_inode
);
961 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
962 rc
= smk_bu_inode(old_dentry
->d_inode
, MAY_READWRITE
, rc
);
964 if (rc
== 0 && d_is_positive(new_dentry
)) {
965 isp
= smk_of_inode(new_dentry
->d_inode
);
966 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
967 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
968 rc
= smk_bu_inode(new_dentry
->d_inode
, MAY_READWRITE
, rc
);
974 * smack_inode_permission - Smack version of permission()
975 * @inode: the inode in question
976 * @mask: the access requested
978 * This is the important Smack hook.
980 * Returns 0 if access is permitted, -EACCES otherwise
982 static int smack_inode_permission(struct inode
*inode
, int mask
)
984 struct smk_audit_info ad
;
985 int no_block
= mask
& MAY_NOT_BLOCK
;
988 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
990 * No permission to check. Existence test. Yup, it's there.
995 /* May be droppable after audit */
998 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
999 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1000 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1001 rc
= smk_bu_inode(inode
, mask
, rc
);
1006 * smack_inode_setattr - Smack check for setting attributes
1007 * @dentry: the object
1008 * @iattr: for the force flag
1010 * Returns 0 if access is permitted, an error code otherwise
1012 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1014 struct smk_audit_info ad
;
1018 * Need to allow for clearing the setuid bit.
1020 if (iattr
->ia_valid
& ATTR_FORCE
)
1022 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1023 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1025 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1026 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1031 * smack_inode_getattr - Smack check for getting attributes
1032 * @mnt: vfsmount of the object
1033 * @dentry: the object
1035 * Returns 0 if access is permitted, an error code otherwise
1037 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
1039 struct smk_audit_info ad
;
1043 path
.dentry
= dentry
;
1046 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1047 smk_ad_setfield_u_fs_path(&ad
, path
);
1048 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
1049 rc
= smk_bu_inode(dentry
->d_inode
, MAY_READ
, rc
);
1054 * smack_inode_setxattr - Smack check for setting xattrs
1055 * @dentry: the object
1056 * @name: name of the attribute
1057 * @value: value of the attribute
1058 * @size: size of the value
1061 * This protects the Smack attribute explicitly.
1063 * Returns 0 if access is permitted, an error code otherwise
1065 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1066 const void *value
, size_t size
, int flags
)
1068 struct smk_audit_info ad
;
1069 struct smack_known
*skp
;
1071 int check_import
= 0;
1076 * Check label validity here so import won't fail in post_setxattr
1078 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1079 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1080 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1083 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1084 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1088 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1090 if (size
!= TRANS_TRUE_SIZE
||
1091 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1094 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1096 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1099 if (rc
== 0 && check_import
) {
1100 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1101 if (skp
== NULL
|| (check_star
&&
1102 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1106 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1107 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1110 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1111 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1118 * smack_inode_post_setxattr - Apply the Smack update approved above
1120 * @name: attribute name
1121 * @value: attribute value
1122 * @size: attribute size
1125 * Set the pointer in the inode blob to the entry found
1126 * in the master label list.
1128 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1129 const void *value
, size_t size
, int flags
)
1131 struct smack_known
*skp
;
1132 struct inode_smack
*isp
= dentry
->d_inode
->i_security
;
1134 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1135 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1139 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1140 skp
= smk_import_entry(value
, size
);
1142 isp
->smk_inode
= skp
;
1144 isp
->smk_inode
= &smack_known_invalid
;
1145 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1146 skp
= smk_import_entry(value
, size
);
1148 isp
->smk_task
= skp
;
1150 isp
->smk_task
= &smack_known_invalid
;
1151 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1152 skp
= smk_import_entry(value
, size
);
1154 isp
->smk_mmap
= skp
;
1156 isp
->smk_mmap
= &smack_known_invalid
;
1163 * smack_inode_getxattr - Smack check on getxattr
1164 * @dentry: the object
1167 * Returns 0 if access is permitted, an error code otherwise
1169 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1171 struct smk_audit_info ad
;
1174 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1175 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1177 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
1178 rc
= smk_bu_inode(dentry
->d_inode
, MAY_READ
, rc
);
1183 * smack_inode_removexattr - Smack check on removexattr
1184 * @dentry: the object
1185 * @name: name of the attribute
1187 * Removing the Smack attribute requires CAP_MAC_ADMIN
1189 * Returns 0 if access is permitted, an error code otherwise
1191 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1193 struct inode_smack
*isp
;
1194 struct smk_audit_info ad
;
1197 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1198 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1199 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1200 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1201 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1202 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1203 if (!smack_privileged(CAP_MAC_ADMIN
))
1206 rc
= cap_inode_removexattr(dentry
, name
);
1211 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1212 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1214 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1215 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1219 isp
= dentry
->d_inode
->i_security
;
1221 * Don't do anything special for these.
1222 * XATTR_NAME_SMACKIPIN
1223 * XATTR_NAME_SMACKIPOUT
1224 * XATTR_NAME_SMACKEXEC
1226 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
1227 isp
->smk_task
= NULL
;
1228 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1229 isp
->smk_mmap
= NULL
;
1230 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1231 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1237 * smack_inode_getsecurity - get smack xattrs
1238 * @inode: the object
1239 * @name: attribute name
1240 * @buffer: where to put the result
1243 * Returns the size of the attribute or an error code
1245 static int smack_inode_getsecurity(const struct inode
*inode
,
1246 const char *name
, void **buffer
,
1249 struct socket_smack
*ssp
;
1250 struct socket
*sock
;
1251 struct super_block
*sbp
;
1252 struct inode
*ip
= (struct inode
*)inode
;
1253 struct smack_known
*isp
;
1257 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1258 isp
= smk_of_inode(inode
);
1259 ilen
= strlen(isp
->smk_known
);
1260 *buffer
= isp
->smk_known
;
1265 * The rest of the Smack xattrs are only on sockets.
1268 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1271 sock
= SOCKET_I(ip
);
1272 if (sock
== NULL
|| sock
->sk
== NULL
)
1275 ssp
= sock
->sk
->sk_security
;
1277 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1279 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1284 ilen
= strlen(isp
->smk_known
);
1286 *buffer
= isp
->smk_known
;
1295 * smack_inode_listsecurity - list the Smack attributes
1296 * @inode: the object
1297 * @buffer: where they go
1298 * @buffer_size: size of buffer
1300 * Returns 0 on success, -EINVAL otherwise
1302 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1305 int len
= sizeof(XATTR_NAME_SMACK
);
1307 if (buffer
!= NULL
&& len
<= buffer_size
)
1308 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1314 * smack_inode_getsecid - Extract inode's security id
1315 * @inode: inode to extract the info from
1316 * @secid: where result will be saved
1318 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
1320 struct inode_smack
*isp
= inode
->i_security
;
1322 *secid
= isp
->smk_inode
->smk_secid
;
1330 * smack_file_permission - Smack check on file operations
1336 * Should access checks be done on each read or write?
1337 * UNICOS and SELinux say yes.
1338 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1340 * I'll say no for now. Smack does not do the frequent
1341 * label changing that SELinux does.
1343 static int smack_file_permission(struct file
*file
, int mask
)
1349 * smack_file_alloc_security - assign a file security blob
1352 * The security blob for a file is a pointer to the master
1353 * label list, so no allocation is done.
1355 * f_security is the owner security information. It
1356 * isn't used on file access checks, it's for send_sigio.
1360 static int smack_file_alloc_security(struct file
*file
)
1362 struct smack_known
*skp
= smk_of_current();
1364 file
->f_security
= skp
;
1369 * smack_file_free_security - clear a file security blob
1372 * The security blob for a file is a pointer to the master
1373 * label list, so no memory is freed.
1375 static void smack_file_free_security(struct file
*file
)
1377 file
->f_security
= NULL
;
1381 * smack_file_ioctl - Smack check on ioctls
1386 * Relies heavily on the correct use of the ioctl command conventions.
1388 * Returns 0 if allowed, error code otherwise
1390 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1394 struct smk_audit_info ad
;
1395 struct inode
*inode
= file_inode(file
);
1397 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1398 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1400 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1401 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1402 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1405 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1406 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1407 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1414 * smack_file_lock - Smack check on file locking
1418 * Returns 0 if current has lock access, error code otherwise
1420 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1422 struct smk_audit_info ad
;
1424 struct inode
*inode
= file_inode(file
);
1426 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1427 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1428 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1429 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1434 * smack_file_fcntl - Smack check on fcntl
1436 * @cmd: what action to check
1439 * Generally these operations are harmless.
1440 * File locking operations present an obvious mechanism
1441 * for passing information, so they require write access.
1443 * Returns 0 if current has access, error code otherwise
1445 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1448 struct smk_audit_info ad
;
1450 struct inode
*inode
= file_inode(file
);
1457 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1458 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1459 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1460 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1464 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1465 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1466 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1467 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1478 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1479 * if mapping anonymous memory.
1480 * @file contains the file structure for file to map (may be NULL).
1481 * @reqprot contains the protection requested by the application.
1482 * @prot contains the protection that will be applied by the kernel.
1483 * @flags contains the operational flags.
1484 * Return 0 if permission is granted.
1486 static int smack_mmap_file(struct file
*file
,
1487 unsigned long reqprot
, unsigned long prot
,
1488 unsigned long flags
)
1490 struct smack_known
*skp
;
1491 struct smack_known
*mkp
;
1492 struct smack_rule
*srp
;
1493 struct task_smack
*tsp
;
1494 struct smack_known
*okp
;
1495 struct inode_smack
*isp
;
1504 isp
= file_inode(file
)->i_security
;
1505 if (isp
->smk_mmap
== NULL
)
1507 mkp
= isp
->smk_mmap
;
1509 tsp
= current_security();
1510 skp
= smk_of_current();
1515 * For each Smack rule associated with the subject
1516 * label verify that the SMACK64MMAP also has access
1517 * to that rule's object label.
1519 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1520 okp
= srp
->smk_object
;
1522 * Matching labels always allows access.
1524 if (mkp
->smk_known
== okp
->smk_known
)
1527 * If there is a matching local rule take
1528 * that into account as well.
1530 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1534 may
= srp
->smk_access
;
1536 may
&= srp
->smk_access
;
1538 * If may is zero the SMACK64MMAP subject can't
1539 * possibly have less access.
1545 * Fetch the global list entry.
1546 * If there isn't one a SMACK64MMAP subject
1547 * can't have as much access as current.
1549 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1551 if (mmay
== -ENOENT
) {
1556 * If there is a local entry it modifies the
1557 * potential access, too.
1559 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1561 if (tmay
!= -ENOENT
)
1565 * If there is any access available to current that is
1566 * not available to a SMACK64MMAP subject
1569 if ((may
| mmay
) != mmay
) {
1581 * smack_file_set_fowner - set the file security blob value
1582 * @file: object in question
1585 static void smack_file_set_fowner(struct file
*file
)
1587 file
->f_security
= smk_of_current();
1591 * smack_file_send_sigiotask - Smack on sigio
1592 * @tsk: The target task
1593 * @fown: the object the signal come from
1596 * Allow a privileged task to get signals even if it shouldn't
1598 * Returns 0 if a subject with the object's smack could
1599 * write to the task, an error code otherwise.
1601 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1602 struct fown_struct
*fown
, int signum
)
1604 struct smack_known
*skp
;
1605 struct smack_known
*tkp
= smk_of_task(tsk
->cred
->security
);
1608 struct smk_audit_info ad
;
1611 * struct fown_struct is never outside the context of a struct file
1613 file
= container_of(fown
, struct file
, f_owner
);
1615 /* we don't log here as rc can be overriden */
1616 skp
= file
->f_security
;
1617 rc
= smk_access(skp
, tkp
, MAY_WRITE
, NULL
);
1618 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_WRITE
, rc
);
1619 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1622 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1623 smk_ad_setfield_u_tsk(&ad
, tsk
);
1624 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_WRITE
, rc
, &ad
);
1629 * smack_file_receive - Smack file receive check
1632 * Returns 0 if current has access, error code otherwise
1634 static int smack_file_receive(struct file
*file
)
1638 struct smk_audit_info ad
;
1639 struct inode
*inode
= file_inode(file
);
1641 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1642 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1644 * This code relies on bitmasks.
1646 if (file
->f_mode
& FMODE_READ
)
1648 if (file
->f_mode
& FMODE_WRITE
)
1651 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1652 rc
= smk_bu_file(file
, may
, rc
);
1657 * smack_file_open - Smack dentry open processing
1659 * @cred: task credential
1661 * Set the security blob in the file structure.
1662 * Allow the open only if the task has read access. There are
1663 * many read operations (e.g. fstat) that you can do with an
1664 * fd even if you have the file open write-only.
1668 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1670 struct task_smack
*tsp
= cred
->security
;
1671 struct inode
*inode
= file_inode(file
);
1672 struct smk_audit_info ad
;
1675 if (smack_privileged(CAP_MAC_OVERRIDE
))
1678 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1679 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1680 rc
= smk_access(tsp
->smk_task
, smk_of_inode(inode
), MAY_READ
, &ad
);
1681 rc
= smk_bu_credfile(cred
, file
, MAY_READ
, rc
);
1691 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1692 * @new: the new credentials
1693 * @gfp: the atomicity of any memory allocations
1695 * Prepare a blank set of credentials for modification. This must allocate all
1696 * the memory the LSM module might require such that cred_transfer() can
1697 * complete without error.
1699 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1701 struct task_smack
*tsp
;
1703 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1707 cred
->security
= tsp
;
1714 * smack_cred_free - "free" task-level security credentials
1715 * @cred: the credentials in question
1718 static void smack_cred_free(struct cred
*cred
)
1720 struct task_smack
*tsp
= cred
->security
;
1721 struct smack_rule
*rp
;
1722 struct list_head
*l
;
1723 struct list_head
*n
;
1727 cred
->security
= NULL
;
1729 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1730 rp
= list_entry(l
, struct smack_rule
, list
);
1731 list_del(&rp
->list
);
1738 * smack_cred_prepare - prepare new set of credentials for modification
1739 * @new: the new credentials
1740 * @old: the original credentials
1741 * @gfp: the atomicity of any memory allocations
1743 * Prepare a new set of credentials for modification.
1745 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1748 struct task_smack
*old_tsp
= old
->security
;
1749 struct task_smack
*new_tsp
;
1752 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
1753 if (new_tsp
== NULL
)
1756 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1760 new->security
= new_tsp
;
1765 * smack_cred_transfer - Transfer the old credentials to the new credentials
1766 * @new: the new credentials
1767 * @old: the original credentials
1769 * Fill in a set of blank credentials from another set of credentials.
1771 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1773 struct task_smack
*old_tsp
= old
->security
;
1774 struct task_smack
*new_tsp
= new->security
;
1776 new_tsp
->smk_task
= old_tsp
->smk_task
;
1777 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1778 mutex_init(&new_tsp
->smk_rules_lock
);
1779 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1782 /* cbs copy rule list */
1786 * smack_kernel_act_as - Set the subjective context in a set of credentials
1787 * @new: points to the set of credentials to be modified.
1788 * @secid: specifies the security ID to be set
1790 * Set the security data for a kernel service.
1792 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1794 struct task_smack
*new_tsp
= new->security
;
1795 struct smack_known
*skp
= smack_from_secid(secid
);
1800 new_tsp
->smk_task
= skp
;
1805 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1806 * @new: points to the set of credentials to be modified
1807 * @inode: points to the inode to use as a reference
1809 * Set the file creation context in a set of credentials to the same
1810 * as the objective context of the specified inode
1812 static int smack_kernel_create_files_as(struct cred
*new,
1813 struct inode
*inode
)
1815 struct inode_smack
*isp
= inode
->i_security
;
1816 struct task_smack
*tsp
= new->security
;
1818 tsp
->smk_forked
= isp
->smk_inode
;
1819 tsp
->smk_task
= tsp
->smk_forked
;
1824 * smk_curacc_on_task - helper to log task related access
1825 * @p: the task object
1826 * @access: the access requested
1827 * @caller: name of the calling function for audit
1829 * Return 0 if access is permitted
1831 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
1834 struct smk_audit_info ad
;
1835 struct smack_known
*skp
= smk_of_task_struct(p
);
1838 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
1839 smk_ad_setfield_u_tsk(&ad
, p
);
1840 rc
= smk_curacc(skp
, access
, &ad
);
1841 rc
= smk_bu_task(p
, access
, rc
);
1846 * smack_task_setpgid - Smack check on setting pgid
1847 * @p: the task object
1850 * Return 0 if write access is permitted
1852 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1854 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1858 * smack_task_getpgid - Smack access check for getpgid
1859 * @p: the object task
1861 * Returns 0 if current can read the object task, error code otherwise
1863 static int smack_task_getpgid(struct task_struct
*p
)
1865 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1869 * smack_task_getsid - Smack access check for getsid
1870 * @p: the object task
1872 * Returns 0 if current can read the object task, error code otherwise
1874 static int smack_task_getsid(struct task_struct
*p
)
1876 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1880 * smack_task_getsecid - get the secid of the task
1881 * @p: the object task
1882 * @secid: where to put the result
1884 * Sets the secid to contain a u32 version of the smack label.
1886 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1888 struct smack_known
*skp
= smk_of_task_struct(p
);
1890 *secid
= skp
->smk_secid
;
1894 * smack_task_setnice - Smack check on setting nice
1895 * @p: the task object
1898 * Return 0 if write access is permitted
1900 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1904 rc
= cap_task_setnice(p
, nice
);
1906 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1911 * smack_task_setioprio - Smack check on setting ioprio
1912 * @p: the task object
1915 * Return 0 if write access is permitted
1917 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1921 rc
= cap_task_setioprio(p
, ioprio
);
1923 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1928 * smack_task_getioprio - Smack check on reading ioprio
1929 * @p: the task object
1931 * Return 0 if read access is permitted
1933 static int smack_task_getioprio(struct task_struct
*p
)
1935 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1939 * smack_task_setscheduler - Smack check on setting scheduler
1940 * @p: the task object
1944 * Return 0 if read access is permitted
1946 static int smack_task_setscheduler(struct task_struct
*p
)
1950 rc
= cap_task_setscheduler(p
);
1952 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1957 * smack_task_getscheduler - Smack check on reading scheduler
1958 * @p: the task object
1960 * Return 0 if read access is permitted
1962 static int smack_task_getscheduler(struct task_struct
*p
)
1964 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1968 * smack_task_movememory - Smack check on moving memory
1969 * @p: the task object
1971 * Return 0 if write access is permitted
1973 static int smack_task_movememory(struct task_struct
*p
)
1975 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1979 * smack_task_kill - Smack check on signal delivery
1980 * @p: the task object
1983 * @secid: identifies the smack to use in lieu of current's
1985 * Return 0 if write access is permitted
1987 * The secid behavior is an artifact of an SELinux hack
1988 * in the USB code. Someday it may go away.
1990 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1993 struct smk_audit_info ad
;
1994 struct smack_known
*skp
;
1995 struct smack_known
*tkp
= smk_of_task_struct(p
);
1998 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1999 smk_ad_setfield_u_tsk(&ad
, p
);
2001 * Sending a signal requires that the sender
2002 * can write the receiver.
2005 rc
= smk_curacc(tkp
, MAY_WRITE
, &ad
);
2006 rc
= smk_bu_task(p
, MAY_WRITE
, rc
);
2010 * If the secid isn't 0 we're dealing with some USB IO
2011 * specific behavior. This is not clean. For one thing
2012 * we can't take privilege into account.
2014 skp
= smack_from_secid(secid
);
2015 rc
= smk_access(skp
, tkp
, MAY_WRITE
, &ad
);
2016 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_WRITE
, rc
);
2021 * smack_task_wait - Smack access check for waiting
2022 * @p: task to wait for
2026 static int smack_task_wait(struct task_struct
*p
)
2029 * Allow the operation to succeed.
2031 * In userless environments (e.g. phones) programs
2032 * get marked with SMACK64EXEC and even if the parent
2033 * and child shouldn't be talking the parent still
2034 * may expect to know when the child exits.
2040 * smack_task_to_inode - copy task smack into the inode blob
2041 * @p: task to copy from
2042 * @inode: inode to copy to
2044 * Sets the smack pointer in the inode security blob
2046 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2048 struct inode_smack
*isp
= inode
->i_security
;
2049 struct smack_known
*skp
= smk_of_task_struct(p
);
2051 isp
->smk_inode
= skp
;
2059 * smack_sk_alloc_security - Allocate a socket blob
2062 * @gfp_flags: memory allocation flags
2064 * Assign Smack pointers to current
2066 * Returns 0 on success, -ENOMEM is there's no memory
2068 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2070 struct smack_known
*skp
= smk_of_current();
2071 struct socket_smack
*ssp
;
2073 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2079 ssp
->smk_packet
= NULL
;
2081 sk
->sk_security
= ssp
;
2087 * smack_sk_free_security - Free a socket blob
2090 * Clears the blob pointer
2092 static void smack_sk_free_security(struct sock
*sk
)
2094 kfree(sk
->sk_security
);
2098 * smack_host_label - check host based restrictions
2099 * @sip: the object end
2101 * looks for host based access restrictions
2103 * This version will only be appropriate for really small sets of single label
2104 * hosts. The caller is responsible for ensuring that the RCU read lock is
2105 * taken before calling this function.
2107 * Returns the label of the far end or NULL if it's not special.
2109 static struct smack_known
*smack_host_label(struct sockaddr_in
*sip
)
2111 struct smk_netlbladdr
*snp
;
2112 struct in_addr
*siap
= &sip
->sin_addr
;
2114 if (siap
->s_addr
== 0)
2117 list_for_each_entry_rcu(snp
, &smk_netlbladdr_list
, list
)
2119 * we break after finding the first match because
2120 * the list is sorted from longest to shortest mask
2121 * so we have found the most specific match
2123 if ((&snp
->smk_host
.sin_addr
)->s_addr
==
2124 (siap
->s_addr
& (&snp
->smk_mask
)->s_addr
)) {
2125 /* we have found the special CIPSO option */
2126 if (snp
->smk_label
== &smack_cipso_option
)
2128 return snp
->smk_label
;
2135 * smack_netlabel - Set the secattr on a socket
2137 * @labeled: socket label scheme
2139 * Convert the outbound smack value (smk_out) to a
2140 * secattr and attach it to the socket.
2142 * Returns 0 on success or an error code
2144 static int smack_netlabel(struct sock
*sk
, int labeled
)
2146 struct smack_known
*skp
;
2147 struct socket_smack
*ssp
= sk
->sk_security
;
2151 * Usually the netlabel code will handle changing the
2152 * packet labeling based on the label.
2153 * The case of a single label host is different, because
2154 * a single label host should never get a labeled packet
2155 * even though the label is usually associated with a packet
2159 bh_lock_sock_nested(sk
);
2161 if (ssp
->smk_out
== smack_net_ambient
||
2162 labeled
== SMACK_UNLABELED_SOCKET
)
2163 netlbl_sock_delattr(sk
);
2166 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2176 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2178 * @sap: the destination address
2180 * Set the correct secattr for the given socket based on the destination
2181 * address and perform any outbound access checks needed.
2183 * Returns 0 on success or an error code.
2186 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2188 struct smack_known
*skp
;
2191 struct smack_known
*hkp
;
2192 struct socket_smack
*ssp
= sk
->sk_security
;
2193 struct smk_audit_info ad
;
2196 hkp
= smack_host_label(sap
);
2199 struct lsm_network_audit net
;
2201 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2202 ad
.a
.u
.net
->family
= sap
->sin_family
;
2203 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2204 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2206 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2208 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2209 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2211 sk_lbl
= SMACK_CIPSO_SOCKET
;
2218 return smack_netlabel(sk
, sk_lbl
);
2221 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2223 * smk_ipv6_port_label - Smack port access table management
2227 * Create or update the port list entry
2229 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2231 struct sock
*sk
= sock
->sk
;
2232 struct sockaddr_in6
*addr6
;
2233 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2234 struct smk_port_label
*spp
;
2235 unsigned short port
= 0;
2237 if (address
== NULL
) {
2239 * This operation is changing the Smack information
2240 * on the bound socket. Take the changes to the port
2243 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2244 if (sk
!= spp
->smk_sock
)
2246 spp
->smk_in
= ssp
->smk_in
;
2247 spp
->smk_out
= ssp
->smk_out
;
2251 * A NULL address is only used for updating existing
2252 * bound entries. If there isn't one, it's OK.
2257 addr6
= (struct sockaddr_in6
*)address
;
2258 port
= ntohs(addr6
->sin6_port
);
2260 * This is a special case that is safely ignored.
2266 * Look for an existing port list entry.
2267 * This is an indication that a port is getting reused.
2269 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2270 if (spp
->smk_port
!= port
)
2272 spp
->smk_port
= port
;
2274 spp
->smk_in
= ssp
->smk_in
;
2275 spp
->smk_out
= ssp
->smk_out
;
2280 * A new port entry is required.
2282 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2286 spp
->smk_port
= port
;
2288 spp
->smk_in
= ssp
->smk_in
;
2289 spp
->smk_out
= ssp
->smk_out
;
2291 list_add(&spp
->list
, &smk_ipv6_port_list
);
2296 * smk_ipv6_port_check - check Smack port access
2300 * Create or update the port list entry
2302 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2307 struct smk_port_label
*spp
;
2308 struct socket_smack
*ssp
= sk
->sk_security
;
2309 struct smack_known
*skp
;
2310 unsigned short port
= 0;
2311 struct smack_known
*object
;
2312 struct smk_audit_info ad
;
2315 struct lsm_network_audit net
;
2318 if (act
== SMK_RECEIVING
) {
2319 skp
= smack_net_ambient
;
2320 object
= ssp
->smk_in
;
2323 object
= smack_net_ambient
;
2327 * Get the IP address and port from the address.
2329 port
= ntohs(address
->sin6_port
);
2330 bep
= (__be16
*)(&address
->sin6_addr
);
2331 be32p
= (__be32
*)(&address
->sin6_addr
);
2334 * It's remote, so port lookup does no good.
2336 if (be32p
[0] || be32p
[1] || be32p
[2] || bep
[6] || ntohs(bep
[7]) != 1)
2340 * It's local so the send check has to have passed.
2342 if (act
== SMK_RECEIVING
) {
2343 skp
= &smack_known_web
;
2347 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2348 if (spp
->smk_port
!= port
)
2350 object
= spp
->smk_in
;
2351 if (act
== SMK_CONNECTING
)
2352 ssp
->smk_packet
= spp
->smk_out
;
2359 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2360 ad
.a
.u
.net
->family
= sk
->sk_family
;
2361 ad
.a
.u
.net
->dport
= port
;
2362 if (act
== SMK_RECEIVING
)
2363 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2365 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2367 rc
= smk_access(skp
, object
, MAY_WRITE
, &ad
);
2368 rc
= smk_bu_note("IPv6 port check", skp
, object
, MAY_WRITE
, rc
);
2371 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2374 * smack_inode_setsecurity - set smack xattrs
2375 * @inode: the object
2376 * @name: attribute name
2377 * @value: attribute value
2378 * @size: size of the attribute
2381 * Sets the named attribute in the appropriate blob
2383 * Returns 0 on success, or an error code
2385 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2386 const void *value
, size_t size
, int flags
)
2388 struct smack_known
*skp
;
2389 struct inode_smack
*nsp
= inode
->i_security
;
2390 struct socket_smack
*ssp
;
2391 struct socket
*sock
;
2394 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2397 skp
= smk_import_entry(value
, size
);
2401 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2402 nsp
->smk_inode
= skp
;
2403 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2407 * The rest of the Smack xattrs are only on sockets.
2409 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2412 sock
= SOCKET_I(inode
);
2413 if (sock
== NULL
|| sock
->sk
== NULL
)
2416 ssp
= sock
->sk
->sk_security
;
2418 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2420 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2422 if (sock
->sk
->sk_family
== PF_INET
) {
2423 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2426 "Smack: \"%s\" netlbl error %d.\n",
2432 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2433 if (sock
->sk
->sk_family
== PF_INET6
)
2434 smk_ipv6_port_label(sock
, NULL
);
2435 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2441 * smack_socket_post_create - finish socket setup
2443 * @family: protocol family
2448 * Sets the netlabel information on the socket
2450 * Returns 0 on success, and error code otherwise
2452 static int smack_socket_post_create(struct socket
*sock
, int family
,
2453 int type
, int protocol
, int kern
)
2455 if (family
!= PF_INET
|| sock
->sk
== NULL
)
2458 * Set the outbound netlbl.
2460 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2463 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
2465 * smack_socket_bind - record port binding information.
2467 * @address: the port address
2468 * @addrlen: size of the address
2470 * Records the label bound to a port.
2474 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2477 #if IS_ENABLED(CONFIG_IPV6)
2478 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2479 smk_ipv6_port_label(sock
, address
);
2484 #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
2487 * smack_socket_connect - connect access check
2489 * @sap: the other end
2490 * @addrlen: size of sap
2492 * Verifies that a connection may be possible
2494 * Returns 0 on success, and error code otherwise
2496 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2501 if (sock
->sk
== NULL
)
2504 switch (sock
->sk
->sk_family
) {
2506 if (addrlen
< sizeof(struct sockaddr_in
))
2508 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2511 if (addrlen
< sizeof(struct sockaddr_in6
))
2513 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2514 rc
= smk_ipv6_port_check(sock
->sk
, (struct sockaddr_in6
*)sap
,
2516 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2523 * smack_flags_to_may - convert S_ to MAY_ values
2524 * @flags: the S_ value
2526 * Returns the equivalent MAY_ value
2528 static int smack_flags_to_may(int flags
)
2532 if (flags
& S_IRUGO
)
2534 if (flags
& S_IWUGO
)
2536 if (flags
& S_IXUGO
)
2543 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2548 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2550 struct smack_known
*skp
= smk_of_current();
2552 msg
->security
= skp
;
2557 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2560 * Clears the blob pointer
2562 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2564 msg
->security
= NULL
;
2568 * smack_of_shm - the smack pointer for the shm
2571 * Returns a pointer to the smack value
2573 static struct smack_known
*smack_of_shm(struct shmid_kernel
*shp
)
2575 return (struct smack_known
*)shp
->shm_perm
.security
;
2579 * smack_shm_alloc_security - Set the security blob for shm
2584 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2586 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2587 struct smack_known
*skp
= smk_of_current();
2589 isp
->security
= skp
;
2594 * smack_shm_free_security - Clear the security blob for shm
2597 * Clears the blob pointer
2599 static void smack_shm_free_security(struct shmid_kernel
*shp
)
2601 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2603 isp
->security
= NULL
;
2607 * smk_curacc_shm : check if current has access on shm
2609 * @access : access requested
2611 * Returns 0 if current has the requested access, error code otherwise
2613 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2615 struct smack_known
*ssp
= smack_of_shm(shp
);
2616 struct smk_audit_info ad
;
2620 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2621 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2623 rc
= smk_curacc(ssp
, access
, &ad
);
2624 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2629 * smack_shm_associate - Smack access check for shm
2631 * @shmflg: access requested
2633 * Returns 0 if current has the requested access, error code otherwise
2635 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2639 may
= smack_flags_to_may(shmflg
);
2640 return smk_curacc_shm(shp
, may
);
2644 * smack_shm_shmctl - Smack access check for shm
2646 * @cmd: what it wants to do
2648 * Returns 0 if current has the requested access, error code otherwise
2650 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2663 may
= MAY_READWRITE
;
2668 * System level information.
2674 return smk_curacc_shm(shp
, may
);
2678 * smack_shm_shmat - Smack access for shmat
2681 * @shmflg: access requested
2683 * Returns 0 if current has the requested access, error code otherwise
2685 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2690 may
= smack_flags_to_may(shmflg
);
2691 return smk_curacc_shm(shp
, may
);
2695 * smack_of_sem - the smack pointer for the sem
2698 * Returns a pointer to the smack value
2700 static struct smack_known
*smack_of_sem(struct sem_array
*sma
)
2702 return (struct smack_known
*)sma
->sem_perm
.security
;
2706 * smack_sem_alloc_security - Set the security blob for sem
2711 static int smack_sem_alloc_security(struct sem_array
*sma
)
2713 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2714 struct smack_known
*skp
= smk_of_current();
2716 isp
->security
= skp
;
2721 * smack_sem_free_security - Clear the security blob for sem
2724 * Clears the blob pointer
2726 static void smack_sem_free_security(struct sem_array
*sma
)
2728 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2730 isp
->security
= NULL
;
2734 * smk_curacc_sem : check if current has access on sem
2736 * @access : access requested
2738 * Returns 0 if current has the requested access, error code otherwise
2740 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2742 struct smack_known
*ssp
= smack_of_sem(sma
);
2743 struct smk_audit_info ad
;
2747 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2748 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2750 rc
= smk_curacc(ssp
, access
, &ad
);
2751 rc
= smk_bu_current("sem", ssp
, access
, rc
);
2756 * smack_sem_associate - Smack access check for sem
2758 * @semflg: access requested
2760 * Returns 0 if current has the requested access, error code otherwise
2762 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2766 may
= smack_flags_to_may(semflg
);
2767 return smk_curacc_sem(sma
, may
);
2771 * smack_sem_shmctl - Smack access check for sem
2773 * @cmd: what it wants to do
2775 * Returns 0 if current has the requested access, error code otherwise
2777 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
2795 may
= MAY_READWRITE
;
2800 * System level information
2807 return smk_curacc_sem(sma
, may
);
2811 * smack_sem_semop - Smack checks of semaphore operations
2817 * Treated as read and write in all cases.
2819 * Returns 0 if access is allowed, error code otherwise
2821 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
2822 unsigned nsops
, int alter
)
2824 return smk_curacc_sem(sma
, MAY_READWRITE
);
2828 * smack_msg_alloc_security - Set the security blob for msg
2833 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
2835 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2836 struct smack_known
*skp
= smk_of_current();
2838 kisp
->security
= skp
;
2843 * smack_msg_free_security - Clear the security blob for msg
2846 * Clears the blob pointer
2848 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
2850 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2852 kisp
->security
= NULL
;
2856 * smack_of_msq - the smack pointer for the msq
2859 * Returns a pointer to the smack label entry
2861 static struct smack_known
*smack_of_msq(struct msg_queue
*msq
)
2863 return (struct smack_known
*)msq
->q_perm
.security
;
2867 * smk_curacc_msq : helper to check if current has access on msq
2869 * @access : access requested
2871 * return 0 if current has access, error otherwise
2873 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
2875 struct smack_known
*msp
= smack_of_msq(msq
);
2876 struct smk_audit_info ad
;
2880 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2881 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
2883 rc
= smk_curacc(msp
, access
, &ad
);
2884 rc
= smk_bu_current("msq", msp
, access
, rc
);
2889 * smack_msg_queue_associate - Smack access check for msg_queue
2891 * @msqflg: access requested
2893 * Returns 0 if current has the requested access, error code otherwise
2895 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
2899 may
= smack_flags_to_may(msqflg
);
2900 return smk_curacc_msq(msq
, may
);
2904 * smack_msg_queue_msgctl - Smack access check for msg_queue
2906 * @cmd: what it wants to do
2908 * Returns 0 if current has the requested access, error code otherwise
2910 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
2921 may
= MAY_READWRITE
;
2926 * System level information
2933 return smk_curacc_msq(msq
, may
);
2937 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2940 * @msqflg: access requested
2942 * Returns 0 if current has the requested access, error code otherwise
2944 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
2949 may
= smack_flags_to_may(msqflg
);
2950 return smk_curacc_msq(msq
, may
);
2954 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2961 * Returns 0 if current has read and write access, error code otherwise
2963 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
2964 struct task_struct
*target
, long type
, int mode
)
2966 return smk_curacc_msq(msq
, MAY_READWRITE
);
2970 * smack_ipc_permission - Smack access for ipc_permission()
2971 * @ipp: the object permissions
2972 * @flag: access requested
2974 * Returns 0 if current has read and write access, error code otherwise
2976 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
2978 struct smack_known
*iskp
= ipp
->security
;
2979 int may
= smack_flags_to_may(flag
);
2980 struct smk_audit_info ad
;
2984 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2985 ad
.a
.u
.ipc_id
= ipp
->id
;
2987 rc
= smk_curacc(iskp
, may
, &ad
);
2988 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
2993 * smack_ipc_getsecid - Extract smack security id
2994 * @ipp: the object permissions
2995 * @secid: where result will be saved
2997 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
2999 struct smack_known
*iskp
= ipp
->security
;
3001 *secid
= iskp
->smk_secid
;
3005 * smack_d_instantiate - Make sure the blob is correct on an inode
3006 * @opt_dentry: dentry where inode will be attached
3007 * @inode: the object
3009 * Set the inode's security blob if it hasn't been done already.
3011 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3013 struct super_block
*sbp
;
3014 struct superblock_smack
*sbsp
;
3015 struct inode_smack
*isp
;
3016 struct smack_known
*skp
;
3017 struct smack_known
*ckp
= smk_of_current();
3018 struct smack_known
*final
;
3019 char trattr
[TRANS_TRUE_SIZE
];
3027 isp
= inode
->i_security
;
3029 mutex_lock(&isp
->smk_lock
);
3031 * If the inode is already instantiated
3032 * take the quick way out
3034 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3038 sbsp
= sbp
->s_security
;
3040 * We're going to use the superblock default label
3041 * if there's no label on the file.
3043 final
= sbsp
->smk_default
;
3046 * If this is the root inode the superblock
3047 * may be in the process of initialization.
3048 * If that is the case use the root value out
3049 * of the superblock.
3051 if (opt_dentry
->d_parent
== opt_dentry
) {
3052 switch (sbp
->s_magic
) {
3053 case CGROUP_SUPER_MAGIC
:
3055 * The cgroup filesystem is never mounted,
3056 * so there's no opportunity to set the mount
3059 sbsp
->smk_root
= &smack_known_star
;
3060 sbsp
->smk_default
= &smack_known_star
;
3061 isp
->smk_inode
= sbsp
->smk_root
;
3065 * What about shmem/tmpfs anonymous files with dentry
3066 * obtained from d_alloc_pseudo()?
3068 isp
->smk_inode
= smk_of_current();
3071 isp
->smk_inode
= sbsp
->smk_root
;
3074 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3079 * This is pretty hackish.
3080 * Casey says that we shouldn't have to do
3081 * file system specific code, but it does help
3082 * with keeping it simple.
3084 switch (sbp
->s_magic
) {
3088 case CGROUP_SUPER_MAGIC
:
3090 * Casey says that it's a little embarrassing
3091 * that the smack file system doesn't do
3092 * extended attributes.
3094 * Casey says pipes are easy (?)
3096 * Socket access is controlled by the socket
3097 * structures associated with the task involved.
3099 * Cgroupfs is special
3101 final
= &smack_known_star
;
3103 case DEVPTS_SUPER_MAGIC
:
3105 * devpts seems content with the label of the task.
3106 * Programs that change smack have to treat the
3111 case PROC_SUPER_MAGIC
:
3113 * Casey says procfs appears not to care.
3114 * The superblock default suffices.
3119 * Device labels should come from the filesystem,
3120 * but watch out, because they're volitile,
3121 * getting recreated on every reboot.
3123 final
= &smack_known_star
;
3127 * If a smack value has been set we want to use it,
3128 * but since tmpfs isn't giving us the opportunity
3129 * to set mount options simulate setting the
3130 * superblock default.
3134 * This isn't an understood special case.
3135 * Get the value from the xattr.
3139 * UNIX domain sockets use lower level socket data.
3141 if (S_ISSOCK(inode
->i_mode
)) {
3142 final
= &smack_known_star
;
3146 * No xattr support means, alas, no SMACK label.
3147 * Use the aforeapplied default.
3148 * It would be curious if the label of the task
3149 * does not match that assigned.
3151 if (inode
->i_op
->getxattr
== NULL
)
3154 * Get the dentry for xattr.
3156 dp
= dget(opt_dentry
);
3157 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3162 * Transmuting directory
3164 if (S_ISDIR(inode
->i_mode
)) {
3166 * If this is a new directory and the label was
3167 * transmuted when the inode was initialized
3168 * set the transmute attribute on the directory
3169 * and mark the inode.
3171 * If there is a transmute attribute on the
3172 * directory mark the inode.
3174 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3175 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3176 rc
= inode
->i_op
->setxattr(dp
,
3177 XATTR_NAME_SMACKTRANSMUTE
,
3178 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3181 rc
= inode
->i_op
->getxattr(dp
,
3182 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3184 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3185 TRANS_TRUE_SIZE
) != 0)
3189 transflag
= SMK_INODE_TRANSMUTE
;
3192 * Don't let the exec or mmap label be "*" or "@".
3194 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3195 if (skp
== &smack_known_star
|| skp
== &smack_known_web
)
3197 isp
->smk_task
= skp
;
3198 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3199 if (skp
== &smack_known_star
|| skp
== &smack_known_web
)
3201 isp
->smk_mmap
= skp
;
3208 isp
->smk_inode
= ckp
;
3210 isp
->smk_inode
= final
;
3212 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3215 mutex_unlock(&isp
->smk_lock
);
3220 * smack_getprocattr - Smack process attribute access
3221 * @p: the object task
3222 * @name: the name of the attribute in /proc/.../attr
3223 * @value: where to put the result
3225 * Places a copy of the task Smack into value
3227 * Returns the length of the smack label or an error code
3229 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3231 struct smack_known
*skp
= smk_of_task_struct(p
);
3235 if (strcmp(name
, "current") != 0)
3238 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3248 * smack_setprocattr - Smack process attribute setting
3249 * @p: the object task
3250 * @name: the name of the attribute in /proc/.../attr
3251 * @value: the value to set
3252 * @size: the size of the value
3254 * Sets the Smack value of the task. Only setting self
3255 * is permitted and only with privilege
3257 * Returns the length of the smack label or an error code
3259 static int smack_setprocattr(struct task_struct
*p
, char *name
,
3260 void *value
, size_t size
)
3262 struct task_smack
*tsp
;
3264 struct smack_known
*skp
;
3267 * Changing another process' Smack value is too dangerous
3268 * and supports no sane use case.
3273 if (!smack_privileged(CAP_MAC_ADMIN
))
3276 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3279 if (strcmp(name
, "current") != 0)
3282 skp
= smk_import_entry(value
, size
);
3287 * No process is ever allowed the web ("@") label.
3289 if (skp
== &smack_known_web
)
3292 new = prepare_creds();
3296 tsp
= new->security
;
3297 tsp
->smk_task
= skp
;
3304 * smack_unix_stream_connect - Smack access on UDS
3306 * @other: the other sock
3309 * Return 0 if a subject with the smack of sock could access
3310 * an object with the smack of other, otherwise an error code
3312 static int smack_unix_stream_connect(struct sock
*sock
,
3313 struct sock
*other
, struct sock
*newsk
)
3315 struct smack_known
*skp
;
3316 struct smack_known
*okp
;
3317 struct socket_smack
*ssp
= sock
->sk_security
;
3318 struct socket_smack
*osp
= other
->sk_security
;
3319 struct socket_smack
*nsp
= newsk
->sk_security
;
3320 struct smk_audit_info ad
;
3323 struct lsm_network_audit net
;
3326 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3330 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3331 smk_ad_setfield_u_net_sk(&ad
, other
);
3333 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3334 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3338 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3339 rc
= smk_bu_note("UDS connect", okp
, skp
,
3345 * Cross reference the peer labels for SO_PEERSEC.
3348 nsp
->smk_packet
= ssp
->smk_out
;
3349 ssp
->smk_packet
= osp
->smk_out
;
3356 * smack_unix_may_send - Smack access on UDS
3358 * @other: the other socket
3360 * Return 0 if a subject with the smack of sock could access
3361 * an object with the smack of other, otherwise an error code
3363 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3365 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3366 struct socket_smack
*osp
= other
->sk
->sk_security
;
3367 struct smk_audit_info ad
;
3371 struct lsm_network_audit net
;
3373 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3374 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3377 if (smack_privileged(CAP_MAC_OVERRIDE
))
3380 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3381 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3386 * smack_socket_sendmsg - Smack check based on destination host
3389 * @size: the size of the message
3391 * Return 0 if the current subject can write to the destination host.
3392 * For IPv4 this is only a question if the destination is a single label host.
3393 * For IPv6 this is a check against the label of the port.
3395 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3398 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3399 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3400 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3401 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3405 * Perfectly reasonable for this to be NULL
3410 switch (sip
->sin_family
) {
3412 rc
= smack_netlabel_send(sock
->sk
, sip
);
3415 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3416 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3417 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3424 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3425 * @sap: netlabel secattr
3426 * @ssp: socket security information
3428 * Returns a pointer to a Smack label entry found on the label list.
3430 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3431 struct socket_smack
*ssp
)
3433 struct smack_known
*skp
;
3438 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3440 * Looks like a CIPSO packet.
3441 * If there are flags but no level netlabel isn't
3442 * behaving the way we expect it to.
3444 * Look it up in the label table
3445 * Without guidance regarding the smack value
3446 * for the packet fall back on the network
3450 list_for_each_entry(skp
, &smack_known_list
, list
) {
3451 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3454 * Compare the catsets. Use the netlbl APIs.
3456 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3457 if ((skp
->smk_netlabel
.flags
&
3458 NETLBL_SECATTR_MLS_CAT
) == 0)
3462 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3463 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3465 kcat
= netlbl_catmap_walk(
3466 skp
->smk_netlabel
.attr
.mls
.cat
,
3468 if (acat
< 0 || kcat
< 0)
3481 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3482 return &smack_known_web
;
3483 return &smack_known_star
;
3485 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
3487 * Looks like a fallback, which gives us a secid.
3489 skp
= smack_from_secid(sap
->attr
.secid
);
3491 * This has got to be a bug because it is
3492 * impossible to specify a fallback without
3493 * specifying the label, which will ensure
3494 * it has a secid, and the only way to get a
3495 * secid is from a fallback.
3497 BUG_ON(skp
== NULL
);
3501 * Without guidance regarding the smack value
3502 * for the packet fall back on the network
3505 return smack_net_ambient
;
3508 #if IS_ENABLED(CONFIG_IPV6)
3509 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3513 int proto
= -EINVAL
;
3514 struct ipv6hdr _ipv6h
;
3515 struct ipv6hdr
*ip6
;
3517 struct tcphdr _tcph
, *th
;
3518 struct udphdr _udph
, *uh
;
3519 struct dccp_hdr _dccph
, *dh
;
3523 offset
= skb_network_offset(skb
);
3524 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3527 sip
->sin6_addr
= ip6
->saddr
;
3529 nexthdr
= ip6
->nexthdr
;
3530 offset
+= sizeof(_ipv6h
);
3531 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3538 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3540 sip
->sin6_port
= th
->source
;
3543 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3545 sip
->sin6_port
= uh
->source
;
3548 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3550 sip
->sin6_port
= dh
->dccph_sport
;
3555 #endif /* CONFIG_IPV6 */
3558 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3562 * Returns 0 if the packet should be delivered, an error code otherwise
3564 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3566 struct netlbl_lsm_secattr secattr
;
3567 struct socket_smack
*ssp
= sk
->sk_security
;
3568 struct smack_known
*skp
= NULL
;
3570 struct smk_audit_info ad
;
3572 struct lsm_network_audit net
;
3574 #if IS_ENABLED(CONFIG_IPV6)
3575 struct sockaddr_in6 sadd
;
3577 #endif /* CONFIG_IPV6 */
3579 switch (sk
->sk_family
) {
3581 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3583 * If there is a secmark use it rather than the CIPSO label.
3584 * If there is no secmark fall back to CIPSO.
3585 * The secmark is assumed to reflect policy better.
3587 if (skb
&& skb
->secmark
!= 0) {
3588 skp
= smack_from_secid(skb
->secmark
);
3591 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3593 * Translate what netlabel gave us.
3595 netlbl_secattr_init(&secattr
);
3597 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
3599 skp
= smack_from_secattr(&secattr
, ssp
);
3601 skp
= smack_net_ambient
;
3603 netlbl_secattr_destroy(&secattr
);
3605 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3609 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3610 ad
.a
.u
.net
->family
= sk
->sk_family
;
3611 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3612 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3615 * Receiving a packet requires that the other end
3616 * be able to write here. Read access is not required.
3617 * This is the simplist possible security model
3620 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3621 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3624 netlbl_skbuff_err(skb
, rc
, 0);
3626 #if IS_ENABLED(CONFIG_IPV6)
3628 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3629 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_TCP
)
3631 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3632 if (skb
&& skb
->secmark
!= 0)
3633 skp
= smack_from_secid(skb
->secmark
);
3635 skp
= smack_net_ambient
;
3637 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3638 ad
.a
.u
.net
->family
= sk
->sk_family
;
3639 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3640 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3641 #endif /* CONFIG_AUDIT */
3642 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3643 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3645 #else /* CONFIG_SECURITY_SMACK_NETFILTER */
3646 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3647 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3649 #endif /* CONFIG_IPV6 */
3656 * smack_socket_getpeersec_stream - pull in packet label
3658 * @optval: user's destination
3659 * @optlen: size thereof
3662 * returns zero on success, an error code otherwise
3664 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3665 char __user
*optval
,
3666 int __user
*optlen
, unsigned len
)
3668 struct socket_smack
*ssp
;
3673 ssp
= sock
->sk
->sk_security
;
3674 if (ssp
->smk_packet
!= NULL
) {
3675 rcp
= ssp
->smk_packet
->smk_known
;
3676 slen
= strlen(rcp
) + 1;
3681 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3684 if (put_user(slen
, optlen
) != 0)
3692 * smack_socket_getpeersec_dgram - pull in packet label
3693 * @sock: the peer socket
3695 * @secid: pointer to where to put the secid of the packet
3697 * Sets the netlabel socket state on sk from parent
3699 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3700 struct sk_buff
*skb
, u32
*secid
)
3703 struct netlbl_lsm_secattr secattr
;
3704 struct socket_smack
*ssp
= NULL
;
3705 struct smack_known
*skp
;
3706 int family
= PF_UNSPEC
;
3707 u32 s
= 0; /* 0 is the invalid secid */
3711 if (skb
->protocol
== htons(ETH_P_IP
))
3713 #if IS_ENABLED(CONFIG_IPV6)
3714 else if (skb
->protocol
== htons(ETH_P_IPV6
))
3716 #endif /* CONFIG_IPV6 */
3718 if (family
== PF_UNSPEC
&& sock
!= NULL
)
3719 family
= sock
->sk
->sk_family
;
3723 ssp
= sock
->sk
->sk_security
;
3724 s
= ssp
->smk_out
->smk_secid
;
3727 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3733 * Translate what netlabel gave us.
3735 if (sock
!= NULL
&& sock
->sk
!= NULL
)
3736 ssp
= sock
->sk
->sk_security
;
3737 netlbl_secattr_init(&secattr
);
3738 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3740 skp
= smack_from_secattr(&secattr
, ssp
);
3743 netlbl_secattr_destroy(&secattr
);
3745 #if IS_ENABLED(CONFIG_IPV6)
3747 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3749 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3751 #endif /* CONFIG_IPV6 */
3760 * smack_sock_graft - Initialize a newly created socket with an existing sock
3762 * @parent: parent socket
3764 * Set the smk_{in,out} state of an existing sock based on the process that
3765 * is creating the new socket.
3767 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3769 struct socket_smack
*ssp
;
3770 struct smack_known
*skp
= smk_of_current();
3773 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3776 ssp
= sk
->sk_security
;
3779 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3783 * smack_inet_conn_request - Smack access check on connect
3784 * @sk: socket involved
3788 * Returns 0 if a task with the packet label could write to
3789 * the socket, otherwise an error code
3791 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
3792 struct request_sock
*req
)
3794 u16 family
= sk
->sk_family
;
3795 struct smack_known
*skp
;
3796 struct socket_smack
*ssp
= sk
->sk_security
;
3797 struct netlbl_lsm_secattr secattr
;
3798 struct sockaddr_in addr
;
3800 struct smack_known
*hskp
;
3802 struct smk_audit_info ad
;
3804 struct lsm_network_audit net
;
3807 #if IS_ENABLED(CONFIG_IPV6)
3808 if (family
== PF_INET6
) {
3810 * Handle mapped IPv4 packets arriving
3811 * via IPv6 sockets. Don't set up netlabel
3812 * processing on IPv6.
3814 if (skb
->protocol
== htons(ETH_P_IP
))
3819 #endif /* CONFIG_IPV6 */
3821 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3823 * If there is a secmark use it rather than the CIPSO label.
3824 * If there is no secmark fall back to CIPSO.
3825 * The secmark is assumed to reflect policy better.
3827 if (skb
&& skb
->secmark
!= 0) {
3828 skp
= smack_from_secid(skb
->secmark
);
3831 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3833 netlbl_secattr_init(&secattr
);
3834 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3836 skp
= smack_from_secattr(&secattr
, ssp
);
3838 skp
= &smack_known_huh
;
3839 netlbl_secattr_destroy(&secattr
);
3841 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3846 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3847 ad
.a
.u
.net
->family
= family
;
3848 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3849 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3852 * Receiving a packet requires that the other end be able to write
3853 * here. Read access is not required.
3855 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3856 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
3861 * Save the peer's label in the request_sock so we can later setup
3862 * smk_packet in the child socket so that SO_PEERCRED can report it.
3864 req
->peer_secid
= skp
->smk_secid
;
3867 * We need to decide if we want to label the incoming connection here
3868 * if we do we only need to label the request_sock and the stack will
3869 * propagate the wire-label to the sock when it is created.
3872 addr
.sin_addr
.s_addr
= hdr
->saddr
;
3874 hskp
= smack_host_label(&addr
);
3878 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
3880 netlbl_req_delattr(req
);
3886 * smack_inet_csk_clone - Copy the connection information to the new socket
3887 * @sk: the new socket
3888 * @req: the connection's request_sock
3890 * Transfer the connection's peer label to the newly created socket.
3892 static void smack_inet_csk_clone(struct sock
*sk
,
3893 const struct request_sock
*req
)
3895 struct socket_smack
*ssp
= sk
->sk_security
;
3896 struct smack_known
*skp
;
3898 if (req
->peer_secid
!= 0) {
3899 skp
= smack_from_secid(req
->peer_secid
);
3900 ssp
->smk_packet
= skp
;
3902 ssp
->smk_packet
= NULL
;
3906 * Key management security hooks
3908 * Casey has not tested key support very heavily.
3909 * The permission check is most likely too restrictive.
3910 * If you care about keys please have a look.
3915 * smack_key_alloc - Set the key security blob
3917 * @cred: the credentials to use
3920 * No allocation required
3924 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
3925 unsigned long flags
)
3927 struct smack_known
*skp
= smk_of_task(cred
->security
);
3929 key
->security
= skp
;
3934 * smack_key_free - Clear the key security blob
3937 * Clear the blob pointer
3939 static void smack_key_free(struct key
*key
)
3941 key
->security
= NULL
;
3945 * smack_key_permission - Smack access on a key
3946 * @key_ref: gets to the object
3947 * @cred: the credentials to use
3948 * @perm: requested key permissions
3950 * Return 0 if the task has read and write to the object,
3951 * an error code otherwise
3953 static int smack_key_permission(key_ref_t key_ref
,
3954 const struct cred
*cred
, unsigned perm
)
3957 struct smk_audit_info ad
;
3958 struct smack_known
*tkp
= smk_of_task(cred
->security
);
3962 keyp
= key_ref_to_ptr(key_ref
);
3966 * If the key hasn't been initialized give it access so that
3969 if (keyp
->security
== NULL
)
3972 * This should not occur
3977 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
3978 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
3979 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
3981 if (perm
& KEY_NEED_READ
)
3983 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
3984 request
= MAY_WRITE
;
3985 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
3986 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
3989 #endif /* CONFIG_KEYS */
3994 * Audit requires a unique representation of each Smack specific
3995 * rule. This unique representation is used to distinguish the
3996 * object to be audited from remaining kernel objects and also
3997 * works as a glue between the audit hooks.
3999 * Since repository entries are added but never deleted, we'll use
4000 * the smack_known label address related to the given audit rule as
4001 * the needed unique representation. This also better fits the smack
4002 * model where nearly everything is a label.
4007 * smack_audit_rule_init - Initialize a smack audit rule
4008 * @field: audit rule fields given from user-space (audit.h)
4009 * @op: required testing operator (=, !=, >, <, ...)
4010 * @rulestr: smack label to be audited
4011 * @vrule: pointer to save our own audit rule representation
4013 * Prepare to audit cases where (@field @op @rulestr) is true.
4014 * The label to be audited is created if necessay.
4016 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4018 struct smack_known
*skp
;
4019 char **rule
= (char **)vrule
;
4022 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4025 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4028 skp
= smk_import_entry(rulestr
, 0);
4030 *rule
= skp
->smk_known
;
4036 * smack_audit_rule_known - Distinguish Smack audit rules
4037 * @krule: rule of interest, in Audit kernel representation format
4039 * This is used to filter Smack rules from remaining Audit ones.
4040 * If it's proved that this rule belongs to us, the
4041 * audit_rule_match hook will be called to do the final judgement.
4043 static int smack_audit_rule_known(struct audit_krule
*krule
)
4045 struct audit_field
*f
;
4048 for (i
= 0; i
< krule
->field_count
; i
++) {
4049 f
= &krule
->fields
[i
];
4051 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4059 * smack_audit_rule_match - Audit given object ?
4060 * @secid: security id for identifying the object to test
4061 * @field: audit rule flags given from user-space
4062 * @op: required testing operator
4063 * @vrule: smack internal rule presentation
4064 * @actx: audit context associated with the check
4066 * The core Audit hook. It's used to take the decision of
4067 * whether to audit or not to audit a given object.
4069 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4070 struct audit_context
*actx
)
4072 struct smack_known
*skp
;
4075 if (unlikely(!rule
)) {
4076 WARN_ONCE(1, "Smack: missing rule\n");
4080 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4083 skp
= smack_from_secid(secid
);
4086 * No need to do string comparisons. If a match occurs,
4087 * both pointers will point to the same smack_known
4090 if (op
== Audit_equal
)
4091 return (rule
== skp
->smk_known
);
4092 if (op
== Audit_not_equal
)
4093 return (rule
!= skp
->smk_known
);
4099 * smack_audit_rule_free - free smack rule representation
4100 * @vrule: rule to be freed.
4102 * No memory was allocated.
4104 static void smack_audit_rule_free(void *vrule
)
4109 #endif /* CONFIG_AUDIT */
4112 * smack_ismaclabel - check if xattr @name references a smack MAC label
4113 * @name: Full xattr name to check.
4115 static int smack_ismaclabel(const char *name
)
4117 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4122 * smack_secid_to_secctx - return the smack label for a secid
4123 * @secid: incoming integer
4124 * @secdata: destination
4125 * @seclen: how long it is
4127 * Exists for networking code.
4129 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4131 struct smack_known
*skp
= smack_from_secid(secid
);
4134 *secdata
= skp
->smk_known
;
4135 *seclen
= strlen(skp
->smk_known
);
4140 * smack_secctx_to_secid - return the secid for a smack label
4141 * @secdata: smack label
4142 * @seclen: how long result is
4143 * @secid: outgoing integer
4145 * Exists for audit and networking code.
4147 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4149 struct smack_known
*skp
= smk_find_entry(secdata
);
4152 *secid
= skp
->smk_secid
;
4159 * smack_release_secctx - don't do anything.
4163 * Exists to make sure nothing gets done, and properly
4165 static void smack_release_secctx(char *secdata
, u32 seclen
)
4169 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4171 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4174 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4176 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4179 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4182 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
4190 struct security_operations smack_ops
= {
4193 .ptrace_access_check
= smack_ptrace_access_check
,
4194 .ptrace_traceme
= smack_ptrace_traceme
,
4195 .syslog
= smack_syslog
,
4197 .sb_alloc_security
= smack_sb_alloc_security
,
4198 .sb_free_security
= smack_sb_free_security
,
4199 .sb_copy_data
= smack_sb_copy_data
,
4200 .sb_kern_mount
= smack_sb_kern_mount
,
4201 .sb_statfs
= smack_sb_statfs
,
4203 .bprm_set_creds
= smack_bprm_set_creds
,
4204 .bprm_committing_creds
= smack_bprm_committing_creds
,
4205 .bprm_secureexec
= smack_bprm_secureexec
,
4207 .inode_alloc_security
= smack_inode_alloc_security
,
4208 .inode_free_security
= smack_inode_free_security
,
4209 .inode_init_security
= smack_inode_init_security
,
4210 .inode_link
= smack_inode_link
,
4211 .inode_unlink
= smack_inode_unlink
,
4212 .inode_rmdir
= smack_inode_rmdir
,
4213 .inode_rename
= smack_inode_rename
,
4214 .inode_permission
= smack_inode_permission
,
4215 .inode_setattr
= smack_inode_setattr
,
4216 .inode_getattr
= smack_inode_getattr
,
4217 .inode_setxattr
= smack_inode_setxattr
,
4218 .inode_post_setxattr
= smack_inode_post_setxattr
,
4219 .inode_getxattr
= smack_inode_getxattr
,
4220 .inode_removexattr
= smack_inode_removexattr
,
4221 .inode_getsecurity
= smack_inode_getsecurity
,
4222 .inode_setsecurity
= smack_inode_setsecurity
,
4223 .inode_listsecurity
= smack_inode_listsecurity
,
4224 .inode_getsecid
= smack_inode_getsecid
,
4226 .file_permission
= smack_file_permission
,
4227 .file_alloc_security
= smack_file_alloc_security
,
4228 .file_free_security
= smack_file_free_security
,
4229 .file_ioctl
= smack_file_ioctl
,
4230 .file_lock
= smack_file_lock
,
4231 .file_fcntl
= smack_file_fcntl
,
4232 .mmap_file
= smack_mmap_file
,
4233 .mmap_addr
= cap_mmap_addr
,
4234 .file_set_fowner
= smack_file_set_fowner
,
4235 .file_send_sigiotask
= smack_file_send_sigiotask
,
4236 .file_receive
= smack_file_receive
,
4238 .file_open
= smack_file_open
,
4240 .cred_alloc_blank
= smack_cred_alloc_blank
,
4241 .cred_free
= smack_cred_free
,
4242 .cred_prepare
= smack_cred_prepare
,
4243 .cred_transfer
= smack_cred_transfer
,
4244 .kernel_act_as
= smack_kernel_act_as
,
4245 .kernel_create_files_as
= smack_kernel_create_files_as
,
4246 .task_setpgid
= smack_task_setpgid
,
4247 .task_getpgid
= smack_task_getpgid
,
4248 .task_getsid
= smack_task_getsid
,
4249 .task_getsecid
= smack_task_getsecid
,
4250 .task_setnice
= smack_task_setnice
,
4251 .task_setioprio
= smack_task_setioprio
,
4252 .task_getioprio
= smack_task_getioprio
,
4253 .task_setscheduler
= smack_task_setscheduler
,
4254 .task_getscheduler
= smack_task_getscheduler
,
4255 .task_movememory
= smack_task_movememory
,
4256 .task_kill
= smack_task_kill
,
4257 .task_wait
= smack_task_wait
,
4258 .task_to_inode
= smack_task_to_inode
,
4260 .ipc_permission
= smack_ipc_permission
,
4261 .ipc_getsecid
= smack_ipc_getsecid
,
4263 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
4264 .msg_msg_free_security
= smack_msg_msg_free_security
,
4266 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
4267 .msg_queue_free_security
= smack_msg_queue_free_security
,
4268 .msg_queue_associate
= smack_msg_queue_associate
,
4269 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
4270 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
4271 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
4273 .shm_alloc_security
= smack_shm_alloc_security
,
4274 .shm_free_security
= smack_shm_free_security
,
4275 .shm_associate
= smack_shm_associate
,
4276 .shm_shmctl
= smack_shm_shmctl
,
4277 .shm_shmat
= smack_shm_shmat
,
4279 .sem_alloc_security
= smack_sem_alloc_security
,
4280 .sem_free_security
= smack_sem_free_security
,
4281 .sem_associate
= smack_sem_associate
,
4282 .sem_semctl
= smack_sem_semctl
,
4283 .sem_semop
= smack_sem_semop
,
4285 .d_instantiate
= smack_d_instantiate
,
4287 .getprocattr
= smack_getprocattr
,
4288 .setprocattr
= smack_setprocattr
,
4290 .unix_stream_connect
= smack_unix_stream_connect
,
4291 .unix_may_send
= smack_unix_may_send
,
4293 .socket_post_create
= smack_socket_post_create
,
4294 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
4295 .socket_bind
= smack_socket_bind
,
4296 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4297 .socket_connect
= smack_socket_connect
,
4298 .socket_sendmsg
= smack_socket_sendmsg
,
4299 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
4300 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
4301 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
4302 .sk_alloc_security
= smack_sk_alloc_security
,
4303 .sk_free_security
= smack_sk_free_security
,
4304 .sock_graft
= smack_sock_graft
,
4305 .inet_conn_request
= smack_inet_conn_request
,
4306 .inet_csk_clone
= smack_inet_csk_clone
,
4308 /* key management security hooks */
4310 .key_alloc
= smack_key_alloc
,
4311 .key_free
= smack_key_free
,
4312 .key_permission
= smack_key_permission
,
4313 #endif /* CONFIG_KEYS */
4317 .audit_rule_init
= smack_audit_rule_init
,
4318 .audit_rule_known
= smack_audit_rule_known
,
4319 .audit_rule_match
= smack_audit_rule_match
,
4320 .audit_rule_free
= smack_audit_rule_free
,
4321 #endif /* CONFIG_AUDIT */
4323 .ismaclabel
= smack_ismaclabel
,
4324 .secid_to_secctx
= smack_secid_to_secctx
,
4325 .secctx_to_secid
= smack_secctx_to_secid
,
4326 .release_secctx
= smack_release_secctx
,
4327 .inode_notifysecctx
= smack_inode_notifysecctx
,
4328 .inode_setsecctx
= smack_inode_setsecctx
,
4329 .inode_getsecctx
= smack_inode_getsecctx
,
4333 static __init
void init_smack_known_list(void)
4336 * Initialize rule list locks
4338 mutex_init(&smack_known_huh
.smk_rules_lock
);
4339 mutex_init(&smack_known_hat
.smk_rules_lock
);
4340 mutex_init(&smack_known_floor
.smk_rules_lock
);
4341 mutex_init(&smack_known_star
.smk_rules_lock
);
4342 mutex_init(&smack_known_invalid
.smk_rules_lock
);
4343 mutex_init(&smack_known_web
.smk_rules_lock
);
4345 * Initialize rule lists
4347 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4348 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4349 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4350 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4351 INIT_LIST_HEAD(&smack_known_invalid
.smk_rules
);
4352 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4354 * Create the known labels list
4356 smk_insert_entry(&smack_known_huh
);
4357 smk_insert_entry(&smack_known_hat
);
4358 smk_insert_entry(&smack_known_star
);
4359 smk_insert_entry(&smack_known_floor
);
4360 smk_insert_entry(&smack_known_invalid
);
4361 smk_insert_entry(&smack_known_web
);
4365 * smack_init - initialize the smack system
4369 static __init
int smack_init(void)
4372 struct task_smack
*tsp
;
4374 if (!security_module_enable(&smack_ops
))
4379 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4380 if (!smack_inode_cache
)
4383 tsp
= new_task_smack(&smack_known_floor
, &smack_known_floor
,
4386 kmem_cache_destroy(smack_inode_cache
);
4390 printk(KERN_INFO
"Smack: Initializing.\n");
4393 * Set the security state for the initial task.
4395 cred
= (struct cred
*) current
->cred
;
4396 cred
->security
= tsp
;
4398 /* initialize the smack_known_list */
4399 init_smack_known_list();
4404 if (register_security(&smack_ops
))
4405 panic("smack: Unable to register with kernel.\n");
4411 * Smack requires early initialization in order to label
4412 * all processes and objects when they are created.
4414 security_initcall(smack_init
);