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_KERNEL
);
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
,
911 } else if (bprm
->unsafe
)
914 bsp
->smk_task
= isp
->smk_task
;
915 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
917 /* Decide if this is a secure exec. */
918 if (bsp
->smk_task
!= bsp
->smk_forked
)
919 bprm
->secureexec
= 1;
929 * smack_inode_alloc_security - allocate an inode blob
930 * @inode: the inode in need of a blob
932 * Returns 0 if it gets a blob, -ENOMEM otherwise
934 static int smack_inode_alloc_security(struct inode
*inode
)
936 struct smack_known
*skp
= smk_of_current();
938 init_inode_smack(inode
, skp
);
943 * smack_inode_init_security - copy out the smack from an inode
944 * @inode: the newly created inode
945 * @dir: containing directory object
947 * @name: where to put the attribute name
948 * @value: where to put the attribute value
949 * @len: where to put the length of the attribute
951 * Returns 0 if it all works out, -ENOMEM if there's no memory
953 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
954 const struct qstr
*qstr
, const char **name
,
955 void **value
, size_t *len
)
957 struct inode_smack
*issp
= smack_inode(inode
);
958 struct smack_known
*skp
= smk_of_current();
959 struct smack_known
*isp
= smk_of_inode(inode
);
960 struct smack_known
*dsp
= smk_of_inode(dir
);
964 *name
= XATTR_SMACK_SUFFIX
;
968 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
973 * If the access rule allows transmutation and
974 * the directory requests transmutation then
975 * by all means transmute.
976 * Mark the inode as changed.
978 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
979 smk_inode_transmutable(dir
)) {
981 issp
->smk_flags
|= SMK_INODE_CHANGED
;
984 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
988 *len
= strlen(isp
->smk_known
);
995 * smack_inode_link - Smack check on link
996 * @old_dentry: the existing object
998 * @new_dentry: the new object
1000 * Returns 0 if access is permitted, an error code otherwise
1002 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1003 struct dentry
*new_dentry
)
1005 struct smack_known
*isp
;
1006 struct smk_audit_info ad
;
1009 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1010 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1012 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1013 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1014 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1016 if (rc
== 0 && d_is_positive(new_dentry
)) {
1017 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1018 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1019 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1020 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1027 * smack_inode_unlink - Smack check on inode deletion
1028 * @dir: containing directory object
1029 * @dentry: file to unlink
1031 * Returns 0 if current can write the containing directory
1032 * and the object, error code otherwise
1034 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1036 struct inode
*ip
= d_backing_inode(dentry
);
1037 struct smk_audit_info ad
;
1040 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1041 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1044 * You need write access to the thing you're unlinking
1046 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1047 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1050 * You also need write access to the containing directory
1052 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1053 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1054 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1055 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1061 * smack_inode_rmdir - Smack check on directory deletion
1062 * @dir: containing directory object
1063 * @dentry: directory to unlink
1065 * Returns 0 if current can write the containing directory
1066 * and the directory, error code otherwise
1068 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1070 struct smk_audit_info ad
;
1073 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1074 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1077 * You need write access to the thing you're removing
1079 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1080 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1083 * You also need write access to the containing directory
1085 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1086 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1087 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1088 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1095 * smack_inode_rename - Smack check on rename
1096 * @old_inode: unused
1097 * @old_dentry: the old object
1098 * @new_inode: unused
1099 * @new_dentry: the new object
1101 * Read and write access is required on both the old and
1104 * Returns 0 if access is permitted, an error code otherwise
1106 static int smack_inode_rename(struct inode
*old_inode
,
1107 struct dentry
*old_dentry
,
1108 struct inode
*new_inode
,
1109 struct dentry
*new_dentry
)
1112 struct smack_known
*isp
;
1113 struct smk_audit_info ad
;
1115 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1116 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1118 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1119 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1120 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1122 if (rc
== 0 && d_is_positive(new_dentry
)) {
1123 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1124 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1125 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1126 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1132 * smack_inode_permission - Smack version of permission()
1133 * @inode: the inode in question
1134 * @mask: the access requested
1136 * This is the important Smack hook.
1138 * Returns 0 if access is permitted, -EACCES otherwise
1140 static int smack_inode_permission(struct inode
*inode
, int mask
)
1142 struct superblock_smack
*sbsp
= smack_superblock(inode
->i_sb
);
1143 struct smk_audit_info ad
;
1144 int no_block
= mask
& MAY_NOT_BLOCK
;
1147 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1149 * No permission to check. Existence test. Yup, it's there.
1154 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1155 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1159 /* May be droppable after audit */
1162 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1163 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1164 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1165 rc
= smk_bu_inode(inode
, mask
, rc
);
1170 * smack_inode_setattr - Smack check for setting attributes
1171 * @dentry: the object
1172 * @iattr: for the force flag
1174 * Returns 0 if access is permitted, an error code otherwise
1176 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1178 struct smk_audit_info ad
;
1182 * Need to allow for clearing the setuid bit.
1184 if (iattr
->ia_valid
& ATTR_FORCE
)
1186 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1187 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1189 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1190 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1195 * smack_inode_getattr - Smack check for getting attributes
1196 * @mnt: vfsmount of the object
1197 * @dentry: the object
1199 * Returns 0 if access is permitted, an error code otherwise
1201 static int smack_inode_getattr(const struct path
*path
)
1203 struct smk_audit_info ad
;
1204 struct inode
*inode
= d_backing_inode(path
->dentry
);
1207 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1208 smk_ad_setfield_u_fs_path(&ad
, *path
);
1209 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1210 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1215 * smack_inode_setxattr - Smack check for setting xattrs
1216 * @dentry: the object
1217 * @name: name of the attribute
1218 * @value: value of the attribute
1219 * @size: size of the value
1222 * This protects the Smack attribute explicitly.
1224 * Returns 0 if access is permitted, an error code otherwise
1226 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1227 const void *value
, size_t size
, int flags
)
1229 struct smk_audit_info ad
;
1230 struct smack_known
*skp
;
1232 int check_import
= 0;
1237 * Check label validity here so import won't fail in post_setxattr
1239 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1240 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1241 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1244 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1245 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1249 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1251 if (size
!= TRANS_TRUE_SIZE
||
1252 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1255 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1257 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1260 if (rc
== 0 && check_import
) {
1261 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1264 else if (skp
== NULL
|| (check_star
&&
1265 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1269 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1270 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1273 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1274 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1281 * smack_inode_post_setxattr - Apply the Smack update approved above
1283 * @name: attribute name
1284 * @value: attribute value
1285 * @size: attribute size
1288 * Set the pointer in the inode blob to the entry found
1289 * in the master label list.
1291 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1292 const void *value
, size_t size
, int flags
)
1294 struct smack_known
*skp
;
1295 struct inode_smack
*isp
= smack_inode(d_backing_inode(dentry
));
1297 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1298 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1302 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1303 skp
= smk_import_entry(value
, size
);
1305 isp
->smk_inode
= skp
;
1306 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1307 skp
= smk_import_entry(value
, size
);
1309 isp
->smk_task
= skp
;
1310 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1311 skp
= smk_import_entry(value
, size
);
1313 isp
->smk_mmap
= skp
;
1320 * smack_inode_getxattr - Smack check on getxattr
1321 * @dentry: the object
1324 * Returns 0 if access is permitted, an error code otherwise
1326 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1328 struct smk_audit_info ad
;
1331 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1332 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1334 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1335 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1340 * smack_inode_removexattr - Smack check on removexattr
1341 * @dentry: the object
1342 * @name: name of the attribute
1344 * Removing the Smack attribute requires CAP_MAC_ADMIN
1346 * Returns 0 if access is permitted, an error code otherwise
1348 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1350 struct inode_smack
*isp
;
1351 struct smk_audit_info ad
;
1354 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1355 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1356 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1357 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1358 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1359 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1360 if (!smack_privileged(CAP_MAC_ADMIN
))
1363 rc
= cap_inode_removexattr(dentry
, name
);
1368 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1369 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1371 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1372 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1376 isp
= smack_inode(d_backing_inode(dentry
));
1378 * Don't do anything special for these.
1379 * XATTR_NAME_SMACKIPIN
1380 * XATTR_NAME_SMACKIPOUT
1382 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1383 struct super_block
*sbp
= dentry
->d_sb
;
1384 struct superblock_smack
*sbsp
= smack_superblock(sbp
);
1386 isp
->smk_inode
= sbsp
->smk_default
;
1387 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1388 isp
->smk_task
= NULL
;
1389 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1390 isp
->smk_mmap
= NULL
;
1391 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1392 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1398 * smack_inode_getsecurity - get smack xattrs
1399 * @inode: the object
1400 * @name: attribute name
1401 * @buffer: where to put the result
1402 * @alloc: duplicate memory
1404 * Returns the size of the attribute or an error code
1406 static int smack_inode_getsecurity(struct inode
*inode
,
1407 const char *name
, void **buffer
,
1410 struct socket_smack
*ssp
;
1411 struct socket
*sock
;
1412 struct super_block
*sbp
;
1413 struct inode
*ip
= (struct inode
*)inode
;
1414 struct smack_known
*isp
;
1416 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1417 isp
= smk_of_inode(inode
);
1420 * The rest of the Smack xattrs are only on sockets.
1423 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1426 sock
= SOCKET_I(ip
);
1427 if (sock
== NULL
|| sock
->sk
== NULL
)
1430 ssp
= smack_sock(sock
->sk
);
1432 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1434 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1441 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1442 if (*buffer
== NULL
)
1446 return strlen(isp
->smk_known
);
1451 * smack_inode_listsecurity - list the Smack attributes
1452 * @inode: the object
1453 * @buffer: where they go
1454 * @buffer_size: size of buffer
1456 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1459 int len
= sizeof(XATTR_NAME_SMACK
);
1461 if (buffer
!= NULL
&& len
<= buffer_size
)
1462 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1468 * smack_inode_getsecid - Extract inode's security id
1469 * @inode: inode to extract the info from
1470 * @secid: where result will be saved
1472 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1474 struct inode_smack
*isp
= smack_inode(inode
);
1476 *secid
= isp
->smk_inode
->smk_secid
;
1484 * There is no smack_file_permission hook
1486 * Should access checks be done on each read or write?
1487 * UNICOS and SELinux say yes.
1488 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1490 * I'll say no for now. Smack does not do the frequent
1491 * label changing that SELinux does.
1495 * smack_file_alloc_security - assign a file security blob
1498 * The security blob for a file is a pointer to the master
1499 * label list, so no allocation is done.
1501 * f_security is the owner security information. It
1502 * isn't used on file access checks, it's for send_sigio.
1506 static int smack_file_alloc_security(struct file
*file
)
1508 struct smack_known
**blob
= smack_file(file
);
1510 *blob
= smk_of_current();
1515 * smack_file_ioctl - Smack check on ioctls
1520 * Relies heavily on the correct use of the ioctl command conventions.
1522 * Returns 0 if allowed, error code otherwise
1524 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1528 struct smk_audit_info ad
;
1529 struct inode
*inode
= file_inode(file
);
1531 if (unlikely(IS_PRIVATE(inode
)))
1534 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1535 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1537 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1538 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1539 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1542 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1543 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1544 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1551 * smack_file_lock - Smack check on file locking
1555 * Returns 0 if current has lock access, error code otherwise
1557 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1559 struct smk_audit_info ad
;
1561 struct inode
*inode
= file_inode(file
);
1563 if (unlikely(IS_PRIVATE(inode
)))
1566 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1567 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1568 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1569 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1574 * smack_file_fcntl - Smack check on fcntl
1576 * @cmd: what action to check
1579 * Generally these operations are harmless.
1580 * File locking operations present an obvious mechanism
1581 * for passing information, so they require write access.
1583 * Returns 0 if current has access, error code otherwise
1585 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1588 struct smk_audit_info ad
;
1590 struct inode
*inode
= file_inode(file
);
1592 if (unlikely(IS_PRIVATE(inode
)))
1600 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1601 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1602 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1603 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1607 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1608 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1609 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1610 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1621 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1622 * if mapping anonymous memory.
1623 * @file contains the file structure for file to map (may be NULL).
1624 * @reqprot contains the protection requested by the application.
1625 * @prot contains the protection that will be applied by the kernel.
1626 * @flags contains the operational flags.
1627 * Return 0 if permission is granted.
1629 static int smack_mmap_file(struct file
*file
,
1630 unsigned long reqprot
, unsigned long prot
,
1631 unsigned long flags
)
1633 struct smack_known
*skp
;
1634 struct smack_known
*mkp
;
1635 struct smack_rule
*srp
;
1636 struct task_smack
*tsp
;
1637 struct smack_known
*okp
;
1638 struct inode_smack
*isp
;
1639 struct superblock_smack
*sbsp
;
1648 if (unlikely(IS_PRIVATE(file_inode(file
))))
1651 isp
= smack_inode(file_inode(file
));
1652 if (isp
->smk_mmap
== NULL
)
1654 sbsp
= smack_superblock(file_inode(file
)->i_sb
);
1655 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1656 isp
->smk_mmap
!= sbsp
->smk_root
)
1658 mkp
= isp
->smk_mmap
;
1660 tsp
= smack_cred(current_cred());
1661 skp
= smk_of_current();
1666 * For each Smack rule associated with the subject
1667 * label verify that the SMACK64MMAP also has access
1668 * to that rule's object label.
1670 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1671 okp
= srp
->smk_object
;
1673 * Matching labels always allows access.
1675 if (mkp
->smk_known
== okp
->smk_known
)
1678 * If there is a matching local rule take
1679 * that into account as well.
1681 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1685 may
= srp
->smk_access
;
1687 may
&= srp
->smk_access
;
1689 * If may is zero the SMACK64MMAP subject can't
1690 * possibly have less access.
1696 * Fetch the global list entry.
1697 * If there isn't one a SMACK64MMAP subject
1698 * can't have as much access as current.
1700 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1702 if (mmay
== -ENOENT
) {
1707 * If there is a local entry it modifies the
1708 * potential access, too.
1710 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1712 if (tmay
!= -ENOENT
)
1716 * If there is any access available to current that is
1717 * not available to a SMACK64MMAP subject
1720 if ((may
| mmay
) != mmay
) {
1732 * smack_file_set_fowner - set the file security blob value
1733 * @file: object in question
1736 static void smack_file_set_fowner(struct file
*file
)
1738 struct smack_known
**blob
= smack_file(file
);
1740 *blob
= smk_of_current();
1744 * smack_file_send_sigiotask - Smack on sigio
1745 * @tsk: The target task
1746 * @fown: the object the signal come from
1749 * Allow a privileged task to get signals even if it shouldn't
1751 * Returns 0 if a subject with the object's smack could
1752 * write to the task, an error code otherwise.
1754 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1755 struct fown_struct
*fown
, int signum
)
1757 struct smack_known
**blob
;
1758 struct smack_known
*skp
;
1759 struct smack_known
*tkp
= smk_of_task(smack_cred(tsk
->cred
));
1762 struct smk_audit_info ad
;
1765 * struct fown_struct is never outside the context of a struct file
1767 file
= container_of(fown
, struct file
, f_owner
);
1769 /* we don't log here as rc can be overriden */
1770 blob
= smack_file(file
);
1772 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1773 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1774 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1777 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1778 smk_ad_setfield_u_tsk(&ad
, tsk
);
1779 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1784 * smack_file_receive - Smack file receive check
1787 * Returns 0 if current has access, error code otherwise
1789 static int smack_file_receive(struct file
*file
)
1793 struct smk_audit_info ad
;
1794 struct inode
*inode
= file_inode(file
);
1795 struct socket
*sock
;
1796 struct task_smack
*tsp
;
1797 struct socket_smack
*ssp
;
1799 if (unlikely(IS_PRIVATE(inode
)))
1802 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1803 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1805 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1806 sock
= SOCKET_I(inode
);
1807 ssp
= smack_sock(sock
->sk
);
1808 tsp
= smack_cred(current_cred());
1810 * If the receiving process can't write to the
1811 * passed socket or if the passed socket can't
1812 * write to the receiving process don't accept
1813 * the passed socket.
1815 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1816 rc
= smk_bu_file(file
, may
, rc
);
1819 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1820 rc
= smk_bu_file(file
, may
, rc
);
1824 * This code relies on bitmasks.
1826 if (file
->f_mode
& FMODE_READ
)
1828 if (file
->f_mode
& FMODE_WRITE
)
1831 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1832 rc
= smk_bu_file(file
, may
, rc
);
1837 * smack_file_open - Smack dentry open processing
1839 * @cred: task credential
1841 * Set the security blob in the file structure.
1842 * Allow the open only if the task has read access. There are
1843 * many read operations (e.g. fstat) that you can do with an
1844 * fd even if you have the file open write-only.
1848 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1850 struct task_smack
*tsp
= smack_cred(cred
);
1851 struct inode
*inode
= file_inode(file
);
1852 struct smk_audit_info ad
;
1855 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1856 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1857 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1858 rc
= smk_bu_credfile(cred
, file
, MAY_READ
, rc
);
1868 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1869 * @new: the new credentials
1870 * @gfp: the atomicity of any memory allocations
1872 * Prepare a blank set of credentials for modification. This must allocate all
1873 * the memory the LSM module might require such that cred_transfer() can
1874 * complete without error.
1876 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1878 init_task_smack(smack_cred(cred
), NULL
, NULL
);
1884 * smack_cred_free - "free" task-level security credentials
1885 * @cred: the credentials in question
1888 static void smack_cred_free(struct cred
*cred
)
1890 struct task_smack
*tsp
= smack_cred(cred
);
1891 struct smack_rule
*rp
;
1892 struct list_head
*l
;
1893 struct list_head
*n
;
1895 smk_destroy_label_list(&tsp
->smk_relabel
);
1897 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1898 rp
= list_entry(l
, struct smack_rule
, list
);
1899 list_del(&rp
->list
);
1905 * smack_cred_prepare - prepare new set of credentials for modification
1906 * @new: the new credentials
1907 * @old: the original credentials
1908 * @gfp: the atomicity of any memory allocations
1910 * Prepare a new set of credentials for modification.
1912 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1915 struct task_smack
*old_tsp
= smack_cred(old
);
1916 struct task_smack
*new_tsp
= smack_cred(new);
1919 init_task_smack(new_tsp
, old_tsp
->smk_task
, old_tsp
->smk_task
);
1921 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1925 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
1931 * smack_cred_transfer - Transfer the old credentials to the new credentials
1932 * @new: the new credentials
1933 * @old: the original credentials
1935 * Fill in a set of blank credentials from another set of credentials.
1937 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1939 struct task_smack
*old_tsp
= smack_cred(old
);
1940 struct task_smack
*new_tsp
= smack_cred(new);
1942 new_tsp
->smk_task
= old_tsp
->smk_task
;
1943 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1944 mutex_init(&new_tsp
->smk_rules_lock
);
1945 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1947 /* cbs copy rule list */
1951 * smack_kernel_act_as - Set the subjective context in a set of credentials
1952 * @new: points to the set of credentials to be modified.
1953 * @secid: specifies the security ID to be set
1955 * Set the security data for a kernel service.
1957 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1959 struct task_smack
*new_tsp
= smack_cred(new);
1961 new_tsp
->smk_task
= smack_from_secid(secid
);
1966 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1967 * @new: points to the set of credentials to be modified
1968 * @inode: points to the inode to use as a reference
1970 * Set the file creation context in a set of credentials to the same
1971 * as the objective context of the specified inode
1973 static int smack_kernel_create_files_as(struct cred
*new,
1974 struct inode
*inode
)
1976 struct inode_smack
*isp
= smack_inode(inode
);
1977 struct task_smack
*tsp
= smack_cred(new);
1979 tsp
->smk_forked
= isp
->smk_inode
;
1980 tsp
->smk_task
= tsp
->smk_forked
;
1985 * smk_curacc_on_task - helper to log task related access
1986 * @p: the task object
1987 * @access: the access requested
1988 * @caller: name of the calling function for audit
1990 * Return 0 if access is permitted
1992 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
1995 struct smk_audit_info ad
;
1996 struct smack_known
*skp
= smk_of_task_struct(p
);
1999 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2000 smk_ad_setfield_u_tsk(&ad
, p
);
2001 rc
= smk_curacc(skp
, access
, &ad
);
2002 rc
= smk_bu_task(p
, access
, rc
);
2007 * smack_task_setpgid - Smack check on setting pgid
2008 * @p: the task object
2011 * Return 0 if write access is permitted
2013 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2015 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2019 * smack_task_getpgid - Smack access check for getpgid
2020 * @p: the object task
2022 * Returns 0 if current can read the object task, error code otherwise
2024 static int smack_task_getpgid(struct task_struct
*p
)
2026 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2030 * smack_task_getsid - Smack access check for getsid
2031 * @p: the object task
2033 * Returns 0 if current can read the object task, error code otherwise
2035 static int smack_task_getsid(struct task_struct
*p
)
2037 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2041 * smack_task_getsecid - get the secid of the task
2042 * @p: the object task
2043 * @secid: where to put the result
2045 * Sets the secid to contain a u32 version of the smack label.
2047 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2049 struct smack_known
*skp
= smk_of_task_struct(p
);
2051 *secid
= skp
->smk_secid
;
2055 * smack_task_setnice - Smack check on setting nice
2056 * @p: the task object
2059 * Return 0 if write access is permitted
2061 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2063 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2067 * smack_task_setioprio - Smack check on setting ioprio
2068 * @p: the task object
2071 * Return 0 if write access is permitted
2073 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2075 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2079 * smack_task_getioprio - Smack check on reading ioprio
2080 * @p: the task object
2082 * Return 0 if read access is permitted
2084 static int smack_task_getioprio(struct task_struct
*p
)
2086 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2090 * smack_task_setscheduler - Smack check on setting scheduler
2091 * @p: the task object
2095 * Return 0 if read access is permitted
2097 static int smack_task_setscheduler(struct task_struct
*p
)
2099 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2103 * smack_task_getscheduler - Smack check on reading scheduler
2104 * @p: the task object
2106 * Return 0 if read access is permitted
2108 static int smack_task_getscheduler(struct task_struct
*p
)
2110 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2114 * smack_task_movememory - Smack check on moving memory
2115 * @p: the task object
2117 * Return 0 if write access is permitted
2119 static int smack_task_movememory(struct task_struct
*p
)
2121 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2125 * smack_task_kill - Smack check on signal delivery
2126 * @p: the task object
2129 * @secid: identifies the smack to use in lieu of current's
2131 * Return 0 if write access is permitted
2133 * The secid behavior is an artifact of an SELinux hack
2134 * in the USB code. Someday it may go away.
2136 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
2139 struct smk_audit_info ad
;
2140 struct smack_known
*skp
;
2141 struct smack_known
*tkp
= smk_of_task_struct(p
);
2145 return 0; /* null signal; existence test */
2147 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2148 smk_ad_setfield_u_tsk(&ad
, p
);
2150 * Sending a signal requires that the sender
2151 * can write the receiver.
2154 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2155 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2159 * If the secid isn't 0 we're dealing with some USB IO
2160 * specific behavior. This is not clean. For one thing
2161 * we can't take privilege into account.
2163 skp
= smack_from_secid(secid
);
2164 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2165 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2170 * smack_task_to_inode - copy task smack into the inode blob
2171 * @p: task to copy from
2172 * @inode: inode to copy to
2174 * Sets the smack pointer in the inode security blob
2176 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2178 struct inode_smack
*isp
= smack_inode(inode
);
2179 struct smack_known
*skp
= smk_of_task_struct(p
);
2181 isp
->smk_inode
= skp
;
2182 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2190 * smack_sk_alloc_security - Allocate a socket blob
2193 * @gfp_flags: memory allocation flags
2195 * Assign Smack pointers to current
2197 * Returns 0 on success, -ENOMEM is there's no memory
2199 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2201 struct smack_known
*skp
= smk_of_current();
2202 struct socket_smack
*ssp
= smack_sock(sk
);
2205 * Sockets created by kernel threads receive web label.
2207 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2208 ssp
->smk_in
= &smack_known_web
;
2209 ssp
->smk_out
= &smack_known_web
;
2214 ssp
->smk_packet
= NULL
;
2219 #ifdef SMACK_IPV6_PORT_LABELING
2221 * smack_sk_free_security - Free a socket blob
2224 * Clears the blob pointer
2226 static void smack_sk_free_security(struct sock
*sk
)
2228 struct smk_port_label
*spp
;
2230 if (sk
->sk_family
== PF_INET6
) {
2232 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2233 if (spp
->smk_sock
!= sk
)
2235 spp
->smk_can_reuse
= 1;
2244 * smack_ipv4host_label - check host based restrictions
2245 * @sip: the object end
2247 * looks for host based access restrictions
2249 * This version will only be appropriate for really small sets of single label
2250 * hosts. The caller is responsible for ensuring that the RCU read lock is
2251 * taken before calling this function.
2253 * Returns the label of the far end or NULL if it's not special.
2255 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2257 struct smk_net4addr
*snp
;
2258 struct in_addr
*siap
= &sip
->sin_addr
;
2260 if (siap
->s_addr
== 0)
2263 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2265 * we break after finding the first match because
2266 * the list is sorted from longest to shortest mask
2267 * so we have found the most specific match
2269 if (snp
->smk_host
.s_addr
==
2270 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2271 return snp
->smk_label
;
2276 #if IS_ENABLED(CONFIG_IPV6)
2278 * smk_ipv6_localhost - Check for local ipv6 host address
2281 * Returns boolean true if this is the localhost address
2283 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2285 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2286 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2288 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2289 ntohs(be16p
[7]) == 1)
2295 * smack_ipv6host_label - check host based restrictions
2296 * @sip: the object end
2298 * looks for host based access restrictions
2300 * This version will only be appropriate for really small sets of single label
2301 * hosts. The caller is responsible for ensuring that the RCU read lock is
2302 * taken before calling this function.
2304 * Returns the label of the far end or NULL if it's not special.
2306 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2308 struct smk_net6addr
*snp
;
2309 struct in6_addr
*sap
= &sip
->sin6_addr
;
2314 * It's local. Don't look for a host label.
2316 if (smk_ipv6_localhost(sip
))
2319 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2321 * If the label is NULL the entry has
2322 * been renounced. Ignore it.
2324 if (snp
->smk_label
== NULL
)
2327 * we break after finding the first match because
2328 * the list is sorted from longest to shortest mask
2329 * so we have found the most specific match
2331 for (found
= 1, i
= 0; i
< 8; i
++) {
2332 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2333 snp
->smk_host
.s6_addr16
[i
]) {
2339 return snp
->smk_label
;
2344 #endif /* CONFIG_IPV6 */
2347 * smack_netlabel - Set the secattr on a socket
2349 * @labeled: socket label scheme
2351 * Convert the outbound smack value (smk_out) to a
2352 * secattr and attach it to the socket.
2354 * Returns 0 on success or an error code
2356 static int smack_netlabel(struct sock
*sk
, int labeled
)
2358 struct smack_known
*skp
;
2359 struct socket_smack
*ssp
= smack_sock(sk
);
2363 * Usually the netlabel code will handle changing the
2364 * packet labeling based on the label.
2365 * The case of a single label host is different, because
2366 * a single label host should never get a labeled packet
2367 * even though the label is usually associated with a packet
2371 bh_lock_sock_nested(sk
);
2373 if (ssp
->smk_out
== smack_net_ambient
||
2374 labeled
== SMACK_UNLABELED_SOCKET
)
2375 netlbl_sock_delattr(sk
);
2378 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2388 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2390 * @sap: the destination address
2392 * Set the correct secattr for the given socket based on the destination
2393 * address and perform any outbound access checks needed.
2395 * Returns 0 on success or an error code.
2398 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2400 struct smack_known
*skp
;
2403 struct smack_known
*hkp
;
2404 struct socket_smack
*ssp
= smack_sock(sk
);
2405 struct smk_audit_info ad
;
2408 hkp
= smack_ipv4host_label(sap
);
2411 struct lsm_network_audit net
;
2413 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2414 ad
.a
.u
.net
->family
= sap
->sin_family
;
2415 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2416 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2418 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2420 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2421 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2423 sk_lbl
= SMACK_CIPSO_SOCKET
;
2430 return smack_netlabel(sk
, sk_lbl
);
2433 #if IS_ENABLED(CONFIG_IPV6)
2435 * smk_ipv6_check - check Smack access
2436 * @subject: subject Smack label
2437 * @object: object Smack label
2439 * @act: the action being taken
2441 * Check an IPv6 access
2443 static int smk_ipv6_check(struct smack_known
*subject
,
2444 struct smack_known
*object
,
2445 struct sockaddr_in6
*address
, int act
)
2448 struct lsm_network_audit net
;
2450 struct smk_audit_info ad
;
2454 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2455 ad
.a
.u
.net
->family
= PF_INET6
;
2456 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2457 if (act
== SMK_RECEIVING
)
2458 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2460 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2462 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2463 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2466 #endif /* CONFIG_IPV6 */
2468 #ifdef SMACK_IPV6_PORT_LABELING
2470 * smk_ipv6_port_label - Smack port access table management
2474 * Create or update the port list entry
2476 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2478 struct sock
*sk
= sock
->sk
;
2479 struct sockaddr_in6
*addr6
;
2480 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
2481 struct smk_port_label
*spp
;
2482 unsigned short port
= 0;
2484 if (address
== NULL
) {
2486 * This operation is changing the Smack information
2487 * on the bound socket. Take the changes to the port
2491 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2492 if (sk
!= spp
->smk_sock
)
2494 spp
->smk_in
= ssp
->smk_in
;
2495 spp
->smk_out
= ssp
->smk_out
;
2500 * A NULL address is only used for updating existing
2501 * bound entries. If there isn't one, it's OK.
2507 addr6
= (struct sockaddr_in6
*)address
;
2508 port
= ntohs(addr6
->sin6_port
);
2510 * This is a special case that is safely ignored.
2516 * Look for an existing port list entry.
2517 * This is an indication that a port is getting reused.
2520 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2521 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2523 if (spp
->smk_can_reuse
!= 1) {
2527 spp
->smk_port
= port
;
2529 spp
->smk_in
= ssp
->smk_in
;
2530 spp
->smk_out
= ssp
->smk_out
;
2531 spp
->smk_can_reuse
= 0;
2537 * A new port entry is required.
2539 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2543 spp
->smk_port
= port
;
2545 spp
->smk_in
= ssp
->smk_in
;
2546 spp
->smk_out
= ssp
->smk_out
;
2547 spp
->smk_sock_type
= sock
->type
;
2548 spp
->smk_can_reuse
= 0;
2550 mutex_lock(&smack_ipv6_lock
);
2551 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2552 mutex_unlock(&smack_ipv6_lock
);
2557 * smk_ipv6_port_check - check Smack port access
2561 * Create or update the port list entry
2563 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2566 struct smk_port_label
*spp
;
2567 struct socket_smack
*ssp
= smack_sock(sk
);
2568 struct smack_known
*skp
= NULL
;
2569 unsigned short port
;
2570 struct smack_known
*object
;
2572 if (act
== SMK_RECEIVING
) {
2573 skp
= smack_ipv6host_label(address
);
2574 object
= ssp
->smk_in
;
2577 object
= smack_ipv6host_label(address
);
2581 * The other end is a single label host.
2583 if (skp
!= NULL
&& object
!= NULL
)
2584 return smk_ipv6_check(skp
, object
, address
, act
);
2586 skp
= smack_net_ambient
;
2588 object
= smack_net_ambient
;
2591 * It's remote, so port lookup does no good.
2593 if (!smk_ipv6_localhost(address
))
2594 return smk_ipv6_check(skp
, object
, address
, act
);
2597 * It's local so the send check has to have passed.
2599 if (act
== SMK_RECEIVING
)
2602 port
= ntohs(address
->sin6_port
);
2604 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2605 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2607 object
= spp
->smk_in
;
2608 if (act
== SMK_CONNECTING
)
2609 ssp
->smk_packet
= spp
->smk_out
;
2614 return smk_ipv6_check(skp
, object
, address
, act
);
2616 #endif /* SMACK_IPV6_PORT_LABELING */
2619 * smack_inode_setsecurity - set smack xattrs
2620 * @inode: the object
2621 * @name: attribute name
2622 * @value: attribute value
2623 * @size: size of the attribute
2626 * Sets the named attribute in the appropriate blob
2628 * Returns 0 on success, or an error code
2630 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2631 const void *value
, size_t size
, int flags
)
2633 struct smack_known
*skp
;
2634 struct inode_smack
*nsp
= smack_inode(inode
);
2635 struct socket_smack
*ssp
;
2636 struct socket
*sock
;
2639 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2642 skp
= smk_import_entry(value
, size
);
2644 return PTR_ERR(skp
);
2646 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2647 nsp
->smk_inode
= skp
;
2648 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2652 * The rest of the Smack xattrs are only on sockets.
2654 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2657 sock
= SOCKET_I(inode
);
2658 if (sock
== NULL
|| sock
->sk
== NULL
)
2661 ssp
= smack_sock(sock
->sk
);
2663 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2665 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2667 if (sock
->sk
->sk_family
== PF_INET
) {
2668 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2671 "Smack: \"%s\" netlbl error %d.\n",
2677 #ifdef SMACK_IPV6_PORT_LABELING
2678 if (sock
->sk
->sk_family
== PF_INET6
)
2679 smk_ipv6_port_label(sock
, NULL
);
2686 * smack_socket_post_create - finish socket setup
2688 * @family: protocol family
2693 * Sets the netlabel information on the socket
2695 * Returns 0 on success, and error code otherwise
2697 static int smack_socket_post_create(struct socket
*sock
, int family
,
2698 int type
, int protocol
, int kern
)
2700 struct socket_smack
*ssp
;
2702 if (sock
->sk
== NULL
)
2706 * Sockets created by kernel threads receive web label.
2708 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2709 ssp
= smack_sock(sock
->sk
);
2710 ssp
->smk_in
= &smack_known_web
;
2711 ssp
->smk_out
= &smack_known_web
;
2714 if (family
!= PF_INET
)
2717 * Set the outbound netlbl.
2719 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2722 #ifdef SMACK_IPV6_PORT_LABELING
2724 * smack_socket_bind - record port binding information.
2726 * @address: the port address
2727 * @addrlen: size of the address
2729 * Records the label bound to a port.
2733 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2736 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2737 smk_ipv6_port_label(sock
, address
);
2740 #endif /* SMACK_IPV6_PORT_LABELING */
2743 * smack_socket_connect - connect access check
2745 * @sap: the other end
2746 * @addrlen: size of sap
2748 * Verifies that a connection may be possible
2750 * Returns 0 on success, and error code otherwise
2752 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2756 #if IS_ENABLED(CONFIG_IPV6)
2757 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2759 #ifdef SMACK_IPV6_SECMARK_LABELING
2760 struct smack_known
*rsp
;
2761 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2764 if (sock
->sk
== NULL
)
2767 switch (sock
->sk
->sk_family
) {
2769 if (addrlen
< sizeof(struct sockaddr_in
))
2771 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2774 if (addrlen
< sizeof(struct sockaddr_in6
))
2776 #ifdef SMACK_IPV6_SECMARK_LABELING
2777 rsp
= smack_ipv6host_label(sip
);
2779 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2782 #ifdef SMACK_IPV6_PORT_LABELING
2783 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2791 * smack_flags_to_may - convert S_ to MAY_ values
2792 * @flags: the S_ value
2794 * Returns the equivalent MAY_ value
2796 static int smack_flags_to_may(int flags
)
2800 if (flags
& S_IRUGO
)
2802 if (flags
& S_IWUGO
)
2804 if (flags
& S_IXUGO
)
2811 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2816 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2818 struct smack_known
**blob
= smack_msg_msg(msg
);
2820 *blob
= smk_of_current();
2825 * smack_shm_alloc_security - Set the security blob for shm
2830 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2832 struct smack_known
**blob
= smack_ipc(&shp
->shm_perm
);
2834 *blob
= smk_of_current();
2839 * smk_curacc_shm : check if current has access on shm
2841 * @access : access requested
2843 * Returns 0 if current has the requested access, error code otherwise
2845 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2847 struct smack_known
**blob
= smack_ipc(&shp
->shm_perm
);
2848 struct smack_known
*ssp
= *blob
;
2849 struct smk_audit_info ad
;
2853 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2854 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2856 rc
= smk_curacc(ssp
, access
, &ad
);
2857 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2862 * smack_shm_associate - Smack access check for shm
2864 * @shmflg: access requested
2866 * Returns 0 if current has the requested access, error code otherwise
2868 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2872 may
= smack_flags_to_may(shmflg
);
2873 return smk_curacc_shm(shp
, may
);
2877 * smack_shm_shmctl - Smack access check for shm
2879 * @cmd: what it wants to do
2881 * Returns 0 if current has the requested access, error code otherwise
2883 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2896 may
= MAY_READWRITE
;
2901 * System level information.
2907 return smk_curacc_shm(shp
, may
);
2911 * smack_shm_shmat - Smack access for shmat
2914 * @shmflg: access requested
2916 * Returns 0 if current has the requested access, error code otherwise
2918 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2923 may
= smack_flags_to_may(shmflg
);
2924 return smk_curacc_shm(shp
, may
);
2928 * smack_sem_alloc_security - Set the security blob for sem
2933 static int smack_sem_alloc_security(struct sem_array
*sma
)
2935 struct smack_known
**blob
= smack_ipc(&sma
->sem_perm
);
2937 *blob
= smk_of_current();
2942 * smk_curacc_sem : check if current has access on sem
2944 * @access : access requested
2946 * Returns 0 if current has the requested access, error code otherwise
2948 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2950 struct smack_known
**blob
= smack_ipc(&sma
->sem_perm
);
2951 struct smack_known
*ssp
= *blob
;
2952 struct smk_audit_info ad
;
2956 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2957 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2959 rc
= smk_curacc(ssp
, access
, &ad
);
2960 rc
= smk_bu_current("sem", ssp
, access
, rc
);
2965 * smack_sem_associate - Smack access check for sem
2967 * @semflg: access requested
2969 * Returns 0 if current has the requested access, error code otherwise
2971 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2975 may
= smack_flags_to_may(semflg
);
2976 return smk_curacc_sem(sma
, may
);
2980 * smack_sem_shmctl - Smack access check for sem
2982 * @cmd: what it wants to do
2984 * Returns 0 if current has the requested access, error code otherwise
2986 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
3004 may
= MAY_READWRITE
;
3009 * System level information
3016 return smk_curacc_sem(sma
, may
);
3020 * smack_sem_semop - Smack checks of semaphore operations
3026 * Treated as read and write in all cases.
3028 * Returns 0 if access is allowed, error code otherwise
3030 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
3031 unsigned nsops
, int alter
)
3033 return smk_curacc_sem(sma
, MAY_READWRITE
);
3037 * smack_msg_queue_alloc_security - Set the security blob for msg
3042 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
3044 struct smack_known
**blob
= smack_ipc(&msq
->q_perm
);
3046 *blob
= smk_of_current();
3051 * smk_curacc_msq : helper to check if current has access on msq
3053 * @access : access requested
3055 * return 0 if current has access, error otherwise
3057 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
3059 struct smack_known
**blob
= smack_ipc(&msq
->q_perm
);
3060 struct smack_known
*msp
= *blob
;
3061 struct smk_audit_info ad
;
3065 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3066 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
3068 rc
= smk_curacc(msp
, access
, &ad
);
3069 rc
= smk_bu_current("msq", msp
, access
, rc
);
3074 * smack_msg_queue_associate - Smack access check for msg_queue
3076 * @msqflg: access requested
3078 * Returns 0 if current has the requested access, error code otherwise
3080 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
3084 may
= smack_flags_to_may(msqflg
);
3085 return smk_curacc_msq(msq
, may
);
3089 * smack_msg_queue_msgctl - Smack access check for msg_queue
3091 * @cmd: what it wants to do
3093 * Returns 0 if current has the requested access, error code otherwise
3095 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
3106 may
= MAY_READWRITE
;
3111 * System level information
3118 return smk_curacc_msq(msq
, may
);
3122 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3125 * @msqflg: access requested
3127 * Returns 0 if current has the requested access, error code otherwise
3129 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
3134 may
= smack_flags_to_may(msqflg
);
3135 return smk_curacc_msq(msq
, may
);
3139 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3146 * Returns 0 if current has read and write access, error code otherwise
3148 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
3149 struct task_struct
*target
, long type
, int mode
)
3151 return smk_curacc_msq(msq
, MAY_READWRITE
);
3155 * smack_ipc_permission - Smack access for ipc_permission()
3156 * @ipp: the object permissions
3157 * @flag: access requested
3159 * Returns 0 if current has read and write access, error code otherwise
3161 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3163 struct smack_known
**blob
= smack_ipc(ipp
);
3164 struct smack_known
*iskp
= *blob
;
3165 int may
= smack_flags_to_may(flag
);
3166 struct smk_audit_info ad
;
3170 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3171 ad
.a
.u
.ipc_id
= ipp
->id
;
3173 rc
= smk_curacc(iskp
, may
, &ad
);
3174 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3179 * smack_ipc_getsecid - Extract smack security id
3180 * @ipp: the object permissions
3181 * @secid: where result will be saved
3183 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3185 struct smack_known
**blob
= smack_ipc(ipp
);
3186 struct smack_known
*iskp
= *blob
;
3188 *secid
= iskp
->smk_secid
;
3192 * smack_d_instantiate - Make sure the blob is correct on an inode
3193 * @opt_dentry: dentry where inode will be attached
3194 * @inode: the object
3196 * Set the inode's security blob if it hasn't been done already.
3198 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3200 struct super_block
*sbp
;
3201 struct superblock_smack
*sbsp
;
3202 struct inode_smack
*isp
;
3203 struct smack_known
*skp
;
3204 struct smack_known
*ckp
= smk_of_current();
3205 struct smack_known
*final
;
3206 char trattr
[TRANS_TRUE_SIZE
];
3214 isp
= smack_inode(inode
);
3216 mutex_lock(&isp
->smk_lock
);
3218 * If the inode is already instantiated
3219 * take the quick way out
3221 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3225 sbsp
= smack_superblock(sbp
);
3227 * We're going to use the superblock default label
3228 * if there's no label on the file.
3230 final
= sbsp
->smk_default
;
3233 * If this is the root inode the superblock
3234 * may be in the process of initialization.
3235 * If that is the case use the root value out
3236 * of the superblock.
3238 if (opt_dentry
->d_parent
== opt_dentry
) {
3239 switch (sbp
->s_magic
) {
3240 case CGROUP_SUPER_MAGIC
:
3242 * The cgroup filesystem is never mounted,
3243 * so there's no opportunity to set the mount
3246 sbsp
->smk_root
= &smack_known_star
;
3247 sbsp
->smk_default
= &smack_known_star
;
3248 isp
->smk_inode
= sbsp
->smk_root
;
3252 * What about shmem/tmpfs anonymous files with dentry
3253 * obtained from d_alloc_pseudo()?
3255 isp
->smk_inode
= smk_of_current();
3258 isp
->smk_inode
= smk_of_current();
3262 * Socket access is controlled by the socket
3263 * structures associated with the task involved.
3265 isp
->smk_inode
= &smack_known_star
;
3268 isp
->smk_inode
= sbsp
->smk_root
;
3271 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3276 * This is pretty hackish.
3277 * Casey says that we shouldn't have to do
3278 * file system specific code, but it does help
3279 * with keeping it simple.
3281 switch (sbp
->s_magic
) {
3283 case CGROUP_SUPER_MAGIC
:
3285 * Casey says that it's a little embarrassing
3286 * that the smack file system doesn't do
3287 * extended attributes.
3289 * Cgroupfs is special
3291 final
= &smack_known_star
;
3293 case DEVPTS_SUPER_MAGIC
:
3295 * devpts seems content with the label of the task.
3296 * Programs that change smack have to treat the
3301 case PROC_SUPER_MAGIC
:
3303 * Casey says procfs appears not to care.
3304 * The superblock default suffices.
3309 * Device labels should come from the filesystem,
3310 * but watch out, because they're volitile,
3311 * getting recreated on every reboot.
3313 final
= &smack_known_star
;
3317 * If a smack value has been set we want to use it,
3318 * but since tmpfs isn't giving us the opportunity
3319 * to set mount options simulate setting the
3320 * superblock default.
3324 * This isn't an understood special case.
3325 * Get the value from the xattr.
3329 * UNIX domain sockets use lower level socket data.
3331 if (S_ISSOCK(inode
->i_mode
)) {
3332 final
= &smack_known_star
;
3336 * No xattr support means, alas, no SMACK label.
3337 * Use the aforeapplied default.
3338 * It would be curious if the label of the task
3339 * does not match that assigned.
3341 if (!(inode
->i_opflags
& IOP_XATTR
))
3344 * Get the dentry for xattr.
3346 dp
= dget(opt_dentry
);
3347 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3348 if (!IS_ERR_OR_NULL(skp
))
3352 * Transmuting directory
3354 if (S_ISDIR(inode
->i_mode
)) {
3356 * If this is a new directory and the label was
3357 * transmuted when the inode was initialized
3358 * set the transmute attribute on the directory
3359 * and mark the inode.
3361 * If there is a transmute attribute on the
3362 * directory mark the inode.
3364 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3365 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3366 rc
= __vfs_setxattr(dp
, inode
,
3367 XATTR_NAME_SMACKTRANSMUTE
,
3368 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3371 rc
= __vfs_getxattr(dp
, inode
,
3372 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3374 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3375 TRANS_TRUE_SIZE
) != 0)
3379 transflag
= SMK_INODE_TRANSMUTE
;
3382 * Don't let the exec or mmap label be "*" or "@".
3384 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3385 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3386 skp
== &smack_known_web
)
3388 isp
->smk_task
= skp
;
3390 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3391 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3392 skp
== &smack_known_web
)
3394 isp
->smk_mmap
= skp
;
3401 isp
->smk_inode
= ckp
;
3403 isp
->smk_inode
= final
;
3405 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3408 mutex_unlock(&isp
->smk_lock
);
3413 * smack_getprocattr - Smack process attribute access
3414 * @p: the object task
3415 * @name: the name of the attribute in /proc/.../attr
3416 * @value: where to put the result
3418 * Places a copy of the task Smack into value
3420 * Returns the length of the smack label or an error code
3422 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3424 struct smack_known
*skp
= smk_of_task_struct(p
);
3427 if (strcmp(name
, "current") == 0) {
3428 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3439 * smack_setprocattr - Smack process attribute setting
3440 * @name: the name of the attribute in /proc/.../attr
3441 * @value: the value to set
3442 * @size: the size of the value
3444 * Sets the Smack value of the task. Only setting self
3445 * is permitted and only with privilege
3447 * Returns the length of the smack label or an error code
3449 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3451 struct task_smack
*tsp
= smack_cred(current_cred());
3453 struct smack_known
*skp
;
3454 struct smack_known_list_elem
*sklep
;
3457 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3460 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3463 if (strcmp(name
, "current") != 0)
3466 skp
= smk_import_entry(value
, size
);
3468 return PTR_ERR(skp
);
3471 * No process is ever allowed the web ("@") label
3472 * and the star ("*") label.
3474 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3477 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3479 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3480 if (sklep
->smk_label
== skp
) {
3488 new = prepare_creds();
3492 tsp
= smack_cred(new);
3493 tsp
->smk_task
= skp
;
3495 * process can change its label only once
3497 smk_destroy_label_list(&tsp
->smk_relabel
);
3504 * smack_unix_stream_connect - Smack access on UDS
3506 * @other: the other sock
3509 * Return 0 if a subject with the smack of sock could access
3510 * an object with the smack of other, otherwise an error code
3512 static int smack_unix_stream_connect(struct sock
*sock
,
3513 struct sock
*other
, struct sock
*newsk
)
3515 struct smack_known
*skp
;
3516 struct smack_known
*okp
;
3517 struct socket_smack
*ssp
= smack_sock(sock
);
3518 struct socket_smack
*osp
= smack_sock(other
);
3519 struct socket_smack
*nsp
= smack_sock(newsk
);
3520 struct smk_audit_info ad
;
3523 struct lsm_network_audit net
;
3526 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3530 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3531 smk_ad_setfield_u_net_sk(&ad
, other
);
3533 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3534 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3538 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3539 rc
= smk_bu_note("UDS connect", okp
, skp
,
3545 * Cross reference the peer labels for SO_PEERSEC.
3548 nsp
->smk_packet
= ssp
->smk_out
;
3549 ssp
->smk_packet
= osp
->smk_out
;
3556 * smack_unix_may_send - Smack access on UDS
3558 * @other: the other socket
3560 * Return 0 if a subject with the smack of sock could access
3561 * an object with the smack of other, otherwise an error code
3563 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3565 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
3566 struct socket_smack
*osp
= smack_sock(other
->sk
);
3567 struct smk_audit_info ad
;
3571 struct lsm_network_audit net
;
3573 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3574 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3577 if (smack_privileged(CAP_MAC_OVERRIDE
))
3580 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3581 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3586 * smack_socket_sendmsg - Smack check based on destination host
3589 * @size: the size of the message
3591 * Return 0 if the current subject can write to the destination host.
3592 * For IPv4 this is only a question if the destination is a single label host.
3593 * For IPv6 this is a check against the label of the port.
3595 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3598 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3599 #if IS_ENABLED(CONFIG_IPV6)
3600 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3602 #ifdef SMACK_IPV6_SECMARK_LABELING
3603 struct socket_smack
*ssp
= smack_sock(sock
->sk
);
3604 struct smack_known
*rsp
;
3609 * Perfectly reasonable for this to be NULL
3614 switch (sock
->sk
->sk_family
) {
3616 rc
= smack_netlabel_send(sock
->sk
, sip
);
3619 #ifdef SMACK_IPV6_SECMARK_LABELING
3620 rsp
= smack_ipv6host_label(sap
);
3622 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3625 #ifdef SMACK_IPV6_PORT_LABELING
3626 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3634 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3635 * @sap: netlabel secattr
3636 * @ssp: socket security information
3638 * Returns a pointer to a Smack label entry found on the label list.
3640 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3641 struct socket_smack
*ssp
)
3643 struct smack_known
*skp
;
3648 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3650 * Looks like a CIPSO packet.
3651 * If there are flags but no level netlabel isn't
3652 * behaving the way we expect it to.
3654 * Look it up in the label table
3655 * Without guidance regarding the smack value
3656 * for the packet fall back on the network
3660 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3661 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3664 * Compare the catsets. Use the netlbl APIs.
3666 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3667 if ((skp
->smk_netlabel
.flags
&
3668 NETLBL_SECATTR_MLS_CAT
) == 0)
3672 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3673 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3675 kcat
= netlbl_catmap_walk(
3676 skp
->smk_netlabel
.attr
.mls
.cat
,
3678 if (acat
< 0 || kcat
< 0)
3691 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3692 return &smack_known_web
;
3693 return &smack_known_star
;
3695 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3697 * Looks like a fallback, which gives us a secid.
3699 return smack_from_secid(sap
->attr
.secid
);
3701 * Without guidance regarding the smack value
3702 * for the packet fall back on the network
3705 return smack_net_ambient
;
3708 #if IS_ENABLED(CONFIG_IPV6)
3709 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3713 int proto
= -EINVAL
;
3714 struct ipv6hdr _ipv6h
;
3715 struct ipv6hdr
*ip6
;
3717 struct tcphdr _tcph
, *th
;
3718 struct udphdr _udph
, *uh
;
3719 struct dccp_hdr _dccph
, *dh
;
3723 offset
= skb_network_offset(skb
);
3724 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3727 sip
->sin6_addr
= ip6
->saddr
;
3729 nexthdr
= ip6
->nexthdr
;
3730 offset
+= sizeof(_ipv6h
);
3731 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3738 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3740 sip
->sin6_port
= th
->source
;
3743 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3745 sip
->sin6_port
= uh
->source
;
3748 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3750 sip
->sin6_port
= dh
->dccph_sport
;
3755 #endif /* CONFIG_IPV6 */
3758 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3762 * Returns 0 if the packet should be delivered, an error code otherwise
3764 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3766 struct netlbl_lsm_secattr secattr
;
3767 struct socket_smack
*ssp
= smack_sock(sk
);
3768 struct smack_known
*skp
= NULL
;
3770 struct smk_audit_info ad
;
3771 u16 family
= sk
->sk_family
;
3773 struct lsm_network_audit net
;
3775 #if IS_ENABLED(CONFIG_IPV6)
3776 struct sockaddr_in6 sadd
;
3779 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3781 #endif /* CONFIG_IPV6 */
3785 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3787 * If there is a secmark use it rather than the CIPSO label.
3788 * If there is no secmark fall back to CIPSO.
3789 * The secmark is assumed to reflect policy better.
3791 if (skb
&& skb
->secmark
!= 0) {
3792 skp
= smack_from_secid(skb
->secmark
);
3795 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3797 * Translate what netlabel gave us.
3799 netlbl_secattr_init(&secattr
);
3801 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3803 skp
= smack_from_secattr(&secattr
, ssp
);
3805 skp
= smack_net_ambient
;
3807 netlbl_secattr_destroy(&secattr
);
3809 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3813 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3814 ad
.a
.u
.net
->family
= family
;
3815 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3816 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3819 * Receiving a packet requires that the other end
3820 * be able to write here. Read access is not required.
3821 * This is the simplist possible security model
3824 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3825 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3828 netlbl_skbuff_err(skb
, family
, rc
, 0);
3830 #if IS_ENABLED(CONFIG_IPV6)
3832 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3833 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_TCP
)
3835 #ifdef SMACK_IPV6_SECMARK_LABELING
3836 if (skb
&& skb
->secmark
!= 0)
3837 skp
= smack_from_secid(skb
->secmark
);
3839 skp
= smack_ipv6host_label(&sadd
);
3841 skp
= smack_net_ambient
;
3843 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3844 ad
.a
.u
.net
->family
= family
;
3845 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3846 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3847 #endif /* CONFIG_AUDIT */
3848 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3849 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3851 #endif /* SMACK_IPV6_SECMARK_LABELING */
3852 #ifdef SMACK_IPV6_PORT_LABELING
3853 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3854 #endif /* SMACK_IPV6_PORT_LABELING */
3856 #endif /* CONFIG_IPV6 */
3863 * smack_socket_getpeersec_stream - pull in packet label
3865 * @optval: user's destination
3866 * @optlen: size thereof
3869 * returns zero on success, an error code otherwise
3871 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3872 char __user
*optval
,
3873 int __user
*optlen
, unsigned len
)
3875 struct socket_smack
*ssp
;
3880 ssp
= smack_sock(sock
->sk
);
3881 if (ssp
->smk_packet
!= NULL
) {
3882 rcp
= ssp
->smk_packet
->smk_known
;
3883 slen
= strlen(rcp
) + 1;
3888 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3891 if (put_user(slen
, optlen
) != 0)
3899 * smack_socket_getpeersec_dgram - pull in packet label
3900 * @sock: the peer socket
3902 * @secid: pointer to where to put the secid of the packet
3904 * Sets the netlabel socket state on sk from parent
3906 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3907 struct sk_buff
*skb
, u32
*secid
)
3910 struct netlbl_lsm_secattr secattr
;
3911 struct socket_smack
*ssp
= NULL
;
3912 struct smack_known
*skp
;
3913 int family
= PF_UNSPEC
;
3914 u32 s
= 0; /* 0 is the invalid secid */
3918 if (skb
->protocol
== htons(ETH_P_IP
))
3920 #if IS_ENABLED(CONFIG_IPV6)
3921 else if (skb
->protocol
== htons(ETH_P_IPV6
))
3923 #endif /* CONFIG_IPV6 */
3925 if (family
== PF_UNSPEC
&& sock
!= NULL
)
3926 family
= sock
->sk
->sk_family
;
3930 ssp
= smack_sock(sock
->sk
);
3931 s
= ssp
->smk_out
->smk_secid
;
3934 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3940 * Translate what netlabel gave us.
3942 if (sock
!= NULL
&& sock
->sk
!= NULL
)
3943 ssp
= smack_sock(sock
->sk
);
3944 netlbl_secattr_init(&secattr
);
3945 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3947 skp
= smack_from_secattr(&secattr
, ssp
);
3950 netlbl_secattr_destroy(&secattr
);
3953 #ifdef SMACK_IPV6_SECMARK_LABELING
3965 * smack_sock_graft - Initialize a newly created socket with an existing sock
3967 * @parent: parent socket
3969 * Set the smk_{in,out} state of an existing sock based on the process that
3970 * is creating the new socket.
3972 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3974 struct socket_smack
*ssp
;
3975 struct smack_known
*skp
= smk_of_current();
3978 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3981 ssp
= smack_sock(sk
);
3984 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3988 * smack_inet_conn_request - Smack access check on connect
3989 * @sk: socket involved
3993 * Returns 0 if a task with the packet label could write to
3994 * the socket, otherwise an error code
3996 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
3997 struct request_sock
*req
)
3999 u16 family
= sk
->sk_family
;
4000 struct smack_known
*skp
;
4001 struct socket_smack
*ssp
= smack_sock(sk
);
4002 struct netlbl_lsm_secattr secattr
;
4003 struct sockaddr_in addr
;
4005 struct smack_known
*hskp
;
4007 struct smk_audit_info ad
;
4009 struct lsm_network_audit net
;
4012 #if IS_ENABLED(CONFIG_IPV6)
4013 if (family
== PF_INET6
) {
4015 * Handle mapped IPv4 packets arriving
4016 * via IPv6 sockets. Don't set up netlabel
4017 * processing on IPv6.
4019 if (skb
->protocol
== htons(ETH_P_IP
))
4024 #endif /* CONFIG_IPV6 */
4026 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4028 * If there is a secmark use it rather than the CIPSO label.
4029 * If there is no secmark fall back to CIPSO.
4030 * The secmark is assumed to reflect policy better.
4032 if (skb
&& skb
->secmark
!= 0) {
4033 skp
= smack_from_secid(skb
->secmark
);
4036 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4038 netlbl_secattr_init(&secattr
);
4039 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4041 skp
= smack_from_secattr(&secattr
, ssp
);
4043 skp
= &smack_known_huh
;
4044 netlbl_secattr_destroy(&secattr
);
4046 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4051 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4052 ad
.a
.u
.net
->family
= family
;
4053 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4054 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4057 * Receiving a packet requires that the other end be able to write
4058 * here. Read access is not required.
4060 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4061 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4066 * Save the peer's label in the request_sock so we can later setup
4067 * smk_packet in the child socket so that SO_PEERCRED can report it.
4069 req
->peer_secid
= skp
->smk_secid
;
4072 * We need to decide if we want to label the incoming connection here
4073 * if we do we only need to label the request_sock and the stack will
4074 * propagate the wire-label to the sock when it is created.
4077 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4079 hskp
= smack_ipv4host_label(&addr
);
4083 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4085 netlbl_req_delattr(req
);
4091 * smack_inet_csk_clone - Copy the connection information to the new socket
4092 * @sk: the new socket
4093 * @req: the connection's request_sock
4095 * Transfer the connection's peer label to the newly created socket.
4097 static void smack_inet_csk_clone(struct sock
*sk
,
4098 const struct request_sock
*req
)
4100 struct socket_smack
*ssp
= smack_sock(sk
);
4101 struct smack_known
*skp
;
4103 if (req
->peer_secid
!= 0) {
4104 skp
= smack_from_secid(req
->peer_secid
);
4105 ssp
->smk_packet
= skp
;
4107 ssp
->smk_packet
= NULL
;
4111 * Key management security hooks
4113 * Casey has not tested key support very heavily.
4114 * The permission check is most likely too restrictive.
4115 * If you care about keys please have a look.
4120 * smack_key_alloc - Set the key security blob
4122 * @cred: the credentials to use
4125 * No allocation required
4129 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4130 unsigned long flags
)
4132 struct smack_known
**blob
= smack_key(key
);
4133 struct smack_known
*skp
= smk_of_task(smack_cred(cred
));
4140 * smack_key_permission - Smack access on a key
4141 * @key_ref: gets to the object
4142 * @cred: the credentials to use
4143 * @perm: requested key permissions
4145 * Return 0 if the task has read and write to the object,
4146 * an error code otherwise
4148 static int smack_key_permission(key_ref_t key_ref
,
4149 const struct cred
*cred
, unsigned perm
)
4151 struct smack_known
**blob
;
4152 struct smack_known
*skp
;
4154 struct smk_audit_info ad
;
4155 struct smack_known
*tkp
= smk_of_task(smack_cred(cred
));
4159 keyp
= key_ref_to_ptr(key_ref
);
4163 * If the key hasn't been initialized give it access so that
4166 blob
= smack_key(keyp
);
4171 * This should not occur
4176 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4177 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4178 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4180 if (perm
& KEY_NEED_READ
)
4182 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4183 request
= MAY_WRITE
;
4184 rc
= smk_access(tkp
, skp
, request
, &ad
);
4185 rc
= smk_bu_note("key access", tkp
, skp
, request
, rc
);
4190 * smack_key_getsecurity - Smack label tagging the key
4191 * @key points to the key to be queried
4192 * @_buffer points to a pointer that should be set to point to the
4193 * resulting string (if no label or an error occurs).
4194 * Return the length of the string (including terminating NUL) or -ve if
4196 * May also return 0 (and a NULL buffer pointer) if there is no label.
4198 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4200 struct smack_known
**blob
= smack_key(key
);
4201 struct smack_known
*skp
= *blob
;
4210 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4213 length
= strlen(copy
) + 1;
4219 #endif /* CONFIG_KEYS */
4224 * Audit requires a unique representation of each Smack specific
4225 * rule. This unique representation is used to distinguish the
4226 * object to be audited from remaining kernel objects and also
4227 * works as a glue between the audit hooks.
4229 * Since repository entries are added but never deleted, we'll use
4230 * the smack_known label address related to the given audit rule as
4231 * the needed unique representation. This also better fits the smack
4232 * model where nearly everything is a label.
4237 * smack_audit_rule_init - Initialize a smack audit rule
4238 * @field: audit rule fields given from user-space (audit.h)
4239 * @op: required testing operator (=, !=, >, <, ...)
4240 * @rulestr: smack label to be audited
4241 * @vrule: pointer to save our own audit rule representation
4243 * Prepare to audit cases where (@field @op @rulestr) is true.
4244 * The label to be audited is created if necessay.
4246 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4248 struct smack_known
*skp
;
4249 char **rule
= (char **)vrule
;
4252 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4255 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4258 skp
= smk_import_entry(rulestr
, 0);
4260 return PTR_ERR(skp
);
4262 *rule
= skp
->smk_known
;
4268 * smack_audit_rule_known - Distinguish Smack audit rules
4269 * @krule: rule of interest, in Audit kernel representation format
4271 * This is used to filter Smack rules from remaining Audit ones.
4272 * If it's proved that this rule belongs to us, the
4273 * audit_rule_match hook will be called to do the final judgement.
4275 static int smack_audit_rule_known(struct audit_krule
*krule
)
4277 struct audit_field
*f
;
4280 for (i
= 0; i
< krule
->field_count
; i
++) {
4281 f
= &krule
->fields
[i
];
4283 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4291 * smack_audit_rule_match - Audit given object ?
4292 * @secid: security id for identifying the object to test
4293 * @field: audit rule flags given from user-space
4294 * @op: required testing operator
4295 * @vrule: smack internal rule presentation
4296 * @actx: audit context associated with the check
4298 * The core Audit hook. It's used to take the decision of
4299 * whether to audit or not to audit a given object.
4301 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4302 struct audit_context
*actx
)
4304 struct smack_known
*skp
;
4307 if (unlikely(!rule
)) {
4308 WARN_ONCE(1, "Smack: missing rule\n");
4312 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4315 skp
= smack_from_secid(secid
);
4318 * No need to do string comparisons. If a match occurs,
4319 * both pointers will point to the same smack_known
4322 if (op
== Audit_equal
)
4323 return (rule
== skp
->smk_known
);
4324 if (op
== Audit_not_equal
)
4325 return (rule
!= skp
->smk_known
);
4331 * There is no need for a smack_audit_rule_free hook.
4332 * No memory was allocated.
4335 #endif /* CONFIG_AUDIT */
4338 * smack_ismaclabel - check if xattr @name references a smack MAC label
4339 * @name: Full xattr name to check.
4341 static int smack_ismaclabel(const char *name
)
4343 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4348 * smack_secid_to_secctx - return the smack label for a secid
4349 * @secid: incoming integer
4350 * @secdata: destination
4351 * @seclen: how long it is
4353 * Exists for networking code.
4355 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4357 struct smack_known
*skp
= smack_from_secid(secid
);
4360 *secdata
= skp
->smk_known
;
4361 *seclen
= strlen(skp
->smk_known
);
4366 * smack_secctx_to_secid - return the secid for a smack label
4367 * @secdata: smack label
4368 * @seclen: how long result is
4369 * @secid: outgoing integer
4371 * Exists for audit and networking code.
4373 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4375 struct smack_known
*skp
= smk_find_entry(secdata
);
4378 *secid
= skp
->smk_secid
;
4385 * There used to be a smack_release_secctx hook
4386 * that did nothing back when hooks were in a vector.
4387 * Now that there's a list such a hook adds cost.
4390 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4392 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4395 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4397 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4400 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4403 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
4411 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4414 struct task_smack
*tsp
;
4415 struct smack_known
*skp
;
4416 struct inode_smack
*isp
;
4417 struct cred
*new_creds
= *new;
4419 if (new_creds
== NULL
) {
4420 new_creds
= prepare_creds();
4421 if (new_creds
== NULL
)
4425 tsp
= smack_cred(new_creds
);
4428 * Get label from overlay inode and set it in create_sid
4430 isp
= smack_inode(d_inode(dentry
->d_parent
));
4431 skp
= isp
->smk_inode
;
4432 tsp
->smk_task
= skp
;
4437 static int smack_inode_copy_up_xattr(const char *name
)
4440 * Return 1 if this is the smack access Smack attribute.
4442 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4448 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4450 const struct cred
*old
,
4453 struct task_smack
*otsp
= smack_cred(old
);
4454 struct task_smack
*ntsp
= smack_cred(new);
4455 struct inode_smack
*isp
;
4459 * Use the process credential unless all of
4460 * the transmuting criteria are met
4462 ntsp
->smk_task
= otsp
->smk_task
;
4465 * the attribute of the containing directory
4467 isp
= smack_inode(d_inode(dentry
->d_parent
));
4469 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4471 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4472 isp
->smk_inode
->smk_known
,
4473 &otsp
->smk_task
->smk_rules
);
4477 * If the directory is transmuting and the rule
4478 * providing access is transmuting use the containing
4479 * directory label instead of the process label.
4481 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4482 ntsp
->smk_task
= isp
->smk_inode
;
4487 struct lsm_blob_sizes smack_blob_sizes
= {
4488 .lbs_cred
= sizeof(struct task_smack
),
4489 .lbs_file
= sizeof(struct smack_known
*),
4490 .lbs_inode
= sizeof(struct inode_smack
),
4491 .lbs_ipc
= sizeof(struct smack_known
*),
4493 .lbs_key
= sizeof(struct smack_known
*),
4494 #endif /* CONFIG_KEYS */
4495 .lbs_msg_msg
= sizeof(struct smack_known
*),
4496 .lbs_sock
= sizeof(struct socket_smack
),
4497 .lbs_superblock
= sizeof(struct superblock_smack
),
4500 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4501 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4502 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4503 LSM_HOOK_INIT(syslog
, smack_syslog
),
4505 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4506 LSM_HOOK_INIT(sb_copy_data
, smack_sb_copy_data
),
4507 LSM_HOOK_INIT(sb_kern_mount
, smack_sb_kern_mount
),
4508 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4509 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4510 LSM_HOOK_INIT(sb_parse_opts_str
, smack_parse_opts_str
),
4512 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4514 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4515 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4516 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4517 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4518 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4519 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4520 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4521 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4522 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4523 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4524 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4525 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4526 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4527 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4528 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4529 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4530 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4532 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4533 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4534 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4535 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4536 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4537 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4538 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4539 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4540 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4542 LSM_HOOK_INIT(file_open
, smack_file_open
),
4544 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4545 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4546 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4547 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4548 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4549 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4550 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4551 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4552 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4553 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4554 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4555 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4556 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4557 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4558 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4559 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4560 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4561 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4563 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4564 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4566 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4568 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_msg_queue_alloc_security
),
4569 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4570 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4571 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4572 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4574 LSM_HOOK_INIT(shm_alloc_security
, smack_shm_alloc_security
),
4575 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4576 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4577 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4579 LSM_HOOK_INIT(sem_alloc_security
, smack_sem_alloc_security
),
4580 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4581 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4582 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4584 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4586 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4587 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4589 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4590 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4592 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4593 #ifdef SMACK_IPV6_PORT_LABELING
4594 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4596 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4597 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4598 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4599 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4600 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4601 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4602 #ifdef SMACK_IPV6_PORT_LABELING
4603 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4605 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4606 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4607 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4609 /* key management security hooks */
4611 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4612 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4613 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4614 #endif /* CONFIG_KEYS */
4618 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4619 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4620 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4621 #endif /* CONFIG_AUDIT */
4623 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4624 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4625 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4626 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4627 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4628 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4629 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4630 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4631 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4635 static __init
void init_smack_known_list(void)
4638 * Initialize rule list locks
4640 mutex_init(&smack_known_huh
.smk_rules_lock
);
4641 mutex_init(&smack_known_hat
.smk_rules_lock
);
4642 mutex_init(&smack_known_floor
.smk_rules_lock
);
4643 mutex_init(&smack_known_star
.smk_rules_lock
);
4644 mutex_init(&smack_known_web
.smk_rules_lock
);
4646 * Initialize rule lists
4648 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4649 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4650 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4651 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4652 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4654 * Create the known labels list
4656 smk_insert_entry(&smack_known_huh
);
4657 smk_insert_entry(&smack_known_hat
);
4658 smk_insert_entry(&smack_known_star
);
4659 smk_insert_entry(&smack_known_floor
);
4660 smk_insert_entry(&smack_known_web
);
4664 * smack_init - initialize the smack system
4668 static __init
int smack_init(void)
4671 struct cred
*cred
= (struct cred
*) current
->cred
;
4672 struct task_smack
*tsp
;
4674 if (!security_module_enable("smack",
4675 IS_ENABLED(CONFIG_SECURITY_SMACK_STACKED
)))
4679 security_add_blobs(&smack_blob_sizes
);
4684 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4685 if (!smack_inode_cache
)
4688 lsm_early_cred(cred
);
4691 * Set the security state for the initial task.
4693 tsp
= smack_cred(cred
);
4694 init_task_smack(tsp
, &smack_known_floor
, &smack_known_floor
);
4699 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4702 pr_info("Smack: Initializing.\n");
4703 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4704 pr_info("Smack: Netfilter enabled.\n");
4706 #ifdef SMACK_IPV6_PORT_LABELING
4707 pr_info("Smack: IPv6 port labeling enabled.\n");
4709 #ifdef SMACK_IPV6_SECMARK_LABELING
4710 pr_info("Smack: IPv6 Netfilter enabled.\n");
4713 /* initialize the smack_known_list */
4714 init_smack_known_list();
4720 * Smack requires early initialization in order to label
4721 * all processes and objects when they are created.
4723 security_initcall(smack_init
);