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 task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
56 LIST_HEAD(smk_ipv6_port_list
);
57 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
58 static struct kmem_cache
*smack_inode_cache
;
61 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
62 static void smk_bu_mode(int mode
, char *s
)
72 if (mode
& MAY_APPEND
)
74 if (mode
& MAY_TRANSMUTE
)
84 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
85 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
86 struct smack_known
*oskp
, int mode
, int rc
)
88 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
93 smk_bu_mode(mode
, acc
);
94 pr_info("Smack Bringup: (%s %s %s) %s\n",
95 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
99 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
102 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
103 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
106 struct task_smack
*tsp
= current_security();
107 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
112 smk_bu_mode(mode
, acc
);
113 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
114 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
115 acc
, current
->comm
, note
);
119 #define smk_bu_current(note, oskp, mode, RC) (RC)
122 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
123 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
125 struct task_smack
*tsp
= current_security();
126 struct task_smack
*otsp
= task_security(otp
);
127 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
132 smk_bu_mode(mode
, acc
);
133 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
134 tsp
->smk_task
->smk_known
, otsp
->smk_task
->smk_known
, acc
,
135 current
->comm
, otp
->comm
);
139 #define smk_bu_task(otp, mode, RC) (RC)
142 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
143 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
145 struct task_smack
*tsp
= current_security();
146 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
151 smk_bu_mode(mode
, acc
);
152 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
153 tsp
->smk_task
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
154 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
158 #define smk_bu_inode(inode, mode, RC) (RC)
161 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
162 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
164 struct task_smack
*tsp
= current_security();
165 struct smack_known
*sskp
= tsp
->smk_task
;
166 struct inode
*inode
= file_inode(file
);
167 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
172 smk_bu_mode(mode
, acc
);
173 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
174 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
175 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
180 #define smk_bu_file(file, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
187 struct task_smack
*tsp
= cred
->security
;
188 struct smack_known
*sskp
= tsp
->smk_task
;
189 struct inode
*inode
= file
->f_inode
;
190 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
195 smk_bu_mode(mode
, acc
);
196 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
197 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
198 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
203 #define smk_bu_credfile(cred, file, mode, RC) (RC)
207 * smk_fetch - Fetch the smack label from a file.
208 * @name: type of the label (attribute)
209 * @ip: a pointer to the inode
210 * @dp: a pointer to the dentry
212 * Returns a pointer to the master list entry for the Smack label
213 * or NULL if there was no label to fetch.
215 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
220 struct smack_known
*skp
= NULL
;
222 if (ip
->i_op
->getxattr
== NULL
)
225 buffer
= kzalloc(SMK_LONGLABEL
, GFP_KERNEL
);
229 rc
= ip
->i_op
->getxattr(dp
, name
, buffer
, SMK_LONGLABEL
);
231 skp
= smk_import_entry(buffer
, rc
);
239 * new_inode_smack - allocate an inode security blob
240 * @skp: a pointer to the Smack label entry to use in the blob
242 * Returns the new blob or NULL if there's no memory available
244 struct inode_smack
*new_inode_smack(struct smack_known
*skp
)
246 struct inode_smack
*isp
;
248 isp
= kmem_cache_zalloc(smack_inode_cache
, GFP_NOFS
);
252 isp
->smk_inode
= skp
;
254 mutex_init(&isp
->smk_lock
);
260 * new_task_smack - allocate a task security blob
261 * @task: a pointer to the Smack label for the running task
262 * @forked: a pointer to the Smack label for the forked task
263 * @gfp: type of the memory for the allocation
265 * Returns the new blob or NULL if there's no memory available
267 static struct task_smack
*new_task_smack(struct smack_known
*task
,
268 struct smack_known
*forked
, gfp_t gfp
)
270 struct task_smack
*tsp
;
272 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
276 tsp
->smk_task
= task
;
277 tsp
->smk_forked
= forked
;
278 INIT_LIST_HEAD(&tsp
->smk_rules
);
279 mutex_init(&tsp
->smk_rules_lock
);
285 * smk_copy_rules - copy a rule set
286 * @nhead: new rules header pointer
287 * @ohead: old rules header pointer
288 * @gfp: type of the memory for the allocation
290 * Returns 0 on success, -ENOMEM on error
292 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
295 struct smack_rule
*nrp
;
296 struct smack_rule
*orp
;
299 INIT_LIST_HEAD(nhead
);
301 list_for_each_entry_rcu(orp
, ohead
, list
) {
302 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
308 list_add_rcu(&nrp
->list
, nhead
);
314 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
315 * @mode - input mode in form of PTRACE_MODE_*
317 * Returns a converted MAY_* mode usable by smack rules
319 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
322 case PTRACE_MODE_READ
:
324 case PTRACE_MODE_ATTACH
:
325 return MAY_READWRITE
;
332 * smk_ptrace_rule_check - helper for ptrace access
333 * @tracer: tracer process
334 * @tracee_known: label entry of the process that's about to be traced
335 * @mode: ptrace attachment mode (PTRACE_MODE_*)
336 * @func: name of the function that called us, used for audit
338 * Returns 0 on access granted, -error on error
340 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
341 struct smack_known
*tracee_known
,
342 unsigned int mode
, const char *func
)
345 struct smk_audit_info ad
, *saip
= NULL
;
346 struct task_smack
*tsp
;
347 struct smack_known
*tracer_known
;
349 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
350 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
351 smk_ad_setfield_u_tsk(&ad
, tracer
);
355 tsp
= task_security(tracer
);
356 tracer_known
= smk_of_task(tsp
);
358 if ((mode
& PTRACE_MODE_ATTACH
) &&
359 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
360 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
361 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
363 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
365 else if (capable(CAP_SYS_PTRACE
))
371 smack_log(tracer_known
->smk_known
,
372 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
);
385 * We he, that is fun!
389 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
390 * @ctp: child task pointer
391 * @mode: ptrace attachment mode (PTRACE_MODE_*)
393 * Returns 0 if access is OK, an error code otherwise
395 * Do the capability checks.
397 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
400 struct smack_known
*skp
;
402 rc
= cap_ptrace_access_check(ctp
, mode
);
406 skp
= smk_of_task(task_security(ctp
));
408 rc
= smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
413 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
414 * @ptp: parent task pointer
416 * Returns 0 if access is OK, an error code otherwise
418 * Do the capability checks, and require PTRACE_MODE_ATTACH.
420 static int smack_ptrace_traceme(struct task_struct
*ptp
)
423 struct smack_known
*skp
;
425 rc
= cap_ptrace_traceme(ptp
);
429 skp
= smk_of_task(current_security());
431 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
436 * smack_syslog - Smack approval on syslog
437 * @type: message type
439 * Returns 0 on success, error code otherwise.
441 static int smack_syslog(int typefrom_file
)
444 struct smack_known
*skp
= smk_of_current();
446 if (smack_privileged(CAP_MAC_OVERRIDE
))
449 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
461 * smack_sb_alloc_security - allocate a superblock blob
462 * @sb: the superblock getting the blob
464 * Returns 0 on success or -ENOMEM on error.
466 static int smack_sb_alloc_security(struct super_block
*sb
)
468 struct superblock_smack
*sbsp
;
470 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
475 sbsp
->smk_root
= &smack_known_floor
;
476 sbsp
->smk_default
= &smack_known_floor
;
477 sbsp
->smk_floor
= &smack_known_floor
;
478 sbsp
->smk_hat
= &smack_known_hat
;
480 * smk_initialized will be zero from kzalloc.
482 sb
->s_security
= sbsp
;
488 * smack_sb_free_security - free a superblock blob
489 * @sb: the superblock getting the blob
492 static void smack_sb_free_security(struct super_block
*sb
)
494 kfree(sb
->s_security
);
495 sb
->s_security
= NULL
;
499 * smack_sb_copy_data - copy mount options data for processing
500 * @orig: where to start
501 * @smackopts: mount options string
503 * Returns 0 on success or -ENOMEM on error.
505 * Copy the Smack specific mount options out of the mount
508 static int smack_sb_copy_data(char *orig
, char *smackopts
)
510 char *cp
, *commap
, *otheropts
, *dp
;
512 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
513 if (otheropts
== NULL
)
516 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
517 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
519 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
521 else if (strstr(cp
, SMK_FSHAT
) == cp
)
523 else if (strstr(cp
, SMK_FSROOT
) == cp
)
525 else if (strstr(cp
, SMK_FSTRANS
) == cp
)
530 commap
= strchr(cp
, ',');
539 strcpy(orig
, otheropts
);
540 free_page((unsigned long)otheropts
);
546 * smack_sb_kern_mount - Smack specific mount processing
547 * @sb: the file system superblock
548 * @flags: the mount flags
549 * @data: the smack mount options
551 * Returns 0 on success, an error code on failure
553 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
555 struct dentry
*root
= sb
->s_root
;
556 struct inode
*inode
= root
->d_inode
;
557 struct superblock_smack
*sp
= sb
->s_security
;
558 struct inode_smack
*isp
;
559 struct smack_known
*skp
;
565 if (sp
->smk_initialized
)
568 sp
->smk_initialized
= 1;
570 for (op
= data
; op
!= NULL
; op
= commap
) {
571 commap
= strchr(op
, ',');
575 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
576 op
+= strlen(SMK_FSHAT
);
577 skp
= smk_import_entry(op
, 0);
582 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
583 op
+= strlen(SMK_FSFLOOR
);
584 skp
= smk_import_entry(op
, 0);
589 } else if (strncmp(op
, SMK_FSDEFAULT
,
590 strlen(SMK_FSDEFAULT
)) == 0) {
591 op
+= strlen(SMK_FSDEFAULT
);
592 skp
= smk_import_entry(op
, 0);
594 sp
->smk_default
= skp
;
597 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
598 op
+= strlen(SMK_FSROOT
);
599 skp
= smk_import_entry(op
, 0);
604 } else if (strncmp(op
, SMK_FSTRANS
, strlen(SMK_FSTRANS
)) == 0) {
605 op
+= strlen(SMK_FSTRANS
);
606 skp
= smk_import_entry(op
, 0);
615 if (!smack_privileged(CAP_MAC_ADMIN
)) {
617 * Unprivileged mounts don't get to specify Smack values.
622 * Unprivileged mounts get root and default from the caller.
624 skp
= smk_of_current();
626 sp
->smk_default
= skp
;
629 * Initialize the root inode.
631 isp
= inode
->i_security
;
633 isp
= new_inode_smack(sp
->smk_root
);
636 inode
->i_security
= isp
;
638 isp
->smk_inode
= sp
->smk_root
;
641 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
647 * smack_sb_statfs - Smack check on statfs
648 * @dentry: identifies the file system in question
650 * Returns 0 if current can read the floor of the filesystem,
651 * and error code otherwise
653 static int smack_sb_statfs(struct dentry
*dentry
)
655 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
657 struct smk_audit_info ad
;
659 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
660 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
662 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
663 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
672 * smack_bprm_set_creds - set creds for exec
673 * @bprm: the exec information
675 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
677 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
679 struct inode
*inode
= file_inode(bprm
->file
);
680 struct task_smack
*bsp
= bprm
->cred
->security
;
681 struct inode_smack
*isp
;
684 rc
= cap_bprm_set_creds(bprm
);
688 if (bprm
->cred_prepared
)
691 isp
= inode
->i_security
;
692 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
695 if (bprm
->unsafe
& (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
696 struct task_struct
*tracer
;
700 tracer
= ptrace_parent(current
);
701 if (likely(tracer
!= NULL
))
702 rc
= smk_ptrace_rule_check(tracer
,
710 } else if (bprm
->unsafe
)
713 bsp
->smk_task
= isp
->smk_task
;
714 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
720 * smack_bprm_committing_creds - Prepare to install the new credentials
723 * @bprm: binprm for exec
725 static void smack_bprm_committing_creds(struct linux_binprm
*bprm
)
727 struct task_smack
*bsp
= bprm
->cred
->security
;
729 if (bsp
->smk_task
!= bsp
->smk_forked
)
730 current
->pdeath_signal
= 0;
734 * smack_bprm_secureexec - Return the decision to use secureexec.
735 * @bprm: binprm for exec
737 * Returns 0 on success.
739 static int smack_bprm_secureexec(struct linux_binprm
*bprm
)
741 struct task_smack
*tsp
= current_security();
742 int ret
= cap_bprm_secureexec(bprm
);
744 if (!ret
&& (tsp
->smk_task
!= tsp
->smk_forked
))
755 * smack_inode_alloc_security - allocate an inode blob
756 * @inode: the inode in need of a blob
758 * Returns 0 if it gets a blob, -ENOMEM otherwise
760 static int smack_inode_alloc_security(struct inode
*inode
)
762 struct smack_known
*skp
= smk_of_current();
764 inode
->i_security
= new_inode_smack(skp
);
765 if (inode
->i_security
== NULL
)
771 * smack_inode_free_security - free an inode blob
772 * @inode: the inode with a blob
774 * Clears the blob pointer in inode
776 static void smack_inode_free_security(struct inode
*inode
)
778 kmem_cache_free(smack_inode_cache
, inode
->i_security
);
779 inode
->i_security
= NULL
;
783 * smack_inode_init_security - copy out the smack from an inode
784 * @inode: the newly created inode
785 * @dir: containing directory object
787 * @name: where to put the attribute name
788 * @value: where to put the attribute value
789 * @len: where to put the length of the attribute
791 * Returns 0 if it all works out, -ENOMEM if there's no memory
793 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
794 const struct qstr
*qstr
, const char **name
,
795 void **value
, size_t *len
)
797 struct inode_smack
*issp
= inode
->i_security
;
798 struct smack_known
*skp
= smk_of_current();
799 struct smack_known
*isp
= smk_of_inode(inode
);
800 struct smack_known
*dsp
= smk_of_inode(dir
);
804 *name
= XATTR_SMACK_SUFFIX
;
808 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
813 * If the access rule allows transmutation and
814 * the directory requests transmutation then
815 * by all means transmute.
816 * Mark the inode as changed.
818 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
819 smk_inode_transmutable(dir
)) {
821 issp
->smk_flags
|= SMK_INODE_CHANGED
;
824 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
828 *len
= strlen(isp
->smk_known
);
835 * smack_inode_link - Smack check on link
836 * @old_dentry: the existing object
838 * @new_dentry: the new object
840 * Returns 0 if access is permitted, an error code otherwise
842 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
843 struct dentry
*new_dentry
)
845 struct smack_known
*isp
;
846 struct smk_audit_info ad
;
849 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
850 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
852 isp
= smk_of_inode(old_dentry
->d_inode
);
853 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
854 rc
= smk_bu_inode(old_dentry
->d_inode
, MAY_WRITE
, rc
);
856 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
857 isp
= smk_of_inode(new_dentry
->d_inode
);
858 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
859 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
860 rc
= smk_bu_inode(new_dentry
->d_inode
, MAY_WRITE
, rc
);
867 * smack_inode_unlink - Smack check on inode deletion
868 * @dir: containing directory object
869 * @dentry: file to unlink
871 * Returns 0 if current can write the containing directory
872 * and the object, error code otherwise
874 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
876 struct inode
*ip
= dentry
->d_inode
;
877 struct smk_audit_info ad
;
880 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
881 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
884 * You need write access to the thing you're unlinking
886 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
887 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
890 * You also need write access to the containing directory
892 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
893 smk_ad_setfield_u_fs_inode(&ad
, dir
);
894 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
895 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
901 * smack_inode_rmdir - Smack check on directory deletion
902 * @dir: containing directory object
903 * @dentry: directory to unlink
905 * Returns 0 if current can write the containing directory
906 * and the directory, error code otherwise
908 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
910 struct smk_audit_info ad
;
913 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
914 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
917 * You need write access to the thing you're removing
919 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
920 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
923 * You also need write access to the containing directory
925 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
926 smk_ad_setfield_u_fs_inode(&ad
, dir
);
927 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
928 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
935 * smack_inode_rename - Smack check on rename
937 * @old_dentry: the old object
939 * @new_dentry: the new object
941 * Read and write access is required on both the old and
944 * Returns 0 if access is permitted, an error code otherwise
946 static int smack_inode_rename(struct inode
*old_inode
,
947 struct dentry
*old_dentry
,
948 struct inode
*new_inode
,
949 struct dentry
*new_dentry
)
952 struct smack_known
*isp
;
953 struct smk_audit_info ad
;
955 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
956 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
958 isp
= smk_of_inode(old_dentry
->d_inode
);
959 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
960 rc
= smk_bu_inode(old_dentry
->d_inode
, MAY_READWRITE
, rc
);
962 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
963 isp
= smk_of_inode(new_dentry
->d_inode
);
964 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
965 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
966 rc
= smk_bu_inode(new_dentry
->d_inode
, MAY_READWRITE
, rc
);
972 * smack_inode_permission - Smack version of permission()
973 * @inode: the inode in question
974 * @mask: the access requested
976 * This is the important Smack hook.
978 * Returns 0 if access is permitted, -EACCES otherwise
980 static int smack_inode_permission(struct inode
*inode
, int mask
)
982 struct smk_audit_info ad
;
983 int no_block
= mask
& MAY_NOT_BLOCK
;
986 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
988 * No permission to check. Existence test. Yup, it's there.
993 /* May be droppable after audit */
996 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
997 smk_ad_setfield_u_fs_inode(&ad
, inode
);
998 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
999 rc
= smk_bu_inode(inode
, mask
, rc
);
1004 * smack_inode_setattr - Smack check for setting attributes
1005 * @dentry: the object
1006 * @iattr: for the force flag
1008 * Returns 0 if access is permitted, an error code otherwise
1010 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1012 struct smk_audit_info ad
;
1016 * Need to allow for clearing the setuid bit.
1018 if (iattr
->ia_valid
& ATTR_FORCE
)
1020 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1021 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1023 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1024 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1029 * smack_inode_getattr - Smack check for getting attributes
1030 * @mnt: vfsmount of the object
1031 * @dentry: the object
1033 * Returns 0 if access is permitted, an error code otherwise
1035 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
1037 struct smk_audit_info ad
;
1041 path
.dentry
= dentry
;
1044 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1045 smk_ad_setfield_u_fs_path(&ad
, path
);
1046 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
1047 rc
= smk_bu_inode(dentry
->d_inode
, MAY_READ
, rc
);
1052 * smack_inode_setxattr - Smack check for setting xattrs
1053 * @dentry: the object
1054 * @name: name of the attribute
1055 * @value: value of the attribute
1056 * @size: size of the value
1059 * This protects the Smack attribute explicitly.
1061 * Returns 0 if access is permitted, an error code otherwise
1063 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1064 const void *value
, size_t size
, int flags
)
1066 struct smk_audit_info ad
;
1067 struct smack_known
*skp
;
1069 int check_import
= 0;
1074 * Check label validity here so import won't fail in post_setxattr
1076 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1077 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1078 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1081 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1082 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1086 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1088 if (size
!= TRANS_TRUE_SIZE
||
1089 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1092 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1094 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1097 if (rc
== 0 && check_import
) {
1098 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1099 if (skp
== NULL
|| (check_star
&&
1100 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1104 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1105 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1108 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1109 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1116 * smack_inode_post_setxattr - Apply the Smack update approved above
1118 * @name: attribute name
1119 * @value: attribute value
1120 * @size: attribute size
1123 * Set the pointer in the inode blob to the entry found
1124 * in the master label list.
1126 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1127 const void *value
, size_t size
, int flags
)
1129 struct smack_known
*skp
;
1130 struct inode_smack
*isp
= dentry
->d_inode
->i_security
;
1132 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1133 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1137 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1138 skp
= smk_import_entry(value
, size
);
1140 isp
->smk_inode
= skp
;
1142 isp
->smk_inode
= &smack_known_invalid
;
1143 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1144 skp
= smk_import_entry(value
, size
);
1146 isp
->smk_task
= skp
;
1148 isp
->smk_task
= &smack_known_invalid
;
1149 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1150 skp
= smk_import_entry(value
, size
);
1152 isp
->smk_mmap
= skp
;
1154 isp
->smk_mmap
= &smack_known_invalid
;
1161 * smack_inode_getxattr - Smack check on getxattr
1162 * @dentry: the object
1165 * Returns 0 if access is permitted, an error code otherwise
1167 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1169 struct smk_audit_info ad
;
1172 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1173 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1175 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
1176 rc
= smk_bu_inode(dentry
->d_inode
, MAY_READ
, rc
);
1181 * smack_inode_removexattr - Smack check on removexattr
1182 * @dentry: the object
1183 * @name: name of the attribute
1185 * Removing the Smack attribute requires CAP_MAC_ADMIN
1187 * Returns 0 if access is permitted, an error code otherwise
1189 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1191 struct inode_smack
*isp
;
1192 struct smk_audit_info ad
;
1195 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1196 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1197 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1198 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1199 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1200 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1201 if (!smack_privileged(CAP_MAC_ADMIN
))
1204 rc
= cap_inode_removexattr(dentry
, name
);
1209 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1210 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1212 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
1213 rc
= smk_bu_inode(dentry
->d_inode
, MAY_WRITE
, rc
);
1217 isp
= dentry
->d_inode
->i_security
;
1219 * Don't do anything special for these.
1220 * XATTR_NAME_SMACKIPIN
1221 * XATTR_NAME_SMACKIPOUT
1222 * XATTR_NAME_SMACKEXEC
1224 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
1225 isp
->smk_task
= NULL
;
1226 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1227 isp
->smk_mmap
= NULL
;
1228 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1229 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1235 * smack_inode_getsecurity - get smack xattrs
1236 * @inode: the object
1237 * @name: attribute name
1238 * @buffer: where to put the result
1241 * Returns the size of the attribute or an error code
1243 static int smack_inode_getsecurity(const struct inode
*inode
,
1244 const char *name
, void **buffer
,
1247 struct socket_smack
*ssp
;
1248 struct socket
*sock
;
1249 struct super_block
*sbp
;
1250 struct inode
*ip
= (struct inode
*)inode
;
1251 struct smack_known
*isp
;
1255 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1256 isp
= smk_of_inode(inode
);
1257 ilen
= strlen(isp
->smk_known
);
1258 *buffer
= isp
->smk_known
;
1263 * The rest of the Smack xattrs are only on sockets.
1266 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1269 sock
= SOCKET_I(ip
);
1270 if (sock
== NULL
|| sock
->sk
== NULL
)
1273 ssp
= sock
->sk
->sk_security
;
1275 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1277 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1282 ilen
= strlen(isp
->smk_known
);
1284 *buffer
= isp
->smk_known
;
1293 * smack_inode_listsecurity - list the Smack attributes
1294 * @inode: the object
1295 * @buffer: where they go
1296 * @buffer_size: size of buffer
1298 * Returns 0 on success, -EINVAL otherwise
1300 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1303 int len
= sizeof(XATTR_NAME_SMACK
);
1305 if (buffer
!= NULL
&& len
<= buffer_size
)
1306 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1312 * smack_inode_getsecid - Extract inode's security id
1313 * @inode: inode to extract the info from
1314 * @secid: where result will be saved
1316 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
1318 struct inode_smack
*isp
= inode
->i_security
;
1320 *secid
= isp
->smk_inode
->smk_secid
;
1328 * smack_file_permission - Smack check on file operations
1334 * Should access checks be done on each read or write?
1335 * UNICOS and SELinux say yes.
1336 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1338 * I'll say no for now. Smack does not do the frequent
1339 * label changing that SELinux does.
1341 static int smack_file_permission(struct file
*file
, int mask
)
1347 * smack_file_alloc_security - assign a file security blob
1350 * The security blob for a file is a pointer to the master
1351 * label list, so no allocation is done.
1353 * f_security is the owner security information. It
1354 * isn't used on file access checks, it's for send_sigio.
1358 static int smack_file_alloc_security(struct file
*file
)
1360 struct smack_known
*skp
= smk_of_current();
1362 file
->f_security
= skp
;
1367 * smack_file_free_security - clear a file security blob
1370 * The security blob for a file is a pointer to the master
1371 * label list, so no memory is freed.
1373 static void smack_file_free_security(struct file
*file
)
1375 file
->f_security
= NULL
;
1379 * smack_file_ioctl - Smack check on ioctls
1384 * Relies heavily on the correct use of the ioctl command conventions.
1386 * Returns 0 if allowed, error code otherwise
1388 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1392 struct smk_audit_info ad
;
1393 struct inode
*inode
= file_inode(file
);
1395 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1396 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1398 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1399 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1400 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1403 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1404 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1405 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1412 * smack_file_lock - Smack check on file locking
1416 * Returns 0 if current has lock access, error code otherwise
1418 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1420 struct smk_audit_info ad
;
1422 struct inode
*inode
= file_inode(file
);
1424 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1425 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1426 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1427 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1432 * smack_file_fcntl - Smack check on fcntl
1434 * @cmd: what action to check
1437 * Generally these operations are harmless.
1438 * File locking operations present an obvious mechanism
1439 * for passing information, so they require write access.
1441 * Returns 0 if current has access, error code otherwise
1443 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1446 struct smk_audit_info ad
;
1448 struct inode
*inode
= file_inode(file
);
1455 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1456 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1457 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1458 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1462 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1463 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1464 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1465 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1476 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1477 * if mapping anonymous memory.
1478 * @file contains the file structure for file to map (may be NULL).
1479 * @reqprot contains the protection requested by the application.
1480 * @prot contains the protection that will be applied by the kernel.
1481 * @flags contains the operational flags.
1482 * Return 0 if permission is granted.
1484 static int smack_mmap_file(struct file
*file
,
1485 unsigned long reqprot
, unsigned long prot
,
1486 unsigned long flags
)
1488 struct smack_known
*skp
;
1489 struct smack_known
*mkp
;
1490 struct smack_rule
*srp
;
1491 struct task_smack
*tsp
;
1492 struct smack_known
*okp
;
1493 struct inode_smack
*isp
;
1502 isp
= file_inode(file
)->i_security
;
1503 if (isp
->smk_mmap
== NULL
)
1505 mkp
= isp
->smk_mmap
;
1507 tsp
= current_security();
1508 skp
= smk_of_current();
1513 * For each Smack rule associated with the subject
1514 * label verify that the SMACK64MMAP also has access
1515 * to that rule's object label.
1517 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1518 okp
= srp
->smk_object
;
1520 * Matching labels always allows access.
1522 if (mkp
->smk_known
== okp
->smk_known
)
1525 * If there is a matching local rule take
1526 * that into account as well.
1528 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1532 may
= srp
->smk_access
;
1534 may
&= srp
->smk_access
;
1536 * If may is zero the SMACK64MMAP subject can't
1537 * possibly have less access.
1543 * Fetch the global list entry.
1544 * If there isn't one a SMACK64MMAP subject
1545 * can't have as much access as current.
1547 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1549 if (mmay
== -ENOENT
) {
1554 * If there is a local entry it modifies the
1555 * potential access, too.
1557 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1559 if (tmay
!= -ENOENT
)
1563 * If there is any access available to current that is
1564 * not available to a SMACK64MMAP subject
1567 if ((may
| mmay
) != mmay
) {
1579 * smack_file_set_fowner - set the file security blob value
1580 * @file: object in question
1583 static void smack_file_set_fowner(struct file
*file
)
1585 file
->f_security
= smk_of_current();
1589 * smack_file_send_sigiotask - Smack on sigio
1590 * @tsk: The target task
1591 * @fown: the object the signal come from
1594 * Allow a privileged task to get signals even if it shouldn't
1596 * Returns 0 if a subject with the object's smack could
1597 * write to the task, an error code otherwise.
1599 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1600 struct fown_struct
*fown
, int signum
)
1602 struct smack_known
*skp
;
1603 struct smack_known
*tkp
= smk_of_task(tsk
->cred
->security
);
1606 struct smk_audit_info ad
;
1609 * struct fown_struct is never outside the context of a struct file
1611 file
= container_of(fown
, struct file
, f_owner
);
1613 /* we don't log here as rc can be overriden */
1614 skp
= file
->f_security
;
1615 rc
= smk_access(skp
, tkp
, MAY_WRITE
, NULL
);
1616 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_WRITE
, rc
);
1617 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1620 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1621 smk_ad_setfield_u_tsk(&ad
, tsk
);
1622 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_WRITE
, rc
, &ad
);
1627 * smack_file_receive - Smack file receive check
1630 * Returns 0 if current has access, error code otherwise
1632 static int smack_file_receive(struct file
*file
)
1636 struct smk_audit_info ad
;
1637 struct inode
*inode
= file_inode(file
);
1639 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1640 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1642 * This code relies on bitmasks.
1644 if (file
->f_mode
& FMODE_READ
)
1646 if (file
->f_mode
& FMODE_WRITE
)
1649 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1650 rc
= smk_bu_file(file
, may
, rc
);
1655 * smack_file_open - Smack dentry open processing
1657 * @cred: task credential
1659 * Set the security blob in the file structure.
1660 * Allow the open only if the task has read access. There are
1661 * many read operations (e.g. fstat) that you can do with an
1662 * fd even if you have the file open write-only.
1666 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1668 struct task_smack
*tsp
= cred
->security
;
1669 struct inode
*inode
= file_inode(file
);
1670 struct smk_audit_info ad
;
1673 if (smack_privileged(CAP_MAC_OVERRIDE
))
1676 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1677 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1678 rc
= smk_access(tsp
->smk_task
, smk_of_inode(inode
), MAY_READ
, &ad
);
1679 rc
= smk_bu_credfile(cred
, file
, MAY_READ
, rc
);
1689 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1690 * @new: the new credentials
1691 * @gfp: the atomicity of any memory allocations
1693 * Prepare a blank set of credentials for modification. This must allocate all
1694 * the memory the LSM module might require such that cred_transfer() can
1695 * complete without error.
1697 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1699 struct task_smack
*tsp
;
1701 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1705 cred
->security
= tsp
;
1712 * smack_cred_free - "free" task-level security credentials
1713 * @cred: the credentials in question
1716 static void smack_cred_free(struct cred
*cred
)
1718 struct task_smack
*tsp
= cred
->security
;
1719 struct smack_rule
*rp
;
1720 struct list_head
*l
;
1721 struct list_head
*n
;
1725 cred
->security
= NULL
;
1727 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1728 rp
= list_entry(l
, struct smack_rule
, list
);
1729 list_del(&rp
->list
);
1736 * smack_cred_prepare - prepare new set of credentials for modification
1737 * @new: the new credentials
1738 * @old: the original credentials
1739 * @gfp: the atomicity of any memory allocations
1741 * Prepare a new set of credentials for modification.
1743 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1746 struct task_smack
*old_tsp
= old
->security
;
1747 struct task_smack
*new_tsp
;
1750 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
1751 if (new_tsp
== NULL
)
1754 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1758 new->security
= new_tsp
;
1763 * smack_cred_transfer - Transfer the old credentials to the new credentials
1764 * @new: the new credentials
1765 * @old: the original credentials
1767 * Fill in a set of blank credentials from another set of credentials.
1769 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1771 struct task_smack
*old_tsp
= old
->security
;
1772 struct task_smack
*new_tsp
= new->security
;
1774 new_tsp
->smk_task
= old_tsp
->smk_task
;
1775 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1776 mutex_init(&new_tsp
->smk_rules_lock
);
1777 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1780 /* cbs copy rule list */
1784 * smack_kernel_act_as - Set the subjective context in a set of credentials
1785 * @new: points to the set of credentials to be modified.
1786 * @secid: specifies the security ID to be set
1788 * Set the security data for a kernel service.
1790 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1792 struct task_smack
*new_tsp
= new->security
;
1793 struct smack_known
*skp
= smack_from_secid(secid
);
1798 new_tsp
->smk_task
= skp
;
1803 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1804 * @new: points to the set of credentials to be modified
1805 * @inode: points to the inode to use as a reference
1807 * Set the file creation context in a set of credentials to the same
1808 * as the objective context of the specified inode
1810 static int smack_kernel_create_files_as(struct cred
*new,
1811 struct inode
*inode
)
1813 struct inode_smack
*isp
= inode
->i_security
;
1814 struct task_smack
*tsp
= new->security
;
1816 tsp
->smk_forked
= isp
->smk_inode
;
1817 tsp
->smk_task
= tsp
->smk_forked
;
1822 * smk_curacc_on_task - helper to log task related access
1823 * @p: the task object
1824 * @access: the access requested
1825 * @caller: name of the calling function for audit
1827 * Return 0 if access is permitted
1829 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
1832 struct smk_audit_info ad
;
1833 struct smack_known
*skp
= smk_of_task(task_security(p
));
1836 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
1837 smk_ad_setfield_u_tsk(&ad
, p
);
1838 rc
= smk_curacc(skp
, access
, &ad
);
1839 rc
= smk_bu_task(p
, access
, rc
);
1844 * smack_task_setpgid - Smack check on setting pgid
1845 * @p: the task object
1848 * Return 0 if write access is permitted
1850 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1852 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1856 * smack_task_getpgid - Smack access check for getpgid
1857 * @p: the object task
1859 * Returns 0 if current can read the object task, error code otherwise
1861 static int smack_task_getpgid(struct task_struct
*p
)
1863 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1867 * smack_task_getsid - Smack access check for getsid
1868 * @p: the object task
1870 * Returns 0 if current can read the object task, error code otherwise
1872 static int smack_task_getsid(struct task_struct
*p
)
1874 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1878 * smack_task_getsecid - get the secid of the task
1879 * @p: the object task
1880 * @secid: where to put the result
1882 * Sets the secid to contain a u32 version of the smack label.
1884 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1886 struct smack_known
*skp
= smk_of_task(task_security(p
));
1888 *secid
= skp
->smk_secid
;
1892 * smack_task_setnice - Smack check on setting nice
1893 * @p: the task object
1896 * Return 0 if write access is permitted
1898 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1902 rc
= cap_task_setnice(p
, nice
);
1904 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1909 * smack_task_setioprio - Smack check on setting ioprio
1910 * @p: the task object
1913 * Return 0 if write access is permitted
1915 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1919 rc
= cap_task_setioprio(p
, ioprio
);
1921 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1926 * smack_task_getioprio - Smack check on reading ioprio
1927 * @p: the task object
1929 * Return 0 if read access is permitted
1931 static int smack_task_getioprio(struct task_struct
*p
)
1933 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1937 * smack_task_setscheduler - Smack check on setting scheduler
1938 * @p: the task object
1942 * Return 0 if read access is permitted
1944 static int smack_task_setscheduler(struct task_struct
*p
)
1948 rc
= cap_task_setscheduler(p
);
1950 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1955 * smack_task_getscheduler - Smack check on reading scheduler
1956 * @p: the task object
1958 * Return 0 if read access is permitted
1960 static int smack_task_getscheduler(struct task_struct
*p
)
1962 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1966 * smack_task_movememory - Smack check on moving memory
1967 * @p: the task object
1969 * Return 0 if write access is permitted
1971 static int smack_task_movememory(struct task_struct
*p
)
1973 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1977 * smack_task_kill - Smack check on signal delivery
1978 * @p: the task object
1981 * @secid: identifies the smack to use in lieu of current's
1983 * Return 0 if write access is permitted
1985 * The secid behavior is an artifact of an SELinux hack
1986 * in the USB code. Someday it may go away.
1988 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1991 struct smk_audit_info ad
;
1992 struct smack_known
*skp
;
1993 struct smack_known
*tkp
= smk_of_task(task_security(p
));
1996 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1997 smk_ad_setfield_u_tsk(&ad
, p
);
1999 * Sending a signal requires that the sender
2000 * can write the receiver.
2003 rc
= smk_curacc(tkp
, MAY_WRITE
, &ad
);
2004 rc
= smk_bu_task(p
, MAY_WRITE
, rc
);
2008 * If the secid isn't 0 we're dealing with some USB IO
2009 * specific behavior. This is not clean. For one thing
2010 * we can't take privilege into account.
2012 skp
= smack_from_secid(secid
);
2013 rc
= smk_access(skp
, tkp
, MAY_WRITE
, &ad
);
2014 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_WRITE
, rc
);
2019 * smack_task_wait - Smack access check for waiting
2020 * @p: task to wait for
2024 static int smack_task_wait(struct task_struct
*p
)
2027 * Allow the operation to succeed.
2029 * In userless environments (e.g. phones) programs
2030 * get marked with SMACK64EXEC and even if the parent
2031 * and child shouldn't be talking the parent still
2032 * may expect to know when the child exits.
2038 * smack_task_to_inode - copy task smack into the inode blob
2039 * @p: task to copy from
2040 * @inode: inode to copy to
2042 * Sets the smack pointer in the inode security blob
2044 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2046 struct inode_smack
*isp
= inode
->i_security
;
2047 struct smack_known
*skp
= smk_of_task(task_security(p
));
2049 isp
->smk_inode
= skp
;
2057 * smack_sk_alloc_security - Allocate a socket blob
2060 * @gfp_flags: memory allocation flags
2062 * Assign Smack pointers to current
2064 * Returns 0 on success, -ENOMEM is there's no memory
2066 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2068 struct smack_known
*skp
= smk_of_current();
2069 struct socket_smack
*ssp
;
2071 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2077 ssp
->smk_packet
= NULL
;
2079 sk
->sk_security
= ssp
;
2085 * smack_sk_free_security - Free a socket blob
2088 * Clears the blob pointer
2090 static void smack_sk_free_security(struct sock
*sk
)
2092 kfree(sk
->sk_security
);
2096 * smack_host_label - check host based restrictions
2097 * @sip: the object end
2099 * looks for host based access restrictions
2101 * This version will only be appropriate for really small sets of single label
2102 * hosts. The caller is responsible for ensuring that the RCU read lock is
2103 * taken before calling this function.
2105 * Returns the label of the far end or NULL if it's not special.
2107 static struct smack_known
*smack_host_label(struct sockaddr_in
*sip
)
2109 struct smk_netlbladdr
*snp
;
2110 struct in_addr
*siap
= &sip
->sin_addr
;
2112 if (siap
->s_addr
== 0)
2115 list_for_each_entry_rcu(snp
, &smk_netlbladdr_list
, list
)
2117 * we break after finding the first match because
2118 * the list is sorted from longest to shortest mask
2119 * so we have found the most specific match
2121 if ((&snp
->smk_host
.sin_addr
)->s_addr
==
2122 (siap
->s_addr
& (&snp
->smk_mask
)->s_addr
)) {
2123 /* we have found the special CIPSO option */
2124 if (snp
->smk_label
== &smack_cipso_option
)
2126 return snp
->smk_label
;
2133 * smack_netlabel - Set the secattr on a socket
2135 * @labeled: socket label scheme
2137 * Convert the outbound smack value (smk_out) to a
2138 * secattr and attach it to the socket.
2140 * Returns 0 on success or an error code
2142 static int smack_netlabel(struct sock
*sk
, int labeled
)
2144 struct smack_known
*skp
;
2145 struct socket_smack
*ssp
= sk
->sk_security
;
2149 * Usually the netlabel code will handle changing the
2150 * packet labeling based on the label.
2151 * The case of a single label host is different, because
2152 * a single label host should never get a labeled packet
2153 * even though the label is usually associated with a packet
2157 bh_lock_sock_nested(sk
);
2159 if (ssp
->smk_out
== smack_net_ambient
||
2160 labeled
== SMACK_UNLABELED_SOCKET
)
2161 netlbl_sock_delattr(sk
);
2164 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2174 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2176 * @sap: the destination address
2178 * Set the correct secattr for the given socket based on the destination
2179 * address and perform any outbound access checks needed.
2181 * Returns 0 on success or an error code.
2184 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2186 struct smack_known
*skp
;
2189 struct smack_known
*hkp
;
2190 struct socket_smack
*ssp
= sk
->sk_security
;
2191 struct smk_audit_info ad
;
2194 hkp
= smack_host_label(sap
);
2197 struct lsm_network_audit net
;
2199 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2200 ad
.a
.u
.net
->family
= sap
->sin_family
;
2201 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2202 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2204 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2206 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2207 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2209 sk_lbl
= SMACK_CIPSO_SOCKET
;
2216 return smack_netlabel(sk
, sk_lbl
);
2219 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2221 * smk_ipv6_port_label - Smack port access table management
2225 * Create or update the port list entry
2227 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2229 struct sock
*sk
= sock
->sk
;
2230 struct sockaddr_in6
*addr6
;
2231 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2232 struct smk_port_label
*spp
;
2233 unsigned short port
= 0;
2235 if (address
== NULL
) {
2237 * This operation is changing the Smack information
2238 * on the bound socket. Take the changes to the port
2241 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2242 if (sk
!= spp
->smk_sock
)
2244 spp
->smk_in
= ssp
->smk_in
;
2245 spp
->smk_out
= ssp
->smk_out
;
2249 * A NULL address is only used for updating existing
2250 * bound entries. If there isn't one, it's OK.
2255 addr6
= (struct sockaddr_in6
*)address
;
2256 port
= ntohs(addr6
->sin6_port
);
2258 * This is a special case that is safely ignored.
2264 * Look for an existing port list entry.
2265 * This is an indication that a port is getting reused.
2267 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2268 if (spp
->smk_port
!= port
)
2270 spp
->smk_port
= port
;
2272 spp
->smk_in
= ssp
->smk_in
;
2273 spp
->smk_out
= ssp
->smk_out
;
2278 * A new port entry is required.
2280 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2284 spp
->smk_port
= port
;
2286 spp
->smk_in
= ssp
->smk_in
;
2287 spp
->smk_out
= ssp
->smk_out
;
2289 list_add(&spp
->list
, &smk_ipv6_port_list
);
2294 * smk_ipv6_port_check - check Smack port access
2298 * Create or update the port list entry
2300 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2305 struct smk_port_label
*spp
;
2306 struct socket_smack
*ssp
= sk
->sk_security
;
2307 struct smack_known
*skp
;
2308 unsigned short port
= 0;
2309 struct smack_known
*object
;
2310 struct smk_audit_info ad
;
2313 struct lsm_network_audit net
;
2316 if (act
== SMK_RECEIVING
) {
2317 skp
= smack_net_ambient
;
2318 object
= ssp
->smk_in
;
2321 object
= smack_net_ambient
;
2325 * Get the IP address and port from the address.
2327 port
= ntohs(address
->sin6_port
);
2328 bep
= (__be16
*)(&address
->sin6_addr
);
2329 be32p
= (__be32
*)(&address
->sin6_addr
);
2332 * It's remote, so port lookup does no good.
2334 if (be32p
[0] || be32p
[1] || be32p
[2] || bep
[6] || ntohs(bep
[7]) != 1)
2338 * It's local so the send check has to have passed.
2340 if (act
== SMK_RECEIVING
) {
2341 skp
= &smack_known_web
;
2345 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2346 if (spp
->smk_port
!= port
)
2348 object
= spp
->smk_in
;
2349 if (act
== SMK_CONNECTING
)
2350 ssp
->smk_packet
= spp
->smk_out
;
2357 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2358 ad
.a
.u
.net
->family
= sk
->sk_family
;
2359 ad
.a
.u
.net
->dport
= port
;
2360 if (act
== SMK_RECEIVING
)
2361 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2363 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2365 rc
= smk_access(skp
, object
, MAY_WRITE
, &ad
);
2366 rc
= smk_bu_note("IPv6 port check", skp
, object
, MAY_WRITE
, rc
);
2369 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2372 * smack_inode_setsecurity - set smack xattrs
2373 * @inode: the object
2374 * @name: attribute name
2375 * @value: attribute value
2376 * @size: size of the attribute
2379 * Sets the named attribute in the appropriate blob
2381 * Returns 0 on success, or an error code
2383 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2384 const void *value
, size_t size
, int flags
)
2386 struct smack_known
*skp
;
2387 struct inode_smack
*nsp
= inode
->i_security
;
2388 struct socket_smack
*ssp
;
2389 struct socket
*sock
;
2392 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2395 skp
= smk_import_entry(value
, size
);
2399 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2400 nsp
->smk_inode
= skp
;
2401 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2405 * The rest of the Smack xattrs are only on sockets.
2407 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2410 sock
= SOCKET_I(inode
);
2411 if (sock
== NULL
|| sock
->sk
== NULL
)
2414 ssp
= sock
->sk
->sk_security
;
2416 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2418 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2420 if (sock
->sk
->sk_family
== PF_INET
) {
2421 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2424 "Smack: \"%s\" netlbl error %d.\n",
2430 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2431 if (sock
->sk
->sk_family
== PF_INET6
)
2432 smk_ipv6_port_label(sock
, NULL
);
2433 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2439 * smack_socket_post_create - finish socket setup
2441 * @family: protocol family
2446 * Sets the netlabel information on the socket
2448 * Returns 0 on success, and error code otherwise
2450 static int smack_socket_post_create(struct socket
*sock
, int family
,
2451 int type
, int protocol
, int kern
)
2453 if (family
!= PF_INET
|| sock
->sk
== NULL
)
2456 * Set the outbound netlbl.
2458 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2461 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
2463 * smack_socket_bind - record port binding information.
2465 * @address: the port address
2466 * @addrlen: size of the address
2468 * Records the label bound to a port.
2472 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2475 #if IS_ENABLED(CONFIG_IPV6)
2476 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2477 smk_ipv6_port_label(sock
, address
);
2482 #endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
2485 * smack_socket_connect - connect access check
2487 * @sap: the other end
2488 * @addrlen: size of sap
2490 * Verifies that a connection may be possible
2492 * Returns 0 on success, and error code otherwise
2494 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2499 if (sock
->sk
== NULL
)
2502 switch (sock
->sk
->sk_family
) {
2504 if (addrlen
< sizeof(struct sockaddr_in
))
2506 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2509 if (addrlen
< sizeof(struct sockaddr_in6
))
2511 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
2512 rc
= smk_ipv6_port_check(sock
->sk
, (struct sockaddr_in6
*)sap
,
2514 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
2521 * smack_flags_to_may - convert S_ to MAY_ values
2522 * @flags: the S_ value
2524 * Returns the equivalent MAY_ value
2526 static int smack_flags_to_may(int flags
)
2530 if (flags
& S_IRUGO
)
2532 if (flags
& S_IWUGO
)
2534 if (flags
& S_IXUGO
)
2541 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2546 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2548 struct smack_known
*skp
= smk_of_current();
2550 msg
->security
= skp
;
2555 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2558 * Clears the blob pointer
2560 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2562 msg
->security
= NULL
;
2566 * smack_of_shm - the smack pointer for the shm
2569 * Returns a pointer to the smack value
2571 static struct smack_known
*smack_of_shm(struct shmid_kernel
*shp
)
2573 return (struct smack_known
*)shp
->shm_perm
.security
;
2577 * smack_shm_alloc_security - Set the security blob for shm
2582 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2584 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2585 struct smack_known
*skp
= smk_of_current();
2587 isp
->security
= skp
;
2592 * smack_shm_free_security - Clear the security blob for shm
2595 * Clears the blob pointer
2597 static void smack_shm_free_security(struct shmid_kernel
*shp
)
2599 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2601 isp
->security
= NULL
;
2605 * smk_curacc_shm : check if current has access on shm
2607 * @access : access requested
2609 * Returns 0 if current has the requested access, error code otherwise
2611 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2613 struct smack_known
*ssp
= smack_of_shm(shp
);
2614 struct smk_audit_info ad
;
2618 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2619 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2621 rc
= smk_curacc(ssp
, access
, &ad
);
2622 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2627 * smack_shm_associate - Smack access check for shm
2629 * @shmflg: access requested
2631 * Returns 0 if current has the requested access, error code otherwise
2633 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2637 may
= smack_flags_to_may(shmflg
);
2638 return smk_curacc_shm(shp
, may
);
2642 * smack_shm_shmctl - Smack access check for shm
2644 * @cmd: what it wants to do
2646 * Returns 0 if current has the requested access, error code otherwise
2648 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2661 may
= MAY_READWRITE
;
2666 * System level information.
2672 return smk_curacc_shm(shp
, may
);
2676 * smack_shm_shmat - Smack access for shmat
2679 * @shmflg: access requested
2681 * Returns 0 if current has the requested access, error code otherwise
2683 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2688 may
= smack_flags_to_may(shmflg
);
2689 return smk_curacc_shm(shp
, may
);
2693 * smack_of_sem - the smack pointer for the sem
2696 * Returns a pointer to the smack value
2698 static struct smack_known
*smack_of_sem(struct sem_array
*sma
)
2700 return (struct smack_known
*)sma
->sem_perm
.security
;
2704 * smack_sem_alloc_security - Set the security blob for sem
2709 static int smack_sem_alloc_security(struct sem_array
*sma
)
2711 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2712 struct smack_known
*skp
= smk_of_current();
2714 isp
->security
= skp
;
2719 * smack_sem_free_security - Clear the security blob for sem
2722 * Clears the blob pointer
2724 static void smack_sem_free_security(struct sem_array
*sma
)
2726 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2728 isp
->security
= NULL
;
2732 * smk_curacc_sem : check if current has access on sem
2734 * @access : access requested
2736 * Returns 0 if current has the requested access, error code otherwise
2738 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2740 struct smack_known
*ssp
= smack_of_sem(sma
);
2741 struct smk_audit_info ad
;
2745 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2746 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2748 rc
= smk_curacc(ssp
, access
, &ad
);
2749 rc
= smk_bu_current("sem", ssp
, access
, rc
);
2754 * smack_sem_associate - Smack access check for sem
2756 * @semflg: access requested
2758 * Returns 0 if current has the requested access, error code otherwise
2760 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2764 may
= smack_flags_to_may(semflg
);
2765 return smk_curacc_sem(sma
, may
);
2769 * smack_sem_shmctl - Smack access check for sem
2771 * @cmd: what it wants to do
2773 * Returns 0 if current has the requested access, error code otherwise
2775 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
2793 may
= MAY_READWRITE
;
2798 * System level information
2805 return smk_curacc_sem(sma
, may
);
2809 * smack_sem_semop - Smack checks of semaphore operations
2815 * Treated as read and write in all cases.
2817 * Returns 0 if access is allowed, error code otherwise
2819 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
2820 unsigned nsops
, int alter
)
2822 return smk_curacc_sem(sma
, MAY_READWRITE
);
2826 * smack_msg_alloc_security - Set the security blob for msg
2831 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
2833 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2834 struct smack_known
*skp
= smk_of_current();
2836 kisp
->security
= skp
;
2841 * smack_msg_free_security - Clear the security blob for msg
2844 * Clears the blob pointer
2846 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
2848 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2850 kisp
->security
= NULL
;
2854 * smack_of_msq - the smack pointer for the msq
2857 * Returns a pointer to the smack label entry
2859 static struct smack_known
*smack_of_msq(struct msg_queue
*msq
)
2861 return (struct smack_known
*)msq
->q_perm
.security
;
2865 * smk_curacc_msq : helper to check if current has access on msq
2867 * @access : access requested
2869 * return 0 if current has access, error otherwise
2871 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
2873 struct smack_known
*msp
= smack_of_msq(msq
);
2874 struct smk_audit_info ad
;
2878 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2879 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
2881 rc
= smk_curacc(msp
, access
, &ad
);
2882 rc
= smk_bu_current("msq", msp
, access
, rc
);
2887 * smack_msg_queue_associate - Smack access check for msg_queue
2889 * @msqflg: access requested
2891 * Returns 0 if current has the requested access, error code otherwise
2893 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
2897 may
= smack_flags_to_may(msqflg
);
2898 return smk_curacc_msq(msq
, may
);
2902 * smack_msg_queue_msgctl - Smack access check for msg_queue
2904 * @cmd: what it wants to do
2906 * Returns 0 if current has the requested access, error code otherwise
2908 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
2919 may
= MAY_READWRITE
;
2924 * System level information
2931 return smk_curacc_msq(msq
, may
);
2935 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2938 * @msqflg: access requested
2940 * Returns 0 if current has the requested access, error code otherwise
2942 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
2947 may
= smack_flags_to_may(msqflg
);
2948 return smk_curacc_msq(msq
, may
);
2952 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2959 * Returns 0 if current has read and write access, error code otherwise
2961 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
2962 struct task_struct
*target
, long type
, int mode
)
2964 return smk_curacc_msq(msq
, MAY_READWRITE
);
2968 * smack_ipc_permission - Smack access for ipc_permission()
2969 * @ipp: the object permissions
2970 * @flag: access requested
2972 * Returns 0 if current has read and write access, error code otherwise
2974 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
2976 struct smack_known
*iskp
= ipp
->security
;
2977 int may
= smack_flags_to_may(flag
);
2978 struct smk_audit_info ad
;
2982 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2983 ad
.a
.u
.ipc_id
= ipp
->id
;
2985 rc
= smk_curacc(iskp
, may
, &ad
);
2986 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
2991 * smack_ipc_getsecid - Extract smack security id
2992 * @ipp: the object permissions
2993 * @secid: where result will be saved
2995 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
2997 struct smack_known
*iskp
= ipp
->security
;
2999 *secid
= iskp
->smk_secid
;
3003 * smack_d_instantiate - Make sure the blob is correct on an inode
3004 * @opt_dentry: dentry where inode will be attached
3005 * @inode: the object
3007 * Set the inode's security blob if it hasn't been done already.
3009 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3011 struct super_block
*sbp
;
3012 struct superblock_smack
*sbsp
;
3013 struct inode_smack
*isp
;
3014 struct smack_known
*skp
;
3015 struct smack_known
*ckp
= smk_of_current();
3016 struct smack_known
*final
;
3017 char trattr
[TRANS_TRUE_SIZE
];
3025 isp
= inode
->i_security
;
3027 mutex_lock(&isp
->smk_lock
);
3029 * If the inode is already instantiated
3030 * take the quick way out
3032 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3036 sbsp
= sbp
->s_security
;
3038 * We're going to use the superblock default label
3039 * if there's no label on the file.
3041 final
= sbsp
->smk_default
;
3044 * If this is the root inode the superblock
3045 * may be in the process of initialization.
3046 * If that is the case use the root value out
3047 * of the superblock.
3049 if (opt_dentry
->d_parent
== opt_dentry
) {
3050 switch (sbp
->s_magic
) {
3051 case CGROUP_SUPER_MAGIC
:
3053 * The cgroup filesystem is never mounted,
3054 * so there's no opportunity to set the mount
3057 sbsp
->smk_root
= &smack_known_star
;
3058 sbsp
->smk_default
= &smack_known_star
;
3059 isp
->smk_inode
= sbsp
->smk_root
;
3063 * What about shmem/tmpfs anonymous files with dentry
3064 * obtained from d_alloc_pseudo()?
3066 isp
->smk_inode
= smk_of_current();
3069 isp
->smk_inode
= sbsp
->smk_root
;
3072 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3077 * This is pretty hackish.
3078 * Casey says that we shouldn't have to do
3079 * file system specific code, but it does help
3080 * with keeping it simple.
3082 switch (sbp
->s_magic
) {
3086 case CGROUP_SUPER_MAGIC
:
3088 * Casey says that it's a little embarrassing
3089 * that the smack file system doesn't do
3090 * extended attributes.
3092 * Casey says pipes are easy (?)
3094 * Socket access is controlled by the socket
3095 * structures associated with the task involved.
3097 * Cgroupfs is special
3099 final
= &smack_known_star
;
3101 case DEVPTS_SUPER_MAGIC
:
3103 * devpts seems content with the label of the task.
3104 * Programs that change smack have to treat the
3109 case PROC_SUPER_MAGIC
:
3111 * Casey says procfs appears not to care.
3112 * The superblock default suffices.
3117 * Device labels should come from the filesystem,
3118 * but watch out, because they're volitile,
3119 * getting recreated on every reboot.
3121 final
= &smack_known_star
;
3125 * If a smack value has been set we want to use it,
3126 * but since tmpfs isn't giving us the opportunity
3127 * to set mount options simulate setting the
3128 * superblock default.
3132 * This isn't an understood special case.
3133 * Get the value from the xattr.
3137 * UNIX domain sockets use lower level socket data.
3139 if (S_ISSOCK(inode
->i_mode
)) {
3140 final
= &smack_known_star
;
3144 * No xattr support means, alas, no SMACK label.
3145 * Use the aforeapplied default.
3146 * It would be curious if the label of the task
3147 * does not match that assigned.
3149 if (inode
->i_op
->getxattr
== NULL
)
3152 * Get the dentry for xattr.
3154 dp
= dget(opt_dentry
);
3155 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3160 * Transmuting directory
3162 if (S_ISDIR(inode
->i_mode
)) {
3164 * If this is a new directory and the label was
3165 * transmuted when the inode was initialized
3166 * set the transmute attribute on the directory
3167 * and mark the inode.
3169 * If there is a transmute attribute on the
3170 * directory mark the inode.
3172 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3173 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3174 rc
= inode
->i_op
->setxattr(dp
,
3175 XATTR_NAME_SMACKTRANSMUTE
,
3176 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3179 rc
= inode
->i_op
->getxattr(dp
,
3180 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3182 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3183 TRANS_TRUE_SIZE
) != 0)
3187 transflag
= SMK_INODE_TRANSMUTE
;
3190 * Don't let the exec or mmap label be "*" or "@".
3192 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3193 if (skp
== &smack_known_star
|| skp
== &smack_known_web
)
3195 isp
->smk_task
= skp
;
3196 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3197 if (skp
== &smack_known_star
|| skp
== &smack_known_web
)
3199 isp
->smk_mmap
= skp
;
3206 isp
->smk_inode
= ckp
;
3208 isp
->smk_inode
= final
;
3210 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3213 mutex_unlock(&isp
->smk_lock
);
3218 * smack_getprocattr - Smack process attribute access
3219 * @p: the object task
3220 * @name: the name of the attribute in /proc/.../attr
3221 * @value: where to put the result
3223 * Places a copy of the task Smack into value
3225 * Returns the length of the smack label or an error code
3227 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3229 struct smack_known
*skp
= smk_of_task(task_security(p
));
3233 if (strcmp(name
, "current") != 0)
3236 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3246 * smack_setprocattr - Smack process attribute setting
3247 * @p: the object task
3248 * @name: the name of the attribute in /proc/.../attr
3249 * @value: the value to set
3250 * @size: the size of the value
3252 * Sets the Smack value of the task. Only setting self
3253 * is permitted and only with privilege
3255 * Returns the length of the smack label or an error code
3257 static int smack_setprocattr(struct task_struct
*p
, char *name
,
3258 void *value
, size_t size
)
3260 struct task_smack
*tsp
;
3262 struct smack_known
*skp
;
3265 * Changing another process' Smack value is too dangerous
3266 * and supports no sane use case.
3271 if (!smack_privileged(CAP_MAC_ADMIN
))
3274 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3277 if (strcmp(name
, "current") != 0)
3280 skp
= smk_import_entry(value
, size
);
3285 * No process is ever allowed the web ("@") label.
3287 if (skp
== &smack_known_web
)
3290 new = prepare_creds();
3294 tsp
= new->security
;
3295 tsp
->smk_task
= skp
;
3302 * smack_unix_stream_connect - Smack access on UDS
3304 * @other: the other sock
3307 * Return 0 if a subject with the smack of sock could access
3308 * an object with the smack of other, otherwise an error code
3310 static int smack_unix_stream_connect(struct sock
*sock
,
3311 struct sock
*other
, struct sock
*newsk
)
3313 struct smack_known
*skp
;
3314 struct smack_known
*okp
;
3315 struct socket_smack
*ssp
= sock
->sk_security
;
3316 struct socket_smack
*osp
= other
->sk_security
;
3317 struct socket_smack
*nsp
= newsk
->sk_security
;
3318 struct smk_audit_info ad
;
3321 struct lsm_network_audit net
;
3324 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3328 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3329 smk_ad_setfield_u_net_sk(&ad
, other
);
3331 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3332 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3336 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3337 rc
= smk_bu_note("UDS connect", okp
, skp
,
3343 * Cross reference the peer labels for SO_PEERSEC.
3346 nsp
->smk_packet
= ssp
->smk_out
;
3347 ssp
->smk_packet
= osp
->smk_out
;
3354 * smack_unix_may_send - Smack access on UDS
3356 * @other: the other socket
3358 * Return 0 if a subject with the smack of sock could access
3359 * an object with the smack of other, otherwise an error code
3361 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3363 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3364 struct socket_smack
*osp
= other
->sk
->sk_security
;
3365 struct smk_audit_info ad
;
3369 struct lsm_network_audit net
;
3371 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3372 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3375 if (smack_privileged(CAP_MAC_OVERRIDE
))
3378 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3379 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3384 * smack_socket_sendmsg - Smack check based on destination host
3387 * @size: the size of the message
3389 * Return 0 if the current subject can write to the destination host.
3390 * For IPv4 this is only a question if the destination is a single label host.
3391 * For IPv6 this is a check against the label of the port.
3393 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3396 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3397 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3398 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3399 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3403 * Perfectly reasonable for this to be NULL
3408 switch (sip
->sin_family
) {
3410 rc
= smack_netlabel_send(sock
->sk
, sip
);
3413 #if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3414 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3415 #endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
3422 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3423 * @sap: netlabel secattr
3424 * @ssp: socket security information
3426 * Returns a pointer to a Smack label entry found on the label list.
3428 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3429 struct socket_smack
*ssp
)
3431 struct smack_known
*skp
;
3436 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3438 * Looks like a CIPSO packet.
3439 * If there are flags but no level netlabel isn't
3440 * behaving the way we expect it to.
3442 * Look it up in the label table
3443 * Without guidance regarding the smack value
3444 * for the packet fall back on the network
3448 list_for_each_entry(skp
, &smack_known_list
, list
) {
3449 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3452 * Compare the catsets. Use the netlbl APIs.
3454 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3455 if ((skp
->smk_netlabel
.flags
&
3456 NETLBL_SECATTR_MLS_CAT
) == 0)
3460 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3461 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3463 kcat
= netlbl_catmap_walk(
3464 skp
->smk_netlabel
.attr
.mls
.cat
,
3466 if (acat
< 0 || kcat
< 0)
3479 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3480 return &smack_known_web
;
3481 return &smack_known_star
;
3483 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
3485 * Looks like a fallback, which gives us a secid.
3487 skp
= smack_from_secid(sap
->attr
.secid
);
3489 * This has got to be a bug because it is
3490 * impossible to specify a fallback without
3491 * specifying the label, which will ensure
3492 * it has a secid, and the only way to get a
3493 * secid is from a fallback.
3495 BUG_ON(skp
== NULL
);
3499 * Without guidance regarding the smack value
3500 * for the packet fall back on the network
3503 return smack_net_ambient
;
3506 #if IS_ENABLED(CONFIG_IPV6)
3507 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3511 int proto
= -EINVAL
;
3512 struct ipv6hdr _ipv6h
;
3513 struct ipv6hdr
*ip6
;
3515 struct tcphdr _tcph
, *th
;
3516 struct udphdr _udph
, *uh
;
3517 struct dccp_hdr _dccph
, *dh
;
3521 offset
= skb_network_offset(skb
);
3522 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3525 sip
->sin6_addr
= ip6
->saddr
;
3527 nexthdr
= ip6
->nexthdr
;
3528 offset
+= sizeof(_ipv6h
);
3529 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3536 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3538 sip
->sin6_port
= th
->source
;
3541 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3543 sip
->sin6_port
= uh
->source
;
3546 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3548 sip
->sin6_port
= dh
->dccph_sport
;
3553 #endif /* CONFIG_IPV6 */
3556 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3560 * Returns 0 if the packet should be delivered, an error code otherwise
3562 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3564 struct netlbl_lsm_secattr secattr
;
3565 struct socket_smack
*ssp
= sk
->sk_security
;
3566 struct smack_known
*skp
= NULL
;
3568 struct smk_audit_info ad
;
3570 struct lsm_network_audit net
;
3572 #if IS_ENABLED(CONFIG_IPV6)
3573 struct sockaddr_in6 sadd
;
3575 #endif /* CONFIG_IPV6 */
3577 switch (sk
->sk_family
) {
3579 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3581 * If there is a secmark use it rather than the CIPSO label.
3582 * If there is no secmark fall back to CIPSO.
3583 * The secmark is assumed to reflect policy better.
3585 if (skb
&& skb
->secmark
!= 0) {
3586 skp
= smack_from_secid(skb
->secmark
);
3589 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3591 * Translate what netlabel gave us.
3593 netlbl_secattr_init(&secattr
);
3595 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
3597 skp
= smack_from_secattr(&secattr
, ssp
);
3599 skp
= smack_net_ambient
;
3601 netlbl_secattr_destroy(&secattr
);
3603 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3607 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3608 ad
.a
.u
.net
->family
= sk
->sk_family
;
3609 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3610 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3613 * Receiving a packet requires that the other end
3614 * be able to write here. Read access is not required.
3615 * This is the simplist possible security model
3618 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3619 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3622 netlbl_skbuff_err(skb
, rc
, 0);
3624 #if IS_ENABLED(CONFIG_IPV6)
3626 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3627 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_TCP
)
3629 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3630 if (skb
&& skb
->secmark
!= 0)
3631 skp
= smack_from_secid(skb
->secmark
);
3633 skp
= smack_net_ambient
;
3635 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3636 ad
.a
.u
.net
->family
= sk
->sk_family
;
3637 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3638 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3639 #endif /* CONFIG_AUDIT */
3640 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3641 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3643 #else /* CONFIG_SECURITY_SMACK_NETFILTER */
3644 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3645 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3647 #endif /* CONFIG_IPV6 */
3654 * smack_socket_getpeersec_stream - pull in packet label
3656 * @optval: user's destination
3657 * @optlen: size thereof
3660 * returns zero on success, an error code otherwise
3662 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3663 char __user
*optval
,
3664 int __user
*optlen
, unsigned len
)
3666 struct socket_smack
*ssp
;
3671 ssp
= sock
->sk
->sk_security
;
3672 if (ssp
->smk_packet
!= NULL
) {
3673 rcp
= ssp
->smk_packet
->smk_known
;
3674 slen
= strlen(rcp
) + 1;
3679 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3682 if (put_user(slen
, optlen
) != 0)
3690 * smack_socket_getpeersec_dgram - pull in packet label
3691 * @sock: the peer socket
3693 * @secid: pointer to where to put the secid of the packet
3695 * Sets the netlabel socket state on sk from parent
3697 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3698 struct sk_buff
*skb
, u32
*secid
)
3701 struct netlbl_lsm_secattr secattr
;
3702 struct socket_smack
*ssp
= NULL
;
3703 struct smack_known
*skp
;
3704 int family
= PF_UNSPEC
;
3705 u32 s
= 0; /* 0 is the invalid secid */
3709 if (skb
->protocol
== htons(ETH_P_IP
))
3711 #if IS_ENABLED(CONFIG_IPV6)
3712 else if (skb
->protocol
== htons(ETH_P_IPV6
))
3714 #endif /* CONFIG_IPV6 */
3716 if (family
== PF_UNSPEC
&& sock
!= NULL
)
3717 family
= sock
->sk
->sk_family
;
3721 ssp
= sock
->sk
->sk_security
;
3722 s
= ssp
->smk_out
->smk_secid
;
3725 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3731 * Translate what netlabel gave us.
3733 if (sock
!= NULL
&& sock
->sk
!= NULL
)
3734 ssp
= sock
->sk
->sk_security
;
3735 netlbl_secattr_init(&secattr
);
3736 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3738 skp
= smack_from_secattr(&secattr
, ssp
);
3741 netlbl_secattr_destroy(&secattr
);
3743 #if IS_ENABLED(CONFIG_IPV6)
3745 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3747 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3749 #endif /* CONFIG_IPV6 */
3758 * smack_sock_graft - Initialize a newly created socket with an existing sock
3760 * @parent: parent socket
3762 * Set the smk_{in,out} state of an existing sock based on the process that
3763 * is creating the new socket.
3765 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3767 struct socket_smack
*ssp
;
3768 struct smack_known
*skp
= smk_of_current();
3771 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3774 ssp
= sk
->sk_security
;
3777 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3781 * smack_inet_conn_request - Smack access check on connect
3782 * @sk: socket involved
3786 * Returns 0 if a task with the packet label could write to
3787 * the socket, otherwise an error code
3789 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
3790 struct request_sock
*req
)
3792 u16 family
= sk
->sk_family
;
3793 struct smack_known
*skp
;
3794 struct socket_smack
*ssp
= sk
->sk_security
;
3795 struct netlbl_lsm_secattr secattr
;
3796 struct sockaddr_in addr
;
3798 struct smack_known
*hskp
;
3800 struct smk_audit_info ad
;
3802 struct lsm_network_audit net
;
3805 #if IS_ENABLED(CONFIG_IPV6)
3806 if (family
== PF_INET6
) {
3808 * Handle mapped IPv4 packets arriving
3809 * via IPv6 sockets. Don't set up netlabel
3810 * processing on IPv6.
3812 if (skb
->protocol
== htons(ETH_P_IP
))
3817 #endif /* CONFIG_IPV6 */
3819 netlbl_secattr_init(&secattr
);
3820 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3822 skp
= smack_from_secattr(&secattr
, ssp
);
3824 skp
= &smack_known_huh
;
3825 netlbl_secattr_destroy(&secattr
);
3828 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3829 ad
.a
.u
.net
->family
= family
;
3830 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3831 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3834 * Receiving a packet requires that the other end be able to write
3835 * here. Read access is not required.
3837 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3838 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
3843 * Save the peer's label in the request_sock so we can later setup
3844 * smk_packet in the child socket so that SO_PEERCRED can report it.
3846 req
->peer_secid
= skp
->smk_secid
;
3849 * We need to decide if we want to label the incoming connection here
3850 * if we do we only need to label the request_sock and the stack will
3851 * propagate the wire-label to the sock when it is created.
3854 addr
.sin_addr
.s_addr
= hdr
->saddr
;
3856 hskp
= smack_host_label(&addr
);
3860 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
3862 netlbl_req_delattr(req
);
3868 * smack_inet_csk_clone - Copy the connection information to the new socket
3869 * @sk: the new socket
3870 * @req: the connection's request_sock
3872 * Transfer the connection's peer label to the newly created socket.
3874 static void smack_inet_csk_clone(struct sock
*sk
,
3875 const struct request_sock
*req
)
3877 struct socket_smack
*ssp
= sk
->sk_security
;
3878 struct smack_known
*skp
;
3880 if (req
->peer_secid
!= 0) {
3881 skp
= smack_from_secid(req
->peer_secid
);
3882 ssp
->smk_packet
= skp
;
3884 ssp
->smk_packet
= NULL
;
3888 * Key management security hooks
3890 * Casey has not tested key support very heavily.
3891 * The permission check is most likely too restrictive.
3892 * If you care about keys please have a look.
3897 * smack_key_alloc - Set the key security blob
3899 * @cred: the credentials to use
3902 * No allocation required
3906 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
3907 unsigned long flags
)
3909 struct smack_known
*skp
= smk_of_task(cred
->security
);
3911 key
->security
= skp
;
3916 * smack_key_free - Clear the key security blob
3919 * Clear the blob pointer
3921 static void smack_key_free(struct key
*key
)
3923 key
->security
= NULL
;
3927 * smack_key_permission - Smack access on a key
3928 * @key_ref: gets to the object
3929 * @cred: the credentials to use
3930 * @perm: requested key permissions
3932 * Return 0 if the task has read and write to the object,
3933 * an error code otherwise
3935 static int smack_key_permission(key_ref_t key_ref
,
3936 const struct cred
*cred
, unsigned perm
)
3939 struct smk_audit_info ad
;
3940 struct smack_known
*tkp
= smk_of_task(cred
->security
);
3944 keyp
= key_ref_to_ptr(key_ref
);
3948 * If the key hasn't been initialized give it access so that
3951 if (keyp
->security
== NULL
)
3954 * This should not occur
3959 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
3960 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
3961 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
3963 if (perm
& KEY_NEED_READ
)
3965 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
3966 request
= MAY_WRITE
;
3967 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
3968 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
3971 #endif /* CONFIG_KEYS */
3976 * Audit requires a unique representation of each Smack specific
3977 * rule. This unique representation is used to distinguish the
3978 * object to be audited from remaining kernel objects and also
3979 * works as a glue between the audit hooks.
3981 * Since repository entries are added but never deleted, we'll use
3982 * the smack_known label address related to the given audit rule as
3983 * the needed unique representation. This also better fits the smack
3984 * model where nearly everything is a label.
3989 * smack_audit_rule_init - Initialize a smack audit rule
3990 * @field: audit rule fields given from user-space (audit.h)
3991 * @op: required testing operator (=, !=, >, <, ...)
3992 * @rulestr: smack label to be audited
3993 * @vrule: pointer to save our own audit rule representation
3995 * Prepare to audit cases where (@field @op @rulestr) is true.
3996 * The label to be audited is created if necessay.
3998 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4000 struct smack_known
*skp
;
4001 char **rule
= (char **)vrule
;
4004 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4007 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4010 skp
= smk_import_entry(rulestr
, 0);
4012 *rule
= skp
->smk_known
;
4018 * smack_audit_rule_known - Distinguish Smack audit rules
4019 * @krule: rule of interest, in Audit kernel representation format
4021 * This is used to filter Smack rules from remaining Audit ones.
4022 * If it's proved that this rule belongs to us, the
4023 * audit_rule_match hook will be called to do the final judgement.
4025 static int smack_audit_rule_known(struct audit_krule
*krule
)
4027 struct audit_field
*f
;
4030 for (i
= 0; i
< krule
->field_count
; i
++) {
4031 f
= &krule
->fields
[i
];
4033 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4041 * smack_audit_rule_match - Audit given object ?
4042 * @secid: security id for identifying the object to test
4043 * @field: audit rule flags given from user-space
4044 * @op: required testing operator
4045 * @vrule: smack internal rule presentation
4046 * @actx: audit context associated with the check
4048 * The core Audit hook. It's used to take the decision of
4049 * whether to audit or not to audit a given object.
4051 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4052 struct audit_context
*actx
)
4054 struct smack_known
*skp
;
4057 if (unlikely(!rule
)) {
4058 WARN_ONCE(1, "Smack: missing rule\n");
4062 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4065 skp
= smack_from_secid(secid
);
4068 * No need to do string comparisons. If a match occurs,
4069 * both pointers will point to the same smack_known
4072 if (op
== Audit_equal
)
4073 return (rule
== skp
->smk_known
);
4074 if (op
== Audit_not_equal
)
4075 return (rule
!= skp
->smk_known
);
4081 * smack_audit_rule_free - free smack rule representation
4082 * @vrule: rule to be freed.
4084 * No memory was allocated.
4086 static void smack_audit_rule_free(void *vrule
)
4091 #endif /* CONFIG_AUDIT */
4094 * smack_ismaclabel - check if xattr @name references a smack MAC label
4095 * @name: Full xattr name to check.
4097 static int smack_ismaclabel(const char *name
)
4099 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4104 * smack_secid_to_secctx - return the smack label for a secid
4105 * @secid: incoming integer
4106 * @secdata: destination
4107 * @seclen: how long it is
4109 * Exists for networking code.
4111 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4113 struct smack_known
*skp
= smack_from_secid(secid
);
4116 *secdata
= skp
->smk_known
;
4117 *seclen
= strlen(skp
->smk_known
);
4122 * smack_secctx_to_secid - return the secid for a smack label
4123 * @secdata: smack label
4124 * @seclen: how long result is
4125 * @secid: outgoing integer
4127 * Exists for audit and networking code.
4129 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4131 struct smack_known
*skp
= smk_find_entry(secdata
);
4134 *secid
= skp
->smk_secid
;
4141 * smack_release_secctx - don't do anything.
4145 * Exists to make sure nothing gets done, and properly
4147 static void smack_release_secctx(char *secdata
, u32 seclen
)
4151 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4153 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4156 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4158 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4161 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4164 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
4172 struct security_operations smack_ops
= {
4175 .ptrace_access_check
= smack_ptrace_access_check
,
4176 .ptrace_traceme
= smack_ptrace_traceme
,
4177 .syslog
= smack_syslog
,
4179 .sb_alloc_security
= smack_sb_alloc_security
,
4180 .sb_free_security
= smack_sb_free_security
,
4181 .sb_copy_data
= smack_sb_copy_data
,
4182 .sb_kern_mount
= smack_sb_kern_mount
,
4183 .sb_statfs
= smack_sb_statfs
,
4185 .bprm_set_creds
= smack_bprm_set_creds
,
4186 .bprm_committing_creds
= smack_bprm_committing_creds
,
4187 .bprm_secureexec
= smack_bprm_secureexec
,
4189 .inode_alloc_security
= smack_inode_alloc_security
,
4190 .inode_free_security
= smack_inode_free_security
,
4191 .inode_init_security
= smack_inode_init_security
,
4192 .inode_link
= smack_inode_link
,
4193 .inode_unlink
= smack_inode_unlink
,
4194 .inode_rmdir
= smack_inode_rmdir
,
4195 .inode_rename
= smack_inode_rename
,
4196 .inode_permission
= smack_inode_permission
,
4197 .inode_setattr
= smack_inode_setattr
,
4198 .inode_getattr
= smack_inode_getattr
,
4199 .inode_setxattr
= smack_inode_setxattr
,
4200 .inode_post_setxattr
= smack_inode_post_setxattr
,
4201 .inode_getxattr
= smack_inode_getxattr
,
4202 .inode_removexattr
= smack_inode_removexattr
,
4203 .inode_getsecurity
= smack_inode_getsecurity
,
4204 .inode_setsecurity
= smack_inode_setsecurity
,
4205 .inode_listsecurity
= smack_inode_listsecurity
,
4206 .inode_getsecid
= smack_inode_getsecid
,
4208 .file_permission
= smack_file_permission
,
4209 .file_alloc_security
= smack_file_alloc_security
,
4210 .file_free_security
= smack_file_free_security
,
4211 .file_ioctl
= smack_file_ioctl
,
4212 .file_lock
= smack_file_lock
,
4213 .file_fcntl
= smack_file_fcntl
,
4214 .mmap_file
= smack_mmap_file
,
4215 .mmap_addr
= cap_mmap_addr
,
4216 .file_set_fowner
= smack_file_set_fowner
,
4217 .file_send_sigiotask
= smack_file_send_sigiotask
,
4218 .file_receive
= smack_file_receive
,
4220 .file_open
= smack_file_open
,
4222 .cred_alloc_blank
= smack_cred_alloc_blank
,
4223 .cred_free
= smack_cred_free
,
4224 .cred_prepare
= smack_cred_prepare
,
4225 .cred_transfer
= smack_cred_transfer
,
4226 .kernel_act_as
= smack_kernel_act_as
,
4227 .kernel_create_files_as
= smack_kernel_create_files_as
,
4228 .task_setpgid
= smack_task_setpgid
,
4229 .task_getpgid
= smack_task_getpgid
,
4230 .task_getsid
= smack_task_getsid
,
4231 .task_getsecid
= smack_task_getsecid
,
4232 .task_setnice
= smack_task_setnice
,
4233 .task_setioprio
= smack_task_setioprio
,
4234 .task_getioprio
= smack_task_getioprio
,
4235 .task_setscheduler
= smack_task_setscheduler
,
4236 .task_getscheduler
= smack_task_getscheduler
,
4237 .task_movememory
= smack_task_movememory
,
4238 .task_kill
= smack_task_kill
,
4239 .task_wait
= smack_task_wait
,
4240 .task_to_inode
= smack_task_to_inode
,
4242 .ipc_permission
= smack_ipc_permission
,
4243 .ipc_getsecid
= smack_ipc_getsecid
,
4245 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
4246 .msg_msg_free_security
= smack_msg_msg_free_security
,
4248 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
4249 .msg_queue_free_security
= smack_msg_queue_free_security
,
4250 .msg_queue_associate
= smack_msg_queue_associate
,
4251 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
4252 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
4253 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
4255 .shm_alloc_security
= smack_shm_alloc_security
,
4256 .shm_free_security
= smack_shm_free_security
,
4257 .shm_associate
= smack_shm_associate
,
4258 .shm_shmctl
= smack_shm_shmctl
,
4259 .shm_shmat
= smack_shm_shmat
,
4261 .sem_alloc_security
= smack_sem_alloc_security
,
4262 .sem_free_security
= smack_sem_free_security
,
4263 .sem_associate
= smack_sem_associate
,
4264 .sem_semctl
= smack_sem_semctl
,
4265 .sem_semop
= smack_sem_semop
,
4267 .d_instantiate
= smack_d_instantiate
,
4269 .getprocattr
= smack_getprocattr
,
4270 .setprocattr
= smack_setprocattr
,
4272 .unix_stream_connect
= smack_unix_stream_connect
,
4273 .unix_may_send
= smack_unix_may_send
,
4275 .socket_post_create
= smack_socket_post_create
,
4276 #ifndef CONFIG_SECURITY_SMACK_NETFILTER
4277 .socket_bind
= smack_socket_bind
,
4278 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4279 .socket_connect
= smack_socket_connect
,
4280 .socket_sendmsg
= smack_socket_sendmsg
,
4281 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
4282 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
4283 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
4284 .sk_alloc_security
= smack_sk_alloc_security
,
4285 .sk_free_security
= smack_sk_free_security
,
4286 .sock_graft
= smack_sock_graft
,
4287 .inet_conn_request
= smack_inet_conn_request
,
4288 .inet_csk_clone
= smack_inet_csk_clone
,
4290 /* key management security hooks */
4292 .key_alloc
= smack_key_alloc
,
4293 .key_free
= smack_key_free
,
4294 .key_permission
= smack_key_permission
,
4295 #endif /* CONFIG_KEYS */
4299 .audit_rule_init
= smack_audit_rule_init
,
4300 .audit_rule_known
= smack_audit_rule_known
,
4301 .audit_rule_match
= smack_audit_rule_match
,
4302 .audit_rule_free
= smack_audit_rule_free
,
4303 #endif /* CONFIG_AUDIT */
4305 .ismaclabel
= smack_ismaclabel
,
4306 .secid_to_secctx
= smack_secid_to_secctx
,
4307 .secctx_to_secid
= smack_secctx_to_secid
,
4308 .release_secctx
= smack_release_secctx
,
4309 .inode_notifysecctx
= smack_inode_notifysecctx
,
4310 .inode_setsecctx
= smack_inode_setsecctx
,
4311 .inode_getsecctx
= smack_inode_getsecctx
,
4315 static __init
void init_smack_known_list(void)
4318 * Initialize rule list locks
4320 mutex_init(&smack_known_huh
.smk_rules_lock
);
4321 mutex_init(&smack_known_hat
.smk_rules_lock
);
4322 mutex_init(&smack_known_floor
.smk_rules_lock
);
4323 mutex_init(&smack_known_star
.smk_rules_lock
);
4324 mutex_init(&smack_known_invalid
.smk_rules_lock
);
4325 mutex_init(&smack_known_web
.smk_rules_lock
);
4327 * Initialize rule lists
4329 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4330 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4331 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4332 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4333 INIT_LIST_HEAD(&smack_known_invalid
.smk_rules
);
4334 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4336 * Create the known labels list
4338 smk_insert_entry(&smack_known_huh
);
4339 smk_insert_entry(&smack_known_hat
);
4340 smk_insert_entry(&smack_known_star
);
4341 smk_insert_entry(&smack_known_floor
);
4342 smk_insert_entry(&smack_known_invalid
);
4343 smk_insert_entry(&smack_known_web
);
4347 * smack_init - initialize the smack system
4351 static __init
int smack_init(void)
4354 struct task_smack
*tsp
;
4356 if (!security_module_enable(&smack_ops
))
4361 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4362 if (!smack_inode_cache
)
4365 tsp
= new_task_smack(&smack_known_floor
, &smack_known_floor
,
4368 kmem_cache_destroy(smack_inode_cache
);
4372 printk(KERN_INFO
"Smack: Initializing.\n");
4375 * Set the security state for the initial task.
4377 cred
= (struct cred
*) current
->cred
;
4378 cred
->security
= tsp
;
4380 /* initialize the smack_known_list */
4381 init_smack_known_list();
4386 if (register_security(&smack_ops
))
4387 panic("smack: Unable to register with kernel.\n");
4393 * Smack requires early initialization in order to label
4394 * all processes and objects when they are created.
4396 security_initcall(smack_init
);