2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
44 #include <linux/parser.h>
47 #define TRANS_TRUE "TRUE"
48 #define TRANS_TRUE_SIZE 4
50 #define SMK_CONNECTING 0
51 #define SMK_RECEIVING 1
54 #ifdef SMACK_IPV6_PORT_LABELING
55 DEFINE_MUTEX(smack_ipv6_lock
);
56 static LIST_HEAD(smk_ipv6_port_list
);
58 static struct kmem_cache
*smack_inode_cache
;
61 static const match_table_t smk_mount_tokens
= {
62 {Opt_fsdefault
, SMK_FSDEFAULT
"%s"},
63 {Opt_fsfloor
, SMK_FSFLOOR
"%s"},
64 {Opt_fshat
, SMK_FSHAT
"%s"},
65 {Opt_fsroot
, SMK_FSROOT
"%s"},
66 {Opt_fstransmute
, SMK_FSTRANS
"%s"},
70 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
71 static char *smk_bu_mess
[] = {
72 "Bringup Error", /* Unused */
73 "Bringup", /* SMACK_BRINGUP_ALLOW */
74 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
75 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
78 static void smk_bu_mode(int mode
, char *s
)
88 if (mode
& MAY_APPEND
)
90 if (mode
& MAY_TRANSMUTE
)
100 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
101 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
102 struct smack_known
*oskp
, int mode
, int rc
)
104 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
108 if (rc
> SMACK_UNCONFINED_OBJECT
)
111 smk_bu_mode(mode
, acc
);
112 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
113 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
117 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
120 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
121 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
124 struct task_smack
*tsp
= smack_cred(current_cred());
125 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
129 if (rc
> SMACK_UNCONFINED_OBJECT
)
132 smk_bu_mode(mode
, acc
);
133 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
134 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
135 acc
, current
->comm
, note
);
139 #define smk_bu_current(note, oskp, mode, RC) (RC)
142 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
143 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
145 struct task_smack
*tsp
= smack_cred(current_cred());
146 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
147 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
151 if (rc
> SMACK_UNCONFINED_OBJECT
)
154 smk_bu_mode(mode
, acc
);
155 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
156 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
157 current
->comm
, otp
->comm
);
161 #define smk_bu_task(otp, mode, RC) (RC)
164 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
165 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
167 struct task_smack
*tsp
= smack_cred(current_cred());
168 struct inode_smack
*isp
= smack_inode(inode
);
169 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
171 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
172 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
173 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
177 if (rc
> SMACK_UNCONFINED_OBJECT
)
179 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
180 (mode
& (MAY_WRITE
| MAY_APPEND
)))
181 isp
->smk_flags
|= SMK_INODE_IMPURE
;
183 smk_bu_mode(mode
, acc
);
185 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
186 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
187 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
191 #define smk_bu_inode(inode, mode, RC) (RC)
194 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
195 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
197 struct task_smack
*tsp
= smack_cred(current_cred());
198 struct smack_known
*sskp
= tsp
->smk_task
;
199 struct inode
*inode
= file_inode(file
);
200 struct inode_smack
*isp
= smack_inode(inode
);
201 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
203 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
204 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
205 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
209 if (rc
> SMACK_UNCONFINED_OBJECT
)
212 smk_bu_mode(mode
, acc
);
213 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
214 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
215 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
220 #define smk_bu_file(file, mode, RC) (RC)
223 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
224 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
227 struct task_smack
*tsp
= smack_cred(cred
);
228 struct smack_known
*sskp
= tsp
->smk_task
;
229 struct inode
*inode
= file_inode(file
);
230 struct inode_smack
*isp
= smack_inode(inode
);
231 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
233 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
234 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
235 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
239 if (rc
> SMACK_UNCONFINED_OBJECT
)
242 smk_bu_mode(mode
, acc
);
243 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
244 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
245 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
250 #define smk_bu_credfile(cred, file, mode, RC) (RC)
254 * smk_fetch - Fetch the smack label from a file.
255 * @name: type of the label (attribute)
256 * @ip: a pointer to the inode
257 * @dp: a pointer to the dentry
259 * Returns a pointer to the master list entry for the Smack label,
260 * NULL if there was no label to fetch, or an error code.
262 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
267 struct smack_known
*skp
= NULL
;
269 if (!(ip
->i_opflags
& IOP_XATTR
))
270 return ERR_PTR(-EOPNOTSUPP
);
272 buffer
= kzalloc(SMK_LONGLABEL
, GFP_NOFS
);
274 return ERR_PTR(-ENOMEM
);
276 rc
= __vfs_getxattr(dp
, ip
, name
, buffer
, SMK_LONGLABEL
);
282 skp
= smk_import_entry(buffer
, rc
);
290 * init_inode_smack - initialize an inode security blob
291 * @isp: the blob to initialize
292 * @skp: a pointer to the Smack label entry to use in the blob
295 static void init_inode_smack(struct inode
*inode
, struct smack_known
*skp
)
297 struct inode_smack
*isp
= smack_inode(inode
);
299 isp
->smk_inode
= skp
;
301 mutex_init(&isp
->smk_lock
);
305 * init_task_smack - initialize a task security blob
306 * @tsp: blob to initialize
307 * @task: a pointer to the Smack label for the running task
308 * @forked: a pointer to the Smack label for the forked task
311 static void init_task_smack(struct task_smack
*tsp
, struct smack_known
*task
,
312 struct smack_known
*forked
)
314 tsp
->smk_task
= task
;
315 tsp
->smk_forked
= forked
;
316 INIT_LIST_HEAD(&tsp
->smk_rules
);
317 INIT_LIST_HEAD(&tsp
->smk_relabel
);
318 mutex_init(&tsp
->smk_rules_lock
);
322 * smk_copy_rules - copy a rule set
323 * @nhead: new rules header pointer
324 * @ohead: old rules header pointer
325 * @gfp: type of the memory for the allocation
327 * Returns 0 on success, -ENOMEM on error
329 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
332 struct smack_rule
*nrp
;
333 struct smack_rule
*orp
;
336 list_for_each_entry_rcu(orp
, ohead
, list
) {
337 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
343 list_add_rcu(&nrp
->list
, nhead
);
349 * smk_copy_relabel - copy smk_relabel labels list
350 * @nhead: new rules header pointer
351 * @ohead: old rules header pointer
352 * @gfp: type of the memory for the allocation
354 * Returns 0 on success, -ENOMEM on error
356 static int smk_copy_relabel(struct list_head
*nhead
, struct list_head
*ohead
,
359 struct smack_known_list_elem
*nklep
;
360 struct smack_known_list_elem
*oklep
;
362 list_for_each_entry(oklep
, ohead
, list
) {
363 nklep
= kzalloc(sizeof(struct smack_known_list_elem
), gfp
);
365 smk_destroy_label_list(nhead
);
368 nklep
->smk_label
= oklep
->smk_label
;
369 list_add(&nklep
->list
, nhead
);
376 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
377 * @mode - input mode in form of PTRACE_MODE_*
379 * Returns a converted MAY_* mode usable by smack rules
381 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
383 if (mode
& PTRACE_MODE_ATTACH
)
384 return MAY_READWRITE
;
385 if (mode
& PTRACE_MODE_READ
)
392 * smk_ptrace_rule_check - helper for ptrace access
393 * @tracer: tracer process
394 * @tracee_known: label entry of the process that's about to be traced
395 * @mode: ptrace attachment mode (PTRACE_MODE_*)
396 * @func: name of the function that called us, used for audit
398 * Returns 0 on access granted, -error on error
400 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
401 struct smack_known
*tracee_known
,
402 unsigned int mode
, const char *func
)
405 struct smk_audit_info ad
, *saip
= NULL
;
406 struct task_smack
*tsp
;
407 struct smack_known
*tracer_known
;
409 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
410 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
411 smk_ad_setfield_u_tsk(&ad
, tracer
);
416 tsp
= smack_cred(__task_cred(tracer
));
417 tracer_known
= smk_of_task(tsp
);
419 if ((mode
& PTRACE_MODE_ATTACH
) &&
420 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
421 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
422 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
424 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
426 else if (capable(CAP_SYS_PTRACE
))
432 smack_log(tracer_known
->smk_known
,
433 tracee_known
->smk_known
,
440 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
441 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
449 * We he, that is fun!
453 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
454 * @ctp: child task pointer
455 * @mode: ptrace attachment mode (PTRACE_MODE_*)
457 * Returns 0 if access is OK, an error code otherwise
459 * Do the capability checks.
461 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
463 struct smack_known
*skp
;
465 skp
= smk_of_task_struct(ctp
);
467 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
471 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
472 * @ptp: parent task pointer
474 * Returns 0 if access is OK, an error code otherwise
476 * Do the capability checks, and require PTRACE_MODE_ATTACH.
478 static int smack_ptrace_traceme(struct task_struct
*ptp
)
481 struct smack_known
*skp
;
483 skp
= smk_of_task(smack_cred(current_cred()));
485 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
490 * smack_syslog - Smack approval on syslog
491 * @type: message type
493 * Returns 0 on success, error code otherwise.
495 static int smack_syslog(int typefrom_file
)
498 struct smack_known
*skp
= smk_of_current();
500 if (smack_privileged(CAP_MAC_OVERRIDE
))
503 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
515 * smack_sb_alloc_security - allocate a superblock blob
516 * @sb: the superblock getting the blob
518 * Returns 0 on success or -ENOMEM on error.
520 static int smack_sb_alloc_security(struct super_block
*sb
)
522 struct superblock_smack
*sbsp
= smack_superblock(sb
);
524 sbsp
->smk_root
= &smack_known_floor
;
525 sbsp
->smk_default
= &smack_known_floor
;
526 sbsp
->smk_floor
= &smack_known_floor
;
527 sbsp
->smk_hat
= &smack_known_hat
;
529 * SMK_SB_INITIALIZED will be zero from kzalloc.
536 * smack_sb_copy_data - copy mount options data for processing
537 * @orig: where to start
538 * @smackopts: mount options string
540 * Returns 0 on success or -ENOMEM on error.
542 * Copy the Smack specific mount options out of the mount
545 static int smack_sb_copy_data(char *orig
, char *smackopts
)
547 char *cp
, *commap
, *otheropts
, *dp
;
549 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
550 if (otheropts
== NULL
)
553 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
554 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
556 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
558 else if (strstr(cp
, SMK_FSHAT
) == cp
)
560 else if (strstr(cp
, SMK_FSROOT
) == cp
)
562 else if (strstr(cp
, SMK_FSTRANS
) == cp
)
567 commap
= strchr(cp
, ',');
576 strcpy(orig
, otheropts
);
577 free_page((unsigned long)otheropts
);
583 * smack_parse_opts_str - parse Smack specific mount options
584 * @options: mount options string
585 * @opts: where to store converted mount opts
587 * Returns 0 on success or -ENOMEM on error.
589 * converts Smack specific mount options to generic security option format
591 static int smack_parse_opts_str(char *options
,
592 struct security_mnt_opts
*opts
)
595 char *fsdefault
= NULL
;
596 char *fsfloor
= NULL
;
599 char *fstransmute
= NULL
;
601 int num_mnt_opts
= 0;
604 opts
->num_mnt_opts
= 0;
609 while ((p
= strsep(&options
, ",")) != NULL
) {
610 substring_t args
[MAX_OPT_ARGS
];
615 token
= match_token(p
, smk_mount_tokens
, args
);
621 fsdefault
= match_strdup(&args
[0]);
628 fsfloor
= match_strdup(&args
[0]);
635 fshat
= match_strdup(&args
[0]);
642 fsroot
= match_strdup(&args
[0]);
646 case Opt_fstransmute
:
649 fstransmute
= match_strdup(&args
[0]);
655 pr_warn("Smack: unknown mount option\n");
660 opts
->mnt_opts
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(char *), GFP_KERNEL
);
664 opts
->mnt_opts_flags
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(int),
666 if (!opts
->mnt_opts_flags
)
670 opts
->mnt_opts
[num_mnt_opts
] = fsdefault
;
671 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSDEFAULT_MNT
;
674 opts
->mnt_opts
[num_mnt_opts
] = fsfloor
;
675 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSFLOOR_MNT
;
678 opts
->mnt_opts
[num_mnt_opts
] = fshat
;
679 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSHAT_MNT
;
682 opts
->mnt_opts
[num_mnt_opts
] = fsroot
;
683 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSROOT_MNT
;
686 opts
->mnt_opts
[num_mnt_opts
] = fstransmute
;
687 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSTRANS_MNT
;
690 opts
->num_mnt_opts
= num_mnt_opts
;
695 pr_warn("Smack: duplicate mount options\n");
707 * smack_set_mnt_opts - set Smack specific mount options
708 * @sb: the file system superblock
709 * @opts: Smack mount options
710 * @kern_flags: mount option from kernel space or user space
711 * @set_kern_flags: where to store converted mount opts
713 * Returns 0 on success, an error code on failure
715 * Allow filesystems with binary mount data to explicitly set Smack mount
718 static int smack_set_mnt_opts(struct super_block
*sb
,
719 struct security_mnt_opts
*opts
,
720 unsigned long kern_flags
,
721 unsigned long *set_kern_flags
)
723 struct dentry
*root
= sb
->s_root
;
724 struct inode
*inode
= d_backing_inode(root
);
725 struct superblock_smack
*sp
= smack_superblock(sb
);
726 struct inode_smack
*isp
;
727 struct smack_known
*skp
;
729 int num_opts
= opts
->num_mnt_opts
;
732 if (sp
->smk_flags
& SMK_SB_INITIALIZED
)
735 if (!smack_privileged(CAP_MAC_ADMIN
)) {
737 * Unprivileged mounts don't get to specify Smack values.
742 * Unprivileged mounts get root and default from the caller.
744 skp
= smk_of_current();
746 sp
->smk_default
= skp
;
748 * For a handful of fs types with no user-controlled
749 * backing store it's okay to trust security labels
750 * in the filesystem. The rest are untrusted.
752 if (sb
->s_user_ns
!= &init_user_ns
&&
753 sb
->s_magic
!= SYSFS_MAGIC
&& sb
->s_magic
!= TMPFS_MAGIC
&&
754 sb
->s_magic
!= RAMFS_MAGIC
) {
756 sp
->smk_flags
|= SMK_SB_UNTRUSTED
;
760 sp
->smk_flags
|= SMK_SB_INITIALIZED
;
762 for (i
= 0; i
< num_opts
; i
++) {
763 switch (opts
->mnt_opts_flags
[i
]) {
765 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
768 sp
->smk_default
= skp
;
771 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
777 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
783 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
789 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
801 * Initialize the root inode.
803 lsm_early_inode(inode
);
804 init_inode_smack(inode
, sp
->smk_root
);
807 isp
= smack_inode(inode
);
808 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
815 * smack_sb_kern_mount - Smack specific mount processing
816 * @sb: the file system superblock
817 * @flags: the mount flags
818 * @data: the smack mount options
820 * Returns 0 on success, an error code on failure
822 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
825 char *options
= data
;
826 struct security_mnt_opts opts
;
828 security_init_mnt_opts(&opts
);
833 rc
= smack_parse_opts_str(options
, &opts
);
838 rc
= smack_set_mnt_opts(sb
, &opts
, 0, NULL
);
841 security_free_mnt_opts(&opts
);
846 * smack_sb_statfs - Smack check on statfs
847 * @dentry: identifies the file system in question
849 * Returns 0 if current can read the floor of the filesystem,
850 * and error code otherwise
852 static int smack_sb_statfs(struct dentry
*dentry
)
854 struct superblock_smack
*sbp
= smack_superblock(dentry
->d_sb
);
856 struct smk_audit_info ad
;
858 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
859 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
861 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
862 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
871 * smack_bprm_set_creds - set creds for exec
872 * @bprm: the exec information
874 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
876 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
878 struct inode
*inode
= file_inode(bprm
->file
);
879 struct task_smack
*bsp
= smack_cred(bprm
->cred
);
880 struct inode_smack
*isp
;
881 struct superblock_smack
*sbsp
;
884 if (bprm
->called_set_creds
)
887 isp
= smack_inode(inode
);
888 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
891 sbsp
= smack_superblock(inode
->i_sb
);
892 if ((sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) &&
893 isp
->smk_task
!= sbsp
->smk_root
)
896 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
897 struct task_struct
*tracer
;
901 tracer
= ptrace_parent(current
);
902 if (likely(tracer
!= NULL
))
903 rc
= smk_ptrace_rule_check(tracer
,
912 if (bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
)
915 bsp
->smk_task
= isp
->smk_task
;
916 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
918 /* Decide if this is a secure exec. */
919 if (bsp
->smk_task
!= bsp
->smk_forked
)
920 bprm
->secureexec
= 1;
930 * smack_inode_alloc_security - allocate an inode blob
931 * @inode: the inode in need of a blob
933 * Returns 0 if it gets a blob, -ENOMEM otherwise
935 static int smack_inode_alloc_security(struct inode
*inode
)
937 struct smack_known
*skp
= smk_of_current();
939 init_inode_smack(inode
, skp
);
944 * smack_inode_init_security - copy out the smack from an inode
945 * @inode: the newly created inode
946 * @dir: containing directory object
948 * @name: where to put the attribute name
949 * @value: where to put the attribute value
950 * @len: where to put the length of the attribute
952 * Returns 0 if it all works out, -ENOMEM if there's no memory
954 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
955 const struct qstr
*qstr
, const char **name
,
956 void **value
, size_t *len
)
958 struct inode_smack
*issp
= smack_inode(inode
);
959 struct smack_known
*skp
= smk_of_current();
960 struct smack_known
*isp
= smk_of_inode(inode
);
961 struct smack_known
*dsp
= smk_of_inode(dir
);
965 *name
= XATTR_SMACK_SUFFIX
;
969 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
974 * If the access rule allows transmutation and
975 * the directory requests transmutation then
976 * by all means transmute.
977 * Mark the inode as changed.
979 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
980 smk_inode_transmutable(dir
)) {
982 issp
->smk_flags
|= SMK_INODE_CHANGED
;
985 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
989 *len
= strlen(isp
->smk_known
);
996 * smack_inode_link - Smack check on link
997 * @old_dentry: the existing object
999 * @new_dentry: the new object
1001 * Returns 0 if access is permitted, an error code otherwise
1003 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1004 struct dentry
*new_dentry
)
1006 struct smack_known
*isp
;
1007 struct smk_audit_info ad
;
1010 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1011 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1013 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1014 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1015 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1017 if (rc
== 0 && d_is_positive(new_dentry
)) {
1018 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1019 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1020 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1021 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1028 * smack_inode_unlink - Smack check on inode deletion
1029 * @dir: containing directory object
1030 * @dentry: file to unlink
1032 * Returns 0 if current can write the containing directory
1033 * and the object, error code otherwise
1035 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1037 struct inode
*ip
= d_backing_inode(dentry
);
1038 struct smk_audit_info ad
;
1041 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1042 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1045 * You need write access to the thing you're unlinking
1047 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1048 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1051 * You also need write access to the containing directory
1053 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1054 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1055 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1056 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1062 * smack_inode_rmdir - Smack check on directory deletion
1063 * @dir: containing directory object
1064 * @dentry: directory to unlink
1066 * Returns 0 if current can write the containing directory
1067 * and the directory, error code otherwise
1069 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1071 struct smk_audit_info ad
;
1074 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1075 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1078 * You need write access to the thing you're removing
1080 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1081 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1084 * You also need write access to the containing directory
1086 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1087 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1088 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1089 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1096 * smack_inode_rename - Smack check on rename
1097 * @old_inode: unused
1098 * @old_dentry: the old object
1099 * @new_inode: unused
1100 * @new_dentry: the new object
1102 * Read and write access is required on both the old and
1105 * Returns 0 if access is permitted, an error code otherwise
1107 static int smack_inode_rename(struct inode
*old_inode
,
1108 struct dentry
*old_dentry
,
1109 struct inode
*new_inode
,
1110 struct dentry
*new_dentry
)
1113 struct smack_known
*isp
;
1114 struct smk_audit_info ad
;
1116 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1117 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1119 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1120 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1121 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1123 if (rc
== 0 && d_is_positive(new_dentry
)) {
1124 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1125 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1126 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1127 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1133 * smack_inode_permission - Smack version of permission()
1134 * @inode: the inode in question
1135 * @mask: the access requested
1137 * This is the important Smack hook.
1139 * Returns 0 if access is permitted, -EACCES otherwise
1141 static int smack_inode_permission(struct inode
*inode
, int mask
)
1143 struct superblock_smack
*sbsp
= smack_superblock(inode
->i_sb
);
1144 struct smk_audit_info ad
;
1145 int no_block
= mask
& MAY_NOT_BLOCK
;
1148 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1150 * No permission to check. Existence test. Yup, it's there.
1155 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1156 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1160 /* May be droppable after audit */
1163 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1164 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1165 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1166 rc
= smk_bu_inode(inode
, mask
, rc
);
1171 * smack_inode_setattr - Smack check for setting attributes
1172 * @dentry: the object
1173 * @iattr: for the force flag
1175 * Returns 0 if access is permitted, an error code otherwise
1177 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1179 struct smk_audit_info ad
;
1183 * Need to allow for clearing the setuid bit.
1185 if (iattr
->ia_valid
& ATTR_FORCE
)
1187 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1188 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1190 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1191 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1196 * smack_inode_getattr - Smack check for getting attributes
1197 * @mnt: vfsmount of the object
1198 * @dentry: the object
1200 * Returns 0 if access is permitted, an error code otherwise
1202 static int smack_inode_getattr(const struct path
*path
)
1204 struct smk_audit_info ad
;
1205 struct inode
*inode
= d_backing_inode(path
->dentry
);
1208 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1209 smk_ad_setfield_u_fs_path(&ad
, *path
);
1210 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1211 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1216 * smack_inode_setxattr - Smack check for setting xattrs
1217 * @dentry: the object
1218 * @name: name of the attribute
1219 * @value: value of the attribute
1220 * @size: size of the value
1223 * This protects the Smack attribute explicitly.
1225 * Returns 0 if access is permitted, an error code otherwise
1227 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1228 const void *value
, size_t size
, int flags
)
1230 struct smk_audit_info ad
;
1231 struct smack_known
*skp
;
1233 int check_import
= 0;
1238 * Check label validity here so import won't fail in post_setxattr
1240 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1241 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1242 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1245 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1246 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1250 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1252 if (size
!= TRANS_TRUE_SIZE
||
1253 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1256 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1258 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1261 if (rc
== 0 && check_import
) {
1262 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1265 else if (skp
== NULL
|| (check_star
&&
1266 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1270 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1271 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1274 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1275 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1282 * smack_inode_post_setxattr - Apply the Smack update approved above
1284 * @name: attribute name
1285 * @value: attribute value
1286 * @size: attribute size
1289 * Set the pointer in the inode blob to the entry found
1290 * in the master label list.
1292 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1293 const void *value
, size_t size
, int flags
)
1295 struct smack_known
*skp
;
1296 struct inode_smack
*isp
= smack_inode(d_backing_inode(dentry
));
1298 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1299 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1303 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1304 skp
= smk_import_entry(value
, size
);
1306 isp
->smk_inode
= skp
;
1307 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1308 skp
= smk_import_entry(value
, size
);
1310 isp
->smk_task
= skp
;
1311 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1312 skp
= smk_import_entry(value
, size
);
1314 isp
->smk_mmap
= skp
;
1321 * smack_inode_getxattr - Smack check on getxattr
1322 * @dentry: the object
1325 * Returns 0 if access is permitted, an error code otherwise
1327 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1329 struct smk_audit_info ad
;
1332 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1333 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1335 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1336 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1341 * smack_inode_removexattr - Smack check on removexattr
1342 * @dentry: the object
1343 * @name: name of the attribute
1345 * Removing the Smack attribute requires CAP_MAC_ADMIN
1347 * Returns 0 if access is permitted, an error code otherwise
1349 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1351 struct inode_smack
*isp
;
1352 struct smk_audit_info ad
;
1355 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1356 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1357 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1358 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1359 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1360 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1361 if (!smack_privileged(CAP_MAC_ADMIN
))
1364 rc
= cap_inode_removexattr(dentry
, name
);
1369 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1370 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1372 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1373 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1377 isp
= smack_inode(d_backing_inode(dentry
));
1379 * Don't do anything special for these.
1380 * XATTR_NAME_SMACKIPIN
1381 * XATTR_NAME_SMACKIPOUT
1383 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1384 struct super_block
*sbp
= dentry
->d_sb
;
1385 struct superblock_smack
*sbsp
= smack_superblock(sbp
);
1387 isp
->smk_inode
= sbsp
->smk_default
;
1388 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1389 isp
->smk_task
= NULL
;
1390 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1391 isp
->smk_mmap
= NULL
;
1392 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1393 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1399 * smack_inode_getsecurity - get smack xattrs
1400 * @inode: the object
1401 * @name: attribute name
1402 * @buffer: where to put the result
1403 * @alloc: duplicate memory
1405 * Returns the size of the attribute or an error code
1407 static int smack_inode_getsecurity(struct inode
*inode
,
1408 const char *name
, void **buffer
,
1411 struct socket_smack
*ssp
;
1412 struct socket
*sock
;
1413 struct super_block
*sbp
;
1414 struct inode
*ip
= (struct inode
*)inode
;
1415 struct smack_known
*isp
;
1417 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1418 isp
= smk_of_inode(inode
);
1421 * The rest of the Smack xattrs are only on sockets.
1424 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1427 sock
= SOCKET_I(ip
);
1428 if (sock
== NULL
|| sock
->sk
== NULL
)
1431 ssp
= smack_sock(sock
->sk
);
1433 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1435 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1442 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1443 if (*buffer
== NULL
)
1447 return strlen(isp
->smk_known
);
1452 * smack_inode_listsecurity - list the Smack attributes
1453 * @inode: the object
1454 * @buffer: where they go
1455 * @buffer_size: size of buffer
1457 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1460 int len
= sizeof(XATTR_NAME_SMACK
);
1462 if (buffer
!= NULL
&& len
<= buffer_size
)
1463 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1469 * smack_inode_getsecid - Extract inode's security id
1470 * @inode: inode to extract the info from
1471 * @secid: where result will be saved
1473 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1475 struct inode_smack
*isp
= smack_inode(inode
);
1477 *secid
= isp
->smk_inode
->smk_secid
;
1485 * There is no smack_file_permission hook
1487 * Should access checks be done on each read or write?
1488 * UNICOS and SELinux say yes.
1489 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1491 * I'll say no for now. Smack does not do the frequent
1492 * label changing that SELinux does.
1496 * smack_file_alloc_security - assign a file security blob
1499 * The security blob for a file is a pointer to the master
1500 * label list, so no allocation is done.
1502 * f_security is the owner security information. It
1503 * isn't used on file access checks, it's for send_sigio.
1507 static int smack_file_alloc_security(struct file
*file
)
1509 struct smack_known
**blob
= smack_file(file
);
1511 *blob
= smk_of_current();
1516 * smack_file_ioctl - Smack check on ioctls
1521 * Relies heavily on the correct use of the ioctl command conventions.
1523 * Returns 0 if allowed, error code otherwise
1525 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1529 struct smk_audit_info ad
;
1530 struct inode
*inode
= file_inode(file
);
1532 if (unlikely(IS_PRIVATE(inode
)))
1535 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1536 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1538 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1539 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1540 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1543 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1544 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1545 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1552 * smack_file_lock - Smack check on file locking
1556 * Returns 0 if current has lock access, error code otherwise
1558 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1560 struct smk_audit_info ad
;
1562 struct inode
*inode
= file_inode(file
);
1564 if (unlikely(IS_PRIVATE(inode
)))
1567 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1568 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1569 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1570 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1575 * smack_file_fcntl - Smack check on fcntl
1577 * @cmd: what action to check
1580 * Generally these operations are harmless.
1581 * File locking operations present an obvious mechanism
1582 * for passing information, so they require write access.
1584 * Returns 0 if current has access, error code otherwise
1586 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1589 struct smk_audit_info ad
;
1591 struct inode
*inode
= file_inode(file
);
1593 if (unlikely(IS_PRIVATE(inode
)))
1601 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1602 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1603 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1604 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1608 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1609 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1610 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1611 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1622 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1623 * if mapping anonymous memory.
1624 * @file contains the file structure for file to map (may be NULL).
1625 * @reqprot contains the protection requested by the application.
1626 * @prot contains the protection that will be applied by the kernel.
1627 * @flags contains the operational flags.
1628 * Return 0 if permission is granted.
1630 static int smack_mmap_file(struct file
*file
,
1631 unsigned long reqprot
, unsigned long prot
,
1632 unsigned long flags
)
1634 struct smack_known
*skp
;
1635 struct smack_known
*mkp
;
1636 struct smack_rule
*srp
;
1637 struct task_smack
*tsp
;
1638 struct smack_known
*okp
;
1639 struct inode_smack
*isp
;
1640 struct superblock_smack
*sbsp
;
1649 if (unlikely(IS_PRIVATE(file_inode(file
))))
1652 isp
= smack_inode(file_inode(file
));
1653 if (isp
->smk_mmap
== NULL
)
1655 sbsp
= smack_superblock(file_inode(file
)->i_sb
);
1656 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1657 isp
->smk_mmap
!= sbsp
->smk_root
)
1659 mkp
= isp
->smk_mmap
;
1661 tsp
= smack_cred(current_cred());
1662 skp
= smk_of_current();
1667 * For each Smack rule associated with the subject
1668 * label verify that the SMACK64MMAP also has access
1669 * to that rule's object label.
1671 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1672 okp
= srp
->smk_object
;
1674 * Matching labels always allows access.
1676 if (mkp
->smk_known
== okp
->smk_known
)
1679 * If there is a matching local rule take
1680 * that into account as well.
1682 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1686 may
= srp
->smk_access
;
1688 may
&= srp
->smk_access
;
1690 * If may is zero the SMACK64MMAP subject can't
1691 * possibly have less access.
1697 * Fetch the global list entry.
1698 * If there isn't one a SMACK64MMAP subject
1699 * can't have as much access as current.
1701 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1703 if (mmay
== -ENOENT
) {
1708 * If there is a local entry it modifies the
1709 * potential access, too.
1711 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1713 if (tmay
!= -ENOENT
)
1717 * If there is any access available to current that is
1718 * not available to a SMACK64MMAP subject
1721 if ((may
| mmay
) != mmay
) {
1733 * smack_file_set_fowner - set the file security blob value
1734 * @file: object in question
1737 static void smack_file_set_fowner(struct file
*file
)
1739 struct smack_known
**blob
= smack_file(file
);
1741 *blob
= smk_of_current();
1745 * smack_file_send_sigiotask - Smack on sigio
1746 * @tsk: The target task
1747 * @fown: the object the signal come from
1750 * Allow a privileged task to get signals even if it shouldn't
1752 * Returns 0 if a subject with the object's smack could
1753 * write to the task, an error code otherwise.
1755 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1756 struct fown_struct
*fown
, int signum
)
1758 struct smack_known
**blob
;
1759 struct smack_known
*skp
;
1760 struct smack_known
*tkp
= smk_of_task(smack_cred(tsk
->cred
));
1763 struct smk_audit_info ad
;
1766 * struct fown_struct is never outside the context of a struct file
1768 file
= container_of(fown
, struct file
, f_owner
);
1770 /* we don't log here as rc can be overriden */
1771 blob
= smack_file(file
);
1773 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1774 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1775 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1778 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1779 smk_ad_setfield_u_tsk(&ad
, tsk
);
1780 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1785 * smack_file_receive - Smack file receive check
1788 * Returns 0 if current has access, error code otherwise
1790 static int smack_file_receive(struct file
*file
)
1794 struct smk_audit_info ad
;
1795 struct inode
*inode
= file_inode(file
);
1796 struct socket
*sock
;
1797 struct task_smack
*tsp
;
1798 struct socket_smack
*ssp
;
1800 if (unlikely(IS_PRIVATE(inode
)))
1803 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1804 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1806 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1807 sock
= SOCKET_I(inode
);
1808 ssp
= smack_sock(sock
->sk
);
1809 tsp
= smack_cred(current_cred());
1811 * If the receiving process can't write to the
1812 * passed socket or if the passed socket can't
1813 * write to the receiving process don't accept
1814 * the passed socket.
1816 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1817 rc
= smk_bu_file(file
, may
, rc
);
1820 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1821 rc
= smk_bu_file(file
, may
, rc
);
1825 * This code relies on bitmasks.
1827 if (file
->f_mode
& FMODE_READ
)
1829 if (file
->f_mode
& FMODE_WRITE
)
1832 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1833 rc
= smk_bu_file(file
, may
, rc
);
1838 * smack_file_open - Smack dentry open processing
1840 * @cred: task credential
1842 * Set the security blob in the file structure.
1843 * Allow the open only if the task has read access. There are
1844 * many read operations (e.g. fstat) that you can do with an
1845 * fd even if you have the file open write-only.
1849 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1851 struct task_smack
*tsp
= smack_cred(cred
);
1852 struct inode
*inode
= file_inode(file
);
1853 struct smk_audit_info ad
;
1856 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1857 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1858 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1859 rc
= smk_bu_credfile(cred
, file
, MAY_READ
, rc
);
1869 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1870 * @new: the new credentials
1871 * @gfp: the atomicity of any memory allocations
1873 * Prepare a blank set of credentials for modification. This must allocate all
1874 * the memory the LSM module might require such that cred_transfer() can
1875 * complete without error.
1877 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1879 init_task_smack(smack_cred(cred
), NULL
, NULL
);
1885 * smack_cred_free - "free" task-level security credentials
1886 * @cred: the credentials in question
1889 static void smack_cred_free(struct cred
*cred
)
1891 struct task_smack
*tsp
= smack_cred(cred
);
1892 struct smack_rule
*rp
;
1893 struct list_head
*l
;
1894 struct list_head
*n
;
1896 smk_destroy_label_list(&tsp
->smk_relabel
);
1898 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1899 rp
= list_entry(l
, struct smack_rule
, list
);
1900 list_del(&rp
->list
);
1906 * smack_cred_prepare - prepare new set of credentials for modification
1907 * @new: the new credentials
1908 * @old: the original credentials
1909 * @gfp: the atomicity of any memory allocations
1911 * Prepare a new set of credentials for modification.
1913 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1916 struct task_smack
*old_tsp
= smack_cred(old
);
1917 struct task_smack
*new_tsp
= smack_cred(new);
1920 init_task_smack(new_tsp
, old_tsp
->smk_task
, old_tsp
->smk_task
);
1922 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1926 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
1932 * smack_cred_transfer - Transfer the old credentials to the new credentials
1933 * @new: the new credentials
1934 * @old: the original credentials
1936 * Fill in a set of blank credentials from another set of credentials.
1938 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1940 struct task_smack
*old_tsp
= smack_cred(old
);
1941 struct task_smack
*new_tsp
= smack_cred(new);
1943 new_tsp
->smk_task
= old_tsp
->smk_task
;
1944 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1945 mutex_init(&new_tsp
->smk_rules_lock
);
1946 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1948 /* cbs copy rule list */
1952 * smack_kernel_act_as - Set the subjective context in a set of credentials
1953 * @new: points to the set of credentials to be modified.
1954 * @secid: specifies the security ID to be set
1956 * Set the security data for a kernel service.
1958 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1960 struct task_smack
*new_tsp
= smack_cred(new);
1962 new_tsp
->smk_task
= smack_from_secid(secid
);
1967 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1968 * @new: points to the set of credentials to be modified
1969 * @inode: points to the inode to use as a reference
1971 * Set the file creation context in a set of credentials to the same
1972 * as the objective context of the specified inode
1974 static int smack_kernel_create_files_as(struct cred
*new,
1975 struct inode
*inode
)
1977 struct inode_smack
*isp
= smack_inode(inode
);
1978 struct task_smack
*tsp
= smack_cred(new);
1980 tsp
->smk_forked
= isp
->smk_inode
;
1981 tsp
->smk_task
= tsp
->smk_forked
;
1986 * smk_curacc_on_task - helper to log task related access
1987 * @p: the task object
1988 * @access: the access requested
1989 * @caller: name of the calling function for audit
1991 * Return 0 if access is permitted
1993 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
1996 struct smk_audit_info ad
;
1997 struct smack_known
*skp
= smk_of_task_struct(p
);
2000 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2001 smk_ad_setfield_u_tsk(&ad
, p
);
2002 rc
= smk_curacc(skp
, access
, &ad
);
2003 rc
= smk_bu_task(p
, access
, rc
);
2008 * smack_task_setpgid - Smack check on setting pgid
2009 * @p: the task object
2012 * Return 0 if write access is permitted
2014 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2016 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2020 * smack_task_getpgid - Smack access check for getpgid
2021 * @p: the object task
2023 * Returns 0 if current can read the object task, error code otherwise
2025 static int smack_task_getpgid(struct task_struct
*p
)
2027 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2031 * smack_task_getsid - Smack access check for getsid
2032 * @p: the object task
2034 * Returns 0 if current can read the object task, error code otherwise
2036 static int smack_task_getsid(struct task_struct
*p
)
2038 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2042 * smack_task_getsecid - get the secid of the task
2043 * @p: the object task
2044 * @secid: where to put the result
2046 * Sets the secid to contain a u32 version of the smack label.
2048 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2050 struct smack_known
*skp
= smk_of_task_struct(p
);
2052 *secid
= skp
->smk_secid
;
2056 * smack_task_setnice - Smack check on setting nice
2057 * @p: the task object
2060 * Return 0 if write access is permitted
2062 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2064 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2068 * smack_task_setioprio - Smack check on setting ioprio
2069 * @p: the task object
2072 * Return 0 if write access is permitted
2074 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2076 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2080 * smack_task_getioprio - Smack check on reading ioprio
2081 * @p: the task object
2083 * Return 0 if read access is permitted
2085 static int smack_task_getioprio(struct task_struct
*p
)
2087 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2091 * smack_task_setscheduler - Smack check on setting scheduler
2092 * @p: the task object
2096 * Return 0 if read access is permitted
2098 static int smack_task_setscheduler(struct task_struct
*p
)
2100 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2104 * smack_task_getscheduler - Smack check on reading scheduler
2105 * @p: the task object
2107 * Return 0 if read access is permitted
2109 static int smack_task_getscheduler(struct task_struct
*p
)
2111 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2115 * smack_task_movememory - Smack check on moving memory
2116 * @p: the task object
2118 * Return 0 if write access is permitted
2120 static int smack_task_movememory(struct task_struct
*p
)
2122 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2126 * smack_task_kill - Smack check on signal delivery
2127 * @p: the task object
2130 * @secid: identifies the smack to use in lieu of current's
2132 * Return 0 if write access is permitted
2134 * The secid behavior is an artifact of an SELinux hack
2135 * in the USB code. Someday it may go away.
2137 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
2140 struct smk_audit_info ad
;
2141 struct smack_known
*skp
;
2142 struct smack_known
*tkp
= smk_of_task_struct(p
);
2146 return 0; /* null signal; existence test */
2148 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2149 smk_ad_setfield_u_tsk(&ad
, p
);
2151 * Sending a signal requires that the sender
2152 * can write the receiver.
2155 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2156 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2160 * If the secid isn't 0 we're dealing with some USB IO
2161 * specific behavior. This is not clean. For one thing
2162 * we can't take privilege into account.
2164 skp
= smack_from_secid(secid
);
2165 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2166 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2171 * smack_task_to_inode - copy task smack into the inode blob
2172 * @p: task to copy from
2173 * @inode: inode to copy to
2175 * Sets the smack pointer in the inode security blob
2177 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2179 struct inode_smack
*isp
= smack_inode(inode
);
2180 struct smack_known
*skp
= smk_of_task_struct(p
);
2182 isp
->smk_inode
= skp
;
2183 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2191 * smack_sk_alloc_security - Allocate a socket blob
2194 * @gfp_flags: memory allocation flags
2196 * Assign Smack pointers to current
2198 * Returns 0 on success, -ENOMEM is there's no memory
2200 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2202 struct smack_known
*skp
= smk_of_current();
2203 struct socket_smack
*ssp
= smack_sock(sk
);
2206 * Sockets created by kernel threads receive web label.
2208 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2209 ssp
->smk_in
= &smack_known_web
;
2210 ssp
->smk_out
= &smack_known_web
;
2215 ssp
->smk_packet
= NULL
;
2220 #ifdef SMACK_IPV6_PORT_LABELING
2222 * smack_sk_free_security - Free a socket blob
2225 * Clears the blob pointer
2227 static void smack_sk_free_security(struct sock
*sk
)
2229 struct smk_port_label
*spp
;
2231 if (sk
->sk_family
== PF_INET6
) {
2233 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2234 if (spp
->smk_sock
!= sk
)
2236 spp
->smk_can_reuse
= 1;
2245 * smack_ipv4host_label - check host based restrictions
2246 * @sip: the object end
2248 * looks for host based access restrictions
2250 * This version will only be appropriate for really small sets of single label
2251 * hosts. The caller is responsible for ensuring that the RCU read lock is
2252 * taken before calling this function.
2254 * Returns the label of the far end or NULL if it's not special.
2256 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2258 struct smk_net4addr
*snp
;
2259 struct in_addr
*siap
= &sip
->sin_addr
;
2261 if (siap
->s_addr
== 0)
2264 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2266 * we break after finding the first match because
2267 * the list is sorted from longest to shortest mask
2268 * so we have found the most specific match
2270 if (snp
->smk_host
.s_addr
==
2271 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2272 return snp
->smk_label
;
2277 #if IS_ENABLED(CONFIG_IPV6)
2279 * smk_ipv6_localhost - Check for local ipv6 host address
2282 * Returns boolean true if this is the localhost address
2284 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2286 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2287 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2289 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2290 ntohs(be16p
[7]) == 1)
2296 * smack_ipv6host_label - check host based restrictions
2297 * @sip: the object end
2299 * looks for host based access restrictions
2301 * This version will only be appropriate for really small sets of single label
2302 * hosts. The caller is responsible for ensuring that the RCU read lock is
2303 * taken before calling this function.
2305 * Returns the label of the far end or NULL if it's not special.
2307 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2309 struct smk_net6addr
*snp
;
2310 struct in6_addr
*sap
= &sip
->sin6_addr
;
2315 * It's local. Don't look for a host label.
2317 if (smk_ipv6_localhost(sip
))
2320 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2322 * If the label is NULL the entry has
2323 * been renounced. Ignore it.
2325 if (snp
->smk_label
== NULL
)
2328 * we break after finding the first match because
2329 * the list is sorted from longest to shortest mask
2330 * so we have found the most specific match
2332 for (found
= 1, i
= 0; i
< 8; i
++) {
2333 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2334 snp
->smk_host
.s6_addr16
[i
]) {
2340 return snp
->smk_label
;
2345 #endif /* CONFIG_IPV6 */
2348 * smack_netlabel - Set the secattr on a socket
2350 * @labeled: socket label scheme
2352 * Convert the outbound smack value (smk_out) to a
2353 * secattr and attach it to the socket.
2355 * Returns 0 on success or an error code
2357 static int smack_netlabel(struct sock
*sk
, int labeled
)
2359 struct smack_known
*skp
;
2360 struct socket_smack
*ssp
= smack_sock(sk
);
2364 * Usually the netlabel code will handle changing the
2365 * packet labeling based on the label.
2366 * The case of a single label host is different, because
2367 * a single label host should never get a labeled packet
2368 * even though the label is usually associated with a packet
2372 bh_lock_sock_nested(sk
);
2374 if (ssp
->smk_out
== smack_net_ambient
||
2375 labeled
== SMACK_UNLABELED_SOCKET
)
2376 netlbl_sock_delattr(sk
);
2379 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2389 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2391 * @sap: the destination address
2393 * Set the correct secattr for the given socket based on the destination
2394 * address and perform any outbound access checks needed.
2396 * Returns 0 on success or an error code.
2399 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2401 struct smack_known
*skp
;
2404 struct smack_known
*hkp
;
2405 struct socket_smack
*ssp
= smack_sock(sk
);
2406 struct smk_audit_info ad
;
2409 hkp
= smack_ipv4host_label(sap
);
2412 struct lsm_network_audit net
;
2414 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2415 ad
.a
.u
.net
->family
= sap
->sin_family
;
2416 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2417 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2419 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2421 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2422 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2424 sk_lbl
= SMACK_CIPSO_SOCKET
;
2431 return smack_netlabel(sk
, sk_lbl
);
2434 #if IS_ENABLED(CONFIG_IPV6)
2436 * smk_ipv6_check - check Smack access
2437 * @subject: subject Smack label
2438 * @object: object Smack label
2440 * @act: the action being taken
2442 * Check an IPv6 access
2444 static int smk_ipv6_check(struct smack_known
*subject
,
2445 struct smack_known
*object
,
2446 struct sockaddr_in6
*address
, int act
)
2449 struct lsm_network_audit net
;
2451 struct smk_audit_info ad
;
2455 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2456 ad
.a
.u
.net
->family
= PF_INET6
;
2457 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2458 if (act
== SMK_RECEIVING
)
2459 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2461 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2463 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2464 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2467 #endif /* CONFIG_IPV6 */
2469 #ifdef SMACK_IPV6_PORT_LABELING
2471 * smk_ipv6_port_label - Smack port access table management
2475 * Create or update the port list entry
2477 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2479 struct sock
*sk
= sock
->sk
;
2480 struct sockaddr_in6
*addr6
;
2481 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
2482 struct smk_port_label
*spp
;
2483 unsigned short port
= 0;
2485 if (address
== NULL
) {
2487 * This operation is changing the Smack information
2488 * on the bound socket. Take the changes to the port
2492 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2493 if (sk
!= spp
->smk_sock
)
2495 spp
->smk_in
= ssp
->smk_in
;
2496 spp
->smk_out
= ssp
->smk_out
;
2501 * A NULL address is only used for updating existing
2502 * bound entries. If there isn't one, it's OK.
2508 addr6
= (struct sockaddr_in6
*)address
;
2509 port
= ntohs(addr6
->sin6_port
);
2511 * This is a special case that is safely ignored.
2517 * Look for an existing port list entry.
2518 * This is an indication that a port is getting reused.
2521 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2522 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2524 if (spp
->smk_can_reuse
!= 1) {
2528 spp
->smk_port
= port
;
2530 spp
->smk_in
= ssp
->smk_in
;
2531 spp
->smk_out
= ssp
->smk_out
;
2532 spp
->smk_can_reuse
= 0;
2538 * A new port entry is required.
2540 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2544 spp
->smk_port
= port
;
2546 spp
->smk_in
= ssp
->smk_in
;
2547 spp
->smk_out
= ssp
->smk_out
;
2548 spp
->smk_sock_type
= sock
->type
;
2549 spp
->smk_can_reuse
= 0;
2551 mutex_lock(&smack_ipv6_lock
);
2552 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2553 mutex_unlock(&smack_ipv6_lock
);
2558 * smk_ipv6_port_check - check Smack port access
2562 * Create or update the port list entry
2564 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2567 struct smk_port_label
*spp
;
2568 struct socket_smack
*ssp
= smack_sock(sk
);
2569 struct smack_known
*skp
= NULL
;
2570 unsigned short port
;
2571 struct smack_known
*object
;
2573 if (act
== SMK_RECEIVING
) {
2574 skp
= smack_ipv6host_label(address
);
2575 object
= ssp
->smk_in
;
2578 object
= smack_ipv6host_label(address
);
2582 * The other end is a single label host.
2584 if (skp
!= NULL
&& object
!= NULL
)
2585 return smk_ipv6_check(skp
, object
, address
, act
);
2587 skp
= smack_net_ambient
;
2589 object
= smack_net_ambient
;
2592 * It's remote, so port lookup does no good.
2594 if (!smk_ipv6_localhost(address
))
2595 return smk_ipv6_check(skp
, object
, address
, act
);
2598 * It's local so the send check has to have passed.
2600 if (act
== SMK_RECEIVING
)
2603 port
= ntohs(address
->sin6_port
);
2605 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2606 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2608 object
= spp
->smk_in
;
2609 if (act
== SMK_CONNECTING
)
2610 ssp
->smk_packet
= spp
->smk_out
;
2615 return smk_ipv6_check(skp
, object
, address
, act
);
2617 #endif /* SMACK_IPV6_PORT_LABELING */
2620 * smack_inode_setsecurity - set smack xattrs
2621 * @inode: the object
2622 * @name: attribute name
2623 * @value: attribute value
2624 * @size: size of the attribute
2627 * Sets the named attribute in the appropriate blob
2629 * Returns 0 on success, or an error code
2631 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2632 const void *value
, size_t size
, int flags
)
2634 struct smack_known
*skp
;
2635 struct inode_smack
*nsp
= smack_inode(inode
);
2636 struct socket_smack
*ssp
;
2637 struct socket
*sock
;
2640 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2643 skp
= smk_import_entry(value
, size
);
2645 return PTR_ERR(skp
);
2647 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2648 nsp
->smk_inode
= skp
;
2649 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2653 * The rest of the Smack xattrs are only on sockets.
2655 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2658 sock
= SOCKET_I(inode
);
2659 if (sock
== NULL
|| sock
->sk
== NULL
)
2662 ssp
= smack_sock(sock
->sk
);
2664 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2666 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2668 if (sock
->sk
->sk_family
== PF_INET
) {
2669 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2672 "Smack: \"%s\" netlbl error %d.\n",
2678 #ifdef SMACK_IPV6_PORT_LABELING
2679 if (sock
->sk
->sk_family
== PF_INET6
)
2680 smk_ipv6_port_label(sock
, NULL
);
2687 * smack_socket_post_create - finish socket setup
2689 * @family: protocol family
2694 * Sets the netlabel information on the socket
2696 * Returns 0 on success, and error code otherwise
2698 static int smack_socket_post_create(struct socket
*sock
, int family
,
2699 int type
, int protocol
, int kern
)
2701 struct socket_smack
*ssp
;
2703 if (sock
->sk
== NULL
)
2707 * Sockets created by kernel threads receive web label.
2709 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2710 ssp
= smack_sock(sock
->sk
);
2711 ssp
->smk_in
= &smack_known_web
;
2712 ssp
->smk_out
= &smack_known_web
;
2715 if (family
!= PF_INET
)
2718 * Set the outbound netlbl.
2720 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2723 #ifdef SMACK_IPV6_PORT_LABELING
2725 * smack_socket_bind - record port binding information.
2727 * @address: the port address
2728 * @addrlen: size of the address
2730 * Records the label bound to a port.
2734 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2737 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2738 smk_ipv6_port_label(sock
, address
);
2741 #endif /* SMACK_IPV6_PORT_LABELING */
2744 * smack_socket_connect - connect access check
2746 * @sap: the other end
2747 * @addrlen: size of sap
2749 * Verifies that a connection may be possible
2751 * Returns 0 on success, and error code otherwise
2753 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2757 #if IS_ENABLED(CONFIG_IPV6)
2758 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2760 #ifdef SMACK_IPV6_SECMARK_LABELING
2761 struct smack_known
*rsp
;
2762 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2765 if (sock
->sk
== NULL
)
2768 switch (sock
->sk
->sk_family
) {
2770 if (addrlen
< sizeof(struct sockaddr_in
))
2772 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2775 if (addrlen
< sizeof(struct sockaddr_in6
))
2777 #ifdef SMACK_IPV6_SECMARK_LABELING
2778 rsp
= smack_ipv6host_label(sip
);
2780 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2783 #ifdef SMACK_IPV6_PORT_LABELING
2784 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2792 * smack_flags_to_may - convert S_ to MAY_ values
2793 * @flags: the S_ value
2795 * Returns the equivalent MAY_ value
2797 static int smack_flags_to_may(int flags
)
2801 if (flags
& S_IRUGO
)
2803 if (flags
& S_IWUGO
)
2805 if (flags
& S_IXUGO
)
2812 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2817 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2819 struct smack_known
**blob
= smack_msg_msg(msg
);
2821 *blob
= smk_of_current();
2826 * smack_shm_alloc_security - Set the security blob for shm
2831 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2833 struct smack_known
**blob
= smack_ipc(&shp
->shm_perm
);
2835 *blob
= smk_of_current();
2840 * smk_curacc_shm : check if current has access on shm
2842 * @access : access requested
2844 * Returns 0 if current has the requested access, error code otherwise
2846 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2848 struct smack_known
**blob
= smack_ipc(&shp
->shm_perm
);
2849 struct smack_known
*ssp
= *blob
;
2850 struct smk_audit_info ad
;
2854 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2855 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2857 rc
= smk_curacc(ssp
, access
, &ad
);
2858 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2863 * smack_shm_associate - Smack access check for shm
2865 * @shmflg: access requested
2867 * Returns 0 if current has the requested access, error code otherwise
2869 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2873 may
= smack_flags_to_may(shmflg
);
2874 return smk_curacc_shm(shp
, may
);
2878 * smack_shm_shmctl - Smack access check for shm
2880 * @cmd: what it wants to do
2882 * Returns 0 if current has the requested access, error code otherwise
2884 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2897 may
= MAY_READWRITE
;
2902 * System level information.
2908 return smk_curacc_shm(shp
, may
);
2912 * smack_shm_shmat - Smack access for shmat
2915 * @shmflg: access requested
2917 * Returns 0 if current has the requested access, error code otherwise
2919 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2924 may
= smack_flags_to_may(shmflg
);
2925 return smk_curacc_shm(shp
, may
);
2929 * smack_sem_alloc_security - Set the security blob for sem
2934 static int smack_sem_alloc_security(struct sem_array
*sma
)
2936 struct smack_known
**blob
= smack_ipc(&sma
->sem_perm
);
2938 *blob
= smk_of_current();
2943 * smk_curacc_sem : check if current has access on sem
2945 * @access : access requested
2947 * Returns 0 if current has the requested access, error code otherwise
2949 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2951 struct smack_known
**blob
= smack_ipc(&sma
->sem_perm
);
2952 struct smack_known
*ssp
= *blob
;
2953 struct smk_audit_info ad
;
2957 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2958 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2960 rc
= smk_curacc(ssp
, access
, &ad
);
2961 rc
= smk_bu_current("sem", ssp
, access
, rc
);
2966 * smack_sem_associate - Smack access check for sem
2968 * @semflg: access requested
2970 * Returns 0 if current has the requested access, error code otherwise
2972 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2976 may
= smack_flags_to_may(semflg
);
2977 return smk_curacc_sem(sma
, may
);
2981 * smack_sem_shmctl - Smack access check for sem
2983 * @cmd: what it wants to do
2985 * Returns 0 if current has the requested access, error code otherwise
2987 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
3005 may
= MAY_READWRITE
;
3010 * System level information
3017 return smk_curacc_sem(sma
, may
);
3021 * smack_sem_semop - Smack checks of semaphore operations
3027 * Treated as read and write in all cases.
3029 * Returns 0 if access is allowed, error code otherwise
3031 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
3032 unsigned nsops
, int alter
)
3034 return smk_curacc_sem(sma
, MAY_READWRITE
);
3038 * smack_msg_queue_alloc_security - Set the security blob for msg
3043 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
3045 struct smack_known
**blob
= smack_ipc(&msq
->q_perm
);
3047 *blob
= smk_of_current();
3052 * smk_curacc_msq : helper to check if current has access on msq
3054 * @access : access requested
3056 * return 0 if current has access, error otherwise
3058 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
3060 struct smack_known
**blob
= smack_ipc(&msq
->q_perm
);
3061 struct smack_known
*msp
= *blob
;
3062 struct smk_audit_info ad
;
3066 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3067 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
3069 rc
= smk_curacc(msp
, access
, &ad
);
3070 rc
= smk_bu_current("msq", msp
, access
, rc
);
3075 * smack_msg_queue_associate - Smack access check for msg_queue
3077 * @msqflg: access requested
3079 * Returns 0 if current has the requested access, error code otherwise
3081 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
3085 may
= smack_flags_to_may(msqflg
);
3086 return smk_curacc_msq(msq
, may
);
3090 * smack_msg_queue_msgctl - Smack access check for msg_queue
3092 * @cmd: what it wants to do
3094 * Returns 0 if current has the requested access, error code otherwise
3096 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
3107 may
= MAY_READWRITE
;
3112 * System level information
3119 return smk_curacc_msq(msq
, may
);
3123 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3126 * @msqflg: access requested
3128 * Returns 0 if current has the requested access, error code otherwise
3130 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
3135 may
= smack_flags_to_may(msqflg
);
3136 return smk_curacc_msq(msq
, may
);
3140 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3147 * Returns 0 if current has read and write access, error code otherwise
3149 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
3150 struct task_struct
*target
, long type
, int mode
)
3152 return smk_curacc_msq(msq
, MAY_READWRITE
);
3156 * smack_ipc_permission - Smack access for ipc_permission()
3157 * @ipp: the object permissions
3158 * @flag: access requested
3160 * Returns 0 if current has read and write access, error code otherwise
3162 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3164 struct smack_known
**blob
= smack_ipc(ipp
);
3165 struct smack_known
*iskp
= *blob
;
3166 int may
= smack_flags_to_may(flag
);
3167 struct smk_audit_info ad
;
3171 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3172 ad
.a
.u
.ipc_id
= ipp
->id
;
3174 rc
= smk_curacc(iskp
, may
, &ad
);
3175 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3180 * smack_ipc_getsecid - Extract smack security id
3181 * @ipp: the object permissions
3182 * @secid: where result will be saved
3184 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3186 struct smack_known
**blob
= smack_ipc(ipp
);
3187 struct smack_known
*iskp
= *blob
;
3189 *secid
= iskp
->smk_secid
;
3193 * smack_d_instantiate - Make sure the blob is correct on an inode
3194 * @opt_dentry: dentry where inode will be attached
3195 * @inode: the object
3197 * Set the inode's security blob if it hasn't been done already.
3199 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3201 struct super_block
*sbp
;
3202 struct superblock_smack
*sbsp
;
3203 struct inode_smack
*isp
;
3204 struct smack_known
*skp
;
3205 struct smack_known
*ckp
= smk_of_current();
3206 struct smack_known
*final
;
3207 char trattr
[TRANS_TRUE_SIZE
];
3215 isp
= smack_inode(inode
);
3217 mutex_lock(&isp
->smk_lock
);
3219 * If the inode is already instantiated
3220 * take the quick way out
3222 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3226 sbsp
= smack_superblock(sbp
);
3228 * We're going to use the superblock default label
3229 * if there's no label on the file.
3231 final
= sbsp
->smk_default
;
3234 * If this is the root inode the superblock
3235 * may be in the process of initialization.
3236 * If that is the case use the root value out
3237 * of the superblock.
3239 if (opt_dentry
->d_parent
== opt_dentry
) {
3240 switch (sbp
->s_magic
) {
3241 case CGROUP_SUPER_MAGIC
:
3243 * The cgroup filesystem is never mounted,
3244 * so there's no opportunity to set the mount
3247 sbsp
->smk_root
= &smack_known_star
;
3248 sbsp
->smk_default
= &smack_known_star
;
3249 isp
->smk_inode
= sbsp
->smk_root
;
3253 * What about shmem/tmpfs anonymous files with dentry
3254 * obtained from d_alloc_pseudo()?
3256 isp
->smk_inode
= smk_of_current();
3259 isp
->smk_inode
= smk_of_current();
3263 * Socket access is controlled by the socket
3264 * structures associated with the task involved.
3266 isp
->smk_inode
= &smack_known_star
;
3269 isp
->smk_inode
= sbsp
->smk_root
;
3272 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3277 * This is pretty hackish.
3278 * Casey says that we shouldn't have to do
3279 * file system specific code, but it does help
3280 * with keeping it simple.
3282 switch (sbp
->s_magic
) {
3284 case CGROUP_SUPER_MAGIC
:
3286 * Casey says that it's a little embarrassing
3287 * that the smack file system doesn't do
3288 * extended attributes.
3290 * Cgroupfs is special
3292 final
= &smack_known_star
;
3294 case DEVPTS_SUPER_MAGIC
:
3296 * devpts seems content with the label of the task.
3297 * Programs that change smack have to treat the
3302 case PROC_SUPER_MAGIC
:
3304 * Casey says procfs appears not to care.
3305 * The superblock default suffices.
3310 * Device labels should come from the filesystem,
3311 * but watch out, because they're volitile,
3312 * getting recreated on every reboot.
3314 final
= &smack_known_star
;
3318 * If a smack value has been set we want to use it,
3319 * but since tmpfs isn't giving us the opportunity
3320 * to set mount options simulate setting the
3321 * superblock default.
3325 * This isn't an understood special case.
3326 * Get the value from the xattr.
3330 * UNIX domain sockets use lower level socket data.
3332 if (S_ISSOCK(inode
->i_mode
)) {
3333 final
= &smack_known_star
;
3337 * No xattr support means, alas, no SMACK label.
3338 * Use the aforeapplied default.
3339 * It would be curious if the label of the task
3340 * does not match that assigned.
3342 if (!(inode
->i_opflags
& IOP_XATTR
))
3345 * Get the dentry for xattr.
3347 dp
= dget(opt_dentry
);
3348 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3349 if (!IS_ERR_OR_NULL(skp
))
3353 * Transmuting directory
3355 if (S_ISDIR(inode
->i_mode
)) {
3357 * If this is a new directory and the label was
3358 * transmuted when the inode was initialized
3359 * set the transmute attribute on the directory
3360 * and mark the inode.
3362 * If there is a transmute attribute on the
3363 * directory mark the inode.
3365 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3366 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3367 rc
= __vfs_setxattr(dp
, inode
,
3368 XATTR_NAME_SMACKTRANSMUTE
,
3369 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3372 rc
= __vfs_getxattr(dp
, inode
,
3373 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3375 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3376 TRANS_TRUE_SIZE
) != 0)
3380 transflag
= SMK_INODE_TRANSMUTE
;
3383 * Don't let the exec or mmap label be "*" or "@".
3385 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3386 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3387 skp
== &smack_known_web
)
3389 isp
->smk_task
= skp
;
3391 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3392 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3393 skp
== &smack_known_web
)
3395 isp
->smk_mmap
= skp
;
3402 isp
->smk_inode
= ckp
;
3404 isp
->smk_inode
= final
;
3406 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3409 mutex_unlock(&isp
->smk_lock
);
3414 * smack_getprocattr - Smack process attribute access
3415 * @p: the object task
3416 * @name: the name of the attribute in /proc/.../attr
3417 * @value: where to put the result
3419 * Places a copy of the task Smack into value
3421 * Returns the length of the smack label or an error code
3423 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3425 struct smack_known
*skp
= smk_of_task_struct(p
);
3428 if (strcmp(name
, "current") == 0) {
3429 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3440 * smack_setprocattr - Smack process attribute setting
3441 * @name: the name of the attribute in /proc/.../attr
3442 * @value: the value to set
3443 * @size: the size of the value
3445 * Sets the Smack value of the task. Only setting self
3446 * is permitted and only with privilege
3448 * Returns the length of the smack label or an error code
3450 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3452 struct task_smack
*tsp
= smack_cred(current_cred());
3454 struct smack_known
*skp
;
3455 struct smack_known_list_elem
*sklep
;
3458 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3461 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3464 if (strcmp(name
, "current") != 0)
3467 skp
= smk_import_entry(value
, size
);
3469 return PTR_ERR(skp
);
3472 * No process is ever allowed the web ("@") label
3473 * and the star ("*") label.
3475 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3478 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3480 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3481 if (sklep
->smk_label
== skp
) {
3489 new = prepare_creds();
3493 tsp
= smack_cred(new);
3494 tsp
->smk_task
= skp
;
3496 * process can change its label only once
3498 smk_destroy_label_list(&tsp
->smk_relabel
);
3505 * smack_unix_stream_connect - Smack access on UDS
3507 * @other: the other sock
3510 * Return 0 if a subject with the smack of sock could access
3511 * an object with the smack of other, otherwise an error code
3513 static int smack_unix_stream_connect(struct sock
*sock
,
3514 struct sock
*other
, struct sock
*newsk
)
3516 struct smack_known
*skp
;
3517 struct smack_known
*okp
;
3518 struct socket_smack
*ssp
= smack_sock(sock
);
3519 struct socket_smack
*osp
= smack_sock(other
);
3520 struct socket_smack
*nsp
= smack_sock(newsk
);
3521 struct smk_audit_info ad
;
3524 struct lsm_network_audit net
;
3527 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3531 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3532 smk_ad_setfield_u_net_sk(&ad
, other
);
3534 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3535 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3539 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3540 rc
= smk_bu_note("UDS connect", okp
, skp
,
3546 * Cross reference the peer labels for SO_PEERSEC.
3549 nsp
->smk_packet
= ssp
->smk_out
;
3550 ssp
->smk_packet
= osp
->smk_out
;
3557 * smack_unix_may_send - Smack access on UDS
3559 * @other: the other socket
3561 * Return 0 if a subject with the smack of sock could access
3562 * an object with the smack of other, otherwise an error code
3564 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3566 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
3567 struct socket_smack
*osp
= smack_sock(other
->sk
);
3568 struct smk_audit_info ad
;
3572 struct lsm_network_audit net
;
3574 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3575 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3578 if (smack_privileged(CAP_MAC_OVERRIDE
))
3581 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3582 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3587 * smack_socket_sendmsg - Smack check based on destination host
3590 * @size: the size of the message
3592 * Return 0 if the current subject can write to the destination host.
3593 * For IPv4 this is only a question if the destination is a single label host.
3594 * For IPv6 this is a check against the label of the port.
3596 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3599 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3600 #if IS_ENABLED(CONFIG_IPV6)
3601 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3603 #ifdef SMACK_IPV6_SECMARK_LABELING
3604 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
3605 struct smack_known
*rsp
;
3610 * Perfectly reasonable for this to be NULL
3615 switch (sock
->sk
->sk_family
) {
3617 rc
= smack_netlabel_send(sock
->sk
, sip
);
3620 #ifdef SMACK_IPV6_SECMARK_LABELING
3621 rsp
= smack_ipv6host_label(sap
);
3623 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3626 #ifdef SMACK_IPV6_PORT_LABELING
3627 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3635 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3636 * @sap: netlabel secattr
3637 * @ssp: socket security information
3639 * Returns a pointer to a Smack label entry found on the label list.
3641 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3642 struct socket_smack
*ssp
)
3644 struct smack_known
*skp
;
3649 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3651 * Looks like a CIPSO packet.
3652 * If there are flags but no level netlabel isn't
3653 * behaving the way we expect it to.
3655 * Look it up in the label table
3656 * Without guidance regarding the smack value
3657 * for the packet fall back on the network
3661 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3662 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3665 * Compare the catsets. Use the netlbl APIs.
3667 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3668 if ((skp
->smk_netlabel
.flags
&
3669 NETLBL_SECATTR_MLS_CAT
) == 0)
3673 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3674 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3676 kcat
= netlbl_catmap_walk(
3677 skp
->smk_netlabel
.attr
.mls
.cat
,
3679 if (acat
< 0 || kcat
< 0)
3692 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3693 return &smack_known_web
;
3694 return &smack_known_star
;
3696 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3698 * Looks like a fallback, which gives us a secid.
3700 return smack_from_secid(sap
->attr
.secid
);
3702 * Without guidance regarding the smack value
3703 * for the packet fall back on the network
3706 return smack_net_ambient
;
3709 #if IS_ENABLED(CONFIG_IPV6)
3710 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3714 int proto
= -EINVAL
;
3715 struct ipv6hdr _ipv6h
;
3716 struct ipv6hdr
*ip6
;
3718 struct tcphdr _tcph
, *th
;
3719 struct udphdr _udph
, *uh
;
3720 struct dccp_hdr _dccph
, *dh
;
3724 offset
= skb_network_offset(skb
);
3725 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3728 sip
->sin6_addr
= ip6
->saddr
;
3730 nexthdr
= ip6
->nexthdr
;
3731 offset
+= sizeof(_ipv6h
);
3732 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3739 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3741 sip
->sin6_port
= th
->source
;
3744 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3746 sip
->sin6_port
= uh
->source
;
3749 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3751 sip
->sin6_port
= dh
->dccph_sport
;
3756 #endif /* CONFIG_IPV6 */
3759 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3763 * Returns 0 if the packet should be delivered, an error code otherwise
3765 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3767 struct netlbl_lsm_secattr secattr
;
3768 struct socket_smack
*ssp
= smack_sock(sk
);
3769 struct smack_known
*skp
= NULL
;
3771 struct smk_audit_info ad
;
3772 u16 family
= sk
->sk_family
;
3774 struct lsm_network_audit net
;
3776 #if IS_ENABLED(CONFIG_IPV6)
3777 struct sockaddr_in6 sadd
;
3780 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3782 #endif /* CONFIG_IPV6 */
3786 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3788 * If there is a secmark use it rather than the CIPSO label.
3789 * If there is no secmark fall back to CIPSO.
3790 * The secmark is assumed to reflect policy better.
3792 if (skb
&& skb
->secmark
!= 0) {
3793 skp
= smack_from_secid(skb
->secmark
);
3796 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3798 * Translate what netlabel gave us.
3800 netlbl_secattr_init(&secattr
);
3802 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3804 skp
= smack_from_secattr(&secattr
, ssp
);
3806 skp
= smack_net_ambient
;
3808 netlbl_secattr_destroy(&secattr
);
3810 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3814 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3815 ad
.a
.u
.net
->family
= family
;
3816 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3817 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3820 * Receiving a packet requires that the other end
3821 * be able to write here. Read access is not required.
3822 * This is the simplist possible security model
3825 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3826 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3829 netlbl_skbuff_err(skb
, family
, rc
, 0);
3831 #if IS_ENABLED(CONFIG_IPV6)
3833 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3834 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_TCP
)
3836 #ifdef SMACK_IPV6_SECMARK_LABELING
3837 if (skb
&& skb
->secmark
!= 0)
3838 skp
= smack_from_secid(skb
->secmark
);
3840 skp
= smack_ipv6host_label(&sadd
);
3842 skp
= smack_net_ambient
;
3846 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3847 ad
.a
.u
.net
->family
= family
;
3848 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3849 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3850 #endif /* CONFIG_AUDIT */
3851 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3852 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3854 #endif /* SMACK_IPV6_SECMARK_LABELING */
3855 #ifdef SMACK_IPV6_PORT_LABELING
3856 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3857 #endif /* SMACK_IPV6_PORT_LABELING */
3859 #endif /* CONFIG_IPV6 */
3866 * smack_socket_getpeersec_stream - pull in packet label
3868 * @optval: user's destination
3869 * @optlen: size thereof
3872 * returns zero on success, an error code otherwise
3874 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3875 char __user
*optval
,
3876 int __user
*optlen
, unsigned len
)
3878 struct socket_smack
*ssp
;
3883 ssp
= smack_sock(sock
->sk
);
3884 if (ssp
->smk_packet
!= NULL
) {
3885 rcp
= ssp
->smk_packet
->smk_known
;
3886 slen
= strlen(rcp
) + 1;
3891 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3894 if (put_user(slen
, optlen
) != 0)
3902 * smack_socket_getpeersec_dgram - pull in packet label
3903 * @sock: the peer socket
3905 * @secid: pointer to where to put the secid of the packet
3907 * Sets the netlabel socket state on sk from parent
3909 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3910 struct sk_buff
*skb
, u32
*secid
)
3913 struct netlbl_lsm_secattr secattr
;
3914 struct socket_smack
*ssp
= NULL
;
3915 struct smack_known
*skp
;
3916 int family
= PF_UNSPEC
;
3917 u32 s
= 0; /* 0 is the invalid secid */
3921 if (skb
->protocol
== htons(ETH_P_IP
))
3923 #if IS_ENABLED(CONFIG_IPV6)
3924 else if (skb
->protocol
== htons(ETH_P_IPV6
))
3926 #endif /* CONFIG_IPV6 */
3928 if (family
== PF_UNSPEC
&& sock
!= NULL
)
3929 family
= sock
->sk
->sk_family
;
3933 ssp
= smack_sock(sock
->sk
);
3934 s
= ssp
->smk_out
->smk_secid
;
3937 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3943 * Translate what netlabel gave us.
3945 if (sock
!= NULL
&& sock
->sk
!= NULL
)
3946 ssp
= smack_sock(sock
->sk
);
3947 netlbl_secattr_init(&secattr
);
3948 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3950 skp
= smack_from_secattr(&secattr
, ssp
);
3953 netlbl_secattr_destroy(&secattr
);
3956 #ifdef SMACK_IPV6_SECMARK_LABELING
3968 * smack_sock_graft - Initialize a newly created socket with an existing sock
3970 * @parent: parent socket
3972 * Set the smk_{in,out} state of an existing sock based on the process that
3973 * is creating the new socket.
3975 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3977 struct socket_smack
*ssp
;
3978 struct smack_known
*skp
= smk_of_current();
3981 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3984 ssp
= smack_sock(sk
);
3987 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3991 * smack_inet_conn_request - Smack access check on connect
3992 * @sk: socket involved
3996 * Returns 0 if a task with the packet label could write to
3997 * the socket, otherwise an error code
3999 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4000 struct request_sock
*req
)
4002 u16 family
= sk
->sk_family
;
4003 struct smack_known
*skp
;
4004 struct socket_smack
*ssp
= smack_sock(sk
);
4005 struct netlbl_lsm_secattr secattr
;
4006 struct sockaddr_in addr
;
4008 struct smack_known
*hskp
;
4010 struct smk_audit_info ad
;
4012 struct lsm_network_audit net
;
4015 #if IS_ENABLED(CONFIG_IPV6)
4016 if (family
== PF_INET6
) {
4018 * Handle mapped IPv4 packets arriving
4019 * via IPv6 sockets. Don't set up netlabel
4020 * processing on IPv6.
4022 if (skb
->protocol
== htons(ETH_P_IP
))
4027 #endif /* CONFIG_IPV6 */
4029 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4031 * If there is a secmark use it rather than the CIPSO label.
4032 * If there is no secmark fall back to CIPSO.
4033 * The secmark is assumed to reflect policy better.
4035 if (skb
&& skb
->secmark
!= 0) {
4036 skp
= smack_from_secid(skb
->secmark
);
4039 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4041 netlbl_secattr_init(&secattr
);
4042 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4044 skp
= smack_from_secattr(&secattr
, ssp
);
4046 skp
= &smack_known_huh
;
4047 netlbl_secattr_destroy(&secattr
);
4049 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4054 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4055 ad
.a
.u
.net
->family
= family
;
4056 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4057 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4060 * Receiving a packet requires that the other end be able to write
4061 * here. Read access is not required.
4063 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4064 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4069 * Save the peer's label in the request_sock so we can later setup
4070 * smk_packet in the child socket so that SO_PEERCRED can report it.
4072 req
->peer_secid
= skp
->smk_secid
;
4075 * We need to decide if we want to label the incoming connection here
4076 * if we do we only need to label the request_sock and the stack will
4077 * propagate the wire-label to the sock when it is created.
4080 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4082 hskp
= smack_ipv4host_label(&addr
);
4086 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4088 netlbl_req_delattr(req
);
4094 * smack_inet_csk_clone - Copy the connection information to the new socket
4095 * @sk: the new socket
4096 * @req: the connection's request_sock
4098 * Transfer the connection's peer label to the newly created socket.
4100 static void smack_inet_csk_clone(struct sock
*sk
,
4101 const struct request_sock
*req
)
4103 struct socket_smack
*ssp
= smack_sock(sk
);
4104 struct smack_known
*skp
;
4106 if (req
->peer_secid
!= 0) {
4107 skp
= smack_from_secid(req
->peer_secid
);
4108 ssp
->smk_packet
= skp
;
4110 ssp
->smk_packet
= NULL
;
4114 * Key management security hooks
4116 * Casey has not tested key support very heavily.
4117 * The permission check is most likely too restrictive.
4118 * If you care about keys please have a look.
4123 * smack_key_alloc - Set the key security blob
4125 * @cred: the credentials to use
4128 * No allocation required
4132 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4133 unsigned long flags
)
4135 struct smack_known
**blob
= smack_key(key
);
4136 struct smack_known
*skp
= smk_of_task(smack_cred(cred
));
4143 * smack_key_permission - Smack access on a key
4144 * @key_ref: gets to the object
4145 * @cred: the credentials to use
4146 * @perm: requested key permissions
4148 * Return 0 if the task has read and write to the object,
4149 * an error code otherwise
4151 static int smack_key_permission(key_ref_t key_ref
,
4152 const struct cred
*cred
, unsigned perm
)
4154 struct smack_known
**blob
;
4155 struct smack_known
*skp
;
4157 struct smk_audit_info ad
;
4158 struct smack_known
*tkp
= smk_of_task(smack_cred(cred
));
4163 * Validate requested permissions
4165 if (perm
& ~KEY_NEED_ALL
)
4168 keyp
= key_ref_to_ptr(key_ref
);
4172 * If the key hasn't been initialized give it access so that
4175 blob
= smack_key(keyp
);
4180 * This should not occur
4185 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4186 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4187 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4189 if (perm
& (KEY_NEED_READ
| KEY_NEED_SEARCH
| KEY_NEED_VIEW
))
4190 request
|= MAY_READ
;
4191 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4192 request
|= MAY_WRITE
;
4193 rc
= smk_access(tkp
, skp
, request
, &ad
);
4194 rc
= smk_bu_note("key access", tkp
, skp
, request
, rc
);
4199 * smack_key_getsecurity - Smack label tagging the key
4200 * @key points to the key to be queried
4201 * @_buffer points to a pointer that should be set to point to the
4202 * resulting string (if no label or an error occurs).
4203 * Return the length of the string (including terminating NUL) or -ve if
4205 * May also return 0 (and a NULL buffer pointer) if there is no label.
4207 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4209 struct smack_known
**blob
= smack_key(key
);
4210 struct smack_known
*skp
= *blob
;
4219 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4222 length
= strlen(copy
) + 1;
4228 #endif /* CONFIG_KEYS */
4233 * Audit requires a unique representation of each Smack specific
4234 * rule. This unique representation is used to distinguish the
4235 * object to be audited from remaining kernel objects and also
4236 * works as a glue between the audit hooks.
4238 * Since repository entries are added but never deleted, we'll use
4239 * the smack_known label address related to the given audit rule as
4240 * the needed unique representation. This also better fits the smack
4241 * model where nearly everything is a label.
4246 * smack_audit_rule_init - Initialize a smack audit rule
4247 * @field: audit rule fields given from user-space (audit.h)
4248 * @op: required testing operator (=, !=, >, <, ...)
4249 * @rulestr: smack label to be audited
4250 * @vrule: pointer to save our own audit rule representation
4252 * Prepare to audit cases where (@field @op @rulestr) is true.
4253 * The label to be audited is created if necessay.
4255 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4257 struct smack_known
*skp
;
4258 char **rule
= (char **)vrule
;
4261 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4264 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4267 skp
= smk_import_entry(rulestr
, 0);
4269 return PTR_ERR(skp
);
4271 *rule
= skp
->smk_known
;
4277 * smack_audit_rule_known - Distinguish Smack audit rules
4278 * @krule: rule of interest, in Audit kernel representation format
4280 * This is used to filter Smack rules from remaining Audit ones.
4281 * If it's proved that this rule belongs to us, the
4282 * audit_rule_match hook will be called to do the final judgement.
4284 static int smack_audit_rule_known(struct audit_krule
*krule
)
4286 struct audit_field
*f
;
4289 for (i
= 0; i
< krule
->field_count
; i
++) {
4290 f
= &krule
->fields
[i
];
4292 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4300 * smack_audit_rule_match - Audit given object ?
4301 * @secid: security id for identifying the object to test
4302 * @field: audit rule flags given from user-space
4303 * @op: required testing operator
4304 * @vrule: smack internal rule presentation
4305 * @actx: audit context associated with the check
4307 * The core Audit hook. It's used to take the decision of
4308 * whether to audit or not to audit a given object.
4310 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4311 struct audit_context
*actx
)
4313 struct smack_known
*skp
;
4316 if (unlikely(!rule
)) {
4317 WARN_ONCE(1, "Smack: missing rule\n");
4321 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4324 skp
= smack_from_secid(secid
);
4327 * No need to do string comparisons. If a match occurs,
4328 * both pointers will point to the same smack_known
4331 if (op
== Audit_equal
)
4332 return (rule
== skp
->smk_known
);
4333 if (op
== Audit_not_equal
)
4334 return (rule
!= skp
->smk_known
);
4340 * There is no need for a smack_audit_rule_free hook.
4341 * No memory was allocated.
4344 #endif /* CONFIG_AUDIT */
4347 * smack_ismaclabel - check if xattr @name references a smack MAC label
4348 * @name: Full xattr name to check.
4350 static int smack_ismaclabel(const char *name
)
4352 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4357 * smack_secid_to_secctx - return the smack label for a secid
4358 * @secid: incoming integer
4359 * @secdata: destination
4360 * @seclen: how long it is
4362 * Exists for networking code.
4364 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4366 struct smack_known
*skp
= smack_from_secid(secid
);
4369 *secdata
= skp
->smk_known
;
4370 *seclen
= strlen(skp
->smk_known
);
4375 * smack_secctx_to_secid - return the secid for a smack label
4376 * @secdata: smack label
4377 * @seclen: how long result is
4378 * @secid: outgoing integer
4380 * Exists for audit and networking code.
4382 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4384 struct smack_known
*skp
= smk_find_entry(secdata
);
4387 *secid
= skp
->smk_secid
;
4394 * There used to be a smack_release_secctx hook
4395 * that did nothing back when hooks were in a vector.
4396 * Now that there's a list such a hook adds cost.
4399 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4401 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4404 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4406 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4409 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4412 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
4420 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4423 struct task_smack
*tsp
;
4424 struct smack_known
*skp
;
4425 struct inode_smack
*isp
;
4426 struct cred
*new_creds
= *new;
4428 if (new_creds
== NULL
) {
4429 new_creds
= prepare_creds();
4430 if (new_creds
== NULL
)
4434 tsp
= smack_cred(new_creds
);
4437 * Get label from overlay inode and set it in create_sid
4439 isp
= smack_inode(d_inode(dentry
->d_parent
));
4440 skp
= isp
->smk_inode
;
4441 tsp
->smk_task
= skp
;
4446 static int smack_inode_copy_up_xattr(const char *name
)
4449 * Return 1 if this is the smack access Smack attribute.
4451 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4457 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4459 const struct cred
*old
,
4462 struct task_smack
*otsp
= smack_cred(old
);
4463 struct task_smack
*ntsp
= smack_cred(new);
4464 struct inode_smack
*isp
;
4468 * Use the process credential unless all of
4469 * the transmuting criteria are met
4471 ntsp
->smk_task
= otsp
->smk_task
;
4474 * the attribute of the containing directory
4476 isp
= smack_inode(d_inode(dentry
->d_parent
));
4478 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4480 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4481 isp
->smk_inode
->smk_known
,
4482 &otsp
->smk_task
->smk_rules
);
4486 * If the directory is transmuting and the rule
4487 * providing access is transmuting use the containing
4488 * directory label instead of the process label.
4490 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4491 ntsp
->smk_task
= isp
->smk_inode
;
4496 struct lsm_blob_sizes smack_blob_sizes
= {
4497 .lbs_cred
= sizeof(struct task_smack
),
4498 .lbs_file
= sizeof(struct smack_known
*),
4499 .lbs_inode
= sizeof(struct inode_smack
),
4500 .lbs_ipc
= sizeof(struct smack_known
*),
4502 .lbs_key
= sizeof(struct smack_known
*),
4503 #endif /* CONFIG_KEYS */
4504 .lbs_msg_msg
= sizeof(struct smack_known
*),
4505 .lbs_sock
= sizeof(struct socket_smack
),
4506 .lbs_superblock
= sizeof(struct superblock_smack
),
4509 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4510 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4511 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4512 LSM_HOOK_INIT(syslog
, smack_syslog
),
4514 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4515 LSM_HOOK_INIT(sb_copy_data
, smack_sb_copy_data
),
4516 LSM_HOOK_INIT(sb_kern_mount
, smack_sb_kern_mount
),
4517 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4518 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4519 LSM_HOOK_INIT(sb_parse_opts_str
, smack_parse_opts_str
),
4521 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4523 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4524 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4525 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4526 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4527 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4528 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4529 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4530 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4531 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4532 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4533 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4534 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4535 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4536 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4537 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4538 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4539 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4541 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4542 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4543 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4544 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4545 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4546 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4547 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4548 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4549 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4551 LSM_HOOK_INIT(file_open
, smack_file_open
),
4553 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4554 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4555 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4556 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4557 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4558 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4559 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4560 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4561 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4562 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4563 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4564 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4565 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4566 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4567 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4568 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4569 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4570 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4572 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4573 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4575 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4577 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_msg_queue_alloc_security
),
4578 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4579 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4580 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4581 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4583 LSM_HOOK_INIT(shm_alloc_security
, smack_shm_alloc_security
),
4584 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4585 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4586 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4588 LSM_HOOK_INIT(sem_alloc_security
, smack_sem_alloc_security
),
4589 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4590 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4591 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4593 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4595 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4596 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4598 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4599 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4601 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4602 #ifdef SMACK_IPV6_PORT_LABELING
4603 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4605 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4606 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4607 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4608 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4609 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4610 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4611 #ifdef SMACK_IPV6_PORT_LABELING
4612 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4614 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4615 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4616 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4618 /* key management security hooks */
4620 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4621 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4622 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4623 #endif /* CONFIG_KEYS */
4627 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4628 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4629 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4630 #endif /* CONFIG_AUDIT */
4632 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4633 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4634 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4635 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4636 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4637 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4638 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4639 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4640 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4644 static __init
void init_smack_known_list(void)
4647 * Initialize rule list locks
4649 mutex_init(&smack_known_huh
.smk_rules_lock
);
4650 mutex_init(&smack_known_hat
.smk_rules_lock
);
4651 mutex_init(&smack_known_floor
.smk_rules_lock
);
4652 mutex_init(&smack_known_star
.smk_rules_lock
);
4653 mutex_init(&smack_known_web
.smk_rules_lock
);
4655 * Initialize rule lists
4657 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4658 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4659 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4660 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4661 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4663 * Create the known labels list
4665 smk_insert_entry(&smack_known_huh
);
4666 smk_insert_entry(&smack_known_hat
);
4667 smk_insert_entry(&smack_known_star
);
4668 smk_insert_entry(&smack_known_floor
);
4669 smk_insert_entry(&smack_known_web
);
4673 * smack_init - initialize the smack system
4677 static __init
int smack_init(void)
4680 struct cred
*cred
= (struct cred
*) current
->cred
;
4681 struct task_smack
*tsp
;
4683 if (!security_module_enable("smack",
4684 IS_ENABLED(CONFIG_SECURITY_SMACK_STACKED
)))
4688 security_add_blobs(&smack_blob_sizes
);
4693 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4694 if (!smack_inode_cache
)
4697 lsm_early_cred(cred
);
4700 * Set the security state for the initial task.
4702 tsp
= smack_cred(cred
);
4703 init_task_smack(tsp
, &smack_known_floor
, &smack_known_floor
);
4708 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4711 pr_info("Smack: Initializing.\n");
4712 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4713 pr_info("Smack: Netfilter enabled.\n");
4715 #ifdef SMACK_IPV6_PORT_LABELING
4716 pr_info("Smack: IPv6 port labeling enabled.\n");
4718 #ifdef SMACK_IPV6_SECMARK_LABELING
4719 pr_info("Smack: IPv6 Netfilter enabled.\n");
4722 /* initialize the smack_known_list */
4723 init_smack_known_list();
4729 * Smack requires early initialization in order to label
4730 * all processes and objects when they are created.
4732 security_initcall(smack_init
);