1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
46 #define TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 #ifdef SMACK_IPV6_PORT_LABELING
54 DEFINE_MUTEX(smack_ipv6_lock
);
55 static LIST_HEAD(smk_ipv6_port_list
);
57 static struct kmem_cache
*smack_inode_cache
;
58 struct kmem_cache
*smack_rule_cache
;
61 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
66 } smk_mount_opts
[] = {
67 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault
},
68 A(fsdefault
), A(fsfloor
), A(fshat
), A(fsroot
), A(fstransmute
)
72 static int match_opt_prefix(char *s
, int l
, char **arg
)
76 for (i
= 0; i
< ARRAY_SIZE(smk_mount_opts
); i
++) {
77 size_t len
= smk_mount_opts
[i
].len
;
78 if (len
> l
|| memcmp(s
, smk_mount_opts
[i
].name
, len
))
80 if (len
== l
|| s
[len
] != '=')
83 return smk_mount_opts
[i
].opt
;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess
[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode
, char *s
)
102 if (mode
& MAY_WRITE
)
106 if (mode
& MAY_APPEND
)
108 if (mode
& MAY_TRANSMUTE
)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
120 struct smack_known
*oskp
, int mode
, int rc
)
122 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
126 if (rc
> SMACK_UNCONFINED_OBJECT
)
129 smk_bu_mode(mode
, acc
);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
131 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
142 struct task_smack
*tsp
= smack_cred(current_cred());
143 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
147 if (rc
> SMACK_UNCONFINED_OBJECT
)
150 smk_bu_mode(mode
, acc
);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
152 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
153 acc
, current
->comm
, note
);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
163 struct task_smack
*tsp
= smack_cred(current_cred());
164 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
165 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
169 if (rc
> SMACK_UNCONFINED_OBJECT
)
172 smk_bu_mode(mode
, acc
);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
174 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
175 current
->comm
, otp
->comm
);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
185 struct task_smack
*tsp
= smack_cred(current_cred());
186 struct inode_smack
*isp
= smack_inode(inode
);
187 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
189 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
195 if (rc
> SMACK_UNCONFINED_OBJECT
)
197 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
198 (mode
& (MAY_WRITE
| MAY_APPEND
)))
199 isp
->smk_flags
|= SMK_INODE_IMPURE
;
201 smk_bu_mode(mode
, acc
);
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
204 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
205 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
209 #define smk_bu_inode(inode, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
215 struct task_smack
*tsp
= smack_cred(current_cred());
216 struct smack_known
*sskp
= tsp
->smk_task
;
217 struct inode
*inode
= file_inode(file
);
218 struct inode_smack
*isp
= smack_inode(inode
);
219 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
221 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
227 if (rc
> SMACK_UNCONFINED_OBJECT
)
230 smk_bu_mode(mode
, acc
);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
232 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
233 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
238 #define smk_bu_file(file, mode, RC) (RC)
241 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
242 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
245 struct task_smack
*tsp
= smack_cred(cred
);
246 struct smack_known
*sskp
= tsp
->smk_task
;
247 struct inode
*inode
= file_inode(file
);
248 struct inode_smack
*isp
= smack_inode(inode
);
249 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
251 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
257 if (rc
> SMACK_UNCONFINED_OBJECT
)
260 smk_bu_mode(mode
, acc
);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
262 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
263 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
268 #define smk_bu_credfile(cred, file, mode, RC) (RC)
272 * smk_fetch - Fetch the smack label from a file.
273 * @name: type of the label (attribute)
274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
280 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
285 struct smack_known
*skp
= NULL
;
287 if (!(ip
->i_opflags
& IOP_XATTR
))
288 return ERR_PTR(-EOPNOTSUPP
);
290 buffer
= kzalloc(SMK_LONGLABEL
, GFP_NOFS
);
292 return ERR_PTR(-ENOMEM
);
294 rc
= __vfs_getxattr(dp
, ip
, name
, buffer
, SMK_LONGLABEL
);
300 skp
= smk_import_entry(buffer
, rc
);
308 * init_inode_smack - initialize an inode security blob
309 * @inode: inode to extract the info from
310 * @skp: a pointer to the Smack label entry to use in the blob
313 static void init_inode_smack(struct inode
*inode
, struct smack_known
*skp
)
315 struct inode_smack
*isp
= smack_inode(inode
);
317 isp
->smk_inode
= skp
;
319 mutex_init(&isp
->smk_lock
);
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack
*tsp
, struct smack_known
*task
,
330 struct smack_known
*forked
)
332 tsp
->smk_task
= task
;
333 tsp
->smk_forked
= forked
;
334 INIT_LIST_HEAD(&tsp
->smk_rules
);
335 INIT_LIST_HEAD(&tsp
->smk_relabel
);
336 mutex_init(&tsp
->smk_rules_lock
);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
350 struct smack_rule
*nrp
;
351 struct smack_rule
*orp
;
354 list_for_each_entry_rcu(orp
, ohead
, list
) {
355 nrp
= kmem_cache_zalloc(smack_rule_cache
, gfp
);
361 list_add_rcu(&nrp
->list
, nhead
);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head
*nhead
, struct list_head
*ohead
,
377 struct smack_known_list_elem
*nklep
;
378 struct smack_known_list_elem
*oklep
;
380 list_for_each_entry(oklep
, ohead
, list
) {
381 nklep
= kzalloc(sizeof(struct smack_known_list_elem
), gfp
);
383 smk_destroy_label_list(nhead
);
386 nklep
->smk_label
= oklep
->smk_label
;
387 list_add(&nklep
->list
, nhead
);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
401 if (mode
& PTRACE_MODE_ATTACH
)
402 return MAY_READWRITE
;
403 if (mode
& PTRACE_MODE_READ
)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
419 struct smack_known
*tracee_known
,
420 unsigned int mode
, const char *func
)
423 struct smk_audit_info ad
, *saip
= NULL
;
424 struct task_smack
*tsp
;
425 struct smack_known
*tracer_known
;
426 const struct cred
*tracercred
;
428 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
429 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
430 smk_ad_setfield_u_tsk(&ad
, tracer
);
435 tracercred
= __task_cred(tracer
);
436 tsp
= smack_cred(tracercred
);
437 tracer_known
= smk_of_task(tsp
);
439 if ((mode
& PTRACE_MODE_ATTACH
) &&
440 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
441 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
442 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
444 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE
, tracercred
))
452 smack_log(tracer_known
->smk_known
,
453 tracee_known
->smk_known
,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
483 struct smack_known
*skp
;
485 skp
= smk_of_task_struct(ctp
);
487 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct
*ptp
)
501 struct smack_known
*skp
;
503 skp
= smk_of_task(smack_cred(current_cred()));
505 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
510 * smack_syslog - Smack approval on syslog
511 * @typefrom_file: unused
513 * Returns 0 on success, error code otherwise.
515 static int smack_syslog(int typefrom_file
)
518 struct smack_known
*skp
= smk_of_current();
520 if (smack_privileged(CAP_MAC_OVERRIDE
))
523 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
534 * smack_sb_alloc_security - allocate a superblock blob
535 * @sb: the superblock getting the blob
537 * Returns 0 on success or -ENOMEM on error.
539 static int smack_sb_alloc_security(struct super_block
*sb
)
541 struct superblock_smack
*sbsp
;
543 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
548 sbsp
->smk_root
= &smack_known_floor
;
549 sbsp
->smk_default
= &smack_known_floor
;
550 sbsp
->smk_floor
= &smack_known_floor
;
551 sbsp
->smk_hat
= &smack_known_hat
;
553 * SMK_SB_INITIALIZED will be zero from kzalloc.
555 sb
->s_security
= sbsp
;
561 * smack_sb_free_security - free a superblock blob
562 * @sb: the superblock getting the blob
565 static void smack_sb_free_security(struct super_block
*sb
)
567 kfree(sb
->s_security
);
568 sb
->s_security
= NULL
;
571 struct smack_mnt_opts
{
572 const char *fsdefault
, *fsfloor
, *fshat
, *fsroot
, *fstransmute
;
575 static void smack_free_mnt_opts(void *mnt_opts
)
577 struct smack_mnt_opts
*opts
= mnt_opts
;
578 kfree(opts
->fsdefault
);
579 kfree(opts
->fsfloor
);
582 kfree(opts
->fstransmute
);
586 static int smack_add_opt(int token
, const char *s
, void **mnt_opts
)
588 struct smack_mnt_opts
*opts
= *mnt_opts
;
591 opts
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
620 case Opt_fstransmute
:
621 if (opts
->fstransmute
)
623 opts
->fstransmute
= s
;
629 pr_warn("Smack: duplicate mount options\n");
634 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
635 * @fc: The new filesystem context.
636 * @src_fc: The source filesystem context being duplicated.
638 * Returns 0 on success or -ENOMEM on error.
640 static int smack_fs_context_dup(struct fs_context
*fc
,
641 struct fs_context
*src_fc
)
643 struct smack_mnt_opts
*dst
, *src
= src_fc
->security
;
648 fc
->security
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
653 if (src
->fsdefault
) {
654 dst
->fsdefault
= kstrdup(src
->fsdefault
, GFP_KERNEL
);
659 dst
->fsfloor
= kstrdup(src
->fsfloor
, GFP_KERNEL
);
664 dst
->fshat
= kstrdup(src
->fshat
, GFP_KERNEL
);
669 dst
->fsroot
= kstrdup(src
->fsroot
, GFP_KERNEL
);
673 if (src
->fstransmute
) {
674 dst
->fstransmute
= kstrdup(src
->fstransmute
, GFP_KERNEL
);
675 if (!dst
->fstransmute
)
681 static const struct fs_parameter_spec smack_param_specs
[] = {
682 fsparam_string("smackfsdef", Opt_fsdefault
),
683 fsparam_string("smackfsdefault", Opt_fsdefault
),
684 fsparam_string("smackfsfloor", Opt_fsfloor
),
685 fsparam_string("smackfshat", Opt_fshat
),
686 fsparam_string("smackfsroot", Opt_fsroot
),
687 fsparam_string("smackfstransmute", Opt_fstransmute
),
691 static const struct fs_parameter_description smack_fs_parameters
= {
693 .specs
= smack_param_specs
,
697 * smack_fs_context_parse_param - Parse a single mount parameter
698 * @fc: The new filesystem context being constructed.
699 * @param: The parameter.
701 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
704 static int smack_fs_context_parse_param(struct fs_context
*fc
,
705 struct fs_parameter
*param
)
707 struct fs_parse_result result
;
710 opt
= fs_parse(fc
, &smack_fs_parameters
, param
, &result
);
714 rc
= smack_add_opt(opt
, param
->string
, &fc
->security
);
716 param
->string
= NULL
;
720 static int smack_sb_eat_lsm_opts(char *options
, void **mnt_opts
)
722 char *from
= options
, *to
= options
;
726 char *next
= strchr(from
, ',');
735 token
= match_opt_prefix(from
, len
, &arg
);
736 if (token
!= Opt_error
) {
737 arg
= kmemdup_nul(arg
, from
+ len
- arg
, GFP_KERNEL
);
738 rc
= smack_add_opt(token
, arg
, mnt_opts
);
742 smack_free_mnt_opts(*mnt_opts
);
747 if (!first
) { // copy with preceding comma
752 memmove(to
, from
, len
);
765 * smack_set_mnt_opts - set Smack specific mount options
766 * @sb: the file system superblock
767 * @mnt_opts: Smack mount options
768 * @kern_flags: mount option from kernel space or user space
769 * @set_kern_flags: where to store converted mount opts
771 * Returns 0 on success, an error code on failure
773 * Allow filesystems with binary mount data to explicitly set Smack mount
776 static int smack_set_mnt_opts(struct super_block
*sb
,
778 unsigned long kern_flags
,
779 unsigned long *set_kern_flags
)
781 struct dentry
*root
= sb
->s_root
;
782 struct inode
*inode
= d_backing_inode(root
);
783 struct superblock_smack
*sp
= sb
->s_security
;
784 struct inode_smack
*isp
;
785 struct smack_known
*skp
;
786 struct smack_mnt_opts
*opts
= mnt_opts
;
787 bool transmute
= false;
789 if (sp
->smk_flags
& SMK_SB_INITIALIZED
)
792 if (inode
->i_security
== NULL
) {
793 int rc
= lsm_inode_alloc(inode
);
799 if (!smack_privileged(CAP_MAC_ADMIN
)) {
801 * Unprivileged mounts don't get to specify Smack values.
806 * Unprivileged mounts get root and default from the caller.
808 skp
= smk_of_current();
810 sp
->smk_default
= skp
;
812 * For a handful of fs types with no user-controlled
813 * backing store it's okay to trust security labels
814 * in the filesystem. The rest are untrusted.
816 if (sb
->s_user_ns
!= &init_user_ns
&&
817 sb
->s_magic
!= SYSFS_MAGIC
&& sb
->s_magic
!= TMPFS_MAGIC
&&
818 sb
->s_magic
!= RAMFS_MAGIC
) {
820 sp
->smk_flags
|= SMK_SB_UNTRUSTED
;
824 sp
->smk_flags
|= SMK_SB_INITIALIZED
;
827 if (opts
->fsdefault
) {
828 skp
= smk_import_entry(opts
->fsdefault
, 0);
831 sp
->smk_default
= skp
;
834 skp
= smk_import_entry(opts
->fsfloor
, 0);
840 skp
= smk_import_entry(opts
->fshat
, 0);
846 skp
= smk_import_entry(opts
->fsroot
, 0);
851 if (opts
->fstransmute
) {
852 skp
= smk_import_entry(opts
->fstransmute
, 0);
861 * Initialize the root inode.
863 init_inode_smack(inode
, sp
->smk_root
);
866 isp
= smack_inode(inode
);
867 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
874 * smack_sb_statfs - Smack check on statfs
875 * @dentry: identifies the file system in question
877 * Returns 0 if current can read the floor of the filesystem,
878 * and error code otherwise
880 static int smack_sb_statfs(struct dentry
*dentry
)
882 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
884 struct smk_audit_info ad
;
886 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
887 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
889 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
890 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
899 * smack_bprm_set_creds - set creds for exec
900 * @bprm: the exec information
902 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
904 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
906 struct inode
*inode
= file_inode(bprm
->file
);
907 struct task_smack
*bsp
= smack_cred(bprm
->cred
);
908 struct inode_smack
*isp
;
909 struct superblock_smack
*sbsp
;
912 if (bprm
->called_set_creds
)
915 isp
= smack_inode(inode
);
916 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
919 sbsp
= inode
->i_sb
->s_security
;
920 if ((sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) &&
921 isp
->smk_task
!= sbsp
->smk_root
)
924 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
925 struct task_struct
*tracer
;
929 tracer
= ptrace_parent(current
);
930 if (likely(tracer
!= NULL
))
931 rc
= smk_ptrace_rule_check(tracer
,
940 if (bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
)
943 bsp
->smk_task
= isp
->smk_task
;
944 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
946 /* Decide if this is a secure exec. */
947 if (bsp
->smk_task
!= bsp
->smk_forked
)
948 bprm
->secureexec
= 1;
958 * smack_inode_alloc_security - allocate an inode blob
959 * @inode: the inode in need of a blob
963 static int smack_inode_alloc_security(struct inode
*inode
)
965 struct smack_known
*skp
= smk_of_current();
967 init_inode_smack(inode
, skp
);
972 * smack_inode_init_security - copy out the smack from an inode
973 * @inode: the newly created inode
974 * @dir: containing directory object
976 * @name: where to put the attribute name
977 * @value: where to put the attribute value
978 * @len: where to put the length of the attribute
980 * Returns 0 if it all works out, -ENOMEM if there's no memory
982 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
983 const struct qstr
*qstr
, const char **name
,
984 void **value
, size_t *len
)
986 struct inode_smack
*issp
= smack_inode(inode
);
987 struct smack_known
*skp
= smk_of_current();
988 struct smack_known
*isp
= smk_of_inode(inode
);
989 struct smack_known
*dsp
= smk_of_inode(dir
);
993 *name
= XATTR_SMACK_SUFFIX
;
997 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
1002 * If the access rule allows transmutation and
1003 * the directory requests transmutation then
1004 * by all means transmute.
1005 * Mark the inode as changed.
1007 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
1008 smk_inode_transmutable(dir
)) {
1010 issp
->smk_flags
|= SMK_INODE_CHANGED
;
1013 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
1017 *len
= strlen(isp
->smk_known
);
1024 * smack_inode_link - Smack check on link
1025 * @old_dentry: the existing object
1027 * @new_dentry: the new object
1029 * Returns 0 if access is permitted, an error code otherwise
1031 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1032 struct dentry
*new_dentry
)
1034 struct smack_known
*isp
;
1035 struct smk_audit_info ad
;
1038 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1039 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1041 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1042 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1043 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1045 if (rc
== 0 && d_is_positive(new_dentry
)) {
1046 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1047 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1048 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1049 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1056 * smack_inode_unlink - Smack check on inode deletion
1057 * @dir: containing directory object
1058 * @dentry: file to unlink
1060 * Returns 0 if current can write the containing directory
1061 * and the object, error code otherwise
1063 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1065 struct inode
*ip
= d_backing_inode(dentry
);
1066 struct smk_audit_info ad
;
1069 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1070 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1073 * You need write access to the thing you're unlinking
1075 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1076 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1079 * You also need write access to the containing directory
1081 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1082 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1083 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1084 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1090 * smack_inode_rmdir - Smack check on directory deletion
1091 * @dir: containing directory object
1092 * @dentry: directory to unlink
1094 * Returns 0 if current can write the containing directory
1095 * and the directory, error code otherwise
1097 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1099 struct smk_audit_info ad
;
1102 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1103 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1106 * You need write access to the thing you're removing
1108 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1109 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1112 * You also need write access to the containing directory
1114 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1115 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1116 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1117 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1124 * smack_inode_rename - Smack check on rename
1125 * @old_inode: unused
1126 * @old_dentry: the old object
1127 * @new_inode: unused
1128 * @new_dentry: the new object
1130 * Read and write access is required on both the old and
1133 * Returns 0 if access is permitted, an error code otherwise
1135 static int smack_inode_rename(struct inode
*old_inode
,
1136 struct dentry
*old_dentry
,
1137 struct inode
*new_inode
,
1138 struct dentry
*new_dentry
)
1141 struct smack_known
*isp
;
1142 struct smk_audit_info ad
;
1144 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1145 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1147 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1148 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1149 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1151 if (rc
== 0 && d_is_positive(new_dentry
)) {
1152 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1153 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1154 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1155 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1161 * smack_inode_permission - Smack version of permission()
1162 * @inode: the inode in question
1163 * @mask: the access requested
1165 * This is the important Smack hook.
1167 * Returns 0 if access is permitted, an error code otherwise
1169 static int smack_inode_permission(struct inode
*inode
, int mask
)
1171 struct superblock_smack
*sbsp
= inode
->i_sb
->s_security
;
1172 struct smk_audit_info ad
;
1173 int no_block
= mask
& MAY_NOT_BLOCK
;
1176 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1178 * No permission to check. Existence test. Yup, it's there.
1183 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1184 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1188 /* May be droppable after audit */
1191 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1192 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1193 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1194 rc
= smk_bu_inode(inode
, mask
, rc
);
1199 * smack_inode_setattr - Smack check for setting attributes
1200 * @dentry: the object
1201 * @iattr: for the force flag
1203 * Returns 0 if access is permitted, an error code otherwise
1205 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1207 struct smk_audit_info ad
;
1211 * Need to allow for clearing the setuid bit.
1213 if (iattr
->ia_valid
& ATTR_FORCE
)
1215 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1216 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1218 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1219 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1224 * smack_inode_getattr - Smack check for getting attributes
1225 * @path: path to extract the info from
1227 * Returns 0 if access is permitted, an error code otherwise
1229 static int smack_inode_getattr(const struct path
*path
)
1231 struct smk_audit_info ad
;
1232 struct inode
*inode
= d_backing_inode(path
->dentry
);
1235 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1236 smk_ad_setfield_u_fs_path(&ad
, *path
);
1237 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1238 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1243 * smack_inode_setxattr - Smack check for setting xattrs
1244 * @dentry: the object
1245 * @name: name of the attribute
1246 * @value: value of the attribute
1247 * @size: size of the value
1250 * This protects the Smack attribute explicitly.
1252 * Returns 0 if access is permitted, an error code otherwise
1254 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1255 const void *value
, size_t size
, int flags
)
1257 struct smk_audit_info ad
;
1258 struct smack_known
*skp
;
1260 int check_import
= 0;
1265 * Check label validity here so import won't fail in post_setxattr
1267 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1268 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1269 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1272 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1273 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1277 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1279 if (size
!= TRANS_TRUE_SIZE
||
1280 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1283 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1285 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1288 if (rc
== 0 && check_import
) {
1289 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1292 else if (skp
== NULL
|| (check_star
&&
1293 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1297 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1298 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1301 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1302 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1309 * smack_inode_post_setxattr - Apply the Smack update approved above
1311 * @name: attribute name
1312 * @value: attribute value
1313 * @size: attribute size
1316 * Set the pointer in the inode blob to the entry found
1317 * in the master label list.
1319 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1320 const void *value
, size_t size
, int flags
)
1322 struct smack_known
*skp
;
1323 struct inode_smack
*isp
= smack_inode(d_backing_inode(dentry
));
1325 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1326 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1330 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1331 skp
= smk_import_entry(value
, size
);
1333 isp
->smk_inode
= skp
;
1334 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1335 skp
= smk_import_entry(value
, size
);
1337 isp
->smk_task
= skp
;
1338 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1339 skp
= smk_import_entry(value
, size
);
1341 isp
->smk_mmap
= skp
;
1348 * smack_inode_getxattr - Smack check on getxattr
1349 * @dentry: the object
1352 * Returns 0 if access is permitted, an error code otherwise
1354 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1356 struct smk_audit_info ad
;
1359 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1360 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1362 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1363 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1368 * smack_inode_removexattr - Smack check on removexattr
1369 * @dentry: the object
1370 * @name: name of the attribute
1372 * Removing the Smack attribute requires CAP_MAC_ADMIN
1374 * Returns 0 if access is permitted, an error code otherwise
1376 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1378 struct inode_smack
*isp
;
1379 struct smk_audit_info ad
;
1382 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1383 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1384 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1385 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1386 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1387 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1388 if (!smack_privileged(CAP_MAC_ADMIN
))
1391 rc
= cap_inode_removexattr(dentry
, name
);
1396 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1397 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1399 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1400 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1404 isp
= smack_inode(d_backing_inode(dentry
));
1406 * Don't do anything special for these.
1407 * XATTR_NAME_SMACKIPIN
1408 * XATTR_NAME_SMACKIPOUT
1410 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1411 struct super_block
*sbp
= dentry
->d_sb
;
1412 struct superblock_smack
*sbsp
= sbp
->s_security
;
1414 isp
->smk_inode
= sbsp
->smk_default
;
1415 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1416 isp
->smk_task
= NULL
;
1417 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1418 isp
->smk_mmap
= NULL
;
1419 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1420 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1426 * smack_inode_getsecurity - get smack xattrs
1427 * @inode: the object
1428 * @name: attribute name
1429 * @buffer: where to put the result
1430 * @alloc: duplicate memory
1432 * Returns the size of the attribute or an error code
1434 static int smack_inode_getsecurity(struct inode
*inode
,
1435 const char *name
, void **buffer
,
1438 struct socket_smack
*ssp
;
1439 struct socket
*sock
;
1440 struct super_block
*sbp
;
1441 struct inode
*ip
= (struct inode
*)inode
;
1442 struct smack_known
*isp
;
1444 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1445 isp
= smk_of_inode(inode
);
1448 * The rest of the Smack xattrs are only on sockets.
1451 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1454 sock
= SOCKET_I(ip
);
1455 if (sock
== NULL
|| sock
->sk
== NULL
)
1458 ssp
= sock
->sk
->sk_security
;
1460 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1462 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1469 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1470 if (*buffer
== NULL
)
1474 return strlen(isp
->smk_known
);
1479 * smack_inode_listsecurity - list the Smack attributes
1480 * @inode: the object
1481 * @buffer: where they go
1482 * @buffer_size: size of buffer
1484 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1487 int len
= sizeof(XATTR_NAME_SMACK
);
1489 if (buffer
!= NULL
&& len
<= buffer_size
)
1490 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1496 * smack_inode_getsecid - Extract inode's security id
1497 * @inode: inode to extract the info from
1498 * @secid: where result will be saved
1500 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1502 struct smack_known
*skp
= smk_of_inode(inode
);
1504 *secid
= skp
->smk_secid
;
1512 * There is no smack_file_permission hook
1514 * Should access checks be done on each read or write?
1515 * UNICOS and SELinux say yes.
1516 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1518 * I'll say no for now. Smack does not do the frequent
1519 * label changing that SELinux does.
1523 * smack_file_alloc_security - assign a file security blob
1526 * The security blob for a file is a pointer to the master
1527 * label list, so no allocation is done.
1529 * f_security is the owner security information. It
1530 * isn't used on file access checks, it's for send_sigio.
1534 static int smack_file_alloc_security(struct file
*file
)
1536 struct smack_known
**blob
= smack_file(file
);
1538 *blob
= smk_of_current();
1543 * smack_file_ioctl - Smack check on ioctls
1548 * Relies heavily on the correct use of the ioctl command conventions.
1550 * Returns 0 if allowed, error code otherwise
1552 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1556 struct smk_audit_info ad
;
1557 struct inode
*inode
= file_inode(file
);
1559 if (unlikely(IS_PRIVATE(inode
)))
1562 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1563 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1565 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1566 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1567 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1570 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1571 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1572 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1579 * smack_file_lock - Smack check on file locking
1583 * Returns 0 if current has lock access, error code otherwise
1585 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1587 struct smk_audit_info ad
;
1589 struct inode
*inode
= file_inode(file
);
1591 if (unlikely(IS_PRIVATE(inode
)))
1594 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1595 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1596 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1597 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1602 * smack_file_fcntl - Smack check on fcntl
1604 * @cmd: what action to check
1607 * Generally these operations are harmless.
1608 * File locking operations present an obvious mechanism
1609 * for passing information, so they require write access.
1611 * Returns 0 if current has access, error code otherwise
1613 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1616 struct smk_audit_info ad
;
1618 struct inode
*inode
= file_inode(file
);
1620 if (unlikely(IS_PRIVATE(inode
)))
1628 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1629 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1630 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1631 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1635 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1636 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1637 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1638 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1649 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1650 * if mapping anonymous memory.
1651 * @file contains the file structure for file to map (may be NULL).
1652 * @reqprot contains the protection requested by the application.
1653 * @prot contains the protection that will be applied by the kernel.
1654 * @flags contains the operational flags.
1655 * Return 0 if permission is granted.
1657 static int smack_mmap_file(struct file
*file
,
1658 unsigned long reqprot
, unsigned long prot
,
1659 unsigned long flags
)
1661 struct smack_known
*skp
;
1662 struct smack_known
*mkp
;
1663 struct smack_rule
*srp
;
1664 struct task_smack
*tsp
;
1665 struct smack_known
*okp
;
1666 struct inode_smack
*isp
;
1667 struct superblock_smack
*sbsp
;
1676 if (unlikely(IS_PRIVATE(file_inode(file
))))
1679 isp
= smack_inode(file_inode(file
));
1680 if (isp
->smk_mmap
== NULL
)
1682 sbsp
= file_inode(file
)->i_sb
->s_security
;
1683 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1684 isp
->smk_mmap
!= sbsp
->smk_root
)
1686 mkp
= isp
->smk_mmap
;
1688 tsp
= smack_cred(current_cred());
1689 skp
= smk_of_current();
1694 * For each Smack rule associated with the subject
1695 * label verify that the SMACK64MMAP also has access
1696 * to that rule's object label.
1698 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1699 okp
= srp
->smk_object
;
1701 * Matching labels always allows access.
1703 if (mkp
->smk_known
== okp
->smk_known
)
1706 * If there is a matching local rule take
1707 * that into account as well.
1709 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1713 may
= srp
->smk_access
;
1715 may
&= srp
->smk_access
;
1717 * If may is zero the SMACK64MMAP subject can't
1718 * possibly have less access.
1724 * Fetch the global list entry.
1725 * If there isn't one a SMACK64MMAP subject
1726 * can't have as much access as current.
1728 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1730 if (mmay
== -ENOENT
) {
1735 * If there is a local entry it modifies the
1736 * potential access, too.
1738 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1740 if (tmay
!= -ENOENT
)
1744 * If there is any access available to current that is
1745 * not available to a SMACK64MMAP subject
1748 if ((may
| mmay
) != mmay
) {
1760 * smack_file_set_fowner - set the file security blob value
1761 * @file: object in question
1764 static void smack_file_set_fowner(struct file
*file
)
1766 struct smack_known
**blob
= smack_file(file
);
1768 *blob
= smk_of_current();
1772 * smack_file_send_sigiotask - Smack on sigio
1773 * @tsk: The target task
1774 * @fown: the object the signal come from
1777 * Allow a privileged task to get signals even if it shouldn't
1779 * Returns 0 if a subject with the object's smack could
1780 * write to the task, an error code otherwise.
1782 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1783 struct fown_struct
*fown
, int signum
)
1785 struct smack_known
**blob
;
1786 struct smack_known
*skp
;
1787 struct smack_known
*tkp
= smk_of_task(smack_cred(tsk
->cred
));
1788 const struct cred
*tcred
;
1791 struct smk_audit_info ad
;
1794 * struct fown_struct is never outside the context of a struct file
1796 file
= container_of(fown
, struct file
, f_owner
);
1798 /* we don't log here as rc can be overriden */
1799 blob
= smack_file(file
);
1801 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1802 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1805 tcred
= __task_cred(tsk
);
1806 if (rc
!= 0 && smack_privileged_cred(CAP_MAC_OVERRIDE
, tcred
))
1810 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1811 smk_ad_setfield_u_tsk(&ad
, tsk
);
1812 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1817 * smack_file_receive - Smack file receive check
1820 * Returns 0 if current has access, error code otherwise
1822 static int smack_file_receive(struct file
*file
)
1826 struct smk_audit_info ad
;
1827 struct inode
*inode
= file_inode(file
);
1828 struct socket
*sock
;
1829 struct task_smack
*tsp
;
1830 struct socket_smack
*ssp
;
1832 if (unlikely(IS_PRIVATE(inode
)))
1835 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1836 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1838 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1839 sock
= SOCKET_I(inode
);
1840 ssp
= sock
->sk
->sk_security
;
1841 tsp
= smack_cred(current_cred());
1843 * If the receiving process can't write to the
1844 * passed socket or if the passed socket can't
1845 * write to the receiving process don't accept
1846 * the passed socket.
1848 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1849 rc
= smk_bu_file(file
, may
, rc
);
1852 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1853 rc
= smk_bu_file(file
, may
, rc
);
1857 * This code relies on bitmasks.
1859 if (file
->f_mode
& FMODE_READ
)
1861 if (file
->f_mode
& FMODE_WRITE
)
1864 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1865 rc
= smk_bu_file(file
, may
, rc
);
1870 * smack_file_open - Smack dentry open processing
1873 * Set the security blob in the file structure.
1874 * Allow the open only if the task has read access. There are
1875 * many read operations (e.g. fstat) that you can do with an
1876 * fd even if you have the file open write-only.
1878 * Returns 0 if current has access, error code otherwise
1880 static int smack_file_open(struct file
*file
)
1882 struct task_smack
*tsp
= smack_cred(file
->f_cred
);
1883 struct inode
*inode
= file_inode(file
);
1884 struct smk_audit_info ad
;
1887 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1888 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1889 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1890 rc
= smk_bu_credfile(file
->f_cred
, file
, MAY_READ
, rc
);
1900 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1901 * @cred: the new credentials
1902 * @gfp: the atomicity of any memory allocations
1904 * Prepare a blank set of credentials for modification. This must allocate all
1905 * the memory the LSM module might require such that cred_transfer() can
1906 * complete without error.
1908 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1910 init_task_smack(smack_cred(cred
), NULL
, NULL
);
1916 * smack_cred_free - "free" task-level security credentials
1917 * @cred: the credentials in question
1920 static void smack_cred_free(struct cred
*cred
)
1922 struct task_smack
*tsp
= smack_cred(cred
);
1923 struct smack_rule
*rp
;
1924 struct list_head
*l
;
1925 struct list_head
*n
;
1927 smk_destroy_label_list(&tsp
->smk_relabel
);
1929 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1930 rp
= list_entry(l
, struct smack_rule
, list
);
1931 list_del(&rp
->list
);
1932 kmem_cache_free(smack_rule_cache
, rp
);
1937 * smack_cred_prepare - prepare new set of credentials for modification
1938 * @new: the new credentials
1939 * @old: the original credentials
1940 * @gfp: the atomicity of any memory allocations
1942 * Prepare a new set of credentials for modification.
1944 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1947 struct task_smack
*old_tsp
= smack_cred(old
);
1948 struct task_smack
*new_tsp
= smack_cred(new);
1951 init_task_smack(new_tsp
, old_tsp
->smk_task
, old_tsp
->smk_task
);
1953 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1957 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
1963 * smack_cred_transfer - Transfer the old credentials to the new credentials
1964 * @new: the new credentials
1965 * @old: the original credentials
1967 * Fill in a set of blank credentials from another set of credentials.
1969 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1971 struct task_smack
*old_tsp
= smack_cred(old
);
1972 struct task_smack
*new_tsp
= smack_cred(new);
1974 new_tsp
->smk_task
= old_tsp
->smk_task
;
1975 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1976 mutex_init(&new_tsp
->smk_rules_lock
);
1977 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1979 /* cbs copy rule list */
1983 * smack_cred_getsecid - get the secid corresponding to a creds structure
1984 * @cred: the object creds
1985 * @secid: where to put the result
1987 * Sets the secid to contain a u32 version of the smack label.
1989 static void smack_cred_getsecid(const struct cred
*cred
, u32
*secid
)
1991 struct smack_known
*skp
;
1994 skp
= smk_of_task(smack_cred(cred
));
1995 *secid
= skp
->smk_secid
;
2000 * smack_kernel_act_as - Set the subjective context in a set of credentials
2001 * @new: points to the set of credentials to be modified.
2002 * @secid: specifies the security ID to be set
2004 * Set the security data for a kernel service.
2006 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
2008 struct task_smack
*new_tsp
= smack_cred(new);
2010 new_tsp
->smk_task
= smack_from_secid(secid
);
2015 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2016 * @new: points to the set of credentials to be modified
2017 * @inode: points to the inode to use as a reference
2019 * Set the file creation context in a set of credentials to the same
2020 * as the objective context of the specified inode
2022 static int smack_kernel_create_files_as(struct cred
*new,
2023 struct inode
*inode
)
2025 struct inode_smack
*isp
= smack_inode(inode
);
2026 struct task_smack
*tsp
= smack_cred(new);
2028 tsp
->smk_forked
= isp
->smk_inode
;
2029 tsp
->smk_task
= tsp
->smk_forked
;
2034 * smk_curacc_on_task - helper to log task related access
2035 * @p: the task object
2036 * @access: the access requested
2037 * @caller: name of the calling function for audit
2039 * Return 0 if access is permitted
2041 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
2044 struct smk_audit_info ad
;
2045 struct smack_known
*skp
= smk_of_task_struct(p
);
2048 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2049 smk_ad_setfield_u_tsk(&ad
, p
);
2050 rc
= smk_curacc(skp
, access
, &ad
);
2051 rc
= smk_bu_task(p
, access
, rc
);
2056 * smack_task_setpgid - Smack check on setting pgid
2057 * @p: the task object
2060 * Return 0 if write access is permitted
2062 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2064 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2068 * smack_task_getpgid - Smack access check for getpgid
2069 * @p: the object task
2071 * Returns 0 if current can read the object task, error code otherwise
2073 static int smack_task_getpgid(struct task_struct
*p
)
2075 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2079 * smack_task_getsid - Smack access check for getsid
2080 * @p: the object task
2082 * Returns 0 if current can read the object task, error code otherwise
2084 static int smack_task_getsid(struct task_struct
*p
)
2086 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2090 * smack_task_getsecid - get the secid of the task
2091 * @p: the object task
2092 * @secid: where to put the result
2094 * Sets the secid to contain a u32 version of the smack label.
2096 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2098 struct smack_known
*skp
= smk_of_task_struct(p
);
2100 *secid
= skp
->smk_secid
;
2104 * smack_task_setnice - Smack check on setting nice
2105 * @p: the task object
2108 * Return 0 if write access is permitted
2110 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2112 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2116 * smack_task_setioprio - Smack check on setting ioprio
2117 * @p: the task object
2120 * Return 0 if write access is permitted
2122 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2124 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2128 * smack_task_getioprio - Smack check on reading ioprio
2129 * @p: the task object
2131 * Return 0 if read access is permitted
2133 static int smack_task_getioprio(struct task_struct
*p
)
2135 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2139 * smack_task_setscheduler - Smack check on setting scheduler
2140 * @p: the task object
2142 * Return 0 if read access is permitted
2144 static int smack_task_setscheduler(struct task_struct
*p
)
2146 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2150 * smack_task_getscheduler - Smack check on reading scheduler
2151 * @p: the task object
2153 * Return 0 if read access is permitted
2155 static int smack_task_getscheduler(struct task_struct
*p
)
2157 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2161 * smack_task_movememory - Smack check on moving memory
2162 * @p: the task object
2164 * Return 0 if write access is permitted
2166 static int smack_task_movememory(struct task_struct
*p
)
2168 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2172 * smack_task_kill - Smack check on signal delivery
2173 * @p: the task object
2176 * @cred: identifies the cred to use in lieu of current's
2178 * Return 0 if write access is permitted
2181 static int smack_task_kill(struct task_struct
*p
, struct kernel_siginfo
*info
,
2182 int sig
, const struct cred
*cred
)
2184 struct smk_audit_info ad
;
2185 struct smack_known
*skp
;
2186 struct smack_known
*tkp
= smk_of_task_struct(p
);
2190 return 0; /* null signal; existence test */
2192 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2193 smk_ad_setfield_u_tsk(&ad
, p
);
2195 * Sending a signal requires that the sender
2196 * can write the receiver.
2199 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2200 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2204 * If the cred isn't NULL we're dealing with some USB IO
2205 * specific behavior. This is not clean. For one thing
2206 * we can't take privilege into account.
2208 skp
= smk_of_task(smack_cred(cred
));
2209 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2210 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2215 * smack_task_to_inode - copy task smack into the inode blob
2216 * @p: task to copy from
2217 * @inode: inode to copy to
2219 * Sets the smack pointer in the inode security blob
2221 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2223 struct inode_smack
*isp
= smack_inode(inode
);
2224 struct smack_known
*skp
= smk_of_task_struct(p
);
2226 isp
->smk_inode
= skp
;
2227 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2235 * smack_sk_alloc_security - Allocate a socket blob
2238 * @gfp_flags: memory allocation flags
2240 * Assign Smack pointers to current
2242 * Returns 0 on success, -ENOMEM is there's no memory
2244 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2246 struct smack_known
*skp
= smk_of_current();
2247 struct socket_smack
*ssp
;
2249 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2254 * Sockets created by kernel threads receive web label.
2256 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2257 ssp
->smk_in
= &smack_known_web
;
2258 ssp
->smk_out
= &smack_known_web
;
2263 ssp
->smk_packet
= NULL
;
2265 sk
->sk_security
= ssp
;
2271 * smack_sk_free_security - Free a socket blob
2274 * Clears the blob pointer
2276 static void smack_sk_free_security(struct sock
*sk
)
2278 #ifdef SMACK_IPV6_PORT_LABELING
2279 struct smk_port_label
*spp
;
2281 if (sk
->sk_family
== PF_INET6
) {
2283 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2284 if (spp
->smk_sock
!= sk
)
2286 spp
->smk_can_reuse
= 1;
2292 kfree(sk
->sk_security
);
2296 * smack_ipv4host_label - check host based restrictions
2297 * @sip: the object end
2299 * looks for host based access restrictions
2301 * This version will only be appropriate for really small sets of single label
2302 * hosts. The caller is responsible for ensuring that the RCU read lock is
2303 * taken before calling this function.
2305 * Returns the label of the far end or NULL if it's not special.
2307 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2309 struct smk_net4addr
*snp
;
2310 struct in_addr
*siap
= &sip
->sin_addr
;
2312 if (siap
->s_addr
== 0)
2315 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2317 * we break after finding the first match because
2318 * the list is sorted from longest to shortest mask
2319 * so we have found the most specific match
2321 if (snp
->smk_host
.s_addr
==
2322 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2323 return snp
->smk_label
;
2328 #if IS_ENABLED(CONFIG_IPV6)
2330 * smk_ipv6_localhost - Check for local ipv6 host address
2333 * Returns boolean true if this is the localhost address
2335 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2337 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2338 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2340 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2341 ntohs(be16p
[7]) == 1)
2347 * smack_ipv6host_label - check host based restrictions
2348 * @sip: the object end
2350 * looks for host based access restrictions
2352 * This version will only be appropriate for really small sets of single label
2353 * hosts. The caller is responsible for ensuring that the RCU read lock is
2354 * taken before calling this function.
2356 * Returns the label of the far end or NULL if it's not special.
2358 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2360 struct smk_net6addr
*snp
;
2361 struct in6_addr
*sap
= &sip
->sin6_addr
;
2366 * It's local. Don't look for a host label.
2368 if (smk_ipv6_localhost(sip
))
2371 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2373 * If the label is NULL the entry has
2374 * been renounced. Ignore it.
2376 if (snp
->smk_label
== NULL
)
2379 * we break after finding the first match because
2380 * the list is sorted from longest to shortest mask
2381 * so we have found the most specific match
2383 for (found
= 1, i
= 0; i
< 8; i
++) {
2384 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2385 snp
->smk_host
.s6_addr16
[i
]) {
2391 return snp
->smk_label
;
2396 #endif /* CONFIG_IPV6 */
2399 * smack_netlabel - Set the secattr on a socket
2401 * @labeled: socket label scheme
2403 * Convert the outbound smack value (smk_out) to a
2404 * secattr and attach it to the socket.
2406 * Returns 0 on success or an error code
2408 static int smack_netlabel(struct sock
*sk
, int labeled
)
2410 struct smack_known
*skp
;
2411 struct socket_smack
*ssp
= sk
->sk_security
;
2415 * Usually the netlabel code will handle changing the
2416 * packet labeling based on the label.
2417 * The case of a single label host is different, because
2418 * a single label host should never get a labeled packet
2419 * even though the label is usually associated with a packet
2423 bh_lock_sock_nested(sk
);
2425 if (ssp
->smk_out
== smack_net_ambient
||
2426 labeled
== SMACK_UNLABELED_SOCKET
)
2427 netlbl_sock_delattr(sk
);
2430 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2440 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2442 * @sap: the destination address
2444 * Set the correct secattr for the given socket based on the destination
2445 * address and perform any outbound access checks needed.
2447 * Returns 0 on success or an error code.
2450 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2452 struct smack_known
*skp
;
2455 struct smack_known
*hkp
;
2456 struct socket_smack
*ssp
= sk
->sk_security
;
2457 struct smk_audit_info ad
;
2460 hkp
= smack_ipv4host_label(sap
);
2463 struct lsm_network_audit net
;
2465 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2466 ad
.a
.u
.net
->family
= sap
->sin_family
;
2467 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2468 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2470 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2472 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2473 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2475 sk_lbl
= SMACK_CIPSO_SOCKET
;
2482 return smack_netlabel(sk
, sk_lbl
);
2485 #if IS_ENABLED(CONFIG_IPV6)
2487 * smk_ipv6_check - check Smack access
2488 * @subject: subject Smack label
2489 * @object: object Smack label
2491 * @act: the action being taken
2493 * Check an IPv6 access
2495 static int smk_ipv6_check(struct smack_known
*subject
,
2496 struct smack_known
*object
,
2497 struct sockaddr_in6
*address
, int act
)
2500 struct lsm_network_audit net
;
2502 struct smk_audit_info ad
;
2506 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2507 ad
.a
.u
.net
->family
= PF_INET6
;
2508 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2509 if (act
== SMK_RECEIVING
)
2510 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2512 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2514 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2515 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2518 #endif /* CONFIG_IPV6 */
2520 #ifdef SMACK_IPV6_PORT_LABELING
2522 * smk_ipv6_port_label - Smack port access table management
2526 * Create or update the port list entry
2528 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2530 struct sock
*sk
= sock
->sk
;
2531 struct sockaddr_in6
*addr6
;
2532 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2533 struct smk_port_label
*spp
;
2534 unsigned short port
= 0;
2536 if (address
== NULL
) {
2538 * This operation is changing the Smack information
2539 * on the bound socket. Take the changes to the port
2543 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2544 if (sk
!= spp
->smk_sock
)
2546 spp
->smk_in
= ssp
->smk_in
;
2547 spp
->smk_out
= ssp
->smk_out
;
2552 * A NULL address is only used for updating existing
2553 * bound entries. If there isn't one, it's OK.
2559 addr6
= (struct sockaddr_in6
*)address
;
2560 port
= ntohs(addr6
->sin6_port
);
2562 * This is a special case that is safely ignored.
2568 * Look for an existing port list entry.
2569 * This is an indication that a port is getting reused.
2572 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2573 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2575 if (spp
->smk_can_reuse
!= 1) {
2579 spp
->smk_port
= port
;
2581 spp
->smk_in
= ssp
->smk_in
;
2582 spp
->smk_out
= ssp
->smk_out
;
2583 spp
->smk_can_reuse
= 0;
2589 * A new port entry is required.
2591 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2595 spp
->smk_port
= port
;
2597 spp
->smk_in
= ssp
->smk_in
;
2598 spp
->smk_out
= ssp
->smk_out
;
2599 spp
->smk_sock_type
= sock
->type
;
2600 spp
->smk_can_reuse
= 0;
2602 mutex_lock(&smack_ipv6_lock
);
2603 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2604 mutex_unlock(&smack_ipv6_lock
);
2609 * smk_ipv6_port_check - check Smack port access
2612 * @act: the action being taken
2614 * Create or update the port list entry
2616 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2619 struct smk_port_label
*spp
;
2620 struct socket_smack
*ssp
= sk
->sk_security
;
2621 struct smack_known
*skp
= NULL
;
2622 unsigned short port
;
2623 struct smack_known
*object
;
2625 if (act
== SMK_RECEIVING
) {
2626 skp
= smack_ipv6host_label(address
);
2627 object
= ssp
->smk_in
;
2630 object
= smack_ipv6host_label(address
);
2634 * The other end is a single label host.
2636 if (skp
!= NULL
&& object
!= NULL
)
2637 return smk_ipv6_check(skp
, object
, address
, act
);
2639 skp
= smack_net_ambient
;
2641 object
= smack_net_ambient
;
2644 * It's remote, so port lookup does no good.
2646 if (!smk_ipv6_localhost(address
))
2647 return smk_ipv6_check(skp
, object
, address
, act
);
2650 * It's local so the send check has to have passed.
2652 if (act
== SMK_RECEIVING
)
2655 port
= ntohs(address
->sin6_port
);
2657 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2658 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2660 object
= spp
->smk_in
;
2661 if (act
== SMK_CONNECTING
)
2662 ssp
->smk_packet
= spp
->smk_out
;
2667 return smk_ipv6_check(skp
, object
, address
, act
);
2669 #endif /* SMACK_IPV6_PORT_LABELING */
2672 * smack_inode_setsecurity - set smack xattrs
2673 * @inode: the object
2674 * @name: attribute name
2675 * @value: attribute value
2676 * @size: size of the attribute
2679 * Sets the named attribute in the appropriate blob
2681 * Returns 0 on success, or an error code
2683 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2684 const void *value
, size_t size
, int flags
)
2686 struct smack_known
*skp
;
2687 struct inode_smack
*nsp
= smack_inode(inode
);
2688 struct socket_smack
*ssp
;
2689 struct socket
*sock
;
2692 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2695 skp
= smk_import_entry(value
, size
);
2697 return PTR_ERR(skp
);
2699 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2700 nsp
->smk_inode
= skp
;
2701 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2705 * The rest of the Smack xattrs are only on sockets.
2707 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2710 sock
= SOCKET_I(inode
);
2711 if (sock
== NULL
|| sock
->sk
== NULL
)
2714 ssp
= sock
->sk
->sk_security
;
2716 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2718 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2720 if (sock
->sk
->sk_family
== PF_INET
) {
2721 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2724 "Smack: \"%s\" netlbl error %d.\n",
2730 #ifdef SMACK_IPV6_PORT_LABELING
2731 if (sock
->sk
->sk_family
== PF_INET6
)
2732 smk_ipv6_port_label(sock
, NULL
);
2739 * smack_socket_post_create - finish socket setup
2741 * @family: protocol family
2746 * Sets the netlabel information on the socket
2748 * Returns 0 on success, and error code otherwise
2750 static int smack_socket_post_create(struct socket
*sock
, int family
,
2751 int type
, int protocol
, int kern
)
2753 struct socket_smack
*ssp
;
2755 if (sock
->sk
== NULL
)
2759 * Sockets created by kernel threads receive web label.
2761 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2762 ssp
= sock
->sk
->sk_security
;
2763 ssp
->smk_in
= &smack_known_web
;
2764 ssp
->smk_out
= &smack_known_web
;
2767 if (family
!= PF_INET
)
2770 * Set the outbound netlbl.
2772 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2776 * smack_socket_socketpair - create socket pair
2777 * @socka: one socket
2778 * @sockb: another socket
2780 * Cross reference the peer labels for SO_PEERSEC
2784 static int smack_socket_socketpair(struct socket
*socka
,
2785 struct socket
*sockb
)
2787 struct socket_smack
*asp
= socka
->sk
->sk_security
;
2788 struct socket_smack
*bsp
= sockb
->sk
->sk_security
;
2790 asp
->smk_packet
= bsp
->smk_out
;
2791 bsp
->smk_packet
= asp
->smk_out
;
2796 #ifdef SMACK_IPV6_PORT_LABELING
2798 * smack_socket_bind - record port binding information.
2800 * @address: the port address
2801 * @addrlen: size of the address
2803 * Records the label bound to a port.
2805 * Returns 0 on success, and error code otherwise
2807 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2810 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
) {
2811 if (addrlen
< SIN6_LEN_RFC2133
||
2812 address
->sa_family
!= AF_INET6
)
2814 smk_ipv6_port_label(sock
, address
);
2818 #endif /* SMACK_IPV6_PORT_LABELING */
2821 * smack_socket_connect - connect access check
2823 * @sap: the other end
2824 * @addrlen: size of sap
2826 * Verifies that a connection may be possible
2828 * Returns 0 on success, and error code otherwise
2830 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2834 #if IS_ENABLED(CONFIG_IPV6)
2835 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2837 #ifdef SMACK_IPV6_SECMARK_LABELING
2838 struct smack_known
*rsp
;
2839 struct socket_smack
*ssp
;
2842 if (sock
->sk
== NULL
)
2845 #ifdef SMACK_IPV6_SECMARK_LABELING
2846 ssp
= sock
->sk
->sk_security
;
2849 switch (sock
->sk
->sk_family
) {
2851 if (addrlen
< sizeof(struct sockaddr_in
) ||
2852 sap
->sa_family
!= AF_INET
)
2854 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2857 if (addrlen
< SIN6_LEN_RFC2133
|| sap
->sa_family
!= AF_INET6
)
2859 #ifdef SMACK_IPV6_SECMARK_LABELING
2860 rsp
= smack_ipv6host_label(sip
);
2862 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2865 #ifdef SMACK_IPV6_PORT_LABELING
2866 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2874 * smack_flags_to_may - convert S_ to MAY_ values
2875 * @flags: the S_ value
2877 * Returns the equivalent MAY_ value
2879 static int smack_flags_to_may(int flags
)
2883 if (flags
& S_IRUGO
)
2885 if (flags
& S_IWUGO
)
2887 if (flags
& S_IXUGO
)
2894 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2899 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2901 struct smack_known
**blob
= smack_msg_msg(msg
);
2903 *blob
= smk_of_current();
2908 * smack_of_ipc - the smack pointer for the ipc
2911 * Returns a pointer to the smack value
2913 static struct smack_known
*smack_of_ipc(struct kern_ipc_perm
*isp
)
2915 struct smack_known
**blob
= smack_ipc(isp
);
2921 * smack_ipc_alloc_security - Set the security blob for ipc
2926 static int smack_ipc_alloc_security(struct kern_ipc_perm
*isp
)
2928 struct smack_known
**blob
= smack_ipc(isp
);
2930 *blob
= smk_of_current();
2935 * smk_curacc_shm : check if current has access on shm
2937 * @access : access requested
2939 * Returns 0 if current has the requested access, error code otherwise
2941 static int smk_curacc_shm(struct kern_ipc_perm
*isp
, int access
)
2943 struct smack_known
*ssp
= smack_of_ipc(isp
);
2944 struct smk_audit_info ad
;
2948 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2949 ad
.a
.u
.ipc_id
= isp
->id
;
2951 rc
= smk_curacc(ssp
, access
, &ad
);
2952 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2957 * smack_shm_associate - Smack access check for shm
2959 * @shmflg: access requested
2961 * Returns 0 if current has the requested access, error code otherwise
2963 static int smack_shm_associate(struct kern_ipc_perm
*isp
, int shmflg
)
2967 may
= smack_flags_to_may(shmflg
);
2968 return smk_curacc_shm(isp
, may
);
2972 * smack_shm_shmctl - Smack access check for shm
2974 * @cmd: what it wants to do
2976 * Returns 0 if current has the requested access, error code otherwise
2978 static int smack_shm_shmctl(struct kern_ipc_perm
*isp
, int cmd
)
2992 may
= MAY_READWRITE
;
2997 * System level information.
3003 return smk_curacc_shm(isp
, may
);
3007 * smack_shm_shmat - Smack access for shmat
3010 * @shmflg: access requested
3012 * Returns 0 if current has the requested access, error code otherwise
3014 static int smack_shm_shmat(struct kern_ipc_perm
*isp
, char __user
*shmaddr
,
3019 may
= smack_flags_to_may(shmflg
);
3020 return smk_curacc_shm(isp
, may
);
3024 * smk_curacc_sem : check if current has access on sem
3026 * @access : access requested
3028 * Returns 0 if current has the requested access, error code otherwise
3030 static int smk_curacc_sem(struct kern_ipc_perm
*isp
, int access
)
3032 struct smack_known
*ssp
= smack_of_ipc(isp
);
3033 struct smk_audit_info ad
;
3037 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3038 ad
.a
.u
.ipc_id
= isp
->id
;
3040 rc
= smk_curacc(ssp
, access
, &ad
);
3041 rc
= smk_bu_current("sem", ssp
, access
, rc
);
3046 * smack_sem_associate - Smack access check for sem
3048 * @semflg: access requested
3050 * Returns 0 if current has the requested access, error code otherwise
3052 static int smack_sem_associate(struct kern_ipc_perm
*isp
, int semflg
)
3056 may
= smack_flags_to_may(semflg
);
3057 return smk_curacc_sem(isp
, may
);
3061 * smack_sem_shmctl - Smack access check for sem
3063 * @cmd: what it wants to do
3065 * Returns 0 if current has the requested access, error code otherwise
3067 static int smack_sem_semctl(struct kern_ipc_perm
*isp
, int cmd
)
3086 may
= MAY_READWRITE
;
3091 * System level information
3098 return smk_curacc_sem(isp
, may
);
3102 * smack_sem_semop - Smack checks of semaphore operations
3108 * Treated as read and write in all cases.
3110 * Returns 0 if access is allowed, error code otherwise
3112 static int smack_sem_semop(struct kern_ipc_perm
*isp
, struct sembuf
*sops
,
3113 unsigned nsops
, int alter
)
3115 return smk_curacc_sem(isp
, MAY_READWRITE
);
3119 * smk_curacc_msq : helper to check if current has access on msq
3121 * @access : access requested
3123 * return 0 if current has access, error otherwise
3125 static int smk_curacc_msq(struct kern_ipc_perm
*isp
, int access
)
3127 struct smack_known
*msp
= smack_of_ipc(isp
);
3128 struct smk_audit_info ad
;
3132 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3133 ad
.a
.u
.ipc_id
= isp
->id
;
3135 rc
= smk_curacc(msp
, access
, &ad
);
3136 rc
= smk_bu_current("msq", msp
, access
, rc
);
3141 * smack_msg_queue_associate - Smack access check for msg_queue
3143 * @msqflg: access requested
3145 * Returns 0 if current has the requested access, error code otherwise
3147 static int smack_msg_queue_associate(struct kern_ipc_perm
*isp
, int msqflg
)
3151 may
= smack_flags_to_may(msqflg
);
3152 return smk_curacc_msq(isp
, may
);
3156 * smack_msg_queue_msgctl - Smack access check for msg_queue
3158 * @cmd: what it wants to do
3160 * Returns 0 if current has the requested access, error code otherwise
3162 static int smack_msg_queue_msgctl(struct kern_ipc_perm
*isp
, int cmd
)
3174 may
= MAY_READWRITE
;
3179 * System level information
3186 return smk_curacc_msq(isp
, may
);
3190 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3193 * @msqflg: access requested
3195 * Returns 0 if current has the requested access, error code otherwise
3197 static int smack_msg_queue_msgsnd(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3202 may
= smack_flags_to_may(msqflg
);
3203 return smk_curacc_msq(isp
, may
);
3207 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3214 * Returns 0 if current has read and write access, error code otherwise
3216 static int smack_msg_queue_msgrcv(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3217 struct task_struct
*target
, long type
, int mode
)
3219 return smk_curacc_msq(isp
, MAY_READWRITE
);
3223 * smack_ipc_permission - Smack access for ipc_permission()
3224 * @ipp: the object permissions
3225 * @flag: access requested
3227 * Returns 0 if current has read and write access, error code otherwise
3229 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3231 struct smack_known
**blob
= smack_ipc(ipp
);
3232 struct smack_known
*iskp
= *blob
;
3233 int may
= smack_flags_to_may(flag
);
3234 struct smk_audit_info ad
;
3238 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3239 ad
.a
.u
.ipc_id
= ipp
->id
;
3241 rc
= smk_curacc(iskp
, may
, &ad
);
3242 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3247 * smack_ipc_getsecid - Extract smack security id
3248 * @ipp: the object permissions
3249 * @secid: where result will be saved
3251 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3253 struct smack_known
**blob
= smack_ipc(ipp
);
3254 struct smack_known
*iskp
= *blob
;
3256 *secid
= iskp
->smk_secid
;
3260 * smack_d_instantiate - Make sure the blob is correct on an inode
3261 * @opt_dentry: dentry where inode will be attached
3262 * @inode: the object
3264 * Set the inode's security blob if it hasn't been done already.
3266 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3268 struct super_block
*sbp
;
3269 struct superblock_smack
*sbsp
;
3270 struct inode_smack
*isp
;
3271 struct smack_known
*skp
;
3272 struct smack_known
*ckp
= smk_of_current();
3273 struct smack_known
*final
;
3274 char trattr
[TRANS_TRUE_SIZE
];
3282 isp
= smack_inode(inode
);
3284 mutex_lock(&isp
->smk_lock
);
3286 * If the inode is already instantiated
3287 * take the quick way out
3289 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3293 sbsp
= sbp
->s_security
;
3295 * We're going to use the superblock default label
3296 * if there's no label on the file.
3298 final
= sbsp
->smk_default
;
3301 * If this is the root inode the superblock
3302 * may be in the process of initialization.
3303 * If that is the case use the root value out
3304 * of the superblock.
3306 if (opt_dentry
->d_parent
== opt_dentry
) {
3307 switch (sbp
->s_magic
) {
3308 case CGROUP_SUPER_MAGIC
:
3309 case CGROUP2_SUPER_MAGIC
:
3311 * The cgroup filesystem is never mounted,
3312 * so there's no opportunity to set the mount
3315 sbsp
->smk_root
= &smack_known_star
;
3316 sbsp
->smk_default
= &smack_known_star
;
3317 isp
->smk_inode
= sbsp
->smk_root
;
3321 * What about shmem/tmpfs anonymous files with dentry
3322 * obtained from d_alloc_pseudo()?
3324 isp
->smk_inode
= smk_of_current();
3327 isp
->smk_inode
= smk_of_current();
3331 * Socket access is controlled by the socket
3332 * structures associated with the task involved.
3334 isp
->smk_inode
= &smack_known_star
;
3337 isp
->smk_inode
= sbsp
->smk_root
;
3340 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3345 * This is pretty hackish.
3346 * Casey says that we shouldn't have to do
3347 * file system specific code, but it does help
3348 * with keeping it simple.
3350 switch (sbp
->s_magic
) {
3352 case CGROUP_SUPER_MAGIC
:
3353 case CGROUP2_SUPER_MAGIC
:
3355 * Casey says that it's a little embarrassing
3356 * that the smack file system doesn't do
3357 * extended attributes.
3359 * Cgroupfs is special
3361 final
= &smack_known_star
;
3363 case DEVPTS_SUPER_MAGIC
:
3365 * devpts seems content with the label of the task.
3366 * Programs that change smack have to treat the
3371 case PROC_SUPER_MAGIC
:
3373 * Casey says procfs appears not to care.
3374 * The superblock default suffices.
3379 * Device labels should come from the filesystem,
3380 * but watch out, because they're volitile,
3381 * getting recreated on every reboot.
3383 final
= &smack_known_star
;
3385 * If a smack value has been set we want to use it,
3386 * but since tmpfs isn't giving us the opportunity
3387 * to set mount options simulate setting the
3388 * superblock default.
3393 * This isn't an understood special case.
3394 * Get the value from the xattr.
3398 * UNIX domain sockets use lower level socket data.
3400 if (S_ISSOCK(inode
->i_mode
)) {
3401 final
= &smack_known_star
;
3405 * No xattr support means, alas, no SMACK label.
3406 * Use the aforeapplied default.
3407 * It would be curious if the label of the task
3408 * does not match that assigned.
3410 if (!(inode
->i_opflags
& IOP_XATTR
))
3413 * Get the dentry for xattr.
3415 dp
= dget(opt_dentry
);
3416 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3417 if (!IS_ERR_OR_NULL(skp
))
3421 * Transmuting directory
3423 if (S_ISDIR(inode
->i_mode
)) {
3425 * If this is a new directory and the label was
3426 * transmuted when the inode was initialized
3427 * set the transmute attribute on the directory
3428 * and mark the inode.
3430 * If there is a transmute attribute on the
3431 * directory mark the inode.
3433 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3434 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3435 rc
= __vfs_setxattr(dp
, inode
,
3436 XATTR_NAME_SMACKTRANSMUTE
,
3437 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3440 rc
= __vfs_getxattr(dp
, inode
,
3441 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3443 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3444 TRANS_TRUE_SIZE
) != 0)
3448 transflag
= SMK_INODE_TRANSMUTE
;
3451 * Don't let the exec or mmap label be "*" or "@".
3453 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3454 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3455 skp
== &smack_known_web
)
3457 isp
->smk_task
= skp
;
3459 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3460 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3461 skp
== &smack_known_web
)
3463 isp
->smk_mmap
= skp
;
3470 isp
->smk_inode
= ckp
;
3472 isp
->smk_inode
= final
;
3474 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3477 mutex_unlock(&isp
->smk_lock
);
3482 * smack_getprocattr - Smack process attribute access
3483 * @p: the object task
3484 * @name: the name of the attribute in /proc/.../attr
3485 * @value: where to put the result
3487 * Places a copy of the task Smack into value
3489 * Returns the length of the smack label or an error code
3491 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3493 struct smack_known
*skp
= smk_of_task_struct(p
);
3497 if (strcmp(name
, "current") != 0)
3500 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3510 * smack_setprocattr - Smack process attribute setting
3511 * @name: the name of the attribute in /proc/.../attr
3512 * @value: the value to set
3513 * @size: the size of the value
3515 * Sets the Smack value of the task. Only setting self
3516 * is permitted and only with privilege
3518 * Returns the length of the smack label or an error code
3520 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3522 struct task_smack
*tsp
= smack_cred(current_cred());
3524 struct smack_known
*skp
;
3525 struct smack_known_list_elem
*sklep
;
3528 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3531 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3534 if (strcmp(name
, "current") != 0)
3537 skp
= smk_import_entry(value
, size
);
3539 return PTR_ERR(skp
);
3542 * No process is ever allowed the web ("@") label
3543 * and the star ("*") label.
3545 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3548 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3550 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3551 if (sklep
->smk_label
== skp
) {
3559 new = prepare_creds();
3563 tsp
= smack_cred(new);
3564 tsp
->smk_task
= skp
;
3566 * process can change its label only once
3568 smk_destroy_label_list(&tsp
->smk_relabel
);
3575 * smack_unix_stream_connect - Smack access on UDS
3577 * @other: the other sock
3580 * Return 0 if a subject with the smack of sock could access
3581 * an object with the smack of other, otherwise an error code
3583 static int smack_unix_stream_connect(struct sock
*sock
,
3584 struct sock
*other
, struct sock
*newsk
)
3586 struct smack_known
*skp
;
3587 struct smack_known
*okp
;
3588 struct socket_smack
*ssp
= sock
->sk_security
;
3589 struct socket_smack
*osp
= other
->sk_security
;
3590 struct socket_smack
*nsp
= newsk
->sk_security
;
3591 struct smk_audit_info ad
;
3594 struct lsm_network_audit net
;
3597 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3601 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3602 smk_ad_setfield_u_net_sk(&ad
, other
);
3604 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3605 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3609 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3610 rc
= smk_bu_note("UDS connect", okp
, skp
,
3616 * Cross reference the peer labels for SO_PEERSEC.
3619 nsp
->smk_packet
= ssp
->smk_out
;
3620 ssp
->smk_packet
= osp
->smk_out
;
3627 * smack_unix_may_send - Smack access on UDS
3629 * @other: the other socket
3631 * Return 0 if a subject with the smack of sock could access
3632 * an object with the smack of other, otherwise an error code
3634 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3636 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3637 struct socket_smack
*osp
= other
->sk
->sk_security
;
3638 struct smk_audit_info ad
;
3642 struct lsm_network_audit net
;
3644 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3645 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3648 if (smack_privileged(CAP_MAC_OVERRIDE
))
3651 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3652 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3657 * smack_socket_sendmsg - Smack check based on destination host
3660 * @size: the size of the message
3662 * Return 0 if the current subject can write to the destination host.
3663 * For IPv4 this is only a question if the destination is a single label host.
3664 * For IPv6 this is a check against the label of the port.
3666 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3669 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3670 #if IS_ENABLED(CONFIG_IPV6)
3671 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3673 #ifdef SMACK_IPV6_SECMARK_LABELING
3674 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3675 struct smack_known
*rsp
;
3680 * Perfectly reasonable for this to be NULL
3685 switch (sock
->sk
->sk_family
) {
3687 if (msg
->msg_namelen
< sizeof(struct sockaddr_in
) ||
3688 sip
->sin_family
!= AF_INET
)
3690 rc
= smack_netlabel_send(sock
->sk
, sip
);
3692 #if IS_ENABLED(CONFIG_IPV6)
3694 if (msg
->msg_namelen
< SIN6_LEN_RFC2133
||
3695 sap
->sin6_family
!= AF_INET6
)
3697 #ifdef SMACK_IPV6_SECMARK_LABELING
3698 rsp
= smack_ipv6host_label(sap
);
3700 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3703 #ifdef SMACK_IPV6_PORT_LABELING
3704 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3706 #endif /* IS_ENABLED(CONFIG_IPV6) */
3713 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3714 * @sap: netlabel secattr
3715 * @ssp: socket security information
3717 * Returns a pointer to a Smack label entry found on the label list.
3719 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3720 struct socket_smack
*ssp
)
3722 struct smack_known
*skp
;
3727 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3729 * Looks like a CIPSO packet.
3730 * If there are flags but no level netlabel isn't
3731 * behaving the way we expect it to.
3733 * Look it up in the label table
3734 * Without guidance regarding the smack value
3735 * for the packet fall back on the network
3739 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3740 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3743 * Compare the catsets. Use the netlbl APIs.
3745 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3746 if ((skp
->smk_netlabel
.flags
&
3747 NETLBL_SECATTR_MLS_CAT
) == 0)
3751 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3752 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3754 kcat
= netlbl_catmap_walk(
3755 skp
->smk_netlabel
.attr
.mls
.cat
,
3757 if (acat
< 0 || kcat
< 0)
3770 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3771 return &smack_known_web
;
3772 return &smack_known_star
;
3774 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3776 * Looks like a fallback, which gives us a secid.
3778 return smack_from_secid(sap
->attr
.secid
);
3780 * Without guidance regarding the smack value
3781 * for the packet fall back on the network
3784 return smack_net_ambient
;
3787 #if IS_ENABLED(CONFIG_IPV6)
3788 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3792 int proto
= -EINVAL
;
3793 struct ipv6hdr _ipv6h
;
3794 struct ipv6hdr
*ip6
;
3796 struct tcphdr _tcph
, *th
;
3797 struct udphdr _udph
, *uh
;
3798 struct dccp_hdr _dccph
, *dh
;
3802 offset
= skb_network_offset(skb
);
3803 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3806 sip
->sin6_addr
= ip6
->saddr
;
3808 nexthdr
= ip6
->nexthdr
;
3809 offset
+= sizeof(_ipv6h
);
3810 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3817 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3819 sip
->sin6_port
= th
->source
;
3822 case IPPROTO_UDPLITE
:
3823 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3825 sip
->sin6_port
= uh
->source
;
3828 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3830 sip
->sin6_port
= dh
->dccph_sport
;
3835 #endif /* CONFIG_IPV6 */
3838 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3842 * Returns 0 if the packet should be delivered, an error code otherwise
3844 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3846 struct netlbl_lsm_secattr secattr
;
3847 struct socket_smack
*ssp
= sk
->sk_security
;
3848 struct smack_known
*skp
= NULL
;
3850 struct smk_audit_info ad
;
3851 u16 family
= sk
->sk_family
;
3853 struct lsm_network_audit net
;
3855 #if IS_ENABLED(CONFIG_IPV6)
3856 struct sockaddr_in6 sadd
;
3859 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3861 #endif /* CONFIG_IPV6 */
3865 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3867 * If there is a secmark use it rather than the CIPSO label.
3868 * If there is no secmark fall back to CIPSO.
3869 * The secmark is assumed to reflect policy better.
3871 if (skb
&& skb
->secmark
!= 0) {
3872 skp
= smack_from_secid(skb
->secmark
);
3875 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3877 * Translate what netlabel gave us.
3879 netlbl_secattr_init(&secattr
);
3881 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3883 skp
= smack_from_secattr(&secattr
, ssp
);
3885 skp
= smack_net_ambient
;
3887 netlbl_secattr_destroy(&secattr
);
3889 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3893 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3894 ad
.a
.u
.net
->family
= family
;
3895 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3896 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3899 * Receiving a packet requires that the other end
3900 * be able to write here. Read access is not required.
3901 * This is the simplist possible security model
3904 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3905 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3908 netlbl_skbuff_err(skb
, family
, rc
, 0);
3910 #if IS_ENABLED(CONFIG_IPV6)
3912 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3913 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_UDPLITE
&&
3914 proto
!= IPPROTO_TCP
&& proto
!= IPPROTO_DCCP
)
3916 #ifdef SMACK_IPV6_SECMARK_LABELING
3917 if (skb
&& skb
->secmark
!= 0)
3918 skp
= smack_from_secid(skb
->secmark
);
3919 else if (smk_ipv6_localhost(&sadd
))
3922 skp
= smack_ipv6host_label(&sadd
);
3924 skp
= smack_net_ambient
;
3928 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3929 ad
.a
.u
.net
->family
= family
;
3930 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3931 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3932 #endif /* CONFIG_AUDIT */
3933 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3934 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3936 #endif /* SMACK_IPV6_SECMARK_LABELING */
3937 #ifdef SMACK_IPV6_PORT_LABELING
3938 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3939 #endif /* SMACK_IPV6_PORT_LABELING */
3941 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
,
3942 ICMPV6_ADM_PROHIBITED
, 0);
3944 #endif /* CONFIG_IPV6 */
3951 * smack_socket_getpeersec_stream - pull in packet label
3953 * @optval: user's destination
3954 * @optlen: size thereof
3957 * returns zero on success, an error code otherwise
3959 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3960 char __user
*optval
,
3961 int __user
*optlen
, unsigned len
)
3963 struct socket_smack
*ssp
;
3968 ssp
= sock
->sk
->sk_security
;
3969 if (ssp
->smk_packet
!= NULL
) {
3970 rcp
= ssp
->smk_packet
->smk_known
;
3971 slen
= strlen(rcp
) + 1;
3976 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3979 if (put_user(slen
, optlen
) != 0)
3987 * smack_socket_getpeersec_dgram - pull in packet label
3988 * @sock: the peer socket
3990 * @secid: pointer to where to put the secid of the packet
3992 * Sets the netlabel socket state on sk from parent
3994 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3995 struct sk_buff
*skb
, u32
*secid
)
3998 struct netlbl_lsm_secattr secattr
;
3999 struct socket_smack
*ssp
= NULL
;
4000 struct smack_known
*skp
;
4001 int family
= PF_UNSPEC
;
4002 u32 s
= 0; /* 0 is the invalid secid */
4006 if (skb
->protocol
== htons(ETH_P_IP
))
4008 #if IS_ENABLED(CONFIG_IPV6)
4009 else if (skb
->protocol
== htons(ETH_P_IPV6
))
4011 #endif /* CONFIG_IPV6 */
4013 if (family
== PF_UNSPEC
&& sock
!= NULL
)
4014 family
= sock
->sk
->sk_family
;
4018 ssp
= sock
->sk
->sk_security
;
4019 s
= ssp
->smk_out
->smk_secid
;
4022 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4028 * Translate what netlabel gave us.
4030 if (sock
!= NULL
&& sock
->sk
!= NULL
)
4031 ssp
= sock
->sk
->sk_security
;
4032 netlbl_secattr_init(&secattr
);
4033 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4035 skp
= smack_from_secattr(&secattr
, ssp
);
4038 netlbl_secattr_destroy(&secattr
);
4041 #ifdef SMACK_IPV6_SECMARK_LABELING
4053 * smack_sock_graft - Initialize a newly created socket with an existing sock
4055 * @parent: parent socket
4057 * Set the smk_{in,out} state of an existing sock based on the process that
4058 * is creating the new socket.
4060 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
4062 struct socket_smack
*ssp
;
4063 struct smack_known
*skp
= smk_of_current();
4066 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
4069 ssp
= sk
->sk_security
;
4072 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4076 * smack_inet_conn_request - Smack access check on connect
4077 * @sk: socket involved
4081 * Returns 0 if a task with the packet label could write to
4082 * the socket, otherwise an error code
4084 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4085 struct request_sock
*req
)
4087 u16 family
= sk
->sk_family
;
4088 struct smack_known
*skp
;
4089 struct socket_smack
*ssp
= sk
->sk_security
;
4090 struct netlbl_lsm_secattr secattr
;
4091 struct sockaddr_in addr
;
4093 struct smack_known
*hskp
;
4095 struct smk_audit_info ad
;
4097 struct lsm_network_audit net
;
4100 #if IS_ENABLED(CONFIG_IPV6)
4101 if (family
== PF_INET6
) {
4103 * Handle mapped IPv4 packets arriving
4104 * via IPv6 sockets. Don't set up netlabel
4105 * processing on IPv6.
4107 if (skb
->protocol
== htons(ETH_P_IP
))
4112 #endif /* CONFIG_IPV6 */
4114 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4116 * If there is a secmark use it rather than the CIPSO label.
4117 * If there is no secmark fall back to CIPSO.
4118 * The secmark is assumed to reflect policy better.
4120 if (skb
&& skb
->secmark
!= 0) {
4121 skp
= smack_from_secid(skb
->secmark
);
4124 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4126 netlbl_secattr_init(&secattr
);
4127 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4129 skp
= smack_from_secattr(&secattr
, ssp
);
4131 skp
= &smack_known_huh
;
4132 netlbl_secattr_destroy(&secattr
);
4134 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4139 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4140 ad
.a
.u
.net
->family
= family
;
4141 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4142 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4145 * Receiving a packet requires that the other end be able to write
4146 * here. Read access is not required.
4148 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4149 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4154 * Save the peer's label in the request_sock so we can later setup
4155 * smk_packet in the child socket so that SO_PEERCRED can report it.
4157 req
->peer_secid
= skp
->smk_secid
;
4160 * We need to decide if we want to label the incoming connection here
4161 * if we do we only need to label the request_sock and the stack will
4162 * propagate the wire-label to the sock when it is created.
4165 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4167 hskp
= smack_ipv4host_label(&addr
);
4171 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4173 netlbl_req_delattr(req
);
4179 * smack_inet_csk_clone - Copy the connection information to the new socket
4180 * @sk: the new socket
4181 * @req: the connection's request_sock
4183 * Transfer the connection's peer label to the newly created socket.
4185 static void smack_inet_csk_clone(struct sock
*sk
,
4186 const struct request_sock
*req
)
4188 struct socket_smack
*ssp
= sk
->sk_security
;
4189 struct smack_known
*skp
;
4191 if (req
->peer_secid
!= 0) {
4192 skp
= smack_from_secid(req
->peer_secid
);
4193 ssp
->smk_packet
= skp
;
4195 ssp
->smk_packet
= NULL
;
4199 * Key management security hooks
4201 * Casey has not tested key support very heavily.
4202 * The permission check is most likely too restrictive.
4203 * If you care about keys please have a look.
4208 * smack_key_alloc - Set the key security blob
4210 * @cred: the credentials to use
4213 * No allocation required
4217 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4218 unsigned long flags
)
4220 struct smack_known
*skp
= smk_of_task(smack_cred(cred
));
4222 key
->security
= skp
;
4227 * smack_key_free - Clear the key security blob
4230 * Clear the blob pointer
4232 static void smack_key_free(struct key
*key
)
4234 key
->security
= NULL
;
4238 * smack_key_permission - Smack access on a key
4239 * @key_ref: gets to the object
4240 * @cred: the credentials to use
4241 * @perm: requested key permissions
4243 * Return 0 if the task has read and write to the object,
4244 * an error code otherwise
4246 static int smack_key_permission(key_ref_t key_ref
,
4247 const struct cred
*cred
, unsigned perm
)
4250 struct smk_audit_info ad
;
4251 struct smack_known
*tkp
= smk_of_task(smack_cred(cred
));
4256 * Validate requested permissions
4258 if (perm
& ~KEY_NEED_ALL
)
4261 keyp
= key_ref_to_ptr(key_ref
);
4265 * If the key hasn't been initialized give it access so that
4268 if (keyp
->security
== NULL
)
4271 * This should not occur
4276 if (smack_privileged_cred(CAP_MAC_OVERRIDE
, cred
))
4280 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4281 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4282 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4284 if (perm
& (KEY_NEED_READ
| KEY_NEED_SEARCH
| KEY_NEED_VIEW
))
4285 request
|= MAY_READ
;
4286 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4287 request
|= MAY_WRITE
;
4288 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
4289 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
4294 * smack_key_getsecurity - Smack label tagging the key
4295 * @key points to the key to be queried
4296 * @_buffer points to a pointer that should be set to point to the
4297 * resulting string (if no label or an error occurs).
4298 * Return the length of the string (including terminating NUL) or -ve if
4300 * May also return 0 (and a NULL buffer pointer) if there is no label.
4302 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4304 struct smack_known
*skp
= key
->security
;
4308 if (key
->security
== NULL
) {
4313 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4316 length
= strlen(copy
) + 1;
4322 #endif /* CONFIG_KEYS */
4327 * Audit requires a unique representation of each Smack specific
4328 * rule. This unique representation is used to distinguish the
4329 * object to be audited from remaining kernel objects and also
4330 * works as a glue between the audit hooks.
4332 * Since repository entries are added but never deleted, we'll use
4333 * the smack_known label address related to the given audit rule as
4334 * the needed unique representation. This also better fits the smack
4335 * model where nearly everything is a label.
4340 * smack_audit_rule_init - Initialize a smack audit rule
4341 * @field: audit rule fields given from user-space (audit.h)
4342 * @op: required testing operator (=, !=, >, <, ...)
4343 * @rulestr: smack label to be audited
4344 * @vrule: pointer to save our own audit rule representation
4346 * Prepare to audit cases where (@field @op @rulestr) is true.
4347 * The label to be audited is created if necessay.
4349 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4351 struct smack_known
*skp
;
4352 char **rule
= (char **)vrule
;
4355 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4358 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4361 skp
= smk_import_entry(rulestr
, 0);
4363 return PTR_ERR(skp
);
4365 *rule
= skp
->smk_known
;
4371 * smack_audit_rule_known - Distinguish Smack audit rules
4372 * @krule: rule of interest, in Audit kernel representation format
4374 * This is used to filter Smack rules from remaining Audit ones.
4375 * If it's proved that this rule belongs to us, the
4376 * audit_rule_match hook will be called to do the final judgement.
4378 static int smack_audit_rule_known(struct audit_krule
*krule
)
4380 struct audit_field
*f
;
4383 for (i
= 0; i
< krule
->field_count
; i
++) {
4384 f
= &krule
->fields
[i
];
4386 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4394 * smack_audit_rule_match - Audit given object ?
4395 * @secid: security id for identifying the object to test
4396 * @field: audit rule flags given from user-space
4397 * @op: required testing operator
4398 * @vrule: smack internal rule presentation
4400 * The core Audit hook. It's used to take the decision of
4401 * whether to audit or not to audit a given object.
4403 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
)
4405 struct smack_known
*skp
;
4408 if (unlikely(!rule
)) {
4409 WARN_ONCE(1, "Smack: missing rule\n");
4413 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4416 skp
= smack_from_secid(secid
);
4419 * No need to do string comparisons. If a match occurs,
4420 * both pointers will point to the same smack_known
4423 if (op
== Audit_equal
)
4424 return (rule
== skp
->smk_known
);
4425 if (op
== Audit_not_equal
)
4426 return (rule
!= skp
->smk_known
);
4432 * There is no need for a smack_audit_rule_free hook.
4433 * No memory was allocated.
4436 #endif /* CONFIG_AUDIT */
4439 * smack_ismaclabel - check if xattr @name references a smack MAC label
4440 * @name: Full xattr name to check.
4442 static int smack_ismaclabel(const char *name
)
4444 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4449 * smack_secid_to_secctx - return the smack label for a secid
4450 * @secid: incoming integer
4451 * @secdata: destination
4452 * @seclen: how long it is
4454 * Exists for networking code.
4456 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4458 struct smack_known
*skp
= smack_from_secid(secid
);
4461 *secdata
= skp
->smk_known
;
4462 *seclen
= strlen(skp
->smk_known
);
4467 * smack_secctx_to_secid - return the secid for a smack label
4468 * @secdata: smack label
4469 * @seclen: how long result is
4470 * @secid: outgoing integer
4472 * Exists for audit and networking code.
4474 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4476 struct smack_known
*skp
= smk_find_entry(secdata
);
4479 *secid
= skp
->smk_secid
;
4486 * There used to be a smack_release_secctx hook
4487 * that did nothing back when hooks were in a vector.
4488 * Now that there's a list such a hook adds cost.
4491 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4493 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4496 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4498 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4501 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4503 struct smack_known
*skp
= smk_of_inode(inode
);
4505 *ctx
= skp
->smk_known
;
4506 *ctxlen
= strlen(skp
->smk_known
);
4510 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4513 struct task_smack
*tsp
;
4514 struct smack_known
*skp
;
4515 struct inode_smack
*isp
;
4516 struct cred
*new_creds
= *new;
4518 if (new_creds
== NULL
) {
4519 new_creds
= prepare_creds();
4520 if (new_creds
== NULL
)
4524 tsp
= smack_cred(new_creds
);
4527 * Get label from overlay inode and set it in create_sid
4529 isp
= smack_inode(d_inode(dentry
->d_parent
));
4530 skp
= isp
->smk_inode
;
4531 tsp
->smk_task
= skp
;
4536 static int smack_inode_copy_up_xattr(const char *name
)
4539 * Return 1 if this is the smack access Smack attribute.
4541 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4547 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4549 const struct cred
*old
,
4552 struct task_smack
*otsp
= smack_cred(old
);
4553 struct task_smack
*ntsp
= smack_cred(new);
4554 struct inode_smack
*isp
;
4558 * Use the process credential unless all of
4559 * the transmuting criteria are met
4561 ntsp
->smk_task
= otsp
->smk_task
;
4564 * the attribute of the containing directory
4566 isp
= smack_inode(d_inode(dentry
->d_parent
));
4568 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4570 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4571 isp
->smk_inode
->smk_known
,
4572 &otsp
->smk_task
->smk_rules
);
4576 * If the directory is transmuting and the rule
4577 * providing access is transmuting use the containing
4578 * directory label instead of the process label.
4580 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4581 ntsp
->smk_task
= isp
->smk_inode
;
4586 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init
= {
4587 .lbs_cred
= sizeof(struct task_smack
),
4588 .lbs_file
= sizeof(struct smack_known
*),
4589 .lbs_inode
= sizeof(struct inode_smack
),
4590 .lbs_ipc
= sizeof(struct smack_known
*),
4591 .lbs_msg_msg
= sizeof(struct smack_known
*),
4594 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4595 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4596 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4597 LSM_HOOK_INIT(syslog
, smack_syslog
),
4599 LSM_HOOK_INIT(fs_context_dup
, smack_fs_context_dup
),
4600 LSM_HOOK_INIT(fs_context_parse_param
, smack_fs_context_parse_param
),
4602 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4603 LSM_HOOK_INIT(sb_free_security
, smack_sb_free_security
),
4604 LSM_HOOK_INIT(sb_free_mnt_opts
, smack_free_mnt_opts
),
4605 LSM_HOOK_INIT(sb_eat_lsm_opts
, smack_sb_eat_lsm_opts
),
4606 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4607 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4609 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4611 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4612 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4613 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4614 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4615 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4616 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4617 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4618 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4619 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4620 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4621 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4622 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4623 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4624 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4625 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4626 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4627 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4629 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4630 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4631 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4632 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4633 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4634 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4635 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4636 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4637 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4639 LSM_HOOK_INIT(file_open
, smack_file_open
),
4641 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4642 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4643 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4644 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4645 LSM_HOOK_INIT(cred_getsecid
, smack_cred_getsecid
),
4646 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4647 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4648 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4649 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4650 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4651 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4652 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4653 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4654 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4655 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4656 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4657 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4658 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4659 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4661 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4662 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4664 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4666 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_ipc_alloc_security
),
4667 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4668 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4669 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4670 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4672 LSM_HOOK_INIT(shm_alloc_security
, smack_ipc_alloc_security
),
4673 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4674 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4675 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4677 LSM_HOOK_INIT(sem_alloc_security
, smack_ipc_alloc_security
),
4678 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4679 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4680 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4682 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4684 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4685 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4687 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4688 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4690 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4691 LSM_HOOK_INIT(socket_socketpair
, smack_socket_socketpair
),
4692 #ifdef SMACK_IPV6_PORT_LABELING
4693 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4695 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4696 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4697 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4698 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4699 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4700 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4701 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4702 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4703 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4704 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4706 /* key management security hooks */
4708 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4709 LSM_HOOK_INIT(key_free
, smack_key_free
),
4710 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4711 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4712 #endif /* CONFIG_KEYS */
4716 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4717 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4718 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4719 #endif /* CONFIG_AUDIT */
4721 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4722 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4723 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4724 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4725 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4726 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4727 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4728 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4729 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4733 static __init
void init_smack_known_list(void)
4736 * Initialize rule list locks
4738 mutex_init(&smack_known_huh
.smk_rules_lock
);
4739 mutex_init(&smack_known_hat
.smk_rules_lock
);
4740 mutex_init(&smack_known_floor
.smk_rules_lock
);
4741 mutex_init(&smack_known_star
.smk_rules_lock
);
4742 mutex_init(&smack_known_web
.smk_rules_lock
);
4744 * Initialize rule lists
4746 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4747 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4748 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4749 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4750 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4752 * Create the known labels list
4754 smk_insert_entry(&smack_known_huh
);
4755 smk_insert_entry(&smack_known_hat
);
4756 smk_insert_entry(&smack_known_star
);
4757 smk_insert_entry(&smack_known_floor
);
4758 smk_insert_entry(&smack_known_web
);
4762 * smack_init - initialize the smack system
4764 * Returns 0 on success, -ENOMEM is there's no memory
4766 static __init
int smack_init(void)
4768 struct cred
*cred
= (struct cred
*) current
->cred
;
4769 struct task_smack
*tsp
;
4771 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4772 if (!smack_inode_cache
)
4775 smack_rule_cache
= KMEM_CACHE(smack_rule
, 0);
4776 if (!smack_rule_cache
) {
4777 kmem_cache_destroy(smack_inode_cache
);
4782 * Set the security state for the initial task.
4784 tsp
= smack_cred(cred
);
4785 init_task_smack(tsp
, &smack_known_floor
, &smack_known_floor
);
4790 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4793 pr_info("Smack: Initializing.\n");
4794 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4795 pr_info("Smack: Netfilter enabled.\n");
4797 #ifdef SMACK_IPV6_PORT_LABELING
4798 pr_info("Smack: IPv6 port labeling enabled.\n");
4800 #ifdef SMACK_IPV6_SECMARK_LABELING
4801 pr_info("Smack: IPv6 Netfilter enabled.\n");
4804 /* initialize the smack_known_list */
4805 init_smack_known_list();
4811 * Smack requires early initialization in order to label
4812 * all processes and objects when they are created.
4814 DEFINE_LSM(smack
) = {
4816 .flags
= LSM_FLAG_LEGACY_MAJOR
| LSM_FLAG_EXCLUSIVE
,
4817 .blobs
= &smack_blob_sizes
,