SSB PCI core driver: use new SPROM data structure
[linux-2.6/sactl.git] / security / smack / smack_lsm.c
blob770eb067e165f493b1e153cac3cfcec6d66953f7
1 /*
2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
6 * Author:
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>
21 #include <linux/kd.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>
30 #include "smack.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
39 /**
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)
49 int rc;
50 char in[SMK_LABELLEN];
52 if (ip->i_op->getxattr == NULL)
53 return NULL;
55 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
56 if (rc < 0)
57 return NULL;
59 return smk_import(in, rc);
62 /**
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);
73 if (isp == NULL)
74 return NULL;
76 isp->smk_inode = smack;
77 isp->smk_flags = 0;
78 mutex_init(&isp->smk_lock);
80 return isp;
84 * LSM hooks.
85 * We he, that is fun!
88 /**
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)
99 int rc;
101 rc = cap_ptrace(ptp, ctp);
102 if (rc != 0)
103 return rc;
105 rc = smk_access(ptp->security, ctp->security, MAY_READWRITE);
106 if (rc != 0 && __capable(ptp, CAP_MAC_OVERRIDE))
107 return 0;
109 return rc;
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)
122 int rc;
123 char *sp = current->security;
125 rc = cap_syslog(type);
126 if (rc != 0)
127 return rc;
129 if (capable(CAP_MAC_OVERRIDE))
130 return 0;
132 if (sp != smack_known_floor.smk_known)
133 rc = -EACCES;
135 return rc;
140 * Superblock Hooks.
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);
155 if (sbsp == NULL)
156 return -ENOMEM;
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;
167 return 0;
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
185 * @smackopts
187 * Returns 0 on success or -ENOMEM on error.
189 * Copy the Smack specific mount options out of the mount
190 * options list.
192 static int smack_sb_copy_data(struct file_system_type *type, void *orig,
193 void *smackopts)
195 char *cp, *commap, *otheropts, *dp;
197 /* Binary mount data: just copy */
198 if (type->fs_flags & FS_BINARY_MOUNTDATA) {
199 copy_page(smackopts, orig);
200 return 0;
203 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
204 if (otheropts == NULL)
205 return -ENOMEM;
207 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
208 if (strstr(cp, SMK_FSDEFAULT) == cp)
209 dp = smackopts;
210 else if (strstr(cp, SMK_FSFLOOR) == cp)
211 dp = smackopts;
212 else if (strstr(cp, SMK_FSHAT) == cp)
213 dp = smackopts;
214 else if (strstr(cp, SMK_FSROOT) == cp)
215 dp = smackopts;
216 else
217 dp = otheropts;
219 commap = strchr(cp, ',');
220 if (commap != NULL)
221 *commap = '\0';
223 if (*dp != '\0')
224 strcat(dp, ",");
225 strcat(dp, cp);
228 strcpy(orig, otheropts);
229 free_page((unsigned long)otheropts);
231 return 0;
235 * smack_sb_kern_mount - Smack specific mount processing
236 * @sb: the file system superblock
237 * @data: the smack mount options
239 * Returns 0 on success, an error code on failure
241 static int smack_sb_kern_mount(struct super_block *sb, void *data)
243 struct dentry *root = sb->s_root;
244 struct inode *inode = root->d_inode;
245 struct superblock_smack *sp = sb->s_security;
246 struct inode_smack *isp;
247 char *op;
248 char *commap;
249 char *nsp;
251 spin_lock(&sp->smk_sblock);
252 if (sp->smk_initialized != 0) {
253 spin_unlock(&sp->smk_sblock);
254 return 0;
256 sp->smk_initialized = 1;
257 spin_unlock(&sp->smk_sblock);
259 for (op = data; op != NULL; op = commap) {
260 commap = strchr(op, ',');
261 if (commap != NULL)
262 *commap++ = '\0';
264 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
265 op += strlen(SMK_FSHAT);
266 nsp = smk_import(op, 0);
267 if (nsp != NULL)
268 sp->smk_hat = nsp;
269 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
270 op += strlen(SMK_FSFLOOR);
271 nsp = smk_import(op, 0);
272 if (nsp != NULL)
273 sp->smk_floor = nsp;
274 } else if (strncmp(op, SMK_FSDEFAULT,
275 strlen(SMK_FSDEFAULT)) == 0) {
276 op += strlen(SMK_FSDEFAULT);
277 nsp = smk_import(op, 0);
278 if (nsp != NULL)
279 sp->smk_default = nsp;
280 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
281 op += strlen(SMK_FSROOT);
282 nsp = smk_import(op, 0);
283 if (nsp != NULL)
284 sp->smk_root = nsp;
289 * Initialize the root inode.
291 isp = inode->i_security;
292 if (isp == NULL)
293 inode->i_security = new_inode_smack(sp->smk_root);
294 else
295 isp->smk_inode = sp->smk_root;
297 return 0;
301 * smack_sb_statfs - Smack check on statfs
302 * @dentry: identifies the file system in question
304 * Returns 0 if current can read the floor of the filesystem,
305 * and error code otherwise
307 static int smack_sb_statfs(struct dentry *dentry)
309 struct superblock_smack *sbp = dentry->d_sb->s_security;
311 return smk_curacc(sbp->smk_floor, MAY_READ);
315 * smack_sb_mount - Smack check for mounting
316 * @dev_name: unused
317 * @nd: mount point
318 * @type: unused
319 * @flags: unused
320 * @data: unused
322 * Returns 0 if current can write the floor of the filesystem
323 * being mounted on, an error code otherwise.
325 static int smack_sb_mount(char *dev_name, struct nameidata *nd,
326 char *type, unsigned long flags, void *data)
328 struct superblock_smack *sbp = nd->path.mnt->mnt_sb->s_security;
330 return smk_curacc(sbp->smk_floor, MAY_WRITE);
334 * smack_sb_umount - Smack check for unmounting
335 * @mnt: file system to unmount
336 * @flags: unused
338 * Returns 0 if current can write the floor of the filesystem
339 * being unmounted, an error code otherwise.
341 static int smack_sb_umount(struct vfsmount *mnt, int flags)
343 struct superblock_smack *sbp;
345 sbp = mnt->mnt_sb->s_security;
347 return smk_curacc(sbp->smk_floor, MAY_WRITE);
351 * Inode hooks
355 * smack_inode_alloc_security - allocate an inode blob
356 * @inode - the inode in need of a blob
358 * Returns 0 if it gets a blob, -ENOMEM otherwise
360 static int smack_inode_alloc_security(struct inode *inode)
362 inode->i_security = new_inode_smack(current->security);
363 if (inode->i_security == NULL)
364 return -ENOMEM;
365 return 0;
369 * smack_inode_free_security - free an inode blob
370 * @inode - the inode with a blob
372 * Clears the blob pointer in inode
374 static void smack_inode_free_security(struct inode *inode)
376 kfree(inode->i_security);
377 inode->i_security = NULL;
381 * smack_inode_init_security - copy out the smack from an inode
382 * @inode: the inode
383 * @dir: unused
384 * @name: where to put the attribute name
385 * @value: where to put the attribute value
386 * @len: where to put the length of the attribute
388 * Returns 0 if it all works out, -ENOMEM if there's no memory
390 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
391 char **name, void **value, size_t *len)
393 char *isp = smk_of_inode(inode);
395 if (name) {
396 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
397 if (*name == NULL)
398 return -ENOMEM;
401 if (value) {
402 *value = kstrdup(isp, GFP_KERNEL);
403 if (*value == NULL)
404 return -ENOMEM;
407 if (len)
408 *len = strlen(isp) + 1;
410 return 0;
414 * smack_inode_link - Smack check on link
415 * @old_dentry: the existing object
416 * @dir: unused
417 * @new_dentry: the new object
419 * Returns 0 if access is permitted, an error code otherwise
421 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
422 struct dentry *new_dentry)
424 int rc;
425 char *isp;
427 isp = smk_of_inode(old_dentry->d_inode);
428 rc = smk_curacc(isp, MAY_WRITE);
430 if (rc == 0 && new_dentry->d_inode != NULL) {
431 isp = smk_of_inode(new_dentry->d_inode);
432 rc = smk_curacc(isp, MAY_WRITE);
435 return rc;
439 * smack_inode_unlink - Smack check on inode deletion
440 * @dir: containing directory object
441 * @dentry: file to unlink
443 * Returns 0 if current can write the containing directory
444 * and the object, error code otherwise
446 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
448 struct inode *ip = dentry->d_inode;
449 int rc;
452 * You need write access to the thing you're unlinking
454 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
455 if (rc == 0)
457 * You also need write access to the containing directory
459 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
461 return rc;
465 * smack_inode_rmdir - Smack check on directory deletion
466 * @dir: containing directory object
467 * @dentry: directory to unlink
469 * Returns 0 if current can write the containing directory
470 * and the directory, error code otherwise
472 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
474 int rc;
477 * You need write access to the thing you're removing
479 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
480 if (rc == 0)
482 * You also need write access to the containing directory
484 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
486 return rc;
490 * smack_inode_rename - Smack check on rename
491 * @old_inode: the old directory
492 * @old_dentry: unused
493 * @new_inode: the new directory
494 * @new_dentry: unused
496 * Read and write access is required on both the old and
497 * new directories.
499 * Returns 0 if access is permitted, an error code otherwise
501 static int smack_inode_rename(struct inode *old_inode,
502 struct dentry *old_dentry,
503 struct inode *new_inode,
504 struct dentry *new_dentry)
506 int rc;
507 char *isp;
509 isp = smk_of_inode(old_dentry->d_inode);
510 rc = smk_curacc(isp, MAY_READWRITE);
512 if (rc == 0 && new_dentry->d_inode != NULL) {
513 isp = smk_of_inode(new_dentry->d_inode);
514 rc = smk_curacc(isp, MAY_READWRITE);
517 return rc;
521 * smack_inode_permission - Smack version of permission()
522 * @inode: the inode in question
523 * @mask: the access requested
524 * @nd: unused
526 * This is the important Smack hook.
528 * Returns 0 if access is permitted, -EACCES otherwise
530 static int smack_inode_permission(struct inode *inode, int mask,
531 struct nameidata *nd)
534 * No permission to check. Existence test. Yup, it's there.
536 if (mask == 0)
537 return 0;
539 return smk_curacc(smk_of_inode(inode), mask);
543 * smack_inode_setattr - Smack check for setting attributes
544 * @dentry: the object
545 * @iattr: for the force flag
547 * Returns 0 if access is permitted, an error code otherwise
549 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
552 * Need to allow for clearing the setuid bit.
554 if (iattr->ia_valid & ATTR_FORCE)
555 return 0;
557 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
561 * smack_inode_getattr - Smack check for getting attributes
562 * @mnt: unused
563 * @dentry: the object
565 * Returns 0 if access is permitted, an error code otherwise
567 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
569 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
573 * smack_inode_setxattr - Smack check for setting xattrs
574 * @dentry: the object
575 * @name: name of the attribute
576 * @value: unused
577 * @size: unused
578 * @flags: unused
580 * This protects the Smack attribute explicitly.
582 * Returns 0 if access is permitted, an error code otherwise
584 static int smack_inode_setxattr(struct dentry *dentry, char *name,
585 void *value, size_t size, int flags)
587 int rc = 0;
589 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
590 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
591 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
592 if (!capable(CAP_MAC_ADMIN))
593 rc = -EPERM;
594 } else
595 rc = cap_inode_setxattr(dentry, name, value, size, flags);
597 if (rc == 0)
598 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
600 return rc;
604 * smack_inode_post_setxattr - Apply the Smack update approved above
605 * @dentry: object
606 * @name: attribute name
607 * @value: attribute value
608 * @size: attribute size
609 * @flags: unused
611 * Set the pointer in the inode blob to the entry found
612 * in the master label list.
614 static void smack_inode_post_setxattr(struct dentry *dentry, char *name,
615 void *value, size_t size, int flags)
617 struct inode_smack *isp;
618 char *nsp;
621 * Not SMACK
623 if (strcmp(name, XATTR_NAME_SMACK))
624 return;
626 if (size >= SMK_LABELLEN)
627 return;
629 isp = dentry->d_inode->i_security;
632 * No locking is done here. This is a pointer
633 * assignment.
635 nsp = smk_import(value, size);
636 if (nsp != NULL)
637 isp->smk_inode = nsp;
638 else
639 isp->smk_inode = smack_known_invalid.smk_known;
641 return;
645 * smack_inode_getxattr - Smack check on getxattr
646 * @dentry: the object
647 * @name: unused
649 * Returns 0 if access is permitted, an error code otherwise
651 static int smack_inode_getxattr(struct dentry *dentry, char *name)
653 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
657 * smack_inode_removexattr - Smack check on removexattr
658 * @dentry: the object
659 * @name: name of the attribute
661 * Removing the Smack attribute requires CAP_MAC_ADMIN
663 * Returns 0 if access is permitted, an error code otherwise
665 static int smack_inode_removexattr(struct dentry *dentry, char *name)
667 int rc = 0;
669 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
670 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
671 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
672 if (!capable(CAP_MAC_ADMIN))
673 rc = -EPERM;
674 } else
675 rc = cap_inode_removexattr(dentry, name);
677 if (rc == 0)
678 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
680 return rc;
684 * smack_inode_getsecurity - get smack xattrs
685 * @inode: the object
686 * @name: attribute name
687 * @buffer: where to put the result
688 * @size: size of the buffer
689 * @err: unused
691 * Returns the size of the attribute or an error code
693 static int smack_inode_getsecurity(const struct inode *inode,
694 const char *name, void **buffer,
695 bool alloc)
697 struct socket_smack *ssp;
698 struct socket *sock;
699 struct super_block *sbp;
700 struct inode *ip = (struct inode *)inode;
701 char *isp;
702 int ilen;
703 int rc = 0;
705 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
706 isp = smk_of_inode(inode);
707 ilen = strlen(isp) + 1;
708 *buffer = isp;
709 return ilen;
713 * The rest of the Smack xattrs are only on sockets.
715 sbp = ip->i_sb;
716 if (sbp->s_magic != SOCKFS_MAGIC)
717 return -EOPNOTSUPP;
719 sock = SOCKET_I(ip);
720 if (sock == NULL || sock->sk == NULL)
721 return -EOPNOTSUPP;
723 ssp = sock->sk->sk_security;
725 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
726 isp = ssp->smk_in;
727 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
728 isp = ssp->smk_out;
729 else
730 return -EOPNOTSUPP;
732 ilen = strlen(isp) + 1;
733 if (rc == 0) {
734 *buffer = isp;
735 rc = ilen;
738 return rc;
743 * smack_inode_listsecurity - list the Smack attributes
744 * @inode: the object
745 * @buffer: where they go
746 * @buffer_size: size of buffer
748 * Returns 0 on success, -EINVAL otherwise
750 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
751 size_t buffer_size)
753 int len = strlen(XATTR_NAME_SMACK);
755 if (buffer != NULL && len <= buffer_size) {
756 memcpy(buffer, XATTR_NAME_SMACK, len);
757 return len;
759 return -EINVAL;
763 * File Hooks
767 * smack_file_permission - Smack check on file operations
768 * @file: unused
769 * @mask: unused
771 * Returns 0
773 * Should access checks be done on each read or write?
774 * UNICOS and SELinux say yes.
775 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
777 * I'll say no for now. Smack does not do the frequent
778 * label changing that SELinux does.
780 static int smack_file_permission(struct file *file, int mask)
782 return 0;
786 * smack_file_alloc_security - assign a file security blob
787 * @file: the object
789 * The security blob for a file is a pointer to the master
790 * label list, so no allocation is done.
792 * Returns 0
794 static int smack_file_alloc_security(struct file *file)
796 file->f_security = current->security;
797 return 0;
801 * smack_file_free_security - clear a file security blob
802 * @file: the object
804 * The security blob for a file is a pointer to the master
805 * label list, so no memory is freed.
807 static void smack_file_free_security(struct file *file)
809 file->f_security = NULL;
813 * smack_file_ioctl - Smack check on ioctls
814 * @file: the object
815 * @cmd: what to do
816 * @arg: unused
818 * Relies heavily on the correct use of the ioctl command conventions.
820 * Returns 0 if allowed, error code otherwise
822 static int smack_file_ioctl(struct file *file, unsigned int cmd,
823 unsigned long arg)
825 int rc = 0;
827 if (_IOC_DIR(cmd) & _IOC_WRITE)
828 rc = smk_curacc(file->f_security, MAY_WRITE);
830 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
831 rc = smk_curacc(file->f_security, MAY_READ);
833 return rc;
837 * smack_file_lock - Smack check on file locking
838 * @file: the object
839 * @cmd unused
841 * Returns 0 if current has write access, error code otherwise
843 static int smack_file_lock(struct file *file, unsigned int cmd)
845 return smk_curacc(file->f_security, MAY_WRITE);
849 * smack_file_fcntl - Smack check on fcntl
850 * @file: the object
851 * @cmd: what action to check
852 * @arg: unused
854 * Returns 0 if current has access, error code otherwise
856 static int smack_file_fcntl(struct file *file, unsigned int cmd,
857 unsigned long arg)
859 int rc;
861 switch (cmd) {
862 case F_DUPFD:
863 case F_GETFD:
864 case F_GETFL:
865 case F_GETLK:
866 case F_GETOWN:
867 case F_GETSIG:
868 rc = smk_curacc(file->f_security, MAY_READ);
869 break;
870 case F_SETFD:
871 case F_SETFL:
872 case F_SETLK:
873 case F_SETLKW:
874 case F_SETOWN:
875 case F_SETSIG:
876 rc = smk_curacc(file->f_security, MAY_WRITE);
877 break;
878 default:
879 rc = smk_curacc(file->f_security, MAY_READWRITE);
882 return rc;
886 * smack_file_set_fowner - set the file security blob value
887 * @file: object in question
889 * Returns 0
890 * Further research may be required on this one.
892 static int smack_file_set_fowner(struct file *file)
894 file->f_security = current->security;
895 return 0;
899 * smack_file_send_sigiotask - Smack on sigio
900 * @tsk: The target task
901 * @fown: the object the signal come from
902 * @signum: unused
904 * Allow a privileged task to get signals even if it shouldn't
906 * Returns 0 if a subject with the object's smack could
907 * write to the task, an error code otherwise.
909 static int smack_file_send_sigiotask(struct task_struct *tsk,
910 struct fown_struct *fown, int signum)
912 struct file *file;
913 int rc;
916 * struct fown_struct is never outside the context of a struct file
918 file = container_of(fown, struct file, f_owner);
919 rc = smk_access(file->f_security, tsk->security, MAY_WRITE);
920 if (rc != 0 && __capable(tsk, CAP_MAC_OVERRIDE))
921 return 0;
922 return rc;
926 * smack_file_receive - Smack file receive check
927 * @file: the object
929 * Returns 0 if current has access, error code otherwise
931 static int smack_file_receive(struct file *file)
933 int may = 0;
936 * This code relies on bitmasks.
938 if (file->f_mode & FMODE_READ)
939 may = MAY_READ;
940 if (file->f_mode & FMODE_WRITE)
941 may |= MAY_WRITE;
943 return smk_curacc(file->f_security, may);
947 * Task hooks
951 * smack_task_alloc_security - "allocate" a task blob
952 * @tsk: the task in need of a blob
954 * Smack isn't using copies of blobs. Everyone
955 * points to an immutable list. No alloc required.
956 * No data copy required.
958 * Always returns 0
960 static int smack_task_alloc_security(struct task_struct *tsk)
962 tsk->security = current->security;
964 return 0;
968 * smack_task_free_security - "free" a task blob
969 * @task: the task with the blob
971 * Smack isn't using copies of blobs. Everyone
972 * points to an immutable list. The blobs never go away.
973 * There is no leak here.
975 static void smack_task_free_security(struct task_struct *task)
977 task->security = NULL;
981 * smack_task_setpgid - Smack check on setting pgid
982 * @p: the task object
983 * @pgid: unused
985 * Return 0 if write access is permitted
987 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
989 return smk_curacc(p->security, MAY_WRITE);
993 * smack_task_getpgid - Smack access check for getpgid
994 * @p: the object task
996 * Returns 0 if current can read the object task, error code otherwise
998 static int smack_task_getpgid(struct task_struct *p)
1000 return smk_curacc(p->security, MAY_READ);
1004 * smack_task_getsid - Smack access check for getsid
1005 * @p: the object task
1007 * Returns 0 if current can read the object task, error code otherwise
1009 static int smack_task_getsid(struct task_struct *p)
1011 return smk_curacc(p->security, MAY_READ);
1015 * smack_task_getsecid - get the secid of the task
1016 * @p: the object task
1017 * @secid: where to put the result
1019 * Sets the secid to contain a u32 version of the smack label.
1021 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1023 *secid = smack_to_secid(p->security);
1027 * smack_task_setnice - Smack check on setting nice
1028 * @p: the task object
1029 * @nice: unused
1031 * Return 0 if write access is permitted
1033 static int smack_task_setnice(struct task_struct *p, int nice)
1035 int rc;
1037 rc = cap_task_setnice(p, nice);
1038 if (rc == 0)
1039 rc = smk_curacc(p->security, MAY_WRITE);
1040 return rc;
1044 * smack_task_setioprio - Smack check on setting ioprio
1045 * @p: the task object
1046 * @ioprio: unused
1048 * Return 0 if write access is permitted
1050 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1052 int rc;
1054 rc = cap_task_setioprio(p, ioprio);
1055 if (rc == 0)
1056 rc = smk_curacc(p->security, MAY_WRITE);
1057 return rc;
1061 * smack_task_getioprio - Smack check on reading ioprio
1062 * @p: the task object
1064 * Return 0 if read access is permitted
1066 static int smack_task_getioprio(struct task_struct *p)
1068 return smk_curacc(p->security, MAY_READ);
1072 * smack_task_setscheduler - Smack check on setting scheduler
1073 * @p: the task object
1074 * @policy: unused
1075 * @lp: unused
1077 * Return 0 if read access is permitted
1079 static int smack_task_setscheduler(struct task_struct *p, int policy,
1080 struct sched_param *lp)
1082 int rc;
1084 rc = cap_task_setscheduler(p, policy, lp);
1085 if (rc == 0)
1086 rc = smk_curacc(p->security, MAY_WRITE);
1087 return rc;
1091 * smack_task_getscheduler - Smack check on reading scheduler
1092 * @p: the task object
1094 * Return 0 if read access is permitted
1096 static int smack_task_getscheduler(struct task_struct *p)
1098 return smk_curacc(p->security, MAY_READ);
1102 * smack_task_movememory - Smack check on moving memory
1103 * @p: the task object
1105 * Return 0 if write access is permitted
1107 static int smack_task_movememory(struct task_struct *p)
1109 return smk_curacc(p->security, MAY_WRITE);
1113 * smack_task_kill - Smack check on signal delivery
1114 * @p: the task object
1115 * @info: unused
1116 * @sig: unused
1117 * @secid: identifies the smack to use in lieu of current's
1119 * Return 0 if write access is permitted
1121 * The secid behavior is an artifact of an SELinux hack
1122 * in the USB code. Someday it may go away.
1124 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1125 int sig, u32 secid)
1127 int rc;
1129 rc = cap_task_kill(p, info, sig, secid);
1130 if (rc != 0)
1131 return rc;
1133 * Special cases where signals really ought to go through
1134 * in spite of policy. Stephen Smalley suggests it may
1135 * make sense to change the caller so that it doesn't
1136 * bother with the LSM hook in these cases.
1138 if (info != SEND_SIG_NOINFO &&
1139 (is_si_special(info) || SI_FROMKERNEL(info)))
1140 return 0;
1142 * Sending a signal requires that the sender
1143 * can write the receiver.
1145 if (secid == 0)
1146 return smk_curacc(p->security, MAY_WRITE);
1148 * If the secid isn't 0 we're dealing with some USB IO
1149 * specific behavior. This is not clean. For one thing
1150 * we can't take privilege into account.
1152 return smk_access(smack_from_secid(secid), p->security, MAY_WRITE);
1156 * smack_task_wait - Smack access check for waiting
1157 * @p: task to wait for
1159 * Returns 0 if current can wait for p, error code otherwise
1161 static int smack_task_wait(struct task_struct *p)
1163 int rc;
1165 rc = smk_access(current->security, p->security, MAY_WRITE);
1166 if (rc == 0)
1167 return 0;
1170 * Allow the operation to succeed if either task
1171 * has privilege to perform operations that might
1172 * account for the smack labels having gotten to
1173 * be different in the first place.
1175 * This breaks the strict subjet/object access
1176 * control ideal, taking the object's privilege
1177 * state into account in the decision as well as
1178 * the smack value.
1180 if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE))
1181 return 0;
1183 return rc;
1187 * smack_task_to_inode - copy task smack into the inode blob
1188 * @p: task to copy from
1189 * inode: inode to copy to
1191 * Sets the smack pointer in the inode security blob
1193 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1195 struct inode_smack *isp = inode->i_security;
1196 isp->smk_inode = p->security;
1200 * Socket hooks.
1204 * smack_sk_alloc_security - Allocate a socket blob
1205 * @sk: the socket
1206 * @family: unused
1207 * @priority: memory allocation priority
1209 * Assign Smack pointers to current
1211 * Returns 0 on success, -ENOMEM is there's no memory
1213 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1215 char *csp = current->security;
1216 struct socket_smack *ssp;
1218 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1219 if (ssp == NULL)
1220 return -ENOMEM;
1222 ssp->smk_in = csp;
1223 ssp->smk_out = csp;
1224 ssp->smk_packet[0] = '\0';
1226 sk->sk_security = ssp;
1228 return 0;
1232 * smack_sk_free_security - Free a socket blob
1233 * @sk: the socket
1235 * Clears the blob pointer
1237 static void smack_sk_free_security(struct sock *sk)
1239 kfree(sk->sk_security);
1243 * smack_set_catset - convert a capset to netlabel mls categories
1244 * @catset: the Smack categories
1245 * @sap: where to put the netlabel categories
1247 * Allocates and fills attr.mls.cat
1249 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1251 unsigned char *cp;
1252 unsigned char m;
1253 int cat;
1254 int rc;
1255 int byte;
1257 if (catset == 0)
1258 return;
1260 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1261 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1262 sap->attr.mls.cat->startbit = 0;
1264 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1265 for (m = 0x80; m != 0; m >>= 1, cat++) {
1266 if ((m & *cp) == 0)
1267 continue;
1268 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1269 cat, GFP_ATOMIC);
1274 * smack_to_secattr - fill a secattr from a smack value
1275 * @smack: the smack value
1276 * @nlsp: where the result goes
1278 * Casey says that CIPSO is good enough for now.
1279 * It can be used to effect.
1280 * It can also be abused to effect when necessary.
1281 * Appologies to the TSIG group in general and GW in particular.
1283 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1285 struct smack_cipso cipso;
1286 int rc;
1288 switch (smack_net_nltype) {
1289 case NETLBL_NLTYPE_CIPSOV4:
1290 nlsp->domain = kstrdup(smack, GFP_ATOMIC);
1291 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1293 rc = smack_to_cipso(smack, &cipso);
1294 if (rc == 0) {
1295 nlsp->attr.mls.lvl = cipso.smk_level;
1296 smack_set_catset(cipso.smk_catset, nlsp);
1297 } else {
1298 nlsp->attr.mls.lvl = smack_cipso_direct;
1299 smack_set_catset(smack, nlsp);
1301 break;
1302 default:
1303 break;
1308 * smack_netlabel - Set the secattr on a socket
1309 * @sk: the socket
1311 * Convert the outbound smack value (smk_out) to a
1312 * secattr and attach it to the socket.
1314 * Returns 0 on success or an error code
1316 static int smack_netlabel(struct sock *sk)
1318 struct socket_smack *ssp;
1319 struct netlbl_lsm_secattr secattr;
1320 int rc;
1322 ssp = sk->sk_security;
1323 netlbl_secattr_init(&secattr);
1324 smack_to_secattr(ssp->smk_out, &secattr);
1325 rc = netlbl_sock_setattr(sk, &secattr);
1326 netlbl_secattr_destroy(&secattr);
1328 return rc;
1332 * smack_inode_setsecurity - set smack xattrs
1333 * @inode: the object
1334 * @name: attribute name
1335 * @value: attribute value
1336 * @size: size of the attribute
1337 * @flags: unused
1339 * Sets the named attribute in the appropriate blob
1341 * Returns 0 on success, or an error code
1343 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1344 const void *value, size_t size, int flags)
1346 char *sp;
1347 struct inode_smack *nsp = inode->i_security;
1348 struct socket_smack *ssp;
1349 struct socket *sock;
1350 int rc = 0;
1352 if (value == NULL || size > SMK_LABELLEN)
1353 return -EACCES;
1355 sp = smk_import(value, size);
1356 if (sp == NULL)
1357 return -EINVAL;
1359 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1360 nsp->smk_inode = sp;
1361 return 0;
1364 * The rest of the Smack xattrs are only on sockets.
1366 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1367 return -EOPNOTSUPP;
1369 sock = SOCKET_I(inode);
1370 if (sock == NULL || sock->sk == NULL)
1371 return -EOPNOTSUPP;
1373 ssp = sock->sk->sk_security;
1375 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1376 ssp->smk_in = sp;
1377 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1378 ssp->smk_out = sp;
1379 rc = smack_netlabel(sock->sk);
1380 if (rc != 0)
1381 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1382 __func__, -rc);
1383 } else
1384 return -EOPNOTSUPP;
1386 return 0;
1390 * smack_socket_post_create - finish socket setup
1391 * @sock: the socket
1392 * @family: protocol family
1393 * @type: unused
1394 * @protocol: unused
1395 * @kern: unused
1397 * Sets the netlabel information on the socket
1399 * Returns 0 on success, and error code otherwise
1401 static int smack_socket_post_create(struct socket *sock, int family,
1402 int type, int protocol, int kern)
1404 if (family != PF_INET || sock->sk == NULL)
1405 return 0;
1407 * Set the outbound netlbl.
1409 return smack_netlabel(sock->sk);
1413 * smack_flags_to_may - convert S_ to MAY_ values
1414 * @flags: the S_ value
1416 * Returns the equivalent MAY_ value
1418 static int smack_flags_to_may(int flags)
1420 int may = 0;
1422 if (flags & S_IRUGO)
1423 may |= MAY_READ;
1424 if (flags & S_IWUGO)
1425 may |= MAY_WRITE;
1426 if (flags & S_IXUGO)
1427 may |= MAY_EXEC;
1429 return may;
1433 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1434 * @msg: the object
1436 * Returns 0
1438 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1440 msg->security = current->security;
1441 return 0;
1445 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1446 * @msg: the object
1448 * Clears the blob pointer
1450 static void smack_msg_msg_free_security(struct msg_msg *msg)
1452 msg->security = NULL;
1456 * smack_of_shm - the smack pointer for the shm
1457 * @shp: the object
1459 * Returns a pointer to the smack value
1461 static char *smack_of_shm(struct shmid_kernel *shp)
1463 return (char *)shp->shm_perm.security;
1467 * smack_shm_alloc_security - Set the security blob for shm
1468 * @shp: the object
1470 * Returns 0
1472 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1474 struct kern_ipc_perm *isp = &shp->shm_perm;
1476 isp->security = current->security;
1477 return 0;
1481 * smack_shm_free_security - Clear the security blob for shm
1482 * @shp: the object
1484 * Clears the blob pointer
1486 static void smack_shm_free_security(struct shmid_kernel *shp)
1488 struct kern_ipc_perm *isp = &shp->shm_perm;
1490 isp->security = NULL;
1494 * smack_shm_associate - Smack access check for shm
1495 * @shp: the object
1496 * @shmflg: access requested
1498 * Returns 0 if current has the requested access, error code otherwise
1500 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1502 char *ssp = smack_of_shm(shp);
1503 int may;
1505 may = smack_flags_to_may(shmflg);
1506 return smk_curacc(ssp, may);
1510 * smack_shm_shmctl - Smack access check for shm
1511 * @shp: the object
1512 * @cmd: what it wants to do
1514 * Returns 0 if current has the requested access, error code otherwise
1516 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1518 char *ssp = smack_of_shm(shp);
1519 int may;
1521 switch (cmd) {
1522 case IPC_STAT:
1523 case SHM_STAT:
1524 may = MAY_READ;
1525 break;
1526 case IPC_SET:
1527 case SHM_LOCK:
1528 case SHM_UNLOCK:
1529 case IPC_RMID:
1530 may = MAY_READWRITE;
1531 break;
1532 case IPC_INFO:
1533 case SHM_INFO:
1535 * System level information.
1537 return 0;
1538 default:
1539 return -EINVAL;
1542 return smk_curacc(ssp, may);
1546 * smack_shm_shmat - Smack access for shmat
1547 * @shp: the object
1548 * @shmaddr: unused
1549 * @shmflg: access requested
1551 * Returns 0 if current has the requested access, error code otherwise
1553 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1554 int shmflg)
1556 char *ssp = smack_of_shm(shp);
1557 int may;
1559 may = smack_flags_to_may(shmflg);
1560 return smk_curacc(ssp, may);
1564 * smack_of_sem - the smack pointer for the sem
1565 * @sma: the object
1567 * Returns a pointer to the smack value
1569 static char *smack_of_sem(struct sem_array *sma)
1571 return (char *)sma->sem_perm.security;
1575 * smack_sem_alloc_security - Set the security blob for sem
1576 * @sma: the object
1578 * Returns 0
1580 static int smack_sem_alloc_security(struct sem_array *sma)
1582 struct kern_ipc_perm *isp = &sma->sem_perm;
1584 isp->security = current->security;
1585 return 0;
1589 * smack_sem_free_security - Clear the security blob for sem
1590 * @sma: the object
1592 * Clears the blob pointer
1594 static void smack_sem_free_security(struct sem_array *sma)
1596 struct kern_ipc_perm *isp = &sma->sem_perm;
1598 isp->security = NULL;
1602 * smack_sem_associate - Smack access check for sem
1603 * @sma: the object
1604 * @semflg: access requested
1606 * Returns 0 if current has the requested access, error code otherwise
1608 static int smack_sem_associate(struct sem_array *sma, int semflg)
1610 char *ssp = smack_of_sem(sma);
1611 int may;
1613 may = smack_flags_to_may(semflg);
1614 return smk_curacc(ssp, may);
1618 * smack_sem_shmctl - Smack access check for sem
1619 * @sma: the object
1620 * @cmd: what it wants to do
1622 * Returns 0 if current has the requested access, error code otherwise
1624 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1626 char *ssp = smack_of_sem(sma);
1627 int may;
1629 switch (cmd) {
1630 case GETPID:
1631 case GETNCNT:
1632 case GETZCNT:
1633 case GETVAL:
1634 case GETALL:
1635 case IPC_STAT:
1636 case SEM_STAT:
1637 may = MAY_READ;
1638 break;
1639 case SETVAL:
1640 case SETALL:
1641 case IPC_RMID:
1642 case IPC_SET:
1643 may = MAY_READWRITE;
1644 break;
1645 case IPC_INFO:
1646 case SEM_INFO:
1648 * System level information
1650 return 0;
1651 default:
1652 return -EINVAL;
1655 return smk_curacc(ssp, may);
1659 * smack_sem_semop - Smack checks of semaphore operations
1660 * @sma: the object
1661 * @sops: unused
1662 * @nsops: unused
1663 * @alter: unused
1665 * Treated as read and write in all cases.
1667 * Returns 0 if access is allowed, error code otherwise
1669 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1670 unsigned nsops, int alter)
1672 char *ssp = smack_of_sem(sma);
1674 return smk_curacc(ssp, MAY_READWRITE);
1678 * smack_msg_alloc_security - Set the security blob for msg
1679 * @msq: the object
1681 * Returns 0
1683 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1685 struct kern_ipc_perm *kisp = &msq->q_perm;
1687 kisp->security = current->security;
1688 return 0;
1692 * smack_msg_free_security - Clear the security blob for msg
1693 * @msq: the object
1695 * Clears the blob pointer
1697 static void smack_msg_queue_free_security(struct msg_queue *msq)
1699 struct kern_ipc_perm *kisp = &msq->q_perm;
1701 kisp->security = NULL;
1705 * smack_of_msq - the smack pointer for the msq
1706 * @msq: the object
1708 * Returns a pointer to the smack value
1710 static char *smack_of_msq(struct msg_queue *msq)
1712 return (char *)msq->q_perm.security;
1716 * smack_msg_queue_associate - Smack access check for msg_queue
1717 * @msq: the object
1718 * @msqflg: access requested
1720 * Returns 0 if current has the requested access, error code otherwise
1722 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1724 char *msp = smack_of_msq(msq);
1725 int may;
1727 may = smack_flags_to_may(msqflg);
1728 return smk_curacc(msp, may);
1732 * smack_msg_queue_msgctl - Smack access check for msg_queue
1733 * @msq: the object
1734 * @cmd: what it wants to do
1736 * Returns 0 if current has the requested access, error code otherwise
1738 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1740 char *msp = smack_of_msq(msq);
1741 int may;
1743 switch (cmd) {
1744 case IPC_STAT:
1745 case MSG_STAT:
1746 may = MAY_READ;
1747 break;
1748 case IPC_SET:
1749 case IPC_RMID:
1750 may = MAY_READWRITE;
1751 break;
1752 case IPC_INFO:
1753 case MSG_INFO:
1755 * System level information
1757 return 0;
1758 default:
1759 return -EINVAL;
1762 return smk_curacc(msp, may);
1766 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1767 * @msq: the object
1768 * @msg: unused
1769 * @msqflg: access requested
1771 * Returns 0 if current has the requested access, error code otherwise
1773 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1774 int msqflg)
1776 char *msp = smack_of_msq(msq);
1777 int rc;
1779 rc = smack_flags_to_may(msqflg);
1780 return smk_curacc(msp, rc);
1784 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1785 * @msq: the object
1786 * @msg: unused
1787 * @target: unused
1788 * @type: unused
1789 * @mode: unused
1791 * Returns 0 if current has read and write access, error code otherwise
1793 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1794 struct task_struct *target, long type, int mode)
1796 char *msp = smack_of_msq(msq);
1798 return smk_curacc(msp, MAY_READWRITE);
1802 * smack_ipc_permission - Smack access for ipc_permission()
1803 * @ipp: the object permissions
1804 * @flag: access requested
1806 * Returns 0 if current has read and write access, error code otherwise
1808 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1810 char *isp = ipp->security;
1811 int may;
1813 may = smack_flags_to_may(flag);
1814 return smk_curacc(isp, may);
1817 /* module stacking operations */
1820 * smack_register_security - stack capability module
1821 * @name: module name
1822 * @ops: module operations - ignored
1824 * Allow the capability module to register.
1826 static int smack_register_security(const char *name,
1827 struct security_operations *ops)
1829 if (strcmp(name, "capability") != 0)
1830 return -EINVAL;
1832 printk(KERN_INFO "%s: Registering secondary module %s\n",
1833 __func__, name);
1835 return 0;
1839 * smack_d_instantiate - Make sure the blob is correct on an inode
1840 * @opt_dentry: unused
1841 * @inode: the object
1843 * Set the inode's security blob if it hasn't been done already.
1845 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1847 struct super_block *sbp;
1848 struct superblock_smack *sbsp;
1849 struct inode_smack *isp;
1850 char *csp = current->security;
1851 char *fetched;
1852 char *final;
1853 struct dentry *dp;
1855 if (inode == NULL)
1856 return;
1858 isp = inode->i_security;
1860 mutex_lock(&isp->smk_lock);
1862 * If the inode is already instantiated
1863 * take the quick way out
1865 if (isp->smk_flags & SMK_INODE_INSTANT)
1866 goto unlockandout;
1868 sbp = inode->i_sb;
1869 sbsp = sbp->s_security;
1871 * We're going to use the superblock default label
1872 * if there's no label on the file.
1874 final = sbsp->smk_default;
1877 * This is pretty hackish.
1878 * Casey says that we shouldn't have to do
1879 * file system specific code, but it does help
1880 * with keeping it simple.
1882 switch (sbp->s_magic) {
1883 case SMACK_MAGIC:
1885 * Casey says that it's a little embarassing
1886 * that the smack file system doesn't do
1887 * extended attributes.
1889 final = smack_known_star.smk_known;
1890 break;
1891 case PIPEFS_MAGIC:
1893 * Casey says pipes are easy (?)
1895 final = smack_known_star.smk_known;
1896 break;
1897 case DEVPTS_SUPER_MAGIC:
1899 * devpts seems content with the label of the task.
1900 * Programs that change smack have to treat the
1901 * pty with respect.
1903 final = csp;
1904 break;
1905 case SOCKFS_MAGIC:
1907 * Casey says sockets get the smack of the task.
1909 final = csp;
1910 break;
1911 case PROC_SUPER_MAGIC:
1913 * Casey says procfs appears not to care.
1914 * The superblock default suffices.
1916 break;
1917 case TMPFS_MAGIC:
1919 * Device labels should come from the filesystem,
1920 * but watch out, because they're volitile,
1921 * getting recreated on every reboot.
1923 final = smack_known_star.smk_known;
1925 * No break.
1927 * If a smack value has been set we want to use it,
1928 * but since tmpfs isn't giving us the opportunity
1929 * to set mount options simulate setting the
1930 * superblock default.
1932 default:
1934 * This isn't an understood special case.
1935 * Get the value from the xattr.
1937 * No xattr support means, alas, no SMACK label.
1938 * Use the aforeapplied default.
1939 * It would be curious if the label of the task
1940 * does not match that assigned.
1942 if (inode->i_op->getxattr == NULL)
1943 break;
1945 * Get the dentry for xattr.
1947 if (opt_dentry == NULL) {
1948 dp = d_find_alias(inode);
1949 if (dp == NULL)
1950 break;
1951 } else {
1952 dp = dget(opt_dentry);
1953 if (dp == NULL)
1954 break;
1957 fetched = smk_fetch(inode, dp);
1958 if (fetched != NULL)
1959 final = fetched;
1961 dput(dp);
1962 break;
1965 if (final == NULL)
1966 isp->smk_inode = csp;
1967 else
1968 isp->smk_inode = final;
1970 isp->smk_flags |= SMK_INODE_INSTANT;
1972 unlockandout:
1973 mutex_unlock(&isp->smk_lock);
1974 return;
1978 * smack_getprocattr - Smack process attribute access
1979 * @p: the object task
1980 * @name: the name of the attribute in /proc/.../attr
1981 * @value: where to put the result
1983 * Places a copy of the task Smack into value
1985 * Returns the length of the smack label or an error code
1987 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
1989 char *cp;
1990 int slen;
1992 if (strcmp(name, "current") != 0)
1993 return -EINVAL;
1995 cp = kstrdup(p->security, GFP_KERNEL);
1996 if (cp == NULL)
1997 return -ENOMEM;
1999 slen = strlen(cp);
2000 *value = cp;
2001 return slen;
2005 * smack_setprocattr - Smack process attribute setting
2006 * @p: the object task
2007 * @name: the name of the attribute in /proc/.../attr
2008 * @value: the value to set
2009 * @size: the size of the value
2011 * Sets the Smack value of the task. Only setting self
2012 * is permitted and only with privilege
2014 * Returns the length of the smack label or an error code
2016 static int smack_setprocattr(struct task_struct *p, char *name,
2017 void *value, size_t size)
2019 char *newsmack;
2021 if (!__capable(p, CAP_MAC_ADMIN))
2022 return -EPERM;
2025 * Changing another process' Smack value is too dangerous
2026 * and supports no sane use case.
2028 if (p != current)
2029 return -EPERM;
2031 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2032 return -EINVAL;
2034 if (strcmp(name, "current") != 0)
2035 return -EINVAL;
2037 newsmack = smk_import(value, size);
2038 if (newsmack == NULL)
2039 return -EINVAL;
2041 p->security = newsmack;
2042 return size;
2046 * smack_unix_stream_connect - Smack access on UDS
2047 * @sock: one socket
2048 * @other: the other socket
2049 * @newsk: unused
2051 * Return 0 if a subject with the smack of sock could access
2052 * an object with the smack of other, otherwise an error code
2054 static int smack_unix_stream_connect(struct socket *sock,
2055 struct socket *other, struct sock *newsk)
2057 struct inode *sp = SOCK_INODE(sock);
2058 struct inode *op = SOCK_INODE(other);
2060 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2064 * smack_unix_may_send - Smack access on UDS
2065 * @sock: one socket
2066 * @other: the other socket
2068 * Return 0 if a subject with the smack of sock could access
2069 * an object with the smack of other, otherwise an error code
2071 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2073 struct inode *sp = SOCK_INODE(sock);
2074 struct inode *op = SOCK_INODE(other);
2076 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2080 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2081 * pair to smack
2082 * @sap: netlabel secattr
2083 * @sip: where to put the result
2085 * Copies a smack label into sip
2087 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2089 char smack[SMK_LABELLEN];
2090 int pcat;
2092 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2094 * If there are flags but no level netlabel isn't
2095 * behaving the way we expect it to.
2097 * Without guidance regarding the smack value
2098 * for the packet fall back on the network
2099 * ambient value.
2101 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2102 return;
2105 * Get the categories, if any
2107 memset(smack, '\0', SMK_LABELLEN);
2108 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2109 for (pcat = -1;;) {
2110 pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2111 pcat + 1);
2112 if (pcat < 0)
2113 break;
2114 smack_catset_bit(pcat, smack);
2117 * If it is CIPSO using smack direct mapping
2118 * we are already done. WeeHee.
2120 if (sap->attr.mls.lvl == smack_cipso_direct) {
2121 memcpy(sip, smack, SMK_MAXLEN);
2122 return;
2125 * Look it up in the supplied table if it is not a direct mapping.
2127 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2128 return;
2132 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2133 * @sk: socket
2134 * @skb: packet
2136 * Returns 0 if the packet should be delivered, an error code otherwise
2138 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2140 struct netlbl_lsm_secattr secattr;
2141 struct socket_smack *ssp = sk->sk_security;
2142 char smack[SMK_LABELLEN];
2143 int rc;
2145 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2146 return 0;
2149 * Translate what netlabel gave us.
2151 memset(smack, '\0', SMK_LABELLEN);
2152 netlbl_secattr_init(&secattr);
2153 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2154 if (rc == 0)
2155 smack_from_secattr(&secattr, smack);
2156 else
2157 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2158 netlbl_secattr_destroy(&secattr);
2160 * Receiving a packet requires that the other end
2161 * be able to write here. Read access is not required.
2162 * This is the simplist possible security model
2163 * for networking.
2165 return smk_access(smack, ssp->smk_in, MAY_WRITE);
2169 * smack_socket_getpeersec_stream - pull in packet label
2170 * @sock: the socket
2171 * @optval: user's destination
2172 * @optlen: size thereof
2173 * @len: max thereoe
2175 * returns zero on success, an error code otherwise
2177 static int smack_socket_getpeersec_stream(struct socket *sock,
2178 char __user *optval,
2179 int __user *optlen, unsigned len)
2181 struct socket_smack *ssp;
2182 int slen;
2183 int rc = 0;
2185 ssp = sock->sk->sk_security;
2186 slen = strlen(ssp->smk_packet) + 1;
2188 if (slen > len)
2189 rc = -ERANGE;
2190 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2191 rc = -EFAULT;
2193 if (put_user(slen, optlen) != 0)
2194 rc = -EFAULT;
2196 return rc;
2201 * smack_socket_getpeersec_dgram - pull in packet label
2202 * @sock: the socket
2203 * @skb: packet data
2204 * @secid: pointer to where to put the secid of the packet
2206 * Sets the netlabel socket state on sk from parent
2208 static int smack_socket_getpeersec_dgram(struct socket *sock,
2209 struct sk_buff *skb, u32 *secid)
2212 struct netlbl_lsm_secattr secattr;
2213 struct sock *sk;
2214 char smack[SMK_LABELLEN];
2215 int family = PF_INET;
2216 u32 s;
2217 int rc;
2220 * Only works for families with packets.
2222 if (sock != NULL) {
2223 sk = sock->sk;
2224 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2225 return 0;
2226 family = sk->sk_family;
2229 * Translate what netlabel gave us.
2231 memset(smack, '\0', SMK_LABELLEN);
2232 netlbl_secattr_init(&secattr);
2233 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2234 if (rc == 0)
2235 smack_from_secattr(&secattr, smack);
2236 netlbl_secattr_destroy(&secattr);
2239 * Give up if we couldn't get anything
2241 if (rc != 0)
2242 return rc;
2244 s = smack_to_secid(smack);
2245 if (s == 0)
2246 return -EINVAL;
2248 *secid = s;
2249 return 0;
2253 * smack_sock_graft - graft access state between two sockets
2254 * @sk: fresh sock
2255 * @parent: donor socket
2257 * Sets the netlabel socket state on sk from parent
2259 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2261 struct socket_smack *ssp;
2262 int rc;
2264 if (sk == NULL)
2265 return;
2267 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2268 return;
2270 ssp = sk->sk_security;
2271 ssp->smk_in = current->security;
2272 ssp->smk_out = current->security;
2273 ssp->smk_packet[0] = '\0';
2275 rc = smack_netlabel(sk);
2276 if (rc != 0)
2277 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2278 __func__, -rc);
2282 * smack_inet_conn_request - Smack access check on connect
2283 * @sk: socket involved
2284 * @skb: packet
2285 * @req: unused
2287 * Returns 0 if a task with the packet label could write to
2288 * the socket, otherwise an error code
2290 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2291 struct request_sock *req)
2293 struct netlbl_lsm_secattr skb_secattr;
2294 struct socket_smack *ssp = sk->sk_security;
2295 char smack[SMK_LABELLEN];
2296 int rc;
2298 if (skb == NULL)
2299 return -EACCES;
2301 memset(smack, '\0', SMK_LABELLEN);
2302 netlbl_secattr_init(&skb_secattr);
2303 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2304 if (rc == 0)
2305 smack_from_secattr(&skb_secattr, smack);
2306 else
2307 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2308 netlbl_secattr_destroy(&skb_secattr);
2310 * Receiving a packet requires that the other end
2311 * be able to write here. Read access is not required.
2313 * If the request is successful save the peer's label
2314 * so that SO_PEERCRED can report it.
2316 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2317 if (rc == 0)
2318 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2320 return rc;
2324 * Key management security hooks
2326 * Casey has not tested key support very heavily.
2327 * The permission check is most likely too restrictive.
2328 * If you care about keys please have a look.
2330 #ifdef CONFIG_KEYS
2333 * smack_key_alloc - Set the key security blob
2334 * @key: object
2335 * @tsk: the task associated with the key
2336 * @flags: unused
2338 * No allocation required
2340 * Returns 0
2342 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2343 unsigned long flags)
2345 key->security = tsk->security;
2346 return 0;
2350 * smack_key_free - Clear the key security blob
2351 * @key: the object
2353 * Clear the blob pointer
2355 static void smack_key_free(struct key *key)
2357 key->security = NULL;
2361 * smack_key_permission - Smack access on a key
2362 * @key_ref: gets to the object
2363 * @context: task involved
2364 * @perm: unused
2366 * Return 0 if the task has read and write to the object,
2367 * an error code otherwise
2369 static int smack_key_permission(key_ref_t key_ref,
2370 struct task_struct *context, key_perm_t perm)
2372 struct key *keyp;
2374 keyp = key_ref_to_ptr(key_ref);
2375 if (keyp == NULL)
2376 return -EINVAL;
2378 * If the key hasn't been initialized give it access so that
2379 * it may do so.
2381 if (keyp->security == NULL)
2382 return 0;
2384 * This should not occur
2386 if (context->security == NULL)
2387 return -EACCES;
2389 return smk_access(context->security, keyp->security, MAY_READWRITE);
2391 #endif /* CONFIG_KEYS */
2394 * smack_secid_to_secctx - return the smack label for a secid
2395 * @secid: incoming integer
2396 * @secdata: destination
2397 * @seclen: how long it is
2399 * Exists for networking code.
2401 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2403 char *sp = smack_from_secid(secid);
2405 *secdata = sp;
2406 *seclen = strlen(sp);
2407 return 0;
2411 * smack_secctx_to_secid - return the secid for a smack label
2412 * @secdata: smack label
2413 * @seclen: how long result is
2414 * @secid: outgoing integer
2416 * Exists for audit and networking code.
2418 static int smack_secctx_to_secid(char *secdata, u32 seclen, u32 *secid)
2420 *secid = smack_to_secid(secdata);
2421 return 0;
2425 * smack_release_secctx - don't do anything.
2426 * @key_ref: unused
2427 * @context: unused
2428 * @perm: unused
2430 * Exists to make sure nothing gets done, and properly
2432 static void smack_release_secctx(char *secdata, u32 seclen)
2436 static struct security_operations smack_ops = {
2437 .ptrace = smack_ptrace,
2438 .capget = cap_capget,
2439 .capset_check = cap_capset_check,
2440 .capset_set = cap_capset_set,
2441 .capable = cap_capable,
2442 .syslog = smack_syslog,
2443 .settime = cap_settime,
2444 .vm_enough_memory = cap_vm_enough_memory,
2446 .bprm_apply_creds = cap_bprm_apply_creds,
2447 .bprm_set_security = cap_bprm_set_security,
2448 .bprm_secureexec = cap_bprm_secureexec,
2450 .sb_alloc_security = smack_sb_alloc_security,
2451 .sb_free_security = smack_sb_free_security,
2452 .sb_copy_data = smack_sb_copy_data,
2453 .sb_kern_mount = smack_sb_kern_mount,
2454 .sb_statfs = smack_sb_statfs,
2455 .sb_mount = smack_sb_mount,
2456 .sb_umount = smack_sb_umount,
2458 .inode_alloc_security = smack_inode_alloc_security,
2459 .inode_free_security = smack_inode_free_security,
2460 .inode_init_security = smack_inode_init_security,
2461 .inode_link = smack_inode_link,
2462 .inode_unlink = smack_inode_unlink,
2463 .inode_rmdir = smack_inode_rmdir,
2464 .inode_rename = smack_inode_rename,
2465 .inode_permission = smack_inode_permission,
2466 .inode_setattr = smack_inode_setattr,
2467 .inode_getattr = smack_inode_getattr,
2468 .inode_setxattr = smack_inode_setxattr,
2469 .inode_post_setxattr = smack_inode_post_setxattr,
2470 .inode_getxattr = smack_inode_getxattr,
2471 .inode_removexattr = smack_inode_removexattr,
2472 .inode_need_killpriv = cap_inode_need_killpriv,
2473 .inode_killpriv = cap_inode_killpriv,
2474 .inode_getsecurity = smack_inode_getsecurity,
2475 .inode_setsecurity = smack_inode_setsecurity,
2476 .inode_listsecurity = smack_inode_listsecurity,
2478 .file_permission = smack_file_permission,
2479 .file_alloc_security = smack_file_alloc_security,
2480 .file_free_security = smack_file_free_security,
2481 .file_ioctl = smack_file_ioctl,
2482 .file_lock = smack_file_lock,
2483 .file_fcntl = smack_file_fcntl,
2484 .file_set_fowner = smack_file_set_fowner,
2485 .file_send_sigiotask = smack_file_send_sigiotask,
2486 .file_receive = smack_file_receive,
2488 .task_alloc_security = smack_task_alloc_security,
2489 .task_free_security = smack_task_free_security,
2490 .task_post_setuid = cap_task_post_setuid,
2491 .task_setpgid = smack_task_setpgid,
2492 .task_getpgid = smack_task_getpgid,
2493 .task_getsid = smack_task_getsid,
2494 .task_getsecid = smack_task_getsecid,
2495 .task_setnice = smack_task_setnice,
2496 .task_setioprio = smack_task_setioprio,
2497 .task_getioprio = smack_task_getioprio,
2498 .task_setscheduler = smack_task_setscheduler,
2499 .task_getscheduler = smack_task_getscheduler,
2500 .task_movememory = smack_task_movememory,
2501 .task_kill = smack_task_kill,
2502 .task_wait = smack_task_wait,
2503 .task_reparent_to_init = cap_task_reparent_to_init,
2504 .task_to_inode = smack_task_to_inode,
2506 .ipc_permission = smack_ipc_permission,
2508 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2509 .msg_msg_free_security = smack_msg_msg_free_security,
2511 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2512 .msg_queue_free_security = smack_msg_queue_free_security,
2513 .msg_queue_associate = smack_msg_queue_associate,
2514 .msg_queue_msgctl = smack_msg_queue_msgctl,
2515 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2516 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2518 .shm_alloc_security = smack_shm_alloc_security,
2519 .shm_free_security = smack_shm_free_security,
2520 .shm_associate = smack_shm_associate,
2521 .shm_shmctl = smack_shm_shmctl,
2522 .shm_shmat = smack_shm_shmat,
2524 .sem_alloc_security = smack_sem_alloc_security,
2525 .sem_free_security = smack_sem_free_security,
2526 .sem_associate = smack_sem_associate,
2527 .sem_semctl = smack_sem_semctl,
2528 .sem_semop = smack_sem_semop,
2530 .netlink_send = cap_netlink_send,
2531 .netlink_recv = cap_netlink_recv,
2533 .register_security = smack_register_security,
2535 .d_instantiate = smack_d_instantiate,
2537 .getprocattr = smack_getprocattr,
2538 .setprocattr = smack_setprocattr,
2540 .unix_stream_connect = smack_unix_stream_connect,
2541 .unix_may_send = smack_unix_may_send,
2543 .socket_post_create = smack_socket_post_create,
2544 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2545 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2546 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2547 .sk_alloc_security = smack_sk_alloc_security,
2548 .sk_free_security = smack_sk_free_security,
2549 .sock_graft = smack_sock_graft,
2550 .inet_conn_request = smack_inet_conn_request,
2551 /* key management security hooks */
2552 #ifdef CONFIG_KEYS
2553 .key_alloc = smack_key_alloc,
2554 .key_free = smack_key_free,
2555 .key_permission = smack_key_permission,
2556 #endif /* CONFIG_KEYS */
2557 .secid_to_secctx = smack_secid_to_secctx,
2558 .secctx_to_secid = smack_secctx_to_secid,
2559 .release_secctx = smack_release_secctx,
2563 * smack_init - initialize the smack system
2565 * Returns 0
2567 static __init int smack_init(void)
2569 printk(KERN_INFO "Smack: Initializing.\n");
2572 * Set the security state for the initial task.
2574 current->security = &smack_known_floor.smk_known;
2577 * Initialize locks
2579 spin_lock_init(&smack_known_unset.smk_cipsolock);
2580 spin_lock_init(&smack_known_huh.smk_cipsolock);
2581 spin_lock_init(&smack_known_hat.smk_cipsolock);
2582 spin_lock_init(&smack_known_star.smk_cipsolock);
2583 spin_lock_init(&smack_known_floor.smk_cipsolock);
2584 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2587 * Register with LSM
2589 if (register_security(&smack_ops))
2590 panic("smack: Unable to register with kernel.\n");
2592 return 0;
2596 * Smack requires early initialization in order to label
2597 * all processes and objects when they are created.
2599 security_initcall(smack_init);