2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
16 #include <linux/xattr.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/stat.h>
20 #include <linux/ext2_fs.h>
22 #include <asm/ioctls.h>
23 #include <linux/tcp.h>
24 #include <linux/udp.h>
25 #include <linux/mutex.h>
26 #include <linux/pipe_fs_i.h>
27 #include <net/netlabel.h>
28 #include <net/cipso_ipv4.h>
29 #include <linux/audit.h>
34 * I hope these are the hokeyist lines of code in the module. Casey.
36 #define DEVPTS_SUPER_MAGIC 0x1cd1
37 #define SOCKFS_MAGIC 0x534F434B
38 #define TMPFS_MAGIC 0x01021994
41 * smk_fetch - Fetch the smack label from a file.
42 * @ip: a pointer to the inode
43 * @dp: a pointer to the dentry
45 * Returns a pointer to the master list entry for the Smack label
46 * or NULL if there was no label to fetch.
48 static char *smk_fetch(struct inode *ip, struct dentry *dp)
51 char in[SMK_LABELLEN];
53 if (ip->i_op->getxattr == NULL)
56 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
60 return smk_import(in, rc);
64 * new_inode_smack - allocate an inode security blob
65 * @smack: a pointer to the Smack label to use in the blob
67 * Returns the new blob or NULL if there's no memory available
69 struct inode_smack *new_inode_smack(char *smack)
71 struct inode_smack *isp;
73 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
77 isp->smk_inode = smack;
79 mutex_init(&isp->smk_lock);
90 * smack_ptrace - Smack approval on ptrace
91 * @ptp: parent task pointer
92 * @ctp: child task pointer
94 * Returns 0 if access is OK, an error code otherwise
96 * Do the capability checks, and require read and write.
98 static int smack_ptrace(struct task_struct *ptp, struct task_struct *ctp,
103 rc = cap_ptrace(ptp, ctp, mode);
107 rc = smk_access(ptp->security, ctp->security, MAY_READWRITE);
108 if (rc != 0 && __capable(ptp, CAP_MAC_OVERRIDE))
115 * smack_syslog - Smack approval on syslog
116 * @type: message type
118 * Require that the task has the floor label
120 * Returns 0 on success, error code otherwise.
122 static int smack_syslog(int type)
125 char *sp = current->security;
127 rc = cap_syslog(type);
131 if (capable(CAP_MAC_OVERRIDE))
134 if (sp != smack_known_floor.smk_known)
146 * smack_sb_alloc_security - allocate a superblock blob
147 * @sb: the superblock getting the blob
149 * Returns 0 on success or -ENOMEM on error.
151 static int smack_sb_alloc_security(struct super_block *sb)
153 struct superblock_smack *sbsp;
155 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
160 sbsp->smk_root = smack_known_floor.smk_known;
161 sbsp->smk_default = smack_known_floor.smk_known;
162 sbsp->smk_floor = smack_known_floor.smk_known;
163 sbsp->smk_hat = smack_known_hat.smk_known;
164 sbsp->smk_initialized = 0;
165 spin_lock_init(&sbsp->smk_sblock);
167 sb->s_security = sbsp;
173 * smack_sb_free_security - free a superblock blob
174 * @sb: the superblock getting the blob
177 static void smack_sb_free_security(struct super_block *sb)
179 kfree(sb->s_security);
180 sb->s_security = NULL;
184 * smack_sb_copy_data - copy mount options data for processing
185 * @type: file system type
186 * @orig: where to start
189 * Returns 0 on success or -ENOMEM on error.
191 * Copy the Smack specific mount options out of the mount
194 static int smack_sb_copy_data(char *orig, char *smackopts)
196 char *cp, *commap, *otheropts, *dp;
198 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
199 if (otheropts == NULL)
202 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
203 if (strstr(cp, SMK_FSDEFAULT) == cp)
205 else if (strstr(cp, SMK_FSFLOOR) == cp)
207 else if (strstr(cp, SMK_FSHAT) == cp)
209 else if (strstr(cp, SMK_FSROOT) == cp)
214 commap = strchr(cp, ',');
223 strcpy(orig, otheropts);
224 free_page((unsigned long)otheropts);
230 * smack_sb_kern_mount - Smack specific mount processing
231 * @sb: the file system superblock
232 * @data: the smack mount options
234 * Returns 0 on success, an error code on failure
236 static int smack_sb_kern_mount(struct super_block *sb, void *data)
238 struct dentry *root = sb->s_root;
239 struct inode *inode = root->d_inode;
240 struct superblock_smack *sp = sb->s_security;
241 struct inode_smack *isp;
246 spin_lock(&sp->smk_sblock);
247 if (sp->smk_initialized != 0) {
248 spin_unlock(&sp->smk_sblock);
251 sp->smk_initialized = 1;
252 spin_unlock(&sp->smk_sblock);
254 for (op = data; op != NULL; op = commap) {
255 commap = strchr(op, ',');
259 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
260 op += strlen(SMK_FSHAT);
261 nsp = smk_import(op, 0);
264 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
265 op += strlen(SMK_FSFLOOR);
266 nsp = smk_import(op, 0);
269 } else if (strncmp(op, SMK_FSDEFAULT,
270 strlen(SMK_FSDEFAULT)) == 0) {
271 op += strlen(SMK_FSDEFAULT);
272 nsp = smk_import(op, 0);
274 sp->smk_default = nsp;
275 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
276 op += strlen(SMK_FSROOT);
277 nsp = smk_import(op, 0);
284 * Initialize the root inode.
286 isp = inode->i_security;
288 inode->i_security = new_inode_smack(sp->smk_root);
290 isp->smk_inode = sp->smk_root;
296 * smack_sb_statfs - Smack check on statfs
297 * @dentry: identifies the file system in question
299 * Returns 0 if current can read the floor of the filesystem,
300 * and error code otherwise
302 static int smack_sb_statfs(struct dentry *dentry)
304 struct superblock_smack *sbp = dentry->d_sb->s_security;
306 return smk_curacc(sbp->smk_floor, MAY_READ);
310 * smack_sb_mount - Smack check for mounting
317 * Returns 0 if current can write the floor of the filesystem
318 * being mounted on, an error code otherwise.
320 static int smack_sb_mount(char *dev_name, struct path *path,
321 char *type, unsigned long flags, void *data)
323 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
325 return smk_curacc(sbp->smk_floor, MAY_WRITE);
329 * smack_sb_umount - Smack check for unmounting
330 * @mnt: file system to unmount
333 * Returns 0 if current can write the floor of the filesystem
334 * being unmounted, an error code otherwise.
336 static int smack_sb_umount(struct vfsmount *mnt, int flags)
338 struct superblock_smack *sbp;
340 sbp = mnt->mnt_sb->s_security;
342 return smk_curacc(sbp->smk_floor, MAY_WRITE);
350 * smack_inode_alloc_security - allocate an inode blob
351 * @inode - the inode in need of a blob
353 * Returns 0 if it gets a blob, -ENOMEM otherwise
355 static int smack_inode_alloc_security(struct inode *inode)
357 inode->i_security = new_inode_smack(current->security);
358 if (inode->i_security == NULL)
364 * smack_inode_free_security - free an inode blob
365 * @inode - the inode with a blob
367 * Clears the blob pointer in inode
369 static void smack_inode_free_security(struct inode *inode)
371 kfree(inode->i_security);
372 inode->i_security = NULL;
376 * smack_inode_init_security - copy out the smack from an inode
379 * @name: where to put the attribute name
380 * @value: where to put the attribute value
381 * @len: where to put the length of the attribute
383 * Returns 0 if it all works out, -ENOMEM if there's no memory
385 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
386 char **name, void **value, size_t *len)
388 char *isp = smk_of_inode(inode);
391 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
397 *value = kstrdup(isp, GFP_KERNEL);
403 *len = strlen(isp) + 1;
409 * smack_inode_link - Smack check on link
410 * @old_dentry: the existing object
412 * @new_dentry: the new object
414 * Returns 0 if access is permitted, an error code otherwise
416 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
417 struct dentry *new_dentry)
422 isp = smk_of_inode(old_dentry->d_inode);
423 rc = smk_curacc(isp, MAY_WRITE);
425 if (rc == 0 && new_dentry->d_inode != NULL) {
426 isp = smk_of_inode(new_dentry->d_inode);
427 rc = smk_curacc(isp, MAY_WRITE);
434 * smack_inode_unlink - Smack check on inode deletion
435 * @dir: containing directory object
436 * @dentry: file to unlink
438 * Returns 0 if current can write the containing directory
439 * and the object, error code otherwise
441 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
443 struct inode *ip = dentry->d_inode;
447 * You need write access to the thing you're unlinking
449 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
452 * You also need write access to the containing directory
454 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
460 * smack_inode_rmdir - Smack check on directory deletion
461 * @dir: containing directory object
462 * @dentry: directory to unlink
464 * Returns 0 if current can write the containing directory
465 * and the directory, error code otherwise
467 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
472 * You need write access to the thing you're removing
474 rc = smk_curacc(smk_of_inode(dentry->d_inode), 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_rename - Smack check on rename
486 * @old_inode: the old directory
487 * @old_dentry: unused
488 * @new_inode: the new directory
489 * @new_dentry: unused
491 * Read and write access is required on both the old and
494 * Returns 0 if access is permitted, an error code otherwise
496 static int smack_inode_rename(struct inode *old_inode,
497 struct dentry *old_dentry,
498 struct inode *new_inode,
499 struct dentry *new_dentry)
504 isp = smk_of_inode(old_dentry->d_inode);
505 rc = smk_curacc(isp, MAY_READWRITE);
507 if (rc == 0 && new_dentry->d_inode != NULL) {
508 isp = smk_of_inode(new_dentry->d_inode);
509 rc = smk_curacc(isp, MAY_READWRITE);
516 * smack_inode_permission - Smack version of permission()
517 * @inode: the inode in question
518 * @mask: the access requested
521 * This is the important Smack hook.
523 * Returns 0 if access is permitted, -EACCES otherwise
525 static int smack_inode_permission(struct inode *inode, int mask,
526 struct nameidata *nd)
529 * No permission to check. Existence test. Yup, it's there.
534 return smk_curacc(smk_of_inode(inode), mask);
538 * smack_inode_setattr - Smack check for setting attributes
539 * @dentry: the object
540 * @iattr: for the force flag
542 * Returns 0 if access is permitted, an error code otherwise
544 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
547 * Need to allow for clearing the setuid bit.
549 if (iattr->ia_valid & ATTR_FORCE)
552 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
556 * smack_inode_getattr - Smack check for getting attributes
558 * @dentry: the object
560 * Returns 0 if access is permitted, an error code otherwise
562 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
564 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
568 * smack_inode_setxattr - Smack check for setting xattrs
569 * @dentry: the object
570 * @name: name of the attribute
575 * This protects the Smack attribute explicitly.
577 * Returns 0 if access is permitted, an error code otherwise
579 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
580 const void *value, size_t size, int flags)
584 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
585 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
586 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
587 if (!capable(CAP_MAC_ADMIN))
590 rc = cap_inode_setxattr(dentry, name, value, size, flags);
593 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
599 * smack_inode_post_setxattr - Apply the Smack update approved above
601 * @name: attribute name
602 * @value: attribute value
603 * @size: attribute size
606 * Set the pointer in the inode blob to the entry found
607 * in the master label list.
609 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
610 const void *value, size_t size, int flags)
612 struct inode_smack *isp;
618 if (strcmp(name, XATTR_NAME_SMACK))
621 if (size >= SMK_LABELLEN)
624 isp = dentry->d_inode->i_security;
627 * No locking is done here. This is a pointer
630 nsp = smk_import(value, size);
632 isp->smk_inode = nsp;
634 isp->smk_inode = smack_known_invalid.smk_known;
640 * smack_inode_getxattr - Smack check on getxattr
641 * @dentry: the object
644 * Returns 0 if access is permitted, an error code otherwise
646 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
648 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
652 * smack_inode_removexattr - Smack check on removexattr
653 * @dentry: the object
654 * @name: name of the attribute
656 * Removing the Smack attribute requires CAP_MAC_ADMIN
658 * Returns 0 if access is permitted, an error code otherwise
660 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
664 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
665 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
666 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
667 if (!capable(CAP_MAC_ADMIN))
670 rc = cap_inode_removexattr(dentry, name);
673 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
679 * smack_inode_getsecurity - get smack xattrs
681 * @name: attribute name
682 * @buffer: where to put the result
683 * @size: size of the buffer
686 * Returns the size of the attribute or an error code
688 static int smack_inode_getsecurity(const struct inode *inode,
689 const char *name, void **buffer,
692 struct socket_smack *ssp;
694 struct super_block *sbp;
695 struct inode *ip = (struct inode *)inode;
700 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
701 isp = smk_of_inode(inode);
702 ilen = strlen(isp) + 1;
708 * The rest of the Smack xattrs are only on sockets.
711 if (sbp->s_magic != SOCKFS_MAGIC)
715 if (sock == NULL || sock->sk == NULL)
718 ssp = sock->sk->sk_security;
720 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
722 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
727 ilen = strlen(isp) + 1;
738 * smack_inode_listsecurity - list the Smack attributes
740 * @buffer: where they go
741 * @buffer_size: size of buffer
743 * Returns 0 on success, -EINVAL otherwise
745 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
748 int len = strlen(XATTR_NAME_SMACK);
750 if (buffer != NULL && len <= buffer_size) {
751 memcpy(buffer, XATTR_NAME_SMACK, len);
758 * smack_inode_getsecid - Extract inode's security id
759 * @inode: inode to extract the info from
760 * @secid: where result will be saved
762 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
764 struct inode_smack *isp = inode->i_security;
766 *secid = smack_to_secid(isp->smk_inode);
774 * smack_file_permission - Smack check on file operations
780 * Should access checks be done on each read or write?
781 * UNICOS and SELinux say yes.
782 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
784 * I'll say no for now. Smack does not do the frequent
785 * label changing that SELinux does.
787 static int smack_file_permission(struct file *file, int mask)
793 * smack_file_alloc_security - assign a file security blob
796 * The security blob for a file is a pointer to the master
797 * label list, so no allocation is done.
801 static int smack_file_alloc_security(struct file *file)
803 file->f_security = current->security;
808 * smack_file_free_security - clear a file security blob
811 * The security blob for a file is a pointer to the master
812 * label list, so no memory is freed.
814 static void smack_file_free_security(struct file *file)
816 file->f_security = NULL;
820 * smack_file_ioctl - Smack check on ioctls
825 * Relies heavily on the correct use of the ioctl command conventions.
827 * Returns 0 if allowed, error code otherwise
829 static int smack_file_ioctl(struct file *file, unsigned int cmd,
834 if (_IOC_DIR(cmd) & _IOC_WRITE)
835 rc = smk_curacc(file->f_security, MAY_WRITE);
837 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
838 rc = smk_curacc(file->f_security, MAY_READ);
844 * smack_file_lock - Smack check on file locking
848 * Returns 0 if current has write access, error code otherwise
850 static int smack_file_lock(struct file *file, unsigned int cmd)
852 return smk_curacc(file->f_security, MAY_WRITE);
856 * smack_file_fcntl - Smack check on fcntl
858 * @cmd: what action to check
861 * Returns 0 if current has access, error code otherwise
863 static int smack_file_fcntl(struct file *file, unsigned int cmd,
875 rc = smk_curacc(file->f_security, MAY_READ);
883 rc = smk_curacc(file->f_security, MAY_WRITE);
886 rc = smk_curacc(file->f_security, MAY_READWRITE);
893 * smack_file_set_fowner - set the file security blob value
894 * @file: object in question
897 * Further research may be required on this one.
899 static int smack_file_set_fowner(struct file *file)
901 file->f_security = current->security;
906 * smack_file_send_sigiotask - Smack on sigio
907 * @tsk: The target task
908 * @fown: the object the signal come from
911 * Allow a privileged task to get signals even if it shouldn't
913 * Returns 0 if a subject with the object's smack could
914 * write to the task, an error code otherwise.
916 static int smack_file_send_sigiotask(struct task_struct *tsk,
917 struct fown_struct *fown, int signum)
923 * struct fown_struct is never outside the context of a struct file
925 file = container_of(fown, struct file, f_owner);
926 rc = smk_access(file->f_security, tsk->security, MAY_WRITE);
927 if (rc != 0 && __capable(tsk, CAP_MAC_OVERRIDE))
933 * smack_file_receive - Smack file receive check
936 * Returns 0 if current has access, error code otherwise
938 static int smack_file_receive(struct file *file)
943 * This code relies on bitmasks.
945 if (file->f_mode & FMODE_READ)
947 if (file->f_mode & FMODE_WRITE)
950 return smk_curacc(file->f_security, may);
958 * smack_task_alloc_security - "allocate" a task blob
959 * @tsk: the task in need of a blob
961 * Smack isn't using copies of blobs. Everyone
962 * points to an immutable list. No alloc required.
963 * No data copy required.
967 static int smack_task_alloc_security(struct task_struct *tsk)
969 tsk->security = current->security;
975 * smack_task_free_security - "free" a task blob
976 * @task: the task with the blob
978 * Smack isn't using copies of blobs. Everyone
979 * points to an immutable list. The blobs never go away.
980 * There is no leak here.
982 static void smack_task_free_security(struct task_struct *task)
984 task->security = NULL;
988 * smack_task_setpgid - Smack check on setting pgid
989 * @p: the task object
992 * Return 0 if write access is permitted
994 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
996 return smk_curacc(p->security, MAY_WRITE);
1000 * smack_task_getpgid - Smack access check for getpgid
1001 * @p: the object task
1003 * Returns 0 if current can read the object task, error code otherwise
1005 static int smack_task_getpgid(struct task_struct *p)
1007 return smk_curacc(p->security, MAY_READ);
1011 * smack_task_getsid - Smack access check for getsid
1012 * @p: the object task
1014 * Returns 0 if current can read the object task, error code otherwise
1016 static int smack_task_getsid(struct task_struct *p)
1018 return smk_curacc(p->security, MAY_READ);
1022 * smack_task_getsecid - get the secid of the task
1023 * @p: the object task
1024 * @secid: where to put the result
1026 * Sets the secid to contain a u32 version of the smack label.
1028 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1030 *secid = smack_to_secid(p->security);
1034 * smack_task_setnice - Smack check on setting nice
1035 * @p: the task object
1038 * Return 0 if write access is permitted
1040 static int smack_task_setnice(struct task_struct *p, int nice)
1044 rc = cap_task_setnice(p, nice);
1046 rc = smk_curacc(p->security, MAY_WRITE);
1051 * smack_task_setioprio - Smack check on setting ioprio
1052 * @p: the task object
1055 * Return 0 if write access is permitted
1057 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1061 rc = cap_task_setioprio(p, ioprio);
1063 rc = smk_curacc(p->security, MAY_WRITE);
1068 * smack_task_getioprio - Smack check on reading ioprio
1069 * @p: the task object
1071 * Return 0 if read access is permitted
1073 static int smack_task_getioprio(struct task_struct *p)
1075 return smk_curacc(p->security, MAY_READ);
1079 * smack_task_setscheduler - Smack check on setting scheduler
1080 * @p: the task object
1084 * Return 0 if read access is permitted
1086 static int smack_task_setscheduler(struct task_struct *p, int policy,
1087 struct sched_param *lp)
1091 rc = cap_task_setscheduler(p, policy, lp);
1093 rc = smk_curacc(p->security, MAY_WRITE);
1098 * smack_task_getscheduler - Smack check on reading scheduler
1099 * @p: the task object
1101 * Return 0 if read access is permitted
1103 static int smack_task_getscheduler(struct task_struct *p)
1105 return smk_curacc(p->security, MAY_READ);
1109 * smack_task_movememory - Smack check on moving memory
1110 * @p: the task object
1112 * Return 0 if write access is permitted
1114 static int smack_task_movememory(struct task_struct *p)
1116 return smk_curacc(p->security, MAY_WRITE);
1120 * smack_task_kill - Smack check on signal delivery
1121 * @p: the task object
1124 * @secid: identifies the smack to use in lieu of current's
1126 * Return 0 if write access is permitted
1128 * The secid behavior is an artifact of an SELinux hack
1129 * in the USB code. Someday it may go away.
1131 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1135 * Sending a signal requires that the sender
1136 * can write the receiver.
1139 return smk_curacc(p->security, MAY_WRITE);
1141 * If the secid isn't 0 we're dealing with some USB IO
1142 * specific behavior. This is not clean. For one thing
1143 * we can't take privilege into account.
1145 return smk_access(smack_from_secid(secid), p->security, MAY_WRITE);
1149 * smack_task_wait - Smack access check for waiting
1150 * @p: task to wait for
1152 * Returns 0 if current can wait for p, error code otherwise
1154 static int smack_task_wait(struct task_struct *p)
1158 rc = smk_access(current->security, p->security, MAY_WRITE);
1163 * Allow the operation to succeed if either task
1164 * has privilege to perform operations that might
1165 * account for the smack labels having gotten to
1166 * be different in the first place.
1168 * This breaks the strict subjet/object access
1169 * control ideal, taking the object's privilege
1170 * state into account in the decision as well as
1173 if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE))
1180 * smack_task_to_inode - copy task smack into the inode blob
1181 * @p: task to copy from
1182 * inode: inode to copy to
1184 * Sets the smack pointer in the inode security blob
1186 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1188 struct inode_smack *isp = inode->i_security;
1189 isp->smk_inode = p->security;
1197 * smack_sk_alloc_security - Allocate a socket blob
1200 * @priority: memory allocation priority
1202 * Assign Smack pointers to current
1204 * Returns 0 on success, -ENOMEM is there's no memory
1206 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1208 char *csp = current->security;
1209 struct socket_smack *ssp;
1211 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1217 ssp->smk_packet[0] = '\0';
1219 sk->sk_security = ssp;
1225 * smack_sk_free_security - Free a socket blob
1228 * Clears the blob pointer
1230 static void smack_sk_free_security(struct sock *sk)
1232 kfree(sk->sk_security);
1236 * smack_set_catset - convert a capset to netlabel mls categories
1237 * @catset: the Smack categories
1238 * @sap: where to put the netlabel categories
1240 * Allocates and fills attr.mls.cat
1242 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1253 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1254 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1255 sap->attr.mls.cat->startbit = 0;
1257 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1258 for (m = 0x80; m != 0; m >>= 1, cat++) {
1261 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1267 * smack_to_secattr - fill a secattr from a smack value
1268 * @smack: the smack value
1269 * @nlsp: where the result goes
1271 * Casey says that CIPSO is good enough for now.
1272 * It can be used to effect.
1273 * It can also be abused to effect when necessary.
1274 * Appologies to the TSIG group in general and GW in particular.
1276 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1278 struct smack_cipso cipso;
1281 switch (smack_net_nltype) {
1282 case NETLBL_NLTYPE_CIPSOV4:
1283 nlsp->domain = smack;
1284 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1286 rc = smack_to_cipso(smack, &cipso);
1288 nlsp->attr.mls.lvl = cipso.smk_level;
1289 smack_set_catset(cipso.smk_catset, nlsp);
1291 nlsp->attr.mls.lvl = smack_cipso_direct;
1292 smack_set_catset(smack, nlsp);
1301 * smack_netlabel - Set the secattr on a socket
1304 * Convert the outbound smack value (smk_out) to a
1305 * secattr and attach it to the socket.
1307 * Returns 0 on success or an error code
1309 static int smack_netlabel(struct sock *sk)
1311 struct socket_smack *ssp;
1312 struct netlbl_lsm_secattr secattr;
1315 ssp = sk->sk_security;
1316 netlbl_secattr_init(&secattr);
1317 smack_to_secattr(ssp->smk_out, &secattr);
1318 rc = netlbl_sock_setattr(sk, &secattr);
1319 netlbl_secattr_destroy(&secattr);
1325 * smack_inode_setsecurity - set smack xattrs
1326 * @inode: the object
1327 * @name: attribute name
1328 * @value: attribute value
1329 * @size: size of the attribute
1332 * Sets the named attribute in the appropriate blob
1334 * Returns 0 on success, or an error code
1336 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1337 const void *value, size_t size, int flags)
1340 struct inode_smack *nsp = inode->i_security;
1341 struct socket_smack *ssp;
1342 struct socket *sock;
1345 if (value == NULL || size > SMK_LABELLEN)
1348 sp = smk_import(value, size);
1352 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1353 nsp->smk_inode = sp;
1357 * The rest of the Smack xattrs are only on sockets.
1359 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1362 sock = SOCKET_I(inode);
1363 if (sock == NULL || sock->sk == NULL)
1366 ssp = sock->sk->sk_security;
1368 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1370 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1372 rc = smack_netlabel(sock->sk);
1374 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1383 * smack_socket_post_create - finish socket setup
1385 * @family: protocol family
1390 * Sets the netlabel information on the socket
1392 * Returns 0 on success, and error code otherwise
1394 static int smack_socket_post_create(struct socket *sock, int family,
1395 int type, int protocol, int kern)
1397 if (family != PF_INET || sock->sk == NULL)
1400 * Set the outbound netlbl.
1402 return smack_netlabel(sock->sk);
1406 * smack_flags_to_may - convert S_ to MAY_ values
1407 * @flags: the S_ value
1409 * Returns the equivalent MAY_ value
1411 static int smack_flags_to_may(int flags)
1415 if (flags & S_IRUGO)
1417 if (flags & S_IWUGO)
1419 if (flags & S_IXUGO)
1426 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1431 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1433 msg->security = current->security;
1438 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1441 * Clears the blob pointer
1443 static void smack_msg_msg_free_security(struct msg_msg *msg)
1445 msg->security = NULL;
1449 * smack_of_shm - the smack pointer for the shm
1452 * Returns a pointer to the smack value
1454 static char *smack_of_shm(struct shmid_kernel *shp)
1456 return (char *)shp->shm_perm.security;
1460 * smack_shm_alloc_security - Set the security blob for shm
1465 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1467 struct kern_ipc_perm *isp = &shp->shm_perm;
1469 isp->security = current->security;
1474 * smack_shm_free_security - Clear the security blob for shm
1477 * Clears the blob pointer
1479 static void smack_shm_free_security(struct shmid_kernel *shp)
1481 struct kern_ipc_perm *isp = &shp->shm_perm;
1483 isp->security = NULL;
1487 * smack_shm_associate - Smack access check for shm
1489 * @shmflg: access requested
1491 * Returns 0 if current has the requested access, error code otherwise
1493 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1495 char *ssp = smack_of_shm(shp);
1498 may = smack_flags_to_may(shmflg);
1499 return smk_curacc(ssp, may);
1503 * smack_shm_shmctl - Smack access check for shm
1505 * @cmd: what it wants to do
1507 * Returns 0 if current has the requested access, error code otherwise
1509 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1523 may = MAY_READWRITE;
1528 * System level information.
1535 ssp = smack_of_shm(shp);
1536 return smk_curacc(ssp, may);
1540 * smack_shm_shmat - Smack access for shmat
1543 * @shmflg: access requested
1545 * Returns 0 if current has the requested access, error code otherwise
1547 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1550 char *ssp = smack_of_shm(shp);
1553 may = smack_flags_to_may(shmflg);
1554 return smk_curacc(ssp, may);
1558 * smack_of_sem - the smack pointer for the sem
1561 * Returns a pointer to the smack value
1563 static char *smack_of_sem(struct sem_array *sma)
1565 return (char *)sma->sem_perm.security;
1569 * smack_sem_alloc_security - Set the security blob for sem
1574 static int smack_sem_alloc_security(struct sem_array *sma)
1576 struct kern_ipc_perm *isp = &sma->sem_perm;
1578 isp->security = current->security;
1583 * smack_sem_free_security - Clear the security blob for sem
1586 * Clears the blob pointer
1588 static void smack_sem_free_security(struct sem_array *sma)
1590 struct kern_ipc_perm *isp = &sma->sem_perm;
1592 isp->security = NULL;
1596 * smack_sem_associate - Smack access check for sem
1598 * @semflg: access requested
1600 * Returns 0 if current has the requested access, error code otherwise
1602 static int smack_sem_associate(struct sem_array *sma, int semflg)
1604 char *ssp = smack_of_sem(sma);
1607 may = smack_flags_to_may(semflg);
1608 return smk_curacc(ssp, may);
1612 * smack_sem_shmctl - Smack access check for sem
1614 * @cmd: what it wants to do
1616 * Returns 0 if current has the requested access, error code otherwise
1618 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1637 may = MAY_READWRITE;
1642 * System level information
1649 ssp = smack_of_sem(sma);
1650 return smk_curacc(ssp, may);
1654 * smack_sem_semop - Smack checks of semaphore operations
1660 * Treated as read and write in all cases.
1662 * Returns 0 if access is allowed, error code otherwise
1664 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1665 unsigned nsops, int alter)
1667 char *ssp = smack_of_sem(sma);
1669 return smk_curacc(ssp, MAY_READWRITE);
1673 * smack_msg_alloc_security - Set the security blob for msg
1678 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1680 struct kern_ipc_perm *kisp = &msq->q_perm;
1682 kisp->security = current->security;
1687 * smack_msg_free_security - Clear the security blob for msg
1690 * Clears the blob pointer
1692 static void smack_msg_queue_free_security(struct msg_queue *msq)
1694 struct kern_ipc_perm *kisp = &msq->q_perm;
1696 kisp->security = NULL;
1700 * smack_of_msq - the smack pointer for the msq
1703 * Returns a pointer to the smack value
1705 static char *smack_of_msq(struct msg_queue *msq)
1707 return (char *)msq->q_perm.security;
1711 * smack_msg_queue_associate - Smack access check for msg_queue
1713 * @msqflg: access requested
1715 * Returns 0 if current has the requested access, error code otherwise
1717 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1719 char *msp = smack_of_msq(msq);
1722 may = smack_flags_to_may(msqflg);
1723 return smk_curacc(msp, may);
1727 * smack_msg_queue_msgctl - Smack access check for msg_queue
1729 * @cmd: what it wants to do
1731 * Returns 0 if current has the requested access, error code otherwise
1733 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1745 may = MAY_READWRITE;
1750 * System level information
1757 msp = smack_of_msq(msq);
1758 return smk_curacc(msp, may);
1762 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1765 * @msqflg: access requested
1767 * Returns 0 if current has the requested access, error code otherwise
1769 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1772 char *msp = smack_of_msq(msq);
1775 rc = smack_flags_to_may(msqflg);
1776 return smk_curacc(msp, rc);
1780 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1787 * Returns 0 if current has read and write access, error code otherwise
1789 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1790 struct task_struct *target, long type, int mode)
1792 char *msp = smack_of_msq(msq);
1794 return smk_curacc(msp, MAY_READWRITE);
1798 * smack_ipc_permission - Smack access for ipc_permission()
1799 * @ipp: the object permissions
1800 * @flag: access requested
1802 * Returns 0 if current has read and write access, error code otherwise
1804 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1806 char *isp = ipp->security;
1809 may = smack_flags_to_may(flag);
1810 return smk_curacc(isp, may);
1814 * smack_ipc_getsecid - Extract smack security id
1815 * @ipcp: the object permissions
1816 * @secid: where result will be saved
1818 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1820 char *smack = ipp->security;
1822 *secid = smack_to_secid(smack);
1825 /* module stacking operations */
1828 * smack_register_security - stack capability module
1829 * @name: module name
1830 * @ops: module operations - ignored
1832 * Allow the capability module to register.
1834 static int smack_register_security(const char *name,
1835 struct security_operations *ops)
1837 if (strcmp(name, "capability") != 0)
1840 printk(KERN_INFO "%s: Registering secondary module %s\n",
1847 * smack_d_instantiate - Make sure the blob is correct on an inode
1848 * @opt_dentry: unused
1849 * @inode: the object
1851 * Set the inode's security blob if it hasn't been done already.
1853 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1855 struct super_block *sbp;
1856 struct superblock_smack *sbsp;
1857 struct inode_smack *isp;
1858 char *csp = current->security;
1866 isp = inode->i_security;
1868 mutex_lock(&isp->smk_lock);
1870 * If the inode is already instantiated
1871 * take the quick way out
1873 if (isp->smk_flags & SMK_INODE_INSTANT)
1877 sbsp = sbp->s_security;
1879 * We're going to use the superblock default label
1880 * if there's no label on the file.
1882 final = sbsp->smk_default;
1885 * If this is the root inode the superblock
1886 * may be in the process of initialization.
1887 * If that is the case use the root value out
1888 * of the superblock.
1890 if (opt_dentry->d_parent == opt_dentry) {
1891 isp->smk_inode = sbsp->smk_root;
1892 isp->smk_flags |= SMK_INODE_INSTANT;
1897 * This is pretty hackish.
1898 * Casey says that we shouldn't have to do
1899 * file system specific code, but it does help
1900 * with keeping it simple.
1902 switch (sbp->s_magic) {
1905 * Casey says that it's a little embarassing
1906 * that the smack file system doesn't do
1907 * extended attributes.
1909 final = smack_known_star.smk_known;
1913 * Casey says pipes are easy (?)
1915 final = smack_known_star.smk_known;
1917 case DEVPTS_SUPER_MAGIC:
1919 * devpts seems content with the label of the task.
1920 * Programs that change smack have to treat the
1927 * Casey says sockets get the smack of the task.
1931 case PROC_SUPER_MAGIC:
1933 * Casey says procfs appears not to care.
1934 * The superblock default suffices.
1939 * Device labels should come from the filesystem,
1940 * but watch out, because they're volitile,
1941 * getting recreated on every reboot.
1943 final = smack_known_star.smk_known;
1947 * If a smack value has been set we want to use it,
1948 * but since tmpfs isn't giving us the opportunity
1949 * to set mount options simulate setting the
1950 * superblock default.
1954 * This isn't an understood special case.
1955 * Get the value from the xattr.
1957 * No xattr support means, alas, no SMACK label.
1958 * Use the aforeapplied default.
1959 * It would be curious if the label of the task
1960 * does not match that assigned.
1962 if (inode->i_op->getxattr == NULL)
1965 * Get the dentry for xattr.
1967 if (opt_dentry == NULL) {
1968 dp = d_find_alias(inode);
1972 dp = dget(opt_dentry);
1977 fetched = smk_fetch(inode, dp);
1978 if (fetched != NULL)
1986 isp->smk_inode = csp;
1988 isp->smk_inode = final;
1990 isp->smk_flags |= SMK_INODE_INSTANT;
1993 mutex_unlock(&isp->smk_lock);
1998 * smack_getprocattr - Smack process attribute access
1999 * @p: the object task
2000 * @name: the name of the attribute in /proc/.../attr
2001 * @value: where to put the result
2003 * Places a copy of the task Smack into value
2005 * Returns the length of the smack label or an error code
2007 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2012 if (strcmp(name, "current") != 0)
2015 cp = kstrdup(p->security, GFP_KERNEL);
2025 * smack_setprocattr - Smack process attribute setting
2026 * @p: the object task
2027 * @name: the name of the attribute in /proc/.../attr
2028 * @value: the value to set
2029 * @size: the size of the value
2031 * Sets the Smack value of the task. Only setting self
2032 * is permitted and only with privilege
2034 * Returns the length of the smack label or an error code
2036 static int smack_setprocattr(struct task_struct *p, char *name,
2037 void *value, size_t size)
2041 if (!__capable(p, CAP_MAC_ADMIN))
2045 * Changing another process' Smack value is too dangerous
2046 * and supports no sane use case.
2051 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2054 if (strcmp(name, "current") != 0)
2057 newsmack = smk_import(value, size);
2058 if (newsmack == NULL)
2061 p->security = newsmack;
2066 * smack_unix_stream_connect - Smack access on UDS
2068 * @other: the other socket
2071 * Return 0 if a subject with the smack of sock could access
2072 * an object with the smack of other, otherwise an error code
2074 static int smack_unix_stream_connect(struct socket *sock,
2075 struct socket *other, struct sock *newsk)
2077 struct inode *sp = SOCK_INODE(sock);
2078 struct inode *op = SOCK_INODE(other);
2080 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2084 * smack_unix_may_send - Smack access on UDS
2086 * @other: the other socket
2088 * Return 0 if a subject with the smack of sock could access
2089 * an object with the smack of other, otherwise an error code
2091 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2093 struct inode *sp = SOCK_INODE(sock);
2094 struct inode *op = SOCK_INODE(other);
2096 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2100 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2102 * @sap: netlabel secattr
2103 * @sip: where to put the result
2105 * Copies a smack label into sip
2107 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2109 char smack[SMK_LABELLEN];
2112 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2114 * If there are flags but no level netlabel isn't
2115 * behaving the way we expect it to.
2117 * Without guidance regarding the smack value
2118 * for the packet fall back on the network
2121 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2125 * Get the categories, if any
2127 memset(smack, '\0', SMK_LABELLEN);
2128 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2130 pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2134 smack_catset_bit(pcat, smack);
2137 * If it is CIPSO using smack direct mapping
2138 * we are already done. WeeHee.
2140 if (sap->attr.mls.lvl == smack_cipso_direct) {
2141 memcpy(sip, smack, SMK_MAXLEN);
2145 * Look it up in the supplied table if it is not a direct mapping.
2147 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2152 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2156 * Returns 0 if the packet should be delivered, an error code otherwise
2158 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2160 struct netlbl_lsm_secattr secattr;
2161 struct socket_smack *ssp = sk->sk_security;
2162 char smack[SMK_LABELLEN];
2165 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2169 * Translate what netlabel gave us.
2171 memset(smack, '\0', SMK_LABELLEN);
2172 netlbl_secattr_init(&secattr);
2173 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2175 smack_from_secattr(&secattr, smack);
2177 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2178 netlbl_secattr_destroy(&secattr);
2180 * Receiving a packet requires that the other end
2181 * be able to write here. Read access is not required.
2182 * This is the simplist possible security model
2185 return smk_access(smack, ssp->smk_in, MAY_WRITE);
2189 * smack_socket_getpeersec_stream - pull in packet label
2191 * @optval: user's destination
2192 * @optlen: size thereof
2195 * returns zero on success, an error code otherwise
2197 static int smack_socket_getpeersec_stream(struct socket *sock,
2198 char __user *optval,
2199 int __user *optlen, unsigned len)
2201 struct socket_smack *ssp;
2205 ssp = sock->sk->sk_security;
2206 slen = strlen(ssp->smk_packet) + 1;
2210 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2213 if (put_user(slen, optlen) != 0)
2221 * smack_socket_getpeersec_dgram - pull in packet label
2224 * @secid: pointer to where to put the secid of the packet
2226 * Sets the netlabel socket state on sk from parent
2228 static int smack_socket_getpeersec_dgram(struct socket *sock,
2229 struct sk_buff *skb, u32 *secid)
2232 struct netlbl_lsm_secattr secattr;
2234 char smack[SMK_LABELLEN];
2235 int family = PF_INET;
2240 * Only works for families with packets.
2244 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2246 family = sk->sk_family;
2249 * Translate what netlabel gave us.
2251 memset(smack, '\0', SMK_LABELLEN);
2252 netlbl_secattr_init(&secattr);
2253 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2255 smack_from_secattr(&secattr, smack);
2256 netlbl_secattr_destroy(&secattr);
2259 * Give up if we couldn't get anything
2264 s = smack_to_secid(smack);
2273 * smack_sock_graft - graft access state between two sockets
2275 * @parent: donor socket
2277 * Sets the netlabel socket state on sk from parent
2279 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2281 struct socket_smack *ssp;
2287 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2290 ssp = sk->sk_security;
2291 ssp->smk_in = current->security;
2292 ssp->smk_out = current->security;
2293 ssp->smk_packet[0] = '\0';
2295 rc = smack_netlabel(sk);
2297 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2302 * smack_inet_conn_request - Smack access check on connect
2303 * @sk: socket involved
2307 * Returns 0 if a task with the packet label could write to
2308 * the socket, otherwise an error code
2310 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2311 struct request_sock *req)
2313 struct netlbl_lsm_secattr skb_secattr;
2314 struct socket_smack *ssp = sk->sk_security;
2315 char smack[SMK_LABELLEN];
2321 memset(smack, '\0', SMK_LABELLEN);
2322 netlbl_secattr_init(&skb_secattr);
2323 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2325 smack_from_secattr(&skb_secattr, smack);
2327 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2328 netlbl_secattr_destroy(&skb_secattr);
2330 * Receiving a packet requires that the other end
2331 * be able to write here. Read access is not required.
2333 * If the request is successful save the peer's label
2334 * so that SO_PEERCRED can report it.
2336 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2338 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2344 * Key management security hooks
2346 * Casey has not tested key support very heavily.
2347 * The permission check is most likely too restrictive.
2348 * If you care about keys please have a look.
2353 * smack_key_alloc - Set the key security blob
2355 * @tsk: the task associated with the key
2358 * No allocation required
2362 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2363 unsigned long flags)
2365 key->security = tsk->security;
2370 * smack_key_free - Clear the key security blob
2373 * Clear the blob pointer
2375 static void smack_key_free(struct key *key)
2377 key->security = NULL;
2381 * smack_key_permission - Smack access on a key
2382 * @key_ref: gets to the object
2383 * @context: task involved
2386 * Return 0 if the task has read and write to the object,
2387 * an error code otherwise
2389 static int smack_key_permission(key_ref_t key_ref,
2390 struct task_struct *context, key_perm_t perm)
2394 keyp = key_ref_to_ptr(key_ref);
2398 * If the key hasn't been initialized give it access so that
2401 if (keyp->security == NULL)
2404 * This should not occur
2406 if (context->security == NULL)
2409 return smk_access(context->security, keyp->security, MAY_READWRITE);
2411 #endif /* CONFIG_KEYS */
2416 * Audit requires a unique representation of each Smack specific
2417 * rule. This unique representation is used to distinguish the
2418 * object to be audited from remaining kernel objects and also
2419 * works as a glue between the audit hooks.
2421 * Since repository entries are added but never deleted, we'll use
2422 * the smack_known label address related to the given audit rule as
2423 * the needed unique representation. This also better fits the smack
2424 * model where nearly everything is a label.
2429 * smack_audit_rule_init - Initialize a smack audit rule
2430 * @field: audit rule fields given from user-space (audit.h)
2431 * @op: required testing operator (=, !=, >, <, ...)
2432 * @rulestr: smack label to be audited
2433 * @vrule: pointer to save our own audit rule representation
2435 * Prepare to audit cases where (@field @op @rulestr) is true.
2436 * The label to be audited is created if necessay.
2438 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2440 char **rule = (char **)vrule;
2443 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2446 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2449 *rule = smk_import(rulestr, 0);
2455 * smack_audit_rule_known - Distinguish Smack audit rules
2456 * @krule: rule of interest, in Audit kernel representation format
2458 * This is used to filter Smack rules from remaining Audit ones.
2459 * If it's proved that this rule belongs to us, the
2460 * audit_rule_match hook will be called to do the final judgement.
2462 static int smack_audit_rule_known(struct audit_krule *krule)
2464 struct audit_field *f;
2467 for (i = 0; i < krule->field_count; i++) {
2468 f = &krule->fields[i];
2470 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2478 * smack_audit_rule_match - Audit given object ?
2479 * @secid: security id for identifying the object to test
2480 * @field: audit rule flags given from user-space
2481 * @op: required testing operator
2482 * @vrule: smack internal rule presentation
2483 * @actx: audit context associated with the check
2485 * The core Audit hook. It's used to take the decision of
2486 * whether to audit or not to audit a given object.
2488 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2489 struct audit_context *actx)
2495 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2496 "Smack: missing rule\n");
2500 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2503 smack = smack_from_secid(secid);
2506 * No need to do string comparisons. If a match occurs,
2507 * both pointers will point to the same smack_known
2510 if (op == AUDIT_EQUAL)
2511 return (rule == smack);
2512 if (op == AUDIT_NOT_EQUAL)
2513 return (rule != smack);
2519 * smack_audit_rule_free - free smack rule representation
2520 * @vrule: rule to be freed.
2522 * No memory was allocated.
2524 static void smack_audit_rule_free(void *vrule)
2529 #endif /* CONFIG_AUDIT */
2532 * smack_secid_to_secctx - return the smack label for a secid
2533 * @secid: incoming integer
2534 * @secdata: destination
2535 * @seclen: how long it is
2537 * Exists for networking code.
2539 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2541 char *sp = smack_from_secid(secid);
2544 *seclen = strlen(sp);
2549 * smack_secctx_to_secid - return the secid for a smack label
2550 * @secdata: smack label
2551 * @seclen: how long result is
2552 * @secid: outgoing integer
2554 * Exists for audit and networking code.
2556 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2558 *secid = smack_to_secid(secdata);
2563 * smack_release_secctx - don't do anything.
2568 * Exists to make sure nothing gets done, and properly
2570 static void smack_release_secctx(char *secdata, u32 seclen)
2574 struct security_operations smack_ops = {
2577 .ptrace = smack_ptrace,
2578 .capget = cap_capget,
2579 .capset_check = cap_capset_check,
2580 .capset_set = cap_capset_set,
2581 .capable = cap_capable,
2582 .syslog = smack_syslog,
2583 .settime = cap_settime,
2584 .vm_enough_memory = cap_vm_enough_memory,
2586 .bprm_apply_creds = cap_bprm_apply_creds,
2587 .bprm_set_security = cap_bprm_set_security,
2588 .bprm_secureexec = cap_bprm_secureexec,
2590 .sb_alloc_security = smack_sb_alloc_security,
2591 .sb_free_security = smack_sb_free_security,
2592 .sb_copy_data = smack_sb_copy_data,
2593 .sb_kern_mount = smack_sb_kern_mount,
2594 .sb_statfs = smack_sb_statfs,
2595 .sb_mount = smack_sb_mount,
2596 .sb_umount = smack_sb_umount,
2598 .inode_alloc_security = smack_inode_alloc_security,
2599 .inode_free_security = smack_inode_free_security,
2600 .inode_init_security = smack_inode_init_security,
2601 .inode_link = smack_inode_link,
2602 .inode_unlink = smack_inode_unlink,
2603 .inode_rmdir = smack_inode_rmdir,
2604 .inode_rename = smack_inode_rename,
2605 .inode_permission = smack_inode_permission,
2606 .inode_setattr = smack_inode_setattr,
2607 .inode_getattr = smack_inode_getattr,
2608 .inode_setxattr = smack_inode_setxattr,
2609 .inode_post_setxattr = smack_inode_post_setxattr,
2610 .inode_getxattr = smack_inode_getxattr,
2611 .inode_removexattr = smack_inode_removexattr,
2612 .inode_need_killpriv = cap_inode_need_killpriv,
2613 .inode_killpriv = cap_inode_killpriv,
2614 .inode_getsecurity = smack_inode_getsecurity,
2615 .inode_setsecurity = smack_inode_setsecurity,
2616 .inode_listsecurity = smack_inode_listsecurity,
2617 .inode_getsecid = smack_inode_getsecid,
2619 .file_permission = smack_file_permission,
2620 .file_alloc_security = smack_file_alloc_security,
2621 .file_free_security = smack_file_free_security,
2622 .file_ioctl = smack_file_ioctl,
2623 .file_lock = smack_file_lock,
2624 .file_fcntl = smack_file_fcntl,
2625 .file_set_fowner = smack_file_set_fowner,
2626 .file_send_sigiotask = smack_file_send_sigiotask,
2627 .file_receive = smack_file_receive,
2629 .task_alloc_security = smack_task_alloc_security,
2630 .task_free_security = smack_task_free_security,
2631 .task_post_setuid = cap_task_post_setuid,
2632 .task_setpgid = smack_task_setpgid,
2633 .task_getpgid = smack_task_getpgid,
2634 .task_getsid = smack_task_getsid,
2635 .task_getsecid = smack_task_getsecid,
2636 .task_setnice = smack_task_setnice,
2637 .task_setioprio = smack_task_setioprio,
2638 .task_getioprio = smack_task_getioprio,
2639 .task_setscheduler = smack_task_setscheduler,
2640 .task_getscheduler = smack_task_getscheduler,
2641 .task_movememory = smack_task_movememory,
2642 .task_kill = smack_task_kill,
2643 .task_wait = smack_task_wait,
2644 .task_reparent_to_init = cap_task_reparent_to_init,
2645 .task_to_inode = smack_task_to_inode,
2646 .task_prctl = cap_task_prctl,
2648 .ipc_permission = smack_ipc_permission,
2649 .ipc_getsecid = smack_ipc_getsecid,
2651 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2652 .msg_msg_free_security = smack_msg_msg_free_security,
2654 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2655 .msg_queue_free_security = smack_msg_queue_free_security,
2656 .msg_queue_associate = smack_msg_queue_associate,
2657 .msg_queue_msgctl = smack_msg_queue_msgctl,
2658 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2659 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2661 .shm_alloc_security = smack_shm_alloc_security,
2662 .shm_free_security = smack_shm_free_security,
2663 .shm_associate = smack_shm_associate,
2664 .shm_shmctl = smack_shm_shmctl,
2665 .shm_shmat = smack_shm_shmat,
2667 .sem_alloc_security = smack_sem_alloc_security,
2668 .sem_free_security = smack_sem_free_security,
2669 .sem_associate = smack_sem_associate,
2670 .sem_semctl = smack_sem_semctl,
2671 .sem_semop = smack_sem_semop,
2673 .netlink_send = cap_netlink_send,
2674 .netlink_recv = cap_netlink_recv,
2676 .register_security = smack_register_security,
2678 .d_instantiate = smack_d_instantiate,
2680 .getprocattr = smack_getprocattr,
2681 .setprocattr = smack_setprocattr,
2683 .unix_stream_connect = smack_unix_stream_connect,
2684 .unix_may_send = smack_unix_may_send,
2686 .socket_post_create = smack_socket_post_create,
2687 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2688 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2689 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2690 .sk_alloc_security = smack_sk_alloc_security,
2691 .sk_free_security = smack_sk_free_security,
2692 .sock_graft = smack_sock_graft,
2693 .inet_conn_request = smack_inet_conn_request,
2695 /* key management security hooks */
2697 .key_alloc = smack_key_alloc,
2698 .key_free = smack_key_free,
2699 .key_permission = smack_key_permission,
2700 #endif /* CONFIG_KEYS */
2704 .audit_rule_init = smack_audit_rule_init,
2705 .audit_rule_known = smack_audit_rule_known,
2706 .audit_rule_match = smack_audit_rule_match,
2707 .audit_rule_free = smack_audit_rule_free,
2708 #endif /* CONFIG_AUDIT */
2710 .secid_to_secctx = smack_secid_to_secctx,
2711 .secctx_to_secid = smack_secctx_to_secid,
2712 .release_secctx = smack_release_secctx,
2716 * smack_init - initialize the smack system
2720 static __init int smack_init(void)
2722 if (!security_module_enable(&smack_ops))
2725 printk(KERN_INFO "Smack: Initializing.\n");
2728 * Set the security state for the initial task.
2730 current->security = &smack_known_floor.smk_known;
2735 spin_lock_init(&smack_known_unset.smk_cipsolock);
2736 spin_lock_init(&smack_known_huh.smk_cipsolock);
2737 spin_lock_init(&smack_known_hat.smk_cipsolock);
2738 spin_lock_init(&smack_known_star.smk_cipsolock);
2739 spin_lock_init(&smack_known_floor.smk_cipsolock);
2740 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2745 if (register_security(&smack_ops))
2746 panic("smack: Unable to register with kernel.\n");
2752 * Smack requires early initialization in order to label
2753 * all processes and objects when they are created.
2755 security_initcall(smack_init);