2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
16 #include <linux/xattr.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/stat.h>
20 #include <linux/ext2_fs.h>
22 #include <asm/ioctls.h>
23 #include <linux/tcp.h>
24 #include <linux/udp.h>
25 #include <linux/mutex.h>
26 #include <linux/pipe_fs_i.h>
27 #include <net/netlabel.h>
28 #include <net/cipso_ipv4.h>
33 * I hope these are the hokeyist lines of code in the module. Casey.
35 #define DEVPTS_SUPER_MAGIC 0x1cd1
36 #define SOCKFS_MAGIC 0x534F434B
37 #define TMPFS_MAGIC 0x01021994
40 * smk_fetch - Fetch the smack label from a file.
41 * @ip: a pointer to the inode
42 * @dp: a pointer to the dentry
44 * Returns a pointer to the master list entry for the Smack label
45 * or NULL if there was no label to fetch.
47 static char *smk_fetch(struct inode
*ip
, struct dentry
*dp
)
50 char in
[SMK_LABELLEN
];
52 if (ip
->i_op
->getxattr
== NULL
)
55 rc
= ip
->i_op
->getxattr(dp
, XATTR_NAME_SMACK
, in
, SMK_LABELLEN
);
59 return smk_import(in
, rc
);
63 * new_inode_smack - allocate an inode security blob
64 * @smack: a pointer to the Smack label to use in the blob
66 * Returns the new blob or NULL if there's no memory available
68 struct inode_smack
*new_inode_smack(char *smack
)
70 struct inode_smack
*isp
;
72 isp
= kzalloc(sizeof(struct inode_smack
), GFP_KERNEL
);
76 isp
->smk_inode
= smack
;
78 mutex_init(&isp
->smk_lock
);
89 * smack_ptrace - Smack approval on ptrace
90 * @ptp: parent task pointer
91 * @ctp: child task pointer
93 * Returns 0 if access is OK, an error code otherwise
95 * Do the capability checks, and require read and write.
97 static int smack_ptrace(struct task_struct
*ptp
, struct task_struct
*ctp
)
101 rc
= cap_ptrace(ptp
, ctp
);
105 rc
= smk_access(ptp
->security
, ctp
->security
, MAY_READWRITE
);
106 if (rc
!= 0 && __capable(ptp
, CAP_MAC_OVERRIDE
))
113 * smack_syslog - Smack approval on syslog
114 * @type: message type
116 * Require that the task has the floor label
118 * Returns 0 on success, error code otherwise.
120 static int smack_syslog(int type
)
123 char *sp
= current
->security
;
125 rc
= cap_syslog(type
);
129 if (capable(CAP_MAC_OVERRIDE
))
132 if (sp
!= smack_known_floor
.smk_known
)
144 * smack_sb_alloc_security - allocate a superblock blob
145 * @sb: the superblock getting the blob
147 * Returns 0 on success or -ENOMEM on error.
149 static int smack_sb_alloc_security(struct super_block
*sb
)
151 struct superblock_smack
*sbsp
;
153 sbsp
= kzalloc(sizeof(struct superblock_smack
), GFP_KERNEL
);
158 sbsp
->smk_root
= smack_known_floor
.smk_known
;
159 sbsp
->smk_default
= smack_known_floor
.smk_known
;
160 sbsp
->smk_floor
= smack_known_floor
.smk_known
;
161 sbsp
->smk_hat
= smack_known_hat
.smk_known
;
162 sbsp
->smk_initialized
= 0;
163 spin_lock_init(&sbsp
->smk_sblock
);
165 sb
->s_security
= sbsp
;
171 * smack_sb_free_security - free a superblock blob
172 * @sb: the superblock getting the blob
175 static void smack_sb_free_security(struct super_block
*sb
)
177 kfree(sb
->s_security
);
178 sb
->s_security
= NULL
;
182 * smack_sb_copy_data - copy mount options data for processing
183 * @type: file system type
184 * @orig: where to start
187 * Returns 0 on success or -ENOMEM on error.
189 * Copy the Smack specific mount options out of the mount
192 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
193 static int smack_sb_copy_data(struct file_system_type
*type
, void *orig
,
196 static int smack_sb_copy_data(char *orig
, char *smackopts
)
197 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
199 char *cp
, *commap
, *otheropts
, *dp
;
201 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
202 /* Binary mount data: just copy */
203 if (type
->fs_flags
& FS_BINARY_MOUNTDATA
) {
204 copy_page(smackopts
, orig
);
209 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
210 otheropts
= (char *)get_zeroed_page(GFP_KERNEL
);
211 if (otheropts
== NULL
)
214 for (cp
= orig
, commap
= orig
; commap
!= NULL
; cp
= commap
+ 1) {
215 if (strstr(cp
, SMK_FSDEFAULT
) == cp
)
217 else if (strstr(cp
, SMK_FSFLOOR
) == cp
)
219 else if (strstr(cp
, SMK_FSHAT
) == cp
)
221 else if (strstr(cp
, SMK_FSROOT
) == cp
)
226 commap
= strchr(cp
, ',');
235 strcpy(orig
, otheropts
);
236 free_page((unsigned long)otheropts
);
242 * smack_sb_kern_mount - Smack specific mount processing
243 * @sb: the file system superblock
244 * @data: the smack mount options
246 * Returns 0 on success, an error code on failure
248 static int smack_sb_kern_mount(struct super_block
*sb
, void *data
)
250 struct dentry
*root
= sb
->s_root
;
251 struct inode
*inode
= root
->d_inode
;
252 struct superblock_smack
*sp
= sb
->s_security
;
253 struct inode_smack
*isp
;
258 spin_lock(&sp
->smk_sblock
);
259 if (sp
->smk_initialized
!= 0) {
260 spin_unlock(&sp
->smk_sblock
);
263 sp
->smk_initialized
= 1;
264 spin_unlock(&sp
->smk_sblock
);
266 for (op
= data
; op
!= NULL
; op
= commap
) {
267 commap
= strchr(op
, ',');
271 if (strncmp(op
, SMK_FSHAT
, strlen(SMK_FSHAT
)) == 0) {
272 op
+= strlen(SMK_FSHAT
);
273 nsp
= smk_import(op
, 0);
276 } else if (strncmp(op
, SMK_FSFLOOR
, strlen(SMK_FSFLOOR
)) == 0) {
277 op
+= strlen(SMK_FSFLOOR
);
278 nsp
= smk_import(op
, 0);
281 } else if (strncmp(op
, SMK_FSDEFAULT
,
282 strlen(SMK_FSDEFAULT
)) == 0) {
283 op
+= strlen(SMK_FSDEFAULT
);
284 nsp
= smk_import(op
, 0);
286 sp
->smk_default
= nsp
;
287 } else if (strncmp(op
, SMK_FSROOT
, strlen(SMK_FSROOT
)) == 0) {
288 op
+= strlen(SMK_FSROOT
);
289 nsp
= smk_import(op
, 0);
296 * Initialize the root inode.
298 isp
= inode
->i_security
;
300 inode
->i_security
= new_inode_smack(sp
->smk_root
);
302 isp
->smk_inode
= sp
->smk_root
;
308 * smack_sb_statfs - Smack check on statfs
309 * @dentry: identifies the file system in question
311 * Returns 0 if current can read the floor of the filesystem,
312 * and error code otherwise
314 static int smack_sb_statfs(struct dentry
*dentry
)
316 struct superblock_smack
*sbp
= dentry
->d_sb
->s_security
;
318 return smk_curacc(sbp
->smk_floor
, MAY_READ
);
322 * smack_sb_mount - Smack check for mounting
329 * Returns 0 if current can write the floor of the filesystem
330 * being mounted on, an error code otherwise.
332 static int smack_sb_mount(char *dev_name
, struct nameidata
*nd
,
333 char *type
, unsigned long flags
, void *data
)
335 struct superblock_smack
*sbp
= nd
->path
.mnt
->mnt_sb
->s_security
;
337 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
);
341 * smack_sb_umount - Smack check for unmounting
342 * @mnt: file system to unmount
345 * Returns 0 if current can write the floor of the filesystem
346 * being unmounted, an error code otherwise.
348 static int smack_sb_umount(struct vfsmount
*mnt
, int flags
)
350 struct superblock_smack
*sbp
;
352 sbp
= mnt
->mnt_sb
->s_security
;
354 return smk_curacc(sbp
->smk_floor
, MAY_WRITE
);
362 * smack_inode_alloc_security - allocate an inode blob
363 * @inode - the inode in need of a blob
365 * Returns 0 if it gets a blob, -ENOMEM otherwise
367 static int smack_inode_alloc_security(struct inode
*inode
)
369 inode
->i_security
= new_inode_smack(current
->security
);
370 if (inode
->i_security
== NULL
)
376 * smack_inode_free_security - free an inode blob
377 * @inode - the inode with a blob
379 * Clears the blob pointer in inode
381 static void smack_inode_free_security(struct inode
*inode
)
383 kfree(inode
->i_security
);
384 inode
->i_security
= NULL
;
388 * smack_inode_init_security - copy out the smack from an inode
391 * @name: where to put the attribute name
392 * @value: where to put the attribute value
393 * @len: where to put the length of the attribute
395 * Returns 0 if it all works out, -ENOMEM if there's no memory
397 static int smack_inode_init_security(struct inode
*inode
, struct inode
*dir
,
398 char **name
, void **value
, size_t *len
)
400 char *isp
= smk_of_inode(inode
);
403 *name
= kstrdup(XATTR_SMACK_SUFFIX
, GFP_KERNEL
);
409 *value
= kstrdup(isp
, GFP_KERNEL
);
415 *len
= strlen(isp
) + 1;
421 * smack_inode_link - Smack check on link
422 * @old_dentry: the existing object
424 * @new_dentry: the new object
426 * Returns 0 if access is permitted, an error code otherwise
428 static int smack_inode_link(struct dentry
*old_dentry
, struct inode
*dir
,
429 struct dentry
*new_dentry
)
434 isp
= smk_of_inode(old_dentry
->d_inode
);
435 rc
= smk_curacc(isp
, MAY_WRITE
);
437 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
438 isp
= smk_of_inode(new_dentry
->d_inode
);
439 rc
= smk_curacc(isp
, MAY_WRITE
);
446 * smack_inode_unlink - Smack check on inode deletion
447 * @dir: containing directory object
448 * @dentry: file to unlink
450 * Returns 0 if current can write the containing directory
451 * and the object, error code otherwise
453 static int smack_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
455 struct inode
*ip
= dentry
->d_inode
;
459 * You need write access to the thing you're unlinking
461 rc
= smk_curacc(smk_of_inode(ip
), MAY_WRITE
);
464 * You also need write access to the containing directory
466 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
);
472 * smack_inode_rmdir - Smack check on directory deletion
473 * @dir: containing directory object
474 * @dentry: directory to unlink
476 * Returns 0 if current can write the containing directory
477 * and the directory, error code otherwise
479 static int smack_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
484 * You need write access to the thing you're removing
486 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
489 * You also need write access to the containing directory
491 rc
= smk_curacc(smk_of_inode(dir
), MAY_WRITE
);
497 * smack_inode_rename - Smack check on rename
498 * @old_inode: the old directory
499 * @old_dentry: unused
500 * @new_inode: the new directory
501 * @new_dentry: unused
503 * Read and write access is required on both the old and
506 * Returns 0 if access is permitted, an error code otherwise
508 static int smack_inode_rename(struct inode
*old_inode
,
509 struct dentry
*old_dentry
,
510 struct inode
*new_inode
,
511 struct dentry
*new_dentry
)
516 isp
= smk_of_inode(old_dentry
->d_inode
);
517 rc
= smk_curacc(isp
, MAY_READWRITE
);
519 if (rc
== 0 && new_dentry
->d_inode
!= NULL
) {
520 isp
= smk_of_inode(new_dentry
->d_inode
);
521 rc
= smk_curacc(isp
, MAY_READWRITE
);
528 * smack_inode_permission - Smack version of permission()
529 * @inode: the inode in question
530 * @mask: the access requested
533 * This is the important Smack hook.
535 * Returns 0 if access is permitted, -EACCES otherwise
537 static int smack_inode_permission(struct inode
*inode
, int mask
,
538 struct nameidata
*nd
)
541 * No permission to check. Existence test. Yup, it's there.
546 return smk_curacc(smk_of_inode(inode
), mask
);
550 * smack_inode_setattr - Smack check for setting attributes
551 * @dentry: the object
552 * @iattr: for the force flag
554 * Returns 0 if access is permitted, an error code otherwise
556 static int smack_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
559 * Need to allow for clearing the setuid bit.
561 if (iattr
->ia_valid
& ATTR_FORCE
)
564 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
568 * smack_inode_getattr - Smack check for getting attributes
570 * @dentry: the object
572 * Returns 0 if access is permitted, an error code otherwise
574 static int smack_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
576 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
);
580 * smack_inode_setxattr - Smack check for setting xattrs
581 * @dentry: the object
582 * @name: name of the attribute
587 * This protects the Smack attribute explicitly.
589 * Returns 0 if access is permitted, an error code otherwise
591 static int smack_inode_setxattr(struct dentry
*dentry
, char *name
,
592 void *value
, size_t size
, int flags
)
594 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
595 if (!capable(CAP_MAC_ADMIN
)) {
596 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
597 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
598 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0)
603 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
605 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
606 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
608 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
609 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
610 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
611 if (!capable(CAP_MAC_ADMIN
))
614 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
617 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
620 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
624 * smack_inode_post_setxattr - Apply the Smack update approved above
626 * @name: attribute name
627 * @value: attribute value
628 * @size: attribute size
631 * Set the pointer in the inode blob to the entry found
632 * in the master label list.
634 static void smack_inode_post_setxattr(struct dentry
*dentry
, char *name
,
635 void *value
, size_t size
, int flags
)
637 struct inode_smack
*isp
;
643 if (strcmp(name
, XATTR_NAME_SMACK
))
646 if (size
>= SMK_LABELLEN
)
649 isp
= dentry
->d_inode
->i_security
;
652 * No locking is done here. This is a pointer
655 nsp
= smk_import(value
, size
);
657 isp
->smk_inode
= nsp
;
659 isp
->smk_inode
= smack_known_invalid
.smk_known
;
665 * smack_inode_getxattr - Smack check on getxattr
666 * @dentry: the object
669 * Returns 0 if access is permitted, an error code otherwise
671 static int smack_inode_getxattr(struct dentry
*dentry
, char *name
)
673 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_READ
);
677 * smack_inode_removexattr - Smack check on removexattr
678 * @dentry: the object
679 * @name: name of the attribute
681 * Removing the Smack attribute requires CAP_MAC_ADMIN
683 * Returns 0 if access is permitted, an error code otherwise
685 static int smack_inode_removexattr(struct dentry
*dentry
, char *name
)
687 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
688 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 && !capable(CAP_MAC_ADMIN
))
692 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
694 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
695 return smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
697 if (strcmp(name
, XATTR_NAME_SMACK
) == 0 ||
698 strcmp(name
, XATTR_NAME_SMACKIPIN
) == 0 ||
699 strcmp(name
, XATTR_NAME_SMACKIPOUT
) == 0) {
700 if (!capable(CAP_MAC_ADMIN
))
703 rc
= cap_inode_removexattr(dentry
, name
);
706 rc
= smk_curacc(smk_of_inode(dentry
->d_inode
), MAY_WRITE
);
709 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
713 * smack_inode_getsecurity - get smack xattrs
715 * @name: attribute name
716 * @buffer: where to put the result
717 * @size: size of the buffer
720 * Returns the size of the attribute or an error code
722 static int smack_inode_getsecurity(const struct inode
*inode
,
723 const char *name
, void **buffer
,
726 struct socket_smack
*ssp
;
728 struct super_block
*sbp
;
729 struct inode
*ip
= (struct inode
*)inode
;
734 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
735 isp
= smk_of_inode(inode
);
736 ilen
= strlen(isp
) + 1;
742 * The rest of the Smack xattrs are only on sockets.
745 if (sbp
->s_magic
!= SOCKFS_MAGIC
)
749 if (sock
== NULL
|| sock
->sk
== NULL
)
752 ssp
= sock
->sk
->sk_security
;
754 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
756 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0)
761 ilen
= strlen(isp
) + 1;
772 * smack_inode_listsecurity - list the Smack attributes
774 * @buffer: where they go
775 * @buffer_size: size of buffer
777 * Returns 0 on success, -EINVAL otherwise
779 static int smack_inode_listsecurity(struct inode
*inode
, char *buffer
,
782 int len
= strlen(XATTR_NAME_SMACK
);
784 if (buffer
!= NULL
&& len
<= buffer_size
) {
785 memcpy(buffer
, XATTR_NAME_SMACK
, len
);
796 * smack_file_permission - Smack check on file operations
802 * Should access checks be done on each read or write?
803 * UNICOS and SELinux say yes.
804 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
806 * I'll say no for now. Smack does not do the frequent
807 * label changing that SELinux does.
809 static int smack_file_permission(struct file
*file
, int mask
)
815 * smack_file_alloc_security - assign a file security blob
818 * The security blob for a file is a pointer to the master
819 * label list, so no allocation is done.
823 static int smack_file_alloc_security(struct file
*file
)
825 file
->f_security
= current
->security
;
830 * smack_file_free_security - clear a file security blob
833 * The security blob for a file is a pointer to the master
834 * label list, so no memory is freed.
836 static void smack_file_free_security(struct file
*file
)
838 file
->f_security
= NULL
;
842 * smack_file_ioctl - Smack check on ioctls
847 * Relies heavily on the correct use of the ioctl command conventions.
849 * Returns 0 if allowed, error code otherwise
851 static int smack_file_ioctl(struct file
*file
, unsigned int cmd
,
856 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
857 rc
= smk_curacc(file
->f_security
, MAY_WRITE
);
859 if (rc
== 0 && (_IOC_DIR(cmd
) & _IOC_READ
))
860 rc
= smk_curacc(file
->f_security
, MAY_READ
);
866 * smack_file_lock - Smack check on file locking
870 * Returns 0 if current has write access, error code otherwise
872 static int smack_file_lock(struct file
*file
, unsigned int cmd
)
874 return smk_curacc(file
->f_security
, MAY_WRITE
);
878 * smack_file_fcntl - Smack check on fcntl
880 * @cmd: what action to check
883 * Returns 0 if current has access, error code otherwise
885 static int smack_file_fcntl(struct file
*file
, unsigned int cmd
,
897 rc
= smk_curacc(file
->f_security
, MAY_READ
);
905 rc
= smk_curacc(file
->f_security
, MAY_WRITE
);
908 rc
= smk_curacc(file
->f_security
, MAY_READWRITE
);
915 * smack_file_set_fowner - set the file security blob value
916 * @file: object in question
919 * Further research may be required on this one.
921 static int smack_file_set_fowner(struct file
*file
)
923 file
->f_security
= current
->security
;
928 * smack_file_send_sigiotask - Smack on sigio
929 * @tsk: The target task
930 * @fown: the object the signal come from
933 * Allow a privileged task to get signals even if it shouldn't
935 * Returns 0 if a subject with the object's smack could
936 * write to the task, an error code otherwise.
938 static int smack_file_send_sigiotask(struct task_struct
*tsk
,
939 struct fown_struct
*fown
, int signum
)
945 * struct fown_struct is never outside the context of a struct file
947 file
= container_of(fown
, struct file
, f_owner
);
948 rc
= smk_access(file
->f_security
, tsk
->security
, MAY_WRITE
);
949 if (rc
!= 0 && __capable(tsk
, CAP_MAC_OVERRIDE
))
955 * smack_file_receive - Smack file receive check
958 * Returns 0 if current has access, error code otherwise
960 static int smack_file_receive(struct file
*file
)
965 * This code relies on bitmasks.
967 if (file
->f_mode
& FMODE_READ
)
969 if (file
->f_mode
& FMODE_WRITE
)
972 return smk_curacc(file
->f_security
, may
);
980 * smack_task_alloc_security - "allocate" a task blob
981 * @tsk: the task in need of a blob
983 * Smack isn't using copies of blobs. Everyone
984 * points to an immutable list. No alloc required.
985 * No data copy required.
989 static int smack_task_alloc_security(struct task_struct
*tsk
)
991 tsk
->security
= current
->security
;
997 * smack_task_free_security - "free" a task blob
998 * @task: the task with the blob
1000 * Smack isn't using copies of blobs. Everyone
1001 * points to an immutable list. The blobs never go away.
1002 * There is no leak here.
1004 static void smack_task_free_security(struct task_struct
*task
)
1006 task
->security
= NULL
;
1010 * smack_task_setpgid - Smack check on setting pgid
1011 * @p: the task object
1014 * Return 0 if write access is permitted
1016 static int smack_task_setpgid(struct task_struct
*p
, pid_t pgid
)
1018 return smk_curacc(p
->security
, MAY_WRITE
);
1022 * smack_task_getpgid - Smack access check for getpgid
1023 * @p: the object task
1025 * Returns 0 if current can read the object task, error code otherwise
1027 static int smack_task_getpgid(struct task_struct
*p
)
1029 return smk_curacc(p
->security
, MAY_READ
);
1033 * smack_task_getsid - Smack access check for getsid
1034 * @p: the object task
1036 * Returns 0 if current can read the object task, error code otherwise
1038 static int smack_task_getsid(struct task_struct
*p
)
1040 return smk_curacc(p
->security
, MAY_READ
);
1044 * smack_task_getsecid - get the secid of the task
1045 * @p: the object task
1046 * @secid: where to put the result
1048 * Sets the secid to contain a u32 version of the smack label.
1050 static void smack_task_getsecid(struct task_struct
*p
, u32
*secid
)
1052 *secid
= smack_to_secid(p
->security
);
1056 * smack_task_setnice - Smack check on setting nice
1057 * @p: the task object
1060 * Return 0 if write access is permitted
1062 static int smack_task_setnice(struct task_struct
*p
, int nice
)
1064 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1065 return smk_curacc(p
->security
, MAY_WRITE
);
1069 rc
= cap_task_setnice(p
, nice
);
1071 rc
= smk_curacc(p
->security
, MAY_WRITE
);
1073 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1077 * smack_task_setioprio - Smack check on setting ioprio
1078 * @p: the task object
1081 * Return 0 if write access is permitted
1083 static int smack_task_setioprio(struct task_struct
*p
, int ioprio
)
1085 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1086 return smk_curacc(p
->security
, MAY_WRITE
);
1090 rc
= cap_task_setioprio(p
, ioprio
);
1092 rc
= smk_curacc(p
->security
, MAY_WRITE
);
1094 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1098 * smack_task_getioprio - Smack check on reading ioprio
1099 * @p: the task object
1101 * Return 0 if read access is permitted
1103 static int smack_task_getioprio(struct task_struct
*p
)
1105 return smk_curacc(p
->security
, MAY_READ
);
1109 * smack_task_setscheduler - Smack check on setting scheduler
1110 * @p: the task object
1114 * Return 0 if read access is permitted
1116 static int smack_task_setscheduler(struct task_struct
*p
, int policy
,
1117 struct sched_param
*lp
)
1119 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1120 return smk_curacc(p
->security
, MAY_WRITE
);
1124 rc
= cap_task_setscheduler(p
, policy
, lp
);
1126 rc
= smk_curacc(p
->security
, MAY_WRITE
);
1128 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1132 * smack_task_getscheduler - Smack check on reading scheduler
1133 * @p: the task object
1135 * Return 0 if read access is permitted
1137 static int smack_task_getscheduler(struct task_struct
*p
)
1139 return smk_curacc(p
->security
, MAY_READ
);
1143 * smack_task_movememory - Smack check on moving memory
1144 * @p: the task object
1146 * Return 0 if write access is permitted
1148 static int smack_task_movememory(struct task_struct
*p
)
1150 return smk_curacc(p
->security
, MAY_WRITE
);
1154 * smack_task_kill - Smack check on signal delivery
1155 * @p: the task object
1158 * @secid: identifies the smack to use in lieu of current's
1160 * Return 0 if write access is permitted
1162 * The secid behavior is an artifact of an SELinux hack
1163 * in the USB code. Someday it may go away.
1165 static int smack_task_kill(struct task_struct
*p
, struct siginfo
*info
,
1168 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1172 rc
= cap_task_kill(p
, info
, sig
, secid
);
1175 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1177 * Special cases where signals really ought to go through
1178 * in spite of policy. Stephen Smalley suggests it may
1179 * make sense to change the caller so that it doesn't
1180 * bother with the LSM hook in these cases.
1182 if (info
!= SEND_SIG_NOINFO
&&
1183 (is_si_special(info
) || SI_FROMKERNEL(info
)))
1186 * Sending a signal requires that the sender
1187 * can write the receiver.
1190 return smk_curacc(p
->security
, MAY_WRITE
);
1192 * If the secid isn't 0 we're dealing with some USB IO
1193 * specific behavior. This is not clean. For one thing
1194 * we can't take privilege into account.
1196 return smk_access(smack_from_secid(secid
), p
->security
, MAY_WRITE
);
1200 * smack_task_wait - Smack access check for waiting
1201 * @p: task to wait for
1203 * Returns 0 if current can wait for p, error code otherwise
1205 static int smack_task_wait(struct task_struct
*p
)
1209 rc
= smk_access(current
->security
, p
->security
, MAY_WRITE
);
1214 * Allow the operation to succeed if either task
1215 * has privilege to perform operations that might
1216 * account for the smack labels having gotten to
1217 * be different in the first place.
1219 * This breaks the strict subjet/object access
1220 * control ideal, taking the object's privilege
1221 * state into account in the decision as well as
1224 if (capable(CAP_MAC_OVERRIDE
) || __capable(p
, CAP_MAC_OVERRIDE
))
1231 * smack_task_to_inode - copy task smack into the inode blob
1232 * @p: task to copy from
1233 * inode: inode to copy to
1235 * Sets the smack pointer in the inode security blob
1237 static void smack_task_to_inode(struct task_struct
*p
, struct inode
*inode
)
1239 struct inode_smack
*isp
= inode
->i_security
;
1240 isp
->smk_inode
= p
->security
;
1248 * smack_sk_alloc_security - Allocate a socket blob
1251 * @priority: memory allocation priority
1253 * Assign Smack pointers to current
1255 * Returns 0 on success, -ENOMEM is there's no memory
1257 static int smack_sk_alloc_security(struct sock
*sk
, int family
, gfp_t gfp_flags
)
1259 char *csp
= current
->security
;
1260 struct socket_smack
*ssp
;
1262 ssp
= kzalloc(sizeof(struct socket_smack
), gfp_flags
);
1268 ssp
->smk_packet
[0] = '\0';
1270 sk
->sk_security
= ssp
;
1276 * smack_sk_free_security - Free a socket blob
1279 * Clears the blob pointer
1281 static void smack_sk_free_security(struct sock
*sk
)
1283 kfree(sk
->sk_security
);
1287 * smack_set_catset - convert a capset to netlabel mls categories
1288 * @catset: the Smack categories
1289 * @sap: where to put the netlabel categories
1291 * Allocates and fills attr.mls.cat
1293 static void smack_set_catset(char *catset
, struct netlbl_lsm_secattr
*sap
)
1304 sap
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1305 sap
->attr
.mls
.cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1306 sap
->attr
.mls
.cat
->startbit
= 0;
1308 for (cat
= 1, cp
= catset
, byte
= 0; byte
< SMK_LABELLEN
; cp
++, byte
++)
1309 for (m
= 0x80; m
!= 0; m
>>= 1, cat
++) {
1312 rc
= netlbl_secattr_catmap_setbit(sap
->attr
.mls
.cat
,
1318 * smack_to_secattr - fill a secattr from a smack value
1319 * @smack: the smack value
1320 * @nlsp: where the result goes
1322 * Casey says that CIPSO is good enough for now.
1323 * It can be used to effect.
1324 * It can also be abused to effect when necessary.
1325 * Appologies to the TSIG group in general and GW in particular.
1327 static void smack_to_secattr(char *smack
, struct netlbl_lsm_secattr
*nlsp
)
1329 struct smack_cipso cipso
;
1332 switch (smack_net_nltype
) {
1333 case NETLBL_NLTYPE_CIPSOV4
:
1334 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1335 nlsp
->domain
= NULL
;
1336 nlsp
->flags
= NETLBL_SECATTR_DOMAIN
;
1337 nlsp
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1339 nlsp
->domain
= kstrdup(smack
, GFP_ATOMIC
);
1340 nlsp
->flags
= NETLBL_SECATTR_DOMAIN
| NETLBL_SECATTR_MLS_LVL
;
1341 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1343 rc
= smack_to_cipso(smack
, &cipso
);
1345 nlsp
->attr
.mls
.lvl
= cipso
.smk_level
;
1346 smack_set_catset(cipso
.smk_catset
, nlsp
);
1348 nlsp
->attr
.mls
.lvl
= smack_cipso_direct
;
1349 smack_set_catset(smack
, nlsp
);
1358 * smack_netlabel - Set the secattr on a socket
1361 * Convert the outbound smack value (smk_out) to a
1362 * secattr and attach it to the socket.
1364 * Returns 0 on success or an error code
1366 static int smack_netlabel(struct sock
*sk
)
1368 struct socket_smack
*ssp
;
1369 struct netlbl_lsm_secattr secattr
;
1370 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1374 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1376 ssp
= sk
->sk_security
;
1377 netlbl_secattr_init(&secattr
);
1378 smack_to_secattr(ssp
->smk_out
, &secattr
);
1379 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1380 if (secattr
.flags
!= NETLBL_SECATTR_NONE
)
1381 rc
= netlbl_sock_setattr(sk
, &secattr
);
1384 rc
= netlbl_sock_setattr(sk
, &secattr
);
1385 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1386 netlbl_secattr_destroy(&secattr
);
1387 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1390 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1395 * smack_inode_setsecurity - set smack xattrs
1396 * @inode: the object
1397 * @name: attribute name
1398 * @value: attribute value
1399 * @size: size of the attribute
1402 * Sets the named attribute in the appropriate blob
1404 * Returns 0 on success, or an error code
1406 static int smack_inode_setsecurity(struct inode
*inode
, const char *name
,
1407 const void *value
, size_t size
, int flags
)
1410 struct inode_smack
*nsp
= inode
->i_security
;
1411 struct socket_smack
*ssp
;
1412 struct socket
*sock
;
1413 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1416 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1418 if (value
== NULL
|| size
> SMK_LABELLEN
)
1421 sp
= smk_import(value
, size
);
1425 if (strcmp(name
, XATTR_SMACK_SUFFIX
) == 0) {
1426 nsp
->smk_inode
= sp
;
1430 * The rest of the Smack xattrs are only on sockets.
1432 if (inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
1435 sock
= SOCKET_I(inode
);
1436 if (sock
== NULL
|| sock
->sk
== NULL
)
1439 ssp
= sock
->sk
->sk_security
;
1441 if (strcmp(name
, XATTR_SMACK_IPIN
) == 0)
1443 else if (strcmp(name
, XATTR_SMACK_IPOUT
) == 0) {
1445 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1446 return smack_netlabel(sock
->sk
);
1448 rc
= smack_netlabel(sock
->sk
);
1450 printk(KERN_WARNING
"Smack: \"%s\" netlbl error %d.\n",
1452 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1460 * smack_socket_post_create - finish socket setup
1462 * @family: protocol family
1467 * Sets the netlabel information on the socket
1469 * Returns 0 on success, and error code otherwise
1471 static int smack_socket_post_create(struct socket
*sock
, int family
,
1472 int type
, int protocol
, int kern
)
1474 if (family
!= PF_INET
|| sock
->sk
== NULL
)
1477 * Set the outbound netlbl.
1479 return smack_netlabel(sock
->sk
);
1483 * smack_flags_to_may - convert S_ to MAY_ values
1484 * @flags: the S_ value
1486 * Returns the equivalent MAY_ value
1488 static int smack_flags_to_may(int flags
)
1492 if (flags
& S_IRUGO
)
1494 if (flags
& S_IWUGO
)
1496 if (flags
& S_IXUGO
)
1503 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1508 static int smack_msg_msg_alloc_security(struct msg_msg
*msg
)
1510 msg
->security
= current
->security
;
1515 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1518 * Clears the blob pointer
1520 static void smack_msg_msg_free_security(struct msg_msg
*msg
)
1522 msg
->security
= NULL
;
1526 * smack_of_shm - the smack pointer for the shm
1529 * Returns a pointer to the smack value
1531 static char *smack_of_shm(struct shmid_kernel
*shp
)
1533 return (char *)shp
->shm_perm
.security
;
1537 * smack_shm_alloc_security - Set the security blob for shm
1542 static int smack_shm_alloc_security(struct shmid_kernel
*shp
)
1544 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
1546 isp
->security
= current
->security
;
1551 * smack_shm_free_security - Clear the security blob for shm
1554 * Clears the blob pointer
1556 static void smack_shm_free_security(struct shmid_kernel
*shp
)
1558 struct kern_ipc_perm
*isp
= &shp
->shm_perm
;
1560 isp
->security
= NULL
;
1564 * smack_shm_associate - Smack access check for shm
1566 * @shmflg: access requested
1568 * Returns 0 if current has the requested access, error code otherwise
1570 static int smack_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
1572 char *ssp
= smack_of_shm(shp
);
1575 may
= smack_flags_to_may(shmflg
);
1576 return smk_curacc(ssp
, may
);
1580 * smack_shm_shmctl - Smack access check for shm
1582 * @cmd: what it wants to do
1584 * Returns 0 if current has the requested access, error code otherwise
1586 static int smack_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
1588 char *ssp
= smack_of_shm(shp
);
1600 may
= MAY_READWRITE
;
1605 * System level information.
1612 return smk_curacc(ssp
, may
);
1616 * smack_shm_shmat - Smack access for shmat
1619 * @shmflg: access requested
1621 * Returns 0 if current has the requested access, error code otherwise
1623 static int smack_shm_shmat(struct shmid_kernel
*shp
, char __user
*shmaddr
,
1626 char *ssp
= smack_of_shm(shp
);
1629 may
= smack_flags_to_may(shmflg
);
1630 return smk_curacc(ssp
, may
);
1634 * smack_of_sem - the smack pointer for the sem
1637 * Returns a pointer to the smack value
1639 static char *smack_of_sem(struct sem_array
*sma
)
1641 return (char *)sma
->sem_perm
.security
;
1645 * smack_sem_alloc_security - Set the security blob for sem
1650 static int smack_sem_alloc_security(struct sem_array
*sma
)
1652 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
1654 isp
->security
= current
->security
;
1659 * smack_sem_free_security - Clear the security blob for sem
1662 * Clears the blob pointer
1664 static void smack_sem_free_security(struct sem_array
*sma
)
1666 struct kern_ipc_perm
*isp
= &sma
->sem_perm
;
1668 isp
->security
= NULL
;
1672 * smack_sem_associate - Smack access check for sem
1674 * @semflg: access requested
1676 * Returns 0 if current has the requested access, error code otherwise
1678 static int smack_sem_associate(struct sem_array
*sma
, int semflg
)
1680 char *ssp
= smack_of_sem(sma
);
1683 may
= smack_flags_to_may(semflg
);
1684 return smk_curacc(ssp
, may
);
1688 * smack_sem_shmctl - Smack access check for sem
1690 * @cmd: what it wants to do
1692 * Returns 0 if current has the requested access, error code otherwise
1694 static int smack_sem_semctl(struct sem_array
*sma
, int cmd
)
1696 char *ssp
= smack_of_sem(sma
);
1713 may
= MAY_READWRITE
;
1718 * System level information
1725 return smk_curacc(ssp
, may
);
1729 * smack_sem_semop - Smack checks of semaphore operations
1735 * Treated as read and write in all cases.
1737 * Returns 0 if access is allowed, error code otherwise
1739 static int smack_sem_semop(struct sem_array
*sma
, struct sembuf
*sops
,
1740 unsigned nsops
, int alter
)
1742 char *ssp
= smack_of_sem(sma
);
1744 return smk_curacc(ssp
, MAY_READWRITE
);
1748 * smack_msg_alloc_security - Set the security blob for msg
1753 static int smack_msg_queue_alloc_security(struct msg_queue
*msq
)
1755 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
1757 kisp
->security
= current
->security
;
1762 * smack_msg_free_security - Clear the security blob for msg
1765 * Clears the blob pointer
1767 static void smack_msg_queue_free_security(struct msg_queue
*msq
)
1769 struct kern_ipc_perm
*kisp
= &msq
->q_perm
;
1771 kisp
->security
= NULL
;
1775 * smack_of_msq - the smack pointer for the msq
1778 * Returns a pointer to the smack value
1780 static char *smack_of_msq(struct msg_queue
*msq
)
1782 return (char *)msq
->q_perm
.security
;
1786 * smack_msg_queue_associate - Smack access check for msg_queue
1788 * @msqflg: access requested
1790 * Returns 0 if current has the requested access, error code otherwise
1792 static int smack_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
1794 char *msp
= smack_of_msq(msq
);
1797 may
= smack_flags_to_may(msqflg
);
1798 return smk_curacc(msp
, may
);
1802 * smack_msg_queue_msgctl - Smack access check for msg_queue
1804 * @cmd: what it wants to do
1806 * Returns 0 if current has the requested access, error code otherwise
1808 static int smack_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
1810 char *msp
= smack_of_msq(msq
);
1820 may
= MAY_READWRITE
;
1825 * System level information
1832 return smk_curacc(msp
, may
);
1836 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1839 * @msqflg: access requested
1841 * Returns 0 if current has the requested access, error code otherwise
1843 static int smack_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
,
1846 char *msp
= smack_of_msq(msq
);
1849 rc
= smack_flags_to_may(msqflg
);
1850 return smk_curacc(msp
, rc
);
1854 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1861 * Returns 0 if current has read and write access, error code otherwise
1863 static int smack_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
1864 struct task_struct
*target
, long type
, int mode
)
1866 char *msp
= smack_of_msq(msq
);
1868 return smk_curacc(msp
, MAY_READWRITE
);
1872 * smack_ipc_permission - Smack access for ipc_permission()
1873 * @ipp: the object permissions
1874 * @flag: access requested
1876 * Returns 0 if current has read and write access, error code otherwise
1878 static int smack_ipc_permission(struct kern_ipc_perm
*ipp
, short flag
)
1880 char *isp
= ipp
->security
;
1883 may
= smack_flags_to_may(flag
);
1884 return smk_curacc(isp
, may
);
1887 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
1889 /* module stacking operations */
1892 * smack_register_security - stack capability module
1893 * @name: module name
1894 * @ops: module operations - ignored
1896 * Allow the capability module to register.
1898 static int smack_register_security(const char *name
,
1899 struct security_operations
*ops
)
1901 if (strcmp(name
, "capability") != 0)
1904 printk(KERN_INFO
"%s: Registering secondary module %s\n",
1910 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
1912 * smack_d_instantiate - Make sure the blob is correct on an inode
1913 * @opt_dentry: unused
1914 * @inode: the object
1916 * Set the inode's security blob if it hasn't been done already.
1918 static void smack_d_instantiate(struct dentry
*opt_dentry
, struct inode
*inode
)
1920 struct super_block
*sbp
;
1921 struct superblock_smack
*sbsp
;
1922 struct inode_smack
*isp
;
1923 char *csp
= current
->security
;
1931 isp
= inode
->i_security
;
1933 mutex_lock(&isp
->smk_lock
);
1935 * If the inode is already instantiated
1936 * take the quick way out
1938 if (isp
->smk_flags
& SMK_INODE_INSTANT
)
1942 sbsp
= sbp
->s_security
;
1944 * We're going to use the superblock default label
1945 * if there's no label on the file.
1947 final
= sbsp
->smk_default
;
1950 * This is pretty hackish.
1951 * Casey says that we shouldn't have to do
1952 * file system specific code, but it does help
1953 * with keeping it simple.
1955 switch (sbp
->s_magic
) {
1958 * Casey says that it's a little embarassing
1959 * that the smack file system doesn't do
1960 * extended attributes.
1962 final
= smack_known_star
.smk_known
;
1966 * Casey says pipes are easy (?)
1968 final
= smack_known_star
.smk_known
;
1970 case DEVPTS_SUPER_MAGIC
:
1972 * devpts seems content with the label of the task.
1973 * Programs that change smack have to treat the
1980 * Casey says sockets get the smack of the task.
1984 case PROC_SUPER_MAGIC
:
1986 * Casey says procfs appears not to care.
1987 * The superblock default suffices.
1992 * Device labels should come from the filesystem,
1993 * but watch out, because they're volitile,
1994 * getting recreated on every reboot.
1996 final
= smack_known_star
.smk_known
;
2000 * If a smack value has been set we want to use it,
2001 * but since tmpfs isn't giving us the opportunity
2002 * to set mount options simulate setting the
2003 * superblock default.
2007 * This isn't an understood special case.
2008 * Get the value from the xattr.
2010 * No xattr support means, alas, no SMACK label.
2011 * Use the aforeapplied default.
2012 * It would be curious if the label of the task
2013 * does not match that assigned.
2015 if (inode
->i_op
->getxattr
== NULL
)
2018 * Get the dentry for xattr.
2020 if (opt_dentry
== NULL
) {
2021 dp
= d_find_alias(inode
);
2025 dp
= dget(opt_dentry
);
2030 fetched
= smk_fetch(inode
, dp
);
2031 if (fetched
!= NULL
)
2039 isp
->smk_inode
= csp
;
2041 isp
->smk_inode
= final
;
2043 isp
->smk_flags
|= SMK_INODE_INSTANT
;
2046 mutex_unlock(&isp
->smk_lock
);
2051 * smack_getprocattr - Smack process attribute access
2052 * @p: the object task
2053 * @name: the name of the attribute in /proc/.../attr
2054 * @value: where to put the result
2056 * Places a copy of the task Smack into value
2058 * Returns the length of the smack label or an error code
2060 static int smack_getprocattr(struct task_struct
*p
, char *name
, char **value
)
2065 if (strcmp(name
, "current") != 0)
2068 cp
= kstrdup(p
->security
, GFP_KERNEL
);
2078 * smack_setprocattr - Smack process attribute setting
2079 * @p: the object task
2080 * @name: the name of the attribute in /proc/.../attr
2081 * @value: the value to set
2082 * @size: the size of the value
2084 * Sets the Smack value of the task. Only setting self
2085 * is permitted and only with privilege
2087 * Returns the length of the smack label or an error code
2089 static int smack_setprocattr(struct task_struct
*p
, char *name
,
2090 void *value
, size_t size
)
2094 if (!__capable(p
, CAP_MAC_ADMIN
))
2098 * Changing another process' Smack value is too dangerous
2099 * and supports no sane use case.
2104 if (value
== NULL
|| size
== 0 || size
>= SMK_LABELLEN
)
2107 if (strcmp(name
, "current") != 0)
2110 newsmack
= smk_import(value
, size
);
2111 if (newsmack
== NULL
)
2114 p
->security
= newsmack
;
2119 * smack_unix_stream_connect - Smack access on UDS
2121 * @other: the other socket
2124 * Return 0 if a subject with the smack of sock could access
2125 * an object with the smack of other, otherwise an error code
2127 static int smack_unix_stream_connect(struct socket
*sock
,
2128 struct socket
*other
, struct sock
*newsk
)
2130 struct inode
*sp
= SOCK_INODE(sock
);
2131 struct inode
*op
= SOCK_INODE(other
);
2133 return smk_access(smk_of_inode(sp
), smk_of_inode(op
), MAY_READWRITE
);
2137 * smack_unix_may_send - Smack access on UDS
2139 * @other: the other socket
2141 * Return 0 if a subject with the smack of sock could access
2142 * an object with the smack of other, otherwise an error code
2144 static int smack_unix_may_send(struct socket
*sock
, struct socket
*other
)
2146 struct inode
*sp
= SOCK_INODE(sock
);
2147 struct inode
*op
= SOCK_INODE(other
);
2149 return smk_access(smk_of_inode(sp
), smk_of_inode(op
), MAY_WRITE
);
2153 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2155 * @sap: netlabel secattr
2156 * @sip: where to put the result
2158 * Copies a smack label into sip
2160 static void smack_from_secattr(struct netlbl_lsm_secattr
*sap
, char *sip
)
2162 char smack
[SMK_LABELLEN
];
2165 if ((sap
->flags
& NETLBL_SECATTR_MLS_LVL
) == 0) {
2167 * If there are flags but no level netlabel isn't
2168 * behaving the way we expect it to.
2170 * Without guidance regarding the smack value
2171 * for the packet fall back on the network
2174 strncpy(sip
, smack_net_ambient
, SMK_MAXLEN
);
2178 * Get the categories, if any
2180 memset(smack
, '\0', SMK_LABELLEN
);
2181 if ((sap
->flags
& NETLBL_SECATTR_MLS_CAT
) != 0)
2183 pcat
= netlbl_secattr_catmap_walk(sap
->attr
.mls
.cat
,
2187 smack_catset_bit(pcat
, smack
);
2190 * If it is CIPSO using smack direct mapping
2191 * we are already done. WeeHee.
2193 if (sap
->attr
.mls
.lvl
== smack_cipso_direct
) {
2194 memcpy(sip
, smack
, SMK_MAXLEN
);
2198 * Look it up in the supplied table if it is not a direct mapping.
2200 smack_from_cipso(sap
->attr
.mls
.lvl
, smack
, sip
);
2205 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2209 * Returns 0 if the packet should be delivered, an error code otherwise
2211 static int smack_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
2213 struct netlbl_lsm_secattr secattr
;
2214 struct socket_smack
*ssp
= sk
->sk_security
;
2215 char smack
[SMK_LABELLEN
];
2218 if (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
)
2222 * Translate what netlabel gave us.
2224 memset(smack
, '\0', SMK_LABELLEN
);
2225 netlbl_secattr_init(&secattr
);
2226 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &secattr
);
2228 smack_from_secattr(&secattr
, smack
);
2230 strncpy(smack
, smack_net_ambient
, SMK_MAXLEN
);
2231 netlbl_secattr_destroy(&secattr
);
2233 * Receiving a packet requires that the other end
2234 * be able to write here. Read access is not required.
2235 * This is the simplist possible security model
2238 return smk_access(smack
, ssp
->smk_in
, MAY_WRITE
);
2242 * smack_socket_getpeersec_stream - pull in packet label
2244 * @optval: user's destination
2245 * @optlen: size thereof
2248 * returns zero on success, an error code otherwise
2250 static int smack_socket_getpeersec_stream(struct socket
*sock
,
2251 char __user
*optval
,
2252 int __user
*optlen
, unsigned len
)
2254 struct socket_smack
*ssp
;
2258 ssp
= sock
->sk
->sk_security
;
2259 slen
= strlen(ssp
->smk_packet
) + 1;
2263 else if (copy_to_user(optval
, ssp
->smk_packet
, slen
) != 0)
2266 if (put_user(slen
, optlen
) != 0)
2274 * smack_socket_getpeersec_dgram - pull in packet label
2277 * @secid: pointer to where to put the secid of the packet
2279 * Sets the netlabel socket state on sk from parent
2281 static int smack_socket_getpeersec_dgram(struct socket
*sock
,
2282 struct sk_buff
*skb
, u32
*secid
)
2285 struct netlbl_lsm_secattr secattr
;
2287 char smack
[SMK_LABELLEN
];
2288 int family
= PF_INET
;
2293 * Only works for families with packets.
2297 if (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
)
2299 family
= sk
->sk_family
;
2302 * Translate what netlabel gave us.
2304 memset(smack
, '\0', SMK_LABELLEN
);
2305 netlbl_secattr_init(&secattr
);
2306 rc
= netlbl_skbuff_getattr(skb
, family
, &secattr
);
2308 smack_from_secattr(&secattr
, smack
);
2309 netlbl_secattr_destroy(&secattr
);
2312 * Give up if we couldn't get anything
2317 s
= smack_to_secid(smack
);
2326 * smack_sock_graft - graft access state between two sockets
2328 * @parent: donor socket
2330 * Sets the netlabel socket state on sk from parent
2332 static void smack_sock_graft(struct sock
*sk
, struct socket
*parent
)
2334 struct socket_smack
*ssp
;
2340 if (sk
->sk_family
!= PF_INET
&& sk
->sk_family
!= PF_INET6
)
2343 ssp
= sk
->sk_security
;
2344 ssp
->smk_in
= current
->security
;
2345 ssp
->smk_out
= current
->security
;
2346 ssp
->smk_packet
[0] = '\0';
2348 rc
= smack_netlabel(sk
);
2349 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
2352 printk(KERN_WARNING
"Smack: \"%s\" netlbl error %d.\n",
2354 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
2358 * smack_inet_conn_request - Smack access check on connect
2359 * @sk: socket involved
2363 * Returns 0 if a task with the packet label could write to
2364 * the socket, otherwise an error code
2366 static int smack_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
2367 struct request_sock
*req
)
2369 struct netlbl_lsm_secattr skb_secattr
;
2370 struct socket_smack
*ssp
= sk
->sk_security
;
2371 char smack
[SMK_LABELLEN
];
2377 memset(smack
, '\0', SMK_LABELLEN
);
2378 netlbl_secattr_init(&skb_secattr
);
2379 rc
= netlbl_skbuff_getattr(skb
, sk
->sk_family
, &skb_secattr
);
2381 smack_from_secattr(&skb_secattr
, smack
);
2383 strncpy(smack
, smack_known_huh
.smk_known
, SMK_MAXLEN
);
2384 netlbl_secattr_destroy(&skb_secattr
);
2386 * Receiving a packet requires that the other end
2387 * be able to write here. Read access is not required.
2389 * If the request is successful save the peer's label
2390 * so that SO_PEERCRED can report it.
2392 rc
= smk_access(smack
, ssp
->smk_in
, MAY_WRITE
);
2394 strncpy(ssp
->smk_packet
, smack
, SMK_MAXLEN
);
2400 * Key management security hooks
2402 * Casey has not tested key support very heavily.
2403 * The permission check is most likely too restrictive.
2404 * If you care about keys please have a look.
2409 * smack_key_alloc - Set the key security blob
2411 * @tsk: the task associated with the key
2414 * No allocation required
2418 static int smack_key_alloc(struct key
*key
, struct task_struct
*tsk
,
2419 unsigned long flags
)
2421 key
->security
= tsk
->security
;
2426 * smack_key_free - Clear the key security blob
2429 * Clear the blob pointer
2431 static void smack_key_free(struct key
*key
)
2433 key
->security
= NULL
;
2437 * smack_key_permission - Smack access on a key
2438 * @key_ref: gets to the object
2439 * @context: task involved
2442 * Return 0 if the task has read and write to the object,
2443 * an error code otherwise
2445 static int smack_key_permission(key_ref_t key_ref
,
2446 struct task_struct
*context
, key_perm_t perm
)
2450 keyp
= key_ref_to_ptr(key_ref
);
2454 * If the key hasn't been initialized give it access so that
2457 if (keyp
->security
== NULL
)
2460 * This should not occur
2462 if (context
->security
== NULL
)
2465 return smk_access(context
->security
, keyp
->security
, MAY_READWRITE
);
2467 #endif /* CONFIG_KEYS */
2470 * smack_secid_to_secctx - return the smack label for a secid
2471 * @secid: incoming integer
2472 * @secdata: destination
2473 * @seclen: how long it is
2475 * Exists for networking code.
2477 static int smack_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
2479 char *sp
= smack_from_secid(secid
);
2482 *seclen
= strlen(sp
);
2487 <<<<<<< HEAD:security/smack/smack_lsm.c
2489 * smack_secctx_to_secid - return the secid for a smack label
2490 * @secdata: smack label
2491 * @seclen: how long result is
2492 * @secid: outgoing integer
2494 * Exists for audit and networking code.
2496 static int smack_secctx_to_secid(char *secdata
, u32 seclen
, u32
*secid
)
2498 *secid
= smack_to_secid(secdata
);
2503 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:security/smack/smack_lsm.c
2504 * smack_release_secctx - don't do anything.
2509 * Exists to make sure nothing gets done, and properly
2511 static void smack_release_secctx(char *secdata
, u32 seclen
)
2515 static struct security_operations smack_ops
= {
2516 .ptrace
= smack_ptrace
,
2517 .capget
= cap_capget
,
2518 .capset_check
= cap_capset_check
,
2519 .capset_set
= cap_capset_set
,
2520 .capable
= cap_capable
,
2521 .syslog
= smack_syslog
,
2522 .settime
= cap_settime
,
2523 .vm_enough_memory
= cap_vm_enough_memory
,
2525 .bprm_apply_creds
= cap_bprm_apply_creds
,
2526 .bprm_set_security
= cap_bprm_set_security
,
2527 .bprm_secureexec
= cap_bprm_secureexec
,
2529 .sb_alloc_security
= smack_sb_alloc_security
,
2530 .sb_free_security
= smack_sb_free_security
,
2531 .sb_copy_data
= smack_sb_copy_data
,
2532 .sb_kern_mount
= smack_sb_kern_mount
,
2533 .sb_statfs
= smack_sb_statfs
,
2534 .sb_mount
= smack_sb_mount
,
2535 .sb_umount
= smack_sb_umount
,
2537 .inode_alloc_security
= smack_inode_alloc_security
,
2538 .inode_free_security
= smack_inode_free_security
,
2539 .inode_init_security
= smack_inode_init_security
,
2540 .inode_link
= smack_inode_link
,
2541 .inode_unlink
= smack_inode_unlink
,
2542 .inode_rmdir
= smack_inode_rmdir
,
2543 .inode_rename
= smack_inode_rename
,
2544 .inode_permission
= smack_inode_permission
,
2545 .inode_setattr
= smack_inode_setattr
,
2546 .inode_getattr
= smack_inode_getattr
,
2547 .inode_setxattr
= smack_inode_setxattr
,
2548 .inode_post_setxattr
= smack_inode_post_setxattr
,
2549 .inode_getxattr
= smack_inode_getxattr
,
2550 .inode_removexattr
= smack_inode_removexattr
,
2551 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
2553 .inode_need_killpriv
= cap_inode_need_killpriv
,
2554 .inode_killpriv
= cap_inode_killpriv
,
2555 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
2556 .inode_getsecurity
= smack_inode_getsecurity
,
2557 .inode_setsecurity
= smack_inode_setsecurity
,
2558 .inode_listsecurity
= smack_inode_listsecurity
,
2560 .file_permission
= smack_file_permission
,
2561 .file_alloc_security
= smack_file_alloc_security
,
2562 .file_free_security
= smack_file_free_security
,
2563 .file_ioctl
= smack_file_ioctl
,
2564 .file_lock
= smack_file_lock
,
2565 .file_fcntl
= smack_file_fcntl
,
2566 .file_set_fowner
= smack_file_set_fowner
,
2567 .file_send_sigiotask
= smack_file_send_sigiotask
,
2568 .file_receive
= smack_file_receive
,
2570 .task_alloc_security
= smack_task_alloc_security
,
2571 .task_free_security
= smack_task_free_security
,
2572 .task_post_setuid
= cap_task_post_setuid
,
2573 .task_setpgid
= smack_task_setpgid
,
2574 .task_getpgid
= smack_task_getpgid
,
2575 .task_getsid
= smack_task_getsid
,
2576 .task_getsecid
= smack_task_getsecid
,
2577 .task_setnice
= smack_task_setnice
,
2578 .task_setioprio
= smack_task_setioprio
,
2579 .task_getioprio
= smack_task_getioprio
,
2580 .task_setscheduler
= smack_task_setscheduler
,
2581 .task_getscheduler
= smack_task_getscheduler
,
2582 .task_movememory
= smack_task_movememory
,
2583 .task_kill
= smack_task_kill
,
2584 .task_wait
= smack_task_wait
,
2585 .task_reparent_to_init
= cap_task_reparent_to_init
,
2586 .task_to_inode
= smack_task_to_inode
,
2588 .ipc_permission
= smack_ipc_permission
,
2590 .msg_msg_alloc_security
= smack_msg_msg_alloc_security
,
2591 .msg_msg_free_security
= smack_msg_msg_free_security
,
2593 .msg_queue_alloc_security
= smack_msg_queue_alloc_security
,
2594 .msg_queue_free_security
= smack_msg_queue_free_security
,
2595 .msg_queue_associate
= smack_msg_queue_associate
,
2596 .msg_queue_msgctl
= smack_msg_queue_msgctl
,
2597 .msg_queue_msgsnd
= smack_msg_queue_msgsnd
,
2598 .msg_queue_msgrcv
= smack_msg_queue_msgrcv
,
2600 .shm_alloc_security
= smack_shm_alloc_security
,
2601 .shm_free_security
= smack_shm_free_security
,
2602 .shm_associate
= smack_shm_associate
,
2603 .shm_shmctl
= smack_shm_shmctl
,
2604 .shm_shmat
= smack_shm_shmat
,
2606 .sem_alloc_security
= smack_sem_alloc_security
,
2607 .sem_free_security
= smack_sem_free_security
,
2608 .sem_associate
= smack_sem_associate
,
2609 .sem_semctl
= smack_sem_semctl
,
2610 .sem_semop
= smack_sem_semop
,
2612 .netlink_send
= cap_netlink_send
,
2613 .netlink_recv
= cap_netlink_recv
,
2615 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
2617 .register_security
= smack_register_security
,
2619 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
2620 .d_instantiate
= smack_d_instantiate
,
2622 .getprocattr
= smack_getprocattr
,
2623 .setprocattr
= smack_setprocattr
,
2625 .unix_stream_connect
= smack_unix_stream_connect
,
2626 .unix_may_send
= smack_unix_may_send
,
2628 .socket_post_create
= smack_socket_post_create
,
2629 .socket_sock_rcv_skb
= smack_socket_sock_rcv_skb
,
2630 .socket_getpeersec_stream
= smack_socket_getpeersec_stream
,
2631 .socket_getpeersec_dgram
= smack_socket_getpeersec_dgram
,
2632 .sk_alloc_security
= smack_sk_alloc_security
,
2633 .sk_free_security
= smack_sk_free_security
,
2634 .sock_graft
= smack_sock_graft
,
2635 .inet_conn_request
= smack_inet_conn_request
,
2636 /* key management security hooks */
2638 .key_alloc
= smack_key_alloc
,
2639 .key_free
= smack_key_free
,
2640 .key_permission
= smack_key_permission
,
2641 #endif /* CONFIG_KEYS */
2642 .secid_to_secctx
= smack_secid_to_secctx
,
2643 <<<<<<< HEAD
:security
/smack
/smack_lsm
.c
2645 .secctx_to_secid
= smack_secctx_to_secid
,
2646 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:security
/smack
/smack_lsm
.c
2647 .release_secctx
= smack_release_secctx
,
2651 * smack_init - initialize the smack system
2655 static __init
int smack_init(void)
2657 printk(KERN_INFO
"Smack: Initializing.\n");
2660 * Set the security state for the initial task.
2662 current
->security
= &smack_known_floor
.smk_known
;
2667 spin_lock_init(&smack_known_unset
.smk_cipsolock
);
2668 spin_lock_init(&smack_known_huh
.smk_cipsolock
);
2669 spin_lock_init(&smack_known_hat
.smk_cipsolock
);
2670 spin_lock_init(&smack_known_star
.smk_cipsolock
);
2671 spin_lock_init(&smack_known_floor
.smk_cipsolock
);
2672 spin_lock_init(&smack_known_invalid
.smk_cipsolock
);
2677 if (register_security(&smack_ops
))
2678 panic("smack: Unable to register with kernel.\n");
2684 * Smack requires early initialization in order to label
2685 * all processes and objects when they are created.
2687 security_initcall(smack_init
);