jbd: Journal block numbers can ever be only 32-bit use unsigned int for them
[linux/fpc-iii.git] / security / smack / smack_lsm.c
blobacae7ef4092dd02872b54d7861ad233f8a74978e
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/mutex.h>
29 #include <linux/pipe_fs_i.h>
30 #include <net/netlabel.h>
31 #include <net/cipso_ipv4.h>
32 #include <linux/audit.h>
33 #include "smack.h"
35 #define task_security(task) (task_cred_xxx((task), security))
38 * I hope these are the hokeyist lines of code in the module. Casey.
40 #define DEVPTS_SUPER_MAGIC 0x1cd1
41 #define SOCKFS_MAGIC 0x534F434B
42 #define TMPFS_MAGIC 0x01021994
44 /**
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
52 static char *smk_fetch(struct inode *ip, struct dentry *dp)
54 int rc;
55 char in[SMK_LABELLEN];
57 if (ip->i_op->getxattr == NULL)
58 return NULL;
60 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
61 if (rc < 0)
62 return NULL;
64 return smk_import(in, rc);
67 /**
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
71 * Returns the new blob or NULL if there's no memory available
73 struct inode_smack *new_inode_smack(char *smack)
75 struct inode_smack *isp;
77 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
78 if (isp == NULL)
79 return NULL;
81 isp->smk_inode = smack;
82 isp->smk_flags = 0;
83 mutex_init(&isp->smk_lock);
85 return isp;
89 * LSM hooks.
90 * We he, that is fun!
93 /**
94 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
95 * @ctp: child task pointer
96 * @mode: ptrace attachment mode
98 * Returns 0 if access is OK, an error code otherwise
100 * Do the capability checks, and require read and write.
102 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
104 int rc;
105 struct smk_audit_info ad;
106 char *sp, *tsp;
108 rc = cap_ptrace_access_check(ctp, mode);
109 if (rc != 0)
110 return rc;
112 sp = current_security();
113 tsp = task_security(ctp);
114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
115 smk_ad_setfield_u_tsk(&ad, ctp);
117 /* we won't log here, because rc can be overriden */
118 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
119 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
120 rc = 0;
122 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
123 return rc;
127 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
128 * @ptp: parent task pointer
130 * Returns 0 if access is OK, an error code otherwise
132 * Do the capability checks, and require read and write.
134 static int smack_ptrace_traceme(struct task_struct *ptp)
136 int rc;
137 struct smk_audit_info ad;
138 char *sp, *tsp;
140 rc = cap_ptrace_traceme(ptp);
141 if (rc != 0)
142 return rc;
144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
145 smk_ad_setfield_u_tsk(&ad, ptp);
147 sp = current_security();
148 tsp = task_security(ptp);
149 /* we won't log here, because rc can be overriden */
150 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
151 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
152 rc = 0;
154 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
155 return rc;
159 * smack_syslog - Smack approval on syslog
160 * @type: message type
162 * Require that the task has the floor label
164 * Returns 0 on success, error code otherwise.
166 static int smack_syslog(int type)
168 int rc;
169 char *sp = current_security();
171 rc = cap_syslog(type);
172 if (rc != 0)
173 return rc;
175 if (capable(CAP_MAC_OVERRIDE))
176 return 0;
178 if (sp != smack_known_floor.smk_known)
179 rc = -EACCES;
181 return rc;
186 * Superblock Hooks.
190 * smack_sb_alloc_security - allocate a superblock blob
191 * @sb: the superblock getting the blob
193 * Returns 0 on success or -ENOMEM on error.
195 static int smack_sb_alloc_security(struct super_block *sb)
197 struct superblock_smack *sbsp;
199 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
201 if (sbsp == NULL)
202 return -ENOMEM;
204 sbsp->smk_root = smack_known_floor.smk_known;
205 sbsp->smk_default = smack_known_floor.smk_known;
206 sbsp->smk_floor = smack_known_floor.smk_known;
207 sbsp->smk_hat = smack_known_hat.smk_known;
208 sbsp->smk_initialized = 0;
209 spin_lock_init(&sbsp->smk_sblock);
211 sb->s_security = sbsp;
213 return 0;
217 * smack_sb_free_security - free a superblock blob
218 * @sb: the superblock getting the blob
221 static void smack_sb_free_security(struct super_block *sb)
223 kfree(sb->s_security);
224 sb->s_security = NULL;
228 * smack_sb_copy_data - copy mount options data for processing
229 * @orig: where to start
230 * @smackopts: mount options string
232 * Returns 0 on success or -ENOMEM on error.
234 * Copy the Smack specific mount options out of the mount
235 * options list.
237 static int smack_sb_copy_data(char *orig, char *smackopts)
239 char *cp, *commap, *otheropts, *dp;
241 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
242 if (otheropts == NULL)
243 return -ENOMEM;
245 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
246 if (strstr(cp, SMK_FSDEFAULT) == cp)
247 dp = smackopts;
248 else if (strstr(cp, SMK_FSFLOOR) == cp)
249 dp = smackopts;
250 else if (strstr(cp, SMK_FSHAT) == cp)
251 dp = smackopts;
252 else if (strstr(cp, SMK_FSROOT) == cp)
253 dp = smackopts;
254 else
255 dp = otheropts;
257 commap = strchr(cp, ',');
258 if (commap != NULL)
259 *commap = '\0';
261 if (*dp != '\0')
262 strcat(dp, ",");
263 strcat(dp, cp);
266 strcpy(orig, otheropts);
267 free_page((unsigned long)otheropts);
269 return 0;
273 * smack_sb_kern_mount - Smack specific mount processing
274 * @sb: the file system superblock
275 * @flags: the mount flags
276 * @data: the smack mount options
278 * Returns 0 on success, an error code on failure
280 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
282 struct dentry *root = sb->s_root;
283 struct inode *inode = root->d_inode;
284 struct superblock_smack *sp = sb->s_security;
285 struct inode_smack *isp;
286 char *op;
287 char *commap;
288 char *nsp;
290 spin_lock(&sp->smk_sblock);
291 if (sp->smk_initialized != 0) {
292 spin_unlock(&sp->smk_sblock);
293 return 0;
295 sp->smk_initialized = 1;
296 spin_unlock(&sp->smk_sblock);
298 for (op = data; op != NULL; op = commap) {
299 commap = strchr(op, ',');
300 if (commap != NULL)
301 *commap++ = '\0';
303 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
304 op += strlen(SMK_FSHAT);
305 nsp = smk_import(op, 0);
306 if (nsp != NULL)
307 sp->smk_hat = nsp;
308 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
309 op += strlen(SMK_FSFLOOR);
310 nsp = smk_import(op, 0);
311 if (nsp != NULL)
312 sp->smk_floor = nsp;
313 } else if (strncmp(op, SMK_FSDEFAULT,
314 strlen(SMK_FSDEFAULT)) == 0) {
315 op += strlen(SMK_FSDEFAULT);
316 nsp = smk_import(op, 0);
317 if (nsp != NULL)
318 sp->smk_default = nsp;
319 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
320 op += strlen(SMK_FSROOT);
321 nsp = smk_import(op, 0);
322 if (nsp != NULL)
323 sp->smk_root = nsp;
328 * Initialize the root inode.
330 isp = inode->i_security;
331 if (isp == NULL)
332 inode->i_security = new_inode_smack(sp->smk_root);
333 else
334 isp->smk_inode = sp->smk_root;
336 return 0;
340 * smack_sb_statfs - Smack check on statfs
341 * @dentry: identifies the file system in question
343 * Returns 0 if current can read the floor of the filesystem,
344 * and error code otherwise
346 static int smack_sb_statfs(struct dentry *dentry)
348 struct superblock_smack *sbp = dentry->d_sb->s_security;
349 int rc;
350 struct smk_audit_info ad;
352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
355 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
356 return rc;
360 * smack_sb_mount - Smack check for mounting
361 * @dev_name: unused
362 * @path: mount point
363 * @type: unused
364 * @flags: unused
365 * @data: unused
367 * Returns 0 if current can write the floor of the filesystem
368 * being mounted on, an error code otherwise.
370 static int smack_sb_mount(char *dev_name, struct path *path,
371 char *type, unsigned long flags, void *data)
373 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
374 struct smk_audit_info ad;
376 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
377 smk_ad_setfield_u_fs_path(&ad, *path);
379 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
383 * smack_sb_umount - Smack check for unmounting
384 * @mnt: file system to unmount
385 * @flags: unused
387 * Returns 0 if current can write the floor of the filesystem
388 * being unmounted, an error code otherwise.
390 static int smack_sb_umount(struct vfsmount *mnt, int flags)
392 struct superblock_smack *sbp;
393 struct smk_audit_info ad;
395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
396 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_mountpoint);
397 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
399 sbp = mnt->mnt_sb->s_security;
400 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
404 * Inode hooks
408 * smack_inode_alloc_security - allocate an inode blob
409 * @inode: the inode in need of a blob
411 * Returns 0 if it gets a blob, -ENOMEM otherwise
413 static int smack_inode_alloc_security(struct inode *inode)
415 inode->i_security = new_inode_smack(current_security());
416 if (inode->i_security == NULL)
417 return -ENOMEM;
418 return 0;
422 * smack_inode_free_security - free an inode blob
423 * @inode: the inode with a blob
425 * Clears the blob pointer in inode
427 static void smack_inode_free_security(struct inode *inode)
429 kfree(inode->i_security);
430 inode->i_security = NULL;
434 * smack_inode_init_security - copy out the smack from an inode
435 * @inode: the inode
436 * @dir: unused
437 * @name: where to put the attribute name
438 * @value: where to put the attribute value
439 * @len: where to put the length of the attribute
441 * Returns 0 if it all works out, -ENOMEM if there's no memory
443 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
444 char **name, void **value, size_t *len)
446 char *isp = smk_of_inode(inode);
448 if (name) {
449 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
450 if (*name == NULL)
451 return -ENOMEM;
454 if (value) {
455 *value = kstrdup(isp, GFP_KERNEL);
456 if (*value == NULL)
457 return -ENOMEM;
460 if (len)
461 *len = strlen(isp) + 1;
463 return 0;
467 * smack_inode_link - Smack check on link
468 * @old_dentry: the existing object
469 * @dir: unused
470 * @new_dentry: the new object
472 * Returns 0 if access is permitted, an error code otherwise
474 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
475 struct dentry *new_dentry)
477 char *isp;
478 struct smk_audit_info ad;
479 int rc;
481 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
482 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
484 isp = smk_of_inode(old_dentry->d_inode);
485 rc = smk_curacc(isp, MAY_WRITE, &ad);
487 if (rc == 0 && new_dentry->d_inode != NULL) {
488 isp = smk_of_inode(new_dentry->d_inode);
489 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
490 rc = smk_curacc(isp, MAY_WRITE, &ad);
493 return rc;
497 * smack_inode_unlink - Smack check on inode deletion
498 * @dir: containing directory object
499 * @dentry: file to unlink
501 * Returns 0 if current can write the containing directory
502 * and the object, error code otherwise
504 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
506 struct inode *ip = dentry->d_inode;
507 struct smk_audit_info ad;
508 int rc;
510 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
511 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
514 * You need write access to the thing you're unlinking
516 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
517 if (rc == 0) {
519 * You also need write access to the containing directory
521 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
522 smk_ad_setfield_u_fs_inode(&ad, dir);
523 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
525 return rc;
529 * smack_inode_rmdir - Smack check on directory deletion
530 * @dir: containing directory object
531 * @dentry: directory to unlink
533 * Returns 0 if current can write the containing directory
534 * and the directory, error code otherwise
536 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
538 struct smk_audit_info ad;
539 int rc;
541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
542 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
545 * You need write access to the thing you're removing
547 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
548 if (rc == 0) {
550 * You also need write access to the containing directory
552 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
553 smk_ad_setfield_u_fs_inode(&ad, dir);
554 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
557 return rc;
561 * smack_inode_rename - Smack check on rename
562 * @old_inode: the old directory
563 * @old_dentry: unused
564 * @new_inode: the new directory
565 * @new_dentry: unused
567 * Read and write access is required on both the old and
568 * new directories.
570 * Returns 0 if access is permitted, an error code otherwise
572 static int smack_inode_rename(struct inode *old_inode,
573 struct dentry *old_dentry,
574 struct inode *new_inode,
575 struct dentry *new_dentry)
577 int rc;
578 char *isp;
579 struct smk_audit_info ad;
581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
582 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
584 isp = smk_of_inode(old_dentry->d_inode);
585 rc = smk_curacc(isp, MAY_READWRITE, &ad);
587 if (rc == 0 && new_dentry->d_inode != NULL) {
588 isp = smk_of_inode(new_dentry->d_inode);
589 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
590 rc = smk_curacc(isp, MAY_READWRITE, &ad);
592 return rc;
596 * smack_inode_permission - Smack version of permission()
597 * @inode: the inode in question
598 * @mask: the access requested
600 * This is the important Smack hook.
602 * Returns 0 if access is permitted, -EACCES otherwise
604 static int smack_inode_permission(struct inode *inode, int mask)
606 struct smk_audit_info ad;
608 * No permission to check. Existence test. Yup, it's there.
610 if (mask == 0)
611 return 0;
612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
613 smk_ad_setfield_u_fs_inode(&ad, inode);
614 return smk_curacc(smk_of_inode(inode), mask, &ad);
618 * smack_inode_setattr - Smack check for setting attributes
619 * @dentry: the object
620 * @iattr: for the force flag
622 * Returns 0 if access is permitted, an error code otherwise
624 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
626 struct smk_audit_info ad;
628 * Need to allow for clearing the setuid bit.
630 if (iattr->ia_valid & ATTR_FORCE)
631 return 0;
632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
633 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
635 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
639 * smack_inode_getattr - Smack check for getting attributes
640 * @mnt: unused
641 * @dentry: the object
643 * Returns 0 if access is permitted, an error code otherwise
645 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
647 struct smk_audit_info ad;
649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
650 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
651 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
652 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
656 * smack_inode_setxattr - Smack check for setting xattrs
657 * @dentry: the object
658 * @name: name of the attribute
659 * @value: unused
660 * @size: unused
661 * @flags: unused
663 * This protects the Smack attribute explicitly.
665 * Returns 0 if access is permitted, an error code otherwise
667 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
668 const void *value, size_t size, int flags)
670 struct smk_audit_info ad;
671 int rc = 0;
673 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
674 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
675 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
676 if (!capable(CAP_MAC_ADMIN))
677 rc = -EPERM;
679 * check label validity here so import wont fail on
680 * post_setxattr
682 if (size == 0 || size >= SMK_LABELLEN ||
683 smk_import(value, size) == NULL)
684 rc = -EINVAL;
685 } else
686 rc = cap_inode_setxattr(dentry, name, value, size, flags);
688 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
689 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
691 if (rc == 0)
692 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
694 return rc;
698 * smack_inode_post_setxattr - Apply the Smack update approved above
699 * @dentry: object
700 * @name: attribute name
701 * @value: attribute value
702 * @size: attribute size
703 * @flags: unused
705 * Set the pointer in the inode blob to the entry found
706 * in the master label list.
708 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
709 const void *value, size_t size, int flags)
711 struct inode_smack *isp;
712 char *nsp;
715 * Not SMACK
717 if (strcmp(name, XATTR_NAME_SMACK))
718 return;
720 isp = dentry->d_inode->i_security;
723 * No locking is done here. This is a pointer
724 * assignment.
726 nsp = smk_import(value, size);
727 if (nsp != NULL)
728 isp->smk_inode = nsp;
729 else
730 isp->smk_inode = smack_known_invalid.smk_known;
732 return;
736 * smack_inode_getxattr - Smack check on getxattr
737 * @dentry: the object
738 * @name: unused
740 * Returns 0 if access is permitted, an error code otherwise
742 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
744 struct smk_audit_info ad;
746 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
747 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
749 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
753 * smack_inode_removexattr - Smack check on removexattr
754 * @dentry: the object
755 * @name: name of the attribute
757 * Removing the Smack attribute requires CAP_MAC_ADMIN
759 * Returns 0 if access is permitted, an error code otherwise
761 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
763 struct smk_audit_info ad;
764 int rc = 0;
766 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
767 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
768 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
769 if (!capable(CAP_MAC_ADMIN))
770 rc = -EPERM;
771 } else
772 rc = cap_inode_removexattr(dentry, name);
774 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
775 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
776 if (rc == 0)
777 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
779 return rc;
783 * smack_inode_getsecurity - get smack xattrs
784 * @inode: the object
785 * @name: attribute name
786 * @buffer: where to put the result
787 * @alloc: unused
789 * Returns the size of the attribute or an error code
791 static int smack_inode_getsecurity(const struct inode *inode,
792 const char *name, void **buffer,
793 bool alloc)
795 struct socket_smack *ssp;
796 struct socket *sock;
797 struct super_block *sbp;
798 struct inode *ip = (struct inode *)inode;
799 char *isp;
800 int ilen;
801 int rc = 0;
803 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
804 isp = smk_of_inode(inode);
805 ilen = strlen(isp) + 1;
806 *buffer = isp;
807 return ilen;
811 * The rest of the Smack xattrs are only on sockets.
813 sbp = ip->i_sb;
814 if (sbp->s_magic != SOCKFS_MAGIC)
815 return -EOPNOTSUPP;
817 sock = SOCKET_I(ip);
818 if (sock == NULL || sock->sk == NULL)
819 return -EOPNOTSUPP;
821 ssp = sock->sk->sk_security;
823 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
824 isp = ssp->smk_in;
825 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
826 isp = ssp->smk_out;
827 else
828 return -EOPNOTSUPP;
830 ilen = strlen(isp) + 1;
831 if (rc == 0) {
832 *buffer = isp;
833 rc = ilen;
836 return rc;
841 * smack_inode_listsecurity - list the Smack attributes
842 * @inode: the object
843 * @buffer: where they go
844 * @buffer_size: size of buffer
846 * Returns 0 on success, -EINVAL otherwise
848 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
849 size_t buffer_size)
851 int len = strlen(XATTR_NAME_SMACK);
853 if (buffer != NULL && len <= buffer_size) {
854 memcpy(buffer, XATTR_NAME_SMACK, len);
855 return len;
857 return -EINVAL;
861 * smack_inode_getsecid - Extract inode's security id
862 * @inode: inode to extract the info from
863 * @secid: where result will be saved
865 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
867 struct inode_smack *isp = inode->i_security;
869 *secid = smack_to_secid(isp->smk_inode);
873 * File Hooks
877 * smack_file_permission - Smack check on file operations
878 * @file: unused
879 * @mask: unused
881 * Returns 0
883 * Should access checks be done on each read or write?
884 * UNICOS and SELinux say yes.
885 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
887 * I'll say no for now. Smack does not do the frequent
888 * label changing that SELinux does.
890 static int smack_file_permission(struct file *file, int mask)
892 return 0;
896 * smack_file_alloc_security - assign a file security blob
897 * @file: the object
899 * The security blob for a file is a pointer to the master
900 * label list, so no allocation is done.
902 * Returns 0
904 static int smack_file_alloc_security(struct file *file)
906 file->f_security = current_security();
907 return 0;
911 * smack_file_free_security - clear a file security blob
912 * @file: the object
914 * The security blob for a file is a pointer to the master
915 * label list, so no memory is freed.
917 static void smack_file_free_security(struct file *file)
919 file->f_security = NULL;
923 * smack_file_ioctl - Smack check on ioctls
924 * @file: the object
925 * @cmd: what to do
926 * @arg: unused
928 * Relies heavily on the correct use of the ioctl command conventions.
930 * Returns 0 if allowed, error code otherwise
932 static int smack_file_ioctl(struct file *file, unsigned int cmd,
933 unsigned long arg)
935 int rc = 0;
936 struct smk_audit_info ad;
938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
941 if (_IOC_DIR(cmd) & _IOC_WRITE)
942 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
944 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
945 rc = smk_curacc(file->f_security, MAY_READ, &ad);
947 return rc;
951 * smack_file_lock - Smack check on file locking
952 * @file: the object
953 * @cmd: unused
955 * Returns 0 if current has write access, error code otherwise
957 static int smack_file_lock(struct file *file, unsigned int cmd)
959 struct smk_audit_info ad;
961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
962 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
963 return smk_curacc(file->f_security, MAY_WRITE, &ad);
967 * smack_file_fcntl - Smack check on fcntl
968 * @file: the object
969 * @cmd: what action to check
970 * @arg: unused
972 * Returns 0 if current has access, error code otherwise
974 static int smack_file_fcntl(struct file *file, unsigned int cmd,
975 unsigned long arg)
977 struct smk_audit_info ad;
978 int rc;
980 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
981 smk_ad_setfield_u_fs_path(&ad, file->f_path);
983 switch (cmd) {
984 case F_DUPFD:
985 case F_GETFD:
986 case F_GETFL:
987 case F_GETLK:
988 case F_GETOWN:
989 case F_GETSIG:
990 rc = smk_curacc(file->f_security, MAY_READ, &ad);
991 break;
992 case F_SETFD:
993 case F_SETFL:
994 case F_SETLK:
995 case F_SETLKW:
996 case F_SETOWN:
997 case F_SETSIG:
998 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
999 break;
1000 default:
1001 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
1004 return rc;
1008 * smack_file_set_fowner - set the file security blob value
1009 * @file: object in question
1011 * Returns 0
1012 * Further research may be required on this one.
1014 static int smack_file_set_fowner(struct file *file)
1016 file->f_security = current_security();
1017 return 0;
1021 * smack_file_send_sigiotask - Smack on sigio
1022 * @tsk: The target task
1023 * @fown: the object the signal come from
1024 * @signum: unused
1026 * Allow a privileged task to get signals even if it shouldn't
1028 * Returns 0 if a subject with the object's smack could
1029 * write to the task, an error code otherwise.
1031 static int smack_file_send_sigiotask(struct task_struct *tsk,
1032 struct fown_struct *fown, int signum)
1034 struct file *file;
1035 int rc;
1036 char *tsp = tsk->cred->security;
1037 struct smk_audit_info ad;
1040 * struct fown_struct is never outside the context of a struct file
1042 file = container_of(fown, struct file, f_owner);
1043 /* we don't log here as rc can be overriden */
1044 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1045 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1046 rc = 0;
1048 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1049 smk_ad_setfield_u_tsk(&ad, tsk);
1050 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1051 return rc;
1055 * smack_file_receive - Smack file receive check
1056 * @file: the object
1058 * Returns 0 if current has access, error code otherwise
1060 static int smack_file_receive(struct file *file)
1062 int may = 0;
1063 struct smk_audit_info ad;
1065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1066 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1068 * This code relies on bitmasks.
1070 if (file->f_mode & FMODE_READ)
1071 may = MAY_READ;
1072 if (file->f_mode & FMODE_WRITE)
1073 may |= MAY_WRITE;
1075 return smk_curacc(file->f_security, may, &ad);
1079 * Task hooks
1083 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1084 * @new: the new credentials
1085 * @gfp: the atomicity of any memory allocations
1087 * Prepare a blank set of credentials for modification. This must allocate all
1088 * the memory the LSM module might require such that cred_transfer() can
1089 * complete without error.
1091 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1093 cred->security = NULL;
1094 return 0;
1099 * smack_cred_free - "free" task-level security credentials
1100 * @cred: the credentials in question
1102 * Smack isn't using copies of blobs. Everyone
1103 * points to an immutable list. The blobs never go away.
1104 * There is no leak here.
1106 static void smack_cred_free(struct cred *cred)
1108 cred->security = NULL;
1112 * smack_cred_prepare - prepare new set of credentials for modification
1113 * @new: the new credentials
1114 * @old: the original credentials
1115 * @gfp: the atomicity of any memory allocations
1117 * Prepare a new set of credentials for modification.
1119 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1120 gfp_t gfp)
1122 new->security = old->security;
1123 return 0;
1127 * smack_cred_commit - commit new credentials
1128 * @new: the new credentials
1129 * @old: the original credentials
1131 static void smack_cred_commit(struct cred *new, const struct cred *old)
1136 * smack_cred_transfer - Transfer the old credentials to the new credentials
1137 * @new: the new credentials
1138 * @old: the original credentials
1140 * Fill in a set of blank credentials from another set of credentials.
1142 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1144 new->security = old->security;
1148 * smack_kernel_act_as - Set the subjective context in a set of credentials
1149 * @new: points to the set of credentials to be modified.
1150 * @secid: specifies the security ID to be set
1152 * Set the security data for a kernel service.
1154 static int smack_kernel_act_as(struct cred *new, u32 secid)
1156 char *smack = smack_from_secid(secid);
1158 if (smack == NULL)
1159 return -EINVAL;
1161 new->security = smack;
1162 return 0;
1166 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1167 * @new: points to the set of credentials to be modified
1168 * @inode: points to the inode to use as a reference
1170 * Set the file creation context in a set of credentials to the same
1171 * as the objective context of the specified inode
1173 static int smack_kernel_create_files_as(struct cred *new,
1174 struct inode *inode)
1176 struct inode_smack *isp = inode->i_security;
1178 new->security = isp->smk_inode;
1179 return 0;
1183 * smk_curacc_on_task - helper to log task related access
1184 * @p: the task object
1185 * @access : the access requested
1187 * Return 0 if access is permitted
1189 static int smk_curacc_on_task(struct task_struct *p, int access)
1191 struct smk_audit_info ad;
1193 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1194 smk_ad_setfield_u_tsk(&ad, p);
1195 return smk_curacc(task_security(p), access, &ad);
1199 * smack_task_setpgid - Smack check on setting pgid
1200 * @p: the task object
1201 * @pgid: unused
1203 * Return 0 if write access is permitted
1205 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1207 return smk_curacc_on_task(p, MAY_WRITE);
1211 * smack_task_getpgid - Smack access check for getpgid
1212 * @p: the object task
1214 * Returns 0 if current can read the object task, error code otherwise
1216 static int smack_task_getpgid(struct task_struct *p)
1218 return smk_curacc_on_task(p, MAY_READ);
1222 * smack_task_getsid - Smack access check for getsid
1223 * @p: the object task
1225 * Returns 0 if current can read the object task, error code otherwise
1227 static int smack_task_getsid(struct task_struct *p)
1229 return smk_curacc_on_task(p, MAY_READ);
1233 * smack_task_getsecid - get the secid of the task
1234 * @p: the object task
1235 * @secid: where to put the result
1237 * Sets the secid to contain a u32 version of the smack label.
1239 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1241 *secid = smack_to_secid(task_security(p));
1245 * smack_task_setnice - Smack check on setting nice
1246 * @p: the task object
1247 * @nice: unused
1249 * Return 0 if write access is permitted
1251 static int smack_task_setnice(struct task_struct *p, int nice)
1253 int rc;
1255 rc = cap_task_setnice(p, nice);
1256 if (rc == 0)
1257 rc = smk_curacc_on_task(p, MAY_WRITE);
1258 return rc;
1262 * smack_task_setioprio - Smack check on setting ioprio
1263 * @p: the task object
1264 * @ioprio: unused
1266 * Return 0 if write access is permitted
1268 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1270 int rc;
1272 rc = cap_task_setioprio(p, ioprio);
1273 if (rc == 0)
1274 rc = smk_curacc_on_task(p, MAY_WRITE);
1275 return rc;
1279 * smack_task_getioprio - Smack check on reading ioprio
1280 * @p: the task object
1282 * Return 0 if read access is permitted
1284 static int smack_task_getioprio(struct task_struct *p)
1286 return smk_curacc_on_task(p, MAY_READ);
1290 * smack_task_setscheduler - Smack check on setting scheduler
1291 * @p: the task object
1292 * @policy: unused
1293 * @lp: unused
1295 * Return 0 if read access is permitted
1297 static int smack_task_setscheduler(struct task_struct *p, int policy,
1298 struct sched_param *lp)
1300 int rc;
1302 rc = cap_task_setscheduler(p, policy, lp);
1303 if (rc == 0)
1304 rc = smk_curacc_on_task(p, MAY_WRITE);
1305 return rc;
1309 * smack_task_getscheduler - Smack check on reading scheduler
1310 * @p: the task object
1312 * Return 0 if read access is permitted
1314 static int smack_task_getscheduler(struct task_struct *p)
1316 return smk_curacc_on_task(p, MAY_READ);
1320 * smack_task_movememory - Smack check on moving memory
1321 * @p: the task object
1323 * Return 0 if write access is permitted
1325 static int smack_task_movememory(struct task_struct *p)
1327 return smk_curacc_on_task(p, MAY_WRITE);
1331 * smack_task_kill - Smack check on signal delivery
1332 * @p: the task object
1333 * @info: unused
1334 * @sig: unused
1335 * @secid: identifies the smack to use in lieu of current's
1337 * Return 0 if write access is permitted
1339 * The secid behavior is an artifact of an SELinux hack
1340 * in the USB code. Someday it may go away.
1342 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1343 int sig, u32 secid)
1345 struct smk_audit_info ad;
1347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1348 smk_ad_setfield_u_tsk(&ad, p);
1350 * Sending a signal requires that the sender
1351 * can write the receiver.
1353 if (secid == 0)
1354 return smk_curacc(task_security(p), MAY_WRITE, &ad);
1356 * If the secid isn't 0 we're dealing with some USB IO
1357 * specific behavior. This is not clean. For one thing
1358 * we can't take privilege into account.
1360 return smk_access(smack_from_secid(secid), task_security(p),
1361 MAY_WRITE, &ad);
1365 * smack_task_wait - Smack access check for waiting
1366 * @p: task to wait for
1368 * Returns 0 if current can wait for p, error code otherwise
1370 static int smack_task_wait(struct task_struct *p)
1372 struct smk_audit_info ad;
1373 char *sp = current_security();
1374 char *tsp = task_security(p);
1375 int rc;
1377 /* we don't log here, we can be overriden */
1378 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
1379 if (rc == 0)
1380 goto out_log;
1383 * Allow the operation to succeed if either task
1384 * has privilege to perform operations that might
1385 * account for the smack labels having gotten to
1386 * be different in the first place.
1388 * This breaks the strict subject/object access
1389 * control ideal, taking the object's privilege
1390 * state into account in the decision as well as
1391 * the smack value.
1393 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1394 rc = 0;
1395 /* we log only if we didn't get overriden */
1396 out_log:
1397 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1398 smk_ad_setfield_u_tsk(&ad, p);
1399 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
1400 return rc;
1404 * smack_task_to_inode - copy task smack into the inode blob
1405 * @p: task to copy from
1406 * @inode: inode to copy to
1408 * Sets the smack pointer in the inode security blob
1410 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1412 struct inode_smack *isp = inode->i_security;
1413 isp->smk_inode = task_security(p);
1417 * Socket hooks.
1421 * smack_sk_alloc_security - Allocate a socket blob
1422 * @sk: the socket
1423 * @family: unused
1424 * @gfp_flags: memory allocation flags
1426 * Assign Smack pointers to current
1428 * Returns 0 on success, -ENOMEM is there's no memory
1430 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1432 char *csp = current_security();
1433 struct socket_smack *ssp;
1435 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1436 if (ssp == NULL)
1437 return -ENOMEM;
1439 ssp->smk_in = csp;
1440 ssp->smk_out = csp;
1441 ssp->smk_packet[0] = '\0';
1443 sk->sk_security = ssp;
1445 return 0;
1449 * smack_sk_free_security - Free a socket blob
1450 * @sk: the socket
1452 * Clears the blob pointer
1454 static void smack_sk_free_security(struct sock *sk)
1456 kfree(sk->sk_security);
1460 * smack_host_label - check host based restrictions
1461 * @sip: the object end
1463 * looks for host based access restrictions
1465 * This version will only be appropriate for really small sets of single label
1466 * hosts. The caller is responsible for ensuring that the RCU read lock is
1467 * taken before calling this function.
1469 * Returns the label of the far end or NULL if it's not special.
1471 static char *smack_host_label(struct sockaddr_in *sip)
1473 struct smk_netlbladdr *snp;
1474 struct in_addr *siap = &sip->sin_addr;
1476 if (siap->s_addr == 0)
1477 return NULL;
1479 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1481 * we break after finding the first match because
1482 * the list is sorted from longest to shortest mask
1483 * so we have found the most specific match
1485 if ((&snp->smk_host.sin_addr)->s_addr ==
1486 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1487 /* we have found the special CIPSO option */
1488 if (snp->smk_label == smack_cipso_option)
1489 return NULL;
1490 return snp->smk_label;
1493 return NULL;
1497 * smack_set_catset - convert a capset to netlabel mls categories
1498 * @catset: the Smack categories
1499 * @sap: where to put the netlabel categories
1501 * Allocates and fills attr.mls.cat
1503 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1505 unsigned char *cp;
1506 unsigned char m;
1507 int cat;
1508 int rc;
1509 int byte;
1511 if (!catset)
1512 return;
1514 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1515 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1516 sap->attr.mls.cat->startbit = 0;
1518 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1519 for (m = 0x80; m != 0; m >>= 1, cat++) {
1520 if ((m & *cp) == 0)
1521 continue;
1522 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1523 cat, GFP_ATOMIC);
1528 * smack_to_secattr - fill a secattr from a smack value
1529 * @smack: the smack value
1530 * @nlsp: where the result goes
1532 * Casey says that CIPSO is good enough for now.
1533 * It can be used to effect.
1534 * It can also be abused to effect when necessary.
1535 * Appologies to the TSIG group in general and GW in particular.
1537 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1539 struct smack_cipso cipso;
1540 int rc;
1542 nlsp->domain = smack;
1543 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1545 rc = smack_to_cipso(smack, &cipso);
1546 if (rc == 0) {
1547 nlsp->attr.mls.lvl = cipso.smk_level;
1548 smack_set_catset(cipso.smk_catset, nlsp);
1549 } else {
1550 nlsp->attr.mls.lvl = smack_cipso_direct;
1551 smack_set_catset(smack, nlsp);
1556 * smack_netlabel - Set the secattr on a socket
1557 * @sk: the socket
1558 * @labeled: socket label scheme
1560 * Convert the outbound smack value (smk_out) to a
1561 * secattr and attach it to the socket.
1563 * Returns 0 on success or an error code
1565 static int smack_netlabel(struct sock *sk, int labeled)
1567 struct socket_smack *ssp = sk->sk_security;
1568 struct netlbl_lsm_secattr secattr;
1569 int rc = 0;
1572 * Usually the netlabel code will handle changing the
1573 * packet labeling based on the label.
1574 * The case of a single label host is different, because
1575 * a single label host should never get a labeled packet
1576 * even though the label is usually associated with a packet
1577 * label.
1579 local_bh_disable();
1580 bh_lock_sock_nested(sk);
1582 if (ssp->smk_out == smack_net_ambient ||
1583 labeled == SMACK_UNLABELED_SOCKET)
1584 netlbl_sock_delattr(sk);
1585 else {
1586 netlbl_secattr_init(&secattr);
1587 smack_to_secattr(ssp->smk_out, &secattr);
1588 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1589 netlbl_secattr_destroy(&secattr);
1592 bh_unlock_sock(sk);
1593 local_bh_enable();
1595 return rc;
1599 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1600 * @sk: the socket
1601 * @sap: the destination address
1603 * Set the correct secattr for the given socket based on the destination
1604 * address and perform any outbound access checks needed.
1606 * Returns 0 on success or an error code.
1609 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1611 int rc;
1612 int sk_lbl;
1613 char *hostsp;
1614 struct socket_smack *ssp = sk->sk_security;
1615 struct smk_audit_info ad;
1617 rcu_read_lock();
1618 hostsp = smack_host_label(sap);
1619 if (hostsp != NULL) {
1620 sk_lbl = SMACK_UNLABELED_SOCKET;
1621 #ifdef CONFIG_AUDIT
1622 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1623 ad.a.u.net.family = sap->sin_family;
1624 ad.a.u.net.dport = sap->sin_port;
1625 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1626 #endif
1627 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1628 } else {
1629 sk_lbl = SMACK_CIPSO_SOCKET;
1630 rc = 0;
1632 rcu_read_unlock();
1633 if (rc != 0)
1634 return rc;
1636 return smack_netlabel(sk, sk_lbl);
1640 * smack_inode_setsecurity - set smack xattrs
1641 * @inode: the object
1642 * @name: attribute name
1643 * @value: attribute value
1644 * @size: size of the attribute
1645 * @flags: unused
1647 * Sets the named attribute in the appropriate blob
1649 * Returns 0 on success, or an error code
1651 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1652 const void *value, size_t size, int flags)
1654 char *sp;
1655 struct inode_smack *nsp = inode->i_security;
1656 struct socket_smack *ssp;
1657 struct socket *sock;
1658 int rc = 0;
1660 if (value == NULL || size > SMK_LABELLEN || size == 0)
1661 return -EACCES;
1663 sp = smk_import(value, size);
1664 if (sp == NULL)
1665 return -EINVAL;
1667 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1668 nsp->smk_inode = sp;
1669 nsp->smk_flags |= SMK_INODE_INSTANT;
1670 return 0;
1673 * The rest of the Smack xattrs are only on sockets.
1675 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1676 return -EOPNOTSUPP;
1678 sock = SOCKET_I(inode);
1679 if (sock == NULL || sock->sk == NULL)
1680 return -EOPNOTSUPP;
1682 ssp = sock->sk->sk_security;
1684 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1685 ssp->smk_in = sp;
1686 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1687 ssp->smk_out = sp;
1688 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1689 if (rc != 0)
1690 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1691 __func__, -rc);
1692 } else
1693 return -EOPNOTSUPP;
1695 return 0;
1699 * smack_socket_post_create - finish socket setup
1700 * @sock: the socket
1701 * @family: protocol family
1702 * @type: unused
1703 * @protocol: unused
1704 * @kern: unused
1706 * Sets the netlabel information on the socket
1708 * Returns 0 on success, and error code otherwise
1710 static int smack_socket_post_create(struct socket *sock, int family,
1711 int type, int protocol, int kern)
1713 if (family != PF_INET || sock->sk == NULL)
1714 return 0;
1716 * Set the outbound netlbl.
1718 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1722 * smack_socket_connect - connect access check
1723 * @sock: the socket
1724 * @sap: the other end
1725 * @addrlen: size of sap
1727 * Verifies that a connection may be possible
1729 * Returns 0 on success, and error code otherwise
1731 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1732 int addrlen)
1734 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1735 return 0;
1736 if (addrlen < sizeof(struct sockaddr_in))
1737 return -EINVAL;
1739 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
1743 * smack_flags_to_may - convert S_ to MAY_ values
1744 * @flags: the S_ value
1746 * Returns the equivalent MAY_ value
1748 static int smack_flags_to_may(int flags)
1750 int may = 0;
1752 if (flags & S_IRUGO)
1753 may |= MAY_READ;
1754 if (flags & S_IWUGO)
1755 may |= MAY_WRITE;
1756 if (flags & S_IXUGO)
1757 may |= MAY_EXEC;
1759 return may;
1763 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1764 * @msg: the object
1766 * Returns 0
1768 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1770 msg->security = current_security();
1771 return 0;
1775 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1776 * @msg: the object
1778 * Clears the blob pointer
1780 static void smack_msg_msg_free_security(struct msg_msg *msg)
1782 msg->security = NULL;
1786 * smack_of_shm - the smack pointer for the shm
1787 * @shp: the object
1789 * Returns a pointer to the smack value
1791 static char *smack_of_shm(struct shmid_kernel *shp)
1793 return (char *)shp->shm_perm.security;
1797 * smack_shm_alloc_security - Set the security blob for shm
1798 * @shp: the object
1800 * Returns 0
1802 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1804 struct kern_ipc_perm *isp = &shp->shm_perm;
1806 isp->security = current_security();
1807 return 0;
1811 * smack_shm_free_security - Clear the security blob for shm
1812 * @shp: the object
1814 * Clears the blob pointer
1816 static void smack_shm_free_security(struct shmid_kernel *shp)
1818 struct kern_ipc_perm *isp = &shp->shm_perm;
1820 isp->security = NULL;
1824 * smk_curacc_shm : check if current has access on shm
1825 * @shp : the object
1826 * @access : access requested
1828 * Returns 0 if current has the requested access, error code otherwise
1830 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1832 char *ssp = smack_of_shm(shp);
1833 struct smk_audit_info ad;
1835 #ifdef CONFIG_AUDIT
1836 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1837 ad.a.u.ipc_id = shp->shm_perm.id;
1838 #endif
1839 return smk_curacc(ssp, access, &ad);
1843 * smack_shm_associate - Smack access check for shm
1844 * @shp: the object
1845 * @shmflg: access requested
1847 * Returns 0 if current has the requested access, error code otherwise
1849 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1851 int may;
1853 may = smack_flags_to_may(shmflg);
1854 return smk_curacc_shm(shp, may);
1858 * smack_shm_shmctl - Smack access check for shm
1859 * @shp: the object
1860 * @cmd: what it wants to do
1862 * Returns 0 if current has the requested access, error code otherwise
1864 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1866 int may;
1868 switch (cmd) {
1869 case IPC_STAT:
1870 case SHM_STAT:
1871 may = MAY_READ;
1872 break;
1873 case IPC_SET:
1874 case SHM_LOCK:
1875 case SHM_UNLOCK:
1876 case IPC_RMID:
1877 may = MAY_READWRITE;
1878 break;
1879 case IPC_INFO:
1880 case SHM_INFO:
1882 * System level information.
1884 return 0;
1885 default:
1886 return -EINVAL;
1888 return smk_curacc_shm(shp, may);
1892 * smack_shm_shmat - Smack access for shmat
1893 * @shp: the object
1894 * @shmaddr: unused
1895 * @shmflg: access requested
1897 * Returns 0 if current has the requested access, error code otherwise
1899 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1900 int shmflg)
1902 int may;
1904 may = smack_flags_to_may(shmflg);
1905 return smk_curacc_shm(shp, may);
1909 * smack_of_sem - the smack pointer for the sem
1910 * @sma: the object
1912 * Returns a pointer to the smack value
1914 static char *smack_of_sem(struct sem_array *sma)
1916 return (char *)sma->sem_perm.security;
1920 * smack_sem_alloc_security - Set the security blob for sem
1921 * @sma: the object
1923 * Returns 0
1925 static int smack_sem_alloc_security(struct sem_array *sma)
1927 struct kern_ipc_perm *isp = &sma->sem_perm;
1929 isp->security = current_security();
1930 return 0;
1934 * smack_sem_free_security - Clear the security blob for sem
1935 * @sma: the object
1937 * Clears the blob pointer
1939 static void smack_sem_free_security(struct sem_array *sma)
1941 struct kern_ipc_perm *isp = &sma->sem_perm;
1943 isp->security = NULL;
1947 * smk_curacc_sem : check if current has access on sem
1948 * @sma : the object
1949 * @access : access requested
1951 * Returns 0 if current has the requested access, error code otherwise
1953 static int smk_curacc_sem(struct sem_array *sma, int access)
1955 char *ssp = smack_of_sem(sma);
1956 struct smk_audit_info ad;
1958 #ifdef CONFIG_AUDIT
1959 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1960 ad.a.u.ipc_id = sma->sem_perm.id;
1961 #endif
1962 return smk_curacc(ssp, access, &ad);
1966 * smack_sem_associate - Smack access check for sem
1967 * @sma: the object
1968 * @semflg: access requested
1970 * Returns 0 if current has the requested access, error code otherwise
1972 static int smack_sem_associate(struct sem_array *sma, int semflg)
1974 int may;
1976 may = smack_flags_to_may(semflg);
1977 return smk_curacc_sem(sma, may);
1981 * smack_sem_shmctl - Smack access check for sem
1982 * @sma: the object
1983 * @cmd: what it wants to do
1985 * Returns 0 if current has the requested access, error code otherwise
1987 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1989 int may;
1991 switch (cmd) {
1992 case GETPID:
1993 case GETNCNT:
1994 case GETZCNT:
1995 case GETVAL:
1996 case GETALL:
1997 case IPC_STAT:
1998 case SEM_STAT:
1999 may = MAY_READ;
2000 break;
2001 case SETVAL:
2002 case SETALL:
2003 case IPC_RMID:
2004 case IPC_SET:
2005 may = MAY_READWRITE;
2006 break;
2007 case IPC_INFO:
2008 case SEM_INFO:
2010 * System level information
2012 return 0;
2013 default:
2014 return -EINVAL;
2017 return smk_curacc_sem(sma, may);
2021 * smack_sem_semop - Smack checks of semaphore operations
2022 * @sma: the object
2023 * @sops: unused
2024 * @nsops: unused
2025 * @alter: unused
2027 * Treated as read and write in all cases.
2029 * Returns 0 if access is allowed, error code otherwise
2031 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2032 unsigned nsops, int alter)
2034 return smk_curacc_sem(sma, MAY_READWRITE);
2038 * smack_msg_alloc_security - Set the security blob for msg
2039 * @msq: the object
2041 * Returns 0
2043 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2045 struct kern_ipc_perm *kisp = &msq->q_perm;
2047 kisp->security = current_security();
2048 return 0;
2052 * smack_msg_free_security - Clear the security blob for msg
2053 * @msq: the object
2055 * Clears the blob pointer
2057 static void smack_msg_queue_free_security(struct msg_queue *msq)
2059 struct kern_ipc_perm *kisp = &msq->q_perm;
2061 kisp->security = NULL;
2065 * smack_of_msq - the smack pointer for the msq
2066 * @msq: the object
2068 * Returns a pointer to the smack value
2070 static char *smack_of_msq(struct msg_queue *msq)
2072 return (char *)msq->q_perm.security;
2076 * smk_curacc_msq : helper to check if current has access on msq
2077 * @msq : the msq
2078 * @access : access requested
2080 * return 0 if current has access, error otherwise
2082 static int smk_curacc_msq(struct msg_queue *msq, int access)
2084 char *msp = smack_of_msq(msq);
2085 struct smk_audit_info ad;
2087 #ifdef CONFIG_AUDIT
2088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2089 ad.a.u.ipc_id = msq->q_perm.id;
2090 #endif
2091 return smk_curacc(msp, access, &ad);
2095 * smack_msg_queue_associate - Smack access check for msg_queue
2096 * @msq: the object
2097 * @msqflg: access requested
2099 * Returns 0 if current has the requested access, error code otherwise
2101 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2103 int may;
2105 may = smack_flags_to_may(msqflg);
2106 return smk_curacc_msq(msq, may);
2110 * smack_msg_queue_msgctl - Smack access check for msg_queue
2111 * @msq: the object
2112 * @cmd: what it wants to do
2114 * Returns 0 if current has the requested access, error code otherwise
2116 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2118 int may;
2120 switch (cmd) {
2121 case IPC_STAT:
2122 case MSG_STAT:
2123 may = MAY_READ;
2124 break;
2125 case IPC_SET:
2126 case IPC_RMID:
2127 may = MAY_READWRITE;
2128 break;
2129 case IPC_INFO:
2130 case MSG_INFO:
2132 * System level information
2134 return 0;
2135 default:
2136 return -EINVAL;
2139 return smk_curacc_msq(msq, may);
2143 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2144 * @msq: the object
2145 * @msg: unused
2146 * @msqflg: access requested
2148 * Returns 0 if current has the requested access, error code otherwise
2150 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2151 int msqflg)
2153 int may;
2155 may = smack_flags_to_may(msqflg);
2156 return smk_curacc_msq(msq, may);
2160 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2161 * @msq: the object
2162 * @msg: unused
2163 * @target: unused
2164 * @type: unused
2165 * @mode: unused
2167 * Returns 0 if current has read and write access, error code otherwise
2169 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2170 struct task_struct *target, long type, int mode)
2172 return smk_curacc_msq(msq, MAY_READWRITE);
2176 * smack_ipc_permission - Smack access for ipc_permission()
2177 * @ipp: the object permissions
2178 * @flag: access requested
2180 * Returns 0 if current has read and write access, error code otherwise
2182 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2184 char *isp = ipp->security;
2185 int may = smack_flags_to_may(flag);
2186 struct smk_audit_info ad;
2188 #ifdef CONFIG_AUDIT
2189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2190 ad.a.u.ipc_id = ipp->id;
2191 #endif
2192 return smk_curacc(isp, may, &ad);
2196 * smack_ipc_getsecid - Extract smack security id
2197 * @ipp: the object permissions
2198 * @secid: where result will be saved
2200 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2202 char *smack = ipp->security;
2204 *secid = smack_to_secid(smack);
2208 * smack_d_instantiate - Make sure the blob is correct on an inode
2209 * @opt_dentry: unused
2210 * @inode: the object
2212 * Set the inode's security blob if it hasn't been done already.
2214 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2216 struct super_block *sbp;
2217 struct superblock_smack *sbsp;
2218 struct inode_smack *isp;
2219 char *csp = current_security();
2220 char *fetched;
2221 char *final;
2222 struct dentry *dp;
2224 if (inode == NULL)
2225 return;
2227 isp = inode->i_security;
2229 mutex_lock(&isp->smk_lock);
2231 * If the inode is already instantiated
2232 * take the quick way out
2234 if (isp->smk_flags & SMK_INODE_INSTANT)
2235 goto unlockandout;
2237 sbp = inode->i_sb;
2238 sbsp = sbp->s_security;
2240 * We're going to use the superblock default label
2241 * if there's no label on the file.
2243 final = sbsp->smk_default;
2246 * If this is the root inode the superblock
2247 * may be in the process of initialization.
2248 * If that is the case use the root value out
2249 * of the superblock.
2251 if (opt_dentry->d_parent == opt_dentry) {
2252 isp->smk_inode = sbsp->smk_root;
2253 isp->smk_flags |= SMK_INODE_INSTANT;
2254 goto unlockandout;
2258 * This is pretty hackish.
2259 * Casey says that we shouldn't have to do
2260 * file system specific code, but it does help
2261 * with keeping it simple.
2263 switch (sbp->s_magic) {
2264 case SMACK_MAGIC:
2266 * Casey says that it's a little embarassing
2267 * that the smack file system doesn't do
2268 * extended attributes.
2270 final = smack_known_star.smk_known;
2271 break;
2272 case PIPEFS_MAGIC:
2274 * Casey says pipes are easy (?)
2276 final = smack_known_star.smk_known;
2277 break;
2278 case DEVPTS_SUPER_MAGIC:
2280 * devpts seems content with the label of the task.
2281 * Programs that change smack have to treat the
2282 * pty with respect.
2284 final = csp;
2285 break;
2286 case SOCKFS_MAGIC:
2288 * Casey says sockets get the smack of the task.
2290 final = csp;
2291 break;
2292 case PROC_SUPER_MAGIC:
2294 * Casey says procfs appears not to care.
2295 * The superblock default suffices.
2297 break;
2298 case TMPFS_MAGIC:
2300 * Device labels should come from the filesystem,
2301 * but watch out, because they're volitile,
2302 * getting recreated on every reboot.
2304 final = smack_known_star.smk_known;
2306 * No break.
2308 * If a smack value has been set we want to use it,
2309 * but since tmpfs isn't giving us the opportunity
2310 * to set mount options simulate setting the
2311 * superblock default.
2313 default:
2315 * This isn't an understood special case.
2316 * Get the value from the xattr.
2318 * No xattr support means, alas, no SMACK label.
2319 * Use the aforeapplied default.
2320 * It would be curious if the label of the task
2321 * does not match that assigned.
2323 if (inode->i_op->getxattr == NULL)
2324 break;
2326 * Get the dentry for xattr.
2328 if (opt_dentry == NULL) {
2329 dp = d_find_alias(inode);
2330 if (dp == NULL)
2331 break;
2332 } else {
2333 dp = dget(opt_dentry);
2334 if (dp == NULL)
2335 break;
2338 fetched = smk_fetch(inode, dp);
2339 if (fetched != NULL)
2340 final = fetched;
2342 dput(dp);
2343 break;
2346 if (final == NULL)
2347 isp->smk_inode = csp;
2348 else
2349 isp->smk_inode = final;
2351 isp->smk_flags |= SMK_INODE_INSTANT;
2353 unlockandout:
2354 mutex_unlock(&isp->smk_lock);
2355 return;
2359 * smack_getprocattr - Smack process attribute access
2360 * @p: the object task
2361 * @name: the name of the attribute in /proc/.../attr
2362 * @value: where to put the result
2364 * Places a copy of the task Smack into value
2366 * Returns the length of the smack label or an error code
2368 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2370 char *cp;
2371 int slen;
2373 if (strcmp(name, "current") != 0)
2374 return -EINVAL;
2376 cp = kstrdup(task_security(p), GFP_KERNEL);
2377 if (cp == NULL)
2378 return -ENOMEM;
2380 slen = strlen(cp);
2381 *value = cp;
2382 return slen;
2386 * smack_setprocattr - Smack process attribute setting
2387 * @p: the object task
2388 * @name: the name of the attribute in /proc/.../attr
2389 * @value: the value to set
2390 * @size: the size of the value
2392 * Sets the Smack value of the task. Only setting self
2393 * is permitted and only with privilege
2395 * Returns the length of the smack label or an error code
2397 static int smack_setprocattr(struct task_struct *p, char *name,
2398 void *value, size_t size)
2400 struct cred *new;
2401 char *newsmack;
2404 * Changing another process' Smack value is too dangerous
2405 * and supports no sane use case.
2407 if (p != current)
2408 return -EPERM;
2410 if (!capable(CAP_MAC_ADMIN))
2411 return -EPERM;
2413 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2414 return -EINVAL;
2416 if (strcmp(name, "current") != 0)
2417 return -EINVAL;
2419 newsmack = smk_import(value, size);
2420 if (newsmack == NULL)
2421 return -EINVAL;
2424 * No process is ever allowed the web ("@") label.
2426 if (newsmack == smack_known_web.smk_known)
2427 return -EPERM;
2429 new = prepare_creds();
2430 if (new == NULL)
2431 return -ENOMEM;
2432 new->security = newsmack;
2433 commit_creds(new);
2434 return size;
2438 * smack_unix_stream_connect - Smack access on UDS
2439 * @sock: one socket
2440 * @other: the other socket
2441 * @newsk: unused
2443 * Return 0 if a subject with the smack of sock could access
2444 * an object with the smack of other, otherwise an error code
2446 static int smack_unix_stream_connect(struct socket *sock,
2447 struct socket *other, struct sock *newsk)
2449 struct inode *sp = SOCK_INODE(sock);
2450 struct inode *op = SOCK_INODE(other);
2451 struct smk_audit_info ad;
2453 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2454 smk_ad_setfield_u_net_sk(&ad, other->sk);
2455 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2456 MAY_READWRITE, &ad);
2460 * smack_unix_may_send - Smack access on UDS
2461 * @sock: one socket
2462 * @other: the other socket
2464 * Return 0 if a subject with the smack of sock could access
2465 * an object with the smack of other, otherwise an error code
2467 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2469 struct inode *sp = SOCK_INODE(sock);
2470 struct inode *op = SOCK_INODE(other);
2471 struct smk_audit_info ad;
2473 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2474 smk_ad_setfield_u_net_sk(&ad, other->sk);
2475 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
2479 * smack_socket_sendmsg - Smack check based on destination host
2480 * @sock: the socket
2481 * @msg: the message
2482 * @size: the size of the message
2484 * Return 0 if the current subject can write to the destination
2485 * host. This is only a question if the destination is a single
2486 * label host.
2488 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2489 int size)
2491 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2494 * Perfectly reasonable for this to be NULL
2496 if (sip == NULL || sip->sin_family != AF_INET)
2497 return 0;
2499 return smack_netlabel_send(sock->sk, sip);
2504 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2505 * @sap: netlabel secattr
2506 * @sip: where to put the result
2508 * Copies a smack label into sip
2510 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2512 char smack[SMK_LABELLEN];
2513 char *sp;
2514 int pcat;
2516 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2518 * Looks like a CIPSO packet.
2519 * If there are flags but no level netlabel isn't
2520 * behaving the way we expect it to.
2522 * Get the categories, if any
2523 * Without guidance regarding the smack value
2524 * for the packet fall back on the network
2525 * ambient value.
2527 memset(smack, '\0', SMK_LABELLEN);
2528 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2529 for (pcat = -1;;) {
2530 pcat = netlbl_secattr_catmap_walk(
2531 sap->attr.mls.cat, pcat + 1);
2532 if (pcat < 0)
2533 break;
2534 smack_catset_bit(pcat, smack);
2537 * If it is CIPSO using smack direct mapping
2538 * we are already done. WeeHee.
2540 if (sap->attr.mls.lvl == smack_cipso_direct) {
2541 memcpy(sip, smack, SMK_MAXLEN);
2542 return;
2545 * Look it up in the supplied table if it is not
2546 * a direct mapping.
2548 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2549 return;
2551 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2553 * Looks like a fallback, which gives us a secid.
2555 sp = smack_from_secid(sap->attr.secid);
2557 * This has got to be a bug because it is
2558 * impossible to specify a fallback without
2559 * specifying the label, which will ensure
2560 * it has a secid, and the only way to get a
2561 * secid is from a fallback.
2563 BUG_ON(sp == NULL);
2564 strncpy(sip, sp, SMK_MAXLEN);
2565 return;
2568 * Without guidance regarding the smack value
2569 * for the packet fall back on the network
2570 * ambient value.
2572 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2573 return;
2577 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2578 * @sk: socket
2579 * @skb: packet
2581 * Returns 0 if the packet should be delivered, an error code otherwise
2583 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2585 struct netlbl_lsm_secattr secattr;
2586 struct socket_smack *ssp = sk->sk_security;
2587 char smack[SMK_LABELLEN];
2588 char *csp;
2589 int rc;
2590 struct smk_audit_info ad;
2591 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2592 return 0;
2595 * Translate what netlabel gave us.
2597 netlbl_secattr_init(&secattr);
2599 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2600 if (rc == 0) {
2601 smack_from_secattr(&secattr, smack);
2602 csp = smack;
2603 } else
2604 csp = smack_net_ambient;
2606 netlbl_secattr_destroy(&secattr);
2608 #ifdef CONFIG_AUDIT
2609 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2610 ad.a.u.net.family = sk->sk_family;
2611 ad.a.u.net.netif = skb->iif;
2612 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2613 #endif
2615 * Receiving a packet requires that the other end
2616 * be able to write here. Read access is not required.
2617 * This is the simplist possible security model
2618 * for networking.
2620 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2621 if (rc != 0)
2622 netlbl_skbuff_err(skb, rc, 0);
2623 return rc;
2627 * smack_socket_getpeersec_stream - pull in packet label
2628 * @sock: the socket
2629 * @optval: user's destination
2630 * @optlen: size thereof
2631 * @len: max thereof
2633 * returns zero on success, an error code otherwise
2635 static int smack_socket_getpeersec_stream(struct socket *sock,
2636 char __user *optval,
2637 int __user *optlen, unsigned len)
2639 struct socket_smack *ssp;
2640 int slen;
2641 int rc = 0;
2643 ssp = sock->sk->sk_security;
2644 slen = strlen(ssp->smk_packet) + 1;
2646 if (slen > len)
2647 rc = -ERANGE;
2648 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2649 rc = -EFAULT;
2651 if (put_user(slen, optlen) != 0)
2652 rc = -EFAULT;
2654 return rc;
2659 * smack_socket_getpeersec_dgram - pull in packet label
2660 * @sock: the socket
2661 * @skb: packet data
2662 * @secid: pointer to where to put the secid of the packet
2664 * Sets the netlabel socket state on sk from parent
2666 static int smack_socket_getpeersec_dgram(struct socket *sock,
2667 struct sk_buff *skb, u32 *secid)
2670 struct netlbl_lsm_secattr secattr;
2671 struct sock *sk;
2672 char smack[SMK_LABELLEN];
2673 int family = PF_INET;
2674 u32 s;
2675 int rc;
2678 * Only works for families with packets.
2680 if (sock != NULL) {
2681 sk = sock->sk;
2682 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2683 return 0;
2684 family = sk->sk_family;
2687 * Translate what netlabel gave us.
2689 netlbl_secattr_init(&secattr);
2690 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2691 if (rc == 0)
2692 smack_from_secattr(&secattr, smack);
2693 netlbl_secattr_destroy(&secattr);
2696 * Give up if we couldn't get anything
2698 if (rc != 0)
2699 return rc;
2701 s = smack_to_secid(smack);
2702 if (s == 0)
2703 return -EINVAL;
2705 *secid = s;
2706 return 0;
2710 * smack_sock_graft - Initialize a newly created socket with an existing sock
2711 * @sk: child sock
2712 * @parent: parent socket
2714 * Set the smk_{in,out} state of an existing sock based on the process that
2715 * is creating the new socket.
2717 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2719 struct socket_smack *ssp;
2721 if (sk == NULL ||
2722 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
2723 return;
2725 ssp = sk->sk_security;
2726 ssp->smk_in = ssp->smk_out = current_security();
2727 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
2731 * smack_inet_conn_request - Smack access check on connect
2732 * @sk: socket involved
2733 * @skb: packet
2734 * @req: unused
2736 * Returns 0 if a task with the packet label could write to
2737 * the socket, otherwise an error code
2739 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2740 struct request_sock *req)
2742 u16 family = sk->sk_family;
2743 struct socket_smack *ssp = sk->sk_security;
2744 struct netlbl_lsm_secattr secattr;
2745 struct sockaddr_in addr;
2746 struct iphdr *hdr;
2747 char smack[SMK_LABELLEN];
2748 int rc;
2749 struct smk_audit_info ad;
2751 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2752 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2753 family = PF_INET;
2755 netlbl_secattr_init(&secattr);
2756 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2757 if (rc == 0)
2758 smack_from_secattr(&secattr, smack);
2759 else
2760 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2761 netlbl_secattr_destroy(&secattr);
2763 #ifdef CONFIG_AUDIT
2764 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2765 ad.a.u.net.family = family;
2766 ad.a.u.net.netif = skb->iif;
2767 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2768 #endif
2770 * Receiving a packet requires that the other end be able to write
2771 * here. Read access is not required.
2773 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
2774 if (rc != 0)
2775 return rc;
2778 * Save the peer's label in the request_sock so we can later setup
2779 * smk_packet in the child socket so that SO_PEERCRED can report it.
2781 req->peer_secid = smack_to_secid(smack);
2784 * We need to decide if we want to label the incoming connection here
2785 * if we do we only need to label the request_sock and the stack will
2786 * propogate the wire-label to the sock when it is created.
2788 hdr = ip_hdr(skb);
2789 addr.sin_addr.s_addr = hdr->saddr;
2790 rcu_read_lock();
2791 if (smack_host_label(&addr) == NULL) {
2792 rcu_read_unlock();
2793 netlbl_secattr_init(&secattr);
2794 smack_to_secattr(smack, &secattr);
2795 rc = netlbl_req_setattr(req, &secattr);
2796 netlbl_secattr_destroy(&secattr);
2797 } else {
2798 rcu_read_unlock();
2799 netlbl_req_delattr(req);
2802 return rc;
2806 * smack_inet_csk_clone - Copy the connection information to the new socket
2807 * @sk: the new socket
2808 * @req: the connection's request_sock
2810 * Transfer the connection's peer label to the newly created socket.
2812 static void smack_inet_csk_clone(struct sock *sk,
2813 const struct request_sock *req)
2815 struct socket_smack *ssp = sk->sk_security;
2816 char *smack;
2818 if (req->peer_secid != 0) {
2819 smack = smack_from_secid(req->peer_secid);
2820 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2821 } else
2822 ssp->smk_packet[0] = '\0';
2826 * Key management security hooks
2828 * Casey has not tested key support very heavily.
2829 * The permission check is most likely too restrictive.
2830 * If you care about keys please have a look.
2832 #ifdef CONFIG_KEYS
2835 * smack_key_alloc - Set the key security blob
2836 * @key: object
2837 * @cred: the credentials to use
2838 * @flags: unused
2840 * No allocation required
2842 * Returns 0
2844 static int smack_key_alloc(struct key *key, const struct cred *cred,
2845 unsigned long flags)
2847 key->security = cred->security;
2848 return 0;
2852 * smack_key_free - Clear the key security blob
2853 * @key: the object
2855 * Clear the blob pointer
2857 static void smack_key_free(struct key *key)
2859 key->security = NULL;
2863 * smack_key_permission - Smack access on a key
2864 * @key_ref: gets to the object
2865 * @cred: the credentials to use
2866 * @perm: unused
2868 * Return 0 if the task has read and write to the object,
2869 * an error code otherwise
2871 static int smack_key_permission(key_ref_t key_ref,
2872 const struct cred *cred, key_perm_t perm)
2874 struct key *keyp;
2875 struct smk_audit_info ad;
2877 keyp = key_ref_to_ptr(key_ref);
2878 if (keyp == NULL)
2879 return -EINVAL;
2881 * If the key hasn't been initialized give it access so that
2882 * it may do so.
2884 if (keyp->security == NULL)
2885 return 0;
2887 * This should not occur
2889 if (cred->security == NULL)
2890 return -EACCES;
2891 #ifdef CONFIG_AUDIT
2892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2893 ad.a.u.key_struct.key = keyp->serial;
2894 ad.a.u.key_struct.key_desc = keyp->description;
2895 #endif
2896 return smk_access(cred->security, keyp->security,
2897 MAY_READWRITE, &ad);
2899 #endif /* CONFIG_KEYS */
2902 * Smack Audit hooks
2904 * Audit requires a unique representation of each Smack specific
2905 * rule. This unique representation is used to distinguish the
2906 * object to be audited from remaining kernel objects and also
2907 * works as a glue between the audit hooks.
2909 * Since repository entries are added but never deleted, we'll use
2910 * the smack_known label address related to the given audit rule as
2911 * the needed unique representation. This also better fits the smack
2912 * model where nearly everything is a label.
2914 #ifdef CONFIG_AUDIT
2917 * smack_audit_rule_init - Initialize a smack audit rule
2918 * @field: audit rule fields given from user-space (audit.h)
2919 * @op: required testing operator (=, !=, >, <, ...)
2920 * @rulestr: smack label to be audited
2921 * @vrule: pointer to save our own audit rule representation
2923 * Prepare to audit cases where (@field @op @rulestr) is true.
2924 * The label to be audited is created if necessay.
2926 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2928 char **rule = (char **)vrule;
2929 *rule = NULL;
2931 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2932 return -EINVAL;
2934 if (op != Audit_equal && op != Audit_not_equal)
2935 return -EINVAL;
2937 *rule = smk_import(rulestr, 0);
2939 return 0;
2943 * smack_audit_rule_known - Distinguish Smack audit rules
2944 * @krule: rule of interest, in Audit kernel representation format
2946 * This is used to filter Smack rules from remaining Audit ones.
2947 * If it's proved that this rule belongs to us, the
2948 * audit_rule_match hook will be called to do the final judgement.
2950 static int smack_audit_rule_known(struct audit_krule *krule)
2952 struct audit_field *f;
2953 int i;
2955 for (i = 0; i < krule->field_count; i++) {
2956 f = &krule->fields[i];
2958 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2959 return 1;
2962 return 0;
2966 * smack_audit_rule_match - Audit given object ?
2967 * @secid: security id for identifying the object to test
2968 * @field: audit rule flags given from user-space
2969 * @op: required testing operator
2970 * @vrule: smack internal rule presentation
2971 * @actx: audit context associated with the check
2973 * The core Audit hook. It's used to take the decision of
2974 * whether to audit or not to audit a given object.
2976 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2977 struct audit_context *actx)
2979 char *smack;
2980 char *rule = vrule;
2982 if (!rule) {
2983 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2984 "Smack: missing rule\n");
2985 return -ENOENT;
2988 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2989 return 0;
2991 smack = smack_from_secid(secid);
2994 * No need to do string comparisons. If a match occurs,
2995 * both pointers will point to the same smack_known
2996 * label.
2998 if (op == Audit_equal)
2999 return (rule == smack);
3000 if (op == Audit_not_equal)
3001 return (rule != smack);
3003 return 0;
3007 * smack_audit_rule_free - free smack rule representation
3008 * @vrule: rule to be freed.
3010 * No memory was allocated.
3012 static void smack_audit_rule_free(void *vrule)
3014 /* No-op */
3017 #endif /* CONFIG_AUDIT */
3020 * smack_secid_to_secctx - return the smack label for a secid
3021 * @secid: incoming integer
3022 * @secdata: destination
3023 * @seclen: how long it is
3025 * Exists for networking code.
3027 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3029 char *sp = smack_from_secid(secid);
3031 *secdata = sp;
3032 *seclen = strlen(sp);
3033 return 0;
3037 * smack_secctx_to_secid - return the secid for a smack label
3038 * @secdata: smack label
3039 * @seclen: how long result is
3040 * @secid: outgoing integer
3042 * Exists for audit and networking code.
3044 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3046 *secid = smack_to_secid(secdata);
3047 return 0;
3051 * smack_release_secctx - don't do anything.
3052 * @secdata: unused
3053 * @seclen: unused
3055 * Exists to make sure nothing gets done, and properly
3057 static void smack_release_secctx(char *secdata, u32 seclen)
3061 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3063 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3066 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3068 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3071 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3073 int len = 0;
3074 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3076 if (len < 0)
3077 return len;
3078 *ctxlen = len;
3079 return 0;
3082 struct security_operations smack_ops = {
3083 .name = "smack",
3085 .ptrace_access_check = smack_ptrace_access_check,
3086 .ptrace_traceme = smack_ptrace_traceme,
3087 .syslog = smack_syslog,
3089 .sb_alloc_security = smack_sb_alloc_security,
3090 .sb_free_security = smack_sb_free_security,
3091 .sb_copy_data = smack_sb_copy_data,
3092 .sb_kern_mount = smack_sb_kern_mount,
3093 .sb_statfs = smack_sb_statfs,
3094 .sb_mount = smack_sb_mount,
3095 .sb_umount = smack_sb_umount,
3097 .inode_alloc_security = smack_inode_alloc_security,
3098 .inode_free_security = smack_inode_free_security,
3099 .inode_init_security = smack_inode_init_security,
3100 .inode_link = smack_inode_link,
3101 .inode_unlink = smack_inode_unlink,
3102 .inode_rmdir = smack_inode_rmdir,
3103 .inode_rename = smack_inode_rename,
3104 .inode_permission = smack_inode_permission,
3105 .inode_setattr = smack_inode_setattr,
3106 .inode_getattr = smack_inode_getattr,
3107 .inode_setxattr = smack_inode_setxattr,
3108 .inode_post_setxattr = smack_inode_post_setxattr,
3109 .inode_getxattr = smack_inode_getxattr,
3110 .inode_removexattr = smack_inode_removexattr,
3111 .inode_getsecurity = smack_inode_getsecurity,
3112 .inode_setsecurity = smack_inode_setsecurity,
3113 .inode_listsecurity = smack_inode_listsecurity,
3114 .inode_getsecid = smack_inode_getsecid,
3116 .file_permission = smack_file_permission,
3117 .file_alloc_security = smack_file_alloc_security,
3118 .file_free_security = smack_file_free_security,
3119 .file_ioctl = smack_file_ioctl,
3120 .file_lock = smack_file_lock,
3121 .file_fcntl = smack_file_fcntl,
3122 .file_set_fowner = smack_file_set_fowner,
3123 .file_send_sigiotask = smack_file_send_sigiotask,
3124 .file_receive = smack_file_receive,
3126 .cred_alloc_blank = smack_cred_alloc_blank,
3127 .cred_free = smack_cred_free,
3128 .cred_prepare = smack_cred_prepare,
3129 .cred_commit = smack_cred_commit,
3130 .cred_transfer = smack_cred_transfer,
3131 .kernel_act_as = smack_kernel_act_as,
3132 .kernel_create_files_as = smack_kernel_create_files_as,
3133 .task_setpgid = smack_task_setpgid,
3134 .task_getpgid = smack_task_getpgid,
3135 .task_getsid = smack_task_getsid,
3136 .task_getsecid = smack_task_getsecid,
3137 .task_setnice = smack_task_setnice,
3138 .task_setioprio = smack_task_setioprio,
3139 .task_getioprio = smack_task_getioprio,
3140 .task_setscheduler = smack_task_setscheduler,
3141 .task_getscheduler = smack_task_getscheduler,
3142 .task_movememory = smack_task_movememory,
3143 .task_kill = smack_task_kill,
3144 .task_wait = smack_task_wait,
3145 .task_to_inode = smack_task_to_inode,
3147 .ipc_permission = smack_ipc_permission,
3148 .ipc_getsecid = smack_ipc_getsecid,
3150 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3151 .msg_msg_free_security = smack_msg_msg_free_security,
3153 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3154 .msg_queue_free_security = smack_msg_queue_free_security,
3155 .msg_queue_associate = smack_msg_queue_associate,
3156 .msg_queue_msgctl = smack_msg_queue_msgctl,
3157 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3158 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3160 .shm_alloc_security = smack_shm_alloc_security,
3161 .shm_free_security = smack_shm_free_security,
3162 .shm_associate = smack_shm_associate,
3163 .shm_shmctl = smack_shm_shmctl,
3164 .shm_shmat = smack_shm_shmat,
3166 .sem_alloc_security = smack_sem_alloc_security,
3167 .sem_free_security = smack_sem_free_security,
3168 .sem_associate = smack_sem_associate,
3169 .sem_semctl = smack_sem_semctl,
3170 .sem_semop = smack_sem_semop,
3172 .d_instantiate = smack_d_instantiate,
3174 .getprocattr = smack_getprocattr,
3175 .setprocattr = smack_setprocattr,
3177 .unix_stream_connect = smack_unix_stream_connect,
3178 .unix_may_send = smack_unix_may_send,
3180 .socket_post_create = smack_socket_post_create,
3181 .socket_connect = smack_socket_connect,
3182 .socket_sendmsg = smack_socket_sendmsg,
3183 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3184 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3185 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3186 .sk_alloc_security = smack_sk_alloc_security,
3187 .sk_free_security = smack_sk_free_security,
3188 .sock_graft = smack_sock_graft,
3189 .inet_conn_request = smack_inet_conn_request,
3190 .inet_csk_clone = smack_inet_csk_clone,
3192 /* key management security hooks */
3193 #ifdef CONFIG_KEYS
3194 .key_alloc = smack_key_alloc,
3195 .key_free = smack_key_free,
3196 .key_permission = smack_key_permission,
3197 #endif /* CONFIG_KEYS */
3199 /* Audit hooks */
3200 #ifdef CONFIG_AUDIT
3201 .audit_rule_init = smack_audit_rule_init,
3202 .audit_rule_known = smack_audit_rule_known,
3203 .audit_rule_match = smack_audit_rule_match,
3204 .audit_rule_free = smack_audit_rule_free,
3205 #endif /* CONFIG_AUDIT */
3207 .secid_to_secctx = smack_secid_to_secctx,
3208 .secctx_to_secid = smack_secctx_to_secid,
3209 .release_secctx = smack_release_secctx,
3210 .inode_notifysecctx = smack_inode_notifysecctx,
3211 .inode_setsecctx = smack_inode_setsecctx,
3212 .inode_getsecctx = smack_inode_getsecctx,
3216 static __init void init_smack_know_list(void)
3218 list_add(&smack_known_huh.list, &smack_known_list);
3219 list_add(&smack_known_hat.list, &smack_known_list);
3220 list_add(&smack_known_star.list, &smack_known_list);
3221 list_add(&smack_known_floor.list, &smack_known_list);
3222 list_add(&smack_known_invalid.list, &smack_known_list);
3223 list_add(&smack_known_web.list, &smack_known_list);
3227 * smack_init - initialize the smack system
3229 * Returns 0
3231 static __init int smack_init(void)
3233 struct cred *cred;
3235 if (!security_module_enable(&smack_ops))
3236 return 0;
3238 printk(KERN_INFO "Smack: Initializing.\n");
3241 * Set the security state for the initial task.
3243 cred = (struct cred *) current->cred;
3244 cred->security = &smack_known_floor.smk_known;
3246 /* initilize the smack_know_list */
3247 init_smack_know_list();
3249 * Initialize locks
3251 spin_lock_init(&smack_known_huh.smk_cipsolock);
3252 spin_lock_init(&smack_known_hat.smk_cipsolock);
3253 spin_lock_init(&smack_known_star.smk_cipsolock);
3254 spin_lock_init(&smack_known_floor.smk_cipsolock);
3255 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3258 * Register with LSM
3260 if (register_security(&smack_ops))
3261 panic("smack: Unable to register with kernel.\n");
3263 return 0;
3267 * Smack requires early initialization in order to label
3268 * all processes and objects when they are created.
3270 security_initcall(smack_init);