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 LIST_HEAD(smk_ipv6_port_list
);
57 static struct kmem_cache
*smack_inode_cache
;
60 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
61 static char *smk_bu_mess
[] = {
62 "Bringup Error", /* Unused */
63 "Bringup", /* SMACK_BRINGUP_ALLOW */
64 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
65 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
68 static const match_table_t tokens
= {
69 {Opt_fsdefault
, SMK_FSDEFAULT
"%s"},
70 {Opt_fsfloor
, SMK_FSFLOOR
"%s"},
71 {Opt_fshat
, SMK_FSHAT
"%s"},
72 {Opt_fsroot
, SMK_FSROOT
"%s"},
73 {Opt_fstransmute
, SMK_FSTRANS
"%s"},
77 static void smk_bu_mode(int mode
, char *s
)
87 if (mode
& MAY_APPEND
)
89 if (mode
& MAY_TRANSMUTE
)
99 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
100 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
101 struct smack_known
*oskp
, int mode
, int rc
)
103 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
107 if (rc
> SMACK_UNCONFINED_OBJECT
)
110 smk_bu_mode(mode
, acc
);
111 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
112 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
116 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
119 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
120 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
123 struct task_smack
*tsp
= current_security();
124 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
128 if (rc
> SMACK_UNCONFINED_OBJECT
)
131 smk_bu_mode(mode
, acc
);
132 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
133 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
134 acc
, current
->comm
, note
);
138 #define smk_bu_current(note, oskp, mode, RC) (RC)
141 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
142 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
144 struct task_smack
*tsp
= current_security();
145 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
146 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
150 if (rc
> SMACK_UNCONFINED_OBJECT
)
153 smk_bu_mode(mode
, acc
);
154 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
155 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
156 current
->comm
, otp
->comm
);
160 #define smk_bu_task(otp, mode, RC) (RC)
163 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
164 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
166 struct task_smack
*tsp
= current_security();
167 struct inode_smack
*isp
= inode
->i_security
;
168 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
170 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
171 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
172 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
176 if (rc
> SMACK_UNCONFINED_OBJECT
)
178 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
179 (mode
& (MAY_WRITE
| MAY_APPEND
)))
180 isp
->smk_flags
|= SMK_INODE_IMPURE
;
182 smk_bu_mode(mode
, acc
);
184 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
185 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
186 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
190 #define smk_bu_inode(inode, mode, RC) (RC)
193 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
194 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
196 struct task_smack
*tsp
= current_security();
197 struct smack_known
*sskp
= tsp
->smk_task
;
198 struct inode
*inode
= file_inode(file
);
199 struct inode_smack
*isp
= inode
->i_security
;
200 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
202 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
203 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
204 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
208 if (rc
> SMACK_UNCONFINED_OBJECT
)
211 smk_bu_mode(mode
, acc
);
212 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
213 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
214 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
219 #define smk_bu_file(file, mode, RC) (RC)
222 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
223 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
226 struct task_smack
*tsp
= cred
->security
;
227 struct smack_known
*sskp
= tsp
->smk_task
;
228 struct inode
*inode
= file
->f_inode
;
229 struct inode_smack
*isp
= inode
->i_security
;
230 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
232 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
233 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
234 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
238 if (rc
> SMACK_UNCONFINED_OBJECT
)
241 smk_bu_mode(mode
, acc
);
242 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
243 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
244 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
249 #define smk_bu_credfile(cred, file, mode, RC) (RC)
253 * smk_fetch - Fetch the smack label from a file.
254 * @name: type of the label (attribute)
255 * @ip: a pointer to the inode
256 * @dp: a pointer to the dentry
258 * Returns a pointer to the master list entry for the Smack label,
259 * NULL if there was no label to fetch, or an error code.
261 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
266 struct smack_known
*skp
= NULL
;
268 if (ip
->i_op
->getxattr
== NULL
)
269 return ERR_PTR(-EOPNOTSUPP
);
271 buffer
= kzalloc(SMK_LONGLABEL
, GFP_KERNEL
);
273 return ERR_PTR(-ENOMEM
);
275 rc
= ip
->i_op
->getxattr(dp
, name
, buffer
, SMK_LONGLABEL
);
281 skp
= smk_import_entry(buffer
, rc
);
289 * new_inode_smack - allocate an inode security blob
290 * @skp: a pointer to the Smack label entry to use in the blob
292 * Returns the new blob or NULL if there's no memory available
294 static struct inode_smack
*new_inode_smack(struct smack_known
*skp
)
296 struct inode_smack
*isp
;
298 isp
= kmem_cache_zalloc(smack_inode_cache
, GFP_NOFS
);
302 isp
->smk_inode
= skp
;
304 mutex_init(&isp
->smk_lock
);
310 * new_task_smack - allocate a task security blob
311 * @task: a pointer to the Smack label for the running task
312 * @forked: a pointer to the Smack label for the forked task
313 * @gfp: type of the memory for the allocation
315 * Returns the new blob or NULL if there's no memory available
317 static struct task_smack
*new_task_smack(struct smack_known
*task
,
318 struct smack_known
*forked
, gfp_t gfp
)
320 struct task_smack
*tsp
;
322 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
326 tsp
->smk_task
= task
;
327 tsp
->smk_forked
= forked
;
328 INIT_LIST_HEAD(&tsp
->smk_rules
);
329 mutex_init(&tsp
->smk_rules_lock
);
335 * smk_copy_rules - copy a rule set
336 * @nhead: new rules header pointer
337 * @ohead: old rules header pointer
338 * @gfp: type of the memory for the allocation
340 * Returns 0 on success, -ENOMEM on error
342 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
345 struct smack_rule
*nrp
;
346 struct smack_rule
*orp
;
349 INIT_LIST_HEAD(nhead
);
351 list_for_each_entry_rcu(orp
, ohead
, list
) {
352 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
358 list_add_rcu(&nrp
->list
, nhead
);
364 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
365 * @mode - input mode in form of PTRACE_MODE_*
367 * Returns a converted MAY_* mode usable by smack rules
369 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
372 case PTRACE_MODE_READ
:
374 case PTRACE_MODE_ATTACH
:
375 return MAY_READWRITE
;
382 * smk_ptrace_rule_check - helper for ptrace access
383 * @tracer: tracer process
384 * @tracee_known: label entry of the process that's about to be traced
385 * @mode: ptrace attachment mode (PTRACE_MODE_*)
386 * @func: name of the function that called us, used for audit
388 * Returns 0 on access granted, -error on error
390 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
391 struct smack_known
*tracee_known
,
392 unsigned int mode
, const char *func
)
395 struct smk_audit_info ad
, *saip
= NULL
;
396 struct task_smack
*tsp
;
397 struct smack_known
*tracer_known
;
399 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
400 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
401 smk_ad_setfield_u_tsk(&ad
, tracer
);
406 tsp
= __task_cred(tracer
)->security
;
407 tracer_known
= smk_of_task(tsp
);
409 if ((mode
& PTRACE_MODE_ATTACH
) &&
410 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
411 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
412 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
414 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
416 else if (capable(CAP_SYS_PTRACE
))
422 smack_log(tracer_known
->smk_known
,
423 tracee_known
->smk_known
,
430 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
431 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
439 * We he, that is fun!
443 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
444 * @ctp: child task pointer
445 * @mode: ptrace attachment mode (PTRACE_MODE_*)
447 * Returns 0 if access is OK, an error code otherwise
449 * Do the capability checks.
451 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
453 struct smack_known
*skp
;
455 skp
= smk_of_task_struct(ctp
);
457 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
461 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
462 * @ptp: parent task pointer
464 * Returns 0 if access is OK, an error code otherwise
466 * Do the capability checks, and require PTRACE_MODE_ATTACH.
468 static int smack_ptrace_traceme(struct task_struct
*ptp
)
471 struct smack_known
*skp
;
473 skp
= smk_of_task(current_security());
475 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
480 * smack_syslog - Smack approval on syslog
481 * @type: message type
483 * Returns 0 on success, error code otherwise.
485 static int smack_syslog(int typefrom_file
)
488 struct smack_known
*skp
= smk_of_current();
490 if (smack_privileged(CAP_MAC_OVERRIDE
))
493 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
505 * smack_sb_alloc_security - allocate a superblock blob
506 * @sb: the superblock getting the blob
508 * Returns 0 on success or -ENOMEM on error.
510 static int smack_sb_alloc_security(struct super_block
*sb
)
512 struct superblock_smack
*sbsp
;
514 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
519 sbsp
->smk_root
= &smack_known_floor
;
520 sbsp
->smk_default
= &smack_known_floor
;
521 sbsp
->smk_floor
= &smack_known_floor
;
522 sbsp
->smk_hat
= &smack_known_hat
;
524 * smk_initialized will be zero from kzalloc.
526 sb
->s_security
= sbsp
;
532 * smack_sb_free_security - free a superblock blob
533 * @sb: the superblock getting the blob
536 static void smack_sb_free_security(struct super_block
*sb
)
538 kfree(sb
->s_security
);
539 sb
->s_security
= NULL
;
543 * smack_sb_copy_data - copy mount options data for processing
544 * @orig: where to start
545 * @smackopts: mount options string
547 * Returns 0 on success or -ENOMEM on error.
549 * Copy the Smack specific mount options out of the mount
552 static int smack_sb_copy_data(char *orig
, char *smackopts
)
554 char *cp
, *commap
, *otheropts
, *dp
;
556 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
557 if (otheropts
== NULL
)
560 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
561 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
563 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
565 else if (strstr(cp
, SMK_FSHAT
) == cp
)
567 else if (strstr(cp
, SMK_FSROOT
) == cp
)
569 else if (strstr(cp
, SMK_FSTRANS
) == cp
)
574 commap
= strchr(cp
, ',');
583 strcpy(orig
, otheropts
);
584 free_page((unsigned long)otheropts
);
590 * smack_parse_opts_str - parse Smack specific mount options
591 * @options: mount options string
592 * @opts: where to store converted mount opts
594 * Returns 0 on success or -ENOMEM on error.
596 * converts Smack specific mount options to generic security option format
598 static int smack_parse_opts_str(char *options
,
599 struct security_mnt_opts
*opts
)
602 char *fsdefault
= NULL
, *fsfloor
= NULL
;
603 char *fshat
= NULL
, *fsroot
= NULL
, *fstransmute
= NULL
;
604 int rc
= -ENOMEM
, num_mnt_opts
= 0;
606 opts
->num_mnt_opts
= 0;
611 while ((p
= strsep(&options
, ",")) != NULL
) {
613 substring_t args
[MAX_OPT_ARGS
];
618 token
= match_token(p
, tokens
, args
);
624 fsdefault
= match_strdup(&args
[0]);
631 fsfloor
= match_strdup(&args
[0]);
638 fshat
= match_strdup(&args
[0]);
645 fsroot
= match_strdup(&args
[0]);
649 case Opt_fstransmute
:
652 fstransmute
= match_strdup(&args
[0]);
658 pr_warn("Smack: unknown mount option\n");
663 opts
->mnt_opts
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(char *), GFP_ATOMIC
);
667 opts
->mnt_opts_flags
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(int),
669 if (!opts
->mnt_opts_flags
) {
670 kfree(opts
->mnt_opts
);
675 opts
->mnt_opts
[num_mnt_opts
] = fsdefault
;
676 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSDEFAULT_MNT
;
679 opts
->mnt_opts
[num_mnt_opts
] = fsfloor
;
680 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSFLOOR_MNT
;
683 opts
->mnt_opts
[num_mnt_opts
] = fshat
;
684 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSHAT_MNT
;
687 opts
->mnt_opts
[num_mnt_opts
] = fsroot
;
688 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSROOT_MNT
;
691 opts
->mnt_opts
[num_mnt_opts
] = fstransmute
;
692 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSTRANS_MNT
;
695 opts
->num_mnt_opts
= num_mnt_opts
;
700 pr_warn("Smack: duplicate mount options\n");
712 * smack_set_mnt_opts - set Smack specific mount options
713 * @sb: the file system superblock
714 * @opts: Smack mount options
715 * @kern_flags: mount option from kernel space or user space
716 * @set_kern_flags: where to store converted mount opts
718 * Returns 0 on success, an error code on failure
720 * Allow filesystems with binary mount data to explicitly set Smack mount
723 static int smack_set_mnt_opts(struct super_block
*sb
,
724 struct security_mnt_opts
*opts
,
725 unsigned long kern_flags
,
726 unsigned long *set_kern_flags
)
728 struct dentry
*root
= sb
->s_root
;
729 struct inode
*inode
= d_backing_inode(root
);
730 struct superblock_smack
*sp
= sb
->s_security
;
731 struct inode_smack
*isp
;
732 struct smack_known
*skp
;
734 int num_opts
= opts
->num_mnt_opts
;
737 if (sp
->smk_initialized
)
740 sp
->smk_initialized
= 1;
742 for (i
= 0; i
< num_opts
; i
++) {
743 switch (opts
->mnt_opts_flags
[i
]) {
745 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
748 sp
->smk_default
= skp
;
751 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
757 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
763 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
769 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
780 if (!smack_privileged(CAP_MAC_ADMIN
)) {
782 * Unprivileged mounts don't get to specify Smack values.
787 * Unprivileged mounts get root and default from the caller.
789 skp
= smk_of_current();
791 sp
->smk_default
= skp
;
795 * Initialize the root inode.
797 isp
= inode
->i_security
;
799 isp
= new_inode_smack(sp
->smk_root
);
802 inode
->i_security
= isp
;
804 isp
->smk_inode
= sp
->smk_root
;
807 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
813 * smack_sb_kern_mount - Smack specific mount processing
814 * @sb: the file system superblock
815 * @flags: the mount flags
816 * @data: the smack mount options
818 * Returns 0 on success, an error code on failure
820 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
823 char *options
= data
;
824 struct security_mnt_opts opts
;
826 security_init_mnt_opts(&opts
);
831 rc
= smack_parse_opts_str(options
, &opts
);
836 rc
= smack_set_mnt_opts(sb
, &opts
, 0, NULL
);
839 security_free_mnt_opts(&opts
);
844 * smack_sb_statfs - Smack check on statfs
845 * @dentry: identifies the file system in question
847 * Returns 0 if current can read the floor of the filesystem,
848 * and error code otherwise
850 static int smack_sb_statfs(struct dentry
*dentry
)
852 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
854 struct smk_audit_info ad
;
856 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
857 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
859 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
860 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
869 * smack_bprm_set_creds - set creds for exec
870 * @bprm: the exec information
872 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
874 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
876 struct inode
*inode
= file_inode(bprm
->file
);
877 struct task_smack
*bsp
= bprm
->cred
->security
;
878 struct inode_smack
*isp
;
881 if (bprm
->cred_prepared
)
884 isp
= inode
->i_security
;
885 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
888 if (bprm
->unsafe
& (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
889 struct task_struct
*tracer
;
893 tracer
= ptrace_parent(current
);
894 if (likely(tracer
!= NULL
))
895 rc
= smk_ptrace_rule_check(tracer
,
903 } else if (bprm
->unsafe
)
906 bsp
->smk_task
= isp
->smk_task
;
907 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
913 * smack_bprm_committing_creds - Prepare to install the new credentials
916 * @bprm: binprm for exec
918 static void smack_bprm_committing_creds(struct linux_binprm
*bprm
)
920 struct task_smack
*bsp
= bprm
->cred
->security
;
922 if (bsp
->smk_task
!= bsp
->smk_forked
)
923 current
->pdeath_signal
= 0;
927 * smack_bprm_secureexec - Return the decision to use secureexec.
928 * @bprm: binprm for exec
930 * Returns 0 on success.
932 static int smack_bprm_secureexec(struct linux_binprm
*bprm
)
934 struct task_smack
*tsp
= current_security();
936 if (tsp
->smk_task
!= tsp
->smk_forked
)
947 * smack_inode_alloc_security - allocate an inode blob
948 * @inode: the inode in need of a blob
950 * Returns 0 if it gets a blob, -ENOMEM otherwise
952 static int smack_inode_alloc_security(struct inode
*inode
)
954 struct smack_known
*skp
= smk_of_current();
956 inode
->i_security
= new_inode_smack(skp
);
957 if (inode
->i_security
== NULL
)
963 * smack_inode_free_security - free an inode blob
964 * @inode: the inode with a blob
966 * Clears the blob pointer in inode
968 static void smack_inode_free_security(struct inode
*inode
)
970 kmem_cache_free(smack_inode_cache
, inode
->i_security
);
971 inode
->i_security
= NULL
;
975 * smack_inode_init_security - copy out the smack from an inode
976 * @inode: the newly created inode
977 * @dir: containing directory object
979 * @name: where to put the attribute name
980 * @value: where to put the attribute value
981 * @len: where to put the length of the attribute
983 * Returns 0 if it all works out, -ENOMEM if there's no memory
985 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
986 const struct qstr
*qstr
, const char **name
,
987 void **value
, size_t *len
)
989 struct inode_smack
*issp
= inode
->i_security
;
990 struct smack_known
*skp
= smk_of_current();
991 struct smack_known
*isp
= smk_of_inode(inode
);
992 struct smack_known
*dsp
= smk_of_inode(dir
);
996 *name
= XATTR_SMACK_SUFFIX
;
1000 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
1005 * If the access rule allows transmutation and
1006 * the directory requests transmutation then
1007 * by all means transmute.
1008 * Mark the inode as changed.
1010 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
1011 smk_inode_transmutable(dir
)) {
1013 issp
->smk_flags
|= SMK_INODE_CHANGED
;
1016 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
1020 *len
= strlen(isp
->smk_known
);
1027 * smack_inode_link - Smack check on link
1028 * @old_dentry: the existing object
1030 * @new_dentry: the new object
1032 * Returns 0 if access is permitted, an error code otherwise
1034 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1035 struct dentry
*new_dentry
)
1037 struct smack_known
*isp
;
1038 struct smk_audit_info ad
;
1041 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1042 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1044 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1045 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1046 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1048 if (rc
== 0 && d_is_positive(new_dentry
)) {
1049 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1050 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1051 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1052 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1059 * smack_inode_unlink - Smack check on inode deletion
1060 * @dir: containing directory object
1061 * @dentry: file to unlink
1063 * Returns 0 if current can write the containing directory
1064 * and the object, error code otherwise
1066 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1068 struct inode
*ip
= d_backing_inode(dentry
);
1069 struct smk_audit_info ad
;
1072 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1073 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1076 * You need write access to the thing you're unlinking
1078 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1079 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1082 * You also need write access to the containing directory
1084 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1085 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1086 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1087 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1093 * smack_inode_rmdir - Smack check on directory deletion
1094 * @dir: containing directory object
1095 * @dentry: directory to unlink
1097 * Returns 0 if current can write the containing directory
1098 * and the directory, error code otherwise
1100 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1102 struct smk_audit_info ad
;
1105 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1106 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1109 * You need write access to the thing you're removing
1111 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1112 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1115 * You also need write access to the containing directory
1117 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1118 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1119 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1120 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1127 * smack_inode_rename - Smack check on rename
1128 * @old_inode: unused
1129 * @old_dentry: the old object
1130 * @new_inode: unused
1131 * @new_dentry: the new object
1133 * Read and write access is required on both the old and
1136 * Returns 0 if access is permitted, an error code otherwise
1138 static int smack_inode_rename(struct inode
*old_inode
,
1139 struct dentry
*old_dentry
,
1140 struct inode
*new_inode
,
1141 struct dentry
*new_dentry
)
1144 struct smack_known
*isp
;
1145 struct smk_audit_info ad
;
1147 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1148 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1150 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1151 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1152 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1154 if (rc
== 0 && d_is_positive(new_dentry
)) {
1155 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1156 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1157 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1158 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1164 * smack_inode_permission - Smack version of permission()
1165 * @inode: the inode in question
1166 * @mask: the access requested
1168 * This is the important Smack hook.
1170 * Returns 0 if access is permitted, -EACCES otherwise
1172 static int smack_inode_permission(struct inode
*inode
, int mask
)
1174 struct smk_audit_info ad
;
1175 int no_block
= mask
& MAY_NOT_BLOCK
;
1178 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1180 * No permission to check. Existence test. Yup, it's there.
1185 /* May be droppable after audit */
1188 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1189 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1190 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1191 rc
= smk_bu_inode(inode
, mask
, rc
);
1196 * smack_inode_setattr - Smack check for setting attributes
1197 * @dentry: the object
1198 * @iattr: for the force flag
1200 * Returns 0 if access is permitted, an error code otherwise
1202 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1204 struct smk_audit_info ad
;
1208 * Need to allow for clearing the setuid bit.
1210 if (iattr
->ia_valid
& ATTR_FORCE
)
1212 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1213 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1215 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1216 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1221 * smack_inode_getattr - Smack check for getting attributes
1222 * @mnt: vfsmount of the object
1223 * @dentry: the object
1225 * Returns 0 if access is permitted, an error code otherwise
1227 static int smack_inode_getattr(const struct path
*path
)
1229 struct smk_audit_info ad
;
1230 struct inode
*inode
= d_backing_inode(path
->dentry
);
1233 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1234 smk_ad_setfield_u_fs_path(&ad
, *path
);
1235 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1236 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1241 * smack_inode_setxattr - Smack check for setting xattrs
1242 * @dentry: the object
1243 * @name: name of the attribute
1244 * @value: value of the attribute
1245 * @size: size of the value
1248 * This protects the Smack attribute explicitly.
1250 * Returns 0 if access is permitted, an error code otherwise
1252 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1253 const void *value
, size_t size
, int flags
)
1255 struct smk_audit_info ad
;
1256 struct smack_known
*skp
;
1258 int check_import
= 0;
1263 * Check label validity here so import won't fail in post_setxattr
1265 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1266 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1267 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1270 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1271 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1275 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1277 if (size
!= TRANS_TRUE_SIZE
||
1278 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1281 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1283 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1286 if (rc
== 0 && check_import
) {
1287 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1290 else if (skp
== NULL
|| (check_star
&&
1291 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1295 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1296 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1299 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1300 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1307 * smack_inode_post_setxattr - Apply the Smack update approved above
1309 * @name: attribute name
1310 * @value: attribute value
1311 * @size: attribute size
1314 * Set the pointer in the inode blob to the entry found
1315 * in the master label list.
1317 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1318 const void *value
, size_t size
, int flags
)
1320 struct smack_known
*skp
;
1321 struct inode_smack
*isp
= d_backing_inode(dentry
)->i_security
;
1323 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1324 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1328 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1329 skp
= smk_import_entry(value
, size
);
1331 isp
->smk_inode
= skp
;
1333 isp
->smk_inode
= &smack_known_invalid
;
1334 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1335 skp
= smk_import_entry(value
, size
);
1337 isp
->smk_task
= skp
;
1339 isp
->smk_task
= &smack_known_invalid
;
1340 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1341 skp
= smk_import_entry(value
, size
);
1343 isp
->smk_mmap
= skp
;
1345 isp
->smk_mmap
= &smack_known_invalid
;
1352 * smack_inode_getxattr - Smack check on getxattr
1353 * @dentry: the object
1356 * Returns 0 if access is permitted, an error code otherwise
1358 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1360 struct smk_audit_info ad
;
1363 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1364 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1366 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1367 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1372 * smack_inode_removexattr - Smack check on removexattr
1373 * @dentry: the object
1374 * @name: name of the attribute
1376 * Removing the Smack attribute requires CAP_MAC_ADMIN
1378 * Returns 0 if access is permitted, an error code otherwise
1380 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1382 struct inode_smack
*isp
;
1383 struct smk_audit_info ad
;
1386 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1387 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1388 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1389 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1390 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1391 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1392 if (!smack_privileged(CAP_MAC_ADMIN
))
1395 rc
= cap_inode_removexattr(dentry
, name
);
1400 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1401 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1403 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1404 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1408 isp
= d_backing_inode(dentry
)->i_security
;
1410 * Don't do anything special for these.
1411 * XATTR_NAME_SMACKIPIN
1412 * XATTR_NAME_SMACKIPOUT
1413 * XATTR_NAME_SMACKEXEC
1415 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
1416 isp
->smk_task
= NULL
;
1417 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1418 isp
->smk_mmap
= NULL
;
1419 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1420 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1426 * smack_inode_getsecurity - get smack xattrs
1427 * @inode: the object
1428 * @name: attribute name
1429 * @buffer: where to put the result
1432 * Returns the size of the attribute or an error code
1434 static int smack_inode_getsecurity(const struct inode
*inode
,
1435 const char *name
, void **buffer
,
1438 struct socket_smack
*ssp
;
1439 struct socket
*sock
;
1440 struct super_block
*sbp
;
1441 struct inode
*ip
= (struct inode
*)inode
;
1442 struct smack_known
*isp
;
1446 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1447 isp
= smk_of_inode(inode
);
1448 ilen
= strlen(isp
->smk_known
);
1449 *buffer
= isp
->smk_known
;
1454 * The rest of the Smack xattrs are only on sockets.
1457 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1460 sock
= SOCKET_I(ip
);
1461 if (sock
== NULL
|| sock
->sk
== NULL
)
1464 ssp
= sock
->sk
->sk_security
;
1466 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1468 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1473 ilen
= strlen(isp
->smk_known
);
1475 *buffer
= isp
->smk_known
;
1484 * smack_inode_listsecurity - list the Smack attributes
1485 * @inode: the object
1486 * @buffer: where they go
1487 * @buffer_size: size of buffer
1489 * Returns 0 on success, -EINVAL otherwise
1491 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1494 int len
= sizeof(XATTR_NAME_SMACK
);
1496 if (buffer
!= NULL
&& len
<= buffer_size
)
1497 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1503 * smack_inode_getsecid - Extract inode's security id
1504 * @inode: inode to extract the info from
1505 * @secid: where result will be saved
1507 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
1509 struct inode_smack
*isp
= inode
->i_security
;
1511 *secid
= isp
->smk_inode
->smk_secid
;
1519 * smack_file_permission - Smack check on file operations
1525 * Should access checks be done on each read or write?
1526 * UNICOS and SELinux say yes.
1527 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1529 * I'll say no for now. Smack does not do the frequent
1530 * label changing that SELinux does.
1532 static int smack_file_permission(struct file
*file
, int mask
)
1538 * smack_file_alloc_security - assign a file security blob
1541 * The security blob for a file is a pointer to the master
1542 * label list, so no allocation is done.
1544 * f_security is the owner security information. It
1545 * isn't used on file access checks, it's for send_sigio.
1549 static int smack_file_alloc_security(struct file
*file
)
1551 struct smack_known
*skp
= smk_of_current();
1553 file
->f_security
= skp
;
1558 * smack_file_free_security - clear a file security blob
1561 * The security blob for a file is a pointer to the master
1562 * label list, so no memory is freed.
1564 static void smack_file_free_security(struct file
*file
)
1566 file
->f_security
= NULL
;
1570 * smack_file_ioctl - Smack check on ioctls
1575 * Relies heavily on the correct use of the ioctl command conventions.
1577 * Returns 0 if allowed, error code otherwise
1579 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1583 struct smk_audit_info ad
;
1584 struct inode
*inode
= file_inode(file
);
1586 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1587 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1589 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1590 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1591 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1594 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1595 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1596 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1603 * smack_file_lock - Smack check on file locking
1607 * Returns 0 if current has lock access, error code otherwise
1609 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1611 struct smk_audit_info ad
;
1613 struct inode
*inode
= file_inode(file
);
1615 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1616 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1617 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1618 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1623 * smack_file_fcntl - Smack check on fcntl
1625 * @cmd: what action to check
1628 * Generally these operations are harmless.
1629 * File locking operations present an obvious mechanism
1630 * for passing information, so they require write access.
1632 * Returns 0 if current has access, error code otherwise
1634 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1637 struct smk_audit_info ad
;
1639 struct inode
*inode
= file_inode(file
);
1646 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1647 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1648 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1649 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1653 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1654 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1655 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1656 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1667 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1668 * if mapping anonymous memory.
1669 * @file contains the file structure for file to map (may be NULL).
1670 * @reqprot contains the protection requested by the application.
1671 * @prot contains the protection that will be applied by the kernel.
1672 * @flags contains the operational flags.
1673 * Return 0 if permission is granted.
1675 static int smack_mmap_file(struct file
*file
,
1676 unsigned long reqprot
, unsigned long prot
,
1677 unsigned long flags
)
1679 struct smack_known
*skp
;
1680 struct smack_known
*mkp
;
1681 struct smack_rule
*srp
;
1682 struct task_smack
*tsp
;
1683 struct smack_known
*okp
;
1684 struct inode_smack
*isp
;
1693 isp
= file_inode(file
)->i_security
;
1694 if (isp
->smk_mmap
== NULL
)
1696 mkp
= isp
->smk_mmap
;
1698 tsp
= current_security();
1699 skp
= smk_of_current();
1704 * For each Smack rule associated with the subject
1705 * label verify that the SMACK64MMAP also has access
1706 * to that rule's object label.
1708 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1709 okp
= srp
->smk_object
;
1711 * Matching labels always allows access.
1713 if (mkp
->smk_known
== okp
->smk_known
)
1716 * If there is a matching local rule take
1717 * that into account as well.
1719 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1723 may
= srp
->smk_access
;
1725 may
&= srp
->smk_access
;
1727 * If may is zero the SMACK64MMAP subject can't
1728 * possibly have less access.
1734 * Fetch the global list entry.
1735 * If there isn't one a SMACK64MMAP subject
1736 * can't have as much access as current.
1738 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1740 if (mmay
== -ENOENT
) {
1745 * If there is a local entry it modifies the
1746 * potential access, too.
1748 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1750 if (tmay
!= -ENOENT
)
1754 * If there is any access available to current that is
1755 * not available to a SMACK64MMAP subject
1758 if ((may
| mmay
) != mmay
) {
1770 * smack_file_set_fowner - set the file security blob value
1771 * @file: object in question
1774 static void smack_file_set_fowner(struct file
*file
)
1776 file
->f_security
= smk_of_current();
1780 * smack_file_send_sigiotask - Smack on sigio
1781 * @tsk: The target task
1782 * @fown: the object the signal come from
1785 * Allow a privileged task to get signals even if it shouldn't
1787 * Returns 0 if a subject with the object's smack could
1788 * write to the task, an error code otherwise.
1790 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1791 struct fown_struct
*fown
, int signum
)
1793 struct smack_known
*skp
;
1794 struct smack_known
*tkp
= smk_of_task(tsk
->cred
->security
);
1797 struct smk_audit_info ad
;
1800 * struct fown_struct is never outside the context of a struct file
1802 file
= container_of(fown
, struct file
, f_owner
);
1804 /* we don't log here as rc can be overriden */
1805 skp
= file
->f_security
;
1806 rc
= smk_access(skp
, tkp
, MAY_WRITE
, NULL
);
1807 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_WRITE
, rc
);
1808 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1811 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1812 smk_ad_setfield_u_tsk(&ad
, tsk
);
1813 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_WRITE
, rc
, &ad
);
1818 * smack_file_receive - Smack file receive check
1821 * Returns 0 if current has access, error code otherwise
1823 static int smack_file_receive(struct file
*file
)
1827 struct smk_audit_info ad
;
1828 struct inode
*inode
= file_inode(file
);
1830 if (unlikely(IS_PRIVATE(inode
)))
1833 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1834 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1836 * This code relies on bitmasks.
1838 if (file
->f_mode
& FMODE_READ
)
1840 if (file
->f_mode
& FMODE_WRITE
)
1843 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1844 rc
= smk_bu_file(file
, may
, rc
);
1849 * smack_file_open - Smack dentry open processing
1851 * @cred: task credential
1853 * Set the security blob in the file structure.
1854 * Allow the open only if the task has read access. There are
1855 * many read operations (e.g. fstat) that you can do with an
1856 * fd even if you have the file open write-only.
1860 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1862 struct task_smack
*tsp
= cred
->security
;
1863 struct inode
*inode
= file_inode(file
);
1864 struct smk_audit_info ad
;
1867 if (smack_privileged(CAP_MAC_OVERRIDE
))
1870 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1871 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1872 rc
= smk_access(tsp
->smk_task
, smk_of_inode(inode
), MAY_READ
, &ad
);
1873 rc
= smk_bu_credfile(cred
, file
, MAY_READ
, rc
);
1883 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1884 * @new: the new credentials
1885 * @gfp: the atomicity of any memory allocations
1887 * Prepare a blank set of credentials for modification. This must allocate all
1888 * the memory the LSM module might require such that cred_transfer() can
1889 * complete without error.
1891 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1893 struct task_smack
*tsp
;
1895 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1899 cred
->security
= tsp
;
1906 * smack_cred_free - "free" task-level security credentials
1907 * @cred: the credentials in question
1910 static void smack_cred_free(struct cred
*cred
)
1912 struct task_smack
*tsp
= cred
->security
;
1913 struct smack_rule
*rp
;
1914 struct list_head
*l
;
1915 struct list_head
*n
;
1919 cred
->security
= NULL
;
1921 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1922 rp
= list_entry(l
, struct smack_rule
, list
);
1923 list_del(&rp
->list
);
1930 * smack_cred_prepare - prepare new set of credentials for modification
1931 * @new: the new credentials
1932 * @old: the original credentials
1933 * @gfp: the atomicity of any memory allocations
1935 * Prepare a new set of credentials for modification.
1937 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1940 struct task_smack
*old_tsp
= old
->security
;
1941 struct task_smack
*new_tsp
;
1944 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
1945 if (new_tsp
== NULL
)
1948 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1952 new->security
= new_tsp
;
1957 * smack_cred_transfer - Transfer the old credentials to the new credentials
1958 * @new: the new credentials
1959 * @old: the original credentials
1961 * Fill in a set of blank credentials from another set of credentials.
1963 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1965 struct task_smack
*old_tsp
= old
->security
;
1966 struct task_smack
*new_tsp
= new->security
;
1968 new_tsp
->smk_task
= old_tsp
->smk_task
;
1969 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1970 mutex_init(&new_tsp
->smk_rules_lock
);
1971 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1974 /* cbs copy rule list */
1978 * smack_kernel_act_as - Set the subjective context in a set of credentials
1979 * @new: points to the set of credentials to be modified.
1980 * @secid: specifies the security ID to be set
1982 * Set the security data for a kernel service.
1984 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1986 struct task_smack
*new_tsp
= new->security
;
1987 struct smack_known
*skp
= smack_from_secid(secid
);
1992 new_tsp
->smk_task
= skp
;
1997 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1998 * @new: points to the set of credentials to be modified
1999 * @inode: points to the inode to use as a reference
2001 * Set the file creation context in a set of credentials to the same
2002 * as the objective context of the specified inode
2004 static int smack_kernel_create_files_as(struct cred
*new,
2005 struct inode
*inode
)
2007 struct inode_smack
*isp
= inode
->i_security
;
2008 struct task_smack
*tsp
= new->security
;
2010 tsp
->smk_forked
= isp
->smk_inode
;
2011 tsp
->smk_task
= tsp
->smk_forked
;
2016 * smk_curacc_on_task - helper to log task related access
2017 * @p: the task object
2018 * @access: the access requested
2019 * @caller: name of the calling function for audit
2021 * Return 0 if access is permitted
2023 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
2026 struct smk_audit_info ad
;
2027 struct smack_known
*skp
= smk_of_task_struct(p
);
2030 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2031 smk_ad_setfield_u_tsk(&ad
, p
);
2032 rc
= smk_curacc(skp
, access
, &ad
);
2033 rc
= smk_bu_task(p
, access
, rc
);
2038 * smack_task_setpgid - Smack check on setting pgid
2039 * @p: the task object
2042 * Return 0 if write access is permitted
2044 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2046 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2050 * smack_task_getpgid - Smack access check for getpgid
2051 * @p: the object task
2053 * Returns 0 if current can read the object task, error code otherwise
2055 static int smack_task_getpgid(struct task_struct
*p
)
2057 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2061 * smack_task_getsid - Smack access check for getsid
2062 * @p: the object task
2064 * Returns 0 if current can read the object task, error code otherwise
2066 static int smack_task_getsid(struct task_struct
*p
)
2068 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2072 * smack_task_getsecid - get the secid of the task
2073 * @p: the object task
2074 * @secid: where to put the result
2076 * Sets the secid to contain a u32 version of the smack label.
2078 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2080 struct smack_known
*skp
= smk_of_task_struct(p
);
2082 *secid
= skp
->smk_secid
;
2086 * smack_task_setnice - Smack check on setting nice
2087 * @p: the task object
2090 * Return 0 if write access is permitted
2092 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2094 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2098 * smack_task_setioprio - Smack check on setting ioprio
2099 * @p: the task object
2102 * Return 0 if write access is permitted
2104 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2106 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2110 * smack_task_getioprio - Smack check on reading ioprio
2111 * @p: the task object
2113 * Return 0 if read access is permitted
2115 static int smack_task_getioprio(struct task_struct
*p
)
2117 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2121 * smack_task_setscheduler - Smack check on setting scheduler
2122 * @p: the task object
2126 * Return 0 if read access is permitted
2128 static int smack_task_setscheduler(struct task_struct
*p
)
2130 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2134 * smack_task_getscheduler - Smack check on reading scheduler
2135 * @p: the task object
2137 * Return 0 if read access is permitted
2139 static int smack_task_getscheduler(struct task_struct
*p
)
2141 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2145 * smack_task_movememory - Smack check on moving memory
2146 * @p: the task object
2148 * Return 0 if write access is permitted
2150 static int smack_task_movememory(struct task_struct
*p
)
2152 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2156 * smack_task_kill - Smack check on signal delivery
2157 * @p: the task object
2160 * @secid: identifies the smack to use in lieu of current's
2162 * Return 0 if write access is permitted
2164 * The secid behavior is an artifact of an SELinux hack
2165 * in the USB code. Someday it may go away.
2167 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
2170 struct smk_audit_info ad
;
2171 struct smack_known
*skp
;
2172 struct smack_known
*tkp
= smk_of_task_struct(p
);
2175 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2176 smk_ad_setfield_u_tsk(&ad
, p
);
2178 * Sending a signal requires that the sender
2179 * can write the receiver.
2182 rc
= smk_curacc(tkp
, MAY_WRITE
, &ad
);
2183 rc
= smk_bu_task(p
, MAY_WRITE
, rc
);
2187 * If the secid isn't 0 we're dealing with some USB IO
2188 * specific behavior. This is not clean. For one thing
2189 * we can't take privilege into account.
2191 skp
= smack_from_secid(secid
);
2192 rc
= smk_access(skp
, tkp
, MAY_WRITE
, &ad
);
2193 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_WRITE
, rc
);
2198 * smack_task_wait - Smack access check for waiting
2199 * @p: task to wait for
2203 static int smack_task_wait(struct task_struct
*p
)
2206 * Allow the operation to succeed.
2208 * In userless environments (e.g. phones) programs
2209 * get marked with SMACK64EXEC and even if the parent
2210 * and child shouldn't be talking the parent still
2211 * may expect to know when the child exits.
2217 * smack_task_to_inode - copy task smack into the inode blob
2218 * @p: task to copy from
2219 * @inode: inode to copy to
2221 * Sets the smack pointer in the inode security blob
2223 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2225 struct inode_smack
*isp
= inode
->i_security
;
2226 struct smack_known
*skp
= smk_of_task_struct(p
);
2228 isp
->smk_inode
= skp
;
2236 * smack_sk_alloc_security - Allocate a socket blob
2239 * @gfp_flags: memory allocation flags
2241 * Assign Smack pointers to current
2243 * Returns 0 on success, -ENOMEM is there's no memory
2245 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2247 struct smack_known
*skp
= smk_of_current();
2248 struct socket_smack
*ssp
;
2250 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2256 ssp
->smk_packet
= NULL
;
2258 sk
->sk_security
= ssp
;
2264 * smack_sk_free_security - Free a socket blob
2267 * Clears the blob pointer
2269 static void smack_sk_free_security(struct sock
*sk
)
2271 kfree(sk
->sk_security
);
2275 * smack_ipv4host_label - check host based restrictions
2276 * @sip: the object end
2278 * looks for host based access restrictions
2280 * This version will only be appropriate for really small sets of single label
2281 * hosts. The caller is responsible for ensuring that the RCU read lock is
2282 * taken before calling this function.
2284 * Returns the label of the far end or NULL if it's not special.
2286 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2288 struct smk_net4addr
*snp
;
2289 struct in_addr
*siap
= &sip
->sin_addr
;
2291 if (siap
->s_addr
== 0)
2294 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2296 * we break after finding the first match because
2297 * the list is sorted from longest to shortest mask
2298 * so we have found the most specific match
2300 if (snp
->smk_host
.s_addr
==
2301 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2302 return snp
->smk_label
;
2307 #if IS_ENABLED(CONFIG_IPV6)
2309 * smk_ipv6_localhost - Check for local ipv6 host address
2312 * Returns boolean true if this is the localhost address
2314 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2316 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2317 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2319 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2320 ntohs(be16p
[7]) == 1)
2326 * smack_ipv6host_label - check host based restrictions
2327 * @sip: the object end
2329 * looks for host based access restrictions
2331 * This version will only be appropriate for really small sets of single label
2332 * hosts. The caller is responsible for ensuring that the RCU read lock is
2333 * taken before calling this function.
2335 * Returns the label of the far end or NULL if it's not special.
2337 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2339 struct smk_net6addr
*snp
;
2340 struct in6_addr
*sap
= &sip
->sin6_addr
;
2345 * It's local. Don't look for a host label.
2347 if (smk_ipv6_localhost(sip
))
2350 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2352 * we break after finding the first match because
2353 * the list is sorted from longest to shortest mask
2354 * so we have found the most specific match
2356 for (found
= 1, i
= 0; i
< 8; i
++) {
2358 * If the label is NULL the entry has
2359 * been renounced. Ignore it.
2361 if (snp
->smk_label
== NULL
)
2363 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2364 snp
->smk_host
.s6_addr16
[i
]) {
2370 return snp
->smk_label
;
2375 #endif /* CONFIG_IPV6 */
2378 * smack_netlabel - Set the secattr on a socket
2380 * @labeled: socket label scheme
2382 * Convert the outbound smack value (smk_out) to a
2383 * secattr and attach it to the socket.
2385 * Returns 0 on success or an error code
2387 static int smack_netlabel(struct sock
*sk
, int labeled
)
2389 struct smack_known
*skp
;
2390 struct socket_smack
*ssp
= sk
->sk_security
;
2394 * Usually the netlabel code will handle changing the
2395 * packet labeling based on the label.
2396 * The case of a single label host is different, because
2397 * a single label host should never get a labeled packet
2398 * even though the label is usually associated with a packet
2402 bh_lock_sock_nested(sk
);
2404 if (ssp
->smk_out
== smack_net_ambient
||
2405 labeled
== SMACK_UNLABELED_SOCKET
)
2406 netlbl_sock_delattr(sk
);
2409 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2419 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2421 * @sap: the destination address
2423 * Set the correct secattr for the given socket based on the destination
2424 * address and perform any outbound access checks needed.
2426 * Returns 0 on success or an error code.
2429 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2431 struct smack_known
*skp
;
2434 struct smack_known
*hkp
;
2435 struct socket_smack
*ssp
= sk
->sk_security
;
2436 struct smk_audit_info ad
;
2439 hkp
= smack_ipv4host_label(sap
);
2442 struct lsm_network_audit net
;
2444 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2445 ad
.a
.u
.net
->family
= sap
->sin_family
;
2446 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2447 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2449 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2451 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2452 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2454 sk_lbl
= SMACK_CIPSO_SOCKET
;
2461 return smack_netlabel(sk
, sk_lbl
);
2464 #if IS_ENABLED(CONFIG_IPV6)
2466 * smk_ipv6_check - check Smack access
2467 * @subject: subject Smack label
2468 * @object: object Smack label
2470 * @act: the action being taken
2472 * Check an IPv6 access
2474 static int smk_ipv6_check(struct smack_known
*subject
,
2475 struct smack_known
*object
,
2476 struct sockaddr_in6
*address
, int act
)
2479 struct lsm_network_audit net
;
2481 struct smk_audit_info ad
;
2485 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2486 ad
.a
.u
.net
->family
= PF_INET6
;
2487 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2488 if (act
== SMK_RECEIVING
)
2489 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2491 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2493 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2494 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2497 #endif /* CONFIG_IPV6 */
2499 #ifdef SMACK_IPV6_PORT_LABELING
2501 * smk_ipv6_port_label - Smack port access table management
2505 * Create or update the port list entry
2507 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2509 struct sock
*sk
= sock
->sk
;
2510 struct sockaddr_in6
*addr6
;
2511 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2512 struct smk_port_label
*spp
;
2513 unsigned short port
= 0;
2515 if (address
== NULL
) {
2517 * This operation is changing the Smack information
2518 * on the bound socket. Take the changes to the port
2521 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2522 if (sk
!= spp
->smk_sock
)
2524 spp
->smk_in
= ssp
->smk_in
;
2525 spp
->smk_out
= ssp
->smk_out
;
2529 * A NULL address is only used for updating existing
2530 * bound entries. If there isn't one, it's OK.
2535 addr6
= (struct sockaddr_in6
*)address
;
2536 port
= ntohs(addr6
->sin6_port
);
2538 * This is a special case that is safely ignored.
2544 * Look for an existing port list entry.
2545 * This is an indication that a port is getting reused.
2547 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2548 if (spp
->smk_port
!= port
)
2550 spp
->smk_port
= port
;
2552 spp
->smk_in
= ssp
->smk_in
;
2553 spp
->smk_out
= ssp
->smk_out
;
2558 * A new port entry is required.
2560 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2564 spp
->smk_port
= port
;
2566 spp
->smk_in
= ssp
->smk_in
;
2567 spp
->smk_out
= ssp
->smk_out
;
2569 list_add(&spp
->list
, &smk_ipv6_port_list
);
2574 * smk_ipv6_port_check - check Smack port access
2578 * Create or update the port list entry
2580 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2583 struct smk_port_label
*spp
;
2584 struct socket_smack
*ssp
= sk
->sk_security
;
2585 struct smack_known
*skp
= NULL
;
2586 unsigned short port
;
2587 struct smack_known
*object
;
2589 if (act
== SMK_RECEIVING
) {
2590 skp
= smack_ipv6host_label(address
);
2591 object
= ssp
->smk_in
;
2594 object
= smack_ipv6host_label(address
);
2598 * The other end is a single label host.
2600 if (skp
!= NULL
&& object
!= NULL
)
2601 return smk_ipv6_check(skp
, object
, address
, act
);
2603 skp
= smack_net_ambient
;
2605 object
= smack_net_ambient
;
2608 * It's remote, so port lookup does no good.
2610 if (!smk_ipv6_localhost(address
))
2611 return smk_ipv6_check(skp
, object
, address
, act
);
2614 * It's local so the send check has to have passed.
2616 if (act
== SMK_RECEIVING
)
2619 port
= ntohs(address
->sin6_port
);
2620 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2621 if (spp
->smk_port
!= port
)
2623 object
= spp
->smk_in
;
2624 if (act
== SMK_CONNECTING
)
2625 ssp
->smk_packet
= spp
->smk_out
;
2629 return smk_ipv6_check(skp
, object
, address
, act
);
2631 #endif /* SMACK_IPV6_PORT_LABELING */
2634 * smack_inode_setsecurity - set smack xattrs
2635 * @inode: the object
2636 * @name: attribute name
2637 * @value: attribute value
2638 * @size: size of the attribute
2641 * Sets the named attribute in the appropriate blob
2643 * Returns 0 on success, or an error code
2645 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2646 const void *value
, size_t size
, int flags
)
2648 struct smack_known
*skp
;
2649 struct inode_smack
*nsp
= inode
->i_security
;
2650 struct socket_smack
*ssp
;
2651 struct socket
*sock
;
2654 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2657 skp
= smk_import_entry(value
, size
);
2659 return PTR_ERR(skp
);
2661 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2662 nsp
->smk_inode
= skp
;
2663 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2667 * The rest of the Smack xattrs are only on sockets.
2669 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2672 sock
= SOCKET_I(inode
);
2673 if (sock
== NULL
|| sock
->sk
== NULL
)
2676 ssp
= sock
->sk
->sk_security
;
2678 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2680 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2682 if (sock
->sk
->sk_family
== PF_INET
) {
2683 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2686 "Smack: \"%s\" netlbl error %d.\n",
2692 #ifdef SMACK_IPV6_PORT_LABELING
2693 if (sock
->sk
->sk_family
== PF_INET6
)
2694 smk_ipv6_port_label(sock
, NULL
);
2701 * smack_socket_post_create - finish socket setup
2703 * @family: protocol family
2708 * Sets the netlabel information on the socket
2710 * Returns 0 on success, and error code otherwise
2712 static int smack_socket_post_create(struct socket
*sock
, int family
,
2713 int type
, int protocol
, int kern
)
2715 struct socket_smack
*ssp
;
2717 if (sock
->sk
== NULL
)
2721 * Sockets created by kernel threads receive web label.
2723 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2724 ssp
= sock
->sk
->sk_security
;
2725 ssp
->smk_in
= &smack_known_web
;
2726 ssp
->smk_out
= &smack_known_web
;
2729 if (family
!= PF_INET
)
2732 * Set the outbound netlbl.
2734 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2737 #ifdef SMACK_IPV6_PORT_LABELING
2739 * smack_socket_bind - record port binding information.
2741 * @address: the port address
2742 * @addrlen: size of the address
2744 * Records the label bound to a port.
2748 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2751 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2752 smk_ipv6_port_label(sock
, address
);
2755 #endif /* SMACK_IPV6_PORT_LABELING */
2758 * smack_socket_connect - connect access check
2760 * @sap: the other end
2761 * @addrlen: size of sap
2763 * Verifies that a connection may be possible
2765 * Returns 0 on success, and error code otherwise
2767 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2771 #if IS_ENABLED(CONFIG_IPV6)
2772 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2774 #ifdef SMACK_IPV6_SECMARK_LABELING
2775 struct smack_known
*rsp
;
2776 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2779 if (sock
->sk
== NULL
)
2782 switch (sock
->sk
->sk_family
) {
2784 if (addrlen
< sizeof(struct sockaddr_in
))
2786 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2789 if (addrlen
< sizeof(struct sockaddr_in6
))
2791 #ifdef SMACK_IPV6_SECMARK_LABELING
2792 rsp
= smack_ipv6host_label(sip
);
2794 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2797 #ifdef SMACK_IPV6_PORT_LABELING
2798 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2806 * smack_flags_to_may - convert S_ to MAY_ values
2807 * @flags: the S_ value
2809 * Returns the equivalent MAY_ value
2811 static int smack_flags_to_may(int flags
)
2815 if (flags
& S_IRUGO
)
2817 if (flags
& S_IWUGO
)
2819 if (flags
& S_IXUGO
)
2826 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2831 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2833 struct smack_known
*skp
= smk_of_current();
2835 msg
->security
= skp
;
2840 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2843 * Clears the blob pointer
2845 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2847 msg
->security
= NULL
;
2851 * smack_of_shm - the smack pointer for the shm
2854 * Returns a pointer to the smack value
2856 static struct smack_known
*smack_of_shm(struct shmid_kernel
*shp
)
2858 return (struct smack_known
*)shp
->shm_perm
.security
;
2862 * smack_shm_alloc_security - Set the security blob for shm
2867 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2869 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2870 struct smack_known
*skp
= smk_of_current();
2872 isp
->security
= skp
;
2877 * smack_shm_free_security - Clear the security blob for shm
2880 * Clears the blob pointer
2882 static void smack_shm_free_security(struct shmid_kernel
*shp
)
2884 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2886 isp
->security
= NULL
;
2890 * smk_curacc_shm : check if current has access on shm
2892 * @access : access requested
2894 * Returns 0 if current has the requested access, error code otherwise
2896 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2898 struct smack_known
*ssp
= smack_of_shm(shp
);
2899 struct smk_audit_info ad
;
2903 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2904 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2906 rc
= smk_curacc(ssp
, access
, &ad
);
2907 rc
= smk_bu_current("shm", ssp
, access
, rc
);
2912 * smack_shm_associate - Smack access check for shm
2914 * @shmflg: access requested
2916 * Returns 0 if current has the requested access, error code otherwise
2918 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2922 may
= smack_flags_to_may(shmflg
);
2923 return smk_curacc_shm(shp
, may
);
2927 * smack_shm_shmctl - Smack access check for shm
2929 * @cmd: what it wants to do
2931 * Returns 0 if current has the requested access, error code otherwise
2933 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2946 may
= MAY_READWRITE
;
2951 * System level information.
2957 return smk_curacc_shm(shp
, may
);
2961 * smack_shm_shmat - Smack access for shmat
2964 * @shmflg: access requested
2966 * Returns 0 if current has the requested access, error code otherwise
2968 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2973 may
= smack_flags_to_may(shmflg
);
2974 return smk_curacc_shm(shp
, may
);
2978 * smack_of_sem - the smack pointer for the sem
2981 * Returns a pointer to the smack value
2983 static struct smack_known
*smack_of_sem(struct sem_array
*sma
)
2985 return (struct smack_known
*)sma
->sem_perm
.security
;
2989 * smack_sem_alloc_security - Set the security blob for sem
2994 static int smack_sem_alloc_security(struct sem_array
*sma
)
2996 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2997 struct smack_known
*skp
= smk_of_current();
2999 isp
->security
= skp
;
3004 * smack_sem_free_security - Clear the security blob for sem
3007 * Clears the blob pointer
3009 static void smack_sem_free_security(struct sem_array
*sma
)
3011 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
3013 isp
->security
= NULL
;
3017 * smk_curacc_sem : check if current has access on sem
3019 * @access : access requested
3021 * Returns 0 if current has the requested access, error code otherwise
3023 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
3025 struct smack_known
*ssp
= smack_of_sem(sma
);
3026 struct smk_audit_info ad
;
3030 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3031 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
3033 rc
= smk_curacc(ssp
, access
, &ad
);
3034 rc
= smk_bu_current("sem", ssp
, access
, rc
);
3039 * smack_sem_associate - Smack access check for sem
3041 * @semflg: access requested
3043 * Returns 0 if current has the requested access, error code otherwise
3045 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
3049 may
= smack_flags_to_may(semflg
);
3050 return smk_curacc_sem(sma
, may
);
3054 * smack_sem_shmctl - Smack access check for sem
3056 * @cmd: what it wants to do
3058 * Returns 0 if current has the requested access, error code otherwise
3060 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
3078 may
= MAY_READWRITE
;
3083 * System level information
3090 return smk_curacc_sem(sma
, may
);
3094 * smack_sem_semop - Smack checks of semaphore operations
3100 * Treated as read and write in all cases.
3102 * Returns 0 if access is allowed, error code otherwise
3104 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
3105 unsigned nsops
, int alter
)
3107 return smk_curacc_sem(sma
, MAY_READWRITE
);
3111 * smack_msg_alloc_security - Set the security blob for msg
3116 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
3118 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
3119 struct smack_known
*skp
= smk_of_current();
3121 kisp
->security
= skp
;
3126 * smack_msg_free_security - Clear the security blob for msg
3129 * Clears the blob pointer
3131 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
3133 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
3135 kisp
->security
= NULL
;
3139 * smack_of_msq - the smack pointer for the msq
3142 * Returns a pointer to the smack label entry
3144 static struct smack_known
*smack_of_msq(struct msg_queue
*msq
)
3146 return (struct smack_known
*)msq
->q_perm
.security
;
3150 * smk_curacc_msq : helper to check if current has access on msq
3152 * @access : access requested
3154 * return 0 if current has access, error otherwise
3156 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
3158 struct smack_known
*msp
= smack_of_msq(msq
);
3159 struct smk_audit_info ad
;
3163 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3164 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
3166 rc
= smk_curacc(msp
, access
, &ad
);
3167 rc
= smk_bu_current("msq", msp
, access
, rc
);
3172 * smack_msg_queue_associate - Smack access check for msg_queue
3174 * @msqflg: access requested
3176 * Returns 0 if current has the requested access, error code otherwise
3178 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
3182 may
= smack_flags_to_may(msqflg
);
3183 return smk_curacc_msq(msq
, may
);
3187 * smack_msg_queue_msgctl - Smack access check for msg_queue
3189 * @cmd: what it wants to do
3191 * Returns 0 if current has the requested access, error code otherwise
3193 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
3204 may
= MAY_READWRITE
;
3209 * System level information
3216 return smk_curacc_msq(msq
, may
);
3220 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3223 * @msqflg: access requested
3225 * Returns 0 if current has the requested access, error code otherwise
3227 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
3232 may
= smack_flags_to_may(msqflg
);
3233 return smk_curacc_msq(msq
, may
);
3237 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3244 * Returns 0 if current has read and write access, error code otherwise
3246 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
3247 struct task_struct
*target
, long type
, int mode
)
3249 return smk_curacc_msq(msq
, MAY_READWRITE
);
3253 * smack_ipc_permission - Smack access for ipc_permission()
3254 * @ipp: the object permissions
3255 * @flag: access requested
3257 * Returns 0 if current has read and write access, error code otherwise
3259 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3261 struct smack_known
*iskp
= ipp
->security
;
3262 int may
= smack_flags_to_may(flag
);
3263 struct smk_audit_info ad
;
3267 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3268 ad
.a
.u
.ipc_id
= ipp
->id
;
3270 rc
= smk_curacc(iskp
, may
, &ad
);
3271 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3276 * smack_ipc_getsecid - Extract smack security id
3277 * @ipp: the object permissions
3278 * @secid: where result will be saved
3280 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3282 struct smack_known
*iskp
= ipp
->security
;
3284 *secid
= iskp
->smk_secid
;
3288 * smack_d_instantiate - Make sure the blob is correct on an inode
3289 * @opt_dentry: dentry where inode will be attached
3290 * @inode: the object
3292 * Set the inode's security blob if it hasn't been done already.
3294 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3296 struct super_block
*sbp
;
3297 struct superblock_smack
*sbsp
;
3298 struct inode_smack
*isp
;
3299 struct smack_known
*skp
;
3300 struct smack_known
*ckp
= smk_of_current();
3301 struct smack_known
*final
;
3302 char trattr
[TRANS_TRUE_SIZE
];
3310 isp
= inode
->i_security
;
3312 mutex_lock(&isp
->smk_lock
);
3314 * If the inode is already instantiated
3315 * take the quick way out
3317 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3321 sbsp
= sbp
->s_security
;
3323 * We're going to use the superblock default label
3324 * if there's no label on the file.
3326 final
= sbsp
->smk_default
;
3329 * If this is the root inode the superblock
3330 * may be in the process of initialization.
3331 * If that is the case use the root value out
3332 * of the superblock.
3334 if (opt_dentry
->d_parent
== opt_dentry
) {
3335 switch (sbp
->s_magic
) {
3336 case CGROUP_SUPER_MAGIC
:
3338 * The cgroup filesystem is never mounted,
3339 * so there's no opportunity to set the mount
3342 sbsp
->smk_root
= &smack_known_star
;
3343 sbsp
->smk_default
= &smack_known_star
;
3344 isp
->smk_inode
= sbsp
->smk_root
;
3348 * What about shmem/tmpfs anonymous files with dentry
3349 * obtained from d_alloc_pseudo()?
3351 isp
->smk_inode
= smk_of_current();
3354 isp
->smk_inode
= sbsp
->smk_root
;
3357 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3362 * This is pretty hackish.
3363 * Casey says that we shouldn't have to do
3364 * file system specific code, but it does help
3365 * with keeping it simple.
3367 switch (sbp
->s_magic
) {
3371 case CGROUP_SUPER_MAGIC
:
3373 * Casey says that it's a little embarrassing
3374 * that the smack file system doesn't do
3375 * extended attributes.
3377 * Casey says pipes are easy (?)
3379 * Socket access is controlled by the socket
3380 * structures associated with the task involved.
3382 * Cgroupfs is special
3384 final
= &smack_known_star
;
3386 case DEVPTS_SUPER_MAGIC
:
3388 * devpts seems content with the label of the task.
3389 * Programs that change smack have to treat the
3394 case PROC_SUPER_MAGIC
:
3396 * Casey says procfs appears not to care.
3397 * The superblock default suffices.
3402 * Device labels should come from the filesystem,
3403 * but watch out, because they're volitile,
3404 * getting recreated on every reboot.
3406 final
= &smack_known_star
;
3410 * If a smack value has been set we want to use it,
3411 * but since tmpfs isn't giving us the opportunity
3412 * to set mount options simulate setting the
3413 * superblock default.
3417 * This isn't an understood special case.
3418 * Get the value from the xattr.
3422 * UNIX domain sockets use lower level socket data.
3424 if (S_ISSOCK(inode
->i_mode
)) {
3425 final
= &smack_known_star
;
3429 * No xattr support means, alas, no SMACK label.
3430 * Use the aforeapplied default.
3431 * It would be curious if the label of the task
3432 * does not match that assigned.
3434 if (inode
->i_op
->getxattr
== NULL
)
3437 * Get the dentry for xattr.
3439 dp
= dget(opt_dentry
);
3440 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3441 if (!IS_ERR_OR_NULL(skp
))
3445 * Transmuting directory
3447 if (S_ISDIR(inode
->i_mode
)) {
3449 * If this is a new directory and the label was
3450 * transmuted when the inode was initialized
3451 * set the transmute attribute on the directory
3452 * and mark the inode.
3454 * If there is a transmute attribute on the
3455 * directory mark the inode.
3457 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3458 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3459 rc
= inode
->i_op
->setxattr(dp
,
3460 XATTR_NAME_SMACKTRANSMUTE
,
3461 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3464 rc
= inode
->i_op
->getxattr(dp
,
3465 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3467 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3468 TRANS_TRUE_SIZE
) != 0)
3472 transflag
= SMK_INODE_TRANSMUTE
;
3475 * Don't let the exec or mmap label be "*" or "@".
3477 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3478 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3479 skp
== &smack_known_web
)
3481 isp
->smk_task
= skp
;
3483 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3484 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3485 skp
== &smack_known_web
)
3487 isp
->smk_mmap
= skp
;
3494 isp
->smk_inode
= ckp
;
3496 isp
->smk_inode
= final
;
3498 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3501 mutex_unlock(&isp
->smk_lock
);
3506 * smack_getprocattr - Smack process attribute access
3507 * @p: the object task
3508 * @name: the name of the attribute in /proc/.../attr
3509 * @value: where to put the result
3511 * Places a copy of the task Smack into value
3513 * Returns the length of the smack label or an error code
3515 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3517 struct smack_known
*skp
= smk_of_task_struct(p
);
3521 if (strcmp(name
, "current") != 0)
3524 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3534 * smack_setprocattr - Smack process attribute setting
3535 * @p: the object task
3536 * @name: the name of the attribute in /proc/.../attr
3537 * @value: the value to set
3538 * @size: the size of the value
3540 * Sets the Smack value of the task. Only setting self
3541 * is permitted and only with privilege
3543 * Returns the length of the smack label or an error code
3545 static int smack_setprocattr(struct task_struct
*p
, char *name
,
3546 void *value
, size_t size
)
3548 struct task_smack
*tsp
;
3550 struct smack_known
*skp
;
3553 * Changing another process' Smack value is too dangerous
3554 * and supports no sane use case.
3559 if (!smack_privileged(CAP_MAC_ADMIN
))
3562 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3565 if (strcmp(name
, "current") != 0)
3568 skp
= smk_import_entry(value
, size
);
3570 return PTR_ERR(skp
);
3573 * No process is ever allowed the web ("@") label.
3575 if (skp
== &smack_known_web
)
3578 new = prepare_creds();
3582 tsp
= new->security
;
3583 tsp
->smk_task
= skp
;
3590 * smack_unix_stream_connect - Smack access on UDS
3592 * @other: the other sock
3595 * Return 0 if a subject with the smack of sock could access
3596 * an object with the smack of other, otherwise an error code
3598 static int smack_unix_stream_connect(struct sock
*sock
,
3599 struct sock
*other
, struct sock
*newsk
)
3601 struct smack_known
*skp
;
3602 struct smack_known
*okp
;
3603 struct socket_smack
*ssp
= sock
->sk_security
;
3604 struct socket_smack
*osp
= other
->sk_security
;
3605 struct socket_smack
*nsp
= newsk
->sk_security
;
3606 struct smk_audit_info ad
;
3609 struct lsm_network_audit net
;
3612 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3616 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3617 smk_ad_setfield_u_net_sk(&ad
, other
);
3619 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3620 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3624 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3625 rc
= smk_bu_note("UDS connect", okp
, skp
,
3631 * Cross reference the peer labels for SO_PEERSEC.
3634 nsp
->smk_packet
= ssp
->smk_out
;
3635 ssp
->smk_packet
= osp
->smk_out
;
3642 * smack_unix_may_send - Smack access on UDS
3644 * @other: the other socket
3646 * Return 0 if a subject with the smack of sock could access
3647 * an object with the smack of other, otherwise an error code
3649 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3651 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3652 struct socket_smack
*osp
= other
->sk
->sk_security
;
3653 struct smk_audit_info ad
;
3657 struct lsm_network_audit net
;
3659 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3660 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3663 if (smack_privileged(CAP_MAC_OVERRIDE
))
3666 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3667 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3672 * smack_socket_sendmsg - Smack check based on destination host
3675 * @size: the size of the message
3677 * Return 0 if the current subject can write to the destination host.
3678 * For IPv4 this is only a question if the destination is a single label host.
3679 * For IPv6 this is a check against the label of the port.
3681 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3684 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3685 #if IS_ENABLED(CONFIG_IPV6)
3686 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3688 #ifdef SMACK_IPV6_SECMARK_LABELING
3689 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3690 struct smack_known
*rsp
;
3695 * Perfectly reasonable for this to be NULL
3700 switch (sip
->sin_family
) {
3702 rc
= smack_netlabel_send(sock
->sk
, sip
);
3705 #ifdef SMACK_IPV6_SECMARK_LABELING
3706 rsp
= smack_ipv6host_label(sap
);
3708 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3711 #ifdef SMACK_IPV6_PORT_LABELING
3712 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3720 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3721 * @sap: netlabel secattr
3722 * @ssp: socket security information
3724 * Returns a pointer to a Smack label entry found on the label list.
3726 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3727 struct socket_smack
*ssp
)
3729 struct smack_known
*skp
;
3734 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3736 * Looks like a CIPSO packet.
3737 * If there are flags but no level netlabel isn't
3738 * behaving the way we expect it to.
3740 * Look it up in the label table
3741 * Without guidance regarding the smack value
3742 * for the packet fall back on the network
3746 list_for_each_entry(skp
, &smack_known_list
, list
) {
3747 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3750 * Compare the catsets. Use the netlbl APIs.
3752 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3753 if ((skp
->smk_netlabel
.flags
&
3754 NETLBL_SECATTR_MLS_CAT
) == 0)
3758 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3759 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3761 kcat
= netlbl_catmap_walk(
3762 skp
->smk_netlabel
.attr
.mls
.cat
,
3764 if (acat
< 0 || kcat
< 0)
3777 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3778 return &smack_known_web
;
3779 return &smack_known_star
;
3781 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
3783 * Looks like a fallback, which gives us a secid.
3785 skp
= smack_from_secid(sap
->attr
.secid
);
3787 * This has got to be a bug because it is
3788 * impossible to specify a fallback without
3789 * specifying the label, which will ensure
3790 * it has a secid, and the only way to get a
3791 * secid is from a fallback.
3793 BUG_ON(skp
== NULL
);
3797 * Without guidance regarding the smack value
3798 * for the packet fall back on the network
3801 return smack_net_ambient
;
3804 #if IS_ENABLED(CONFIG_IPV6)
3805 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3809 int proto
= -EINVAL
;
3810 struct ipv6hdr _ipv6h
;
3811 struct ipv6hdr
*ip6
;
3813 struct tcphdr _tcph
, *th
;
3814 struct udphdr _udph
, *uh
;
3815 struct dccp_hdr _dccph
, *dh
;
3819 offset
= skb_network_offset(skb
);
3820 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3823 sip
->sin6_addr
= ip6
->saddr
;
3825 nexthdr
= ip6
->nexthdr
;
3826 offset
+= sizeof(_ipv6h
);
3827 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3834 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3836 sip
->sin6_port
= th
->source
;
3839 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3841 sip
->sin6_port
= uh
->source
;
3844 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3846 sip
->sin6_port
= dh
->dccph_sport
;
3851 #endif /* CONFIG_IPV6 */
3854 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3858 * Returns 0 if the packet should be delivered, an error code otherwise
3860 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3862 struct netlbl_lsm_secattr secattr
;
3863 struct socket_smack
*ssp
= sk
->sk_security
;
3864 struct smack_known
*skp
= NULL
;
3866 struct smk_audit_info ad
;
3868 struct lsm_network_audit net
;
3870 #if IS_ENABLED(CONFIG_IPV6)
3871 struct sockaddr_in6 sadd
;
3873 #endif /* CONFIG_IPV6 */
3875 switch (sk
->sk_family
) {
3877 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3879 * If there is a secmark use it rather than the CIPSO label.
3880 * If there is no secmark fall back to CIPSO.
3881 * The secmark is assumed to reflect policy better.
3883 if (skb
&& skb
->secmark
!= 0) {
3884 skp
= smack_from_secid(skb
->secmark
);
3887 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3889 * Translate what netlabel gave us.
3891 netlbl_secattr_init(&secattr
);
3893 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
3895 skp
= smack_from_secattr(&secattr
, ssp
);
3897 skp
= smack_net_ambient
;
3899 netlbl_secattr_destroy(&secattr
);
3901 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3905 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3906 ad
.a
.u
.net
->family
= sk
->sk_family
;
3907 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3908 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3911 * Receiving a packet requires that the other end
3912 * be able to write here. Read access is not required.
3913 * This is the simplist possible security model
3916 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3917 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3920 netlbl_skbuff_err(skb
, rc
, 0);
3922 #if IS_ENABLED(CONFIG_IPV6)
3924 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3925 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_TCP
)
3927 #ifdef SMACK_IPV6_SECMARK_LABELING
3928 if (skb
&& skb
->secmark
!= 0)
3929 skp
= smack_from_secid(skb
->secmark
);
3931 skp
= smack_ipv6host_label(&sadd
);
3933 skp
= smack_net_ambient
;
3935 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3936 ad
.a
.u
.net
->family
= sk
->sk_family
;
3937 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3938 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3939 #endif /* CONFIG_AUDIT */
3940 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3941 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
3943 #endif /* SMACK_IPV6_SECMARK_LABELING */
3944 #ifdef SMACK_IPV6_PORT_LABELING
3945 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3946 #endif /* SMACK_IPV6_PORT_LABELING */
3948 #endif /* CONFIG_IPV6 */
3955 * smack_socket_getpeersec_stream - pull in packet label
3957 * @optval: user's destination
3958 * @optlen: size thereof
3961 * returns zero on success, an error code otherwise
3963 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3964 char __user
*optval
,
3965 int __user
*optlen
, unsigned len
)
3967 struct socket_smack
*ssp
;
3972 ssp
= sock
->sk
->sk_security
;
3973 if (ssp
->smk_packet
!= NULL
) {
3974 rcp
= ssp
->smk_packet
->smk_known
;
3975 slen
= strlen(rcp
) + 1;
3980 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3983 if (put_user(slen
, optlen
) != 0)
3991 * smack_socket_getpeersec_dgram - pull in packet label
3992 * @sock: the peer socket
3994 * @secid: pointer to where to put the secid of the packet
3996 * Sets the netlabel socket state on sk from parent
3998 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3999 struct sk_buff
*skb
, u32
*secid
)
4002 struct netlbl_lsm_secattr secattr
;
4003 struct socket_smack
*ssp
= NULL
;
4004 struct smack_known
*skp
;
4005 int family
= PF_UNSPEC
;
4006 u32 s
= 0; /* 0 is the invalid secid */
4010 if (skb
->protocol
== htons(ETH_P_IP
))
4012 #if IS_ENABLED(CONFIG_IPV6)
4013 else if (skb
->protocol
== htons(ETH_P_IPV6
))
4015 #endif /* CONFIG_IPV6 */
4017 if (family
== PF_UNSPEC
&& sock
!= NULL
)
4018 family
= sock
->sk
->sk_family
;
4022 ssp
= sock
->sk
->sk_security
;
4023 s
= ssp
->smk_out
->smk_secid
;
4026 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4032 * Translate what netlabel gave us.
4034 if (sock
!= NULL
&& sock
->sk
!= NULL
)
4035 ssp
= sock
->sk
->sk_security
;
4036 netlbl_secattr_init(&secattr
);
4037 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4039 skp
= smack_from_secattr(&secattr
, ssp
);
4042 netlbl_secattr_destroy(&secattr
);
4045 #ifdef SMACK_IPV6_SECMARK_LABELING
4057 * smack_sock_graft - Initialize a newly created socket with an existing sock
4059 * @parent: parent socket
4061 * Set the smk_{in,out} state of an existing sock based on the process that
4062 * is creating the new socket.
4064 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
4066 struct socket_smack
*ssp
;
4067 struct smack_known
*skp
= smk_of_current();
4070 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
4073 ssp
= sk
->sk_security
;
4076 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4080 * smack_inet_conn_request - Smack access check on connect
4081 * @sk: socket involved
4085 * Returns 0 if a task with the packet label could write to
4086 * the socket, otherwise an error code
4088 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4089 struct request_sock
*req
)
4091 u16 family
= sk
->sk_family
;
4092 struct smack_known
*skp
;
4093 struct socket_smack
*ssp
= sk
->sk_security
;
4094 struct netlbl_lsm_secattr secattr
;
4095 struct sockaddr_in addr
;
4097 struct smack_known
*hskp
;
4099 struct smk_audit_info ad
;
4101 struct lsm_network_audit net
;
4104 #if IS_ENABLED(CONFIG_IPV6)
4105 if (family
== PF_INET6
) {
4107 * Handle mapped IPv4 packets arriving
4108 * via IPv6 sockets. Don't set up netlabel
4109 * processing on IPv6.
4111 if (skb
->protocol
== htons(ETH_P_IP
))
4116 #endif /* CONFIG_IPV6 */
4118 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4120 * If there is a secmark use it rather than the CIPSO label.
4121 * If there is no secmark fall back to CIPSO.
4122 * The secmark is assumed to reflect policy better.
4124 if (skb
&& skb
->secmark
!= 0) {
4125 skp
= smack_from_secid(skb
->secmark
);
4128 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4130 netlbl_secattr_init(&secattr
);
4131 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4133 skp
= smack_from_secattr(&secattr
, ssp
);
4135 skp
= &smack_known_huh
;
4136 netlbl_secattr_destroy(&secattr
);
4138 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4143 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4144 ad
.a
.u
.net
->family
= family
;
4145 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4146 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4149 * Receiving a packet requires that the other end be able to write
4150 * here. Read access is not required.
4152 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4153 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4158 * Save the peer's label in the request_sock so we can later setup
4159 * smk_packet in the child socket so that SO_PEERCRED can report it.
4161 req
->peer_secid
= skp
->smk_secid
;
4164 * We need to decide if we want to label the incoming connection here
4165 * if we do we only need to label the request_sock and the stack will
4166 * propagate the wire-label to the sock when it is created.
4169 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4171 hskp
= smack_ipv4host_label(&addr
);
4175 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4177 netlbl_req_delattr(req
);
4183 * smack_inet_csk_clone - Copy the connection information to the new socket
4184 * @sk: the new socket
4185 * @req: the connection's request_sock
4187 * Transfer the connection's peer label to the newly created socket.
4189 static void smack_inet_csk_clone(struct sock
*sk
,
4190 const struct request_sock
*req
)
4192 struct socket_smack
*ssp
= sk
->sk_security
;
4193 struct smack_known
*skp
;
4195 if (req
->peer_secid
!= 0) {
4196 skp
= smack_from_secid(req
->peer_secid
);
4197 ssp
->smk_packet
= skp
;
4199 ssp
->smk_packet
= NULL
;
4203 * Key management security hooks
4205 * Casey has not tested key support very heavily.
4206 * The permission check is most likely too restrictive.
4207 * If you care about keys please have a look.
4212 * smack_key_alloc - Set the key security blob
4214 * @cred: the credentials to use
4217 * No allocation required
4221 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4222 unsigned long flags
)
4224 struct smack_known
*skp
= smk_of_task(cred
->security
);
4226 key
->security
= skp
;
4231 * smack_key_free - Clear the key security blob
4234 * Clear the blob pointer
4236 static void smack_key_free(struct key
*key
)
4238 key
->security
= NULL
;
4242 * smack_key_permission - Smack access on a key
4243 * @key_ref: gets to the object
4244 * @cred: the credentials to use
4245 * @perm: requested key permissions
4247 * Return 0 if the task has read and write to the object,
4248 * an error code otherwise
4250 static int smack_key_permission(key_ref_t key_ref
,
4251 const struct cred
*cred
, unsigned perm
)
4254 struct smk_audit_info ad
;
4255 struct smack_known
*tkp
= smk_of_task(cred
->security
);
4259 keyp
= key_ref_to_ptr(key_ref
);
4263 * If the key hasn't been initialized give it access so that
4266 if (keyp
->security
== NULL
)
4269 * This should not occur
4274 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4275 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4276 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4278 if (perm
& KEY_NEED_READ
)
4280 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4281 request
= MAY_WRITE
;
4282 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
4283 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
4288 * smack_key_getsecurity - Smack label tagging the key
4289 * @key points to the key to be queried
4290 * @_buffer points to a pointer that should be set to point to the
4291 * resulting string (if no label or an error occurs).
4292 * Return the length of the string (including terminating NUL) or -ve if
4294 * May also return 0 (and a NULL buffer pointer) if there is no label.
4296 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4298 struct smack_known
*skp
= key
->security
;
4302 if (key
->security
== NULL
) {
4307 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4310 length
= strlen(copy
) + 1;
4316 #endif /* CONFIG_KEYS */
4321 * Audit requires a unique representation of each Smack specific
4322 * rule. This unique representation is used to distinguish the
4323 * object to be audited from remaining kernel objects and also
4324 * works as a glue between the audit hooks.
4326 * Since repository entries are added but never deleted, we'll use
4327 * the smack_known label address related to the given audit rule as
4328 * the needed unique representation. This also better fits the smack
4329 * model where nearly everything is a label.
4334 * smack_audit_rule_init - Initialize a smack audit rule
4335 * @field: audit rule fields given from user-space (audit.h)
4336 * @op: required testing operator (=, !=, >, <, ...)
4337 * @rulestr: smack label to be audited
4338 * @vrule: pointer to save our own audit rule representation
4340 * Prepare to audit cases where (@field @op @rulestr) is true.
4341 * The label to be audited is created if necessay.
4343 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4345 struct smack_known
*skp
;
4346 char **rule
= (char **)vrule
;
4349 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4352 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4355 skp
= smk_import_entry(rulestr
, 0);
4357 return PTR_ERR(skp
);
4359 *rule
= skp
->smk_known
;
4365 * smack_audit_rule_known - Distinguish Smack audit rules
4366 * @krule: rule of interest, in Audit kernel representation format
4368 * This is used to filter Smack rules from remaining Audit ones.
4369 * If it's proved that this rule belongs to us, the
4370 * audit_rule_match hook will be called to do the final judgement.
4372 static int smack_audit_rule_known(struct audit_krule
*krule
)
4374 struct audit_field
*f
;
4377 for (i
= 0; i
< krule
->field_count
; i
++) {
4378 f
= &krule
->fields
[i
];
4380 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4388 * smack_audit_rule_match - Audit given object ?
4389 * @secid: security id for identifying the object to test
4390 * @field: audit rule flags given from user-space
4391 * @op: required testing operator
4392 * @vrule: smack internal rule presentation
4393 * @actx: audit context associated with the check
4395 * The core Audit hook. It's used to take the decision of
4396 * whether to audit or not to audit a given object.
4398 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4399 struct audit_context
*actx
)
4401 struct smack_known
*skp
;
4404 if (unlikely(!rule
)) {
4405 WARN_ONCE(1, "Smack: missing rule\n");
4409 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4412 skp
= smack_from_secid(secid
);
4415 * No need to do string comparisons. If a match occurs,
4416 * both pointers will point to the same smack_known
4419 if (op
== Audit_equal
)
4420 return (rule
== skp
->smk_known
);
4421 if (op
== Audit_not_equal
)
4422 return (rule
!= skp
->smk_known
);
4428 * smack_audit_rule_free - free smack rule representation
4429 * @vrule: rule to be freed.
4431 * No memory was allocated.
4433 static void smack_audit_rule_free(void *vrule
)
4438 #endif /* CONFIG_AUDIT */
4441 * smack_ismaclabel - check if xattr @name references a smack MAC label
4442 * @name: Full xattr name to check.
4444 static int smack_ismaclabel(const char *name
)
4446 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4451 * smack_secid_to_secctx - return the smack label for a secid
4452 * @secid: incoming integer
4453 * @secdata: destination
4454 * @seclen: how long it is
4456 * Exists for networking code.
4458 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4460 struct smack_known
*skp
= smack_from_secid(secid
);
4463 *secdata
= skp
->smk_known
;
4464 *seclen
= strlen(skp
->smk_known
);
4469 * smack_secctx_to_secid - return the secid for a smack label
4470 * @secdata: smack label
4471 * @seclen: how long result is
4472 * @secid: outgoing integer
4474 * Exists for audit and networking code.
4476 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4478 struct smack_known
*skp
= smk_find_entry(secdata
);
4481 *secid
= skp
->smk_secid
;
4488 * smack_release_secctx - don't do anything.
4492 * Exists to make sure nothing gets done, and properly
4494 static void smack_release_secctx(char *secdata
, u32 seclen
)
4498 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4500 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4503 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4505 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4508 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4511 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
4519 static struct security_hook_list smack_hooks
[] = {
4520 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4521 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4522 LSM_HOOK_INIT(syslog
, smack_syslog
),
4524 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4525 LSM_HOOK_INIT(sb_free_security
, smack_sb_free_security
),
4526 LSM_HOOK_INIT(sb_copy_data
, smack_sb_copy_data
),
4527 LSM_HOOK_INIT(sb_kern_mount
, smack_sb_kern_mount
),
4528 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4529 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4530 LSM_HOOK_INIT(sb_parse_opts_str
, smack_parse_opts_str
),
4532 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4533 LSM_HOOK_INIT(bprm_committing_creds
, smack_bprm_committing_creds
),
4534 LSM_HOOK_INIT(bprm_secureexec
, smack_bprm_secureexec
),
4536 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4537 LSM_HOOK_INIT(inode_free_security
, smack_inode_free_security
),
4538 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4539 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4540 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4541 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4542 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4543 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4544 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4545 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4546 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4547 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4548 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4549 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4550 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4551 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4552 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4553 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4555 LSM_HOOK_INIT(file_permission
, smack_file_permission
),
4556 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4557 LSM_HOOK_INIT(file_free_security
, smack_file_free_security
),
4558 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4559 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4560 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4561 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4562 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4563 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4564 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4565 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4567 LSM_HOOK_INIT(file_open
, smack_file_open
),
4569 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4570 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4571 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4572 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4573 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4574 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4575 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4576 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4577 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4578 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4579 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4580 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4581 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4582 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4583 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4584 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4585 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4586 LSM_HOOK_INIT(task_wait
, smack_task_wait
),
4587 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4589 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4590 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4592 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4593 LSM_HOOK_INIT(msg_msg_free_security
, smack_msg_msg_free_security
),
4595 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_msg_queue_alloc_security
),
4596 LSM_HOOK_INIT(msg_queue_free_security
, smack_msg_queue_free_security
),
4597 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4598 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4599 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4600 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4602 LSM_HOOK_INIT(shm_alloc_security
, smack_shm_alloc_security
),
4603 LSM_HOOK_INIT(shm_free_security
, smack_shm_free_security
),
4604 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4605 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4606 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4608 LSM_HOOK_INIT(sem_alloc_security
, smack_sem_alloc_security
),
4609 LSM_HOOK_INIT(sem_free_security
, smack_sem_free_security
),
4610 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4611 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4612 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4614 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4616 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4617 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4619 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4620 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4622 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4623 #ifdef SMACK_IPV6_PORT_LABELING
4624 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4626 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4627 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4628 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4629 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4630 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4631 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4632 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4633 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4634 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4635 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4637 /* key management security hooks */
4639 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4640 LSM_HOOK_INIT(key_free
, smack_key_free
),
4641 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4642 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4643 #endif /* CONFIG_KEYS */
4647 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4648 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4649 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4650 LSM_HOOK_INIT(audit_rule_free
, smack_audit_rule_free
),
4651 #endif /* CONFIG_AUDIT */
4653 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4654 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4655 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4656 LSM_HOOK_INIT(release_secctx
, smack_release_secctx
),
4657 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4658 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4659 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4663 static __init
void init_smack_known_list(void)
4666 * Initialize rule list locks
4668 mutex_init(&smack_known_huh
.smk_rules_lock
);
4669 mutex_init(&smack_known_hat
.smk_rules_lock
);
4670 mutex_init(&smack_known_floor
.smk_rules_lock
);
4671 mutex_init(&smack_known_star
.smk_rules_lock
);
4672 mutex_init(&smack_known_invalid
.smk_rules_lock
);
4673 mutex_init(&smack_known_web
.smk_rules_lock
);
4675 * Initialize rule lists
4677 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4678 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4679 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4680 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4681 INIT_LIST_HEAD(&smack_known_invalid
.smk_rules
);
4682 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4684 * Create the known labels list
4686 smk_insert_entry(&smack_known_huh
);
4687 smk_insert_entry(&smack_known_hat
);
4688 smk_insert_entry(&smack_known_star
);
4689 smk_insert_entry(&smack_known_floor
);
4690 smk_insert_entry(&smack_known_invalid
);
4691 smk_insert_entry(&smack_known_web
);
4695 * smack_init - initialize the smack system
4699 static __init
int smack_init(void)
4702 struct task_smack
*tsp
;
4704 if (!security_module_enable("smack"))
4709 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4710 if (!smack_inode_cache
)
4713 tsp
= new_task_smack(&smack_known_floor
, &smack_known_floor
,
4716 kmem_cache_destroy(smack_inode_cache
);
4720 pr_info("Smack: Initializing.\n");
4721 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4722 pr_info("Smack: Netfilter enabled.\n");
4724 #ifdef SMACK_IPV6_PORT_LABELING
4725 pr_info("Smack: IPv6 port labeling enabled.\n");
4727 #ifdef SMACK_IPV6_SECMARK_LABELING
4728 pr_info("Smack: IPv6 Netfilter enabled.\n");
4732 * Set the security state for the initial task.
4734 cred
= (struct cred
*) current
->cred
;
4735 cred
->security
= tsp
;
4737 /* initialize the smack_known_list */
4738 init_smack_known_list();
4743 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
));
4749 * Smack requires early initialization in order to label
4750 * all processes and objects when they are created.
4752 security_initcall(smack_init
);