2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
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>
24 #include <asm/ioctls.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>
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
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)
56 char in[SMK_LABELLEN];
58 if (ip->i_op->getxattr == NULL)
61 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
65 return smk_import(in, rc);
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);
82 isp->smk_inode = smack;
84 mutex_init(&isp->smk_lock);
95 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
96 * @ctp: child task pointer
97 * @mode: ptrace attachment mode
99 * Returns 0 if access is OK, an error code otherwise
101 * Do the capability checks, and require read and write.
103 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
107 rc = cap_ptrace_may_access(ctp, mode);
111 rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE);
112 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
118 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
119 * @ptp: parent task pointer
121 * Returns 0 if access is OK, an error code otherwise
123 * Do the capability checks, and require read and write.
125 static int smack_ptrace_traceme(struct task_struct *ptp)
129 rc = cap_ptrace_traceme(ptp);
133 rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE);
134 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
140 * smack_syslog - Smack approval on syslog
141 * @type: message type
143 * Require that the task has the floor label
145 * Returns 0 on success, error code otherwise.
147 static int smack_syslog(int type)
150 char *sp = current_security();
152 rc = cap_syslog(type);
156 if (capable(CAP_MAC_OVERRIDE))
159 if (sp != smack_known_floor.smk_known)
171 * smack_sb_alloc_security - allocate a superblock blob
172 * @sb: the superblock getting the blob
174 * Returns 0 on success or -ENOMEM on error.
176 static int smack_sb_alloc_security(struct super_block *sb)
178 struct superblock_smack *sbsp;
180 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
185 sbsp->smk_root = smack_known_floor.smk_known;
186 sbsp->smk_default = smack_known_floor.smk_known;
187 sbsp->smk_floor = smack_known_floor.smk_known;
188 sbsp->smk_hat = smack_known_hat.smk_known;
189 sbsp->smk_initialized = 0;
190 spin_lock_init(&sbsp->smk_sblock);
192 sb->s_security = sbsp;
198 * smack_sb_free_security - free a superblock blob
199 * @sb: the superblock getting the blob
202 static void smack_sb_free_security(struct super_block *sb)
204 kfree(sb->s_security);
205 sb->s_security = NULL;
209 * smack_sb_copy_data - copy mount options data for processing
210 * @orig: where to start
211 * @smackopts: mount options string
213 * Returns 0 on success or -ENOMEM on error.
215 * Copy the Smack specific mount options out of the mount
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)
226 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
227 if (strstr(cp, SMK_FSDEFAULT) == cp)
229 else if (strstr(cp, SMK_FSFLOOR) == cp)
231 else if (strstr(cp, SMK_FSHAT) == cp)
233 else if (strstr(cp, SMK_FSROOT) == cp)
238 commap = strchr(cp, ',');
247 strcpy(orig, otheropts);
248 free_page((unsigned long)otheropts);
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;
271 spin_lock(&sp->smk_sblock);
272 if (sp->smk_initialized != 0) {
273 spin_unlock(&sp->smk_sblock);
276 sp->smk_initialized = 1;
277 spin_unlock(&sp->smk_sblock);
279 for (op = data; op != NULL; op = commap) {
280 commap = strchr(op, ',');
284 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
285 op += strlen(SMK_FSHAT);
286 nsp = smk_import(op, 0);
289 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
290 op += strlen(SMK_FSFLOOR);
291 nsp = smk_import(op, 0);
294 } else if (strncmp(op, SMK_FSDEFAULT,
295 strlen(SMK_FSDEFAULT)) == 0) {
296 op += strlen(SMK_FSDEFAULT);
297 nsp = smk_import(op, 0);
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);
309 * Initialize the root inode.
311 isp = inode->i_security;
313 inode->i_security = new_inode_smack(sp->smk_root);
315 isp->smk_inode = sp->smk_root;
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
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
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);
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)
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
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);
416 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
422 *value = kstrdup(isp, GFP_KERNEL);
428 *len = strlen(isp) + 1;
434 * smack_inode_link - Smack check on link
435 * @old_dentry: the existing object
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)
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);
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;
472 * You need write access to the thing you're unlinking
474 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
477 * You also need write access to the containing directory
479 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
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)
497 * You need write access to the thing you're removing
499 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
502 * You also need write access to the containing directory
504 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
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
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)
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);
541 * smack_inode_permission - Smack version of permission()
542 * @inode: the inode in question
543 * @mask: the access requested
545 * This is the important Smack hook.
547 * Returns 0 if access is permitted, -EACCES otherwise
549 static int smack_inode_permission(struct inode *inode, int mask)
552 * No permission to check. Existence test. Yup, it's there.
557 return smk_curacc(smk_of_inode(inode), mask);
561 * smack_inode_setattr - Smack check for setting attributes
562 * @dentry: the object
563 * @iattr: for the force flag
565 * Returns 0 if access is permitted, an error code otherwise
567 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
570 * Need to allow for clearing the setuid bit.
572 if (iattr->ia_valid & ATTR_FORCE)
575 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
579 * smack_inode_getattr - Smack check for getting attributes
581 * @dentry: the object
583 * Returns 0 if access is permitted, an error code otherwise
585 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
587 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
591 * smack_inode_setxattr - Smack check for setting xattrs
592 * @dentry: the object
593 * @name: name of the attribute
598 * This protects the Smack attribute explicitly.
600 * Returns 0 if access is permitted, an error code otherwise
602 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
603 const void *value, size_t size, int flags)
607 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
608 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
609 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
610 if (!capable(CAP_MAC_ADMIN))
613 rc = cap_inode_setxattr(dentry, name, value, size, flags);
616 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
622 * smack_inode_post_setxattr - Apply the Smack update approved above
624 * @name: attribute name
625 * @value: attribute value
626 * @size: attribute size
629 * Set the pointer in the inode blob to the entry found
630 * in the master label list.
632 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
633 const void *value, size_t size, int flags)
635 struct inode_smack *isp;
641 if (strcmp(name, XATTR_NAME_SMACK))
644 if (size >= SMK_LABELLEN)
647 isp = dentry->d_inode->i_security;
650 * No locking is done here. This is a pointer
653 nsp = smk_import(value, size);
655 isp->smk_inode = nsp;
657 isp->smk_inode = smack_known_invalid.smk_known;
663 * smack_inode_getxattr - Smack check on getxattr
664 * @dentry: the object
667 * Returns 0 if access is permitted, an error code otherwise
669 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
671 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
675 * smack_inode_removexattr - Smack check on removexattr
676 * @dentry: the object
677 * @name: name of the attribute
679 * Removing the Smack attribute requires CAP_MAC_ADMIN
681 * Returns 0 if access is permitted, an error code otherwise
683 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
687 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
688 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
689 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
690 if (!capable(CAP_MAC_ADMIN))
693 rc = cap_inode_removexattr(dentry, name);
696 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
702 * smack_inode_getsecurity - get smack xattrs
704 * @name: attribute name
705 * @buffer: where to put the result
708 * Returns the size of the attribute or an error code
710 static int smack_inode_getsecurity(const struct inode *inode,
711 const char *name, void **buffer,
714 struct socket_smack *ssp;
716 struct super_block *sbp;
717 struct inode *ip = (struct inode *)inode;
722 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
723 isp = smk_of_inode(inode);
724 ilen = strlen(isp) + 1;
730 * The rest of the Smack xattrs are only on sockets.
733 if (sbp->s_magic != SOCKFS_MAGIC)
737 if (sock == NULL || sock->sk == NULL)
740 ssp = sock->sk->sk_security;
742 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
744 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
749 ilen = strlen(isp) + 1;
760 * smack_inode_listsecurity - list the Smack attributes
762 * @buffer: where they go
763 * @buffer_size: size of buffer
765 * Returns 0 on success, -EINVAL otherwise
767 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
770 int len = strlen(XATTR_NAME_SMACK);
772 if (buffer != NULL && len <= buffer_size) {
773 memcpy(buffer, XATTR_NAME_SMACK, len);
780 * smack_inode_getsecid - Extract inode's security id
781 * @inode: inode to extract the info from
782 * @secid: where result will be saved
784 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
786 struct inode_smack *isp = inode->i_security;
788 *secid = smack_to_secid(isp->smk_inode);
796 * smack_file_permission - Smack check on file operations
802 * Should access checks be done on each read or write?
803 * UNICOS and SELinux say yes.
804 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
806 * I'll say no for now. Smack does not do the frequent
807 * label changing that SELinux does.
809 static int smack_file_permission(struct file *file, int mask)
815 * smack_file_alloc_security - assign a file security blob
818 * The security blob for a file is a pointer to the master
819 * label list, so no allocation is done.
823 static int smack_file_alloc_security(struct file *file)
825 file->f_security = current_security();
830 * smack_file_free_security - clear a file security blob
833 * The security blob for a file is a pointer to the master
834 * label list, so no memory is freed.
836 static void smack_file_free_security(struct file *file)
838 file->f_security = NULL;
842 * smack_file_ioctl - Smack check on ioctls
847 * Relies heavily on the correct use of the ioctl command conventions.
849 * Returns 0 if allowed, error code otherwise
851 static int smack_file_ioctl(struct file *file, unsigned int cmd,
856 if (_IOC_DIR(cmd) & _IOC_WRITE)
857 rc = smk_curacc(file->f_security, MAY_WRITE);
859 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
860 rc = smk_curacc(file->f_security, MAY_READ);
866 * smack_file_lock - Smack check on file locking
870 * Returns 0 if current has write access, error code otherwise
872 static int smack_file_lock(struct file *file, unsigned int cmd)
874 return smk_curacc(file->f_security, MAY_WRITE);
878 * smack_file_fcntl - Smack check on fcntl
880 * @cmd: what action to check
883 * Returns 0 if current has access, error code otherwise
885 static int smack_file_fcntl(struct file *file, unsigned int cmd,
897 rc = smk_curacc(file->f_security, MAY_READ);
905 rc = smk_curacc(file->f_security, MAY_WRITE);
908 rc = smk_curacc(file->f_security, MAY_READWRITE);
915 * smack_file_set_fowner - set the file security blob value
916 * @file: object in question
919 * Further research may be required on this one.
921 static int smack_file_set_fowner(struct file *file)
923 file->f_security = current_security();
928 * smack_file_send_sigiotask - Smack on sigio
929 * @tsk: The target task
930 * @fown: the object the signal come from
933 * Allow a privileged task to get signals even if it shouldn't
935 * Returns 0 if a subject with the object's smack could
936 * write to the task, an error code otherwise.
938 static int smack_file_send_sigiotask(struct task_struct *tsk,
939 struct fown_struct *fown, int signum)
945 * struct fown_struct is never outside the context of a struct file
947 file = container_of(fown, struct file, f_owner);
948 rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE);
949 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
955 * smack_file_receive - Smack file receive check
958 * Returns 0 if current has access, error code otherwise
960 static int smack_file_receive(struct file *file)
965 * This code relies on bitmasks.
967 if (file->f_mode & FMODE_READ)
969 if (file->f_mode & FMODE_WRITE)
972 return smk_curacc(file->f_security, may);
980 * smack_cred_free - "free" task-level security credentials
981 * @cred: the credentials in question
983 * Smack isn't using copies of blobs. Everyone
984 * points to an immutable list. The blobs never go away.
985 * There is no leak here.
987 static void smack_cred_free(struct cred *cred)
989 cred->security = NULL;
993 * smack_cred_prepare - prepare new set of credentials for modification
994 * @new: the new credentials
995 * @old: the original credentials
996 * @gfp: the atomicity of any memory allocations
998 * Prepare a new set of credentials for modification.
1000 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1003 new->security = old->security;
1008 * smack_cred_commit - commit new credentials
1009 * @new: the new credentials
1010 * @old: the original credentials
1012 static void smack_cred_commit(struct cred *new, const struct cred *old)
1017 * smack_kernel_act_as - Set the subjective context in a set of credentials
1018 * @new: points to the set of credentials to be modified.
1019 * @secid: specifies the security ID to be set
1021 * Set the security data for a kernel service.
1023 static int smack_kernel_act_as(struct cred *new, u32 secid)
1025 char *smack = smack_from_secid(secid);
1030 new->security = smack;
1035 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1036 * @new: points to the set of credentials to be modified
1037 * @inode: points to the inode to use as a reference
1039 * Set the file creation context in a set of credentials to the same
1040 * as the objective context of the specified inode
1042 static int smack_kernel_create_files_as(struct cred *new,
1043 struct inode *inode)
1045 struct inode_smack *isp = inode->i_security;
1047 new->security = isp->smk_inode;
1052 * smack_task_setpgid - Smack check on setting pgid
1053 * @p: the task object
1056 * Return 0 if write access is permitted
1058 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1060 return smk_curacc(task_security(p), MAY_WRITE);
1064 * smack_task_getpgid - Smack access check for getpgid
1065 * @p: the object task
1067 * Returns 0 if current can read the object task, error code otherwise
1069 static int smack_task_getpgid(struct task_struct *p)
1071 return smk_curacc(task_security(p), MAY_READ);
1075 * smack_task_getsid - Smack access check for getsid
1076 * @p: the object task
1078 * Returns 0 if current can read the object task, error code otherwise
1080 static int smack_task_getsid(struct task_struct *p)
1082 return smk_curacc(task_security(p), MAY_READ);
1086 * smack_task_getsecid - get the secid of the task
1087 * @p: the object task
1088 * @secid: where to put the result
1090 * Sets the secid to contain a u32 version of the smack label.
1092 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1094 *secid = smack_to_secid(task_security(p));
1098 * smack_task_setnice - Smack check on setting nice
1099 * @p: the task object
1102 * Return 0 if write access is permitted
1104 static int smack_task_setnice(struct task_struct *p, int nice)
1108 rc = cap_task_setnice(p, nice);
1110 rc = smk_curacc(task_security(p), MAY_WRITE);
1115 * smack_task_setioprio - Smack check on setting ioprio
1116 * @p: the task object
1119 * Return 0 if write access is permitted
1121 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1125 rc = cap_task_setioprio(p, ioprio);
1127 rc = smk_curacc(task_security(p), MAY_WRITE);
1132 * smack_task_getioprio - Smack check on reading ioprio
1133 * @p: the task object
1135 * Return 0 if read access is permitted
1137 static int smack_task_getioprio(struct task_struct *p)
1139 return smk_curacc(task_security(p), MAY_READ);
1143 * smack_task_setscheduler - Smack check on setting scheduler
1144 * @p: the task object
1148 * Return 0 if read access is permitted
1150 static int smack_task_setscheduler(struct task_struct *p, int policy,
1151 struct sched_param *lp)
1155 rc = cap_task_setscheduler(p, policy, lp);
1157 rc = smk_curacc(task_security(p), MAY_WRITE);
1162 * smack_task_getscheduler - Smack check on reading scheduler
1163 * @p: the task object
1165 * Return 0 if read access is permitted
1167 static int smack_task_getscheduler(struct task_struct *p)
1169 return smk_curacc(task_security(p), MAY_READ);
1173 * smack_task_movememory - Smack check on moving memory
1174 * @p: the task object
1176 * Return 0 if write access is permitted
1178 static int smack_task_movememory(struct task_struct *p)
1180 return smk_curacc(task_security(p), MAY_WRITE);
1184 * smack_task_kill - Smack check on signal delivery
1185 * @p: the task object
1188 * @secid: identifies the smack to use in lieu of current's
1190 * Return 0 if write access is permitted
1192 * The secid behavior is an artifact of an SELinux hack
1193 * in the USB code. Someday it may go away.
1195 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1199 * Sending a signal requires that the sender
1200 * can write the receiver.
1203 return smk_curacc(task_security(p), MAY_WRITE);
1205 * If the secid isn't 0 we're dealing with some USB IO
1206 * specific behavior. This is not clean. For one thing
1207 * we can't take privilege into account.
1209 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1213 * smack_task_wait - Smack access check for waiting
1214 * @p: task to wait for
1216 * Returns 0 if current can wait for p, error code otherwise
1218 static int smack_task_wait(struct task_struct *p)
1222 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1227 * Allow the operation to succeed if either task
1228 * has privilege to perform operations that might
1229 * account for the smack labels having gotten to
1230 * be different in the first place.
1232 * This breaks the strict subject/object access
1233 * control ideal, taking the object's privilege
1234 * state into account in the decision as well as
1237 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1244 * smack_task_to_inode - copy task smack into the inode blob
1245 * @p: task to copy from
1246 * @inode: inode to copy to
1248 * Sets the smack pointer in the inode security blob
1250 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1252 struct inode_smack *isp = inode->i_security;
1253 isp->smk_inode = task_security(p);
1261 * smack_sk_alloc_security - Allocate a socket blob
1264 * @gfp_flags: memory allocation flags
1266 * Assign Smack pointers to current
1268 * Returns 0 on success, -ENOMEM is there's no memory
1270 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1272 char *csp = current_security();
1273 struct socket_smack *ssp;
1275 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1281 ssp->smk_packet[0] = '\0';
1283 sk->sk_security = ssp;
1289 * smack_sk_free_security - Free a socket blob
1292 * Clears the blob pointer
1294 static void smack_sk_free_security(struct sock *sk)
1296 kfree(sk->sk_security);
1300 * smack_host_label - check host based restrictions
1301 * @sip: the object end
1303 * looks for host based access restrictions
1305 * This version will only be appropriate for really small sets of single label
1306 * hosts. The caller is responsible for ensuring that the RCU read lock is
1307 * taken before calling this function.
1309 * Returns the label of the far end or NULL if it's not special.
1311 static char *smack_host_label(struct sockaddr_in *sip)
1313 struct smk_netlbladdr *snp;
1314 struct in_addr *siap = &sip->sin_addr;
1316 if (siap->s_addr == 0)
1319 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1321 * we break after finding the first match because
1322 * the list is sorted from longest to shortest mask
1323 * so we have found the most specific match
1325 if ((&snp->smk_host.sin_addr)->s_addr ==
1326 (siap->s_addr & (&snp->smk_mask)->s_addr))
1327 return snp->smk_label;
1333 * smack_set_catset - convert a capset to netlabel mls categories
1334 * @catset: the Smack categories
1335 * @sap: where to put the netlabel categories
1337 * Allocates and fills attr.mls.cat
1339 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1350 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1351 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1352 sap->attr.mls.cat->startbit = 0;
1354 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1355 for (m = 0x80; m != 0; m >>= 1, cat++) {
1358 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1364 * smack_to_secattr - fill a secattr from a smack value
1365 * @smack: the smack value
1366 * @nlsp: where the result goes
1368 * Casey says that CIPSO is good enough for now.
1369 * It can be used to effect.
1370 * It can also be abused to effect when necessary.
1371 * Appologies to the TSIG group in general and GW in particular.
1373 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1375 struct smack_cipso cipso;
1378 nlsp->domain = smack;
1379 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1381 rc = smack_to_cipso(smack, &cipso);
1383 nlsp->attr.mls.lvl = cipso.smk_level;
1384 smack_set_catset(cipso.smk_catset, nlsp);
1386 nlsp->attr.mls.lvl = smack_cipso_direct;
1387 smack_set_catset(smack, nlsp);
1392 * smack_netlabel - Set the secattr on a socket
1394 * @labeled: socket label scheme
1396 * Convert the outbound smack value (smk_out) to a
1397 * secattr and attach it to the socket.
1399 * Returns 0 on success or an error code
1401 static int smack_netlabel(struct sock *sk, int labeled)
1403 struct socket_smack *ssp = sk->sk_security;
1404 struct netlbl_lsm_secattr secattr;
1408 * Usually the netlabel code will handle changing the
1409 * packet labeling based on the label.
1410 * The case of a single label host is different, because
1411 * a single label host should never get a labeled packet
1412 * even though the label is usually associated with a packet
1416 bh_lock_sock_nested(sk);
1418 if (ssp->smk_out == smack_net_ambient ||
1419 labeled == SMACK_UNLABELED_SOCKET)
1420 netlbl_sock_delattr(sk);
1422 netlbl_secattr_init(&secattr);
1423 smack_to_secattr(ssp->smk_out, &secattr);
1424 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1425 netlbl_secattr_destroy(&secattr);
1435 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1437 * @sap: the destination address
1439 * Set the correct secattr for the given socket based on the destination
1440 * address and perform any outbound access checks needed.
1442 * Returns 0 on success or an error code.
1445 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1450 struct socket_smack *ssp = sk->sk_security;
1453 hostsp = smack_host_label(sap);
1454 if (hostsp != NULL) {
1455 sk_lbl = SMACK_UNLABELED_SOCKET;
1456 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE);
1458 sk_lbl = SMACK_CIPSO_SOCKET;
1465 return smack_netlabel(sk, sk_lbl);
1469 * smack_inode_setsecurity - set smack xattrs
1470 * @inode: the object
1471 * @name: attribute name
1472 * @value: attribute value
1473 * @size: size of the attribute
1476 * Sets the named attribute in the appropriate blob
1478 * Returns 0 on success, or an error code
1480 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1481 const void *value, size_t size, int flags)
1484 struct inode_smack *nsp = inode->i_security;
1485 struct socket_smack *ssp;
1486 struct socket *sock;
1489 if (value == NULL || size > SMK_LABELLEN)
1492 sp = smk_import(value, size);
1496 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1497 nsp->smk_inode = sp;
1501 * The rest of the Smack xattrs are only on sockets.
1503 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1506 sock = SOCKET_I(inode);
1507 if (sock == NULL || sock->sk == NULL)
1510 ssp = sock->sk->sk_security;
1512 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1514 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1516 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1518 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1527 * smack_socket_post_create - finish socket setup
1529 * @family: protocol family
1534 * Sets the netlabel information on the socket
1536 * Returns 0 on success, and error code otherwise
1538 static int smack_socket_post_create(struct socket *sock, int family,
1539 int type, int protocol, int kern)
1541 if (family != PF_INET || sock->sk == NULL)
1544 * Set the outbound netlbl.
1546 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1550 * smack_socket_connect - connect access check
1552 * @sap: the other end
1553 * @addrlen: size of sap
1555 * Verifies that a connection may be possible
1557 * Returns 0 on success, and error code otherwise
1559 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1562 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1564 if (addrlen < sizeof(struct sockaddr_in))
1567 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1571 * smack_flags_to_may - convert S_ to MAY_ values
1572 * @flags: the S_ value
1574 * Returns the equivalent MAY_ value
1576 static int smack_flags_to_may(int flags)
1580 if (flags & S_IRUGO)
1582 if (flags & S_IWUGO)
1584 if (flags & S_IXUGO)
1591 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1596 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1598 msg->security = current_security();
1603 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1606 * Clears the blob pointer
1608 static void smack_msg_msg_free_security(struct msg_msg *msg)
1610 msg->security = NULL;
1614 * smack_of_shm - the smack pointer for the shm
1617 * Returns a pointer to the smack value
1619 static char *smack_of_shm(struct shmid_kernel *shp)
1621 return (char *)shp->shm_perm.security;
1625 * smack_shm_alloc_security - Set the security blob for shm
1630 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1632 struct kern_ipc_perm *isp = &shp->shm_perm;
1634 isp->security = current_security();
1639 * smack_shm_free_security - Clear the security blob for shm
1642 * Clears the blob pointer
1644 static void smack_shm_free_security(struct shmid_kernel *shp)
1646 struct kern_ipc_perm *isp = &shp->shm_perm;
1648 isp->security = NULL;
1652 * smack_shm_associate - Smack access check for shm
1654 * @shmflg: access requested
1656 * Returns 0 if current has the requested access, error code otherwise
1658 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1660 char *ssp = smack_of_shm(shp);
1663 may = smack_flags_to_may(shmflg);
1664 return smk_curacc(ssp, may);
1668 * smack_shm_shmctl - Smack access check for shm
1670 * @cmd: what it wants to do
1672 * Returns 0 if current has the requested access, error code otherwise
1674 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1688 may = MAY_READWRITE;
1693 * System level information.
1700 ssp = smack_of_shm(shp);
1701 return smk_curacc(ssp, may);
1705 * smack_shm_shmat - Smack access for shmat
1708 * @shmflg: access requested
1710 * Returns 0 if current has the requested access, error code otherwise
1712 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1715 char *ssp = smack_of_shm(shp);
1718 may = smack_flags_to_may(shmflg);
1719 return smk_curacc(ssp, may);
1723 * smack_of_sem - the smack pointer for the sem
1726 * Returns a pointer to the smack value
1728 static char *smack_of_sem(struct sem_array *sma)
1730 return (char *)sma->sem_perm.security;
1734 * smack_sem_alloc_security - Set the security blob for sem
1739 static int smack_sem_alloc_security(struct sem_array *sma)
1741 struct kern_ipc_perm *isp = &sma->sem_perm;
1743 isp->security = current_security();
1748 * smack_sem_free_security - Clear the security blob for sem
1751 * Clears the blob pointer
1753 static void smack_sem_free_security(struct sem_array *sma)
1755 struct kern_ipc_perm *isp = &sma->sem_perm;
1757 isp->security = NULL;
1761 * smack_sem_associate - Smack access check for sem
1763 * @semflg: access requested
1765 * Returns 0 if current has the requested access, error code otherwise
1767 static int smack_sem_associate(struct sem_array *sma, int semflg)
1769 char *ssp = smack_of_sem(sma);
1772 may = smack_flags_to_may(semflg);
1773 return smk_curacc(ssp, may);
1777 * smack_sem_shmctl - Smack access check for sem
1779 * @cmd: what it wants to do
1781 * Returns 0 if current has the requested access, error code otherwise
1783 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1802 may = MAY_READWRITE;
1807 * System level information
1814 ssp = smack_of_sem(sma);
1815 return smk_curacc(ssp, may);
1819 * smack_sem_semop - Smack checks of semaphore operations
1825 * Treated as read and write in all cases.
1827 * Returns 0 if access is allowed, error code otherwise
1829 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1830 unsigned nsops, int alter)
1832 char *ssp = smack_of_sem(sma);
1834 return smk_curacc(ssp, MAY_READWRITE);
1838 * smack_msg_alloc_security - Set the security blob for msg
1843 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1845 struct kern_ipc_perm *kisp = &msq->q_perm;
1847 kisp->security = current_security();
1852 * smack_msg_free_security - Clear the security blob for msg
1855 * Clears the blob pointer
1857 static void smack_msg_queue_free_security(struct msg_queue *msq)
1859 struct kern_ipc_perm *kisp = &msq->q_perm;
1861 kisp->security = NULL;
1865 * smack_of_msq - the smack pointer for the msq
1868 * Returns a pointer to the smack value
1870 static char *smack_of_msq(struct msg_queue *msq)
1872 return (char *)msq->q_perm.security;
1876 * smack_msg_queue_associate - Smack access check for msg_queue
1878 * @msqflg: access requested
1880 * Returns 0 if current has the requested access, error code otherwise
1882 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1884 char *msp = smack_of_msq(msq);
1887 may = smack_flags_to_may(msqflg);
1888 return smk_curacc(msp, may);
1892 * smack_msg_queue_msgctl - Smack access check for msg_queue
1894 * @cmd: what it wants to do
1896 * Returns 0 if current has the requested access, error code otherwise
1898 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1910 may = MAY_READWRITE;
1915 * System level information
1922 msp = smack_of_msq(msq);
1923 return smk_curacc(msp, may);
1927 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1930 * @msqflg: access requested
1932 * Returns 0 if current has the requested access, error code otherwise
1934 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1937 char *msp = smack_of_msq(msq);
1940 rc = smack_flags_to_may(msqflg);
1941 return smk_curacc(msp, rc);
1945 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1952 * Returns 0 if current has read and write access, error code otherwise
1954 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1955 struct task_struct *target, long type, int mode)
1957 char *msp = smack_of_msq(msq);
1959 return smk_curacc(msp, MAY_READWRITE);
1963 * smack_ipc_permission - Smack access for ipc_permission()
1964 * @ipp: the object permissions
1965 * @flag: access requested
1967 * Returns 0 if current has read and write access, error code otherwise
1969 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1971 char *isp = ipp->security;
1974 may = smack_flags_to_may(flag);
1975 return smk_curacc(isp, may);
1979 * smack_ipc_getsecid - Extract smack security id
1980 * @ipp: the object permissions
1981 * @secid: where result will be saved
1983 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1985 char *smack = ipp->security;
1987 *secid = smack_to_secid(smack);
1991 * smack_d_instantiate - Make sure the blob is correct on an inode
1992 * @opt_dentry: unused
1993 * @inode: the object
1995 * Set the inode's security blob if it hasn't been done already.
1997 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1999 struct super_block *sbp;
2000 struct superblock_smack *sbsp;
2001 struct inode_smack *isp;
2002 char *csp = current_security();
2010 isp = inode->i_security;
2012 mutex_lock(&isp->smk_lock);
2014 * If the inode is already instantiated
2015 * take the quick way out
2017 if (isp->smk_flags & SMK_INODE_INSTANT)
2021 sbsp = sbp->s_security;
2023 * We're going to use the superblock default label
2024 * if there's no label on the file.
2026 final = sbsp->smk_default;
2029 * If this is the root inode the superblock
2030 * may be in the process of initialization.
2031 * If that is the case use the root value out
2032 * of the superblock.
2034 if (opt_dentry->d_parent == opt_dentry) {
2035 isp->smk_inode = sbsp->smk_root;
2036 isp->smk_flags |= SMK_INODE_INSTANT;
2041 * This is pretty hackish.
2042 * Casey says that we shouldn't have to do
2043 * file system specific code, but it does help
2044 * with keeping it simple.
2046 switch (sbp->s_magic) {
2049 * Casey says that it's a little embarassing
2050 * that the smack file system doesn't do
2051 * extended attributes.
2053 final = smack_known_star.smk_known;
2057 * Casey says pipes are easy (?)
2059 final = smack_known_star.smk_known;
2061 case DEVPTS_SUPER_MAGIC:
2063 * devpts seems content with the label of the task.
2064 * Programs that change smack have to treat the
2071 * Casey says sockets get the smack of the task.
2075 case PROC_SUPER_MAGIC:
2077 * Casey says procfs appears not to care.
2078 * The superblock default suffices.
2083 * Device labels should come from the filesystem,
2084 * but watch out, because they're volitile,
2085 * getting recreated on every reboot.
2087 final = smack_known_star.smk_known;
2091 * If a smack value has been set we want to use it,
2092 * but since tmpfs isn't giving us the opportunity
2093 * to set mount options simulate setting the
2094 * superblock default.
2098 * This isn't an understood special case.
2099 * Get the value from the xattr.
2101 * No xattr support means, alas, no SMACK label.
2102 * Use the aforeapplied default.
2103 * It would be curious if the label of the task
2104 * does not match that assigned.
2106 if (inode->i_op->getxattr == NULL)
2109 * Get the dentry for xattr.
2111 if (opt_dentry == NULL) {
2112 dp = d_find_alias(inode);
2116 dp = dget(opt_dentry);
2121 fetched = smk_fetch(inode, dp);
2122 if (fetched != NULL)
2130 isp->smk_inode = csp;
2132 isp->smk_inode = final;
2134 isp->smk_flags |= SMK_INODE_INSTANT;
2137 mutex_unlock(&isp->smk_lock);
2142 * smack_getprocattr - Smack process attribute access
2143 * @p: the object task
2144 * @name: the name of the attribute in /proc/.../attr
2145 * @value: where to put the result
2147 * Places a copy of the task Smack into value
2149 * Returns the length of the smack label or an error code
2151 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2156 if (strcmp(name, "current") != 0)
2159 cp = kstrdup(task_security(p), GFP_KERNEL);
2169 * smack_setprocattr - Smack process attribute setting
2170 * @p: the object task
2171 * @name: the name of the attribute in /proc/.../attr
2172 * @value: the value to set
2173 * @size: the size of the value
2175 * Sets the Smack value of the task. Only setting self
2176 * is permitted and only with privilege
2178 * Returns the length of the smack label or an error code
2180 static int smack_setprocattr(struct task_struct *p, char *name,
2181 void *value, size_t size)
2187 * Changing another process' Smack value is too dangerous
2188 * and supports no sane use case.
2193 if (!capable(CAP_MAC_ADMIN))
2196 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2199 if (strcmp(name, "current") != 0)
2202 newsmack = smk_import(value, size);
2203 if (newsmack == NULL)
2207 * No process is ever allowed the web ("@") label.
2209 if (newsmack == smack_known_web.smk_known)
2212 new = prepare_creds();
2215 new->security = newsmack;
2221 * smack_unix_stream_connect - Smack access on UDS
2223 * @other: the other socket
2226 * Return 0 if a subject with the smack of sock could access
2227 * an object with the smack of other, otherwise an error code
2229 static int smack_unix_stream_connect(struct socket *sock,
2230 struct socket *other, struct sock *newsk)
2232 struct inode *sp = SOCK_INODE(sock);
2233 struct inode *op = SOCK_INODE(other);
2235 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2239 * smack_unix_may_send - Smack access on UDS
2241 * @other: the other socket
2243 * Return 0 if a subject with the smack of sock could access
2244 * an object with the smack of other, otherwise an error code
2246 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2248 struct inode *sp = SOCK_INODE(sock);
2249 struct inode *op = SOCK_INODE(other);
2251 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2255 * smack_socket_sendmsg - Smack check based on destination host
2258 * @size: the size of the message
2260 * Return 0 if the current subject can write to the destination
2261 * host. This is only a question if the destination is a single
2264 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2267 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2270 * Perfectly reasonable for this to be NULL
2272 if (sip == NULL || sip->sin_family != PF_INET)
2275 return smack_netlabel_send(sock->sk, sip);
2280 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2281 * @sap: netlabel secattr
2282 * @sip: where to put the result
2284 * Copies a smack label into sip
2286 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2288 char smack[SMK_LABELLEN];
2292 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2294 * Looks like a CIPSO packet.
2295 * If there are flags but no level netlabel isn't
2296 * behaving the way we expect it to.
2298 * Get the categories, if any
2299 * Without guidance regarding the smack value
2300 * for the packet fall back on the network
2303 memset(smack, '\0', SMK_LABELLEN);
2304 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2306 pcat = netlbl_secattr_catmap_walk(
2307 sap->attr.mls.cat, pcat + 1);
2310 smack_catset_bit(pcat, smack);
2313 * If it is CIPSO using smack direct mapping
2314 * we are already done. WeeHee.
2316 if (sap->attr.mls.lvl == smack_cipso_direct) {
2317 memcpy(sip, smack, SMK_MAXLEN);
2321 * Look it up in the supplied table if it is not
2324 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2327 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2329 * Looks like a fallback, which gives us a secid.
2331 sp = smack_from_secid(sap->attr.secid);
2333 * This has got to be a bug because it is
2334 * impossible to specify a fallback without
2335 * specifying the label, which will ensure
2336 * it has a secid, and the only way to get a
2337 * secid is from a fallback.
2340 strncpy(sip, sp, SMK_MAXLEN);
2344 * Without guidance regarding the smack value
2345 * for the packet fall back on the network
2348 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2353 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2357 * Returns 0 if the packet should be delivered, an error code otherwise
2359 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2361 struct netlbl_lsm_secattr secattr;
2362 struct socket_smack *ssp = sk->sk_security;
2363 char smack[SMK_LABELLEN];
2367 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2371 * Translate what netlabel gave us.
2373 netlbl_secattr_init(&secattr);
2375 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2377 smack_from_secattr(&secattr, smack);
2380 csp = smack_net_ambient;
2382 netlbl_secattr_destroy(&secattr);
2385 * Receiving a packet requires that the other end
2386 * be able to write here. Read access is not required.
2387 * This is the simplist possible security model
2390 rc = smk_access(csp, ssp->smk_in, MAY_WRITE);
2392 netlbl_skbuff_err(skb, rc, 0);
2397 * smack_socket_getpeersec_stream - pull in packet label
2399 * @optval: user's destination
2400 * @optlen: size thereof
2403 * returns zero on success, an error code otherwise
2405 static int smack_socket_getpeersec_stream(struct socket *sock,
2406 char __user *optval,
2407 int __user *optlen, unsigned len)
2409 struct socket_smack *ssp;
2413 ssp = sock->sk->sk_security;
2414 slen = strlen(ssp->smk_packet) + 1;
2418 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2421 if (put_user(slen, optlen) != 0)
2429 * smack_socket_getpeersec_dgram - pull in packet label
2432 * @secid: pointer to where to put the secid of the packet
2434 * Sets the netlabel socket state on sk from parent
2436 static int smack_socket_getpeersec_dgram(struct socket *sock,
2437 struct sk_buff *skb, u32 *secid)
2440 struct netlbl_lsm_secattr secattr;
2442 char smack[SMK_LABELLEN];
2443 int family = PF_INET;
2448 * Only works for families with packets.
2452 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2454 family = sk->sk_family;
2457 * Translate what netlabel gave us.
2459 netlbl_secattr_init(&secattr);
2460 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2462 smack_from_secattr(&secattr, smack);
2463 netlbl_secattr_destroy(&secattr);
2466 * Give up if we couldn't get anything
2471 s = smack_to_secid(smack);
2480 * smack_sock_graft - Initialize a newly created socket with an existing sock
2482 * @parent: parent socket
2484 * Set the smk_{in,out} state of an existing sock based on the process that
2485 * is creating the new socket.
2487 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2489 struct socket_smack *ssp;
2492 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2495 ssp = sk->sk_security;
2496 ssp->smk_in = ssp->smk_out = current_security();
2497 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2501 * smack_inet_conn_request - Smack access check on connect
2502 * @sk: socket involved
2506 * Returns 0 if a task with the packet label could write to
2507 * the socket, otherwise an error code
2509 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2510 struct request_sock *req)
2512 u16 family = sk->sk_family;
2513 struct socket_smack *ssp = sk->sk_security;
2514 struct netlbl_lsm_secattr secattr;
2515 struct sockaddr_in addr;
2517 char smack[SMK_LABELLEN];
2520 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2521 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2524 netlbl_secattr_init(&secattr);
2525 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2527 smack_from_secattr(&secattr, smack);
2529 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2530 netlbl_secattr_destroy(&secattr);
2533 * Receiving a packet requires that the other end be able to write
2534 * here. Read access is not required.
2536 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2541 * Save the peer's label in the request_sock so we can later setup
2542 * smk_packet in the child socket so that SO_PEERCRED can report it.
2544 req->peer_secid = smack_to_secid(smack);
2547 * We need to decide if we want to label the incoming connection here
2548 * if we do we only need to label the request_sock and the stack will
2549 * propogate the wire-label to the sock when it is created.
2552 addr.sin_addr.s_addr = hdr->saddr;
2554 if (smack_host_label(&addr) == NULL) {
2556 netlbl_secattr_init(&secattr);
2557 smack_to_secattr(smack, &secattr);
2558 rc = netlbl_req_setattr(req, &secattr);
2559 netlbl_secattr_destroy(&secattr);
2562 netlbl_req_delattr(req);
2569 * smack_inet_csk_clone - Copy the connection information to the new socket
2570 * @sk: the new socket
2571 * @req: the connection's request_sock
2573 * Transfer the connection's peer label to the newly created socket.
2575 static void smack_inet_csk_clone(struct sock *sk,
2576 const struct request_sock *req)
2578 struct socket_smack *ssp = sk->sk_security;
2581 if (req->peer_secid != 0) {
2582 smack = smack_from_secid(req->peer_secid);
2583 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2585 ssp->smk_packet[0] = '\0';
2589 * Key management security hooks
2591 * Casey has not tested key support very heavily.
2592 * The permission check is most likely too restrictive.
2593 * If you care about keys please have a look.
2598 * smack_key_alloc - Set the key security blob
2600 * @cred: the credentials to use
2603 * No allocation required
2607 static int smack_key_alloc(struct key *key, const struct cred *cred,
2608 unsigned long flags)
2610 key->security = cred->security;
2615 * smack_key_free - Clear the key security blob
2618 * Clear the blob pointer
2620 static void smack_key_free(struct key *key)
2622 key->security = NULL;
2626 * smack_key_permission - Smack access on a key
2627 * @key_ref: gets to the object
2628 * @cred: the credentials to use
2631 * Return 0 if the task has read and write to the object,
2632 * an error code otherwise
2634 static int smack_key_permission(key_ref_t key_ref,
2635 const struct cred *cred, key_perm_t perm)
2639 keyp = key_ref_to_ptr(key_ref);
2643 * If the key hasn't been initialized give it access so that
2646 if (keyp->security == NULL)
2649 * This should not occur
2651 if (cred->security == NULL)
2654 return smk_access(cred->security, keyp->security, MAY_READWRITE);
2656 #endif /* CONFIG_KEYS */
2661 * Audit requires a unique representation of each Smack specific
2662 * rule. This unique representation is used to distinguish the
2663 * object to be audited from remaining kernel objects and also
2664 * works as a glue between the audit hooks.
2666 * Since repository entries are added but never deleted, we'll use
2667 * the smack_known label address related to the given audit rule as
2668 * the needed unique representation. This also better fits the smack
2669 * model where nearly everything is a label.
2674 * smack_audit_rule_init - Initialize a smack audit rule
2675 * @field: audit rule fields given from user-space (audit.h)
2676 * @op: required testing operator (=, !=, >, <, ...)
2677 * @rulestr: smack label to be audited
2678 * @vrule: pointer to save our own audit rule representation
2680 * Prepare to audit cases where (@field @op @rulestr) is true.
2681 * The label to be audited is created if necessay.
2683 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2685 char **rule = (char **)vrule;
2688 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2691 if (op != Audit_equal && op != Audit_not_equal)
2694 *rule = smk_import(rulestr, 0);
2700 * smack_audit_rule_known - Distinguish Smack audit rules
2701 * @krule: rule of interest, in Audit kernel representation format
2703 * This is used to filter Smack rules from remaining Audit ones.
2704 * If it's proved that this rule belongs to us, the
2705 * audit_rule_match hook will be called to do the final judgement.
2707 static int smack_audit_rule_known(struct audit_krule *krule)
2709 struct audit_field *f;
2712 for (i = 0; i < krule->field_count; i++) {
2713 f = &krule->fields[i];
2715 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2723 * smack_audit_rule_match - Audit given object ?
2724 * @secid: security id for identifying the object to test
2725 * @field: audit rule flags given from user-space
2726 * @op: required testing operator
2727 * @vrule: smack internal rule presentation
2728 * @actx: audit context associated with the check
2730 * The core Audit hook. It's used to take the decision of
2731 * whether to audit or not to audit a given object.
2733 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2734 struct audit_context *actx)
2740 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2741 "Smack: missing rule\n");
2745 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2748 smack = smack_from_secid(secid);
2751 * No need to do string comparisons. If a match occurs,
2752 * both pointers will point to the same smack_known
2755 if (op == Audit_equal)
2756 return (rule == smack);
2757 if (op == Audit_not_equal)
2758 return (rule != smack);
2764 * smack_audit_rule_free - free smack rule representation
2765 * @vrule: rule to be freed.
2767 * No memory was allocated.
2769 static void smack_audit_rule_free(void *vrule)
2774 #endif /* CONFIG_AUDIT */
2777 * smack_secid_to_secctx - return the smack label for a secid
2778 * @secid: incoming integer
2779 * @secdata: destination
2780 * @seclen: how long it is
2782 * Exists for networking code.
2784 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2786 char *sp = smack_from_secid(secid);
2789 *seclen = strlen(sp);
2794 * smack_secctx_to_secid - return the secid for a smack label
2795 * @secdata: smack label
2796 * @seclen: how long result is
2797 * @secid: outgoing integer
2799 * Exists for audit and networking code.
2801 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2803 *secid = smack_to_secid(secdata);
2808 * smack_release_secctx - don't do anything.
2812 * Exists to make sure nothing gets done, and properly
2814 static void smack_release_secctx(char *secdata, u32 seclen)
2818 struct security_operations smack_ops = {
2821 .ptrace_may_access = smack_ptrace_may_access,
2822 .ptrace_traceme = smack_ptrace_traceme,
2823 .capget = cap_capget,
2824 .capset = cap_capset,
2825 .capable = cap_capable,
2826 .syslog = smack_syslog,
2827 .settime = cap_settime,
2828 .vm_enough_memory = cap_vm_enough_memory,
2830 .bprm_set_creds = cap_bprm_set_creds,
2831 .bprm_secureexec = cap_bprm_secureexec,
2833 .sb_alloc_security = smack_sb_alloc_security,
2834 .sb_free_security = smack_sb_free_security,
2835 .sb_copy_data = smack_sb_copy_data,
2836 .sb_kern_mount = smack_sb_kern_mount,
2837 .sb_statfs = smack_sb_statfs,
2838 .sb_mount = smack_sb_mount,
2839 .sb_umount = smack_sb_umount,
2841 .inode_alloc_security = smack_inode_alloc_security,
2842 .inode_free_security = smack_inode_free_security,
2843 .inode_init_security = smack_inode_init_security,
2844 .inode_link = smack_inode_link,
2845 .inode_unlink = smack_inode_unlink,
2846 .inode_rmdir = smack_inode_rmdir,
2847 .inode_rename = smack_inode_rename,
2848 .inode_permission = smack_inode_permission,
2849 .inode_setattr = smack_inode_setattr,
2850 .inode_getattr = smack_inode_getattr,
2851 .inode_setxattr = smack_inode_setxattr,
2852 .inode_post_setxattr = smack_inode_post_setxattr,
2853 .inode_getxattr = smack_inode_getxattr,
2854 .inode_removexattr = smack_inode_removexattr,
2855 .inode_need_killpriv = cap_inode_need_killpriv,
2856 .inode_killpriv = cap_inode_killpriv,
2857 .inode_getsecurity = smack_inode_getsecurity,
2858 .inode_setsecurity = smack_inode_setsecurity,
2859 .inode_listsecurity = smack_inode_listsecurity,
2860 .inode_getsecid = smack_inode_getsecid,
2862 .file_permission = smack_file_permission,
2863 .file_alloc_security = smack_file_alloc_security,
2864 .file_free_security = smack_file_free_security,
2865 .file_ioctl = smack_file_ioctl,
2866 .file_lock = smack_file_lock,
2867 .file_fcntl = smack_file_fcntl,
2868 .file_set_fowner = smack_file_set_fowner,
2869 .file_send_sigiotask = smack_file_send_sigiotask,
2870 .file_receive = smack_file_receive,
2872 .cred_free = smack_cred_free,
2873 .cred_prepare = smack_cred_prepare,
2874 .cred_commit = smack_cred_commit,
2875 .kernel_act_as = smack_kernel_act_as,
2876 .kernel_create_files_as = smack_kernel_create_files_as,
2877 .task_fix_setuid = cap_task_fix_setuid,
2878 .task_setpgid = smack_task_setpgid,
2879 .task_getpgid = smack_task_getpgid,
2880 .task_getsid = smack_task_getsid,
2881 .task_getsecid = smack_task_getsecid,
2882 .task_setnice = smack_task_setnice,
2883 .task_setioprio = smack_task_setioprio,
2884 .task_getioprio = smack_task_getioprio,
2885 .task_setscheduler = smack_task_setscheduler,
2886 .task_getscheduler = smack_task_getscheduler,
2887 .task_movememory = smack_task_movememory,
2888 .task_kill = smack_task_kill,
2889 .task_wait = smack_task_wait,
2890 .task_to_inode = smack_task_to_inode,
2891 .task_prctl = cap_task_prctl,
2893 .ipc_permission = smack_ipc_permission,
2894 .ipc_getsecid = smack_ipc_getsecid,
2896 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2897 .msg_msg_free_security = smack_msg_msg_free_security,
2899 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2900 .msg_queue_free_security = smack_msg_queue_free_security,
2901 .msg_queue_associate = smack_msg_queue_associate,
2902 .msg_queue_msgctl = smack_msg_queue_msgctl,
2903 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2904 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2906 .shm_alloc_security = smack_shm_alloc_security,
2907 .shm_free_security = smack_shm_free_security,
2908 .shm_associate = smack_shm_associate,
2909 .shm_shmctl = smack_shm_shmctl,
2910 .shm_shmat = smack_shm_shmat,
2912 .sem_alloc_security = smack_sem_alloc_security,
2913 .sem_free_security = smack_sem_free_security,
2914 .sem_associate = smack_sem_associate,
2915 .sem_semctl = smack_sem_semctl,
2916 .sem_semop = smack_sem_semop,
2918 .netlink_send = cap_netlink_send,
2919 .netlink_recv = cap_netlink_recv,
2921 .d_instantiate = smack_d_instantiate,
2923 .getprocattr = smack_getprocattr,
2924 .setprocattr = smack_setprocattr,
2926 .unix_stream_connect = smack_unix_stream_connect,
2927 .unix_may_send = smack_unix_may_send,
2929 .socket_post_create = smack_socket_post_create,
2930 .socket_connect = smack_socket_connect,
2931 .socket_sendmsg = smack_socket_sendmsg,
2932 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2933 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2934 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2935 .sk_alloc_security = smack_sk_alloc_security,
2936 .sk_free_security = smack_sk_free_security,
2937 .sock_graft = smack_sock_graft,
2938 .inet_conn_request = smack_inet_conn_request,
2939 .inet_csk_clone = smack_inet_csk_clone,
2941 /* key management security hooks */
2943 .key_alloc = smack_key_alloc,
2944 .key_free = smack_key_free,
2945 .key_permission = smack_key_permission,
2946 #endif /* CONFIG_KEYS */
2950 .audit_rule_init = smack_audit_rule_init,
2951 .audit_rule_known = smack_audit_rule_known,
2952 .audit_rule_match = smack_audit_rule_match,
2953 .audit_rule_free = smack_audit_rule_free,
2954 #endif /* CONFIG_AUDIT */
2956 .secid_to_secctx = smack_secid_to_secctx,
2957 .secctx_to_secid = smack_secctx_to_secid,
2958 .release_secctx = smack_release_secctx,
2962 static __init void init_smack_know_list(void)
2964 list_add(&smack_known_huh.list, &smack_known_list);
2965 list_add(&smack_known_hat.list, &smack_known_list);
2966 list_add(&smack_known_star.list, &smack_known_list);
2967 list_add(&smack_known_floor.list, &smack_known_list);
2968 list_add(&smack_known_invalid.list, &smack_known_list);
2969 list_add(&smack_known_web.list, &smack_known_list);
2973 * smack_init - initialize the smack system
2977 static __init int smack_init(void)
2981 if (!security_module_enable(&smack_ops))
2984 printk(KERN_INFO "Smack: Initializing.\n");
2987 * Set the security state for the initial task.
2989 cred = (struct cred *) current->cred;
2990 cred->security = &smack_known_floor.smk_known;
2992 /* initilize the smack_know_list */
2993 init_smack_know_list();
2997 spin_lock_init(&smack_known_huh.smk_cipsolock);
2998 spin_lock_init(&smack_known_hat.smk_cipsolock);
2999 spin_lock_init(&smack_known_star.smk_cipsolock);
3000 spin_lock_init(&smack_known_floor.smk_cipsolock);
3001 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3006 if (register_security(&smack_ops))
3007 panic("smack: Unable to register with kernel.\n");
3013 * Smack requires early initialization in order to label
3014 * all processes and objects when they are created.
3016 security_initcall(smack_init);