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/icmpv6.h>
32 #include <linux/slab.h>
33 #include <linux/mutex.h>
34 #include <linux/pipe_fs_i.h>
35 #include <net/cipso_ipv4.h>
38 #include <linux/audit.h>
39 #include <linux/magic.h>
40 #include <linux/dcache.h>
41 #include <linux/personality.h>
42 #include <linux/msg.h>
43 #include <linux/shm.h>
44 #include <linux/binfmts.h>
45 #include <linux/parser.h>
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 #ifdef SMACK_IPV6_PORT_LABELING
56 DEFINE_MUTEX(smack_ipv6_lock
);
57 static LIST_HEAD(smk_ipv6_port_list
);
59 static struct kmem_cache
*smack_inode_cache
;
62 static const match_table_t smk_mount_tokens
= {
63 {Opt_fsdefault
, SMK_FSDEFAULT
"%s"},
64 {Opt_fsfloor
, SMK_FSFLOOR
"%s"},
65 {Opt_fshat
, SMK_FSHAT
"%s"},
66 {Opt_fsroot
, SMK_FSROOT
"%s"},
67 {Opt_fstransmute
, SMK_FSTRANS
"%s"},
71 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
72 static char *smk_bu_mess
[] = {
73 "Bringup Error", /* Unused */
74 "Bringup", /* SMACK_BRINGUP_ALLOW */
75 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
76 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
79 static void smk_bu_mode(int mode
, char *s
)
89 if (mode
& MAY_APPEND
)
91 if (mode
& MAY_TRANSMUTE
)
101 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
102 static int smk_bu_note(char *note
, struct smack_known
*sskp
,
103 struct smack_known
*oskp
, int mode
, int rc
)
105 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
109 if (rc
> SMACK_UNCONFINED_OBJECT
)
112 smk_bu_mode(mode
, acc
);
113 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess
[rc
],
114 sskp
->smk_known
, oskp
->smk_known
, acc
, note
);
118 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
121 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
122 static int smk_bu_current(char *note
, struct smack_known
*oskp
,
125 struct task_smack
*tsp
= current_security();
126 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
130 if (rc
> SMACK_UNCONFINED_OBJECT
)
133 smk_bu_mode(mode
, acc
);
134 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess
[rc
],
135 tsp
->smk_task
->smk_known
, oskp
->smk_known
,
136 acc
, current
->comm
, note
);
140 #define smk_bu_current(note, oskp, mode, RC) (RC)
143 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
144 static int smk_bu_task(struct task_struct
*otp
, int mode
, int rc
)
146 struct task_smack
*tsp
= current_security();
147 struct smack_known
*smk_task
= smk_of_task_struct(otp
);
148 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
152 if (rc
> SMACK_UNCONFINED_OBJECT
)
155 smk_bu_mode(mode
, acc
);
156 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess
[rc
],
157 tsp
->smk_task
->smk_known
, smk_task
->smk_known
, acc
,
158 current
->comm
, otp
->comm
);
162 #define smk_bu_task(otp, mode, RC) (RC)
165 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
166 static int smk_bu_inode(struct inode
*inode
, int mode
, int rc
)
168 struct task_smack
*tsp
= current_security();
169 struct inode_smack
*isp
= inode
->i_security
;
170 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
172 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
173 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
174 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
178 if (rc
> SMACK_UNCONFINED_OBJECT
)
180 if (rc
== SMACK_UNCONFINED_SUBJECT
&&
181 (mode
& (MAY_WRITE
| MAY_APPEND
)))
182 isp
->smk_flags
|= SMK_INODE_IMPURE
;
184 smk_bu_mode(mode
, acc
);
186 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess
[rc
],
187 tsp
->smk_task
->smk_known
, isp
->smk_inode
->smk_known
, acc
,
188 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
192 #define smk_bu_inode(inode, mode, RC) (RC)
195 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
196 static int smk_bu_file(struct file
*file
, int mode
, int rc
)
198 struct task_smack
*tsp
= current_security();
199 struct smack_known
*sskp
= tsp
->smk_task
;
200 struct inode
*inode
= file_inode(file
);
201 struct inode_smack
*isp
= inode
->i_security
;
202 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
204 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
205 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
206 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
210 if (rc
> SMACK_UNCONFINED_OBJECT
)
213 smk_bu_mode(mode
, acc
);
214 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
215 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
216 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
221 #define smk_bu_file(file, mode, RC) (RC)
224 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
225 static int smk_bu_credfile(const struct cred
*cred
, struct file
*file
,
228 struct task_smack
*tsp
= cred
->security
;
229 struct smack_known
*sskp
= tsp
->smk_task
;
230 struct inode
*inode
= file_inode(file
);
231 struct inode_smack
*isp
= inode
->i_security
;
232 char acc
[SMK_NUM_ACCESS_TYPE
+ 1];
234 if (isp
->smk_flags
& SMK_INODE_IMPURE
)
235 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
236 inode
->i_sb
->s_id
, inode
->i_ino
, current
->comm
);
240 if (rc
> SMACK_UNCONFINED_OBJECT
)
243 smk_bu_mode(mode
, acc
);
244 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess
[rc
],
245 sskp
->smk_known
, smk_of_inode(inode
)->smk_known
, acc
,
246 inode
->i_sb
->s_id
, inode
->i_ino
, file
,
251 #define smk_bu_credfile(cred, file, mode, RC) (RC)
255 * smk_fetch - Fetch the smack label from a file.
256 * @name: type of the label (attribute)
257 * @ip: a pointer to the inode
258 * @dp: a pointer to the dentry
260 * Returns a pointer to the master list entry for the Smack label,
261 * NULL if there was no label to fetch, or an error code.
263 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
268 struct smack_known
*skp
= NULL
;
270 if (!(ip
->i_opflags
& IOP_XATTR
))
271 return ERR_PTR(-EOPNOTSUPP
);
273 buffer
= kzalloc(SMK_LONGLABEL
, GFP_NOFS
);
275 return ERR_PTR(-ENOMEM
);
277 rc
= __vfs_getxattr(dp
, ip
, name
, buffer
, SMK_LONGLABEL
);
283 skp
= smk_import_entry(buffer
, rc
);
291 * new_inode_smack - allocate an inode security blob
292 * @skp: a pointer to the Smack label entry to use in the blob
294 * Returns the new blob or NULL if there's no memory available
296 static struct inode_smack
*new_inode_smack(struct smack_known
*skp
)
298 struct inode_smack
*isp
;
300 isp
= kmem_cache_zalloc(smack_inode_cache
, GFP_NOFS
);
304 isp
->smk_inode
= skp
;
306 mutex_init(&isp
->smk_lock
);
312 * new_task_smack - allocate a task security blob
313 * @task: a pointer to the Smack label for the running task
314 * @forked: a pointer to the Smack label for the forked task
315 * @gfp: type of the memory for the allocation
317 * Returns the new blob or NULL if there's no memory available
319 static struct task_smack
*new_task_smack(struct smack_known
*task
,
320 struct smack_known
*forked
, gfp_t gfp
)
322 struct task_smack
*tsp
;
324 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
328 tsp
->smk_task
= task
;
329 tsp
->smk_forked
= forked
;
330 INIT_LIST_HEAD(&tsp
->smk_rules
);
331 INIT_LIST_HEAD(&tsp
->smk_relabel
);
332 mutex_init(&tsp
->smk_rules_lock
);
338 * smk_copy_rules - copy a rule set
339 * @nhead: new rules header pointer
340 * @ohead: old rules header pointer
341 * @gfp: type of the memory for the allocation
343 * Returns 0 on success, -ENOMEM on error
345 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
348 struct smack_rule
*nrp
;
349 struct smack_rule
*orp
;
352 list_for_each_entry_rcu(orp
, ohead
, list
) {
353 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
359 list_add_rcu(&nrp
->list
, nhead
);
365 * smk_copy_relabel - copy smk_relabel labels list
366 * @nhead: new rules header pointer
367 * @ohead: old rules header pointer
368 * @gfp: type of the memory for the allocation
370 * Returns 0 on success, -ENOMEM on error
372 static int smk_copy_relabel(struct list_head
*nhead
, struct list_head
*ohead
,
375 struct smack_known_list_elem
*nklep
;
376 struct smack_known_list_elem
*oklep
;
378 list_for_each_entry(oklep
, ohead
, list
) {
379 nklep
= kzalloc(sizeof(struct smack_known_list_elem
), gfp
);
381 smk_destroy_label_list(nhead
);
384 nklep
->smk_label
= oklep
->smk_label
;
385 list_add(&nklep
->list
, nhead
);
392 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
393 * @mode - input mode in form of PTRACE_MODE_*
395 * Returns a converted MAY_* mode usable by smack rules
397 static inline unsigned int smk_ptrace_mode(unsigned int mode
)
399 if (mode
& PTRACE_MODE_ATTACH
)
400 return MAY_READWRITE
;
401 if (mode
& PTRACE_MODE_READ
)
408 * smk_ptrace_rule_check - helper for ptrace access
409 * @tracer: tracer process
410 * @tracee_known: label entry of the process that's about to be traced
411 * @mode: ptrace attachment mode (PTRACE_MODE_*)
412 * @func: name of the function that called us, used for audit
414 * Returns 0 on access granted, -error on error
416 static int smk_ptrace_rule_check(struct task_struct
*tracer
,
417 struct smack_known
*tracee_known
,
418 unsigned int mode
, const char *func
)
421 struct smk_audit_info ad
, *saip
= NULL
;
422 struct task_smack
*tsp
;
423 struct smack_known
*tracer_known
;
424 const struct cred
*tracercred
;
426 if ((mode
& PTRACE_MODE_NOAUDIT
) == 0) {
427 smk_ad_init(&ad
, func
, LSM_AUDIT_DATA_TASK
);
428 smk_ad_setfield_u_tsk(&ad
, tracer
);
433 tracercred
= __task_cred(tracer
);
434 tsp
= tracercred
->security
;
435 tracer_known
= smk_of_task(tsp
);
437 if ((mode
& PTRACE_MODE_ATTACH
) &&
438 (smack_ptrace_rule
== SMACK_PTRACE_EXACT
||
439 smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)) {
440 if (tracer_known
->smk_known
== tracee_known
->smk_known
)
442 else if (smack_ptrace_rule
== SMACK_PTRACE_DRACONIAN
)
444 else if (smack_privileged_cred(CAP_SYS_PTRACE
, tracercred
))
450 smack_log(tracer_known
->smk_known
,
451 tracee_known
->smk_known
,
458 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
459 rc
= smk_tskacc(tsp
, tracee_known
, smk_ptrace_mode(mode
), saip
);
467 * We he, that is fun!
471 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
472 * @ctp: child task pointer
473 * @mode: ptrace attachment mode (PTRACE_MODE_*)
475 * Returns 0 if access is OK, an error code otherwise
477 * Do the capability checks.
479 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
481 struct smack_known
*skp
;
483 skp
= smk_of_task_struct(ctp
);
485 return smk_ptrace_rule_check(current
, skp
, mode
, __func__
);
489 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
490 * @ptp: parent task pointer
492 * Returns 0 if access is OK, an error code otherwise
494 * Do the capability checks, and require PTRACE_MODE_ATTACH.
496 static int smack_ptrace_traceme(struct task_struct
*ptp
)
499 struct smack_known
*skp
;
501 skp
= smk_of_task(current_security());
503 rc
= smk_ptrace_rule_check(ptp
, skp
, PTRACE_MODE_ATTACH
, __func__
);
508 * smack_syslog - Smack approval on syslog
509 * @type: message type
511 * Returns 0 on success, error code otherwise.
513 static int smack_syslog(int typefrom_file
)
516 struct smack_known
*skp
= smk_of_current();
518 if (smack_privileged(CAP_MAC_OVERRIDE
))
521 if (smack_syslog_label
!= NULL
&& smack_syslog_label
!= skp
)
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
536 * Returns 0 on success or -ENOMEM on error.
538 static int smack_sb_alloc_security(struct super_block
*sb
)
540 struct superblock_smack
*sbsp
;
542 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
547 sbsp
->smk_root
= &smack_known_floor
;
548 sbsp
->smk_default
= &smack_known_floor
;
549 sbsp
->smk_floor
= &smack_known_floor
;
550 sbsp
->smk_hat
= &smack_known_hat
;
552 * SMK_SB_INITIALIZED will be zero from kzalloc.
554 sb
->s_security
= sbsp
;
560 * smack_sb_free_security - free a superblock blob
561 * @sb: the superblock getting the blob
564 static void smack_sb_free_security(struct super_block
*sb
)
566 kfree(sb
->s_security
);
567 sb
->s_security
= NULL
;
571 * smack_sb_copy_data - copy mount options data for processing
572 * @orig: where to start
573 * @smackopts: mount options string
575 * Returns 0 on success or -ENOMEM on error.
577 * Copy the Smack specific mount options out of the mount
580 static int smack_sb_copy_data(char *orig
, char *smackopts
)
582 char *cp
, *commap
, *otheropts
, *dp
;
584 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
585 if (otheropts
== NULL
)
588 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
589 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
591 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
593 else if (strstr(cp
, SMK_FSHAT
) == cp
)
595 else if (strstr(cp
, SMK_FSROOT
) == cp
)
597 else if (strstr(cp
, SMK_FSTRANS
) == cp
)
602 commap
= strchr(cp
, ',');
611 strcpy(orig
, otheropts
);
612 free_page((unsigned long)otheropts
);
618 * smack_parse_opts_str - parse Smack specific mount options
619 * @options: mount options string
620 * @opts: where to store converted mount opts
622 * Returns 0 on success or -ENOMEM on error.
624 * converts Smack specific mount options to generic security option format
626 static int smack_parse_opts_str(char *options
,
627 struct security_mnt_opts
*opts
)
630 char *fsdefault
= NULL
;
631 char *fsfloor
= NULL
;
634 char *fstransmute
= NULL
;
636 int num_mnt_opts
= 0;
639 opts
->num_mnt_opts
= 0;
644 while ((p
= strsep(&options
, ",")) != NULL
) {
645 substring_t args
[MAX_OPT_ARGS
];
650 token
= match_token(p
, smk_mount_tokens
, args
);
656 fsdefault
= match_strdup(&args
[0]);
663 fsfloor
= match_strdup(&args
[0]);
670 fshat
= match_strdup(&args
[0]);
677 fsroot
= match_strdup(&args
[0]);
681 case Opt_fstransmute
:
684 fstransmute
= match_strdup(&args
[0]);
690 pr_warn("Smack: unknown mount option\n");
695 opts
->mnt_opts
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(char *), GFP_KERNEL
);
699 opts
->mnt_opts_flags
= kcalloc(NUM_SMK_MNT_OPTS
, sizeof(int),
701 if (!opts
->mnt_opts_flags
)
705 opts
->mnt_opts
[num_mnt_opts
] = fsdefault
;
706 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSDEFAULT_MNT
;
709 opts
->mnt_opts
[num_mnt_opts
] = fsfloor
;
710 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSFLOOR_MNT
;
713 opts
->mnt_opts
[num_mnt_opts
] = fshat
;
714 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSHAT_MNT
;
717 opts
->mnt_opts
[num_mnt_opts
] = fsroot
;
718 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSROOT_MNT
;
721 opts
->mnt_opts
[num_mnt_opts
] = fstransmute
;
722 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSTRANS_MNT
;
725 opts
->num_mnt_opts
= num_mnt_opts
;
730 pr_warn("Smack: duplicate mount options\n");
742 * smack_set_mnt_opts - set Smack specific mount options
743 * @sb: the file system superblock
744 * @opts: Smack mount options
745 * @kern_flags: mount option from kernel space or user space
746 * @set_kern_flags: where to store converted mount opts
748 * Returns 0 on success, an error code on failure
750 * Allow filesystems with binary mount data to explicitly set Smack mount
753 static int smack_set_mnt_opts(struct super_block
*sb
,
754 struct security_mnt_opts
*opts
,
755 unsigned long kern_flags
,
756 unsigned long *set_kern_flags
)
758 struct dentry
*root
= sb
->s_root
;
759 struct inode
*inode
= d_backing_inode(root
);
760 struct superblock_smack
*sp
= sb
->s_security
;
761 struct inode_smack
*isp
;
762 struct smack_known
*skp
;
764 int num_opts
= opts
->num_mnt_opts
;
767 if (sp
->smk_flags
& SMK_SB_INITIALIZED
)
770 if (!smack_privileged(CAP_MAC_ADMIN
)) {
772 * Unprivileged mounts don't get to specify Smack values.
777 * Unprivileged mounts get root and default from the caller.
779 skp
= smk_of_current();
781 sp
->smk_default
= skp
;
783 * For a handful of fs types with no user-controlled
784 * backing store it's okay to trust security labels
785 * in the filesystem. The rest are untrusted.
787 if (sb
->s_user_ns
!= &init_user_ns
&&
788 sb
->s_magic
!= SYSFS_MAGIC
&& sb
->s_magic
!= TMPFS_MAGIC
&&
789 sb
->s_magic
!= RAMFS_MAGIC
) {
791 sp
->smk_flags
|= SMK_SB_UNTRUSTED
;
795 sp
->smk_flags
|= SMK_SB_INITIALIZED
;
797 for (i
= 0; i
< num_opts
; i
++) {
798 switch (opts
->mnt_opts_flags
[i
]) {
800 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
803 sp
->smk_default
= skp
;
806 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
812 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
818 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
824 skp
= smk_import_entry(opts
->mnt_opts
[i
], 0);
836 * Initialize the root inode.
838 isp
= inode
->i_security
;
840 isp
= new_inode_smack(sp
->smk_root
);
843 inode
->i_security
= isp
;
845 isp
->smk_inode
= sp
->smk_root
;
848 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
854 * smack_sb_kern_mount - Smack specific mount processing
855 * @sb: the file system superblock
856 * @flags: the mount flags
857 * @data: the smack mount options
859 * Returns 0 on success, an error code on failure
861 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
864 char *options
= data
;
865 struct security_mnt_opts opts
;
867 security_init_mnt_opts(&opts
);
872 rc
= smack_parse_opts_str(options
, &opts
);
877 rc
= smack_set_mnt_opts(sb
, &opts
, 0, NULL
);
880 security_free_mnt_opts(&opts
);
885 * smack_sb_statfs - Smack check on statfs
886 * @dentry: identifies the file system in question
888 * Returns 0 if current can read the floor of the filesystem,
889 * and error code otherwise
891 static int smack_sb_statfs(struct dentry
*dentry
)
893 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
895 struct smk_audit_info ad
;
897 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
898 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
900 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
901 rc
= smk_bu_current("statfs", sbp
->smk_floor
, MAY_READ
, rc
);
910 * smack_bprm_set_creds - set creds for exec
911 * @bprm: the exec information
913 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
915 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
917 struct inode
*inode
= file_inode(bprm
->file
);
918 struct task_smack
*bsp
= bprm
->cred
->security
;
919 struct inode_smack
*isp
;
920 struct superblock_smack
*sbsp
;
923 if (bprm
->called_set_creds
)
926 isp
= inode
->i_security
;
927 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
930 sbsp
= inode
->i_sb
->s_security
;
931 if ((sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) &&
932 isp
->smk_task
!= sbsp
->smk_root
)
935 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
936 struct task_struct
*tracer
;
940 tracer
= ptrace_parent(current
);
941 if (likely(tracer
!= NULL
))
942 rc
= smk_ptrace_rule_check(tracer
,
951 if (bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
)
954 bsp
->smk_task
= isp
->smk_task
;
955 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
957 /* Decide if this is a secure exec. */
958 if (bsp
->smk_task
!= bsp
->smk_forked
)
959 bprm
->secureexec
= 1;
969 * smack_inode_alloc_security - allocate an inode blob
970 * @inode: the inode in need of a blob
972 * Returns 0 if it gets a blob, -ENOMEM otherwise
974 static int smack_inode_alloc_security(struct inode
*inode
)
976 struct smack_known
*skp
= smk_of_current();
978 inode
->i_security
= new_inode_smack(skp
);
979 if (inode
->i_security
== NULL
)
985 * smack_inode_free_rcu - Free inode_smack blob from cache
986 * @head: the rcu_head for getting inode_smack pointer
988 * Call back function called from call_rcu() to free
989 * the i_security blob pointer in inode
991 static void smack_inode_free_rcu(struct rcu_head
*head
)
993 struct inode_smack
*issp
;
995 issp
= container_of(head
, struct inode_smack
, smk_rcu
);
996 kmem_cache_free(smack_inode_cache
, issp
);
1000 * smack_inode_free_security - free an inode blob using call_rcu()
1001 * @inode: the inode with a blob
1003 * Clears the blob pointer in inode using RCU
1005 static void smack_inode_free_security(struct inode
*inode
)
1007 struct inode_smack
*issp
= inode
->i_security
;
1010 * The inode may still be referenced in a path walk and
1011 * a call to smack_inode_permission() can be made
1012 * after smack_inode_free_security() is called.
1013 * To avoid race condition free the i_security via RCU
1014 * and leave the current inode->i_security pointer intact.
1015 * The inode will be freed after the RCU grace period too.
1017 call_rcu(&issp
->smk_rcu
, smack_inode_free_rcu
);
1021 * smack_inode_init_security - copy out the smack from an inode
1022 * @inode: the newly created inode
1023 * @dir: containing directory object
1025 * @name: where to put the attribute name
1026 * @value: where to put the attribute value
1027 * @len: where to put the length of the attribute
1029 * Returns 0 if it all works out, -ENOMEM if there's no memory
1031 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
1032 const struct qstr
*qstr
, const char **name
,
1033 void **value
, size_t *len
)
1035 struct inode_smack
*issp
= inode
->i_security
;
1036 struct smack_known
*skp
= smk_of_current();
1037 struct smack_known
*isp
= smk_of_inode(inode
);
1038 struct smack_known
*dsp
= smk_of_inode(dir
);
1042 *name
= XATTR_SMACK_SUFFIX
;
1046 may
= smk_access_entry(skp
->smk_known
, dsp
->smk_known
,
1051 * If the access rule allows transmutation and
1052 * the directory requests transmutation then
1053 * by all means transmute.
1054 * Mark the inode as changed.
1056 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
1057 smk_inode_transmutable(dir
)) {
1059 issp
->smk_flags
|= SMK_INODE_CHANGED
;
1062 *value
= kstrdup(isp
->smk_known
, GFP_NOFS
);
1066 *len
= strlen(isp
->smk_known
);
1073 * smack_inode_link - Smack check on link
1074 * @old_dentry: the existing object
1076 * @new_dentry: the new object
1078 * Returns 0 if access is permitted, an error code otherwise
1080 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
1081 struct dentry
*new_dentry
)
1083 struct smack_known
*isp
;
1084 struct smk_audit_info ad
;
1087 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1088 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1090 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1091 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1092 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_WRITE
, rc
);
1094 if (rc
== 0 && d_is_positive(new_dentry
)) {
1095 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1096 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1097 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
1098 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_WRITE
, rc
);
1105 * smack_inode_unlink - Smack check on inode deletion
1106 * @dir: containing directory object
1107 * @dentry: file to unlink
1109 * Returns 0 if current can write the containing directory
1110 * and the object, error code otherwise
1112 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
1114 struct inode
*ip
= d_backing_inode(dentry
);
1115 struct smk_audit_info ad
;
1118 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1119 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1122 * You need write access to the thing you're unlinking
1124 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
1125 rc
= smk_bu_inode(ip
, MAY_WRITE
, rc
);
1128 * You also need write access to the containing directory
1130 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1131 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1132 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1133 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1139 * smack_inode_rmdir - Smack check on directory deletion
1140 * @dir: containing directory object
1141 * @dentry: directory to unlink
1143 * Returns 0 if current can write the containing directory
1144 * and the directory, error code otherwise
1146 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1148 struct smk_audit_info ad
;
1151 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1152 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1155 * You need write access to the thing you're removing
1157 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1158 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1161 * You also need write access to the containing directory
1163 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1164 smk_ad_setfield_u_fs_inode(&ad
, dir
);
1165 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
1166 rc
= smk_bu_inode(dir
, MAY_WRITE
, rc
);
1173 * smack_inode_rename - Smack check on rename
1174 * @old_inode: unused
1175 * @old_dentry: the old object
1176 * @new_inode: unused
1177 * @new_dentry: the new object
1179 * Read and write access is required on both the old and
1182 * Returns 0 if access is permitted, an error code otherwise
1184 static int smack_inode_rename(struct inode
*old_inode
,
1185 struct dentry
*old_dentry
,
1186 struct inode
*new_inode
,
1187 struct dentry
*new_dentry
)
1190 struct smack_known
*isp
;
1191 struct smk_audit_info ad
;
1193 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1194 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
1196 isp
= smk_of_inode(d_backing_inode(old_dentry
));
1197 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1198 rc
= smk_bu_inode(d_backing_inode(old_dentry
), MAY_READWRITE
, rc
);
1200 if (rc
== 0 && d_is_positive(new_dentry
)) {
1201 isp
= smk_of_inode(d_backing_inode(new_dentry
));
1202 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
1203 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
1204 rc
= smk_bu_inode(d_backing_inode(new_dentry
), MAY_READWRITE
, rc
);
1210 * smack_inode_permission - Smack version of permission()
1211 * @inode: the inode in question
1212 * @mask: the access requested
1214 * This is the important Smack hook.
1216 * Returns 0 if access is permitted, -EACCES otherwise
1218 static int smack_inode_permission(struct inode
*inode
, int mask
)
1220 struct superblock_smack
*sbsp
= inode
->i_sb
->s_security
;
1221 struct smk_audit_info ad
;
1222 int no_block
= mask
& MAY_NOT_BLOCK
;
1225 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
1227 * No permission to check. Existence test. Yup, it's there.
1232 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
) {
1233 if (smk_of_inode(inode
) != sbsp
->smk_root
)
1237 /* May be droppable after audit */
1240 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
1241 smk_ad_setfield_u_fs_inode(&ad
, inode
);
1242 rc
= smk_curacc(smk_of_inode(inode
), mask
, &ad
);
1243 rc
= smk_bu_inode(inode
, mask
, rc
);
1248 * smack_inode_setattr - Smack check for setting attributes
1249 * @dentry: the object
1250 * @iattr: for the force flag
1252 * Returns 0 if access is permitted, an error code otherwise
1254 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1256 struct smk_audit_info ad
;
1260 * Need to allow for clearing the setuid bit.
1262 if (iattr
->ia_valid
& ATTR_FORCE
)
1264 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1265 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1267 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1268 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1273 * smack_inode_getattr - Smack check for getting attributes
1274 * @mnt: vfsmount of the object
1275 * @dentry: the object
1277 * Returns 0 if access is permitted, an error code otherwise
1279 static int smack_inode_getattr(const struct path
*path
)
1281 struct smk_audit_info ad
;
1282 struct inode
*inode
= d_backing_inode(path
->dentry
);
1285 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1286 smk_ad_setfield_u_fs_path(&ad
, *path
);
1287 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1288 rc
= smk_bu_inode(inode
, MAY_READ
, rc
);
1293 * smack_inode_setxattr - Smack check for setting xattrs
1294 * @dentry: the object
1295 * @name: name of the attribute
1296 * @value: value of the attribute
1297 * @size: size of the value
1300 * This protects the Smack attribute explicitly.
1302 * Returns 0 if access is permitted, an error code otherwise
1304 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
1305 const void *value
, size_t size
, int flags
)
1307 struct smk_audit_info ad
;
1308 struct smack_known
*skp
;
1310 int check_import
= 0;
1315 * Check label validity here so import won't fail in post_setxattr
1317 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1318 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1319 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
1322 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1323 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1327 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1329 if (size
!= TRANS_TRUE_SIZE
||
1330 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
1333 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
1335 if (check_priv
&& !smack_privileged(CAP_MAC_ADMIN
))
1338 if (rc
== 0 && check_import
) {
1339 skp
= size
? smk_import_entry(value
, size
) : NULL
;
1342 else if (skp
== NULL
|| (check_star
&&
1343 (skp
== &smack_known_star
|| skp
== &smack_known_web
)))
1347 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1348 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1351 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1352 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1359 * smack_inode_post_setxattr - Apply the Smack update approved above
1361 * @name: attribute name
1362 * @value: attribute value
1363 * @size: attribute size
1366 * Set the pointer in the inode blob to the entry found
1367 * in the master label list.
1369 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
1370 const void *value
, size_t size
, int flags
)
1372 struct smack_known
*skp
;
1373 struct inode_smack
*isp
= d_backing_inode(dentry
)->i_security
;
1375 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
1376 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
1380 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1381 skp
= smk_import_entry(value
, size
);
1383 isp
->smk_inode
= skp
;
1384 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
1385 skp
= smk_import_entry(value
, size
);
1387 isp
->smk_task
= skp
;
1388 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1389 skp
= smk_import_entry(value
, size
);
1391 isp
->smk_mmap
= skp
;
1398 * smack_inode_getxattr - Smack check on getxattr
1399 * @dentry: the object
1402 * Returns 0 if access is permitted, an error code otherwise
1404 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
1406 struct smk_audit_info ad
;
1409 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1410 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1412 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_READ
, &ad
);
1413 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_READ
, rc
);
1418 * smack_inode_removexattr - Smack check on removexattr
1419 * @dentry: the object
1420 * @name: name of the attribute
1422 * Removing the Smack attribute requires CAP_MAC_ADMIN
1424 * Returns 0 if access is permitted, an error code otherwise
1426 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
1428 struct inode_smack
*isp
;
1429 struct smk_audit_info ad
;
1432 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
1433 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
1434 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
1435 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
1436 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
1437 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
1438 if (!smack_privileged(CAP_MAC_ADMIN
))
1441 rc
= cap_inode_removexattr(dentry
, name
);
1446 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
1447 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
1449 rc
= smk_curacc(smk_of_inode(d_backing_inode(dentry
)), MAY_WRITE
, &ad
);
1450 rc
= smk_bu_inode(d_backing_inode(dentry
), MAY_WRITE
, rc
);
1454 isp
= d_backing_inode(dentry
)->i_security
;
1456 * Don't do anything special for these.
1457 * XATTR_NAME_SMACKIPIN
1458 * XATTR_NAME_SMACKIPOUT
1460 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
1461 struct super_block
*sbp
= dentry
->d_sb
;
1462 struct superblock_smack
*sbsp
= sbp
->s_security
;
1464 isp
->smk_inode
= sbsp
->smk_default
;
1465 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0)
1466 isp
->smk_task
= NULL
;
1467 else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0)
1468 isp
->smk_mmap
= NULL
;
1469 else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
1470 isp
->smk_flags
&= ~SMK_INODE_TRANSMUTE
;
1476 * smack_inode_getsecurity - get smack xattrs
1477 * @inode: the object
1478 * @name: attribute name
1479 * @buffer: where to put the result
1480 * @alloc: duplicate memory
1482 * Returns the size of the attribute or an error code
1484 static int smack_inode_getsecurity(struct inode
*inode
,
1485 const char *name
, void **buffer
,
1488 struct socket_smack
*ssp
;
1489 struct socket
*sock
;
1490 struct super_block
*sbp
;
1491 struct inode
*ip
= (struct inode
*)inode
;
1492 struct smack_known
*isp
;
1494 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0)
1495 isp
= smk_of_inode(inode
);
1498 * The rest of the Smack xattrs are only on sockets.
1501 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
1504 sock
= SOCKET_I(ip
);
1505 if (sock
== NULL
|| sock
->sk
== NULL
)
1508 ssp
= sock
->sk
->sk_security
;
1510 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1512 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1519 *buffer
= kstrdup(isp
->smk_known
, GFP_KERNEL
);
1520 if (*buffer
== NULL
)
1524 return strlen(isp
->smk_known
);
1529 * smack_inode_listsecurity - list the Smack attributes
1530 * @inode: the object
1531 * @buffer: where they go
1532 * @buffer_size: size of buffer
1534 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1537 int len
= sizeof(XATTR_NAME_SMACK
);
1539 if (buffer
!= NULL
&& len
<= buffer_size
)
1540 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1546 * smack_inode_getsecid - Extract inode's security id
1547 * @inode: inode to extract the info from
1548 * @secid: where result will be saved
1550 static void smack_inode_getsecid(struct inode
*inode
, u32
*secid
)
1552 struct smack_known
*skp
= smk_of_inode(inode
);
1554 *secid
= skp
->smk_secid
;
1562 * There is no smack_file_permission hook
1564 * Should access checks be done on each read or write?
1565 * UNICOS and SELinux say yes.
1566 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1568 * I'll say no for now. Smack does not do the frequent
1569 * label changing that SELinux does.
1573 * smack_file_alloc_security - assign a file security blob
1576 * The security blob for a file is a pointer to the master
1577 * label list, so no allocation is done.
1579 * f_security is the owner security information. It
1580 * isn't used on file access checks, it's for send_sigio.
1584 static int smack_file_alloc_security(struct file
*file
)
1586 struct smack_known
*skp
= smk_of_current();
1588 file
->f_security
= skp
;
1593 * smack_file_free_security - clear a file security blob
1596 * The security blob for a file is a pointer to the master
1597 * label list, so no memory is freed.
1599 static void smack_file_free_security(struct file
*file
)
1601 file
->f_security
= NULL
;
1605 * smack_file_ioctl - Smack check on ioctls
1610 * Relies heavily on the correct use of the ioctl command conventions.
1612 * Returns 0 if allowed, error code otherwise
1614 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1618 struct smk_audit_info ad
;
1619 struct inode
*inode
= file_inode(file
);
1621 if (unlikely(IS_PRIVATE(inode
)))
1624 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1625 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1627 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1628 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1629 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1632 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
)) {
1633 rc
= smk_curacc(smk_of_inode(inode
), MAY_READ
, &ad
);
1634 rc
= smk_bu_file(file
, MAY_READ
, rc
);
1641 * smack_file_lock - Smack check on file locking
1645 * Returns 0 if current has lock access, error code otherwise
1647 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1649 struct smk_audit_info ad
;
1651 struct inode
*inode
= file_inode(file
);
1653 if (unlikely(IS_PRIVATE(inode
)))
1656 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1657 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1658 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1659 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1664 * smack_file_fcntl - Smack check on fcntl
1666 * @cmd: what action to check
1669 * Generally these operations are harmless.
1670 * File locking operations present an obvious mechanism
1671 * for passing information, so they require write access.
1673 * Returns 0 if current has access, error code otherwise
1675 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1678 struct smk_audit_info ad
;
1680 struct inode
*inode
= file_inode(file
);
1682 if (unlikely(IS_PRIVATE(inode
)))
1690 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1691 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1692 rc
= smk_curacc(smk_of_inode(inode
), MAY_LOCK
, &ad
);
1693 rc
= smk_bu_file(file
, MAY_LOCK
, rc
);
1697 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1698 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1699 rc
= smk_curacc(smk_of_inode(inode
), MAY_WRITE
, &ad
);
1700 rc
= smk_bu_file(file
, MAY_WRITE
, rc
);
1711 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1712 * if mapping anonymous memory.
1713 * @file contains the file structure for file to map (may be NULL).
1714 * @reqprot contains the protection requested by the application.
1715 * @prot contains the protection that will be applied by the kernel.
1716 * @flags contains the operational flags.
1717 * Return 0 if permission is granted.
1719 static int smack_mmap_file(struct file
*file
,
1720 unsigned long reqprot
, unsigned long prot
,
1721 unsigned long flags
)
1723 struct smack_known
*skp
;
1724 struct smack_known
*mkp
;
1725 struct smack_rule
*srp
;
1726 struct task_smack
*tsp
;
1727 struct smack_known
*okp
;
1728 struct inode_smack
*isp
;
1729 struct superblock_smack
*sbsp
;
1738 if (unlikely(IS_PRIVATE(file_inode(file
))))
1741 isp
= file_inode(file
)->i_security
;
1742 if (isp
->smk_mmap
== NULL
)
1744 sbsp
= file_inode(file
)->i_sb
->s_security
;
1745 if (sbsp
->smk_flags
& SMK_SB_UNTRUSTED
&&
1746 isp
->smk_mmap
!= sbsp
->smk_root
)
1748 mkp
= isp
->smk_mmap
;
1750 tsp
= current_security();
1751 skp
= smk_of_current();
1756 * For each Smack rule associated with the subject
1757 * label verify that the SMACK64MMAP also has access
1758 * to that rule's object label.
1760 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1761 okp
= srp
->smk_object
;
1763 * Matching labels always allows access.
1765 if (mkp
->smk_known
== okp
->smk_known
)
1768 * If there is a matching local rule take
1769 * that into account as well.
1771 may
= smk_access_entry(srp
->smk_subject
->smk_known
,
1775 may
= srp
->smk_access
;
1777 may
&= srp
->smk_access
;
1779 * If may is zero the SMACK64MMAP subject can't
1780 * possibly have less access.
1786 * Fetch the global list entry.
1787 * If there isn't one a SMACK64MMAP subject
1788 * can't have as much access as current.
1790 mmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1792 if (mmay
== -ENOENT
) {
1797 * If there is a local entry it modifies the
1798 * potential access, too.
1800 tmay
= smk_access_entry(mkp
->smk_known
, okp
->smk_known
,
1802 if (tmay
!= -ENOENT
)
1806 * If there is any access available to current that is
1807 * not available to a SMACK64MMAP subject
1810 if ((may
| mmay
) != mmay
) {
1822 * smack_file_set_fowner - set the file security blob value
1823 * @file: object in question
1826 static void smack_file_set_fowner(struct file
*file
)
1828 file
->f_security
= smk_of_current();
1832 * smack_file_send_sigiotask - Smack on sigio
1833 * @tsk: The target task
1834 * @fown: the object the signal come from
1837 * Allow a privileged task to get signals even if it shouldn't
1839 * Returns 0 if a subject with the object's smack could
1840 * write to the task, an error code otherwise.
1842 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1843 struct fown_struct
*fown
, int signum
)
1845 struct smack_known
*skp
;
1846 struct smack_known
*tkp
= smk_of_task(tsk
->cred
->security
);
1847 const struct cred
*tcred
;
1850 struct smk_audit_info ad
;
1853 * struct fown_struct is never outside the context of a struct file
1855 file
= container_of(fown
, struct file
, f_owner
);
1857 /* we don't log here as rc can be overriden */
1858 skp
= file
->f_security
;
1859 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, NULL
);
1860 rc
= smk_bu_note("sigiotask", skp
, tkp
, MAY_DELIVER
, rc
);
1863 tcred
= __task_cred(tsk
);
1864 if (rc
!= 0 && smack_privileged_cred(CAP_MAC_OVERRIDE
, tcred
))
1868 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1869 smk_ad_setfield_u_tsk(&ad
, tsk
);
1870 smack_log(skp
->smk_known
, tkp
->smk_known
, MAY_DELIVER
, rc
, &ad
);
1875 * smack_file_receive - Smack file receive check
1878 * Returns 0 if current has access, error code otherwise
1880 static int smack_file_receive(struct file
*file
)
1884 struct smk_audit_info ad
;
1885 struct inode
*inode
= file_inode(file
);
1886 struct socket
*sock
;
1887 struct task_smack
*tsp
;
1888 struct socket_smack
*ssp
;
1890 if (unlikely(IS_PRIVATE(inode
)))
1893 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1894 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1896 if (inode
->i_sb
->s_magic
== SOCKFS_MAGIC
) {
1897 sock
= SOCKET_I(inode
);
1898 ssp
= sock
->sk
->sk_security
;
1899 tsp
= current_security();
1901 * If the receiving process can't write to the
1902 * passed socket or if the passed socket can't
1903 * write to the receiving process don't accept
1904 * the passed socket.
1906 rc
= smk_access(tsp
->smk_task
, ssp
->smk_out
, MAY_WRITE
, &ad
);
1907 rc
= smk_bu_file(file
, may
, rc
);
1910 rc
= smk_access(ssp
->smk_in
, tsp
->smk_task
, MAY_WRITE
, &ad
);
1911 rc
= smk_bu_file(file
, may
, rc
);
1915 * This code relies on bitmasks.
1917 if (file
->f_mode
& FMODE_READ
)
1919 if (file
->f_mode
& FMODE_WRITE
)
1922 rc
= smk_curacc(smk_of_inode(inode
), may
, &ad
);
1923 rc
= smk_bu_file(file
, may
, rc
);
1928 * smack_file_open - Smack dentry open processing
1930 * @cred: task credential
1932 * Set the security blob in the file structure.
1933 * Allow the open only if the task has read access. There are
1934 * many read operations (e.g. fstat) that you can do with an
1935 * fd even if you have the file open write-only.
1939 static int smack_file_open(struct file
*file
)
1941 struct task_smack
*tsp
= file
->f_cred
->security
;
1942 struct inode
*inode
= file_inode(file
);
1943 struct smk_audit_info ad
;
1946 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1947 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1948 rc
= smk_tskacc(tsp
, smk_of_inode(inode
), MAY_READ
, &ad
);
1949 rc
= smk_bu_credfile(file
->f_cred
, file
, MAY_READ
, rc
);
1959 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1960 * @new: the new credentials
1961 * @gfp: the atomicity of any memory allocations
1963 * Prepare a blank set of credentials for modification. This must allocate all
1964 * the memory the LSM module might require such that cred_transfer() can
1965 * complete without error.
1967 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1969 struct task_smack
*tsp
;
1971 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1975 cred
->security
= tsp
;
1982 * smack_cred_free - "free" task-level security credentials
1983 * @cred: the credentials in question
1986 static void smack_cred_free(struct cred
*cred
)
1988 struct task_smack
*tsp
= cred
->security
;
1989 struct smack_rule
*rp
;
1990 struct list_head
*l
;
1991 struct list_head
*n
;
1995 cred
->security
= NULL
;
1997 smk_destroy_label_list(&tsp
->smk_relabel
);
1999 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
2000 rp
= list_entry(l
, struct smack_rule
, list
);
2001 list_del(&rp
->list
);
2008 * smack_cred_prepare - prepare new set of credentials for modification
2009 * @new: the new credentials
2010 * @old: the original credentials
2011 * @gfp: the atomicity of any memory allocations
2013 * Prepare a new set of credentials for modification.
2015 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
2018 struct task_smack
*old_tsp
= old
->security
;
2019 struct task_smack
*new_tsp
;
2022 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
2023 if (new_tsp
== NULL
)
2026 new->security
= new_tsp
;
2028 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
2032 rc
= smk_copy_relabel(&new_tsp
->smk_relabel
, &old_tsp
->smk_relabel
,
2041 * smack_cred_transfer - Transfer the old credentials to the new credentials
2042 * @new: the new credentials
2043 * @old: the original credentials
2045 * Fill in a set of blank credentials from another set of credentials.
2047 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
2049 struct task_smack
*old_tsp
= old
->security
;
2050 struct task_smack
*new_tsp
= new->security
;
2052 new_tsp
->smk_task
= old_tsp
->smk_task
;
2053 new_tsp
->smk_forked
= old_tsp
->smk_task
;
2054 mutex_init(&new_tsp
->smk_rules_lock
);
2055 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
2058 /* cbs copy rule list */
2062 * smack_cred_getsecid - get the secid corresponding to a creds structure
2063 * @c: the object creds
2064 * @secid: where to put the result
2066 * Sets the secid to contain a u32 version of the smack label.
2068 static void smack_cred_getsecid(const struct cred
*c
, u32
*secid
)
2070 struct smack_known
*skp
;
2073 skp
= smk_of_task(c
->security
);
2074 *secid
= skp
->smk_secid
;
2079 * smack_kernel_act_as - Set the subjective context in a set of credentials
2080 * @new: points to the set of credentials to be modified.
2081 * @secid: specifies the security ID to be set
2083 * Set the security data for a kernel service.
2085 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
2087 struct task_smack
*new_tsp
= new->security
;
2089 new_tsp
->smk_task
= smack_from_secid(secid
);
2094 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2095 * @new: points to the set of credentials to be modified
2096 * @inode: points to the inode to use as a reference
2098 * Set the file creation context in a set of credentials to the same
2099 * as the objective context of the specified inode
2101 static int smack_kernel_create_files_as(struct cred
*new,
2102 struct inode
*inode
)
2104 struct inode_smack
*isp
= inode
->i_security
;
2105 struct task_smack
*tsp
= new->security
;
2107 tsp
->smk_forked
= isp
->smk_inode
;
2108 tsp
->smk_task
= tsp
->smk_forked
;
2113 * smk_curacc_on_task - helper to log task related access
2114 * @p: the task object
2115 * @access: the access requested
2116 * @caller: name of the calling function for audit
2118 * Return 0 if access is permitted
2120 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
2123 struct smk_audit_info ad
;
2124 struct smack_known
*skp
= smk_of_task_struct(p
);
2127 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
2128 smk_ad_setfield_u_tsk(&ad
, p
);
2129 rc
= smk_curacc(skp
, access
, &ad
);
2130 rc
= smk_bu_task(p
, access
, rc
);
2135 * smack_task_setpgid - Smack check on setting pgid
2136 * @p: the task object
2139 * Return 0 if write access is permitted
2141 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2143 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2147 * smack_task_getpgid - Smack access check for getpgid
2148 * @p: the object task
2150 * Returns 0 if current can read the object task, error code otherwise
2152 static int smack_task_getpgid(struct task_struct
*p
)
2154 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2158 * smack_task_getsid - Smack access check for getsid
2159 * @p: the object task
2161 * Returns 0 if current can read the object task, error code otherwise
2163 static int smack_task_getsid(struct task_struct
*p
)
2165 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2169 * smack_task_getsecid - get the secid of the task
2170 * @p: the object task
2171 * @secid: where to put the result
2173 * Sets the secid to contain a u32 version of the smack label.
2175 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
2177 struct smack_known
*skp
= smk_of_task_struct(p
);
2179 *secid
= skp
->smk_secid
;
2183 * smack_task_setnice - Smack check on setting nice
2184 * @p: the task object
2187 * Return 0 if write access is permitted
2189 static int smack_task_setnice(struct task_struct
*p
, int nice
)
2191 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2195 * smack_task_setioprio - Smack check on setting ioprio
2196 * @p: the task object
2199 * Return 0 if write access is permitted
2201 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
2203 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2207 * smack_task_getioprio - Smack check on reading ioprio
2208 * @p: the task object
2210 * Return 0 if read access is permitted
2212 static int smack_task_getioprio(struct task_struct
*p
)
2214 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2218 * smack_task_setscheduler - Smack check on setting scheduler
2219 * @p: the task object
2223 * Return 0 if read access is permitted
2225 static int smack_task_setscheduler(struct task_struct
*p
)
2227 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2231 * smack_task_getscheduler - Smack check on reading scheduler
2232 * @p: the task object
2234 * Return 0 if read access is permitted
2236 static int smack_task_getscheduler(struct task_struct
*p
)
2238 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
2242 * smack_task_movememory - Smack check on moving memory
2243 * @p: the task object
2245 * Return 0 if write access is permitted
2247 static int smack_task_movememory(struct task_struct
*p
)
2249 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
2253 * smack_task_kill - Smack check on signal delivery
2254 * @p: the task object
2257 * @cred: identifies the cred to use in lieu of current's
2259 * Return 0 if write access is permitted
2262 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
2263 int sig
, const struct cred
*cred
)
2265 struct smk_audit_info ad
;
2266 struct smack_known
*skp
;
2267 struct smack_known
*tkp
= smk_of_task_struct(p
);
2271 return 0; /* null signal; existence test */
2273 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
2274 smk_ad_setfield_u_tsk(&ad
, p
);
2276 * Sending a signal requires that the sender
2277 * can write the receiver.
2280 rc
= smk_curacc(tkp
, MAY_DELIVER
, &ad
);
2281 rc
= smk_bu_task(p
, MAY_DELIVER
, rc
);
2285 * If the cred isn't NULL we're dealing with some USB IO
2286 * specific behavior. This is not clean. For one thing
2287 * we can't take privilege into account.
2289 skp
= smk_of_task(cred
->security
);
2290 rc
= smk_access(skp
, tkp
, MAY_DELIVER
, &ad
);
2291 rc
= smk_bu_note("USB signal", skp
, tkp
, MAY_DELIVER
, rc
);
2296 * smack_task_to_inode - copy task smack into the inode blob
2297 * @p: task to copy from
2298 * @inode: inode to copy to
2300 * Sets the smack pointer in the inode security blob
2302 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
2304 struct inode_smack
*isp
= inode
->i_security
;
2305 struct smack_known
*skp
= smk_of_task_struct(p
);
2307 isp
->smk_inode
= skp
;
2308 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2316 * smack_sk_alloc_security - Allocate a socket blob
2319 * @gfp_flags: memory allocation flags
2321 * Assign Smack pointers to current
2323 * Returns 0 on success, -ENOMEM is there's no memory
2325 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
2327 struct smack_known
*skp
= smk_of_current();
2328 struct socket_smack
*ssp
;
2330 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
2335 * Sockets created by kernel threads receive web label.
2337 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2338 ssp
->smk_in
= &smack_known_web
;
2339 ssp
->smk_out
= &smack_known_web
;
2344 ssp
->smk_packet
= NULL
;
2346 sk
->sk_security
= ssp
;
2352 * smack_sk_free_security - Free a socket blob
2355 * Clears the blob pointer
2357 static void smack_sk_free_security(struct sock
*sk
)
2359 #ifdef SMACK_IPV6_PORT_LABELING
2360 struct smk_port_label
*spp
;
2362 if (sk
->sk_family
== PF_INET6
) {
2364 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2365 if (spp
->smk_sock
!= sk
)
2367 spp
->smk_can_reuse
= 1;
2373 kfree(sk
->sk_security
);
2377 * smack_ipv4host_label - check host based restrictions
2378 * @sip: the object end
2380 * looks for host based access restrictions
2382 * This version will only be appropriate for really small sets of single label
2383 * hosts. The caller is responsible for ensuring that the RCU read lock is
2384 * taken before calling this function.
2386 * Returns the label of the far end or NULL if it's not special.
2388 static struct smack_known
*smack_ipv4host_label(struct sockaddr_in
*sip
)
2390 struct smk_net4addr
*snp
;
2391 struct in_addr
*siap
= &sip
->sin_addr
;
2393 if (siap
->s_addr
== 0)
2396 list_for_each_entry_rcu(snp
, &smk_net4addr_list
, list
)
2398 * we break after finding the first match because
2399 * the list is sorted from longest to shortest mask
2400 * so we have found the most specific match
2402 if (snp
->smk_host
.s_addr
==
2403 (siap
->s_addr
& snp
->smk_mask
.s_addr
))
2404 return snp
->smk_label
;
2409 #if IS_ENABLED(CONFIG_IPV6)
2411 * smk_ipv6_localhost - Check for local ipv6 host address
2414 * Returns boolean true if this is the localhost address
2416 static bool smk_ipv6_localhost(struct sockaddr_in6
*sip
)
2418 __be16
*be16p
= (__be16
*)&sip
->sin6_addr
;
2419 __be32
*be32p
= (__be32
*)&sip
->sin6_addr
;
2421 if (be32p
[0] == 0 && be32p
[1] == 0 && be32p
[2] == 0 && be16p
[6] == 0 &&
2422 ntohs(be16p
[7]) == 1)
2428 * smack_ipv6host_label - check host based restrictions
2429 * @sip: the object end
2431 * looks for host based access restrictions
2433 * This version will only be appropriate for really small sets of single label
2434 * hosts. The caller is responsible for ensuring that the RCU read lock is
2435 * taken before calling this function.
2437 * Returns the label of the far end or NULL if it's not special.
2439 static struct smack_known
*smack_ipv6host_label(struct sockaddr_in6
*sip
)
2441 struct smk_net6addr
*snp
;
2442 struct in6_addr
*sap
= &sip
->sin6_addr
;
2447 * It's local. Don't look for a host label.
2449 if (smk_ipv6_localhost(sip
))
2452 list_for_each_entry_rcu(snp
, &smk_net6addr_list
, list
) {
2454 * If the label is NULL the entry has
2455 * been renounced. Ignore it.
2457 if (snp
->smk_label
== NULL
)
2460 * we break after finding the first match because
2461 * the list is sorted from longest to shortest mask
2462 * so we have found the most specific match
2464 for (found
= 1, i
= 0; i
< 8; i
++) {
2465 if ((sap
->s6_addr16
[i
] & snp
->smk_mask
.s6_addr16
[i
]) !=
2466 snp
->smk_host
.s6_addr16
[i
]) {
2472 return snp
->smk_label
;
2477 #endif /* CONFIG_IPV6 */
2480 * smack_netlabel - Set the secattr on a socket
2482 * @labeled: socket label scheme
2484 * Convert the outbound smack value (smk_out) to a
2485 * secattr and attach it to the socket.
2487 * Returns 0 on success or an error code
2489 static int smack_netlabel(struct sock
*sk
, int labeled
)
2491 struct smack_known
*skp
;
2492 struct socket_smack
*ssp
= sk
->sk_security
;
2496 * Usually the netlabel code will handle changing the
2497 * packet labeling based on the label.
2498 * The case of a single label host is different, because
2499 * a single label host should never get a labeled packet
2500 * even though the label is usually associated with a packet
2504 bh_lock_sock_nested(sk
);
2506 if (ssp
->smk_out
== smack_net_ambient
||
2507 labeled
== SMACK_UNLABELED_SOCKET
)
2508 netlbl_sock_delattr(sk
);
2511 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
2521 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2523 * @sap: the destination address
2525 * Set the correct secattr for the given socket based on the destination
2526 * address and perform any outbound access checks needed.
2528 * Returns 0 on success or an error code.
2531 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
2533 struct smack_known
*skp
;
2536 struct smack_known
*hkp
;
2537 struct socket_smack
*ssp
= sk
->sk_security
;
2538 struct smk_audit_info ad
;
2541 hkp
= smack_ipv4host_label(sap
);
2544 struct lsm_network_audit net
;
2546 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2547 ad
.a
.u
.net
->family
= sap
->sin_family
;
2548 ad
.a
.u
.net
->dport
= sap
->sin_port
;
2549 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
2551 sk_lbl
= SMACK_UNLABELED_SOCKET
;
2553 rc
= smk_access(skp
, hkp
, MAY_WRITE
, &ad
);
2554 rc
= smk_bu_note("IPv4 host check", skp
, hkp
, MAY_WRITE
, rc
);
2556 sk_lbl
= SMACK_CIPSO_SOCKET
;
2563 return smack_netlabel(sk
, sk_lbl
);
2566 #if IS_ENABLED(CONFIG_IPV6)
2568 * smk_ipv6_check - check Smack access
2569 * @subject: subject Smack label
2570 * @object: object Smack label
2572 * @act: the action being taken
2574 * Check an IPv6 access
2576 static int smk_ipv6_check(struct smack_known
*subject
,
2577 struct smack_known
*object
,
2578 struct sockaddr_in6
*address
, int act
)
2581 struct lsm_network_audit net
;
2583 struct smk_audit_info ad
;
2587 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2588 ad
.a
.u
.net
->family
= PF_INET6
;
2589 ad
.a
.u
.net
->dport
= ntohs(address
->sin6_port
);
2590 if (act
== SMK_RECEIVING
)
2591 ad
.a
.u
.net
->v6info
.saddr
= address
->sin6_addr
;
2593 ad
.a
.u
.net
->v6info
.daddr
= address
->sin6_addr
;
2595 rc
= smk_access(subject
, object
, MAY_WRITE
, &ad
);
2596 rc
= smk_bu_note("IPv6 check", subject
, object
, MAY_WRITE
, rc
);
2599 #endif /* CONFIG_IPV6 */
2601 #ifdef SMACK_IPV6_PORT_LABELING
2603 * smk_ipv6_port_label - Smack port access table management
2607 * Create or update the port list entry
2609 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
2611 struct sock
*sk
= sock
->sk
;
2612 struct sockaddr_in6
*addr6
;
2613 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2614 struct smk_port_label
*spp
;
2615 unsigned short port
= 0;
2617 if (address
== NULL
) {
2619 * This operation is changing the Smack information
2620 * on the bound socket. Take the changes to the port
2624 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2625 if (sk
!= spp
->smk_sock
)
2627 spp
->smk_in
= ssp
->smk_in
;
2628 spp
->smk_out
= ssp
->smk_out
;
2633 * A NULL address is only used for updating existing
2634 * bound entries. If there isn't one, it's OK.
2640 addr6
= (struct sockaddr_in6
*)address
;
2641 port
= ntohs(addr6
->sin6_port
);
2643 * This is a special case that is safely ignored.
2649 * Look for an existing port list entry.
2650 * This is an indication that a port is getting reused.
2653 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2654 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sock
->type
)
2656 if (spp
->smk_can_reuse
!= 1) {
2660 spp
->smk_port
= port
;
2662 spp
->smk_in
= ssp
->smk_in
;
2663 spp
->smk_out
= ssp
->smk_out
;
2664 spp
->smk_can_reuse
= 0;
2670 * A new port entry is required.
2672 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
2676 spp
->smk_port
= port
;
2678 spp
->smk_in
= ssp
->smk_in
;
2679 spp
->smk_out
= ssp
->smk_out
;
2680 spp
->smk_sock_type
= sock
->type
;
2681 spp
->smk_can_reuse
= 0;
2683 mutex_lock(&smack_ipv6_lock
);
2684 list_add_rcu(&spp
->list
, &smk_ipv6_port_list
);
2685 mutex_unlock(&smack_ipv6_lock
);
2690 * smk_ipv6_port_check - check Smack port access
2694 * Create or update the port list entry
2696 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr_in6
*address
,
2699 struct smk_port_label
*spp
;
2700 struct socket_smack
*ssp
= sk
->sk_security
;
2701 struct smack_known
*skp
= NULL
;
2702 unsigned short port
;
2703 struct smack_known
*object
;
2705 if (act
== SMK_RECEIVING
) {
2706 skp
= smack_ipv6host_label(address
);
2707 object
= ssp
->smk_in
;
2710 object
= smack_ipv6host_label(address
);
2714 * The other end is a single label host.
2716 if (skp
!= NULL
&& object
!= NULL
)
2717 return smk_ipv6_check(skp
, object
, address
, act
);
2719 skp
= smack_net_ambient
;
2721 object
= smack_net_ambient
;
2724 * It's remote, so port lookup does no good.
2726 if (!smk_ipv6_localhost(address
))
2727 return smk_ipv6_check(skp
, object
, address
, act
);
2730 * It's local so the send check has to have passed.
2732 if (act
== SMK_RECEIVING
)
2735 port
= ntohs(address
->sin6_port
);
2737 list_for_each_entry_rcu(spp
, &smk_ipv6_port_list
, list
) {
2738 if (spp
->smk_port
!= port
|| spp
->smk_sock_type
!= sk
->sk_type
)
2740 object
= spp
->smk_in
;
2741 if (act
== SMK_CONNECTING
)
2742 ssp
->smk_packet
= spp
->smk_out
;
2747 return smk_ipv6_check(skp
, object
, address
, act
);
2749 #endif /* SMACK_IPV6_PORT_LABELING */
2752 * smack_inode_setsecurity - set smack xattrs
2753 * @inode: the object
2754 * @name: attribute name
2755 * @value: attribute value
2756 * @size: size of the attribute
2759 * Sets the named attribute in the appropriate blob
2761 * Returns 0 on success, or an error code
2763 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2764 const void *value
, size_t size
, int flags
)
2766 struct smack_known
*skp
;
2767 struct inode_smack
*nsp
= inode
->i_security
;
2768 struct socket_smack
*ssp
;
2769 struct socket
*sock
;
2772 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2775 skp
= smk_import_entry(value
, size
);
2777 return PTR_ERR(skp
);
2779 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2780 nsp
->smk_inode
= skp
;
2781 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2785 * The rest of the Smack xattrs are only on sockets.
2787 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2790 sock
= SOCKET_I(inode
);
2791 if (sock
== NULL
|| sock
->sk
== NULL
)
2794 ssp
= sock
->sk
->sk_security
;
2796 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2798 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2800 if (sock
->sk
->sk_family
== PF_INET
) {
2801 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2804 "Smack: \"%s\" netlbl error %d.\n",
2810 #ifdef SMACK_IPV6_PORT_LABELING
2811 if (sock
->sk
->sk_family
== PF_INET6
)
2812 smk_ipv6_port_label(sock
, NULL
);
2819 * smack_socket_post_create - finish socket setup
2821 * @family: protocol family
2826 * Sets the netlabel information on the socket
2828 * Returns 0 on success, and error code otherwise
2830 static int smack_socket_post_create(struct socket
*sock
, int family
,
2831 int type
, int protocol
, int kern
)
2833 struct socket_smack
*ssp
;
2835 if (sock
->sk
== NULL
)
2839 * Sockets created by kernel threads receive web label.
2841 if (unlikely(current
->flags
& PF_KTHREAD
)) {
2842 ssp
= sock
->sk
->sk_security
;
2843 ssp
->smk_in
= &smack_known_web
;
2844 ssp
->smk_out
= &smack_known_web
;
2847 if (family
!= PF_INET
)
2850 * Set the outbound netlbl.
2852 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2856 * smack_socket_socketpair - create socket pair
2857 * @socka: one socket
2858 * @sockb: another socket
2860 * Cross reference the peer labels for SO_PEERSEC
2862 * Returns 0 on success, and error code otherwise
2864 static int smack_socket_socketpair(struct socket
*socka
,
2865 struct socket
*sockb
)
2867 struct socket_smack
*asp
= socka
->sk
->sk_security
;
2868 struct socket_smack
*bsp
= sockb
->sk
->sk_security
;
2870 asp
->smk_packet
= bsp
->smk_out
;
2871 bsp
->smk_packet
= asp
->smk_out
;
2876 #ifdef SMACK_IPV6_PORT_LABELING
2878 * smack_socket_bind - record port binding information.
2880 * @address: the port address
2881 * @addrlen: size of the address
2883 * Records the label bound to a port.
2887 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2890 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2891 smk_ipv6_port_label(sock
, address
);
2894 #endif /* SMACK_IPV6_PORT_LABELING */
2897 * smack_socket_connect - connect access check
2899 * @sap: the other end
2900 * @addrlen: size of sap
2902 * Verifies that a connection may be possible
2904 * Returns 0 on success, and error code otherwise
2906 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2910 #if IS_ENABLED(CONFIG_IPV6)
2911 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
2913 #ifdef SMACK_IPV6_SECMARK_LABELING
2914 struct smack_known
*rsp
;
2915 struct socket_smack
*ssp
;
2918 if (sock
->sk
== NULL
)
2921 #ifdef SMACK_IPV6_SECMARK_LABELING
2922 ssp
= sock
->sk
->sk_security
;
2925 switch (sock
->sk
->sk_family
) {
2927 if (addrlen
< sizeof(struct sockaddr_in
))
2929 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2932 if (addrlen
< sizeof(struct sockaddr_in6
))
2934 #ifdef SMACK_IPV6_SECMARK_LABELING
2935 rsp
= smack_ipv6host_label(sip
);
2937 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sip
,
2940 #ifdef SMACK_IPV6_PORT_LABELING
2941 rc
= smk_ipv6_port_check(sock
->sk
, sip
, SMK_CONNECTING
);
2949 * smack_flags_to_may - convert S_ to MAY_ values
2950 * @flags: the S_ value
2952 * Returns the equivalent MAY_ value
2954 static int smack_flags_to_may(int flags
)
2958 if (flags
& S_IRUGO
)
2960 if (flags
& S_IWUGO
)
2962 if (flags
& S_IXUGO
)
2969 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2974 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2976 struct smack_known
*skp
= smk_of_current();
2978 msg
->security
= skp
;
2983 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2986 * Clears the blob pointer
2988 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2990 msg
->security
= NULL
;
2994 * smack_of_ipc - the smack pointer for the ipc
2997 * Returns a pointer to the smack value
2999 static struct smack_known
*smack_of_ipc(struct kern_ipc_perm
*isp
)
3001 return (struct smack_known
*)isp
->security
;
3005 * smack_ipc_alloc_security - Set the security blob for ipc
3010 static int smack_ipc_alloc_security(struct kern_ipc_perm
*isp
)
3012 struct smack_known
*skp
= smk_of_current();
3014 isp
->security
= skp
;
3019 * smack_ipc_free_security - Clear the security blob for ipc
3022 * Clears the blob pointer
3024 static void smack_ipc_free_security(struct kern_ipc_perm
*isp
)
3026 isp
->security
= NULL
;
3030 * smk_curacc_shm : check if current has access on shm
3032 * @access : access requested
3034 * Returns 0 if current has the requested access, error code otherwise
3036 static int smk_curacc_shm(struct kern_ipc_perm
*isp
, int access
)
3038 struct smack_known
*ssp
= smack_of_ipc(isp
);
3039 struct smk_audit_info ad
;
3043 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3044 ad
.a
.u
.ipc_id
= isp
->id
;
3046 rc
= smk_curacc(ssp
, access
, &ad
);
3047 rc
= smk_bu_current("shm", ssp
, access
, rc
);
3052 * smack_shm_associate - Smack access check for shm
3054 * @shmflg: access requested
3056 * Returns 0 if current has the requested access, error code otherwise
3058 static int smack_shm_associate(struct kern_ipc_perm
*isp
, int shmflg
)
3062 may
= smack_flags_to_may(shmflg
);
3063 return smk_curacc_shm(isp
, may
);
3067 * smack_shm_shmctl - Smack access check for shm
3069 * @cmd: what it wants to do
3071 * Returns 0 if current has the requested access, error code otherwise
3073 static int smack_shm_shmctl(struct kern_ipc_perm
*isp
, int cmd
)
3087 may
= MAY_READWRITE
;
3092 * System level information.
3098 return smk_curacc_shm(isp
, may
);
3102 * smack_shm_shmat - Smack access for shmat
3105 * @shmflg: access requested
3107 * Returns 0 if current has the requested access, error code otherwise
3109 static int smack_shm_shmat(struct kern_ipc_perm
*ipc
, char __user
*shmaddr
,
3114 may
= smack_flags_to_may(shmflg
);
3115 return smk_curacc_shm(ipc
, may
);
3119 * smk_curacc_sem : check if current has access on sem
3121 * @access : access requested
3123 * Returns 0 if current has the requested access, error code otherwise
3125 static int smk_curacc_sem(struct kern_ipc_perm
*isp
, int access
)
3127 struct smack_known
*ssp
= smack_of_ipc(isp
);
3128 struct smk_audit_info ad
;
3132 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3133 ad
.a
.u
.ipc_id
= isp
->id
;
3135 rc
= smk_curacc(ssp
, access
, &ad
);
3136 rc
= smk_bu_current("sem", ssp
, access
, rc
);
3141 * smack_sem_associate - Smack access check for sem
3143 * @semflg: access requested
3145 * Returns 0 if current has the requested access, error code otherwise
3147 static int smack_sem_associate(struct kern_ipc_perm
*isp
, int semflg
)
3151 may
= smack_flags_to_may(semflg
);
3152 return smk_curacc_sem(isp
, may
);
3156 * smack_sem_shmctl - Smack access check for sem
3158 * @cmd: what it wants to do
3160 * Returns 0 if current has the requested access, error code otherwise
3162 static int smack_sem_semctl(struct kern_ipc_perm
*isp
, int cmd
)
3181 may
= MAY_READWRITE
;
3186 * System level information
3193 return smk_curacc_sem(isp
, may
);
3197 * smack_sem_semop - Smack checks of semaphore operations
3203 * Treated as read and write in all cases.
3205 * Returns 0 if access is allowed, error code otherwise
3207 static int smack_sem_semop(struct kern_ipc_perm
*isp
, struct sembuf
*sops
,
3208 unsigned nsops
, int alter
)
3210 return smk_curacc_sem(isp
, MAY_READWRITE
);
3214 * smk_curacc_msq : helper to check if current has access on msq
3216 * @access : access requested
3218 * return 0 if current has access, error otherwise
3220 static int smk_curacc_msq(struct kern_ipc_perm
*isp
, int access
)
3222 struct smack_known
*msp
= smack_of_ipc(isp
);
3223 struct smk_audit_info ad
;
3227 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3228 ad
.a
.u
.ipc_id
= isp
->id
;
3230 rc
= smk_curacc(msp
, access
, &ad
);
3231 rc
= smk_bu_current("msq", msp
, access
, rc
);
3236 * smack_msg_queue_associate - Smack access check for msg_queue
3238 * @msqflg: access requested
3240 * Returns 0 if current has the requested access, error code otherwise
3242 static int smack_msg_queue_associate(struct kern_ipc_perm
*isp
, int msqflg
)
3246 may
= smack_flags_to_may(msqflg
);
3247 return smk_curacc_msq(isp
, may
);
3251 * smack_msg_queue_msgctl - Smack access check for msg_queue
3253 * @cmd: what it wants to do
3255 * Returns 0 if current has the requested access, error code otherwise
3257 static int smack_msg_queue_msgctl(struct kern_ipc_perm
*isp
, int cmd
)
3269 may
= MAY_READWRITE
;
3274 * System level information
3281 return smk_curacc_msq(isp
, may
);
3285 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3288 * @msqflg: access requested
3290 * Returns 0 if current has the requested access, error code otherwise
3292 static int smack_msg_queue_msgsnd(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3297 may
= smack_flags_to_may(msqflg
);
3298 return smk_curacc_msq(isp
, may
);
3302 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3309 * Returns 0 if current has read and write access, error code otherwise
3311 static int smack_msg_queue_msgrcv(struct kern_ipc_perm
*isp
, struct msg_msg
*msg
,
3312 struct task_struct
*target
, long type
, int mode
)
3314 return smk_curacc_msq(isp
, MAY_READWRITE
);
3318 * smack_ipc_permission - Smack access for ipc_permission()
3319 * @ipp: the object permissions
3320 * @flag: access requested
3322 * Returns 0 if current has read and write access, error code otherwise
3324 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
3326 struct smack_known
*iskp
= ipp
->security
;
3327 int may
= smack_flags_to_may(flag
);
3328 struct smk_audit_info ad
;
3332 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
3333 ad
.a
.u
.ipc_id
= ipp
->id
;
3335 rc
= smk_curacc(iskp
, may
, &ad
);
3336 rc
= smk_bu_current("svipc", iskp
, may
, rc
);
3341 * smack_ipc_getsecid - Extract smack security id
3342 * @ipp: the object permissions
3343 * @secid: where result will be saved
3345 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
3347 struct smack_known
*iskp
= ipp
->security
;
3349 *secid
= iskp
->smk_secid
;
3353 * smack_d_instantiate - Make sure the blob is correct on an inode
3354 * @opt_dentry: dentry where inode will be attached
3355 * @inode: the object
3357 * Set the inode's security blob if it hasn't been done already.
3359 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
3361 struct super_block
*sbp
;
3362 struct superblock_smack
*sbsp
;
3363 struct inode_smack
*isp
;
3364 struct smack_known
*skp
;
3365 struct smack_known
*ckp
= smk_of_current();
3366 struct smack_known
*final
;
3367 char trattr
[TRANS_TRUE_SIZE
];
3375 isp
= inode
->i_security
;
3377 mutex_lock(&isp
->smk_lock
);
3379 * If the inode is already instantiated
3380 * take the quick way out
3382 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
3386 sbsp
= sbp
->s_security
;
3388 * We're going to use the superblock default label
3389 * if there's no label on the file.
3391 final
= sbsp
->smk_default
;
3394 * If this is the root inode the superblock
3395 * may be in the process of initialization.
3396 * If that is the case use the root value out
3397 * of the superblock.
3399 if (opt_dentry
->d_parent
== opt_dentry
) {
3400 switch (sbp
->s_magic
) {
3401 case CGROUP_SUPER_MAGIC
:
3402 case CGROUP2_SUPER_MAGIC
:
3404 * The cgroup filesystem is never mounted,
3405 * so there's no opportunity to set the mount
3408 sbsp
->smk_root
= &smack_known_star
;
3409 sbsp
->smk_default
= &smack_known_star
;
3410 isp
->smk_inode
= sbsp
->smk_root
;
3414 * What about shmem/tmpfs anonymous files with dentry
3415 * obtained from d_alloc_pseudo()?
3417 isp
->smk_inode
= smk_of_current();
3420 isp
->smk_inode
= smk_of_current();
3424 * Socket access is controlled by the socket
3425 * structures associated with the task involved.
3427 isp
->smk_inode
= &smack_known_star
;
3430 isp
->smk_inode
= sbsp
->smk_root
;
3433 isp
->smk_flags
|= SMK_INODE_INSTANT
;
3438 * This is pretty hackish.
3439 * Casey says that we shouldn't have to do
3440 * file system specific code, but it does help
3441 * with keeping it simple.
3443 switch (sbp
->s_magic
) {
3445 case CGROUP_SUPER_MAGIC
:
3446 case CGROUP2_SUPER_MAGIC
:
3448 * Casey says that it's a little embarrassing
3449 * that the smack file system doesn't do
3450 * extended attributes.
3452 * Cgroupfs is special
3454 final
= &smack_known_star
;
3456 case DEVPTS_SUPER_MAGIC
:
3458 * devpts seems content with the label of the task.
3459 * Programs that change smack have to treat the
3464 case PROC_SUPER_MAGIC
:
3466 * Casey says procfs appears not to care.
3467 * The superblock default suffices.
3472 * Device labels should come from the filesystem,
3473 * but watch out, because they're volitile,
3474 * getting recreated on every reboot.
3476 final
= &smack_known_star
;
3480 * If a smack value has been set we want to use it,
3481 * but since tmpfs isn't giving us the opportunity
3482 * to set mount options simulate setting the
3483 * superblock default.
3487 * This isn't an understood special case.
3488 * Get the value from the xattr.
3492 * UNIX domain sockets use lower level socket data.
3494 if (S_ISSOCK(inode
->i_mode
)) {
3495 final
= &smack_known_star
;
3499 * No xattr support means, alas, no SMACK label.
3500 * Use the aforeapplied default.
3501 * It would be curious if the label of the task
3502 * does not match that assigned.
3504 if (!(inode
->i_opflags
& IOP_XATTR
))
3507 * Get the dentry for xattr.
3509 dp
= dget(opt_dentry
);
3510 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
3511 if (!IS_ERR_OR_NULL(skp
))
3515 * Transmuting directory
3517 if (S_ISDIR(inode
->i_mode
)) {
3519 * If this is a new directory and the label was
3520 * transmuted when the inode was initialized
3521 * set the transmute attribute on the directory
3522 * and mark the inode.
3524 * If there is a transmute attribute on the
3525 * directory mark the inode.
3527 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
3528 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
3529 rc
= __vfs_setxattr(dp
, inode
,
3530 XATTR_NAME_SMACKTRANSMUTE
,
3531 TRANS_TRUE
, TRANS_TRUE_SIZE
,
3534 rc
= __vfs_getxattr(dp
, inode
,
3535 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
3537 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
3538 TRANS_TRUE_SIZE
) != 0)
3542 transflag
= SMK_INODE_TRANSMUTE
;
3545 * Don't let the exec or mmap label be "*" or "@".
3547 skp
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
3548 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3549 skp
== &smack_known_web
)
3551 isp
->smk_task
= skp
;
3553 skp
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
3554 if (IS_ERR(skp
) || skp
== &smack_known_star
||
3555 skp
== &smack_known_web
)
3557 isp
->smk_mmap
= skp
;
3564 isp
->smk_inode
= ckp
;
3566 isp
->smk_inode
= final
;
3568 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
3571 mutex_unlock(&isp
->smk_lock
);
3576 * smack_getprocattr - Smack process attribute access
3577 * @p: the object task
3578 * @name: the name of the attribute in /proc/.../attr
3579 * @value: where to put the result
3581 * Places a copy of the task Smack into value
3583 * Returns the length of the smack label or an error code
3585 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
3587 struct smack_known
*skp
= smk_of_task_struct(p
);
3591 if (strcmp(name
, "current") != 0)
3594 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
3604 * smack_setprocattr - Smack process attribute setting
3605 * @name: the name of the attribute in /proc/.../attr
3606 * @value: the value to set
3607 * @size: the size of the value
3609 * Sets the Smack value of the task. Only setting self
3610 * is permitted and only with privilege
3612 * Returns the length of the smack label or an error code
3614 static int smack_setprocattr(const char *name
, void *value
, size_t size
)
3616 struct task_smack
*tsp
= current_security();
3618 struct smack_known
*skp
;
3619 struct smack_known_list_elem
*sklep
;
3622 if (!smack_privileged(CAP_MAC_ADMIN
) && list_empty(&tsp
->smk_relabel
))
3625 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
3628 if (strcmp(name
, "current") != 0)
3631 skp
= smk_import_entry(value
, size
);
3633 return PTR_ERR(skp
);
3636 * No process is ever allowed the web ("@") label
3637 * and the star ("*") label.
3639 if (skp
== &smack_known_web
|| skp
== &smack_known_star
)
3642 if (!smack_privileged(CAP_MAC_ADMIN
)) {
3644 list_for_each_entry(sklep
, &tsp
->smk_relabel
, list
)
3645 if (sklep
->smk_label
== skp
) {
3653 new = prepare_creds();
3657 tsp
= new->security
;
3658 tsp
->smk_task
= skp
;
3660 * process can change its label only once
3662 smk_destroy_label_list(&tsp
->smk_relabel
);
3669 * smack_unix_stream_connect - Smack access on UDS
3671 * @other: the other sock
3674 * Return 0 if a subject with the smack of sock could access
3675 * an object with the smack of other, otherwise an error code
3677 static int smack_unix_stream_connect(struct sock
*sock
,
3678 struct sock
*other
, struct sock
*newsk
)
3680 struct smack_known
*skp
;
3681 struct smack_known
*okp
;
3682 struct socket_smack
*ssp
= sock
->sk_security
;
3683 struct socket_smack
*osp
= other
->sk_security
;
3684 struct socket_smack
*nsp
= newsk
->sk_security
;
3685 struct smk_audit_info ad
;
3688 struct lsm_network_audit net
;
3691 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
3695 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3696 smk_ad_setfield_u_net_sk(&ad
, other
);
3698 rc
= smk_access(skp
, okp
, MAY_WRITE
, &ad
);
3699 rc
= smk_bu_note("UDS connect", skp
, okp
, MAY_WRITE
, rc
);
3703 rc
= smk_access(okp
, skp
, MAY_WRITE
, &ad
);
3704 rc
= smk_bu_note("UDS connect", okp
, skp
,
3710 * Cross reference the peer labels for SO_PEERSEC.
3713 nsp
->smk_packet
= ssp
->smk_out
;
3714 ssp
->smk_packet
= osp
->smk_out
;
3721 * smack_unix_may_send - Smack access on UDS
3723 * @other: the other socket
3725 * Return 0 if a subject with the smack of sock could access
3726 * an object with the smack of other, otherwise an error code
3728 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
3730 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3731 struct socket_smack
*osp
= other
->sk
->sk_security
;
3732 struct smk_audit_info ad
;
3736 struct lsm_network_audit net
;
3738 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3739 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3742 if (smack_privileged(CAP_MAC_OVERRIDE
))
3745 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
3746 rc
= smk_bu_note("UDS send", ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, rc
);
3751 * smack_socket_sendmsg - Smack check based on destination host
3754 * @size: the size of the message
3756 * Return 0 if the current subject can write to the destination host.
3757 * For IPv4 this is only a question if the destination is a single label host.
3758 * For IPv6 this is a check against the label of the port.
3760 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3763 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3764 #if IS_ENABLED(CONFIG_IPV6)
3765 struct sockaddr_in6
*sap
= (struct sockaddr_in6
*) msg
->msg_name
;
3767 #ifdef SMACK_IPV6_SECMARK_LABELING
3768 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
3769 struct smack_known
*rsp
;
3774 * Perfectly reasonable for this to be NULL
3779 switch (sock
->sk
->sk_family
) {
3781 rc
= smack_netlabel_send(sock
->sk
, sip
);
3784 #ifdef SMACK_IPV6_SECMARK_LABELING
3785 rsp
= smack_ipv6host_label(sap
);
3787 rc
= smk_ipv6_check(ssp
->smk_out
, rsp
, sap
,
3790 #ifdef SMACK_IPV6_PORT_LABELING
3791 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3799 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3800 * @sap: netlabel secattr
3801 * @ssp: socket security information
3803 * Returns a pointer to a Smack label entry found on the label list.
3805 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3806 struct socket_smack
*ssp
)
3808 struct smack_known
*skp
;
3813 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3815 * Looks like a CIPSO packet.
3816 * If there are flags but no level netlabel isn't
3817 * behaving the way we expect it to.
3819 * Look it up in the label table
3820 * Without guidance regarding the smack value
3821 * for the packet fall back on the network
3825 list_for_each_entry_rcu(skp
, &smack_known_list
, list
) {
3826 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3829 * Compare the catsets. Use the netlbl APIs.
3831 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) == 0) {
3832 if ((skp
->smk_netlabel
.flags
&
3833 NETLBL_SECATTR_MLS_CAT
) == 0)
3837 for (acat
= -1, kcat
= -1; acat
== kcat
; ) {
3838 acat
= netlbl_catmap_walk(sap
->attr
.mls
.cat
,
3840 kcat
= netlbl_catmap_walk(
3841 skp
->smk_netlabel
.attr
.mls
.cat
,
3843 if (acat
< 0 || kcat
< 0)
3856 if (ssp
!= NULL
&& ssp
->smk_in
== &smack_known_star
)
3857 return &smack_known_web
;
3858 return &smack_known_star
;
3860 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0)
3862 * Looks like a fallback, which gives us a secid.
3864 return smack_from_secid(sap
->attr
.secid
);
3866 * Without guidance regarding the smack value
3867 * for the packet fall back on the network
3870 return smack_net_ambient
;
3873 #if IS_ENABLED(CONFIG_IPV6)
3874 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr_in6
*sip
)
3878 int proto
= -EINVAL
;
3879 struct ipv6hdr _ipv6h
;
3880 struct ipv6hdr
*ip6
;
3882 struct tcphdr _tcph
, *th
;
3883 struct udphdr _udph
, *uh
;
3884 struct dccp_hdr _dccph
, *dh
;
3888 offset
= skb_network_offset(skb
);
3889 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3892 sip
->sin6_addr
= ip6
->saddr
;
3894 nexthdr
= ip6
->nexthdr
;
3895 offset
+= sizeof(_ipv6h
);
3896 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3903 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3905 sip
->sin6_port
= th
->source
;
3908 case IPPROTO_UDPLITE
:
3909 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3911 sip
->sin6_port
= uh
->source
;
3914 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3916 sip
->sin6_port
= dh
->dccph_sport
;
3921 #endif /* CONFIG_IPV6 */
3924 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3928 * Returns 0 if the packet should be delivered, an error code otherwise
3930 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3932 struct netlbl_lsm_secattr secattr
;
3933 struct socket_smack
*ssp
= sk
->sk_security
;
3934 struct smack_known
*skp
= NULL
;
3936 struct smk_audit_info ad
;
3937 u16 family
= sk
->sk_family
;
3939 struct lsm_network_audit net
;
3941 #if IS_ENABLED(CONFIG_IPV6)
3942 struct sockaddr_in6 sadd
;
3945 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3947 #endif /* CONFIG_IPV6 */
3951 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3953 * If there is a secmark use it rather than the CIPSO label.
3954 * If there is no secmark fall back to CIPSO.
3955 * The secmark is assumed to reflect policy better.
3957 if (skb
&& skb
->secmark
!= 0) {
3958 skp
= smack_from_secid(skb
->secmark
);
3961 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3963 * Translate what netlabel gave us.
3965 netlbl_secattr_init(&secattr
);
3967 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3969 skp
= smack_from_secattr(&secattr
, ssp
);
3971 skp
= smack_net_ambient
;
3973 netlbl_secattr_destroy(&secattr
);
3975 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3979 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3980 ad
.a
.u
.net
->family
= family
;
3981 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3982 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3985 * Receiving a packet requires that the other end
3986 * be able to write here. Read access is not required.
3987 * This is the simplist possible security model
3990 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3991 rc
= smk_bu_note("IPv4 delivery", skp
, ssp
->smk_in
,
3994 netlbl_skbuff_err(skb
, family
, rc
, 0);
3996 #if IS_ENABLED(CONFIG_IPV6)
3998 proto
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3999 if (proto
!= IPPROTO_UDP
&& proto
!= IPPROTO_UDPLITE
&&
4000 proto
!= IPPROTO_TCP
&& proto
!= IPPROTO_DCCP
)
4002 #ifdef SMACK_IPV6_SECMARK_LABELING
4003 if (skb
&& skb
->secmark
!= 0)
4004 skp
= smack_from_secid(skb
->secmark
);
4006 skp
= smack_ipv6host_label(&sadd
);
4008 skp
= smack_net_ambient
;
4012 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4013 ad
.a
.u
.net
->family
= family
;
4014 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4015 ipv6_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4016 #endif /* CONFIG_AUDIT */
4017 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4018 rc
= smk_bu_note("IPv6 delivery", skp
, ssp
->smk_in
,
4020 #endif /* SMACK_IPV6_SECMARK_LABELING */
4021 #ifdef SMACK_IPV6_PORT_LABELING
4022 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
4023 #endif /* SMACK_IPV6_PORT_LABELING */
4025 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
,
4026 ICMPV6_ADM_PROHIBITED
, 0);
4028 #endif /* CONFIG_IPV6 */
4035 * smack_socket_getpeersec_stream - pull in packet label
4037 * @optval: user's destination
4038 * @optlen: size thereof
4041 * returns zero on success, an error code otherwise
4043 static int smack_socket_getpeersec_stream(struct socket
*sock
,
4044 char __user
*optval
,
4045 int __user
*optlen
, unsigned len
)
4047 struct socket_smack
*ssp
;
4052 ssp
= sock
->sk
->sk_security
;
4053 if (ssp
->smk_packet
!= NULL
) {
4054 rcp
= ssp
->smk_packet
->smk_known
;
4055 slen
= strlen(rcp
) + 1;
4060 else if (copy_to_user(optval
, rcp
, slen
) != 0)
4063 if (put_user(slen
, optlen
) != 0)
4071 * smack_socket_getpeersec_dgram - pull in packet label
4072 * @sock: the peer socket
4074 * @secid: pointer to where to put the secid of the packet
4076 * Sets the netlabel socket state on sk from parent
4078 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
4079 struct sk_buff
*skb
, u32
*secid
)
4082 struct netlbl_lsm_secattr secattr
;
4083 struct socket_smack
*ssp
= NULL
;
4084 struct smack_known
*skp
;
4085 int family
= PF_UNSPEC
;
4086 u32 s
= 0; /* 0 is the invalid secid */
4090 if (skb
->protocol
== htons(ETH_P_IP
))
4092 #if IS_ENABLED(CONFIG_IPV6)
4093 else if (skb
->protocol
== htons(ETH_P_IPV6
))
4095 #endif /* CONFIG_IPV6 */
4097 if (family
== PF_UNSPEC
&& sock
!= NULL
)
4098 family
= sock
->sk
->sk_family
;
4102 ssp
= sock
->sk
->sk_security
;
4103 s
= ssp
->smk_out
->smk_secid
;
4106 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4112 * Translate what netlabel gave us.
4114 if (sock
!= NULL
&& sock
->sk
!= NULL
)
4115 ssp
= sock
->sk
->sk_security
;
4116 netlbl_secattr_init(&secattr
);
4117 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4119 skp
= smack_from_secattr(&secattr
, ssp
);
4122 netlbl_secattr_destroy(&secattr
);
4125 #ifdef SMACK_IPV6_SECMARK_LABELING
4137 * smack_sock_graft - Initialize a newly created socket with an existing sock
4139 * @parent: parent socket
4141 * Set the smk_{in,out} state of an existing sock based on the process that
4142 * is creating the new socket.
4144 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
4146 struct socket_smack
*ssp
;
4147 struct smack_known
*skp
= smk_of_current();
4150 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
4153 ssp
= sk
->sk_security
;
4156 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4160 * smack_inet_conn_request - Smack access check on connect
4161 * @sk: socket involved
4165 * Returns 0 if a task with the packet label could write to
4166 * the socket, otherwise an error code
4168 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4169 struct request_sock
*req
)
4171 u16 family
= sk
->sk_family
;
4172 struct smack_known
*skp
;
4173 struct socket_smack
*ssp
= sk
->sk_security
;
4174 struct netlbl_lsm_secattr secattr
;
4175 struct sockaddr_in addr
;
4177 struct smack_known
*hskp
;
4179 struct smk_audit_info ad
;
4181 struct lsm_network_audit net
;
4184 #if IS_ENABLED(CONFIG_IPV6)
4185 if (family
== PF_INET6
) {
4187 * Handle mapped IPv4 packets arriving
4188 * via IPv6 sockets. Don't set up netlabel
4189 * processing on IPv6.
4191 if (skb
->protocol
== htons(ETH_P_IP
))
4196 #endif /* CONFIG_IPV6 */
4198 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4200 * If there is a secmark use it rather than the CIPSO label.
4201 * If there is no secmark fall back to CIPSO.
4202 * The secmark is assumed to reflect policy better.
4204 if (skb
&& skb
->secmark
!= 0) {
4205 skp
= smack_from_secid(skb
->secmark
);
4208 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4210 netlbl_secattr_init(&secattr
);
4211 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
4213 skp
= smack_from_secattr(&secattr
, ssp
);
4215 skp
= &smack_known_huh
;
4216 netlbl_secattr_destroy(&secattr
);
4218 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4223 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
4224 ad
.a
.u
.net
->family
= family
;
4225 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
4226 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
4229 * Receiving a packet requires that the other end be able to write
4230 * here. Read access is not required.
4232 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
4233 rc
= smk_bu_note("IPv4 connect", skp
, ssp
->smk_in
, MAY_WRITE
, rc
);
4238 * Save the peer's label in the request_sock so we can later setup
4239 * smk_packet in the child socket so that SO_PEERCRED can report it.
4241 req
->peer_secid
= skp
->smk_secid
;
4244 * We need to decide if we want to label the incoming connection here
4245 * if we do we only need to label the request_sock and the stack will
4246 * propagate the wire-label to the sock when it is created.
4249 addr
.sin_addr
.s_addr
= hdr
->saddr
;
4251 hskp
= smack_ipv4host_label(&addr
);
4255 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
4257 netlbl_req_delattr(req
);
4263 * smack_inet_csk_clone - Copy the connection information to the new socket
4264 * @sk: the new socket
4265 * @req: the connection's request_sock
4267 * Transfer the connection's peer label to the newly created socket.
4269 static void smack_inet_csk_clone(struct sock
*sk
,
4270 const struct request_sock
*req
)
4272 struct socket_smack
*ssp
= sk
->sk_security
;
4273 struct smack_known
*skp
;
4275 if (req
->peer_secid
!= 0) {
4276 skp
= smack_from_secid(req
->peer_secid
);
4277 ssp
->smk_packet
= skp
;
4279 ssp
->smk_packet
= NULL
;
4283 * Key management security hooks
4285 * Casey has not tested key support very heavily.
4286 * The permission check is most likely too restrictive.
4287 * If you care about keys please have a look.
4292 * smack_key_alloc - Set the key security blob
4294 * @cred: the credentials to use
4297 * No allocation required
4301 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
4302 unsigned long flags
)
4304 struct smack_known
*skp
= smk_of_task(cred
->security
);
4306 key
->security
= skp
;
4311 * smack_key_free - Clear the key security blob
4314 * Clear the blob pointer
4316 static void smack_key_free(struct key
*key
)
4318 key
->security
= NULL
;
4322 * smack_key_permission - Smack access on a key
4323 * @key_ref: gets to the object
4324 * @cred: the credentials to use
4325 * @perm: requested key permissions
4327 * Return 0 if the task has read and write to the object,
4328 * an error code otherwise
4330 static int smack_key_permission(key_ref_t key_ref
,
4331 const struct cred
*cred
, unsigned perm
)
4334 struct smk_audit_info ad
;
4335 struct smack_known
*tkp
= smk_of_task(cred
->security
);
4340 * Validate requested permissions
4342 if (perm
& ~KEY_NEED_ALL
)
4345 keyp
= key_ref_to_ptr(key_ref
);
4349 * If the key hasn't been initialized give it access so that
4352 if (keyp
->security
== NULL
)
4355 * This should not occur
4360 if (smack_privileged_cred(CAP_MAC_OVERRIDE
, cred
))
4364 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
4365 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
4366 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
4368 if (perm
& (KEY_NEED_READ
| KEY_NEED_SEARCH
| KEY_NEED_VIEW
))
4369 request
|= MAY_READ
;
4370 if (perm
& (KEY_NEED_WRITE
| KEY_NEED_LINK
| KEY_NEED_SETATTR
))
4371 request
|= MAY_WRITE
;
4372 rc
= smk_access(tkp
, keyp
->security
, request
, &ad
);
4373 rc
= smk_bu_note("key access", tkp
, keyp
->security
, request
, rc
);
4378 * smack_key_getsecurity - Smack label tagging the key
4379 * @key points to the key to be queried
4380 * @_buffer points to a pointer that should be set to point to the
4381 * resulting string (if no label or an error occurs).
4382 * Return the length of the string (including terminating NUL) or -ve if
4384 * May also return 0 (and a NULL buffer pointer) if there is no label.
4386 static int smack_key_getsecurity(struct key
*key
, char **_buffer
)
4388 struct smack_known
*skp
= key
->security
;
4392 if (key
->security
== NULL
) {
4397 copy
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
4400 length
= strlen(copy
) + 1;
4406 #endif /* CONFIG_KEYS */
4411 * Audit requires a unique representation of each Smack specific
4412 * rule. This unique representation is used to distinguish the
4413 * object to be audited from remaining kernel objects and also
4414 * works as a glue between the audit hooks.
4416 * Since repository entries are added but never deleted, we'll use
4417 * the smack_known label address related to the given audit rule as
4418 * the needed unique representation. This also better fits the smack
4419 * model where nearly everything is a label.
4424 * smack_audit_rule_init - Initialize a smack audit rule
4425 * @field: audit rule fields given from user-space (audit.h)
4426 * @op: required testing operator (=, !=, >, <, ...)
4427 * @rulestr: smack label to be audited
4428 * @vrule: pointer to save our own audit rule representation
4430 * Prepare to audit cases where (@field @op @rulestr) is true.
4431 * The label to be audited is created if necessay.
4433 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
4435 struct smack_known
*skp
;
4436 char **rule
= (char **)vrule
;
4439 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4442 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
4445 skp
= smk_import_entry(rulestr
, 0);
4447 return PTR_ERR(skp
);
4449 *rule
= skp
->smk_known
;
4455 * smack_audit_rule_known - Distinguish Smack audit rules
4456 * @krule: rule of interest, in Audit kernel representation format
4458 * This is used to filter Smack rules from remaining Audit ones.
4459 * If it's proved that this rule belongs to us, the
4460 * audit_rule_match hook will be called to do the final judgement.
4462 static int smack_audit_rule_known(struct audit_krule
*krule
)
4464 struct audit_field
*f
;
4467 for (i
= 0; i
< krule
->field_count
; i
++) {
4468 f
= &krule
->fields
[i
];
4470 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
4478 * smack_audit_rule_match - Audit given object ?
4479 * @secid: security id for identifying the object to test
4480 * @field: audit rule flags given from user-space
4481 * @op: required testing operator
4482 * @vrule: smack internal rule presentation
4483 * @actx: audit context associated with the check
4485 * The core Audit hook. It's used to take the decision of
4486 * whether to audit or not to audit a given object.
4488 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
4489 struct audit_context
*actx
)
4491 struct smack_known
*skp
;
4494 if (unlikely(!rule
)) {
4495 WARN_ONCE(1, "Smack: missing rule\n");
4499 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
4502 skp
= smack_from_secid(secid
);
4505 * No need to do string comparisons. If a match occurs,
4506 * both pointers will point to the same smack_known
4509 if (op
== Audit_equal
)
4510 return (rule
== skp
->smk_known
);
4511 if (op
== Audit_not_equal
)
4512 return (rule
!= skp
->smk_known
);
4518 * There is no need for a smack_audit_rule_free hook.
4519 * No memory was allocated.
4522 #endif /* CONFIG_AUDIT */
4525 * smack_ismaclabel - check if xattr @name references a smack MAC label
4526 * @name: Full xattr name to check.
4528 static int smack_ismaclabel(const char *name
)
4530 return (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0);
4535 * smack_secid_to_secctx - return the smack label for a secid
4536 * @secid: incoming integer
4537 * @secdata: destination
4538 * @seclen: how long it is
4540 * Exists for networking code.
4542 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
4544 struct smack_known
*skp
= smack_from_secid(secid
);
4547 *secdata
= skp
->smk_known
;
4548 *seclen
= strlen(skp
->smk_known
);
4553 * smack_secctx_to_secid - return the secid for a smack label
4554 * @secdata: smack label
4555 * @seclen: how long result is
4556 * @secid: outgoing integer
4558 * Exists for audit and networking code.
4560 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
4562 struct smack_known
*skp
= smk_find_entry(secdata
);
4565 *secid
= skp
->smk_secid
;
4572 * There used to be a smack_release_secctx hook
4573 * that did nothing back when hooks were in a vector.
4574 * Now that there's a list such a hook adds cost.
4577 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
4579 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
4582 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
4584 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
4587 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
4589 struct smack_known
*skp
= smk_of_inode(inode
);
4591 *ctx
= skp
->smk_known
;
4592 *ctxlen
= strlen(skp
->smk_known
);
4596 static int smack_inode_copy_up(struct dentry
*dentry
, struct cred
**new)
4599 struct task_smack
*tsp
;
4600 struct smack_known
*skp
;
4601 struct inode_smack
*isp
;
4602 struct cred
*new_creds
= *new;
4604 if (new_creds
== NULL
) {
4605 new_creds
= prepare_creds();
4606 if (new_creds
== NULL
)
4610 tsp
= new_creds
->security
;
4613 * Get label from overlay inode and set it in create_sid
4615 isp
= d_inode(dentry
->d_parent
)->i_security
;
4616 skp
= isp
->smk_inode
;
4617 tsp
->smk_task
= skp
;
4622 static int smack_inode_copy_up_xattr(const char *name
)
4625 * Return 1 if this is the smack access Smack attribute.
4627 if (strcmp(name
, XATTR_NAME_SMACK
) == 0)
4633 static int smack_dentry_create_files_as(struct dentry
*dentry
, int mode
,
4635 const struct cred
*old
,
4638 struct task_smack
*otsp
= old
->security
;
4639 struct task_smack
*ntsp
= new->security
;
4640 struct inode_smack
*isp
;
4644 * Use the process credential unless all of
4645 * the transmuting criteria are met
4647 ntsp
->smk_task
= otsp
->smk_task
;
4650 * the attribute of the containing directory
4652 isp
= d_inode(dentry
->d_parent
)->i_security
;
4654 if (isp
->smk_flags
& SMK_INODE_TRANSMUTE
) {
4656 may
= smk_access_entry(otsp
->smk_task
->smk_known
,
4657 isp
->smk_inode
->smk_known
,
4658 &otsp
->smk_task
->smk_rules
);
4662 * If the directory is transmuting and the rule
4663 * providing access is transmuting use the containing
4664 * directory label instead of the process label.
4666 if (may
> 0 && (may
& MAY_TRANSMUTE
))
4667 ntsp
->smk_task
= isp
->smk_inode
;
4672 static struct security_hook_list smack_hooks
[] __lsm_ro_after_init
= {
4673 LSM_HOOK_INIT(ptrace_access_check
, smack_ptrace_access_check
),
4674 LSM_HOOK_INIT(ptrace_traceme
, smack_ptrace_traceme
),
4675 LSM_HOOK_INIT(syslog
, smack_syslog
),
4677 LSM_HOOK_INIT(sb_alloc_security
, smack_sb_alloc_security
),
4678 LSM_HOOK_INIT(sb_free_security
, smack_sb_free_security
),
4679 LSM_HOOK_INIT(sb_copy_data
, smack_sb_copy_data
),
4680 LSM_HOOK_INIT(sb_kern_mount
, smack_sb_kern_mount
),
4681 LSM_HOOK_INIT(sb_statfs
, smack_sb_statfs
),
4682 LSM_HOOK_INIT(sb_set_mnt_opts
, smack_set_mnt_opts
),
4683 LSM_HOOK_INIT(sb_parse_opts_str
, smack_parse_opts_str
),
4685 LSM_HOOK_INIT(bprm_set_creds
, smack_bprm_set_creds
),
4687 LSM_HOOK_INIT(inode_alloc_security
, smack_inode_alloc_security
),
4688 LSM_HOOK_INIT(inode_free_security
, smack_inode_free_security
),
4689 LSM_HOOK_INIT(inode_init_security
, smack_inode_init_security
),
4690 LSM_HOOK_INIT(inode_link
, smack_inode_link
),
4691 LSM_HOOK_INIT(inode_unlink
, smack_inode_unlink
),
4692 LSM_HOOK_INIT(inode_rmdir
, smack_inode_rmdir
),
4693 LSM_HOOK_INIT(inode_rename
, smack_inode_rename
),
4694 LSM_HOOK_INIT(inode_permission
, smack_inode_permission
),
4695 LSM_HOOK_INIT(inode_setattr
, smack_inode_setattr
),
4696 LSM_HOOK_INIT(inode_getattr
, smack_inode_getattr
),
4697 LSM_HOOK_INIT(inode_setxattr
, smack_inode_setxattr
),
4698 LSM_HOOK_INIT(inode_post_setxattr
, smack_inode_post_setxattr
),
4699 LSM_HOOK_INIT(inode_getxattr
, smack_inode_getxattr
),
4700 LSM_HOOK_INIT(inode_removexattr
, smack_inode_removexattr
),
4701 LSM_HOOK_INIT(inode_getsecurity
, smack_inode_getsecurity
),
4702 LSM_HOOK_INIT(inode_setsecurity
, smack_inode_setsecurity
),
4703 LSM_HOOK_INIT(inode_listsecurity
, smack_inode_listsecurity
),
4704 LSM_HOOK_INIT(inode_getsecid
, smack_inode_getsecid
),
4706 LSM_HOOK_INIT(file_alloc_security
, smack_file_alloc_security
),
4707 LSM_HOOK_INIT(file_free_security
, smack_file_free_security
),
4708 LSM_HOOK_INIT(file_ioctl
, smack_file_ioctl
),
4709 LSM_HOOK_INIT(file_lock
, smack_file_lock
),
4710 LSM_HOOK_INIT(file_fcntl
, smack_file_fcntl
),
4711 LSM_HOOK_INIT(mmap_file
, smack_mmap_file
),
4712 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
4713 LSM_HOOK_INIT(file_set_fowner
, smack_file_set_fowner
),
4714 LSM_HOOK_INIT(file_send_sigiotask
, smack_file_send_sigiotask
),
4715 LSM_HOOK_INIT(file_receive
, smack_file_receive
),
4717 LSM_HOOK_INIT(file_open
, smack_file_open
),
4719 LSM_HOOK_INIT(cred_alloc_blank
, smack_cred_alloc_blank
),
4720 LSM_HOOK_INIT(cred_free
, smack_cred_free
),
4721 LSM_HOOK_INIT(cred_prepare
, smack_cred_prepare
),
4722 LSM_HOOK_INIT(cred_transfer
, smack_cred_transfer
),
4723 LSM_HOOK_INIT(cred_getsecid
, smack_cred_getsecid
),
4724 LSM_HOOK_INIT(kernel_act_as
, smack_kernel_act_as
),
4725 LSM_HOOK_INIT(kernel_create_files_as
, smack_kernel_create_files_as
),
4726 LSM_HOOK_INIT(task_setpgid
, smack_task_setpgid
),
4727 LSM_HOOK_INIT(task_getpgid
, smack_task_getpgid
),
4728 LSM_HOOK_INIT(task_getsid
, smack_task_getsid
),
4729 LSM_HOOK_INIT(task_getsecid
, smack_task_getsecid
),
4730 LSM_HOOK_INIT(task_setnice
, smack_task_setnice
),
4731 LSM_HOOK_INIT(task_setioprio
, smack_task_setioprio
),
4732 LSM_HOOK_INIT(task_getioprio
, smack_task_getioprio
),
4733 LSM_HOOK_INIT(task_setscheduler
, smack_task_setscheduler
),
4734 LSM_HOOK_INIT(task_getscheduler
, smack_task_getscheduler
),
4735 LSM_HOOK_INIT(task_movememory
, smack_task_movememory
),
4736 LSM_HOOK_INIT(task_kill
, smack_task_kill
),
4737 LSM_HOOK_INIT(task_to_inode
, smack_task_to_inode
),
4739 LSM_HOOK_INIT(ipc_permission
, smack_ipc_permission
),
4740 LSM_HOOK_INIT(ipc_getsecid
, smack_ipc_getsecid
),
4742 LSM_HOOK_INIT(msg_msg_alloc_security
, smack_msg_msg_alloc_security
),
4743 LSM_HOOK_INIT(msg_msg_free_security
, smack_msg_msg_free_security
),
4745 LSM_HOOK_INIT(msg_queue_alloc_security
, smack_ipc_alloc_security
),
4746 LSM_HOOK_INIT(msg_queue_free_security
, smack_ipc_free_security
),
4747 LSM_HOOK_INIT(msg_queue_associate
, smack_msg_queue_associate
),
4748 LSM_HOOK_INIT(msg_queue_msgctl
, smack_msg_queue_msgctl
),
4749 LSM_HOOK_INIT(msg_queue_msgsnd
, smack_msg_queue_msgsnd
),
4750 LSM_HOOK_INIT(msg_queue_msgrcv
, smack_msg_queue_msgrcv
),
4752 LSM_HOOK_INIT(shm_alloc_security
, smack_ipc_alloc_security
),
4753 LSM_HOOK_INIT(shm_free_security
, smack_ipc_free_security
),
4754 LSM_HOOK_INIT(shm_associate
, smack_shm_associate
),
4755 LSM_HOOK_INIT(shm_shmctl
, smack_shm_shmctl
),
4756 LSM_HOOK_INIT(shm_shmat
, smack_shm_shmat
),
4758 LSM_HOOK_INIT(sem_alloc_security
, smack_ipc_alloc_security
),
4759 LSM_HOOK_INIT(sem_free_security
, smack_ipc_free_security
),
4760 LSM_HOOK_INIT(sem_associate
, smack_sem_associate
),
4761 LSM_HOOK_INIT(sem_semctl
, smack_sem_semctl
),
4762 LSM_HOOK_INIT(sem_semop
, smack_sem_semop
),
4764 LSM_HOOK_INIT(d_instantiate
, smack_d_instantiate
),
4766 LSM_HOOK_INIT(getprocattr
, smack_getprocattr
),
4767 LSM_HOOK_INIT(setprocattr
, smack_setprocattr
),
4769 LSM_HOOK_INIT(unix_stream_connect
, smack_unix_stream_connect
),
4770 LSM_HOOK_INIT(unix_may_send
, smack_unix_may_send
),
4772 LSM_HOOK_INIT(socket_post_create
, smack_socket_post_create
),
4773 LSM_HOOK_INIT(socket_socketpair
, smack_socket_socketpair
),
4774 #ifdef SMACK_IPV6_PORT_LABELING
4775 LSM_HOOK_INIT(socket_bind
, smack_socket_bind
),
4777 LSM_HOOK_INIT(socket_connect
, smack_socket_connect
),
4778 LSM_HOOK_INIT(socket_sendmsg
, smack_socket_sendmsg
),
4779 LSM_HOOK_INIT(socket_sock_rcv_skb
, smack_socket_sock_rcv_skb
),
4780 LSM_HOOK_INIT(socket_getpeersec_stream
, smack_socket_getpeersec_stream
),
4781 LSM_HOOK_INIT(socket_getpeersec_dgram
, smack_socket_getpeersec_dgram
),
4782 LSM_HOOK_INIT(sk_alloc_security
, smack_sk_alloc_security
),
4783 LSM_HOOK_INIT(sk_free_security
, smack_sk_free_security
),
4784 LSM_HOOK_INIT(sock_graft
, smack_sock_graft
),
4785 LSM_HOOK_INIT(inet_conn_request
, smack_inet_conn_request
),
4786 LSM_HOOK_INIT(inet_csk_clone
, smack_inet_csk_clone
),
4788 /* key management security hooks */
4790 LSM_HOOK_INIT(key_alloc
, smack_key_alloc
),
4791 LSM_HOOK_INIT(key_free
, smack_key_free
),
4792 LSM_HOOK_INIT(key_permission
, smack_key_permission
),
4793 LSM_HOOK_INIT(key_getsecurity
, smack_key_getsecurity
),
4794 #endif /* CONFIG_KEYS */
4798 LSM_HOOK_INIT(audit_rule_init
, smack_audit_rule_init
),
4799 LSM_HOOK_INIT(audit_rule_known
, smack_audit_rule_known
),
4800 LSM_HOOK_INIT(audit_rule_match
, smack_audit_rule_match
),
4801 #endif /* CONFIG_AUDIT */
4803 LSM_HOOK_INIT(ismaclabel
, smack_ismaclabel
),
4804 LSM_HOOK_INIT(secid_to_secctx
, smack_secid_to_secctx
),
4805 LSM_HOOK_INIT(secctx_to_secid
, smack_secctx_to_secid
),
4806 LSM_HOOK_INIT(inode_notifysecctx
, smack_inode_notifysecctx
),
4807 LSM_HOOK_INIT(inode_setsecctx
, smack_inode_setsecctx
),
4808 LSM_HOOK_INIT(inode_getsecctx
, smack_inode_getsecctx
),
4809 LSM_HOOK_INIT(inode_copy_up
, smack_inode_copy_up
),
4810 LSM_HOOK_INIT(inode_copy_up_xattr
, smack_inode_copy_up_xattr
),
4811 LSM_HOOK_INIT(dentry_create_files_as
, smack_dentry_create_files_as
),
4815 static __init
void init_smack_known_list(void)
4818 * Initialize rule list locks
4820 mutex_init(&smack_known_huh
.smk_rules_lock
);
4821 mutex_init(&smack_known_hat
.smk_rules_lock
);
4822 mutex_init(&smack_known_floor
.smk_rules_lock
);
4823 mutex_init(&smack_known_star
.smk_rules_lock
);
4824 mutex_init(&smack_known_web
.smk_rules_lock
);
4826 * Initialize rule lists
4828 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
4829 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
4830 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
4831 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
4832 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
4834 * Create the known labels list
4836 smk_insert_entry(&smack_known_huh
);
4837 smk_insert_entry(&smack_known_hat
);
4838 smk_insert_entry(&smack_known_star
);
4839 smk_insert_entry(&smack_known_floor
);
4840 smk_insert_entry(&smack_known_web
);
4844 * smack_init - initialize the smack system
4848 static __init
int smack_init(void)
4851 struct task_smack
*tsp
;
4853 if (!security_module_enable("smack"))
4856 smack_inode_cache
= KMEM_CACHE(inode_smack
, 0);
4857 if (!smack_inode_cache
)
4860 tsp
= new_task_smack(&smack_known_floor
, &smack_known_floor
,
4863 kmem_cache_destroy(smack_inode_cache
);
4869 pr_info("Smack: Initializing.\n");
4870 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4871 pr_info("Smack: Netfilter enabled.\n");
4873 #ifdef SMACK_IPV6_PORT_LABELING
4874 pr_info("Smack: IPv6 port labeling enabled.\n");
4876 #ifdef SMACK_IPV6_SECMARK_LABELING
4877 pr_info("Smack: IPv6 Netfilter enabled.\n");
4881 * Set the security state for the initial task.
4883 cred
= (struct cred
*) current
->cred
;
4884 cred
->security
= tsp
;
4886 /* initialize the smack_known_list */
4887 init_smack_known_list();
4892 security_add_hooks(smack_hooks
, ARRAY_SIZE(smack_hooks
), "smack");
4898 * Smack requires early initialization in order to label
4899 * all processes and objects when they are created.
4901 security_initcall(smack_init
);