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>
38 #define task_security(task) (task_cred_xxx((task), security))
40 #define TRANS_TRUE "TRUE"
41 #define TRANS_TRUE_SIZE 4
44 * smk_fetch - Fetch the smack label from a file.
45 * @ip: a pointer to the inode
46 * @dp: a pointer to the dentry
48 * Returns a pointer to the master list entry for the Smack label
49 * or NULL if there was no label to fetch.
51 static char *smk_fetch(const char *name
, struct inode
*ip
, struct dentry
*dp
)
54 char in
[SMK_LABELLEN
];
56 if (ip
->i_op
->getxattr
== NULL
)
59 rc
= ip
->i_op
->getxattr(dp
, name
, in
, SMK_LABELLEN
);
63 return smk_import(in
, rc
);
67 * new_inode_smack - allocate an inode security blob
68 * @smack: a pointer to the Smack label to use in the blob
70 * Returns the new blob or NULL if there's no memory available
72 struct inode_smack
*new_inode_smack(char *smack
)
74 struct inode_smack
*isp
;
76 isp
= kzalloc(sizeof(struct inode_smack
), GFP_KERNEL
);
80 isp
->smk_inode
= smack
;
82 mutex_init(&isp
->smk_lock
);
93 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
94 * @ctp: child task pointer
95 * @mode: ptrace attachment mode
97 * Returns 0 if access is OK, an error code otherwise
99 * Do the capability checks, and require read and write.
101 static int smack_ptrace_access_check(struct task_struct
*ctp
, unsigned int mode
)
104 struct smk_audit_info ad
;
107 rc
= cap_ptrace_access_check(ctp
, mode
);
111 sp
= smk_of_current();
112 tsp
= smk_of_task(task_security(ctp
));
113 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
114 smk_ad_setfield_u_tsk(&ad
, ctp
);
116 /* we won't log here, because rc can be overriden */
117 rc
= smk_access(sp
, tsp
, MAY_READWRITE
, NULL
);
118 if (rc
!= 0 && capable(CAP_MAC_OVERRIDE
))
121 smack_log(sp
, tsp
, MAY_READWRITE
, rc
, &ad
);
126 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
127 * @ptp: parent task pointer
129 * Returns 0 if access is OK, an error code otherwise
131 * Do the capability checks, and require read and write.
133 static int smack_ptrace_traceme(struct task_struct
*ptp
)
136 struct smk_audit_info ad
;
139 rc
= cap_ptrace_traceme(ptp
);
143 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
144 smk_ad_setfield_u_tsk(&ad
, ptp
);
146 sp
= smk_of_current();
147 tsp
= smk_of_task(task_security(ptp
));
148 /* we won't log here, because rc can be overriden */
149 rc
= smk_access(tsp
, sp
, MAY_READWRITE
, NULL
);
150 if (rc
!= 0 && has_capability(ptp
, CAP_MAC_OVERRIDE
))
153 smack_log(tsp
, sp
, MAY_READWRITE
, rc
, &ad
);
158 * smack_syslog - Smack approval on syslog
159 * @type: message type
161 * Require that the task has the floor label
163 * Returns 0 on success, error code otherwise.
165 static int smack_syslog(int typefrom_file
)
168 char *sp
= smk_of_current();
170 if (capable(CAP_MAC_OVERRIDE
))
173 if (sp
!= smack_known_floor
.smk_known
)
185 * smack_sb_alloc_security - allocate a superblock blob
186 * @sb: the superblock getting the blob
188 * Returns 0 on success or -ENOMEM on error.
190 static int smack_sb_alloc_security(struct super_block
*sb
)
192 struct superblock_smack
*sbsp
;
194 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
199 sbsp
->smk_root
= smack_known_floor
.smk_known
;
200 sbsp
->smk_default
= smack_known_floor
.smk_known
;
201 sbsp
->smk_floor
= smack_known_floor
.smk_known
;
202 sbsp
->smk_hat
= smack_known_hat
.smk_known
;
203 sbsp
->smk_initialized
= 0;
204 spin_lock_init(&sbsp
->smk_sblock
);
206 sb
->s_security
= sbsp
;
212 * smack_sb_free_security - free a superblock blob
213 * @sb: the superblock getting the blob
216 static void smack_sb_free_security(struct super_block
*sb
)
218 kfree(sb
->s_security
);
219 sb
->s_security
= NULL
;
223 * smack_sb_copy_data - copy mount options data for processing
224 * @orig: where to start
225 * @smackopts: mount options string
227 * Returns 0 on success or -ENOMEM on error.
229 * Copy the Smack specific mount options out of the mount
232 static int smack_sb_copy_data(char *orig
, char *smackopts
)
234 char *cp
, *commap
, *otheropts
, *dp
;
236 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
237 if (otheropts
== NULL
)
240 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
241 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
243 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
245 else if (strstr(cp
, SMK_FSHAT
) == cp
)
247 else if (strstr(cp
, SMK_FSROOT
) == cp
)
252 commap
= strchr(cp
, ',');
261 strcpy(orig
, otheropts
);
262 free_page((unsigned long)otheropts
);
268 * smack_sb_kern_mount - Smack specific mount processing
269 * @sb: the file system superblock
270 * @flags: the mount flags
271 * @data: the smack mount options
273 * Returns 0 on success, an error code on failure
275 static int smack_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
277 struct dentry
*root
= sb
->s_root
;
278 struct inode
*inode
= root
->d_inode
;
279 struct superblock_smack
*sp
= sb
->s_security
;
280 struct inode_smack
*isp
;
285 spin_lock(&sp
->smk_sblock
);
286 if (sp
->smk_initialized
!= 0) {
287 spin_unlock(&sp
->smk_sblock
);
290 sp
->smk_initialized
= 1;
291 spin_unlock(&sp
->smk_sblock
);
293 for (op
= data
; op
!= NULL
; op
= commap
) {
294 commap
= strchr(op
, ',');
298 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
299 op
+= strlen(SMK_FSHAT
);
300 nsp
= smk_import(op
, 0);
303 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
304 op
+= strlen(SMK_FSFLOOR
);
305 nsp
= smk_import(op
, 0);
308 } else if (strncmp(op
, SMK_FSDEFAULT
,
309 strlen(SMK_FSDEFAULT
)) == 0) {
310 op
+= strlen(SMK_FSDEFAULT
);
311 nsp
= smk_import(op
, 0);
313 sp
->smk_default
= nsp
;
314 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
315 op
+= strlen(SMK_FSROOT
);
316 nsp
= smk_import(op
, 0);
323 * Initialize the root inode.
325 isp
= inode
->i_security
;
327 inode
->i_security
= new_inode_smack(sp
->smk_root
);
329 isp
->smk_inode
= sp
->smk_root
;
335 * smack_sb_statfs - Smack check on statfs
336 * @dentry: identifies the file system in question
338 * Returns 0 if current can read the floor of the filesystem,
339 * and error code otherwise
341 static int smack_sb_statfs(struct dentry
*dentry
)
343 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
345 struct smk_audit_info ad
;
347 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
348 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
350 rc
= smk_curacc(sbp
->smk_floor
, MAY_READ
, &ad
);
355 * smack_sb_mount - Smack check for mounting
362 * Returns 0 if current can write the floor of the filesystem
363 * being mounted on, an error code otherwise.
365 static int smack_sb_mount(char *dev_name
, struct path
*path
,
366 char *type
, unsigned long flags
, void *data
)
368 struct superblock_smack
*sbp
= path
->mnt
->mnt_sb
->s_security
;
369 struct smk_audit_info ad
;
371 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
372 smk_ad_setfield_u_fs_path(&ad
, *path
);
374 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
378 * smack_sb_umount - Smack check for unmounting
379 * @mnt: file system to unmount
382 * Returns 0 if current can write the floor of the filesystem
383 * being unmounted, an error code otherwise.
385 static int smack_sb_umount(struct vfsmount
*mnt
, int flags
)
387 struct superblock_smack
*sbp
;
388 struct smk_audit_info ad
;
390 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
391 smk_ad_setfield_u_fs_path_dentry(&ad
, mnt
->mnt_root
);
392 smk_ad_setfield_u_fs_path_mnt(&ad
, mnt
);
394 sbp
= mnt
->mnt_sb
->s_security
;
395 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
, &ad
);
402 static int smack_bprm_set_creds(struct linux_binprm
*bprm
)
404 struct task_smack
*tsp
= bprm
->cred
->security
;
405 struct inode_smack
*isp
;
409 rc
= cap_bprm_set_creds(bprm
);
413 if (bprm
->cred_prepared
)
416 if (bprm
->file
== NULL
|| bprm
->file
->f_dentry
== NULL
)
419 dp
= bprm
->file
->f_dentry
;
421 if (dp
->d_inode
== NULL
)
424 isp
= dp
->d_inode
->i_security
;
426 if (isp
->smk_task
!= NULL
)
427 tsp
->smk_task
= isp
->smk_task
;
437 * smack_inode_alloc_security - allocate an inode blob
438 * @inode: the inode in need of a blob
440 * Returns 0 if it gets a blob, -ENOMEM otherwise
442 static int smack_inode_alloc_security(struct inode
*inode
)
444 inode
->i_security
= new_inode_smack(smk_of_current());
445 if (inode
->i_security
== NULL
)
451 * smack_inode_free_security - free an inode blob
452 * @inode: the inode with a blob
454 * Clears the blob pointer in inode
456 static void smack_inode_free_security(struct inode
*inode
)
458 kfree(inode
->i_security
);
459 inode
->i_security
= NULL
;
463 * smack_inode_init_security - copy out the smack from an inode
466 * @name: where to put the attribute name
467 * @value: where to put the attribute value
468 * @len: where to put the length of the attribute
470 * Returns 0 if it all works out, -ENOMEM if there's no memory
472 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
473 char **name
, void **value
, size_t *len
)
475 char *isp
= smk_of_inode(inode
);
476 char *dsp
= smk_of_inode(dir
);
480 *name
= kstrdup(XATTR_SMACK_SUFFIX
, GFP_KERNEL
);
486 may
= smk_access_entry(smk_of_current(), dsp
);
489 * If the access rule allows transmutation and
490 * the directory requests transmutation then
491 * by all means transmute.
493 if (((may
& MAY_TRANSMUTE
) != 0) && smk_inode_transmutable(dir
))
496 *value
= kstrdup(isp
, GFP_KERNEL
);
502 *len
= strlen(isp
) + 1;
508 * smack_inode_link - Smack check on link
509 * @old_dentry: the existing object
511 * @new_dentry: the new object
513 * Returns 0 if access is permitted, an error code otherwise
515 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
516 struct dentry
*new_dentry
)
519 struct smk_audit_info ad
;
522 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
523 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
525 isp
= smk_of_inode(old_dentry
->d_inode
);
526 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
528 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
529 isp
= smk_of_inode(new_dentry
->d_inode
);
530 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
531 rc
= smk_curacc(isp
, MAY_WRITE
, &ad
);
538 * smack_inode_unlink - Smack check on inode deletion
539 * @dir: containing directory object
540 * @dentry: file to unlink
542 * Returns 0 if current can write the containing directory
543 * and the object, error code otherwise
545 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
547 struct inode
*ip
= dentry
->d_inode
;
548 struct smk_audit_info ad
;
551 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
552 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
555 * You need write access to the thing you're unlinking
557 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
, &ad
);
560 * You also need write access to the containing directory
562 smk_ad_setfield_u_fs_path_dentry(&ad
, NULL
);
563 smk_ad_setfield_u_fs_inode(&ad
, dir
);
564 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
570 * smack_inode_rmdir - Smack check on directory deletion
571 * @dir: containing directory object
572 * @dentry: directory to unlink
574 * Returns 0 if current can write the containing directory
575 * and the directory, error code otherwise
577 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
579 struct smk_audit_info ad
;
582 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
583 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
586 * You need write access to the thing you're removing
588 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
591 * You also need write access to the containing directory
593 smk_ad_setfield_u_fs_path_dentry(&ad
, NULL
);
594 smk_ad_setfield_u_fs_inode(&ad
, dir
);
595 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
, &ad
);
602 * smack_inode_rename - Smack check on rename
603 * @old_inode: the old directory
604 * @old_dentry: unused
605 * @new_inode: the new directory
606 * @new_dentry: unused
608 * Read and write access is required on both the old and
611 * Returns 0 if access is permitted, an error code otherwise
613 static int smack_inode_rename(struct inode
*old_inode
,
614 struct dentry
*old_dentry
,
615 struct inode
*new_inode
,
616 struct dentry
*new_dentry
)
620 struct smk_audit_info ad
;
622 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
623 smk_ad_setfield_u_fs_path_dentry(&ad
, old_dentry
);
625 isp
= smk_of_inode(old_dentry
->d_inode
);
626 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
628 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
629 isp
= smk_of_inode(new_dentry
->d_inode
);
630 smk_ad_setfield_u_fs_path_dentry(&ad
, new_dentry
);
631 rc
= smk_curacc(isp
, MAY_READWRITE
, &ad
);
637 * smack_inode_permission - Smack version of permission()
638 * @inode: the inode in question
639 * @mask: the access requested
641 * This is the important Smack hook.
643 * Returns 0 if access is permitted, -EACCES otherwise
645 static int smack_inode_permission(struct inode
*inode
, int mask
)
647 struct smk_audit_info ad
;
649 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
651 * No permission to check. Existence test. Yup, it's there.
655 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
656 smk_ad_setfield_u_fs_inode(&ad
, inode
);
657 return smk_curacc(smk_of_inode(inode
), mask
, &ad
);
661 * smack_inode_setattr - Smack check for setting attributes
662 * @dentry: the object
663 * @iattr: for the force flag
665 * Returns 0 if access is permitted, an error code otherwise
667 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
669 struct smk_audit_info ad
;
671 * Need to allow for clearing the setuid bit.
673 if (iattr
->ia_valid
& ATTR_FORCE
)
675 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
676 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
678 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
682 * smack_inode_getattr - Smack check for getting attributes
684 * @dentry: the object
686 * Returns 0 if access is permitted, an error code otherwise
688 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
690 struct smk_audit_info ad
;
692 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
693 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
694 smk_ad_setfield_u_fs_path_mnt(&ad
, mnt
);
695 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
699 * smack_inode_setxattr - Smack check for setting xattrs
700 * @dentry: the object
701 * @name: name of the attribute
706 * This protects the Smack attribute explicitly.
708 * Returns 0 if access is permitted, an error code otherwise
710 static int smack_inode_setxattr(struct dentry
*dentry
, const char *name
,
711 const void *value
, size_t size
, int flags
)
713 struct smk_audit_info ad
;
716 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
717 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
718 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
719 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
720 if (!capable(CAP_MAC_ADMIN
))
723 * check label validity here so import wont fail on
726 if (size
== 0 || size
>= SMK_LABELLEN
||
727 smk_import(value
, size
) == NULL
)
729 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
730 if (!capable(CAP_MAC_ADMIN
))
732 if (size
!= TRANS_TRUE_SIZE
||
733 strncmp(value
, TRANS_TRUE
, TRANS_TRUE_SIZE
) != 0)
736 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
738 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
739 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
742 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
748 * smack_inode_post_setxattr - Apply the Smack update approved above
750 * @name: attribute name
751 * @value: attribute value
752 * @size: attribute size
755 * Set the pointer in the inode blob to the entry found
756 * in the master label list.
758 static void smack_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
759 const void *value
, size_t size
, int flags
)
762 struct inode_smack
*isp
= dentry
->d_inode
->i_security
;
764 if (strcmp(name
, XATTR_NAME_SMACK
) == 0) {
765 nsp
= smk_import(value
, size
);
767 isp
->smk_inode
= nsp
;
769 isp
->smk_inode
= smack_known_invalid
.smk_known
;
770 } else if (strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0) {
771 nsp
= smk_import(value
, size
);
775 isp
->smk_task
= smack_known_invalid
.smk_known
;
776 } else if (strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0)
777 isp
->smk_flags
|= SMK_INODE_TRANSMUTE
;
783 * smack_inode_getxattr - Smack check on getxattr
784 * @dentry: the object
787 * Returns 0 if access is permitted, an error code otherwise
789 static int smack_inode_getxattr(struct dentry
*dentry
, const char *name
)
791 struct smk_audit_info ad
;
793 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
794 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
796 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
, &ad
);
800 * smack_inode_removexattr - Smack check on removexattr
801 * @dentry: the object
802 * @name: name of the attribute
804 * Removing the Smack attribute requires CAP_MAC_ADMIN
806 * Returns 0 if access is permitted, an error code otherwise
808 static int smack_inode_removexattr(struct dentry
*dentry
, const char *name
)
810 struct inode_smack
*isp
;
811 struct smk_audit_info ad
;
814 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
815 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
816 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0 ||
817 strcmp(name
, XATTR_NAME_SMACKEXEC
) == 0 ||
818 strcmp(name
, XATTR_NAME_SMACKTRANSMUTE
) == 0) {
819 if (!capable(CAP_MAC_ADMIN
))
822 rc
= cap_inode_removexattr(dentry
, name
);
824 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
825 smk_ad_setfield_u_fs_path_dentry(&ad
, dentry
);
827 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
, &ad
);
830 isp
= dentry
->d_inode
->i_security
;
831 isp
->smk_task
= NULL
;
838 * smack_inode_getsecurity - get smack xattrs
840 * @name: attribute name
841 * @buffer: where to put the result
844 * Returns the size of the attribute or an error code
846 static int smack_inode_getsecurity(const struct inode
*inode
,
847 const char *name
, void **buffer
,
850 struct socket_smack
*ssp
;
852 struct super_block
*sbp
;
853 struct inode
*ip
= (struct inode
*)inode
;
858 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
859 isp
= smk_of_inode(inode
);
860 ilen
= strlen(isp
) + 1;
866 * The rest of the Smack xattrs are only on sockets.
869 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
873 if (sock
== NULL
|| sock
->sk
== NULL
)
876 ssp
= sock
->sk
->sk_security
;
878 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
880 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
885 ilen
= strlen(isp
) + 1;
896 * smack_inode_listsecurity - list the Smack attributes
898 * @buffer: where they go
899 * @buffer_size: size of buffer
901 * Returns 0 on success, -EINVAL otherwise
903 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
906 int len
= strlen(XATTR_NAME_SMACK
);
908 if (buffer
!= NULL
&& len
<= buffer_size
) {
909 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
916 * smack_inode_getsecid - Extract inode's security id
917 * @inode: inode to extract the info from
918 * @secid: where result will be saved
920 static void smack_inode_getsecid(const struct inode
*inode
, u32
*secid
)
922 struct inode_smack
*isp
= inode
->i_security
;
924 *secid
= smack_to_secid(isp
->smk_inode
);
932 * smack_file_permission - Smack check on file operations
938 * Should access checks be done on each read or write?
939 * UNICOS and SELinux say yes.
940 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
942 * I'll say no for now. Smack does not do the frequent
943 * label changing that SELinux does.
945 static int smack_file_permission(struct file
*file
, int mask
)
951 * smack_file_alloc_security - assign a file security blob
954 * The security blob for a file is a pointer to the master
955 * label list, so no allocation is done.
959 static int smack_file_alloc_security(struct file
*file
)
961 file
->f_security
= smk_of_current();
966 * smack_file_free_security - clear a file security blob
969 * The security blob for a file is a pointer to the master
970 * label list, so no memory is freed.
972 static void smack_file_free_security(struct file
*file
)
974 file
->f_security
= NULL
;
978 * smack_file_ioctl - Smack check on ioctls
983 * Relies heavily on the correct use of the ioctl command conventions.
985 * Returns 0 if allowed, error code otherwise
987 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
991 struct smk_audit_info ad
;
993 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
994 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
996 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
997 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
999 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
))
1000 rc
= smk_curacc(file
->f_security
, MAY_READ
, &ad
);
1006 * smack_file_lock - Smack check on file locking
1010 * Returns 0 if current has write access, error code otherwise
1012 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
1014 struct smk_audit_info ad
;
1016 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
1017 smk_ad_setfield_u_fs_path_dentry(&ad
, file
->f_path
.dentry
);
1018 return smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1022 * smack_file_fcntl - Smack check on fcntl
1024 * @cmd: what action to check
1027 * Returns 0 if current has access, error code otherwise
1029 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
1032 struct smk_audit_info ad
;
1035 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_FS
);
1036 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1045 rc
= smk_curacc(file
->f_security
, MAY_READ
, &ad
);
1053 rc
= smk_curacc(file
->f_security
, MAY_WRITE
, &ad
);
1056 rc
= smk_curacc(file
->f_security
, MAY_READWRITE
, &ad
);
1063 * smack_file_set_fowner - set the file security blob value
1064 * @file: object in question
1067 * Further research may be required on this one.
1069 static int smack_file_set_fowner(struct file
*file
)
1071 file
->f_security
= smk_of_current();
1076 * smack_file_send_sigiotask - Smack on sigio
1077 * @tsk: The target task
1078 * @fown: the object the signal come from
1081 * Allow a privileged task to get signals even if it shouldn't
1083 * Returns 0 if a subject with the object's smack could
1084 * write to the task, an error code otherwise.
1086 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
1087 struct fown_struct
*fown
, int signum
)
1091 char *tsp
= smk_of_task(tsk
->cred
->security
);
1092 struct smk_audit_info ad
;
1095 * struct fown_struct is never outside the context of a struct file
1097 file
= container_of(fown
, struct file
, f_owner
);
1098 /* we don't log here as rc can be overriden */
1099 rc
= smk_access(file
->f_security
, tsp
, MAY_WRITE
, NULL
);
1100 if (rc
!= 0 && has_capability(tsk
, CAP_MAC_OVERRIDE
))
1103 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1104 smk_ad_setfield_u_tsk(&ad
, tsk
);
1105 smack_log(file
->f_security
, tsp
, MAY_WRITE
, rc
, &ad
);
1110 * smack_file_receive - Smack file receive check
1113 * Returns 0 if current has access, error code otherwise
1115 static int smack_file_receive(struct file
*file
)
1118 struct smk_audit_info ad
;
1120 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1121 smk_ad_setfield_u_fs_path(&ad
, file
->f_path
);
1123 * This code relies on bitmasks.
1125 if (file
->f_mode
& FMODE_READ
)
1127 if (file
->f_mode
& FMODE_WRITE
)
1130 return smk_curacc(file
->f_security
, may
, &ad
);
1138 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1139 * @new: the new credentials
1140 * @gfp: the atomicity of any memory allocations
1142 * Prepare a blank set of credentials for modification. This must allocate all
1143 * the memory the LSM module might require such that cred_transfer() can
1144 * complete without error.
1146 static int smack_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
1148 cred
->security
= kzalloc(sizeof(struct task_smack
), gfp
);
1149 if (cred
->security
== NULL
)
1156 * smack_cred_free - "free" task-level security credentials
1157 * @cred: the credentials in question
1159 * Smack isn't using copies of blobs. Everyone
1160 * points to an immutable list. The blobs never go away.
1161 * There is no leak here.
1163 static void smack_cred_free(struct cred
*cred
)
1165 kfree(cred
->security
);
1169 * smack_cred_prepare - prepare new set of credentials for modification
1170 * @new: the new credentials
1171 * @old: the original credentials
1172 * @gfp: the atomicity of any memory allocations
1174 * Prepare a new set of credentials for modification.
1176 static int smack_cred_prepare(struct cred
*new, const struct cred
*old
,
1179 struct task_smack
*old_tsp
= old
->security
;
1180 struct task_smack
*new_tsp
;
1182 new_tsp
= kzalloc(sizeof(struct task_smack
), gfp
);
1183 if (new_tsp
== NULL
)
1186 new_tsp
->smk_task
= old_tsp
->smk_task
;
1187 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1188 new->security
= new_tsp
;
1193 * smack_cred_transfer - Transfer the old credentials to the new credentials
1194 * @new: the new credentials
1195 * @old: the original credentials
1197 * Fill in a set of blank credentials from another set of credentials.
1199 static void smack_cred_transfer(struct cred
*new, const struct cred
*old
)
1201 struct task_smack
*old_tsp
= old
->security
;
1202 struct task_smack
*new_tsp
= new->security
;
1204 new_tsp
->smk_task
= old_tsp
->smk_task
;
1205 new_tsp
->smk_forked
= old_tsp
->smk_task
;
1209 * smack_kernel_act_as - Set the subjective context in a set of credentials
1210 * @new: points to the set of credentials to be modified.
1211 * @secid: specifies the security ID to be set
1213 * Set the security data for a kernel service.
1215 static int smack_kernel_act_as(struct cred
*new, u32 secid
)
1217 struct task_smack
*new_tsp
= new->security
;
1218 char *smack
= smack_from_secid(secid
);
1223 new_tsp
->smk_task
= smack
;
1228 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1229 * @new: points to the set of credentials to be modified
1230 * @inode: points to the inode to use as a reference
1232 * Set the file creation context in a set of credentials to the same
1233 * as the objective context of the specified inode
1235 static int smack_kernel_create_files_as(struct cred
*new,
1236 struct inode
*inode
)
1238 struct inode_smack
*isp
= inode
->i_security
;
1239 struct task_smack
*tsp
= new->security
;
1241 tsp
->smk_forked
= isp
->smk_inode
;
1242 tsp
->smk_task
= isp
->smk_inode
;
1247 * smk_curacc_on_task - helper to log task related access
1248 * @p: the task object
1249 * @access : the access requested
1251 * Return 0 if access is permitted
1253 static int smk_curacc_on_task(struct task_struct
*p
, int access
)
1255 struct smk_audit_info ad
;
1257 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1258 smk_ad_setfield_u_tsk(&ad
, p
);
1259 return smk_curacc(smk_of_task(task_security(p
)), access
, &ad
);
1263 * smack_task_setpgid - Smack check on setting pgid
1264 * @p: the task object
1267 * Return 0 if write access is permitted
1269 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1271 return smk_curacc_on_task(p
, MAY_WRITE
);
1275 * smack_task_getpgid - Smack access check for getpgid
1276 * @p: the object task
1278 * Returns 0 if current can read the object task, error code otherwise
1280 static int smack_task_getpgid(struct task_struct
*p
)
1282 return smk_curacc_on_task(p
, MAY_READ
);
1286 * smack_task_getsid - Smack access check for getsid
1287 * @p: the object task
1289 * Returns 0 if current can read the object task, error code otherwise
1291 static int smack_task_getsid(struct task_struct
*p
)
1293 return smk_curacc_on_task(p
, MAY_READ
);
1297 * smack_task_getsecid - get the secid of the task
1298 * @p: the object task
1299 * @secid: where to put the result
1301 * Sets the secid to contain a u32 version of the smack label.
1303 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1305 *secid
= smack_to_secid(smk_of_task(task_security(p
)));
1309 * smack_task_setnice - Smack check on setting nice
1310 * @p: the task object
1313 * Return 0 if write access is permitted
1315 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1319 rc
= cap_task_setnice(p
, nice
);
1321 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1326 * smack_task_setioprio - Smack check on setting ioprio
1327 * @p: the task object
1330 * Return 0 if write access is permitted
1332 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1336 rc
= cap_task_setioprio(p
, ioprio
);
1338 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1343 * smack_task_getioprio - Smack check on reading ioprio
1344 * @p: the task object
1346 * Return 0 if read access is permitted
1348 static int smack_task_getioprio(struct task_struct
*p
)
1350 return smk_curacc_on_task(p
, MAY_READ
);
1354 * smack_task_setscheduler - Smack check on setting scheduler
1355 * @p: the task object
1359 * Return 0 if read access is permitted
1361 static int smack_task_setscheduler(struct task_struct
*p
)
1365 rc
= cap_task_setscheduler(p
);
1367 rc
= smk_curacc_on_task(p
, MAY_WRITE
);
1372 * smack_task_getscheduler - Smack check on reading scheduler
1373 * @p: the task object
1375 * Return 0 if read access is permitted
1377 static int smack_task_getscheduler(struct task_struct
*p
)
1379 return smk_curacc_on_task(p
, MAY_READ
);
1383 * smack_task_movememory - Smack check on moving memory
1384 * @p: the task object
1386 * Return 0 if write access is permitted
1388 static int smack_task_movememory(struct task_struct
*p
)
1390 return smk_curacc_on_task(p
, MAY_WRITE
);
1394 * smack_task_kill - Smack check on signal delivery
1395 * @p: the task object
1398 * @secid: identifies the smack to use in lieu of current's
1400 * Return 0 if write access is permitted
1402 * The secid behavior is an artifact of an SELinux hack
1403 * in the USB code. Someday it may go away.
1405 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1408 struct smk_audit_info ad
;
1410 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1411 smk_ad_setfield_u_tsk(&ad
, p
);
1413 * Sending a signal requires that the sender
1414 * can write the receiver.
1417 return smk_curacc(smk_of_task(task_security(p
)), MAY_WRITE
,
1420 * If the secid isn't 0 we're dealing with some USB IO
1421 * specific behavior. This is not clean. For one thing
1422 * we can't take privilege into account.
1424 return smk_access(smack_from_secid(secid
),
1425 smk_of_task(task_security(p
)), MAY_WRITE
, &ad
);
1429 * smack_task_wait - Smack access check for waiting
1430 * @p: task to wait for
1432 * Returns 0 if current can wait for p, error code otherwise
1434 static int smack_task_wait(struct task_struct
*p
)
1436 struct smk_audit_info ad
;
1437 char *sp
= smk_of_current();
1438 char *tsp
= smk_of_forked(task_security(p
));
1441 /* we don't log here, we can be overriden */
1442 rc
= smk_access(tsp
, sp
, MAY_WRITE
, NULL
);
1447 * Allow the operation to succeed if either task
1448 * has privilege to perform operations that might
1449 * account for the smack labels having gotten to
1450 * be different in the first place.
1452 * This breaks the strict subject/object access
1453 * control ideal, taking the object's privilege
1454 * state into account in the decision as well as
1457 if (capable(CAP_MAC_OVERRIDE
) || has_capability(p
, CAP_MAC_OVERRIDE
))
1459 /* we log only if we didn't get overriden */
1461 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_TASK
);
1462 smk_ad_setfield_u_tsk(&ad
, p
);
1463 smack_log(tsp
, sp
, MAY_WRITE
, rc
, &ad
);
1468 * smack_task_to_inode - copy task smack into the inode blob
1469 * @p: task to copy from
1470 * @inode: inode to copy to
1472 * Sets the smack pointer in the inode security blob
1474 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
1476 struct inode_smack
*isp
= inode
->i_security
;
1477 isp
->smk_inode
= smk_of_task(task_security(p
));
1485 * smack_sk_alloc_security - Allocate a socket blob
1488 * @gfp_flags: memory allocation flags
1490 * Assign Smack pointers to current
1492 * Returns 0 on success, -ENOMEM is there's no memory
1494 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
1496 char *csp
= smk_of_current();
1497 struct socket_smack
*ssp
;
1499 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
1505 ssp
->smk_packet
[0] = '\0';
1507 sk
->sk_security
= ssp
;
1513 * smack_sk_free_security - Free a socket blob
1516 * Clears the blob pointer
1518 static void smack_sk_free_security(struct sock
*sk
)
1520 kfree(sk
->sk_security
);
1524 * smack_host_label - check host based restrictions
1525 * @sip: the object end
1527 * looks for host based access restrictions
1529 * This version will only be appropriate for really small sets of single label
1530 * hosts. The caller is responsible for ensuring that the RCU read lock is
1531 * taken before calling this function.
1533 * Returns the label of the far end or NULL if it's not special.
1535 static char *smack_host_label(struct sockaddr_in
*sip
)
1537 struct smk_netlbladdr
*snp
;
1538 struct in_addr
*siap
= &sip
->sin_addr
;
1540 if (siap
->s_addr
== 0)
1543 list_for_each_entry_rcu(snp
, &smk_netlbladdr_list
, list
)
1545 * we break after finding the first match because
1546 * the list is sorted from longest to shortest mask
1547 * so we have found the most specific match
1549 if ((&snp
->smk_host
.sin_addr
)->s_addr
==
1550 (siap
->s_addr
& (&snp
->smk_mask
)->s_addr
)) {
1551 /* we have found the special CIPSO option */
1552 if (snp
->smk_label
== smack_cipso_option
)
1554 return snp
->smk_label
;
1561 * smack_set_catset - convert a capset to netlabel mls categories
1562 * @catset: the Smack categories
1563 * @sap: where to put the netlabel categories
1565 * Allocates and fills attr.mls.cat
1567 static void smack_set_catset(char *catset
, struct netlbl_lsm_secattr
*sap
)
1578 sap
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1579 sap
->attr
.mls
.cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1580 sap
->attr
.mls
.cat
->startbit
= 0;
1582 for (cat
= 1, cp
= catset
, byte
= 0; byte
< SMK_LABELLEN
; cp
++, byte
++)
1583 for (m
= 0x80; m
!= 0; m
>>= 1, cat
++) {
1586 rc
= netlbl_secattr_catmap_setbit(sap
->attr
.mls
.cat
,
1592 * smack_to_secattr - fill a secattr from a smack value
1593 * @smack: the smack value
1594 * @nlsp: where the result goes
1596 * Casey says that CIPSO is good enough for now.
1597 * It can be used to effect.
1598 * It can also be abused to effect when necessary.
1599 * Appologies to the TSIG group in general and GW in particular.
1601 static void smack_to_secattr(char *smack
, struct netlbl_lsm_secattr
*nlsp
)
1603 struct smack_cipso cipso
;
1606 nlsp
->domain
= smack
;
1607 nlsp
->flags
= NETLBL_SECATTR_DOMAIN
| NETLBL_SECATTR_MLS_LVL
;
1609 rc
= smack_to_cipso(smack
, &cipso
);
1611 nlsp
->attr
.mls
.lvl
= cipso
.smk_level
;
1612 smack_set_catset(cipso
.smk_catset
, nlsp
);
1614 nlsp
->attr
.mls
.lvl
= smack_cipso_direct
;
1615 smack_set_catset(smack
, nlsp
);
1620 * smack_netlabel - Set the secattr on a socket
1622 * @labeled: socket label scheme
1624 * Convert the outbound smack value (smk_out) to a
1625 * secattr and attach it to the socket.
1627 * Returns 0 on success or an error code
1629 static int smack_netlabel(struct sock
*sk
, int labeled
)
1631 struct socket_smack
*ssp
= sk
->sk_security
;
1632 struct netlbl_lsm_secattr secattr
;
1636 * Usually the netlabel code will handle changing the
1637 * packet labeling based on the label.
1638 * The case of a single label host is different, because
1639 * a single label host should never get a labeled packet
1640 * even though the label is usually associated with a packet
1644 bh_lock_sock_nested(sk
);
1646 if (ssp
->smk_out
== smack_net_ambient
||
1647 labeled
== SMACK_UNLABELED_SOCKET
)
1648 netlbl_sock_delattr(sk
);
1650 netlbl_secattr_init(&secattr
);
1651 smack_to_secattr(ssp
->smk_out
, &secattr
);
1652 rc
= netlbl_sock_setattr(sk
, sk
->sk_family
, &secattr
);
1653 netlbl_secattr_destroy(&secattr
);
1663 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1665 * @sap: the destination address
1667 * Set the correct secattr for the given socket based on the destination
1668 * address and perform any outbound access checks needed.
1670 * Returns 0 on success or an error code.
1673 static int smack_netlabel_send(struct sock
*sk
, struct sockaddr_in
*sap
)
1678 struct socket_smack
*ssp
= sk
->sk_security
;
1679 struct smk_audit_info ad
;
1682 hostsp
= smack_host_label(sap
);
1683 if (hostsp
!= NULL
) {
1684 sk_lbl
= SMACK_UNLABELED_SOCKET
;
1686 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
1687 ad
.a
.u
.net
.family
= sap
->sin_family
;
1688 ad
.a
.u
.net
.dport
= sap
->sin_port
;
1689 ad
.a
.u
.net
.v4info
.daddr
= sap
->sin_addr
.s_addr
;
1691 rc
= smk_access(ssp
->smk_out
, hostsp
, MAY_WRITE
, &ad
);
1693 sk_lbl
= SMACK_CIPSO_SOCKET
;
1700 return smack_netlabel(sk
, sk_lbl
);
1704 * smack_inode_setsecurity - set smack xattrs
1705 * @inode: the object
1706 * @name: attribute name
1707 * @value: attribute value
1708 * @size: size of the attribute
1711 * Sets the named attribute in the appropriate blob
1713 * Returns 0 on success, or an error code
1715 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
1716 const void *value
, size_t size
, int flags
)
1719 struct inode_smack
*nsp
= inode
->i_security
;
1720 struct socket_smack
*ssp
;
1721 struct socket
*sock
;
1724 if (value
== NULL
|| size
> SMK_LABELLEN
|| size
== 0)
1727 sp
= smk_import(value
, size
);
1731 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1732 nsp
->smk_inode
= sp
;
1733 nsp
->smk_flags
|= SMK_INODE_INSTANT
;
1737 * The rest of the Smack xattrs are only on sockets.
1739 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
1742 sock
= SOCKET_I(inode
);
1743 if (sock
== NULL
|| sock
->sk
== NULL
)
1746 ssp
= sock
->sk
->sk_security
;
1748 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1750 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
1752 if (sock
->sk
->sk_family
!= PF_UNIX
) {
1753 rc
= smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
1756 "Smack: \"%s\" netlbl error %d.\n",
1766 * smack_socket_post_create - finish socket setup
1768 * @family: protocol family
1773 * Sets the netlabel information on the socket
1775 * Returns 0 on success, and error code otherwise
1777 static int smack_socket_post_create(struct socket
*sock
, int family
,
1778 int type
, int protocol
, int kern
)
1780 if (family
!= PF_INET
|| sock
->sk
== NULL
)
1783 * Set the outbound netlbl.
1785 return smack_netlabel(sock
->sk
, SMACK_CIPSO_SOCKET
);
1789 * smack_socket_connect - connect access check
1791 * @sap: the other end
1792 * @addrlen: size of sap
1794 * Verifies that a connection may be possible
1796 * Returns 0 on success, and error code otherwise
1798 static int smack_socket_connect(struct socket
*sock
, struct sockaddr
*sap
,
1801 if (sock
->sk
== NULL
|| sock
->sk
->sk_family
!= PF_INET
)
1803 if (addrlen
< sizeof(struct sockaddr_in
))
1806 return smack_netlabel_send(sock
->sk
, (struct sockaddr_in
*)sap
);
1810 * smack_flags_to_may - convert S_ to MAY_ values
1811 * @flags: the S_ value
1813 * Returns the equivalent MAY_ value
1815 static int smack_flags_to_may(int flags
)
1819 if (flags
& S_IRUGO
)
1821 if (flags
& S_IWUGO
)
1823 if (flags
& S_IXUGO
)
1830 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1835 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
1837 msg
->security
= smk_of_current();
1842 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1845 * Clears the blob pointer
1847 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
1849 msg
->security
= NULL
;
1853 * smack_of_shm - the smack pointer for the shm
1856 * Returns a pointer to the smack value
1858 static char *smack_of_shm(struct shmid_kernel
*shp
)
1860 return (char *)shp
->shm_perm
.security
;
1864 * smack_shm_alloc_security - Set the security blob for shm
1869 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
1871 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
1873 isp
->security
= smk_of_current();
1878 * smack_shm_free_security - Clear the security blob for shm
1881 * Clears the blob pointer
1883 static void smack_shm_free_security(struct shmid_kernel
*shp
)
1885 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
1887 isp
->security
= NULL
;
1891 * smk_curacc_shm : check if current has access on shm
1893 * @access : access requested
1895 * Returns 0 if current has the requested access, error code otherwise
1897 static int smk_curacc_shm(struct shmid_kernel
*shp
, int access
)
1899 char *ssp
= smack_of_shm(shp
);
1900 struct smk_audit_info ad
;
1903 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
1904 ad
.a
.u
.ipc_id
= shp
->shm_perm
.id
;
1906 return smk_curacc(ssp
, access
, &ad
);
1910 * smack_shm_associate - Smack access check for shm
1912 * @shmflg: access requested
1914 * Returns 0 if current has the requested access, error code otherwise
1916 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
1920 may
= smack_flags_to_may(shmflg
);
1921 return smk_curacc_shm(shp
, may
);
1925 * smack_shm_shmctl - Smack access check for shm
1927 * @cmd: what it wants to do
1929 * Returns 0 if current has the requested access, error code otherwise
1931 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
1944 may
= MAY_READWRITE
;
1949 * System level information.
1955 return smk_curacc_shm(shp
, may
);
1959 * smack_shm_shmat - Smack access for shmat
1962 * @shmflg: access requested
1964 * Returns 0 if current has the requested access, error code otherwise
1966 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
1971 may
= smack_flags_to_may(shmflg
);
1972 return smk_curacc_shm(shp
, may
);
1976 * smack_of_sem - the smack pointer for the sem
1979 * Returns a pointer to the smack value
1981 static char *smack_of_sem(struct sem_array
*sma
)
1983 return (char *)sma
->sem_perm
.security
;
1987 * smack_sem_alloc_security - Set the security blob for sem
1992 static int smack_sem_alloc_security(struct sem_array
*sma
)
1994 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
1996 isp
->security
= smk_of_current();
2001 * smack_sem_free_security - Clear the security blob for sem
2004 * Clears the blob pointer
2006 static void smack_sem_free_security(struct sem_array
*sma
)
2008 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
2010 isp
->security
= NULL
;
2014 * smk_curacc_sem : check if current has access on sem
2016 * @access : access requested
2018 * Returns 0 if current has the requested access, error code otherwise
2020 static int smk_curacc_sem(struct sem_array
*sma
, int access
)
2022 char *ssp
= smack_of_sem(sma
);
2023 struct smk_audit_info ad
;
2026 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2027 ad
.a
.u
.ipc_id
= sma
->sem_perm
.id
;
2029 return smk_curacc(ssp
, access
, &ad
);
2033 * smack_sem_associate - Smack access check for sem
2035 * @semflg: access requested
2037 * Returns 0 if current has the requested access, error code otherwise
2039 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
2043 may
= smack_flags_to_may(semflg
);
2044 return smk_curacc_sem(sma
, may
);
2048 * smack_sem_shmctl - Smack access check for sem
2050 * @cmd: what it wants to do
2052 * Returns 0 if current has the requested access, error code otherwise
2054 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
2072 may
= MAY_READWRITE
;
2077 * System level information
2084 return smk_curacc_sem(sma
, may
);
2088 * smack_sem_semop - Smack checks of semaphore operations
2094 * Treated as read and write in all cases.
2096 * Returns 0 if access is allowed, error code otherwise
2098 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
2099 unsigned nsops
, int alter
)
2101 return smk_curacc_sem(sma
, MAY_READWRITE
);
2105 * smack_msg_alloc_security - Set the security blob for msg
2110 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
2112 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2114 kisp
->security
= smk_of_current();
2119 * smack_msg_free_security - Clear the security blob for msg
2122 * Clears the blob pointer
2124 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
2126 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
2128 kisp
->security
= NULL
;
2132 * smack_of_msq - the smack pointer for the msq
2135 * Returns a pointer to the smack value
2137 static char *smack_of_msq(struct msg_queue
*msq
)
2139 return (char *)msq
->q_perm
.security
;
2143 * smk_curacc_msq : helper to check if current has access on msq
2145 * @access : access requested
2147 * return 0 if current has access, error otherwise
2149 static int smk_curacc_msq(struct msg_queue
*msq
, int access
)
2151 char *msp
= smack_of_msq(msq
);
2152 struct smk_audit_info ad
;
2155 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2156 ad
.a
.u
.ipc_id
= msq
->q_perm
.id
;
2158 return smk_curacc(msp
, access
, &ad
);
2162 * smack_msg_queue_associate - Smack access check for msg_queue
2164 * @msqflg: access requested
2166 * Returns 0 if current has the requested access, error code otherwise
2168 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
2172 may
= smack_flags_to_may(msqflg
);
2173 return smk_curacc_msq(msq
, may
);
2177 * smack_msg_queue_msgctl - Smack access check for msg_queue
2179 * @cmd: what it wants to do
2181 * Returns 0 if current has the requested access, error code otherwise
2183 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
2194 may
= MAY_READWRITE
;
2199 * System level information
2206 return smk_curacc_msq(msq
, may
);
2210 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2213 * @msqflg: access requested
2215 * Returns 0 if current has the requested access, error code otherwise
2217 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
2222 may
= smack_flags_to_may(msqflg
);
2223 return smk_curacc_msq(msq
, may
);
2227 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2234 * Returns 0 if current has read and write access, error code otherwise
2236 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
2237 struct task_struct
*target
, long type
, int mode
)
2239 return smk_curacc_msq(msq
, MAY_READWRITE
);
2243 * smack_ipc_permission - Smack access for ipc_permission()
2244 * @ipp: the object permissions
2245 * @flag: access requested
2247 * Returns 0 if current has read and write access, error code otherwise
2249 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
2251 char *isp
= ipp
->security
;
2252 int may
= smack_flags_to_may(flag
);
2253 struct smk_audit_info ad
;
2256 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_IPC
);
2257 ad
.a
.u
.ipc_id
= ipp
->id
;
2259 return smk_curacc(isp
, may
, &ad
);
2263 * smack_ipc_getsecid - Extract smack security id
2264 * @ipp: the object permissions
2265 * @secid: where result will be saved
2267 static void smack_ipc_getsecid(struct kern_ipc_perm
*ipp
, u32
*secid
)
2269 char *smack
= ipp
->security
;
2271 *secid
= smack_to_secid(smack
);
2275 * smack_d_instantiate - Make sure the blob is correct on an inode
2276 * @opt_dentry: dentry where inode will be attached
2277 * @inode: the object
2279 * Set the inode's security blob if it hasn't been done already.
2281 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
2283 struct super_block
*sbp
;
2284 struct superblock_smack
*sbsp
;
2285 struct inode_smack
*isp
;
2286 char *csp
= smk_of_current();
2289 char trattr
[TRANS_TRUE_SIZE
];
2296 isp
= inode
->i_security
;
2298 mutex_lock(&isp
->smk_lock
);
2300 * If the inode is already instantiated
2301 * take the quick way out
2303 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
2307 sbsp
= sbp
->s_security
;
2309 * We're going to use the superblock default label
2310 * if there's no label on the file.
2312 final
= sbsp
->smk_default
;
2315 * If this is the root inode the superblock
2316 * may be in the process of initialization.
2317 * If that is the case use the root value out
2318 * of the superblock.
2320 if (opt_dentry
->d_parent
== opt_dentry
) {
2321 isp
->smk_inode
= sbsp
->smk_root
;
2322 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2327 * This is pretty hackish.
2328 * Casey says that we shouldn't have to do
2329 * file system specific code, but it does help
2330 * with keeping it simple.
2332 switch (sbp
->s_magic
) {
2335 * Casey says that it's a little embarassing
2336 * that the smack file system doesn't do
2337 * extended attributes.
2339 final
= smack_known_star
.smk_known
;
2343 * Casey says pipes are easy (?)
2345 final
= smack_known_star
.smk_known
;
2347 case DEVPTS_SUPER_MAGIC
:
2349 * devpts seems content with the label of the task.
2350 * Programs that change smack have to treat the
2357 * Socket access is controlled by the socket
2358 * structures associated with the task involved.
2360 final
= smack_known_star
.smk_known
;
2362 case PROC_SUPER_MAGIC
:
2364 * Casey says procfs appears not to care.
2365 * The superblock default suffices.
2370 * Device labels should come from the filesystem,
2371 * but watch out, because they're volitile,
2372 * getting recreated on every reboot.
2374 final
= smack_known_star
.smk_known
;
2378 * If a smack value has been set we want to use it,
2379 * but since tmpfs isn't giving us the opportunity
2380 * to set mount options simulate setting the
2381 * superblock default.
2385 * This isn't an understood special case.
2386 * Get the value from the xattr.
2390 * UNIX domain sockets use lower level socket data.
2392 if (S_ISSOCK(inode
->i_mode
)) {
2393 final
= smack_known_star
.smk_known
;
2397 * No xattr support means, alas, no SMACK label.
2398 * Use the aforeapplied default.
2399 * It would be curious if the label of the task
2400 * does not match that assigned.
2402 if (inode
->i_op
->getxattr
== NULL
)
2405 * Get the dentry for xattr.
2407 dp
= dget(opt_dentry
);
2408 fetched
= smk_fetch(XATTR_NAME_SMACK
, inode
, dp
);
2409 if (fetched
!= NULL
) {
2411 if (S_ISDIR(inode
->i_mode
)) {
2413 inode
->i_op
->getxattr(dp
,
2414 XATTR_NAME_SMACKTRANSMUTE
,
2415 trattr
, TRANS_TRUE_SIZE
);
2416 if (strncmp(trattr
, TRANS_TRUE
,
2417 TRANS_TRUE_SIZE
) == 0)
2418 transflag
= SMK_INODE_TRANSMUTE
;
2421 isp
->smk_task
= smk_fetch(XATTR_NAME_SMACKEXEC
, inode
, dp
);
2428 isp
->smk_inode
= csp
;
2430 isp
->smk_inode
= final
;
2432 isp
->smk_flags
|= (SMK_INODE_INSTANT
| transflag
);
2435 mutex_unlock(&isp
->smk_lock
);
2440 * smack_getprocattr - Smack process attribute access
2441 * @p: the object task
2442 * @name: the name of the attribute in /proc/.../attr
2443 * @value: where to put the result
2445 * Places a copy of the task Smack into value
2447 * Returns the length of the smack label or an error code
2449 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
2454 if (strcmp(name
, "current") != 0)
2457 cp
= kstrdup(smk_of_task(task_security(p
)), GFP_KERNEL
);
2467 * smack_setprocattr - Smack process attribute setting
2468 * @p: the object task
2469 * @name: the name of the attribute in /proc/.../attr
2470 * @value: the value to set
2471 * @size: the size of the value
2473 * Sets the Smack value of the task. Only setting self
2474 * is permitted and only with privilege
2476 * Returns the length of the smack label or an error code
2478 static int smack_setprocattr(struct task_struct
*p
, char *name
,
2479 void *value
, size_t size
)
2481 struct task_smack
*tsp
;
2482 struct task_smack
*oldtsp
;
2487 * Changing another process' Smack value is too dangerous
2488 * and supports no sane use case.
2493 if (!capable(CAP_MAC_ADMIN
))
2496 if (value
== NULL
|| size
== 0 || size
>= SMK_LABELLEN
)
2499 if (strcmp(name
, "current") != 0)
2502 newsmack
= smk_import(value
, size
);
2503 if (newsmack
== NULL
)
2507 * No process is ever allowed the web ("@") label.
2509 if (newsmack
== smack_known_web
.smk_known
)
2512 oldtsp
= p
->cred
->security
;
2513 new = prepare_creds();
2516 tsp
= kzalloc(sizeof(struct task_smack
), GFP_KERNEL
);
2521 tsp
->smk_task
= newsmack
;
2522 tsp
->smk_forked
= oldtsp
->smk_forked
;
2523 new->security
= tsp
;
2529 * smack_unix_stream_connect - Smack access on UDS
2531 * @other: the other sock
2534 * Return 0 if a subject with the smack of sock could access
2535 * an object with the smack of other, otherwise an error code
2537 static int smack_unix_stream_connect(struct sock
*sock
,
2538 struct sock
*other
, struct sock
*newsk
)
2540 struct socket_smack
*ssp
= sock
->sk_security
;
2541 struct socket_smack
*osp
= other
->sk_security
;
2542 struct smk_audit_info ad
;
2545 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2546 smk_ad_setfield_u_net_sk(&ad
, other
);
2548 if (!capable(CAP_MAC_OVERRIDE
))
2549 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
2555 * smack_unix_may_send - Smack access on UDS
2557 * @other: the other socket
2559 * Return 0 if a subject with the smack of sock could access
2560 * an object with the smack of other, otherwise an error code
2562 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
2564 struct socket_smack
*ssp
= sock
->sk
->sk_security
;
2565 struct socket_smack
*osp
= other
->sk
->sk_security
;
2566 struct smk_audit_info ad
;
2569 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2570 smk_ad_setfield_u_net_sk(&ad
, other
->sk
);
2572 if (!capable(CAP_MAC_OVERRIDE
))
2573 rc
= smk_access(ssp
->smk_out
, osp
->smk_in
, MAY_WRITE
, &ad
);
2579 * smack_socket_sendmsg - Smack check based on destination host
2582 * @size: the size of the message
2584 * Return 0 if the current subject can write to the destination
2585 * host. This is only a question if the destination is a single
2588 static int smack_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
2591 struct sockaddr_in
*sip
= (struct sockaddr_in
*) msg
->msg_name
;
2594 * Perfectly reasonable for this to be NULL
2596 if (sip
== NULL
|| sip
->sin_family
!= AF_INET
)
2599 return smack_netlabel_send(sock
->sk
, sip
);
2604 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2605 * @sap: netlabel secattr
2606 * @sip: where to put the result
2608 * Copies a smack label into sip
2610 static void smack_from_secattr(struct netlbl_lsm_secattr
*sap
, char *sip
)
2612 char smack
[SMK_LABELLEN
];
2616 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) != 0) {
2618 * Looks like a CIPSO packet.
2619 * If there are flags but no level netlabel isn't
2620 * behaving the way we expect it to.
2622 * Get the categories, if any
2623 * Without guidance regarding the smack value
2624 * for the packet fall back on the network
2627 memset(smack
, '\0', SMK_LABELLEN
);
2628 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) != 0)
2630 pcat
= netlbl_secattr_catmap_walk(
2631 sap
->attr
.mls
.cat
, pcat
+ 1);
2634 smack_catset_bit(pcat
, smack
);
2637 * If it is CIPSO using smack direct mapping
2638 * we are already done. WeeHee.
2640 if (sap
->attr
.mls
.lvl
== smack_cipso_direct
) {
2641 memcpy(sip
, smack
, SMK_MAXLEN
);
2645 * Look it up in the supplied table if it is not
2648 smack_from_cipso(sap
->attr
.mls
.lvl
, smack
, sip
);
2651 if ((sap
->flags
& NETLBL_SECATTR_SECID
) != 0) {
2653 * Looks like a fallback, which gives us a secid.
2655 sp
= smack_from_secid(sap
->attr
.secid
);
2657 * This has got to be a bug because it is
2658 * impossible to specify a fallback without
2659 * specifying the label, which will ensure
2660 * it has a secid, and the only way to get a
2661 * secid is from a fallback.
2664 strncpy(sip
, sp
, SMK_MAXLEN
);
2668 * Without guidance regarding the smack value
2669 * for the packet fall back on the network
2672 strncpy(sip
, smack_net_ambient
, SMK_MAXLEN
);
2677 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2681 * Returns 0 if the packet should be delivered, an error code otherwise
2683 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
2685 struct netlbl_lsm_secattr secattr
;
2686 struct socket_smack
*ssp
= sk
->sk_security
;
2687 char smack
[SMK_LABELLEN
];
2690 struct smk_audit_info ad
;
2691 if (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
)
2695 * Translate what netlabel gave us.
2697 netlbl_secattr_init(&secattr
);
2699 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
2701 smack_from_secattr(&secattr
, smack
);
2704 csp
= smack_net_ambient
;
2706 netlbl_secattr_destroy(&secattr
);
2709 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2710 ad
.a
.u
.net
.family
= sk
->sk_family
;
2711 ad
.a
.u
.net
.netif
= skb
->skb_iif
;
2712 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
2715 * Receiving a packet requires that the other end
2716 * be able to write here. Read access is not required.
2717 * This is the simplist possible security model
2720 rc
= smk_access(csp
, ssp
->smk_in
, MAY_WRITE
, &ad
);
2722 netlbl_skbuff_err(skb
, rc
, 0);
2727 * smack_socket_getpeersec_stream - pull in packet label
2729 * @optval: user's destination
2730 * @optlen: size thereof
2733 * returns zero on success, an error code otherwise
2735 static int smack_socket_getpeersec_stream(struct socket
*sock
,
2736 char __user
*optval
,
2737 int __user
*optlen
, unsigned len
)
2739 struct socket_smack
*ssp
;
2743 ssp
= sock
->sk
->sk_security
;
2744 slen
= strlen(ssp
->smk_packet
) + 1;
2748 else if (copy_to_user(optval
, ssp
->smk_packet
, slen
) != 0)
2751 if (put_user(slen
, optlen
) != 0)
2759 * smack_socket_getpeersec_dgram - pull in packet label
2760 * @sock: the peer socket
2762 * @secid: pointer to where to put the secid of the packet
2764 * Sets the netlabel socket state on sk from parent
2766 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
2767 struct sk_buff
*skb
, u32
*secid
)
2770 struct netlbl_lsm_secattr secattr
;
2771 struct socket_smack
*sp
;
2772 char smack
[SMK_LABELLEN
];
2773 int family
= PF_UNSPEC
;
2774 u32 s
= 0; /* 0 is the invalid secid */
2778 if (skb
->protocol
== htons(ETH_P_IP
))
2780 else if (skb
->protocol
== htons(ETH_P_IPV6
))
2783 if (family
== PF_UNSPEC
&& sock
!= NULL
)
2784 family
= sock
->sk
->sk_family
;
2786 if (family
== PF_UNIX
) {
2787 sp
= sock
->sk
->sk_security
;
2788 s
= smack_to_secid(sp
->smk_out
);
2789 } else if (family
== PF_INET
|| family
== PF_INET6
) {
2791 * Translate what netlabel gave us.
2793 netlbl_secattr_init(&secattr
);
2794 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
2796 smack_from_secattr(&secattr
, smack
);
2797 s
= smack_to_secid(smack
);
2799 netlbl_secattr_destroy(&secattr
);
2808 * smack_sock_graft - Initialize a newly created socket with an existing sock
2810 * @parent: parent socket
2812 * Set the smk_{in,out} state of an existing sock based on the process that
2813 * is creating the new socket.
2815 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
2817 struct socket_smack
*ssp
;
2820 (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
))
2823 ssp
= sk
->sk_security
;
2824 ssp
->smk_in
= ssp
->smk_out
= smk_of_current();
2825 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2829 * smack_inet_conn_request - Smack access check on connect
2830 * @sk: socket involved
2834 * Returns 0 if a task with the packet label could write to
2835 * the socket, otherwise an error code
2837 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
2838 struct request_sock
*req
)
2840 u16 family
= sk
->sk_family
;
2841 struct socket_smack
*ssp
= sk
->sk_security
;
2842 struct netlbl_lsm_secattr secattr
;
2843 struct sockaddr_in addr
;
2845 char smack
[SMK_LABELLEN
];
2847 struct smk_audit_info ad
;
2849 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2850 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
2853 netlbl_secattr_init(&secattr
);
2854 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
2856 smack_from_secattr(&secattr
, smack
);
2858 strncpy(smack
, smack_known_huh
.smk_known
, SMK_MAXLEN
);
2859 netlbl_secattr_destroy(&secattr
);
2862 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_NET
);
2863 ad
.a
.u
.net
.family
= family
;
2864 ad
.a
.u
.net
.netif
= skb
->skb_iif
;
2865 ipv4_skb_to_auditdata(skb
, &ad
.a
, NULL
);
2868 * Receiving a packet requires that the other end be able to write
2869 * here. Read access is not required.
2871 rc
= smk_access(smack
, ssp
->smk_in
, MAY_WRITE
, &ad
);
2876 * Save the peer's label in the request_sock so we can later setup
2877 * smk_packet in the child socket so that SO_PEERCRED can report it.
2879 req
->peer_secid
= smack_to_secid(smack
);
2882 * We need to decide if we want to label the incoming connection here
2883 * if we do we only need to label the request_sock and the stack will
2884 * propogate the wire-label to the sock when it is created.
2887 addr
.sin_addr
.s_addr
= hdr
->saddr
;
2889 if (smack_host_label(&addr
) == NULL
) {
2891 netlbl_secattr_init(&secattr
);
2892 smack_to_secattr(smack
, &secattr
);
2893 rc
= netlbl_req_setattr(req
, &secattr
);
2894 netlbl_secattr_destroy(&secattr
);
2897 netlbl_req_delattr(req
);
2904 * smack_inet_csk_clone - Copy the connection information to the new socket
2905 * @sk: the new socket
2906 * @req: the connection's request_sock
2908 * Transfer the connection's peer label to the newly created socket.
2910 static void smack_inet_csk_clone(struct sock
*sk
,
2911 const struct request_sock
*req
)
2913 struct socket_smack
*ssp
= sk
->sk_security
;
2916 if (req
->peer_secid
!= 0) {
2917 smack
= smack_from_secid(req
->peer_secid
);
2918 strncpy(ssp
->smk_packet
, smack
, SMK_MAXLEN
);
2920 ssp
->smk_packet
[0] = '\0';
2924 * Key management security hooks
2926 * Casey has not tested key support very heavily.
2927 * The permission check is most likely too restrictive.
2928 * If you care about keys please have a look.
2933 * smack_key_alloc - Set the key security blob
2935 * @cred: the credentials to use
2938 * No allocation required
2942 static int smack_key_alloc(struct key
*key
, const struct cred
*cred
,
2943 unsigned long flags
)
2945 key
->security
= smk_of_task(cred
->security
);
2950 * smack_key_free - Clear the key security blob
2953 * Clear the blob pointer
2955 static void smack_key_free(struct key
*key
)
2957 key
->security
= NULL
;
2961 * smack_key_permission - Smack access on a key
2962 * @key_ref: gets to the object
2963 * @cred: the credentials to use
2966 * Return 0 if the task has read and write to the object,
2967 * an error code otherwise
2969 static int smack_key_permission(key_ref_t key_ref
,
2970 const struct cred
*cred
, key_perm_t perm
)
2973 struct smk_audit_info ad
;
2974 char *tsp
= smk_of_task(cred
->security
);
2976 keyp
= key_ref_to_ptr(key_ref
);
2980 * If the key hasn't been initialized give it access so that
2983 if (keyp
->security
== NULL
)
2986 * This should not occur
2991 smk_ad_init(&ad
, __func__
, LSM_AUDIT_DATA_KEY
);
2992 ad
.a
.u
.key_struct
.key
= keyp
->serial
;
2993 ad
.a
.u
.key_struct
.key_desc
= keyp
->description
;
2995 return smk_access(tsp
, keyp
->security
,
2996 MAY_READWRITE
, &ad
);
2998 #endif /* CONFIG_KEYS */
3003 * Audit requires a unique representation of each Smack specific
3004 * rule. This unique representation is used to distinguish the
3005 * object to be audited from remaining kernel objects and also
3006 * works as a glue between the audit hooks.
3008 * Since repository entries are added but never deleted, we'll use
3009 * the smack_known label address related to the given audit rule as
3010 * the needed unique representation. This also better fits the smack
3011 * model where nearly everything is a label.
3016 * smack_audit_rule_init - Initialize a smack audit rule
3017 * @field: audit rule fields given from user-space (audit.h)
3018 * @op: required testing operator (=, !=, >, <, ...)
3019 * @rulestr: smack label to be audited
3020 * @vrule: pointer to save our own audit rule representation
3022 * Prepare to audit cases where (@field @op @rulestr) is true.
3023 * The label to be audited is created if necessay.
3025 static int smack_audit_rule_init(u32 field
, u32 op
, char *rulestr
, void **vrule
)
3027 char **rule
= (char **)vrule
;
3030 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3033 if (op
!= Audit_equal
&& op
!= Audit_not_equal
)
3036 *rule
= smk_import(rulestr
, 0);
3042 * smack_audit_rule_known - Distinguish Smack audit rules
3043 * @krule: rule of interest, in Audit kernel representation format
3045 * This is used to filter Smack rules from remaining Audit ones.
3046 * If it's proved that this rule belongs to us, the
3047 * audit_rule_match hook will be called to do the final judgement.
3049 static int smack_audit_rule_known(struct audit_krule
*krule
)
3051 struct audit_field
*f
;
3054 for (i
= 0; i
< krule
->field_count
; i
++) {
3055 f
= &krule
->fields
[i
];
3057 if (f
->type
== AUDIT_SUBJ_USER
|| f
->type
== AUDIT_OBJ_USER
)
3065 * smack_audit_rule_match - Audit given object ?
3066 * @secid: security id for identifying the object to test
3067 * @field: audit rule flags given from user-space
3068 * @op: required testing operator
3069 * @vrule: smack internal rule presentation
3070 * @actx: audit context associated with the check
3072 * The core Audit hook. It's used to take the decision of
3073 * whether to audit or not to audit a given object.
3075 static int smack_audit_rule_match(u32 secid
, u32 field
, u32 op
, void *vrule
,
3076 struct audit_context
*actx
)
3082 audit_log(actx
, GFP_KERNEL
, AUDIT_SELINUX_ERR
,
3083 "Smack: missing rule\n");
3087 if (field
!= AUDIT_SUBJ_USER
&& field
!= AUDIT_OBJ_USER
)
3090 smack
= smack_from_secid(secid
);
3093 * No need to do string comparisons. If a match occurs,
3094 * both pointers will point to the same smack_known
3097 if (op
== Audit_equal
)
3098 return (rule
== smack
);
3099 if (op
== Audit_not_equal
)
3100 return (rule
!= smack
);
3106 * smack_audit_rule_free - free smack rule representation
3107 * @vrule: rule to be freed.
3109 * No memory was allocated.
3111 static void smack_audit_rule_free(void *vrule
)
3116 #endif /* CONFIG_AUDIT */
3119 * smack_secid_to_secctx - return the smack label for a secid
3120 * @secid: incoming integer
3121 * @secdata: destination
3122 * @seclen: how long it is
3124 * Exists for networking code.
3126 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
3128 char *sp
= smack_from_secid(secid
);
3132 *seclen
= strlen(sp
);
3137 * smack_secctx_to_secid - return the secid for a smack label
3138 * @secdata: smack label
3139 * @seclen: how long result is
3140 * @secid: outgoing integer
3142 * Exists for audit and networking code.
3144 static int smack_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
3146 *secid
= smack_to_secid(secdata
);
3151 * smack_release_secctx - don't do anything.
3155 * Exists to make sure nothing gets done, and properly
3157 static void smack_release_secctx(char *secdata
, u32 seclen
)
3161 static int smack_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
3163 return smack_inode_setsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, ctxlen
, 0);
3166 static int smack_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
3168 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SMACK
, ctx
, ctxlen
, 0);
3171 static int smack_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
3174 len
= smack_inode_getsecurity(inode
, XATTR_SMACK_SUFFIX
, ctx
, true);
3182 struct security_operations smack_ops
= {
3185 .ptrace_access_check
= smack_ptrace_access_check
,
3186 .ptrace_traceme
= smack_ptrace_traceme
,
3187 .syslog
= smack_syslog
,
3189 .sb_alloc_security
= smack_sb_alloc_security
,
3190 .sb_free_security
= smack_sb_free_security
,
3191 .sb_copy_data
= smack_sb_copy_data
,
3192 .sb_kern_mount
= smack_sb_kern_mount
,
3193 .sb_statfs
= smack_sb_statfs
,
3194 .sb_mount
= smack_sb_mount
,
3195 .sb_umount
= smack_sb_umount
,
3197 .bprm_set_creds
= smack_bprm_set_creds
,
3199 .inode_alloc_security
= smack_inode_alloc_security
,
3200 .inode_free_security
= smack_inode_free_security
,
3201 .inode_init_security
= smack_inode_init_security
,
3202 .inode_link
= smack_inode_link
,
3203 .inode_unlink
= smack_inode_unlink
,
3204 .inode_rmdir
= smack_inode_rmdir
,
3205 .inode_rename
= smack_inode_rename
,
3206 .inode_permission
= smack_inode_permission
,
3207 .inode_setattr
= smack_inode_setattr
,
3208 .inode_getattr
= smack_inode_getattr
,
3209 .inode_setxattr
= smack_inode_setxattr
,
3210 .inode_post_setxattr
= smack_inode_post_setxattr
,
3211 .inode_getxattr
= smack_inode_getxattr
,
3212 .inode_removexattr
= smack_inode_removexattr
,
3213 .inode_getsecurity
= smack_inode_getsecurity
,
3214 .inode_setsecurity
= smack_inode_setsecurity
,
3215 .inode_listsecurity
= smack_inode_listsecurity
,
3216 .inode_getsecid
= smack_inode_getsecid
,
3218 .file_permission
= smack_file_permission
,
3219 .file_alloc_security
= smack_file_alloc_security
,
3220 .file_free_security
= smack_file_free_security
,
3221 .file_ioctl
= smack_file_ioctl
,
3222 .file_lock
= smack_file_lock
,
3223 .file_fcntl
= smack_file_fcntl
,
3224 .file_set_fowner
= smack_file_set_fowner
,
3225 .file_send_sigiotask
= smack_file_send_sigiotask
,
3226 .file_receive
= smack_file_receive
,
3228 .cred_alloc_blank
= smack_cred_alloc_blank
,
3229 .cred_free
= smack_cred_free
,
3230 .cred_prepare
= smack_cred_prepare
,
3231 .cred_transfer
= smack_cred_transfer
,
3232 .kernel_act_as
= smack_kernel_act_as
,
3233 .kernel_create_files_as
= smack_kernel_create_files_as
,
3234 .task_setpgid
= smack_task_setpgid
,
3235 .task_getpgid
= smack_task_getpgid
,
3236 .task_getsid
= smack_task_getsid
,
3237 .task_getsecid
= smack_task_getsecid
,
3238 .task_setnice
= smack_task_setnice
,
3239 .task_setioprio
= smack_task_setioprio
,
3240 .task_getioprio
= smack_task_getioprio
,
3241 .task_setscheduler
= smack_task_setscheduler
,
3242 .task_getscheduler
= smack_task_getscheduler
,
3243 .task_movememory
= smack_task_movememory
,
3244 .task_kill
= smack_task_kill
,
3245 .task_wait
= smack_task_wait
,
3246 .task_to_inode
= smack_task_to_inode
,
3248 .ipc_permission
= smack_ipc_permission
,
3249 .ipc_getsecid
= smack_ipc_getsecid
,
3251 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
3252 .msg_msg_free_security
= smack_msg_msg_free_security
,
3254 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
3255 .msg_queue_free_security
= smack_msg_queue_free_security
,
3256 .msg_queue_associate
= smack_msg_queue_associate
,
3257 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
3258 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
3259 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
3261 .shm_alloc_security
= smack_shm_alloc_security
,
3262 .shm_free_security
= smack_shm_free_security
,
3263 .shm_associate
= smack_shm_associate
,
3264 .shm_shmctl
= smack_shm_shmctl
,
3265 .shm_shmat
= smack_shm_shmat
,
3267 .sem_alloc_security
= smack_sem_alloc_security
,
3268 .sem_free_security
= smack_sem_free_security
,
3269 .sem_associate
= smack_sem_associate
,
3270 .sem_semctl
= smack_sem_semctl
,
3271 .sem_semop
= smack_sem_semop
,
3273 .d_instantiate
= smack_d_instantiate
,
3275 .getprocattr
= smack_getprocattr
,
3276 .setprocattr
= smack_setprocattr
,
3278 .unix_stream_connect
= smack_unix_stream_connect
,
3279 .unix_may_send
= smack_unix_may_send
,
3281 .socket_post_create
= smack_socket_post_create
,
3282 .socket_connect
= smack_socket_connect
,
3283 .socket_sendmsg
= smack_socket_sendmsg
,
3284 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
3285 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
3286 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
3287 .sk_alloc_security
= smack_sk_alloc_security
,
3288 .sk_free_security
= smack_sk_free_security
,
3289 .sock_graft
= smack_sock_graft
,
3290 .inet_conn_request
= smack_inet_conn_request
,
3291 .inet_csk_clone
= smack_inet_csk_clone
,
3293 /* key management security hooks */
3295 .key_alloc
= smack_key_alloc
,
3296 .key_free
= smack_key_free
,
3297 .key_permission
= smack_key_permission
,
3298 #endif /* CONFIG_KEYS */
3302 .audit_rule_init
= smack_audit_rule_init
,
3303 .audit_rule_known
= smack_audit_rule_known
,
3304 .audit_rule_match
= smack_audit_rule_match
,
3305 .audit_rule_free
= smack_audit_rule_free
,
3306 #endif /* CONFIG_AUDIT */
3308 .secid_to_secctx
= smack_secid_to_secctx
,
3309 .secctx_to_secid
= smack_secctx_to_secid
,
3310 .release_secctx
= smack_release_secctx
,
3311 .inode_notifysecctx
= smack_inode_notifysecctx
,
3312 .inode_setsecctx
= smack_inode_setsecctx
,
3313 .inode_getsecctx
= smack_inode_getsecctx
,
3317 static __init
void init_smack_know_list(void)
3319 list_add(&smack_known_huh
.list
, &smack_known_list
);
3320 list_add(&smack_known_hat
.list
, &smack_known_list
);
3321 list_add(&smack_known_star
.list
, &smack_known_list
);
3322 list_add(&smack_known_floor
.list
, &smack_known_list
);
3323 list_add(&smack_known_invalid
.list
, &smack_known_list
);
3324 list_add(&smack_known_web
.list
, &smack_known_list
);
3328 * smack_init - initialize the smack system
3332 static __init
int smack_init(void)
3335 struct task_smack
*tsp
;
3337 tsp
= kzalloc(sizeof(struct task_smack
), GFP_KERNEL
);
3341 if (!security_module_enable(&smack_ops
)) {
3346 printk(KERN_INFO
"Smack: Initializing.\n");
3349 * Set the security state for the initial task.
3351 cred
= (struct cred
*) current
->cred
;
3352 tsp
->smk_forked
= smack_known_floor
.smk_known
;
3353 tsp
->smk_task
= smack_known_floor
.smk_known
;
3354 cred
->security
= tsp
;
3356 /* initialize the smack_know_list */
3357 init_smack_know_list();
3361 spin_lock_init(&smack_known_huh
.smk_cipsolock
);
3362 spin_lock_init(&smack_known_hat
.smk_cipsolock
);
3363 spin_lock_init(&smack_known_star
.smk_cipsolock
);
3364 spin_lock_init(&smack_known_floor
.smk_cipsolock
);
3365 spin_lock_init(&smack_known_invalid
.smk_cipsolock
);
3370 if (register_security(&smack_ops
))
3371 panic("smack: Unable to register with kernel.\n");
3377 * Smack requires early initialization in order to label
3378 * all processes and objects when they are created.
3380 security_initcall(smack_init
);