sound: seq_midi_event: fix decoding of (N)RPN events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / smack / smack_lsm.c
blobb4e811bb45070d6212843c12cb6c5def12113c2e
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>
10 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
11 * Paul Moore <paul.moore@hp.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2,
15 * as published by the Free Software Foundation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/kd.h>
24 #include <asm/ioctls.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/mutex.h>
29 #include <linux/pipe_fs_i.h>
30 #include <net/netlabel.h>
31 #include <net/cipso_ipv4.h>
32 #include <linux/audit.h>
34 #include "smack.h"
36 #define task_security(task) (task_cred_xxx((task), security))
39 * I hope these are the hokeyist lines of code in the module. Casey.
41 #define DEVPTS_SUPER_MAGIC 0x1cd1
42 #define SOCKFS_MAGIC 0x534F434B
43 #define TMPFS_MAGIC 0x01021994
45 /**
46 * smk_fetch - Fetch the smack label from a file.
47 * @ip: a pointer to the inode
48 * @dp: a pointer to the dentry
50 * Returns a pointer to the master list entry for the Smack label
51 * or NULL if there was no label to fetch.
53 static char *smk_fetch(struct inode *ip, struct dentry *dp)
55 int rc;
56 char in[SMK_LABELLEN];
58 if (ip->i_op->getxattr == NULL)
59 return NULL;
61 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
62 if (rc < 0)
63 return NULL;
65 return smk_import(in, rc);
68 /**
69 * new_inode_smack - allocate an inode security blob
70 * @smack: a pointer to the Smack label to use in the blob
72 * Returns the new blob or NULL if there's no memory available
74 struct inode_smack *new_inode_smack(char *smack)
76 struct inode_smack *isp;
78 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
79 if (isp == NULL)
80 return NULL;
82 isp->smk_inode = smack;
83 isp->smk_flags = 0;
84 mutex_init(&isp->smk_lock);
86 return isp;
90 * LSM hooks.
91 * We he, that is fun!
94 /**
95 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
96 * @ctp: child task pointer
98 * Returns 0 if access is OK, an error code otherwise
100 * Do the capability checks, and require read and write.
102 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
104 int rc;
106 rc = cap_ptrace_may_access(ctp, mode);
107 if (rc != 0)
108 return rc;
110 rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
111 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
112 return 0;
113 return rc;
117 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
118 * @ptp: parent task pointer
120 * Returns 0 if access is OK, an error code otherwise
122 * Do the capability checks, and require read and write.
124 static int smack_ptrace_traceme(struct task_struct *ptp)
126 int rc;
128 rc = cap_ptrace_traceme(ptp);
129 if (rc != 0)
130 return rc;
132 rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
133 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
134 return 0;
135 return rc;
139 * smack_syslog - Smack approval on syslog
140 * @type: message type
142 * Require that the task has the floor label
144 * Returns 0 on success, error code otherwise.
146 static int smack_syslog(int type)
148 int rc;
149 char *sp = current_security();
151 rc = cap_syslog(type);
152 if (rc != 0)
153 return rc;
155 if (capable(CAP_MAC_OVERRIDE))
156 return 0;
158 if (sp != smack_known_floor.smk_known)
159 rc = -EACCES;
161 return rc;
166 * Superblock Hooks.
170 * smack_sb_alloc_security - allocate a superblock blob
171 * @sb: the superblock getting the blob
173 * Returns 0 on success or -ENOMEM on error.
175 static int smack_sb_alloc_security(struct super_block *sb)
177 struct superblock_smack *sbsp;
179 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
181 if (sbsp == NULL)
182 return -ENOMEM;
184 sbsp->smk_root = smack_known_floor.smk_known;
185 sbsp->smk_default = smack_known_floor.smk_known;
186 sbsp->smk_floor = smack_known_floor.smk_known;
187 sbsp->smk_hat = smack_known_hat.smk_known;
188 sbsp->smk_initialized = 0;
189 spin_lock_init(&sbsp->smk_sblock);
191 sb->s_security = sbsp;
193 return 0;
197 * smack_sb_free_security - free a superblock blob
198 * @sb: the superblock getting the blob
201 static void smack_sb_free_security(struct super_block *sb)
203 kfree(sb->s_security);
204 sb->s_security = NULL;
208 * smack_sb_copy_data - copy mount options data for processing
209 * @type: file system type
210 * @orig: where to start
211 * @smackopts
213 * Returns 0 on success or -ENOMEM on error.
215 * Copy the Smack specific mount options out of the mount
216 * options list.
218 static int smack_sb_copy_data(char *orig, char *smackopts)
220 char *cp, *commap, *otheropts, *dp;
222 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
223 if (otheropts == NULL)
224 return -ENOMEM;
226 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
227 if (strstr(cp, SMK_FSDEFAULT) == cp)
228 dp = smackopts;
229 else if (strstr(cp, SMK_FSFLOOR) == cp)
230 dp = smackopts;
231 else if (strstr(cp, SMK_FSHAT) == cp)
232 dp = smackopts;
233 else if (strstr(cp, SMK_FSROOT) == cp)
234 dp = smackopts;
235 else
236 dp = otheropts;
238 commap = strchr(cp, ',');
239 if (commap != NULL)
240 *commap = '\0';
242 if (*dp != '\0')
243 strcat(dp, ",");
244 strcat(dp, cp);
247 strcpy(orig, otheropts);
248 free_page((unsigned long)otheropts);
250 return 0;
254 * smack_sb_kern_mount - Smack specific mount processing
255 * @sb: the file system superblock
256 * @flags: the mount flags
257 * @data: the smack mount options
259 * Returns 0 on success, an error code on failure
261 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
263 struct dentry *root = sb->s_root;
264 struct inode *inode = root->d_inode;
265 struct superblock_smack *sp = sb->s_security;
266 struct inode_smack *isp;
267 char *op;
268 char *commap;
269 char *nsp;
271 spin_lock(&sp->smk_sblock);
272 if (sp->smk_initialized != 0) {
273 spin_unlock(&sp->smk_sblock);
274 return 0;
276 sp->smk_initialized = 1;
277 spin_unlock(&sp->smk_sblock);
279 for (op = data; op != NULL; op = commap) {
280 commap = strchr(op, ',');
281 if (commap != NULL)
282 *commap++ = '\0';
284 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
285 op += strlen(SMK_FSHAT);
286 nsp = smk_import(op, 0);
287 if (nsp != NULL)
288 sp->smk_hat = nsp;
289 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
290 op += strlen(SMK_FSFLOOR);
291 nsp = smk_import(op, 0);
292 if (nsp != NULL)
293 sp->smk_floor = nsp;
294 } else if (strncmp(op, SMK_FSDEFAULT,
295 strlen(SMK_FSDEFAULT)) == 0) {
296 op += strlen(SMK_FSDEFAULT);
297 nsp = smk_import(op, 0);
298 if (nsp != NULL)
299 sp->smk_default = nsp;
300 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
301 op += strlen(SMK_FSROOT);
302 nsp = smk_import(op, 0);
303 if (nsp != NULL)
304 sp->smk_root = nsp;
309 * Initialize the root inode.
311 isp = inode->i_security;
312 if (isp == NULL)
313 inode->i_security = new_inode_smack(sp->smk_root);
314 else
315 isp->smk_inode = sp->smk_root;
317 return 0;
321 * smack_sb_statfs - Smack check on statfs
322 * @dentry: identifies the file system in question
324 * Returns 0 if current can read the floor of the filesystem,
325 * and error code otherwise
327 static int smack_sb_statfs(struct dentry *dentry)
329 struct superblock_smack *sbp = dentry->d_sb->s_security;
331 return smk_curacc(sbp->smk_floor, MAY_READ);
335 * smack_sb_mount - Smack check for mounting
336 * @dev_name: unused
337 * @nd: mount point
338 * @type: unused
339 * @flags: unused
340 * @data: unused
342 * Returns 0 if current can write the floor of the filesystem
343 * being mounted on, an error code otherwise.
345 static int smack_sb_mount(char *dev_name, struct path *path,
346 char *type, unsigned long flags, void *data)
348 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
350 return smk_curacc(sbp->smk_floor, MAY_WRITE);
354 * smack_sb_umount - Smack check for unmounting
355 * @mnt: file system to unmount
356 * @flags: unused
358 * Returns 0 if current can write the floor of the filesystem
359 * being unmounted, an error code otherwise.
361 static int smack_sb_umount(struct vfsmount *mnt, int flags)
363 struct superblock_smack *sbp;
365 sbp = mnt->mnt_sb->s_security;
367 return smk_curacc(sbp->smk_floor, MAY_WRITE);
371 * Inode hooks
375 * smack_inode_alloc_security - allocate an inode blob
376 * @inode - the inode in need of a blob
378 * Returns 0 if it gets a blob, -ENOMEM otherwise
380 static int smack_inode_alloc_security(struct inode *inode)
382 inode->i_security = new_inode_smack(current_security());
383 if (inode->i_security == NULL)
384 return -ENOMEM;
385 return 0;
389 * smack_inode_free_security - free an inode blob
390 * @inode - the inode with a blob
392 * Clears the blob pointer in inode
394 static void smack_inode_free_security(struct inode *inode)
396 kfree(inode->i_security);
397 inode->i_security = NULL;
401 * smack_inode_init_security - copy out the smack from an inode
402 * @inode: the inode
403 * @dir: unused
404 * @name: where to put the attribute name
405 * @value: where to put the attribute value
406 * @len: where to put the length of the attribute
408 * Returns 0 if it all works out, -ENOMEM if there's no memory
410 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
411 char **name, void **value, size_t *len)
413 char *isp = smk_of_inode(inode);
415 if (name) {
416 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
417 if (*name == NULL)
418 return -ENOMEM;
421 if (value) {
422 *value = kstrdup(isp, GFP_KERNEL);
423 if (*value == NULL)
424 return -ENOMEM;
427 if (len)
428 *len = strlen(isp) + 1;
430 return 0;
434 * smack_inode_link - Smack check on link
435 * @old_dentry: the existing object
436 * @dir: unused
437 * @new_dentry: the new object
439 * Returns 0 if access is permitted, an error code otherwise
441 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
442 struct dentry *new_dentry)
444 int rc;
445 char *isp;
447 isp = smk_of_inode(old_dentry->d_inode);
448 rc = smk_curacc(isp, MAY_WRITE);
450 if (rc == 0 && new_dentry->d_inode != NULL) {
451 isp = smk_of_inode(new_dentry->d_inode);
452 rc = smk_curacc(isp, MAY_WRITE);
455 return rc;
459 * smack_inode_unlink - Smack check on inode deletion
460 * @dir: containing directory object
461 * @dentry: file to unlink
463 * Returns 0 if current can write the containing directory
464 * and the object, error code otherwise
466 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
468 struct inode *ip = dentry->d_inode;
469 int rc;
472 * You need write access to the thing you're unlinking
474 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
475 if (rc == 0)
477 * You also need write access to the containing directory
479 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
481 return rc;
485 * smack_inode_rmdir - Smack check on directory deletion
486 * @dir: containing directory object
487 * @dentry: directory to unlink
489 * Returns 0 if current can write the containing directory
490 * and the directory, error code otherwise
492 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
494 int rc;
497 * You need write access to the thing you're removing
499 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
500 if (rc == 0)
502 * You also need write access to the containing directory
504 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
506 return rc;
510 * smack_inode_rename - Smack check on rename
511 * @old_inode: the old directory
512 * @old_dentry: unused
513 * @new_inode: the new directory
514 * @new_dentry: unused
516 * Read and write access is required on both the old and
517 * new directories.
519 * Returns 0 if access is permitted, an error code otherwise
521 static int smack_inode_rename(struct inode *old_inode,
522 struct dentry *old_dentry,
523 struct inode *new_inode,
524 struct dentry *new_dentry)
526 int rc;
527 char *isp;
529 isp = smk_of_inode(old_dentry->d_inode);
530 rc = smk_curacc(isp, MAY_READWRITE);
532 if (rc == 0 && new_dentry->d_inode != NULL) {
533 isp = smk_of_inode(new_dentry->d_inode);
534 rc = smk_curacc(isp, MAY_READWRITE);
537 return rc;
541 * smack_inode_permission - Smack version of permission()
542 * @inode: the inode in question
543 * @mask: the access requested
544 * @nd: unused
546 * This is the important Smack hook.
548 * Returns 0 if access is permitted, -EACCES otherwise
550 static int smack_inode_permission(struct inode *inode, int mask)
553 * No permission to check. Existence test. Yup, it's there.
555 if (mask == 0)
556 return 0;
558 return smk_curacc(smk_of_inode(inode), mask);
562 * smack_inode_setattr - Smack check for setting attributes
563 * @dentry: the object
564 * @iattr: for the force flag
566 * Returns 0 if access is permitted, an error code otherwise
568 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
571 * Need to allow for clearing the setuid bit.
573 if (iattr->ia_valid & ATTR_FORCE)
574 return 0;
576 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
580 * smack_inode_getattr - Smack check for getting attributes
581 * @mnt: unused
582 * @dentry: the object
584 * Returns 0 if access is permitted, an error code otherwise
586 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
588 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
592 * smack_inode_setxattr - Smack check for setting xattrs
593 * @dentry: the object
594 * @name: name of the attribute
595 * @value: unused
596 * @size: unused
597 * @flags: unused
599 * This protects the Smack attribute explicitly.
601 * Returns 0 if access is permitted, an error code otherwise
603 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
604 const void *value, size_t size, int flags)
606 int rc = 0;
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))
612 rc = -EPERM;
613 if (size == 0)
614 rc = -EINVAL;
615 } else
616 rc = cap_inode_setxattr(dentry, name, value, size, flags);
618 if (rc == 0)
619 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
621 return rc;
625 * smack_inode_post_setxattr - Apply the Smack update approved above
626 * @dentry: object
627 * @name: attribute name
628 * @value: attribute value
629 * @size: attribute size
630 * @flags: unused
632 * Set the pointer in the inode blob to the entry found
633 * in the master label list.
635 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
636 const void *value, size_t size, int flags)
638 struct inode_smack *isp;
639 char *nsp;
642 * Not SMACK
644 if (strcmp(name, XATTR_NAME_SMACK))
645 return;
647 if (size >= SMK_LABELLEN)
648 return;
650 isp = dentry->d_inode->i_security;
653 * No locking is done here. This is a pointer
654 * assignment.
656 nsp = smk_import(value, size);
657 if (nsp != NULL)
658 isp->smk_inode = nsp;
659 else
660 isp->smk_inode = smack_known_invalid.smk_known;
662 return;
666 * smack_inode_getxattr - Smack check on getxattr
667 * @dentry: the object
668 * @name: unused
670 * Returns 0 if access is permitted, an error code otherwise
672 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
674 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
678 * smack_inode_removexattr - Smack check on removexattr
679 * @dentry: the object
680 * @name: name of the attribute
682 * Removing the Smack attribute requires CAP_MAC_ADMIN
684 * Returns 0 if access is permitted, an error code otherwise
686 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
688 int rc = 0;
690 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
691 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
692 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
693 if (!capable(CAP_MAC_ADMIN))
694 rc = -EPERM;
695 } else
696 rc = cap_inode_removexattr(dentry, name);
698 if (rc == 0)
699 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
701 return rc;
705 * smack_inode_getsecurity - get smack xattrs
706 * @inode: the object
707 * @name: attribute name
708 * @buffer: where to put the result
709 * @size: size of the buffer
710 * @err: unused
712 * Returns the size of the attribute or an error code
714 static int smack_inode_getsecurity(const struct inode *inode,
715 const char *name, void **buffer,
716 bool alloc)
718 struct socket_smack *ssp;
719 struct socket *sock;
720 struct super_block *sbp;
721 struct inode *ip = (struct inode *)inode;
722 char *isp;
723 int ilen;
724 int rc = 0;
726 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
727 isp = smk_of_inode(inode);
728 ilen = strlen(isp) + 1;
729 *buffer = isp;
730 return ilen;
734 * The rest of the Smack xattrs are only on sockets.
736 sbp = ip->i_sb;
737 if (sbp->s_magic != SOCKFS_MAGIC)
738 return -EOPNOTSUPP;
740 sock = SOCKET_I(ip);
741 if (sock == NULL || sock->sk == NULL)
742 return -EOPNOTSUPP;
744 ssp = sock->sk->sk_security;
746 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
747 isp = ssp->smk_in;
748 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
749 isp = ssp->smk_out;
750 else
751 return -EOPNOTSUPP;
753 ilen = strlen(isp) + 1;
754 if (rc == 0) {
755 *buffer = isp;
756 rc = ilen;
759 return rc;
764 * smack_inode_listsecurity - list the Smack attributes
765 * @inode: the object
766 * @buffer: where they go
767 * @buffer_size: size of buffer
769 * Returns 0 on success, -EINVAL otherwise
771 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
772 size_t buffer_size)
774 int len = strlen(XATTR_NAME_SMACK);
776 if (buffer != NULL && len <= buffer_size) {
777 memcpy(buffer, XATTR_NAME_SMACK, len);
778 return len;
780 return -EINVAL;
784 * smack_inode_getsecid - Extract inode's security id
785 * @inode: inode to extract the info from
786 * @secid: where result will be saved
788 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
790 struct inode_smack *isp = inode->i_security;
792 *secid = smack_to_secid(isp->smk_inode);
796 * File Hooks
800 * smack_file_permission - Smack check on file operations
801 * @file: unused
802 * @mask: unused
804 * Returns 0
806 * Should access checks be done on each read or write?
807 * UNICOS and SELinux say yes.
808 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
810 * I'll say no for now. Smack does not do the frequent
811 * label changing that SELinux does.
813 static int smack_file_permission(struct file *file, int mask)
815 return 0;
819 * smack_file_alloc_security - assign a file security blob
820 * @file: the object
822 * The security blob for a file is a pointer to the master
823 * label list, so no allocation is done.
825 * Returns 0
827 static int smack_file_alloc_security(struct file *file)
829 file->f_security = current_security();
830 return 0;
834 * smack_file_free_security - clear a file security blob
835 * @file: the object
837 * The security blob for a file is a pointer to the master
838 * label list, so no memory is freed.
840 static void smack_file_free_security(struct file *file)
842 file->f_security = NULL;
846 * smack_file_ioctl - Smack check on ioctls
847 * @file: the object
848 * @cmd: what to do
849 * @arg: unused
851 * Relies heavily on the correct use of the ioctl command conventions.
853 * Returns 0 if allowed, error code otherwise
855 static int smack_file_ioctl(struct file *file, unsigned int cmd,
856 unsigned long arg)
858 int rc = 0;
860 if (_IOC_DIR(cmd) & _IOC_WRITE)
861 rc = smk_curacc(file->f_security, MAY_WRITE);
863 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
864 rc = smk_curacc(file->f_security, MAY_READ);
866 return rc;
870 * smack_file_lock - Smack check on file locking
871 * @file: the object
872 * @cmd unused
874 * Returns 0 if current has write access, error code otherwise
876 static int smack_file_lock(struct file *file, unsigned int cmd)
878 return smk_curacc(file->f_security, MAY_WRITE);
882 * smack_file_fcntl - Smack check on fcntl
883 * @file: the object
884 * @cmd: what action to check
885 * @arg: unused
887 * Returns 0 if current has access, error code otherwise
889 static int smack_file_fcntl(struct file *file, unsigned int cmd,
890 unsigned long arg)
892 int rc;
894 switch (cmd) {
895 case F_DUPFD:
896 case F_GETFD:
897 case F_GETFL:
898 case F_GETLK:
899 case F_GETOWN:
900 case F_GETSIG:
901 rc = smk_curacc(file->f_security, MAY_READ);
902 break;
903 case F_SETFD:
904 case F_SETFL:
905 case F_SETLK:
906 case F_SETLKW:
907 case F_SETOWN:
908 case F_SETSIG:
909 rc = smk_curacc(file->f_security, MAY_WRITE);
910 break;
911 default:
912 rc = smk_curacc(file->f_security, MAY_READWRITE);
915 return rc;
919 * smack_file_set_fowner - set the file security blob value
920 * @file: object in question
922 * Returns 0
923 * Further research may be required on this one.
925 static int smack_file_set_fowner(struct file *file)
927 file->f_security = current_security();
928 return 0;
932 * smack_file_send_sigiotask - Smack on sigio
933 * @tsk: The target task
934 * @fown: the object the signal come from
935 * @signum: unused
937 * Allow a privileged task to get signals even if it shouldn't
939 * Returns 0 if a subject with the object's smack could
940 * write to the task, an error code otherwise.
942 static int smack_file_send_sigiotask(struct task_struct *tsk,
943 struct fown_struct *fown, int signum)
945 struct file *file;
946 int rc;
949 * struct fown_struct is never outside the context of a struct file
951 file = container_of(fown, struct file, f_owner);
952 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
953 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
954 return 0;
955 return rc;
959 * smack_file_receive - Smack file receive check
960 * @file: the object
962 * Returns 0 if current has access, error code otherwise
964 static int smack_file_receive(struct file *file)
966 int may = 0;
969 * This code relies on bitmasks.
971 if (file->f_mode & FMODE_READ)
972 may = MAY_READ;
973 if (file->f_mode & FMODE_WRITE)
974 may |= MAY_WRITE;
976 return smk_curacc(file->f_security, may);
980 * Task hooks
984 * smack_cred_free - "free" task-level security credentials
985 * @cred: the credentials in question
987 * Smack isn't using copies of blobs. Everyone
988 * points to an immutable list. The blobs never go away.
989 * There is no leak here.
991 static void smack_cred_free(struct cred *cred)
993 cred->security = NULL;
997 * smack_cred_prepare - prepare new set of credentials for modification
998 * @new: the new credentials
999 * @old: the original credentials
1000 * @gfp: the atomicity of any memory allocations
1002 * Prepare a new set of credentials for modification.
1004 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1005 gfp_t gfp)
1007 new->security = old->security;
1008 return 0;
1012 * commit new credentials
1013 * @new: the new credentials
1014 * @old: the original credentials
1016 static void smack_cred_commit(struct cred *new, const struct cred *old)
1021 * smack_kernel_act_as - Set the subjective context in a set of credentials
1022 * @new points to the set of credentials to be modified.
1023 * @secid specifies the security ID to be set
1025 * Set the security data for a kernel service.
1027 static int smack_kernel_act_as(struct cred *new, u32 secid)
1029 char *smack = smack_from_secid(secid);
1031 if (smack == NULL)
1032 return -EINVAL;
1034 new->security = smack;
1035 return 0;
1039 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1040 * @new points to the set of credentials to be modified
1041 * @inode points to the inode to use as a reference
1043 * Set the file creation context in a set of credentials to the same
1044 * as the objective context of the specified inode
1046 static int smack_kernel_create_files_as(struct cred *new,
1047 struct inode *inode)
1049 struct inode_smack *isp = inode->i_security;
1051 new->security = isp->smk_inode;
1052 return 0;
1056 * smack_task_setpgid - Smack check on setting pgid
1057 * @p: the task object
1058 * @pgid: unused
1060 * Return 0 if write access is permitted
1062 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1064 return smk_curacc(task_security(p), MAY_WRITE);
1068 * smack_task_getpgid - Smack access check for getpgid
1069 * @p: the object task
1071 * Returns 0 if current can read the object task, error code otherwise
1073 static int smack_task_getpgid(struct task_struct *p)
1075 return smk_curacc(task_security(p), MAY_READ);
1079 * smack_task_getsid - Smack access check for getsid
1080 * @p: the object task
1082 * Returns 0 if current can read the object task, error code otherwise
1084 static int smack_task_getsid(struct task_struct *p)
1086 return smk_curacc(task_security(p), MAY_READ);
1090 * smack_task_getsecid - get the secid of the task
1091 * @p: the object task
1092 * @secid: where to put the result
1094 * Sets the secid to contain a u32 version of the smack label.
1096 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1098 *secid = smack_to_secid(task_security(p));
1102 * smack_task_setnice - Smack check on setting nice
1103 * @p: the task object
1104 * @nice: unused
1106 * Return 0 if write access is permitted
1108 static int smack_task_setnice(struct task_struct *p, int nice)
1110 int rc;
1112 rc = cap_task_setnice(p, nice);
1113 if (rc == 0)
1114 rc = smk_curacc(task_security(p), MAY_WRITE);
1115 return rc;
1119 * smack_task_setioprio - Smack check on setting ioprio
1120 * @p: the task object
1121 * @ioprio: unused
1123 * Return 0 if write access is permitted
1125 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1127 int rc;
1129 rc = cap_task_setioprio(p, ioprio);
1130 if (rc == 0)
1131 rc = smk_curacc(task_security(p), MAY_WRITE);
1132 return rc;
1136 * smack_task_getioprio - Smack check on reading ioprio
1137 * @p: the task object
1139 * Return 0 if read access is permitted
1141 static int smack_task_getioprio(struct task_struct *p)
1143 return smk_curacc(task_security(p), MAY_READ);
1147 * smack_task_setscheduler - Smack check on setting scheduler
1148 * @p: the task object
1149 * @policy: unused
1150 * @lp: unused
1152 * Return 0 if read access is permitted
1154 static int smack_task_setscheduler(struct task_struct *p, int policy,
1155 struct sched_param *lp)
1157 int rc;
1159 rc = cap_task_setscheduler(p, policy, lp);
1160 if (rc == 0)
1161 rc = smk_curacc(task_security(p), MAY_WRITE);
1162 return rc;
1166 * smack_task_getscheduler - Smack check on reading scheduler
1167 * @p: the task object
1169 * Return 0 if read access is permitted
1171 static int smack_task_getscheduler(struct task_struct *p)
1173 return smk_curacc(task_security(p), MAY_READ);
1177 * smack_task_movememory - Smack check on moving memory
1178 * @p: the task object
1180 * Return 0 if write access is permitted
1182 static int smack_task_movememory(struct task_struct *p)
1184 return smk_curacc(task_security(p), MAY_WRITE);
1188 * smack_task_kill - Smack check on signal delivery
1189 * @p: the task object
1190 * @info: unused
1191 * @sig: unused
1192 * @secid: identifies the smack to use in lieu of current's
1194 * Return 0 if write access is permitted
1196 * The secid behavior is an artifact of an SELinux hack
1197 * in the USB code. Someday it may go away.
1199 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1200 int sig, u32 secid)
1203 * Sending a signal requires that the sender
1204 * can write the receiver.
1206 if (secid == 0)
1207 return smk_curacc(task_security(p), MAY_WRITE);
1209 * If the secid isn't 0 we're dealing with some USB IO
1210 * specific behavior. This is not clean. For one thing
1211 * we can't take privilege into account.
1213 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1217 * smack_task_wait - Smack access check for waiting
1218 * @p: task to wait for
1220 * Returns 0 if current can wait for p, error code otherwise
1222 static int smack_task_wait(struct task_struct *p)
1224 int rc;
1226 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1227 if (rc == 0)
1228 return 0;
1231 * Allow the operation to succeed if either task
1232 * has privilege to perform operations that might
1233 * account for the smack labels having gotten to
1234 * be different in the first place.
1236 * This breaks the strict subject/object access
1237 * control ideal, taking the object's privilege
1238 * state into account in the decision as well as
1239 * the smack value.
1241 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1242 return 0;
1244 return rc;
1248 * smack_task_to_inode - copy task smack into the inode blob
1249 * @p: task to copy from
1250 * inode: inode to copy to
1252 * Sets the smack pointer in the inode security blob
1254 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1256 struct inode_smack *isp = inode->i_security;
1257 isp->smk_inode = task_security(p);
1261 * Socket hooks.
1265 * smack_sk_alloc_security - Allocate a socket blob
1266 * @sk: the socket
1267 * @family: unused
1268 * @priority: memory allocation priority
1270 * Assign Smack pointers to current
1272 * Returns 0 on success, -ENOMEM is there's no memory
1274 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1276 char *csp = current_security();
1277 struct socket_smack *ssp;
1279 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1280 if (ssp == NULL)
1281 return -ENOMEM;
1283 ssp->smk_in = csp;
1284 ssp->smk_out = csp;
1285 ssp->smk_packet[0] = '\0';
1287 sk->sk_security = ssp;
1289 return 0;
1293 * smack_sk_free_security - Free a socket blob
1294 * @sk: the socket
1296 * Clears the blob pointer
1298 static void smack_sk_free_security(struct sock *sk)
1300 kfree(sk->sk_security);
1304 * smack_set_catset - convert a capset to netlabel mls categories
1305 * @catset: the Smack categories
1306 * @sap: where to put the netlabel categories
1308 * Allocates and fills attr.mls.cat
1310 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1312 unsigned char *cp;
1313 unsigned char m;
1314 int cat;
1315 int rc;
1316 int byte;
1318 if (!catset)
1319 return;
1321 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1322 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1323 sap->attr.mls.cat->startbit = 0;
1325 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1326 for (m = 0x80; m != 0; m >>= 1, cat++) {
1327 if ((m & *cp) == 0)
1328 continue;
1329 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1330 cat, GFP_ATOMIC);
1335 * smack_to_secattr - fill a secattr from a smack value
1336 * @smack: the smack value
1337 * @nlsp: where the result goes
1339 * Casey says that CIPSO is good enough for now.
1340 * It can be used to effect.
1341 * It can also be abused to effect when necessary.
1342 * Appologies to the TSIG group in general and GW in particular.
1344 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1346 struct smack_cipso cipso;
1347 int rc;
1349 nlsp->domain = smack;
1350 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1352 rc = smack_to_cipso(smack, &cipso);
1353 if (rc == 0) {
1354 nlsp->attr.mls.lvl = cipso.smk_level;
1355 smack_set_catset(cipso.smk_catset, nlsp);
1356 } else {
1357 nlsp->attr.mls.lvl = smack_cipso_direct;
1358 smack_set_catset(smack, nlsp);
1363 * smack_netlabel - Set the secattr on a socket
1364 * @sk: the socket
1365 * @labeled: socket label scheme
1367 * Convert the outbound smack value (smk_out) to a
1368 * secattr and attach it to the socket.
1370 * Returns 0 on success or an error code
1372 static int smack_netlabel(struct sock *sk, int labeled)
1374 struct socket_smack *ssp;
1375 struct netlbl_lsm_secattr secattr;
1376 int rc = 0;
1378 ssp = sk->sk_security;
1380 * Usually the netlabel code will handle changing the
1381 * packet labeling based on the label.
1382 * The case of a single label host is different, because
1383 * a single label host should never get a labeled packet
1384 * even though the label is usually associated with a packet
1385 * label.
1387 local_bh_disable();
1388 bh_lock_sock_nested(sk);
1390 if (ssp->smk_out == smack_net_ambient ||
1391 labeled == SMACK_UNLABELED_SOCKET)
1392 netlbl_sock_delattr(sk);
1393 else {
1394 netlbl_secattr_init(&secattr);
1395 smack_to_secattr(ssp->smk_out, &secattr);
1396 rc = netlbl_sock_setattr(sk, &secattr);
1397 netlbl_secattr_destroy(&secattr);
1400 bh_unlock_sock(sk);
1401 local_bh_enable();
1403 return rc;
1407 * smack_inode_setsecurity - set smack xattrs
1408 * @inode: the object
1409 * @name: attribute name
1410 * @value: attribute value
1411 * @size: size of the attribute
1412 * @flags: unused
1414 * Sets the named attribute in the appropriate blob
1416 * Returns 0 on success, or an error code
1418 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1419 const void *value, size_t size, int flags)
1421 char *sp;
1422 struct inode_smack *nsp = inode->i_security;
1423 struct socket_smack *ssp;
1424 struct socket *sock;
1425 int rc = 0;
1427 if (value == NULL || size > SMK_LABELLEN || size == 0)
1428 return -EACCES;
1430 sp = smk_import(value, size);
1431 if (sp == NULL)
1432 return -EINVAL;
1434 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1435 nsp->smk_inode = sp;
1436 return 0;
1439 * The rest of the Smack xattrs are only on sockets.
1441 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1442 return -EOPNOTSUPP;
1444 sock = SOCKET_I(inode);
1445 if (sock == NULL || sock->sk == NULL)
1446 return -EOPNOTSUPP;
1448 ssp = sock->sk->sk_security;
1450 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1451 ssp->smk_in = sp;
1452 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1453 ssp->smk_out = sp;
1454 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1455 if (rc != 0)
1456 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1457 __func__, -rc);
1458 } else
1459 return -EOPNOTSUPP;
1461 return 0;
1465 * smack_socket_post_create - finish socket setup
1466 * @sock: the socket
1467 * @family: protocol family
1468 * @type: unused
1469 * @protocol: unused
1470 * @kern: unused
1472 * Sets the netlabel information on the socket
1474 * Returns 0 on success, and error code otherwise
1476 static int smack_socket_post_create(struct socket *sock, int family,
1477 int type, int protocol, int kern)
1479 if (family != PF_INET || sock->sk == NULL)
1480 return 0;
1482 * Set the outbound netlbl.
1484 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1489 * smack_host_label - check host based restrictions
1490 * @sip: the object end
1492 * looks for host based access restrictions
1494 * This version will only be appropriate for really small
1495 * sets of single label hosts.
1497 * Returns the label of the far end or NULL if it's not special.
1499 static char *smack_host_label(struct sockaddr_in *sip)
1501 struct smk_netlbladdr *snp;
1502 struct in_addr *siap = &sip->sin_addr;
1504 if (siap->s_addr == 0)
1505 return NULL;
1507 for (snp = smack_netlbladdrs; snp != NULL; snp = snp->smk_next) {
1509 * we break after finding the first match because
1510 * the list is sorted from longest to shortest mask
1511 * so we have found the most specific match
1513 if ((&snp->smk_host.sin_addr)->s_addr ==
1514 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1515 return snp->smk_label;
1519 return NULL;
1523 * smack_socket_connect - connect access check
1524 * @sock: the socket
1525 * @sap: the other end
1526 * @addrlen: size of sap
1528 * Verifies that a connection may be possible
1530 * Returns 0 on success, and error code otherwise
1532 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1533 int addrlen)
1535 struct socket_smack *ssp = sock->sk->sk_security;
1536 char *hostsp;
1537 int rc;
1539 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1540 return 0;
1542 if (addrlen < sizeof(struct sockaddr_in))
1543 return -EINVAL;
1545 hostsp = smack_host_label((struct sockaddr_in *)sap);
1546 if (hostsp == NULL)
1547 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1549 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
1550 if (rc != 0)
1551 return rc;
1553 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
1557 * smack_flags_to_may - convert S_ to MAY_ values
1558 * @flags: the S_ value
1560 * Returns the equivalent MAY_ value
1562 static int smack_flags_to_may(int flags)
1564 int may = 0;
1566 if (flags & S_IRUGO)
1567 may |= MAY_READ;
1568 if (flags & S_IWUGO)
1569 may |= MAY_WRITE;
1570 if (flags & S_IXUGO)
1571 may |= MAY_EXEC;
1573 return may;
1577 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1578 * @msg: the object
1580 * Returns 0
1582 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1584 msg->security = current_security();
1585 return 0;
1589 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1590 * @msg: the object
1592 * Clears the blob pointer
1594 static void smack_msg_msg_free_security(struct msg_msg *msg)
1596 msg->security = NULL;
1600 * smack_of_shm - the smack pointer for the shm
1601 * @shp: the object
1603 * Returns a pointer to the smack value
1605 static char *smack_of_shm(struct shmid_kernel *shp)
1607 return (char *)shp->shm_perm.security;
1611 * smack_shm_alloc_security - Set the security blob for shm
1612 * @shp: the object
1614 * Returns 0
1616 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1618 struct kern_ipc_perm *isp = &shp->shm_perm;
1620 isp->security = current_security();
1621 return 0;
1625 * smack_shm_free_security - Clear the security blob for shm
1626 * @shp: the object
1628 * Clears the blob pointer
1630 static void smack_shm_free_security(struct shmid_kernel *shp)
1632 struct kern_ipc_perm *isp = &shp->shm_perm;
1634 isp->security = NULL;
1638 * smack_shm_associate - Smack access check for shm
1639 * @shp: the object
1640 * @shmflg: access requested
1642 * Returns 0 if current has the requested access, error code otherwise
1644 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1646 char *ssp = smack_of_shm(shp);
1647 int may;
1649 may = smack_flags_to_may(shmflg);
1650 return smk_curacc(ssp, may);
1654 * smack_shm_shmctl - Smack access check for shm
1655 * @shp: the object
1656 * @cmd: what it wants to do
1658 * Returns 0 if current has the requested access, error code otherwise
1660 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1662 char *ssp;
1663 int may;
1665 switch (cmd) {
1666 case IPC_STAT:
1667 case SHM_STAT:
1668 may = MAY_READ;
1669 break;
1670 case IPC_SET:
1671 case SHM_LOCK:
1672 case SHM_UNLOCK:
1673 case IPC_RMID:
1674 may = MAY_READWRITE;
1675 break;
1676 case IPC_INFO:
1677 case SHM_INFO:
1679 * System level information.
1681 return 0;
1682 default:
1683 return -EINVAL;
1686 ssp = smack_of_shm(shp);
1687 return smk_curacc(ssp, may);
1691 * smack_shm_shmat - Smack access for shmat
1692 * @shp: the object
1693 * @shmaddr: unused
1694 * @shmflg: access requested
1696 * Returns 0 if current has the requested access, error code otherwise
1698 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1699 int shmflg)
1701 char *ssp = smack_of_shm(shp);
1702 int may;
1704 may = smack_flags_to_may(shmflg);
1705 return smk_curacc(ssp, may);
1709 * smack_of_sem - the smack pointer for the sem
1710 * @sma: the object
1712 * Returns a pointer to the smack value
1714 static char *smack_of_sem(struct sem_array *sma)
1716 return (char *)sma->sem_perm.security;
1720 * smack_sem_alloc_security - Set the security blob for sem
1721 * @sma: the object
1723 * Returns 0
1725 static int smack_sem_alloc_security(struct sem_array *sma)
1727 struct kern_ipc_perm *isp = &sma->sem_perm;
1729 isp->security = current_security();
1730 return 0;
1734 * smack_sem_free_security - Clear the security blob for sem
1735 * @sma: the object
1737 * Clears the blob pointer
1739 static void smack_sem_free_security(struct sem_array *sma)
1741 struct kern_ipc_perm *isp = &sma->sem_perm;
1743 isp->security = NULL;
1747 * smack_sem_associate - Smack access check for sem
1748 * @sma: the object
1749 * @semflg: access requested
1751 * Returns 0 if current has the requested access, error code otherwise
1753 static int smack_sem_associate(struct sem_array *sma, int semflg)
1755 char *ssp = smack_of_sem(sma);
1756 int may;
1758 may = smack_flags_to_may(semflg);
1759 return smk_curacc(ssp, may);
1763 * smack_sem_shmctl - Smack access check for sem
1764 * @sma: the object
1765 * @cmd: what it wants to do
1767 * Returns 0 if current has the requested access, error code otherwise
1769 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1771 char *ssp;
1772 int may;
1774 switch (cmd) {
1775 case GETPID:
1776 case GETNCNT:
1777 case GETZCNT:
1778 case GETVAL:
1779 case GETALL:
1780 case IPC_STAT:
1781 case SEM_STAT:
1782 may = MAY_READ;
1783 break;
1784 case SETVAL:
1785 case SETALL:
1786 case IPC_RMID:
1787 case IPC_SET:
1788 may = MAY_READWRITE;
1789 break;
1790 case IPC_INFO:
1791 case SEM_INFO:
1793 * System level information
1795 return 0;
1796 default:
1797 return -EINVAL;
1800 ssp = smack_of_sem(sma);
1801 return smk_curacc(ssp, may);
1805 * smack_sem_semop - Smack checks of semaphore operations
1806 * @sma: the object
1807 * @sops: unused
1808 * @nsops: unused
1809 * @alter: unused
1811 * Treated as read and write in all cases.
1813 * Returns 0 if access is allowed, error code otherwise
1815 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1816 unsigned nsops, int alter)
1818 char *ssp = smack_of_sem(sma);
1820 return smk_curacc(ssp, MAY_READWRITE);
1824 * smack_msg_alloc_security - Set the security blob for msg
1825 * @msq: the object
1827 * Returns 0
1829 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1831 struct kern_ipc_perm *kisp = &msq->q_perm;
1833 kisp->security = current_security();
1834 return 0;
1838 * smack_msg_free_security - Clear the security blob for msg
1839 * @msq: the object
1841 * Clears the blob pointer
1843 static void smack_msg_queue_free_security(struct msg_queue *msq)
1845 struct kern_ipc_perm *kisp = &msq->q_perm;
1847 kisp->security = NULL;
1851 * smack_of_msq - the smack pointer for the msq
1852 * @msq: the object
1854 * Returns a pointer to the smack value
1856 static char *smack_of_msq(struct msg_queue *msq)
1858 return (char *)msq->q_perm.security;
1862 * smack_msg_queue_associate - Smack access check for msg_queue
1863 * @msq: the object
1864 * @msqflg: access requested
1866 * Returns 0 if current has the requested access, error code otherwise
1868 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1870 char *msp = smack_of_msq(msq);
1871 int may;
1873 may = smack_flags_to_may(msqflg);
1874 return smk_curacc(msp, may);
1878 * smack_msg_queue_msgctl - Smack access check for msg_queue
1879 * @msq: the object
1880 * @cmd: what it wants to do
1882 * Returns 0 if current has the requested access, error code otherwise
1884 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1886 char *msp;
1887 int may;
1889 switch (cmd) {
1890 case IPC_STAT:
1891 case MSG_STAT:
1892 may = MAY_READ;
1893 break;
1894 case IPC_SET:
1895 case IPC_RMID:
1896 may = MAY_READWRITE;
1897 break;
1898 case IPC_INFO:
1899 case MSG_INFO:
1901 * System level information
1903 return 0;
1904 default:
1905 return -EINVAL;
1908 msp = smack_of_msq(msq);
1909 return smk_curacc(msp, may);
1913 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1914 * @msq: the object
1915 * @msg: unused
1916 * @msqflg: access requested
1918 * Returns 0 if current has the requested access, error code otherwise
1920 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1921 int msqflg)
1923 char *msp = smack_of_msq(msq);
1924 int rc;
1926 rc = smack_flags_to_may(msqflg);
1927 return smk_curacc(msp, rc);
1931 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1932 * @msq: the object
1933 * @msg: unused
1934 * @target: unused
1935 * @type: unused
1936 * @mode: unused
1938 * Returns 0 if current has read and write access, error code otherwise
1940 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1941 struct task_struct *target, long type, int mode)
1943 char *msp = smack_of_msq(msq);
1945 return smk_curacc(msp, MAY_READWRITE);
1949 * smack_ipc_permission - Smack access for ipc_permission()
1950 * @ipp: the object permissions
1951 * @flag: access requested
1953 * Returns 0 if current has read and write access, error code otherwise
1955 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1957 char *isp = ipp->security;
1958 int may;
1960 may = smack_flags_to_may(flag);
1961 return smk_curacc(isp, may);
1965 * smack_ipc_getsecid - Extract smack security id
1966 * @ipcp: the object permissions
1967 * @secid: where result will be saved
1969 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1971 char *smack = ipp->security;
1973 *secid = smack_to_secid(smack);
1977 * smack_d_instantiate - Make sure the blob is correct on an inode
1978 * @opt_dentry: unused
1979 * @inode: the object
1981 * Set the inode's security blob if it hasn't been done already.
1983 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1985 struct super_block *sbp;
1986 struct superblock_smack *sbsp;
1987 struct inode_smack *isp;
1988 char *csp = current_security();
1989 char *fetched;
1990 char *final;
1991 struct dentry *dp;
1993 if (inode == NULL)
1994 return;
1996 isp = inode->i_security;
1998 mutex_lock(&isp->smk_lock);
2000 * If the inode is already instantiated
2001 * take the quick way out
2003 if (isp->smk_flags & SMK_INODE_INSTANT)
2004 goto unlockandout;
2006 sbp = inode->i_sb;
2007 sbsp = sbp->s_security;
2009 * We're going to use the superblock default label
2010 * if there's no label on the file.
2012 final = sbsp->smk_default;
2015 * If this is the root inode the superblock
2016 * may be in the process of initialization.
2017 * If that is the case use the root value out
2018 * of the superblock.
2020 if (opt_dentry->d_parent == opt_dentry) {
2021 isp->smk_inode = sbsp->smk_root;
2022 isp->smk_flags |= SMK_INODE_INSTANT;
2023 goto unlockandout;
2027 * This is pretty hackish.
2028 * Casey says that we shouldn't have to do
2029 * file system specific code, but it does help
2030 * with keeping it simple.
2032 switch (sbp->s_magic) {
2033 case SMACK_MAGIC:
2035 * Casey says that it's a little embarassing
2036 * that the smack file system doesn't do
2037 * extended attributes.
2039 final = smack_known_star.smk_known;
2040 break;
2041 case PIPEFS_MAGIC:
2043 * Casey says pipes are easy (?)
2045 final = smack_known_star.smk_known;
2046 break;
2047 case DEVPTS_SUPER_MAGIC:
2049 * devpts seems content with the label of the task.
2050 * Programs that change smack have to treat the
2051 * pty with respect.
2053 final = csp;
2054 break;
2055 case SOCKFS_MAGIC:
2057 * Casey says sockets get the smack of the task.
2059 final = csp;
2060 break;
2061 case PROC_SUPER_MAGIC:
2063 * Casey says procfs appears not to care.
2064 * The superblock default suffices.
2066 break;
2067 case TMPFS_MAGIC:
2069 * Device labels should come from the filesystem,
2070 * but watch out, because they're volitile,
2071 * getting recreated on every reboot.
2073 final = smack_known_star.smk_known;
2075 * No break.
2077 * If a smack value has been set we want to use it,
2078 * but since tmpfs isn't giving us the opportunity
2079 * to set mount options simulate setting the
2080 * superblock default.
2082 default:
2084 * This isn't an understood special case.
2085 * Get the value from the xattr.
2087 * No xattr support means, alas, no SMACK label.
2088 * Use the aforeapplied default.
2089 * It would be curious if the label of the task
2090 * does not match that assigned.
2092 if (inode->i_op->getxattr == NULL)
2093 break;
2095 * Get the dentry for xattr.
2097 if (opt_dentry == NULL) {
2098 dp = d_find_alias(inode);
2099 if (dp == NULL)
2100 break;
2101 } else {
2102 dp = dget(opt_dentry);
2103 if (dp == NULL)
2104 break;
2107 fetched = smk_fetch(inode, dp);
2108 if (fetched != NULL)
2109 final = fetched;
2111 dput(dp);
2112 break;
2115 if (final == NULL)
2116 isp->smk_inode = csp;
2117 else
2118 isp->smk_inode = final;
2120 isp->smk_flags |= SMK_INODE_INSTANT;
2122 unlockandout:
2123 mutex_unlock(&isp->smk_lock);
2124 return;
2128 * smack_getprocattr - Smack process attribute access
2129 * @p: the object task
2130 * @name: the name of the attribute in /proc/.../attr
2131 * @value: where to put the result
2133 * Places a copy of the task Smack into value
2135 * Returns the length of the smack label or an error code
2137 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2139 char *cp;
2140 int slen;
2142 if (strcmp(name, "current") != 0)
2143 return -EINVAL;
2145 cp = kstrdup(task_security(p), GFP_KERNEL);
2146 if (cp == NULL)
2147 return -ENOMEM;
2149 slen = strlen(cp);
2150 *value = cp;
2151 return slen;
2155 * smack_setprocattr - Smack process attribute setting
2156 * @p: the object task
2157 * @name: the name of the attribute in /proc/.../attr
2158 * @value: the value to set
2159 * @size: the size of the value
2161 * Sets the Smack value of the task. Only setting self
2162 * is permitted and only with privilege
2164 * Returns the length of the smack label or an error code
2166 static int smack_setprocattr(struct task_struct *p, char *name,
2167 void *value, size_t size)
2169 struct cred *new;
2170 char *newsmack;
2173 * Changing another process' Smack value is too dangerous
2174 * and supports no sane use case.
2176 if (p != current)
2177 return -EPERM;
2179 if (!capable(CAP_MAC_ADMIN))
2180 return -EPERM;
2182 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2183 return -EINVAL;
2185 if (strcmp(name, "current") != 0)
2186 return -EINVAL;
2188 newsmack = smk_import(value, size);
2189 if (newsmack == NULL)
2190 return -EINVAL;
2193 * No process is ever allowed the web ("@") label.
2195 if (newsmack == smack_known_web.smk_known)
2196 return -EPERM;
2198 new = prepare_creds();
2199 if (new == NULL)
2200 return -ENOMEM;
2201 new->security = newsmack;
2202 commit_creds(new);
2203 return size;
2207 * smack_unix_stream_connect - Smack access on UDS
2208 * @sock: one socket
2209 * @other: the other socket
2210 * @newsk: unused
2212 * Return 0 if a subject with the smack of sock could access
2213 * an object with the smack of other, otherwise an error code
2215 static int smack_unix_stream_connect(struct socket *sock,
2216 struct socket *other, struct sock *newsk)
2218 struct inode *sp = SOCK_INODE(sock);
2219 struct inode *op = SOCK_INODE(other);
2221 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2225 * smack_unix_may_send - Smack access on UDS
2226 * @sock: one socket
2227 * @other: the other socket
2229 * Return 0 if a subject with the smack of sock could access
2230 * an object with the smack of other, otherwise an error code
2232 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2234 struct inode *sp = SOCK_INODE(sock);
2235 struct inode *op = SOCK_INODE(other);
2237 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2241 * smack_socket_sendmsg - Smack check based on destination host
2242 * @sock: the socket
2243 * @msghdr: the message
2244 * @size: the size of the message
2246 * Return 0 if the current subject can write to the destination
2247 * host. This is only a question if the destination is a single
2248 * label host.
2250 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2251 int size)
2253 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2254 struct socket_smack *ssp = sock->sk->sk_security;
2255 char *hostsp;
2256 int rc;
2259 * Perfectly reasonable for this to be NULL
2261 if (sip == NULL || sip->sin_family != PF_INET)
2262 return 0;
2264 hostsp = smack_host_label(sip);
2265 if (hostsp == NULL)
2266 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2268 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
2269 if (rc != 0)
2270 return rc;
2272 return smack_netlabel(sock->sk, SMACK_UNLABELED_SOCKET);
2277 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2278 * pair to smack
2279 * @sap: netlabel secattr
2280 * @sip: where to put the result
2282 * Copies a smack label into sip
2284 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2286 char smack[SMK_LABELLEN];
2287 char *sp;
2288 int pcat;
2290 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2292 * Looks like a CIPSO packet.
2293 * If there are flags but no level netlabel isn't
2294 * behaving the way we expect it to.
2296 * Get the categories, if any
2297 * Without guidance regarding the smack value
2298 * for the packet fall back on the network
2299 * ambient value.
2301 memset(smack, '\0', SMK_LABELLEN);
2302 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2303 for (pcat = -1;;) {
2304 pcat = netlbl_secattr_catmap_walk(
2305 sap->attr.mls.cat, pcat + 1);
2306 if (pcat < 0)
2307 break;
2308 smack_catset_bit(pcat, smack);
2311 * If it is CIPSO using smack direct mapping
2312 * we are already done. WeeHee.
2314 if (sap->attr.mls.lvl == smack_cipso_direct) {
2315 memcpy(sip, smack, SMK_MAXLEN);
2316 return;
2319 * Look it up in the supplied table if it is not
2320 * a direct mapping.
2322 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2323 return;
2325 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2327 * Looks like a fallback, which gives us a secid.
2329 sp = smack_from_secid(sap->attr.secid);
2331 * This has got to be a bug because it is
2332 * impossible to specify a fallback without
2333 * specifying the label, which will ensure
2334 * it has a secid, and the only way to get a
2335 * secid is from a fallback.
2337 BUG_ON(sp == NULL);
2338 strncpy(sip, sp, SMK_MAXLEN);
2339 return;
2342 * Without guidance regarding the smack value
2343 * for the packet fall back on the network
2344 * ambient value.
2346 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2347 return;
2351 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2352 * @sk: socket
2353 * @skb: packet
2355 * Returns 0 if the packet should be delivered, an error code otherwise
2357 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2359 struct netlbl_lsm_secattr secattr;
2360 struct socket_smack *ssp = sk->sk_security;
2361 char smack[SMK_LABELLEN];
2362 char *csp;
2363 int rc;
2365 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2366 return 0;
2369 * Translate what netlabel gave us.
2371 netlbl_secattr_init(&secattr);
2373 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2374 if (rc == 0) {
2375 smack_from_secattr(&secattr, smack);
2376 csp = smack;
2377 } else
2378 csp = smack_net_ambient;
2380 netlbl_secattr_destroy(&secattr);
2383 * Receiving a packet requires that the other end
2384 * be able to write here. Read access is not required.
2385 * This is the simplist possible security model
2386 * for networking.
2388 rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
2389 if (rc != 0)
2390 netlbl_skbuff_err(skb, rc, 0);
2391 return rc;
2395 * smack_socket_getpeersec_stream - pull in packet label
2396 * @sock: the socket
2397 * @optval: user's destination
2398 * @optlen: size thereof
2399 * @len: max thereoe
2401 * returns zero on success, an error code otherwise
2403 static int smack_socket_getpeersec_stream(struct socket *sock,
2404 char __user *optval,
2405 int __user *optlen, unsigned len)
2407 struct socket_smack *ssp;
2408 int slen;
2409 int rc = 0;
2411 ssp = sock->sk->sk_security;
2412 slen = strlen(ssp->smk_packet) + 1;
2414 if (slen > len)
2415 rc = -ERANGE;
2416 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2417 rc = -EFAULT;
2419 if (put_user(slen, optlen) != 0)
2420 rc = -EFAULT;
2422 return rc;
2427 * smack_socket_getpeersec_dgram - pull in packet label
2428 * @sock: the socket
2429 * @skb: packet data
2430 * @secid: pointer to where to put the secid of the packet
2432 * Sets the netlabel socket state on sk from parent
2434 static int smack_socket_getpeersec_dgram(struct socket *sock,
2435 struct sk_buff *skb, u32 *secid)
2438 struct netlbl_lsm_secattr secattr;
2439 struct sock *sk;
2440 char smack[SMK_LABELLEN];
2441 int family = PF_INET;
2442 u32 s;
2443 int rc;
2446 * Only works for families with packets.
2448 if (sock != NULL) {
2449 sk = sock->sk;
2450 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2451 return 0;
2452 family = sk->sk_family;
2455 * Translate what netlabel gave us.
2457 netlbl_secattr_init(&secattr);
2458 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2459 if (rc == 0)
2460 smack_from_secattr(&secattr, smack);
2461 netlbl_secattr_destroy(&secattr);
2464 * Give up if we couldn't get anything
2466 if (rc != 0)
2467 return rc;
2469 s = smack_to_secid(smack);
2470 if (s == 0)
2471 return -EINVAL;
2473 *secid = s;
2474 return 0;
2478 * smack_sock_graft - graft access state between two sockets
2479 * @sk: fresh sock
2480 * @parent: donor socket
2482 * Sets the netlabel socket state on sk from parent
2484 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2486 struct socket_smack *ssp;
2488 if (sk == NULL ||
2489 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2490 return;
2492 ssp = sk->sk_security;
2493 ssp->smk_in = ssp->smk_out = current_security();
2494 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2498 * smack_inet_conn_request - Smack access check on connect
2499 * @sk: socket involved
2500 * @skb: packet
2501 * @req: unused
2503 * Returns 0 if a task with the packet label could write to
2504 * the socket, otherwise an error code
2506 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2507 struct request_sock *req)
2509 u16 family = sk->sk_family;
2510 struct socket_smack *ssp = sk->sk_security;
2511 struct netlbl_lsm_secattr secattr;
2512 struct sockaddr_in addr;
2513 struct iphdr *hdr;
2514 char smack[SMK_LABELLEN];
2515 int rc;
2517 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2518 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2519 family = PF_INET;
2521 netlbl_secattr_init(&secattr);
2522 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2523 if (rc == 0)
2524 smack_from_secattr(&secattr, smack);
2525 else
2526 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2527 netlbl_secattr_destroy(&secattr);
2530 * Receiving a packet requires that the other end be able to write
2531 * here. Read access is not required.
2533 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2534 if (rc != 0)
2535 return rc;
2538 * Save the peer's label in the request_sock so we can later setup
2539 * smk_packet in the child socket so that SO_PEERCRED can report it.
2541 req->peer_secid = smack_to_secid(smack);
2544 * We need to decide if we want to label the incoming connection here
2545 * if we do we only need to label the request_sock and the stack will
2546 * propogate the wire-label to the sock when it is created.
2548 hdr = ip_hdr(skb);
2549 addr.sin_addr.s_addr = hdr->saddr;
2550 rcu_read_lock();
2551 if (smack_host_label(&addr) == NULL) {
2552 rcu_read_unlock();
2553 netlbl_secattr_init(&secattr);
2554 smack_to_secattr(smack, &secattr);
2555 rc = netlbl_req_setattr(req, &secattr);
2556 netlbl_secattr_destroy(&secattr);
2557 } else {
2558 rcu_read_unlock();
2559 netlbl_req_delattr(req);
2562 return rc;
2566 * smack_inet_csk_clone - Copy the connection information to the new socket
2567 * @sk: the new socket
2568 * @req: the connection's request_sock
2570 * Transfer the connection's peer label to the newly created socket.
2572 static void smack_inet_csk_clone(struct sock *sk,
2573 const struct request_sock *req)
2575 struct socket_smack *ssp = sk->sk_security;
2576 char *smack;
2578 if (req->peer_secid != 0) {
2579 smack = smack_from_secid(req->peer_secid);
2580 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2581 } else
2582 ssp->smk_packet[0] = '\0';
2586 * Key management security hooks
2588 * Casey has not tested key support very heavily.
2589 * The permission check is most likely too restrictive.
2590 * If you care about keys please have a look.
2592 #ifdef CONFIG_KEYS
2595 * smack_key_alloc - Set the key security blob
2596 * @key: object
2597 * @cred: the credentials to use
2598 * @flags: unused
2600 * No allocation required
2602 * Returns 0
2604 static int smack_key_alloc(struct key *key, const struct cred *cred,
2605 unsigned long flags)
2607 key->security = cred->security;
2608 return 0;
2612 * smack_key_free - Clear the key security blob
2613 * @key: the object
2615 * Clear the blob pointer
2617 static void smack_key_free(struct key *key)
2619 key->security = NULL;
2623 * smack_key_permission - Smack access on a key
2624 * @key_ref: gets to the object
2625 * @cred: the credentials to use
2626 * @perm: unused
2628 * Return 0 if the task has read and write to the object,
2629 * an error code otherwise
2631 static int smack_key_permission(key_ref_t key_ref,
2632 const struct cred *cred, key_perm_t perm)
2634 struct key *keyp;
2636 keyp = key_ref_to_ptr(key_ref);
2637 if (keyp == NULL)
2638 return -EINVAL;
2640 * If the key hasn't been initialized give it access so that
2641 * it may do so.
2643 if (keyp->security == NULL)
2644 return 0;
2646 * This should not occur
2648 if (cred->security == NULL)
2649 return -EACCES;
2651 return smk_access(cred->security, keyp->security, MAY_READWRITE);
2653 #endif /* CONFIG_KEYS */
2656 * Smack Audit hooks
2658 * Audit requires a unique representation of each Smack specific
2659 * rule. This unique representation is used to distinguish the
2660 * object to be audited from remaining kernel objects and also
2661 * works as a glue between the audit hooks.
2663 * Since repository entries are added but never deleted, we'll use
2664 * the smack_known label address related to the given audit rule as
2665 * the needed unique representation. This also better fits the smack
2666 * model where nearly everything is a label.
2668 #ifdef CONFIG_AUDIT
2671 * smack_audit_rule_init - Initialize a smack audit rule
2672 * @field: audit rule fields given from user-space (audit.h)
2673 * @op: required testing operator (=, !=, >, <, ...)
2674 * @rulestr: smack label to be audited
2675 * @vrule: pointer to save our own audit rule representation
2677 * Prepare to audit cases where (@field @op @rulestr) is true.
2678 * The label to be audited is created if necessay.
2680 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2682 char **rule = (char **)vrule;
2683 *rule = NULL;
2685 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2686 return -EINVAL;
2688 if (op != Audit_equal && op != Audit_not_equal)
2689 return -EINVAL;
2691 *rule = smk_import(rulestr, 0);
2693 return 0;
2697 * smack_audit_rule_known - Distinguish Smack audit rules
2698 * @krule: rule of interest, in Audit kernel representation format
2700 * This is used to filter Smack rules from remaining Audit ones.
2701 * If it's proved that this rule belongs to us, the
2702 * audit_rule_match hook will be called to do the final judgement.
2704 static int smack_audit_rule_known(struct audit_krule *krule)
2706 struct audit_field *f;
2707 int i;
2709 for (i = 0; i < krule->field_count; i++) {
2710 f = &krule->fields[i];
2712 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2713 return 1;
2716 return 0;
2720 * smack_audit_rule_match - Audit given object ?
2721 * @secid: security id for identifying the object to test
2722 * @field: audit rule flags given from user-space
2723 * @op: required testing operator
2724 * @vrule: smack internal rule presentation
2725 * @actx: audit context associated with the check
2727 * The core Audit hook. It's used to take the decision of
2728 * whether to audit or not to audit a given object.
2730 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2731 struct audit_context *actx)
2733 char *smack;
2734 char *rule = vrule;
2736 if (!rule) {
2737 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2738 "Smack: missing rule\n");
2739 return -ENOENT;
2742 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2743 return 0;
2745 smack = smack_from_secid(secid);
2748 * No need to do string comparisons. If a match occurs,
2749 * both pointers will point to the same smack_known
2750 * label.
2752 if (op == Audit_equal)
2753 return (rule == smack);
2754 if (op == Audit_not_equal)
2755 return (rule != smack);
2757 return 0;
2761 * smack_audit_rule_free - free smack rule representation
2762 * @vrule: rule to be freed.
2764 * No memory was allocated.
2766 static void smack_audit_rule_free(void *vrule)
2768 /* No-op */
2771 #endif /* CONFIG_AUDIT */
2774 * smack_secid_to_secctx - return the smack label for a secid
2775 * @secid: incoming integer
2776 * @secdata: destination
2777 * @seclen: how long it is
2779 * Exists for networking code.
2781 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2783 char *sp = smack_from_secid(secid);
2785 *secdata = sp;
2786 *seclen = strlen(sp);
2787 return 0;
2791 * smack_secctx_to_secid - return the secid for a smack label
2792 * @secdata: smack label
2793 * @seclen: how long result is
2794 * @secid: outgoing integer
2796 * Exists for audit and networking code.
2798 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2800 *secid = smack_to_secid(secdata);
2801 return 0;
2805 * smack_release_secctx - don't do anything.
2806 * @key_ref: unused
2807 * @context: unused
2808 * @perm: unused
2810 * Exists to make sure nothing gets done, and properly
2812 static void smack_release_secctx(char *secdata, u32 seclen)
2816 struct security_operations smack_ops = {
2817 .name = "smack",
2819 .ptrace_may_access = smack_ptrace_may_access,
2820 .ptrace_traceme = smack_ptrace_traceme,
2821 .capget = cap_capget,
2822 .capset = cap_capset,
2823 .capable = cap_capable,
2824 .syslog = smack_syslog,
2825 .settime = cap_settime,
2826 .vm_enough_memory = cap_vm_enough_memory,
2828 .bprm_set_creds = cap_bprm_set_creds,
2829 .bprm_secureexec = cap_bprm_secureexec,
2831 .sb_alloc_security = smack_sb_alloc_security,
2832 .sb_free_security = smack_sb_free_security,
2833 .sb_copy_data = smack_sb_copy_data,
2834 .sb_kern_mount = smack_sb_kern_mount,
2835 .sb_statfs = smack_sb_statfs,
2836 .sb_mount = smack_sb_mount,
2837 .sb_umount = smack_sb_umount,
2839 .inode_alloc_security = smack_inode_alloc_security,
2840 .inode_free_security = smack_inode_free_security,
2841 .inode_init_security = smack_inode_init_security,
2842 .inode_link = smack_inode_link,
2843 .inode_unlink = smack_inode_unlink,
2844 .inode_rmdir = smack_inode_rmdir,
2845 .inode_rename = smack_inode_rename,
2846 .inode_permission = smack_inode_permission,
2847 .inode_setattr = smack_inode_setattr,
2848 .inode_getattr = smack_inode_getattr,
2849 .inode_setxattr = smack_inode_setxattr,
2850 .inode_post_setxattr = smack_inode_post_setxattr,
2851 .inode_getxattr = smack_inode_getxattr,
2852 .inode_removexattr = smack_inode_removexattr,
2853 .inode_need_killpriv = cap_inode_need_killpriv,
2854 .inode_killpriv = cap_inode_killpriv,
2855 .inode_getsecurity = smack_inode_getsecurity,
2856 .inode_setsecurity = smack_inode_setsecurity,
2857 .inode_listsecurity = smack_inode_listsecurity,
2858 .inode_getsecid = smack_inode_getsecid,
2860 .file_permission = smack_file_permission,
2861 .file_alloc_security = smack_file_alloc_security,
2862 .file_free_security = smack_file_free_security,
2863 .file_ioctl = smack_file_ioctl,
2864 .file_lock = smack_file_lock,
2865 .file_fcntl = smack_file_fcntl,
2866 .file_set_fowner = smack_file_set_fowner,
2867 .file_send_sigiotask = smack_file_send_sigiotask,
2868 .file_receive = smack_file_receive,
2870 .cred_free = smack_cred_free,
2871 .cred_prepare = smack_cred_prepare,
2872 .cred_commit = smack_cred_commit,
2873 .kernel_act_as = smack_kernel_act_as,
2874 .kernel_create_files_as = smack_kernel_create_files_as,
2875 .task_fix_setuid = cap_task_fix_setuid,
2876 .task_setpgid = smack_task_setpgid,
2877 .task_getpgid = smack_task_getpgid,
2878 .task_getsid = smack_task_getsid,
2879 .task_getsecid = smack_task_getsecid,
2880 .task_setnice = smack_task_setnice,
2881 .task_setioprio = smack_task_setioprio,
2882 .task_getioprio = smack_task_getioprio,
2883 .task_setscheduler = smack_task_setscheduler,
2884 .task_getscheduler = smack_task_getscheduler,
2885 .task_movememory = smack_task_movememory,
2886 .task_kill = smack_task_kill,
2887 .task_wait = smack_task_wait,
2888 .task_to_inode = smack_task_to_inode,
2889 .task_prctl = cap_task_prctl,
2891 .ipc_permission = smack_ipc_permission,
2892 .ipc_getsecid = smack_ipc_getsecid,
2894 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2895 .msg_msg_free_security = smack_msg_msg_free_security,
2897 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2898 .msg_queue_free_security = smack_msg_queue_free_security,
2899 .msg_queue_associate = smack_msg_queue_associate,
2900 .msg_queue_msgctl = smack_msg_queue_msgctl,
2901 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2902 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2904 .shm_alloc_security = smack_shm_alloc_security,
2905 .shm_free_security = smack_shm_free_security,
2906 .shm_associate = smack_shm_associate,
2907 .shm_shmctl = smack_shm_shmctl,
2908 .shm_shmat = smack_shm_shmat,
2910 .sem_alloc_security = smack_sem_alloc_security,
2911 .sem_free_security = smack_sem_free_security,
2912 .sem_associate = smack_sem_associate,
2913 .sem_semctl = smack_sem_semctl,
2914 .sem_semop = smack_sem_semop,
2916 .netlink_send = cap_netlink_send,
2917 .netlink_recv = cap_netlink_recv,
2919 .d_instantiate = smack_d_instantiate,
2921 .getprocattr = smack_getprocattr,
2922 .setprocattr = smack_setprocattr,
2924 .unix_stream_connect = smack_unix_stream_connect,
2925 .unix_may_send = smack_unix_may_send,
2927 .socket_post_create = smack_socket_post_create,
2928 .socket_connect = smack_socket_connect,
2929 .socket_sendmsg = smack_socket_sendmsg,
2930 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2931 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2932 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2933 .sk_alloc_security = smack_sk_alloc_security,
2934 .sk_free_security = smack_sk_free_security,
2935 .sock_graft = smack_sock_graft,
2936 .inet_conn_request = smack_inet_conn_request,
2937 .inet_csk_clone = smack_inet_csk_clone,
2939 /* key management security hooks */
2940 #ifdef CONFIG_KEYS
2941 .key_alloc = smack_key_alloc,
2942 .key_free = smack_key_free,
2943 .key_permission = smack_key_permission,
2944 #endif /* CONFIG_KEYS */
2946 /* Audit hooks */
2947 #ifdef CONFIG_AUDIT
2948 .audit_rule_init = smack_audit_rule_init,
2949 .audit_rule_known = smack_audit_rule_known,
2950 .audit_rule_match = smack_audit_rule_match,
2951 .audit_rule_free = smack_audit_rule_free,
2952 #endif /* CONFIG_AUDIT */
2954 .secid_to_secctx = smack_secid_to_secctx,
2955 .secctx_to_secid = smack_secctx_to_secid,
2956 .release_secctx = smack_release_secctx,
2960 * smack_init - initialize the smack system
2962 * Returns 0
2964 static __init int smack_init(void)
2966 struct cred *cred;
2968 if (!security_module_enable(&smack_ops))
2969 return 0;
2971 printk(KERN_INFO "Smack: Initializing.\n");
2974 * Set the security state for the initial task.
2976 cred = (struct cred *) current->cred;
2977 cred->security = &smack_known_floor.smk_known;
2980 * Initialize locks
2982 spin_lock_init(&smack_known_huh.smk_cipsolock);
2983 spin_lock_init(&smack_known_hat.smk_cipsolock);
2984 spin_lock_init(&smack_known_star.smk_cipsolock);
2985 spin_lock_init(&smack_known_floor.smk_cipsolock);
2986 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2989 * Register with LSM
2991 if (register_security(&smack_ops))
2992 panic("smack: Unable to register with kernel.\n");
2994 return 0;
2998 * Smack requires early initialization in order to label
2999 * all processes and objects when they are created.
3001 security_initcall(smack_init);