staging: tidspbridge: adjust error return code (bugfix)
[linux-2.6/btrfs-unstable.git] / security / smack / smack_lsm.c
blobb0be893ad44d52bd0f062a1747199330c4d6940d
1 /*
2 * Simplified MAC Kernel (smack) security module
4 * This file contains the smack hook function implementations.
6 * Authors:
7 * Casey Schaufler <casey@schaufler-ca.com>
8 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12 * Paul Moore <paul@paul-moore.com>
13 * Copyright (C) 2010 Nokia Corporation
14 * Copyright (C) 2011 Intel Corporation.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/xattr.h>
22 #include <linux/pagemap.h>
23 #include <linux/mount.h>
24 #include <linux/stat.h>
25 #include <linux/kd.h>
26 #include <asm/ioctls.h>
27 #include <linux/ip.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/dccp.h>
31 #include <linux/slab.h>
32 #include <linux/mutex.h>
33 #include <linux/pipe_fs_i.h>
34 #include <net/cipso_ipv4.h>
35 #include <net/ip.h>
36 #include <net/ipv6.h>
37 #include <linux/audit.h>
38 #include <linux/magic.h>
39 #include <linux/dcache.h>
40 #include <linux/personality.h>
41 #include <linux/msg.h>
42 #include <linux/shm.h>
43 #include <linux/binfmts.h>
44 #include "smack.h"
46 #define task_security(task) (task_cred_xxx((task), security))
48 #define TRANS_TRUE "TRUE"
49 #define TRANS_TRUE_SIZE 4
51 #define SMK_CONNECTING 0
52 #define SMK_RECEIVING 1
53 #define SMK_SENDING 2
55 LIST_HEAD(smk_ipv6_port_list);
57 /**
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
65 static struct smack_known *smk_fetch(const char *name, struct inode *ip,
66 struct dentry *dp)
68 int rc;
69 char *buffer;
70 struct smack_known *skp = NULL;
72 if (ip->i_op->getxattr == NULL)
73 return NULL;
75 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
76 if (buffer == NULL)
77 return NULL;
79 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
80 if (rc > 0)
81 skp = smk_import_entry(buffer, rc);
83 kfree(buffer);
85 return skp;
88 /**
89 * new_inode_smack - allocate an inode security blob
90 * @smack: a pointer to the Smack label to use in the blob
92 * Returns the new blob or NULL if there's no memory available
94 struct inode_smack *new_inode_smack(char *smack)
96 struct inode_smack *isp;
98 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
99 if (isp == NULL)
100 return NULL;
102 isp->smk_inode = smack;
103 isp->smk_flags = 0;
104 mutex_init(&isp->smk_lock);
106 return isp;
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
113 * Returns the new blob or NULL if there's no memory available
115 static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
118 struct task_smack *tsp;
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
121 if (tsp == NULL)
122 return NULL;
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
129 return tsp;
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
137 * Returns 0 on success, -ENOMEM on error
139 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
140 gfp_t gfp)
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
144 int rc = 0;
146 INIT_LIST_HEAD(nhead);
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
150 if (nrp == NULL) {
151 rc = -ENOMEM;
152 break;
154 *nrp = *orp;
155 list_add_rcu(&nrp->list, nhead);
157 return rc;
161 * LSM hooks.
162 * We he, that is fun!
166 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
167 * @ctp: child task pointer
168 * @mode: ptrace attachment mode
170 * Returns 0 if access is OK, an error code otherwise
172 * Do the capability checks, and require read and write.
174 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
176 int rc;
177 struct smk_audit_info ad;
178 struct smack_known *skp;
180 rc = cap_ptrace_access_check(ctp, mode);
181 if (rc != 0)
182 return rc;
184 skp = smk_of_task(task_security(ctp));
185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
186 smk_ad_setfield_u_tsk(&ad, ctp);
188 rc = smk_curacc(skp->smk_known, mode, &ad);
189 return rc;
193 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
194 * @ptp: parent task pointer
196 * Returns 0 if access is OK, an error code otherwise
198 * Do the capability checks, and require read and write.
200 static int smack_ptrace_traceme(struct task_struct *ptp)
202 int rc;
203 struct smk_audit_info ad;
204 struct smack_known *skp;
206 rc = cap_ptrace_traceme(ptp);
207 if (rc != 0)
208 return rc;
210 skp = smk_of_task(task_security(ptp));
211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
212 smk_ad_setfield_u_tsk(&ad, ptp);
214 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
215 return rc;
219 * smack_syslog - Smack approval on syslog
220 * @type: message type
222 * Require that the task has the floor label
224 * Returns 0 on success, error code otherwise.
226 static int smack_syslog(int typefrom_file)
228 int rc = 0;
229 struct smack_known *skp = smk_of_current();
231 if (smack_privileged(CAP_MAC_OVERRIDE))
232 return 0;
234 if (skp != &smack_known_floor)
235 rc = -EACCES;
237 return rc;
242 * Superblock Hooks.
246 * smack_sb_alloc_security - allocate a superblock blob
247 * @sb: the superblock getting the blob
249 * Returns 0 on success or -ENOMEM on error.
251 static int smack_sb_alloc_security(struct super_block *sb)
253 struct superblock_smack *sbsp;
255 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
257 if (sbsp == NULL)
258 return -ENOMEM;
260 sbsp->smk_root = smack_known_floor.smk_known;
261 sbsp->smk_default = smack_known_floor.smk_known;
262 sbsp->smk_floor = smack_known_floor.smk_known;
263 sbsp->smk_hat = smack_known_hat.smk_known;
265 * smk_initialized will be zero from kzalloc.
267 sb->s_security = sbsp;
269 return 0;
273 * smack_sb_free_security - free a superblock blob
274 * @sb: the superblock getting the blob
277 static void smack_sb_free_security(struct super_block *sb)
279 kfree(sb->s_security);
280 sb->s_security = NULL;
284 * smack_sb_copy_data - copy mount options data for processing
285 * @orig: where to start
286 * @smackopts: mount options string
288 * Returns 0 on success or -ENOMEM on error.
290 * Copy the Smack specific mount options out of the mount
291 * options list.
293 static int smack_sb_copy_data(char *orig, char *smackopts)
295 char *cp, *commap, *otheropts, *dp;
297 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
298 if (otheropts == NULL)
299 return -ENOMEM;
301 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
302 if (strstr(cp, SMK_FSDEFAULT) == cp)
303 dp = smackopts;
304 else if (strstr(cp, SMK_FSFLOOR) == cp)
305 dp = smackopts;
306 else if (strstr(cp, SMK_FSHAT) == cp)
307 dp = smackopts;
308 else if (strstr(cp, SMK_FSROOT) == cp)
309 dp = smackopts;
310 else if (strstr(cp, SMK_FSTRANS) == cp)
311 dp = smackopts;
312 else
313 dp = otheropts;
315 commap = strchr(cp, ',');
316 if (commap != NULL)
317 *commap = '\0';
319 if (*dp != '\0')
320 strcat(dp, ",");
321 strcat(dp, cp);
324 strcpy(orig, otheropts);
325 free_page((unsigned long)otheropts);
327 return 0;
331 * smack_sb_kern_mount - Smack specific mount processing
332 * @sb: the file system superblock
333 * @flags: the mount flags
334 * @data: the smack mount options
336 * Returns 0 on success, an error code on failure
338 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
340 struct dentry *root = sb->s_root;
341 struct inode *inode = root->d_inode;
342 struct superblock_smack *sp = sb->s_security;
343 struct inode_smack *isp;
344 char *op;
345 char *commap;
346 char *nsp;
347 int transmute = 0;
349 if (sp->smk_initialized)
350 return 0;
352 sp->smk_initialized = 1;
354 for (op = data; op != NULL; op = commap) {
355 commap = strchr(op, ',');
356 if (commap != NULL)
357 *commap++ = '\0';
359 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
360 op += strlen(SMK_FSHAT);
361 nsp = smk_import(op, 0);
362 if (nsp != NULL)
363 sp->smk_hat = nsp;
364 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
365 op += strlen(SMK_FSFLOOR);
366 nsp = smk_import(op, 0);
367 if (nsp != NULL)
368 sp->smk_floor = nsp;
369 } else if (strncmp(op, SMK_FSDEFAULT,
370 strlen(SMK_FSDEFAULT)) == 0) {
371 op += strlen(SMK_FSDEFAULT);
372 nsp = smk_import(op, 0);
373 if (nsp != NULL)
374 sp->smk_default = nsp;
375 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
376 op += strlen(SMK_FSROOT);
377 nsp = smk_import(op, 0);
378 if (nsp != NULL)
379 sp->smk_root = nsp;
380 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
381 op += strlen(SMK_FSTRANS);
382 nsp = smk_import(op, 0);
383 if (nsp != NULL) {
384 sp->smk_root = nsp;
385 transmute = 1;
391 * Initialize the root inode.
393 isp = inode->i_security;
394 if (inode->i_security == NULL) {
395 inode->i_security = new_inode_smack(sp->smk_root);
396 isp = inode->i_security;
397 } else
398 isp->smk_inode = sp->smk_root;
400 if (transmute)
401 isp->smk_flags |= SMK_INODE_TRANSMUTE;
403 return 0;
407 * smack_sb_statfs - Smack check on statfs
408 * @dentry: identifies the file system in question
410 * Returns 0 if current can read the floor of the filesystem,
411 * and error code otherwise
413 static int smack_sb_statfs(struct dentry *dentry)
415 struct superblock_smack *sbp = dentry->d_sb->s_security;
416 int rc;
417 struct smk_audit_info ad;
419 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
420 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
422 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
423 return rc;
427 * smack_sb_mount - Smack check for mounting
428 * @dev_name: unused
429 * @path: mount point
430 * @type: unused
431 * @flags: unused
432 * @data: unused
434 * Returns 0 if current can write the floor of the filesystem
435 * being mounted on, an error code otherwise.
437 static int smack_sb_mount(const char *dev_name, struct path *path,
438 const char *type, unsigned long flags, void *data)
440 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
441 struct smk_audit_info ad;
443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
444 smk_ad_setfield_u_fs_path(&ad, *path);
446 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
450 * smack_sb_umount - Smack check for unmounting
451 * @mnt: file system to unmount
452 * @flags: unused
454 * Returns 0 if current can write the floor of the filesystem
455 * being unmounted, an error code otherwise.
457 static int smack_sb_umount(struct vfsmount *mnt, int flags)
459 struct superblock_smack *sbp;
460 struct smk_audit_info ad;
461 struct path path;
463 path.dentry = mnt->mnt_root;
464 path.mnt = mnt;
466 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
467 smk_ad_setfield_u_fs_path(&ad, path);
469 sbp = path.dentry->d_sb->s_security;
470 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
474 * BPRM hooks
478 * smack_bprm_set_creds - set creds for exec
479 * @bprm: the exec information
481 * Returns 0 if it gets a blob, -ENOMEM otherwise
483 static int smack_bprm_set_creds(struct linux_binprm *bprm)
485 struct inode *inode = file_inode(bprm->file);
486 struct task_smack *bsp = bprm->cred->security;
487 struct inode_smack *isp;
488 int rc;
490 rc = cap_bprm_set_creds(bprm);
491 if (rc != 0)
492 return rc;
494 if (bprm->cred_prepared)
495 return 0;
497 isp = inode->i_security;
498 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
499 return 0;
501 if (bprm->unsafe)
502 return -EPERM;
504 bsp->smk_task = isp->smk_task;
505 bprm->per_clear |= PER_CLEAR_ON_SETID;
507 return 0;
511 * smack_bprm_committing_creds - Prepare to install the new credentials
512 * from bprm.
514 * @bprm: binprm for exec
516 static void smack_bprm_committing_creds(struct linux_binprm *bprm)
518 struct task_smack *bsp = bprm->cred->security;
520 if (bsp->smk_task != bsp->smk_forked)
521 current->pdeath_signal = 0;
525 * smack_bprm_secureexec - Return the decision to use secureexec.
526 * @bprm: binprm for exec
528 * Returns 0 on success.
530 static int smack_bprm_secureexec(struct linux_binprm *bprm)
532 struct task_smack *tsp = current_security();
533 int ret = cap_bprm_secureexec(bprm);
535 if (!ret && (tsp->smk_task != tsp->smk_forked))
536 ret = 1;
538 return ret;
542 * Inode hooks
546 * smack_inode_alloc_security - allocate an inode blob
547 * @inode: the inode in need of a blob
549 * Returns 0 if it gets a blob, -ENOMEM otherwise
551 static int smack_inode_alloc_security(struct inode *inode)
553 struct smack_known *skp = smk_of_current();
555 inode->i_security = new_inode_smack(skp->smk_known);
556 if (inode->i_security == NULL)
557 return -ENOMEM;
558 return 0;
562 * smack_inode_free_security - free an inode blob
563 * @inode: the inode with a blob
565 * Clears the blob pointer in inode
567 static void smack_inode_free_security(struct inode *inode)
569 kfree(inode->i_security);
570 inode->i_security = NULL;
574 * smack_inode_init_security - copy out the smack from an inode
575 * @inode: the inode
576 * @dir: unused
577 * @qstr: unused
578 * @name: where to put the attribute name
579 * @value: where to put the attribute value
580 * @len: where to put the length of the attribute
582 * Returns 0 if it all works out, -ENOMEM if there's no memory
584 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
585 const struct qstr *qstr, const char **name,
586 void **value, size_t *len)
588 struct inode_smack *issp = inode->i_security;
589 struct smack_known *skp = smk_of_current();
590 char *isp = smk_of_inode(inode);
591 char *dsp = smk_of_inode(dir);
592 int may;
594 if (name)
595 *name = XATTR_SMACK_SUFFIX;
597 if (value) {
598 rcu_read_lock();
599 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
600 rcu_read_unlock();
603 * If the access rule allows transmutation and
604 * the directory requests transmutation then
605 * by all means transmute.
606 * Mark the inode as changed.
608 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
609 smk_inode_transmutable(dir)) {
610 isp = dsp;
611 issp->smk_flags |= SMK_INODE_CHANGED;
614 *value = kstrdup(isp, GFP_NOFS);
615 if (*value == NULL)
616 return -ENOMEM;
619 if (len)
620 *len = strlen(isp) + 1;
622 return 0;
626 * smack_inode_link - Smack check on link
627 * @old_dentry: the existing object
628 * @dir: unused
629 * @new_dentry: the new object
631 * Returns 0 if access is permitted, an error code otherwise
633 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
634 struct dentry *new_dentry)
636 char *isp;
637 struct smk_audit_info ad;
638 int rc;
640 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
641 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
643 isp = smk_of_inode(old_dentry->d_inode);
644 rc = smk_curacc(isp, MAY_WRITE, &ad);
646 if (rc == 0 && new_dentry->d_inode != NULL) {
647 isp = smk_of_inode(new_dentry->d_inode);
648 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
649 rc = smk_curacc(isp, MAY_WRITE, &ad);
652 return rc;
656 * smack_inode_unlink - Smack check on inode deletion
657 * @dir: containing directory object
658 * @dentry: file to unlink
660 * Returns 0 if current can write the containing directory
661 * and the object, error code otherwise
663 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
665 struct inode *ip = dentry->d_inode;
666 struct smk_audit_info ad;
667 int rc;
669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
670 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
673 * You need write access to the thing you're unlinking
675 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
676 if (rc == 0) {
678 * You also need write access to the containing directory
680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
681 smk_ad_setfield_u_fs_inode(&ad, dir);
682 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
684 return rc;
688 * smack_inode_rmdir - Smack check on directory deletion
689 * @dir: containing directory object
690 * @dentry: directory to unlink
692 * Returns 0 if current can write the containing directory
693 * and the directory, error code otherwise
695 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
697 struct smk_audit_info ad;
698 int rc;
700 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
701 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
704 * You need write access to the thing you're removing
706 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
707 if (rc == 0) {
709 * You also need write access to the containing directory
711 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
712 smk_ad_setfield_u_fs_inode(&ad, dir);
713 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
716 return rc;
720 * smack_inode_rename - Smack check on rename
721 * @old_inode: the old directory
722 * @old_dentry: unused
723 * @new_inode: the new directory
724 * @new_dentry: unused
726 * Read and write access is required on both the old and
727 * new directories.
729 * Returns 0 if access is permitted, an error code otherwise
731 static int smack_inode_rename(struct inode *old_inode,
732 struct dentry *old_dentry,
733 struct inode *new_inode,
734 struct dentry *new_dentry)
736 int rc;
737 char *isp;
738 struct smk_audit_info ad;
740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
741 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
743 isp = smk_of_inode(old_dentry->d_inode);
744 rc = smk_curacc(isp, MAY_READWRITE, &ad);
746 if (rc == 0 && new_dentry->d_inode != NULL) {
747 isp = smk_of_inode(new_dentry->d_inode);
748 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
749 rc = smk_curacc(isp, MAY_READWRITE, &ad);
751 return rc;
755 * smack_inode_permission - Smack version of permission()
756 * @inode: the inode in question
757 * @mask: the access requested
759 * This is the important Smack hook.
761 * Returns 0 if access is permitted, -EACCES otherwise
763 static int smack_inode_permission(struct inode *inode, int mask)
765 struct smk_audit_info ad;
766 int no_block = mask & MAY_NOT_BLOCK;
768 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
770 * No permission to check. Existence test. Yup, it's there.
772 if (mask == 0)
773 return 0;
775 /* May be droppable after audit */
776 if (no_block)
777 return -ECHILD;
778 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
779 smk_ad_setfield_u_fs_inode(&ad, inode);
780 return smk_curacc(smk_of_inode(inode), mask, &ad);
784 * smack_inode_setattr - Smack check for setting attributes
785 * @dentry: the object
786 * @iattr: for the force flag
788 * Returns 0 if access is permitted, an error code otherwise
790 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
792 struct smk_audit_info ad;
794 * Need to allow for clearing the setuid bit.
796 if (iattr->ia_valid & ATTR_FORCE)
797 return 0;
798 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
799 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
801 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
805 * smack_inode_getattr - Smack check for getting attributes
806 * @mnt: unused
807 * @dentry: the object
809 * Returns 0 if access is permitted, an error code otherwise
811 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
813 struct smk_audit_info ad;
814 struct path path;
816 path.dentry = dentry;
817 path.mnt = mnt;
819 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
820 smk_ad_setfield_u_fs_path(&ad, path);
821 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
825 * smack_inode_setxattr - Smack check for setting xattrs
826 * @dentry: the object
827 * @name: name of the attribute
828 * @value: unused
829 * @size: unused
830 * @flags: unused
832 * This protects the Smack attribute explicitly.
834 * Returns 0 if access is permitted, an error code otherwise
836 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
837 const void *value, size_t size, int flags)
839 struct smk_audit_info ad;
840 int rc = 0;
842 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
843 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
844 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
845 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
847 if (!smack_privileged(CAP_MAC_ADMIN))
848 rc = -EPERM;
850 * check label validity here so import wont fail on
851 * post_setxattr
853 if (size == 0 || size >= SMK_LONGLABEL ||
854 smk_import(value, size) == NULL)
855 rc = -EINVAL;
856 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
857 if (!smack_privileged(CAP_MAC_ADMIN))
858 rc = -EPERM;
859 if (size != TRANS_TRUE_SIZE ||
860 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
861 rc = -EINVAL;
862 } else
863 rc = cap_inode_setxattr(dentry, name, value, size, flags);
865 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
866 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
868 if (rc == 0)
869 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
871 return rc;
875 * smack_inode_post_setxattr - Apply the Smack update approved above
876 * @dentry: object
877 * @name: attribute name
878 * @value: attribute value
879 * @size: attribute size
880 * @flags: unused
882 * Set the pointer in the inode blob to the entry found
883 * in the master label list.
885 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
886 const void *value, size_t size, int flags)
888 struct smack_known *skp;
889 struct inode_smack *isp = dentry->d_inode->i_security;
891 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
892 isp->smk_flags |= SMK_INODE_TRANSMUTE;
893 return;
896 skp = smk_import_entry(value, size);
897 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
898 if (skp != NULL)
899 isp->smk_inode = skp->smk_known;
900 else
901 isp->smk_inode = smack_known_invalid.smk_known;
902 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
903 if (skp != NULL)
904 isp->smk_task = skp;
905 else
906 isp->smk_task = &smack_known_invalid;
907 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
908 if (skp != NULL)
909 isp->smk_mmap = skp;
910 else
911 isp->smk_mmap = &smack_known_invalid;
914 return;
918 * smack_inode_getxattr - Smack check on getxattr
919 * @dentry: the object
920 * @name: unused
922 * Returns 0 if access is permitted, an error code otherwise
924 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
926 struct smk_audit_info ad;
928 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
929 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
931 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
935 * smack_inode_removexattr - Smack check on removexattr
936 * @dentry: the object
937 * @name: name of the attribute
939 * Removing the Smack attribute requires CAP_MAC_ADMIN
941 * Returns 0 if access is permitted, an error code otherwise
943 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
945 struct inode_smack *isp;
946 struct smk_audit_info ad;
947 int rc = 0;
949 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
950 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
951 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
952 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
953 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
954 strcmp(name, XATTR_NAME_SMACKMMAP)) {
955 if (!smack_privileged(CAP_MAC_ADMIN))
956 rc = -EPERM;
957 } else
958 rc = cap_inode_removexattr(dentry, name);
960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
961 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
962 if (rc == 0)
963 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
965 if (rc == 0) {
966 isp = dentry->d_inode->i_security;
967 isp->smk_task = NULL;
968 isp->smk_mmap = NULL;
971 return rc;
975 * smack_inode_getsecurity - get smack xattrs
976 * @inode: the object
977 * @name: attribute name
978 * @buffer: where to put the result
979 * @alloc: unused
981 * Returns the size of the attribute or an error code
983 static int smack_inode_getsecurity(const struct inode *inode,
984 const char *name, void **buffer,
985 bool alloc)
987 struct socket_smack *ssp;
988 struct socket *sock;
989 struct super_block *sbp;
990 struct inode *ip = (struct inode *)inode;
991 char *isp;
992 int ilen;
993 int rc = 0;
995 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
996 isp = smk_of_inode(inode);
997 ilen = strlen(isp) + 1;
998 *buffer = isp;
999 return ilen;
1003 * The rest of the Smack xattrs are only on sockets.
1005 sbp = ip->i_sb;
1006 if (sbp->s_magic != SOCKFS_MAGIC)
1007 return -EOPNOTSUPP;
1009 sock = SOCKET_I(ip);
1010 if (sock == NULL || sock->sk == NULL)
1011 return -EOPNOTSUPP;
1013 ssp = sock->sk->sk_security;
1015 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1016 isp = ssp->smk_in;
1017 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1018 isp = ssp->smk_out->smk_known;
1019 else
1020 return -EOPNOTSUPP;
1022 ilen = strlen(isp) + 1;
1023 if (rc == 0) {
1024 *buffer = isp;
1025 rc = ilen;
1028 return rc;
1033 * smack_inode_listsecurity - list the Smack attributes
1034 * @inode: the object
1035 * @buffer: where they go
1036 * @buffer_size: size of buffer
1038 * Returns 0 on success, -EINVAL otherwise
1040 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1041 size_t buffer_size)
1043 int len = strlen(XATTR_NAME_SMACK);
1045 if (buffer != NULL && len <= buffer_size) {
1046 memcpy(buffer, XATTR_NAME_SMACK, len);
1047 return len;
1049 return -EINVAL;
1053 * smack_inode_getsecid - Extract inode's security id
1054 * @inode: inode to extract the info from
1055 * @secid: where result will be saved
1057 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1059 struct inode_smack *isp = inode->i_security;
1061 *secid = smack_to_secid(isp->smk_inode);
1065 * File Hooks
1069 * smack_file_permission - Smack check on file operations
1070 * @file: unused
1071 * @mask: unused
1073 * Returns 0
1075 * Should access checks be done on each read or write?
1076 * UNICOS and SELinux say yes.
1077 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1079 * I'll say no for now. Smack does not do the frequent
1080 * label changing that SELinux does.
1082 static int smack_file_permission(struct file *file, int mask)
1084 return 0;
1088 * smack_file_alloc_security - assign a file security blob
1089 * @file: the object
1091 * The security blob for a file is a pointer to the master
1092 * label list, so no allocation is done.
1094 * Returns 0
1096 static int smack_file_alloc_security(struct file *file)
1098 struct smack_known *skp = smk_of_current();
1100 file->f_security = skp->smk_known;
1101 return 0;
1105 * smack_file_free_security - clear a file security blob
1106 * @file: the object
1108 * The security blob for a file is a pointer to the master
1109 * label list, so no memory is freed.
1111 static void smack_file_free_security(struct file *file)
1113 file->f_security = NULL;
1117 * smack_file_ioctl - Smack check on ioctls
1118 * @file: the object
1119 * @cmd: what to do
1120 * @arg: unused
1122 * Relies heavily on the correct use of the ioctl command conventions.
1124 * Returns 0 if allowed, error code otherwise
1126 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1127 unsigned long arg)
1129 int rc = 0;
1130 struct smk_audit_info ad;
1132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1133 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1135 if (_IOC_DIR(cmd) & _IOC_WRITE)
1136 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1138 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1139 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1141 return rc;
1145 * smack_file_lock - Smack check on file locking
1146 * @file: the object
1147 * @cmd: unused
1149 * Returns 0 if current has lock access, error code otherwise
1151 static int smack_file_lock(struct file *file, unsigned int cmd)
1153 struct smk_audit_info ad;
1155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1156 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1157 return smk_curacc(file->f_security, MAY_LOCK, &ad);
1161 * smack_file_fcntl - Smack check on fcntl
1162 * @file: the object
1163 * @cmd: what action to check
1164 * @arg: unused
1166 * Generally these operations are harmless.
1167 * File locking operations present an obvious mechanism
1168 * for passing information, so they require write access.
1170 * Returns 0 if current has access, error code otherwise
1172 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1173 unsigned long arg)
1175 struct smk_audit_info ad;
1176 int rc = 0;
1179 switch (cmd) {
1180 case F_GETLK:
1181 break;
1182 case F_SETLK:
1183 case F_SETLKW:
1184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1185 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1186 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1187 break;
1188 case F_SETOWN:
1189 case F_SETSIG:
1190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1191 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1192 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1193 break;
1194 default:
1195 break;
1198 return rc;
1202 * smack_mmap_file :
1203 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1204 * if mapping anonymous memory.
1205 * @file contains the file structure for file to map (may be NULL).
1206 * @reqprot contains the protection requested by the application.
1207 * @prot contains the protection that will be applied by the kernel.
1208 * @flags contains the operational flags.
1209 * Return 0 if permission is granted.
1211 static int smack_mmap_file(struct file *file,
1212 unsigned long reqprot, unsigned long prot,
1213 unsigned long flags)
1215 struct smack_known *skp;
1216 struct smack_known *mkp;
1217 struct smack_rule *srp;
1218 struct task_smack *tsp;
1219 char *osmack;
1220 struct inode_smack *isp;
1221 int may;
1222 int mmay;
1223 int tmay;
1224 int rc;
1226 if (file == NULL)
1227 return 0;
1229 isp = file_inode(file)->i_security;
1230 if (isp->smk_mmap == NULL)
1231 return 0;
1232 mkp = isp->smk_mmap;
1234 tsp = current_security();
1235 skp = smk_of_current();
1236 rc = 0;
1238 rcu_read_lock();
1240 * For each Smack rule associated with the subject
1241 * label verify that the SMACK64MMAP also has access
1242 * to that rule's object label.
1244 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1245 osmack = srp->smk_object;
1247 * Matching labels always allows access.
1249 if (mkp->smk_known == osmack)
1250 continue;
1252 * If there is a matching local rule take
1253 * that into account as well.
1255 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
1256 &tsp->smk_rules);
1257 if (may == -ENOENT)
1258 may = srp->smk_access;
1259 else
1260 may &= srp->smk_access;
1262 * If may is zero the SMACK64MMAP subject can't
1263 * possibly have less access.
1265 if (may == 0)
1266 continue;
1269 * Fetch the global list entry.
1270 * If there isn't one a SMACK64MMAP subject
1271 * can't have as much access as current.
1273 mmay = smk_access_entry(mkp->smk_known, osmack,
1274 &mkp->smk_rules);
1275 if (mmay == -ENOENT) {
1276 rc = -EACCES;
1277 break;
1280 * If there is a local entry it modifies the
1281 * potential access, too.
1283 tmay = smk_access_entry(mkp->smk_known, osmack,
1284 &tsp->smk_rules);
1285 if (tmay != -ENOENT)
1286 mmay &= tmay;
1289 * If there is any access available to current that is
1290 * not available to a SMACK64MMAP subject
1291 * deny access.
1293 if ((may | mmay) != mmay) {
1294 rc = -EACCES;
1295 break;
1299 rcu_read_unlock();
1301 return rc;
1305 * smack_file_set_fowner - set the file security blob value
1306 * @file: object in question
1308 * Returns 0
1309 * Further research may be required on this one.
1311 static int smack_file_set_fowner(struct file *file)
1313 struct smack_known *skp = smk_of_current();
1315 file->f_security = skp->smk_known;
1316 return 0;
1320 * smack_file_send_sigiotask - Smack on sigio
1321 * @tsk: The target task
1322 * @fown: the object the signal come from
1323 * @signum: unused
1325 * Allow a privileged task to get signals even if it shouldn't
1327 * Returns 0 if a subject with the object's smack could
1328 * write to the task, an error code otherwise.
1330 static int smack_file_send_sigiotask(struct task_struct *tsk,
1331 struct fown_struct *fown, int signum)
1333 struct smack_known *skp;
1334 struct smack_known *tkp = smk_of_task(tsk->cred->security);
1335 struct file *file;
1336 int rc;
1337 struct smk_audit_info ad;
1340 * struct fown_struct is never outside the context of a struct file
1342 file = container_of(fown, struct file, f_owner);
1344 /* we don't log here as rc can be overriden */
1345 skp = smk_find_entry(file->f_security);
1346 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
1347 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1348 rc = 0;
1350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1351 smk_ad_setfield_u_tsk(&ad, tsk);
1352 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
1353 return rc;
1357 * smack_file_receive - Smack file receive check
1358 * @file: the object
1360 * Returns 0 if current has access, error code otherwise
1362 static int smack_file_receive(struct file *file)
1364 int may = 0;
1365 struct smk_audit_info ad;
1367 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1368 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1370 * This code relies on bitmasks.
1372 if (file->f_mode & FMODE_READ)
1373 may = MAY_READ;
1374 if (file->f_mode & FMODE_WRITE)
1375 may |= MAY_WRITE;
1377 return smk_curacc(file->f_security, may, &ad);
1381 * smack_file_open - Smack dentry open processing
1382 * @file: the object
1383 * @cred: unused
1385 * Set the security blob in the file structure.
1387 * Returns 0
1389 static int smack_file_open(struct file *file, const struct cred *cred)
1391 struct inode_smack *isp = file_inode(file)->i_security;
1393 file->f_security = isp->smk_inode;
1395 return 0;
1399 * Task hooks
1403 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1404 * @new: the new credentials
1405 * @gfp: the atomicity of any memory allocations
1407 * Prepare a blank set of credentials for modification. This must allocate all
1408 * the memory the LSM module might require such that cred_transfer() can
1409 * complete without error.
1411 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1413 struct task_smack *tsp;
1415 tsp = new_task_smack(NULL, NULL, gfp);
1416 if (tsp == NULL)
1417 return -ENOMEM;
1419 cred->security = tsp;
1421 return 0;
1426 * smack_cred_free - "free" task-level security credentials
1427 * @cred: the credentials in question
1430 static void smack_cred_free(struct cred *cred)
1432 struct task_smack *tsp = cred->security;
1433 struct smack_rule *rp;
1434 struct list_head *l;
1435 struct list_head *n;
1437 if (tsp == NULL)
1438 return;
1439 cred->security = NULL;
1441 list_for_each_safe(l, n, &tsp->smk_rules) {
1442 rp = list_entry(l, struct smack_rule, list);
1443 list_del(&rp->list);
1444 kfree(rp);
1446 kfree(tsp);
1450 * smack_cred_prepare - prepare new set of credentials for modification
1451 * @new: the new credentials
1452 * @old: the original credentials
1453 * @gfp: the atomicity of any memory allocations
1455 * Prepare a new set of credentials for modification.
1457 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1458 gfp_t gfp)
1460 struct task_smack *old_tsp = old->security;
1461 struct task_smack *new_tsp;
1462 int rc;
1464 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1465 if (new_tsp == NULL)
1466 return -ENOMEM;
1468 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1469 if (rc != 0)
1470 return rc;
1472 new->security = new_tsp;
1473 return 0;
1477 * smack_cred_transfer - Transfer the old credentials to the new credentials
1478 * @new: the new credentials
1479 * @old: the original credentials
1481 * Fill in a set of blank credentials from another set of credentials.
1483 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1485 struct task_smack *old_tsp = old->security;
1486 struct task_smack *new_tsp = new->security;
1488 new_tsp->smk_task = old_tsp->smk_task;
1489 new_tsp->smk_forked = old_tsp->smk_task;
1490 mutex_init(&new_tsp->smk_rules_lock);
1491 INIT_LIST_HEAD(&new_tsp->smk_rules);
1494 /* cbs copy rule list */
1498 * smack_kernel_act_as - Set the subjective context in a set of credentials
1499 * @new: points to the set of credentials to be modified.
1500 * @secid: specifies the security ID to be set
1502 * Set the security data for a kernel service.
1504 static int smack_kernel_act_as(struct cred *new, u32 secid)
1506 struct task_smack *new_tsp = new->security;
1507 struct smack_known *skp = smack_from_secid(secid);
1509 if (skp == NULL)
1510 return -EINVAL;
1512 new_tsp->smk_task = skp;
1513 return 0;
1517 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1518 * @new: points to the set of credentials to be modified
1519 * @inode: points to the inode to use as a reference
1521 * Set the file creation context in a set of credentials to the same
1522 * as the objective context of the specified inode
1524 static int smack_kernel_create_files_as(struct cred *new,
1525 struct inode *inode)
1527 struct inode_smack *isp = inode->i_security;
1528 struct task_smack *tsp = new->security;
1530 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1531 tsp->smk_task = tsp->smk_forked;
1532 return 0;
1536 * smk_curacc_on_task - helper to log task related access
1537 * @p: the task object
1538 * @access: the access requested
1539 * @caller: name of the calling function for audit
1541 * Return 0 if access is permitted
1543 static int smk_curacc_on_task(struct task_struct *p, int access,
1544 const char *caller)
1546 struct smk_audit_info ad;
1547 struct smack_known *skp = smk_of_task(task_security(p));
1549 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
1550 smk_ad_setfield_u_tsk(&ad, p);
1551 return smk_curacc(skp->smk_known, access, &ad);
1555 * smack_task_setpgid - Smack check on setting pgid
1556 * @p: the task object
1557 * @pgid: unused
1559 * Return 0 if write access is permitted
1561 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1563 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1567 * smack_task_getpgid - Smack access check for getpgid
1568 * @p: the object task
1570 * Returns 0 if current can read the object task, error code otherwise
1572 static int smack_task_getpgid(struct task_struct *p)
1574 return smk_curacc_on_task(p, MAY_READ, __func__);
1578 * smack_task_getsid - Smack access check for getsid
1579 * @p: the object task
1581 * Returns 0 if current can read the object task, error code otherwise
1583 static int smack_task_getsid(struct task_struct *p)
1585 return smk_curacc_on_task(p, MAY_READ, __func__);
1589 * smack_task_getsecid - get the secid of the task
1590 * @p: the object task
1591 * @secid: where to put the result
1593 * Sets the secid to contain a u32 version of the smack label.
1595 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1597 struct smack_known *skp = smk_of_task(task_security(p));
1599 *secid = skp->smk_secid;
1603 * smack_task_setnice - Smack check on setting nice
1604 * @p: the task object
1605 * @nice: unused
1607 * Return 0 if write access is permitted
1609 static int smack_task_setnice(struct task_struct *p, int nice)
1611 int rc;
1613 rc = cap_task_setnice(p, nice);
1614 if (rc == 0)
1615 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1616 return rc;
1620 * smack_task_setioprio - Smack check on setting ioprio
1621 * @p: the task object
1622 * @ioprio: unused
1624 * Return 0 if write access is permitted
1626 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1628 int rc;
1630 rc = cap_task_setioprio(p, ioprio);
1631 if (rc == 0)
1632 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1633 return rc;
1637 * smack_task_getioprio - Smack check on reading ioprio
1638 * @p: the task object
1640 * Return 0 if read access is permitted
1642 static int smack_task_getioprio(struct task_struct *p)
1644 return smk_curacc_on_task(p, MAY_READ, __func__);
1648 * smack_task_setscheduler - Smack check on setting scheduler
1649 * @p: the task object
1650 * @policy: unused
1651 * @lp: unused
1653 * Return 0 if read access is permitted
1655 static int smack_task_setscheduler(struct task_struct *p)
1657 int rc;
1659 rc = cap_task_setscheduler(p);
1660 if (rc == 0)
1661 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
1662 return rc;
1666 * smack_task_getscheduler - Smack check on reading scheduler
1667 * @p: the task object
1669 * Return 0 if read access is permitted
1671 static int smack_task_getscheduler(struct task_struct *p)
1673 return smk_curacc_on_task(p, MAY_READ, __func__);
1677 * smack_task_movememory - Smack check on moving memory
1678 * @p: the task object
1680 * Return 0 if write access is permitted
1682 static int smack_task_movememory(struct task_struct *p)
1684 return smk_curacc_on_task(p, MAY_WRITE, __func__);
1688 * smack_task_kill - Smack check on signal delivery
1689 * @p: the task object
1690 * @info: unused
1691 * @sig: unused
1692 * @secid: identifies the smack to use in lieu of current's
1694 * Return 0 if write access is permitted
1696 * The secid behavior is an artifact of an SELinux hack
1697 * in the USB code. Someday it may go away.
1699 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1700 int sig, u32 secid)
1702 struct smk_audit_info ad;
1703 struct smack_known *skp;
1704 struct smack_known *tkp = smk_of_task(task_security(p));
1706 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1707 smk_ad_setfield_u_tsk(&ad, p);
1709 * Sending a signal requires that the sender
1710 * can write the receiver.
1712 if (secid == 0)
1713 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1715 * If the secid isn't 0 we're dealing with some USB IO
1716 * specific behavior. This is not clean. For one thing
1717 * we can't take privilege into account.
1719 skp = smack_from_secid(secid);
1720 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
1724 * smack_task_wait - Smack access check for waiting
1725 * @p: task to wait for
1727 * Returns 0
1729 static int smack_task_wait(struct task_struct *p)
1732 * Allow the operation to succeed.
1733 * Zombies are bad.
1734 * In userless environments (e.g. phones) programs
1735 * get marked with SMACK64EXEC and even if the parent
1736 * and child shouldn't be talking the parent still
1737 * may expect to know when the child exits.
1739 return 0;
1743 * smack_task_to_inode - copy task smack into the inode blob
1744 * @p: task to copy from
1745 * @inode: inode to copy to
1747 * Sets the smack pointer in the inode security blob
1749 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1751 struct inode_smack *isp = inode->i_security;
1752 struct smack_known *skp = smk_of_task(task_security(p));
1754 isp->smk_inode = skp->smk_known;
1758 * Socket hooks.
1762 * smack_sk_alloc_security - Allocate a socket blob
1763 * @sk: the socket
1764 * @family: unused
1765 * @gfp_flags: memory allocation flags
1767 * Assign Smack pointers to current
1769 * Returns 0 on success, -ENOMEM is there's no memory
1771 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1773 struct smack_known *skp = smk_of_current();
1774 struct socket_smack *ssp;
1776 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1777 if (ssp == NULL)
1778 return -ENOMEM;
1780 ssp->smk_in = skp->smk_known;
1781 ssp->smk_out = skp;
1782 ssp->smk_packet = NULL;
1784 sk->sk_security = ssp;
1786 return 0;
1790 * smack_sk_free_security - Free a socket blob
1791 * @sk: the socket
1793 * Clears the blob pointer
1795 static void smack_sk_free_security(struct sock *sk)
1797 kfree(sk->sk_security);
1801 * smack_host_label - check host based restrictions
1802 * @sip: the object end
1804 * looks for host based access restrictions
1806 * This version will only be appropriate for really small sets of single label
1807 * hosts. The caller is responsible for ensuring that the RCU read lock is
1808 * taken before calling this function.
1810 * Returns the label of the far end or NULL if it's not special.
1812 static char *smack_host_label(struct sockaddr_in *sip)
1814 struct smk_netlbladdr *snp;
1815 struct in_addr *siap = &sip->sin_addr;
1817 if (siap->s_addr == 0)
1818 return NULL;
1820 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1822 * we break after finding the first match because
1823 * the list is sorted from longest to shortest mask
1824 * so we have found the most specific match
1826 if ((&snp->smk_host.sin_addr)->s_addr ==
1827 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1828 /* we have found the special CIPSO option */
1829 if (snp->smk_label == smack_cipso_option)
1830 return NULL;
1831 return snp->smk_label;
1834 return NULL;
1838 * smack_netlabel - Set the secattr on a socket
1839 * @sk: the socket
1840 * @labeled: socket label scheme
1842 * Convert the outbound smack value (smk_out) to a
1843 * secattr and attach it to the socket.
1845 * Returns 0 on success or an error code
1847 static int smack_netlabel(struct sock *sk, int labeled)
1849 struct smack_known *skp;
1850 struct socket_smack *ssp = sk->sk_security;
1851 int rc = 0;
1854 * Usually the netlabel code will handle changing the
1855 * packet labeling based on the label.
1856 * The case of a single label host is different, because
1857 * a single label host should never get a labeled packet
1858 * even though the label is usually associated with a packet
1859 * label.
1861 local_bh_disable();
1862 bh_lock_sock_nested(sk);
1864 if (ssp->smk_out == smack_net_ambient ||
1865 labeled == SMACK_UNLABELED_SOCKET)
1866 netlbl_sock_delattr(sk);
1867 else {
1868 skp = ssp->smk_out;
1869 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
1872 bh_unlock_sock(sk);
1873 local_bh_enable();
1875 return rc;
1879 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1880 * @sk: the socket
1881 * @sap: the destination address
1883 * Set the correct secattr for the given socket based on the destination
1884 * address and perform any outbound access checks needed.
1886 * Returns 0 on success or an error code.
1889 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1891 struct smack_known *skp;
1892 int rc;
1893 int sk_lbl;
1894 char *hostsp;
1895 struct socket_smack *ssp = sk->sk_security;
1896 struct smk_audit_info ad;
1898 rcu_read_lock();
1899 hostsp = smack_host_label(sap);
1900 if (hostsp != NULL) {
1901 #ifdef CONFIG_AUDIT
1902 struct lsm_network_audit net;
1904 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1905 ad.a.u.net->family = sap->sin_family;
1906 ad.a.u.net->dport = sap->sin_port;
1907 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
1908 #endif
1909 sk_lbl = SMACK_UNLABELED_SOCKET;
1910 skp = ssp->smk_out;
1911 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
1912 } else {
1913 sk_lbl = SMACK_CIPSO_SOCKET;
1914 rc = 0;
1916 rcu_read_unlock();
1917 if (rc != 0)
1918 return rc;
1920 return smack_netlabel(sk, sk_lbl);
1924 * smk_ipv6_port_label - Smack port access table management
1925 * @sock: socket
1926 * @address: address
1928 * Create or update the port list entry
1930 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1932 struct sock *sk = sock->sk;
1933 struct sockaddr_in6 *addr6;
1934 struct socket_smack *ssp = sock->sk->sk_security;
1935 struct smk_port_label *spp;
1936 unsigned short port = 0;
1938 if (address == NULL) {
1940 * This operation is changing the Smack information
1941 * on the bound socket. Take the changes to the port
1942 * as well.
1944 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1945 if (sk != spp->smk_sock)
1946 continue;
1947 spp->smk_in = ssp->smk_in;
1948 spp->smk_out = ssp->smk_out;
1949 return;
1952 * A NULL address is only used for updating existing
1953 * bound entries. If there isn't one, it's OK.
1955 return;
1958 addr6 = (struct sockaddr_in6 *)address;
1959 port = ntohs(addr6->sin6_port);
1961 * This is a special case that is safely ignored.
1963 if (port == 0)
1964 return;
1967 * Look for an existing port list entry.
1968 * This is an indication that a port is getting reused.
1970 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1971 if (spp->smk_port != port)
1972 continue;
1973 spp->smk_port = port;
1974 spp->smk_sock = sk;
1975 spp->smk_in = ssp->smk_in;
1976 spp->smk_out = ssp->smk_out;
1977 return;
1981 * A new port entry is required.
1983 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1984 if (spp == NULL)
1985 return;
1987 spp->smk_port = port;
1988 spp->smk_sock = sk;
1989 spp->smk_in = ssp->smk_in;
1990 spp->smk_out = ssp->smk_out;
1992 list_add(&spp->list, &smk_ipv6_port_list);
1993 return;
1997 * smk_ipv6_port_check - check Smack port access
1998 * @sock: socket
1999 * @address: address
2001 * Create or update the port list entry
2003 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2004 int act)
2006 __be16 *bep;
2007 __be32 *be32p;
2008 struct smk_port_label *spp;
2009 struct socket_smack *ssp = sk->sk_security;
2010 struct smack_known *skp;
2011 unsigned short port = 0;
2012 char *object;
2013 struct smk_audit_info ad;
2014 #ifdef CONFIG_AUDIT
2015 struct lsm_network_audit net;
2016 #endif
2018 if (act == SMK_RECEIVING) {
2019 skp = smack_net_ambient;
2020 object = ssp->smk_in;
2021 } else {
2022 skp = ssp->smk_out;
2023 object = smack_net_ambient->smk_known;
2027 * Get the IP address and port from the address.
2029 port = ntohs(address->sin6_port);
2030 bep = (__be16 *)(&address->sin6_addr);
2031 be32p = (__be32 *)(&address->sin6_addr);
2034 * It's remote, so port lookup does no good.
2036 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2037 goto auditout;
2040 * It's local so the send check has to have passed.
2042 if (act == SMK_RECEIVING) {
2043 skp = &smack_known_web;
2044 goto auditout;
2047 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2048 if (spp->smk_port != port)
2049 continue;
2050 object = spp->smk_in;
2051 if (act == SMK_CONNECTING)
2052 ssp->smk_packet = spp->smk_out->smk_known;
2053 break;
2056 auditout:
2058 #ifdef CONFIG_AUDIT
2059 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2060 ad.a.u.net->family = sk->sk_family;
2061 ad.a.u.net->dport = port;
2062 if (act == SMK_RECEIVING)
2063 ad.a.u.net->v6info.saddr = address->sin6_addr;
2064 else
2065 ad.a.u.net->v6info.daddr = address->sin6_addr;
2066 #endif
2067 return smk_access(skp, object, MAY_WRITE, &ad);
2071 * smack_inode_setsecurity - set smack xattrs
2072 * @inode: the object
2073 * @name: attribute name
2074 * @value: attribute value
2075 * @size: size of the attribute
2076 * @flags: unused
2078 * Sets the named attribute in the appropriate blob
2080 * Returns 0 on success, or an error code
2082 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2083 const void *value, size_t size, int flags)
2085 struct smack_known *skp;
2086 struct inode_smack *nsp = inode->i_security;
2087 struct socket_smack *ssp;
2088 struct socket *sock;
2089 int rc = 0;
2091 if (value == NULL || size > SMK_LONGLABEL || size == 0)
2092 return -EACCES;
2094 skp = smk_import_entry(value, size);
2095 if (skp == NULL)
2096 return -EINVAL;
2098 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2099 nsp->smk_inode = skp->smk_known;
2100 nsp->smk_flags |= SMK_INODE_INSTANT;
2101 return 0;
2104 * The rest of the Smack xattrs are only on sockets.
2106 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2107 return -EOPNOTSUPP;
2109 sock = SOCKET_I(inode);
2110 if (sock == NULL || sock->sk == NULL)
2111 return -EOPNOTSUPP;
2113 ssp = sock->sk->sk_security;
2115 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2116 ssp->smk_in = skp->smk_known;
2117 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2118 ssp->smk_out = skp;
2119 if (sock->sk->sk_family == PF_INET) {
2120 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2121 if (rc != 0)
2122 printk(KERN_WARNING
2123 "Smack: \"%s\" netlbl error %d.\n",
2124 __func__, -rc);
2126 } else
2127 return -EOPNOTSUPP;
2129 if (sock->sk->sk_family == PF_INET6)
2130 smk_ipv6_port_label(sock, NULL);
2132 return 0;
2136 * smack_socket_post_create - finish socket setup
2137 * @sock: the socket
2138 * @family: protocol family
2139 * @type: unused
2140 * @protocol: unused
2141 * @kern: unused
2143 * Sets the netlabel information on the socket
2145 * Returns 0 on success, and error code otherwise
2147 static int smack_socket_post_create(struct socket *sock, int family,
2148 int type, int protocol, int kern)
2150 if (family != PF_INET || sock->sk == NULL)
2151 return 0;
2153 * Set the outbound netlbl.
2155 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2159 * smack_socket_bind - record port binding information.
2160 * @sock: the socket
2161 * @address: the port address
2162 * @addrlen: size of the address
2164 * Records the label bound to a port.
2166 * Returns 0
2168 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2169 int addrlen)
2171 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2172 smk_ipv6_port_label(sock, address);
2174 return 0;
2178 * smack_socket_connect - connect access check
2179 * @sock: the socket
2180 * @sap: the other end
2181 * @addrlen: size of sap
2183 * Verifies that a connection may be possible
2185 * Returns 0 on success, and error code otherwise
2187 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2188 int addrlen)
2190 int rc = 0;
2192 if (sock->sk == NULL)
2193 return 0;
2195 switch (sock->sk->sk_family) {
2196 case PF_INET:
2197 if (addrlen < sizeof(struct sockaddr_in))
2198 return -EINVAL;
2199 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2200 break;
2201 case PF_INET6:
2202 if (addrlen < sizeof(struct sockaddr_in6))
2203 return -EINVAL;
2204 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2205 SMK_CONNECTING);
2206 break;
2208 return rc;
2212 * smack_flags_to_may - convert S_ to MAY_ values
2213 * @flags: the S_ value
2215 * Returns the equivalent MAY_ value
2217 static int smack_flags_to_may(int flags)
2219 int may = 0;
2221 if (flags & S_IRUGO)
2222 may |= MAY_READ;
2223 if (flags & S_IWUGO)
2224 may |= MAY_WRITE;
2225 if (flags & S_IXUGO)
2226 may |= MAY_EXEC;
2228 return may;
2232 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2233 * @msg: the object
2235 * Returns 0
2237 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2239 struct smack_known *skp = smk_of_current();
2241 msg->security = skp->smk_known;
2242 return 0;
2246 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2247 * @msg: the object
2249 * Clears the blob pointer
2251 static void smack_msg_msg_free_security(struct msg_msg *msg)
2253 msg->security = NULL;
2257 * smack_of_shm - the smack pointer for the shm
2258 * @shp: the object
2260 * Returns a pointer to the smack value
2262 static char *smack_of_shm(struct shmid_kernel *shp)
2264 return (char *)shp->shm_perm.security;
2268 * smack_shm_alloc_security - Set the security blob for shm
2269 * @shp: the object
2271 * Returns 0
2273 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2275 struct kern_ipc_perm *isp = &shp->shm_perm;
2276 struct smack_known *skp = smk_of_current();
2278 isp->security = skp->smk_known;
2279 return 0;
2283 * smack_shm_free_security - Clear the security blob for shm
2284 * @shp: the object
2286 * Clears the blob pointer
2288 static void smack_shm_free_security(struct shmid_kernel *shp)
2290 struct kern_ipc_perm *isp = &shp->shm_perm;
2292 isp->security = NULL;
2296 * smk_curacc_shm : check if current has access on shm
2297 * @shp : the object
2298 * @access : access requested
2300 * Returns 0 if current has the requested access, error code otherwise
2302 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2304 char *ssp = smack_of_shm(shp);
2305 struct smk_audit_info ad;
2307 #ifdef CONFIG_AUDIT
2308 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2309 ad.a.u.ipc_id = shp->shm_perm.id;
2310 #endif
2311 return smk_curacc(ssp, access, &ad);
2315 * smack_shm_associate - Smack access check for shm
2316 * @shp: the object
2317 * @shmflg: access requested
2319 * Returns 0 if current has the requested access, error code otherwise
2321 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2323 int may;
2325 may = smack_flags_to_may(shmflg);
2326 return smk_curacc_shm(shp, may);
2330 * smack_shm_shmctl - Smack access check for shm
2331 * @shp: the object
2332 * @cmd: what it wants to do
2334 * Returns 0 if current has the requested access, error code otherwise
2336 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2338 int may;
2340 switch (cmd) {
2341 case IPC_STAT:
2342 case SHM_STAT:
2343 may = MAY_READ;
2344 break;
2345 case IPC_SET:
2346 case SHM_LOCK:
2347 case SHM_UNLOCK:
2348 case IPC_RMID:
2349 may = MAY_READWRITE;
2350 break;
2351 case IPC_INFO:
2352 case SHM_INFO:
2354 * System level information.
2356 return 0;
2357 default:
2358 return -EINVAL;
2360 return smk_curacc_shm(shp, may);
2364 * smack_shm_shmat - Smack access for shmat
2365 * @shp: the object
2366 * @shmaddr: unused
2367 * @shmflg: access requested
2369 * Returns 0 if current has the requested access, error code otherwise
2371 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2372 int shmflg)
2374 int may;
2376 may = smack_flags_to_may(shmflg);
2377 return smk_curacc_shm(shp, may);
2381 * smack_of_sem - the smack pointer for the sem
2382 * @sma: the object
2384 * Returns a pointer to the smack value
2386 static char *smack_of_sem(struct sem_array *sma)
2388 return (char *)sma->sem_perm.security;
2392 * smack_sem_alloc_security - Set the security blob for sem
2393 * @sma: the object
2395 * Returns 0
2397 static int smack_sem_alloc_security(struct sem_array *sma)
2399 struct kern_ipc_perm *isp = &sma->sem_perm;
2400 struct smack_known *skp = smk_of_current();
2402 isp->security = skp->smk_known;
2403 return 0;
2407 * smack_sem_free_security - Clear the security blob for sem
2408 * @sma: the object
2410 * Clears the blob pointer
2412 static void smack_sem_free_security(struct sem_array *sma)
2414 struct kern_ipc_perm *isp = &sma->sem_perm;
2416 isp->security = NULL;
2420 * smk_curacc_sem : check if current has access on sem
2421 * @sma : the object
2422 * @access : access requested
2424 * Returns 0 if current has the requested access, error code otherwise
2426 static int smk_curacc_sem(struct sem_array *sma, int access)
2428 char *ssp = smack_of_sem(sma);
2429 struct smk_audit_info ad;
2431 #ifdef CONFIG_AUDIT
2432 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2433 ad.a.u.ipc_id = sma->sem_perm.id;
2434 #endif
2435 return smk_curacc(ssp, access, &ad);
2439 * smack_sem_associate - Smack access check for sem
2440 * @sma: the object
2441 * @semflg: access requested
2443 * Returns 0 if current has the requested access, error code otherwise
2445 static int smack_sem_associate(struct sem_array *sma, int semflg)
2447 int may;
2449 may = smack_flags_to_may(semflg);
2450 return smk_curacc_sem(sma, may);
2454 * smack_sem_shmctl - Smack access check for sem
2455 * @sma: the object
2456 * @cmd: what it wants to do
2458 * Returns 0 if current has the requested access, error code otherwise
2460 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2462 int may;
2464 switch (cmd) {
2465 case GETPID:
2466 case GETNCNT:
2467 case GETZCNT:
2468 case GETVAL:
2469 case GETALL:
2470 case IPC_STAT:
2471 case SEM_STAT:
2472 may = MAY_READ;
2473 break;
2474 case SETVAL:
2475 case SETALL:
2476 case IPC_RMID:
2477 case IPC_SET:
2478 may = MAY_READWRITE;
2479 break;
2480 case IPC_INFO:
2481 case SEM_INFO:
2483 * System level information
2485 return 0;
2486 default:
2487 return -EINVAL;
2490 return smk_curacc_sem(sma, may);
2494 * smack_sem_semop - Smack checks of semaphore operations
2495 * @sma: the object
2496 * @sops: unused
2497 * @nsops: unused
2498 * @alter: unused
2500 * Treated as read and write in all cases.
2502 * Returns 0 if access is allowed, error code otherwise
2504 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2505 unsigned nsops, int alter)
2507 return smk_curacc_sem(sma, MAY_READWRITE);
2511 * smack_msg_alloc_security - Set the security blob for msg
2512 * @msq: the object
2514 * Returns 0
2516 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2518 struct kern_ipc_perm *kisp = &msq->q_perm;
2519 struct smack_known *skp = smk_of_current();
2521 kisp->security = skp->smk_known;
2522 return 0;
2526 * smack_msg_free_security - Clear the security blob for msg
2527 * @msq: the object
2529 * Clears the blob pointer
2531 static void smack_msg_queue_free_security(struct msg_queue *msq)
2533 struct kern_ipc_perm *kisp = &msq->q_perm;
2535 kisp->security = NULL;
2539 * smack_of_msq - the smack pointer for the msq
2540 * @msq: the object
2542 * Returns a pointer to the smack value
2544 static char *smack_of_msq(struct msg_queue *msq)
2546 return (char *)msq->q_perm.security;
2550 * smk_curacc_msq : helper to check if current has access on msq
2551 * @msq : the msq
2552 * @access : access requested
2554 * return 0 if current has access, error otherwise
2556 static int smk_curacc_msq(struct msg_queue *msq, int access)
2558 char *msp = smack_of_msq(msq);
2559 struct smk_audit_info ad;
2561 #ifdef CONFIG_AUDIT
2562 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2563 ad.a.u.ipc_id = msq->q_perm.id;
2564 #endif
2565 return smk_curacc(msp, access, &ad);
2569 * smack_msg_queue_associate - Smack access check for msg_queue
2570 * @msq: the object
2571 * @msqflg: access requested
2573 * Returns 0 if current has the requested access, error code otherwise
2575 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2577 int may;
2579 may = smack_flags_to_may(msqflg);
2580 return smk_curacc_msq(msq, may);
2584 * smack_msg_queue_msgctl - Smack access check for msg_queue
2585 * @msq: the object
2586 * @cmd: what it wants to do
2588 * Returns 0 if current has the requested access, error code otherwise
2590 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2592 int may;
2594 switch (cmd) {
2595 case IPC_STAT:
2596 case MSG_STAT:
2597 may = MAY_READ;
2598 break;
2599 case IPC_SET:
2600 case IPC_RMID:
2601 may = MAY_READWRITE;
2602 break;
2603 case IPC_INFO:
2604 case MSG_INFO:
2606 * System level information
2608 return 0;
2609 default:
2610 return -EINVAL;
2613 return smk_curacc_msq(msq, may);
2617 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2618 * @msq: the object
2619 * @msg: unused
2620 * @msqflg: access requested
2622 * Returns 0 if current has the requested access, error code otherwise
2624 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2625 int msqflg)
2627 int may;
2629 may = smack_flags_to_may(msqflg);
2630 return smk_curacc_msq(msq, may);
2634 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2635 * @msq: the object
2636 * @msg: unused
2637 * @target: unused
2638 * @type: unused
2639 * @mode: unused
2641 * Returns 0 if current has read and write access, error code otherwise
2643 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2644 struct task_struct *target, long type, int mode)
2646 return smk_curacc_msq(msq, MAY_READWRITE);
2650 * smack_ipc_permission - Smack access for ipc_permission()
2651 * @ipp: the object permissions
2652 * @flag: access requested
2654 * Returns 0 if current has read and write access, error code otherwise
2656 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2658 char *isp = ipp->security;
2659 int may = smack_flags_to_may(flag);
2660 struct smk_audit_info ad;
2662 #ifdef CONFIG_AUDIT
2663 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2664 ad.a.u.ipc_id = ipp->id;
2665 #endif
2666 return smk_curacc(isp, may, &ad);
2670 * smack_ipc_getsecid - Extract smack security id
2671 * @ipp: the object permissions
2672 * @secid: where result will be saved
2674 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2676 char *smack = ipp->security;
2678 *secid = smack_to_secid(smack);
2682 * smack_d_instantiate - Make sure the blob is correct on an inode
2683 * @opt_dentry: dentry where inode will be attached
2684 * @inode: the object
2686 * Set the inode's security blob if it hasn't been done already.
2688 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2690 struct super_block *sbp;
2691 struct superblock_smack *sbsp;
2692 struct inode_smack *isp;
2693 struct smack_known *skp;
2694 struct smack_known *ckp = smk_of_current();
2695 char *final;
2696 char trattr[TRANS_TRUE_SIZE];
2697 int transflag = 0;
2698 int rc;
2699 struct dentry *dp;
2701 if (inode == NULL)
2702 return;
2704 isp = inode->i_security;
2706 mutex_lock(&isp->smk_lock);
2708 * If the inode is already instantiated
2709 * take the quick way out
2711 if (isp->smk_flags & SMK_INODE_INSTANT)
2712 goto unlockandout;
2714 sbp = inode->i_sb;
2715 sbsp = sbp->s_security;
2717 * We're going to use the superblock default label
2718 * if there's no label on the file.
2720 final = sbsp->smk_default;
2723 * If this is the root inode the superblock
2724 * may be in the process of initialization.
2725 * If that is the case use the root value out
2726 * of the superblock.
2728 if (opt_dentry->d_parent == opt_dentry) {
2729 isp->smk_inode = sbsp->smk_root;
2730 isp->smk_flags |= SMK_INODE_INSTANT;
2731 goto unlockandout;
2735 * This is pretty hackish.
2736 * Casey says that we shouldn't have to do
2737 * file system specific code, but it does help
2738 * with keeping it simple.
2740 switch (sbp->s_magic) {
2741 case SMACK_MAGIC:
2743 * Casey says that it's a little embarrassing
2744 * that the smack file system doesn't do
2745 * extended attributes.
2747 final = smack_known_star.smk_known;
2748 break;
2749 case PIPEFS_MAGIC:
2751 * Casey says pipes are easy (?)
2753 final = smack_known_star.smk_known;
2754 break;
2755 case DEVPTS_SUPER_MAGIC:
2757 * devpts seems content with the label of the task.
2758 * Programs that change smack have to treat the
2759 * pty with respect.
2761 final = ckp->smk_known;
2762 break;
2763 case SOCKFS_MAGIC:
2765 * Socket access is controlled by the socket
2766 * structures associated with the task involved.
2768 final = smack_known_star.smk_known;
2769 break;
2770 case PROC_SUPER_MAGIC:
2772 * Casey says procfs appears not to care.
2773 * The superblock default suffices.
2775 break;
2776 case TMPFS_MAGIC:
2778 * Device labels should come from the filesystem,
2779 * but watch out, because they're volitile,
2780 * getting recreated on every reboot.
2782 final = smack_known_star.smk_known;
2784 * No break.
2786 * If a smack value has been set we want to use it,
2787 * but since tmpfs isn't giving us the opportunity
2788 * to set mount options simulate setting the
2789 * superblock default.
2791 default:
2793 * This isn't an understood special case.
2794 * Get the value from the xattr.
2798 * UNIX domain sockets use lower level socket data.
2800 if (S_ISSOCK(inode->i_mode)) {
2801 final = smack_known_star.smk_known;
2802 break;
2805 * No xattr support means, alas, no SMACK label.
2806 * Use the aforeapplied default.
2807 * It would be curious if the label of the task
2808 * does not match that assigned.
2810 if (inode->i_op->getxattr == NULL)
2811 break;
2813 * Get the dentry for xattr.
2815 dp = dget(opt_dentry);
2816 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2817 if (skp != NULL)
2818 final = skp->smk_known;
2821 * Transmuting directory
2823 if (S_ISDIR(inode->i_mode)) {
2825 * If this is a new directory and the label was
2826 * transmuted when the inode was initialized
2827 * set the transmute attribute on the directory
2828 * and mark the inode.
2830 * If there is a transmute attribute on the
2831 * directory mark the inode.
2833 if (isp->smk_flags & SMK_INODE_CHANGED) {
2834 isp->smk_flags &= ~SMK_INODE_CHANGED;
2835 rc = inode->i_op->setxattr(dp,
2836 XATTR_NAME_SMACKTRANSMUTE,
2837 TRANS_TRUE, TRANS_TRUE_SIZE,
2839 } else {
2840 rc = inode->i_op->getxattr(dp,
2841 XATTR_NAME_SMACKTRANSMUTE, trattr,
2842 TRANS_TRUE_SIZE);
2843 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2844 TRANS_TRUE_SIZE) != 0)
2845 rc = -EINVAL;
2847 if (rc >= 0)
2848 transflag = SMK_INODE_TRANSMUTE;
2850 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2851 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2853 dput(dp);
2854 break;
2857 if (final == NULL)
2858 isp->smk_inode = ckp->smk_known;
2859 else
2860 isp->smk_inode = final;
2862 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2864 unlockandout:
2865 mutex_unlock(&isp->smk_lock);
2866 return;
2870 * smack_getprocattr - Smack process attribute access
2871 * @p: the object task
2872 * @name: the name of the attribute in /proc/.../attr
2873 * @value: where to put the result
2875 * Places a copy of the task Smack into value
2877 * Returns the length of the smack label or an error code
2879 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2881 struct smack_known *skp = smk_of_task(task_security(p));
2882 char *cp;
2883 int slen;
2885 if (strcmp(name, "current") != 0)
2886 return -EINVAL;
2888 cp = kstrdup(skp->smk_known, GFP_KERNEL);
2889 if (cp == NULL)
2890 return -ENOMEM;
2892 slen = strlen(cp);
2893 *value = cp;
2894 return slen;
2898 * smack_setprocattr - Smack process attribute setting
2899 * @p: the object task
2900 * @name: the name of the attribute in /proc/.../attr
2901 * @value: the value to set
2902 * @size: the size of the value
2904 * Sets the Smack value of the task. Only setting self
2905 * is permitted and only with privilege
2907 * Returns the length of the smack label or an error code
2909 static int smack_setprocattr(struct task_struct *p, char *name,
2910 void *value, size_t size)
2912 struct task_smack *tsp;
2913 struct cred *new;
2914 struct smack_known *skp;
2917 * Changing another process' Smack value is too dangerous
2918 * and supports no sane use case.
2920 if (p != current)
2921 return -EPERM;
2923 if (!smack_privileged(CAP_MAC_ADMIN))
2924 return -EPERM;
2926 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
2927 return -EINVAL;
2929 if (strcmp(name, "current") != 0)
2930 return -EINVAL;
2932 skp = smk_import_entry(value, size);
2933 if (skp == NULL)
2934 return -EINVAL;
2937 * No process is ever allowed the web ("@") label.
2939 if (skp == &smack_known_web)
2940 return -EPERM;
2942 new = prepare_creds();
2943 if (new == NULL)
2944 return -ENOMEM;
2946 tsp = new->security;
2947 tsp->smk_task = skp;
2949 commit_creds(new);
2950 return size;
2954 * smack_unix_stream_connect - Smack access on UDS
2955 * @sock: one sock
2956 * @other: the other sock
2957 * @newsk: unused
2959 * Return 0 if a subject with the smack of sock could access
2960 * an object with the smack of other, otherwise an error code
2962 static int smack_unix_stream_connect(struct sock *sock,
2963 struct sock *other, struct sock *newsk)
2965 struct smack_known *skp;
2966 struct socket_smack *ssp = sock->sk_security;
2967 struct socket_smack *osp = other->sk_security;
2968 struct socket_smack *nsp = newsk->sk_security;
2969 struct smk_audit_info ad;
2970 int rc = 0;
2972 #ifdef CONFIG_AUDIT
2973 struct lsm_network_audit net;
2975 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2976 smk_ad_setfield_u_net_sk(&ad, other);
2977 #endif
2979 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2980 skp = ssp->smk_out;
2981 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2985 * Cross reference the peer labels for SO_PEERSEC.
2987 if (rc == 0) {
2988 nsp->smk_packet = ssp->smk_out->smk_known;
2989 ssp->smk_packet = osp->smk_out->smk_known;
2992 return rc;
2996 * smack_unix_may_send - Smack access on UDS
2997 * @sock: one socket
2998 * @other: the other socket
3000 * Return 0 if a subject with the smack of sock could access
3001 * an object with the smack of other, otherwise an error code
3003 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3005 struct socket_smack *ssp = sock->sk->sk_security;
3006 struct socket_smack *osp = other->sk->sk_security;
3007 struct smack_known *skp;
3008 struct smk_audit_info ad;
3010 #ifdef CONFIG_AUDIT
3011 struct lsm_network_audit net;
3013 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3014 smk_ad_setfield_u_net_sk(&ad, other->sk);
3015 #endif
3017 if (smack_privileged(CAP_MAC_OVERRIDE))
3018 return 0;
3020 skp = ssp->smk_out;
3021 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3025 * smack_socket_sendmsg - Smack check based on destination host
3026 * @sock: the socket
3027 * @msg: the message
3028 * @size: the size of the message
3030 * Return 0 if the current subject can write to the destination host.
3031 * For IPv4 this is only a question if the destination is a single label host.
3032 * For IPv6 this is a check against the label of the port.
3034 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3035 int size)
3037 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3038 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3039 int rc = 0;
3042 * Perfectly reasonable for this to be NULL
3044 if (sip == NULL)
3045 return 0;
3047 switch (sip->sin_family) {
3048 case AF_INET:
3049 rc = smack_netlabel_send(sock->sk, sip);
3050 break;
3051 case AF_INET6:
3052 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3053 break;
3055 return rc;
3059 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3060 * @sap: netlabel secattr
3061 * @ssp: socket security information
3063 * Returns a pointer to a Smack label entry found on the label list.
3065 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3066 struct socket_smack *ssp)
3068 struct smack_known *skp;
3069 int found = 0;
3070 int acat;
3071 int kcat;
3073 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3075 * Looks like a CIPSO packet.
3076 * If there are flags but no level netlabel isn't
3077 * behaving the way we expect it to.
3079 * Look it up in the label table
3080 * Without guidance regarding the smack value
3081 * for the packet fall back on the network
3082 * ambient value.
3084 rcu_read_lock();
3085 list_for_each_entry(skp, &smack_known_list, list) {
3086 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3087 continue;
3089 * Compare the catsets. Use the netlbl APIs.
3091 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3092 if ((skp->smk_netlabel.flags &
3093 NETLBL_SECATTR_MLS_CAT) == 0)
3094 found = 1;
3095 break;
3097 for (acat = -1, kcat = -1; acat == kcat; ) {
3098 acat = netlbl_secattr_catmap_walk(
3099 sap->attr.mls.cat, acat + 1);
3100 kcat = netlbl_secattr_catmap_walk(
3101 skp->smk_netlabel.attr.mls.cat,
3102 kcat + 1);
3103 if (acat < 0 || kcat < 0)
3104 break;
3106 if (acat == kcat) {
3107 found = 1;
3108 break;
3111 rcu_read_unlock();
3113 if (found)
3114 return skp;
3116 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3117 return &smack_known_web;
3118 return &smack_known_star;
3120 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3122 * Looks like a fallback, which gives us a secid.
3124 skp = smack_from_secid(sap->attr.secid);
3126 * This has got to be a bug because it is
3127 * impossible to specify a fallback without
3128 * specifying the label, which will ensure
3129 * it has a secid, and the only way to get a
3130 * secid is from a fallback.
3132 BUG_ON(skp == NULL);
3133 return skp;
3136 * Without guidance regarding the smack value
3137 * for the packet fall back on the network
3138 * ambient value.
3140 return smack_net_ambient;
3143 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3145 u8 nexthdr;
3146 int offset;
3147 int proto = -EINVAL;
3148 struct ipv6hdr _ipv6h;
3149 struct ipv6hdr *ip6;
3150 __be16 frag_off;
3151 struct tcphdr _tcph, *th;
3152 struct udphdr _udph, *uh;
3153 struct dccp_hdr _dccph, *dh;
3155 sip->sin6_port = 0;
3157 offset = skb_network_offset(skb);
3158 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3159 if (ip6 == NULL)
3160 return -EINVAL;
3161 sip->sin6_addr = ip6->saddr;
3163 nexthdr = ip6->nexthdr;
3164 offset += sizeof(_ipv6h);
3165 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3166 if (offset < 0)
3167 return -EINVAL;
3169 proto = nexthdr;
3170 switch (proto) {
3171 case IPPROTO_TCP:
3172 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3173 if (th != NULL)
3174 sip->sin6_port = th->source;
3175 break;
3176 case IPPROTO_UDP:
3177 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3178 if (uh != NULL)
3179 sip->sin6_port = uh->source;
3180 break;
3181 case IPPROTO_DCCP:
3182 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3183 if (dh != NULL)
3184 sip->sin6_port = dh->dccph_sport;
3185 break;
3187 return proto;
3191 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3192 * @sk: socket
3193 * @skb: packet
3195 * Returns 0 if the packet should be delivered, an error code otherwise
3197 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3199 struct netlbl_lsm_secattr secattr;
3200 struct socket_smack *ssp = sk->sk_security;
3201 struct smack_known *skp;
3202 struct sockaddr_in6 sadd;
3203 int rc = 0;
3204 struct smk_audit_info ad;
3205 #ifdef CONFIG_AUDIT
3206 struct lsm_network_audit net;
3207 #endif
3208 switch (sk->sk_family) {
3209 case PF_INET:
3211 * Translate what netlabel gave us.
3213 netlbl_secattr_init(&secattr);
3215 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3216 if (rc == 0)
3217 skp = smack_from_secattr(&secattr, ssp);
3218 else
3219 skp = smack_net_ambient;
3221 netlbl_secattr_destroy(&secattr);
3223 #ifdef CONFIG_AUDIT
3224 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3225 ad.a.u.net->family = sk->sk_family;
3226 ad.a.u.net->netif = skb->skb_iif;
3227 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3228 #endif
3230 * Receiving a packet requires that the other end
3231 * be able to write here. Read access is not required.
3232 * This is the simplist possible security model
3233 * for networking.
3235 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3236 if (rc != 0)
3237 netlbl_skbuff_err(skb, rc, 0);
3238 break;
3239 case PF_INET6:
3240 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3241 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3242 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3243 else
3244 rc = 0;
3245 break;
3247 return rc;
3251 * smack_socket_getpeersec_stream - pull in packet label
3252 * @sock: the socket
3253 * @optval: user's destination
3254 * @optlen: size thereof
3255 * @len: max thereof
3257 * returns zero on success, an error code otherwise
3259 static int smack_socket_getpeersec_stream(struct socket *sock,
3260 char __user *optval,
3261 int __user *optlen, unsigned len)
3263 struct socket_smack *ssp;
3264 char *rcp = "";
3265 int slen = 1;
3266 int rc = 0;
3268 ssp = sock->sk->sk_security;
3269 if (ssp->smk_packet != NULL) {
3270 rcp = ssp->smk_packet;
3271 slen = strlen(rcp) + 1;
3274 if (slen > len)
3275 rc = -ERANGE;
3276 else if (copy_to_user(optval, rcp, slen) != 0)
3277 rc = -EFAULT;
3279 if (put_user(slen, optlen) != 0)
3280 rc = -EFAULT;
3282 return rc;
3287 * smack_socket_getpeersec_dgram - pull in packet label
3288 * @sock: the peer socket
3289 * @skb: packet data
3290 * @secid: pointer to where to put the secid of the packet
3292 * Sets the netlabel socket state on sk from parent
3294 static int smack_socket_getpeersec_dgram(struct socket *sock,
3295 struct sk_buff *skb, u32 *secid)
3298 struct netlbl_lsm_secattr secattr;
3299 struct socket_smack *ssp = NULL;
3300 struct smack_known *skp;
3301 int family = PF_UNSPEC;
3302 u32 s = 0; /* 0 is the invalid secid */
3303 int rc;
3305 if (skb != NULL) {
3306 if (skb->protocol == htons(ETH_P_IP))
3307 family = PF_INET;
3308 else if (skb->protocol == htons(ETH_P_IPV6))
3309 family = PF_INET6;
3311 if (family == PF_UNSPEC && sock != NULL)
3312 family = sock->sk->sk_family;
3314 if (family == PF_UNIX) {
3315 ssp = sock->sk->sk_security;
3316 s = ssp->smk_out->smk_secid;
3317 } else if (family == PF_INET || family == PF_INET6) {
3319 * Translate what netlabel gave us.
3321 if (sock != NULL && sock->sk != NULL)
3322 ssp = sock->sk->sk_security;
3323 netlbl_secattr_init(&secattr);
3324 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3325 if (rc == 0) {
3326 skp = smack_from_secattr(&secattr, ssp);
3327 s = skp->smk_secid;
3329 netlbl_secattr_destroy(&secattr);
3331 *secid = s;
3332 if (s == 0)
3333 return -EINVAL;
3334 return 0;
3338 * smack_sock_graft - Initialize a newly created socket with an existing sock
3339 * @sk: child sock
3340 * @parent: parent socket
3342 * Set the smk_{in,out} state of an existing sock based on the process that
3343 * is creating the new socket.
3345 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3347 struct socket_smack *ssp;
3348 struct smack_known *skp = smk_of_current();
3350 if (sk == NULL ||
3351 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3352 return;
3354 ssp = sk->sk_security;
3355 ssp->smk_in = skp->smk_known;
3356 ssp->smk_out = skp;
3357 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3361 * smack_inet_conn_request - Smack access check on connect
3362 * @sk: socket involved
3363 * @skb: packet
3364 * @req: unused
3366 * Returns 0 if a task with the packet label could write to
3367 * the socket, otherwise an error code
3369 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3370 struct request_sock *req)
3372 u16 family = sk->sk_family;
3373 struct smack_known *skp;
3374 struct socket_smack *ssp = sk->sk_security;
3375 struct netlbl_lsm_secattr secattr;
3376 struct sockaddr_in addr;
3377 struct iphdr *hdr;
3378 char *hsp;
3379 int rc;
3380 struct smk_audit_info ad;
3381 #ifdef CONFIG_AUDIT
3382 struct lsm_network_audit net;
3383 #endif
3385 if (family == PF_INET6) {
3387 * Handle mapped IPv4 packets arriving
3388 * via IPv6 sockets. Don't set up netlabel
3389 * processing on IPv6.
3391 if (skb->protocol == htons(ETH_P_IP))
3392 family = PF_INET;
3393 else
3394 return 0;
3397 netlbl_secattr_init(&secattr);
3398 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3399 if (rc == 0)
3400 skp = smack_from_secattr(&secattr, ssp);
3401 else
3402 skp = &smack_known_huh;
3403 netlbl_secattr_destroy(&secattr);
3405 #ifdef CONFIG_AUDIT
3406 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3407 ad.a.u.net->family = family;
3408 ad.a.u.net->netif = skb->skb_iif;
3409 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3410 #endif
3412 * Receiving a packet requires that the other end be able to write
3413 * here. Read access is not required.
3415 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3416 if (rc != 0)
3417 return rc;
3420 * Save the peer's label in the request_sock so we can later setup
3421 * smk_packet in the child socket so that SO_PEERCRED can report it.
3423 req->peer_secid = skp->smk_secid;
3426 * We need to decide if we want to label the incoming connection here
3427 * if we do we only need to label the request_sock and the stack will
3428 * propagate the wire-label to the sock when it is created.
3430 hdr = ip_hdr(skb);
3431 addr.sin_addr.s_addr = hdr->saddr;
3432 rcu_read_lock();
3433 hsp = smack_host_label(&addr);
3434 rcu_read_unlock();
3436 if (hsp == NULL)
3437 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3438 else
3439 netlbl_req_delattr(req);
3441 return rc;
3445 * smack_inet_csk_clone - Copy the connection information to the new socket
3446 * @sk: the new socket
3447 * @req: the connection's request_sock
3449 * Transfer the connection's peer label to the newly created socket.
3451 static void smack_inet_csk_clone(struct sock *sk,
3452 const struct request_sock *req)
3454 struct socket_smack *ssp = sk->sk_security;
3455 struct smack_known *skp;
3457 if (req->peer_secid != 0) {
3458 skp = smack_from_secid(req->peer_secid);
3459 ssp->smk_packet = skp->smk_known;
3460 } else
3461 ssp->smk_packet = NULL;
3465 * Key management security hooks
3467 * Casey has not tested key support very heavily.
3468 * The permission check is most likely too restrictive.
3469 * If you care about keys please have a look.
3471 #ifdef CONFIG_KEYS
3474 * smack_key_alloc - Set the key security blob
3475 * @key: object
3476 * @cred: the credentials to use
3477 * @flags: unused
3479 * No allocation required
3481 * Returns 0
3483 static int smack_key_alloc(struct key *key, const struct cred *cred,
3484 unsigned long flags)
3486 struct smack_known *skp = smk_of_task(cred->security);
3488 key->security = skp->smk_known;
3489 return 0;
3493 * smack_key_free - Clear the key security blob
3494 * @key: the object
3496 * Clear the blob pointer
3498 static void smack_key_free(struct key *key)
3500 key->security = NULL;
3504 * smack_key_permission - Smack access on a key
3505 * @key_ref: gets to the object
3506 * @cred: the credentials to use
3507 * @perm: unused
3509 * Return 0 if the task has read and write to the object,
3510 * an error code otherwise
3512 static int smack_key_permission(key_ref_t key_ref,
3513 const struct cred *cred, key_perm_t perm)
3515 struct key *keyp;
3516 struct smk_audit_info ad;
3517 struct smack_known *tkp = smk_of_task(cred->security);
3519 keyp = key_ref_to_ptr(key_ref);
3520 if (keyp == NULL)
3521 return -EINVAL;
3523 * If the key hasn't been initialized give it access so that
3524 * it may do so.
3526 if (keyp->security == NULL)
3527 return 0;
3529 * This should not occur
3531 if (tkp == NULL)
3532 return -EACCES;
3533 #ifdef CONFIG_AUDIT
3534 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3535 ad.a.u.key_struct.key = keyp->serial;
3536 ad.a.u.key_struct.key_desc = keyp->description;
3537 #endif
3538 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
3540 #endif /* CONFIG_KEYS */
3543 * Smack Audit hooks
3545 * Audit requires a unique representation of each Smack specific
3546 * rule. This unique representation is used to distinguish the
3547 * object to be audited from remaining kernel objects and also
3548 * works as a glue between the audit hooks.
3550 * Since repository entries are added but never deleted, we'll use
3551 * the smack_known label address related to the given audit rule as
3552 * the needed unique representation. This also better fits the smack
3553 * model where nearly everything is a label.
3555 #ifdef CONFIG_AUDIT
3558 * smack_audit_rule_init - Initialize a smack audit rule
3559 * @field: audit rule fields given from user-space (audit.h)
3560 * @op: required testing operator (=, !=, >, <, ...)
3561 * @rulestr: smack label to be audited
3562 * @vrule: pointer to save our own audit rule representation
3564 * Prepare to audit cases where (@field @op @rulestr) is true.
3565 * The label to be audited is created if necessay.
3567 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3569 char **rule = (char **)vrule;
3570 *rule = NULL;
3572 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3573 return -EINVAL;
3575 if (op != Audit_equal && op != Audit_not_equal)
3576 return -EINVAL;
3578 *rule = smk_import(rulestr, 0);
3580 return 0;
3584 * smack_audit_rule_known - Distinguish Smack audit rules
3585 * @krule: rule of interest, in Audit kernel representation format
3587 * This is used to filter Smack rules from remaining Audit ones.
3588 * If it's proved that this rule belongs to us, the
3589 * audit_rule_match hook will be called to do the final judgement.
3591 static int smack_audit_rule_known(struct audit_krule *krule)
3593 struct audit_field *f;
3594 int i;
3596 for (i = 0; i < krule->field_count; i++) {
3597 f = &krule->fields[i];
3599 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3600 return 1;
3603 return 0;
3607 * smack_audit_rule_match - Audit given object ?
3608 * @secid: security id for identifying the object to test
3609 * @field: audit rule flags given from user-space
3610 * @op: required testing operator
3611 * @vrule: smack internal rule presentation
3612 * @actx: audit context associated with the check
3614 * The core Audit hook. It's used to take the decision of
3615 * whether to audit or not to audit a given object.
3617 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3618 struct audit_context *actx)
3620 struct smack_known *skp;
3621 char *rule = vrule;
3623 if (!rule) {
3624 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3625 "Smack: missing rule\n");
3626 return -ENOENT;
3629 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3630 return 0;
3632 skp = smack_from_secid(secid);
3635 * No need to do string comparisons. If a match occurs,
3636 * both pointers will point to the same smack_known
3637 * label.
3639 if (op == Audit_equal)
3640 return (rule == skp->smk_known);
3641 if (op == Audit_not_equal)
3642 return (rule != skp->smk_known);
3644 return 0;
3648 * smack_audit_rule_free - free smack rule representation
3649 * @vrule: rule to be freed.
3651 * No memory was allocated.
3653 static void smack_audit_rule_free(void *vrule)
3655 /* No-op */
3658 #endif /* CONFIG_AUDIT */
3661 * smack_ismaclabel - check if xattr @name references a smack MAC label
3662 * @name: Full xattr name to check.
3664 static int smack_ismaclabel(const char *name)
3666 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3671 * smack_secid_to_secctx - return the smack label for a secid
3672 * @secid: incoming integer
3673 * @secdata: destination
3674 * @seclen: how long it is
3676 * Exists for networking code.
3678 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3680 struct smack_known *skp = smack_from_secid(secid);
3682 if (secdata)
3683 *secdata = skp->smk_known;
3684 *seclen = strlen(skp->smk_known);
3685 return 0;
3689 * smack_secctx_to_secid - return the secid for a smack label
3690 * @secdata: smack label
3691 * @seclen: how long result is
3692 * @secid: outgoing integer
3694 * Exists for audit and networking code.
3696 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3698 *secid = smack_to_secid(secdata);
3699 return 0;
3703 * smack_release_secctx - don't do anything.
3704 * @secdata: unused
3705 * @seclen: unused
3707 * Exists to make sure nothing gets done, and properly
3709 static void smack_release_secctx(char *secdata, u32 seclen)
3713 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3715 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3718 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3720 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3723 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3725 int len = 0;
3726 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3728 if (len < 0)
3729 return len;
3730 *ctxlen = len;
3731 return 0;
3734 struct security_operations smack_ops = {
3735 .name = "smack",
3737 .ptrace_access_check = smack_ptrace_access_check,
3738 .ptrace_traceme = smack_ptrace_traceme,
3739 .syslog = smack_syslog,
3741 .sb_alloc_security = smack_sb_alloc_security,
3742 .sb_free_security = smack_sb_free_security,
3743 .sb_copy_data = smack_sb_copy_data,
3744 .sb_kern_mount = smack_sb_kern_mount,
3745 .sb_statfs = smack_sb_statfs,
3746 .sb_mount = smack_sb_mount,
3747 .sb_umount = smack_sb_umount,
3749 .bprm_set_creds = smack_bprm_set_creds,
3750 .bprm_committing_creds = smack_bprm_committing_creds,
3751 .bprm_secureexec = smack_bprm_secureexec,
3753 .inode_alloc_security = smack_inode_alloc_security,
3754 .inode_free_security = smack_inode_free_security,
3755 .inode_init_security = smack_inode_init_security,
3756 .inode_link = smack_inode_link,
3757 .inode_unlink = smack_inode_unlink,
3758 .inode_rmdir = smack_inode_rmdir,
3759 .inode_rename = smack_inode_rename,
3760 .inode_permission = smack_inode_permission,
3761 .inode_setattr = smack_inode_setattr,
3762 .inode_getattr = smack_inode_getattr,
3763 .inode_setxattr = smack_inode_setxattr,
3764 .inode_post_setxattr = smack_inode_post_setxattr,
3765 .inode_getxattr = smack_inode_getxattr,
3766 .inode_removexattr = smack_inode_removexattr,
3767 .inode_getsecurity = smack_inode_getsecurity,
3768 .inode_setsecurity = smack_inode_setsecurity,
3769 .inode_listsecurity = smack_inode_listsecurity,
3770 .inode_getsecid = smack_inode_getsecid,
3772 .file_permission = smack_file_permission,
3773 .file_alloc_security = smack_file_alloc_security,
3774 .file_free_security = smack_file_free_security,
3775 .file_ioctl = smack_file_ioctl,
3776 .file_lock = smack_file_lock,
3777 .file_fcntl = smack_file_fcntl,
3778 .mmap_file = smack_mmap_file,
3779 .mmap_addr = cap_mmap_addr,
3780 .file_set_fowner = smack_file_set_fowner,
3781 .file_send_sigiotask = smack_file_send_sigiotask,
3782 .file_receive = smack_file_receive,
3784 .file_open = smack_file_open,
3786 .cred_alloc_blank = smack_cred_alloc_blank,
3787 .cred_free = smack_cred_free,
3788 .cred_prepare = smack_cred_prepare,
3789 .cred_transfer = smack_cred_transfer,
3790 .kernel_act_as = smack_kernel_act_as,
3791 .kernel_create_files_as = smack_kernel_create_files_as,
3792 .task_setpgid = smack_task_setpgid,
3793 .task_getpgid = smack_task_getpgid,
3794 .task_getsid = smack_task_getsid,
3795 .task_getsecid = smack_task_getsecid,
3796 .task_setnice = smack_task_setnice,
3797 .task_setioprio = smack_task_setioprio,
3798 .task_getioprio = smack_task_getioprio,
3799 .task_setscheduler = smack_task_setscheduler,
3800 .task_getscheduler = smack_task_getscheduler,
3801 .task_movememory = smack_task_movememory,
3802 .task_kill = smack_task_kill,
3803 .task_wait = smack_task_wait,
3804 .task_to_inode = smack_task_to_inode,
3806 .ipc_permission = smack_ipc_permission,
3807 .ipc_getsecid = smack_ipc_getsecid,
3809 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3810 .msg_msg_free_security = smack_msg_msg_free_security,
3812 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3813 .msg_queue_free_security = smack_msg_queue_free_security,
3814 .msg_queue_associate = smack_msg_queue_associate,
3815 .msg_queue_msgctl = smack_msg_queue_msgctl,
3816 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3817 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3819 .shm_alloc_security = smack_shm_alloc_security,
3820 .shm_free_security = smack_shm_free_security,
3821 .shm_associate = smack_shm_associate,
3822 .shm_shmctl = smack_shm_shmctl,
3823 .shm_shmat = smack_shm_shmat,
3825 .sem_alloc_security = smack_sem_alloc_security,
3826 .sem_free_security = smack_sem_free_security,
3827 .sem_associate = smack_sem_associate,
3828 .sem_semctl = smack_sem_semctl,
3829 .sem_semop = smack_sem_semop,
3831 .d_instantiate = smack_d_instantiate,
3833 .getprocattr = smack_getprocattr,
3834 .setprocattr = smack_setprocattr,
3836 .unix_stream_connect = smack_unix_stream_connect,
3837 .unix_may_send = smack_unix_may_send,
3839 .socket_post_create = smack_socket_post_create,
3840 .socket_bind = smack_socket_bind,
3841 .socket_connect = smack_socket_connect,
3842 .socket_sendmsg = smack_socket_sendmsg,
3843 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3844 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3845 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3846 .sk_alloc_security = smack_sk_alloc_security,
3847 .sk_free_security = smack_sk_free_security,
3848 .sock_graft = smack_sock_graft,
3849 .inet_conn_request = smack_inet_conn_request,
3850 .inet_csk_clone = smack_inet_csk_clone,
3852 /* key management security hooks */
3853 #ifdef CONFIG_KEYS
3854 .key_alloc = smack_key_alloc,
3855 .key_free = smack_key_free,
3856 .key_permission = smack_key_permission,
3857 #endif /* CONFIG_KEYS */
3859 /* Audit hooks */
3860 #ifdef CONFIG_AUDIT
3861 .audit_rule_init = smack_audit_rule_init,
3862 .audit_rule_known = smack_audit_rule_known,
3863 .audit_rule_match = smack_audit_rule_match,
3864 .audit_rule_free = smack_audit_rule_free,
3865 #endif /* CONFIG_AUDIT */
3867 .ismaclabel = smack_ismaclabel,
3868 .secid_to_secctx = smack_secid_to_secctx,
3869 .secctx_to_secid = smack_secctx_to_secid,
3870 .release_secctx = smack_release_secctx,
3871 .inode_notifysecctx = smack_inode_notifysecctx,
3872 .inode_setsecctx = smack_inode_setsecctx,
3873 .inode_getsecctx = smack_inode_getsecctx,
3877 static __init void init_smack_known_list(void)
3880 * Initialize rule list locks
3882 mutex_init(&smack_known_huh.smk_rules_lock);
3883 mutex_init(&smack_known_hat.smk_rules_lock);
3884 mutex_init(&smack_known_floor.smk_rules_lock);
3885 mutex_init(&smack_known_star.smk_rules_lock);
3886 mutex_init(&smack_known_invalid.smk_rules_lock);
3887 mutex_init(&smack_known_web.smk_rules_lock);
3889 * Initialize rule lists
3891 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3892 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3893 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3894 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3895 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3896 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3898 * Create the known labels list
3900 smk_insert_entry(&smack_known_huh);
3901 smk_insert_entry(&smack_known_hat);
3902 smk_insert_entry(&smack_known_star);
3903 smk_insert_entry(&smack_known_floor);
3904 smk_insert_entry(&smack_known_invalid);
3905 smk_insert_entry(&smack_known_web);
3909 * smack_init - initialize the smack system
3911 * Returns 0
3913 static __init int smack_init(void)
3915 struct cred *cred;
3916 struct task_smack *tsp;
3918 if (!security_module_enable(&smack_ops))
3919 return 0;
3921 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3922 GFP_KERNEL);
3923 if (tsp == NULL)
3924 return -ENOMEM;
3926 printk(KERN_INFO "Smack: Initializing.\n");
3929 * Set the security state for the initial task.
3931 cred = (struct cred *) current->cred;
3932 cred->security = tsp;
3934 /* initialize the smack_known_list */
3935 init_smack_known_list();
3938 * Register with LSM
3940 if (register_security(&smack_ops))
3941 panic("smack: Unable to register with kernel.\n");
3943 return 0;
3947 * Smack requires early initialization in order to label
3948 * all processes and objects when they are created.
3950 security_initcall(smack_init);