1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplified MAC Kernel (smack) security module
5 * This file contains the smack hook function implementations.
8 * Casey Schaufler <casey@schaufler-ca.com>
9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
13 * Paul Moore <paul@paul-moore.com>
14 * Copyright (C) 2010 Nokia Corporation
15 * Copyright (C) 2011 Intel Corporation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
23 #include <asm/ioctls.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/dccp.h>
28 #include <linux/icmpv6.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include <linux/personality.h>
38 #include <linux/msg.h>
39 #include <linux/shm.h>
40 #include <linux/binfmts.h>
41 #include <linux/parser.h>
42 #include <linux/fs_context.h>
43 #include <linux/fs_parser.h>
46 #define TRANS_TRUE "TRUE"
47 #define TRANS_TRUE_SIZE 4
49 #define SMK_CONNECTING 0
50 #define SMK_RECEIVING 1
53 #ifdef SMACK_IPV6_PORT_LABELING
54 DEFINE_MUTEX(smack_ipv6_lock
);
55 static LIST_HEAD(smk_ipv6_port_list
);
57 static struct kmem_cache
*smack_inode_cache
;
58 struct kmem_cache
*smack_rule_cache
;
61 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
66 } smk_mount_opts
[] = {
67 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault
},
68 A(fsdefault
), A(fsfloor
), A(fshat
), A(fsroot
), A(fstransmute
)
72 static int match_opt_prefix(char *s
, int l
, char **arg
)
76 for (i
= 0; i
< ARRAY_SIZE(smk_mount_opts
); i
++) {
77 size_t len
= smk_mount_opts
[i
].len
;
78 if (len
> l
|| memcmp(s
, smk_mount_opts
[i
].name
, len
))
80 if (len
== l
|| s
[len
] != '=')
83 return smk_mount_opts
[i
].opt
;
88 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
89 static char *smk_bu_mess
[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96 static void smk_bu_mode(int mode
, char *s
)
102 if (mode
& MAY_WRITE
)
106 if (mode
& MAY_APPEND
)
108 if (mode
& MAY_TRANSMUTE
)
118 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
119 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
120 struct smack_known
*oskp
, int mode
, int rc
)
122 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
126 if (rc
> SMACK_UNCONFINED_OBJECT
)
129 smk_bu_mode(mode
, acc
);
130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
131 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
135 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
138 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
139 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
142 struct task_smack
*tsp
= smack_cred(current_cred());
143 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
147 if (rc
> SMACK_UNCONFINED_OBJECT
)
150 smk_bu_mode(mode
, acc
);
151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
152 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
153 acc
, current
->comm
, note
);
157 #define smk_bu_current(note, oskp, mode, RC) (RC)
160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
163 struct task_smack
*tsp
= smack_cred(current_cred());
164 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
165 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
169 if (rc
> SMACK_UNCONFINED_OBJECT
)
172 smk_bu_mode(mode
, acc
);
173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
174 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
175 current
->comm
, otp
->comm
);
179 #define smk_bu_task(otp, mode, RC) (RC)
182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
185 struct task_smack
*tsp
= smack_cred(current_cred());
186 struct inode_smack
*isp
= smack_inode(inode
);
187 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
189 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
195 if (rc
> SMACK_UNCONFINED_OBJECT
)
197 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
198 (mode
& (MAY_WRITE
| MAY_APPEND
)))
199 isp
->smk_flags
|= SMK_INODE_IMPURE
;
201 smk_bu_mode(mode
, acc
);
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
204 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
205 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
209 #define smk_bu_inode(inode, mode, RC) (RC)
212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
215 struct task_smack
*tsp
= smack_cred(current_cred());
216 struct smack_known
*sskp
= tsp
->smk_task
;
217 struct inode
*inode
= file_inode(file
);
218 struct inode_smack
*isp
= smack_inode(inode
);
219 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
221 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
227 if (rc
> SMACK_UNCONFINED_OBJECT
)
230 smk_bu_mode(mode
, acc
);
231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
232 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
233 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
238 #define smk_bu_file(file, mode, RC) (RC)
241 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
242 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
245 struct task_smack
*tsp
= smack_cred(cred
);
246 struct smack_known
*sskp
= tsp
->smk_task
;
247 struct inode
*inode
= file_inode(file
);
248 struct inode_smack
*isp
= smack_inode(inode
);
249 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
251 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
257 if (rc
> SMACK_UNCONFINED_OBJECT
)
260 smk_bu_mode(mode
, acc
);
261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
262 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
263 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
268 #define smk_bu_credfile(cred, file, mode, RC) (RC)
272 * smk_fetch - Fetch the smack label from a file.
273 * @name: type of the label (attribute)
274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
280 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
285 struct smack_known
*skp
= NULL
;
287 if (!(ip
->i_opflags
& IOP_XATTR
))
288 return ERR_PTR(-EOPNOTSUPP
);
290 buffer
= kzalloc(SMK_LONGLABEL
, GFP_NOFS
);
292 return ERR_PTR(-ENOMEM
);
294 rc
= __vfs_getxattr(dp
, ip
, name
, buffer
, SMK_LONGLABEL
);
300 skp
= smk_import_entry(buffer
, rc
);
308 * init_inode_smack - initialize an inode security blob
309 * @inode: inode to extract the info from
310 * @skp: a pointer to the Smack label entry to use in the blob
313 static void init_inode_smack(struct inode
*inode
, struct smack_known
*skp
)
315 struct inode_smack
*isp
= smack_inode(inode
);
317 isp
->smk_inode
= skp
;
319 mutex_init(&isp
->smk_lock
);
323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
329 static void init_task_smack(struct task_smack
*tsp
, struct smack_known
*task
,
330 struct smack_known
*forked
)
332 tsp
->smk_task
= task
;
333 tsp
->smk_forked
= forked
;
334 INIT_LIST_HEAD(&tsp
->smk_rules
);
335 INIT_LIST_HEAD(&tsp
->smk_relabel
);
336 mutex_init(&tsp
->smk_rules_lock
);
340 * smk_copy_rules - copy a rule set
341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
345 * Returns 0 on success, -ENOMEM on error
347 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
350 struct smack_rule
*nrp
;
351 struct smack_rule
*orp
;
354 list_for_each_entry_rcu(orp
, ohead
, list
) {
355 nrp
= kmem_cache_zalloc(smack_rule_cache
, gfp
);
361 list_add_rcu(&nrp
->list
, nhead
);
367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
372 * Returns 0 on success, -ENOMEM on error
374 static int smk_copy_relabel(struct list_head
*nhead
, struct list_head
*ohead
,
377 struct smack_known_list_elem
*nklep
;
378 struct smack_known_list_elem
*oklep
;
380 list_for_each_entry(oklep
, ohead
, list
) {
381 nklep
= kzalloc(sizeof(struct smack_known_list_elem
), gfp
);
383 smk_destroy_label_list(nhead
);
386 nklep
->smk_label
= oklep
->smk_label
;
387 list_add(&nklep
->list
, nhead
);
394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
397 * Returns a converted MAY_* mode usable by smack rules
399 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
401 if (mode
& PTRACE_MODE_ATTACH
)
402 return MAY_READWRITE
;
403 if (mode
& PTRACE_MODE_READ
)
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
412 * @tracee_known: label entry of the process that's about to be traced
413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
416 * Returns 0 on access granted, -error on error
418 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
419 struct smack_known
*tracee_known
,
420 unsigned int mode
, const char *func
)
423 struct smk_audit_info ad
, *saip
= NULL
;
424 struct task_smack
*tsp
;
425 struct smack_known
*tracer_known
;
426 const struct cred
*tracercred
;
428 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
429 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
430 smk_ad_setfield_u_tsk(&ad
, tracer
);
435 tracercred
= __task_cred(tracer
);
436 tsp
= smack_cred(tracercred
);
437 tracer_known
= smk_of_task(tsp
);
439 if ((mode
& PTRACE_MODE_ATTACH
) &&
440 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
441 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
442 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
444 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
446 else if (smack_privileged_cred(CAP_SYS_PTRACE
, tracercred
))
452 smack_log(tracer_known
->smk_known
,
453 tracee_known
->smk_known
,
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
461 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
469 * We he, that is fun!
473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
474 * @ctp: child task pointer
475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
477 * Returns 0 if access is OK, an error code otherwise
479 * Do the capability checks.
481 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
483 struct smack_known
*skp
;
485 skp
= smk_of_task_struct(ctp
);
487 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
494 * Returns 0 if access is OK, an error code otherwise
496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
498 static int smack_ptrace_traceme(struct task_struct
*ptp
)
501 struct smack_known
*skp
;
503 skp
= smk_of_task(smack_cred(current_cred()));
505 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
510 * smack_syslog - Smack approval on syslog
511 * @typefrom_file: unused
513 * Returns 0 on success, error code otherwise.
515 static int smack_syslog(int typefrom_file
)
518 struct smack_known
*skp
= smk_of_current();
520 if (smack_privileged(CAP_MAC_OVERRIDE
))
523 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
534 * smack_sb_alloc_security - allocate a superblock blob
535 * @sb: the superblock getting the blob
537 * Returns 0 on success or -ENOMEM on error.
539 static int smack_sb_alloc_security(struct super_block
*sb
)
541 struct superblock_smack
*sbsp
;
543 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
548 sbsp
->smk_root
= &smack_known_floor
;
549 sbsp
->smk_default
= &smack_known_floor
;
550 sbsp
->smk_floor
= &smack_known_floor
;
551 sbsp
->smk_hat
= &smack_known_hat
;
553 * SMK_SB_INITIALIZED will be zero from kzalloc.
555 sb
->s_security
= sbsp
;
561 * smack_sb_free_security - free a superblock blob
562 * @sb: the superblock getting the blob
565 static void smack_sb_free_security(struct super_block
*sb
)
567 kfree(sb
->s_security
);
568 sb
->s_security
= NULL
;
571 struct smack_mnt_opts
{
572 const char *fsdefault
, *fsfloor
, *fshat
, *fsroot
, *fstransmute
;
575 static void smack_free_mnt_opts(void *mnt_opts
)
577 struct smack_mnt_opts
*opts
= mnt_opts
;
578 kfree(opts
->fsdefault
);
579 kfree(opts
->fsfloor
);
582 kfree(opts
->fstransmute
);
586 static int smack_add_opt(int token
, const char *s
, void **mnt_opts
)
588 struct smack_mnt_opts
*opts
= *mnt_opts
;
591 opts
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
620 case Opt_fstransmute
:
621 if (opts
->fstransmute
)
623 opts
->fstransmute
= s
;
629 pr_warn("Smack: duplicate mount options\n");
634 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
635 * @fc: The new filesystem context.
636 * @src_fc: The source filesystem context being duplicated.
638 * Returns 0 on success or -ENOMEM on error.
640 static int smack_fs_context_dup(struct fs_context
*fc
,
641 struct fs_context
*src_fc
)
643 struct smack_mnt_opts
*dst
, *src
= src_fc
->security
;
648 fc
->security
= kzalloc(sizeof(struct smack_mnt_opts
), GFP_KERNEL
);
653 if (src
->fsdefault
) {
654 dst
->fsdefault
= kstrdup(src
->fsdefault
, GFP_KERNEL
);
659 dst
->fsfloor
= kstrdup(src
->fsfloor
, GFP_KERNEL
);
664 dst
->fshat
= kstrdup(src
->fshat
, GFP_KERNEL
);
669 dst
->fsroot
= kstrdup(src
->fsroot
, GFP_KERNEL
);
673 if (src
->fstransmute
) {
674 dst
->fstransmute
= kstrdup(src
->fstransmute
, GFP_KERNEL
);
675 if (!dst
->fstransmute
)
681 static const struct fs_parameter_spec smack_fs_parameters
[] = {
682 fsparam_string("smackfsdef", Opt_fsdefault
),
683 fsparam_string("smackfsdefault", Opt_fsdefault
),
684 fsparam_string("smackfsfloor", Opt_fsfloor
),
685 fsparam_string("smackfshat", Opt_fshat
),
686 fsparam_string("smackfsroot", Opt_fsroot
),
687 fsparam_string("smackfstransmute", Opt_fstransmute
),
692 * smack_fs_context_parse_param - Parse a single mount parameter
693 * @fc: The new filesystem context being constructed.
694 * @param: The parameter.
696 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
699 static int smack_fs_context_parse_param(struct fs_context
*fc
,
700 struct fs_parameter
*param
)
702 struct fs_parse_result result
;
705 opt
= fs_parse(fc
, smack_fs_parameters
, param
, &result
);
709 rc
= smack_add_opt(opt
, param
->string
, &fc
->security
);
711 param
->string
= NULL
;
715 static int smack_sb_eat_lsm_opts(char *options
, void **mnt_opts
)
717 char *from
= options
, *to
= options
;
721 char *next
= strchr(from
, ',');
730 token
= match_opt_prefix(from
, len
, &arg
);
731 if (token
!= Opt_error
) {
732 arg
= kmemdup_nul(arg
, from
+ len
- arg
, GFP_KERNEL
);
733 rc
= smack_add_opt(token
, arg
, mnt_opts
);
737 smack_free_mnt_opts(*mnt_opts
);
742 if (!first
) { // copy with preceding comma
747 memmove(to
, from
, len
);
760 * smack_set_mnt_opts - set Smack specific mount options
761 * @sb: the file system superblock
762 * @mnt_opts: Smack mount options
763 * @kern_flags: mount option from kernel space or user space
764 * @set_kern_flags: where to store converted mount opts
766 * Returns 0 on success, an error code on failure
768 * Allow filesystems with binary mount data to explicitly set Smack mount
771 static int smack_set_mnt_opts(struct super_block
*sb
,
773 unsigned long kern_flags
,
774 unsigned long *set_kern_flags
)
776 struct dentry
*root
= sb
->s_root
;
777 struct inode
*inode
= d_backing_inode(root
);
778 struct superblock_smack
*sp
= sb
->s_security
;
779 struct inode_smack
*isp
;
780 struct smack_known
*skp
;
781 struct smack_mnt_opts
*opts
= mnt_opts
;
782 bool transmute
= false;
784 if (sp
->smk_flags
& SMK_SB_INITIALIZED
)
787 if (inode
->i_security
== NULL
) {
788 int rc
= lsm_inode_alloc(inode
);
794 if (!smack_privileged(CAP_MAC_ADMIN
)) {
796 * Unprivileged mounts don't get to specify Smack values.
801 * Unprivileged mounts get root and default from the caller.
803 skp
= smk_of_current();
805 sp
->smk_default
= skp
;
807 * For a handful of fs types with no user-controlled
808 * backing store it's okay to trust security labels
809 * in the filesystem. The rest are untrusted.
811 if (sb
->s_user_ns
!= &init_user_ns
&&
812 sb
->s_magic
!= SYSFS_MAGIC
&& sb
->s_magic
!= TMPFS_MAGIC
&&
813 sb
->s_magic
!= RAMFS_MAGIC
) {
815 sp
->smk_flags
|= SMK_SB_UNTRUSTED
;
819 sp
->smk_flags
|= SMK_SB_INITIALIZED
;
822 if (opts
->fsdefault
) {
823 skp
= smk_import_entry(opts
->fsdefault
, 0);
826 sp
->smk_default
= skp
;
829 skp
= smk_import_entry(opts
->fsfloor
, 0);
835 skp
= smk_import_entry(opts
->fshat
, 0);
841 skp
= smk_import_entry(opts
->fsroot
, 0);
846 if (opts
->fstransmute
) {
847 skp
= smk_import_entry(opts
->fstransmute
, 0);
856 * Initialize the root inode.
858 init_inode_smack(inode
, sp
->smk_root
);
861 isp
= smack_inode(inode
);
862 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
869 * smack_sb_statfs - Smack check on statfs
870 * @dentry: identifies the file system in question
872 * Returns 0 if current can read the floor of the filesystem,
873 * and error code otherwise
875 static int smack_sb_statfs(struct dentry
*dentry
)
877 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
879 struct smk_audit_info ad
;
881 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
882 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
884 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
885 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
894 * smack_bprm_set_creds - set creds for exec
895 * @bprm: the exec information
897 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
899 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
901 struct inode
*inode
= file_inode(bprm
->file
);
902 struct task_smack
*bsp
= smack_cred(bprm
->cred
);
903 struct inode_smack
*isp
;
904 struct superblock_smack
*sbsp
;
907 if (bprm
->called_set_creds
)
910 isp
= smack_inode(inode
);
911 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
914 sbsp
= inode
->i_sb
->s_security
;
915 if ((sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) &&
916 isp
->smk_task
!= sbsp
->smk_root
)
919 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
920 struct task_struct
*tracer
;
924 tracer
= ptrace_parent(current
);
925 if (likely(tracer
!= NULL
))
926 rc
= smk_ptrace_rule_check(tracer
,
935 if (bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
)
938 bsp
->smk_task
= isp
->smk_task
;
939 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
941 /* Decide if this is a secure exec. */
942 if (bsp
->smk_task
!= bsp
->smk_forked
)
943 bprm
->secureexec
= 1;
953 * smack_inode_alloc_security - allocate an inode blob
954 * @inode: the inode in need of a blob
958 static int smack_inode_alloc_security(struct inode
*inode
)
960 struct smack_known
*skp
= smk_of_current();
962 init_inode_smack(inode
, skp
);
967 * smack_inode_init_security - copy out the smack from an inode
968 * @inode: the newly created inode
969 * @dir: containing directory object
971 * @name: where to put the attribute name
972 * @value: where to put the attribute value
973 * @len: where to put the length of the attribute
975 * Returns 0 if it all works out, -ENOMEM if there's no memory
977 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
978 const struct qstr
*qstr
, const char **name
,
979 void **value
, size_t *len
)
981 struct inode_smack
*issp
= smack_inode(inode
);
982 struct smack_known
*skp
= smk_of_current();
983 struct smack_known
*isp
= smk_of_inode(inode
);
984 struct smack_known
*dsp
= smk_of_inode(dir
);
988 *name
= XATTR_SMACK_SUFFIX
;
992 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
997 * If the access rule allows transmutation and
998 * the directory requests transmutation then
999 * by all means transmute.
1000 * Mark the inode as changed.
1002 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
1003 smk_inode_transmutable(dir
)) {
1005 issp
->smk_flags
|= SMK_INODE_CHANGED
;
1008 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
1012 *len
= strlen(isp
->smk_known
);
1019 * smack_inode_link - Smack check on link
1020 * @old_dentry: the existing object
1022 * @new_dentry: the new object
1024 * Returns 0 if access is permitted, an error code otherwise
1026 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1027 struct dentry
*new_dentry
)
1029 struct smack_known
*isp
;
1030 struct smk_audit_info ad
;
1033 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1034 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1036 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1037 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1038 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1040 if (rc
== 0 && d_is_positive(new_dentry
)) {
1041 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1042 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1043 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1044 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1051 * smack_inode_unlink - Smack check on inode deletion
1052 * @dir: containing directory object
1053 * @dentry: file to unlink
1055 * Returns 0 if current can write the containing directory
1056 * and the object, error code otherwise
1058 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1060 struct inode
*ip
= d_backing_inode(dentry
);
1061 struct smk_audit_info ad
;
1064 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1065 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1068 * You need write access to the thing you're unlinking
1070 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1071 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1074 * You also need write access to the containing directory
1076 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1077 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1078 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1079 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1085 * smack_inode_rmdir - Smack check on directory deletion
1086 * @dir: containing directory object
1087 * @dentry: directory to unlink
1089 * Returns 0 if current can write the containing directory
1090 * and the directory, error code otherwise
1092 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1094 struct smk_audit_info ad
;
1097 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1098 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1101 * You need write access to the thing you're removing
1103 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1104 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1107 * You also need write access to the containing directory
1109 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1110 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1111 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1112 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1119 * smack_inode_rename - Smack check on rename
1120 * @old_inode: unused
1121 * @old_dentry: the old object
1122 * @new_inode: unused
1123 * @new_dentry: the new object
1125 * Read and write access is required on both the old and
1128 * Returns 0 if access is permitted, an error code otherwise
1130 static int smack_inode_rename(struct inode
*old_inode
,
1131 struct dentry
*old_dentry
,
1132 struct inode
*new_inode
,
1133 struct dentry
*new_dentry
)
1136 struct smack_known
*isp
;
1137 struct smk_audit_info ad
;
1139 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1140 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1142 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1143 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1144 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1146 if (rc
== 0 && d_is_positive(new_dentry
)) {
1147 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1148 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1149 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1150 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1156 * smack_inode_permission - Smack version of permission()
1157 * @inode: the inode in question
1158 * @mask: the access requested
1160 * This is the important Smack hook.
1162 * Returns 0 if access is permitted, an error code otherwise
1164 static int smack_inode_permission(struct inode
*inode
, int mask
)
1166 struct superblock_smack
*sbsp
= inode
->i_sb
->s_security
;
1167 struct smk_audit_info ad
;
1168 int no_block
= mask
& MAY_NOT_BLOCK
;
1171 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1173 * No permission to check. Existence test. Yup, it's there.
1178 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1179 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1183 /* May be droppable after audit */
1186 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1187 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1188 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1189 rc
= smk_bu_inode(inode
, mask
, rc
);
1194 * smack_inode_setattr - Smack check for setting attributes
1195 * @dentry: the object
1196 * @iattr: for the force flag
1198 * Returns 0 if access is permitted, an error code otherwise
1200 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1202 struct smk_audit_info ad
;
1206 * Need to allow for clearing the setuid bit.
1208 if (iattr
->ia_valid
& ATTR_FORCE
)
1210 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1211 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1213 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1214 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1219 * smack_inode_getattr - Smack check for getting attributes
1220 * @path: path to extract the info from
1222 * Returns 0 if access is permitted, an error code otherwise
1224 static int smack_inode_getattr(const struct path
*path
)
1226 struct smk_audit_info ad
;
1227 struct inode
*inode
= d_backing_inode(path
->dentry
);
1230 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1231 smk_ad_setfield_u_fs_path(&ad
, *path
);
1232 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1233 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1238 * smack_inode_setxattr - Smack check for setting xattrs
1239 * @dentry: the object
1240 * @name: name of the attribute
1241 * @value: value of the attribute
1242 * @size: size of the value
1245 * This protects the Smack attribute explicitly.
1247 * Returns 0 if access is permitted, an error code otherwise
1249 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1250 const void *value
, size_t size
, int flags
)
1252 struct smk_audit_info ad
;
1253 struct smack_known
*skp
;
1255 int check_import
= 0;
1260 * Check label validity here so import won't fail in post_setxattr
1262 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1263 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1264 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1267 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1268 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1272 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1274 if (size
!= TRANS_TRUE_SIZE
||
1275 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1278 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1280 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1283 if (rc
== 0 && check_import
) {
1284 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1287 else if (skp
== NULL
|| (check_star
&&
1288 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1292 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1293 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1296 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1297 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1304 * smack_inode_post_setxattr - Apply the Smack update approved above
1306 * @name: attribute name
1307 * @value: attribute value
1308 * @size: attribute size
1311 * Set the pointer in the inode blob to the entry found
1312 * in the master label list.
1314 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1315 const void *value
, size_t size
, int flags
)
1317 struct smack_known
*skp
;
1318 struct inode_smack
*isp
= smack_inode(d_backing_inode(dentry
));
1320 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1321 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1325 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1326 skp
= smk_import_entry(value
, size
);
1328 isp
->smk_inode
= skp
;
1329 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1330 skp
= smk_import_entry(value
, size
);
1332 isp
->smk_task
= skp
;
1333 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1334 skp
= smk_import_entry(value
, size
);
1336 isp
->smk_mmap
= skp
;
1343 * smack_inode_getxattr - Smack check on getxattr
1344 * @dentry: the object
1347 * Returns 0 if access is permitted, an error code otherwise
1349 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1351 struct smk_audit_info ad
;
1354 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1355 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1357 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1358 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1363 * smack_inode_removexattr - Smack check on removexattr
1364 * @dentry: the object
1365 * @name: name of the attribute
1367 * Removing the Smack attribute requires CAP_MAC_ADMIN
1369 * Returns 0 if access is permitted, an error code otherwise
1371 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1373 struct inode_smack
*isp
;
1374 struct smk_audit_info ad
;
1377 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1378 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1379 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1380 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1381 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1382 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1383 if (!smack_privileged(CAP_MAC_ADMIN
))
1386 rc
= cap_inode_removexattr(dentry
, name
);
1391 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1392 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1394 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1395 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1399 isp
= smack_inode(d_backing_inode(dentry
));
1401 * Don't do anything special for these.
1402 * XATTR_NAME_SMACKIPIN
1403 * XATTR_NAME_SMACKIPOUT
1405 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1406 struct super_block
*sbp
= dentry
->d_sb
;
1407 struct superblock_smack
*sbsp
= sbp
->s_security
;
1409 isp
->smk_inode
= sbsp
->smk_default
;
1410 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1411 isp
->smk_task
= NULL
;
1412 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1413 isp
->smk_mmap
= NULL
;
1414 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1415 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1421 * smack_inode_getsecurity - get smack xattrs
1422 * @inode: the object
1423 * @name: attribute name
1424 * @buffer: where to put the result
1425 * @alloc: duplicate memory
1427 * Returns the size of the attribute or an error code
1429 static int smack_inode_getsecurity(struct inode
*inode
,
1430 const char *name
, void **buffer
,
1433 struct socket_smack
*ssp
;
1434 struct socket
*sock
;
1435 struct super_block
*sbp
;
1436 struct inode
*ip
= (struct inode
*)inode
;
1437 struct smack_known
*isp
;
1439 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1440 isp
= smk_of_inode(inode
);
1443 * The rest of the Smack xattrs are only on sockets.
1446 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1449 sock
= SOCKET_I(ip
);
1450 if (sock
== NULL
|| sock
->sk
== NULL
)
1453 ssp
= sock
->sk
->sk_security
;
1455 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1457 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1464 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1465 if (*buffer
== NULL
)
1469 return strlen(isp
->smk_known
);
1474 * smack_inode_listsecurity - list the Smack attributes
1475 * @inode: the object
1476 * @buffer: where they go
1477 * @buffer_size: size of buffer
1479 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1482 int len
= sizeof(XATTR_NAME_SMACK
);
1484 if (buffer
!= NULL
&& len
<= buffer_size
)
1485 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1491 * smack_inode_getsecid - Extract inode's security id
1492 * @inode: inode to extract the info from
1493 * @secid: where result will be saved
1495 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1497 struct smack_known
*skp
= smk_of_inode(inode
);
1499 *secid
= skp
->smk_secid
;
1507 * There is no smack_file_permission hook
1509 * Should access checks be done on each read or write?
1510 * UNICOS and SELinux say yes.
1511 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1513 * I'll say no for now. Smack does not do the frequent
1514 * label changing that SELinux does.
1518 * smack_file_alloc_security - assign a file security blob
1521 * The security blob for a file is a pointer to the master
1522 * label list, so no allocation is done.
1524 * f_security is the owner security information. It
1525 * isn't used on file access checks, it's for send_sigio.
1529 static int smack_file_alloc_security(struct file
*file
)
1531 struct smack_known
**blob
= smack_file(file
);
1533 *blob
= smk_of_current();
1538 * smack_file_ioctl - Smack check on ioctls
1543 * Relies heavily on the correct use of the ioctl command conventions.
1545 * Returns 0 if allowed, error code otherwise
1547 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1551 struct smk_audit_info ad
;
1552 struct inode
*inode
= file_inode(file
);
1554 if (unlikely(IS_PRIVATE(inode
)))
1557 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1558 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1560 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1561 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1562 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1565 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1566 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1567 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1574 * smack_file_lock - Smack check on file locking
1578 * Returns 0 if current has lock access, error code otherwise
1580 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1582 struct smk_audit_info ad
;
1584 struct inode
*inode
= file_inode(file
);
1586 if (unlikely(IS_PRIVATE(inode
)))
1589 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1590 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1591 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1592 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1597 * smack_file_fcntl - Smack check on fcntl
1599 * @cmd: what action to check
1602 * Generally these operations are harmless.
1603 * File locking operations present an obvious mechanism
1604 * for passing information, so they require write access.
1606 * Returns 0 if current has access, error code otherwise
1608 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1611 struct smk_audit_info ad
;
1613 struct inode
*inode
= file_inode(file
);
1615 if (unlikely(IS_PRIVATE(inode
)))
1623 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1624 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1625 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1626 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1630 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1631 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1632 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1633 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1644 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1645 * if mapping anonymous memory.
1646 * @file contains the file structure for file to map (may be NULL).
1647 * @reqprot contains the protection requested by the application.
1648 * @prot contains the protection that will be applied by the kernel.
1649 * @flags contains the operational flags.
1650 * Return 0 if permission is granted.
1652 static int smack_mmap_file(struct file
*file
,
1653 unsigned long reqprot
, unsigned long prot
,
1654 unsigned long flags
)
1656 struct smack_known
*skp
;
1657 struct smack_known
*mkp
;
1658 struct smack_rule
*srp
;
1659 struct task_smack
*tsp
;
1660 struct smack_known
*okp
;
1661 struct inode_smack
*isp
;
1662 struct superblock_smack
*sbsp
;
1671 if (unlikely(IS_PRIVATE(file_inode(file
))))
1674 isp
= smack_inode(file_inode(file
));
1675 if (isp
->smk_mmap
== NULL
)
1677 sbsp
= file_inode(file
)->i_sb
->s_security
;
1678 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1679 isp
->smk_mmap
!= sbsp
->smk_root
)
1681 mkp
= isp
->smk_mmap
;
1683 tsp
= smack_cred(current_cred());
1684 skp
= smk_of_current();
1689 * For each Smack rule associated with the subject
1690 * label verify that the SMACK64MMAP also has access
1691 * to that rule's object label.
1693 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1694 okp
= srp
->smk_object
;
1696 * Matching labels always allows access.
1698 if (mkp
->smk_known
== okp
->smk_known
)
1701 * If there is a matching local rule take
1702 * that into account as well.
1704 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1708 may
= srp
->smk_access
;
1710 may
&= srp
->smk_access
;
1712 * If may is zero the SMACK64MMAP subject can't
1713 * possibly have less access.
1719 * Fetch the global list entry.
1720 * If there isn't one a SMACK64MMAP subject
1721 * can't have as much access as current.
1723 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1725 if (mmay
== -ENOENT
) {
1730 * If there is a local entry it modifies the
1731 * potential access, too.
1733 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1735 if (tmay
!= -ENOENT
)
1739 * If there is any access available to current that is
1740 * not available to a SMACK64MMAP subject
1743 if ((may
| mmay
) != mmay
) {
1755 * smack_file_set_fowner - set the file security blob value
1756 * @file: object in question
1759 static void smack_file_set_fowner(struct file
*file
)
1761 struct smack_known
**blob
= smack_file(file
);
1763 *blob
= smk_of_current();
1767 * smack_file_send_sigiotask - Smack on sigio
1768 * @tsk: The target task
1769 * @fown: the object the signal come from
1772 * Allow a privileged task to get signals even if it shouldn't
1774 * Returns 0 if a subject with the object's smack could
1775 * write to the task, an error code otherwise.
1777 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1778 struct fown_struct
*fown
, int signum
)
1780 struct smack_known
**blob
;
1781 struct smack_known
*skp
;
1782 struct smack_known
*tkp
= smk_of_task(smack_cred(tsk
->cred
));
1783 const struct cred
*tcred
;
1786 struct smk_audit_info ad
;
1789 * struct fown_struct is never outside the context of a struct file
1791 file
= container_of(fown
, struct file
, f_owner
);
1793 /* we don't log here as rc can be overriden */
1794 blob
= smack_file(file
);
1796 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1797 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1800 tcred
= __task_cred(tsk
);
1801 if (rc
!= 0 && smack_privileged_cred(CAP_MAC_OVERRIDE
, tcred
))
1805 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1806 smk_ad_setfield_u_tsk(&ad
, tsk
);
1807 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1812 * smack_file_receive - Smack file receive check
1815 * Returns 0 if current has access, error code otherwise
1817 static int smack_file_receive(struct file
*file
)
1821 struct smk_audit_info ad
;
1822 struct inode
*inode
= file_inode(file
);
1823 struct socket
*sock
;
1824 struct task_smack
*tsp
;
1825 struct socket_smack
*ssp
;
1827 if (unlikely(IS_PRIVATE(inode
)))
1830 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1831 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1833 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1834 sock
= SOCKET_I(inode
);
1835 ssp
= sock
->sk
->sk_security
;
1836 tsp
= smack_cred(current_cred());
1838 * If the receiving process can't write to the
1839 * passed socket or if the passed socket can't
1840 * write to the receiving process don't accept
1841 * the passed socket.
1843 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1844 rc
= smk_bu_file(file
, may
, rc
);
1847 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1848 rc
= smk_bu_file(file
, may
, rc
);
1852 * This code relies on bitmasks.
1854 if (file
->f_mode
& FMODE_READ
)
1856 if (file
->f_mode
& FMODE_WRITE
)
1859 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1860 rc
= smk_bu_file(file
, may
, rc
);
1865 * smack_file_open - Smack dentry open processing
1868 * Set the security blob in the file structure.
1869 * Allow the open only if the task has read access. There are
1870 * many read operations (e.g. fstat) that you can do with an
1871 * fd even if you have the file open write-only.
1873 * Returns 0 if current has access, error code otherwise
1875 static int smack_file_open(struct file
*file
)
1877 struct task_smack
*tsp
= smack_cred(file
->f_cred
);
1878 struct inode
*inode
= file_inode(file
);
1879 struct smk_audit_info ad
;
1882 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1883 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1884 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1885 rc
= smk_bu_credfile(file
->f_cred
, file
, MAY_READ
, rc
);
1895 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1896 * @cred: the new credentials
1897 * @gfp: the atomicity of any memory allocations
1899 * Prepare a blank set of credentials for modification. This must allocate all
1900 * the memory the LSM module might require such that cred_transfer() can
1901 * complete without error.
1903 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1905 init_task_smack(smack_cred(cred
), NULL
, NULL
);
1911 * smack_cred_free - "free" task-level security credentials
1912 * @cred: the credentials in question
1915 static void smack_cred_free(struct cred
*cred
)
1917 struct task_smack
*tsp
= smack_cred(cred
);
1918 struct smack_rule
*rp
;
1919 struct list_head
*l
;
1920 struct list_head
*n
;
1922 smk_destroy_label_list(&tsp
->smk_relabel
);
1924 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1925 rp
= list_entry(l
, struct smack_rule
, list
);
1926 list_del(&rp
->list
);
1927 kmem_cache_free(smack_rule_cache
, rp
);
1932 * smack_cred_prepare - prepare new set of credentials for modification
1933 * @new: the new credentials
1934 * @old: the original credentials
1935 * @gfp: the atomicity of any memory allocations
1937 * Prepare a new set of credentials for modification.
1939 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1942 struct task_smack
*old_tsp
= smack_cred(old
);
1943 struct task_smack
*new_tsp
= smack_cred(new);
1946 init_task_smack(new_tsp
, old_tsp
->smk_task
, old_tsp
->smk_task
);
1948 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1952 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
1958 * smack_cred_transfer - Transfer the old credentials to the new credentials
1959 * @new: the new credentials
1960 * @old: the original credentials
1962 * Fill in a set of blank credentials from another set of credentials.
1964 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1966 struct task_smack
*old_tsp
= smack_cred(old
);
1967 struct task_smack
*new_tsp
= smack_cred(new);
1969 new_tsp
->smk_task
= old_tsp
->smk_task
;
1970 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1971 mutex_init(&new_tsp
->smk_rules_lock
);
1972 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1974 /* cbs copy rule list */
1978 * smack_cred_getsecid - get the secid corresponding to a creds structure
1979 * @cred: the object creds
1980 * @secid: where to put the result
1982 * Sets the secid to contain a u32 version of the smack label.
1984 static void smack_cred_getsecid(const struct cred
*cred
, u32
*secid
)
1986 struct smack_known
*skp
;
1989 skp
= smk_of_task(smack_cred(cred
));
1990 *secid
= skp
->smk_secid
;
1995 * smack_kernel_act_as - Set the subjective context in a set of credentials
1996 * @new: points to the set of credentials to be modified.
1997 * @secid: specifies the security ID to be set
1999 * Set the security data for a kernel service.
2001 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
2003 struct task_smack
*new_tsp
= smack_cred(new);
2005 new_tsp
->smk_task
= smack_from_secid(secid
);
2010 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2011 * @new: points to the set of credentials to be modified
2012 * @inode: points to the inode to use as a reference
2014 * Set the file creation context in a set of credentials to the same
2015 * as the objective context of the specified inode
2017 static int smack_kernel_create_files_as(struct cred
*new,
2018 struct inode
*inode
)
2020 struct inode_smack
*isp
= smack_inode(inode
);
2021 struct task_smack
*tsp
= smack_cred(new);
2023 tsp
->smk_forked
= isp
->smk_inode
;
2024 tsp
->smk_task
= tsp
->smk_forked
;
2029 * smk_curacc_on_task - helper to log task related access
2030 * @p: the task object
2031 * @access: the access requested
2032 * @caller: name of the calling function for audit
2034 * Return 0 if access is permitted
2036 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
2039 struct smk_audit_info ad
;
2040 struct smack_known
*skp
= smk_of_task_struct(p
);
2043 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2044 smk_ad_setfield_u_tsk(&ad
, p
);
2045 rc
= smk_curacc(skp
, access
, &ad
);
2046 rc
= smk_bu_task(p
, access
, rc
);
2051 * smack_task_setpgid - Smack check on setting pgid
2052 * @p: the task object
2055 * Return 0 if write access is permitted
2057 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2059 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2063 * smack_task_getpgid - Smack access check for getpgid
2064 * @p: the object task
2066 * Returns 0 if current can read the object task, error code otherwise
2068 static int smack_task_getpgid(struct task_struct
*p
)
2070 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2074 * smack_task_getsid - Smack access check for getsid
2075 * @p: the object task
2077 * Returns 0 if current can read the object task, error code otherwise
2079 static int smack_task_getsid(struct task_struct
*p
)
2081 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2085 * smack_task_getsecid - get the secid of the task
2086 * @p: the object task
2087 * @secid: where to put the result
2089 * Sets the secid to contain a u32 version of the smack label.
2091 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2093 struct smack_known
*skp
= smk_of_task_struct(p
);
2095 *secid
= skp
->smk_secid
;
2099 * smack_task_setnice - Smack check on setting nice
2100 * @p: the task object
2103 * Return 0 if write access is permitted
2105 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2107 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2111 * smack_task_setioprio - Smack check on setting ioprio
2112 * @p: the task object
2115 * Return 0 if write access is permitted
2117 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2119 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2123 * smack_task_getioprio - Smack check on reading ioprio
2124 * @p: the task object
2126 * Return 0 if read access is permitted
2128 static int smack_task_getioprio(struct task_struct
*p
)
2130 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2134 * smack_task_setscheduler - Smack check on setting scheduler
2135 * @p: the task object
2137 * Return 0 if read access is permitted
2139 static int smack_task_setscheduler(struct task_struct
*p
)
2141 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2145 * smack_task_getscheduler - Smack check on reading scheduler
2146 * @p: the task object
2148 * Return 0 if read access is permitted
2150 static int smack_task_getscheduler(struct task_struct
*p
)
2152 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2156 * smack_task_movememory - Smack check on moving memory
2157 * @p: the task object
2159 * Return 0 if write access is permitted
2161 static int smack_task_movememory(struct task_struct
*p
)
2163 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2167 * smack_task_kill - Smack check on signal delivery
2168 * @p: the task object
2171 * @cred: identifies the cred to use in lieu of current's
2173 * Return 0 if write access is permitted
2176 static int smack_task_kill(struct task_struct
*p
, struct kernel_siginfo
*info
,
2177 int sig
, const struct cred
*cred
)
2179 struct smk_audit_info ad
;
2180 struct smack_known
*skp
;
2181 struct smack_known
*tkp
= smk_of_task_struct(p
);
2185 return 0; /* null signal; existence test */
2187 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2188 smk_ad_setfield_u_tsk(&ad
, p
);
2190 * Sending a signal requires that the sender
2191 * can write the receiver.
2194 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2195 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2199 * If the cred isn't NULL we're dealing with some USB IO
2200 * specific behavior. This is not clean. For one thing
2201 * we can't take privilege into account.
2203 skp
= smk_of_task(smack_cred(cred
));
2204 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2205 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2210 * smack_task_to_inode - copy task smack into the inode blob
2211 * @p: task to copy from
2212 * @inode: inode to copy to
2214 * Sets the smack pointer in the inode security blob
2216 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2218 struct inode_smack
*isp
= smack_inode(inode
);
2219 struct smack_known
*skp
= smk_of_task_struct(p
);
2221 isp
->smk_inode
= skp
;
2222 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2230 * smack_sk_alloc_security - Allocate a socket blob
2233 * @gfp_flags: memory allocation flags
2235 * Assign Smack pointers to current
2237 * Returns 0 on success, -ENOMEM is there's no memory
2239 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2241 struct smack_known
*skp
= smk_of_current();
2242 struct socket_smack
*ssp
;
2244 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2249 * Sockets created by kernel threads receive web label.
2251 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2252 ssp
->smk_in
= &smack_known_web
;
2253 ssp
->smk_out
= &smack_known_web
;
2258 ssp
->smk_packet
= NULL
;
2260 sk
->sk_security
= ssp
;
2266 * smack_sk_free_security - Free a socket blob
2269 * Clears the blob pointer
2271 static void smack_sk_free_security(struct sock
*sk
)
2273 #ifdef SMACK_IPV6_PORT_LABELING
2274 struct smk_port_label
*spp
;
2276 if (sk
->sk_family
== PF_INET6
) {
2278 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2279 if (spp
->smk_sock
!= sk
)
2281 spp
->smk_can_reuse
= 1;
2287 kfree(sk
->sk_security
);
2291 * smack_ipv4host_label - check host based restrictions
2292 * @sip: the object end
2294 * looks for host based access restrictions
2296 * This version will only be appropriate for really small sets of single label
2297 * hosts. The caller is responsible for ensuring that the RCU read lock is
2298 * taken before calling this function.
2300 * Returns the label of the far end or NULL if it's not special.
2302 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2304 struct smk_net4addr
*snp
;
2305 struct in_addr
*siap
= &sip
->sin_addr
;
2307 if (siap
->s_addr
== 0)
2310 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2312 * we break after finding the first match because
2313 * the list is sorted from longest to shortest mask
2314 * so we have found the most specific match
2316 if (snp
->smk_host
.s_addr
==
2317 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2318 return snp
->smk_label
;
2323 #if IS_ENABLED(CONFIG_IPV6)
2325 * smk_ipv6_localhost - Check for local ipv6 host address
2328 * Returns boolean true if this is the localhost address
2330 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2332 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2333 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2335 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2336 ntohs(be16p
[7]) == 1)
2342 * smack_ipv6host_label - check host based restrictions
2343 * @sip: the object end
2345 * looks for host based access restrictions
2347 * This version will only be appropriate for really small sets of single label
2348 * hosts. The caller is responsible for ensuring that the RCU read lock is
2349 * taken before calling this function.
2351 * Returns the label of the far end or NULL if it's not special.
2353 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2355 struct smk_net6addr
*snp
;
2356 struct in6_addr
*sap
= &sip
->sin6_addr
;
2361 * It's local. Don't look for a host label.
2363 if (smk_ipv6_localhost(sip
))
2366 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2368 * If the label is NULL the entry has
2369 * been renounced. Ignore it.
2371 if (snp
->smk_label
== NULL
)
2374 * we break after finding the first match because
2375 * the list is sorted from longest to shortest mask
2376 * so we have found the most specific match
2378 for (found
= 1, i
= 0; i
< 8; i
++) {
2379 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2380 snp
->smk_host
.s6_addr16
[i
]) {
2386 return snp
->smk_label
;
2391 #endif /* CONFIG_IPV6 */
2394 * smack_netlabel - Set the secattr on a socket
2396 * @labeled: socket label scheme
2398 * Convert the outbound smack value (smk_out) to a
2399 * secattr and attach it to the socket.
2401 * Returns 0 on success or an error code
2403 static int smack_netlabel(struct sock
*sk
, int labeled
)
2405 struct smack_known
*skp
;
2406 struct socket_smack
*ssp
= sk
->sk_security
;
2410 * Usually the netlabel code will handle changing the
2411 * packet labeling based on the label.
2412 * The case of a single label host is different, because
2413 * a single label host should never get a labeled packet
2414 * even though the label is usually associated with a packet
2418 bh_lock_sock_nested(sk
);
2420 if (ssp
->smk_out
== smack_net_ambient
||
2421 labeled
== SMACK_UNLABELED_SOCKET
)
2422 netlbl_sock_delattr(sk
);
2425 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2435 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2437 * @sap: the destination address
2439 * Set the correct secattr for the given socket based on the destination
2440 * address and perform any outbound access checks needed.
2442 * Returns 0 on success or an error code.
2445 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2447 struct smack_known
*skp
;
2450 struct smack_known
*hkp
;
2451 struct socket_smack
*ssp
= sk
->sk_security
;
2452 struct smk_audit_info ad
;
2455 hkp
= smack_ipv4host_label(sap
);
2458 struct lsm_network_audit net
;
2460 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2461 ad
.a
.u
.net
->family
= sap
->sin_family
;
2462 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2463 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2465 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2467 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2468 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2470 sk_lbl
= SMACK_CIPSO_SOCKET
;
2477 return smack_netlabel(sk
, sk_lbl
);
2480 #if IS_ENABLED(CONFIG_IPV6)
2482 * smk_ipv6_check - check Smack access
2483 * @subject: subject Smack label
2484 * @object: object Smack label
2486 * @act: the action being taken
2488 * Check an IPv6 access
2490 static int smk_ipv6_check(struct smack_known
*subject
,
2491 struct smack_known
*object
,
2492 struct sockaddr_in6
*address
, int act
)
2495 struct lsm_network_audit net
;
2497 struct smk_audit_info ad
;
2501 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2502 ad
.a
.u
.net
->family
= PF_INET6
;
2503 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2504 if (act
== SMK_RECEIVING
)
2505 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2507 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2509 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2510 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2513 #endif /* CONFIG_IPV6 */
2515 #ifdef SMACK_IPV6_PORT_LABELING
2517 * smk_ipv6_port_label - Smack port access table management
2521 * Create or update the port list entry
2523 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2525 struct sock
*sk
= sock
->sk
;
2526 struct sockaddr_in6
*addr6
;
2527 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2528 struct smk_port_label
*spp
;
2529 unsigned short port
= 0;
2531 if (address
== NULL
) {
2533 * This operation is changing the Smack information
2534 * on the bound socket. Take the changes to the port
2538 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2539 if (sk
!= spp
->smk_sock
)
2541 spp
->smk_in
= ssp
->smk_in
;
2542 spp
->smk_out
= ssp
->smk_out
;
2547 * A NULL address is only used for updating existing
2548 * bound entries. If there isn't one, it's OK.
2554 addr6
= (struct sockaddr_in6
*)address
;
2555 port
= ntohs(addr6
->sin6_port
);
2557 * This is a special case that is safely ignored.
2563 * Look for an existing port list entry.
2564 * This is an indication that a port is getting reused.
2567 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2568 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2570 if (spp
->smk_can_reuse
!= 1) {
2574 spp
->smk_port
= port
;
2576 spp
->smk_in
= ssp
->smk_in
;
2577 spp
->smk_out
= ssp
->smk_out
;
2578 spp
->smk_can_reuse
= 0;
2584 * A new port entry is required.
2586 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2590 spp
->smk_port
= port
;
2592 spp
->smk_in
= ssp
->smk_in
;
2593 spp
->smk_out
= ssp
->smk_out
;
2594 spp
->smk_sock_type
= sock
->type
;
2595 spp
->smk_can_reuse
= 0;
2597 mutex_lock(&smack_ipv6_lock
);
2598 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2599 mutex_unlock(&smack_ipv6_lock
);
2604 * smk_ipv6_port_check - check Smack port access
2607 * @act: the action being taken
2609 * Create or update the port list entry
2611 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2614 struct smk_port_label
*spp
;
2615 struct socket_smack
*ssp
= sk
->sk_security
;
2616 struct smack_known
*skp
= NULL
;
2617 unsigned short port
;
2618 struct smack_known
*object
;
2620 if (act
== SMK_RECEIVING
) {
2621 skp
= smack_ipv6host_label(address
);
2622 object
= ssp
->smk_in
;
2625 object
= smack_ipv6host_label(address
);
2629 * The other end is a single label host.
2631 if (skp
!= NULL
&& object
!= NULL
)
2632 return smk_ipv6_check(skp
, object
, address
, act
);
2634 skp
= smack_net_ambient
;
2636 object
= smack_net_ambient
;
2639 * It's remote, so port lookup does no good.
2641 if (!smk_ipv6_localhost(address
))
2642 return smk_ipv6_check(skp
, object
, address
, act
);
2645 * It's local so the send check has to have passed.
2647 if (act
== SMK_RECEIVING
)
2650 port
= ntohs(address
->sin6_port
);
2652 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2653 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2655 object
= spp
->smk_in
;
2656 if (act
== SMK_CONNECTING
)
2657 ssp
->smk_packet
= spp
->smk_out
;
2662 return smk_ipv6_check(skp
, object
, address
, act
);
2664 #endif /* SMACK_IPV6_PORT_LABELING */
2667 * smack_inode_setsecurity - set smack xattrs
2668 * @inode: the object
2669 * @name: attribute name
2670 * @value: attribute value
2671 * @size: size of the attribute
2674 * Sets the named attribute in the appropriate blob
2676 * Returns 0 on success, or an error code
2678 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2679 const void *value
, size_t size
, int flags
)
2681 struct smack_known
*skp
;
2682 struct inode_smack
*nsp
= smack_inode(inode
);
2683 struct socket_smack
*ssp
;
2684 struct socket
*sock
;
2687 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2690 skp
= smk_import_entry(value
, size
);
2692 return PTR_ERR(skp
);
2694 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2695 nsp
->smk_inode
= skp
;
2696 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2700 * The rest of the Smack xattrs are only on sockets.
2702 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2705 sock
= SOCKET_I(inode
);
2706 if (sock
== NULL
|| sock
->sk
== NULL
)
2709 ssp
= sock
->sk
->sk_security
;
2711 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2713 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2715 if (sock
->sk
->sk_family
== PF_INET
) {
2716 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2719 "Smack: \"%s\" netlbl error %d.\n",
2725 #ifdef SMACK_IPV6_PORT_LABELING
2726 if (sock
->sk
->sk_family
== PF_INET6
)
2727 smk_ipv6_port_label(sock
, NULL
);
2734 * smack_socket_post_create - finish socket setup
2736 * @family: protocol family
2741 * Sets the netlabel information on the socket
2743 * Returns 0 on success, and error code otherwise
2745 static int smack_socket_post_create(struct socket
*sock
, int family
,
2746 int type
, int protocol
, int kern
)
2748 struct socket_smack
*ssp
;
2750 if (sock
->sk
== NULL
)
2754 * Sockets created by kernel threads receive web label.
2756 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2757 ssp
= sock
->sk
->sk_security
;
2758 ssp
->smk_in
= &smack_known_web
;
2759 ssp
->smk_out
= &smack_known_web
;
2762 if (family
!= PF_INET
)
2765 * Set the outbound netlbl.
2767 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2771 * smack_socket_socketpair - create socket pair
2772 * @socka: one socket
2773 * @sockb: another socket
2775 * Cross reference the peer labels for SO_PEERSEC
2779 static int smack_socket_socketpair(struct socket
*socka
,
2780 struct socket
*sockb
)
2782 struct socket_smack
*asp
= socka
->sk
->sk_security
;
2783 struct socket_smack
*bsp
= sockb
->sk
->sk_security
;
2785 asp
->smk_packet
= bsp
->smk_out
;
2786 bsp
->smk_packet
= asp
->smk_out
;
2791 #ifdef SMACK_IPV6_PORT_LABELING
2793 * smack_socket_bind - record port binding information.
2795 * @address: the port address
2796 * @addrlen: size of the address
2798 * Records the label bound to a port.
2800 * Returns 0 on success, and error code otherwise
2802 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2805 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
) {
2806 if (addrlen
< SIN6_LEN_RFC2133
||
2807 address
->sa_family
!= AF_INET6
)
2809 smk_ipv6_port_label(sock
, address
);
2813 #endif /* SMACK_IPV6_PORT_LABELING */
2816 * smack_socket_connect - connect access check
2818 * @sap: the other end
2819 * @addrlen: size of sap
2821 * Verifies that a connection may be possible
2823 * Returns 0 on success, and error code otherwise
2825 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2830 if (sock
->sk
== NULL
)
2832 if (sock
->sk
->sk_family
!= PF_INET
&&
2833 (!IS_ENABLED(CONFIG_IPV6
) || sock
->sk
->sk_family
!= PF_INET6
))
2835 if (addrlen
< offsetofend(struct sockaddr
, sa_family
))
2837 if (IS_ENABLED(CONFIG_IPV6
) && sap
->sa_family
== AF_INET6
) {
2838 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2839 #ifdef SMACK_IPV6_SECMARK_LABELING
2840 struct smack_known
*rsp
;
2843 if (addrlen
< SIN6_LEN_RFC2133
)
2845 #ifdef SMACK_IPV6_SECMARK_LABELING
2846 rsp
= smack_ipv6host_label(sip
);
2848 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2850 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2854 #ifdef SMACK_IPV6_PORT_LABELING
2855 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2859 if (sap
->sa_family
!= AF_INET
|| addrlen
< sizeof(struct sockaddr_in
))
2861 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2866 * smack_flags_to_may - convert S_ to MAY_ values
2867 * @flags: the S_ value
2869 * Returns the equivalent MAY_ value
2871 static int smack_flags_to_may(int flags
)
2875 if (flags
& S_IRUGO
)
2877 if (flags
& S_IWUGO
)
2879 if (flags
& S_IXUGO
)
2886 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2891 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2893 struct smack_known
**blob
= smack_msg_msg(msg
);
2895 *blob
= smk_of_current();
2900 * smack_of_ipc - the smack pointer for the ipc
2903 * Returns a pointer to the smack value
2905 static struct smack_known
*smack_of_ipc(struct kern_ipc_perm
*isp
)
2907 struct smack_known
**blob
= smack_ipc(isp
);
2913 * smack_ipc_alloc_security - Set the security blob for ipc
2918 static int smack_ipc_alloc_security(struct kern_ipc_perm
*isp
)
2920 struct smack_known
**blob
= smack_ipc(isp
);
2922 *blob
= smk_of_current();
2927 * smk_curacc_shm : check if current has access on shm
2929 * @access : access requested
2931 * Returns 0 if current has the requested access, error code otherwise
2933 static int smk_curacc_shm(struct kern_ipc_perm
*isp
, int access
)
2935 struct smack_known
*ssp
= smack_of_ipc(isp
);
2936 struct smk_audit_info ad
;
2940 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2941 ad
.a
.u
.ipc_id
= isp
->id
;
2943 rc
= smk_curacc(ssp
, access
, &ad
);
2944 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2949 * smack_shm_associate - Smack access check for shm
2951 * @shmflg: access requested
2953 * Returns 0 if current has the requested access, error code otherwise
2955 static int smack_shm_associate(struct kern_ipc_perm
*isp
, int shmflg
)
2959 may
= smack_flags_to_may(shmflg
);
2960 return smk_curacc_shm(isp
, may
);
2964 * smack_shm_shmctl - Smack access check for shm
2966 * @cmd: what it wants to do
2968 * Returns 0 if current has the requested access, error code otherwise
2970 static int smack_shm_shmctl(struct kern_ipc_perm
*isp
, int cmd
)
2984 may
= MAY_READWRITE
;
2989 * System level information.
2995 return smk_curacc_shm(isp
, may
);
2999 * smack_shm_shmat - Smack access for shmat
3002 * @shmflg: access requested
3004 * Returns 0 if current has the requested access, error code otherwise
3006 static int smack_shm_shmat(struct kern_ipc_perm
*isp
, char __user
*shmaddr
,
3011 may
= smack_flags_to_may(shmflg
);
3012 return smk_curacc_shm(isp
, may
);
3016 * smk_curacc_sem : check if current has access on sem
3018 * @access : access requested
3020 * Returns 0 if current has the requested access, error code otherwise
3022 static int smk_curacc_sem(struct kern_ipc_perm
*isp
, int access
)
3024 struct smack_known
*ssp
= smack_of_ipc(isp
);
3025 struct smk_audit_info ad
;
3029 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3030 ad
.a
.u
.ipc_id
= isp
->id
;
3032 rc
= smk_curacc(ssp
, access
, &ad
);
3033 rc
= smk_bu_current("sem", ssp
, access
, rc
);
3038 * smack_sem_associate - Smack access check for sem
3040 * @semflg: access requested
3042 * Returns 0 if current has the requested access, error code otherwise
3044 static int smack_sem_associate(struct kern_ipc_perm
*isp
, int semflg
)
3048 may
= smack_flags_to_may(semflg
);
3049 return smk_curacc_sem(isp
, may
);
3053 * smack_sem_shmctl - Smack access check for sem
3055 * @cmd: what it wants to do
3057 * Returns 0 if current has the requested access, error code otherwise
3059 static int smack_sem_semctl(struct kern_ipc_perm
*isp
, int cmd
)
3078 may
= MAY_READWRITE
;
3083 * System level information
3090 return smk_curacc_sem(isp
, may
);
3094 * smack_sem_semop - Smack checks of semaphore operations
3100 * Treated as read and write in all cases.
3102 * Returns 0 if access is allowed, error code otherwise
3104 static int smack_sem_semop(struct kern_ipc_perm
*isp
, struct sembuf
*sops
,
3105 unsigned nsops
, int alter
)
3107 return smk_curacc_sem(isp
, MAY_READWRITE
);
3111 * smk_curacc_msq : helper to check if current has access on msq
3113 * @access : access requested
3115 * return 0 if current has access, error otherwise
3117 static int smk_curacc_msq(struct kern_ipc_perm
*isp
, int access
)
3119 struct smack_known
*msp
= smack_of_ipc(isp
);
3120 struct smk_audit_info ad
;
3124 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3125 ad
.a
.u
.ipc_id
= isp
->id
;
3127 rc
= smk_curacc(msp
, access
, &ad
);
3128 rc
= smk_bu_current("msq", msp
, access
, rc
);
3133 * smack_msg_queue_associate - Smack access check for msg_queue
3135 * @msqflg: access requested
3137 * Returns 0 if current has the requested access, error code otherwise
3139 static int smack_msg_queue_associate(struct kern_ipc_perm
*isp
, int msqflg
)
3143 may
= smack_flags_to_may(msqflg
);
3144 return smk_curacc_msq(isp
, may
);
3148 * smack_msg_queue_msgctl - Smack access check for msg_queue
3150 * @cmd: what it wants to do
3152 * Returns 0 if current has the requested access, error code otherwise
3154 static int smack_msg_queue_msgctl(struct kern_ipc_perm
*isp
, int cmd
)
3166 may
= MAY_READWRITE
;
3171 * System level information
3178 return smk_curacc_msq(isp
, may
);
3182 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3185 * @msqflg: access requested
3187 * Returns 0 if current has the requested access, error code otherwise
3189 static int smack_msg_queue_msgsnd(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3194 may
= smack_flags_to_may(msqflg
);
3195 return smk_curacc_msq(isp
, may
);
3199 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3206 * Returns 0 if current has read and write access, error code otherwise
3208 static int smack_msg_queue_msgrcv(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3209 struct task_struct
*target
, long type
, int mode
)
3211 return smk_curacc_msq(isp
, MAY_READWRITE
);
3215 * smack_ipc_permission - Smack access for ipc_permission()
3216 * @ipp: the object permissions
3217 * @flag: access requested
3219 * Returns 0 if current has read and write access, error code otherwise
3221 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3223 struct smack_known
**blob
= smack_ipc(ipp
);
3224 struct smack_known
*iskp
= *blob
;
3225 int may
= smack_flags_to_may(flag
);
3226 struct smk_audit_info ad
;
3230 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3231 ad
.a
.u
.ipc_id
= ipp
->id
;
3233 rc
= smk_curacc(iskp
, may
, &ad
);
3234 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3239 * smack_ipc_getsecid - Extract smack security id
3240 * @ipp: the object permissions
3241 * @secid: where result will be saved
3243 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3245 struct smack_known
**blob
= smack_ipc(ipp
);
3246 struct smack_known
*iskp
= *blob
;
3248 *secid
= iskp
->smk_secid
;
3252 * smack_d_instantiate - Make sure the blob is correct on an inode
3253 * @opt_dentry: dentry where inode will be attached
3254 * @inode: the object
3256 * Set the inode's security blob if it hasn't been done already.
3258 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3260 struct super_block
*sbp
;
3261 struct superblock_smack
*sbsp
;
3262 struct inode_smack
*isp
;
3263 struct smack_known
*skp
;
3264 struct smack_known
*ckp
= smk_of_current();
3265 struct smack_known
*final
;
3266 char trattr
[TRANS_TRUE_SIZE
];
3274 isp
= smack_inode(inode
);
3276 mutex_lock(&isp
->smk_lock
);
3278 * If the inode is already instantiated
3279 * take the quick way out
3281 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3285 sbsp
= sbp
->s_security
;
3287 * We're going to use the superblock default label
3288 * if there's no label on the file.
3290 final
= sbsp
->smk_default
;
3293 * If this is the root inode the superblock
3294 * may be in the process of initialization.
3295 * If that is the case use the root value out
3296 * of the superblock.
3298 if (opt_dentry
->d_parent
== opt_dentry
) {
3299 switch (sbp
->s_magic
) {
3300 case CGROUP_SUPER_MAGIC
:
3301 case CGROUP2_SUPER_MAGIC
:
3303 * The cgroup filesystem is never mounted,
3304 * so there's no opportunity to set the mount
3307 sbsp
->smk_root
= &smack_known_star
;
3308 sbsp
->smk_default
= &smack_known_star
;
3309 isp
->smk_inode
= sbsp
->smk_root
;
3313 * What about shmem/tmpfs anonymous files with dentry
3314 * obtained from d_alloc_pseudo()?
3316 isp
->smk_inode
= smk_of_current();
3319 isp
->smk_inode
= smk_of_current();
3323 * Socket access is controlled by the socket
3324 * structures associated with the task involved.
3326 isp
->smk_inode
= &smack_known_star
;
3329 isp
->smk_inode
= sbsp
->smk_root
;
3332 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3337 * This is pretty hackish.
3338 * Casey says that we shouldn't have to do
3339 * file system specific code, but it does help
3340 * with keeping it simple.
3342 switch (sbp
->s_magic
) {
3344 case CGROUP_SUPER_MAGIC
:
3345 case CGROUP2_SUPER_MAGIC
:
3347 * Casey says that it's a little embarrassing
3348 * that the smack file system doesn't do
3349 * extended attributes.
3351 * Cgroupfs is special
3353 final
= &smack_known_star
;
3355 case DEVPTS_SUPER_MAGIC
:
3357 * devpts seems content with the label of the task.
3358 * Programs that change smack have to treat the
3363 case PROC_SUPER_MAGIC
:
3365 * Casey says procfs appears not to care.
3366 * The superblock default suffices.
3371 * Device labels should come from the filesystem,
3372 * but watch out, because they're volitile,
3373 * getting recreated on every reboot.
3375 final
= &smack_known_star
;
3377 * If a smack value has been set we want to use it,
3378 * but since tmpfs isn't giving us the opportunity
3379 * to set mount options simulate setting the
3380 * superblock default.
3385 * This isn't an understood special case.
3386 * Get the value from the xattr.
3390 * UNIX domain sockets use lower level socket data.
3392 if (S_ISSOCK(inode
->i_mode
)) {
3393 final
= &smack_known_star
;
3397 * No xattr support means, alas, no SMACK label.
3398 * Use the aforeapplied default.
3399 * It would be curious if the label of the task
3400 * does not match that assigned.
3402 if (!(inode
->i_opflags
& IOP_XATTR
))
3405 * Get the dentry for xattr.
3407 dp
= dget(opt_dentry
);
3408 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3409 if (!IS_ERR_OR_NULL(skp
))
3413 * Transmuting directory
3415 if (S_ISDIR(inode
->i_mode
)) {
3417 * If this is a new directory and the label was
3418 * transmuted when the inode was initialized
3419 * set the transmute attribute on the directory
3420 * and mark the inode.
3422 * If there is a transmute attribute on the
3423 * directory mark the inode.
3425 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3426 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3427 rc
= __vfs_setxattr(dp
, inode
,
3428 XATTR_NAME_SMACKTRANSMUTE
,
3429 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3432 rc
= __vfs_getxattr(dp
, inode
,
3433 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3435 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3436 TRANS_TRUE_SIZE
) != 0)
3440 transflag
= SMK_INODE_TRANSMUTE
;
3443 * Don't let the exec or mmap label be "*" or "@".
3445 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3446 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3447 skp
== &smack_known_web
)
3449 isp
->smk_task
= skp
;
3451 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3452 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3453 skp
== &smack_known_web
)
3455 isp
->smk_mmap
= skp
;
3462 isp
->smk_inode
= ckp
;
3464 isp
->smk_inode
= final
;
3466 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3469 mutex_unlock(&isp
->smk_lock
);
3474 * smack_getprocattr - Smack process attribute access
3475 * @p: the object task
3476 * @name: the name of the attribute in /proc/.../attr
3477 * @value: where to put the result
3479 * Places a copy of the task Smack into value
3481 * Returns the length of the smack label or an error code
3483 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3485 struct smack_known
*skp
= smk_of_task_struct(p
);
3489 if (strcmp(name
, "current") != 0)
3492 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3502 * smack_setprocattr - Smack process attribute setting
3503 * @name: the name of the attribute in /proc/.../attr
3504 * @value: the value to set
3505 * @size: the size of the value
3507 * Sets the Smack value of the task. Only setting self
3508 * is permitted and only with privilege
3510 * Returns the length of the smack label or an error code
3512 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3514 struct task_smack
*tsp
= smack_cred(current_cred());
3516 struct smack_known
*skp
;
3517 struct smack_known_list_elem
*sklep
;
3520 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3523 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3526 if (strcmp(name
, "current") != 0)
3529 skp
= smk_import_entry(value
, size
);
3531 return PTR_ERR(skp
);
3534 * No process is ever allowed the web ("@") label
3535 * and the star ("*") label.
3537 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3540 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3542 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3543 if (sklep
->smk_label
== skp
) {
3551 new = prepare_creds();
3555 tsp
= smack_cred(new);
3556 tsp
->smk_task
= skp
;
3558 * process can change its label only once
3560 smk_destroy_label_list(&tsp
->smk_relabel
);
3567 * smack_unix_stream_connect - Smack access on UDS
3569 * @other: the other sock
3572 * Return 0 if a subject with the smack of sock could access
3573 * an object with the smack of other, otherwise an error code
3575 static int smack_unix_stream_connect(struct sock
*sock
,
3576 struct sock
*other
, struct sock
*newsk
)
3578 struct smack_known
*skp
;
3579 struct smack_known
*okp
;
3580 struct socket_smack
*ssp
= sock
->sk_security
;
3581 struct socket_smack
*osp
= other
->sk_security
;
3582 struct socket_smack
*nsp
= newsk
->sk_security
;
3583 struct smk_audit_info ad
;
3586 struct lsm_network_audit net
;
3589 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3593 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3594 smk_ad_setfield_u_net_sk(&ad
, other
);
3596 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3597 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3601 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3602 rc
= smk_bu_note("UDS connect", okp
, skp
,
3608 * Cross reference the peer labels for SO_PEERSEC.
3611 nsp
->smk_packet
= ssp
->smk_out
;
3612 ssp
->smk_packet
= osp
->smk_out
;
3619 * smack_unix_may_send - Smack access on UDS
3621 * @other: the other socket
3623 * Return 0 if a subject with the smack of sock could access
3624 * an object with the smack of other, otherwise an error code
3626 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3628 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3629 struct socket_smack
*osp
= other
->sk
->sk_security
;
3630 struct smk_audit_info ad
;
3634 struct lsm_network_audit net
;
3636 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3637 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3640 if (smack_privileged(CAP_MAC_OVERRIDE
))
3643 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3644 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3649 * smack_socket_sendmsg - Smack check based on destination host
3652 * @size: the size of the message
3654 * Return 0 if the current subject can write to the destination host.
3655 * For IPv4 this is only a question if the destination is a single label host.
3656 * For IPv6 this is a check against the label of the port.
3658 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3661 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3662 #if IS_ENABLED(CONFIG_IPV6)
3663 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3665 #ifdef SMACK_IPV6_SECMARK_LABELING
3666 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3667 struct smack_known
*rsp
;
3672 * Perfectly reasonable for this to be NULL
3677 switch (sock
->sk
->sk_family
) {
3679 if (msg
->msg_namelen
< sizeof(struct sockaddr_in
) ||
3680 sip
->sin_family
!= AF_INET
)
3682 rc
= smack_netlabel_send(sock
->sk
, sip
);
3684 #if IS_ENABLED(CONFIG_IPV6)
3686 if (msg
->msg_namelen
< SIN6_LEN_RFC2133
||
3687 sap
->sin6_family
!= AF_INET6
)
3689 #ifdef SMACK_IPV6_SECMARK_LABELING
3690 rsp
= smack_ipv6host_label(sap
);
3692 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3695 #ifdef SMACK_IPV6_PORT_LABELING
3696 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3698 #endif /* IS_ENABLED(CONFIG_IPV6) */
3705 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3706 * @sap: netlabel secattr
3707 * @ssp: socket security information
3709 * Returns a pointer to a Smack label entry found on the label list.
3711 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3712 struct socket_smack
*ssp
)
3714 struct smack_known
*skp
;
3719 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3721 * Looks like a CIPSO packet.
3722 * If there are flags but no level netlabel isn't
3723 * behaving the way we expect it to.
3725 * Look it up in the label table
3726 * Without guidance regarding the smack value
3727 * for the packet fall back on the network
3731 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3732 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3735 * Compare the catsets. Use the netlbl APIs.
3737 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3738 if ((skp
->smk_netlabel
.flags
&
3739 NETLBL_SECATTR_MLS_CAT
) == 0)
3743 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3744 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3746 kcat
= netlbl_catmap_walk(
3747 skp
->smk_netlabel
.attr
.mls
.cat
,
3749 if (acat
< 0 || kcat
< 0)
3762 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3763 return &smack_known_web
;
3764 return &smack_known_star
;
3766 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3768 * Looks like a fallback, which gives us a secid.
3770 return smack_from_secid(sap
->attr
.secid
);
3772 * Without guidance regarding the smack value
3773 * for the packet fall back on the network
3776 return smack_net_ambient
;
3779 #if IS_ENABLED(CONFIG_IPV6)
3780 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3784 int proto
= -EINVAL
;
3785 struct ipv6hdr _ipv6h
;
3786 struct ipv6hdr
*ip6
;
3788 struct tcphdr _tcph
, *th
;
3789 struct udphdr _udph
, *uh
;
3790 struct dccp_hdr _dccph
, *dh
;
3794 offset
= skb_network_offset(skb
);
3795 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3798 sip
->sin6_addr
= ip6
->saddr
;
3800 nexthdr
= ip6
->nexthdr
;
3801 offset
+= sizeof(_ipv6h
);
3802 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3809 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3811 sip
->sin6_port
= th
->source
;
3814 case IPPROTO_UDPLITE
:
3815 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3817 sip
->sin6_port
= uh
->source
;
3820 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3822 sip
->sin6_port
= dh
->dccph_sport
;
3827 #endif /* CONFIG_IPV6 */
3830 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3834 * Returns 0 if the packet should be delivered, an error code otherwise
3836 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3838 struct netlbl_lsm_secattr secattr
;
3839 struct socket_smack
*ssp
= sk
->sk_security
;
3840 struct smack_known
*skp
= NULL
;
3842 struct smk_audit_info ad
;
3843 u16 family
= sk
->sk_family
;
3845 struct lsm_network_audit net
;
3847 #if IS_ENABLED(CONFIG_IPV6)
3848 struct sockaddr_in6 sadd
;
3851 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3853 #endif /* CONFIG_IPV6 */
3857 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3859 * If there is a secmark use it rather than the CIPSO label.
3860 * If there is no secmark fall back to CIPSO.
3861 * The secmark is assumed to reflect policy better.
3863 if (skb
&& skb
->secmark
!= 0) {
3864 skp
= smack_from_secid(skb
->secmark
);
3867 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3869 * Translate what netlabel gave us.
3871 netlbl_secattr_init(&secattr
);
3873 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3875 skp
= smack_from_secattr(&secattr
, ssp
);
3877 skp
= smack_net_ambient
;
3879 netlbl_secattr_destroy(&secattr
);
3881 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3885 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3886 ad
.a
.u
.net
->family
= family
;
3887 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3888 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3891 * Receiving a packet requires that the other end
3892 * be able to write here. Read access is not required.
3893 * This is the simplist possible security model
3896 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3897 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3900 netlbl_skbuff_err(skb
, family
, rc
, 0);
3902 #if IS_ENABLED(CONFIG_IPV6)
3904 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3905 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_UDPLITE
&&
3906 proto
!= IPPROTO_TCP
&& proto
!= IPPROTO_DCCP
)
3908 #ifdef SMACK_IPV6_SECMARK_LABELING
3909 if (skb
&& skb
->secmark
!= 0)
3910 skp
= smack_from_secid(skb
->secmark
);
3911 else if (smk_ipv6_localhost(&sadd
))
3914 skp
= smack_ipv6host_label(&sadd
);
3916 skp
= smack_net_ambient
;
3920 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3921 ad
.a
.u
.net
->family
= family
;
3922 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3923 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3924 #endif /* CONFIG_AUDIT */
3925 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3926 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3928 #endif /* SMACK_IPV6_SECMARK_LABELING */
3929 #ifdef SMACK_IPV6_PORT_LABELING
3930 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3931 #endif /* SMACK_IPV6_PORT_LABELING */
3933 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
,
3934 ICMPV6_ADM_PROHIBITED
, 0);
3936 #endif /* CONFIG_IPV6 */
3943 * smack_socket_getpeersec_stream - pull in packet label
3945 * @optval: user's destination
3946 * @optlen: size thereof
3949 * returns zero on success, an error code otherwise
3951 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3952 char __user
*optval
,
3953 int __user
*optlen
, unsigned len
)
3955 struct socket_smack
*ssp
;
3960 ssp
= sock
->sk
->sk_security
;
3961 if (ssp
->smk_packet
!= NULL
) {
3962 rcp
= ssp
->smk_packet
->smk_known
;
3963 slen
= strlen(rcp
) + 1;
3968 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3971 if (put_user(slen
, optlen
) != 0)
3979 * smack_socket_getpeersec_dgram - pull in packet label
3980 * @sock: the peer socket
3982 * @secid: pointer to where to put the secid of the packet
3984 * Sets the netlabel socket state on sk from parent
3986 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3987 struct sk_buff
*skb
, u32
*secid
)
3990 struct netlbl_lsm_secattr secattr
;
3991 struct socket_smack
*ssp
= NULL
;
3992 struct smack_known
*skp
;
3993 int family
= PF_UNSPEC
;
3994 u32 s
= 0; /* 0 is the invalid secid */
3998 if (skb
->protocol
== htons(ETH_P_IP
))
4000 #if IS_ENABLED(CONFIG_IPV6)
4001 else if (skb
->protocol
== htons(ETH_P_IPV6
))
4003 #endif /* CONFIG_IPV6 */
4005 if (family
== PF_UNSPEC
&& sock
!= NULL
)
4006 family
= sock
->sk
->sk_family
;
4010 ssp
= sock
->sk
->sk_security
;
4011 s
= ssp
->smk_out
->smk_secid
;
4014 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4020 * Translate what netlabel gave us.
4022 if (sock
!= NULL
&& sock
->sk
!= NULL
)
4023 ssp
= sock
->sk
->sk_security
;
4024 netlbl_secattr_init(&secattr
);
4025 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4027 skp
= smack_from_secattr(&secattr
, ssp
);
4030 netlbl_secattr_destroy(&secattr
);
4033 #ifdef SMACK_IPV6_SECMARK_LABELING
4045 * smack_sock_graft - Initialize a newly created socket with an existing sock
4047 * @parent: parent socket
4049 * Set the smk_{in,out} state of an existing sock based on the process that
4050 * is creating the new socket.
4052 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
4054 struct socket_smack
*ssp
;
4055 struct smack_known
*skp
= smk_of_current();
4058 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
4061 ssp
= sk
->sk_security
;
4064 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4068 * smack_inet_conn_request - Smack access check on connect
4069 * @sk: socket involved
4073 * Returns 0 if a task with the packet label could write to
4074 * the socket, otherwise an error code
4076 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4077 struct request_sock
*req
)
4079 u16 family
= sk
->sk_family
;
4080 struct smack_known
*skp
;
4081 struct socket_smack
*ssp
= sk
->sk_security
;
4082 struct netlbl_lsm_secattr secattr
;
4083 struct sockaddr_in addr
;
4085 struct smack_known
*hskp
;
4087 struct smk_audit_info ad
;
4089 struct lsm_network_audit net
;
4092 #if IS_ENABLED(CONFIG_IPV6)
4093 if (family
== PF_INET6
) {
4095 * Handle mapped IPv4 packets arriving
4096 * via IPv6 sockets. Don't set up netlabel
4097 * processing on IPv6.
4099 if (skb
->protocol
== htons(ETH_P_IP
))
4104 #endif /* CONFIG_IPV6 */
4106 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4108 * If there is a secmark use it rather than the CIPSO label.
4109 * If there is no secmark fall back to CIPSO.
4110 * The secmark is assumed to reflect policy better.
4112 if (skb
&& skb
->secmark
!= 0) {
4113 skp
= smack_from_secid(skb
->secmark
);
4116 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4118 netlbl_secattr_init(&secattr
);
4119 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4121 skp
= smack_from_secattr(&secattr
, ssp
);
4123 skp
= &smack_known_huh
;
4124 netlbl_secattr_destroy(&secattr
);
4126 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4131 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4132 ad
.a
.u
.net
->family
= family
;
4133 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4134 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4137 * Receiving a packet requires that the other end be able to write
4138 * here. Read access is not required.
4140 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4141 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4146 * Save the peer's label in the request_sock so we can later setup
4147 * smk_packet in the child socket so that SO_PEERCRED can report it.
4149 req
->peer_secid
= skp
->smk_secid
;
4152 * We need to decide if we want to label the incoming connection here
4153 * if we do we only need to label the request_sock and the stack will
4154 * propagate the wire-label to the sock when it is created.
4157 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4159 hskp
= smack_ipv4host_label(&addr
);
4163 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4165 netlbl_req_delattr(req
);
4171 * smack_inet_csk_clone - Copy the connection information to the new socket
4172 * @sk: the new socket
4173 * @req: the connection's request_sock
4175 * Transfer the connection's peer label to the newly created socket.
4177 static void smack_inet_csk_clone(struct sock
*sk
,
4178 const struct request_sock
*req
)
4180 struct socket_smack
*ssp
= sk
->sk_security
;
4181 struct smack_known
*skp
;
4183 if (req
->peer_secid
!= 0) {
4184 skp
= smack_from_secid(req
->peer_secid
);
4185 ssp
->smk_packet
= skp
;
4187 ssp
->smk_packet
= NULL
;
4191 * Key management security hooks
4193 * Casey has not tested key support very heavily.
4194 * The permission check is most likely too restrictive.
4195 * If you care about keys please have a look.
4200 * smack_key_alloc - Set the key security blob
4202 * @cred: the credentials to use
4205 * No allocation required
4209 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4210 unsigned long flags
)
4212 struct smack_known
*skp
= smk_of_task(smack_cred(cred
));
4214 key
->security
= skp
;
4219 * smack_key_free - Clear the key security blob
4222 * Clear the blob pointer
4224 static void smack_key_free(struct key
*key
)
4226 key
->security
= NULL
;
4230 * smack_key_permission - Smack access on a key
4231 * @key_ref: gets to the object
4232 * @cred: the credentials to use
4233 * @perm: requested key permissions
4235 * Return 0 if the task has read and write to the object,
4236 * an error code otherwise
4238 static int smack_key_permission(key_ref_t key_ref
,
4239 const struct cred
*cred
, unsigned perm
)
4242 struct smk_audit_info ad
;
4243 struct smack_known
*tkp
= smk_of_task(smack_cred(cred
));
4248 * Validate requested permissions
4250 if (perm
& ~KEY_NEED_ALL
)
4253 keyp
= key_ref_to_ptr(key_ref
);
4257 * If the key hasn't been initialized give it access so that
4260 if (keyp
->security
== NULL
)
4263 * This should not occur
4268 if (smack_privileged_cred(CAP_MAC_OVERRIDE
, cred
))
4272 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4273 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4274 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4276 if (perm
& (KEY_NEED_READ
| KEY_NEED_SEARCH
| KEY_NEED_VIEW
))
4277 request
|= MAY_READ
;
4278 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4279 request
|= MAY_WRITE
;
4280 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
4281 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
4286 * smack_key_getsecurity - Smack label tagging the key
4287 * @key points to the key to be queried
4288 * @_buffer points to a pointer that should be set to point to the
4289 * resulting string (if no label or an error occurs).
4290 * Return the length of the string (including terminating NUL) or -ve if
4292 * May also return 0 (and a NULL buffer pointer) if there is no label.
4294 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4296 struct smack_known
*skp
= key
->security
;
4300 if (key
->security
== NULL
) {
4305 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4308 length
= strlen(copy
) + 1;
4314 #endif /* CONFIG_KEYS */
4319 * Audit requires a unique representation of each Smack specific
4320 * rule. This unique representation is used to distinguish the
4321 * object to be audited from remaining kernel objects and also
4322 * works as a glue between the audit hooks.
4324 * Since repository entries are added but never deleted, we'll use
4325 * the smack_known label address related to the given audit rule as
4326 * the needed unique representation. This also better fits the smack
4327 * model where nearly everything is a label.
4332 * smack_audit_rule_init - Initialize a smack audit rule
4333 * @field: audit rule fields given from user-space (audit.h)
4334 * @op: required testing operator (=, !=, >, <, ...)
4335 * @rulestr: smack label to be audited
4336 * @vrule: pointer to save our own audit rule representation
4338 * Prepare to audit cases where (@field @op @rulestr) is true.
4339 * The label to be audited is created if necessay.
4341 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4343 struct smack_known
*skp
;
4344 char **rule
= (char **)vrule
;
4347 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4350 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4353 skp
= smk_import_entry(rulestr
, 0);
4355 return PTR_ERR(skp
);
4357 *rule
= skp
->smk_known
;
4363 * smack_audit_rule_known - Distinguish Smack audit rules
4364 * @krule: rule of interest, in Audit kernel representation format
4366 * This is used to filter Smack rules from remaining Audit ones.
4367 * If it's proved that this rule belongs to us, the
4368 * audit_rule_match hook will be called to do the final judgement.
4370 static int smack_audit_rule_known(struct audit_krule
*krule
)
4372 struct audit_field
*f
;
4375 for (i
= 0; i
< krule
->field_count
; i
++) {
4376 f
= &krule
->fields
[i
];
4378 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4386 * smack_audit_rule_match - Audit given object ?
4387 * @secid: security id for identifying the object to test
4388 * @field: audit rule flags given from user-space
4389 * @op: required testing operator
4390 * @vrule: smack internal rule presentation
4392 * The core Audit hook. It's used to take the decision of
4393 * whether to audit or not to audit a given object.
4395 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
)
4397 struct smack_known
*skp
;
4400 if (unlikely(!rule
)) {
4401 WARN_ONCE(1, "Smack: missing rule\n");
4405 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4408 skp
= smack_from_secid(secid
);
4411 * No need to do string comparisons. If a match occurs,
4412 * both pointers will point to the same smack_known
4415 if (op
== Audit_equal
)
4416 return (rule
== skp
->smk_known
);
4417 if (op
== Audit_not_equal
)
4418 return (rule
!= skp
->smk_known
);
4424 * There is no need for a smack_audit_rule_free hook.
4425 * No memory was allocated.
4428 #endif /* CONFIG_AUDIT */
4431 * smack_ismaclabel - check if xattr @name references a smack MAC label
4432 * @name: Full xattr name to check.
4434 static int smack_ismaclabel(const char *name
)
4436 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4441 * smack_secid_to_secctx - return the smack label for a secid
4442 * @secid: incoming integer
4443 * @secdata: destination
4444 * @seclen: how long it is
4446 * Exists for networking code.
4448 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4450 struct smack_known
*skp
= smack_from_secid(secid
);
4453 *secdata
= skp
->smk_known
;
4454 *seclen
= strlen(skp
->smk_known
);
4459 * smack_secctx_to_secid - return the secid for a smack label
4460 * @secdata: smack label
4461 * @seclen: how long result is
4462 * @secid: outgoing integer
4464 * Exists for audit and networking code.
4466 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4468 struct smack_known
*skp
= smk_find_entry(secdata
);
4471 *secid
= skp
->smk_secid
;
4478 * There used to be a smack_release_secctx hook
4479 * that did nothing back when hooks were in a vector.
4480 * Now that there's a list such a hook adds cost.
4483 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4485 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4488 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4490 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4493 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4495 struct smack_known
*skp
= smk_of_inode(inode
);
4497 *ctx
= skp
->smk_known
;
4498 *ctxlen
= strlen(skp
->smk_known
);
4502 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4505 struct task_smack
*tsp
;
4506 struct smack_known
*skp
;
4507 struct inode_smack
*isp
;
4508 struct cred
*new_creds
= *new;
4510 if (new_creds
== NULL
) {
4511 new_creds
= prepare_creds();
4512 if (new_creds
== NULL
)
4516 tsp
= smack_cred(new_creds
);
4519 * Get label from overlay inode and set it in create_sid
4521 isp
= smack_inode(d_inode(dentry
->d_parent
));
4522 skp
= isp
->smk_inode
;
4523 tsp
->smk_task
= skp
;
4528 static int smack_inode_copy_up_xattr(const char *name
)
4531 * Return 1 if this is the smack access Smack attribute.
4533 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4539 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4541 const struct cred
*old
,
4544 struct task_smack
*otsp
= smack_cred(old
);
4545 struct task_smack
*ntsp
= smack_cred(new);
4546 struct inode_smack
*isp
;
4550 * Use the process credential unless all of
4551 * the transmuting criteria are met
4553 ntsp
->smk_task
= otsp
->smk_task
;
4556 * the attribute of the containing directory
4558 isp
= smack_inode(d_inode(dentry
->d_parent
));
4560 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4562 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4563 isp
->smk_inode
->smk_known
,
4564 &otsp
->smk_task
->smk_rules
);
4568 * If the directory is transmuting and the rule
4569 * providing access is transmuting use the containing
4570 * directory label instead of the process label.
4572 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4573 ntsp
->smk_task
= isp
->smk_inode
;
4578 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init
= {
4579 .lbs_cred
= sizeof(struct task_smack
),
4580 .lbs_file
= sizeof(struct smack_known
*),
4581 .lbs_inode
= sizeof(struct inode_smack
),
4582 .lbs_ipc
= sizeof(struct smack_known
*),
4583 .lbs_msg_msg
= sizeof(struct smack_known
*),
4586 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4587 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4588 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4589 LSM_HOOK_INIT(syslog
, smack_syslog
),
4591 LSM_HOOK_INIT(fs_context_dup
, smack_fs_context_dup
),
4592 LSM_HOOK_INIT(fs_context_parse_param
, smack_fs_context_parse_param
),
4594 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4595 LSM_HOOK_INIT(sb_free_security
, smack_sb_free_security
),
4596 LSM_HOOK_INIT(sb_free_mnt_opts
, smack_free_mnt_opts
),
4597 LSM_HOOK_INIT(sb_eat_lsm_opts
, smack_sb_eat_lsm_opts
),
4598 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4599 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4601 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4603 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4604 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4605 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4606 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4607 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4608 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4609 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4610 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4611 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4612 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4613 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4614 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4615 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4616 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4617 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4618 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4619 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4621 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4622 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4623 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4624 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4625 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4626 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4627 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4628 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4629 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4631 LSM_HOOK_INIT(file_open
, smack_file_open
),
4633 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4634 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4635 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4636 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4637 LSM_HOOK_INIT(cred_getsecid
, smack_cred_getsecid
),
4638 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4639 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4640 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4641 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4642 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4643 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4644 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4645 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4646 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4647 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4648 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4649 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4650 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4651 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4653 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4654 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4656 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4658 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_ipc_alloc_security
),
4659 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4660 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4661 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4662 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4664 LSM_HOOK_INIT(shm_alloc_security
, smack_ipc_alloc_security
),
4665 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4666 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4667 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4669 LSM_HOOK_INIT(sem_alloc_security
, smack_ipc_alloc_security
),
4670 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4671 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4672 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4674 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4676 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4677 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4679 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4680 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4682 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4683 LSM_HOOK_INIT(socket_socketpair
, smack_socket_socketpair
),
4684 #ifdef SMACK_IPV6_PORT_LABELING
4685 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4687 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4688 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4689 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4690 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4691 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4692 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4693 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4694 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4695 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4696 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4698 /* key management security hooks */
4700 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4701 LSM_HOOK_INIT(key_free
, smack_key_free
),
4702 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4703 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4704 #endif /* CONFIG_KEYS */
4708 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4709 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4710 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4711 #endif /* CONFIG_AUDIT */
4713 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4714 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4715 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4716 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4717 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4718 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4719 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4720 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4721 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4725 static __init
void init_smack_known_list(void)
4728 * Initialize rule list locks
4730 mutex_init(&smack_known_huh
.smk_rules_lock
);
4731 mutex_init(&smack_known_hat
.smk_rules_lock
);
4732 mutex_init(&smack_known_floor
.smk_rules_lock
);
4733 mutex_init(&smack_known_star
.smk_rules_lock
);
4734 mutex_init(&smack_known_web
.smk_rules_lock
);
4736 * Initialize rule lists
4738 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4739 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4740 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4741 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4742 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4744 * Create the known labels list
4746 smk_insert_entry(&smack_known_huh
);
4747 smk_insert_entry(&smack_known_hat
);
4748 smk_insert_entry(&smack_known_star
);
4749 smk_insert_entry(&smack_known_floor
);
4750 smk_insert_entry(&smack_known_web
);
4754 * smack_init - initialize the smack system
4756 * Returns 0 on success, -ENOMEM is there's no memory
4758 static __init
int smack_init(void)
4760 struct cred
*cred
= (struct cred
*) current
->cred
;
4761 struct task_smack
*tsp
;
4763 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4764 if (!smack_inode_cache
)
4767 smack_rule_cache
= KMEM_CACHE(smack_rule
, 0);
4768 if (!smack_rule_cache
) {
4769 kmem_cache_destroy(smack_inode_cache
);
4774 * Set the security state for the initial task.
4776 tsp
= smack_cred(cred
);
4777 init_task_smack(tsp
, &smack_known_floor
, &smack_known_floor
);
4782 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4785 pr_info("Smack: Initializing.\n");
4786 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4787 pr_info("Smack: Netfilter enabled.\n");
4789 #ifdef SMACK_IPV6_PORT_LABELING
4790 pr_info("Smack: IPv6 port labeling enabled.\n");
4792 #ifdef SMACK_IPV6_SECMARK_LABELING
4793 pr_info("Smack: IPv6 Netfilter enabled.\n");
4796 /* initialize the smack_known_list */
4797 init_smack_known_list();
4803 * Smack requires early initialization in order to label
4804 * all processes and objects when they are created.
4806 DEFINE_LSM(smack
) = {
4808 .flags
= LSM_FLAG_LEGACY_MAJOR
| LSM_FLAG_EXCLUSIVE
,
4809 .blobs
= &smack_blob_sizes
,