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>
33 #define task_security(task) (task_cred_xxx((task), security))
36 * I hope these are the hokeyist lines of code in the module. Casey.
38 #define DEVPTS_SUPER_MAGIC 0x1cd1
39 #define SOCKFS_MAGIC 0x534F434B
40 #define TMPFS_MAGIC 0x01021994
43 * smk_fetch - Fetch the smack label from a file.
44 * @ip: a pointer to the inode
45 * @dp: a pointer to the dentry
47 * Returns a pointer to the master list entry for the Smack label
48 * or NULL if there was no label to fetch.
50 static char *smk_fetch(struct inode *ip, struct dentry *dp)
53 char in[SMK_LABELLEN];
55 if (ip->i_op->getxattr == NULL)
58 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
62 return smk_import(in, rc);
66 * new_inode_smack - allocate an inode security blob
67 * @smack: a pointer to the Smack label to use in the blob
69 * Returns the new blob or NULL if there's no memory available
71 struct inode_smack *new_inode_smack(char *smack)
73 struct inode_smack *isp;
75 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
79 isp->smk_inode = smack;
81 mutex_init(&isp->smk_lock);
92 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
93 * @ctp: child task pointer
95 * Returns 0 if access is OK, an error code otherwise
97 * Do the capability checks, and require read and write.
99 static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
103 rc = cap_ptrace_may_access(ctp, mode);
107 rc = smk_access(current->cred->security, ctp->cred->security,
109 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
115 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
116 * @ptp: parent task pointer
118 * Returns 0 if access is OK, an error code otherwise
120 * Do the capability checks, and require read and write.
122 static int smack_ptrace_traceme(struct task_struct *ptp)
126 rc = cap_ptrace_traceme(ptp);
130 rc = smk_access(ptp->cred->security, current->cred->security,
132 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
138 * smack_syslog - Smack approval on syslog
139 * @type: message type
141 * Require that the task has the floor label
143 * Returns 0 on success, error code otherwise.
145 static int smack_syslog(int type)
148 char *sp = current_security();
150 rc = cap_syslog(type);
154 if (capable(CAP_MAC_OVERRIDE))
157 if (sp != smack_known_floor.smk_known)
169 * smack_sb_alloc_security - allocate a superblock blob
170 * @sb: the superblock getting the blob
172 * Returns 0 on success or -ENOMEM on error.
174 static int smack_sb_alloc_security(struct super_block *sb)
176 struct superblock_smack *sbsp;
178 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
183 sbsp->smk_root = smack_known_floor.smk_known;
184 sbsp->smk_default = smack_known_floor.smk_known;
185 sbsp->smk_floor = smack_known_floor.smk_known;
186 sbsp->smk_hat = smack_known_hat.smk_known;
187 sbsp->smk_initialized = 0;
188 spin_lock_init(&sbsp->smk_sblock);
190 sb->s_security = sbsp;
196 * smack_sb_free_security - free a superblock blob
197 * @sb: the superblock getting the blob
200 static void smack_sb_free_security(struct super_block *sb)
202 kfree(sb->s_security);
203 sb->s_security = NULL;
207 * smack_sb_copy_data - copy mount options data for processing
208 * @type: file system type
209 * @orig: where to start
212 * Returns 0 on success or -ENOMEM on error.
214 * Copy the Smack specific mount options out of the mount
217 static int smack_sb_copy_data(char *orig, char *smackopts)
219 char *cp, *commap, *otheropts, *dp;
221 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
222 if (otheropts == NULL)
225 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
226 if (strstr(cp, SMK_FSDEFAULT) == cp)
228 else if (strstr(cp, SMK_FSFLOOR) == cp)
230 else if (strstr(cp, SMK_FSHAT) == cp)
232 else if (strstr(cp, SMK_FSROOT) == cp)
237 commap = strchr(cp, ',');
246 strcpy(orig, otheropts);
247 free_page((unsigned long)otheropts);
253 * smack_sb_kern_mount - Smack specific mount processing
254 * @sb: the file system superblock
255 * @data: the smack mount options
257 * Returns 0 on success, an error code on failure
259 static int smack_sb_kern_mount(struct super_block *sb, void *data)
261 struct dentry *root = sb->s_root;
262 struct inode *inode = root->d_inode;
263 struct superblock_smack *sp = sb->s_security;
264 struct inode_smack *isp;
269 spin_lock(&sp->smk_sblock);
270 if (sp->smk_initialized != 0) {
271 spin_unlock(&sp->smk_sblock);
274 sp->smk_initialized = 1;
275 spin_unlock(&sp->smk_sblock);
277 for (op = data; op != NULL; op = commap) {
278 commap = strchr(op, ',');
282 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
283 op += strlen(SMK_FSHAT);
284 nsp = smk_import(op, 0);
287 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
288 op += strlen(SMK_FSFLOOR);
289 nsp = smk_import(op, 0);
292 } else if (strncmp(op, SMK_FSDEFAULT,
293 strlen(SMK_FSDEFAULT)) == 0) {
294 op += strlen(SMK_FSDEFAULT);
295 nsp = smk_import(op, 0);
297 sp->smk_default = nsp;
298 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
299 op += strlen(SMK_FSROOT);
300 nsp = smk_import(op, 0);
307 * Initialize the root inode.
309 isp = inode->i_security;
311 inode->i_security = new_inode_smack(sp->smk_root);
313 isp->smk_inode = sp->smk_root;
319 * smack_sb_statfs - Smack check on statfs
320 * @dentry: identifies the file system in question
322 * Returns 0 if current can read the floor of the filesystem,
323 * and error code otherwise
325 static int smack_sb_statfs(struct dentry *dentry)
327 struct superblock_smack *sbp = dentry->d_sb->s_security;
329 return smk_curacc(sbp->smk_floor, MAY_READ);
333 * smack_sb_mount - Smack check for mounting
340 * Returns 0 if current can write the floor of the filesystem
341 * being mounted on, an error code otherwise.
343 static int smack_sb_mount(char *dev_name, struct path *path,
344 char *type, unsigned long flags, void *data)
346 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
348 return smk_curacc(sbp->smk_floor, MAY_WRITE);
352 * smack_sb_umount - Smack check for unmounting
353 * @mnt: file system to unmount
356 * Returns 0 if current can write the floor of the filesystem
357 * being unmounted, an error code otherwise.
359 static int smack_sb_umount(struct vfsmount *mnt, int flags)
361 struct superblock_smack *sbp;
363 sbp = mnt->mnt_sb->s_security;
365 return smk_curacc(sbp->smk_floor, MAY_WRITE);
373 * smack_inode_alloc_security - allocate an inode blob
374 * @inode - the inode in need of a blob
376 * Returns 0 if it gets a blob, -ENOMEM otherwise
378 static int smack_inode_alloc_security(struct inode *inode)
380 inode->i_security = new_inode_smack(current_security());
381 if (inode->i_security == NULL)
387 * smack_inode_free_security - free an inode blob
388 * @inode - the inode with a blob
390 * Clears the blob pointer in inode
392 static void smack_inode_free_security(struct inode *inode)
394 kfree(inode->i_security);
395 inode->i_security = NULL;
399 * smack_inode_init_security - copy out the smack from an inode
402 * @name: where to put the attribute name
403 * @value: where to put the attribute value
404 * @len: where to put the length of the attribute
406 * Returns 0 if it all works out, -ENOMEM if there's no memory
408 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
409 char **name, void **value, size_t *len)
411 char *isp = smk_of_inode(inode);
414 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
420 *value = kstrdup(isp, GFP_KERNEL);
426 *len = strlen(isp) + 1;
432 * smack_inode_link - Smack check on link
433 * @old_dentry: the existing object
435 * @new_dentry: the new object
437 * Returns 0 if access is permitted, an error code otherwise
439 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
440 struct dentry *new_dentry)
445 isp = smk_of_inode(old_dentry->d_inode);
446 rc = smk_curacc(isp, MAY_WRITE);
448 if (rc == 0 && new_dentry->d_inode != NULL) {
449 isp = smk_of_inode(new_dentry->d_inode);
450 rc = smk_curacc(isp, MAY_WRITE);
457 * smack_inode_unlink - Smack check on inode deletion
458 * @dir: containing directory object
459 * @dentry: file to unlink
461 * Returns 0 if current can write the containing directory
462 * and the object, error code otherwise
464 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
466 struct inode *ip = dentry->d_inode;
470 * You need write access to the thing you're unlinking
472 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
475 * You also need write access to the containing directory
477 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
483 * smack_inode_rmdir - Smack check on directory deletion
484 * @dir: containing directory object
485 * @dentry: directory to unlink
487 * Returns 0 if current can write the containing directory
488 * and the directory, error code otherwise
490 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
495 * You need write access to the thing you're removing
497 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
500 * You also need write access to the containing directory
502 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
508 * smack_inode_rename - Smack check on rename
509 * @old_inode: the old directory
510 * @old_dentry: unused
511 * @new_inode: the new directory
512 * @new_dentry: unused
514 * Read and write access is required on both the old and
517 * Returns 0 if access is permitted, an error code otherwise
519 static int smack_inode_rename(struct inode *old_inode,
520 struct dentry *old_dentry,
521 struct inode *new_inode,
522 struct dentry *new_dentry)
527 isp = smk_of_inode(old_dentry->d_inode);
528 rc = smk_curacc(isp, MAY_READWRITE);
530 if (rc == 0 && new_dentry->d_inode != NULL) {
531 isp = smk_of_inode(new_dentry->d_inode);
532 rc = smk_curacc(isp, MAY_READWRITE);
539 * smack_inode_permission - Smack version of permission()
540 * @inode: the inode in question
541 * @mask: the access requested
544 * This is the important Smack hook.
546 * Returns 0 if access is permitted, -EACCES otherwise
548 static int smack_inode_permission(struct inode *inode, int mask)
551 * No permission to check. Existence test. Yup, it's there.
556 return smk_curacc(smk_of_inode(inode), mask);
560 * smack_inode_setattr - Smack check for setting attributes
561 * @dentry: the object
562 * @iattr: for the force flag
564 * Returns 0 if access is permitted, an error code otherwise
566 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
569 * Need to allow for clearing the setuid bit.
571 if (iattr->ia_valid & ATTR_FORCE)
574 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
578 * smack_inode_getattr - Smack check for getting attributes
580 * @dentry: the object
582 * Returns 0 if access is permitted, an error code otherwise
584 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
586 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
590 * smack_inode_setxattr - Smack check for setting xattrs
591 * @dentry: the object
592 * @name: name of the attribute
597 * This protects the Smack attribute explicitly.
599 * Returns 0 if access is permitted, an error code otherwise
601 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
602 const void *value, size_t size, int flags)
606 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
607 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
608 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
609 if (!capable(CAP_MAC_ADMIN))
612 rc = cap_inode_setxattr(dentry, name, value, size, flags);
615 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
621 * smack_inode_post_setxattr - Apply the Smack update approved above
623 * @name: attribute name
624 * @value: attribute value
625 * @size: attribute size
628 * Set the pointer in the inode blob to the entry found
629 * in the master label list.
631 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
632 const void *value, size_t size, int flags)
634 struct inode_smack *isp;
640 if (strcmp(name, XATTR_NAME_SMACK))
643 if (size >= SMK_LABELLEN)
646 isp = dentry->d_inode->i_security;
649 * No locking is done here. This is a pointer
652 nsp = smk_import(value, size);
654 isp->smk_inode = nsp;
656 isp->smk_inode = smack_known_invalid.smk_known;
662 * smack_inode_getxattr - Smack check on getxattr
663 * @dentry: the object
666 * Returns 0 if access is permitted, an error code otherwise
668 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
670 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
674 * smack_inode_removexattr - Smack check on removexattr
675 * @dentry: the object
676 * @name: name of the attribute
678 * Removing the Smack attribute requires CAP_MAC_ADMIN
680 * Returns 0 if access is permitted, an error code otherwise
682 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
686 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
687 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
688 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
689 if (!capable(CAP_MAC_ADMIN))
692 rc = cap_inode_removexattr(dentry, name);
695 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
701 * smack_inode_getsecurity - get smack xattrs
703 * @name: attribute name
704 * @buffer: where to put the result
705 * @size: size of the buffer
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_alloc_security - "allocate" a task cred blob
981 * @cred: the task creds in need of a blob
983 * Smack isn't using copies of blobs. Everyone
984 * points to an immutable list. No alloc required.
985 * No data copy required.
989 static int smack_cred_alloc_security(struct cred *cred)
991 cred->security = current_security();
996 * smack_cred_free - "free" task-level security credentials
997 * @cred: the credentials in question
999 * Smack isn't using copies of blobs. Everyone
1000 * points to an immutable list. The blobs never go away.
1001 * There is no leak here.
1003 static void smack_cred_free(struct cred *cred)
1005 cred->security = NULL;
1009 * smack_task_setpgid - Smack check on setting pgid
1010 * @p: the task object
1013 * Return 0 if write access is permitted
1015 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1017 return smk_curacc(task_security(p), MAY_WRITE);
1021 * smack_task_getpgid - Smack access check for getpgid
1022 * @p: the object task
1024 * Returns 0 if current can read the object task, error code otherwise
1026 static int smack_task_getpgid(struct task_struct *p)
1028 return smk_curacc(task_security(p), MAY_READ);
1032 * smack_task_getsid - Smack access check for getsid
1033 * @p: the object task
1035 * Returns 0 if current can read the object task, error code otherwise
1037 static int smack_task_getsid(struct task_struct *p)
1039 return smk_curacc(task_security(p), MAY_READ);
1043 * smack_task_getsecid - get the secid of the task
1044 * @p: the object task
1045 * @secid: where to put the result
1047 * Sets the secid to contain a u32 version of the smack label.
1049 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1051 *secid = smack_to_secid(task_security(p));
1055 * smack_task_setnice - Smack check on setting nice
1056 * @p: the task object
1059 * Return 0 if write access is permitted
1061 static int smack_task_setnice(struct task_struct *p, int nice)
1065 rc = cap_task_setnice(p, nice);
1067 rc = smk_curacc(task_security(p), MAY_WRITE);
1072 * smack_task_setioprio - Smack check on setting ioprio
1073 * @p: the task object
1076 * Return 0 if write access is permitted
1078 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1082 rc = cap_task_setioprio(p, ioprio);
1084 rc = smk_curacc(task_security(p), MAY_WRITE);
1089 * smack_task_getioprio - Smack check on reading ioprio
1090 * @p: the task object
1092 * Return 0 if read access is permitted
1094 static int smack_task_getioprio(struct task_struct *p)
1096 return smk_curacc(task_security(p), MAY_READ);
1100 * smack_task_setscheduler - Smack check on setting scheduler
1101 * @p: the task object
1105 * Return 0 if read access is permitted
1107 static int smack_task_setscheduler(struct task_struct *p, int policy,
1108 struct sched_param *lp)
1112 rc = cap_task_setscheduler(p, policy, lp);
1114 rc = smk_curacc(task_security(p), MAY_WRITE);
1119 * smack_task_getscheduler - Smack check on reading scheduler
1120 * @p: the task object
1122 * Return 0 if read access is permitted
1124 static int smack_task_getscheduler(struct task_struct *p)
1126 return smk_curacc(task_security(p), MAY_READ);
1130 * smack_task_movememory - Smack check on moving memory
1131 * @p: the task object
1133 * Return 0 if write access is permitted
1135 static int smack_task_movememory(struct task_struct *p)
1137 return smk_curacc(task_security(p), MAY_WRITE);
1141 * smack_task_kill - Smack check on signal delivery
1142 * @p: the task object
1145 * @secid: identifies the smack to use in lieu of current's
1147 * Return 0 if write access is permitted
1149 * The secid behavior is an artifact of an SELinux hack
1150 * in the USB code. Someday it may go away.
1152 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1156 * Sending a signal requires that the sender
1157 * can write the receiver.
1160 return smk_curacc(task_security(p), MAY_WRITE);
1162 * If the secid isn't 0 we're dealing with some USB IO
1163 * specific behavior. This is not clean. For one thing
1164 * we can't take privilege into account.
1166 return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE);
1170 * smack_task_wait - Smack access check for waiting
1171 * @p: task to wait for
1173 * Returns 0 if current can wait for p, error code otherwise
1175 static int smack_task_wait(struct task_struct *p)
1179 rc = smk_access(current_security(), task_security(p), MAY_WRITE);
1184 * Allow the operation to succeed if either task
1185 * has privilege to perform operations that might
1186 * account for the smack labels having gotten to
1187 * be different in the first place.
1189 * This breaks the strict subject/object access
1190 * control ideal, taking the object's privilege
1191 * state into account in the decision as well as
1194 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1201 * smack_task_to_inode - copy task smack into the inode blob
1202 * @p: task to copy from
1203 * inode: inode to copy to
1205 * Sets the smack pointer in the inode security blob
1207 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1209 struct inode_smack *isp = inode->i_security;
1210 isp->smk_inode = task_security(p);
1218 * smack_sk_alloc_security - Allocate a socket blob
1221 * @priority: memory allocation priority
1223 * Assign Smack pointers to current
1225 * Returns 0 on success, -ENOMEM is there's no memory
1227 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1229 char *csp = current_security();
1230 struct socket_smack *ssp;
1232 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1238 ssp->smk_packet[0] = '\0';
1240 sk->sk_security = ssp;
1246 * smack_sk_free_security - Free a socket blob
1249 * Clears the blob pointer
1251 static void smack_sk_free_security(struct sock *sk)
1253 kfree(sk->sk_security);
1257 * smack_set_catset - convert a capset to netlabel mls categories
1258 * @catset: the Smack categories
1259 * @sap: where to put the netlabel categories
1261 * Allocates and fills attr.mls.cat
1263 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1274 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1275 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1276 sap->attr.mls.cat->startbit = 0;
1278 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1279 for (m = 0x80; m != 0; m >>= 1, cat++) {
1282 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1288 * smack_to_secattr - fill a secattr from a smack value
1289 * @smack: the smack value
1290 * @nlsp: where the result goes
1292 * Casey says that CIPSO is good enough for now.
1293 * It can be used to effect.
1294 * It can also be abused to effect when necessary.
1295 * Appologies to the TSIG group in general and GW in particular.
1297 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1299 struct smack_cipso cipso;
1302 switch (smack_net_nltype) {
1303 case NETLBL_NLTYPE_CIPSOV4:
1304 nlsp->domain = smack;
1305 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1307 rc = smack_to_cipso(smack, &cipso);
1309 nlsp->attr.mls.lvl = cipso.smk_level;
1310 smack_set_catset(cipso.smk_catset, nlsp);
1312 nlsp->attr.mls.lvl = smack_cipso_direct;
1313 smack_set_catset(smack, nlsp);
1322 * smack_netlabel - Set the secattr on a socket
1325 * Convert the outbound smack value (smk_out) to a
1326 * secattr and attach it to the socket.
1328 * Returns 0 on success or an error code
1330 static int smack_netlabel(struct sock *sk)
1332 struct socket_smack *ssp;
1333 struct netlbl_lsm_secattr secattr;
1336 ssp = sk->sk_security;
1337 netlbl_secattr_init(&secattr);
1338 smack_to_secattr(ssp->smk_out, &secattr);
1339 rc = netlbl_sock_setattr(sk, &secattr);
1340 netlbl_secattr_destroy(&secattr);
1346 * smack_inode_setsecurity - set smack xattrs
1347 * @inode: the object
1348 * @name: attribute name
1349 * @value: attribute value
1350 * @size: size of the attribute
1353 * Sets the named attribute in the appropriate blob
1355 * Returns 0 on success, or an error code
1357 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1358 const void *value, size_t size, int flags)
1361 struct inode_smack *nsp = inode->i_security;
1362 struct socket_smack *ssp;
1363 struct socket *sock;
1366 if (value == NULL || size > SMK_LABELLEN)
1369 sp = smk_import(value, size);
1373 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1374 nsp->smk_inode = sp;
1378 * The rest of the Smack xattrs are only on sockets.
1380 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1383 sock = SOCKET_I(inode);
1384 if (sock == NULL || sock->sk == NULL)
1387 ssp = sock->sk->sk_security;
1389 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1391 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1393 rc = smack_netlabel(sock->sk);
1395 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1404 * smack_socket_post_create - finish socket setup
1406 * @family: protocol family
1411 * Sets the netlabel information on the socket
1413 * Returns 0 on success, and error code otherwise
1415 static int smack_socket_post_create(struct socket *sock, int family,
1416 int type, int protocol, int kern)
1418 if (family != PF_INET || sock->sk == NULL)
1421 * Set the outbound netlbl.
1423 return smack_netlabel(sock->sk);
1427 * smack_flags_to_may - convert S_ to MAY_ values
1428 * @flags: the S_ value
1430 * Returns the equivalent MAY_ value
1432 static int smack_flags_to_may(int flags)
1436 if (flags & S_IRUGO)
1438 if (flags & S_IWUGO)
1440 if (flags & S_IXUGO)
1447 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1452 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1454 msg->security = current_security();
1459 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1462 * Clears the blob pointer
1464 static void smack_msg_msg_free_security(struct msg_msg *msg)
1466 msg->security = NULL;
1470 * smack_of_shm - the smack pointer for the shm
1473 * Returns a pointer to the smack value
1475 static char *smack_of_shm(struct shmid_kernel *shp)
1477 return (char *)shp->shm_perm.security;
1481 * smack_shm_alloc_security - Set the security blob for shm
1486 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1488 struct kern_ipc_perm *isp = &shp->shm_perm;
1490 isp->security = current_security();
1495 * smack_shm_free_security - Clear the security blob for shm
1498 * Clears the blob pointer
1500 static void smack_shm_free_security(struct shmid_kernel *shp)
1502 struct kern_ipc_perm *isp = &shp->shm_perm;
1504 isp->security = NULL;
1508 * smack_shm_associate - Smack access check for shm
1510 * @shmflg: access requested
1512 * Returns 0 if current has the requested access, error code otherwise
1514 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1516 char *ssp = smack_of_shm(shp);
1519 may = smack_flags_to_may(shmflg);
1520 return smk_curacc(ssp, may);
1524 * smack_shm_shmctl - Smack access check for shm
1526 * @cmd: what it wants to do
1528 * Returns 0 if current has the requested access, error code otherwise
1530 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1544 may = MAY_READWRITE;
1549 * System level information.
1556 ssp = smack_of_shm(shp);
1557 return smk_curacc(ssp, may);
1561 * smack_shm_shmat - Smack access for shmat
1564 * @shmflg: access requested
1566 * Returns 0 if current has the requested access, error code otherwise
1568 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1571 char *ssp = smack_of_shm(shp);
1574 may = smack_flags_to_may(shmflg);
1575 return smk_curacc(ssp, may);
1579 * smack_of_sem - the smack pointer for the sem
1582 * Returns a pointer to the smack value
1584 static char *smack_of_sem(struct sem_array *sma)
1586 return (char *)sma->sem_perm.security;
1590 * smack_sem_alloc_security - Set the security blob for sem
1595 static int smack_sem_alloc_security(struct sem_array *sma)
1597 struct kern_ipc_perm *isp = &sma->sem_perm;
1599 isp->security = current_security();
1604 * smack_sem_free_security - Clear the security blob for sem
1607 * Clears the blob pointer
1609 static void smack_sem_free_security(struct sem_array *sma)
1611 struct kern_ipc_perm *isp = &sma->sem_perm;
1613 isp->security = NULL;
1617 * smack_sem_associate - Smack access check for sem
1619 * @semflg: access requested
1621 * Returns 0 if current has the requested access, error code otherwise
1623 static int smack_sem_associate(struct sem_array *sma, int semflg)
1625 char *ssp = smack_of_sem(sma);
1628 may = smack_flags_to_may(semflg);
1629 return smk_curacc(ssp, may);
1633 * smack_sem_shmctl - Smack access check for sem
1635 * @cmd: what it wants to do
1637 * Returns 0 if current has the requested access, error code otherwise
1639 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1658 may = MAY_READWRITE;
1663 * System level information
1670 ssp = smack_of_sem(sma);
1671 return smk_curacc(ssp, may);
1675 * smack_sem_semop - Smack checks of semaphore operations
1681 * Treated as read and write in all cases.
1683 * Returns 0 if access is allowed, error code otherwise
1685 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1686 unsigned nsops, int alter)
1688 char *ssp = smack_of_sem(sma);
1690 return smk_curacc(ssp, MAY_READWRITE);
1694 * smack_msg_alloc_security - Set the security blob for msg
1699 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1701 struct kern_ipc_perm *kisp = &msq->q_perm;
1703 kisp->security = current_security();
1708 * smack_msg_free_security - Clear the security blob for msg
1711 * Clears the blob pointer
1713 static void smack_msg_queue_free_security(struct msg_queue *msq)
1715 struct kern_ipc_perm *kisp = &msq->q_perm;
1717 kisp->security = NULL;
1721 * smack_of_msq - the smack pointer for the msq
1724 * Returns a pointer to the smack value
1726 static char *smack_of_msq(struct msg_queue *msq)
1728 return (char *)msq->q_perm.security;
1732 * smack_msg_queue_associate - Smack access check for msg_queue
1734 * @msqflg: access requested
1736 * Returns 0 if current has the requested access, error code otherwise
1738 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1740 char *msp = smack_of_msq(msq);
1743 may = smack_flags_to_may(msqflg);
1744 return smk_curacc(msp, may);
1748 * smack_msg_queue_msgctl - Smack access check for msg_queue
1750 * @cmd: what it wants to do
1752 * Returns 0 if current has the requested access, error code otherwise
1754 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1766 may = MAY_READWRITE;
1771 * System level information
1778 msp = smack_of_msq(msq);
1779 return smk_curacc(msp, may);
1783 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1786 * @msqflg: access requested
1788 * Returns 0 if current has the requested access, error code otherwise
1790 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1793 char *msp = smack_of_msq(msq);
1796 rc = smack_flags_to_may(msqflg);
1797 return smk_curacc(msp, rc);
1801 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1808 * Returns 0 if current has read and write access, error code otherwise
1810 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1811 struct task_struct *target, long type, int mode)
1813 char *msp = smack_of_msq(msq);
1815 return smk_curacc(msp, MAY_READWRITE);
1819 * smack_ipc_permission - Smack access for ipc_permission()
1820 * @ipp: the object permissions
1821 * @flag: access requested
1823 * Returns 0 if current has read and write access, error code otherwise
1825 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1827 char *isp = ipp->security;
1830 may = smack_flags_to_may(flag);
1831 return smk_curacc(isp, may);
1835 * smack_ipc_getsecid - Extract smack security id
1836 * @ipcp: the object permissions
1837 * @secid: where result will be saved
1839 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
1841 char *smack = ipp->security;
1843 *secid = smack_to_secid(smack);
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(task_security(p), 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)
2042 * Changing another process' Smack value is too dangerous
2043 * and supports no sane use case.
2048 if (!capable(CAP_MAC_ADMIN))
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->cred->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 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2187 netlbl_skbuff_err(skb, rc, 0);
2192 * smack_socket_getpeersec_stream - pull in packet label
2194 * @optval: user's destination
2195 * @optlen: size thereof
2198 * returns zero on success, an error code otherwise
2200 static int smack_socket_getpeersec_stream(struct socket *sock,
2201 char __user *optval,
2202 int __user *optlen, unsigned len)
2204 struct socket_smack *ssp;
2208 ssp = sock->sk->sk_security;
2209 slen = strlen(ssp->smk_packet) + 1;
2213 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2216 if (put_user(slen, optlen) != 0)
2224 * smack_socket_getpeersec_dgram - pull in packet label
2227 * @secid: pointer to where to put the secid of the packet
2229 * Sets the netlabel socket state on sk from parent
2231 static int smack_socket_getpeersec_dgram(struct socket *sock,
2232 struct sk_buff *skb, u32 *secid)
2235 struct netlbl_lsm_secattr secattr;
2237 char smack[SMK_LABELLEN];
2238 int family = PF_INET;
2243 * Only works for families with packets.
2247 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2249 family = sk->sk_family;
2252 * Translate what netlabel gave us.
2254 memset(smack, '\0', SMK_LABELLEN);
2255 netlbl_secattr_init(&secattr);
2256 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2258 smack_from_secattr(&secattr, smack);
2259 netlbl_secattr_destroy(&secattr);
2262 * Give up if we couldn't get anything
2267 s = smack_to_secid(smack);
2276 * smack_sock_graft - graft access state between two sockets
2278 * @parent: donor socket
2280 * Sets the netlabel socket state on sk from parent
2282 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2284 struct socket_smack *ssp;
2290 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2293 ssp = sk->sk_security;
2294 ssp->smk_in = ssp->smk_out = current_security();
2295 ssp->smk_packet[0] = '\0';
2297 rc = smack_netlabel(sk);
2299 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2304 * smack_inet_conn_request - Smack access check on connect
2305 * @sk: socket involved
2309 * Returns 0 if a task with the packet label could write to
2310 * the socket, otherwise an error code
2312 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2313 struct request_sock *req)
2315 struct netlbl_lsm_secattr skb_secattr;
2316 struct socket_smack *ssp = sk->sk_security;
2317 char smack[SMK_LABELLEN];
2323 memset(smack, '\0', SMK_LABELLEN);
2324 netlbl_secattr_init(&skb_secattr);
2325 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2327 smack_from_secattr(&skb_secattr, smack);
2329 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2330 netlbl_secattr_destroy(&skb_secattr);
2332 * Receiving a packet requires that the other end
2333 * be able to write here. Read access is not required.
2335 * If the request is successful save the peer's label
2336 * so that SO_PEERCRED can report it.
2338 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2340 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2346 * Key management security hooks
2348 * Casey has not tested key support very heavily.
2349 * The permission check is most likely too restrictive.
2350 * If you care about keys please have a look.
2355 * smack_key_alloc - Set the key security blob
2357 * @tsk: the task associated with the key
2360 * No allocation required
2364 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2365 unsigned long flags)
2367 key->security = tsk->cred->security;
2372 * smack_key_free - Clear the key security blob
2375 * Clear the blob pointer
2377 static void smack_key_free(struct key *key)
2379 key->security = NULL;
2383 * smack_key_permission - Smack access on a key
2384 * @key_ref: gets to the object
2385 * @context: task involved
2388 * Return 0 if the task has read and write to the object,
2389 * an error code otherwise
2391 static int smack_key_permission(key_ref_t key_ref,
2392 struct task_struct *context, key_perm_t perm)
2396 keyp = key_ref_to_ptr(key_ref);
2400 * If the key hasn't been initialized give it access so that
2403 if (keyp->security == NULL)
2406 * This should not occur
2408 if (context->cred->security == NULL)
2411 return smk_access(context->cred->security, keyp->security,
2414 #endif /* CONFIG_KEYS */
2419 * Audit requires a unique representation of each Smack specific
2420 * rule. This unique representation is used to distinguish the
2421 * object to be audited from remaining kernel objects and also
2422 * works as a glue between the audit hooks.
2424 * Since repository entries are added but never deleted, we'll use
2425 * the smack_known label address related to the given audit rule as
2426 * the needed unique representation. This also better fits the smack
2427 * model where nearly everything is a label.
2432 * smack_audit_rule_init - Initialize a smack audit rule
2433 * @field: audit rule fields given from user-space (audit.h)
2434 * @op: required testing operator (=, !=, >, <, ...)
2435 * @rulestr: smack label to be audited
2436 * @vrule: pointer to save our own audit rule representation
2438 * Prepare to audit cases where (@field @op @rulestr) is true.
2439 * The label to be audited is created if necessay.
2441 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2443 char **rule = (char **)vrule;
2446 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2449 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2452 *rule = smk_import(rulestr, 0);
2458 * smack_audit_rule_known - Distinguish Smack audit rules
2459 * @krule: rule of interest, in Audit kernel representation format
2461 * This is used to filter Smack rules from remaining Audit ones.
2462 * If it's proved that this rule belongs to us, the
2463 * audit_rule_match hook will be called to do the final judgement.
2465 static int smack_audit_rule_known(struct audit_krule *krule)
2467 struct audit_field *f;
2470 for (i = 0; i < krule->field_count; i++) {
2471 f = &krule->fields[i];
2473 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2481 * smack_audit_rule_match - Audit given object ?
2482 * @secid: security id for identifying the object to test
2483 * @field: audit rule flags given from user-space
2484 * @op: required testing operator
2485 * @vrule: smack internal rule presentation
2486 * @actx: audit context associated with the check
2488 * The core Audit hook. It's used to take the decision of
2489 * whether to audit or not to audit a given object.
2491 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2492 struct audit_context *actx)
2498 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2499 "Smack: missing rule\n");
2503 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2506 smack = smack_from_secid(secid);
2509 * No need to do string comparisons. If a match occurs,
2510 * both pointers will point to the same smack_known
2513 if (op == AUDIT_EQUAL)
2514 return (rule == smack);
2515 if (op == AUDIT_NOT_EQUAL)
2516 return (rule != smack);
2522 * smack_audit_rule_free - free smack rule representation
2523 * @vrule: rule to be freed.
2525 * No memory was allocated.
2527 static void smack_audit_rule_free(void *vrule)
2532 #endif /* CONFIG_AUDIT */
2535 * smack_secid_to_secctx - return the smack label for a secid
2536 * @secid: incoming integer
2537 * @secdata: destination
2538 * @seclen: how long it is
2540 * Exists for networking code.
2542 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2544 char *sp = smack_from_secid(secid);
2547 *seclen = strlen(sp);
2552 * smack_secctx_to_secid - return the secid for a smack label
2553 * @secdata: smack label
2554 * @seclen: how long result is
2555 * @secid: outgoing integer
2557 * Exists for audit and networking code.
2559 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2561 *secid = smack_to_secid(secdata);
2566 * smack_release_secctx - don't do anything.
2571 * Exists to make sure nothing gets done, and properly
2573 static void smack_release_secctx(char *secdata, u32 seclen)
2577 struct security_operations smack_ops = {
2580 .ptrace_may_access = smack_ptrace_may_access,
2581 .ptrace_traceme = smack_ptrace_traceme,
2582 .capget = cap_capget,
2583 .capset_check = cap_capset_check,
2584 .capset_set = cap_capset_set,
2585 .capable = cap_capable,
2586 .syslog = smack_syslog,
2587 .settime = cap_settime,
2588 .vm_enough_memory = cap_vm_enough_memory,
2590 .bprm_apply_creds = cap_bprm_apply_creds,
2591 .bprm_set_security = cap_bprm_set_security,
2592 .bprm_secureexec = cap_bprm_secureexec,
2594 .sb_alloc_security = smack_sb_alloc_security,
2595 .sb_free_security = smack_sb_free_security,
2596 .sb_copy_data = smack_sb_copy_data,
2597 .sb_kern_mount = smack_sb_kern_mount,
2598 .sb_statfs = smack_sb_statfs,
2599 .sb_mount = smack_sb_mount,
2600 .sb_umount = smack_sb_umount,
2602 .inode_alloc_security = smack_inode_alloc_security,
2603 .inode_free_security = smack_inode_free_security,
2604 .inode_init_security = smack_inode_init_security,
2605 .inode_link = smack_inode_link,
2606 .inode_unlink = smack_inode_unlink,
2607 .inode_rmdir = smack_inode_rmdir,
2608 .inode_rename = smack_inode_rename,
2609 .inode_permission = smack_inode_permission,
2610 .inode_setattr = smack_inode_setattr,
2611 .inode_getattr = smack_inode_getattr,
2612 .inode_setxattr = smack_inode_setxattr,
2613 .inode_post_setxattr = smack_inode_post_setxattr,
2614 .inode_getxattr = smack_inode_getxattr,
2615 .inode_removexattr = smack_inode_removexattr,
2616 .inode_need_killpriv = cap_inode_need_killpriv,
2617 .inode_killpriv = cap_inode_killpriv,
2618 .inode_getsecurity = smack_inode_getsecurity,
2619 .inode_setsecurity = smack_inode_setsecurity,
2620 .inode_listsecurity = smack_inode_listsecurity,
2621 .inode_getsecid = smack_inode_getsecid,
2623 .file_permission = smack_file_permission,
2624 .file_alloc_security = smack_file_alloc_security,
2625 .file_free_security = smack_file_free_security,
2626 .file_ioctl = smack_file_ioctl,
2627 .file_lock = smack_file_lock,
2628 .file_fcntl = smack_file_fcntl,
2629 .file_set_fowner = smack_file_set_fowner,
2630 .file_send_sigiotask = smack_file_send_sigiotask,
2631 .file_receive = smack_file_receive,
2633 .cred_alloc_security = smack_cred_alloc_security,
2634 .cred_free = smack_cred_free,
2635 .task_post_setuid = cap_task_post_setuid,
2636 .task_setpgid = smack_task_setpgid,
2637 .task_getpgid = smack_task_getpgid,
2638 .task_getsid = smack_task_getsid,
2639 .task_getsecid = smack_task_getsecid,
2640 .task_setnice = smack_task_setnice,
2641 .task_setioprio = smack_task_setioprio,
2642 .task_getioprio = smack_task_getioprio,
2643 .task_setscheduler = smack_task_setscheduler,
2644 .task_getscheduler = smack_task_getscheduler,
2645 .task_movememory = smack_task_movememory,
2646 .task_kill = smack_task_kill,
2647 .task_wait = smack_task_wait,
2648 .task_reparent_to_init = cap_task_reparent_to_init,
2649 .task_to_inode = smack_task_to_inode,
2650 .task_prctl = cap_task_prctl,
2652 .ipc_permission = smack_ipc_permission,
2653 .ipc_getsecid = smack_ipc_getsecid,
2655 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2656 .msg_msg_free_security = smack_msg_msg_free_security,
2658 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2659 .msg_queue_free_security = smack_msg_queue_free_security,
2660 .msg_queue_associate = smack_msg_queue_associate,
2661 .msg_queue_msgctl = smack_msg_queue_msgctl,
2662 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2663 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2665 .shm_alloc_security = smack_shm_alloc_security,
2666 .shm_free_security = smack_shm_free_security,
2667 .shm_associate = smack_shm_associate,
2668 .shm_shmctl = smack_shm_shmctl,
2669 .shm_shmat = smack_shm_shmat,
2671 .sem_alloc_security = smack_sem_alloc_security,
2672 .sem_free_security = smack_sem_free_security,
2673 .sem_associate = smack_sem_associate,
2674 .sem_semctl = smack_sem_semctl,
2675 .sem_semop = smack_sem_semop,
2677 .netlink_send = cap_netlink_send,
2678 .netlink_recv = cap_netlink_recv,
2680 .d_instantiate = smack_d_instantiate,
2682 .getprocattr = smack_getprocattr,
2683 .setprocattr = smack_setprocattr,
2685 .unix_stream_connect = smack_unix_stream_connect,
2686 .unix_may_send = smack_unix_may_send,
2688 .socket_post_create = smack_socket_post_create,
2689 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2690 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2691 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2692 .sk_alloc_security = smack_sk_alloc_security,
2693 .sk_free_security = smack_sk_free_security,
2694 .sock_graft = smack_sock_graft,
2695 .inet_conn_request = smack_inet_conn_request,
2697 /* key management security hooks */
2699 .key_alloc = smack_key_alloc,
2700 .key_free = smack_key_free,
2701 .key_permission = smack_key_permission,
2702 #endif /* CONFIG_KEYS */
2706 .audit_rule_init = smack_audit_rule_init,
2707 .audit_rule_known = smack_audit_rule_known,
2708 .audit_rule_match = smack_audit_rule_match,
2709 .audit_rule_free = smack_audit_rule_free,
2710 #endif /* CONFIG_AUDIT */
2712 .secid_to_secctx = smack_secid_to_secctx,
2713 .secctx_to_secid = smack_secctx_to_secid,
2714 .release_secctx = smack_release_secctx,
2718 * smack_init - initialize the smack system
2722 static __init int smack_init(void)
2724 if (!security_module_enable(&smack_ops))
2727 printk(KERN_INFO "Smack: Initializing.\n");
2730 * Set the security state for the initial task.
2732 current->cred->security = &smack_known_floor.smk_known;
2737 spin_lock_init(&smack_known_unset.smk_cipsolock);
2738 spin_lock_init(&smack_known_huh.smk_cipsolock);
2739 spin_lock_init(&smack_known_hat.smk_cipsolock);
2740 spin_lock_init(&smack_known_star.smk_cipsolock);
2741 spin_lock_init(&smack_known_floor.smk_cipsolock);
2742 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2747 if (register_security(&smack_ops))
2748 panic("smack: Unable to register with kernel.\n");
2754 * Smack requires early initialization in order to label
2755 * all processes and objects when they are created.
2757 security_initcall(smack_init);