msm: dma: add 7x30 security domain abstraction
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / smack / smack_lsm.c
blobfdfeaa2f28ec641bc5b86f24c765d6ff49d2d702
1 /*
2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
6 * Author:
7 * Casey Schaufler <casey@schaufler-ca.com>
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
10 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
11 * Paul Moore <paul.moore@hp.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2,
15 * as published by the Free Software Foundation.
18 #include <linux/xattr.h>
19 #include <linux/pagemap.h>
20 #include <linux/mount.h>
21 #include <linux/stat.h>
22 #include <linux/ext2_fs.h>
23 #include <linux/kd.h>
24 #include <asm/ioctls.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/pipe_fs_i.h>
31 #include <net/netlabel.h>
32 #include <net/cipso_ipv4.h>
33 #include <linux/audit.h>
34 #include <linux/magic.h>
35 #include "smack.h"
37 #define task_security(task) (task_cred_xxx((task), security))
39 /**
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)
49 int rc;
50 char in[SMK_LABELLEN];
52 if (ip->i_op->getxattr == NULL)
53 return NULL;
55 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
56 if (rc < 0)
57 return NULL;
59 return smk_import(in, rc);
62 /**
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);
73 if (isp == NULL)
74 return NULL;
76 isp->smk_inode = smack;
77 isp->smk_flags = 0;
78 mutex_init(&isp->smk_lock);
80 return isp;
84 * LSM hooks.
85 * We he, that is fun!
88 /**
89 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
90 * @ctp: child task pointer
91 * @mode: ptrace attachment mode
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_access_check(struct task_struct *ctp, unsigned int mode)
99 int rc;
100 struct smk_audit_info ad;
101 char *sp, *tsp;
103 rc = cap_ptrace_access_check(ctp, mode);
104 if (rc != 0)
105 return rc;
107 sp = current_security();
108 tsp = task_security(ctp);
109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
110 smk_ad_setfield_u_tsk(&ad, ctp);
112 /* we won't log here, because rc can be overriden */
113 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
114 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
115 rc = 0;
117 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
118 return rc;
122 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
123 * @ptp: parent task pointer
125 * Returns 0 if access is OK, an error code otherwise
127 * Do the capability checks, and require read and write.
129 static int smack_ptrace_traceme(struct task_struct *ptp)
131 int rc;
132 struct smk_audit_info ad;
133 char *sp, *tsp;
135 rc = cap_ptrace_traceme(ptp);
136 if (rc != 0)
137 return rc;
139 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
140 smk_ad_setfield_u_tsk(&ad, ptp);
142 sp = current_security();
143 tsp = task_security(ptp);
144 /* we won't log here, because rc can be overriden */
145 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
146 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
147 rc = 0;
149 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
150 return rc;
154 * smack_syslog - Smack approval on syslog
155 * @type: message type
157 * Require that the task has the floor label
159 * Returns 0 on success, error code otherwise.
161 static int smack_syslog(int type, bool from_file)
163 int rc;
164 char *sp = current_security();
166 rc = cap_syslog(type, from_file);
167 if (rc != 0)
168 return rc;
170 if (capable(CAP_MAC_OVERRIDE))
171 return 0;
173 if (sp != smack_known_floor.smk_known)
174 rc = -EACCES;
176 return rc;
181 * Superblock Hooks.
185 * smack_sb_alloc_security - allocate a superblock blob
186 * @sb: the superblock getting the blob
188 * Returns 0 on success or -ENOMEM on error.
190 static int smack_sb_alloc_security(struct super_block *sb)
192 struct superblock_smack *sbsp;
194 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
196 if (sbsp == NULL)
197 return -ENOMEM;
199 sbsp->smk_root = smack_known_floor.smk_known;
200 sbsp->smk_default = smack_known_floor.smk_known;
201 sbsp->smk_floor = smack_known_floor.smk_known;
202 sbsp->smk_hat = smack_known_hat.smk_known;
203 sbsp->smk_initialized = 0;
204 spin_lock_init(&sbsp->smk_sblock);
206 sb->s_security = sbsp;
208 return 0;
212 * smack_sb_free_security - free a superblock blob
213 * @sb: the superblock getting the blob
216 static void smack_sb_free_security(struct super_block *sb)
218 kfree(sb->s_security);
219 sb->s_security = NULL;
223 * smack_sb_copy_data - copy mount options data for processing
224 * @orig: where to start
225 * @smackopts: mount options string
227 * Returns 0 on success or -ENOMEM on error.
229 * Copy the Smack specific mount options out of the mount
230 * options list.
232 static int smack_sb_copy_data(char *orig, char *smackopts)
234 char *cp, *commap, *otheropts, *dp;
236 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
237 if (otheropts == NULL)
238 return -ENOMEM;
240 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
241 if (strstr(cp, SMK_FSDEFAULT) == cp)
242 dp = smackopts;
243 else if (strstr(cp, SMK_FSFLOOR) == cp)
244 dp = smackopts;
245 else if (strstr(cp, SMK_FSHAT) == cp)
246 dp = smackopts;
247 else if (strstr(cp, SMK_FSROOT) == cp)
248 dp = smackopts;
249 else
250 dp = otheropts;
252 commap = strchr(cp, ',');
253 if (commap != NULL)
254 *commap = '\0';
256 if (*dp != '\0')
257 strcat(dp, ",");
258 strcat(dp, cp);
261 strcpy(orig, otheropts);
262 free_page((unsigned long)otheropts);
264 return 0;
268 * smack_sb_kern_mount - Smack specific mount processing
269 * @sb: the file system superblock
270 * @flags: the mount flags
271 * @data: the smack mount options
273 * Returns 0 on success, an error code on failure
275 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
277 struct dentry *root = sb->s_root;
278 struct inode *inode = root->d_inode;
279 struct superblock_smack *sp = sb->s_security;
280 struct inode_smack *isp;
281 char *op;
282 char *commap;
283 char *nsp;
285 spin_lock(&sp->smk_sblock);
286 if (sp->smk_initialized != 0) {
287 spin_unlock(&sp->smk_sblock);
288 return 0;
290 sp->smk_initialized = 1;
291 spin_unlock(&sp->smk_sblock);
293 for (op = data; op != NULL; op = commap) {
294 commap = strchr(op, ',');
295 if (commap != NULL)
296 *commap++ = '\0';
298 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
299 op += strlen(SMK_FSHAT);
300 nsp = smk_import(op, 0);
301 if (nsp != NULL)
302 sp->smk_hat = nsp;
303 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
304 op += strlen(SMK_FSFLOOR);
305 nsp = smk_import(op, 0);
306 if (nsp != NULL)
307 sp->smk_floor = nsp;
308 } else if (strncmp(op, SMK_FSDEFAULT,
309 strlen(SMK_FSDEFAULT)) == 0) {
310 op += strlen(SMK_FSDEFAULT);
311 nsp = smk_import(op, 0);
312 if (nsp != NULL)
313 sp->smk_default = nsp;
314 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
315 op += strlen(SMK_FSROOT);
316 nsp = smk_import(op, 0);
317 if (nsp != NULL)
318 sp->smk_root = nsp;
323 * Initialize the root inode.
325 isp = inode->i_security;
326 if (isp == NULL)
327 inode->i_security = new_inode_smack(sp->smk_root);
328 else
329 isp->smk_inode = sp->smk_root;
331 return 0;
335 * smack_sb_statfs - Smack check on statfs
336 * @dentry: identifies the file system in question
338 * Returns 0 if current can read the floor of the filesystem,
339 * and error code otherwise
341 static int smack_sb_statfs(struct dentry *dentry)
343 struct superblock_smack *sbp = dentry->d_sb->s_security;
344 int rc;
345 struct smk_audit_info ad;
347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
348 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
350 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
351 return rc;
355 * smack_sb_mount - Smack check for mounting
356 * @dev_name: unused
357 * @path: mount point
358 * @type: unused
359 * @flags: unused
360 * @data: unused
362 * Returns 0 if current can write the floor of the filesystem
363 * being mounted on, an error code otherwise.
365 static int smack_sb_mount(char *dev_name, struct path *path,
366 char *type, unsigned long flags, void *data)
368 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
369 struct smk_audit_info ad;
371 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
372 smk_ad_setfield_u_fs_path(&ad, *path);
374 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
378 * smack_sb_umount - Smack check for unmounting
379 * @mnt: file system to unmount
380 * @flags: unused
382 * Returns 0 if current can write the floor of the filesystem
383 * being unmounted, an error code otherwise.
385 static int smack_sb_umount(struct vfsmount *mnt, int flags)
387 struct superblock_smack *sbp;
388 struct smk_audit_info ad;
390 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
391 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
392 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
394 sbp = mnt->mnt_sb->s_security;
395 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
399 * Inode hooks
403 * smack_inode_alloc_security - allocate an inode blob
404 * @inode: the inode in need of a blob
406 * Returns 0 if it gets a blob, -ENOMEM otherwise
408 static int smack_inode_alloc_security(struct inode *inode)
410 inode->i_security = new_inode_smack(current_security());
411 if (inode->i_security == NULL)
412 return -ENOMEM;
413 return 0;
417 * smack_inode_free_security - free an inode blob
418 * @inode: the inode with a blob
420 * Clears the blob pointer in inode
422 static void smack_inode_free_security(struct inode *inode)
424 kfree(inode->i_security);
425 inode->i_security = NULL;
429 * smack_inode_init_security - copy out the smack from an inode
430 * @inode: the inode
431 * @dir: unused
432 * @name: where to put the attribute name
433 * @value: where to put the attribute value
434 * @len: where to put the length of the attribute
436 * Returns 0 if it all works out, -ENOMEM if there's no memory
438 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
439 char **name, void **value, size_t *len)
441 char *isp = smk_of_inode(inode);
443 if (name) {
444 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
445 if (*name == NULL)
446 return -ENOMEM;
449 if (value) {
450 *value = kstrdup(isp, GFP_KERNEL);
451 if (*value == NULL)
452 return -ENOMEM;
455 if (len)
456 *len = strlen(isp) + 1;
458 return 0;
462 * smack_inode_link - Smack check on link
463 * @old_dentry: the existing object
464 * @dir: unused
465 * @new_dentry: the new object
467 * Returns 0 if access is permitted, an error code otherwise
469 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
470 struct dentry *new_dentry)
472 char *isp;
473 struct smk_audit_info ad;
474 int rc;
476 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
477 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
479 isp = smk_of_inode(old_dentry->d_inode);
480 rc = smk_curacc(isp, MAY_WRITE, &ad);
482 if (rc == 0 && new_dentry->d_inode != NULL) {
483 isp = smk_of_inode(new_dentry->d_inode);
484 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
485 rc = smk_curacc(isp, MAY_WRITE, &ad);
488 return rc;
492 * smack_inode_unlink - Smack check on inode deletion
493 * @dir: containing directory object
494 * @dentry: file to unlink
496 * Returns 0 if current can write the containing directory
497 * and the object, error code otherwise
499 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
501 struct inode *ip = dentry->d_inode;
502 struct smk_audit_info ad;
503 int rc;
505 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
506 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
509 * You need write access to the thing you're unlinking
511 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
512 if (rc == 0) {
514 * You also need write access to the containing directory
516 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
517 smk_ad_setfield_u_fs_inode(&ad, dir);
518 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
520 return rc;
524 * smack_inode_rmdir - Smack check on directory deletion
525 * @dir: containing directory object
526 * @dentry: directory to unlink
528 * Returns 0 if current can write the containing directory
529 * and the directory, error code otherwise
531 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
533 struct smk_audit_info ad;
534 int rc;
536 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
537 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
540 * You need write access to the thing you're removing
542 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
543 if (rc == 0) {
545 * You also need write access to the containing directory
547 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
548 smk_ad_setfield_u_fs_inode(&ad, dir);
549 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
552 return rc;
556 * smack_inode_rename - Smack check on rename
557 * @old_inode: the old directory
558 * @old_dentry: unused
559 * @new_inode: the new directory
560 * @new_dentry: unused
562 * Read and write access is required on both the old and
563 * new directories.
565 * Returns 0 if access is permitted, an error code otherwise
567 static int smack_inode_rename(struct inode *old_inode,
568 struct dentry *old_dentry,
569 struct inode *new_inode,
570 struct dentry *new_dentry)
572 int rc;
573 char *isp;
574 struct smk_audit_info ad;
576 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
577 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
579 isp = smk_of_inode(old_dentry->d_inode);
580 rc = smk_curacc(isp, MAY_READWRITE, &ad);
582 if (rc == 0 && new_dentry->d_inode != NULL) {
583 isp = smk_of_inode(new_dentry->d_inode);
584 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
585 rc = smk_curacc(isp, MAY_READWRITE, &ad);
587 return rc;
591 * smack_inode_permission - Smack version of permission()
592 * @inode: the inode in question
593 * @mask: the access requested
595 * This is the important Smack hook.
597 * Returns 0 if access is permitted, -EACCES otherwise
599 static int smack_inode_permission(struct inode *inode, int mask)
601 struct smk_audit_info ad;
603 * No permission to check. Existence test. Yup, it's there.
605 if (mask == 0)
606 return 0;
607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
608 smk_ad_setfield_u_fs_inode(&ad, inode);
609 return smk_curacc(smk_of_inode(inode), mask, &ad);
613 * smack_inode_setattr - Smack check for setting attributes
614 * @dentry: the object
615 * @iattr: for the force flag
617 * Returns 0 if access is permitted, an error code otherwise
619 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
621 struct smk_audit_info ad;
623 * Need to allow for clearing the setuid bit.
625 if (iattr->ia_valid & ATTR_FORCE)
626 return 0;
627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
628 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
630 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
634 * smack_inode_getattr - Smack check for getting attributes
635 * @mnt: unused
636 * @dentry: the object
638 * Returns 0 if access is permitted, an error code otherwise
640 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
642 struct smk_audit_info ad;
644 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
645 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
646 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
647 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
651 * smack_inode_setxattr - Smack check for setting xattrs
652 * @dentry: the object
653 * @name: name of the attribute
654 * @value: unused
655 * @size: unused
656 * @flags: unused
658 * This protects the Smack attribute explicitly.
660 * Returns 0 if access is permitted, an error code otherwise
662 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
663 const void *value, size_t size, int flags)
665 struct smk_audit_info ad;
666 int rc = 0;
668 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
669 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
670 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
671 if (!capable(CAP_MAC_ADMIN))
672 rc = -EPERM;
674 * check label validity here so import wont fail on
675 * post_setxattr
677 if (size == 0 || size >= SMK_LABELLEN ||
678 smk_import(value, size) == NULL)
679 rc = -EINVAL;
680 } else
681 rc = cap_inode_setxattr(dentry, name, value, size, flags);
683 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
684 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
686 if (rc == 0)
687 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
689 return rc;
693 * smack_inode_post_setxattr - Apply the Smack update approved above
694 * @dentry: object
695 * @name: attribute name
696 * @value: attribute value
697 * @size: attribute size
698 * @flags: unused
700 * Set the pointer in the inode blob to the entry found
701 * in the master label list.
703 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
704 const void *value, size_t size, int flags)
706 struct inode_smack *isp;
707 char *nsp;
710 * Not SMACK
712 if (strcmp(name, XATTR_NAME_SMACK))
713 return;
715 isp = dentry->d_inode->i_security;
718 * No locking is done here. This is a pointer
719 * assignment.
721 nsp = smk_import(value, size);
722 if (nsp != NULL)
723 isp->smk_inode = nsp;
724 else
725 isp->smk_inode = smack_known_invalid.smk_known;
727 return;
731 * smack_inode_getxattr - Smack check on getxattr
732 * @dentry: the object
733 * @name: unused
735 * Returns 0 if access is permitted, an error code otherwise
737 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
739 struct smk_audit_info ad;
741 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
742 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
744 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
748 * smack_inode_removexattr - Smack check on removexattr
749 * @dentry: the object
750 * @name: name of the attribute
752 * Removing the Smack attribute requires CAP_MAC_ADMIN
754 * Returns 0 if access is permitted, an error code otherwise
756 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
758 struct smk_audit_info ad;
759 int rc = 0;
761 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
762 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
763 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
764 if (!capable(CAP_MAC_ADMIN))
765 rc = -EPERM;
766 } else
767 rc = cap_inode_removexattr(dentry, name);
769 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
770 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
771 if (rc == 0)
772 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
774 return rc;
778 * smack_inode_getsecurity - get smack xattrs
779 * @inode: the object
780 * @name: attribute name
781 * @buffer: where to put the result
782 * @alloc: unused
784 * Returns the size of the attribute or an error code
786 static int smack_inode_getsecurity(const struct inode *inode,
787 const char *name, void **buffer,
788 bool alloc)
790 struct socket_smack *ssp;
791 struct socket *sock;
792 struct super_block *sbp;
793 struct inode *ip = (struct inode *)inode;
794 char *isp;
795 int ilen;
796 int rc = 0;
798 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
799 isp = smk_of_inode(inode);
800 ilen = strlen(isp) + 1;
801 *buffer = isp;
802 return ilen;
806 * The rest of the Smack xattrs are only on sockets.
808 sbp = ip->i_sb;
809 if (sbp->s_magic != SOCKFS_MAGIC)
810 return -EOPNOTSUPP;
812 sock = SOCKET_I(ip);
813 if (sock == NULL || sock->sk == NULL)
814 return -EOPNOTSUPP;
816 ssp = sock->sk->sk_security;
818 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
819 isp = ssp->smk_in;
820 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
821 isp = ssp->smk_out;
822 else
823 return -EOPNOTSUPP;
825 ilen = strlen(isp) + 1;
826 if (rc == 0) {
827 *buffer = isp;
828 rc = ilen;
831 return rc;
836 * smack_inode_listsecurity - list the Smack attributes
837 * @inode: the object
838 * @buffer: where they go
839 * @buffer_size: size of buffer
841 * Returns 0 on success, -EINVAL otherwise
843 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
844 size_t buffer_size)
846 int len = strlen(XATTR_NAME_SMACK);
848 if (buffer != NULL && len <= buffer_size) {
849 memcpy(buffer, XATTR_NAME_SMACK, len);
850 return len;
852 return -EINVAL;
856 * smack_inode_getsecid - Extract inode's security id
857 * @inode: inode to extract the info from
858 * @secid: where result will be saved
860 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
862 struct inode_smack *isp = inode->i_security;
864 *secid = smack_to_secid(isp->smk_inode);
868 * File Hooks
872 * smack_file_permission - Smack check on file operations
873 * @file: unused
874 * @mask: unused
876 * Returns 0
878 * Should access checks be done on each read or write?
879 * UNICOS and SELinux say yes.
880 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
882 * I'll say no for now. Smack does not do the frequent
883 * label changing that SELinux does.
885 static int smack_file_permission(struct file *file, int mask)
887 return 0;
891 * smack_file_alloc_security - assign a file security blob
892 * @file: the object
894 * The security blob for a file is a pointer to the master
895 * label list, so no allocation is done.
897 * Returns 0
899 static int smack_file_alloc_security(struct file *file)
901 file->f_security = current_security();
902 return 0;
906 * smack_file_free_security - clear a file security blob
907 * @file: the object
909 * The security blob for a file is a pointer to the master
910 * label list, so no memory is freed.
912 static void smack_file_free_security(struct file *file)
914 file->f_security = NULL;
918 * smack_file_ioctl - Smack check on ioctls
919 * @file: the object
920 * @cmd: what to do
921 * @arg: unused
923 * Relies heavily on the correct use of the ioctl command conventions.
925 * Returns 0 if allowed, error code otherwise
927 static int smack_file_ioctl(struct file *file, unsigned int cmd,
928 unsigned long arg)
930 int rc = 0;
931 struct smk_audit_info ad;
933 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
934 smk_ad_setfield_u_fs_path(&ad, file->f_path);
936 if (_IOC_DIR(cmd) & _IOC_WRITE)
937 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
939 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
940 rc = smk_curacc(file->f_security, MAY_READ, &ad);
942 return rc;
946 * smack_file_lock - Smack check on file locking
947 * @file: the object
948 * @cmd: unused
950 * Returns 0 if current has write access, error code otherwise
952 static int smack_file_lock(struct file *file, unsigned int cmd)
954 struct smk_audit_info ad;
956 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
957 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
958 return smk_curacc(file->f_security, MAY_WRITE, &ad);
962 * smack_file_fcntl - Smack check on fcntl
963 * @file: the object
964 * @cmd: what action to check
965 * @arg: unused
967 * Returns 0 if current has access, error code otherwise
969 static int smack_file_fcntl(struct file *file, unsigned int cmd,
970 unsigned long arg)
972 struct smk_audit_info ad;
973 int rc;
975 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
976 smk_ad_setfield_u_fs_path(&ad, file->f_path);
978 switch (cmd) {
979 case F_DUPFD:
980 case F_GETFD:
981 case F_GETFL:
982 case F_GETLK:
983 case F_GETOWN:
984 case F_GETSIG:
985 rc = smk_curacc(file->f_security, MAY_READ, &ad);
986 break;
987 case F_SETFD:
988 case F_SETFL:
989 case F_SETLK:
990 case F_SETLKW:
991 case F_SETOWN:
992 case F_SETSIG:
993 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
994 break;
995 default:
996 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
999 return rc;
1003 * smack_file_set_fowner - set the file security blob value
1004 * @file: object in question
1006 * Returns 0
1007 * Further research may be required on this one.
1009 static int smack_file_set_fowner(struct file *file)
1011 file->f_security = current_security();
1012 return 0;
1016 * smack_file_send_sigiotask - Smack on sigio
1017 * @tsk: The target task
1018 * @fown: the object the signal come from
1019 * @signum: unused
1021 * Allow a privileged task to get signals even if it shouldn't
1023 * Returns 0 if a subject with the object's smack could
1024 * write to the task, an error code otherwise.
1026 static int smack_file_send_sigiotask(struct task_struct *tsk,
1027 struct fown_struct *fown, int signum)
1029 struct file *file;
1030 int rc;
1031 char *tsp = tsk->cred->security;
1032 struct smk_audit_info ad;
1035 * struct fown_struct is never outside the context of a struct file
1037 file = container_of(fown, struct file, f_owner);
1038 /* we don't log here as rc can be overriden */
1039 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1040 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1041 rc = 0;
1043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1044 smk_ad_setfield_u_tsk(&ad, tsk);
1045 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1046 return rc;
1050 * smack_file_receive - Smack file receive check
1051 * @file: the object
1053 * Returns 0 if current has access, error code otherwise
1055 static int smack_file_receive(struct file *file)
1057 int may = 0;
1058 struct smk_audit_info ad;
1060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1061 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1063 * This code relies on bitmasks.
1065 if (file->f_mode & FMODE_READ)
1066 may = MAY_READ;
1067 if (file->f_mode & FMODE_WRITE)
1068 may |= MAY_WRITE;
1070 return smk_curacc(file->f_security, may, &ad);
1074 * Task hooks
1078 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1079 * @new: the new credentials
1080 * @gfp: the atomicity of any memory allocations
1082 * Prepare a blank set of credentials for modification. This must allocate all
1083 * the memory the LSM module might require such that cred_transfer() can
1084 * complete without error.
1086 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1088 cred->security = NULL;
1089 return 0;
1094 * smack_cred_free - "free" task-level security credentials
1095 * @cred: the credentials in question
1097 * Smack isn't using copies of blobs. Everyone
1098 * points to an immutable list. The blobs never go away.
1099 * There is no leak here.
1101 static void smack_cred_free(struct cred *cred)
1103 cred->security = NULL;
1107 * smack_cred_prepare - prepare new set of credentials for modification
1108 * @new: the new credentials
1109 * @old: the original credentials
1110 * @gfp: the atomicity of any memory allocations
1112 * Prepare a new set of credentials for modification.
1114 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1115 gfp_t gfp)
1117 new->security = old->security;
1118 return 0;
1122 * smack_cred_commit - commit new credentials
1123 * @new: the new credentials
1124 * @old: the original credentials
1126 static void smack_cred_commit(struct cred *new, const struct cred *old)
1131 * smack_cred_transfer - Transfer the old credentials to the new credentials
1132 * @new: the new credentials
1133 * @old: the original credentials
1135 * Fill in a set of blank credentials from another set of credentials.
1137 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1139 new->security = old->security;
1143 * smack_kernel_act_as - Set the subjective context in a set of credentials
1144 * @new: points to the set of credentials to be modified.
1145 * @secid: specifies the security ID to be set
1147 * Set the security data for a kernel service.
1149 static int smack_kernel_act_as(struct cred *new, u32 secid)
1151 char *smack = smack_from_secid(secid);
1153 if (smack == NULL)
1154 return -EINVAL;
1156 new->security = smack;
1157 return 0;
1161 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1162 * @new: points to the set of credentials to be modified
1163 * @inode: points to the inode to use as a reference
1165 * Set the file creation context in a set of credentials to the same
1166 * as the objective context of the specified inode
1168 static int smack_kernel_create_files_as(struct cred *new,
1169 struct inode *inode)
1171 struct inode_smack *isp = inode->i_security;
1173 new->security = isp->smk_inode;
1174 return 0;
1178 * smk_curacc_on_task - helper to log task related access
1179 * @p: the task object
1180 * @access : the access requested
1182 * Return 0 if access is permitted
1184 static int smk_curacc_on_task(struct task_struct *p, int access)
1186 struct smk_audit_info ad;
1188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1189 smk_ad_setfield_u_tsk(&ad, p);
1190 return smk_curacc(task_security(p), access, &ad);
1194 * smack_task_setpgid - Smack check on setting pgid
1195 * @p: the task object
1196 * @pgid: unused
1198 * Return 0 if write access is permitted
1200 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1202 return smk_curacc_on_task(p, MAY_WRITE);
1206 * smack_task_getpgid - Smack access check for getpgid
1207 * @p: the object task
1209 * Returns 0 if current can read the object task, error code otherwise
1211 static int smack_task_getpgid(struct task_struct *p)
1213 return smk_curacc_on_task(p, MAY_READ);
1217 * smack_task_getsid - Smack access check for getsid
1218 * @p: the object task
1220 * Returns 0 if current can read the object task, error code otherwise
1222 static int smack_task_getsid(struct task_struct *p)
1224 return smk_curacc_on_task(p, MAY_READ);
1228 * smack_task_getsecid - get the secid of the task
1229 * @p: the object task
1230 * @secid: where to put the result
1232 * Sets the secid to contain a u32 version of the smack label.
1234 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1236 *secid = smack_to_secid(task_security(p));
1240 * smack_task_setnice - Smack check on setting nice
1241 * @p: the task object
1242 * @nice: unused
1244 * Return 0 if write access is permitted
1246 static int smack_task_setnice(struct task_struct *p, int nice)
1248 int rc;
1250 rc = cap_task_setnice(p, nice);
1251 if (rc == 0)
1252 rc = smk_curacc_on_task(p, MAY_WRITE);
1253 return rc;
1257 * smack_task_setioprio - Smack check on setting ioprio
1258 * @p: the task object
1259 * @ioprio: unused
1261 * Return 0 if write access is permitted
1263 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1265 int rc;
1267 rc = cap_task_setioprio(p, ioprio);
1268 if (rc == 0)
1269 rc = smk_curacc_on_task(p, MAY_WRITE);
1270 return rc;
1274 * smack_task_getioprio - Smack check on reading ioprio
1275 * @p: the task object
1277 * Return 0 if read access is permitted
1279 static int smack_task_getioprio(struct task_struct *p)
1281 return smk_curacc_on_task(p, MAY_READ);
1285 * smack_task_setscheduler - Smack check on setting scheduler
1286 * @p: the task object
1287 * @policy: unused
1288 * @lp: unused
1290 * Return 0 if read access is permitted
1292 static int smack_task_setscheduler(struct task_struct *p, int policy,
1293 struct sched_param *lp)
1295 int rc;
1297 rc = cap_task_setscheduler(p, policy, lp);
1298 if (rc == 0)
1299 rc = smk_curacc_on_task(p, MAY_WRITE);
1300 return rc;
1304 * smack_task_getscheduler - Smack check on reading scheduler
1305 * @p: the task object
1307 * Return 0 if read access is permitted
1309 static int smack_task_getscheduler(struct task_struct *p)
1311 return smk_curacc_on_task(p, MAY_READ);
1315 * smack_task_movememory - Smack check on moving memory
1316 * @p: the task object
1318 * Return 0 if write access is permitted
1320 static int smack_task_movememory(struct task_struct *p)
1322 return smk_curacc_on_task(p, MAY_WRITE);
1326 * smack_task_kill - Smack check on signal delivery
1327 * @p: the task object
1328 * @info: unused
1329 * @sig: unused
1330 * @secid: identifies the smack to use in lieu of current's
1332 * Return 0 if write access is permitted
1334 * The secid behavior is an artifact of an SELinux hack
1335 * in the USB code. Someday it may go away.
1337 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1338 int sig, u32 secid)
1340 struct smk_audit_info ad;
1342 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1343 smk_ad_setfield_u_tsk(&ad, p);
1345 * Sending a signal requires that the sender
1346 * can write the receiver.
1348 if (secid == 0)
1349 return smk_curacc(task_security(p), MAY_WRITE, &ad);
1351 * If the secid isn't 0 we're dealing with some USB IO
1352 * specific behavior. This is not clean. For one thing
1353 * we can't take privilege into account.
1355 return smk_access(smack_from_secid(secid), task_security(p),
1356 MAY_WRITE, &ad);
1360 * smack_task_wait - Smack access check for waiting
1361 * @p: task to wait for
1363 * Returns 0 if current can wait for p, error code otherwise
1365 static int smack_task_wait(struct task_struct *p)
1367 struct smk_audit_info ad;
1368 char *sp = current_security();
1369 char *tsp = task_security(p);
1370 int rc;
1372 /* we don't log here, we can be overriden */
1373 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
1374 if (rc == 0)
1375 goto out_log;
1378 * Allow the operation to succeed if either task
1379 * has privilege to perform operations that might
1380 * account for the smack labels having gotten to
1381 * be different in the first place.
1383 * This breaks the strict subject/object access
1384 * control ideal, taking the object's privilege
1385 * state into account in the decision as well as
1386 * the smack value.
1388 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1389 rc = 0;
1390 /* we log only if we didn't get overriden */
1391 out_log:
1392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1393 smk_ad_setfield_u_tsk(&ad, p);
1394 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
1395 return rc;
1399 * smack_task_to_inode - copy task smack into the inode blob
1400 * @p: task to copy from
1401 * @inode: inode to copy to
1403 * Sets the smack pointer in the inode security blob
1405 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1407 struct inode_smack *isp = inode->i_security;
1408 isp->smk_inode = task_security(p);
1412 * Socket hooks.
1416 * smack_sk_alloc_security - Allocate a socket blob
1417 * @sk: the socket
1418 * @family: unused
1419 * @gfp_flags: memory allocation flags
1421 * Assign Smack pointers to current
1423 * Returns 0 on success, -ENOMEM is there's no memory
1425 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1427 char *csp = current_security();
1428 struct socket_smack *ssp;
1430 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1431 if (ssp == NULL)
1432 return -ENOMEM;
1434 ssp->smk_in = csp;
1435 ssp->smk_out = csp;
1436 ssp->smk_packet[0] = '\0';
1438 sk->sk_security = ssp;
1440 return 0;
1444 * smack_sk_free_security - Free a socket blob
1445 * @sk: the socket
1447 * Clears the blob pointer
1449 static void smack_sk_free_security(struct sock *sk)
1451 kfree(sk->sk_security);
1455 * smack_host_label - check host based restrictions
1456 * @sip: the object end
1458 * looks for host based access restrictions
1460 * This version will only be appropriate for really small sets of single label
1461 * hosts. The caller is responsible for ensuring that the RCU read lock is
1462 * taken before calling this function.
1464 * Returns the label of the far end or NULL if it's not special.
1466 static char *smack_host_label(struct sockaddr_in *sip)
1468 struct smk_netlbladdr *snp;
1469 struct in_addr *siap = &sip->sin_addr;
1471 if (siap->s_addr == 0)
1472 return NULL;
1474 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1476 * we break after finding the first match because
1477 * the list is sorted from longest to shortest mask
1478 * so we have found the most specific match
1480 if ((&snp->smk_host.sin_addr)->s_addr ==
1481 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1482 /* we have found the special CIPSO option */
1483 if (snp->smk_label == smack_cipso_option)
1484 return NULL;
1485 return snp->smk_label;
1488 return NULL;
1492 * smack_set_catset - convert a capset to netlabel mls categories
1493 * @catset: the Smack categories
1494 * @sap: where to put the netlabel categories
1496 * Allocates and fills attr.mls.cat
1498 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1500 unsigned char *cp;
1501 unsigned char m;
1502 int cat;
1503 int rc;
1504 int byte;
1506 if (!catset)
1507 return;
1509 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1510 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1511 sap->attr.mls.cat->startbit = 0;
1513 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1514 for (m = 0x80; m != 0; m >>= 1, cat++) {
1515 if ((m & *cp) == 0)
1516 continue;
1517 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1518 cat, GFP_ATOMIC);
1523 * smack_to_secattr - fill a secattr from a smack value
1524 * @smack: the smack value
1525 * @nlsp: where the result goes
1527 * Casey says that CIPSO is good enough for now.
1528 * It can be used to effect.
1529 * It can also be abused to effect when necessary.
1530 * Appologies to the TSIG group in general and GW in particular.
1532 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1534 struct smack_cipso cipso;
1535 int rc;
1537 nlsp->domain = smack;
1538 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1540 rc = smack_to_cipso(smack, &cipso);
1541 if (rc == 0) {
1542 nlsp->attr.mls.lvl = cipso.smk_level;
1543 smack_set_catset(cipso.smk_catset, nlsp);
1544 } else {
1545 nlsp->attr.mls.lvl = smack_cipso_direct;
1546 smack_set_catset(smack, nlsp);
1551 * smack_netlabel - Set the secattr on a socket
1552 * @sk: the socket
1553 * @labeled: socket label scheme
1555 * Convert the outbound smack value (smk_out) to a
1556 * secattr and attach it to the socket.
1558 * Returns 0 on success or an error code
1560 static int smack_netlabel(struct sock *sk, int labeled)
1562 struct socket_smack *ssp = sk->sk_security;
1563 struct netlbl_lsm_secattr secattr;
1564 int rc = 0;
1567 * Usually the netlabel code will handle changing the
1568 * packet labeling based on the label.
1569 * The case of a single label host is different, because
1570 * a single label host should never get a labeled packet
1571 * even though the label is usually associated with a packet
1572 * label.
1574 local_bh_disable();
1575 bh_lock_sock_nested(sk);
1577 if (ssp->smk_out == smack_net_ambient ||
1578 labeled == SMACK_UNLABELED_SOCKET)
1579 netlbl_sock_delattr(sk);
1580 else {
1581 netlbl_secattr_init(&secattr);
1582 smack_to_secattr(ssp->smk_out, &secattr);
1583 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1584 netlbl_secattr_destroy(&secattr);
1587 bh_unlock_sock(sk);
1588 local_bh_enable();
1590 return rc;
1594 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1595 * @sk: the socket
1596 * @sap: the destination address
1598 * Set the correct secattr for the given socket based on the destination
1599 * address and perform any outbound access checks needed.
1601 * Returns 0 on success or an error code.
1604 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1606 int rc;
1607 int sk_lbl;
1608 char *hostsp;
1609 struct socket_smack *ssp = sk->sk_security;
1610 struct smk_audit_info ad;
1612 rcu_read_lock();
1613 hostsp = smack_host_label(sap);
1614 if (hostsp != NULL) {
1615 sk_lbl = SMACK_UNLABELED_SOCKET;
1616 #ifdef CONFIG_AUDIT
1617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1618 ad.a.u.net.family = sap->sin_family;
1619 ad.a.u.net.dport = sap->sin_port;
1620 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1621 #endif
1622 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1623 } else {
1624 sk_lbl = SMACK_CIPSO_SOCKET;
1625 rc = 0;
1627 rcu_read_unlock();
1628 if (rc != 0)
1629 return rc;
1631 return smack_netlabel(sk, sk_lbl);
1635 * smack_inode_setsecurity - set smack xattrs
1636 * @inode: the object
1637 * @name: attribute name
1638 * @value: attribute value
1639 * @size: size of the attribute
1640 * @flags: unused
1642 * Sets the named attribute in the appropriate blob
1644 * Returns 0 on success, or an error code
1646 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1647 const void *value, size_t size, int flags)
1649 char *sp;
1650 struct inode_smack *nsp = inode->i_security;
1651 struct socket_smack *ssp;
1652 struct socket *sock;
1653 int rc = 0;
1655 if (value == NULL || size > SMK_LABELLEN || size == 0)
1656 return -EACCES;
1658 sp = smk_import(value, size);
1659 if (sp == NULL)
1660 return -EINVAL;
1662 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1663 nsp->smk_inode = sp;
1664 nsp->smk_flags |= SMK_INODE_INSTANT;
1665 return 0;
1668 * The rest of the Smack xattrs are only on sockets.
1670 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1671 return -EOPNOTSUPP;
1673 sock = SOCKET_I(inode);
1674 if (sock == NULL || sock->sk == NULL)
1675 return -EOPNOTSUPP;
1677 ssp = sock->sk->sk_security;
1679 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1680 ssp->smk_in = sp;
1681 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1682 ssp->smk_out = sp;
1683 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1684 if (rc != 0)
1685 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1686 __func__, -rc);
1687 } else
1688 return -EOPNOTSUPP;
1690 return 0;
1694 * smack_socket_post_create - finish socket setup
1695 * @sock: the socket
1696 * @family: protocol family
1697 * @type: unused
1698 * @protocol: unused
1699 * @kern: unused
1701 * Sets the netlabel information on the socket
1703 * Returns 0 on success, and error code otherwise
1705 static int smack_socket_post_create(struct socket *sock, int family,
1706 int type, int protocol, int kern)
1708 if (family != PF_INET || sock->sk == NULL)
1709 return 0;
1711 * Set the outbound netlbl.
1713 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1717 * smack_socket_connect - connect access check
1718 * @sock: the socket
1719 * @sap: the other end
1720 * @addrlen: size of sap
1722 * Verifies that a connection may be possible
1724 * Returns 0 on success, and error code otherwise
1726 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1727 int addrlen)
1729 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1730 return 0;
1731 if (addrlen < sizeof(struct sockaddr_in))
1732 return -EINVAL;
1734 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1738 * smack_flags_to_may - convert S_ to MAY_ values
1739 * @flags: the S_ value
1741 * Returns the equivalent MAY_ value
1743 static int smack_flags_to_may(int flags)
1745 int may = 0;
1747 if (flags & S_IRUGO)
1748 may |= MAY_READ;
1749 if (flags & S_IWUGO)
1750 may |= MAY_WRITE;
1751 if (flags & S_IXUGO)
1752 may |= MAY_EXEC;
1754 return may;
1758 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1759 * @msg: the object
1761 * Returns 0
1763 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1765 msg->security = current_security();
1766 return 0;
1770 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1771 * @msg: the object
1773 * Clears the blob pointer
1775 static void smack_msg_msg_free_security(struct msg_msg *msg)
1777 msg->security = NULL;
1781 * smack_of_shm - the smack pointer for the shm
1782 * @shp: the object
1784 * Returns a pointer to the smack value
1786 static char *smack_of_shm(struct shmid_kernel *shp)
1788 return (char *)shp->shm_perm.security;
1792 * smack_shm_alloc_security - Set the security blob for shm
1793 * @shp: the object
1795 * Returns 0
1797 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1799 struct kern_ipc_perm *isp = &shp->shm_perm;
1801 isp->security = current_security();
1802 return 0;
1806 * smack_shm_free_security - Clear the security blob for shm
1807 * @shp: the object
1809 * Clears the blob pointer
1811 static void smack_shm_free_security(struct shmid_kernel *shp)
1813 struct kern_ipc_perm *isp = &shp->shm_perm;
1815 isp->security = NULL;
1819 * smk_curacc_shm : check if current has access on shm
1820 * @shp : the object
1821 * @access : access requested
1823 * Returns 0 if current has the requested access, error code otherwise
1825 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1827 char *ssp = smack_of_shm(shp);
1828 struct smk_audit_info ad;
1830 #ifdef CONFIG_AUDIT
1831 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1832 ad.a.u.ipc_id = shp->shm_perm.id;
1833 #endif
1834 return smk_curacc(ssp, access, &ad);
1838 * smack_shm_associate - Smack access check for shm
1839 * @shp: the object
1840 * @shmflg: access requested
1842 * Returns 0 if current has the requested access, error code otherwise
1844 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1846 int may;
1848 may = smack_flags_to_may(shmflg);
1849 return smk_curacc_shm(shp, may);
1853 * smack_shm_shmctl - Smack access check for shm
1854 * @shp: the object
1855 * @cmd: what it wants to do
1857 * Returns 0 if current has the requested access, error code otherwise
1859 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1861 int may;
1863 switch (cmd) {
1864 case IPC_STAT:
1865 case SHM_STAT:
1866 may = MAY_READ;
1867 break;
1868 case IPC_SET:
1869 case SHM_LOCK:
1870 case SHM_UNLOCK:
1871 case IPC_RMID:
1872 may = MAY_READWRITE;
1873 break;
1874 case IPC_INFO:
1875 case SHM_INFO:
1877 * System level information.
1879 return 0;
1880 default:
1881 return -EINVAL;
1883 return smk_curacc_shm(shp, may);
1887 * smack_shm_shmat - Smack access for shmat
1888 * @shp: the object
1889 * @shmaddr: unused
1890 * @shmflg: access requested
1892 * Returns 0 if current has the requested access, error code otherwise
1894 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1895 int shmflg)
1897 int may;
1899 may = smack_flags_to_may(shmflg);
1900 return smk_curacc_shm(shp, may);
1904 * smack_of_sem - the smack pointer for the sem
1905 * @sma: the object
1907 * Returns a pointer to the smack value
1909 static char *smack_of_sem(struct sem_array *sma)
1911 return (char *)sma->sem_perm.security;
1915 * smack_sem_alloc_security - Set the security blob for sem
1916 * @sma: the object
1918 * Returns 0
1920 static int smack_sem_alloc_security(struct sem_array *sma)
1922 struct kern_ipc_perm *isp = &sma->sem_perm;
1924 isp->security = current_security();
1925 return 0;
1929 * smack_sem_free_security - Clear the security blob for sem
1930 * @sma: the object
1932 * Clears the blob pointer
1934 static void smack_sem_free_security(struct sem_array *sma)
1936 struct kern_ipc_perm *isp = &sma->sem_perm;
1938 isp->security = NULL;
1942 * smk_curacc_sem : check if current has access on sem
1943 * @sma : the object
1944 * @access : access requested
1946 * Returns 0 if current has the requested access, error code otherwise
1948 static int smk_curacc_sem(struct sem_array *sma, int access)
1950 char *ssp = smack_of_sem(sma);
1951 struct smk_audit_info ad;
1953 #ifdef CONFIG_AUDIT
1954 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1955 ad.a.u.ipc_id = sma->sem_perm.id;
1956 #endif
1957 return smk_curacc(ssp, access, &ad);
1961 * smack_sem_associate - Smack access check for sem
1962 * @sma: the object
1963 * @semflg: access requested
1965 * Returns 0 if current has the requested access, error code otherwise
1967 static int smack_sem_associate(struct sem_array *sma, int semflg)
1969 int may;
1971 may = smack_flags_to_may(semflg);
1972 return smk_curacc_sem(sma, may);
1976 * smack_sem_shmctl - Smack access check for sem
1977 * @sma: the object
1978 * @cmd: what it wants to do
1980 * Returns 0 if current has the requested access, error code otherwise
1982 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1984 int may;
1986 switch (cmd) {
1987 case GETPID:
1988 case GETNCNT:
1989 case GETZCNT:
1990 case GETVAL:
1991 case GETALL:
1992 case IPC_STAT:
1993 case SEM_STAT:
1994 may = MAY_READ;
1995 break;
1996 case SETVAL:
1997 case SETALL:
1998 case IPC_RMID:
1999 case IPC_SET:
2000 may = MAY_READWRITE;
2001 break;
2002 case IPC_INFO:
2003 case SEM_INFO:
2005 * System level information
2007 return 0;
2008 default:
2009 return -EINVAL;
2012 return smk_curacc_sem(sma, may);
2016 * smack_sem_semop - Smack checks of semaphore operations
2017 * @sma: the object
2018 * @sops: unused
2019 * @nsops: unused
2020 * @alter: unused
2022 * Treated as read and write in all cases.
2024 * Returns 0 if access is allowed, error code otherwise
2026 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2027 unsigned nsops, int alter)
2029 return smk_curacc_sem(sma, MAY_READWRITE);
2033 * smack_msg_alloc_security - Set the security blob for msg
2034 * @msq: the object
2036 * Returns 0
2038 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2040 struct kern_ipc_perm *kisp = &msq->q_perm;
2042 kisp->security = current_security();
2043 return 0;
2047 * smack_msg_free_security - Clear the security blob for msg
2048 * @msq: the object
2050 * Clears the blob pointer
2052 static void smack_msg_queue_free_security(struct msg_queue *msq)
2054 struct kern_ipc_perm *kisp = &msq->q_perm;
2056 kisp->security = NULL;
2060 * smack_of_msq - the smack pointer for the msq
2061 * @msq: the object
2063 * Returns a pointer to the smack value
2065 static char *smack_of_msq(struct msg_queue *msq)
2067 return (char *)msq->q_perm.security;
2071 * smk_curacc_msq : helper to check if current has access on msq
2072 * @msq : the msq
2073 * @access : access requested
2075 * return 0 if current has access, error otherwise
2077 static int smk_curacc_msq(struct msg_queue *msq, int access)
2079 char *msp = smack_of_msq(msq);
2080 struct smk_audit_info ad;
2082 #ifdef CONFIG_AUDIT
2083 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2084 ad.a.u.ipc_id = msq->q_perm.id;
2085 #endif
2086 return smk_curacc(msp, access, &ad);
2090 * smack_msg_queue_associate - Smack access check for msg_queue
2091 * @msq: the object
2092 * @msqflg: access requested
2094 * Returns 0 if current has the requested access, error code otherwise
2096 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2098 int may;
2100 may = smack_flags_to_may(msqflg);
2101 return smk_curacc_msq(msq, may);
2105 * smack_msg_queue_msgctl - Smack access check for msg_queue
2106 * @msq: the object
2107 * @cmd: what it wants to do
2109 * Returns 0 if current has the requested access, error code otherwise
2111 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2113 int may;
2115 switch (cmd) {
2116 case IPC_STAT:
2117 case MSG_STAT:
2118 may = MAY_READ;
2119 break;
2120 case IPC_SET:
2121 case IPC_RMID:
2122 may = MAY_READWRITE;
2123 break;
2124 case IPC_INFO:
2125 case MSG_INFO:
2127 * System level information
2129 return 0;
2130 default:
2131 return -EINVAL;
2134 return smk_curacc_msq(msq, may);
2138 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2139 * @msq: the object
2140 * @msg: unused
2141 * @msqflg: access requested
2143 * Returns 0 if current has the requested access, error code otherwise
2145 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2146 int msqflg)
2148 int may;
2150 may = smack_flags_to_may(msqflg);
2151 return smk_curacc_msq(msq, may);
2155 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2156 * @msq: the object
2157 * @msg: unused
2158 * @target: unused
2159 * @type: unused
2160 * @mode: unused
2162 * Returns 0 if current has read and write access, error code otherwise
2164 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2165 struct task_struct *target, long type, int mode)
2167 return smk_curacc_msq(msq, MAY_READWRITE);
2171 * smack_ipc_permission - Smack access for ipc_permission()
2172 * @ipp: the object permissions
2173 * @flag: access requested
2175 * Returns 0 if current has read and write access, error code otherwise
2177 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2179 char *isp = ipp->security;
2180 int may = smack_flags_to_may(flag);
2181 struct smk_audit_info ad;
2183 #ifdef CONFIG_AUDIT
2184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2185 ad.a.u.ipc_id = ipp->id;
2186 #endif
2187 return smk_curacc(isp, may, &ad);
2191 * smack_ipc_getsecid - Extract smack security id
2192 * @ipp: the object permissions
2193 * @secid: where result will be saved
2195 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2197 char *smack = ipp->security;
2199 *secid = smack_to_secid(smack);
2203 * smack_d_instantiate - Make sure the blob is correct on an inode
2204 * @opt_dentry: unused
2205 * @inode: the object
2207 * Set the inode's security blob if it hasn't been done already.
2209 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2211 struct super_block *sbp;
2212 struct superblock_smack *sbsp;
2213 struct inode_smack *isp;
2214 char *csp = current_security();
2215 char *fetched;
2216 char *final;
2217 struct dentry *dp;
2219 if (inode == NULL)
2220 return;
2222 isp = inode->i_security;
2224 mutex_lock(&isp->smk_lock);
2226 * If the inode is already instantiated
2227 * take the quick way out
2229 if (isp->smk_flags & SMK_INODE_INSTANT)
2230 goto unlockandout;
2232 sbp = inode->i_sb;
2233 sbsp = sbp->s_security;
2235 * We're going to use the superblock default label
2236 * if there's no label on the file.
2238 final = sbsp->smk_default;
2241 * If this is the root inode the superblock
2242 * may be in the process of initialization.
2243 * If that is the case use the root value out
2244 * of the superblock.
2246 if (opt_dentry->d_parent == opt_dentry) {
2247 isp->smk_inode = sbsp->smk_root;
2248 isp->smk_flags |= SMK_INODE_INSTANT;
2249 goto unlockandout;
2253 * This is pretty hackish.
2254 * Casey says that we shouldn't have to do
2255 * file system specific code, but it does help
2256 * with keeping it simple.
2258 switch (sbp->s_magic) {
2259 case SMACK_MAGIC:
2261 * Casey says that it's a little embarassing
2262 * that the smack file system doesn't do
2263 * extended attributes.
2265 final = smack_known_star.smk_known;
2266 break;
2267 case PIPEFS_MAGIC:
2269 * Casey says pipes are easy (?)
2271 final = smack_known_star.smk_known;
2272 break;
2273 case DEVPTS_SUPER_MAGIC:
2275 * devpts seems content with the label of the task.
2276 * Programs that change smack have to treat the
2277 * pty with respect.
2279 final = csp;
2280 break;
2281 case SOCKFS_MAGIC:
2283 * Casey says sockets get the smack of the task.
2285 final = csp;
2286 break;
2287 case PROC_SUPER_MAGIC:
2289 * Casey says procfs appears not to care.
2290 * The superblock default suffices.
2292 break;
2293 case TMPFS_MAGIC:
2295 * Device labels should come from the filesystem,
2296 * but watch out, because they're volitile,
2297 * getting recreated on every reboot.
2299 final = smack_known_star.smk_known;
2301 * No break.
2303 * If a smack value has been set we want to use it,
2304 * but since tmpfs isn't giving us the opportunity
2305 * to set mount options simulate setting the
2306 * superblock default.
2308 default:
2310 * This isn't an understood special case.
2311 * Get the value from the xattr.
2313 * No xattr support means, alas, no SMACK label.
2314 * Use the aforeapplied default.
2315 * It would be curious if the label of the task
2316 * does not match that assigned.
2318 if (inode->i_op->getxattr == NULL)
2319 break;
2321 * Get the dentry for xattr.
2323 if (opt_dentry == NULL) {
2324 dp = d_find_alias(inode);
2325 if (dp == NULL)
2326 break;
2327 } else {
2328 dp = dget(opt_dentry);
2329 if (dp == NULL)
2330 break;
2333 fetched = smk_fetch(inode, dp);
2334 if (fetched != NULL)
2335 final = fetched;
2337 dput(dp);
2338 break;
2341 if (final == NULL)
2342 isp->smk_inode = csp;
2343 else
2344 isp->smk_inode = final;
2346 isp->smk_flags |= SMK_INODE_INSTANT;
2348 unlockandout:
2349 mutex_unlock(&isp->smk_lock);
2350 return;
2354 * smack_getprocattr - Smack process attribute access
2355 * @p: the object task
2356 * @name: the name of the attribute in /proc/.../attr
2357 * @value: where to put the result
2359 * Places a copy of the task Smack into value
2361 * Returns the length of the smack label or an error code
2363 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2365 char *cp;
2366 int slen;
2368 if (strcmp(name, "current") != 0)
2369 return -EINVAL;
2371 cp = kstrdup(task_security(p), GFP_KERNEL);
2372 if (cp == NULL)
2373 return -ENOMEM;
2375 slen = strlen(cp);
2376 *value = cp;
2377 return slen;
2381 * smack_setprocattr - Smack process attribute setting
2382 * @p: the object task
2383 * @name: the name of the attribute in /proc/.../attr
2384 * @value: the value to set
2385 * @size: the size of the value
2387 * Sets the Smack value of the task. Only setting self
2388 * is permitted and only with privilege
2390 * Returns the length of the smack label or an error code
2392 static int smack_setprocattr(struct task_struct *p, char *name,
2393 void *value, size_t size)
2395 struct cred *new;
2396 char *newsmack;
2399 * Changing another process' Smack value is too dangerous
2400 * and supports no sane use case.
2402 if (p != current)
2403 return -EPERM;
2405 if (!capable(CAP_MAC_ADMIN))
2406 return -EPERM;
2408 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2409 return -EINVAL;
2411 if (strcmp(name, "current") != 0)
2412 return -EINVAL;
2414 newsmack = smk_import(value, size);
2415 if (newsmack == NULL)
2416 return -EINVAL;
2419 * No process is ever allowed the web ("@") label.
2421 if (newsmack == smack_known_web.smk_known)
2422 return -EPERM;
2424 new = prepare_creds();
2425 if (new == NULL)
2426 return -ENOMEM;
2427 new->security = newsmack;
2428 commit_creds(new);
2429 return size;
2433 * smack_unix_stream_connect - Smack access on UDS
2434 * @sock: one socket
2435 * @other: the other socket
2436 * @newsk: unused
2438 * Return 0 if a subject with the smack of sock could access
2439 * an object with the smack of other, otherwise an error code
2441 static int smack_unix_stream_connect(struct socket *sock,
2442 struct socket *other, struct sock *newsk)
2444 struct inode *sp = SOCK_INODE(sock);
2445 struct inode *op = SOCK_INODE(other);
2446 struct smk_audit_info ad;
2448 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2449 smk_ad_setfield_u_net_sk(&ad, other->sk);
2450 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2451 MAY_READWRITE, &ad);
2455 * smack_unix_may_send - Smack access on UDS
2456 * @sock: one socket
2457 * @other: the other socket
2459 * Return 0 if a subject with the smack of sock could access
2460 * an object with the smack of other, otherwise an error code
2462 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2464 struct inode *sp = SOCK_INODE(sock);
2465 struct inode *op = SOCK_INODE(other);
2466 struct smk_audit_info ad;
2468 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2469 smk_ad_setfield_u_net_sk(&ad, other->sk);
2470 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
2474 * smack_socket_sendmsg - Smack check based on destination host
2475 * @sock: the socket
2476 * @msg: the message
2477 * @size: the size of the message
2479 * Return 0 if the current subject can write to the destination
2480 * host. This is only a question if the destination is a single
2481 * label host.
2483 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2484 int size)
2486 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2489 * Perfectly reasonable for this to be NULL
2491 if (sip == NULL || sip->sin_family != AF_INET)
2492 return 0;
2494 return smack_netlabel_send(sock->sk, sip);
2499 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2500 * @sap: netlabel secattr
2501 * @sip: where to put the result
2503 * Copies a smack label into sip
2505 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2507 char smack[SMK_LABELLEN];
2508 char *sp;
2509 int pcat;
2511 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2513 * Looks like a CIPSO packet.
2514 * If there are flags but no level netlabel isn't
2515 * behaving the way we expect it to.
2517 * Get the categories, if any
2518 * Without guidance regarding the smack value
2519 * for the packet fall back on the network
2520 * ambient value.
2522 memset(smack, '\0', SMK_LABELLEN);
2523 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2524 for (pcat = -1;;) {
2525 pcat = netlbl_secattr_catmap_walk(
2526 sap->attr.mls.cat, pcat + 1);
2527 if (pcat < 0)
2528 break;
2529 smack_catset_bit(pcat, smack);
2532 * If it is CIPSO using smack direct mapping
2533 * we are already done. WeeHee.
2535 if (sap->attr.mls.lvl == smack_cipso_direct) {
2536 memcpy(sip, smack, SMK_MAXLEN);
2537 return;
2540 * Look it up in the supplied table if it is not
2541 * a direct mapping.
2543 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2544 return;
2546 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2548 * Looks like a fallback, which gives us a secid.
2550 sp = smack_from_secid(sap->attr.secid);
2552 * This has got to be a bug because it is
2553 * impossible to specify a fallback without
2554 * specifying the label, which will ensure
2555 * it has a secid, and the only way to get a
2556 * secid is from a fallback.
2558 BUG_ON(sp == NULL);
2559 strncpy(sip, sp, SMK_MAXLEN);
2560 return;
2563 * Without guidance regarding the smack value
2564 * for the packet fall back on the network
2565 * ambient value.
2567 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2568 return;
2572 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2573 * @sk: socket
2574 * @skb: packet
2576 * Returns 0 if the packet should be delivered, an error code otherwise
2578 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2580 struct netlbl_lsm_secattr secattr;
2581 struct socket_smack *ssp = sk->sk_security;
2582 char smack[SMK_LABELLEN];
2583 char *csp;
2584 int rc;
2585 struct smk_audit_info ad;
2586 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2587 return 0;
2590 * Translate what netlabel gave us.
2592 netlbl_secattr_init(&secattr);
2594 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2595 if (rc == 0) {
2596 smack_from_secattr(&secattr, smack);
2597 csp = smack;
2598 } else
2599 csp = smack_net_ambient;
2601 netlbl_secattr_destroy(&secattr);
2603 #ifdef CONFIG_AUDIT
2604 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2605 ad.a.u.net.family = sk->sk_family;
2606 ad.a.u.net.netif = skb->skb_iif;
2607 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2608 #endif
2610 * Receiving a packet requires that the other end
2611 * be able to write here. Read access is not required.
2612 * This is the simplist possible security model
2613 * for networking.
2615 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2616 if (rc != 0)
2617 netlbl_skbuff_err(skb, rc, 0);
2618 return rc;
2622 * smack_socket_getpeersec_stream - pull in packet label
2623 * @sock: the socket
2624 * @optval: user's destination
2625 * @optlen: size thereof
2626 * @len: max thereof
2628 * returns zero on success, an error code otherwise
2630 static int smack_socket_getpeersec_stream(struct socket *sock,
2631 char __user *optval,
2632 int __user *optlen, unsigned len)
2634 struct socket_smack *ssp;
2635 int slen;
2636 int rc = 0;
2638 ssp = sock->sk->sk_security;
2639 slen = strlen(ssp->smk_packet) + 1;
2641 if (slen > len)
2642 rc = -ERANGE;
2643 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2644 rc = -EFAULT;
2646 if (put_user(slen, optlen) != 0)
2647 rc = -EFAULT;
2649 return rc;
2654 * smack_socket_getpeersec_dgram - pull in packet label
2655 * @sock: the socket
2656 * @skb: packet data
2657 * @secid: pointer to where to put the secid of the packet
2659 * Sets the netlabel socket state on sk from parent
2661 static int smack_socket_getpeersec_dgram(struct socket *sock,
2662 struct sk_buff *skb, u32 *secid)
2665 struct netlbl_lsm_secattr secattr;
2666 struct sock *sk;
2667 char smack[SMK_LABELLEN];
2668 int family = PF_INET;
2669 u32 s;
2670 int rc;
2673 * Only works for families with packets.
2675 if (sock != NULL) {
2676 sk = sock->sk;
2677 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2678 return 0;
2679 family = sk->sk_family;
2682 * Translate what netlabel gave us.
2684 netlbl_secattr_init(&secattr);
2685 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2686 if (rc == 0)
2687 smack_from_secattr(&secattr, smack);
2688 netlbl_secattr_destroy(&secattr);
2691 * Give up if we couldn't get anything
2693 if (rc != 0)
2694 return rc;
2696 s = smack_to_secid(smack);
2697 if (s == 0)
2698 return -EINVAL;
2700 *secid = s;
2701 return 0;
2705 * smack_sock_graft - Initialize a newly created socket with an existing sock
2706 * @sk: child sock
2707 * @parent: parent socket
2709 * Set the smk_{in,out} state of an existing sock based on the process that
2710 * is creating the new socket.
2712 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2714 struct socket_smack *ssp;
2716 if (sk == NULL ||
2717 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2718 return;
2720 ssp = sk->sk_security;
2721 ssp->smk_in = ssp->smk_out = current_security();
2722 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2726 * smack_inet_conn_request - Smack access check on connect
2727 * @sk: socket involved
2728 * @skb: packet
2729 * @req: unused
2731 * Returns 0 if a task with the packet label could write to
2732 * the socket, otherwise an error code
2734 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2735 struct request_sock *req)
2737 u16 family = sk->sk_family;
2738 struct socket_smack *ssp = sk->sk_security;
2739 struct netlbl_lsm_secattr secattr;
2740 struct sockaddr_in addr;
2741 struct iphdr *hdr;
2742 char smack[SMK_LABELLEN];
2743 int rc;
2744 struct smk_audit_info ad;
2746 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2747 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2748 family = PF_INET;
2750 netlbl_secattr_init(&secattr);
2751 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2752 if (rc == 0)
2753 smack_from_secattr(&secattr, smack);
2754 else
2755 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2756 netlbl_secattr_destroy(&secattr);
2758 #ifdef CONFIG_AUDIT
2759 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2760 ad.a.u.net.family = family;
2761 ad.a.u.net.netif = skb->skb_iif;
2762 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2763 #endif
2765 * Receiving a packet requires that the other end be able to write
2766 * here. Read access is not required.
2768 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
2769 if (rc != 0)
2770 return rc;
2773 * Save the peer's label in the request_sock so we can later setup
2774 * smk_packet in the child socket so that SO_PEERCRED can report it.
2776 req->peer_secid = smack_to_secid(smack);
2779 * We need to decide if we want to label the incoming connection here
2780 * if we do we only need to label the request_sock and the stack will
2781 * propogate the wire-label to the sock when it is created.
2783 hdr = ip_hdr(skb);
2784 addr.sin_addr.s_addr = hdr->saddr;
2785 rcu_read_lock();
2786 if (smack_host_label(&addr) == NULL) {
2787 rcu_read_unlock();
2788 netlbl_secattr_init(&secattr);
2789 smack_to_secattr(smack, &secattr);
2790 rc = netlbl_req_setattr(req, &secattr);
2791 netlbl_secattr_destroy(&secattr);
2792 } else {
2793 rcu_read_unlock();
2794 netlbl_req_delattr(req);
2797 return rc;
2801 * smack_inet_csk_clone - Copy the connection information to the new socket
2802 * @sk: the new socket
2803 * @req: the connection's request_sock
2805 * Transfer the connection's peer label to the newly created socket.
2807 static void smack_inet_csk_clone(struct sock *sk,
2808 const struct request_sock *req)
2810 struct socket_smack *ssp = sk->sk_security;
2811 char *smack;
2813 if (req->peer_secid != 0) {
2814 smack = smack_from_secid(req->peer_secid);
2815 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2816 } else
2817 ssp->smk_packet[0] = '\0';
2821 * Key management security hooks
2823 * Casey has not tested key support very heavily.
2824 * The permission check is most likely too restrictive.
2825 * If you care about keys please have a look.
2827 #ifdef CONFIG_KEYS
2830 * smack_key_alloc - Set the key security blob
2831 * @key: object
2832 * @cred: the credentials to use
2833 * @flags: unused
2835 * No allocation required
2837 * Returns 0
2839 static int smack_key_alloc(struct key *key, const struct cred *cred,
2840 unsigned long flags)
2842 key->security = cred->security;
2843 return 0;
2847 * smack_key_free - Clear the key security blob
2848 * @key: the object
2850 * Clear the blob pointer
2852 static void smack_key_free(struct key *key)
2854 key->security = NULL;
2858 * smack_key_permission - Smack access on a key
2859 * @key_ref: gets to the object
2860 * @cred: the credentials to use
2861 * @perm: unused
2863 * Return 0 if the task has read and write to the object,
2864 * an error code otherwise
2866 static int smack_key_permission(key_ref_t key_ref,
2867 const struct cred *cred, key_perm_t perm)
2869 struct key *keyp;
2870 struct smk_audit_info ad;
2872 keyp = key_ref_to_ptr(key_ref);
2873 if (keyp == NULL)
2874 return -EINVAL;
2876 * If the key hasn't been initialized give it access so that
2877 * it may do so.
2879 if (keyp->security == NULL)
2880 return 0;
2882 * This should not occur
2884 if (cred->security == NULL)
2885 return -EACCES;
2886 #ifdef CONFIG_AUDIT
2887 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2888 ad.a.u.key_struct.key = keyp->serial;
2889 ad.a.u.key_struct.key_desc = keyp->description;
2890 #endif
2891 return smk_access(cred->security, keyp->security,
2892 MAY_READWRITE, &ad);
2894 #endif /* CONFIG_KEYS */
2897 * Smack Audit hooks
2899 * Audit requires a unique representation of each Smack specific
2900 * rule. This unique representation is used to distinguish the
2901 * object to be audited from remaining kernel objects and also
2902 * works as a glue between the audit hooks.
2904 * Since repository entries are added but never deleted, we'll use
2905 * the smack_known label address related to the given audit rule as
2906 * the needed unique representation. This also better fits the smack
2907 * model where nearly everything is a label.
2909 #ifdef CONFIG_AUDIT
2912 * smack_audit_rule_init - Initialize a smack audit rule
2913 * @field: audit rule fields given from user-space (audit.h)
2914 * @op: required testing operator (=, !=, >, <, ...)
2915 * @rulestr: smack label to be audited
2916 * @vrule: pointer to save our own audit rule representation
2918 * Prepare to audit cases where (@field @op @rulestr) is true.
2919 * The label to be audited is created if necessay.
2921 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2923 char **rule = (char **)vrule;
2924 *rule = NULL;
2926 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2927 return -EINVAL;
2929 if (op != Audit_equal && op != Audit_not_equal)
2930 return -EINVAL;
2932 *rule = smk_import(rulestr, 0);
2934 return 0;
2938 * smack_audit_rule_known - Distinguish Smack audit rules
2939 * @krule: rule of interest, in Audit kernel representation format
2941 * This is used to filter Smack rules from remaining Audit ones.
2942 * If it's proved that this rule belongs to us, the
2943 * audit_rule_match hook will be called to do the final judgement.
2945 static int smack_audit_rule_known(struct audit_krule *krule)
2947 struct audit_field *f;
2948 int i;
2950 for (i = 0; i < krule->field_count; i++) {
2951 f = &krule->fields[i];
2953 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2954 return 1;
2957 return 0;
2961 * smack_audit_rule_match - Audit given object ?
2962 * @secid: security id for identifying the object to test
2963 * @field: audit rule flags given from user-space
2964 * @op: required testing operator
2965 * @vrule: smack internal rule presentation
2966 * @actx: audit context associated with the check
2968 * The core Audit hook. It's used to take the decision of
2969 * whether to audit or not to audit a given object.
2971 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2972 struct audit_context *actx)
2974 char *smack;
2975 char *rule = vrule;
2977 if (!rule) {
2978 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2979 "Smack: missing rule\n");
2980 return -ENOENT;
2983 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2984 return 0;
2986 smack = smack_from_secid(secid);
2989 * No need to do string comparisons. If a match occurs,
2990 * both pointers will point to the same smack_known
2991 * label.
2993 if (op == Audit_equal)
2994 return (rule == smack);
2995 if (op == Audit_not_equal)
2996 return (rule != smack);
2998 return 0;
3002 * smack_audit_rule_free - free smack rule representation
3003 * @vrule: rule to be freed.
3005 * No memory was allocated.
3007 static void smack_audit_rule_free(void *vrule)
3009 /* No-op */
3012 #endif /* CONFIG_AUDIT */
3015 * smack_secid_to_secctx - return the smack label for a secid
3016 * @secid: incoming integer
3017 * @secdata: destination
3018 * @seclen: how long it is
3020 * Exists for networking code.
3022 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3024 char *sp = smack_from_secid(secid);
3026 *secdata = sp;
3027 *seclen = strlen(sp);
3028 return 0;
3032 * smack_secctx_to_secid - return the secid for a smack label
3033 * @secdata: smack label
3034 * @seclen: how long result is
3035 * @secid: outgoing integer
3037 * Exists for audit and networking code.
3039 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3041 *secid = smack_to_secid(secdata);
3042 return 0;
3046 * smack_release_secctx - don't do anything.
3047 * @secdata: unused
3048 * @seclen: unused
3050 * Exists to make sure nothing gets done, and properly
3052 static void smack_release_secctx(char *secdata, u32 seclen)
3056 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3058 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3061 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3063 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3066 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3068 int len = 0;
3069 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3071 if (len < 0)
3072 return len;
3073 *ctxlen = len;
3074 return 0;
3077 struct security_operations smack_ops = {
3078 .name = "smack",
3080 .ptrace_access_check = smack_ptrace_access_check,
3081 .ptrace_traceme = smack_ptrace_traceme,
3082 .syslog = smack_syslog,
3084 .sb_alloc_security = smack_sb_alloc_security,
3085 .sb_free_security = smack_sb_free_security,
3086 .sb_copy_data = smack_sb_copy_data,
3087 .sb_kern_mount = smack_sb_kern_mount,
3088 .sb_statfs = smack_sb_statfs,
3089 .sb_mount = smack_sb_mount,
3090 .sb_umount = smack_sb_umount,
3092 .inode_alloc_security = smack_inode_alloc_security,
3093 .inode_free_security = smack_inode_free_security,
3094 .inode_init_security = smack_inode_init_security,
3095 .inode_link = smack_inode_link,
3096 .inode_unlink = smack_inode_unlink,
3097 .inode_rmdir = smack_inode_rmdir,
3098 .inode_rename = smack_inode_rename,
3099 .inode_permission = smack_inode_permission,
3100 .inode_setattr = smack_inode_setattr,
3101 .inode_getattr = smack_inode_getattr,
3102 .inode_setxattr = smack_inode_setxattr,
3103 .inode_post_setxattr = smack_inode_post_setxattr,
3104 .inode_getxattr = smack_inode_getxattr,
3105 .inode_removexattr = smack_inode_removexattr,
3106 .inode_getsecurity = smack_inode_getsecurity,
3107 .inode_setsecurity = smack_inode_setsecurity,
3108 .inode_listsecurity = smack_inode_listsecurity,
3109 .inode_getsecid = smack_inode_getsecid,
3111 .file_permission = smack_file_permission,
3112 .file_alloc_security = smack_file_alloc_security,
3113 .file_free_security = smack_file_free_security,
3114 .file_ioctl = smack_file_ioctl,
3115 .file_lock = smack_file_lock,
3116 .file_fcntl = smack_file_fcntl,
3117 .file_set_fowner = smack_file_set_fowner,
3118 .file_send_sigiotask = smack_file_send_sigiotask,
3119 .file_receive = smack_file_receive,
3121 .cred_alloc_blank = smack_cred_alloc_blank,
3122 .cred_free = smack_cred_free,
3123 .cred_prepare = smack_cred_prepare,
3124 .cred_commit = smack_cred_commit,
3125 .cred_transfer = smack_cred_transfer,
3126 .kernel_act_as = smack_kernel_act_as,
3127 .kernel_create_files_as = smack_kernel_create_files_as,
3128 .task_setpgid = smack_task_setpgid,
3129 .task_getpgid = smack_task_getpgid,
3130 .task_getsid = smack_task_getsid,
3131 .task_getsecid = smack_task_getsecid,
3132 .task_setnice = smack_task_setnice,
3133 .task_setioprio = smack_task_setioprio,
3134 .task_getioprio = smack_task_getioprio,
3135 .task_setscheduler = smack_task_setscheduler,
3136 .task_getscheduler = smack_task_getscheduler,
3137 .task_movememory = smack_task_movememory,
3138 .task_kill = smack_task_kill,
3139 .task_wait = smack_task_wait,
3140 .task_to_inode = smack_task_to_inode,
3142 .ipc_permission = smack_ipc_permission,
3143 .ipc_getsecid = smack_ipc_getsecid,
3145 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3146 .msg_msg_free_security = smack_msg_msg_free_security,
3148 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3149 .msg_queue_free_security = smack_msg_queue_free_security,
3150 .msg_queue_associate = smack_msg_queue_associate,
3151 .msg_queue_msgctl = smack_msg_queue_msgctl,
3152 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3153 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3155 .shm_alloc_security = smack_shm_alloc_security,
3156 .shm_free_security = smack_shm_free_security,
3157 .shm_associate = smack_shm_associate,
3158 .shm_shmctl = smack_shm_shmctl,
3159 .shm_shmat = smack_shm_shmat,
3161 .sem_alloc_security = smack_sem_alloc_security,
3162 .sem_free_security = smack_sem_free_security,
3163 .sem_associate = smack_sem_associate,
3164 .sem_semctl = smack_sem_semctl,
3165 .sem_semop = smack_sem_semop,
3167 .d_instantiate = smack_d_instantiate,
3169 .getprocattr = smack_getprocattr,
3170 .setprocattr = smack_setprocattr,
3172 .unix_stream_connect = smack_unix_stream_connect,
3173 .unix_may_send = smack_unix_may_send,
3175 .socket_post_create = smack_socket_post_create,
3176 .socket_connect = smack_socket_connect,
3177 .socket_sendmsg = smack_socket_sendmsg,
3178 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3179 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3180 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3181 .sk_alloc_security = smack_sk_alloc_security,
3182 .sk_free_security = smack_sk_free_security,
3183 .sock_graft = smack_sock_graft,
3184 .inet_conn_request = smack_inet_conn_request,
3185 .inet_csk_clone = smack_inet_csk_clone,
3187 /* key management security hooks */
3188 #ifdef CONFIG_KEYS
3189 .key_alloc = smack_key_alloc,
3190 .key_free = smack_key_free,
3191 .key_permission = smack_key_permission,
3192 #endif /* CONFIG_KEYS */
3194 /* Audit hooks */
3195 #ifdef CONFIG_AUDIT
3196 .audit_rule_init = smack_audit_rule_init,
3197 .audit_rule_known = smack_audit_rule_known,
3198 .audit_rule_match = smack_audit_rule_match,
3199 .audit_rule_free = smack_audit_rule_free,
3200 #endif /* CONFIG_AUDIT */
3202 .secid_to_secctx = smack_secid_to_secctx,
3203 .secctx_to_secid = smack_secctx_to_secid,
3204 .release_secctx = smack_release_secctx,
3205 .inode_notifysecctx = smack_inode_notifysecctx,
3206 .inode_setsecctx = smack_inode_setsecctx,
3207 .inode_getsecctx = smack_inode_getsecctx,
3211 static __init void init_smack_know_list(void)
3213 list_add(&smack_known_huh.list, &smack_known_list);
3214 list_add(&smack_known_hat.list, &smack_known_list);
3215 list_add(&smack_known_star.list, &smack_known_list);
3216 list_add(&smack_known_floor.list, &smack_known_list);
3217 list_add(&smack_known_invalid.list, &smack_known_list);
3218 list_add(&smack_known_web.list, &smack_known_list);
3222 * smack_init - initialize the smack system
3224 * Returns 0
3226 static __init int smack_init(void)
3228 struct cred *cred;
3230 if (!security_module_enable(&smack_ops))
3231 return 0;
3233 printk(KERN_INFO "Smack: Initializing.\n");
3236 * Set the security state for the initial task.
3238 cred = (struct cred *) current->cred;
3239 cred->security = &smack_known_floor.smk_known;
3241 /* initilize the smack_know_list */
3242 init_smack_know_list();
3244 * Initialize locks
3246 spin_lock_init(&smack_known_huh.smk_cipsolock);
3247 spin_lock_init(&smack_known_hat.smk_cipsolock);
3248 spin_lock_init(&smack_known_star.smk_cipsolock);
3249 spin_lock_init(&smack_known_floor.smk_cipsolock);
3250 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3253 * Register with LSM
3255 if (register_security(&smack_ops))
3256 panic("smack: Unable to register with kernel.\n");
3258 return 0;
3262 * Smack requires early initialization in order to label
3263 * all processes and objects when they are created.
3265 security_initcall(smack_init);