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>
33 * I hope these are the hokeyist lines of code in the module. Casey.
35 #define DEVPTS_SUPER_MAGIC 0x1cd1
36 #define SOCKFS_MAGIC 0x534F434B
37 #define TMPFS_MAGIC 0x01021994
40 * smk_fetch - Fetch the smack label from a file.
41 * @ip: a pointer to the inode
42 * @dp: a pointer to the dentry
44 * Returns a pointer to the master list entry for the Smack label
45 * or NULL if there was no label to fetch.
47 static char *smk_fetch(struct inode *ip, struct dentry *dp)
50 char in[SMK_LABELLEN];
52 if (ip->i_op->getxattr == NULL)
55 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
59 return smk_import(in, rc);
63 * new_inode_smack - allocate an inode security blob
64 * @smack: a pointer to the Smack label to use in the blob
66 * Returns the new blob or NULL if there's no memory available
68 struct inode_smack *new_inode_smack(char *smack)
70 struct inode_smack *isp;
72 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
76 isp->smk_inode = smack;
78 mutex_init(&isp->smk_lock);
89 * smack_ptrace - Smack approval on ptrace
90 * @ptp: parent task pointer
91 * @ctp: child task pointer
93 * Returns 0 if access is OK, an error code otherwise
95 * Do the capability checks, and require read and write.
97 static int smack_ptrace(struct task_struct *ptp, struct task_struct *ctp)
101 rc = cap_ptrace(ptp, ctp);
105 rc = smk_access(ptp->security, ctp->security, MAY_READWRITE);
106 if (rc != 0 && __capable(ptp, CAP_MAC_OVERRIDE))
113 * smack_syslog - Smack approval on syslog
114 * @type: message type
116 * Require that the task has the floor label
118 * Returns 0 on success, error code otherwise.
120 static int smack_syslog(int type)
123 char *sp = current->security;
125 rc = cap_syslog(type);
129 if (capable(CAP_MAC_OVERRIDE))
132 if (sp != smack_known_floor.smk_known)
144 * smack_sb_alloc_security - allocate a superblock blob
145 * @sb: the superblock getting the blob
147 * Returns 0 on success or -ENOMEM on error.
149 static int smack_sb_alloc_security(struct super_block *sb)
151 struct superblock_smack *sbsp;
153 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
158 sbsp->smk_root = smack_known_floor.smk_known;
159 sbsp->smk_default = smack_known_floor.smk_known;
160 sbsp->smk_floor = smack_known_floor.smk_known;
161 sbsp->smk_hat = smack_known_hat.smk_known;
162 sbsp->smk_initialized = 0;
163 spin_lock_init(&sbsp->smk_sblock);
165 sb->s_security = sbsp;
171 * smack_sb_free_security - free a superblock blob
172 * @sb: the superblock getting the blob
175 static void smack_sb_free_security(struct super_block *sb)
177 kfree(sb->s_security);
178 sb->s_security = NULL;
182 * smack_sb_copy_data - copy mount options data for processing
183 * @type: file system type
184 * @orig: where to start
187 * Returns 0 on success or -ENOMEM on error.
189 * Copy the Smack specific mount options out of the mount
192 static int smack_sb_copy_data(char *orig, char *smackopts)
194 char *cp, *commap, *otheropts, *dp;
196 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
197 if (otheropts == NULL)
200 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
201 if (strstr(cp, SMK_FSDEFAULT) == cp)
203 else if (strstr(cp, SMK_FSFLOOR) == cp)
205 else if (strstr(cp, SMK_FSHAT) == cp)
207 else if (strstr(cp, SMK_FSROOT) == cp)
212 commap = strchr(cp, ',');
221 strcpy(orig, otheropts);
222 free_page((unsigned long)otheropts);
228 * smack_sb_kern_mount - Smack specific mount processing
229 * @sb: the file system superblock
230 * @data: the smack mount options
232 * Returns 0 on success, an error code on failure
234 static int smack_sb_kern_mount(struct super_block *sb, void *data)
236 struct dentry *root = sb->s_root;
237 struct inode *inode = root->d_inode;
238 struct superblock_smack *sp = sb->s_security;
239 struct inode_smack *isp;
244 spin_lock(&sp->smk_sblock);
245 if (sp->smk_initialized != 0) {
246 spin_unlock(&sp->smk_sblock);
249 sp->smk_initialized = 1;
250 spin_unlock(&sp->smk_sblock);
252 for (op = data; op != NULL; op = commap) {
253 commap = strchr(op, ',');
257 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
258 op += strlen(SMK_FSHAT);
259 nsp = smk_import(op, 0);
262 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
263 op += strlen(SMK_FSFLOOR);
264 nsp = smk_import(op, 0);
267 } else if (strncmp(op, SMK_FSDEFAULT,
268 strlen(SMK_FSDEFAULT)) == 0) {
269 op += strlen(SMK_FSDEFAULT);
270 nsp = smk_import(op, 0);
272 sp->smk_default = nsp;
273 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
274 op += strlen(SMK_FSROOT);
275 nsp = smk_import(op, 0);
282 * Initialize the root inode.
284 isp = inode->i_security;
286 inode->i_security = new_inode_smack(sp->smk_root);
288 isp->smk_inode = sp->smk_root;
294 * smack_sb_statfs - Smack check on statfs
295 * @dentry: identifies the file system in question
297 * Returns 0 if current can read the floor of the filesystem,
298 * and error code otherwise
300 static int smack_sb_statfs(struct dentry *dentry)
302 struct superblock_smack *sbp = dentry->d_sb->s_security;
304 return smk_curacc(sbp->smk_floor, MAY_READ);
308 * smack_sb_mount - Smack check for mounting
315 * Returns 0 if current can write the floor of the filesystem
316 * being mounted on, an error code otherwise.
318 static int smack_sb_mount(char *dev_name, struct nameidata *nd,
319 char *type, unsigned long flags, void *data)
321 struct superblock_smack *sbp = nd->path.mnt->mnt_sb->s_security;
323 return smk_curacc(sbp->smk_floor, MAY_WRITE);
327 * smack_sb_umount - Smack check for unmounting
328 * @mnt: file system to unmount
331 * Returns 0 if current can write the floor of the filesystem
332 * being unmounted, an error code otherwise.
334 static int smack_sb_umount(struct vfsmount *mnt, int flags)
336 struct superblock_smack *sbp;
338 sbp = mnt->mnt_sb->s_security;
340 return smk_curacc(sbp->smk_floor, MAY_WRITE);
348 * smack_inode_alloc_security - allocate an inode blob
349 * @inode - the inode in need of a blob
351 * Returns 0 if it gets a blob, -ENOMEM otherwise
353 static int smack_inode_alloc_security(struct inode *inode)
355 inode->i_security = new_inode_smack(current->security);
356 if (inode->i_security == NULL)
362 * smack_inode_free_security - free an inode blob
363 * @inode - the inode with a blob
365 * Clears the blob pointer in inode
367 static void smack_inode_free_security(struct inode *inode)
369 kfree(inode->i_security);
370 inode->i_security = NULL;
374 * smack_inode_init_security - copy out the smack from an inode
377 * @name: where to put the attribute name
378 * @value: where to put the attribute value
379 * @len: where to put the length of the attribute
381 * Returns 0 if it all works out, -ENOMEM if there's no memory
383 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
384 char **name, void **value, size_t *len)
386 char *isp = smk_of_inode(inode);
389 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
395 *value = kstrdup(isp, GFP_KERNEL);
401 *len = strlen(isp) + 1;
407 * smack_inode_link - Smack check on link
408 * @old_dentry: the existing object
410 * @new_dentry: the new object
412 * Returns 0 if access is permitted, an error code otherwise
414 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
415 struct dentry *new_dentry)
420 isp = smk_of_inode(old_dentry->d_inode);
421 rc = smk_curacc(isp, MAY_WRITE);
423 if (rc == 0 && new_dentry->d_inode != NULL) {
424 isp = smk_of_inode(new_dentry->d_inode);
425 rc = smk_curacc(isp, MAY_WRITE);
432 * smack_inode_unlink - Smack check on inode deletion
433 * @dir: containing directory object
434 * @dentry: file to unlink
436 * Returns 0 if current can write the containing directory
437 * and the object, error code otherwise
439 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
441 struct inode *ip = dentry->d_inode;
445 * You need write access to the thing you're unlinking
447 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
450 * You also need write access to the containing directory
452 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
458 * smack_inode_rmdir - Smack check on directory deletion
459 * @dir: containing directory object
460 * @dentry: directory to unlink
462 * Returns 0 if current can write the containing directory
463 * and the directory, error code otherwise
465 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
470 * You need write access to the thing you're removing
472 rc = smk_curacc(smk_of_inode(dentry->d_inode), 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_rename - Smack check on rename
484 * @old_inode: the old directory
485 * @old_dentry: unused
486 * @new_inode: the new directory
487 * @new_dentry: unused
489 * Read and write access is required on both the old and
492 * Returns 0 if access is permitted, an error code otherwise
494 static int smack_inode_rename(struct inode *old_inode,
495 struct dentry *old_dentry,
496 struct inode *new_inode,
497 struct dentry *new_dentry)
502 isp = smk_of_inode(old_dentry->d_inode);
503 rc = smk_curacc(isp, MAY_READWRITE);
505 if (rc == 0 && new_dentry->d_inode != NULL) {
506 isp = smk_of_inode(new_dentry->d_inode);
507 rc = smk_curacc(isp, MAY_READWRITE);
514 * smack_inode_permission - Smack version of permission()
515 * @inode: the inode in question
516 * @mask: the access requested
519 * This is the important Smack hook.
521 * Returns 0 if access is permitted, -EACCES otherwise
523 static int smack_inode_permission(struct inode *inode, int mask,
524 struct nameidata *nd)
527 * No permission to check. Existence test. Yup, it's there.
532 return smk_curacc(smk_of_inode(inode), mask);
536 * smack_inode_setattr - Smack check for setting attributes
537 * @dentry: the object
538 * @iattr: for the force flag
540 * Returns 0 if access is permitted, an error code otherwise
542 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
545 * Need to allow for clearing the setuid bit.
547 if (iattr->ia_valid & ATTR_FORCE)
550 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
554 * smack_inode_getattr - Smack check for getting attributes
556 * @dentry: the object
558 * Returns 0 if access is permitted, an error code otherwise
560 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
562 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
566 * smack_inode_setxattr - Smack check for setting xattrs
567 * @dentry: the object
568 * @name: name of the attribute
573 * This protects the Smack attribute explicitly.
575 * Returns 0 if access is permitted, an error code otherwise
577 static int smack_inode_setxattr(struct dentry *dentry, char *name,
578 void *value, size_t size, int flags)
582 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
583 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
584 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
585 if (!capable(CAP_MAC_ADMIN))
588 rc = cap_inode_setxattr(dentry, name, value, size, flags);
591 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
597 * smack_inode_post_setxattr - Apply the Smack update approved above
599 * @name: attribute name
600 * @value: attribute value
601 * @size: attribute size
604 * Set the pointer in the inode blob to the entry found
605 * in the master label list.
607 static void smack_inode_post_setxattr(struct dentry *dentry, char *name,
608 void *value, size_t size, int flags)
610 struct inode_smack *isp;
616 if (strcmp(name, XATTR_NAME_SMACK))
619 if (size >= SMK_LABELLEN)
622 isp = dentry->d_inode->i_security;
625 * No locking is done here. This is a pointer
628 nsp = smk_import(value, size);
630 isp->smk_inode = nsp;
632 isp->smk_inode = smack_known_invalid.smk_known;
638 * smack_inode_getxattr - Smack check on getxattr
639 * @dentry: the object
642 * Returns 0 if access is permitted, an error code otherwise
644 static int smack_inode_getxattr(struct dentry *dentry, char *name)
646 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
650 * smack_inode_removexattr - Smack check on removexattr
651 * @dentry: the object
652 * @name: name of the attribute
654 * Removing the Smack attribute requires CAP_MAC_ADMIN
656 * Returns 0 if access is permitted, an error code otherwise
658 static int smack_inode_removexattr(struct dentry *dentry, char *name)
662 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
663 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
664 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
665 if (!capable(CAP_MAC_ADMIN))
668 rc = cap_inode_removexattr(dentry, name);
671 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
677 * smack_inode_getsecurity - get smack xattrs
679 * @name: attribute name
680 * @buffer: where to put the result
681 * @size: size of the buffer
684 * Returns the size of the attribute or an error code
686 static int smack_inode_getsecurity(const struct inode *inode,
687 const char *name, void **buffer,
690 struct socket_smack *ssp;
692 struct super_block *sbp;
693 struct inode *ip = (struct inode *)inode;
698 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
699 isp = smk_of_inode(inode);
700 ilen = strlen(isp) + 1;
706 * The rest of the Smack xattrs are only on sockets.
709 if (sbp->s_magic != SOCKFS_MAGIC)
713 if (sock == NULL || sock->sk == NULL)
716 ssp = sock->sk->sk_security;
718 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
720 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
725 ilen = strlen(isp) + 1;
736 * smack_inode_listsecurity - list the Smack attributes
738 * @buffer: where they go
739 * @buffer_size: size of buffer
741 * Returns 0 on success, -EINVAL otherwise
743 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
746 int len = strlen(XATTR_NAME_SMACK);
748 if (buffer != NULL && len <= buffer_size) {
749 memcpy(buffer, XATTR_NAME_SMACK, len);
760 * smack_file_permission - Smack check on file operations
766 * Should access checks be done on each read or write?
767 * UNICOS and SELinux say yes.
768 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
770 * I'll say no for now. Smack does not do the frequent
771 * label changing that SELinux does.
773 static int smack_file_permission(struct file *file, int mask)
779 * smack_file_alloc_security - assign a file security blob
782 * The security blob for a file is a pointer to the master
783 * label list, so no allocation is done.
787 static int smack_file_alloc_security(struct file *file)
789 file->f_security = current->security;
794 * smack_file_free_security - clear a file security blob
797 * The security blob for a file is a pointer to the master
798 * label list, so no memory is freed.
800 static void smack_file_free_security(struct file *file)
802 file->f_security = NULL;
806 * smack_file_ioctl - Smack check on ioctls
811 * Relies heavily on the correct use of the ioctl command conventions.
813 * Returns 0 if allowed, error code otherwise
815 static int smack_file_ioctl(struct file *file, unsigned int cmd,
820 if (_IOC_DIR(cmd) & _IOC_WRITE)
821 rc = smk_curacc(file->f_security, MAY_WRITE);
823 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
824 rc = smk_curacc(file->f_security, MAY_READ);
830 * smack_file_lock - Smack check on file locking
834 * Returns 0 if current has write access, error code otherwise
836 static int smack_file_lock(struct file *file, unsigned int cmd)
838 return smk_curacc(file->f_security, MAY_WRITE);
842 * smack_file_fcntl - Smack check on fcntl
844 * @cmd: what action to check
847 * Returns 0 if current has access, error code otherwise
849 static int smack_file_fcntl(struct file *file, unsigned int cmd,
861 rc = smk_curacc(file->f_security, MAY_READ);
869 rc = smk_curacc(file->f_security, MAY_WRITE);
872 rc = smk_curacc(file->f_security, MAY_READWRITE);
879 * smack_file_set_fowner - set the file security blob value
880 * @file: object in question
883 * Further research may be required on this one.
885 static int smack_file_set_fowner(struct file *file)
887 file->f_security = current->security;
892 * smack_file_send_sigiotask - Smack on sigio
893 * @tsk: The target task
894 * @fown: the object the signal come from
897 * Allow a privileged task to get signals even if it shouldn't
899 * Returns 0 if a subject with the object's smack could
900 * write to the task, an error code otherwise.
902 static int smack_file_send_sigiotask(struct task_struct *tsk,
903 struct fown_struct *fown, int signum)
909 * struct fown_struct is never outside the context of a struct file
911 file = container_of(fown, struct file, f_owner);
912 rc = smk_access(file->f_security, tsk->security, MAY_WRITE);
913 if (rc != 0 && __capable(tsk, CAP_MAC_OVERRIDE))
919 * smack_file_receive - Smack file receive check
922 * Returns 0 if current has access, error code otherwise
924 static int smack_file_receive(struct file *file)
929 * This code relies on bitmasks.
931 if (file->f_mode & FMODE_READ)
933 if (file->f_mode & FMODE_WRITE)
936 return smk_curacc(file->f_security, may);
944 * smack_task_alloc_security - "allocate" a task blob
945 * @tsk: the task in need of a blob
947 * Smack isn't using copies of blobs. Everyone
948 * points to an immutable list. No alloc required.
949 * No data copy required.
953 static int smack_task_alloc_security(struct task_struct *tsk)
955 tsk->security = current->security;
961 * smack_task_free_security - "free" a task blob
962 * @task: the task with the blob
964 * Smack isn't using copies of blobs. Everyone
965 * points to an immutable list. The blobs never go away.
966 * There is no leak here.
968 static void smack_task_free_security(struct task_struct *task)
970 task->security = NULL;
974 * smack_task_setpgid - Smack check on setting pgid
975 * @p: the task object
978 * Return 0 if write access is permitted
980 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
982 return smk_curacc(p->security, MAY_WRITE);
986 * smack_task_getpgid - Smack access check for getpgid
987 * @p: the object task
989 * Returns 0 if current can read the object task, error code otherwise
991 static int smack_task_getpgid(struct task_struct *p)
993 return smk_curacc(p->security, MAY_READ);
997 * smack_task_getsid - Smack access check for getsid
998 * @p: the object task
1000 * Returns 0 if current can read the object task, error code otherwise
1002 static int smack_task_getsid(struct task_struct *p)
1004 return smk_curacc(p->security, MAY_READ);
1008 * smack_task_getsecid - get the secid of the task
1009 * @p: the object task
1010 * @secid: where to put the result
1012 * Sets the secid to contain a u32 version of the smack label.
1014 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1016 *secid = smack_to_secid(p->security);
1020 * smack_task_setnice - Smack check on setting nice
1021 * @p: the task object
1024 * Return 0 if write access is permitted
1026 static int smack_task_setnice(struct task_struct *p, int nice)
1030 rc = cap_task_setnice(p, nice);
1032 rc = smk_curacc(p->security, MAY_WRITE);
1037 * smack_task_setioprio - Smack check on setting ioprio
1038 * @p: the task object
1041 * Return 0 if write access is permitted
1043 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1047 rc = cap_task_setioprio(p, ioprio);
1049 rc = smk_curacc(p->security, MAY_WRITE);
1054 * smack_task_getioprio - Smack check on reading ioprio
1055 * @p: the task object
1057 * Return 0 if read access is permitted
1059 static int smack_task_getioprio(struct task_struct *p)
1061 return smk_curacc(p->security, MAY_READ);
1065 * smack_task_setscheduler - Smack check on setting scheduler
1066 * @p: the task object
1070 * Return 0 if read access is permitted
1072 static int smack_task_setscheduler(struct task_struct *p, int policy,
1073 struct sched_param *lp)
1077 rc = cap_task_setscheduler(p, policy, lp);
1079 rc = smk_curacc(p->security, MAY_WRITE);
1084 * smack_task_getscheduler - Smack check on reading scheduler
1085 * @p: the task object
1087 * Return 0 if read access is permitted
1089 static int smack_task_getscheduler(struct task_struct *p)
1091 return smk_curacc(p->security, MAY_READ);
1095 * smack_task_movememory - Smack check on moving memory
1096 * @p: the task object
1098 * Return 0 if write access is permitted
1100 static int smack_task_movememory(struct task_struct *p)
1102 return smk_curacc(p->security, MAY_WRITE);
1106 * smack_task_kill - Smack check on signal delivery
1107 * @p: the task object
1110 * @secid: identifies the smack to use in lieu of current's
1112 * Return 0 if write access is permitted
1114 * The secid behavior is an artifact of an SELinux hack
1115 * in the USB code. Someday it may go away.
1117 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1122 rc = cap_task_kill(p, info, sig, secid);
1126 * Special cases where signals really ought to go through
1127 * in spite of policy. Stephen Smalley suggests it may
1128 * make sense to change the caller so that it doesn't
1129 * bother with the LSM hook in these cases.
1131 if (info != SEND_SIG_NOINFO &&
1132 (is_si_special(info) || SI_FROMKERNEL(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 = kstrdup(smack, GFP_ATOMIC);
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)
1511 char *ssp = smack_of_shm(shp);
1523 may = MAY_READWRITE;
1528 * System level information.
1535 return smk_curacc(ssp, may);
1539 * smack_shm_shmat - Smack access for shmat
1542 * @shmflg: access requested
1544 * Returns 0 if current has the requested access, error code otherwise
1546 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1549 char *ssp = smack_of_shm(shp);
1552 may = smack_flags_to_may(shmflg);
1553 return smk_curacc(ssp, may);
1557 * smack_of_sem - the smack pointer for the sem
1560 * Returns a pointer to the smack value
1562 static char *smack_of_sem(struct sem_array *sma)
1564 return (char *)sma->sem_perm.security;
1568 * smack_sem_alloc_security - Set the security blob for sem
1573 static int smack_sem_alloc_security(struct sem_array *sma)
1575 struct kern_ipc_perm *isp = &sma->sem_perm;
1577 isp->security = current->security;
1582 * smack_sem_free_security - Clear the security blob for sem
1585 * Clears the blob pointer
1587 static void smack_sem_free_security(struct sem_array *sma)
1589 struct kern_ipc_perm *isp = &sma->sem_perm;
1591 isp->security = NULL;
1595 * smack_sem_associate - Smack access check for sem
1597 * @semflg: access requested
1599 * Returns 0 if current has the requested access, error code otherwise
1601 static int smack_sem_associate(struct sem_array *sma, int semflg)
1603 char *ssp = smack_of_sem(sma);
1606 may = smack_flags_to_may(semflg);
1607 return smk_curacc(ssp, may);
1611 * smack_sem_shmctl - Smack access check for sem
1613 * @cmd: what it wants to do
1615 * Returns 0 if current has the requested access, error code otherwise
1617 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1619 char *ssp = smack_of_sem(sma);
1636 may = MAY_READWRITE;
1641 * System level information
1648 return smk_curacc(ssp, may);
1652 * smack_sem_semop - Smack checks of semaphore operations
1658 * Treated as read and write in all cases.
1660 * Returns 0 if access is allowed, error code otherwise
1662 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1663 unsigned nsops, int alter)
1665 char *ssp = smack_of_sem(sma);
1667 return smk_curacc(ssp, MAY_READWRITE);
1671 * smack_msg_alloc_security - Set the security blob for msg
1676 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1678 struct kern_ipc_perm *kisp = &msq->q_perm;
1680 kisp->security = current->security;
1685 * smack_msg_free_security - Clear the security blob for msg
1688 * Clears the blob pointer
1690 static void smack_msg_queue_free_security(struct msg_queue *msq)
1692 struct kern_ipc_perm *kisp = &msq->q_perm;
1694 kisp->security = NULL;
1698 * smack_of_msq - the smack pointer for the msq
1701 * Returns a pointer to the smack value
1703 static char *smack_of_msq(struct msg_queue *msq)
1705 return (char *)msq->q_perm.security;
1709 * smack_msg_queue_associate - Smack access check for msg_queue
1711 * @msqflg: access requested
1713 * Returns 0 if current has the requested access, error code otherwise
1715 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1717 char *msp = smack_of_msq(msq);
1720 may = smack_flags_to_may(msqflg);
1721 return smk_curacc(msp, may);
1725 * smack_msg_queue_msgctl - Smack access check for msg_queue
1727 * @cmd: what it wants to do
1729 * Returns 0 if current has the requested access, error code otherwise
1731 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1733 char *msp = smack_of_msq(msq);
1743 may = MAY_READWRITE;
1748 * System level information
1755 return smk_curacc(msp, may);
1759 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1762 * @msqflg: access requested
1764 * Returns 0 if current has the requested access, error code otherwise
1766 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1769 char *msp = smack_of_msq(msq);
1772 rc = smack_flags_to_may(msqflg);
1773 return smk_curacc(msp, rc);
1777 * smack_msg_queue_msgsnd - Smack access check for msg_queue
1784 * Returns 0 if current has read and write access, error code otherwise
1786 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1787 struct task_struct *target, long type, int mode)
1789 char *msp = smack_of_msq(msq);
1791 return smk_curacc(msp, MAY_READWRITE);
1795 * smack_ipc_permission - Smack access for ipc_permission()
1796 * @ipp: the object permissions
1797 * @flag: access requested
1799 * Returns 0 if current has read and write access, error code otherwise
1801 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1803 char *isp = ipp->security;
1806 may = smack_flags_to_may(flag);
1807 return smk_curacc(isp, may);
1810 /* module stacking operations */
1813 * smack_register_security - stack capability module
1814 * @name: module name
1815 * @ops: module operations - ignored
1817 * Allow the capability module to register.
1819 static int smack_register_security(const char *name,
1820 struct security_operations *ops)
1822 if (strcmp(name, "capability") != 0)
1825 printk(KERN_INFO "%s: Registering secondary module %s\n",
1832 * smack_d_instantiate - Make sure the blob is correct on an inode
1833 * @opt_dentry: unused
1834 * @inode: the object
1836 * Set the inode's security blob if it hasn't been done already.
1838 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1840 struct super_block *sbp;
1841 struct superblock_smack *sbsp;
1842 struct inode_smack *isp;
1843 char *csp = current->security;
1851 isp = inode->i_security;
1853 mutex_lock(&isp->smk_lock);
1855 * If the inode is already instantiated
1856 * take the quick way out
1858 if (isp->smk_flags & SMK_INODE_INSTANT)
1862 sbsp = sbp->s_security;
1864 * We're going to use the superblock default label
1865 * if there's no label on the file.
1867 final = sbsp->smk_default;
1870 * This is pretty hackish.
1871 * Casey says that we shouldn't have to do
1872 * file system specific code, but it does help
1873 * with keeping it simple.
1875 switch (sbp->s_magic) {
1878 * Casey says that it's a little embarassing
1879 * that the smack file system doesn't do
1880 * extended attributes.
1882 final = smack_known_star.smk_known;
1886 * Casey says pipes are easy (?)
1888 final = smack_known_star.smk_known;
1890 case DEVPTS_SUPER_MAGIC:
1892 * devpts seems content with the label of the task.
1893 * Programs that change smack have to treat the
1900 * Casey says sockets get the smack of the task.
1904 case PROC_SUPER_MAGIC:
1906 * Casey says procfs appears not to care.
1907 * The superblock default suffices.
1912 * Device labels should come from the filesystem,
1913 * but watch out, because they're volitile,
1914 * getting recreated on every reboot.
1916 final = smack_known_star.smk_known;
1920 * If a smack value has been set we want to use it,
1921 * but since tmpfs isn't giving us the opportunity
1922 * to set mount options simulate setting the
1923 * superblock default.
1927 * This isn't an understood special case.
1928 * Get the value from the xattr.
1930 * No xattr support means, alas, no SMACK label.
1931 * Use the aforeapplied default.
1932 * It would be curious if the label of the task
1933 * does not match that assigned.
1935 if (inode->i_op->getxattr == NULL)
1938 * Get the dentry for xattr.
1940 if (opt_dentry == NULL) {
1941 dp = d_find_alias(inode);
1945 dp = dget(opt_dentry);
1950 fetched = smk_fetch(inode, dp);
1951 if (fetched != NULL)
1959 isp->smk_inode = csp;
1961 isp->smk_inode = final;
1963 isp->smk_flags |= SMK_INODE_INSTANT;
1966 mutex_unlock(&isp->smk_lock);
1971 * smack_getprocattr - Smack process attribute access
1972 * @p: the object task
1973 * @name: the name of the attribute in /proc/.../attr
1974 * @value: where to put the result
1976 * Places a copy of the task Smack into value
1978 * Returns the length of the smack label or an error code
1980 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
1985 if (strcmp(name, "current") != 0)
1988 cp = kstrdup(p->security, GFP_KERNEL);
1998 * smack_setprocattr - Smack process attribute setting
1999 * @p: the object task
2000 * @name: the name of the attribute in /proc/.../attr
2001 * @value: the value to set
2002 * @size: the size of the value
2004 * Sets the Smack value of the task. Only setting self
2005 * is permitted and only with privilege
2007 * Returns the length of the smack label or an error code
2009 static int smack_setprocattr(struct task_struct *p, char *name,
2010 void *value, size_t size)
2014 if (!__capable(p, CAP_MAC_ADMIN))
2018 * Changing another process' Smack value is too dangerous
2019 * and supports no sane use case.
2024 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2027 if (strcmp(name, "current") != 0)
2030 newsmack = smk_import(value, size);
2031 if (newsmack == NULL)
2034 p->security = newsmack;
2039 * smack_unix_stream_connect - Smack access on UDS
2041 * @other: the other socket
2044 * Return 0 if a subject with the smack of sock could access
2045 * an object with the smack of other, otherwise an error code
2047 static int smack_unix_stream_connect(struct socket *sock,
2048 struct socket *other, struct sock *newsk)
2050 struct inode *sp = SOCK_INODE(sock);
2051 struct inode *op = SOCK_INODE(other);
2053 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2057 * smack_unix_may_send - Smack access on UDS
2059 * @other: the other socket
2061 * Return 0 if a subject with the smack of sock could access
2062 * an object with the smack of other, otherwise an error code
2064 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2066 struct inode *sp = SOCK_INODE(sock);
2067 struct inode *op = SOCK_INODE(other);
2069 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2073 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2075 * @sap: netlabel secattr
2076 * @sip: where to put the result
2078 * Copies a smack label into sip
2080 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2082 char smack[SMK_LABELLEN];
2085 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2087 * If there are flags but no level netlabel isn't
2088 * behaving the way we expect it to.
2090 * Without guidance regarding the smack value
2091 * for the packet fall back on the network
2094 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2098 * Get the categories, if any
2100 memset(smack, '\0', SMK_LABELLEN);
2101 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2103 pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2107 smack_catset_bit(pcat, smack);
2110 * If it is CIPSO using smack direct mapping
2111 * we are already done. WeeHee.
2113 if (sap->attr.mls.lvl == smack_cipso_direct) {
2114 memcpy(sip, smack, SMK_MAXLEN);
2118 * Look it up in the supplied table if it is not a direct mapping.
2120 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2125 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2129 * Returns 0 if the packet should be delivered, an error code otherwise
2131 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2133 struct netlbl_lsm_secattr secattr;
2134 struct socket_smack *ssp = sk->sk_security;
2135 char smack[SMK_LABELLEN];
2138 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2142 * Translate what netlabel gave us.
2144 memset(smack, '\0', SMK_LABELLEN);
2145 netlbl_secattr_init(&secattr);
2146 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2148 smack_from_secattr(&secattr, smack);
2150 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2151 netlbl_secattr_destroy(&secattr);
2153 * Receiving a packet requires that the other end
2154 * be able to write here. Read access is not required.
2155 * This is the simplist possible security model
2158 return smk_access(smack, ssp->smk_in, MAY_WRITE);
2162 * smack_socket_getpeersec_stream - pull in packet label
2164 * @optval: user's destination
2165 * @optlen: size thereof
2168 * returns zero on success, an error code otherwise
2170 static int smack_socket_getpeersec_stream(struct socket *sock,
2171 char __user *optval,
2172 int __user *optlen, unsigned len)
2174 struct socket_smack *ssp;
2178 ssp = sock->sk->sk_security;
2179 slen = strlen(ssp->smk_packet) + 1;
2183 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2186 if (put_user(slen, optlen) != 0)
2194 * smack_socket_getpeersec_dgram - pull in packet label
2197 * @secid: pointer to where to put the secid of the packet
2199 * Sets the netlabel socket state on sk from parent
2201 static int smack_socket_getpeersec_dgram(struct socket *sock,
2202 struct sk_buff *skb, u32 *secid)
2205 struct netlbl_lsm_secattr secattr;
2207 char smack[SMK_LABELLEN];
2208 int family = PF_INET;
2213 * Only works for families with packets.
2217 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2219 family = sk->sk_family;
2222 * Translate what netlabel gave us.
2224 memset(smack, '\0', SMK_LABELLEN);
2225 netlbl_secattr_init(&secattr);
2226 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2228 smack_from_secattr(&secattr, smack);
2229 netlbl_secattr_destroy(&secattr);
2232 * Give up if we couldn't get anything
2237 s = smack_to_secid(smack);
2246 * smack_sock_graft - graft access state between two sockets
2248 * @parent: donor socket
2250 * Sets the netlabel socket state on sk from parent
2252 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2254 struct socket_smack *ssp;
2260 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2263 ssp = sk->sk_security;
2264 ssp->smk_in = current->security;
2265 ssp->smk_out = current->security;
2266 ssp->smk_packet[0] = '\0';
2268 rc = smack_netlabel(sk);
2270 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2275 * smack_inet_conn_request - Smack access check on connect
2276 * @sk: socket involved
2280 * Returns 0 if a task with the packet label could write to
2281 * the socket, otherwise an error code
2283 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2284 struct request_sock *req)
2286 struct netlbl_lsm_secattr skb_secattr;
2287 struct socket_smack *ssp = sk->sk_security;
2288 char smack[SMK_LABELLEN];
2294 memset(smack, '\0', SMK_LABELLEN);
2295 netlbl_secattr_init(&skb_secattr);
2296 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2298 smack_from_secattr(&skb_secattr, smack);
2300 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2301 netlbl_secattr_destroy(&skb_secattr);
2303 * Receiving a packet requires that the other end
2304 * be able to write here. Read access is not required.
2306 * If the request is successful save the peer's label
2307 * so that SO_PEERCRED can report it.
2309 rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2311 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2317 * Key management security hooks
2319 * Casey has not tested key support very heavily.
2320 * The permission check is most likely too restrictive.
2321 * If you care about keys please have a look.
2326 * smack_key_alloc - Set the key security blob
2328 * @tsk: the task associated with the key
2331 * No allocation required
2335 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2336 unsigned long flags)
2338 key->security = tsk->security;
2343 * smack_key_free - Clear the key security blob
2346 * Clear the blob pointer
2348 static void smack_key_free(struct key *key)
2350 key->security = NULL;
2354 * smack_key_permission - Smack access on a key
2355 * @key_ref: gets to the object
2356 * @context: task involved
2359 * Return 0 if the task has read and write to the object,
2360 * an error code otherwise
2362 static int smack_key_permission(key_ref_t key_ref,
2363 struct task_struct *context, key_perm_t perm)
2367 keyp = key_ref_to_ptr(key_ref);
2371 * If the key hasn't been initialized give it access so that
2374 if (keyp->security == NULL)
2377 * This should not occur
2379 if (context->security == NULL)
2382 return smk_access(context->security, keyp->security, MAY_READWRITE);
2384 #endif /* CONFIG_KEYS */
2387 * smack_secid_to_secctx - return the smack label for a secid
2388 * @secid: incoming integer
2389 * @secdata: destination
2390 * @seclen: how long it is
2392 * Exists for networking code.
2394 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2396 char *sp = smack_from_secid(secid);
2399 *seclen = strlen(sp);
2404 * smack_secctx_to_secid - return the secid for a smack label
2405 * @secdata: smack label
2406 * @seclen: how long result is
2407 * @secid: outgoing integer
2409 * Exists for audit and networking code.
2411 static int smack_secctx_to_secid(char *secdata, u32 seclen, u32 *secid)
2413 *secid = smack_to_secid(secdata);
2418 * smack_release_secctx - don't do anything.
2423 * Exists to make sure nothing gets done, and properly
2425 static void smack_release_secctx(char *secdata, u32 seclen)
2429 static struct security_operations smack_ops = {
2430 .ptrace = smack_ptrace,
2431 .capget = cap_capget,
2432 .capset_check = cap_capset_check,
2433 .capset_set = cap_capset_set,
2434 .capable = cap_capable,
2435 .syslog = smack_syslog,
2436 .settime = cap_settime,
2437 .vm_enough_memory = cap_vm_enough_memory,
2439 .bprm_apply_creds = cap_bprm_apply_creds,
2440 .bprm_set_security = cap_bprm_set_security,
2441 .bprm_secureexec = cap_bprm_secureexec,
2443 .sb_alloc_security = smack_sb_alloc_security,
2444 .sb_free_security = smack_sb_free_security,
2445 .sb_copy_data = smack_sb_copy_data,
2446 .sb_kern_mount = smack_sb_kern_mount,
2447 .sb_statfs = smack_sb_statfs,
2448 .sb_mount = smack_sb_mount,
2449 .sb_umount = smack_sb_umount,
2451 .inode_alloc_security = smack_inode_alloc_security,
2452 .inode_free_security = smack_inode_free_security,
2453 .inode_init_security = smack_inode_init_security,
2454 .inode_link = smack_inode_link,
2455 .inode_unlink = smack_inode_unlink,
2456 .inode_rmdir = smack_inode_rmdir,
2457 .inode_rename = smack_inode_rename,
2458 .inode_permission = smack_inode_permission,
2459 .inode_setattr = smack_inode_setattr,
2460 .inode_getattr = smack_inode_getattr,
2461 .inode_setxattr = smack_inode_setxattr,
2462 .inode_post_setxattr = smack_inode_post_setxattr,
2463 .inode_getxattr = smack_inode_getxattr,
2464 .inode_removexattr = smack_inode_removexattr,
2465 .inode_need_killpriv = cap_inode_need_killpriv,
2466 .inode_killpriv = cap_inode_killpriv,
2467 .inode_getsecurity = smack_inode_getsecurity,
2468 .inode_setsecurity = smack_inode_setsecurity,
2469 .inode_listsecurity = smack_inode_listsecurity,
2471 .file_permission = smack_file_permission,
2472 .file_alloc_security = smack_file_alloc_security,
2473 .file_free_security = smack_file_free_security,
2474 .file_ioctl = smack_file_ioctl,
2475 .file_lock = smack_file_lock,
2476 .file_fcntl = smack_file_fcntl,
2477 .file_set_fowner = smack_file_set_fowner,
2478 .file_send_sigiotask = smack_file_send_sigiotask,
2479 .file_receive = smack_file_receive,
2481 .task_alloc_security = smack_task_alloc_security,
2482 .task_free_security = smack_task_free_security,
2483 .task_post_setuid = cap_task_post_setuid,
2484 .task_setpgid = smack_task_setpgid,
2485 .task_getpgid = smack_task_getpgid,
2486 .task_getsid = smack_task_getsid,
2487 .task_getsecid = smack_task_getsecid,
2488 .task_setnice = smack_task_setnice,
2489 .task_setioprio = smack_task_setioprio,
2490 .task_getioprio = smack_task_getioprio,
2491 .task_setscheduler = smack_task_setscheduler,
2492 .task_getscheduler = smack_task_getscheduler,
2493 .task_movememory = smack_task_movememory,
2494 .task_kill = smack_task_kill,
2495 .task_wait = smack_task_wait,
2496 .task_reparent_to_init = cap_task_reparent_to_init,
2497 .task_to_inode = smack_task_to_inode,
2499 .ipc_permission = smack_ipc_permission,
2501 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
2502 .msg_msg_free_security = smack_msg_msg_free_security,
2504 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
2505 .msg_queue_free_security = smack_msg_queue_free_security,
2506 .msg_queue_associate = smack_msg_queue_associate,
2507 .msg_queue_msgctl = smack_msg_queue_msgctl,
2508 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
2509 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
2511 .shm_alloc_security = smack_shm_alloc_security,
2512 .shm_free_security = smack_shm_free_security,
2513 .shm_associate = smack_shm_associate,
2514 .shm_shmctl = smack_shm_shmctl,
2515 .shm_shmat = smack_shm_shmat,
2517 .sem_alloc_security = smack_sem_alloc_security,
2518 .sem_free_security = smack_sem_free_security,
2519 .sem_associate = smack_sem_associate,
2520 .sem_semctl = smack_sem_semctl,
2521 .sem_semop = smack_sem_semop,
2523 .netlink_send = cap_netlink_send,
2524 .netlink_recv = cap_netlink_recv,
2526 .register_security = smack_register_security,
2528 .d_instantiate = smack_d_instantiate,
2530 .getprocattr = smack_getprocattr,
2531 .setprocattr = smack_setprocattr,
2533 .unix_stream_connect = smack_unix_stream_connect,
2534 .unix_may_send = smack_unix_may_send,
2536 .socket_post_create = smack_socket_post_create,
2537 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
2538 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
2539 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
2540 .sk_alloc_security = smack_sk_alloc_security,
2541 .sk_free_security = smack_sk_free_security,
2542 .sock_graft = smack_sock_graft,
2543 .inet_conn_request = smack_inet_conn_request,
2544 /* key management security hooks */
2546 .key_alloc = smack_key_alloc,
2547 .key_free = smack_key_free,
2548 .key_permission = smack_key_permission,
2549 #endif /* CONFIG_KEYS */
2550 .secid_to_secctx = smack_secid_to_secctx,
2551 .secctx_to_secid = smack_secctx_to_secid,
2552 .release_secctx = smack_release_secctx,
2556 * smack_init - initialize the smack system
2560 static __init int smack_init(void)
2562 printk(KERN_INFO "Smack: Initializing.\n");
2565 * Set the security state for the initial task.
2567 current->security = &smack_known_floor.smk_known;
2572 spin_lock_init(&smack_known_unset.smk_cipsolock);
2573 spin_lock_init(&smack_known_huh.smk_cipsolock);
2574 spin_lock_init(&smack_known_hat.smk_cipsolock);
2575 spin_lock_init(&smack_known_star.smk_cipsolock);
2576 spin_lock_init(&smack_known_floor.smk_cipsolock);
2577 spin_lock_init(&smack_known_invalid.smk_cipsolock);
2582 if (register_security(&smack_ops))
2583 panic("smack: Unable to register with kernel.\n");
2589 * Smack requires early initialization in order to label
2590 * all processes and objects when they are created.
2592 security_initcall(smack_init);