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 <ext-jarkko.2.sakkinen@nokia.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.moore@hp.com>
13 * Copyright (C) 2010 Nokia Corporation
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2,
17 * as published by the Free Software Foundation.
20 #include <linux/xattr.h>
21 #include <linux/pagemap.h>
22 #include <linux/mount.h>
23 #include <linux/stat.h>
25 #include <asm/ioctls.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <linux/pipe_fs_i.h>
32 #include <net/netlabel.h>
33 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
39 #define task_security(task) (task_cred_xxx((task), security))
41 #define TRANS_TRUE "TRUE"
42 #define TRANS_TRUE_SIZE 4
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
52 static char *smk_fetch(const char *name
, struct inode
*ip
, struct dentry
*dp
)
55 char in
[SMK_LABELLEN
];
57 if (ip
->i_op
->getxattr
== NULL
)
60 rc
= ip
->i_op
->getxattr(dp
, name
, in
, SMK_LABELLEN
);
64 return smk_import(in
, rc
);
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
71 * Returns the new blob or NULL if there's no memory available
73 struct inode_smack
*new_inode_smack(char *smack
)
75 struct inode_smack
*isp
;
77 isp
= kzalloc(sizeof(struct inode_smack
), GFP_KERNEL
);
81 isp
->smk_inode
= smack
;
83 mutex_init(&isp
->smk_lock
);
89 * new_task_smack - allocate a task security blob
90 * @smack: a pointer to the Smack label to use in the blob
92 * Returns the new blob or NULL if there's no memory available
94 static struct task_smack
*new_task_smack(char *task
, char *forked
, gfp_t gfp
)
96 struct task_smack
*tsp
;
98 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
102 tsp
->smk_task
= task
;
103 tsp
->smk_forked
= forked
;
104 INIT_LIST_HEAD(&tsp
->smk_rules
);
105 mutex_init(&tsp
->smk_rules_lock
);
111 * smk_copy_rules - copy a rule set
112 * @nhead - new rules header pointer
113 * @ohead - old rules header pointer
115 * Returns 0 on success, -ENOMEM on error
117 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
120 struct smack_rule
*nrp
;
121 struct smack_rule
*orp
;
124 INIT_LIST_HEAD(nhead
);
126 list_for_each_entry_rcu(orp
, ohead
, list
) {
127 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
133 list_add_rcu(&nrp
->list
, nhead
);
140 * We he, that is fun!
144 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
145 * @ctp: child task pointer
146 * @mode: ptrace attachment mode
148 * Returns 0 if access is OK, an error code otherwise
150 * Do the capability checks, and require read and write.
152 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
155 struct smk_audit_info ad
;
158 rc
= cap_ptrace_access_check(ctp
, mode
);
162 tsp
= smk_of_task(task_security(ctp
));
163 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
164 smk_ad_setfield_u_tsk(&ad
, ctp
);
166 rc
= smk_curacc(tsp
, MAY_READWRITE
, &ad
);
171 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
172 * @ptp: parent task pointer
174 * Returns 0 if access is OK, an error code otherwise
176 * Do the capability checks, and require read and write.
178 static int smack_ptrace_traceme(struct task_struct
*ptp
)
181 struct smk_audit_info ad
;
184 rc
= cap_ptrace_traceme(ptp
);
188 tsp
= smk_of_task(task_security(ptp
));
189 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
190 smk_ad_setfield_u_tsk(&ad
, ptp
);
192 rc
= smk_curacc(tsp
, MAY_READWRITE
, &ad
);
197 * smack_syslog - Smack approval on syslog
198 * @type: message type
200 * Require that the task has the floor label
202 * Returns 0 on success, error code otherwise.
204 static int smack_syslog(int typefrom_file
)
207 char *sp
= smk_of_current();
209 if (capable(CAP_MAC_OVERRIDE
))
212 if (sp
!= smack_known_floor
.smk_known
)
224 * smack_sb_alloc_security - allocate a superblock blob
225 * @sb: the superblock getting the blob
227 * Returns 0 on success or -ENOMEM on error.
229 static int smack_sb_alloc_security(struct super_block
*sb
)
231 struct superblock_smack
*sbsp
;
233 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
238 sbsp
->smk_root
= smack_known_floor
.smk_known
;
239 sbsp
->smk_default
= smack_known_floor
.smk_known
;
240 sbsp
->smk_floor
= smack_known_floor
.smk_known
;
241 sbsp
->smk_hat
= smack_known_hat
.smk_known
;
242 sbsp
->smk_initialized
= 0;
243 spin_lock_init(&sbsp
->smk_sblock
);
245 sb
->s_security
= sbsp
;
251 * smack_sb_free_security - free a superblock blob
252 * @sb: the superblock getting the blob
255 static void smack_sb_free_security(struct super_block
*sb
)
257 kfree(sb
->s_security
);
258 sb
->s_security
= NULL
;
262 * smack_sb_copy_data - copy mount options data for processing
263 * @orig: where to start
264 * @smackopts: mount options string
266 * Returns 0 on success or -ENOMEM on error.
268 * Copy the Smack specific mount options out of the mount
271 static int smack_sb_copy_data(char *orig
, char *smackopts
)
273 char *cp
, *commap
, *otheropts
, *dp
;
275 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
276 if (otheropts
== NULL
)
279 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
280 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
282 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
284 else if (strstr(cp
, SMK_FSHAT
) == cp
)
286 else if (strstr(cp
, SMK_FSROOT
) == cp
)
291 commap
= strchr(cp
, ',');
300 strcpy(orig
, otheropts
);
301 free_page((unsigned long)otheropts
);
307 * smack_sb_kern_mount - Smack specific mount processing
308 * @sb: the file system superblock
309 * @flags: the mount flags
310 * @data: the smack mount options
312 * Returns 0 on success, an error code on failure
314 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
316 struct dentry
*root
= sb
->s_root
;
317 struct inode
*inode
= root
->d_inode
;
318 struct superblock_smack
*sp
= sb
->s_security
;
319 struct inode_smack
*isp
;
324 spin_lock(&sp
->smk_sblock
);
325 if (sp
->smk_initialized
!= 0) {
326 spin_unlock(&sp
->smk_sblock
);
329 sp
->smk_initialized
= 1;
330 spin_unlock(&sp
->smk_sblock
);
332 for (op
= data
; op
!= NULL
; op
= commap
) {
333 commap
= strchr(op
, ',');
337 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
338 op
+= strlen(SMK_FSHAT
);
339 nsp
= smk_import(op
, 0);
342 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
343 op
+= strlen(SMK_FSFLOOR
);
344 nsp
= smk_import(op
, 0);
347 } else if (strncmp(op
, SMK_FSDEFAULT
,
348 strlen(SMK_FSDEFAULT
)) == 0) {
349 op
+= strlen(SMK_FSDEFAULT
);
350 nsp
= smk_import(op
, 0);
352 sp
->smk_default
= nsp
;
353 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
354 op
+= strlen(SMK_FSROOT
);
355 nsp
= smk_import(op
, 0);
362 * Initialize the root inode.
364 isp
= inode
->i_security
;
366 inode
->i_security
= new_inode_smack(sp
->smk_root
);
368 isp
->smk_inode
= sp
->smk_root
;
374 * smack_sb_statfs - Smack check on statfs
375 * @dentry: identifies the file system in question
377 * Returns 0 if current can read the floor of the filesystem,
378 * and error code otherwise
380 static int smack_sb_statfs(struct dentry
*dentry
)
382 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
384 struct smk_audit_info ad
;
386 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
387 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
389 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
394 * smack_sb_mount - Smack check for mounting
401 * Returns 0 if current can write the floor of the filesystem
402 * being mounted on, an error code otherwise.
404 static int smack_sb_mount(char *dev_name
, struct path
*path
,
405 char *type
, unsigned long flags
, void *data
)
407 struct superblock_smack
*sbp
= path
->mnt
->mnt_sb
->s_security
;
408 struct smk_audit_info ad
;
410 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
411 smk_ad_setfield_u_fs_path(&ad
, *path
);
413 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
417 * smack_sb_umount - Smack check for unmounting
418 * @mnt: file system to unmount
421 * Returns 0 if current can write the floor of the filesystem
422 * being unmounted, an error code otherwise.
424 static int smack_sb_umount(struct vfsmount
*mnt
, int flags
)
426 struct superblock_smack
*sbp
;
427 struct smk_audit_info ad
;
430 path
.dentry
= mnt
->mnt_root
;
433 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
434 smk_ad_setfield_u_fs_path(&ad
, path
);
436 sbp
= mnt
->mnt_sb
->s_security
;
437 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
444 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
446 struct task_smack
*tsp
= bprm
->cred
->security
;
447 struct inode_smack
*isp
;
451 rc
= cap_bprm_set_creds(bprm
);
455 if (bprm
->cred_prepared
)
458 if (bprm
->file
== NULL
|| bprm
->file
->f_dentry
== NULL
)
461 dp
= bprm
->file
->f_dentry
;
463 if (dp
->d_inode
== NULL
)
466 isp
= dp
->d_inode
->i_security
;
468 if (isp
->smk_task
!= NULL
)
469 tsp
->smk_task
= isp
->smk_task
;
479 * smack_inode_alloc_security - allocate an inode blob
480 * @inode: the inode in need of a blob
482 * Returns 0 if it gets a blob, -ENOMEM otherwise
484 static int smack_inode_alloc_security(struct inode
*inode
)
486 inode
->i_security
= new_inode_smack(smk_of_current());
487 if (inode
->i_security
== NULL
)
493 * smack_inode_free_security - free an inode blob
494 * @inode: the inode with a blob
496 * Clears the blob pointer in inode
498 static void smack_inode_free_security(struct inode
*inode
)
500 kfree(inode
->i_security
);
501 inode
->i_security
= NULL
;
505 * smack_inode_init_security - copy out the smack from an inode
509 * @name: where to put the attribute name
510 * @value: where to put the attribute value
511 * @len: where to put the length of the attribute
513 * Returns 0 if it all works out, -ENOMEM if there's no memory
515 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
516 const struct qstr
*qstr
, char **name
,
517 void **value
, size_t *len
)
519 char *isp
= smk_of_inode(inode
);
520 char *dsp
= smk_of_inode(dir
);
524 *name
= kstrdup(XATTR_SMACK_SUFFIX
, GFP_KERNEL
);
531 may
= smk_access_entry(smk_of_current(), dsp
, &smack_rule_list
);
535 * If the access rule allows transmutation and
536 * the directory requests transmutation then
537 * by all means transmute.
539 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
540 smk_inode_transmutable(dir
))
543 *value
= kstrdup(isp
, GFP_KERNEL
);
549 *len
= strlen(isp
) + 1;
555 * smack_inode_link - Smack check on link
556 * @old_dentry: the existing object
558 * @new_dentry: the new object
560 * Returns 0 if access is permitted, an error code otherwise
562 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
563 struct dentry
*new_dentry
)
566 struct smk_audit_info ad
;
569 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
570 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
572 isp
= smk_of_inode(old_dentry
->d_inode
);
573 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
575 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
576 isp
= smk_of_inode(new_dentry
->d_inode
);
577 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
578 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
585 * smack_inode_unlink - Smack check on inode deletion
586 * @dir: containing directory object
587 * @dentry: file to unlink
589 * Returns 0 if current can write the containing directory
590 * and the object, error code otherwise
592 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
594 struct inode
*ip
= dentry
->d_inode
;
595 struct smk_audit_info ad
;
598 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
599 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
602 * You need write access to the thing you're unlinking
604 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
607 * You also need write access to the containing directory
609 smk_ad_setfield_u_fs_path_dentry(&ad
, NULL
);
610 smk_ad_setfield_u_fs_inode(&ad
, dir
);
611 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
617 * smack_inode_rmdir - Smack check on directory deletion
618 * @dir: containing directory object
619 * @dentry: directory to unlink
621 * Returns 0 if current can write the containing directory
622 * and the directory, error code otherwise
624 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
626 struct smk_audit_info ad
;
629 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
630 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
633 * You need write access to the thing you're removing
635 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
638 * You also need write access to the containing directory
640 smk_ad_setfield_u_fs_path_dentry(&ad
, NULL
);
641 smk_ad_setfield_u_fs_inode(&ad
, dir
);
642 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
649 * smack_inode_rename - Smack check on rename
650 * @old_inode: the old directory
651 * @old_dentry: unused
652 * @new_inode: the new directory
653 * @new_dentry: unused
655 * Read and write access is required on both the old and
658 * Returns 0 if access is permitted, an error code otherwise
660 static int smack_inode_rename(struct inode
*old_inode
,
661 struct dentry
*old_dentry
,
662 struct inode
*new_inode
,
663 struct dentry
*new_dentry
)
667 struct smk_audit_info ad
;
669 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
670 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
672 isp
= smk_of_inode(old_dentry
->d_inode
);
673 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
675 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
676 isp
= smk_of_inode(new_dentry
->d_inode
);
677 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
678 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
684 * smack_inode_permission - Smack version of permission()
685 * @inode: the inode in question
686 * @mask: the access requested
688 * This is the important Smack hook.
690 * Returns 0 if access is permitted, -EACCES otherwise
692 static int smack_inode_permission(struct inode
*inode
, int mask
, unsigned flags
)
694 struct smk_audit_info ad
;
696 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
698 * No permission to check. Existence test. Yup, it's there.
703 /* May be droppable after audit */
704 if (flags
& IPERM_FLAG_RCU
)
706 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
707 smk_ad_setfield_u_fs_inode(&ad
, inode
);
708 return smk_curacc(smk_of_inode(inode
), mask
, &ad
);
712 * smack_inode_setattr - Smack check for setting attributes
713 * @dentry: the object
714 * @iattr: for the force flag
716 * Returns 0 if access is permitted, an error code otherwise
718 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
720 struct smk_audit_info ad
;
722 * Need to allow for clearing the setuid bit.
724 if (iattr
->ia_valid
& ATTR_FORCE
)
726 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
727 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
729 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
733 * smack_inode_getattr - Smack check for getting attributes
735 * @dentry: the object
737 * Returns 0 if access is permitted, an error code otherwise
739 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
741 struct smk_audit_info ad
;
744 path
.dentry
= dentry
;
747 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
748 smk_ad_setfield_u_fs_path(&ad
, path
);
749 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
753 * smack_inode_setxattr - Smack check for setting xattrs
754 * @dentry: the object
755 * @name: name of the attribute
760 * This protects the Smack attribute explicitly.
762 * Returns 0 if access is permitted, an error code otherwise
764 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
765 const void *value
, size_t size
, int flags
)
767 struct smk_audit_info ad
;
770 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
771 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
772 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
773 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
774 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
775 if (!capable(CAP_MAC_ADMIN
))
778 * check label validity here so import wont fail on
781 if (size
== 0 || size
>= SMK_LABELLEN
||
782 smk_import(value
, size
) == NULL
)
784 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
785 if (!capable(CAP_MAC_ADMIN
))
787 if (size
!= TRANS_TRUE_SIZE
||
788 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
791 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
793 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
794 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
797 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
803 * smack_inode_post_setxattr - Apply the Smack update approved above
805 * @name: attribute name
806 * @value: attribute value
807 * @size: attribute size
810 * Set the pointer in the inode blob to the entry found
811 * in the master label list.
813 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
814 const void *value
, size_t size
, int flags
)
817 struct inode_smack
*isp
= dentry
->d_inode
->i_security
;
819 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
820 nsp
= smk_import(value
, size
);
822 isp
->smk_inode
= nsp
;
824 isp
->smk_inode
= smack_known_invalid
.smk_known
;
825 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
826 nsp
= smk_import(value
, size
);
830 isp
->smk_task
= smack_known_invalid
.smk_known
;
831 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
832 nsp
= smk_import(value
, size
);
836 isp
->smk_mmap
= smack_known_invalid
.smk_known
;
837 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
838 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
844 * smack_inode_getxattr - Smack check on getxattr
845 * @dentry: the object
848 * Returns 0 if access is permitted, an error code otherwise
850 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
852 struct smk_audit_info ad
;
854 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
855 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
857 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
861 * smack_inode_removexattr - Smack check on removexattr
862 * @dentry: the object
863 * @name: name of the attribute
865 * Removing the Smack attribute requires CAP_MAC_ADMIN
867 * Returns 0 if access is permitted, an error code otherwise
869 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
871 struct inode_smack
*isp
;
872 struct smk_audit_info ad
;
875 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
876 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
877 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
878 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
879 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
880 strcmp(name
, XATTR_NAME_SMACKMMAP
)) {
881 if (!capable(CAP_MAC_ADMIN
))
884 rc
= cap_inode_removexattr(dentry
, name
);
886 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
887 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
889 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
892 isp
= dentry
->d_inode
->i_security
;
893 isp
->smk_task
= NULL
;
894 isp
->smk_mmap
= NULL
;
901 * smack_inode_getsecurity - get smack xattrs
903 * @name: attribute name
904 * @buffer: where to put the result
907 * Returns the size of the attribute or an error code
909 static int smack_inode_getsecurity(const struct inode
*inode
,
910 const char *name
, void **buffer
,
913 struct socket_smack
*ssp
;
915 struct super_block
*sbp
;
916 struct inode
*ip
= (struct inode
*)inode
;
921 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
922 isp
= smk_of_inode(inode
);
923 ilen
= strlen(isp
) + 1;
929 * The rest of the Smack xattrs are only on sockets.
932 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
936 if (sock
== NULL
|| sock
->sk
== NULL
)
939 ssp
= sock
->sk
->sk_security
;
941 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
943 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
948 ilen
= strlen(isp
) + 1;
959 * smack_inode_listsecurity - list the Smack attributes
961 * @buffer: where they go
962 * @buffer_size: size of buffer
964 * Returns 0 on success, -EINVAL otherwise
966 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
969 int len
= strlen(XATTR_NAME_SMACK
);
971 if (buffer
!= NULL
&& len
<= buffer_size
) {
972 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
979 * smack_inode_getsecid - Extract inode's security id
980 * @inode: inode to extract the info from
981 * @secid: where result will be saved
983 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
985 struct inode_smack
*isp
= inode
->i_security
;
987 *secid
= smack_to_secid(isp
->smk_inode
);
995 * smack_file_permission - Smack check on file operations
1001 * Should access checks be done on each read or write?
1002 * UNICOS and SELinux say yes.
1003 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1005 * I'll say no for now. Smack does not do the frequent
1006 * label changing that SELinux does.
1008 static int smack_file_permission(struct file
*file
, int mask
)
1014 * smack_file_alloc_security - assign a file security blob
1017 * The security blob for a file is a pointer to the master
1018 * label list, so no allocation is done.
1022 static int smack_file_alloc_security(struct file
*file
)
1024 file
->f_security
= smk_of_current();
1029 * smack_file_free_security - clear a file security blob
1032 * The security blob for a file is a pointer to the master
1033 * label list, so no memory is freed.
1035 static void smack_file_free_security(struct file
*file
)
1037 file
->f_security
= NULL
;
1041 * smack_file_ioctl - Smack check on ioctls
1046 * Relies heavily on the correct use of the ioctl command conventions.
1048 * Returns 0 if allowed, error code otherwise
1050 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1054 struct smk_audit_info ad
;
1056 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1057 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1059 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1060 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1062 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
))
1063 rc
= smk_curacc(file
->f_security
, MAY_READ
, &ad
);
1069 * smack_file_lock - Smack check on file locking
1073 * Returns 0 if current has write access, error code otherwise
1075 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1077 struct smk_audit_info ad
;
1079 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1080 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1081 return smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1085 * smack_file_fcntl - Smack check on fcntl
1087 * @cmd: what action to check
1090 * Returns 0 if current has access, error code otherwise
1092 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1095 struct smk_audit_info ad
;
1098 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1099 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1108 rc
= smk_curacc(file
->f_security
, MAY_READ
, &ad
);
1116 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1119 rc
= smk_curacc(file
->f_security
, MAY_READWRITE
, &ad
);
1127 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1128 * if mapping anonymous memory.
1129 * @file contains the file structure for file to map (may be NULL).
1130 * @reqprot contains the protection requested by the application.
1131 * @prot contains the protection that will be applied by the kernel.
1132 * @flags contains the operational flags.
1133 * Return 0 if permission is granted.
1135 static int smack_file_mmap(struct file
*file
,
1136 unsigned long reqprot
, unsigned long prot
,
1137 unsigned long flags
, unsigned long addr
,
1138 unsigned long addr_only
)
1140 struct smack_rule
*srp
;
1141 struct task_smack
*tsp
;
1145 struct inode_smack
*isp
;
1152 /* do DAC check on address space usage */
1153 rc
= cap_file_mmap(file
, reqprot
, prot
, flags
, addr
, addr_only
);
1154 if (rc
|| addr_only
)
1157 if (file
== NULL
|| file
->f_dentry
== NULL
)
1160 dp
= file
->f_dentry
;
1162 if (dp
->d_inode
== NULL
)
1165 isp
= dp
->d_inode
->i_security
;
1166 if (isp
->smk_mmap
== NULL
)
1168 msmack
= isp
->smk_mmap
;
1170 tsp
= current_security();
1171 sp
= smk_of_current();
1176 * For each Smack rule associated with the subject
1177 * label verify that the SMACK64MMAP also has access
1178 * to that rule's object label.
1180 * Because neither of the labels comes
1181 * from the networking code it is sufficient
1182 * to compare pointers.
1184 list_for_each_entry_rcu(srp
, &smack_rule_list
, list
) {
1185 if (srp
->smk_subject
!= sp
)
1188 osmack
= srp
->smk_object
;
1190 * Matching labels always allows access.
1192 if (msmack
== osmack
)
1195 * If there is a matching local rule take
1196 * that into account as well.
1198 may
= smk_access_entry(srp
->smk_subject
, osmack
,
1201 may
= srp
->smk_access
;
1203 may
&= srp
->smk_access
;
1205 * If may is zero the SMACK64MMAP subject can't
1206 * possibly have less access.
1212 * Fetch the global list entry.
1213 * If there isn't one a SMACK64MMAP subject
1214 * can't have as much access as current.
1216 mmay
= smk_access_entry(msmack
, osmack
, &smack_rule_list
);
1217 if (mmay
== -ENOENT
) {
1222 * If there is a local entry it modifies the
1223 * potential access, too.
1225 tmay
= smk_access_entry(msmack
, osmack
, &tsp
->smk_rules
);
1226 if (tmay
!= -ENOENT
)
1230 * If there is any access available to current that is
1231 * not available to a SMACK64MMAP subject
1234 if ((may
| mmay
) != mmay
) {
1246 * smack_file_set_fowner - set the file security blob value
1247 * @file: object in question
1250 * Further research may be required on this one.
1252 static int smack_file_set_fowner(struct file
*file
)
1254 file
->f_security
= smk_of_current();
1259 * smack_file_send_sigiotask - Smack on sigio
1260 * @tsk: The target task
1261 * @fown: the object the signal come from
1264 * Allow a privileged task to get signals even if it shouldn't
1266 * Returns 0 if a subject with the object's smack could
1267 * write to the task, an error code otherwise.
1269 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1270 struct fown_struct
*fown
, int signum
)
1274 char *tsp
= smk_of_task(tsk
->cred
->security
);
1275 struct smk_audit_info ad
;
1278 * struct fown_struct is never outside the context of a struct file
1280 file
= container_of(fown
, struct file
, f_owner
);
1282 /* we don't log here as rc can be overriden */
1283 rc
= smk_access(file
->f_security
, tsp
, MAY_WRITE
, NULL
);
1284 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1287 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1288 smk_ad_setfield_u_tsk(&ad
, tsk
);
1289 smack_log(file
->f_security
, tsp
, MAY_WRITE
, rc
, &ad
);
1294 * smack_file_receive - Smack file receive check
1297 * Returns 0 if current has access, error code otherwise
1299 static int smack_file_receive(struct file
*file
)
1302 struct smk_audit_info ad
;
1304 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1305 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1307 * This code relies on bitmasks.
1309 if (file
->f_mode
& FMODE_READ
)
1311 if (file
->f_mode
& FMODE_WRITE
)
1314 return smk_curacc(file
->f_security
, may
, &ad
);
1322 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1323 * @new: the new credentials
1324 * @gfp: the atomicity of any memory allocations
1326 * Prepare a blank set of credentials for modification. This must allocate all
1327 * the memory the LSM module might require such that cred_transfer() can
1328 * complete without error.
1330 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1332 struct task_smack
*tsp
;
1334 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1338 cred
->security
= tsp
;
1345 * smack_cred_free - "free" task-level security credentials
1346 * @cred: the credentials in question
1349 static void smack_cred_free(struct cred
*cred
)
1351 struct task_smack
*tsp
= cred
->security
;
1352 struct smack_rule
*rp
;
1353 struct list_head
*l
;
1354 struct list_head
*n
;
1358 cred
->security
= NULL
;
1360 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1361 rp
= list_entry(l
, struct smack_rule
, list
);
1362 list_del(&rp
->list
);
1369 * smack_cred_prepare - prepare new set of credentials for modification
1370 * @new: the new credentials
1371 * @old: the original credentials
1372 * @gfp: the atomicity of any memory allocations
1374 * Prepare a new set of credentials for modification.
1376 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1379 struct task_smack
*old_tsp
= old
->security
;
1380 struct task_smack
*new_tsp
;
1383 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
1384 if (new_tsp
== NULL
)
1387 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1391 new->security
= new_tsp
;
1396 * smack_cred_transfer - Transfer the old credentials to the new credentials
1397 * @new: the new credentials
1398 * @old: the original credentials
1400 * Fill in a set of blank credentials from another set of credentials.
1402 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1404 struct task_smack
*old_tsp
= old
->security
;
1405 struct task_smack
*new_tsp
= new->security
;
1407 new_tsp
->smk_task
= old_tsp
->smk_task
;
1408 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1409 mutex_init(&new_tsp
->smk_rules_lock
);
1410 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1413 /* cbs copy rule list */
1417 * smack_kernel_act_as - Set the subjective context in a set of credentials
1418 * @new: points to the set of credentials to be modified.
1419 * @secid: specifies the security ID to be set
1421 * Set the security data for a kernel service.
1423 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1425 struct task_smack
*new_tsp
= new->security
;
1426 char *smack
= smack_from_secid(secid
);
1431 new_tsp
->smk_task
= smack
;
1436 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1437 * @new: points to the set of credentials to be modified
1438 * @inode: points to the inode to use as a reference
1440 * Set the file creation context in a set of credentials to the same
1441 * as the objective context of the specified inode
1443 static int smack_kernel_create_files_as(struct cred
*new,
1444 struct inode
*inode
)
1446 struct inode_smack
*isp
= inode
->i_security
;
1447 struct task_smack
*tsp
= new->security
;
1449 tsp
->smk_forked
= isp
->smk_inode
;
1450 tsp
->smk_task
= isp
->smk_inode
;
1455 * smk_curacc_on_task - helper to log task related access
1456 * @p: the task object
1457 * @access : the access requested
1459 * Return 0 if access is permitted
1461 static int smk_curacc_on_task(struct task_struct
*p
, int access
)
1463 struct smk_audit_info ad
;
1465 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1466 smk_ad_setfield_u_tsk(&ad
, p
);
1467 return smk_curacc(smk_of_task(task_security(p
)), access
, &ad
);
1471 * smack_task_setpgid - Smack check on setting pgid
1472 * @p: the task object
1475 * Return 0 if write access is permitted
1477 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1479 return smk_curacc_on_task(p
, MAY_WRITE
);
1483 * smack_task_getpgid - Smack access check for getpgid
1484 * @p: the object task
1486 * Returns 0 if current can read the object task, error code otherwise
1488 static int smack_task_getpgid(struct task_struct
*p
)
1490 return smk_curacc_on_task(p
, MAY_READ
);
1494 * smack_task_getsid - Smack access check for getsid
1495 * @p: the object task
1497 * Returns 0 if current can read the object task, error code otherwise
1499 static int smack_task_getsid(struct task_struct
*p
)
1501 return smk_curacc_on_task(p
, MAY_READ
);
1505 * smack_task_getsecid - get the secid of the task
1506 * @p: the object task
1507 * @secid: where to put the result
1509 * Sets the secid to contain a u32 version of the smack label.
1511 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1513 *secid
= smack_to_secid(smk_of_task(task_security(p
)));
1517 * smack_task_setnice - Smack check on setting nice
1518 * @p: the task object
1521 * Return 0 if write access is permitted
1523 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1527 rc
= cap_task_setnice(p
, nice
);
1529 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1534 * smack_task_setioprio - Smack check on setting ioprio
1535 * @p: the task object
1538 * Return 0 if write access is permitted
1540 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1544 rc
= cap_task_setioprio(p
, ioprio
);
1546 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1551 * smack_task_getioprio - Smack check on reading ioprio
1552 * @p: the task object
1554 * Return 0 if read access is permitted
1556 static int smack_task_getioprio(struct task_struct
*p
)
1558 return smk_curacc_on_task(p
, MAY_READ
);
1562 * smack_task_setscheduler - Smack check on setting scheduler
1563 * @p: the task object
1567 * Return 0 if read access is permitted
1569 static int smack_task_setscheduler(struct task_struct
*p
)
1573 rc
= cap_task_setscheduler(p
);
1575 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1580 * smack_task_getscheduler - Smack check on reading scheduler
1581 * @p: the task object
1583 * Return 0 if read access is permitted
1585 static int smack_task_getscheduler(struct task_struct
*p
)
1587 return smk_curacc_on_task(p
, MAY_READ
);
1591 * smack_task_movememory - Smack check on moving memory
1592 * @p: the task object
1594 * Return 0 if write access is permitted
1596 static int smack_task_movememory(struct task_struct
*p
)
1598 return smk_curacc_on_task(p
, MAY_WRITE
);
1602 * smack_task_kill - Smack check on signal delivery
1603 * @p: the task object
1606 * @secid: identifies the smack to use in lieu of current's
1608 * Return 0 if write access is permitted
1610 * The secid behavior is an artifact of an SELinux hack
1611 * in the USB code. Someday it may go away.
1613 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1616 struct smk_audit_info ad
;
1618 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1619 smk_ad_setfield_u_tsk(&ad
, p
);
1621 * Sending a signal requires that the sender
1622 * can write the receiver.
1625 return smk_curacc(smk_of_task(task_security(p
)), MAY_WRITE
,
1628 * If the secid isn't 0 we're dealing with some USB IO
1629 * specific behavior. This is not clean. For one thing
1630 * we can't take privilege into account.
1632 return smk_access(smack_from_secid(secid
),
1633 smk_of_task(task_security(p
)), MAY_WRITE
, &ad
);
1637 * smack_task_wait - Smack access check for waiting
1638 * @p: task to wait for
1640 * Returns 0 if current can wait for p, error code otherwise
1642 static int smack_task_wait(struct task_struct
*p
)
1644 struct smk_audit_info ad
;
1645 char *sp
= smk_of_current();
1646 char *tsp
= smk_of_forked(task_security(p
));
1649 /* we don't log here, we can be overriden */
1650 rc
= smk_access(tsp
, sp
, MAY_WRITE
, NULL
);
1655 * Allow the operation to succeed if either task
1656 * has privilege to perform operations that might
1657 * account for the smack labels having gotten to
1658 * be different in the first place.
1660 * This breaks the strict subject/object access
1661 * control ideal, taking the object's privilege
1662 * state into account in the decision as well as
1665 if (capable(CAP_MAC_OVERRIDE
) || has_capability(p
, CAP_MAC_OVERRIDE
))
1667 /* we log only if we didn't get overriden */
1669 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1670 smk_ad_setfield_u_tsk(&ad
, p
);
1671 smack_log(tsp
, sp
, MAY_WRITE
, rc
, &ad
);
1676 * smack_task_to_inode - copy task smack into the inode blob
1677 * @p: task to copy from
1678 * @inode: inode to copy to
1680 * Sets the smack pointer in the inode security blob
1682 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
1684 struct inode_smack
*isp
= inode
->i_security
;
1685 isp
->smk_inode
= smk_of_task(task_security(p
));
1693 * smack_sk_alloc_security - Allocate a socket blob
1696 * @gfp_flags: memory allocation flags
1698 * Assign Smack pointers to current
1700 * Returns 0 on success, -ENOMEM is there's no memory
1702 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
1704 char *csp
= smk_of_current();
1705 struct socket_smack
*ssp
;
1707 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
1713 ssp
->smk_packet
[0] = '\0';
1715 sk
->sk_security
= ssp
;
1721 * smack_sk_free_security - Free a socket blob
1724 * Clears the blob pointer
1726 static void smack_sk_free_security(struct sock
*sk
)
1728 kfree(sk
->sk_security
);
1732 * smack_host_label - check host based restrictions
1733 * @sip: the object end
1735 * looks for host based access restrictions
1737 * This version will only be appropriate for really small sets of single label
1738 * hosts. The caller is responsible for ensuring that the RCU read lock is
1739 * taken before calling this function.
1741 * Returns the label of the far end or NULL if it's not special.
1743 static char *smack_host_label(struct sockaddr_in
*sip
)
1745 struct smk_netlbladdr
*snp
;
1746 struct in_addr
*siap
= &sip
->sin_addr
;
1748 if (siap
->s_addr
== 0)
1751 list_for_each_entry_rcu(snp
, &smk_netlbladdr_list
, list
)
1753 * we break after finding the first match because
1754 * the list is sorted from longest to shortest mask
1755 * so we have found the most specific match
1757 if ((&snp
->smk_host
.sin_addr
)->s_addr
==
1758 (siap
->s_addr
& (&snp
->smk_mask
)->s_addr
)) {
1759 /* we have found the special CIPSO option */
1760 if (snp
->smk_label
== smack_cipso_option
)
1762 return snp
->smk_label
;
1769 * smack_set_catset - convert a capset to netlabel mls categories
1770 * @catset: the Smack categories
1771 * @sap: where to put the netlabel categories
1773 * Allocates and fills attr.mls.cat
1775 static void smack_set_catset(char *catset
, struct netlbl_lsm_secattr
*sap
)
1786 sap
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1787 sap
->attr
.mls
.cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1788 sap
->attr
.mls
.cat
->startbit
= 0;
1790 for (cat
= 1, cp
= catset
, byte
= 0; byte
< SMK_LABELLEN
; cp
++, byte
++)
1791 for (m
= 0x80; m
!= 0; m
>>= 1, cat
++) {
1794 rc
= netlbl_secattr_catmap_setbit(sap
->attr
.mls
.cat
,
1800 * smack_to_secattr - fill a secattr from a smack value
1801 * @smack: the smack value
1802 * @nlsp: where the result goes
1804 * Casey says that CIPSO is good enough for now.
1805 * It can be used to effect.
1806 * It can also be abused to effect when necessary.
1807 * Apologies to the TSIG group in general and GW in particular.
1809 static void smack_to_secattr(char *smack
, struct netlbl_lsm_secattr
*nlsp
)
1811 struct smack_cipso cipso
;
1814 nlsp
->domain
= smack
;
1815 nlsp
->flags
= NETLBL_SECATTR_DOMAIN
| NETLBL_SECATTR_MLS_LVL
;
1817 rc
= smack_to_cipso(smack
, &cipso
);
1819 nlsp
->attr
.mls
.lvl
= cipso
.smk_level
;
1820 smack_set_catset(cipso
.smk_catset
, nlsp
);
1822 nlsp
->attr
.mls
.lvl
= smack_cipso_direct
;
1823 smack_set_catset(smack
, nlsp
);
1828 * smack_netlabel - Set the secattr on a socket
1830 * @labeled: socket label scheme
1832 * Convert the outbound smack value (smk_out) to a
1833 * secattr and attach it to the socket.
1835 * Returns 0 on success or an error code
1837 static int smack_netlabel(struct sock
*sk
, int labeled
)
1839 struct socket_smack
*ssp
= sk
->sk_security
;
1840 struct netlbl_lsm_secattr secattr
;
1844 * Usually the netlabel code will handle changing the
1845 * packet labeling based on the label.
1846 * The case of a single label host is different, because
1847 * a single label host should never get a labeled packet
1848 * even though the label is usually associated with a packet
1852 bh_lock_sock_nested(sk
);
1854 if (ssp
->smk_out
== smack_net_ambient
||
1855 labeled
== SMACK_UNLABELED_SOCKET
)
1856 netlbl_sock_delattr(sk
);
1858 netlbl_secattr_init(&secattr
);
1859 smack_to_secattr(ssp
->smk_out
, &secattr
);
1860 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &secattr
);
1861 netlbl_secattr_destroy(&secattr
);
1871 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1873 * @sap: the destination address
1875 * Set the correct secattr for the given socket based on the destination
1876 * address and perform any outbound access checks needed.
1878 * Returns 0 on success or an error code.
1881 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
1886 struct socket_smack
*ssp
= sk
->sk_security
;
1887 struct smk_audit_info ad
;
1890 hostsp
= smack_host_label(sap
);
1891 if (hostsp
!= NULL
) {
1892 sk_lbl
= SMACK_UNLABELED_SOCKET
;
1894 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
1895 ad
.a
.u
.net
.family
= sap
->sin_family
;
1896 ad
.a
.u
.net
.dport
= sap
->sin_port
;
1897 ad
.a
.u
.net
.v4info
.daddr
= sap
->sin_addr
.s_addr
;
1899 rc
= smk_access(ssp
->smk_out
, hostsp
, MAY_WRITE
, &ad
);
1901 sk_lbl
= SMACK_CIPSO_SOCKET
;
1908 return smack_netlabel(sk
, sk_lbl
);
1912 * smack_inode_setsecurity - set smack xattrs
1913 * @inode: the object
1914 * @name: attribute name
1915 * @value: attribute value
1916 * @size: size of the attribute
1919 * Sets the named attribute in the appropriate blob
1921 * Returns 0 on success, or an error code
1923 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
1924 const void *value
, size_t size
, int flags
)
1927 struct inode_smack
*nsp
= inode
->i_security
;
1928 struct socket_smack
*ssp
;
1929 struct socket
*sock
;
1932 if (value
== NULL
|| size
> SMK_LABELLEN
|| size
== 0)
1935 sp
= smk_import(value
, size
);
1939 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1940 nsp
->smk_inode
= sp
;
1941 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
1945 * The rest of the Smack xattrs are only on sockets.
1947 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
1950 sock
= SOCKET_I(inode
);
1951 if (sock
== NULL
|| sock
->sk
== NULL
)
1954 ssp
= sock
->sk
->sk_security
;
1956 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1958 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
1960 if (sock
->sk
->sk_family
!= PF_UNIX
) {
1961 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
1964 "Smack: \"%s\" netlbl error %d.\n",
1974 * smack_socket_post_create - finish socket setup
1976 * @family: protocol family
1981 * Sets the netlabel information on the socket
1983 * Returns 0 on success, and error code otherwise
1985 static int smack_socket_post_create(struct socket
*sock
, int family
,
1986 int type
, int protocol
, int kern
)
1988 if (family
!= PF_INET
|| sock
->sk
== NULL
)
1991 * Set the outbound netlbl.
1993 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
1997 * smack_socket_connect - connect access check
1999 * @sap: the other end
2000 * @addrlen: size of sap
2002 * Verifies that a connection may be possible
2004 * Returns 0 on success, and error code otherwise
2006 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2009 if (sock
->sk
== NULL
|| sock
->sk
->sk_family
!= PF_INET
)
2011 if (addrlen
< sizeof(struct sockaddr_in
))
2014 return smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2018 * smack_flags_to_may - convert S_ to MAY_ values
2019 * @flags: the S_ value
2021 * Returns the equivalent MAY_ value
2023 static int smack_flags_to_may(int flags
)
2027 if (flags
& S_IRUGO
)
2029 if (flags
& S_IWUGO
)
2031 if (flags
& S_IXUGO
)
2038 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2043 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2045 msg
->security
= smk_of_current();
2050 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2053 * Clears the blob pointer
2055 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2057 msg
->security
= NULL
;
2061 * smack_of_shm - the smack pointer for the shm
2064 * Returns a pointer to the smack value
2066 static char *smack_of_shm(struct shmid_kernel
*shp
)
2068 return (char *)shp
->shm_perm
.security
;
2072 * smack_shm_alloc_security - Set the security blob for shm
2077 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2079 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2081 isp
->security
= smk_of_current();
2086 * smack_shm_free_security - Clear the security blob for shm
2089 * Clears the blob pointer
2091 static void smack_shm_free_security(struct shmid_kernel
*shp
)
2093 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2095 isp
->security
= NULL
;
2099 * smk_curacc_shm : check if current has access on shm
2101 * @access : access requested
2103 * Returns 0 if current has the requested access, error code otherwise
2105 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2107 char *ssp
= smack_of_shm(shp
);
2108 struct smk_audit_info ad
;
2111 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2112 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2114 return smk_curacc(ssp
, access
, &ad
);
2118 * smack_shm_associate - Smack access check for shm
2120 * @shmflg: access requested
2122 * Returns 0 if current has the requested access, error code otherwise
2124 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2128 may
= smack_flags_to_may(shmflg
);
2129 return smk_curacc_shm(shp
, may
);
2133 * smack_shm_shmctl - Smack access check for shm
2135 * @cmd: what it wants to do
2137 * Returns 0 if current has the requested access, error code otherwise
2139 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2152 may
= MAY_READWRITE
;
2157 * System level information.
2163 return smk_curacc_shm(shp
, may
);
2167 * smack_shm_shmat - Smack access for shmat
2170 * @shmflg: access requested
2172 * Returns 0 if current has the requested access, error code otherwise
2174 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2179 may
= smack_flags_to_may(shmflg
);
2180 return smk_curacc_shm(shp
, may
);
2184 * smack_of_sem - the smack pointer for the sem
2187 * Returns a pointer to the smack value
2189 static char *smack_of_sem(struct sem_array
*sma
)
2191 return (char *)sma
->sem_perm
.security
;
2195 * smack_sem_alloc_security - Set the security blob for sem
2200 static int smack_sem_alloc_security(struct sem_array
*sma
)
2202 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2204 isp
->security
= smk_of_current();
2209 * smack_sem_free_security - Clear the security blob for sem
2212 * Clears the blob pointer
2214 static void smack_sem_free_security(struct sem_array
*sma
)
2216 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2218 isp
->security
= NULL
;
2222 * smk_curacc_sem : check if current has access on sem
2224 * @access : access requested
2226 * Returns 0 if current has the requested access, error code otherwise
2228 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2230 char *ssp
= smack_of_sem(sma
);
2231 struct smk_audit_info ad
;
2234 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2235 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2237 return smk_curacc(ssp
, access
, &ad
);
2241 * smack_sem_associate - Smack access check for sem
2243 * @semflg: access requested
2245 * Returns 0 if current has the requested access, error code otherwise
2247 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2251 may
= smack_flags_to_may(semflg
);
2252 return smk_curacc_sem(sma
, may
);
2256 * smack_sem_shmctl - Smack access check for sem
2258 * @cmd: what it wants to do
2260 * Returns 0 if current has the requested access, error code otherwise
2262 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
2280 may
= MAY_READWRITE
;
2285 * System level information
2292 return smk_curacc_sem(sma
, may
);
2296 * smack_sem_semop - Smack checks of semaphore operations
2302 * Treated as read and write in all cases.
2304 * Returns 0 if access is allowed, error code otherwise
2306 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
2307 unsigned nsops
, int alter
)
2309 return smk_curacc_sem(sma
, MAY_READWRITE
);
2313 * smack_msg_alloc_security - Set the security blob for msg
2318 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
2320 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2322 kisp
->security
= smk_of_current();
2327 * smack_msg_free_security - Clear the security blob for msg
2330 * Clears the blob pointer
2332 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
2334 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2336 kisp
->security
= NULL
;
2340 * smack_of_msq - the smack pointer for the msq
2343 * Returns a pointer to the smack value
2345 static char *smack_of_msq(struct msg_queue
*msq
)
2347 return (char *)msq
->q_perm
.security
;
2351 * smk_curacc_msq : helper to check if current has access on msq
2353 * @access : access requested
2355 * return 0 if current has access, error otherwise
2357 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
2359 char *msp
= smack_of_msq(msq
);
2360 struct smk_audit_info ad
;
2363 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2364 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
2366 return smk_curacc(msp
, access
, &ad
);
2370 * smack_msg_queue_associate - Smack access check for msg_queue
2372 * @msqflg: access requested
2374 * Returns 0 if current has the requested access, error code otherwise
2376 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
2380 may
= smack_flags_to_may(msqflg
);
2381 return smk_curacc_msq(msq
, may
);
2385 * smack_msg_queue_msgctl - Smack access check for msg_queue
2387 * @cmd: what it wants to do
2389 * Returns 0 if current has the requested access, error code otherwise
2391 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
2402 may
= MAY_READWRITE
;
2407 * System level information
2414 return smk_curacc_msq(msq
, may
);
2418 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2421 * @msqflg: access requested
2423 * Returns 0 if current has the requested access, error code otherwise
2425 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
2430 may
= smack_flags_to_may(msqflg
);
2431 return smk_curacc_msq(msq
, may
);
2435 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2442 * Returns 0 if current has read and write access, error code otherwise
2444 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
2445 struct task_struct
*target
, long type
, int mode
)
2447 return smk_curacc_msq(msq
, MAY_READWRITE
);
2451 * smack_ipc_permission - Smack access for ipc_permission()
2452 * @ipp: the object permissions
2453 * @flag: access requested
2455 * Returns 0 if current has read and write access, error code otherwise
2457 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
2459 char *isp
= ipp
->security
;
2460 int may
= smack_flags_to_may(flag
);
2461 struct smk_audit_info ad
;
2464 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2465 ad
.a
.u
.ipc_id
= ipp
->id
;
2467 return smk_curacc(isp
, may
, &ad
);
2471 * smack_ipc_getsecid - Extract smack security id
2472 * @ipp: the object permissions
2473 * @secid: where result will be saved
2475 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
2477 char *smack
= ipp
->security
;
2479 *secid
= smack_to_secid(smack
);
2483 * smack_d_instantiate - Make sure the blob is correct on an inode
2484 * @opt_dentry: dentry where inode will be attached
2485 * @inode: the object
2487 * Set the inode's security blob if it hasn't been done already.
2489 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
2491 struct super_block
*sbp
;
2492 struct superblock_smack
*sbsp
;
2493 struct inode_smack
*isp
;
2494 char *csp
= smk_of_current();
2497 char trattr
[TRANS_TRUE_SIZE
];
2504 isp
= inode
->i_security
;
2506 mutex_lock(&isp
->smk_lock
);
2508 * If the inode is already instantiated
2509 * take the quick way out
2511 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
2515 sbsp
= sbp
->s_security
;
2517 * We're going to use the superblock default label
2518 * if there's no label on the file.
2520 final
= sbsp
->smk_default
;
2523 * If this is the root inode the superblock
2524 * may be in the process of initialization.
2525 * If that is the case use the root value out
2526 * of the superblock.
2528 if (opt_dentry
->d_parent
== opt_dentry
) {
2529 isp
->smk_inode
= sbsp
->smk_root
;
2530 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2535 * This is pretty hackish.
2536 * Casey says that we shouldn't have to do
2537 * file system specific code, but it does help
2538 * with keeping it simple.
2540 switch (sbp
->s_magic
) {
2543 * Casey says that it's a little embarrassing
2544 * that the smack file system doesn't do
2545 * extended attributes.
2547 final
= smack_known_star
.smk_known
;
2551 * Casey says pipes are easy (?)
2553 final
= smack_known_star
.smk_known
;
2555 case DEVPTS_SUPER_MAGIC
:
2557 * devpts seems content with the label of the task.
2558 * Programs that change smack have to treat the
2565 * Socket access is controlled by the socket
2566 * structures associated with the task involved.
2568 final
= smack_known_star
.smk_known
;
2570 case PROC_SUPER_MAGIC
:
2572 * Casey says procfs appears not to care.
2573 * The superblock default suffices.
2578 * Device labels should come from the filesystem,
2579 * but watch out, because they're volitile,
2580 * getting recreated on every reboot.
2582 final
= smack_known_star
.smk_known
;
2586 * If a smack value has been set we want to use it,
2587 * but since tmpfs isn't giving us the opportunity
2588 * to set mount options simulate setting the
2589 * superblock default.
2593 * This isn't an understood special case.
2594 * Get the value from the xattr.
2598 * UNIX domain sockets use lower level socket data.
2600 if (S_ISSOCK(inode
->i_mode
)) {
2601 final
= smack_known_star
.smk_known
;
2605 * No xattr support means, alas, no SMACK label.
2606 * Use the aforeapplied default.
2607 * It would be curious if the label of the task
2608 * does not match that assigned.
2610 if (inode
->i_op
->getxattr
== NULL
)
2613 * Get the dentry for xattr.
2615 dp
= dget(opt_dentry
);
2616 fetched
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
2617 if (fetched
!= NULL
) {
2619 if (S_ISDIR(inode
->i_mode
)) {
2621 inode
->i_op
->getxattr(dp
,
2622 XATTR_NAME_SMACKTRANSMUTE
,
2623 trattr
, TRANS_TRUE_SIZE
);
2624 if (strncmp(trattr
, TRANS_TRUE
,
2625 TRANS_TRUE_SIZE
) == 0)
2626 transflag
= SMK_INODE_TRANSMUTE
;
2629 isp
->smk_task
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
2630 isp
->smk_mmap
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
2637 isp
->smk_inode
= csp
;
2639 isp
->smk_inode
= final
;
2641 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
2644 mutex_unlock(&isp
->smk_lock
);
2649 * smack_getprocattr - Smack process attribute access
2650 * @p: the object task
2651 * @name: the name of the attribute in /proc/.../attr
2652 * @value: where to put the result
2654 * Places a copy of the task Smack into value
2656 * Returns the length of the smack label or an error code
2658 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
2663 if (strcmp(name
, "current") != 0)
2666 cp
= kstrdup(smk_of_task(task_security(p
)), GFP_KERNEL
);
2676 * smack_setprocattr - Smack process attribute setting
2677 * @p: the object task
2678 * @name: the name of the attribute in /proc/.../attr
2679 * @value: the value to set
2680 * @size: the size of the value
2682 * Sets the Smack value of the task. Only setting self
2683 * is permitted and only with privilege
2685 * Returns the length of the smack label or an error code
2687 static int smack_setprocattr(struct task_struct
*p
, char *name
,
2688 void *value
, size_t size
)
2691 struct task_smack
*tsp
;
2692 struct task_smack
*oldtsp
;
2697 * Changing another process' Smack value is too dangerous
2698 * and supports no sane use case.
2703 if (!capable(CAP_MAC_ADMIN
))
2706 if (value
== NULL
|| size
== 0 || size
>= SMK_LABELLEN
)
2709 if (strcmp(name
, "current") != 0)
2712 newsmack
= smk_import(value
, size
);
2713 if (newsmack
== NULL
)
2717 * No process is ever allowed the web ("@") label.
2719 if (newsmack
== smack_known_web
.smk_known
)
2722 oldtsp
= p
->cred
->security
;
2723 new = prepare_creds();
2727 tsp
= new_task_smack(newsmack
, oldtsp
->smk_forked
, GFP_KERNEL
);
2732 rc
= smk_copy_rules(&tsp
->smk_rules
, &oldtsp
->smk_rules
, GFP_KERNEL
);
2736 new->security
= tsp
;
2742 * smack_unix_stream_connect - Smack access on UDS
2744 * @other: the other sock
2747 * Return 0 if a subject with the smack of sock could access
2748 * an object with the smack of other, otherwise an error code
2750 static int smack_unix_stream_connect(struct sock
*sock
,
2751 struct sock
*other
, struct sock
*newsk
)
2753 struct socket_smack
*ssp
= sock
->sk_security
;
2754 struct socket_smack
*osp
= other
->sk_security
;
2755 struct smk_audit_info ad
;
2758 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2759 smk_ad_setfield_u_net_sk(&ad
, other
);
2761 if (!capable(CAP_MAC_OVERRIDE
))
2762 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
2768 * smack_unix_may_send - Smack access on UDS
2770 * @other: the other socket
2772 * Return 0 if a subject with the smack of sock could access
2773 * an object with the smack of other, otherwise an error code
2775 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
2777 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2778 struct socket_smack
*osp
= other
->sk
->sk_security
;
2779 struct smk_audit_info ad
;
2782 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2783 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
2785 if (!capable(CAP_MAC_OVERRIDE
))
2786 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
2792 * smack_socket_sendmsg - Smack check based on destination host
2795 * @size: the size of the message
2797 * Return 0 if the current subject can write to the destination
2798 * host. This is only a question if the destination is a single
2801 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
2804 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
2807 * Perfectly reasonable for this to be NULL
2809 if (sip
== NULL
|| sip
->sin_family
!= AF_INET
)
2812 return smack_netlabel_send(sock
->sk
, sip
);
2817 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2818 * @sap: netlabel secattr
2819 * @sip: where to put the result
2821 * Copies a smack label into sip
2823 static void smack_from_secattr(struct netlbl_lsm_secattr
*sap
, char *sip
)
2825 char smack
[SMK_LABELLEN
];
2829 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
2831 * Looks like a CIPSO packet.
2832 * If there are flags but no level netlabel isn't
2833 * behaving the way we expect it to.
2835 * Get the categories, if any
2836 * Without guidance regarding the smack value
2837 * for the packet fall back on the network
2840 memset(smack
, '\0', SMK_LABELLEN
);
2841 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) != 0)
2843 pcat
= netlbl_secattr_catmap_walk(
2844 sap
->attr
.mls
.cat
, pcat
+ 1);
2847 smack_catset_bit(pcat
, smack
);
2850 * If it is CIPSO using smack direct mapping
2851 * we are already done. WeeHee.
2853 if (sap
->attr
.mls
.lvl
== smack_cipso_direct
) {
2854 memcpy(sip
, smack
, SMK_MAXLEN
);
2858 * Look it up in the supplied table if it is not
2861 smack_from_cipso(sap
->attr
.mls
.lvl
, smack
, sip
);
2864 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
2866 * Looks like a fallback, which gives us a secid.
2868 sp
= smack_from_secid(sap
->attr
.secid
);
2870 * This has got to be a bug because it is
2871 * impossible to specify a fallback without
2872 * specifying the label, which will ensure
2873 * it has a secid, and the only way to get a
2874 * secid is from a fallback.
2877 strncpy(sip
, sp
, SMK_MAXLEN
);
2881 * Without guidance regarding the smack value
2882 * for the packet fall back on the network
2885 strncpy(sip
, smack_net_ambient
, SMK_MAXLEN
);
2890 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2894 * Returns 0 if the packet should be delivered, an error code otherwise
2896 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
2898 struct netlbl_lsm_secattr secattr
;
2899 struct socket_smack
*ssp
= sk
->sk_security
;
2900 char smack
[SMK_LABELLEN
];
2903 struct smk_audit_info ad
;
2904 if (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
)
2908 * Translate what netlabel gave us.
2910 netlbl_secattr_init(&secattr
);
2912 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
2914 smack_from_secattr(&secattr
, smack
);
2917 csp
= smack_net_ambient
;
2919 netlbl_secattr_destroy(&secattr
);
2922 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2923 ad
.a
.u
.net
.family
= sk
->sk_family
;
2924 ad
.a
.u
.net
.netif
= skb
->skb_iif
;
2925 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
2928 * Receiving a packet requires that the other end
2929 * be able to write here. Read access is not required.
2930 * This is the simplist possible security model
2933 rc
= smk_access(csp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
2935 netlbl_skbuff_err(skb
, rc
, 0);
2940 * smack_socket_getpeersec_stream - pull in packet label
2942 * @optval: user's destination
2943 * @optlen: size thereof
2946 * returns zero on success, an error code otherwise
2948 static int smack_socket_getpeersec_stream(struct socket
*sock
,
2949 char __user
*optval
,
2950 int __user
*optlen
, unsigned len
)
2952 struct socket_smack
*ssp
;
2956 ssp
= sock
->sk
->sk_security
;
2957 slen
= strlen(ssp
->smk_packet
) + 1;
2961 else if (copy_to_user(optval
, ssp
->smk_packet
, slen
) != 0)
2964 if (put_user(slen
, optlen
) != 0)
2972 * smack_socket_getpeersec_dgram - pull in packet label
2973 * @sock: the peer socket
2975 * @secid: pointer to where to put the secid of the packet
2977 * Sets the netlabel socket state on sk from parent
2979 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
2980 struct sk_buff
*skb
, u32
*secid
)
2983 struct netlbl_lsm_secattr secattr
;
2984 struct socket_smack
*sp
;
2985 char smack
[SMK_LABELLEN
];
2986 int family
= PF_UNSPEC
;
2987 u32 s
= 0; /* 0 is the invalid secid */
2991 if (skb
->protocol
== htons(ETH_P_IP
))
2993 else if (skb
->protocol
== htons(ETH_P_IPV6
))
2996 if (family
== PF_UNSPEC
&& sock
!= NULL
)
2997 family
= sock
->sk
->sk_family
;
2999 if (family
== PF_UNIX
) {
3000 sp
= sock
->sk
->sk_security
;
3001 s
= smack_to_secid(sp
->smk_out
);
3002 } else if (family
== PF_INET
|| family
== PF_INET6
) {
3004 * Translate what netlabel gave us.
3006 netlbl_secattr_init(&secattr
);
3007 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3009 smack_from_secattr(&secattr
, smack
);
3010 s
= smack_to_secid(smack
);
3012 netlbl_secattr_destroy(&secattr
);
3021 * smack_sock_graft - Initialize a newly created socket with an existing sock
3023 * @parent: parent socket
3025 * Set the smk_{in,out} state of an existing sock based on the process that
3026 * is creating the new socket.
3028 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3030 struct socket_smack
*ssp
;
3033 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3036 ssp
= sk
->sk_security
;
3037 ssp
->smk_in
= ssp
->smk_out
= smk_of_current();
3038 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3042 * smack_inet_conn_request - Smack access check on connect
3043 * @sk: socket involved
3047 * Returns 0 if a task with the packet label could write to
3048 * the socket, otherwise an error code
3050 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
3051 struct request_sock
*req
)
3053 u16 family
= sk
->sk_family
;
3054 struct socket_smack
*ssp
= sk
->sk_security
;
3055 struct netlbl_lsm_secattr secattr
;
3056 struct sockaddr_in addr
;
3058 char smack
[SMK_LABELLEN
];
3060 struct smk_audit_info ad
;
3062 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3063 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
3066 netlbl_secattr_init(&secattr
);
3067 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3069 smack_from_secattr(&secattr
, smack
);
3071 strncpy(smack
, smack_known_huh
.smk_known
, SMK_MAXLEN
);
3072 netlbl_secattr_destroy(&secattr
);
3075 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
3076 ad
.a
.u
.net
.family
= family
;
3077 ad
.a
.u
.net
.netif
= skb
->skb_iif
;
3078 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3081 * Receiving a packet requires that the other end be able to write
3082 * here. Read access is not required.
3084 rc
= smk_access(smack
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3089 * Save the peer's label in the request_sock so we can later setup
3090 * smk_packet in the child socket so that SO_PEERCRED can report it.
3092 req
->peer_secid
= smack_to_secid(smack
);
3095 * We need to decide if we want to label the incoming connection here
3096 * if we do we only need to label the request_sock and the stack will
3097 * propagate the wire-label to the sock when it is created.
3100 addr
.sin_addr
.s_addr
= hdr
->saddr
;
3102 if (smack_host_label(&addr
) == NULL
) {
3104 netlbl_secattr_init(&secattr
);
3105 smack_to_secattr(smack
, &secattr
);
3106 rc
= netlbl_req_setattr(req
, &secattr
);
3107 netlbl_secattr_destroy(&secattr
);
3110 netlbl_req_delattr(req
);
3117 * smack_inet_csk_clone - Copy the connection information to the new socket
3118 * @sk: the new socket
3119 * @req: the connection's request_sock
3121 * Transfer the connection's peer label to the newly created socket.
3123 static void smack_inet_csk_clone(struct sock
*sk
,
3124 const struct request_sock
*req
)
3126 struct socket_smack
*ssp
= sk
->sk_security
;
3129 if (req
->peer_secid
!= 0) {
3130 smack
= smack_from_secid(req
->peer_secid
);
3131 strncpy(ssp
->smk_packet
, smack
, SMK_MAXLEN
);
3133 ssp
->smk_packet
[0] = '\0';
3137 * Key management security hooks
3139 * Casey has not tested key support very heavily.
3140 * The permission check is most likely too restrictive.
3141 * If you care about keys please have a look.
3146 * smack_key_alloc - Set the key security blob
3148 * @cred: the credentials to use
3151 * No allocation required
3155 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
3156 unsigned long flags
)
3158 key
->security
= smk_of_task(cred
->security
);
3163 * smack_key_free - Clear the key security blob
3166 * Clear the blob pointer
3168 static void smack_key_free(struct key
*key
)
3170 key
->security
= NULL
;
3174 * smack_key_permission - Smack access on a key
3175 * @key_ref: gets to the object
3176 * @cred: the credentials to use
3179 * Return 0 if the task has read and write to the object,
3180 * an error code otherwise
3182 static int smack_key_permission(key_ref_t key_ref
,
3183 const struct cred
*cred
, key_perm_t perm
)
3186 struct smk_audit_info ad
;
3187 char *tsp
= smk_of_task(cred
->security
);
3189 keyp
= key_ref_to_ptr(key_ref
);
3193 * If the key hasn't been initialized give it access so that
3196 if (keyp
->security
== NULL
)
3199 * This should not occur
3204 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
3205 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
3206 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
3208 return smk_access(tsp
, keyp
->security
,
3209 MAY_READWRITE
, &ad
);
3211 #endif /* CONFIG_KEYS */
3216 * Audit requires a unique representation of each Smack specific
3217 * rule. This unique representation is used to distinguish the
3218 * object to be audited from remaining kernel objects and also
3219 * works as a glue between the audit hooks.
3221 * Since repository entries are added but never deleted, we'll use
3222 * the smack_known label address related to the given audit rule as
3223 * the needed unique representation. This also better fits the smack
3224 * model where nearly everything is a label.
3229 * smack_audit_rule_init - Initialize a smack audit rule
3230 * @field: audit rule fields given from user-space (audit.h)
3231 * @op: required testing operator (=, !=, >, <, ...)
3232 * @rulestr: smack label to be audited
3233 * @vrule: pointer to save our own audit rule representation
3235 * Prepare to audit cases where (@field @op @rulestr) is true.
3236 * The label to be audited is created if necessay.
3238 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
3240 char **rule
= (char **)vrule
;
3243 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3246 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
3249 *rule
= smk_import(rulestr
, 0);
3255 * smack_audit_rule_known - Distinguish Smack audit rules
3256 * @krule: rule of interest, in Audit kernel representation format
3258 * This is used to filter Smack rules from remaining Audit ones.
3259 * If it's proved that this rule belongs to us, the
3260 * audit_rule_match hook will be called to do the final judgement.
3262 static int smack_audit_rule_known(struct audit_krule
*krule
)
3264 struct audit_field
*f
;
3267 for (i
= 0; i
< krule
->field_count
; i
++) {
3268 f
= &krule
->fields
[i
];
3270 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
3278 * smack_audit_rule_match - Audit given object ?
3279 * @secid: security id for identifying the object to test
3280 * @field: audit rule flags given from user-space
3281 * @op: required testing operator
3282 * @vrule: smack internal rule presentation
3283 * @actx: audit context associated with the check
3285 * The core Audit hook. It's used to take the decision of
3286 * whether to audit or not to audit a given object.
3288 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
3289 struct audit_context
*actx
)
3295 audit_log(actx
, GFP_KERNEL
, AUDIT_SELINUX_ERR
,
3296 "Smack: missing rule\n");
3300 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3303 smack
= smack_from_secid(secid
);
3306 * No need to do string comparisons. If a match occurs,
3307 * both pointers will point to the same smack_known
3310 if (op
== Audit_equal
)
3311 return (rule
== smack
);
3312 if (op
== Audit_not_equal
)
3313 return (rule
!= smack
);
3319 * smack_audit_rule_free - free smack rule representation
3320 * @vrule: rule to be freed.
3322 * No memory was allocated.
3324 static void smack_audit_rule_free(void *vrule
)
3329 #endif /* CONFIG_AUDIT */
3332 * smack_secid_to_secctx - return the smack label for a secid
3333 * @secid: incoming integer
3334 * @secdata: destination
3335 * @seclen: how long it is
3337 * Exists for networking code.
3339 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
3341 char *sp
= smack_from_secid(secid
);
3345 *seclen
= strlen(sp
);
3350 * smack_secctx_to_secid - return the secid for a smack label
3351 * @secdata: smack label
3352 * @seclen: how long result is
3353 * @secid: outgoing integer
3355 * Exists for audit and networking code.
3357 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
3359 *secid
= smack_to_secid(secdata
);
3364 * smack_release_secctx - don't do anything.
3368 * Exists to make sure nothing gets done, and properly
3370 static void smack_release_secctx(char *secdata
, u32 seclen
)
3374 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
3376 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
3379 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
3381 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
3384 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
3387 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
3395 struct security_operations smack_ops
= {
3398 .ptrace_access_check
= smack_ptrace_access_check
,
3399 .ptrace_traceme
= smack_ptrace_traceme
,
3400 .syslog
= smack_syslog
,
3402 .sb_alloc_security
= smack_sb_alloc_security
,
3403 .sb_free_security
= smack_sb_free_security
,
3404 .sb_copy_data
= smack_sb_copy_data
,
3405 .sb_kern_mount
= smack_sb_kern_mount
,
3406 .sb_statfs
= smack_sb_statfs
,
3407 .sb_mount
= smack_sb_mount
,
3408 .sb_umount
= smack_sb_umount
,
3410 .bprm_set_creds
= smack_bprm_set_creds
,
3412 .inode_alloc_security
= smack_inode_alloc_security
,
3413 .inode_free_security
= smack_inode_free_security
,
3414 .inode_init_security
= smack_inode_init_security
,
3415 .inode_link
= smack_inode_link
,
3416 .inode_unlink
= smack_inode_unlink
,
3417 .inode_rmdir
= smack_inode_rmdir
,
3418 .inode_rename
= smack_inode_rename
,
3419 .inode_permission
= smack_inode_permission
,
3420 .inode_setattr
= smack_inode_setattr
,
3421 .inode_getattr
= smack_inode_getattr
,
3422 .inode_setxattr
= smack_inode_setxattr
,
3423 .inode_post_setxattr
= smack_inode_post_setxattr
,
3424 .inode_getxattr
= smack_inode_getxattr
,
3425 .inode_removexattr
= smack_inode_removexattr
,
3426 .inode_getsecurity
= smack_inode_getsecurity
,
3427 .inode_setsecurity
= smack_inode_setsecurity
,
3428 .inode_listsecurity
= smack_inode_listsecurity
,
3429 .inode_getsecid
= smack_inode_getsecid
,
3431 .file_permission
= smack_file_permission
,
3432 .file_alloc_security
= smack_file_alloc_security
,
3433 .file_free_security
= smack_file_free_security
,
3434 .file_ioctl
= smack_file_ioctl
,
3435 .file_lock
= smack_file_lock
,
3436 .file_fcntl
= smack_file_fcntl
,
3437 .file_mmap
= smack_file_mmap
,
3438 .file_set_fowner
= smack_file_set_fowner
,
3439 .file_send_sigiotask
= smack_file_send_sigiotask
,
3440 .file_receive
= smack_file_receive
,
3442 .cred_alloc_blank
= smack_cred_alloc_blank
,
3443 .cred_free
= smack_cred_free
,
3444 .cred_prepare
= smack_cred_prepare
,
3445 .cred_transfer
= smack_cred_transfer
,
3446 .kernel_act_as
= smack_kernel_act_as
,
3447 .kernel_create_files_as
= smack_kernel_create_files_as
,
3448 .task_setpgid
= smack_task_setpgid
,
3449 .task_getpgid
= smack_task_getpgid
,
3450 .task_getsid
= smack_task_getsid
,
3451 .task_getsecid
= smack_task_getsecid
,
3452 .task_setnice
= smack_task_setnice
,
3453 .task_setioprio
= smack_task_setioprio
,
3454 .task_getioprio
= smack_task_getioprio
,
3455 .task_setscheduler
= smack_task_setscheduler
,
3456 .task_getscheduler
= smack_task_getscheduler
,
3457 .task_movememory
= smack_task_movememory
,
3458 .task_kill
= smack_task_kill
,
3459 .task_wait
= smack_task_wait
,
3460 .task_to_inode
= smack_task_to_inode
,
3462 .ipc_permission
= smack_ipc_permission
,
3463 .ipc_getsecid
= smack_ipc_getsecid
,
3465 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
3466 .msg_msg_free_security
= smack_msg_msg_free_security
,
3468 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
3469 .msg_queue_free_security
= smack_msg_queue_free_security
,
3470 .msg_queue_associate
= smack_msg_queue_associate
,
3471 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
3472 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
3473 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
3475 .shm_alloc_security
= smack_shm_alloc_security
,
3476 .shm_free_security
= smack_shm_free_security
,
3477 .shm_associate
= smack_shm_associate
,
3478 .shm_shmctl
= smack_shm_shmctl
,
3479 .shm_shmat
= smack_shm_shmat
,
3481 .sem_alloc_security
= smack_sem_alloc_security
,
3482 .sem_free_security
= smack_sem_free_security
,
3483 .sem_associate
= smack_sem_associate
,
3484 .sem_semctl
= smack_sem_semctl
,
3485 .sem_semop
= smack_sem_semop
,
3487 .d_instantiate
= smack_d_instantiate
,
3489 .getprocattr
= smack_getprocattr
,
3490 .setprocattr
= smack_setprocattr
,
3492 .unix_stream_connect
= smack_unix_stream_connect
,
3493 .unix_may_send
= smack_unix_may_send
,
3495 .socket_post_create
= smack_socket_post_create
,
3496 .socket_connect
= smack_socket_connect
,
3497 .socket_sendmsg
= smack_socket_sendmsg
,
3498 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
3499 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
3500 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
3501 .sk_alloc_security
= smack_sk_alloc_security
,
3502 .sk_free_security
= smack_sk_free_security
,
3503 .sock_graft
= smack_sock_graft
,
3504 .inet_conn_request
= smack_inet_conn_request
,
3505 .inet_csk_clone
= smack_inet_csk_clone
,
3507 /* key management security hooks */
3509 .key_alloc
= smack_key_alloc
,
3510 .key_free
= smack_key_free
,
3511 .key_permission
= smack_key_permission
,
3512 #endif /* CONFIG_KEYS */
3516 .audit_rule_init
= smack_audit_rule_init
,
3517 .audit_rule_known
= smack_audit_rule_known
,
3518 .audit_rule_match
= smack_audit_rule_match
,
3519 .audit_rule_free
= smack_audit_rule_free
,
3520 #endif /* CONFIG_AUDIT */
3522 .secid_to_secctx
= smack_secid_to_secctx
,
3523 .secctx_to_secid
= smack_secctx_to_secid
,
3524 .release_secctx
= smack_release_secctx
,
3525 .inode_notifysecctx
= smack_inode_notifysecctx
,
3526 .inode_setsecctx
= smack_inode_setsecctx
,
3527 .inode_getsecctx
= smack_inode_getsecctx
,
3531 static __init
void init_smack_know_list(void)
3533 list_add(&smack_known_huh
.list
, &smack_known_list
);
3534 list_add(&smack_known_hat
.list
, &smack_known_list
);
3535 list_add(&smack_known_star
.list
, &smack_known_list
);
3536 list_add(&smack_known_floor
.list
, &smack_known_list
);
3537 list_add(&smack_known_invalid
.list
, &smack_known_list
);
3538 list_add(&smack_known_web
.list
, &smack_known_list
);
3542 * smack_init - initialize the smack system
3546 static __init
int smack_init(void)
3549 struct task_smack
*tsp
;
3551 if (!security_module_enable(&smack_ops
))
3554 tsp
= new_task_smack(smack_known_floor
.smk_known
,
3555 smack_known_floor
.smk_known
, GFP_KERNEL
);
3559 printk(KERN_INFO
"Smack: Initializing.\n");
3562 * Set the security state for the initial task.
3564 cred
= (struct cred
*) current
->cred
;
3565 cred
->security
= tsp
;
3567 /* initialize the smack_know_list */
3568 init_smack_know_list();
3572 spin_lock_init(&smack_known_huh
.smk_cipsolock
);
3573 spin_lock_init(&smack_known_hat
.smk_cipsolock
);
3574 spin_lock_init(&smack_known_star
.smk_cipsolock
);
3575 spin_lock_init(&smack_known_floor
.smk_cipsolock
);
3576 spin_lock_init(&smack_known_invalid
.smk_cipsolock
);
3581 if (register_security(&smack_ops
))
3582 panic("smack: Unable to register with kernel.\n");
3588 * Smack requires early initialization in order to label
3589 * all processes and objects when they are created.
3591 security_initcall(smack_init
);