2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
26 #include <asm/ioctls.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
46 #define task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
55 LIST_HEAD(smk_ipv6_port_list
);
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
65 static struct smack_known
*smk_fetch(const char *name
, struct inode
*ip
,
70 struct smack_known
*skp
= NULL
;
72 if (ip
->i_op
->getxattr
== NULL
)
75 buffer
= kzalloc(SMK_LONGLABEL
, GFP_KERNEL
);
79 rc
= ip
->i_op
->getxattr(dp
, name
, buffer
, SMK_LONGLABEL
);
81 skp
= smk_import_entry(buffer
, rc
);
89 * new_inode_smack - allocate an inode 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 struct inode_smack
*new_inode_smack(char *smack
)
96 struct inode_smack
*isp
;
98 isp
= kzalloc(sizeof(struct inode_smack
), GFP_NOFS
);
102 isp
->smk_inode
= smack
;
104 mutex_init(&isp
->smk_lock
);
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
113 * Returns the new blob or NULL if there's no memory available
115 static struct task_smack
*new_task_smack(struct smack_known
*task
,
116 struct smack_known
*forked
, gfp_t gfp
)
118 struct task_smack
*tsp
;
120 tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
124 tsp
->smk_task
= task
;
125 tsp
->smk_forked
= forked
;
126 INIT_LIST_HEAD(&tsp
->smk_rules
);
127 mutex_init(&tsp
->smk_rules_lock
);
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
137 * Returns 0 on success, -ENOMEM on error
139 static int smk_copy_rules(struct list_head
*nhead
, struct list_head
*ohead
,
142 struct smack_rule
*nrp
;
143 struct smack_rule
*orp
;
146 INIT_LIST_HEAD(nhead
);
148 list_for_each_entry_rcu(orp
, ohead
, list
) {
149 nrp
= kzalloc(sizeof(struct smack_rule
), gfp
);
155 list_add_rcu(&nrp
->list
, nhead
);
162 * We he, that is fun!
166 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
167 * @ctp: child task pointer
168 * @mode: ptrace attachment mode
170 * Returns 0 if access is OK, an error code otherwise
172 * Do the capability checks, and require read and write.
174 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
177 struct smk_audit_info ad
;
178 struct smack_known
*skp
;
180 rc
= cap_ptrace_access_check(ctp
, mode
);
184 skp
= smk_of_task(task_security(ctp
));
185 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
186 smk_ad_setfield_u_tsk(&ad
, ctp
);
188 rc
= smk_curacc(skp
->smk_known
, MAY_READWRITE
, &ad
);
193 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
194 * @ptp: parent task pointer
196 * Returns 0 if access is OK, an error code otherwise
198 * Do the capability checks, and require read and write.
200 static int smack_ptrace_traceme(struct task_struct
*ptp
)
203 struct smk_audit_info ad
;
204 struct smack_known
*skp
;
206 rc
= cap_ptrace_traceme(ptp
);
210 skp
= smk_of_task(task_security(ptp
));
211 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
212 smk_ad_setfield_u_tsk(&ad
, ptp
);
214 rc
= smk_curacc(skp
->smk_known
, MAY_READWRITE
, &ad
);
219 * smack_syslog - Smack approval on syslog
220 * @type: message type
222 * Require that the task has the floor label
224 * Returns 0 on success, error code otherwise.
226 static int smack_syslog(int typefrom_file
)
229 struct smack_known
*skp
= smk_of_current();
231 if (smack_privileged(CAP_MAC_OVERRIDE
))
234 if (skp
!= &smack_known_floor
)
246 * smack_sb_alloc_security - allocate a superblock blob
247 * @sb: the superblock getting the blob
249 * Returns 0 on success or -ENOMEM on error.
251 static int smack_sb_alloc_security(struct super_block
*sb
)
253 struct superblock_smack
*sbsp
;
255 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
260 sbsp
->smk_root
= smack_known_floor
.smk_known
;
261 sbsp
->smk_default
= smack_known_floor
.smk_known
;
262 sbsp
->smk_floor
= smack_known_floor
.smk_known
;
263 sbsp
->smk_hat
= smack_known_hat
.smk_known
;
264 sbsp
->smk_initialized
= 0;
266 sb
->s_security
= sbsp
;
272 * smack_sb_free_security - free a superblock blob
273 * @sb: the superblock getting the blob
276 static void smack_sb_free_security(struct super_block
*sb
)
278 kfree(sb
->s_security
);
279 sb
->s_security
= NULL
;
283 * smack_sb_copy_data - copy mount options data for processing
284 * @orig: where to start
285 * @smackopts: mount options string
287 * Returns 0 on success or -ENOMEM on error.
289 * Copy the Smack specific mount options out of the mount
292 static int smack_sb_copy_data(char *orig
, char *smackopts
)
294 char *cp
, *commap
, *otheropts
, *dp
;
296 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
297 if (otheropts
== NULL
)
300 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
301 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
303 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
305 else if (strstr(cp
, SMK_FSHAT
) == cp
)
307 else if (strstr(cp
, SMK_FSROOT
) == cp
)
312 commap
= strchr(cp
, ',');
321 strcpy(orig
, otheropts
);
322 free_page((unsigned long)otheropts
);
328 * smack_sb_kern_mount - Smack specific mount processing
329 * @sb: the file system superblock
330 * @flags: the mount flags
331 * @data: the smack mount options
333 * Returns 0 on success, an error code on failure
335 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
337 struct dentry
*root
= sb
->s_root
;
338 struct inode
*inode
= root
->d_inode
;
339 struct superblock_smack
*sp
= sb
->s_security
;
340 struct inode_smack
*isp
;
345 if (sp
->smk_initialized
!= 0)
348 sp
->smk_initialized
= 1;
350 for (op
= data
; op
!= NULL
; op
= commap
) {
351 commap
= strchr(op
, ',');
355 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
356 op
+= strlen(SMK_FSHAT
);
357 nsp
= smk_import(op
, 0);
360 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
361 op
+= strlen(SMK_FSFLOOR
);
362 nsp
= smk_import(op
, 0);
365 } else if (strncmp(op
, SMK_FSDEFAULT
,
366 strlen(SMK_FSDEFAULT
)) == 0) {
367 op
+= strlen(SMK_FSDEFAULT
);
368 nsp
= smk_import(op
, 0);
370 sp
->smk_default
= nsp
;
371 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
372 op
+= strlen(SMK_FSROOT
);
373 nsp
= smk_import(op
, 0);
380 * Initialize the root inode.
382 isp
= inode
->i_security
;
384 inode
->i_security
= new_inode_smack(sp
->smk_root
);
386 isp
->smk_inode
= sp
->smk_root
;
392 * smack_sb_statfs - Smack check on statfs
393 * @dentry: identifies the file system in question
395 * Returns 0 if current can read the floor of the filesystem,
396 * and error code otherwise
398 static int smack_sb_statfs(struct dentry
*dentry
)
400 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
402 struct smk_audit_info ad
;
404 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
405 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
407 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
412 * smack_sb_mount - Smack check for mounting
419 * Returns 0 if current can write the floor of the filesystem
420 * being mounted on, an error code otherwise.
422 static int smack_sb_mount(const char *dev_name
, struct path
*path
,
423 const char *type
, unsigned long flags
, void *data
)
425 struct superblock_smack
*sbp
= path
->dentry
->d_sb
->s_security
;
426 struct smk_audit_info ad
;
428 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
429 smk_ad_setfield_u_fs_path(&ad
, *path
);
431 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
435 * smack_sb_umount - Smack check for unmounting
436 * @mnt: file system to unmount
439 * Returns 0 if current can write the floor of the filesystem
440 * being unmounted, an error code otherwise.
442 static int smack_sb_umount(struct vfsmount
*mnt
, int flags
)
444 struct superblock_smack
*sbp
;
445 struct smk_audit_info ad
;
448 path
.dentry
= mnt
->mnt_root
;
451 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
452 smk_ad_setfield_u_fs_path(&ad
, path
);
454 sbp
= path
.dentry
->d_sb
->s_security
;
455 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
463 * smack_bprm_set_creds - set creds for exec
464 * @bprm: the exec information
466 * Returns 0 if it gets a blob, -ENOMEM otherwise
468 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
470 struct inode
*inode
= file_inode(bprm
->file
);
471 struct task_smack
*bsp
= bprm
->cred
->security
;
472 struct inode_smack
*isp
;
475 rc
= cap_bprm_set_creds(bprm
);
479 if (bprm
->cred_prepared
)
482 isp
= inode
->i_security
;
483 if (isp
->smk_task
== NULL
|| isp
->smk_task
== bsp
->smk_task
)
489 bsp
->smk_task
= isp
->smk_task
;
490 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
496 * smack_bprm_committing_creds - Prepare to install the new credentials
499 * @bprm: binprm for exec
501 static void smack_bprm_committing_creds(struct linux_binprm
*bprm
)
503 struct task_smack
*bsp
= bprm
->cred
->security
;
505 if (bsp
->smk_task
!= bsp
->smk_forked
)
506 current
->pdeath_signal
= 0;
510 * smack_bprm_secureexec - Return the decision to use secureexec.
511 * @bprm: binprm for exec
513 * Returns 0 on success.
515 static int smack_bprm_secureexec(struct linux_binprm
*bprm
)
517 struct task_smack
*tsp
= current_security();
518 int ret
= cap_bprm_secureexec(bprm
);
520 if (!ret
&& (tsp
->smk_task
!= tsp
->smk_forked
))
531 * smack_inode_alloc_security - allocate an inode blob
532 * @inode: the inode in need of a blob
534 * Returns 0 if it gets a blob, -ENOMEM otherwise
536 static int smack_inode_alloc_security(struct inode
*inode
)
538 struct smack_known
*skp
= smk_of_current();
540 inode
->i_security
= new_inode_smack(skp
->smk_known
);
541 if (inode
->i_security
== NULL
)
547 * smack_inode_free_security - free an inode blob
548 * @inode: the inode with a blob
550 * Clears the blob pointer in inode
552 static void smack_inode_free_security(struct inode
*inode
)
554 kfree(inode
->i_security
);
555 inode
->i_security
= NULL
;
559 * smack_inode_init_security - copy out the smack from an inode
563 * @name: where to put the attribute name
564 * @value: where to put the attribute value
565 * @len: where to put the length of the attribute
567 * Returns 0 if it all works out, -ENOMEM if there's no memory
569 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
570 const struct qstr
*qstr
, char **name
,
571 void **value
, size_t *len
)
573 struct inode_smack
*issp
= inode
->i_security
;
574 struct smack_known
*skp
= smk_of_current();
575 char *isp
= smk_of_inode(inode
);
576 char *dsp
= smk_of_inode(dir
);
580 *name
= kstrdup(XATTR_SMACK_SUFFIX
, GFP_NOFS
);
587 may
= smk_access_entry(skp
->smk_known
, dsp
, &skp
->smk_rules
);
591 * If the access rule allows transmutation and
592 * the directory requests transmutation then
593 * by all means transmute.
594 * Mark the inode as changed.
596 if (may
> 0 && ((may
& MAY_TRANSMUTE
) != 0) &&
597 smk_inode_transmutable(dir
)) {
599 issp
->smk_flags
|= SMK_INODE_CHANGED
;
602 *value
= kstrdup(isp
, GFP_NOFS
);
608 *len
= strlen(isp
) + 1;
614 * smack_inode_link - Smack check on link
615 * @old_dentry: the existing object
617 * @new_dentry: the new object
619 * Returns 0 if access is permitted, an error code otherwise
621 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
622 struct dentry
*new_dentry
)
625 struct smk_audit_info ad
;
628 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
629 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
631 isp
= smk_of_inode(old_dentry
->d_inode
);
632 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
634 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
635 isp
= smk_of_inode(new_dentry
->d_inode
);
636 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
637 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
644 * smack_inode_unlink - Smack check on inode deletion
645 * @dir: containing directory object
646 * @dentry: file to unlink
648 * Returns 0 if current can write the containing directory
649 * and the object, error code otherwise
651 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
653 struct inode
*ip
= dentry
->d_inode
;
654 struct smk_audit_info ad
;
657 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
658 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
661 * You need write access to the thing you're unlinking
663 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
666 * You also need write access to the containing directory
668 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
669 smk_ad_setfield_u_fs_inode(&ad
, dir
);
670 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
676 * smack_inode_rmdir - Smack check on directory deletion
677 * @dir: containing directory object
678 * @dentry: directory to unlink
680 * Returns 0 if current can write the containing directory
681 * and the directory, error code otherwise
683 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
685 struct smk_audit_info ad
;
688 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
689 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
692 * You need write access to the thing you're removing
694 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
697 * You also need write access to the containing directory
699 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
700 smk_ad_setfield_u_fs_inode(&ad
, dir
);
701 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
708 * smack_inode_rename - Smack check on rename
709 * @old_inode: the old directory
710 * @old_dentry: unused
711 * @new_inode: the new directory
712 * @new_dentry: unused
714 * Read and write access is required on both the old and
717 * Returns 0 if access is permitted, an error code otherwise
719 static int smack_inode_rename(struct inode
*old_inode
,
720 struct dentry
*old_dentry
,
721 struct inode
*new_inode
,
722 struct dentry
*new_dentry
)
726 struct smk_audit_info ad
;
728 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
729 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
731 isp
= smk_of_inode(old_dentry
->d_inode
);
732 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
734 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
735 isp
= smk_of_inode(new_dentry
->d_inode
);
736 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
737 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
743 * smack_inode_permission - Smack version of permission()
744 * @inode: the inode in question
745 * @mask: the access requested
747 * This is the important Smack hook.
749 * Returns 0 if access is permitted, -EACCES otherwise
751 static int smack_inode_permission(struct inode
*inode
, int mask
)
753 struct smk_audit_info ad
;
754 int no_block
= mask
& MAY_NOT_BLOCK
;
756 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
758 * No permission to check. Existence test. Yup, it's there.
763 /* May be droppable after audit */
766 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_INODE
);
767 smk_ad_setfield_u_fs_inode(&ad
, inode
);
768 return smk_curacc(smk_of_inode(inode
), mask
, &ad
);
772 * smack_inode_setattr - Smack check for setting attributes
773 * @dentry: the object
774 * @iattr: for the force flag
776 * Returns 0 if access is permitted, an error code otherwise
778 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
780 struct smk_audit_info ad
;
782 * Need to allow for clearing the setuid bit.
784 if (iattr
->ia_valid
& ATTR_FORCE
)
786 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
787 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
789 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
793 * smack_inode_getattr - Smack check for getting attributes
795 * @dentry: the object
797 * Returns 0 if access is permitted, an error code otherwise
799 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
801 struct smk_audit_info ad
;
804 path
.dentry
= dentry
;
807 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
808 smk_ad_setfield_u_fs_path(&ad
, path
);
809 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
813 * smack_inode_setxattr - Smack check for setting xattrs
814 * @dentry: the object
815 * @name: name of the attribute
820 * This protects the Smack attribute explicitly.
822 * Returns 0 if access is permitted, an error code otherwise
824 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
825 const void *value
, size_t size
, int flags
)
827 struct smk_audit_info ad
;
830 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
831 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
832 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
833 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
834 strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
835 if (!smack_privileged(CAP_MAC_ADMIN
))
838 * check label validity here so import wont fail on
841 if (size
== 0 || size
>= SMK_LONGLABEL
||
842 smk_import(value
, size
) == NULL
)
844 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
845 if (!smack_privileged(CAP_MAC_ADMIN
))
847 if (size
!= TRANS_TRUE_SIZE
||
848 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
851 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
853 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
854 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
857 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
863 * smack_inode_post_setxattr - Apply the Smack update approved above
865 * @name: attribute name
866 * @value: attribute value
867 * @size: attribute size
870 * Set the pointer in the inode blob to the entry found
871 * in the master label list.
873 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
874 const void *value
, size_t size
, int flags
)
876 struct smack_known
*skp
;
877 struct inode_smack
*isp
= dentry
->d_inode
->i_security
;
879 if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
880 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
884 skp
= smk_import_entry(value
, size
);
885 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
887 isp
->smk_inode
= skp
->smk_known
;
889 isp
->smk_inode
= smack_known_invalid
.smk_known
;
890 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
894 isp
->smk_task
= &smack_known_invalid
;
895 } else if (strcmp(name
, XATTR_NAME_SMACKMMAP
) == 0) {
899 isp
->smk_mmap
= &smack_known_invalid
;
906 * smack_inode_getxattr - Smack check on getxattr
907 * @dentry: the object
910 * Returns 0 if access is permitted, an error code otherwise
912 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
914 struct smk_audit_info ad
;
916 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
917 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
919 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
923 * smack_inode_removexattr - Smack check on removexattr
924 * @dentry: the object
925 * @name: name of the attribute
927 * Removing the Smack attribute requires CAP_MAC_ADMIN
929 * Returns 0 if access is permitted, an error code otherwise
931 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
933 struct inode_smack
*isp
;
934 struct smk_audit_info ad
;
937 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
938 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
939 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
940 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
941 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0 ||
942 strcmp(name
, XATTR_NAME_SMACKMMAP
)) {
943 if (!smack_privileged(CAP_MAC_ADMIN
))
946 rc
= cap_inode_removexattr(dentry
, name
);
948 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_DENTRY
);
949 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
951 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
954 isp
= dentry
->d_inode
->i_security
;
955 isp
->smk_task
= NULL
;
956 isp
->smk_mmap
= NULL
;
963 * smack_inode_getsecurity - get smack xattrs
965 * @name: attribute name
966 * @buffer: where to put the result
969 * Returns the size of the attribute or an error code
971 static int smack_inode_getsecurity(const struct inode
*inode
,
972 const char *name
, void **buffer
,
975 struct socket_smack
*ssp
;
977 struct super_block
*sbp
;
978 struct inode
*ip
= (struct inode
*)inode
;
983 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
984 isp
= smk_of_inode(inode
);
985 ilen
= strlen(isp
) + 1;
991 * The rest of the Smack xattrs are only on sockets.
994 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
998 if (sock
== NULL
|| sock
->sk
== NULL
)
1001 ssp
= sock
->sk
->sk_security
;
1003 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1005 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
1006 isp
= ssp
->smk_out
->smk_known
;
1010 ilen
= strlen(isp
) + 1;
1021 * smack_inode_listsecurity - list the Smack attributes
1022 * @inode: the object
1023 * @buffer: where they go
1024 * @buffer_size: size of buffer
1026 * Returns 0 on success, -EINVAL otherwise
1028 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
1031 int len
= strlen(XATTR_NAME_SMACK
);
1033 if (buffer
!= NULL
&& len
<= buffer_size
) {
1034 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
1041 * smack_inode_getsecid - Extract inode's security id
1042 * @inode: inode to extract the info from
1043 * @secid: where result will be saved
1045 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
1047 struct inode_smack
*isp
= inode
->i_security
;
1049 *secid
= smack_to_secid(isp
->smk_inode
);
1057 * smack_file_permission - Smack check on file operations
1063 * Should access checks be done on each read or write?
1064 * UNICOS and SELinux say yes.
1065 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1067 * I'll say no for now. Smack does not do the frequent
1068 * label changing that SELinux does.
1070 static int smack_file_permission(struct file
*file
, int mask
)
1076 * smack_file_alloc_security - assign a file security blob
1079 * The security blob for a file is a pointer to the master
1080 * label list, so no allocation is done.
1084 static int smack_file_alloc_security(struct file
*file
)
1086 struct smack_known
*skp
= smk_of_current();
1088 file
->f_security
= skp
->smk_known
;
1093 * smack_file_free_security - clear a file security blob
1096 * The security blob for a file is a pointer to the master
1097 * label list, so no memory is freed.
1099 static void smack_file_free_security(struct file
*file
)
1101 file
->f_security
= NULL
;
1105 * smack_file_ioctl - Smack check on ioctls
1110 * Relies heavily on the correct use of the ioctl command conventions.
1112 * Returns 0 if allowed, error code otherwise
1114 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
1118 struct smk_audit_info ad
;
1120 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1121 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1123 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1124 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1126 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
))
1127 rc
= smk_curacc(file
->f_security
, MAY_READ
, &ad
);
1133 * smack_file_lock - Smack check on file locking
1137 * Returns 0 if current has write access, error code otherwise
1139 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1141 struct smk_audit_info ad
;
1143 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1144 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1145 return smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1149 * smack_file_fcntl - Smack check on fcntl
1151 * @cmd: what action to check
1154 * Generally these operations are harmless.
1155 * File locking operations present an obvious mechanism
1156 * for passing information, so they require write access.
1158 * Returns 0 if current has access, error code otherwise
1160 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1163 struct smk_audit_info ad
;
1173 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_PATH
);
1174 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1175 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1186 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1187 * if mapping anonymous memory.
1188 * @file contains the file structure for file to map (may be NULL).
1189 * @reqprot contains the protection requested by the application.
1190 * @prot contains the protection that will be applied by the kernel.
1191 * @flags contains the operational flags.
1192 * Return 0 if permission is granted.
1194 static int smack_mmap_file(struct file
*file
,
1195 unsigned long reqprot
, unsigned long prot
,
1196 unsigned long flags
)
1198 struct smack_known
*skp
;
1199 struct smack_known
*mkp
;
1200 struct smack_rule
*srp
;
1201 struct task_smack
*tsp
;
1203 struct inode_smack
*isp
;
1212 isp
= file_inode(file
)->i_security
;
1213 if (isp
->smk_mmap
== NULL
)
1215 mkp
= isp
->smk_mmap
;
1217 tsp
= current_security();
1218 skp
= smk_of_current();
1223 * For each Smack rule associated with the subject
1224 * label verify that the SMACK64MMAP also has access
1225 * to that rule's object label.
1227 list_for_each_entry_rcu(srp
, &skp
->smk_rules
, list
) {
1228 osmack
= srp
->smk_object
;
1230 * Matching labels always allows access.
1232 if (mkp
->smk_known
== osmack
)
1235 * If there is a matching local rule take
1236 * that into account as well.
1238 may
= smk_access_entry(srp
->smk_subject
->smk_known
, osmack
,
1241 may
= srp
->smk_access
;
1243 may
&= srp
->smk_access
;
1245 * If may is zero the SMACK64MMAP subject can't
1246 * possibly have less access.
1252 * Fetch the global list entry.
1253 * If there isn't one a SMACK64MMAP subject
1254 * can't have as much access as current.
1256 mmay
= smk_access_entry(mkp
->smk_known
, osmack
,
1258 if (mmay
== -ENOENT
) {
1263 * If there is a local entry it modifies the
1264 * potential access, too.
1266 tmay
= smk_access_entry(mkp
->smk_known
, osmack
,
1268 if (tmay
!= -ENOENT
)
1272 * If there is any access available to current that is
1273 * not available to a SMACK64MMAP subject
1276 if ((may
| mmay
) != mmay
) {
1288 * smack_file_set_fowner - set the file security blob value
1289 * @file: object in question
1292 * Further research may be required on this one.
1294 static int smack_file_set_fowner(struct file
*file
)
1296 struct smack_known
*skp
= smk_of_current();
1298 file
->f_security
= skp
->smk_known
;
1303 * smack_file_send_sigiotask - Smack on sigio
1304 * @tsk: The target task
1305 * @fown: the object the signal come from
1308 * Allow a privileged task to get signals even if it shouldn't
1310 * Returns 0 if a subject with the object's smack could
1311 * write to the task, an error code otherwise.
1313 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1314 struct fown_struct
*fown
, int signum
)
1316 struct smack_known
*skp
;
1317 struct smack_known
*tkp
= smk_of_task(tsk
->cred
->security
);
1320 struct smk_audit_info ad
;
1323 * struct fown_struct is never outside the context of a struct file
1325 file
= container_of(fown
, struct file
, f_owner
);
1327 /* we don't log here as rc can be overriden */
1328 skp
= smk_find_entry(file
->f_security
);
1329 rc
= smk_access(skp
, tkp
->smk_known
, MAY_WRITE
, NULL
);
1330 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1333 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1334 smk_ad_setfield_u_tsk(&ad
, tsk
);
1335 smack_log(file
->f_security
, tkp
->smk_known
, MAY_WRITE
, rc
, &ad
);
1340 * smack_file_receive - Smack file receive check
1343 * Returns 0 if current has access, error code otherwise
1345 static int smack_file_receive(struct file
*file
)
1348 struct smk_audit_info ad
;
1350 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1351 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1353 * This code relies on bitmasks.
1355 if (file
->f_mode
& FMODE_READ
)
1357 if (file
->f_mode
& FMODE_WRITE
)
1360 return smk_curacc(file
->f_security
, may
, &ad
);
1364 * smack_file_open - Smack dentry open processing
1368 * Set the security blob in the file structure.
1372 static int smack_file_open(struct file
*file
, const struct cred
*cred
)
1374 struct inode_smack
*isp
= file_inode(file
)->i_security
;
1376 file
->f_security
= isp
->smk_inode
;
1386 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1387 * @new: the new credentials
1388 * @gfp: the atomicity of any memory allocations
1390 * Prepare a blank set of credentials for modification. This must allocate all
1391 * the memory the LSM module might require such that cred_transfer() can
1392 * complete without error.
1394 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1396 struct task_smack
*tsp
;
1398 tsp
= new_task_smack(NULL
, NULL
, gfp
);
1402 cred
->security
= tsp
;
1409 * smack_cred_free - "free" task-level security credentials
1410 * @cred: the credentials in question
1413 static void smack_cred_free(struct cred
*cred
)
1415 struct task_smack
*tsp
= cred
->security
;
1416 struct smack_rule
*rp
;
1417 struct list_head
*l
;
1418 struct list_head
*n
;
1422 cred
->security
= NULL
;
1424 list_for_each_safe(l
, n
, &tsp
->smk_rules
) {
1425 rp
= list_entry(l
, struct smack_rule
, list
);
1426 list_del(&rp
->list
);
1433 * smack_cred_prepare - prepare new set of credentials for modification
1434 * @new: the new credentials
1435 * @old: the original credentials
1436 * @gfp: the atomicity of any memory allocations
1438 * Prepare a new set of credentials for modification.
1440 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1443 struct task_smack
*old_tsp
= old
->security
;
1444 struct task_smack
*new_tsp
;
1447 new_tsp
= new_task_smack(old_tsp
->smk_task
, old_tsp
->smk_task
, gfp
);
1448 if (new_tsp
== NULL
)
1451 rc
= smk_copy_rules(&new_tsp
->smk_rules
, &old_tsp
->smk_rules
, gfp
);
1455 new->security
= new_tsp
;
1460 * smack_cred_transfer - Transfer the old credentials to the new credentials
1461 * @new: the new credentials
1462 * @old: the original credentials
1464 * Fill in a set of blank credentials from another set of credentials.
1466 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1468 struct task_smack
*old_tsp
= old
->security
;
1469 struct task_smack
*new_tsp
= new->security
;
1471 new_tsp
->smk_task
= old_tsp
->smk_task
;
1472 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1473 mutex_init(&new_tsp
->smk_rules_lock
);
1474 INIT_LIST_HEAD(&new_tsp
->smk_rules
);
1477 /* cbs copy rule list */
1481 * smack_kernel_act_as - Set the subjective context in a set of credentials
1482 * @new: points to the set of credentials to be modified.
1483 * @secid: specifies the security ID to be set
1485 * Set the security data for a kernel service.
1487 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1489 struct task_smack
*new_tsp
= new->security
;
1490 struct smack_known
*skp
= smack_from_secid(secid
);
1495 new_tsp
->smk_task
= skp
;
1500 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1501 * @new: points to the set of credentials to be modified
1502 * @inode: points to the inode to use as a reference
1504 * Set the file creation context in a set of credentials to the same
1505 * as the objective context of the specified inode
1507 static int smack_kernel_create_files_as(struct cred
*new,
1508 struct inode
*inode
)
1510 struct inode_smack
*isp
= inode
->i_security
;
1511 struct task_smack
*tsp
= new->security
;
1513 tsp
->smk_forked
= smk_find_entry(isp
->smk_inode
);
1514 tsp
->smk_task
= tsp
->smk_forked
;
1519 * smk_curacc_on_task - helper to log task related access
1520 * @p: the task object
1521 * @access: the access requested
1522 * @caller: name of the calling function for audit
1524 * Return 0 if access is permitted
1526 static int smk_curacc_on_task(struct task_struct
*p
, int access
,
1529 struct smk_audit_info ad
;
1530 struct smack_known
*skp
= smk_of_task(task_security(p
));
1532 smk_ad_init(&ad
, caller
, LSM_AUDIT_DATA_TASK
);
1533 smk_ad_setfield_u_tsk(&ad
, p
);
1534 return smk_curacc(skp
->smk_known
, access
, &ad
);
1538 * smack_task_setpgid - Smack check on setting pgid
1539 * @p: the task object
1542 * Return 0 if write access is permitted
1544 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1546 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1550 * smack_task_getpgid - Smack access check for getpgid
1551 * @p: the object task
1553 * Returns 0 if current can read the object task, error code otherwise
1555 static int smack_task_getpgid(struct task_struct
*p
)
1557 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1561 * smack_task_getsid - Smack access check for getsid
1562 * @p: the object task
1564 * Returns 0 if current can read the object task, error code otherwise
1566 static int smack_task_getsid(struct task_struct
*p
)
1568 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1572 * smack_task_getsecid - get the secid of the task
1573 * @p: the object task
1574 * @secid: where to put the result
1576 * Sets the secid to contain a u32 version of the smack label.
1578 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1580 struct smack_known
*skp
= smk_of_task(task_security(p
));
1582 *secid
= skp
->smk_secid
;
1586 * smack_task_setnice - Smack check on setting nice
1587 * @p: the task object
1590 * Return 0 if write access is permitted
1592 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1596 rc
= cap_task_setnice(p
, nice
);
1598 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1603 * smack_task_setioprio - Smack check on setting ioprio
1604 * @p: the task object
1607 * Return 0 if write access is permitted
1609 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1613 rc
= cap_task_setioprio(p
, ioprio
);
1615 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1620 * smack_task_getioprio - Smack check on reading ioprio
1621 * @p: the task object
1623 * Return 0 if read access is permitted
1625 static int smack_task_getioprio(struct task_struct
*p
)
1627 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1631 * smack_task_setscheduler - Smack check on setting scheduler
1632 * @p: the task object
1636 * Return 0 if read access is permitted
1638 static int smack_task_setscheduler(struct task_struct
*p
)
1642 rc
= cap_task_setscheduler(p
);
1644 rc
= smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1649 * smack_task_getscheduler - Smack check on reading scheduler
1650 * @p: the task object
1652 * Return 0 if read access is permitted
1654 static int smack_task_getscheduler(struct task_struct
*p
)
1656 return smk_curacc_on_task(p
, MAY_READ
, __func__
);
1660 * smack_task_movememory - Smack check on moving memory
1661 * @p: the task object
1663 * Return 0 if write access is permitted
1665 static int smack_task_movememory(struct task_struct
*p
)
1667 return smk_curacc_on_task(p
, MAY_WRITE
, __func__
);
1671 * smack_task_kill - Smack check on signal delivery
1672 * @p: the task object
1675 * @secid: identifies the smack to use in lieu of current's
1677 * Return 0 if write access is permitted
1679 * The secid behavior is an artifact of an SELinux hack
1680 * in the USB code. Someday it may go away.
1682 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1685 struct smk_audit_info ad
;
1686 struct smack_known
*skp
;
1687 struct smack_known
*tkp
= smk_of_task(task_security(p
));
1689 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1690 smk_ad_setfield_u_tsk(&ad
, p
);
1692 * Sending a signal requires that the sender
1693 * can write the receiver.
1696 return smk_curacc(tkp
->smk_known
, MAY_WRITE
, &ad
);
1698 * If the secid isn't 0 we're dealing with some USB IO
1699 * specific behavior. This is not clean. For one thing
1700 * we can't take privilege into account.
1702 skp
= smack_from_secid(secid
);
1703 return smk_access(skp
, tkp
->smk_known
, MAY_WRITE
, &ad
);
1707 * smack_task_wait - Smack access check for waiting
1708 * @p: task to wait for
1712 static int smack_task_wait(struct task_struct
*p
)
1715 * Allow the operation to succeed.
1717 * In userless environments (e.g. phones) programs
1718 * get marked with SMACK64EXEC and even if the parent
1719 * and child shouldn't be talking the parent still
1720 * may expect to know when the child exits.
1726 * smack_task_to_inode - copy task smack into the inode blob
1727 * @p: task to copy from
1728 * @inode: inode to copy to
1730 * Sets the smack pointer in the inode security blob
1732 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
1734 struct inode_smack
*isp
= inode
->i_security
;
1735 struct smack_known
*skp
= smk_of_task(task_security(p
));
1737 isp
->smk_inode
= skp
->smk_known
;
1745 * smack_sk_alloc_security - Allocate a socket blob
1748 * @gfp_flags: memory allocation flags
1750 * Assign Smack pointers to current
1752 * Returns 0 on success, -ENOMEM is there's no memory
1754 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
1756 struct smack_known
*skp
= smk_of_current();
1757 struct socket_smack
*ssp
;
1759 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
1763 ssp
->smk_in
= skp
->smk_known
;
1765 ssp
->smk_packet
= NULL
;
1767 sk
->sk_security
= ssp
;
1773 * smack_sk_free_security - Free a socket blob
1776 * Clears the blob pointer
1778 static void smack_sk_free_security(struct sock
*sk
)
1780 kfree(sk
->sk_security
);
1784 * smack_host_label - check host based restrictions
1785 * @sip: the object end
1787 * looks for host based access restrictions
1789 * This version will only be appropriate for really small sets of single label
1790 * hosts. The caller is responsible for ensuring that the RCU read lock is
1791 * taken before calling this function.
1793 * Returns the label of the far end or NULL if it's not special.
1795 static char *smack_host_label(struct sockaddr_in
*sip
)
1797 struct smk_netlbladdr
*snp
;
1798 struct in_addr
*siap
= &sip
->sin_addr
;
1800 if (siap
->s_addr
== 0)
1803 list_for_each_entry_rcu(snp
, &smk_netlbladdr_list
, list
)
1805 * we break after finding the first match because
1806 * the list is sorted from longest to shortest mask
1807 * so we have found the most specific match
1809 if ((&snp
->smk_host
.sin_addr
)->s_addr
==
1810 (siap
->s_addr
& (&snp
->smk_mask
)->s_addr
)) {
1811 /* we have found the special CIPSO option */
1812 if (snp
->smk_label
== smack_cipso_option
)
1814 return snp
->smk_label
;
1821 * smack_netlabel - Set the secattr on a socket
1823 * @labeled: socket label scheme
1825 * Convert the outbound smack value (smk_out) to a
1826 * secattr and attach it to the socket.
1828 * Returns 0 on success or an error code
1830 static int smack_netlabel(struct sock
*sk
, int labeled
)
1832 struct smack_known
*skp
;
1833 struct socket_smack
*ssp
= sk
->sk_security
;
1837 * Usually the netlabel code will handle changing the
1838 * packet labeling based on the label.
1839 * The case of a single label host is different, because
1840 * a single label host should never get a labeled packet
1841 * even though the label is usually associated with a packet
1845 bh_lock_sock_nested(sk
);
1847 if (ssp
->smk_out
== smack_net_ambient
||
1848 labeled
== SMACK_UNLABELED_SOCKET
)
1849 netlbl_sock_delattr(sk
);
1852 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &skp
->smk_netlabel
);
1862 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1864 * @sap: the destination address
1866 * Set the correct secattr for the given socket based on the destination
1867 * address and perform any outbound access checks needed.
1869 * Returns 0 on success or an error code.
1872 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
1874 struct smack_known
*skp
;
1878 struct socket_smack
*ssp
= sk
->sk_security
;
1879 struct smk_audit_info ad
;
1882 hostsp
= smack_host_label(sap
);
1883 if (hostsp
!= NULL
) {
1885 struct lsm_network_audit net
;
1887 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
1888 ad
.a
.u
.net
->family
= sap
->sin_family
;
1889 ad
.a
.u
.net
->dport
= sap
->sin_port
;
1890 ad
.a
.u
.net
->v4info
.daddr
= sap
->sin_addr
.s_addr
;
1892 sk_lbl
= SMACK_UNLABELED_SOCKET
;
1894 rc
= smk_access(skp
, hostsp
, MAY_WRITE
, &ad
);
1896 sk_lbl
= SMACK_CIPSO_SOCKET
;
1903 return smack_netlabel(sk
, sk_lbl
);
1907 * smk_ipv6_port_label - Smack port access table management
1911 * Create or update the port list entry
1913 static void smk_ipv6_port_label(struct socket
*sock
, struct sockaddr
*address
)
1915 struct sock
*sk
= sock
->sk
;
1916 struct sockaddr_in6
*addr6
;
1917 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
1918 struct smk_port_label
*spp
;
1919 unsigned short port
= 0;
1921 if (address
== NULL
) {
1923 * This operation is changing the Smack information
1924 * on the bound socket. Take the changes to the port
1927 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
1928 if (sk
!= spp
->smk_sock
)
1930 spp
->smk_in
= ssp
->smk_in
;
1931 spp
->smk_out
= ssp
->smk_out
;
1935 * A NULL address is only used for updating existing
1936 * bound entries. If there isn't one, it's OK.
1941 addr6
= (struct sockaddr_in6
*)address
;
1942 port
= ntohs(addr6
->sin6_port
);
1944 * This is a special case that is safely ignored.
1950 * Look for an existing port list entry.
1951 * This is an indication that a port is getting reused.
1953 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
1954 if (spp
->smk_port
!= port
)
1956 spp
->smk_port
= port
;
1958 spp
->smk_in
= ssp
->smk_in
;
1959 spp
->smk_out
= ssp
->smk_out
;
1964 * A new port entry is required.
1966 spp
= kzalloc(sizeof(*spp
), GFP_KERNEL
);
1970 spp
->smk_port
= port
;
1972 spp
->smk_in
= ssp
->smk_in
;
1973 spp
->smk_out
= ssp
->smk_out
;
1975 list_add(&spp
->list
, &smk_ipv6_port_list
);
1980 * smk_ipv6_port_check - check Smack port access
1984 * Create or update the port list entry
1986 static int smk_ipv6_port_check(struct sock
*sk
, struct sockaddr
*address
,
1991 struct sockaddr_in6
*addr6
;
1992 struct smk_port_label
*spp
;
1993 struct socket_smack
*ssp
= sk
->sk_security
;
1994 struct smack_known
*skp
;
1995 unsigned short port
= 0;
1997 struct smk_audit_info ad
;
1999 struct lsm_network_audit net
;
2002 if (act
== SMK_RECEIVING
) {
2003 skp
= smack_net_ambient
;
2004 object
= ssp
->smk_in
;
2007 object
= smack_net_ambient
->smk_known
;
2011 * Get the IP address and port from the address.
2013 addr6
= (struct sockaddr_in6
*)address
;
2014 port
= ntohs(addr6
->sin6_port
);
2015 bep
= (__be16
*)(&addr6
->sin6_addr
);
2016 be32p
= (__be32
*)(&addr6
->sin6_addr
);
2019 * It's remote, so port lookup does no good.
2021 if (be32p
[0] || be32p
[1] || be32p
[2] || bep
[6] || ntohs(bep
[7]) != 1)
2025 * It's local so the send check has to have passed.
2027 if (act
== SMK_RECEIVING
) {
2028 skp
= &smack_known_web
;
2032 list_for_each_entry(spp
, &smk_ipv6_port_list
, list
) {
2033 if (spp
->smk_port
!= port
)
2035 object
= spp
->smk_in
;
2036 if (act
== SMK_CONNECTING
)
2037 ssp
->smk_packet
= spp
->smk_out
->smk_known
;
2044 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2045 ad
.a
.u
.net
->family
= sk
->sk_family
;
2046 ad
.a
.u
.net
->dport
= port
;
2047 if (act
== SMK_RECEIVING
)
2048 ad
.a
.u
.net
->v6info
.saddr
= addr6
->sin6_addr
;
2050 ad
.a
.u
.net
->v6info
.daddr
= addr6
->sin6_addr
;
2052 return smk_access(skp
, object
, MAY_WRITE
, &ad
);
2056 * smack_inode_setsecurity - set smack xattrs
2057 * @inode: the object
2058 * @name: attribute name
2059 * @value: attribute value
2060 * @size: size of the attribute
2063 * Sets the named attribute in the appropriate blob
2065 * Returns 0 on success, or an error code
2067 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
2068 const void *value
, size_t size
, int flags
)
2070 struct smack_known
*skp
;
2071 struct inode_smack
*nsp
= inode
->i_security
;
2072 struct socket_smack
*ssp
;
2073 struct socket
*sock
;
2076 if (value
== NULL
|| size
> SMK_LONGLABEL
|| size
== 0)
2079 skp
= smk_import_entry(value
, size
);
2083 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
2084 nsp
->smk_inode
= skp
->smk_known
;
2085 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
2089 * The rest of the Smack xattrs are only on sockets.
2091 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2094 sock
= SOCKET_I(inode
);
2095 if (sock
== NULL
|| sock
->sk
== NULL
)
2098 ssp
= sock
->sk
->sk_security
;
2100 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
2101 ssp
->smk_in
= skp
->smk_known
;
2102 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
2104 if (sock
->sk
->sk_family
== PF_INET
) {
2105 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2108 "Smack: \"%s\" netlbl error %d.\n",
2114 if (sock
->sk
->sk_family
== PF_INET6
)
2115 smk_ipv6_port_label(sock
, NULL
);
2121 * smack_socket_post_create - finish socket setup
2123 * @family: protocol family
2128 * Sets the netlabel information on the socket
2130 * Returns 0 on success, and error code otherwise
2132 static int smack_socket_post_create(struct socket
*sock
, int family
,
2133 int type
, int protocol
, int kern
)
2135 if (family
!= PF_INET
|| sock
->sk
== NULL
)
2138 * Set the outbound netlbl.
2140 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
2144 * smack_socket_bind - record port binding information.
2146 * @address: the port address
2147 * @addrlen: size of the address
2149 * Records the label bound to a port.
2153 static int smack_socket_bind(struct socket
*sock
, struct sockaddr
*address
,
2156 if (sock
->sk
!= NULL
&& sock
->sk
->sk_family
== PF_INET6
)
2157 smk_ipv6_port_label(sock
, address
);
2163 * smack_socket_connect - connect access check
2165 * @sap: the other end
2166 * @addrlen: size of sap
2168 * Verifies that a connection may be possible
2170 * Returns 0 on success, and error code otherwise
2172 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
2177 if (sock
->sk
== NULL
)
2180 switch (sock
->sk
->sk_family
) {
2182 if (addrlen
< sizeof(struct sockaddr_in
))
2184 rc
= smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
2187 if (addrlen
< sizeof(struct sockaddr_in6
))
2189 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_CONNECTING
);
2196 * smack_flags_to_may - convert S_ to MAY_ values
2197 * @flags: the S_ value
2199 * Returns the equivalent MAY_ value
2201 static int smack_flags_to_may(int flags
)
2205 if (flags
& S_IRUGO
)
2207 if (flags
& S_IWUGO
)
2209 if (flags
& S_IXUGO
)
2216 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2221 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
2223 struct smack_known
*skp
= smk_of_current();
2225 msg
->security
= skp
->smk_known
;
2230 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2233 * Clears the blob pointer
2235 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
2237 msg
->security
= NULL
;
2241 * smack_of_shm - the smack pointer for the shm
2244 * Returns a pointer to the smack value
2246 static char *smack_of_shm(struct shmid_kernel
*shp
)
2248 return (char *)shp
->shm_perm
.security
;
2252 * smack_shm_alloc_security - Set the security blob for shm
2257 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
2259 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2260 struct smack_known
*skp
= smk_of_current();
2262 isp
->security
= skp
->smk_known
;
2267 * smack_shm_free_security - Clear the security blob for shm
2270 * Clears the blob pointer
2272 static void smack_shm_free_security(struct shmid_kernel
*shp
)
2274 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
2276 isp
->security
= NULL
;
2280 * smk_curacc_shm : check if current has access on shm
2282 * @access : access requested
2284 * Returns 0 if current has the requested access, error code otherwise
2286 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
2288 char *ssp
= smack_of_shm(shp
);
2289 struct smk_audit_info ad
;
2292 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2293 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
2295 return smk_curacc(ssp
, access
, &ad
);
2299 * smack_shm_associate - Smack access check for shm
2301 * @shmflg: access requested
2303 * Returns 0 if current has the requested access, error code otherwise
2305 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
2309 may
= smack_flags_to_may(shmflg
);
2310 return smk_curacc_shm(shp
, may
);
2314 * smack_shm_shmctl - Smack access check for shm
2316 * @cmd: what it wants to do
2318 * Returns 0 if current has the requested access, error code otherwise
2320 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
2333 may
= MAY_READWRITE
;
2338 * System level information.
2344 return smk_curacc_shm(shp
, may
);
2348 * smack_shm_shmat - Smack access for shmat
2351 * @shmflg: access requested
2353 * Returns 0 if current has the requested access, error code otherwise
2355 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
2360 may
= smack_flags_to_may(shmflg
);
2361 return smk_curacc_shm(shp
, may
);
2365 * smack_of_sem - the smack pointer for the sem
2368 * Returns a pointer to the smack value
2370 static char *smack_of_sem(struct sem_array
*sma
)
2372 return (char *)sma
->sem_perm
.security
;
2376 * smack_sem_alloc_security - Set the security blob for sem
2381 static int smack_sem_alloc_security(struct sem_array
*sma
)
2383 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2384 struct smack_known
*skp
= smk_of_current();
2386 isp
->security
= skp
->smk_known
;
2391 * smack_sem_free_security - Clear the security blob for sem
2394 * Clears the blob pointer
2396 static void smack_sem_free_security(struct sem_array
*sma
)
2398 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2400 isp
->security
= NULL
;
2404 * smk_curacc_sem : check if current has access on sem
2406 * @access : access requested
2408 * Returns 0 if current has the requested access, error code otherwise
2410 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2412 char *ssp
= smack_of_sem(sma
);
2413 struct smk_audit_info ad
;
2416 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2417 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2419 return smk_curacc(ssp
, access
, &ad
);
2423 * smack_sem_associate - Smack access check for sem
2425 * @semflg: access requested
2427 * Returns 0 if current has the requested access, error code otherwise
2429 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2433 may
= smack_flags_to_may(semflg
);
2434 return smk_curacc_sem(sma
, may
);
2438 * smack_sem_shmctl - Smack access check for sem
2440 * @cmd: what it wants to do
2442 * Returns 0 if current has the requested access, error code otherwise
2444 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
2462 may
= MAY_READWRITE
;
2467 * System level information
2474 return smk_curacc_sem(sma
, may
);
2478 * smack_sem_semop - Smack checks of semaphore operations
2484 * Treated as read and write in all cases.
2486 * Returns 0 if access is allowed, error code otherwise
2488 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
2489 unsigned nsops
, int alter
)
2491 return smk_curacc_sem(sma
, MAY_READWRITE
);
2495 * smack_msg_alloc_security - Set the security blob for msg
2500 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
2502 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2503 struct smack_known
*skp
= smk_of_current();
2505 kisp
->security
= skp
->smk_known
;
2510 * smack_msg_free_security - Clear the security blob for msg
2513 * Clears the blob pointer
2515 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
2517 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2519 kisp
->security
= NULL
;
2523 * smack_of_msq - the smack pointer for the msq
2526 * Returns a pointer to the smack value
2528 static char *smack_of_msq(struct msg_queue
*msq
)
2530 return (char *)msq
->q_perm
.security
;
2534 * smk_curacc_msq : helper to check if current has access on msq
2536 * @access : access requested
2538 * return 0 if current has access, error otherwise
2540 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
2542 char *msp
= smack_of_msq(msq
);
2543 struct smk_audit_info ad
;
2546 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2547 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
2549 return smk_curacc(msp
, access
, &ad
);
2553 * smack_msg_queue_associate - Smack access check for msg_queue
2555 * @msqflg: access requested
2557 * Returns 0 if current has the requested access, error code otherwise
2559 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
2563 may
= smack_flags_to_may(msqflg
);
2564 return smk_curacc_msq(msq
, may
);
2568 * smack_msg_queue_msgctl - Smack access check for msg_queue
2570 * @cmd: what it wants to do
2572 * Returns 0 if current has the requested access, error code otherwise
2574 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
2585 may
= MAY_READWRITE
;
2590 * System level information
2597 return smk_curacc_msq(msq
, may
);
2601 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2604 * @msqflg: access requested
2606 * Returns 0 if current has the requested access, error code otherwise
2608 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
2613 may
= smack_flags_to_may(msqflg
);
2614 return smk_curacc_msq(msq
, may
);
2618 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2625 * Returns 0 if current has read and write access, error code otherwise
2627 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
2628 struct task_struct
*target
, long type
, int mode
)
2630 return smk_curacc_msq(msq
, MAY_READWRITE
);
2634 * smack_ipc_permission - Smack access for ipc_permission()
2635 * @ipp: the object permissions
2636 * @flag: access requested
2638 * Returns 0 if current has read and write access, error code otherwise
2640 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
2642 char *isp
= ipp
->security
;
2643 int may
= smack_flags_to_may(flag
);
2644 struct smk_audit_info ad
;
2647 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2648 ad
.a
.u
.ipc_id
= ipp
->id
;
2650 return smk_curacc(isp
, may
, &ad
);
2654 * smack_ipc_getsecid - Extract smack security id
2655 * @ipp: the object permissions
2656 * @secid: where result will be saved
2658 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
2660 char *smack
= ipp
->security
;
2662 *secid
= smack_to_secid(smack
);
2666 * smack_d_instantiate - Make sure the blob is correct on an inode
2667 * @opt_dentry: dentry where inode will be attached
2668 * @inode: the object
2670 * Set the inode's security blob if it hasn't been done already.
2672 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
2674 struct super_block
*sbp
;
2675 struct superblock_smack
*sbsp
;
2676 struct inode_smack
*isp
;
2677 struct smack_known
*skp
;
2678 struct smack_known
*ckp
= smk_of_current();
2680 char trattr
[TRANS_TRUE_SIZE
];
2688 isp
= inode
->i_security
;
2690 mutex_lock(&isp
->smk_lock
);
2692 * If the inode is already instantiated
2693 * take the quick way out
2695 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
2699 sbsp
= sbp
->s_security
;
2701 * We're going to use the superblock default label
2702 * if there's no label on the file.
2704 final
= sbsp
->smk_default
;
2707 * If this is the root inode the superblock
2708 * may be in the process of initialization.
2709 * If that is the case use the root value out
2710 * of the superblock.
2712 if (opt_dentry
->d_parent
== opt_dentry
) {
2713 isp
->smk_inode
= sbsp
->smk_root
;
2714 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2719 * This is pretty hackish.
2720 * Casey says that we shouldn't have to do
2721 * file system specific code, but it does help
2722 * with keeping it simple.
2724 switch (sbp
->s_magic
) {
2727 * Casey says that it's a little embarrassing
2728 * that the smack file system doesn't do
2729 * extended attributes.
2731 final
= smack_known_star
.smk_known
;
2735 * Casey says pipes are easy (?)
2737 final
= smack_known_star
.smk_known
;
2739 case DEVPTS_SUPER_MAGIC
:
2741 * devpts seems content with the label of the task.
2742 * Programs that change smack have to treat the
2745 final
= ckp
->smk_known
;
2749 * Socket access is controlled by the socket
2750 * structures associated with the task involved.
2752 final
= smack_known_star
.smk_known
;
2754 case PROC_SUPER_MAGIC
:
2756 * Casey says procfs appears not to care.
2757 * The superblock default suffices.
2762 * Device labels should come from the filesystem,
2763 * but watch out, because they're volitile,
2764 * getting recreated on every reboot.
2766 final
= smack_known_star
.smk_known
;
2770 * If a smack value has been set we want to use it,
2771 * but since tmpfs isn't giving us the opportunity
2772 * to set mount options simulate setting the
2773 * superblock default.
2777 * This isn't an understood special case.
2778 * Get the value from the xattr.
2782 * UNIX domain sockets use lower level socket data.
2784 if (S_ISSOCK(inode
->i_mode
)) {
2785 final
= smack_known_star
.smk_known
;
2789 * No xattr support means, alas, no SMACK label.
2790 * Use the aforeapplied default.
2791 * It would be curious if the label of the task
2792 * does not match that assigned.
2794 if (inode
->i_op
->getxattr
== NULL
)
2797 * Get the dentry for xattr.
2799 dp
= dget(opt_dentry
);
2800 skp
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
2802 final
= skp
->smk_known
;
2805 * Transmuting directory
2807 if (S_ISDIR(inode
->i_mode
)) {
2809 * If this is a new directory and the label was
2810 * transmuted when the inode was initialized
2811 * set the transmute attribute on the directory
2812 * and mark the inode.
2814 * If there is a transmute attribute on the
2815 * directory mark the inode.
2817 if (isp
->smk_flags
& SMK_INODE_CHANGED
) {
2818 isp
->smk_flags
&= ~SMK_INODE_CHANGED
;
2819 rc
= inode
->i_op
->setxattr(dp
,
2820 XATTR_NAME_SMACKTRANSMUTE
,
2821 TRANS_TRUE
, TRANS_TRUE_SIZE
,
2824 rc
= inode
->i_op
->getxattr(dp
,
2825 XATTR_NAME_SMACKTRANSMUTE
, trattr
,
2827 if (rc
>= 0 && strncmp(trattr
, TRANS_TRUE
,
2828 TRANS_TRUE_SIZE
) != 0)
2832 transflag
= SMK_INODE_TRANSMUTE
;
2834 isp
->smk_task
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
2835 isp
->smk_mmap
= smk_fetch(XATTR_NAME_SMACKMMAP
, inode
, dp
);
2842 isp
->smk_inode
= ckp
->smk_known
;
2844 isp
->smk_inode
= final
;
2846 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
2849 mutex_unlock(&isp
->smk_lock
);
2854 * smack_getprocattr - Smack process attribute access
2855 * @p: the object task
2856 * @name: the name of the attribute in /proc/.../attr
2857 * @value: where to put the result
2859 * Places a copy of the task Smack into value
2861 * Returns the length of the smack label or an error code
2863 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
2865 struct smack_known
*skp
= smk_of_task(task_security(p
));
2869 if (strcmp(name
, "current") != 0)
2872 cp
= kstrdup(skp
->smk_known
, GFP_KERNEL
);
2882 * smack_setprocattr - Smack process attribute setting
2883 * @p: the object task
2884 * @name: the name of the attribute in /proc/.../attr
2885 * @value: the value to set
2886 * @size: the size of the value
2888 * Sets the Smack value of the task. Only setting self
2889 * is permitted and only with privilege
2891 * Returns the length of the smack label or an error code
2893 static int smack_setprocattr(struct task_struct
*p
, char *name
,
2894 void *value
, size_t size
)
2896 struct task_smack
*tsp
;
2898 struct smack_known
*skp
;
2901 * Changing another process' Smack value is too dangerous
2902 * and supports no sane use case.
2907 if (!smack_privileged(CAP_MAC_ADMIN
))
2910 if (value
== NULL
|| size
== 0 || size
>= SMK_LONGLABEL
)
2913 if (strcmp(name
, "current") != 0)
2916 skp
= smk_import_entry(value
, size
);
2921 * No process is ever allowed the web ("@") label.
2923 if (skp
== &smack_known_web
)
2926 new = prepare_creds();
2930 tsp
= new->security
;
2931 tsp
->smk_task
= skp
;
2938 * smack_unix_stream_connect - Smack access on UDS
2940 * @other: the other sock
2943 * Return 0 if a subject with the smack of sock could access
2944 * an object with the smack of other, otherwise an error code
2946 static int smack_unix_stream_connect(struct sock
*sock
,
2947 struct sock
*other
, struct sock
*newsk
)
2949 struct smack_known
*skp
;
2950 struct socket_smack
*ssp
= sock
->sk_security
;
2951 struct socket_smack
*osp
= other
->sk_security
;
2952 struct socket_smack
*nsp
= newsk
->sk_security
;
2953 struct smk_audit_info ad
;
2957 struct lsm_network_audit net
;
2959 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2960 smk_ad_setfield_u_net_sk(&ad
, other
);
2963 if (!smack_privileged(CAP_MAC_OVERRIDE
)) {
2965 rc
= smk_access(skp
, osp
->smk_in
, MAY_WRITE
, &ad
);
2969 * Cross reference the peer labels for SO_PEERSEC.
2972 nsp
->smk_packet
= ssp
->smk_out
->smk_known
;
2973 ssp
->smk_packet
= osp
->smk_out
->smk_known
;
2980 * smack_unix_may_send - Smack access on UDS
2982 * @other: the other socket
2984 * Return 0 if a subject with the smack of sock could access
2985 * an object with the smack of other, otherwise an error code
2987 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
2989 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2990 struct socket_smack
*osp
= other
->sk
->sk_security
;
2991 struct smack_known
*skp
;
2992 struct smk_audit_info ad
;
2995 struct lsm_network_audit net
;
2997 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
2998 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
3001 if (smack_privileged(CAP_MAC_OVERRIDE
))
3005 return smk_access(skp
, osp
->smk_in
, MAY_WRITE
, &ad
);
3009 * smack_socket_sendmsg - Smack check based on destination host
3012 * @size: the size of the message
3014 * Return 0 if the current subject can write to the destination host.
3015 * For IPv4 this is only a question if the destination is a single label host.
3016 * For IPv6 this is a check against the label of the port.
3018 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3021 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
3022 struct sockaddr
*sap
= (struct sockaddr
*) msg
->msg_name
;
3026 * Perfectly reasonable for this to be NULL
3031 switch (sip
->sin_family
) {
3033 rc
= smack_netlabel_send(sock
->sk
, sip
);
3036 rc
= smk_ipv6_port_check(sock
->sk
, sap
, SMK_SENDING
);
3043 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3044 * @sap: netlabel secattr
3045 * @ssp: socket security information
3047 * Returns a pointer to a Smack label entry found on the label list.
3049 static struct smack_known
*smack_from_secattr(struct netlbl_lsm_secattr
*sap
,
3050 struct socket_smack
*ssp
)
3052 struct smack_known
*skp
;
3055 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
3057 * Looks like a CIPSO packet.
3058 * If there are flags but no level netlabel isn't
3059 * behaving the way we expect it to.
3061 * Look it up in the label table
3062 * Without guidance regarding the smack value
3063 * for the packet fall back on the network
3067 list_for_each_entry(skp
, &smack_known_list
, list
) {
3068 if (sap
->attr
.mls
.lvl
!= skp
->smk_netlabel
.attr
.mls
.lvl
)
3070 if (memcmp(sap
->attr
.mls
.cat
,
3071 skp
->smk_netlabel
.attr
.mls
.cat
,
3082 if (ssp
!= NULL
&& ssp
->smk_in
== smack_known_star
.smk_known
)
3083 return &smack_known_web
;
3084 return &smack_known_star
;
3086 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
3088 * Looks like a fallback, which gives us a secid.
3090 skp
= smack_from_secid(sap
->attr
.secid
);
3092 * This has got to be a bug because it is
3093 * impossible to specify a fallback without
3094 * specifying the label, which will ensure
3095 * it has a secid, and the only way to get a
3096 * secid is from a fallback.
3098 BUG_ON(skp
== NULL
);
3102 * Without guidance regarding the smack value
3103 * for the packet fall back on the network
3106 return smack_net_ambient
;
3109 static int smk_skb_to_addr_ipv6(struct sk_buff
*skb
, struct sockaddr
*sap
)
3111 struct sockaddr_in6
*sip
= (struct sockaddr_in6
*)sap
;
3114 int proto
= -EINVAL
;
3115 struct ipv6hdr _ipv6h
;
3116 struct ipv6hdr
*ip6
;
3118 struct tcphdr _tcph
, *th
;
3119 struct udphdr _udph
, *uh
;
3120 struct dccp_hdr _dccph
, *dh
;
3124 offset
= skb_network_offset(skb
);
3125 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3128 sip
->sin6_addr
= ip6
->saddr
;
3130 nexthdr
= ip6
->nexthdr
;
3131 offset
+= sizeof(_ipv6h
);
3132 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3139 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3141 sip
->sin6_port
= th
->source
;
3144 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3146 sip
->sin6_port
= uh
->source
;
3149 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3151 sip
->sin6_port
= dh
->dccph_sport
;
3158 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3162 * Returns 0 if the packet should be delivered, an error code otherwise
3164 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3166 struct netlbl_lsm_secattr secattr
;
3167 struct socket_smack
*ssp
= sk
->sk_security
;
3168 struct smack_known
*skp
;
3169 struct sockaddr sadd
;
3171 struct smk_audit_info ad
;
3173 struct lsm_network_audit net
;
3175 switch (sk
->sk_family
) {
3178 * Translate what netlabel gave us.
3180 netlbl_secattr_init(&secattr
);
3182 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
3184 skp
= smack_from_secattr(&secattr
, ssp
);
3186 skp
= smack_net_ambient
;
3188 netlbl_secattr_destroy(&secattr
);
3191 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3192 ad
.a
.u
.net
->family
= sk
->sk_family
;
3193 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3194 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3197 * Receiving a packet requires that the other end
3198 * be able to write here. Read access is not required.
3199 * This is the simplist possible security model
3202 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3204 netlbl_skbuff_err(skb
, rc
, 0);
3207 rc
= smk_skb_to_addr_ipv6(skb
, &sadd
);
3208 if (rc
== IPPROTO_UDP
|| rc
== IPPROTO_TCP
)
3209 rc
= smk_ipv6_port_check(sk
, &sadd
, SMK_RECEIVING
);
3218 * smack_socket_getpeersec_stream - pull in packet label
3220 * @optval: user's destination
3221 * @optlen: size thereof
3224 * returns zero on success, an error code otherwise
3226 static int smack_socket_getpeersec_stream(struct socket
*sock
,
3227 char __user
*optval
,
3228 int __user
*optlen
, unsigned len
)
3230 struct socket_smack
*ssp
;
3235 ssp
= sock
->sk
->sk_security
;
3236 if (ssp
->smk_packet
!= NULL
) {
3237 rcp
= ssp
->smk_packet
;
3238 slen
= strlen(rcp
) + 1;
3243 else if (copy_to_user(optval
, rcp
, slen
) != 0)
3246 if (put_user(slen
, optlen
) != 0)
3254 * smack_socket_getpeersec_dgram - pull in packet label
3255 * @sock: the peer socket
3257 * @secid: pointer to where to put the secid of the packet
3259 * Sets the netlabel socket state on sk from parent
3261 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
3262 struct sk_buff
*skb
, u32
*secid
)
3265 struct netlbl_lsm_secattr secattr
;
3266 struct socket_smack
*ssp
= NULL
;
3267 struct smack_known
*skp
;
3268 int family
= PF_UNSPEC
;
3269 u32 s
= 0; /* 0 is the invalid secid */
3273 if (skb
->protocol
== htons(ETH_P_IP
))
3275 else if (skb
->protocol
== htons(ETH_P_IPV6
))
3278 if (family
== PF_UNSPEC
&& sock
!= NULL
)
3279 family
= sock
->sk
->sk_family
;
3281 if (family
== PF_UNIX
) {
3282 ssp
= sock
->sk
->sk_security
;
3283 s
= ssp
->smk_out
->smk_secid
;
3284 } else if (family
== PF_INET
|| family
== PF_INET6
) {
3286 * Translate what netlabel gave us.
3288 if (sock
!= NULL
&& sock
->sk
!= NULL
)
3289 ssp
= sock
->sk
->sk_security
;
3290 netlbl_secattr_init(&secattr
);
3291 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3293 skp
= smack_from_secattr(&secattr
, ssp
);
3296 netlbl_secattr_destroy(&secattr
);
3305 * smack_sock_graft - Initialize a newly created socket with an existing sock
3307 * @parent: parent socket
3309 * Set the smk_{in,out} state of an existing sock based on the process that
3310 * is creating the new socket.
3312 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
3314 struct socket_smack
*ssp
;
3315 struct smack_known
*skp
= smk_of_current();
3318 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
3321 ssp
= sk
->sk_security
;
3322 ssp
->smk_in
= skp
->smk_known
;
3324 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3328 * smack_inet_conn_request - Smack access check on connect
3329 * @sk: socket involved
3333 * Returns 0 if a task with the packet label could write to
3334 * the socket, otherwise an error code
3336 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
3337 struct request_sock
*req
)
3339 u16 family
= sk
->sk_family
;
3340 struct smack_known
*skp
;
3341 struct socket_smack
*ssp
= sk
->sk_security
;
3342 struct netlbl_lsm_secattr secattr
;
3343 struct sockaddr_in addr
;
3347 struct smk_audit_info ad
;
3349 struct lsm_network_audit net
;
3352 if (family
== PF_INET6
) {
3354 * Handle mapped IPv4 packets arriving
3355 * via IPv6 sockets. Don't set up netlabel
3356 * processing on IPv6.
3358 if (skb
->protocol
== htons(ETH_P_IP
))
3364 netlbl_secattr_init(&secattr
);
3365 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
3367 skp
= smack_from_secattr(&secattr
, ssp
);
3369 skp
= &smack_known_huh
;
3370 netlbl_secattr_destroy(&secattr
);
3373 smk_ad_init_net(&ad
, __func__
, LSM_AUDIT_DATA_NET
, &net
);
3374 ad
.a
.u
.net
->family
= family
;
3375 ad
.a
.u
.net
->netif
= skb
->skb_iif
;
3376 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
3379 * Receiving a packet requires that the other end be able to write
3380 * here. Read access is not required.
3382 rc
= smk_access(skp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
3387 * Save the peer's label in the request_sock so we can later setup
3388 * smk_packet in the child socket so that SO_PEERCRED can report it.
3390 req
->peer_secid
= skp
->smk_secid
;
3393 * We need to decide if we want to label the incoming connection here
3394 * if we do we only need to label the request_sock and the stack will
3395 * propagate the wire-label to the sock when it is created.
3398 addr
.sin_addr
.s_addr
= hdr
->saddr
;
3400 hsp
= smack_host_label(&addr
);
3404 rc
= netlbl_req_setattr(req
, &skp
->smk_netlabel
);
3406 netlbl_req_delattr(req
);
3412 * smack_inet_csk_clone - Copy the connection information to the new socket
3413 * @sk: the new socket
3414 * @req: the connection's request_sock
3416 * Transfer the connection's peer label to the newly created socket.
3418 static void smack_inet_csk_clone(struct sock
*sk
,
3419 const struct request_sock
*req
)
3421 struct socket_smack
*ssp
= sk
->sk_security
;
3422 struct smack_known
*skp
;
3424 if (req
->peer_secid
!= 0) {
3425 skp
= smack_from_secid(req
->peer_secid
);
3426 ssp
->smk_packet
= skp
->smk_known
;
3428 ssp
->smk_packet
= NULL
;
3432 * Key management security hooks
3434 * Casey has not tested key support very heavily.
3435 * The permission check is most likely too restrictive.
3436 * If you care about keys please have a look.
3441 * smack_key_alloc - Set the key security blob
3443 * @cred: the credentials to use
3446 * No allocation required
3450 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
3451 unsigned long flags
)
3453 struct smack_known
*skp
= smk_of_task(cred
->security
);
3455 key
->security
= skp
->smk_known
;
3460 * smack_key_free - Clear the key security blob
3463 * Clear the blob pointer
3465 static void smack_key_free(struct key
*key
)
3467 key
->security
= NULL
;
3471 * smack_key_permission - Smack access on a key
3472 * @key_ref: gets to the object
3473 * @cred: the credentials to use
3476 * Return 0 if the task has read and write to the object,
3477 * an error code otherwise
3479 static int smack_key_permission(key_ref_t key_ref
,
3480 const struct cred
*cred
, key_perm_t perm
)
3483 struct smk_audit_info ad
;
3484 struct smack_known
*tkp
= smk_of_task(cred
->security
);
3486 keyp
= key_ref_to_ptr(key_ref
);
3490 * If the key hasn't been initialized give it access so that
3493 if (keyp
->security
== NULL
)
3496 * This should not occur
3501 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
3502 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
3503 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
3505 return smk_access(tkp
, keyp
->security
, MAY_READWRITE
, &ad
);
3507 #endif /* CONFIG_KEYS */
3512 * Audit requires a unique representation of each Smack specific
3513 * rule. This unique representation is used to distinguish the
3514 * object to be audited from remaining kernel objects and also
3515 * works as a glue between the audit hooks.
3517 * Since repository entries are added but never deleted, we'll use
3518 * the smack_known label address related to the given audit rule as
3519 * the needed unique representation. This also better fits the smack
3520 * model where nearly everything is a label.
3525 * smack_audit_rule_init - Initialize a smack audit rule
3526 * @field: audit rule fields given from user-space (audit.h)
3527 * @op: required testing operator (=, !=, >, <, ...)
3528 * @rulestr: smack label to be audited
3529 * @vrule: pointer to save our own audit rule representation
3531 * Prepare to audit cases where (@field @op @rulestr) is true.
3532 * The label to be audited is created if necessay.
3534 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
3536 char **rule
= (char **)vrule
;
3539 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3542 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
3545 *rule
= smk_import(rulestr
, 0);
3551 * smack_audit_rule_known - Distinguish Smack audit rules
3552 * @krule: rule of interest, in Audit kernel representation format
3554 * This is used to filter Smack rules from remaining Audit ones.
3555 * If it's proved that this rule belongs to us, the
3556 * audit_rule_match hook will be called to do the final judgement.
3558 static int smack_audit_rule_known(struct audit_krule
*krule
)
3560 struct audit_field
*f
;
3563 for (i
= 0; i
< krule
->field_count
; i
++) {
3564 f
= &krule
->fields
[i
];
3566 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
3574 * smack_audit_rule_match - Audit given object ?
3575 * @secid: security id for identifying the object to test
3576 * @field: audit rule flags given from user-space
3577 * @op: required testing operator
3578 * @vrule: smack internal rule presentation
3579 * @actx: audit context associated with the check
3581 * The core Audit hook. It's used to take the decision of
3582 * whether to audit or not to audit a given object.
3584 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
3585 struct audit_context
*actx
)
3587 struct smack_known
*skp
;
3591 audit_log(actx
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
3592 "Smack: missing rule\n");
3596 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3599 skp
= smack_from_secid(secid
);
3602 * No need to do string comparisons. If a match occurs,
3603 * both pointers will point to the same smack_known
3606 if (op
== Audit_equal
)
3607 return (rule
== skp
->smk_known
);
3608 if (op
== Audit_not_equal
)
3609 return (rule
!= skp
->smk_known
);
3615 * smack_audit_rule_free - free smack rule representation
3616 * @vrule: rule to be freed.
3618 * No memory was allocated.
3620 static void smack_audit_rule_free(void *vrule
)
3625 #endif /* CONFIG_AUDIT */
3628 * smack_secid_to_secctx - return the smack label for a secid
3629 * @secid: incoming integer
3630 * @secdata: destination
3631 * @seclen: how long it is
3633 * Exists for networking code.
3635 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
3637 struct smack_known
*skp
= smack_from_secid(secid
);
3640 *secdata
= skp
->smk_known
;
3641 *seclen
= strlen(skp
->smk_known
);
3646 * smack_secctx_to_secid - return the secid for a smack label
3647 * @secdata: smack label
3648 * @seclen: how long result is
3649 * @secid: outgoing integer
3651 * Exists for audit and networking code.
3653 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
3655 *secid
= smack_to_secid(secdata
);
3660 * smack_release_secctx - don't do anything.
3664 * Exists to make sure nothing gets done, and properly
3666 static void smack_release_secctx(char *secdata
, u32 seclen
)
3670 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
3672 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
3675 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
3677 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
3680 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
3683 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
3691 struct security_operations smack_ops
= {
3694 .ptrace_access_check
= smack_ptrace_access_check
,
3695 .ptrace_traceme
= smack_ptrace_traceme
,
3696 .syslog
= smack_syslog
,
3698 .sb_alloc_security
= smack_sb_alloc_security
,
3699 .sb_free_security
= smack_sb_free_security
,
3700 .sb_copy_data
= smack_sb_copy_data
,
3701 .sb_kern_mount
= smack_sb_kern_mount
,
3702 .sb_statfs
= smack_sb_statfs
,
3703 .sb_mount
= smack_sb_mount
,
3704 .sb_umount
= smack_sb_umount
,
3706 .bprm_set_creds
= smack_bprm_set_creds
,
3707 .bprm_committing_creds
= smack_bprm_committing_creds
,
3708 .bprm_secureexec
= smack_bprm_secureexec
,
3710 .inode_alloc_security
= smack_inode_alloc_security
,
3711 .inode_free_security
= smack_inode_free_security
,
3712 .inode_init_security
= smack_inode_init_security
,
3713 .inode_link
= smack_inode_link
,
3714 .inode_unlink
= smack_inode_unlink
,
3715 .inode_rmdir
= smack_inode_rmdir
,
3716 .inode_rename
= smack_inode_rename
,
3717 .inode_permission
= smack_inode_permission
,
3718 .inode_setattr
= smack_inode_setattr
,
3719 .inode_getattr
= smack_inode_getattr
,
3720 .inode_setxattr
= smack_inode_setxattr
,
3721 .inode_post_setxattr
= smack_inode_post_setxattr
,
3722 .inode_getxattr
= smack_inode_getxattr
,
3723 .inode_removexattr
= smack_inode_removexattr
,
3724 .inode_getsecurity
= smack_inode_getsecurity
,
3725 .inode_setsecurity
= smack_inode_setsecurity
,
3726 .inode_listsecurity
= smack_inode_listsecurity
,
3727 .inode_getsecid
= smack_inode_getsecid
,
3729 .file_permission
= smack_file_permission
,
3730 .file_alloc_security
= smack_file_alloc_security
,
3731 .file_free_security
= smack_file_free_security
,
3732 .file_ioctl
= smack_file_ioctl
,
3733 .file_lock
= smack_file_lock
,
3734 .file_fcntl
= smack_file_fcntl
,
3735 .mmap_file
= smack_mmap_file
,
3736 .mmap_addr
= cap_mmap_addr
,
3737 .file_set_fowner
= smack_file_set_fowner
,
3738 .file_send_sigiotask
= smack_file_send_sigiotask
,
3739 .file_receive
= smack_file_receive
,
3741 .file_open
= smack_file_open
,
3743 .cred_alloc_blank
= smack_cred_alloc_blank
,
3744 .cred_free
= smack_cred_free
,
3745 .cred_prepare
= smack_cred_prepare
,
3746 .cred_transfer
= smack_cred_transfer
,
3747 .kernel_act_as
= smack_kernel_act_as
,
3748 .kernel_create_files_as
= smack_kernel_create_files_as
,
3749 .task_setpgid
= smack_task_setpgid
,
3750 .task_getpgid
= smack_task_getpgid
,
3751 .task_getsid
= smack_task_getsid
,
3752 .task_getsecid
= smack_task_getsecid
,
3753 .task_setnice
= smack_task_setnice
,
3754 .task_setioprio
= smack_task_setioprio
,
3755 .task_getioprio
= smack_task_getioprio
,
3756 .task_setscheduler
= smack_task_setscheduler
,
3757 .task_getscheduler
= smack_task_getscheduler
,
3758 .task_movememory
= smack_task_movememory
,
3759 .task_kill
= smack_task_kill
,
3760 .task_wait
= smack_task_wait
,
3761 .task_to_inode
= smack_task_to_inode
,
3763 .ipc_permission
= smack_ipc_permission
,
3764 .ipc_getsecid
= smack_ipc_getsecid
,
3766 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
3767 .msg_msg_free_security
= smack_msg_msg_free_security
,
3769 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
3770 .msg_queue_free_security
= smack_msg_queue_free_security
,
3771 .msg_queue_associate
= smack_msg_queue_associate
,
3772 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
3773 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
3774 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
3776 .shm_alloc_security
= smack_shm_alloc_security
,
3777 .shm_free_security
= smack_shm_free_security
,
3778 .shm_associate
= smack_shm_associate
,
3779 .shm_shmctl
= smack_shm_shmctl
,
3780 .shm_shmat
= smack_shm_shmat
,
3782 .sem_alloc_security
= smack_sem_alloc_security
,
3783 .sem_free_security
= smack_sem_free_security
,
3784 .sem_associate
= smack_sem_associate
,
3785 .sem_semctl
= smack_sem_semctl
,
3786 .sem_semop
= smack_sem_semop
,
3788 .d_instantiate
= smack_d_instantiate
,
3790 .getprocattr
= smack_getprocattr
,
3791 .setprocattr
= smack_setprocattr
,
3793 .unix_stream_connect
= smack_unix_stream_connect
,
3794 .unix_may_send
= smack_unix_may_send
,
3796 .socket_post_create
= smack_socket_post_create
,
3797 .socket_bind
= smack_socket_bind
,
3798 .socket_connect
= smack_socket_connect
,
3799 .socket_sendmsg
= smack_socket_sendmsg
,
3800 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
3801 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
3802 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
3803 .sk_alloc_security
= smack_sk_alloc_security
,
3804 .sk_free_security
= smack_sk_free_security
,
3805 .sock_graft
= smack_sock_graft
,
3806 .inet_conn_request
= smack_inet_conn_request
,
3807 .inet_csk_clone
= smack_inet_csk_clone
,
3809 /* key management security hooks */
3811 .key_alloc
= smack_key_alloc
,
3812 .key_free
= smack_key_free
,
3813 .key_permission
= smack_key_permission
,
3814 #endif /* CONFIG_KEYS */
3818 .audit_rule_init
= smack_audit_rule_init
,
3819 .audit_rule_known
= smack_audit_rule_known
,
3820 .audit_rule_match
= smack_audit_rule_match
,
3821 .audit_rule_free
= smack_audit_rule_free
,
3822 #endif /* CONFIG_AUDIT */
3824 .secid_to_secctx
= smack_secid_to_secctx
,
3825 .secctx_to_secid
= smack_secctx_to_secid
,
3826 .release_secctx
= smack_release_secctx
,
3827 .inode_notifysecctx
= smack_inode_notifysecctx
,
3828 .inode_setsecctx
= smack_inode_setsecctx
,
3829 .inode_getsecctx
= smack_inode_getsecctx
,
3833 static __init
void init_smack_known_list(void)
3836 * Initialize rule list locks
3838 mutex_init(&smack_known_huh
.smk_rules_lock
);
3839 mutex_init(&smack_known_hat
.smk_rules_lock
);
3840 mutex_init(&smack_known_floor
.smk_rules_lock
);
3841 mutex_init(&smack_known_star
.smk_rules_lock
);
3842 mutex_init(&smack_known_invalid
.smk_rules_lock
);
3843 mutex_init(&smack_known_web
.smk_rules_lock
);
3845 * Initialize rule lists
3847 INIT_LIST_HEAD(&smack_known_huh
.smk_rules
);
3848 INIT_LIST_HEAD(&smack_known_hat
.smk_rules
);
3849 INIT_LIST_HEAD(&smack_known_star
.smk_rules
);
3850 INIT_LIST_HEAD(&smack_known_floor
.smk_rules
);
3851 INIT_LIST_HEAD(&smack_known_invalid
.smk_rules
);
3852 INIT_LIST_HEAD(&smack_known_web
.smk_rules
);
3854 * Create the known labels list
3856 list_add(&smack_known_huh
.list
, &smack_known_list
);
3857 list_add(&smack_known_hat
.list
, &smack_known_list
);
3858 list_add(&smack_known_star
.list
, &smack_known_list
);
3859 list_add(&smack_known_floor
.list
, &smack_known_list
);
3860 list_add(&smack_known_invalid
.list
, &smack_known_list
);
3861 list_add(&smack_known_web
.list
, &smack_known_list
);
3865 * smack_init - initialize the smack system
3869 static __init
int smack_init(void)
3872 struct task_smack
*tsp
;
3874 if (!security_module_enable(&smack_ops
))
3877 tsp
= new_task_smack(&smack_known_floor
, &smack_known_floor
,
3882 printk(KERN_INFO
"Smack: Initializing.\n");
3885 * Set the security state for the initial task.
3887 cred
= (struct cred
*) current
->cred
;
3888 cred
->security
= tsp
;
3890 /* initialize the smack_known_list */
3891 init_smack_known_list();
3896 if (register_security(&smack_ops
))
3897 panic("smack: Unable to register with kernel.\n");
3903 * Smack requires early initialization in order to label
3904 * all processes and objects when they are created.
3906 security_initcall(smack_init
);