clockevents: Provide combined configure and register function
[linux-2.6/linux-mips.git] / security / smack / smack_lsm.c
blob400a5d5cde6183c9bc3c6d348f58b623e2821b46
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 <ext-jarkko.2.sakkinen@nokia.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.moore@hp.com>
13 * Copyright (C) 2010 Nokia Corporation
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2,
17 * as published by the Free Software Foundation.
20 #include <linux/xattr.h>
21 #include <linux/pagemap.h>
22 #include <linux/mount.h>
23 #include <linux/stat.h>
24 #include <linux/kd.h>
25 #include <asm/ioctls.h>
26 #include <linux/ip.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <linux/pipe_fs_i.h>
32 #include <net/netlabel.h>
33 #include <net/cipso_ipv4.h>
34 #include <linux/audit.h>
35 #include <linux/magic.h>
36 #include <linux/dcache.h>
37 #include "smack.h"
39 #define task_security(task) (task_cred_xxx((task), security))
41 #define TRANS_TRUE "TRUE"
42 #define TRANS_TRUE_SIZE 4
44 /**
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
52 static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
54 int rc;
55 char in[SMK_LABELLEN];
57 if (ip->i_op->getxattr == NULL)
58 return NULL;
60 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
61 if (rc < 0)
62 return NULL;
64 return smk_import(in, rc);
67 /**
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
71 * Returns the new blob or NULL if there's no memory available
73 struct inode_smack *new_inode_smack(char *smack)
75 struct inode_smack *isp;
77 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
78 if (isp == NULL)
79 return NULL;
81 isp->smk_inode = smack;
82 isp->smk_flags = 0;
83 mutex_init(&isp->smk_lock);
85 return isp;
88 /**
89 * new_task_smack - allocate a task 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 static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
96 struct task_smack *tsp;
98 tsp = kzalloc(sizeof(struct task_smack), gfp);
99 if (tsp == NULL)
100 return NULL;
102 tsp->smk_task = task;
103 tsp->smk_forked = forked;
104 INIT_LIST_HEAD(&tsp->smk_rules);
105 mutex_init(&tsp->smk_rules_lock);
107 return tsp;
111 * smk_copy_rules - copy a rule set
112 * @nhead - new rules header pointer
113 * @ohead - old rules header pointer
115 * Returns 0 on success, -ENOMEM on error
117 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
118 gfp_t gfp)
120 struct smack_rule *nrp;
121 struct smack_rule *orp;
122 int rc = 0;
124 INIT_LIST_HEAD(nhead);
126 list_for_each_entry_rcu(orp, ohead, list) {
127 nrp = kzalloc(sizeof(struct smack_rule), gfp);
128 if (nrp == NULL) {
129 rc = -ENOMEM;
130 break;
132 *nrp = *orp;
133 list_add_rcu(&nrp->list, nhead);
135 return rc;
139 * LSM hooks.
140 * We he, that is fun!
144 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
145 * @ctp: child task pointer
146 * @mode: ptrace attachment mode
148 * Returns 0 if access is OK, an error code otherwise
150 * Do the capability checks, and require read and write.
152 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
154 int rc;
155 struct smk_audit_info ad;
156 char *tsp;
158 rc = cap_ptrace_access_check(ctp, mode);
159 if (rc != 0)
160 return rc;
162 tsp = smk_of_task(task_security(ctp));
163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
164 smk_ad_setfield_u_tsk(&ad, ctp);
166 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
167 return rc;
171 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
172 * @ptp: parent task pointer
174 * Returns 0 if access is OK, an error code otherwise
176 * Do the capability checks, and require read and write.
178 static int smack_ptrace_traceme(struct task_struct *ptp)
180 int rc;
181 struct smk_audit_info ad;
182 char *tsp;
184 rc = cap_ptrace_traceme(ptp);
185 if (rc != 0)
186 return rc;
188 tsp = smk_of_task(task_security(ptp));
189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
190 smk_ad_setfield_u_tsk(&ad, ptp);
192 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
193 return rc;
197 * smack_syslog - Smack approval on syslog
198 * @type: message type
200 * Require that the task has the floor label
202 * Returns 0 on success, error code otherwise.
204 static int smack_syslog(int typefrom_file)
206 int rc = 0;
207 char *sp = smk_of_current();
209 if (capable(CAP_MAC_OVERRIDE))
210 return 0;
212 if (sp != smack_known_floor.smk_known)
213 rc = -EACCES;
215 return rc;
220 * Superblock Hooks.
224 * smack_sb_alloc_security - allocate a superblock blob
225 * @sb: the superblock getting the blob
227 * Returns 0 on success or -ENOMEM on error.
229 static int smack_sb_alloc_security(struct super_block *sb)
231 struct superblock_smack *sbsp;
233 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
235 if (sbsp == NULL)
236 return -ENOMEM;
238 sbsp->smk_root = smack_known_floor.smk_known;
239 sbsp->smk_default = smack_known_floor.smk_known;
240 sbsp->smk_floor = smack_known_floor.smk_known;
241 sbsp->smk_hat = smack_known_hat.smk_known;
242 sbsp->smk_initialized = 0;
243 spin_lock_init(&sbsp->smk_sblock);
245 sb->s_security = sbsp;
247 return 0;
251 * smack_sb_free_security - free a superblock blob
252 * @sb: the superblock getting the blob
255 static void smack_sb_free_security(struct super_block *sb)
257 kfree(sb->s_security);
258 sb->s_security = NULL;
262 * smack_sb_copy_data - copy mount options data for processing
263 * @orig: where to start
264 * @smackopts: mount options string
266 * Returns 0 on success or -ENOMEM on error.
268 * Copy the Smack specific mount options out of the mount
269 * options list.
271 static int smack_sb_copy_data(char *orig, char *smackopts)
273 char *cp, *commap, *otheropts, *dp;
275 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
276 if (otheropts == NULL)
277 return -ENOMEM;
279 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
280 if (strstr(cp, SMK_FSDEFAULT) == cp)
281 dp = smackopts;
282 else if (strstr(cp, SMK_FSFLOOR) == cp)
283 dp = smackopts;
284 else if (strstr(cp, SMK_FSHAT) == cp)
285 dp = smackopts;
286 else if (strstr(cp, SMK_FSROOT) == cp)
287 dp = smackopts;
288 else
289 dp = otheropts;
291 commap = strchr(cp, ',');
292 if (commap != NULL)
293 *commap = '\0';
295 if (*dp != '\0')
296 strcat(dp, ",");
297 strcat(dp, cp);
300 strcpy(orig, otheropts);
301 free_page((unsigned long)otheropts);
303 return 0;
307 * smack_sb_kern_mount - Smack specific mount processing
308 * @sb: the file system superblock
309 * @flags: the mount flags
310 * @data: the smack mount options
312 * Returns 0 on success, an error code on failure
314 static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
316 struct dentry *root = sb->s_root;
317 struct inode *inode = root->d_inode;
318 struct superblock_smack *sp = sb->s_security;
319 struct inode_smack *isp;
320 char *op;
321 char *commap;
322 char *nsp;
324 spin_lock(&sp->smk_sblock);
325 if (sp->smk_initialized != 0) {
326 spin_unlock(&sp->smk_sblock);
327 return 0;
329 sp->smk_initialized = 1;
330 spin_unlock(&sp->smk_sblock);
332 for (op = data; op != NULL; op = commap) {
333 commap = strchr(op, ',');
334 if (commap != NULL)
335 *commap++ = '\0';
337 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
338 op += strlen(SMK_FSHAT);
339 nsp = smk_import(op, 0);
340 if (nsp != NULL)
341 sp->smk_hat = nsp;
342 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
343 op += strlen(SMK_FSFLOOR);
344 nsp = smk_import(op, 0);
345 if (nsp != NULL)
346 sp->smk_floor = nsp;
347 } else if (strncmp(op, SMK_FSDEFAULT,
348 strlen(SMK_FSDEFAULT)) == 0) {
349 op += strlen(SMK_FSDEFAULT);
350 nsp = smk_import(op, 0);
351 if (nsp != NULL)
352 sp->smk_default = nsp;
353 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
354 op += strlen(SMK_FSROOT);
355 nsp = smk_import(op, 0);
356 if (nsp != NULL)
357 sp->smk_root = nsp;
362 * Initialize the root inode.
364 isp = inode->i_security;
365 if (isp == NULL)
366 inode->i_security = new_inode_smack(sp->smk_root);
367 else
368 isp->smk_inode = sp->smk_root;
370 return 0;
374 * smack_sb_statfs - Smack check on statfs
375 * @dentry: identifies the file system in question
377 * Returns 0 if current can read the floor of the filesystem,
378 * and error code otherwise
380 static int smack_sb_statfs(struct dentry *dentry)
382 struct superblock_smack *sbp = dentry->d_sb->s_security;
383 int rc;
384 struct smk_audit_info ad;
386 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
387 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
389 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
390 return rc;
394 * smack_sb_mount - Smack check for mounting
395 * @dev_name: unused
396 * @path: mount point
397 * @type: unused
398 * @flags: unused
399 * @data: unused
401 * Returns 0 if current can write the floor of the filesystem
402 * being mounted on, an error code otherwise.
404 static int smack_sb_mount(char *dev_name, struct path *path,
405 char *type, unsigned long flags, void *data)
407 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
408 struct smk_audit_info ad;
410 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
411 smk_ad_setfield_u_fs_path(&ad, *path);
413 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
417 * smack_sb_umount - Smack check for unmounting
418 * @mnt: file system to unmount
419 * @flags: unused
421 * Returns 0 if current can write the floor of the filesystem
422 * being unmounted, an error code otherwise.
424 static int smack_sb_umount(struct vfsmount *mnt, int flags)
426 struct superblock_smack *sbp;
427 struct smk_audit_info ad;
429 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
430 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
431 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
433 sbp = mnt->mnt_sb->s_security;
434 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
438 * BPRM hooks
441 static int smack_bprm_set_creds(struct linux_binprm *bprm)
443 struct task_smack *tsp = bprm->cred->security;
444 struct inode_smack *isp;
445 struct dentry *dp;
446 int rc;
448 rc = cap_bprm_set_creds(bprm);
449 if (rc != 0)
450 return rc;
452 if (bprm->cred_prepared)
453 return 0;
455 if (bprm->file == NULL || bprm->file->f_dentry == NULL)
456 return 0;
458 dp = bprm->file->f_dentry;
460 if (dp->d_inode == NULL)
461 return 0;
463 isp = dp->d_inode->i_security;
465 if (isp->smk_task != NULL)
466 tsp->smk_task = isp->smk_task;
468 return 0;
472 * Inode hooks
476 * smack_inode_alloc_security - allocate an inode blob
477 * @inode: the inode in need of a blob
479 * Returns 0 if it gets a blob, -ENOMEM otherwise
481 static int smack_inode_alloc_security(struct inode *inode)
483 inode->i_security = new_inode_smack(smk_of_current());
484 if (inode->i_security == NULL)
485 return -ENOMEM;
486 return 0;
490 * smack_inode_free_security - free an inode blob
491 * @inode: the inode with a blob
493 * Clears the blob pointer in inode
495 static void smack_inode_free_security(struct inode *inode)
497 kfree(inode->i_security);
498 inode->i_security = NULL;
502 * smack_inode_init_security - copy out the smack from an inode
503 * @inode: the inode
504 * @dir: unused
505 * @qstr: unused
506 * @name: where to put the attribute name
507 * @value: where to put the attribute value
508 * @len: where to put the length of the attribute
510 * Returns 0 if it all works out, -ENOMEM if there's no memory
512 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
513 const struct qstr *qstr, char **name,
514 void **value, size_t *len)
516 char *isp = smk_of_inode(inode);
517 char *dsp = smk_of_inode(dir);
518 int may;
520 if (name) {
521 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
522 if (*name == NULL)
523 return -ENOMEM;
526 if (value) {
527 rcu_read_lock();
528 may = smk_access_entry(smk_of_current(), dsp, &smack_rule_list);
529 rcu_read_unlock();
532 * If the access rule allows transmutation and
533 * the directory requests transmutation then
534 * by all means transmute.
536 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
537 smk_inode_transmutable(dir))
538 isp = dsp;
540 *value = kstrdup(isp, GFP_KERNEL);
541 if (*value == NULL)
542 return -ENOMEM;
545 if (len)
546 *len = strlen(isp) + 1;
548 return 0;
552 * smack_inode_link - Smack check on link
553 * @old_dentry: the existing object
554 * @dir: unused
555 * @new_dentry: the new object
557 * Returns 0 if access is permitted, an error code otherwise
559 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
560 struct dentry *new_dentry)
562 char *isp;
563 struct smk_audit_info ad;
564 int rc;
566 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
567 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
569 isp = smk_of_inode(old_dentry->d_inode);
570 rc = smk_curacc(isp, MAY_WRITE, &ad);
572 if (rc == 0 && new_dentry->d_inode != NULL) {
573 isp = smk_of_inode(new_dentry->d_inode);
574 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
575 rc = smk_curacc(isp, MAY_WRITE, &ad);
578 return rc;
582 * smack_inode_unlink - Smack check on inode deletion
583 * @dir: containing directory object
584 * @dentry: file to unlink
586 * Returns 0 if current can write the containing directory
587 * and the object, error code otherwise
589 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
591 struct inode *ip = dentry->d_inode;
592 struct smk_audit_info ad;
593 int rc;
595 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
596 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
599 * You need write access to the thing you're unlinking
601 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
602 if (rc == 0) {
604 * You also need write access to the containing directory
606 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
607 smk_ad_setfield_u_fs_inode(&ad, dir);
608 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
610 return rc;
614 * smack_inode_rmdir - Smack check on directory deletion
615 * @dir: containing directory object
616 * @dentry: directory to unlink
618 * Returns 0 if current can write the containing directory
619 * and the directory, error code otherwise
621 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
623 struct smk_audit_info ad;
624 int rc;
626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
627 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
630 * You need write access to the thing you're removing
632 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
633 if (rc == 0) {
635 * You also need write access to the containing directory
637 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
638 smk_ad_setfield_u_fs_inode(&ad, dir);
639 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
642 return rc;
646 * smack_inode_rename - Smack check on rename
647 * @old_inode: the old directory
648 * @old_dentry: unused
649 * @new_inode: the new directory
650 * @new_dentry: unused
652 * Read and write access is required on both the old and
653 * new directories.
655 * Returns 0 if access is permitted, an error code otherwise
657 static int smack_inode_rename(struct inode *old_inode,
658 struct dentry *old_dentry,
659 struct inode *new_inode,
660 struct dentry *new_dentry)
662 int rc;
663 char *isp;
664 struct smk_audit_info ad;
666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
667 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
669 isp = smk_of_inode(old_dentry->d_inode);
670 rc = smk_curacc(isp, MAY_READWRITE, &ad);
672 if (rc == 0 && new_dentry->d_inode != NULL) {
673 isp = smk_of_inode(new_dentry->d_inode);
674 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
675 rc = smk_curacc(isp, MAY_READWRITE, &ad);
677 return rc;
681 * smack_inode_permission - Smack version of permission()
682 * @inode: the inode in question
683 * @mask: the access requested
685 * This is the important Smack hook.
687 * Returns 0 if access is permitted, -EACCES otherwise
689 static int smack_inode_permission(struct inode *inode, int mask, unsigned flags)
691 struct smk_audit_info ad;
693 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
695 * No permission to check. Existence test. Yup, it's there.
697 if (mask == 0)
698 return 0;
700 /* May be droppable after audit */
701 if (flags & IPERM_FLAG_RCU)
702 return -ECHILD;
703 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
704 smk_ad_setfield_u_fs_inode(&ad, inode);
705 return smk_curacc(smk_of_inode(inode), mask, &ad);
709 * smack_inode_setattr - Smack check for setting attributes
710 * @dentry: the object
711 * @iattr: for the force flag
713 * Returns 0 if access is permitted, an error code otherwise
715 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
717 struct smk_audit_info ad;
719 * Need to allow for clearing the setuid bit.
721 if (iattr->ia_valid & ATTR_FORCE)
722 return 0;
723 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
724 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
726 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
730 * smack_inode_getattr - Smack check for getting attributes
731 * @mnt: unused
732 * @dentry: the object
734 * Returns 0 if access is permitted, an error code otherwise
736 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
738 struct smk_audit_info ad;
740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
741 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
742 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
743 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
747 * smack_inode_setxattr - Smack check for setting xattrs
748 * @dentry: the object
749 * @name: name of the attribute
750 * @value: unused
751 * @size: unused
752 * @flags: unused
754 * This protects the Smack attribute explicitly.
756 * Returns 0 if access is permitted, an error code otherwise
758 static int smack_inode_setxattr(struct dentry *dentry, const char *name,
759 const void *value, size_t size, int flags)
761 struct smk_audit_info ad;
762 int rc = 0;
764 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
765 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
766 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
767 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
768 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
769 if (!capable(CAP_MAC_ADMIN))
770 rc = -EPERM;
772 * check label validity here so import wont fail on
773 * post_setxattr
775 if (size == 0 || size >= SMK_LABELLEN ||
776 smk_import(value, size) == NULL)
777 rc = -EINVAL;
778 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
779 if (!capable(CAP_MAC_ADMIN))
780 rc = -EPERM;
781 if (size != TRANS_TRUE_SIZE ||
782 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
783 rc = -EINVAL;
784 } else
785 rc = cap_inode_setxattr(dentry, name, value, size, flags);
787 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
788 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
790 if (rc == 0)
791 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
793 return rc;
797 * smack_inode_post_setxattr - Apply the Smack update approved above
798 * @dentry: object
799 * @name: attribute name
800 * @value: attribute value
801 * @size: attribute size
802 * @flags: unused
804 * Set the pointer in the inode blob to the entry found
805 * in the master label list.
807 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
808 const void *value, size_t size, int flags)
810 char *nsp;
811 struct inode_smack *isp = dentry->d_inode->i_security;
813 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
814 nsp = smk_import(value, size);
815 if (nsp != NULL)
816 isp->smk_inode = nsp;
817 else
818 isp->smk_inode = smack_known_invalid.smk_known;
819 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
820 nsp = smk_import(value, size);
821 if (nsp != NULL)
822 isp->smk_task = nsp;
823 else
824 isp->smk_task = smack_known_invalid.smk_known;
825 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
826 nsp = smk_import(value, size);
827 if (nsp != NULL)
828 isp->smk_mmap = nsp;
829 else
830 isp->smk_mmap = smack_known_invalid.smk_known;
831 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
832 isp->smk_flags |= SMK_INODE_TRANSMUTE;
834 return;
838 * smack_inode_getxattr - Smack check on getxattr
839 * @dentry: the object
840 * @name: unused
842 * Returns 0 if access is permitted, an error code otherwise
844 static int smack_inode_getxattr(struct dentry *dentry, const char *name)
846 struct smk_audit_info ad;
848 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
849 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
851 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
855 * smack_inode_removexattr - Smack check on removexattr
856 * @dentry: the object
857 * @name: name of the attribute
859 * Removing the Smack attribute requires CAP_MAC_ADMIN
861 * Returns 0 if access is permitted, an error code otherwise
863 static int smack_inode_removexattr(struct dentry *dentry, const char *name)
865 struct inode_smack *isp;
866 struct smk_audit_info ad;
867 int rc = 0;
869 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
870 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
871 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
872 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
873 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
874 strcmp(name, XATTR_NAME_SMACKMMAP)) {
875 if (!capable(CAP_MAC_ADMIN))
876 rc = -EPERM;
877 } else
878 rc = cap_inode_removexattr(dentry, name);
880 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
881 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
882 if (rc == 0)
883 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
885 if (rc == 0) {
886 isp = dentry->d_inode->i_security;
887 isp->smk_task = NULL;
888 isp->smk_mmap = NULL;
891 return rc;
895 * smack_inode_getsecurity - get smack xattrs
896 * @inode: the object
897 * @name: attribute name
898 * @buffer: where to put the result
899 * @alloc: unused
901 * Returns the size of the attribute or an error code
903 static int smack_inode_getsecurity(const struct inode *inode,
904 const char *name, void **buffer,
905 bool alloc)
907 struct socket_smack *ssp;
908 struct socket *sock;
909 struct super_block *sbp;
910 struct inode *ip = (struct inode *)inode;
911 char *isp;
912 int ilen;
913 int rc = 0;
915 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
916 isp = smk_of_inode(inode);
917 ilen = strlen(isp) + 1;
918 *buffer = isp;
919 return ilen;
923 * The rest of the Smack xattrs are only on sockets.
925 sbp = ip->i_sb;
926 if (sbp->s_magic != SOCKFS_MAGIC)
927 return -EOPNOTSUPP;
929 sock = SOCKET_I(ip);
930 if (sock == NULL || sock->sk == NULL)
931 return -EOPNOTSUPP;
933 ssp = sock->sk->sk_security;
935 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
936 isp = ssp->smk_in;
937 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
938 isp = ssp->smk_out;
939 else
940 return -EOPNOTSUPP;
942 ilen = strlen(isp) + 1;
943 if (rc == 0) {
944 *buffer = isp;
945 rc = ilen;
948 return rc;
953 * smack_inode_listsecurity - list the Smack attributes
954 * @inode: the object
955 * @buffer: where they go
956 * @buffer_size: size of buffer
958 * Returns 0 on success, -EINVAL otherwise
960 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
961 size_t buffer_size)
963 int len = strlen(XATTR_NAME_SMACK);
965 if (buffer != NULL && len <= buffer_size) {
966 memcpy(buffer, XATTR_NAME_SMACK, len);
967 return len;
969 return -EINVAL;
973 * smack_inode_getsecid - Extract inode's security id
974 * @inode: inode to extract the info from
975 * @secid: where result will be saved
977 static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
979 struct inode_smack *isp = inode->i_security;
981 *secid = smack_to_secid(isp->smk_inode);
985 * File Hooks
989 * smack_file_permission - Smack check on file operations
990 * @file: unused
991 * @mask: unused
993 * Returns 0
995 * Should access checks be done on each read or write?
996 * UNICOS and SELinux say yes.
997 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
999 * I'll say no for now. Smack does not do the frequent
1000 * label changing that SELinux does.
1002 static int smack_file_permission(struct file *file, int mask)
1004 return 0;
1008 * smack_file_alloc_security - assign a file security blob
1009 * @file: the object
1011 * The security blob for a file is a pointer to the master
1012 * label list, so no allocation is done.
1014 * Returns 0
1016 static int smack_file_alloc_security(struct file *file)
1018 file->f_security = smk_of_current();
1019 return 0;
1023 * smack_file_free_security - clear a file security blob
1024 * @file: the object
1026 * The security blob for a file is a pointer to the master
1027 * label list, so no memory is freed.
1029 static void smack_file_free_security(struct file *file)
1031 file->f_security = NULL;
1035 * smack_file_ioctl - Smack check on ioctls
1036 * @file: the object
1037 * @cmd: what to do
1038 * @arg: unused
1040 * Relies heavily on the correct use of the ioctl command conventions.
1042 * Returns 0 if allowed, error code otherwise
1044 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1045 unsigned long arg)
1047 int rc = 0;
1048 struct smk_audit_info ad;
1050 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1051 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1053 if (_IOC_DIR(cmd) & _IOC_WRITE)
1054 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1056 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
1057 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1059 return rc;
1063 * smack_file_lock - Smack check on file locking
1064 * @file: the object
1065 * @cmd: unused
1067 * Returns 0 if current has write access, error code otherwise
1069 static int smack_file_lock(struct file *file, unsigned int cmd)
1071 struct smk_audit_info ad;
1073 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1074 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
1075 return smk_curacc(file->f_security, MAY_WRITE, &ad);
1079 * smack_file_fcntl - Smack check on fcntl
1080 * @file: the object
1081 * @cmd: what action to check
1082 * @arg: unused
1084 * Returns 0 if current has access, error code otherwise
1086 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1087 unsigned long arg)
1089 struct smk_audit_info ad;
1090 int rc;
1092 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1093 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1095 switch (cmd) {
1096 case F_DUPFD:
1097 case F_GETFD:
1098 case F_GETFL:
1099 case F_GETLK:
1100 case F_GETOWN:
1101 case F_GETSIG:
1102 rc = smk_curacc(file->f_security, MAY_READ, &ad);
1103 break;
1104 case F_SETFD:
1105 case F_SETFL:
1106 case F_SETLK:
1107 case F_SETLKW:
1108 case F_SETOWN:
1109 case F_SETSIG:
1110 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1111 break;
1112 default:
1113 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
1116 return rc;
1120 * smack_file_mmap :
1121 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1122 * if mapping anonymous memory.
1123 * @file contains the file structure for file to map (may be NULL).
1124 * @reqprot contains the protection requested by the application.
1125 * @prot contains the protection that will be applied by the kernel.
1126 * @flags contains the operational flags.
1127 * Return 0 if permission is granted.
1129 static int smack_file_mmap(struct file *file,
1130 unsigned long reqprot, unsigned long prot,
1131 unsigned long flags, unsigned long addr,
1132 unsigned long addr_only)
1134 struct smack_rule *srp;
1135 struct task_smack *tsp;
1136 char *sp;
1137 char *msmack;
1138 char *osmack;
1139 struct inode_smack *isp;
1140 struct dentry *dp;
1141 int may;
1142 int mmay;
1143 int tmay;
1144 int rc;
1146 /* do DAC check on address space usage */
1147 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1148 if (rc || addr_only)
1149 return rc;
1151 if (file == NULL || file->f_dentry == NULL)
1152 return 0;
1154 dp = file->f_dentry;
1156 if (dp->d_inode == NULL)
1157 return 0;
1159 isp = dp->d_inode->i_security;
1160 if (isp->smk_mmap == NULL)
1161 return 0;
1162 msmack = isp->smk_mmap;
1164 tsp = current_security();
1165 sp = smk_of_current();
1166 rc = 0;
1168 rcu_read_lock();
1170 * For each Smack rule associated with the subject
1171 * label verify that the SMACK64MMAP also has access
1172 * to that rule's object label.
1174 * Because neither of the labels comes
1175 * from the networking code it is sufficient
1176 * to compare pointers.
1178 list_for_each_entry_rcu(srp, &smack_rule_list, list) {
1179 if (srp->smk_subject != sp)
1180 continue;
1182 osmack = srp->smk_object;
1184 * Matching labels always allows access.
1186 if (msmack == osmack)
1187 continue;
1189 * If there is a matching local rule take
1190 * that into account as well.
1192 may = smk_access_entry(srp->smk_subject, osmack,
1193 &tsp->smk_rules);
1194 if (may == -ENOENT)
1195 may = srp->smk_access;
1196 else
1197 may &= srp->smk_access;
1199 * If may is zero the SMACK64MMAP subject can't
1200 * possibly have less access.
1202 if (may == 0)
1203 continue;
1206 * Fetch the global list entry.
1207 * If there isn't one a SMACK64MMAP subject
1208 * can't have as much access as current.
1210 mmay = smk_access_entry(msmack, osmack, &smack_rule_list);
1211 if (mmay == -ENOENT) {
1212 rc = -EACCES;
1213 break;
1216 * If there is a local entry it modifies the
1217 * potential access, too.
1219 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1220 if (tmay != -ENOENT)
1221 mmay &= tmay;
1224 * If there is any access available to current that is
1225 * not available to a SMACK64MMAP subject
1226 * deny access.
1228 if ((may | mmay) != mmay) {
1229 rc = -EACCES;
1230 break;
1234 rcu_read_unlock();
1236 return rc;
1240 * smack_file_set_fowner - set the file security blob value
1241 * @file: object in question
1243 * Returns 0
1244 * Further research may be required on this one.
1246 static int smack_file_set_fowner(struct file *file)
1248 file->f_security = smk_of_current();
1249 return 0;
1253 * smack_file_send_sigiotask - Smack on sigio
1254 * @tsk: The target task
1255 * @fown: the object the signal come from
1256 * @signum: unused
1258 * Allow a privileged task to get signals even if it shouldn't
1260 * Returns 0 if a subject with the object's smack could
1261 * write to the task, an error code otherwise.
1263 static int smack_file_send_sigiotask(struct task_struct *tsk,
1264 struct fown_struct *fown, int signum)
1266 struct file *file;
1267 int rc;
1268 char *tsp = smk_of_task(tsk->cred->security);
1269 struct smk_audit_info ad;
1272 * struct fown_struct is never outside the context of a struct file
1274 file = container_of(fown, struct file, f_owner);
1276 /* we don't log here as rc can be overriden */
1277 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
1278 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
1279 rc = 0;
1281 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1282 smk_ad_setfield_u_tsk(&ad, tsk);
1283 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
1284 return rc;
1288 * smack_file_receive - Smack file receive check
1289 * @file: the object
1291 * Returns 0 if current has access, error code otherwise
1293 static int smack_file_receive(struct file *file)
1295 int may = 0;
1296 struct smk_audit_info ad;
1298 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1299 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1301 * This code relies on bitmasks.
1303 if (file->f_mode & FMODE_READ)
1304 may = MAY_READ;
1305 if (file->f_mode & FMODE_WRITE)
1306 may |= MAY_WRITE;
1308 return smk_curacc(file->f_security, may, &ad);
1312 * Task hooks
1316 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1317 * @new: the new credentials
1318 * @gfp: the atomicity of any memory allocations
1320 * Prepare a blank set of credentials for modification. This must allocate all
1321 * the memory the LSM module might require such that cred_transfer() can
1322 * complete without error.
1324 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1326 struct task_smack *tsp;
1328 tsp = new_task_smack(NULL, NULL, gfp);
1329 if (tsp == NULL)
1330 return -ENOMEM;
1332 cred->security = tsp;
1334 return 0;
1339 * smack_cred_free - "free" task-level security credentials
1340 * @cred: the credentials in question
1343 static void smack_cred_free(struct cred *cred)
1345 struct task_smack *tsp = cred->security;
1346 struct smack_rule *rp;
1347 struct list_head *l;
1348 struct list_head *n;
1350 if (tsp == NULL)
1351 return;
1352 cred->security = NULL;
1354 list_for_each_safe(l, n, &tsp->smk_rules) {
1355 rp = list_entry(l, struct smack_rule, list);
1356 list_del(&rp->list);
1357 kfree(rp);
1359 kfree(tsp);
1363 * smack_cred_prepare - prepare new set of credentials for modification
1364 * @new: the new credentials
1365 * @old: the original credentials
1366 * @gfp: the atomicity of any memory allocations
1368 * Prepare a new set of credentials for modification.
1370 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1371 gfp_t gfp)
1373 struct task_smack *old_tsp = old->security;
1374 struct task_smack *new_tsp;
1375 int rc;
1377 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
1378 if (new_tsp == NULL)
1379 return -ENOMEM;
1381 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1382 if (rc != 0)
1383 return rc;
1385 new->security = new_tsp;
1386 return 0;
1390 * smack_cred_transfer - Transfer the old credentials to the new credentials
1391 * @new: the new credentials
1392 * @old: the original credentials
1394 * Fill in a set of blank credentials from another set of credentials.
1396 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1398 struct task_smack *old_tsp = old->security;
1399 struct task_smack *new_tsp = new->security;
1401 new_tsp->smk_task = old_tsp->smk_task;
1402 new_tsp->smk_forked = old_tsp->smk_task;
1403 mutex_init(&new_tsp->smk_rules_lock);
1404 INIT_LIST_HEAD(&new_tsp->smk_rules);
1407 /* cbs copy rule list */
1411 * smack_kernel_act_as - Set the subjective context in a set of credentials
1412 * @new: points to the set of credentials to be modified.
1413 * @secid: specifies the security ID to be set
1415 * Set the security data for a kernel service.
1417 static int smack_kernel_act_as(struct cred *new, u32 secid)
1419 struct task_smack *new_tsp = new->security;
1420 char *smack = smack_from_secid(secid);
1422 if (smack == NULL)
1423 return -EINVAL;
1425 new_tsp->smk_task = smack;
1426 return 0;
1430 * smack_kernel_create_files_as - Set the file creation label in a set of creds
1431 * @new: points to the set of credentials to be modified
1432 * @inode: points to the inode to use as a reference
1434 * Set the file creation context in a set of credentials to the same
1435 * as the objective context of the specified inode
1437 static int smack_kernel_create_files_as(struct cred *new,
1438 struct inode *inode)
1440 struct inode_smack *isp = inode->i_security;
1441 struct task_smack *tsp = new->security;
1443 tsp->smk_forked = isp->smk_inode;
1444 tsp->smk_task = isp->smk_inode;
1445 return 0;
1449 * smk_curacc_on_task - helper to log task related access
1450 * @p: the task object
1451 * @access : the access requested
1453 * Return 0 if access is permitted
1455 static int smk_curacc_on_task(struct task_struct *p, int access)
1457 struct smk_audit_info ad;
1459 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1460 smk_ad_setfield_u_tsk(&ad, p);
1461 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
1465 * smack_task_setpgid - Smack check on setting pgid
1466 * @p: the task object
1467 * @pgid: unused
1469 * Return 0 if write access is permitted
1471 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1473 return smk_curacc_on_task(p, MAY_WRITE);
1477 * smack_task_getpgid - Smack access check for getpgid
1478 * @p: the object task
1480 * Returns 0 if current can read the object task, error code otherwise
1482 static int smack_task_getpgid(struct task_struct *p)
1484 return smk_curacc_on_task(p, MAY_READ);
1488 * smack_task_getsid - Smack access check for getsid
1489 * @p: the object task
1491 * Returns 0 if current can read the object task, error code otherwise
1493 static int smack_task_getsid(struct task_struct *p)
1495 return smk_curacc_on_task(p, MAY_READ);
1499 * smack_task_getsecid - get the secid of the task
1500 * @p: the object task
1501 * @secid: where to put the result
1503 * Sets the secid to contain a u32 version of the smack label.
1505 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1507 *secid = smack_to_secid(smk_of_task(task_security(p)));
1511 * smack_task_setnice - Smack check on setting nice
1512 * @p: the task object
1513 * @nice: unused
1515 * Return 0 if write access is permitted
1517 static int smack_task_setnice(struct task_struct *p, int nice)
1519 int rc;
1521 rc = cap_task_setnice(p, nice);
1522 if (rc == 0)
1523 rc = smk_curacc_on_task(p, MAY_WRITE);
1524 return rc;
1528 * smack_task_setioprio - Smack check on setting ioprio
1529 * @p: the task object
1530 * @ioprio: unused
1532 * Return 0 if write access is permitted
1534 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1536 int rc;
1538 rc = cap_task_setioprio(p, ioprio);
1539 if (rc == 0)
1540 rc = smk_curacc_on_task(p, MAY_WRITE);
1541 return rc;
1545 * smack_task_getioprio - Smack check on reading ioprio
1546 * @p: the task object
1548 * Return 0 if read access is permitted
1550 static int smack_task_getioprio(struct task_struct *p)
1552 return smk_curacc_on_task(p, MAY_READ);
1556 * smack_task_setscheduler - Smack check on setting scheduler
1557 * @p: the task object
1558 * @policy: unused
1559 * @lp: unused
1561 * Return 0 if read access is permitted
1563 static int smack_task_setscheduler(struct task_struct *p)
1565 int rc;
1567 rc = cap_task_setscheduler(p);
1568 if (rc == 0)
1569 rc = smk_curacc_on_task(p, MAY_WRITE);
1570 return rc;
1574 * smack_task_getscheduler - Smack check on reading scheduler
1575 * @p: the task object
1577 * Return 0 if read access is permitted
1579 static int smack_task_getscheduler(struct task_struct *p)
1581 return smk_curacc_on_task(p, MAY_READ);
1585 * smack_task_movememory - Smack check on moving memory
1586 * @p: the task object
1588 * Return 0 if write access is permitted
1590 static int smack_task_movememory(struct task_struct *p)
1592 return smk_curacc_on_task(p, MAY_WRITE);
1596 * smack_task_kill - Smack check on signal delivery
1597 * @p: the task object
1598 * @info: unused
1599 * @sig: unused
1600 * @secid: identifies the smack to use in lieu of current's
1602 * Return 0 if write access is permitted
1604 * The secid behavior is an artifact of an SELinux hack
1605 * in the USB code. Someday it may go away.
1607 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1608 int sig, u32 secid)
1610 struct smk_audit_info ad;
1612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1613 smk_ad_setfield_u_tsk(&ad, p);
1615 * Sending a signal requires that the sender
1616 * can write the receiver.
1618 if (secid == 0)
1619 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1620 &ad);
1622 * If the secid isn't 0 we're dealing with some USB IO
1623 * specific behavior. This is not clean. For one thing
1624 * we can't take privilege into account.
1626 return smk_access(smack_from_secid(secid),
1627 smk_of_task(task_security(p)), MAY_WRITE, &ad);
1631 * smack_task_wait - Smack access check for waiting
1632 * @p: task to wait for
1634 * Returns 0 if current can wait for p, error code otherwise
1636 static int smack_task_wait(struct task_struct *p)
1638 struct smk_audit_info ad;
1639 char *sp = smk_of_current();
1640 char *tsp = smk_of_forked(task_security(p));
1641 int rc;
1643 /* we don't log here, we can be overriden */
1644 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
1645 if (rc == 0)
1646 goto out_log;
1649 * Allow the operation to succeed if either task
1650 * has privilege to perform operations that might
1651 * account for the smack labels having gotten to
1652 * be different in the first place.
1654 * This breaks the strict subject/object access
1655 * control ideal, taking the object's privilege
1656 * state into account in the decision as well as
1657 * the smack value.
1659 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
1660 rc = 0;
1661 /* we log only if we didn't get overriden */
1662 out_log:
1663 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1664 smk_ad_setfield_u_tsk(&ad, p);
1665 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
1666 return rc;
1670 * smack_task_to_inode - copy task smack into the inode blob
1671 * @p: task to copy from
1672 * @inode: inode to copy to
1674 * Sets the smack pointer in the inode security blob
1676 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1678 struct inode_smack *isp = inode->i_security;
1679 isp->smk_inode = smk_of_task(task_security(p));
1683 * Socket hooks.
1687 * smack_sk_alloc_security - Allocate a socket blob
1688 * @sk: the socket
1689 * @family: unused
1690 * @gfp_flags: memory allocation flags
1692 * Assign Smack pointers to current
1694 * Returns 0 on success, -ENOMEM is there's no memory
1696 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1698 char *csp = smk_of_current();
1699 struct socket_smack *ssp;
1701 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1702 if (ssp == NULL)
1703 return -ENOMEM;
1705 ssp->smk_in = csp;
1706 ssp->smk_out = csp;
1707 ssp->smk_packet[0] = '\0';
1709 sk->sk_security = ssp;
1711 return 0;
1715 * smack_sk_free_security - Free a socket blob
1716 * @sk: the socket
1718 * Clears the blob pointer
1720 static void smack_sk_free_security(struct sock *sk)
1722 kfree(sk->sk_security);
1726 * smack_host_label - check host based restrictions
1727 * @sip: the object end
1729 * looks for host based access restrictions
1731 * This version will only be appropriate for really small sets of single label
1732 * hosts. The caller is responsible for ensuring that the RCU read lock is
1733 * taken before calling this function.
1735 * Returns the label of the far end or NULL if it's not special.
1737 static char *smack_host_label(struct sockaddr_in *sip)
1739 struct smk_netlbladdr *snp;
1740 struct in_addr *siap = &sip->sin_addr;
1742 if (siap->s_addr == 0)
1743 return NULL;
1745 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1747 * we break after finding the first match because
1748 * the list is sorted from longest to shortest mask
1749 * so we have found the most specific match
1751 if ((&snp->smk_host.sin_addr)->s_addr ==
1752 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1753 /* we have found the special CIPSO option */
1754 if (snp->smk_label == smack_cipso_option)
1755 return NULL;
1756 return snp->smk_label;
1759 return NULL;
1763 * smack_set_catset - convert a capset to netlabel mls categories
1764 * @catset: the Smack categories
1765 * @sap: where to put the netlabel categories
1767 * Allocates and fills attr.mls.cat
1769 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1771 unsigned char *cp;
1772 unsigned char m;
1773 int cat;
1774 int rc;
1775 int byte;
1777 if (!catset)
1778 return;
1780 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1781 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1782 sap->attr.mls.cat->startbit = 0;
1784 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1785 for (m = 0x80; m != 0; m >>= 1, cat++) {
1786 if ((m & *cp) == 0)
1787 continue;
1788 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1789 cat, GFP_ATOMIC);
1794 * smack_to_secattr - fill a secattr from a smack value
1795 * @smack: the smack value
1796 * @nlsp: where the result goes
1798 * Casey says that CIPSO is good enough for now.
1799 * It can be used to effect.
1800 * It can also be abused to effect when necessary.
1801 * Apologies to the TSIG group in general and GW in particular.
1803 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1805 struct smack_cipso cipso;
1806 int rc;
1808 nlsp->domain = smack;
1809 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1811 rc = smack_to_cipso(smack, &cipso);
1812 if (rc == 0) {
1813 nlsp->attr.mls.lvl = cipso.smk_level;
1814 smack_set_catset(cipso.smk_catset, nlsp);
1815 } else {
1816 nlsp->attr.mls.lvl = smack_cipso_direct;
1817 smack_set_catset(smack, nlsp);
1822 * smack_netlabel - Set the secattr on a socket
1823 * @sk: the socket
1824 * @labeled: socket label scheme
1826 * Convert the outbound smack value (smk_out) to a
1827 * secattr and attach it to the socket.
1829 * Returns 0 on success or an error code
1831 static int smack_netlabel(struct sock *sk, int labeled)
1833 struct socket_smack *ssp = sk->sk_security;
1834 struct netlbl_lsm_secattr secattr;
1835 int rc = 0;
1838 * Usually the netlabel code will handle changing the
1839 * packet labeling based on the label.
1840 * The case of a single label host is different, because
1841 * a single label host should never get a labeled packet
1842 * even though the label is usually associated with a packet
1843 * label.
1845 local_bh_disable();
1846 bh_lock_sock_nested(sk);
1848 if (ssp->smk_out == smack_net_ambient ||
1849 labeled == SMACK_UNLABELED_SOCKET)
1850 netlbl_sock_delattr(sk);
1851 else {
1852 netlbl_secattr_init(&secattr);
1853 smack_to_secattr(ssp->smk_out, &secattr);
1854 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
1855 netlbl_secattr_destroy(&secattr);
1858 bh_unlock_sock(sk);
1859 local_bh_enable();
1861 return rc;
1865 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1866 * @sk: the socket
1867 * @sap: the destination address
1869 * Set the correct secattr for the given socket based on the destination
1870 * address and perform any outbound access checks needed.
1872 * Returns 0 on success or an error code.
1875 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1877 int rc;
1878 int sk_lbl;
1879 char *hostsp;
1880 struct socket_smack *ssp = sk->sk_security;
1881 struct smk_audit_info ad;
1883 rcu_read_lock();
1884 hostsp = smack_host_label(sap);
1885 if (hostsp != NULL) {
1886 sk_lbl = SMACK_UNLABELED_SOCKET;
1887 #ifdef CONFIG_AUDIT
1888 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1889 ad.a.u.net.family = sap->sin_family;
1890 ad.a.u.net.dport = sap->sin_port;
1891 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1892 #endif
1893 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
1894 } else {
1895 sk_lbl = SMACK_CIPSO_SOCKET;
1896 rc = 0;
1898 rcu_read_unlock();
1899 if (rc != 0)
1900 return rc;
1902 return smack_netlabel(sk, sk_lbl);
1906 * smack_inode_setsecurity - set smack xattrs
1907 * @inode: the object
1908 * @name: attribute name
1909 * @value: attribute value
1910 * @size: size of the attribute
1911 * @flags: unused
1913 * Sets the named attribute in the appropriate blob
1915 * Returns 0 on success, or an error code
1917 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1918 const void *value, size_t size, int flags)
1920 char *sp;
1921 struct inode_smack *nsp = inode->i_security;
1922 struct socket_smack *ssp;
1923 struct socket *sock;
1924 int rc = 0;
1926 if (value == NULL || size > SMK_LABELLEN || size == 0)
1927 return -EACCES;
1929 sp = smk_import(value, size);
1930 if (sp == NULL)
1931 return -EINVAL;
1933 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1934 nsp->smk_inode = sp;
1935 nsp->smk_flags |= SMK_INODE_INSTANT;
1936 return 0;
1939 * The rest of the Smack xattrs are only on sockets.
1941 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1942 return -EOPNOTSUPP;
1944 sock = SOCKET_I(inode);
1945 if (sock == NULL || sock->sk == NULL)
1946 return -EOPNOTSUPP;
1948 ssp = sock->sk->sk_security;
1950 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1951 ssp->smk_in = sp;
1952 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1953 ssp->smk_out = sp;
1954 if (sock->sk->sk_family != PF_UNIX) {
1955 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1956 if (rc != 0)
1957 printk(KERN_WARNING
1958 "Smack: \"%s\" netlbl error %d.\n",
1959 __func__, -rc);
1961 } else
1962 return -EOPNOTSUPP;
1964 return 0;
1968 * smack_socket_post_create - finish socket setup
1969 * @sock: the socket
1970 * @family: protocol family
1971 * @type: unused
1972 * @protocol: unused
1973 * @kern: unused
1975 * Sets the netlabel information on the socket
1977 * Returns 0 on success, and error code otherwise
1979 static int smack_socket_post_create(struct socket *sock, int family,
1980 int type, int protocol, int kern)
1982 if (family != PF_INET || sock->sk == NULL)
1983 return 0;
1985 * Set the outbound netlbl.
1987 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1991 * smack_socket_connect - connect access check
1992 * @sock: the socket
1993 * @sap: the other end
1994 * @addrlen: size of sap
1996 * Verifies that a connection may be possible
1998 * Returns 0 on success, and error code otherwise
2000 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2001 int addrlen)
2003 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2004 return 0;
2005 if (addrlen < sizeof(struct sockaddr_in))
2006 return -EINVAL;
2008 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2012 * smack_flags_to_may - convert S_ to MAY_ values
2013 * @flags: the S_ value
2015 * Returns the equivalent MAY_ value
2017 static int smack_flags_to_may(int flags)
2019 int may = 0;
2021 if (flags & S_IRUGO)
2022 may |= MAY_READ;
2023 if (flags & S_IWUGO)
2024 may |= MAY_WRITE;
2025 if (flags & S_IXUGO)
2026 may |= MAY_EXEC;
2028 return may;
2032 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2033 * @msg: the object
2035 * Returns 0
2037 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2039 msg->security = smk_of_current();
2040 return 0;
2044 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2045 * @msg: the object
2047 * Clears the blob pointer
2049 static void smack_msg_msg_free_security(struct msg_msg *msg)
2051 msg->security = NULL;
2055 * smack_of_shm - the smack pointer for the shm
2056 * @shp: the object
2058 * Returns a pointer to the smack value
2060 static char *smack_of_shm(struct shmid_kernel *shp)
2062 return (char *)shp->shm_perm.security;
2066 * smack_shm_alloc_security - Set the security blob for shm
2067 * @shp: the object
2069 * Returns 0
2071 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2073 struct kern_ipc_perm *isp = &shp->shm_perm;
2075 isp->security = smk_of_current();
2076 return 0;
2080 * smack_shm_free_security - Clear the security blob for shm
2081 * @shp: the object
2083 * Clears the blob pointer
2085 static void smack_shm_free_security(struct shmid_kernel *shp)
2087 struct kern_ipc_perm *isp = &shp->shm_perm;
2089 isp->security = NULL;
2093 * smk_curacc_shm : check if current has access on shm
2094 * @shp : the object
2095 * @access : access requested
2097 * Returns 0 if current has the requested access, error code otherwise
2099 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2101 char *ssp = smack_of_shm(shp);
2102 struct smk_audit_info ad;
2104 #ifdef CONFIG_AUDIT
2105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2106 ad.a.u.ipc_id = shp->shm_perm.id;
2107 #endif
2108 return smk_curacc(ssp, access, &ad);
2112 * smack_shm_associate - Smack access check for shm
2113 * @shp: the object
2114 * @shmflg: access requested
2116 * Returns 0 if current has the requested access, error code otherwise
2118 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2120 int may;
2122 may = smack_flags_to_may(shmflg);
2123 return smk_curacc_shm(shp, may);
2127 * smack_shm_shmctl - Smack access check for shm
2128 * @shp: the object
2129 * @cmd: what it wants to do
2131 * Returns 0 if current has the requested access, error code otherwise
2133 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2135 int may;
2137 switch (cmd) {
2138 case IPC_STAT:
2139 case SHM_STAT:
2140 may = MAY_READ;
2141 break;
2142 case IPC_SET:
2143 case SHM_LOCK:
2144 case SHM_UNLOCK:
2145 case IPC_RMID:
2146 may = MAY_READWRITE;
2147 break;
2148 case IPC_INFO:
2149 case SHM_INFO:
2151 * System level information.
2153 return 0;
2154 default:
2155 return -EINVAL;
2157 return smk_curacc_shm(shp, may);
2161 * smack_shm_shmat - Smack access for shmat
2162 * @shp: the object
2163 * @shmaddr: unused
2164 * @shmflg: access requested
2166 * Returns 0 if current has the requested access, error code otherwise
2168 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2169 int shmflg)
2171 int may;
2173 may = smack_flags_to_may(shmflg);
2174 return smk_curacc_shm(shp, may);
2178 * smack_of_sem - the smack pointer for the sem
2179 * @sma: the object
2181 * Returns a pointer to the smack value
2183 static char *smack_of_sem(struct sem_array *sma)
2185 return (char *)sma->sem_perm.security;
2189 * smack_sem_alloc_security - Set the security blob for sem
2190 * @sma: the object
2192 * Returns 0
2194 static int smack_sem_alloc_security(struct sem_array *sma)
2196 struct kern_ipc_perm *isp = &sma->sem_perm;
2198 isp->security = smk_of_current();
2199 return 0;
2203 * smack_sem_free_security - Clear the security blob for sem
2204 * @sma: the object
2206 * Clears the blob pointer
2208 static void smack_sem_free_security(struct sem_array *sma)
2210 struct kern_ipc_perm *isp = &sma->sem_perm;
2212 isp->security = NULL;
2216 * smk_curacc_sem : check if current has access on sem
2217 * @sma : the object
2218 * @access : access requested
2220 * Returns 0 if current has the requested access, error code otherwise
2222 static int smk_curacc_sem(struct sem_array *sma, int access)
2224 char *ssp = smack_of_sem(sma);
2225 struct smk_audit_info ad;
2227 #ifdef CONFIG_AUDIT
2228 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2229 ad.a.u.ipc_id = sma->sem_perm.id;
2230 #endif
2231 return smk_curacc(ssp, access, &ad);
2235 * smack_sem_associate - Smack access check for sem
2236 * @sma: the object
2237 * @semflg: access requested
2239 * Returns 0 if current has the requested access, error code otherwise
2241 static int smack_sem_associate(struct sem_array *sma, int semflg)
2243 int may;
2245 may = smack_flags_to_may(semflg);
2246 return smk_curacc_sem(sma, may);
2250 * smack_sem_shmctl - Smack access check for sem
2251 * @sma: the object
2252 * @cmd: what it wants to do
2254 * Returns 0 if current has the requested access, error code otherwise
2256 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2258 int may;
2260 switch (cmd) {
2261 case GETPID:
2262 case GETNCNT:
2263 case GETZCNT:
2264 case GETVAL:
2265 case GETALL:
2266 case IPC_STAT:
2267 case SEM_STAT:
2268 may = MAY_READ;
2269 break;
2270 case SETVAL:
2271 case SETALL:
2272 case IPC_RMID:
2273 case IPC_SET:
2274 may = MAY_READWRITE;
2275 break;
2276 case IPC_INFO:
2277 case SEM_INFO:
2279 * System level information
2281 return 0;
2282 default:
2283 return -EINVAL;
2286 return smk_curacc_sem(sma, may);
2290 * smack_sem_semop - Smack checks of semaphore operations
2291 * @sma: the object
2292 * @sops: unused
2293 * @nsops: unused
2294 * @alter: unused
2296 * Treated as read and write in all cases.
2298 * Returns 0 if access is allowed, error code otherwise
2300 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2301 unsigned nsops, int alter)
2303 return smk_curacc_sem(sma, MAY_READWRITE);
2307 * smack_msg_alloc_security - Set the security blob for msg
2308 * @msq: the object
2310 * Returns 0
2312 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2314 struct kern_ipc_perm *kisp = &msq->q_perm;
2316 kisp->security = smk_of_current();
2317 return 0;
2321 * smack_msg_free_security - Clear the security blob for msg
2322 * @msq: the object
2324 * Clears the blob pointer
2326 static void smack_msg_queue_free_security(struct msg_queue *msq)
2328 struct kern_ipc_perm *kisp = &msq->q_perm;
2330 kisp->security = NULL;
2334 * smack_of_msq - the smack pointer for the msq
2335 * @msq: the object
2337 * Returns a pointer to the smack value
2339 static char *smack_of_msq(struct msg_queue *msq)
2341 return (char *)msq->q_perm.security;
2345 * smk_curacc_msq : helper to check if current has access on msq
2346 * @msq : the msq
2347 * @access : access requested
2349 * return 0 if current has access, error otherwise
2351 static int smk_curacc_msq(struct msg_queue *msq, int access)
2353 char *msp = smack_of_msq(msq);
2354 struct smk_audit_info ad;
2356 #ifdef CONFIG_AUDIT
2357 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2358 ad.a.u.ipc_id = msq->q_perm.id;
2359 #endif
2360 return smk_curacc(msp, access, &ad);
2364 * smack_msg_queue_associate - Smack access check for msg_queue
2365 * @msq: the object
2366 * @msqflg: access requested
2368 * Returns 0 if current has the requested access, error code otherwise
2370 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2372 int may;
2374 may = smack_flags_to_may(msqflg);
2375 return smk_curacc_msq(msq, may);
2379 * smack_msg_queue_msgctl - Smack access check for msg_queue
2380 * @msq: the object
2381 * @cmd: what it wants to do
2383 * Returns 0 if current has the requested access, error code otherwise
2385 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2387 int may;
2389 switch (cmd) {
2390 case IPC_STAT:
2391 case MSG_STAT:
2392 may = MAY_READ;
2393 break;
2394 case IPC_SET:
2395 case IPC_RMID:
2396 may = MAY_READWRITE;
2397 break;
2398 case IPC_INFO:
2399 case MSG_INFO:
2401 * System level information
2403 return 0;
2404 default:
2405 return -EINVAL;
2408 return smk_curacc_msq(msq, may);
2412 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2413 * @msq: the object
2414 * @msg: unused
2415 * @msqflg: access requested
2417 * Returns 0 if current has the requested access, error code otherwise
2419 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2420 int msqflg)
2422 int may;
2424 may = smack_flags_to_may(msqflg);
2425 return smk_curacc_msq(msq, may);
2429 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2430 * @msq: the object
2431 * @msg: unused
2432 * @target: unused
2433 * @type: unused
2434 * @mode: unused
2436 * Returns 0 if current has read and write access, error code otherwise
2438 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2439 struct task_struct *target, long type, int mode)
2441 return smk_curacc_msq(msq, MAY_READWRITE);
2445 * smack_ipc_permission - Smack access for ipc_permission()
2446 * @ipp: the object permissions
2447 * @flag: access requested
2449 * Returns 0 if current has read and write access, error code otherwise
2451 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2453 char *isp = ipp->security;
2454 int may = smack_flags_to_may(flag);
2455 struct smk_audit_info ad;
2457 #ifdef CONFIG_AUDIT
2458 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2459 ad.a.u.ipc_id = ipp->id;
2460 #endif
2461 return smk_curacc(isp, may, &ad);
2465 * smack_ipc_getsecid - Extract smack security id
2466 * @ipp: the object permissions
2467 * @secid: where result will be saved
2469 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2471 char *smack = ipp->security;
2473 *secid = smack_to_secid(smack);
2477 * smack_d_instantiate - Make sure the blob is correct on an inode
2478 * @opt_dentry: dentry where inode will be attached
2479 * @inode: the object
2481 * Set the inode's security blob if it hasn't been done already.
2483 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2485 struct super_block *sbp;
2486 struct superblock_smack *sbsp;
2487 struct inode_smack *isp;
2488 char *csp = smk_of_current();
2489 char *fetched;
2490 char *final;
2491 char trattr[TRANS_TRUE_SIZE];
2492 int transflag = 0;
2493 struct dentry *dp;
2495 if (inode == NULL)
2496 return;
2498 isp = inode->i_security;
2500 mutex_lock(&isp->smk_lock);
2502 * If the inode is already instantiated
2503 * take the quick way out
2505 if (isp->smk_flags & SMK_INODE_INSTANT)
2506 goto unlockandout;
2508 sbp = inode->i_sb;
2509 sbsp = sbp->s_security;
2511 * We're going to use the superblock default label
2512 * if there's no label on the file.
2514 final = sbsp->smk_default;
2517 * If this is the root inode the superblock
2518 * may be in the process of initialization.
2519 * If that is the case use the root value out
2520 * of the superblock.
2522 if (opt_dentry->d_parent == opt_dentry) {
2523 isp->smk_inode = sbsp->smk_root;
2524 isp->smk_flags |= SMK_INODE_INSTANT;
2525 goto unlockandout;
2529 * This is pretty hackish.
2530 * Casey says that we shouldn't have to do
2531 * file system specific code, but it does help
2532 * with keeping it simple.
2534 switch (sbp->s_magic) {
2535 case SMACK_MAGIC:
2537 * Casey says that it's a little embarrassing
2538 * that the smack file system doesn't do
2539 * extended attributes.
2541 final = smack_known_star.smk_known;
2542 break;
2543 case PIPEFS_MAGIC:
2545 * Casey says pipes are easy (?)
2547 final = smack_known_star.smk_known;
2548 break;
2549 case DEVPTS_SUPER_MAGIC:
2551 * devpts seems content with the label of the task.
2552 * Programs that change smack have to treat the
2553 * pty with respect.
2555 final = csp;
2556 break;
2557 case SOCKFS_MAGIC:
2559 * Socket access is controlled by the socket
2560 * structures associated with the task involved.
2562 final = smack_known_star.smk_known;
2563 break;
2564 case PROC_SUPER_MAGIC:
2566 * Casey says procfs appears not to care.
2567 * The superblock default suffices.
2569 break;
2570 case TMPFS_MAGIC:
2572 * Device labels should come from the filesystem,
2573 * but watch out, because they're volitile,
2574 * getting recreated on every reboot.
2576 final = smack_known_star.smk_known;
2578 * No break.
2580 * If a smack value has been set we want to use it,
2581 * but since tmpfs isn't giving us the opportunity
2582 * to set mount options simulate setting the
2583 * superblock default.
2585 default:
2587 * This isn't an understood special case.
2588 * Get the value from the xattr.
2592 * UNIX domain sockets use lower level socket data.
2594 if (S_ISSOCK(inode->i_mode)) {
2595 final = smack_known_star.smk_known;
2596 break;
2599 * No xattr support means, alas, no SMACK label.
2600 * Use the aforeapplied default.
2601 * It would be curious if the label of the task
2602 * does not match that assigned.
2604 if (inode->i_op->getxattr == NULL)
2605 break;
2607 * Get the dentry for xattr.
2609 dp = dget(opt_dentry);
2610 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2611 if (fetched != NULL) {
2612 final = fetched;
2613 if (S_ISDIR(inode->i_mode)) {
2614 trattr[0] = '\0';
2615 inode->i_op->getxattr(dp,
2616 XATTR_NAME_SMACKTRANSMUTE,
2617 trattr, TRANS_TRUE_SIZE);
2618 if (strncmp(trattr, TRANS_TRUE,
2619 TRANS_TRUE_SIZE) == 0)
2620 transflag = SMK_INODE_TRANSMUTE;
2623 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2624 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2626 dput(dp);
2627 break;
2630 if (final == NULL)
2631 isp->smk_inode = csp;
2632 else
2633 isp->smk_inode = final;
2635 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
2637 unlockandout:
2638 mutex_unlock(&isp->smk_lock);
2639 return;
2643 * smack_getprocattr - Smack process attribute access
2644 * @p: the object task
2645 * @name: the name of the attribute in /proc/.../attr
2646 * @value: where to put the result
2648 * Places a copy of the task Smack into value
2650 * Returns the length of the smack label or an error code
2652 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2654 char *cp;
2655 int slen;
2657 if (strcmp(name, "current") != 0)
2658 return -EINVAL;
2660 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
2661 if (cp == NULL)
2662 return -ENOMEM;
2664 slen = strlen(cp);
2665 *value = cp;
2666 return slen;
2670 * smack_setprocattr - Smack process attribute setting
2671 * @p: the object task
2672 * @name: the name of the attribute in /proc/.../attr
2673 * @value: the value to set
2674 * @size: the size of the value
2676 * Sets the Smack value of the task. Only setting self
2677 * is permitted and only with privilege
2679 * Returns the length of the smack label or an error code
2681 static int smack_setprocattr(struct task_struct *p, char *name,
2682 void *value, size_t size)
2684 int rc;
2685 struct task_smack *tsp;
2686 struct task_smack *oldtsp;
2687 struct cred *new;
2688 char *newsmack;
2691 * Changing another process' Smack value is too dangerous
2692 * and supports no sane use case.
2694 if (p != current)
2695 return -EPERM;
2697 if (!capable(CAP_MAC_ADMIN))
2698 return -EPERM;
2700 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2701 return -EINVAL;
2703 if (strcmp(name, "current") != 0)
2704 return -EINVAL;
2706 newsmack = smk_import(value, size);
2707 if (newsmack == NULL)
2708 return -EINVAL;
2711 * No process is ever allowed the web ("@") label.
2713 if (newsmack == smack_known_web.smk_known)
2714 return -EPERM;
2716 oldtsp = p->cred->security;
2717 new = prepare_creds();
2718 if (new == NULL)
2719 return -ENOMEM;
2721 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
2722 if (tsp == NULL) {
2723 kfree(new);
2724 return -ENOMEM;
2726 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2727 if (rc != 0)
2728 return rc;
2730 new->security = tsp;
2731 commit_creds(new);
2732 return size;
2736 * smack_unix_stream_connect - Smack access on UDS
2737 * @sock: one sock
2738 * @other: the other sock
2739 * @newsk: unused
2741 * Return 0 if a subject with the smack of sock could access
2742 * an object with the smack of other, otherwise an error code
2744 static int smack_unix_stream_connect(struct sock *sock,
2745 struct sock *other, struct sock *newsk)
2747 struct socket_smack *ssp = sock->sk_security;
2748 struct socket_smack *osp = other->sk_security;
2749 struct smk_audit_info ad;
2750 int rc = 0;
2752 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2753 smk_ad_setfield_u_net_sk(&ad, other);
2755 if (!capable(CAP_MAC_OVERRIDE))
2756 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2758 return rc;
2762 * smack_unix_may_send - Smack access on UDS
2763 * @sock: one socket
2764 * @other: the other socket
2766 * Return 0 if a subject with the smack of sock could access
2767 * an object with the smack of other, otherwise an error code
2769 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2771 struct socket_smack *ssp = sock->sk->sk_security;
2772 struct socket_smack *osp = other->sk->sk_security;
2773 struct smk_audit_info ad;
2774 int rc = 0;
2776 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2777 smk_ad_setfield_u_net_sk(&ad, other->sk);
2779 if (!capable(CAP_MAC_OVERRIDE))
2780 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2782 return rc;
2786 * smack_socket_sendmsg - Smack check based on destination host
2787 * @sock: the socket
2788 * @msg: the message
2789 * @size: the size of the message
2791 * Return 0 if the current subject can write to the destination
2792 * host. This is only a question if the destination is a single
2793 * label host.
2795 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2796 int size)
2798 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
2801 * Perfectly reasonable for this to be NULL
2803 if (sip == NULL || sip->sin_family != AF_INET)
2804 return 0;
2806 return smack_netlabel_send(sock->sk, sip);
2811 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
2812 * @sap: netlabel secattr
2813 * @sip: where to put the result
2815 * Copies a smack label into sip
2817 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2819 char smack[SMK_LABELLEN];
2820 char *sp;
2821 int pcat;
2823 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
2825 * Looks like a CIPSO packet.
2826 * If there are flags but no level netlabel isn't
2827 * behaving the way we expect it to.
2829 * Get the categories, if any
2830 * Without guidance regarding the smack value
2831 * for the packet fall back on the network
2832 * ambient value.
2834 memset(smack, '\0', SMK_LABELLEN);
2835 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2836 for (pcat = -1;;) {
2837 pcat = netlbl_secattr_catmap_walk(
2838 sap->attr.mls.cat, pcat + 1);
2839 if (pcat < 0)
2840 break;
2841 smack_catset_bit(pcat, smack);
2844 * If it is CIPSO using smack direct mapping
2845 * we are already done. WeeHee.
2847 if (sap->attr.mls.lvl == smack_cipso_direct) {
2848 memcpy(sip, smack, SMK_MAXLEN);
2849 return;
2852 * Look it up in the supplied table if it is not
2853 * a direct mapping.
2855 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2856 return;
2858 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2860 * Looks like a fallback, which gives us a secid.
2862 sp = smack_from_secid(sap->attr.secid);
2864 * This has got to be a bug because it is
2865 * impossible to specify a fallback without
2866 * specifying the label, which will ensure
2867 * it has a secid, and the only way to get a
2868 * secid is from a fallback.
2870 BUG_ON(sp == NULL);
2871 strncpy(sip, sp, SMK_MAXLEN);
2872 return;
2875 * Without guidance regarding the smack value
2876 * for the packet fall back on the network
2877 * ambient value.
2879 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2880 return;
2884 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2885 * @sk: socket
2886 * @skb: packet
2888 * Returns 0 if the packet should be delivered, an error code otherwise
2890 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2892 struct netlbl_lsm_secattr secattr;
2893 struct socket_smack *ssp = sk->sk_security;
2894 char smack[SMK_LABELLEN];
2895 char *csp;
2896 int rc;
2897 struct smk_audit_info ad;
2898 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2899 return 0;
2902 * Translate what netlabel gave us.
2904 netlbl_secattr_init(&secattr);
2906 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2907 if (rc == 0) {
2908 smack_from_secattr(&secattr, smack);
2909 csp = smack;
2910 } else
2911 csp = smack_net_ambient;
2913 netlbl_secattr_destroy(&secattr);
2915 #ifdef CONFIG_AUDIT
2916 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2917 ad.a.u.net.family = sk->sk_family;
2918 ad.a.u.net.netif = skb->skb_iif;
2919 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2920 #endif
2922 * Receiving a packet requires that the other end
2923 * be able to write here. Read access is not required.
2924 * This is the simplist possible security model
2925 * for networking.
2927 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
2928 if (rc != 0)
2929 netlbl_skbuff_err(skb, rc, 0);
2930 return rc;
2934 * smack_socket_getpeersec_stream - pull in packet label
2935 * @sock: the socket
2936 * @optval: user's destination
2937 * @optlen: size thereof
2938 * @len: max thereof
2940 * returns zero on success, an error code otherwise
2942 static int smack_socket_getpeersec_stream(struct socket *sock,
2943 char __user *optval,
2944 int __user *optlen, unsigned len)
2946 struct socket_smack *ssp;
2947 int slen;
2948 int rc = 0;
2950 ssp = sock->sk->sk_security;
2951 slen = strlen(ssp->smk_packet) + 1;
2953 if (slen > len)
2954 rc = -ERANGE;
2955 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2956 rc = -EFAULT;
2958 if (put_user(slen, optlen) != 0)
2959 rc = -EFAULT;
2961 return rc;
2966 * smack_socket_getpeersec_dgram - pull in packet label
2967 * @sock: the peer socket
2968 * @skb: packet data
2969 * @secid: pointer to where to put the secid of the packet
2971 * Sets the netlabel socket state on sk from parent
2973 static int smack_socket_getpeersec_dgram(struct socket *sock,
2974 struct sk_buff *skb, u32 *secid)
2977 struct netlbl_lsm_secattr secattr;
2978 struct socket_smack *sp;
2979 char smack[SMK_LABELLEN];
2980 int family = PF_UNSPEC;
2981 u32 s = 0; /* 0 is the invalid secid */
2982 int rc;
2984 if (skb != NULL) {
2985 if (skb->protocol == htons(ETH_P_IP))
2986 family = PF_INET;
2987 else if (skb->protocol == htons(ETH_P_IPV6))
2988 family = PF_INET6;
2990 if (family == PF_UNSPEC && sock != NULL)
2991 family = sock->sk->sk_family;
2993 if (family == PF_UNIX) {
2994 sp = sock->sk->sk_security;
2995 s = smack_to_secid(sp->smk_out);
2996 } else if (family == PF_INET || family == PF_INET6) {
2998 * Translate what netlabel gave us.
3000 netlbl_secattr_init(&secattr);
3001 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3002 if (rc == 0) {
3003 smack_from_secattr(&secattr, smack);
3004 s = smack_to_secid(smack);
3006 netlbl_secattr_destroy(&secattr);
3008 *secid = s;
3009 if (s == 0)
3010 return -EINVAL;
3011 return 0;
3015 * smack_sock_graft - Initialize a newly created socket with an existing sock
3016 * @sk: child sock
3017 * @parent: parent socket
3019 * Set the smk_{in,out} state of an existing sock based on the process that
3020 * is creating the new socket.
3022 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3024 struct socket_smack *ssp;
3026 if (sk == NULL ||
3027 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3028 return;
3030 ssp = sk->sk_security;
3031 ssp->smk_in = ssp->smk_out = smk_of_current();
3032 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3036 * smack_inet_conn_request - Smack access check on connect
3037 * @sk: socket involved
3038 * @skb: packet
3039 * @req: unused
3041 * Returns 0 if a task with the packet label could write to
3042 * the socket, otherwise an error code
3044 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3045 struct request_sock *req)
3047 u16 family = sk->sk_family;
3048 struct socket_smack *ssp = sk->sk_security;
3049 struct netlbl_lsm_secattr secattr;
3050 struct sockaddr_in addr;
3051 struct iphdr *hdr;
3052 char smack[SMK_LABELLEN];
3053 int rc;
3054 struct smk_audit_info ad;
3056 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3057 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3058 family = PF_INET;
3060 netlbl_secattr_init(&secattr);
3061 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3062 if (rc == 0)
3063 smack_from_secattr(&secattr, smack);
3064 else
3065 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
3066 netlbl_secattr_destroy(&secattr);
3068 #ifdef CONFIG_AUDIT
3069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3070 ad.a.u.net.family = family;
3071 ad.a.u.net.netif = skb->skb_iif;
3072 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3073 #endif
3075 * Receiving a packet requires that the other end be able to write
3076 * here. Read access is not required.
3078 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
3079 if (rc != 0)
3080 return rc;
3083 * Save the peer's label in the request_sock so we can later setup
3084 * smk_packet in the child socket so that SO_PEERCRED can report it.
3086 req->peer_secid = smack_to_secid(smack);
3089 * We need to decide if we want to label the incoming connection here
3090 * if we do we only need to label the request_sock and the stack will
3091 * propagate the wire-label to the sock when it is created.
3093 hdr = ip_hdr(skb);
3094 addr.sin_addr.s_addr = hdr->saddr;
3095 rcu_read_lock();
3096 if (smack_host_label(&addr) == NULL) {
3097 rcu_read_unlock();
3098 netlbl_secattr_init(&secattr);
3099 smack_to_secattr(smack, &secattr);
3100 rc = netlbl_req_setattr(req, &secattr);
3101 netlbl_secattr_destroy(&secattr);
3102 } else {
3103 rcu_read_unlock();
3104 netlbl_req_delattr(req);
3107 return rc;
3111 * smack_inet_csk_clone - Copy the connection information to the new socket
3112 * @sk: the new socket
3113 * @req: the connection's request_sock
3115 * Transfer the connection's peer label to the newly created socket.
3117 static void smack_inet_csk_clone(struct sock *sk,
3118 const struct request_sock *req)
3120 struct socket_smack *ssp = sk->sk_security;
3121 char *smack;
3123 if (req->peer_secid != 0) {
3124 smack = smack_from_secid(req->peer_secid);
3125 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
3126 } else
3127 ssp->smk_packet[0] = '\0';
3131 * Key management security hooks
3133 * Casey has not tested key support very heavily.
3134 * The permission check is most likely too restrictive.
3135 * If you care about keys please have a look.
3137 #ifdef CONFIG_KEYS
3140 * smack_key_alloc - Set the key security blob
3141 * @key: object
3142 * @cred: the credentials to use
3143 * @flags: unused
3145 * No allocation required
3147 * Returns 0
3149 static int smack_key_alloc(struct key *key, const struct cred *cred,
3150 unsigned long flags)
3152 key->security = smk_of_task(cred->security);
3153 return 0;
3157 * smack_key_free - Clear the key security blob
3158 * @key: the object
3160 * Clear the blob pointer
3162 static void smack_key_free(struct key *key)
3164 key->security = NULL;
3168 * smack_key_permission - Smack access on a key
3169 * @key_ref: gets to the object
3170 * @cred: the credentials to use
3171 * @perm: unused
3173 * Return 0 if the task has read and write to the object,
3174 * an error code otherwise
3176 static int smack_key_permission(key_ref_t key_ref,
3177 const struct cred *cred, key_perm_t perm)
3179 struct key *keyp;
3180 struct smk_audit_info ad;
3181 char *tsp = smk_of_task(cred->security);
3183 keyp = key_ref_to_ptr(key_ref);
3184 if (keyp == NULL)
3185 return -EINVAL;
3187 * If the key hasn't been initialized give it access so that
3188 * it may do so.
3190 if (keyp->security == NULL)
3191 return 0;
3193 * This should not occur
3195 if (tsp == NULL)
3196 return -EACCES;
3197 #ifdef CONFIG_AUDIT
3198 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3199 ad.a.u.key_struct.key = keyp->serial;
3200 ad.a.u.key_struct.key_desc = keyp->description;
3201 #endif
3202 return smk_access(tsp, keyp->security,
3203 MAY_READWRITE, &ad);
3205 #endif /* CONFIG_KEYS */
3208 * Smack Audit hooks
3210 * Audit requires a unique representation of each Smack specific
3211 * rule. This unique representation is used to distinguish the
3212 * object to be audited from remaining kernel objects and also
3213 * works as a glue between the audit hooks.
3215 * Since repository entries are added but never deleted, we'll use
3216 * the smack_known label address related to the given audit rule as
3217 * the needed unique representation. This also better fits the smack
3218 * model where nearly everything is a label.
3220 #ifdef CONFIG_AUDIT
3223 * smack_audit_rule_init - Initialize a smack audit rule
3224 * @field: audit rule fields given from user-space (audit.h)
3225 * @op: required testing operator (=, !=, >, <, ...)
3226 * @rulestr: smack label to be audited
3227 * @vrule: pointer to save our own audit rule representation
3229 * Prepare to audit cases where (@field @op @rulestr) is true.
3230 * The label to be audited is created if necessay.
3232 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3234 char **rule = (char **)vrule;
3235 *rule = NULL;
3237 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3238 return -EINVAL;
3240 if (op != Audit_equal && op != Audit_not_equal)
3241 return -EINVAL;
3243 *rule = smk_import(rulestr, 0);
3245 return 0;
3249 * smack_audit_rule_known - Distinguish Smack audit rules
3250 * @krule: rule of interest, in Audit kernel representation format
3252 * This is used to filter Smack rules from remaining Audit ones.
3253 * If it's proved that this rule belongs to us, the
3254 * audit_rule_match hook will be called to do the final judgement.
3256 static int smack_audit_rule_known(struct audit_krule *krule)
3258 struct audit_field *f;
3259 int i;
3261 for (i = 0; i < krule->field_count; i++) {
3262 f = &krule->fields[i];
3264 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3265 return 1;
3268 return 0;
3272 * smack_audit_rule_match - Audit given object ?
3273 * @secid: security id for identifying the object to test
3274 * @field: audit rule flags given from user-space
3275 * @op: required testing operator
3276 * @vrule: smack internal rule presentation
3277 * @actx: audit context associated with the check
3279 * The core Audit hook. It's used to take the decision of
3280 * whether to audit or not to audit a given object.
3282 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3283 struct audit_context *actx)
3285 char *smack;
3286 char *rule = vrule;
3288 if (!rule) {
3289 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3290 "Smack: missing rule\n");
3291 return -ENOENT;
3294 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3295 return 0;
3297 smack = smack_from_secid(secid);
3300 * No need to do string comparisons. If a match occurs,
3301 * both pointers will point to the same smack_known
3302 * label.
3304 if (op == Audit_equal)
3305 return (rule == smack);
3306 if (op == Audit_not_equal)
3307 return (rule != smack);
3309 return 0;
3313 * smack_audit_rule_free - free smack rule representation
3314 * @vrule: rule to be freed.
3316 * No memory was allocated.
3318 static void smack_audit_rule_free(void *vrule)
3320 /* No-op */
3323 #endif /* CONFIG_AUDIT */
3326 * smack_secid_to_secctx - return the smack label for a secid
3327 * @secid: incoming integer
3328 * @secdata: destination
3329 * @seclen: how long it is
3331 * Exists for networking code.
3333 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3335 char *sp = smack_from_secid(secid);
3337 if (secdata)
3338 *secdata = sp;
3339 *seclen = strlen(sp);
3340 return 0;
3344 * smack_secctx_to_secid - return the secid for a smack label
3345 * @secdata: smack label
3346 * @seclen: how long result is
3347 * @secid: outgoing integer
3349 * Exists for audit and networking code.
3351 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
3353 *secid = smack_to_secid(secdata);
3354 return 0;
3358 * smack_release_secctx - don't do anything.
3359 * @secdata: unused
3360 * @seclen: unused
3362 * Exists to make sure nothing gets done, and properly
3364 static void smack_release_secctx(char *secdata, u32 seclen)
3368 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3370 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3373 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3375 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3378 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3380 int len = 0;
3381 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3383 if (len < 0)
3384 return len;
3385 *ctxlen = len;
3386 return 0;
3389 struct security_operations smack_ops = {
3390 .name = "smack",
3392 .ptrace_access_check = smack_ptrace_access_check,
3393 .ptrace_traceme = smack_ptrace_traceme,
3394 .syslog = smack_syslog,
3396 .sb_alloc_security = smack_sb_alloc_security,
3397 .sb_free_security = smack_sb_free_security,
3398 .sb_copy_data = smack_sb_copy_data,
3399 .sb_kern_mount = smack_sb_kern_mount,
3400 .sb_statfs = smack_sb_statfs,
3401 .sb_mount = smack_sb_mount,
3402 .sb_umount = smack_sb_umount,
3404 .bprm_set_creds = smack_bprm_set_creds,
3406 .inode_alloc_security = smack_inode_alloc_security,
3407 .inode_free_security = smack_inode_free_security,
3408 .inode_init_security = smack_inode_init_security,
3409 .inode_link = smack_inode_link,
3410 .inode_unlink = smack_inode_unlink,
3411 .inode_rmdir = smack_inode_rmdir,
3412 .inode_rename = smack_inode_rename,
3413 .inode_permission = smack_inode_permission,
3414 .inode_setattr = smack_inode_setattr,
3415 .inode_getattr = smack_inode_getattr,
3416 .inode_setxattr = smack_inode_setxattr,
3417 .inode_post_setxattr = smack_inode_post_setxattr,
3418 .inode_getxattr = smack_inode_getxattr,
3419 .inode_removexattr = smack_inode_removexattr,
3420 .inode_getsecurity = smack_inode_getsecurity,
3421 .inode_setsecurity = smack_inode_setsecurity,
3422 .inode_listsecurity = smack_inode_listsecurity,
3423 .inode_getsecid = smack_inode_getsecid,
3425 .file_permission = smack_file_permission,
3426 .file_alloc_security = smack_file_alloc_security,
3427 .file_free_security = smack_file_free_security,
3428 .file_ioctl = smack_file_ioctl,
3429 .file_lock = smack_file_lock,
3430 .file_fcntl = smack_file_fcntl,
3431 .file_mmap = smack_file_mmap,
3432 .file_set_fowner = smack_file_set_fowner,
3433 .file_send_sigiotask = smack_file_send_sigiotask,
3434 .file_receive = smack_file_receive,
3436 .cred_alloc_blank = smack_cred_alloc_blank,
3437 .cred_free = smack_cred_free,
3438 .cred_prepare = smack_cred_prepare,
3439 .cred_transfer = smack_cred_transfer,
3440 .kernel_act_as = smack_kernel_act_as,
3441 .kernel_create_files_as = smack_kernel_create_files_as,
3442 .task_setpgid = smack_task_setpgid,
3443 .task_getpgid = smack_task_getpgid,
3444 .task_getsid = smack_task_getsid,
3445 .task_getsecid = smack_task_getsecid,
3446 .task_setnice = smack_task_setnice,
3447 .task_setioprio = smack_task_setioprio,
3448 .task_getioprio = smack_task_getioprio,
3449 .task_setscheduler = smack_task_setscheduler,
3450 .task_getscheduler = smack_task_getscheduler,
3451 .task_movememory = smack_task_movememory,
3452 .task_kill = smack_task_kill,
3453 .task_wait = smack_task_wait,
3454 .task_to_inode = smack_task_to_inode,
3456 .ipc_permission = smack_ipc_permission,
3457 .ipc_getsecid = smack_ipc_getsecid,
3459 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3460 .msg_msg_free_security = smack_msg_msg_free_security,
3462 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3463 .msg_queue_free_security = smack_msg_queue_free_security,
3464 .msg_queue_associate = smack_msg_queue_associate,
3465 .msg_queue_msgctl = smack_msg_queue_msgctl,
3466 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3467 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3469 .shm_alloc_security = smack_shm_alloc_security,
3470 .shm_free_security = smack_shm_free_security,
3471 .shm_associate = smack_shm_associate,
3472 .shm_shmctl = smack_shm_shmctl,
3473 .shm_shmat = smack_shm_shmat,
3475 .sem_alloc_security = smack_sem_alloc_security,
3476 .sem_free_security = smack_sem_free_security,
3477 .sem_associate = smack_sem_associate,
3478 .sem_semctl = smack_sem_semctl,
3479 .sem_semop = smack_sem_semop,
3481 .d_instantiate = smack_d_instantiate,
3483 .getprocattr = smack_getprocattr,
3484 .setprocattr = smack_setprocattr,
3486 .unix_stream_connect = smack_unix_stream_connect,
3487 .unix_may_send = smack_unix_may_send,
3489 .socket_post_create = smack_socket_post_create,
3490 .socket_connect = smack_socket_connect,
3491 .socket_sendmsg = smack_socket_sendmsg,
3492 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3493 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3494 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3495 .sk_alloc_security = smack_sk_alloc_security,
3496 .sk_free_security = smack_sk_free_security,
3497 .sock_graft = smack_sock_graft,
3498 .inet_conn_request = smack_inet_conn_request,
3499 .inet_csk_clone = smack_inet_csk_clone,
3501 /* key management security hooks */
3502 #ifdef CONFIG_KEYS
3503 .key_alloc = smack_key_alloc,
3504 .key_free = smack_key_free,
3505 .key_permission = smack_key_permission,
3506 #endif /* CONFIG_KEYS */
3508 /* Audit hooks */
3509 #ifdef CONFIG_AUDIT
3510 .audit_rule_init = smack_audit_rule_init,
3511 .audit_rule_known = smack_audit_rule_known,
3512 .audit_rule_match = smack_audit_rule_match,
3513 .audit_rule_free = smack_audit_rule_free,
3514 #endif /* CONFIG_AUDIT */
3516 .secid_to_secctx = smack_secid_to_secctx,
3517 .secctx_to_secid = smack_secctx_to_secid,
3518 .release_secctx = smack_release_secctx,
3519 .inode_notifysecctx = smack_inode_notifysecctx,
3520 .inode_setsecctx = smack_inode_setsecctx,
3521 .inode_getsecctx = smack_inode_getsecctx,
3525 static __init void init_smack_know_list(void)
3527 list_add(&smack_known_huh.list, &smack_known_list);
3528 list_add(&smack_known_hat.list, &smack_known_list);
3529 list_add(&smack_known_star.list, &smack_known_list);
3530 list_add(&smack_known_floor.list, &smack_known_list);
3531 list_add(&smack_known_invalid.list, &smack_known_list);
3532 list_add(&smack_known_web.list, &smack_known_list);
3536 * smack_init - initialize the smack system
3538 * Returns 0
3540 static __init int smack_init(void)
3542 struct cred *cred;
3543 struct task_smack *tsp;
3545 if (!security_module_enable(&smack_ops))
3546 return 0;
3548 tsp = new_task_smack(smack_known_floor.smk_known,
3549 smack_known_floor.smk_known, GFP_KERNEL);
3550 if (tsp == NULL)
3551 return -ENOMEM;
3553 printk(KERN_INFO "Smack: Initializing.\n");
3556 * Set the security state for the initial task.
3558 cred = (struct cred *) current->cred;
3559 cred->security = tsp;
3561 /* initialize the smack_know_list */
3562 init_smack_know_list();
3564 * Initialize locks
3566 spin_lock_init(&smack_known_huh.smk_cipsolock);
3567 spin_lock_init(&smack_known_hat.smk_cipsolock);
3568 spin_lock_init(&smack_known_star.smk_cipsolock);
3569 spin_lock_init(&smack_known_floor.smk_cipsolock);
3570 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3573 * Register with LSM
3575 if (register_security(&smack_ops))
3576 panic("smack: Unable to register with kernel.\n");
3578 return 0;
3582 * Smack requires early initialization in order to label
3583 * all processes and objects when they are created.
3585 security_initcall(smack_init);