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 <linux/pipe_fs_i.h>
32 #include <net/cipso_ipv4.h>
35 #include <linux/audit.h>
36 #include <linux/magic.h>
37 #include <linux/dcache.h>
38 #include <linux/personality.h>
39 #include <linux/msg.h>
40 #include <linux/shm.h>
41 #include <linux/binfmts.h>
42 #include <linux/parser.h>
43 #include <linux/fs_context.h>
44 #include <linux/fs_parser.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 DEFINE_MUTEX(smack_ipv6_lock
);
56 static LIST_HEAD(smk_ipv6_port_list
);
58 static struct kmem_cache
*smack_inode_cache
;
59 struct kmem_cache
*smack_rule_cache
;
62 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
67 } smk_mount_opts
[] = {
68 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault
},
69 A(fsdefault
), A(fsfloor
), A(fshat
), A(fsroot
), A(fstransmute
)
73 static int match_opt_prefix(char *s
, int l
, char **arg
)
77 for (i
= 0; i
< ARRAY_SIZE(smk_mount_opts
); i
++) {
78 size_t len
= smk_mount_opts
[i
].len
;
79 if (len
> l
|| memcmp(s
, smk_mount_opts
[i
].name
, len
))
81 if (len
== l
|| s
[len
] != '=')
84 return smk_mount_opts
[i
].opt
;
89 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
90 static char *smk_bu_mess
[] = {
91 "Bringup Error", /* Unused */
92 "Bringup", /* SMACK_BRINGUP_ALLOW */
93 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
94 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
97 static void smk_bu_mode(int mode
, char *s
)
103 if (mode
& MAY_WRITE
)
107 if (mode
& MAY_APPEND
)
109 if (mode
& MAY_TRANSMUTE
)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
121 struct smack_known
*oskp
, int mode
, int rc
)
123 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
127 if (rc
> SMACK_UNCONFINED_OBJECT
)
130 smk_bu_mode(mode
, acc
);
131 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
132 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
136 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
139 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
140 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
143 struct task_smack
*tsp
= smack_cred(current_cred());
144 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
148 if (rc
> SMACK_UNCONFINED_OBJECT
)
151 smk_bu_mode(mode
, acc
);
152 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
153 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
154 acc
, current
->comm
, note
);
158 #define smk_bu_current(note, oskp, mode, RC) (RC)
161 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
162 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
164 struct task_smack
*tsp
= smack_cred(current_cred());
165 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
166 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
170 if (rc
> SMACK_UNCONFINED_OBJECT
)
173 smk_bu_mode(mode
, acc
);
174 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
175 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
176 current
->comm
, otp
->comm
);
180 #define smk_bu_task(otp, mode, RC) (RC)
183 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
184 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
186 struct task_smack
*tsp
= smack_cred(current_cred());
187 struct inode_smack
*isp
= smack_inode(inode
);
188 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
190 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
191 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
192 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
196 if (rc
> SMACK_UNCONFINED_OBJECT
)
198 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
199 (mode
& (MAY_WRITE
| MAY_APPEND
)))
200 isp
->smk_flags
|= SMK_INODE_IMPURE
;
202 smk_bu_mode(mode
, acc
);
204 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
205 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
206 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
210 #define smk_bu_inode(inode, mode, RC) (RC)
213 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
214 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
216 struct task_smack
*tsp
= smack_cred(current_cred());
217 struct smack_known
*sskp
= tsp
->smk_task
;
218 struct inode
*inode
= file_inode(file
);
219 struct inode_smack
*isp
= smack_inode(inode
);
220 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
222 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
228 if (rc
> SMACK_UNCONFINED_OBJECT
)
231 smk_bu_mode(mode
, acc
);
232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
233 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
234 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
239 #define smk_bu_file(file, mode, RC) (RC)
242 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
243 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
246 struct task_smack
*tsp
= smack_cred(cred
);
247 struct smack_known
*sskp
= tsp
->smk_task
;
248 struct inode
*inode
= file_inode(file
);
249 struct inode_smack
*isp
= smack_inode(inode
);
250 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
252 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
253 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
254 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
258 if (rc
> SMACK_UNCONFINED_OBJECT
)
261 smk_bu_mode(mode
, acc
);
262 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
263 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
264 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
269 #define smk_bu_credfile(cred, file, mode, RC) (RC)
273 * smk_fetch - Fetch the smack label from a file.
274 * @name: type of the label (attribute)
275 * @ip: a pointer to the inode
276 * @dp: a pointer to the dentry
278 * Returns a pointer to the master list entry for the Smack label,
279 * NULL if there was no label to fetch, or an error code.
281 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
286 struct smack_known
*skp
= NULL
;
288 if (!(ip
->i_opflags
& IOP_XATTR
))
289 return ERR_PTR(-EOPNOTSUPP
);
291 buffer
= kzalloc(SMK_LONGLABEL
, GFP_NOFS
);
293 return ERR_PTR(-ENOMEM
);
295 rc
= __vfs_getxattr(dp
, ip
, name
, buffer
, SMK_LONGLABEL
);
301 skp
= smk_import_entry(buffer
, rc
);
309 * init_inode_smack - initialize an inode security blob
310 * @inode: inode to extract the info from
311 * @skp: a pointer to the Smack label entry to use in the blob
314 static void init_inode_smack(struct inode
*inode
, struct smack_known
*skp
)
316 struct inode_smack
*isp
= smack_inode(inode
);
318 isp
->smk_inode
= skp
;
320 mutex_init(&isp
->smk_lock
);
324 * init_task_smack - initialize a task security blob
325 * @tsp: blob to initialize
326 * @task: a pointer to the Smack label for the running task
327 * @forked: a pointer to the Smack label for the forked task
330 static void init_task_smack(struct task_smack
*tsp
, struct smack_known
*task
,
331 struct smack_known
*forked
)
333 tsp
->smk_task
= task
;
334 tsp
->smk_forked
= forked
;
335 INIT_LIST_HEAD(&tsp
->smk_rules
);
336 INIT_LIST_HEAD(&tsp
->smk_relabel
);
337 mutex_init(&tsp
->smk_rules_lock
);
341 * smk_copy_rules - copy a rule set
342 * @nhead: new rules header pointer
343 * @ohead: old rules header pointer
344 * @gfp: type of the memory for the allocation
346 * Returns 0 on success, -ENOMEM on error
348 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
351 struct smack_rule
*nrp
;
352 struct smack_rule
*orp
;
355 list_for_each_entry_rcu(orp
, ohead
, list
) {
356 nrp
= kmem_cache_zalloc(smack_rule_cache
, gfp
);
362 list_add_rcu(&nrp
->list
, nhead
);
368 * smk_copy_relabel - copy smk_relabel labels list
369 * @nhead: new rules header pointer
370 * @ohead: old rules header pointer
371 * @gfp: type of the memory for the allocation
373 * Returns 0 on success, -ENOMEM on error
375 static int smk_copy_relabel(struct list_head
*nhead
, struct list_head
*ohead
,
378 struct smack_known_list_elem
*nklep
;
379 struct smack_known_list_elem
*oklep
;
381 list_for_each_entry(oklep
, ohead
, list
) {
382 nklep
= kzalloc(sizeof(struct smack_known_list_elem
), gfp
);
384 smk_destroy_label_list(nhead
);
387 nklep
->smk_label
= oklep
->smk_label
;
388 list_add(&nklep
->list
, nhead
);
395 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
396 * @mode - input mode in form of PTRACE_MODE_*
398 * Returns a converted MAY_* mode usable by smack rules
400 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
402 if (mode
& PTRACE_MODE_ATTACH
)
403 return MAY_READWRITE
;
404 if (mode
& PTRACE_MODE_READ
)
411 * smk_ptrace_rule_check - helper for ptrace access
412 * @tracer: tracer process
413 * @tracee_known: label entry of the process that's about to be traced
414 * @mode: ptrace attachment mode (PTRACE_MODE_*)
415 * @func: name of the function that called us, used for audit
417 * Returns 0 on access granted, -error on error
419 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
420 struct smack_known
*tracee_known
,
421 unsigned int mode
, const char *func
)
424 struct smk_audit_info ad
, *saip
= NULL
;
425 struct task_smack
*tsp
;
426 struct smack_known
*tracer_known
;
427 const struct cred
*tracercred
;
429 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
430 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
431 smk_ad_setfield_u_tsk(&ad
, tracer
);
436 tracercred
= __task_cred(tracer
);
437 tsp
= smack_cred(tracercred
);
438 tracer_known
= smk_of_task(tsp
);
440 if ((mode
& PTRACE_MODE_ATTACH
) &&
441 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
442 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
443 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
445 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
447 else if (smack_privileged_cred(CAP_SYS_PTRACE
, tracercred
))
453 smack_log(tracer_known
->smk_known
,
454 tracee_known
->smk_known
,
461 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
462 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
470 * We he, that is fun!
474 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
475 * @ctp: child task pointer
476 * @mode: ptrace attachment mode (PTRACE_MODE_*)
478 * Returns 0 if access is OK, an error code otherwise
480 * Do the capability checks.
482 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
484 struct smack_known
*skp
;
486 skp
= smk_of_task_struct(ctp
);
488 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
492 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
493 * @ptp: parent task pointer
495 * Returns 0 if access is OK, an error code otherwise
497 * Do the capability checks, and require PTRACE_MODE_ATTACH.
499 static int smack_ptrace_traceme(struct task_struct
*ptp
)
502 struct smack_known
*skp
;
504 skp
= smk_of_task(smack_cred(current_cred()));
506 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
511 * smack_syslog - Smack approval on syslog
512 * @typefrom_file: unused
514 * Returns 0 on success, error code otherwise.
516 static int smack_syslog(int typefrom_file
)
519 struct smack_known
*skp
= smk_of_current();
521 if (smack_privileged(CAP_MAC_OVERRIDE
))
524 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
538 * Returns 0 on success or -ENOMEM on error.
540 static int smack_sb_alloc_security(struct super_block
*sb
)
542 struct superblock_smack
*sbsp
;
544 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
549 sbsp
->smk_root
= &smack_known_floor
;
550 sbsp
->smk_default
= &smack_known_floor
;
551 sbsp
->smk_floor
= &smack_known_floor
;
552 sbsp
->smk_hat
= &smack_known_hat
;
554 * SMK_SB_INITIALIZED will be zero from kzalloc.
556 sb
->s_security
= sbsp
;
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
566 static void smack_sb_free_security(struct super_block
*sb
)
568 kfree(sb
->s_security
);
569 sb
->s_security
= NULL
;
572 struct smack_mnt_opts
{
573 const char *fsdefault
, *fsfloor
, *fshat
, *fsroot
, *fstransmute
;
576 static void smack_free_mnt_opts(void *mnt_opts
)
578 struct smack_mnt_opts
*opts
= mnt_opts
;
579 kfree(opts
->fsdefault
);
580 kfree(opts
->fsfloor
);
583 kfree(opts
->fstransmute
);
587 static int smack_add_opt(int token
, const char *s
, void **mnt_opts
)
589 struct smack_mnt_opts
*opts
= *mnt_opts
;
592 opts
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
621 case Opt_fstransmute
:
622 if (opts
->fstransmute
)
624 opts
->fstransmute
= s
;
630 pr_warn("Smack: duplicate mount options\n");
635 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
636 * @fc: The new filesystem context.
637 * @src_fc: The source filesystem context being duplicated.
639 * Returns 0 on success or -ENOMEM on error.
641 static int smack_fs_context_dup(struct fs_context
*fc
,
642 struct fs_context
*src_fc
)
644 struct smack_mnt_opts
*dst
, *src
= src_fc
->security
;
649 fc
->security
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
654 if (src
->fsdefault
) {
655 dst
->fsdefault
= kstrdup(src
->fsdefault
, GFP_KERNEL
);
660 dst
->fsfloor
= kstrdup(src
->fsfloor
, GFP_KERNEL
);
665 dst
->fshat
= kstrdup(src
->fshat
, GFP_KERNEL
);
670 dst
->fsroot
= kstrdup(src
->fsroot
, GFP_KERNEL
);
674 if (src
->fstransmute
) {
675 dst
->fstransmute
= kstrdup(src
->fstransmute
, GFP_KERNEL
);
676 if (!dst
->fstransmute
)
682 static const struct fs_parameter_spec smack_param_specs
[] = {
683 fsparam_string("smackfsdef", Opt_fsdefault
),
684 fsparam_string("smackfsdefault", Opt_fsdefault
),
685 fsparam_string("smackfsfloor", Opt_fsfloor
),
686 fsparam_string("smackfshat", Opt_fshat
),
687 fsparam_string("smackfsroot", Opt_fsroot
),
688 fsparam_string("smackfstransmute", Opt_fstransmute
),
692 static const struct fs_parameter_description smack_fs_parameters
= {
694 .specs
= smack_param_specs
,
698 * smack_fs_context_parse_param - Parse a single mount parameter
699 * @fc: The new filesystem context being constructed.
700 * @param: The parameter.
702 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
705 static int smack_fs_context_parse_param(struct fs_context
*fc
,
706 struct fs_parameter
*param
)
708 struct fs_parse_result result
;
711 opt
= fs_parse(fc
, &smack_fs_parameters
, param
, &result
);
715 rc
= smack_add_opt(opt
, param
->string
, &fc
->security
);
717 param
->string
= NULL
;
721 static int smack_sb_eat_lsm_opts(char *options
, void **mnt_opts
)
723 char *from
= options
, *to
= options
;
727 char *next
= strchr(from
, ',');
736 token
= match_opt_prefix(from
, len
, &arg
);
737 if (token
!= Opt_error
) {
738 arg
= kmemdup_nul(arg
, from
+ len
- arg
, GFP_KERNEL
);
739 rc
= smack_add_opt(token
, arg
, mnt_opts
);
743 smack_free_mnt_opts(*mnt_opts
);
748 if (!first
) { // copy with preceding comma
753 memmove(to
, from
, len
);
766 * smack_set_mnt_opts - set Smack specific mount options
767 * @sb: the file system superblock
768 * @mnt_opts: Smack mount options
769 * @kern_flags: mount option from kernel space or user space
770 * @set_kern_flags: where to store converted mount opts
772 * Returns 0 on success, an error code on failure
774 * Allow filesystems with binary mount data to explicitly set Smack mount
777 static int smack_set_mnt_opts(struct super_block
*sb
,
779 unsigned long kern_flags
,
780 unsigned long *set_kern_flags
)
782 struct dentry
*root
= sb
->s_root
;
783 struct inode
*inode
= d_backing_inode(root
);
784 struct superblock_smack
*sp
= sb
->s_security
;
785 struct inode_smack
*isp
;
786 struct smack_known
*skp
;
787 struct smack_mnt_opts
*opts
= mnt_opts
;
788 bool transmute
= false;
790 if (sp
->smk_flags
& SMK_SB_INITIALIZED
)
793 if (inode
->i_security
== NULL
) {
794 int rc
= lsm_inode_alloc(inode
);
800 if (!smack_privileged(CAP_MAC_ADMIN
)) {
802 * Unprivileged mounts don't get to specify Smack values.
807 * Unprivileged mounts get root and default from the caller.
809 skp
= smk_of_current();
811 sp
->smk_default
= skp
;
813 * For a handful of fs types with no user-controlled
814 * backing store it's okay to trust security labels
815 * in the filesystem. The rest are untrusted.
817 if (sb
->s_user_ns
!= &init_user_ns
&&
818 sb
->s_magic
!= SYSFS_MAGIC
&& sb
->s_magic
!= TMPFS_MAGIC
&&
819 sb
->s_magic
!= RAMFS_MAGIC
) {
821 sp
->smk_flags
|= SMK_SB_UNTRUSTED
;
825 sp
->smk_flags
|= SMK_SB_INITIALIZED
;
828 if (opts
->fsdefault
) {
829 skp
= smk_import_entry(opts
->fsdefault
, 0);
832 sp
->smk_default
= skp
;
835 skp
= smk_import_entry(opts
->fsfloor
, 0);
841 skp
= smk_import_entry(opts
->fshat
, 0);
847 skp
= smk_import_entry(opts
->fsroot
, 0);
852 if (opts
->fstransmute
) {
853 skp
= smk_import_entry(opts
->fstransmute
, 0);
862 * Initialize the root inode.
864 init_inode_smack(inode
, sp
->smk_root
);
867 isp
= smack_inode(inode
);
868 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
875 * smack_sb_statfs - Smack check on statfs
876 * @dentry: identifies the file system in question
878 * Returns 0 if current can read the floor of the filesystem,
879 * and error code otherwise
881 static int smack_sb_statfs(struct dentry
*dentry
)
883 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
885 struct smk_audit_info ad
;
887 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
888 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
890 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
891 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
900 * smack_bprm_set_creds - set creds for exec
901 * @bprm: the exec information
903 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
905 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
907 struct inode
*inode
= file_inode(bprm
->file
);
908 struct task_smack
*bsp
= smack_cred(bprm
->cred
);
909 struct inode_smack
*isp
;
910 struct superblock_smack
*sbsp
;
913 if (bprm
->called_set_creds
)
916 isp
= smack_inode(inode
);
917 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
920 sbsp
= inode
->i_sb
->s_security
;
921 if ((sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) &&
922 isp
->smk_task
!= sbsp
->smk_root
)
925 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
926 struct task_struct
*tracer
;
930 tracer
= ptrace_parent(current
);
931 if (likely(tracer
!= NULL
))
932 rc
= smk_ptrace_rule_check(tracer
,
941 if (bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
)
944 bsp
->smk_task
= isp
->smk_task
;
945 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
947 /* Decide if this is a secure exec. */
948 if (bsp
->smk_task
!= bsp
->smk_forked
)
949 bprm
->secureexec
= 1;
959 * smack_inode_alloc_security - allocate an inode blob
960 * @inode: the inode in need of a blob
964 static int smack_inode_alloc_security(struct inode
*inode
)
966 struct smack_known
*skp
= smk_of_current();
968 init_inode_smack(inode
, skp
);
973 * smack_inode_init_security - copy out the smack from an inode
974 * @inode: the newly created inode
975 * @dir: containing directory object
977 * @name: where to put the attribute name
978 * @value: where to put the attribute value
979 * @len: where to put the length of the attribute
981 * Returns 0 if it all works out, -ENOMEM if there's no memory
983 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
984 const struct qstr
*qstr
, const char **name
,
985 void **value
, size_t *len
)
987 struct inode_smack
*issp
= smack_inode(inode
);
988 struct smack_known
*skp
= smk_of_current();
989 struct smack_known
*isp
= smk_of_inode(inode
);
990 struct smack_known
*dsp
= smk_of_inode(dir
);
994 *name
= XATTR_SMACK_SUFFIX
;
998 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
1003 * If the access rule allows transmutation and
1004 * the directory requests transmutation then
1005 * by all means transmute.
1006 * Mark the inode as changed.
1008 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
1009 smk_inode_transmutable(dir
)) {
1011 issp
->smk_flags
|= SMK_INODE_CHANGED
;
1014 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
1018 *len
= strlen(isp
->smk_known
);
1025 * smack_inode_link - Smack check on link
1026 * @old_dentry: the existing object
1028 * @new_dentry: the new object
1030 * Returns 0 if access is permitted, an error code otherwise
1032 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1033 struct dentry
*new_dentry
)
1035 struct smack_known
*isp
;
1036 struct smk_audit_info ad
;
1039 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1040 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1042 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1043 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1044 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1046 if (rc
== 0 && d_is_positive(new_dentry
)) {
1047 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1048 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1049 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1050 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1057 * smack_inode_unlink - Smack check on inode deletion
1058 * @dir: containing directory object
1059 * @dentry: file to unlink
1061 * Returns 0 if current can write the containing directory
1062 * and the object, error code otherwise
1064 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1066 struct inode
*ip
= d_backing_inode(dentry
);
1067 struct smk_audit_info ad
;
1070 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1071 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1074 * You need write access to the thing you're unlinking
1076 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1077 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1080 * You also need write access to the containing directory
1082 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1083 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1084 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1085 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1091 * smack_inode_rmdir - Smack check on directory deletion
1092 * @dir: containing directory object
1093 * @dentry: directory to unlink
1095 * Returns 0 if current can write the containing directory
1096 * and the directory, error code otherwise
1098 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1100 struct smk_audit_info ad
;
1103 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1104 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1107 * You need write access to the thing you're removing
1109 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1110 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1113 * You also need write access to the containing directory
1115 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1116 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1117 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1118 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1125 * smack_inode_rename - Smack check on rename
1126 * @old_inode: unused
1127 * @old_dentry: the old object
1128 * @new_inode: unused
1129 * @new_dentry: the new object
1131 * Read and write access is required on both the old and
1134 * Returns 0 if access is permitted, an error code otherwise
1136 static int smack_inode_rename(struct inode
*old_inode
,
1137 struct dentry
*old_dentry
,
1138 struct inode
*new_inode
,
1139 struct dentry
*new_dentry
)
1142 struct smack_known
*isp
;
1143 struct smk_audit_info ad
;
1145 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1146 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1148 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1149 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1150 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1152 if (rc
== 0 && d_is_positive(new_dentry
)) {
1153 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1154 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1155 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1156 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1162 * smack_inode_permission - Smack version of permission()
1163 * @inode: the inode in question
1164 * @mask: the access requested
1166 * This is the important Smack hook.
1168 * Returns 0 if access is permitted, an error code otherwise
1170 static int smack_inode_permission(struct inode
*inode
, int mask
)
1172 struct superblock_smack
*sbsp
= inode
->i_sb
->s_security
;
1173 struct smk_audit_info ad
;
1174 int no_block
= mask
& MAY_NOT_BLOCK
;
1177 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1179 * No permission to check. Existence test. Yup, it's there.
1184 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1185 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1189 /* May be droppable after audit */
1192 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1193 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1194 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1195 rc
= smk_bu_inode(inode
, mask
, rc
);
1200 * smack_inode_setattr - Smack check for setting attributes
1201 * @dentry: the object
1202 * @iattr: for the force flag
1204 * Returns 0 if access is permitted, an error code otherwise
1206 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1208 struct smk_audit_info ad
;
1212 * Need to allow for clearing the setuid bit.
1214 if (iattr
->ia_valid
& ATTR_FORCE
)
1216 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1217 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1219 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1220 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1225 * smack_inode_getattr - Smack check for getting attributes
1226 * @path: path to extract the info from
1228 * Returns 0 if access is permitted, an error code otherwise
1230 static int smack_inode_getattr(const struct path
*path
)
1232 struct smk_audit_info ad
;
1233 struct inode
*inode
= d_backing_inode(path
->dentry
);
1236 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1237 smk_ad_setfield_u_fs_path(&ad
, *path
);
1238 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1239 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1244 * smack_inode_setxattr - Smack check for setting xattrs
1245 * @dentry: the object
1246 * @name: name of the attribute
1247 * @value: value of the attribute
1248 * @size: size of the value
1251 * This protects the Smack attribute explicitly.
1253 * Returns 0 if access is permitted, an error code otherwise
1255 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1256 const void *value
, size_t size
, int flags
)
1258 struct smk_audit_info ad
;
1259 struct smack_known
*skp
;
1261 int check_import
= 0;
1266 * Check label validity here so import won't fail in post_setxattr
1268 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1269 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1270 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1273 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1274 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1278 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1280 if (size
!= TRANS_TRUE_SIZE
||
1281 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1284 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1286 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1289 if (rc
== 0 && check_import
) {
1290 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1293 else if (skp
== NULL
|| (check_star
&&
1294 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1298 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1299 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1302 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1303 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1310 * smack_inode_post_setxattr - Apply the Smack update approved above
1312 * @name: attribute name
1313 * @value: attribute value
1314 * @size: attribute size
1317 * Set the pointer in the inode blob to the entry found
1318 * in the master label list.
1320 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1321 const void *value
, size_t size
, int flags
)
1323 struct smack_known
*skp
;
1324 struct inode_smack
*isp
= smack_inode(d_backing_inode(dentry
));
1326 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1327 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1331 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1332 skp
= smk_import_entry(value
, size
);
1334 isp
->smk_inode
= skp
;
1335 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1336 skp
= smk_import_entry(value
, size
);
1338 isp
->smk_task
= skp
;
1339 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1340 skp
= smk_import_entry(value
, size
);
1342 isp
->smk_mmap
= skp
;
1349 * smack_inode_getxattr - Smack check on getxattr
1350 * @dentry: the object
1353 * Returns 0 if access is permitted, an error code otherwise
1355 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1357 struct smk_audit_info ad
;
1360 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1361 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1363 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1364 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1369 * smack_inode_removexattr - Smack check on removexattr
1370 * @dentry: the object
1371 * @name: name of the attribute
1373 * Removing the Smack attribute requires CAP_MAC_ADMIN
1375 * Returns 0 if access is permitted, an error code otherwise
1377 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1379 struct inode_smack
*isp
;
1380 struct smk_audit_info ad
;
1383 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1384 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1385 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1386 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1387 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1388 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1389 if (!smack_privileged(CAP_MAC_ADMIN
))
1392 rc
= cap_inode_removexattr(dentry
, name
);
1397 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1398 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1400 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1401 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1405 isp
= smack_inode(d_backing_inode(dentry
));
1407 * Don't do anything special for these.
1408 * XATTR_NAME_SMACKIPIN
1409 * XATTR_NAME_SMACKIPOUT
1411 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1412 struct super_block
*sbp
= dentry
->d_sb
;
1413 struct superblock_smack
*sbsp
= sbp
->s_security
;
1415 isp
->smk_inode
= sbsp
->smk_default
;
1416 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1417 isp
->smk_task
= NULL
;
1418 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1419 isp
->smk_mmap
= NULL
;
1420 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1421 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1427 * smack_inode_getsecurity - get smack xattrs
1428 * @inode: the object
1429 * @name: attribute name
1430 * @buffer: where to put the result
1431 * @alloc: duplicate memory
1433 * Returns the size of the attribute or an error code
1435 static int smack_inode_getsecurity(struct inode
*inode
,
1436 const char *name
, void **buffer
,
1439 struct socket_smack
*ssp
;
1440 struct socket
*sock
;
1441 struct super_block
*sbp
;
1442 struct inode
*ip
= (struct inode
*)inode
;
1443 struct smack_known
*isp
;
1445 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1446 isp
= smk_of_inode(inode
);
1449 * The rest of the Smack xattrs are only on sockets.
1452 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1455 sock
= SOCKET_I(ip
);
1456 if (sock
== NULL
|| sock
->sk
== NULL
)
1459 ssp
= sock
->sk
->sk_security
;
1461 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1463 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1470 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1471 if (*buffer
== NULL
)
1475 return strlen(isp
->smk_known
);
1480 * smack_inode_listsecurity - list the Smack attributes
1481 * @inode: the object
1482 * @buffer: where they go
1483 * @buffer_size: size of buffer
1485 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1488 int len
= sizeof(XATTR_NAME_SMACK
);
1490 if (buffer
!= NULL
&& len
<= buffer_size
)
1491 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1497 * smack_inode_getsecid - Extract inode's security id
1498 * @inode: inode to extract the info from
1499 * @secid: where result will be saved
1501 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1503 struct smack_known
*skp
= smk_of_inode(inode
);
1505 *secid
= skp
->smk_secid
;
1513 * There is no smack_file_permission hook
1515 * Should access checks be done on each read or write?
1516 * UNICOS and SELinux say yes.
1517 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1519 * I'll say no for now. Smack does not do the frequent
1520 * label changing that SELinux does.
1524 * smack_file_alloc_security - assign a file security blob
1527 * The security blob for a file is a pointer to the master
1528 * label list, so no allocation is done.
1530 * f_security is the owner security information. It
1531 * isn't used on file access checks, it's for send_sigio.
1535 static int smack_file_alloc_security(struct file
*file
)
1537 struct smack_known
**blob
= smack_file(file
);
1539 *blob
= smk_of_current();
1544 * smack_file_ioctl - Smack check on ioctls
1549 * Relies heavily on the correct use of the ioctl command conventions.
1551 * Returns 0 if allowed, error code otherwise
1553 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1557 struct smk_audit_info ad
;
1558 struct inode
*inode
= file_inode(file
);
1560 if (unlikely(IS_PRIVATE(inode
)))
1563 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1564 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1566 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1567 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1568 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1571 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1572 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1573 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1580 * smack_file_lock - Smack check on file locking
1584 * Returns 0 if current has lock access, error code otherwise
1586 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1588 struct smk_audit_info ad
;
1590 struct inode
*inode
= file_inode(file
);
1592 if (unlikely(IS_PRIVATE(inode
)))
1595 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1596 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1597 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1598 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1603 * smack_file_fcntl - Smack check on fcntl
1605 * @cmd: what action to check
1608 * Generally these operations are harmless.
1609 * File locking operations present an obvious mechanism
1610 * for passing information, so they require write access.
1612 * Returns 0 if current has access, error code otherwise
1614 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1617 struct smk_audit_info ad
;
1619 struct inode
*inode
= file_inode(file
);
1621 if (unlikely(IS_PRIVATE(inode
)))
1629 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1630 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1631 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1632 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1636 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1637 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1638 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1639 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1650 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1651 * if mapping anonymous memory.
1652 * @file contains the file structure for file to map (may be NULL).
1653 * @reqprot contains the protection requested by the application.
1654 * @prot contains the protection that will be applied by the kernel.
1655 * @flags contains the operational flags.
1656 * Return 0 if permission is granted.
1658 static int smack_mmap_file(struct file
*file
,
1659 unsigned long reqprot
, unsigned long prot
,
1660 unsigned long flags
)
1662 struct smack_known
*skp
;
1663 struct smack_known
*mkp
;
1664 struct smack_rule
*srp
;
1665 struct task_smack
*tsp
;
1666 struct smack_known
*okp
;
1667 struct inode_smack
*isp
;
1668 struct superblock_smack
*sbsp
;
1677 if (unlikely(IS_PRIVATE(file_inode(file
))))
1680 isp
= smack_inode(file_inode(file
));
1681 if (isp
->smk_mmap
== NULL
)
1683 sbsp
= file_inode(file
)->i_sb
->s_security
;
1684 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1685 isp
->smk_mmap
!= sbsp
->smk_root
)
1687 mkp
= isp
->smk_mmap
;
1689 tsp
= smack_cred(current_cred());
1690 skp
= smk_of_current();
1695 * For each Smack rule associated with the subject
1696 * label verify that the SMACK64MMAP also has access
1697 * to that rule's object label.
1699 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1700 okp
= srp
->smk_object
;
1702 * Matching labels always allows access.
1704 if (mkp
->smk_known
== okp
->smk_known
)
1707 * If there is a matching local rule take
1708 * that into account as well.
1710 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1714 may
= srp
->smk_access
;
1716 may
&= srp
->smk_access
;
1718 * If may is zero the SMACK64MMAP subject can't
1719 * possibly have less access.
1725 * Fetch the global list entry.
1726 * If there isn't one a SMACK64MMAP subject
1727 * can't have as much access as current.
1729 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1731 if (mmay
== -ENOENT
) {
1736 * If there is a local entry it modifies the
1737 * potential access, too.
1739 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1741 if (tmay
!= -ENOENT
)
1745 * If there is any access available to current that is
1746 * not available to a SMACK64MMAP subject
1749 if ((may
| mmay
) != mmay
) {
1761 * smack_file_set_fowner - set the file security blob value
1762 * @file: object in question
1765 static void smack_file_set_fowner(struct file
*file
)
1767 struct smack_known
**blob
= smack_file(file
);
1769 *blob
= smk_of_current();
1773 * smack_file_send_sigiotask - Smack on sigio
1774 * @tsk: The target task
1775 * @fown: the object the signal come from
1778 * Allow a privileged task to get signals even if it shouldn't
1780 * Returns 0 if a subject with the object's smack could
1781 * write to the task, an error code otherwise.
1783 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1784 struct fown_struct
*fown
, int signum
)
1786 struct smack_known
**blob
;
1787 struct smack_known
*skp
;
1788 struct smack_known
*tkp
= smk_of_task(smack_cred(tsk
->cred
));
1789 const struct cred
*tcred
;
1792 struct smk_audit_info ad
;
1795 * struct fown_struct is never outside the context of a struct file
1797 file
= container_of(fown
, struct file
, f_owner
);
1799 /* we don't log here as rc can be overriden */
1800 blob
= smack_file(file
);
1802 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1803 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1806 tcred
= __task_cred(tsk
);
1807 if (rc
!= 0 && smack_privileged_cred(CAP_MAC_OVERRIDE
, tcred
))
1811 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1812 smk_ad_setfield_u_tsk(&ad
, tsk
);
1813 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1818 * smack_file_receive - Smack file receive check
1821 * Returns 0 if current has access, error code otherwise
1823 static int smack_file_receive(struct file
*file
)
1827 struct smk_audit_info ad
;
1828 struct inode
*inode
= file_inode(file
);
1829 struct socket
*sock
;
1830 struct task_smack
*tsp
;
1831 struct socket_smack
*ssp
;
1833 if (unlikely(IS_PRIVATE(inode
)))
1836 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1837 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1839 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1840 sock
= SOCKET_I(inode
);
1841 ssp
= sock
->sk
->sk_security
;
1842 tsp
= smack_cred(current_cred());
1844 * If the receiving process can't write to the
1845 * passed socket or if the passed socket can't
1846 * write to the receiving process don't accept
1847 * the passed socket.
1849 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1850 rc
= smk_bu_file(file
, may
, rc
);
1853 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1854 rc
= smk_bu_file(file
, may
, rc
);
1858 * This code relies on bitmasks.
1860 if (file
->f_mode
& FMODE_READ
)
1862 if (file
->f_mode
& FMODE_WRITE
)
1865 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1866 rc
= smk_bu_file(file
, may
, rc
);
1871 * smack_file_open - Smack dentry open processing
1874 * Set the security blob in the file structure.
1875 * Allow the open only if the task has read access. There are
1876 * many read operations (e.g. fstat) that you can do with an
1877 * fd even if you have the file open write-only.
1879 * Returns 0 if current has access, error code otherwise
1881 static int smack_file_open(struct file
*file
)
1883 struct task_smack
*tsp
= smack_cred(file
->f_cred
);
1884 struct inode
*inode
= file_inode(file
);
1885 struct smk_audit_info ad
;
1888 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1889 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1890 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1891 rc
= smk_bu_credfile(file
->f_cred
, file
, MAY_READ
, rc
);
1901 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1902 * @cred: the new credentials
1903 * @gfp: the atomicity of any memory allocations
1905 * Prepare a blank set of credentials for modification. This must allocate all
1906 * the memory the LSM module might require such that cred_transfer() can
1907 * complete without error.
1909 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1911 init_task_smack(smack_cred(cred
), NULL
, NULL
);
1917 * smack_cred_free - "free" task-level security credentials
1918 * @cred: the credentials in question
1921 static void smack_cred_free(struct cred
*cred
)
1923 struct task_smack
*tsp
= smack_cred(cred
);
1924 struct smack_rule
*rp
;
1925 struct list_head
*l
;
1926 struct list_head
*n
;
1928 smk_destroy_label_list(&tsp
->smk_relabel
);
1930 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1931 rp
= list_entry(l
, struct smack_rule
, list
);
1932 list_del(&rp
->list
);
1933 kmem_cache_free(smack_rule_cache
, rp
);
1938 * smack_cred_prepare - prepare new set of credentials for modification
1939 * @new: the new credentials
1940 * @old: the original credentials
1941 * @gfp: the atomicity of any memory allocations
1943 * Prepare a new set of credentials for modification.
1945 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1948 struct task_smack
*old_tsp
= smack_cred(old
);
1949 struct task_smack
*new_tsp
= smack_cred(new);
1952 init_task_smack(new_tsp
, old_tsp
->smk_task
, old_tsp
->smk_task
);
1954 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1958 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
1964 * smack_cred_transfer - Transfer the old credentials to the new credentials
1965 * @new: the new credentials
1966 * @old: the original credentials
1968 * Fill in a set of blank credentials from another set of credentials.
1970 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1972 struct task_smack
*old_tsp
= smack_cred(old
);
1973 struct task_smack
*new_tsp
= smack_cred(new);
1975 new_tsp
->smk_task
= old_tsp
->smk_task
;
1976 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1977 mutex_init(&new_tsp
->smk_rules_lock
);
1978 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1980 /* cbs copy rule list */
1984 * smack_cred_getsecid - get the secid corresponding to a creds structure
1985 * @cred: the object creds
1986 * @secid: where to put the result
1988 * Sets the secid to contain a u32 version of the smack label.
1990 static void smack_cred_getsecid(const struct cred
*cred
, u32
*secid
)
1992 struct smack_known
*skp
;
1995 skp
= smk_of_task(smack_cred(cred
));
1996 *secid
= skp
->smk_secid
;
2001 * smack_kernel_act_as - Set the subjective context in a set of credentials
2002 * @new: points to the set of credentials to be modified.
2003 * @secid: specifies the security ID to be set
2005 * Set the security data for a kernel service.
2007 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
2009 struct task_smack
*new_tsp
= smack_cred(new);
2011 new_tsp
->smk_task
= smack_from_secid(secid
);
2016 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2017 * @new: points to the set of credentials to be modified
2018 * @inode: points to the inode to use as a reference
2020 * Set the file creation context in a set of credentials to the same
2021 * as the objective context of the specified inode
2023 static int smack_kernel_create_files_as(struct cred
*new,
2024 struct inode
*inode
)
2026 struct inode_smack
*isp
= smack_inode(inode
);
2027 struct task_smack
*tsp
= smack_cred(new);
2029 tsp
->smk_forked
= isp
->smk_inode
;
2030 tsp
->smk_task
= tsp
->smk_forked
;
2035 * smk_curacc_on_task - helper to log task related access
2036 * @p: the task object
2037 * @access: the access requested
2038 * @caller: name of the calling function for audit
2040 * Return 0 if access is permitted
2042 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
2045 struct smk_audit_info ad
;
2046 struct smack_known
*skp
= smk_of_task_struct(p
);
2049 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2050 smk_ad_setfield_u_tsk(&ad
, p
);
2051 rc
= smk_curacc(skp
, access
, &ad
);
2052 rc
= smk_bu_task(p
, access
, rc
);
2057 * smack_task_setpgid - Smack check on setting pgid
2058 * @p: the task object
2061 * Return 0 if write access is permitted
2063 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2065 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2069 * smack_task_getpgid - Smack access check for getpgid
2070 * @p: the object task
2072 * Returns 0 if current can read the object task, error code otherwise
2074 static int smack_task_getpgid(struct task_struct
*p
)
2076 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2080 * smack_task_getsid - Smack access check for getsid
2081 * @p: the object task
2083 * Returns 0 if current can read the object task, error code otherwise
2085 static int smack_task_getsid(struct task_struct
*p
)
2087 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2091 * smack_task_getsecid - get the secid of the task
2092 * @p: the object task
2093 * @secid: where to put the result
2095 * Sets the secid to contain a u32 version of the smack label.
2097 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2099 struct smack_known
*skp
= smk_of_task_struct(p
);
2101 *secid
= skp
->smk_secid
;
2105 * smack_task_setnice - Smack check on setting nice
2106 * @p: the task object
2109 * Return 0 if write access is permitted
2111 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2113 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2117 * smack_task_setioprio - Smack check on setting ioprio
2118 * @p: the task object
2121 * Return 0 if write access is permitted
2123 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2125 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2129 * smack_task_getioprio - Smack check on reading ioprio
2130 * @p: the task object
2132 * Return 0 if read access is permitted
2134 static int smack_task_getioprio(struct task_struct
*p
)
2136 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2140 * smack_task_setscheduler - Smack check on setting scheduler
2141 * @p: the task object
2143 * Return 0 if read access is permitted
2145 static int smack_task_setscheduler(struct task_struct
*p
)
2147 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2151 * smack_task_getscheduler - Smack check on reading scheduler
2152 * @p: the task object
2154 * Return 0 if read access is permitted
2156 static int smack_task_getscheduler(struct task_struct
*p
)
2158 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2162 * smack_task_movememory - Smack check on moving memory
2163 * @p: the task object
2165 * Return 0 if write access is permitted
2167 static int smack_task_movememory(struct task_struct
*p
)
2169 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2173 * smack_task_kill - Smack check on signal delivery
2174 * @p: the task object
2177 * @cred: identifies the cred to use in lieu of current's
2179 * Return 0 if write access is permitted
2182 static int smack_task_kill(struct task_struct
*p
, struct kernel_siginfo
*info
,
2183 int sig
, const struct cred
*cred
)
2185 struct smk_audit_info ad
;
2186 struct smack_known
*skp
;
2187 struct smack_known
*tkp
= smk_of_task_struct(p
);
2191 return 0; /* null signal; existence test */
2193 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2194 smk_ad_setfield_u_tsk(&ad
, p
);
2196 * Sending a signal requires that the sender
2197 * can write the receiver.
2200 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2201 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2205 * If the cred isn't NULL we're dealing with some USB IO
2206 * specific behavior. This is not clean. For one thing
2207 * we can't take privilege into account.
2209 skp
= smk_of_task(smack_cred(cred
));
2210 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2211 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2216 * smack_task_to_inode - copy task smack into the inode blob
2217 * @p: task to copy from
2218 * @inode: inode to copy to
2220 * Sets the smack pointer in the inode security blob
2222 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2224 struct inode_smack
*isp
= smack_inode(inode
);
2225 struct smack_known
*skp
= smk_of_task_struct(p
);
2227 isp
->smk_inode
= skp
;
2228 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2236 * smack_sk_alloc_security - Allocate a socket blob
2239 * @gfp_flags: memory allocation flags
2241 * Assign Smack pointers to current
2243 * Returns 0 on success, -ENOMEM is there's no memory
2245 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2247 struct smack_known
*skp
= smk_of_current();
2248 struct socket_smack
*ssp
;
2250 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2255 * Sockets created by kernel threads receive web label.
2257 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2258 ssp
->smk_in
= &smack_known_web
;
2259 ssp
->smk_out
= &smack_known_web
;
2264 ssp
->smk_packet
= NULL
;
2266 sk
->sk_security
= ssp
;
2272 * smack_sk_free_security - Free a socket blob
2275 * Clears the blob pointer
2277 static void smack_sk_free_security(struct sock
*sk
)
2279 #ifdef SMACK_IPV6_PORT_LABELING
2280 struct smk_port_label
*spp
;
2282 if (sk
->sk_family
== PF_INET6
) {
2284 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2285 if (spp
->smk_sock
!= sk
)
2287 spp
->smk_can_reuse
= 1;
2293 kfree(sk
->sk_security
);
2297 * smack_ipv4host_label - check host based restrictions
2298 * @sip: the object end
2300 * looks for host based access restrictions
2302 * This version will only be appropriate for really small sets of single label
2303 * hosts. The caller is responsible for ensuring that the RCU read lock is
2304 * taken before calling this function.
2306 * Returns the label of the far end or NULL if it's not special.
2308 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2310 struct smk_net4addr
*snp
;
2311 struct in_addr
*siap
= &sip
->sin_addr
;
2313 if (siap
->s_addr
== 0)
2316 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2318 * we break after finding the first match because
2319 * the list is sorted from longest to shortest mask
2320 * so we have found the most specific match
2322 if (snp
->smk_host
.s_addr
==
2323 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2324 return snp
->smk_label
;
2329 #if IS_ENABLED(CONFIG_IPV6)
2331 * smk_ipv6_localhost - Check for local ipv6 host address
2334 * Returns boolean true if this is the localhost address
2336 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2338 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2339 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2341 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2342 ntohs(be16p
[7]) == 1)
2348 * smack_ipv6host_label - check host based restrictions
2349 * @sip: the object end
2351 * looks for host based access restrictions
2353 * This version will only be appropriate for really small sets of single label
2354 * hosts. The caller is responsible for ensuring that the RCU read lock is
2355 * taken before calling this function.
2357 * Returns the label of the far end or NULL if it's not special.
2359 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2361 struct smk_net6addr
*snp
;
2362 struct in6_addr
*sap
= &sip
->sin6_addr
;
2367 * It's local. Don't look for a host label.
2369 if (smk_ipv6_localhost(sip
))
2372 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2374 * If the label is NULL the entry has
2375 * been renounced. Ignore it.
2377 if (snp
->smk_label
== NULL
)
2380 * we break after finding the first match because
2381 * the list is sorted from longest to shortest mask
2382 * so we have found the most specific match
2384 for (found
= 1, i
= 0; i
< 8; i
++) {
2385 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2386 snp
->smk_host
.s6_addr16
[i
]) {
2392 return snp
->smk_label
;
2397 #endif /* CONFIG_IPV6 */
2400 * smack_netlabel - Set the secattr on a socket
2402 * @labeled: socket label scheme
2404 * Convert the outbound smack value (smk_out) to a
2405 * secattr and attach it to the socket.
2407 * Returns 0 on success or an error code
2409 static int smack_netlabel(struct sock
*sk
, int labeled
)
2411 struct smack_known
*skp
;
2412 struct socket_smack
*ssp
= sk
->sk_security
;
2416 * Usually the netlabel code will handle changing the
2417 * packet labeling based on the label.
2418 * The case of a single label host is different, because
2419 * a single label host should never get a labeled packet
2420 * even though the label is usually associated with a packet
2424 bh_lock_sock_nested(sk
);
2426 if (ssp
->smk_out
== smack_net_ambient
||
2427 labeled
== SMACK_UNLABELED_SOCKET
)
2428 netlbl_sock_delattr(sk
);
2431 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2441 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2443 * @sap: the destination address
2445 * Set the correct secattr for the given socket based on the destination
2446 * address and perform any outbound access checks needed.
2448 * Returns 0 on success or an error code.
2451 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2453 struct smack_known
*skp
;
2456 struct smack_known
*hkp
;
2457 struct socket_smack
*ssp
= sk
->sk_security
;
2458 struct smk_audit_info ad
;
2461 hkp
= smack_ipv4host_label(sap
);
2464 struct lsm_network_audit net
;
2466 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2467 ad
.a
.u
.net
->family
= sap
->sin_family
;
2468 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2469 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2471 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2473 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2474 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2476 sk_lbl
= SMACK_CIPSO_SOCKET
;
2483 return smack_netlabel(sk
, sk_lbl
);
2486 #if IS_ENABLED(CONFIG_IPV6)
2488 * smk_ipv6_check - check Smack access
2489 * @subject: subject Smack label
2490 * @object: object Smack label
2492 * @act: the action being taken
2494 * Check an IPv6 access
2496 static int smk_ipv6_check(struct smack_known
*subject
,
2497 struct smack_known
*object
,
2498 struct sockaddr_in6
*address
, int act
)
2501 struct lsm_network_audit net
;
2503 struct smk_audit_info ad
;
2507 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2508 ad
.a
.u
.net
->family
= PF_INET6
;
2509 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2510 if (act
== SMK_RECEIVING
)
2511 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2513 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2515 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2516 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2519 #endif /* CONFIG_IPV6 */
2521 #ifdef SMACK_IPV6_PORT_LABELING
2523 * smk_ipv6_port_label - Smack port access table management
2527 * Create or update the port list entry
2529 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2531 struct sock
*sk
= sock
->sk
;
2532 struct sockaddr_in6
*addr6
;
2533 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2534 struct smk_port_label
*spp
;
2535 unsigned short port
= 0;
2537 if (address
== NULL
) {
2539 * This operation is changing the Smack information
2540 * on the bound socket. Take the changes to the port
2544 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2545 if (sk
!= spp
->smk_sock
)
2547 spp
->smk_in
= ssp
->smk_in
;
2548 spp
->smk_out
= ssp
->smk_out
;
2553 * A NULL address is only used for updating existing
2554 * bound entries. If there isn't one, it's OK.
2560 addr6
= (struct sockaddr_in6
*)address
;
2561 port
= ntohs(addr6
->sin6_port
);
2563 * This is a special case that is safely ignored.
2569 * Look for an existing port list entry.
2570 * This is an indication that a port is getting reused.
2573 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2574 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2576 if (spp
->smk_can_reuse
!= 1) {
2580 spp
->smk_port
= port
;
2582 spp
->smk_in
= ssp
->smk_in
;
2583 spp
->smk_out
= ssp
->smk_out
;
2584 spp
->smk_can_reuse
= 0;
2590 * A new port entry is required.
2592 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2596 spp
->smk_port
= port
;
2598 spp
->smk_in
= ssp
->smk_in
;
2599 spp
->smk_out
= ssp
->smk_out
;
2600 spp
->smk_sock_type
= sock
->type
;
2601 spp
->smk_can_reuse
= 0;
2603 mutex_lock(&smack_ipv6_lock
);
2604 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2605 mutex_unlock(&smack_ipv6_lock
);
2610 * smk_ipv6_port_check - check Smack port access
2613 * @act: the action being taken
2615 * Create or update the port list entry
2617 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2620 struct smk_port_label
*spp
;
2621 struct socket_smack
*ssp
= sk
->sk_security
;
2622 struct smack_known
*skp
= NULL
;
2623 unsigned short port
;
2624 struct smack_known
*object
;
2626 if (act
== SMK_RECEIVING
) {
2627 skp
= smack_ipv6host_label(address
);
2628 object
= ssp
->smk_in
;
2631 object
= smack_ipv6host_label(address
);
2635 * The other end is a single label host.
2637 if (skp
!= NULL
&& object
!= NULL
)
2638 return smk_ipv6_check(skp
, object
, address
, act
);
2640 skp
= smack_net_ambient
;
2642 object
= smack_net_ambient
;
2645 * It's remote, so port lookup does no good.
2647 if (!smk_ipv6_localhost(address
))
2648 return smk_ipv6_check(skp
, object
, address
, act
);
2651 * It's local so the send check has to have passed.
2653 if (act
== SMK_RECEIVING
)
2656 port
= ntohs(address
->sin6_port
);
2658 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2659 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2661 object
= spp
->smk_in
;
2662 if (act
== SMK_CONNECTING
)
2663 ssp
->smk_packet
= spp
->smk_out
;
2668 return smk_ipv6_check(skp
, object
, address
, act
);
2670 #endif /* SMACK_IPV6_PORT_LABELING */
2673 * smack_inode_setsecurity - set smack xattrs
2674 * @inode: the object
2675 * @name: attribute name
2676 * @value: attribute value
2677 * @size: size of the attribute
2680 * Sets the named attribute in the appropriate blob
2682 * Returns 0 on success, or an error code
2684 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2685 const void *value
, size_t size
, int flags
)
2687 struct smack_known
*skp
;
2688 struct inode_smack
*nsp
= smack_inode(inode
);
2689 struct socket_smack
*ssp
;
2690 struct socket
*sock
;
2693 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2696 skp
= smk_import_entry(value
, size
);
2698 return PTR_ERR(skp
);
2700 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2701 nsp
->smk_inode
= skp
;
2702 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2706 * The rest of the Smack xattrs are only on sockets.
2708 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2711 sock
= SOCKET_I(inode
);
2712 if (sock
== NULL
|| sock
->sk
== NULL
)
2715 ssp
= sock
->sk
->sk_security
;
2717 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2719 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2721 if (sock
->sk
->sk_family
== PF_INET
) {
2722 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2725 "Smack: \"%s\" netlbl error %d.\n",
2731 #ifdef SMACK_IPV6_PORT_LABELING
2732 if (sock
->sk
->sk_family
== PF_INET6
)
2733 smk_ipv6_port_label(sock
, NULL
);
2740 * smack_socket_post_create - finish socket setup
2742 * @family: protocol family
2747 * Sets the netlabel information on the socket
2749 * Returns 0 on success, and error code otherwise
2751 static int smack_socket_post_create(struct socket
*sock
, int family
,
2752 int type
, int protocol
, int kern
)
2754 struct socket_smack
*ssp
;
2756 if (sock
->sk
== NULL
)
2760 * Sockets created by kernel threads receive web label.
2762 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2763 ssp
= sock
->sk
->sk_security
;
2764 ssp
->smk_in
= &smack_known_web
;
2765 ssp
->smk_out
= &smack_known_web
;
2768 if (family
!= PF_INET
)
2771 * Set the outbound netlbl.
2773 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2777 * smack_socket_socketpair - create socket pair
2778 * @socka: one socket
2779 * @sockb: another socket
2781 * Cross reference the peer labels for SO_PEERSEC
2785 static int smack_socket_socketpair(struct socket
*socka
,
2786 struct socket
*sockb
)
2788 struct socket_smack
*asp
= socka
->sk
->sk_security
;
2789 struct socket_smack
*bsp
= sockb
->sk
->sk_security
;
2791 asp
->smk_packet
= bsp
->smk_out
;
2792 bsp
->smk_packet
= asp
->smk_out
;
2797 #ifdef SMACK_IPV6_PORT_LABELING
2799 * smack_socket_bind - record port binding information.
2801 * @address: the port address
2802 * @addrlen: size of the address
2804 * Records the label bound to a port.
2806 * Returns 0 on success, and error code otherwise
2808 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2811 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
) {
2812 if (addrlen
< SIN6_LEN_RFC2133
||
2813 address
->sa_family
!= AF_INET6
)
2815 smk_ipv6_port_label(sock
, address
);
2819 #endif /* SMACK_IPV6_PORT_LABELING */
2822 * smack_socket_connect - connect access check
2824 * @sap: the other end
2825 * @addrlen: size of sap
2827 * Verifies that a connection may be possible
2829 * Returns 0 on success, and error code otherwise
2831 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2835 #if IS_ENABLED(CONFIG_IPV6)
2836 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2838 #ifdef SMACK_IPV6_SECMARK_LABELING
2839 struct smack_known
*rsp
;
2840 struct socket_smack
*ssp
;
2843 if (sock
->sk
== NULL
)
2846 #ifdef SMACK_IPV6_SECMARK_LABELING
2847 ssp
= sock
->sk
->sk_security
;
2850 switch (sock
->sk
->sk_family
) {
2852 if (addrlen
< sizeof(struct sockaddr_in
) ||
2853 sap
->sa_family
!= AF_INET
)
2855 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2858 if (addrlen
< SIN6_LEN_RFC2133
|| sap
->sa_family
!= AF_INET6
)
2860 #ifdef SMACK_IPV6_SECMARK_LABELING
2861 rsp
= smack_ipv6host_label(sip
);
2863 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2866 #ifdef SMACK_IPV6_PORT_LABELING
2867 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2875 * smack_flags_to_may - convert S_ to MAY_ values
2876 * @flags: the S_ value
2878 * Returns the equivalent MAY_ value
2880 static int smack_flags_to_may(int flags
)
2884 if (flags
& S_IRUGO
)
2886 if (flags
& S_IWUGO
)
2888 if (flags
& S_IXUGO
)
2895 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2900 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2902 struct smack_known
**blob
= smack_msg_msg(msg
);
2904 *blob
= smk_of_current();
2909 * smack_of_ipc - the smack pointer for the ipc
2912 * Returns a pointer to the smack value
2914 static struct smack_known
*smack_of_ipc(struct kern_ipc_perm
*isp
)
2916 struct smack_known
**blob
= smack_ipc(isp
);
2922 * smack_ipc_alloc_security - Set the security blob for ipc
2927 static int smack_ipc_alloc_security(struct kern_ipc_perm
*isp
)
2929 struct smack_known
**blob
= smack_ipc(isp
);
2931 *blob
= smk_of_current();
2936 * smk_curacc_shm : check if current has access on shm
2938 * @access : access requested
2940 * Returns 0 if current has the requested access, error code otherwise
2942 static int smk_curacc_shm(struct kern_ipc_perm
*isp
, int access
)
2944 struct smack_known
*ssp
= smack_of_ipc(isp
);
2945 struct smk_audit_info ad
;
2949 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2950 ad
.a
.u
.ipc_id
= isp
->id
;
2952 rc
= smk_curacc(ssp
, access
, &ad
);
2953 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2958 * smack_shm_associate - Smack access check for shm
2960 * @shmflg: access requested
2962 * Returns 0 if current has the requested access, error code otherwise
2964 static int smack_shm_associate(struct kern_ipc_perm
*isp
, int shmflg
)
2968 may
= smack_flags_to_may(shmflg
);
2969 return smk_curacc_shm(isp
, may
);
2973 * smack_shm_shmctl - Smack access check for shm
2975 * @cmd: what it wants to do
2977 * Returns 0 if current has the requested access, error code otherwise
2979 static int smack_shm_shmctl(struct kern_ipc_perm
*isp
, int cmd
)
2993 may
= MAY_READWRITE
;
2998 * System level information.
3004 return smk_curacc_shm(isp
, may
);
3008 * smack_shm_shmat - Smack access for shmat
3011 * @shmflg: access requested
3013 * Returns 0 if current has the requested access, error code otherwise
3015 static int smack_shm_shmat(struct kern_ipc_perm
*isp
, char __user
*shmaddr
,
3020 may
= smack_flags_to_may(shmflg
);
3021 return smk_curacc_shm(isp
, may
);
3025 * smk_curacc_sem : check if current has access on sem
3027 * @access : access requested
3029 * Returns 0 if current has the requested access, error code otherwise
3031 static int smk_curacc_sem(struct kern_ipc_perm
*isp
, int access
)
3033 struct smack_known
*ssp
= smack_of_ipc(isp
);
3034 struct smk_audit_info ad
;
3038 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3039 ad
.a
.u
.ipc_id
= isp
->id
;
3041 rc
= smk_curacc(ssp
, access
, &ad
);
3042 rc
= smk_bu_current("sem", ssp
, access
, rc
);
3047 * smack_sem_associate - Smack access check for sem
3049 * @semflg: access requested
3051 * Returns 0 if current has the requested access, error code otherwise
3053 static int smack_sem_associate(struct kern_ipc_perm
*isp
, int semflg
)
3057 may
= smack_flags_to_may(semflg
);
3058 return smk_curacc_sem(isp
, may
);
3062 * smack_sem_shmctl - Smack access check for sem
3064 * @cmd: what it wants to do
3066 * Returns 0 if current has the requested access, error code otherwise
3068 static int smack_sem_semctl(struct kern_ipc_perm
*isp
, int cmd
)
3087 may
= MAY_READWRITE
;
3092 * System level information
3099 return smk_curacc_sem(isp
, may
);
3103 * smack_sem_semop - Smack checks of semaphore operations
3109 * Treated as read and write in all cases.
3111 * Returns 0 if access is allowed, error code otherwise
3113 static int smack_sem_semop(struct kern_ipc_perm
*isp
, struct sembuf
*sops
,
3114 unsigned nsops
, int alter
)
3116 return smk_curacc_sem(isp
, MAY_READWRITE
);
3120 * smk_curacc_msq : helper to check if current has access on msq
3122 * @access : access requested
3124 * return 0 if current has access, error otherwise
3126 static int smk_curacc_msq(struct kern_ipc_perm
*isp
, int access
)
3128 struct smack_known
*msp
= smack_of_ipc(isp
);
3129 struct smk_audit_info ad
;
3133 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3134 ad
.a
.u
.ipc_id
= isp
->id
;
3136 rc
= smk_curacc(msp
, access
, &ad
);
3137 rc
= smk_bu_current("msq", msp
, access
, rc
);
3142 * smack_msg_queue_associate - Smack access check for msg_queue
3144 * @msqflg: access requested
3146 * Returns 0 if current has the requested access, error code otherwise
3148 static int smack_msg_queue_associate(struct kern_ipc_perm
*isp
, int msqflg
)
3152 may
= smack_flags_to_may(msqflg
);
3153 return smk_curacc_msq(isp
, may
);
3157 * smack_msg_queue_msgctl - Smack access check for msg_queue
3159 * @cmd: what it wants to do
3161 * Returns 0 if current has the requested access, error code otherwise
3163 static int smack_msg_queue_msgctl(struct kern_ipc_perm
*isp
, int cmd
)
3175 may
= MAY_READWRITE
;
3180 * System level information
3187 return smk_curacc_msq(isp
, may
);
3191 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3194 * @msqflg: access requested
3196 * Returns 0 if current has the requested access, error code otherwise
3198 static int smack_msg_queue_msgsnd(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3203 may
= smack_flags_to_may(msqflg
);
3204 return smk_curacc_msq(isp
, may
);
3208 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3215 * Returns 0 if current has read and write access, error code otherwise
3217 static int smack_msg_queue_msgrcv(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3218 struct task_struct
*target
, long type
, int mode
)
3220 return smk_curacc_msq(isp
, MAY_READWRITE
);
3224 * smack_ipc_permission - Smack access for ipc_permission()
3225 * @ipp: the object permissions
3226 * @flag: access requested
3228 * Returns 0 if current has read and write access, error code otherwise
3230 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3232 struct smack_known
**blob
= smack_ipc(ipp
);
3233 struct smack_known
*iskp
= *blob
;
3234 int may
= smack_flags_to_may(flag
);
3235 struct smk_audit_info ad
;
3239 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3240 ad
.a
.u
.ipc_id
= ipp
->id
;
3242 rc
= smk_curacc(iskp
, may
, &ad
);
3243 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3248 * smack_ipc_getsecid - Extract smack security id
3249 * @ipp: the object permissions
3250 * @secid: where result will be saved
3252 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3254 struct smack_known
**blob
= smack_ipc(ipp
);
3255 struct smack_known
*iskp
= *blob
;
3257 *secid
= iskp
->smk_secid
;
3261 * smack_d_instantiate - Make sure the blob is correct on an inode
3262 * @opt_dentry: dentry where inode will be attached
3263 * @inode: the object
3265 * Set the inode's security blob if it hasn't been done already.
3267 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3269 struct super_block
*sbp
;
3270 struct superblock_smack
*sbsp
;
3271 struct inode_smack
*isp
;
3272 struct smack_known
*skp
;
3273 struct smack_known
*ckp
= smk_of_current();
3274 struct smack_known
*final
;
3275 char trattr
[TRANS_TRUE_SIZE
];
3283 isp
= smack_inode(inode
);
3285 mutex_lock(&isp
->smk_lock
);
3287 * If the inode is already instantiated
3288 * take the quick way out
3290 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3294 sbsp
= sbp
->s_security
;
3296 * We're going to use the superblock default label
3297 * if there's no label on the file.
3299 final
= sbsp
->smk_default
;
3302 * If this is the root inode the superblock
3303 * may be in the process of initialization.
3304 * If that is the case use the root value out
3305 * of the superblock.
3307 if (opt_dentry
->d_parent
== opt_dentry
) {
3308 switch (sbp
->s_magic
) {
3309 case CGROUP_SUPER_MAGIC
:
3310 case CGROUP2_SUPER_MAGIC
:
3312 * The cgroup filesystem is never mounted,
3313 * so there's no opportunity to set the mount
3316 sbsp
->smk_root
= &smack_known_star
;
3317 sbsp
->smk_default
= &smack_known_star
;
3318 isp
->smk_inode
= sbsp
->smk_root
;
3322 * What about shmem/tmpfs anonymous files with dentry
3323 * obtained from d_alloc_pseudo()?
3325 isp
->smk_inode
= smk_of_current();
3328 isp
->smk_inode
= smk_of_current();
3332 * Socket access is controlled by the socket
3333 * structures associated with the task involved.
3335 isp
->smk_inode
= &smack_known_star
;
3338 isp
->smk_inode
= sbsp
->smk_root
;
3341 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3346 * This is pretty hackish.
3347 * Casey says that we shouldn't have to do
3348 * file system specific code, but it does help
3349 * with keeping it simple.
3351 switch (sbp
->s_magic
) {
3353 case CGROUP_SUPER_MAGIC
:
3354 case CGROUP2_SUPER_MAGIC
:
3356 * Casey says that it's a little embarrassing
3357 * that the smack file system doesn't do
3358 * extended attributes.
3360 * Cgroupfs is special
3362 final
= &smack_known_star
;
3364 case DEVPTS_SUPER_MAGIC
:
3366 * devpts seems content with the label of the task.
3367 * Programs that change smack have to treat the
3372 case PROC_SUPER_MAGIC
:
3374 * Casey says procfs appears not to care.
3375 * The superblock default suffices.
3380 * Device labels should come from the filesystem,
3381 * but watch out, because they're volitile,
3382 * getting recreated on every reboot.
3384 final
= &smack_known_star
;
3386 * If a smack value has been set we want to use it,
3387 * but since tmpfs isn't giving us the opportunity
3388 * to set mount options simulate setting the
3389 * superblock default.
3394 * This isn't an understood special case.
3395 * Get the value from the xattr.
3399 * UNIX domain sockets use lower level socket data.
3401 if (S_ISSOCK(inode
->i_mode
)) {
3402 final
= &smack_known_star
;
3406 * No xattr support means, alas, no SMACK label.
3407 * Use the aforeapplied default.
3408 * It would be curious if the label of the task
3409 * does not match that assigned.
3411 if (!(inode
->i_opflags
& IOP_XATTR
))
3414 * Get the dentry for xattr.
3416 dp
= dget(opt_dentry
);
3417 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3418 if (!IS_ERR_OR_NULL(skp
))
3422 * Transmuting directory
3424 if (S_ISDIR(inode
->i_mode
)) {
3426 * If this is a new directory and the label was
3427 * transmuted when the inode was initialized
3428 * set the transmute attribute on the directory
3429 * and mark the inode.
3431 * If there is a transmute attribute on the
3432 * directory mark the inode.
3434 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3435 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3436 rc
= __vfs_setxattr(dp
, inode
,
3437 XATTR_NAME_SMACKTRANSMUTE
,
3438 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3441 rc
= __vfs_getxattr(dp
, inode
,
3442 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3444 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3445 TRANS_TRUE_SIZE
) != 0)
3449 transflag
= SMK_INODE_TRANSMUTE
;
3452 * Don't let the exec or mmap label be "*" or "@".
3454 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3455 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3456 skp
== &smack_known_web
)
3458 isp
->smk_task
= skp
;
3460 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3461 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3462 skp
== &smack_known_web
)
3464 isp
->smk_mmap
= skp
;
3471 isp
->smk_inode
= ckp
;
3473 isp
->smk_inode
= final
;
3475 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3478 mutex_unlock(&isp
->smk_lock
);
3483 * smack_getprocattr - Smack process attribute access
3484 * @p: the object task
3485 * @name: the name of the attribute in /proc/.../attr
3486 * @value: where to put the result
3488 * Places a copy of the task Smack into value
3490 * Returns the length of the smack label or an error code
3492 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3494 struct smack_known
*skp
= smk_of_task_struct(p
);
3498 if (strcmp(name
, "current") != 0)
3501 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3511 * smack_setprocattr - Smack process attribute setting
3512 * @name: the name of the attribute in /proc/.../attr
3513 * @value: the value to set
3514 * @size: the size of the value
3516 * Sets the Smack value of the task. Only setting self
3517 * is permitted and only with privilege
3519 * Returns the length of the smack label or an error code
3521 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3523 struct task_smack
*tsp
= smack_cred(current_cred());
3525 struct smack_known
*skp
;
3526 struct smack_known_list_elem
*sklep
;
3529 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3532 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3535 if (strcmp(name
, "current") != 0)
3538 skp
= smk_import_entry(value
, size
);
3540 return PTR_ERR(skp
);
3543 * No process is ever allowed the web ("@") label
3544 * and the star ("*") label.
3546 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3549 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3551 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3552 if (sklep
->smk_label
== skp
) {
3560 new = prepare_creds();
3564 tsp
= smack_cred(new);
3565 tsp
->smk_task
= skp
;
3567 * process can change its label only once
3569 smk_destroy_label_list(&tsp
->smk_relabel
);
3576 * smack_unix_stream_connect - Smack access on UDS
3578 * @other: the other sock
3581 * Return 0 if a subject with the smack of sock could access
3582 * an object with the smack of other, otherwise an error code
3584 static int smack_unix_stream_connect(struct sock
*sock
,
3585 struct sock
*other
, struct sock
*newsk
)
3587 struct smack_known
*skp
;
3588 struct smack_known
*okp
;
3589 struct socket_smack
*ssp
= sock
->sk_security
;
3590 struct socket_smack
*osp
= other
->sk_security
;
3591 struct socket_smack
*nsp
= newsk
->sk_security
;
3592 struct smk_audit_info ad
;
3595 struct lsm_network_audit net
;
3598 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3602 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3603 smk_ad_setfield_u_net_sk(&ad
, other
);
3605 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3606 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3610 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3611 rc
= smk_bu_note("UDS connect", okp
, skp
,
3617 * Cross reference the peer labels for SO_PEERSEC.
3620 nsp
->smk_packet
= ssp
->smk_out
;
3621 ssp
->smk_packet
= osp
->smk_out
;
3628 * smack_unix_may_send - Smack access on UDS
3630 * @other: the other socket
3632 * Return 0 if a subject with the smack of sock could access
3633 * an object with the smack of other, otherwise an error code
3635 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3637 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3638 struct socket_smack
*osp
= other
->sk
->sk_security
;
3639 struct smk_audit_info ad
;
3643 struct lsm_network_audit net
;
3645 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3646 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3649 if (smack_privileged(CAP_MAC_OVERRIDE
))
3652 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3653 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3658 * smack_socket_sendmsg - Smack check based on destination host
3661 * @size: the size of the message
3663 * Return 0 if the current subject can write to the destination host.
3664 * For IPv4 this is only a question if the destination is a single label host.
3665 * For IPv6 this is a check against the label of the port.
3667 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3670 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3671 #if IS_ENABLED(CONFIG_IPV6)
3672 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3674 #ifdef SMACK_IPV6_SECMARK_LABELING
3675 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3676 struct smack_known
*rsp
;
3681 * Perfectly reasonable for this to be NULL
3686 switch (sock
->sk
->sk_family
) {
3688 if (msg
->msg_namelen
< sizeof(struct sockaddr_in
) ||
3689 sip
->sin_family
!= AF_INET
)
3691 rc
= smack_netlabel_send(sock
->sk
, sip
);
3693 #if IS_ENABLED(CONFIG_IPV6)
3695 if (msg
->msg_namelen
< SIN6_LEN_RFC2133
||
3696 sap
->sin6_family
!= AF_INET6
)
3698 #ifdef SMACK_IPV6_SECMARK_LABELING
3699 rsp
= smack_ipv6host_label(sap
);
3701 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3704 #ifdef SMACK_IPV6_PORT_LABELING
3705 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3707 #endif /* IS_ENABLED(CONFIG_IPV6) */
3714 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3715 * @sap: netlabel secattr
3716 * @ssp: socket security information
3718 * Returns a pointer to a Smack label entry found on the label list.
3720 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3721 struct socket_smack
*ssp
)
3723 struct smack_known
*skp
;
3728 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3730 * Looks like a CIPSO packet.
3731 * If there are flags but no level netlabel isn't
3732 * behaving the way we expect it to.
3734 * Look it up in the label table
3735 * Without guidance regarding the smack value
3736 * for the packet fall back on the network
3740 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3741 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3744 * Compare the catsets. Use the netlbl APIs.
3746 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3747 if ((skp
->smk_netlabel
.flags
&
3748 NETLBL_SECATTR_MLS_CAT
) == 0)
3752 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3753 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3755 kcat
= netlbl_catmap_walk(
3756 skp
->smk_netlabel
.attr
.mls
.cat
,
3758 if (acat
< 0 || kcat
< 0)
3771 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3772 return &smack_known_web
;
3773 return &smack_known_star
;
3775 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3777 * Looks like a fallback, which gives us a secid.
3779 return smack_from_secid(sap
->attr
.secid
);
3781 * Without guidance regarding the smack value
3782 * for the packet fall back on the network
3785 return smack_net_ambient
;
3788 #if IS_ENABLED(CONFIG_IPV6)
3789 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3793 int proto
= -EINVAL
;
3794 struct ipv6hdr _ipv6h
;
3795 struct ipv6hdr
*ip6
;
3797 struct tcphdr _tcph
, *th
;
3798 struct udphdr _udph
, *uh
;
3799 struct dccp_hdr _dccph
, *dh
;
3803 offset
= skb_network_offset(skb
);
3804 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3807 sip
->sin6_addr
= ip6
->saddr
;
3809 nexthdr
= ip6
->nexthdr
;
3810 offset
+= sizeof(_ipv6h
);
3811 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3818 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3820 sip
->sin6_port
= th
->source
;
3823 case IPPROTO_UDPLITE
:
3824 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3826 sip
->sin6_port
= uh
->source
;
3829 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3831 sip
->sin6_port
= dh
->dccph_sport
;
3836 #endif /* CONFIG_IPV6 */
3839 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3843 * Returns 0 if the packet should be delivered, an error code otherwise
3845 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3847 struct netlbl_lsm_secattr secattr
;
3848 struct socket_smack
*ssp
= sk
->sk_security
;
3849 struct smack_known
*skp
= NULL
;
3851 struct smk_audit_info ad
;
3852 u16 family
= sk
->sk_family
;
3854 struct lsm_network_audit net
;
3856 #if IS_ENABLED(CONFIG_IPV6)
3857 struct sockaddr_in6 sadd
;
3860 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3862 #endif /* CONFIG_IPV6 */
3866 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3868 * If there is a secmark use it rather than the CIPSO label.
3869 * If there is no secmark fall back to CIPSO.
3870 * The secmark is assumed to reflect policy better.
3872 if (skb
&& skb
->secmark
!= 0) {
3873 skp
= smack_from_secid(skb
->secmark
);
3876 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3878 * Translate what netlabel gave us.
3880 netlbl_secattr_init(&secattr
);
3882 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3884 skp
= smack_from_secattr(&secattr
, ssp
);
3886 skp
= smack_net_ambient
;
3888 netlbl_secattr_destroy(&secattr
);
3890 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3894 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3895 ad
.a
.u
.net
->family
= family
;
3896 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3897 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3900 * Receiving a packet requires that the other end
3901 * be able to write here. Read access is not required.
3902 * This is the simplist possible security model
3905 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3906 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3909 netlbl_skbuff_err(skb
, family
, rc
, 0);
3911 #if IS_ENABLED(CONFIG_IPV6)
3913 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3914 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_UDPLITE
&&
3915 proto
!= IPPROTO_TCP
&& proto
!= IPPROTO_DCCP
)
3917 #ifdef SMACK_IPV6_SECMARK_LABELING
3918 if (skb
&& skb
->secmark
!= 0)
3919 skp
= smack_from_secid(skb
->secmark
);
3920 else if (smk_ipv6_localhost(&sadd
))
3923 skp
= smack_ipv6host_label(&sadd
);
3925 skp
= smack_net_ambient
;
3929 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3930 ad
.a
.u
.net
->family
= family
;
3931 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3932 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3933 #endif /* CONFIG_AUDIT */
3934 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3935 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3937 #endif /* SMACK_IPV6_SECMARK_LABELING */
3938 #ifdef SMACK_IPV6_PORT_LABELING
3939 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3940 #endif /* SMACK_IPV6_PORT_LABELING */
3942 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
,
3943 ICMPV6_ADM_PROHIBITED
, 0);
3945 #endif /* CONFIG_IPV6 */
3952 * smack_socket_getpeersec_stream - pull in packet label
3954 * @optval: user's destination
3955 * @optlen: size thereof
3958 * returns zero on success, an error code otherwise
3960 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3961 char __user
*optval
,
3962 int __user
*optlen
, unsigned len
)
3964 struct socket_smack
*ssp
;
3969 ssp
= sock
->sk
->sk_security
;
3970 if (ssp
->smk_packet
!= NULL
) {
3971 rcp
= ssp
->smk_packet
->smk_known
;
3972 slen
= strlen(rcp
) + 1;
3977 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3980 if (put_user(slen
, optlen
) != 0)
3988 * smack_socket_getpeersec_dgram - pull in packet label
3989 * @sock: the peer socket
3991 * @secid: pointer to where to put the secid of the packet
3993 * Sets the netlabel socket state on sk from parent
3995 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3996 struct sk_buff
*skb
, u32
*secid
)
3999 struct netlbl_lsm_secattr secattr
;
4000 struct socket_smack
*ssp
= NULL
;
4001 struct smack_known
*skp
;
4002 int family
= PF_UNSPEC
;
4003 u32 s
= 0; /* 0 is the invalid secid */
4007 if (skb
->protocol
== htons(ETH_P_IP
))
4009 #if IS_ENABLED(CONFIG_IPV6)
4010 else if (skb
->protocol
== htons(ETH_P_IPV6
))
4012 #endif /* CONFIG_IPV6 */
4014 if (family
== PF_UNSPEC
&& sock
!= NULL
)
4015 family
= sock
->sk
->sk_family
;
4019 ssp
= sock
->sk
->sk_security
;
4020 s
= ssp
->smk_out
->smk_secid
;
4023 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4029 * Translate what netlabel gave us.
4031 if (sock
!= NULL
&& sock
->sk
!= NULL
)
4032 ssp
= sock
->sk
->sk_security
;
4033 netlbl_secattr_init(&secattr
);
4034 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4036 skp
= smack_from_secattr(&secattr
, ssp
);
4039 netlbl_secattr_destroy(&secattr
);
4042 #ifdef SMACK_IPV6_SECMARK_LABELING
4054 * smack_sock_graft - Initialize a newly created socket with an existing sock
4056 * @parent: parent socket
4058 * Set the smk_{in,out} state of an existing sock based on the process that
4059 * is creating the new socket.
4061 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
4063 struct socket_smack
*ssp
;
4064 struct smack_known
*skp
= smk_of_current();
4067 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
4070 ssp
= sk
->sk_security
;
4073 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4077 * smack_inet_conn_request - Smack access check on connect
4078 * @sk: socket involved
4082 * Returns 0 if a task with the packet label could write to
4083 * the socket, otherwise an error code
4085 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4086 struct request_sock
*req
)
4088 u16 family
= sk
->sk_family
;
4089 struct smack_known
*skp
;
4090 struct socket_smack
*ssp
= sk
->sk_security
;
4091 struct netlbl_lsm_secattr secattr
;
4092 struct sockaddr_in addr
;
4094 struct smack_known
*hskp
;
4096 struct smk_audit_info ad
;
4098 struct lsm_network_audit net
;
4101 #if IS_ENABLED(CONFIG_IPV6)
4102 if (family
== PF_INET6
) {
4104 * Handle mapped IPv4 packets arriving
4105 * via IPv6 sockets. Don't set up netlabel
4106 * processing on IPv6.
4108 if (skb
->protocol
== htons(ETH_P_IP
))
4113 #endif /* CONFIG_IPV6 */
4115 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4117 * If there is a secmark use it rather than the CIPSO label.
4118 * If there is no secmark fall back to CIPSO.
4119 * The secmark is assumed to reflect policy better.
4121 if (skb
&& skb
->secmark
!= 0) {
4122 skp
= smack_from_secid(skb
->secmark
);
4125 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4127 netlbl_secattr_init(&secattr
);
4128 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4130 skp
= smack_from_secattr(&secattr
, ssp
);
4132 skp
= &smack_known_huh
;
4133 netlbl_secattr_destroy(&secattr
);
4135 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4140 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4141 ad
.a
.u
.net
->family
= family
;
4142 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4143 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4146 * Receiving a packet requires that the other end be able to write
4147 * here. Read access is not required.
4149 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4150 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4155 * Save the peer's label in the request_sock so we can later setup
4156 * smk_packet in the child socket so that SO_PEERCRED can report it.
4158 req
->peer_secid
= skp
->smk_secid
;
4161 * We need to decide if we want to label the incoming connection here
4162 * if we do we only need to label the request_sock and the stack will
4163 * propagate the wire-label to the sock when it is created.
4166 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4168 hskp
= smack_ipv4host_label(&addr
);
4172 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4174 netlbl_req_delattr(req
);
4180 * smack_inet_csk_clone - Copy the connection information to the new socket
4181 * @sk: the new socket
4182 * @req: the connection's request_sock
4184 * Transfer the connection's peer label to the newly created socket.
4186 static void smack_inet_csk_clone(struct sock
*sk
,
4187 const struct request_sock
*req
)
4189 struct socket_smack
*ssp
= sk
->sk_security
;
4190 struct smack_known
*skp
;
4192 if (req
->peer_secid
!= 0) {
4193 skp
= smack_from_secid(req
->peer_secid
);
4194 ssp
->smk_packet
= skp
;
4196 ssp
->smk_packet
= NULL
;
4200 * Key management security hooks
4202 * Casey has not tested key support very heavily.
4203 * The permission check is most likely too restrictive.
4204 * If you care about keys please have a look.
4209 * smack_key_alloc - Set the key security blob
4211 * @cred: the credentials to use
4214 * No allocation required
4218 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4219 unsigned long flags
)
4221 struct smack_known
*skp
= smk_of_task(smack_cred(cred
));
4223 key
->security
= skp
;
4228 * smack_key_free - Clear the key security blob
4231 * Clear the blob pointer
4233 static void smack_key_free(struct key
*key
)
4235 key
->security
= NULL
;
4239 * smack_key_permission - Smack access on a key
4240 * @key_ref: gets to the object
4241 * @cred: the credentials to use
4242 * @perm: requested key permissions
4244 * Return 0 if the task has read and write to the object,
4245 * an error code otherwise
4247 static int smack_key_permission(key_ref_t key_ref
,
4248 const struct cred
*cred
, unsigned perm
)
4251 struct smk_audit_info ad
;
4252 struct smack_known
*tkp
= smk_of_task(smack_cred(cred
));
4257 * Validate requested permissions
4259 if (perm
& ~KEY_NEED_ALL
)
4262 keyp
= key_ref_to_ptr(key_ref
);
4266 * If the key hasn't been initialized give it access so that
4269 if (keyp
->security
== NULL
)
4272 * This should not occur
4277 if (smack_privileged_cred(CAP_MAC_OVERRIDE
, cred
))
4281 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4282 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4283 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4285 if (perm
& (KEY_NEED_READ
| KEY_NEED_SEARCH
| KEY_NEED_VIEW
))
4286 request
|= MAY_READ
;
4287 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4288 request
|= MAY_WRITE
;
4289 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
4290 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
4295 * smack_key_getsecurity - Smack label tagging the key
4296 * @key points to the key to be queried
4297 * @_buffer points to a pointer that should be set to point to the
4298 * resulting string (if no label or an error occurs).
4299 * Return the length of the string (including terminating NUL) or -ve if
4301 * May also return 0 (and a NULL buffer pointer) if there is no label.
4303 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4305 struct smack_known
*skp
= key
->security
;
4309 if (key
->security
== NULL
) {
4314 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4317 length
= strlen(copy
) + 1;
4323 #endif /* CONFIG_KEYS */
4328 * Audit requires a unique representation of each Smack specific
4329 * rule. This unique representation is used to distinguish the
4330 * object to be audited from remaining kernel objects and also
4331 * works as a glue between the audit hooks.
4333 * Since repository entries are added but never deleted, we'll use
4334 * the smack_known label address related to the given audit rule as
4335 * the needed unique representation. This also better fits the smack
4336 * model where nearly everything is a label.
4341 * smack_audit_rule_init - Initialize a smack audit rule
4342 * @field: audit rule fields given from user-space (audit.h)
4343 * @op: required testing operator (=, !=, >, <, ...)
4344 * @rulestr: smack label to be audited
4345 * @vrule: pointer to save our own audit rule representation
4347 * Prepare to audit cases where (@field @op @rulestr) is true.
4348 * The label to be audited is created if necessay.
4350 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4352 struct smack_known
*skp
;
4353 char **rule
= (char **)vrule
;
4356 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4359 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4362 skp
= smk_import_entry(rulestr
, 0);
4364 return PTR_ERR(skp
);
4366 *rule
= skp
->smk_known
;
4372 * smack_audit_rule_known - Distinguish Smack audit rules
4373 * @krule: rule of interest, in Audit kernel representation format
4375 * This is used to filter Smack rules from remaining Audit ones.
4376 * If it's proved that this rule belongs to us, the
4377 * audit_rule_match hook will be called to do the final judgement.
4379 static int smack_audit_rule_known(struct audit_krule
*krule
)
4381 struct audit_field
*f
;
4384 for (i
= 0; i
< krule
->field_count
; i
++) {
4385 f
= &krule
->fields
[i
];
4387 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4395 * smack_audit_rule_match - Audit given object ?
4396 * @secid: security id for identifying the object to test
4397 * @field: audit rule flags given from user-space
4398 * @op: required testing operator
4399 * @vrule: smack internal rule presentation
4401 * The core Audit hook. It's used to take the decision of
4402 * whether to audit or not to audit a given object.
4404 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
)
4406 struct smack_known
*skp
;
4409 if (unlikely(!rule
)) {
4410 WARN_ONCE(1, "Smack: missing rule\n");
4414 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4417 skp
= smack_from_secid(secid
);
4420 * No need to do string comparisons. If a match occurs,
4421 * both pointers will point to the same smack_known
4424 if (op
== Audit_equal
)
4425 return (rule
== skp
->smk_known
);
4426 if (op
== Audit_not_equal
)
4427 return (rule
!= skp
->smk_known
);
4433 * There is no need for a smack_audit_rule_free hook.
4434 * No memory was allocated.
4437 #endif /* CONFIG_AUDIT */
4440 * smack_ismaclabel - check if xattr @name references a smack MAC label
4441 * @name: Full xattr name to check.
4443 static int smack_ismaclabel(const char *name
)
4445 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4450 * smack_secid_to_secctx - return the smack label for a secid
4451 * @secid: incoming integer
4452 * @secdata: destination
4453 * @seclen: how long it is
4455 * Exists for networking code.
4457 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4459 struct smack_known
*skp
= smack_from_secid(secid
);
4462 *secdata
= skp
->smk_known
;
4463 *seclen
= strlen(skp
->smk_known
);
4468 * smack_secctx_to_secid - return the secid for a smack label
4469 * @secdata: smack label
4470 * @seclen: how long result is
4471 * @secid: outgoing integer
4473 * Exists for audit and networking code.
4475 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4477 struct smack_known
*skp
= smk_find_entry(secdata
);
4480 *secid
= skp
->smk_secid
;
4487 * There used to be a smack_release_secctx hook
4488 * that did nothing back when hooks were in a vector.
4489 * Now that there's a list such a hook adds cost.
4492 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4494 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4497 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4499 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4502 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4504 struct smack_known
*skp
= smk_of_inode(inode
);
4506 *ctx
= skp
->smk_known
;
4507 *ctxlen
= strlen(skp
->smk_known
);
4511 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4514 struct task_smack
*tsp
;
4515 struct smack_known
*skp
;
4516 struct inode_smack
*isp
;
4517 struct cred
*new_creds
= *new;
4519 if (new_creds
== NULL
) {
4520 new_creds
= prepare_creds();
4521 if (new_creds
== NULL
)
4525 tsp
= smack_cred(new_creds
);
4528 * Get label from overlay inode and set it in create_sid
4530 isp
= smack_inode(d_inode(dentry
->d_parent
));
4531 skp
= isp
->smk_inode
;
4532 tsp
->smk_task
= skp
;
4537 static int smack_inode_copy_up_xattr(const char *name
)
4540 * Return 1 if this is the smack access Smack attribute.
4542 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4548 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4550 const struct cred
*old
,
4553 struct task_smack
*otsp
= smack_cred(old
);
4554 struct task_smack
*ntsp
= smack_cred(new);
4555 struct inode_smack
*isp
;
4559 * Use the process credential unless all of
4560 * the transmuting criteria are met
4562 ntsp
->smk_task
= otsp
->smk_task
;
4565 * the attribute of the containing directory
4567 isp
= smack_inode(d_inode(dentry
->d_parent
));
4569 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4571 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4572 isp
->smk_inode
->smk_known
,
4573 &otsp
->smk_task
->smk_rules
);
4577 * If the directory is transmuting and the rule
4578 * providing access is transmuting use the containing
4579 * directory label instead of the process label.
4581 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4582 ntsp
->smk_task
= isp
->smk_inode
;
4587 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init
= {
4588 .lbs_cred
= sizeof(struct task_smack
),
4589 .lbs_file
= sizeof(struct smack_known
*),
4590 .lbs_inode
= sizeof(struct inode_smack
),
4591 .lbs_ipc
= sizeof(struct smack_known
*),
4592 .lbs_msg_msg
= sizeof(struct smack_known
*),
4595 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4596 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4597 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4598 LSM_HOOK_INIT(syslog
, smack_syslog
),
4600 LSM_HOOK_INIT(fs_context_dup
, smack_fs_context_dup
),
4601 LSM_HOOK_INIT(fs_context_parse_param
, smack_fs_context_parse_param
),
4603 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4604 LSM_HOOK_INIT(sb_free_security
, smack_sb_free_security
),
4605 LSM_HOOK_INIT(sb_free_mnt_opts
, smack_free_mnt_opts
),
4606 LSM_HOOK_INIT(sb_eat_lsm_opts
, smack_sb_eat_lsm_opts
),
4607 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4608 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4610 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4612 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4613 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4614 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4615 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4616 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4617 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4618 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4619 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4620 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4621 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4622 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4623 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4624 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4625 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4626 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4627 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4628 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4630 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4631 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4632 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4633 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4634 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4635 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4636 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4637 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4638 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4640 LSM_HOOK_INIT(file_open
, smack_file_open
),
4642 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4643 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4644 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4645 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4646 LSM_HOOK_INIT(cred_getsecid
, smack_cred_getsecid
),
4647 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4648 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4649 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4650 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4651 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4652 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4653 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4654 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4655 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4656 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4657 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4658 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4659 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4660 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4662 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4663 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4665 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4667 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_ipc_alloc_security
),
4668 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4669 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4670 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4671 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4673 LSM_HOOK_INIT(shm_alloc_security
, smack_ipc_alloc_security
),
4674 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4675 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4676 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4678 LSM_HOOK_INIT(sem_alloc_security
, smack_ipc_alloc_security
),
4679 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4680 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4681 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4683 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4685 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4686 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4688 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4689 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4691 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4692 LSM_HOOK_INIT(socket_socketpair
, smack_socket_socketpair
),
4693 #ifdef SMACK_IPV6_PORT_LABELING
4694 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4696 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4697 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4698 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4699 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4700 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4701 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4702 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4703 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4704 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4705 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4707 /* key management security hooks */
4709 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4710 LSM_HOOK_INIT(key_free
, smack_key_free
),
4711 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4712 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4713 #endif /* CONFIG_KEYS */
4717 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4718 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4719 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4720 #endif /* CONFIG_AUDIT */
4722 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4723 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4724 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4725 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4726 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4727 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4728 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4729 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4730 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4734 static __init
void init_smack_known_list(void)
4737 * Initialize rule list locks
4739 mutex_init(&smack_known_huh
.smk_rules_lock
);
4740 mutex_init(&smack_known_hat
.smk_rules_lock
);
4741 mutex_init(&smack_known_floor
.smk_rules_lock
);
4742 mutex_init(&smack_known_star
.smk_rules_lock
);
4743 mutex_init(&smack_known_web
.smk_rules_lock
);
4745 * Initialize rule lists
4747 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4748 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4749 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4750 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4751 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4753 * Create the known labels list
4755 smk_insert_entry(&smack_known_huh
);
4756 smk_insert_entry(&smack_known_hat
);
4757 smk_insert_entry(&smack_known_star
);
4758 smk_insert_entry(&smack_known_floor
);
4759 smk_insert_entry(&smack_known_web
);
4763 * smack_init - initialize the smack system
4765 * Returns 0 on success, -ENOMEM is there's no memory
4767 static __init
int smack_init(void)
4769 struct cred
*cred
= (struct cred
*) current
->cred
;
4770 struct task_smack
*tsp
;
4772 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4773 if (!smack_inode_cache
)
4776 smack_rule_cache
= KMEM_CACHE(smack_rule
, 0);
4777 if (!smack_rule_cache
) {
4778 kmem_cache_destroy(smack_inode_cache
);
4783 * Set the security state for the initial task.
4785 tsp
= smack_cred(cred
);
4786 init_task_smack(tsp
, &smack_known_floor
, &smack_known_floor
);
4791 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4794 pr_info("Smack: Initializing.\n");
4795 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4796 pr_info("Smack: Netfilter enabled.\n");
4798 #ifdef SMACK_IPV6_PORT_LABELING
4799 pr_info("Smack: IPv6 port labeling enabled.\n");
4801 #ifdef SMACK_IPV6_SECMARK_LABELING
4802 pr_info("Smack: IPv6 Netfilter enabled.\n");
4805 /* initialize the smack_known_list */
4806 init_smack_known_list();
4812 * Smack requires early initialization in order to label
4813 * all processes and objects when they are created.
4815 DEFINE_LSM(smack
) = {
4817 .flags
= LSM_FLAG_LEGACY_MAJOR
| LSM_FLAG_EXCLUSIVE
,
4818 .blobs
= &smack_blob_sizes
,