]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - security/smack/smack_lsm.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6-omap-h63xx.git] / security / smack / smack_lsm.c
1 /*
2  *  Simplified MAC Kernel (smack) security module
3  *
4  *  This file contains the smack hook function implementations.
5  *
6  *  Author:
7  *      Casey Schaufler <casey@schaufler-ca.com>
8  *
9  *  Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
10  *
11  *      This program is free software; you can redistribute it and/or modify
12  *      it under the terms of the GNU General Public License version 2,
13  *      as published by the Free Software Foundation.
14  */
15
16 #include <linux/xattr.h>
17 #include <linux/pagemap.h>
18 #include <linux/mount.h>
19 #include <linux/stat.h>
20 #include <linux/ext2_fs.h>
21 #include <linux/kd.h>
22 #include <asm/ioctls.h>
23 #include <linux/tcp.h>
24 #include <linux/udp.h>
25 #include <linux/mutex.h>
26 #include <linux/pipe_fs_i.h>
27 #include <net/netlabel.h>
28 #include <net/cipso_ipv4.h>
29
30 #include "smack.h"
31
32 /*
33  * I hope these are the hokeyist lines of code in the module. Casey.
34  */
35 #define DEVPTS_SUPER_MAGIC      0x1cd1
36 #define SOCKFS_MAGIC            0x534F434B
37 #define TMPFS_MAGIC             0x01021994
38
39 /**
40  * smk_fetch - Fetch the smack label from a file.
41  * @ip: a pointer to the inode
42  * @dp: a pointer to the dentry
43  *
44  * Returns a pointer to the master list entry for the Smack label
45  * or NULL if there was no label to fetch.
46  */
47 static char *smk_fetch(struct inode *ip, struct dentry *dp)
48 {
49         int rc;
50         char in[SMK_LABELLEN];
51
52         if (ip->i_op->getxattr == NULL)
53                 return NULL;
54
55         rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
56         if (rc < 0)
57                 return NULL;
58
59         return smk_import(in, rc);
60 }
61
62 /**
63  * new_inode_smack - allocate an inode security blob
64  * @smack: a pointer to the Smack label to use in the blob
65  *
66  * Returns the new blob or NULL if there's no memory available
67  */
68 struct inode_smack *new_inode_smack(char *smack)
69 {
70         struct inode_smack *isp;
71
72         isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
73         if (isp == NULL)
74                 return NULL;
75
76         isp->smk_inode = smack;
77         isp->smk_flags = 0;
78         mutex_init(&isp->smk_lock);
79
80         return isp;
81 }
82
83 /*
84  * LSM hooks.
85  * We he, that is fun!
86  */
87
88 /**
89  * smack_ptrace - Smack approval on ptrace
90  * @ptp: parent task pointer
91  * @ctp: child task pointer
92  *
93  * Returns 0 if access is OK, an error code otherwise
94  *
95  * Do the capability checks, and require read and write.
96  */
97 static int smack_ptrace(struct task_struct *ptp, struct task_struct *ctp)
98 {
99         int rc;
100
101         rc = cap_ptrace(ptp, ctp);
102         if (rc != 0)
103                 return rc;
104
105         rc = smk_access(ptp->security, ctp->security, MAY_READWRITE);
106         if (rc != 0 && __capable(ptp, CAP_MAC_OVERRIDE))
107                 return 0;
108
109         return rc;
110 }
111
112 /**
113  * smack_syslog - Smack approval on syslog
114  * @type: message type
115  *
116  * Require that the task has the floor label
117  *
118  * Returns 0 on success, error code otherwise.
119  */
120 static int smack_syslog(int type)
121 {
122         int rc;
123         char *sp = current->security;
124
125         rc = cap_syslog(type);
126         if (rc != 0)
127                 return rc;
128
129         if (capable(CAP_MAC_OVERRIDE))
130                 return 0;
131
132          if (sp != smack_known_floor.smk_known)
133                 rc = -EACCES;
134
135         return rc;
136 }
137
138
139 /*
140  * Superblock Hooks.
141  */
142
143 /**
144  * smack_sb_alloc_security - allocate a superblock blob
145  * @sb: the superblock getting the blob
146  *
147  * Returns 0 on success or -ENOMEM on error.
148  */
149 static int smack_sb_alloc_security(struct super_block *sb)
150 {
151         struct superblock_smack *sbsp;
152
153         sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
154
155         if (sbsp == NULL)
156                 return -ENOMEM;
157
158         sbsp->smk_root = smack_known_floor.smk_known;
159         sbsp->smk_default = smack_known_floor.smk_known;
160         sbsp->smk_floor = smack_known_floor.smk_known;
161         sbsp->smk_hat = smack_known_hat.smk_known;
162         sbsp->smk_initialized = 0;
163         spin_lock_init(&sbsp->smk_sblock);
164
165         sb->s_security = sbsp;
166
167         return 0;
168 }
169
170 /**
171  * smack_sb_free_security - free a superblock blob
172  * @sb: the superblock getting the blob
173  *
174  */
175 static void smack_sb_free_security(struct super_block *sb)
176 {
177         kfree(sb->s_security);
178         sb->s_security = NULL;
179 }
180
181 /**
182  * smack_sb_copy_data - copy mount options data for processing
183  * @type: file system type
184  * @orig: where to start
185  * @smackopts
186  *
187  * Returns 0 on success or -ENOMEM on error.
188  *
189  * Copy the Smack specific mount options out of the mount
190  * options list.
191  */
192 static int smack_sb_copy_data(struct file_system_type *type, void *orig,
193                               void *smackopts)
194 {
195         char *cp, *commap, *otheropts, *dp;
196
197         /* Binary mount data: just copy */
198         if (type->fs_flags & FS_BINARY_MOUNTDATA) {
199                 copy_page(smackopts, orig);
200                 return 0;
201         }
202
203         otheropts = (char *)get_zeroed_page(GFP_KERNEL);
204         if (otheropts == NULL)
205                 return -ENOMEM;
206
207         for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
208                 if (strstr(cp, SMK_FSDEFAULT) == cp)
209                         dp = smackopts;
210                 else if (strstr(cp, SMK_FSFLOOR) == cp)
211                         dp = smackopts;
212                 else if (strstr(cp, SMK_FSHAT) == cp)
213                         dp = smackopts;
214                 else if (strstr(cp, SMK_FSROOT) == cp)
215                         dp = smackopts;
216                 else
217                         dp = otheropts;
218
219                 commap = strchr(cp, ',');
220                 if (commap != NULL)
221                         *commap = '\0';
222
223                 if (*dp != '\0')
224                         strcat(dp, ",");
225                 strcat(dp, cp);
226         }
227
228         strcpy(orig, otheropts);
229         free_page((unsigned long)otheropts);
230
231         return 0;
232 }
233
234 /**
235  * smack_sb_kern_mount - Smack specific mount processing
236  * @sb: the file system superblock
237  * @data: the smack mount options
238  *
239  * Returns 0 on success, an error code on failure
240  */
241 static int smack_sb_kern_mount(struct super_block *sb, void *data)
242 {
243         struct dentry *root = sb->s_root;
244         struct inode *inode = root->d_inode;
245         struct superblock_smack *sp = sb->s_security;
246         struct inode_smack *isp;
247         char *op;
248         char *commap;
249         char *nsp;
250
251         spin_lock(&sp->smk_sblock);
252         if (sp->smk_initialized != 0) {
253                 spin_unlock(&sp->smk_sblock);
254                 return 0;
255         }
256         sp->smk_initialized = 1;
257         spin_unlock(&sp->smk_sblock);
258
259         for (op = data; op != NULL; op = commap) {
260                 commap = strchr(op, ',');
261                 if (commap != NULL)
262                         *commap++ = '\0';
263
264                 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
265                         op += strlen(SMK_FSHAT);
266                         nsp = smk_import(op, 0);
267                         if (nsp != NULL)
268                                 sp->smk_hat = nsp;
269                 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
270                         op += strlen(SMK_FSFLOOR);
271                         nsp = smk_import(op, 0);
272                         if (nsp != NULL)
273                                 sp->smk_floor = nsp;
274                 } else if (strncmp(op, SMK_FSDEFAULT,
275                                    strlen(SMK_FSDEFAULT)) == 0) {
276                         op += strlen(SMK_FSDEFAULT);
277                         nsp = smk_import(op, 0);
278                         if (nsp != NULL)
279                                 sp->smk_default = nsp;
280                 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
281                         op += strlen(SMK_FSROOT);
282                         nsp = smk_import(op, 0);
283                         if (nsp != NULL)
284                                 sp->smk_root = nsp;
285                 }
286         }
287
288         /*
289          * Initialize the root inode.
290          */
291         isp = inode->i_security;
292         if (isp == NULL)
293                 inode->i_security = new_inode_smack(sp->smk_root);
294         else
295                 isp->smk_inode = sp->smk_root;
296
297         return 0;
298 }
299
300 /**
301  * smack_sb_statfs - Smack check on statfs
302  * @dentry: identifies the file system in question
303  *
304  * Returns 0 if current can read the floor of the filesystem,
305  * and error code otherwise
306  */
307 static int smack_sb_statfs(struct dentry *dentry)
308 {
309         struct superblock_smack *sbp = dentry->d_sb->s_security;
310
311         return smk_curacc(sbp->smk_floor, MAY_READ);
312 }
313
314 /**
315  * smack_sb_mount - Smack check for mounting
316  * @dev_name: unused
317  * @nd: mount point
318  * @type: unused
319  * @flags: unused
320  * @data: unused
321  *
322  * Returns 0 if current can write the floor of the filesystem
323  * being mounted on, an error code otherwise.
324  */
325 static int smack_sb_mount(char *dev_name, struct nameidata *nd,
326                           char *type, unsigned long flags, void *data)
327 {
328         struct superblock_smack *sbp = nd->path.mnt->mnt_sb->s_security;
329
330         return smk_curacc(sbp->smk_floor, MAY_WRITE);
331 }
332
333 /**
334  * smack_sb_umount - Smack check for unmounting
335  * @mnt: file system to unmount
336  * @flags: unused
337  *
338  * Returns 0 if current can write the floor of the filesystem
339  * being unmounted, an error code otherwise.
340  */
341 static int smack_sb_umount(struct vfsmount *mnt, int flags)
342 {
343         struct superblock_smack *sbp;
344
345         sbp = mnt->mnt_sb->s_security;
346
347         return smk_curacc(sbp->smk_floor, MAY_WRITE);
348 }
349
350 /*
351  * Inode hooks
352  */
353
354 /**
355  * smack_inode_alloc_security - allocate an inode blob
356  * @inode - the inode in need of a blob
357  *
358  * Returns 0 if it gets a blob, -ENOMEM otherwise
359  */
360 static int smack_inode_alloc_security(struct inode *inode)
361 {
362         inode->i_security = new_inode_smack(current->security);
363         if (inode->i_security == NULL)
364                 return -ENOMEM;
365         return 0;
366 }
367
368 /**
369  * smack_inode_free_security - free an inode blob
370  * @inode - the inode with a blob
371  *
372  * Clears the blob pointer in inode
373  */
374 static void smack_inode_free_security(struct inode *inode)
375 {
376         kfree(inode->i_security);
377         inode->i_security = NULL;
378 }
379
380 /**
381  * smack_inode_init_security - copy out the smack from an inode
382  * @inode: the inode
383  * @dir: unused
384  * @name: where to put the attribute name
385  * @value: where to put the attribute value
386  * @len: where to put the length of the attribute
387  *
388  * Returns 0 if it all works out, -ENOMEM if there's no memory
389  */
390 static int smack_inode_init_security(struct inode *inode, struct inode *dir,
391                                      char **name, void **value, size_t *len)
392 {
393         char *isp = smk_of_inode(inode);
394
395         if (name) {
396                 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
397                 if (*name == NULL)
398                         return -ENOMEM;
399         }
400
401         if (value) {
402                 *value = kstrdup(isp, GFP_KERNEL);
403                 if (*value == NULL)
404                         return -ENOMEM;
405         }
406
407         if (len)
408                 *len = strlen(isp) + 1;
409
410         return 0;
411 }
412
413 /**
414  * smack_inode_link - Smack check on link
415  * @old_dentry: the existing object
416  * @dir: unused
417  * @new_dentry: the new object
418  *
419  * Returns 0 if access is permitted, an error code otherwise
420  */
421 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
422                             struct dentry *new_dentry)
423 {
424         int rc;
425         char *isp;
426
427         isp = smk_of_inode(old_dentry->d_inode);
428         rc = smk_curacc(isp, MAY_WRITE);
429
430         if (rc == 0 && new_dentry->d_inode != NULL) {
431                 isp = smk_of_inode(new_dentry->d_inode);
432                 rc = smk_curacc(isp, MAY_WRITE);
433         }
434
435         return rc;
436 }
437
438 /**
439  * smack_inode_unlink - Smack check on inode deletion
440  * @dir: containing directory object
441  * @dentry: file to unlink
442  *
443  * Returns 0 if current can write the containing directory
444  * and the object, error code otherwise
445  */
446 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
447 {
448         struct inode *ip = dentry->d_inode;
449         int rc;
450
451         /*
452          * You need write access to the thing you're unlinking
453          */
454         rc = smk_curacc(smk_of_inode(ip), MAY_WRITE);
455         if (rc == 0)
456                 /*
457                  * You also need write access to the containing directory
458                  */
459                 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
460
461         return rc;
462 }
463
464 /**
465  * smack_inode_rmdir - Smack check on directory deletion
466  * @dir: containing directory object
467  * @dentry: directory to unlink
468  *
469  * Returns 0 if current can write the containing directory
470  * and the directory, error code otherwise
471  */
472 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
473 {
474         int rc;
475
476         /*
477          * You need write access to the thing you're removing
478          */
479         rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
480         if (rc == 0)
481                 /*
482                  * You also need write access to the containing directory
483                  */
484                 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE);
485
486         return rc;
487 }
488
489 /**
490  * smack_inode_rename - Smack check on rename
491  * @old_inode: the old directory
492  * @old_dentry: unused
493  * @new_inode: the new directory
494  * @new_dentry: unused
495  *
496  * Read and write access is required on both the old and
497  * new directories.
498  *
499  * Returns 0 if access is permitted, an error code otherwise
500  */
501 static int smack_inode_rename(struct inode *old_inode,
502                               struct dentry *old_dentry,
503                               struct inode *new_inode,
504                               struct dentry *new_dentry)
505 {
506         int rc;
507         char *isp;
508
509         isp = smk_of_inode(old_dentry->d_inode);
510         rc = smk_curacc(isp, MAY_READWRITE);
511
512         if (rc == 0 && new_dentry->d_inode != NULL) {
513                 isp = smk_of_inode(new_dentry->d_inode);
514                 rc = smk_curacc(isp, MAY_READWRITE);
515         }
516
517         return rc;
518 }
519
520 /**
521  * smack_inode_permission - Smack version of permission()
522  * @inode: the inode in question
523  * @mask: the access requested
524  * @nd: unused
525  *
526  * This is the important Smack hook.
527  *
528  * Returns 0 if access is permitted, -EACCES otherwise
529  */
530 static int smack_inode_permission(struct inode *inode, int mask,
531                                   struct nameidata *nd)
532 {
533         /*
534          * No permission to check. Existence test. Yup, it's there.
535          */
536         if (mask == 0)
537                 return 0;
538
539         return smk_curacc(smk_of_inode(inode), mask);
540 }
541
542 /**
543  * smack_inode_setattr - Smack check for setting attributes
544  * @dentry: the object
545  * @iattr: for the force flag
546  *
547  * Returns 0 if access is permitted, an error code otherwise
548  */
549 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
550 {
551         /*
552          * Need to allow for clearing the setuid bit.
553          */
554         if (iattr->ia_valid & ATTR_FORCE)
555                 return 0;
556
557         return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
558 }
559
560 /**
561  * smack_inode_getattr - Smack check for getting attributes
562  * @mnt: unused
563  * @dentry: the object
564  *
565  * Returns 0 if access is permitted, an error code otherwise
566  */
567 static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
568 {
569         return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
570 }
571
572 /**
573  * smack_inode_setxattr - Smack check for setting xattrs
574  * @dentry: the object
575  * @name: name of the attribute
576  * @value: unused
577  * @size: unused
578  * @flags: unused
579  *
580  * This protects the Smack attribute explicitly.
581  *
582  * Returns 0 if access is permitted, an error code otherwise
583  */
584 static int smack_inode_setxattr(struct dentry *dentry, char *name,
585                                 void *value, size_t size, int flags)
586 {
587         if (!capable(CAP_MAC_ADMIN)) {
588                 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
589                     strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
590                     strcmp(name, XATTR_NAME_SMACKIPOUT) == 0)
591                         return -EPERM;
592         }
593
594         return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
595 }
596
597 /**
598  * smack_inode_post_setxattr - Apply the Smack update approved above
599  * @dentry: object
600  * @name: attribute name
601  * @value: attribute value
602  * @size: attribute size
603  * @flags: unused
604  *
605  * Set the pointer in the inode blob to the entry found
606  * in the master label list.
607  */
608 static void smack_inode_post_setxattr(struct dentry *dentry, char *name,
609                                       void *value, size_t size, int flags)
610 {
611         struct inode_smack *isp;
612         char *nsp;
613
614         /*
615          * Not SMACK
616          */
617         if (strcmp(name, XATTR_NAME_SMACK))
618                 return;
619
620         if (size >= SMK_LABELLEN)
621                 return;
622
623         isp = dentry->d_inode->i_security;
624
625         /*
626          * No locking is done here. This is a pointer
627          * assignment.
628          */
629         nsp = smk_import(value, size);
630         if (nsp != NULL)
631                 isp->smk_inode = nsp;
632         else
633                 isp->smk_inode = smack_known_invalid.smk_known;
634
635         return;
636 }
637
638 /*
639  * smack_inode_getxattr - Smack check on getxattr
640  * @dentry: the object
641  * @name: unused
642  *
643  * Returns 0 if access is permitted, an error code otherwise
644  */
645 static int smack_inode_getxattr(struct dentry *dentry, char *name)
646 {
647         return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ);
648 }
649
650 /*
651  * smack_inode_removexattr - Smack check on removexattr
652  * @dentry: the object
653  * @name: name of the attribute
654  *
655  * Removing the Smack attribute requires CAP_MAC_ADMIN
656  *
657  * Returns 0 if access is permitted, an error code otherwise
658  */
659 static int smack_inode_removexattr(struct dentry *dentry, char *name)
660 {
661         if (strcmp(name, XATTR_NAME_SMACK) == 0 && !capable(CAP_MAC_ADMIN))
662                 return -EPERM;
663
664         return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE);
665 }
666
667 /**
668  * smack_inode_getsecurity - get smack xattrs
669  * @inode: the object
670  * @name: attribute name
671  * @buffer: where to put the result
672  * @size: size of the buffer
673  * @err: unused
674  *
675  * Returns the size of the attribute or an error code
676  */
677 static int smack_inode_getsecurity(const struct inode *inode,
678                                    const char *name, void **buffer,
679                                    bool alloc)
680 {
681         struct socket_smack *ssp;
682         struct socket *sock;
683         struct super_block *sbp;
684         struct inode *ip = (struct inode *)inode;
685         char *isp;
686         int ilen;
687         int rc = 0;
688
689         if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
690                 isp = smk_of_inode(inode);
691                 ilen = strlen(isp) + 1;
692                 *buffer = isp;
693                 return ilen;
694         }
695
696         /*
697          * The rest of the Smack xattrs are only on sockets.
698          */
699         sbp = ip->i_sb;
700         if (sbp->s_magic != SOCKFS_MAGIC)
701                 return -EOPNOTSUPP;
702
703         sock = SOCKET_I(ip);
704         if (sock == NULL || sock->sk == NULL)
705                 return -EOPNOTSUPP;
706
707         ssp = sock->sk->sk_security;
708
709         if (strcmp(name, XATTR_SMACK_IPIN) == 0)
710                 isp = ssp->smk_in;
711         else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
712                 isp = ssp->smk_out;
713         else
714                 return -EOPNOTSUPP;
715
716         ilen = strlen(isp) + 1;
717         if (rc == 0) {
718                 *buffer = isp;
719                 rc = ilen;
720         }
721
722         return rc;
723 }
724
725
726 /**
727  * smack_inode_listsecurity - list the Smack attributes
728  * @inode: the object
729  * @buffer: where they go
730  * @buffer_size: size of buffer
731  *
732  * Returns 0 on success, -EINVAL otherwise
733  */
734 static int smack_inode_listsecurity(struct inode *inode, char *buffer,
735                                     size_t buffer_size)
736 {
737         int len = strlen(XATTR_NAME_SMACK);
738
739         if (buffer != NULL && len <= buffer_size) {
740                 memcpy(buffer, XATTR_NAME_SMACK, len);
741                 return len;
742         }
743         return -EINVAL;
744 }
745
746 /*
747  * File Hooks
748  */
749
750 /**
751  * smack_file_permission - Smack check on file operations
752  * @file: unused
753  * @mask: unused
754  *
755  * Returns 0
756  *
757  * Should access checks be done on each read or write?
758  * UNICOS and SELinux say yes.
759  * Trusted Solaris, Trusted Irix, and just about everyone else says no.
760  *
761  * I'll say no for now. Smack does not do the frequent
762  * label changing that SELinux does.
763  */
764 static int smack_file_permission(struct file *file, int mask)
765 {
766         return 0;
767 }
768
769 /**
770  * smack_file_alloc_security - assign a file security blob
771  * @file: the object
772  *
773  * The security blob for a file is a pointer to the master
774  * label list, so no allocation is done.
775  *
776  * Returns 0
777  */
778 static int smack_file_alloc_security(struct file *file)
779 {
780         file->f_security = current->security;
781         return 0;
782 }
783
784 /**
785  * smack_file_free_security - clear a file security blob
786  * @file: the object
787  *
788  * The security blob for a file is a pointer to the master
789  * label list, so no memory is freed.
790  */
791 static void smack_file_free_security(struct file *file)
792 {
793         file->f_security = NULL;
794 }
795
796 /**
797  * smack_file_ioctl - Smack check on ioctls
798  * @file: the object
799  * @cmd: what to do
800  * @arg: unused
801  *
802  * Relies heavily on the correct use of the ioctl command conventions.
803  *
804  * Returns 0 if allowed, error code otherwise
805  */
806 static int smack_file_ioctl(struct file *file, unsigned int cmd,
807                             unsigned long arg)
808 {
809         int rc = 0;
810
811         if (_IOC_DIR(cmd) & _IOC_WRITE)
812                 rc = smk_curacc(file->f_security, MAY_WRITE);
813
814         if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
815                 rc = smk_curacc(file->f_security, MAY_READ);
816
817         return rc;
818 }
819
820 /**
821  * smack_file_lock - Smack check on file locking
822  * @file: the object
823  * @cmd unused
824  *
825  * Returns 0 if current has write access, error code otherwise
826  */
827 static int smack_file_lock(struct file *file, unsigned int cmd)
828 {
829         return smk_curacc(file->f_security, MAY_WRITE);
830 }
831
832 /**
833  * smack_file_fcntl - Smack check on fcntl
834  * @file: the object
835  * @cmd: what action to check
836  * @arg: unused
837  *
838  * Returns 0 if current has access, error code otherwise
839  */
840 static int smack_file_fcntl(struct file *file, unsigned int cmd,
841                             unsigned long arg)
842 {
843         int rc;
844
845         switch (cmd) {
846         case F_DUPFD:
847         case F_GETFD:
848         case F_GETFL:
849         case F_GETLK:
850         case F_GETOWN:
851         case F_GETSIG:
852                 rc = smk_curacc(file->f_security, MAY_READ);
853                 break;
854         case F_SETFD:
855         case F_SETFL:
856         case F_SETLK:
857         case F_SETLKW:
858         case F_SETOWN:
859         case F_SETSIG:
860                 rc = smk_curacc(file->f_security, MAY_WRITE);
861                 break;
862         default:
863                 rc = smk_curacc(file->f_security, MAY_READWRITE);
864         }
865
866         return rc;
867 }
868
869 /**
870  * smack_file_set_fowner - set the file security blob value
871  * @file: object in question
872  *
873  * Returns 0
874  * Further research may be required on this one.
875  */
876 static int smack_file_set_fowner(struct file *file)
877 {
878         file->f_security = current->security;
879         return 0;
880 }
881
882 /**
883  * smack_file_send_sigiotask - Smack on sigio
884  * @tsk: The target task
885  * @fown: the object the signal come from
886  * @signum: unused
887  *
888  * Allow a privileged task to get signals even if it shouldn't
889  *
890  * Returns 0 if a subject with the object's smack could
891  * write to the task, an error code otherwise.
892  */
893 static int smack_file_send_sigiotask(struct task_struct *tsk,
894                                      struct fown_struct *fown, int signum)
895 {
896         struct file *file;
897         int rc;
898
899         /*
900          * struct fown_struct is never outside the context of a struct file
901          */
902         file = container_of(fown, struct file, f_owner);
903         rc = smk_access(file->f_security, tsk->security, MAY_WRITE);
904         if (rc != 0 && __capable(tsk, CAP_MAC_OVERRIDE))
905                 return 0;
906         return rc;
907 }
908
909 /**
910  * smack_file_receive - Smack file receive check
911  * @file: the object
912  *
913  * Returns 0 if current has access, error code otherwise
914  */
915 static int smack_file_receive(struct file *file)
916 {
917         int may = 0;
918
919         /*
920          * This code relies on bitmasks.
921          */
922         if (file->f_mode & FMODE_READ)
923                 may = MAY_READ;
924         if (file->f_mode & FMODE_WRITE)
925                 may |= MAY_WRITE;
926
927         return smk_curacc(file->f_security, may);
928 }
929
930 /*
931  * Task hooks
932  */
933
934 /**
935  * smack_task_alloc_security - "allocate" a task blob
936  * @tsk: the task in need of a blob
937  *
938  * Smack isn't using copies of blobs. Everyone
939  * points to an immutable list. No alloc required.
940  * No data copy required.
941  *
942  * Always returns 0
943  */
944 static int smack_task_alloc_security(struct task_struct *tsk)
945 {
946         tsk->security = current->security;
947
948         return 0;
949 }
950
951 /**
952  * smack_task_free_security - "free" a task blob
953  * @task: the task with the blob
954  *
955  * Smack isn't using copies of blobs. Everyone
956  * points to an immutable list. The blobs never go away.
957  * There is no leak here.
958  */
959 static void smack_task_free_security(struct task_struct *task)
960 {
961         task->security = NULL;
962 }
963
964 /**
965  * smack_task_setpgid - Smack check on setting pgid
966  * @p: the task object
967  * @pgid: unused
968  *
969  * Return 0 if write access is permitted
970  */
971 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
972 {
973         return smk_curacc(p->security, MAY_WRITE);
974 }
975
976 /**
977  * smack_task_getpgid - Smack access check for getpgid
978  * @p: the object task
979  *
980  * Returns 0 if current can read the object task, error code otherwise
981  */
982 static int smack_task_getpgid(struct task_struct *p)
983 {
984         return smk_curacc(p->security, MAY_READ);
985 }
986
987 /**
988  * smack_task_getsid - Smack access check for getsid
989  * @p: the object task
990  *
991  * Returns 0 if current can read the object task, error code otherwise
992  */
993 static int smack_task_getsid(struct task_struct *p)
994 {
995         return smk_curacc(p->security, MAY_READ);
996 }
997
998 /**
999  * smack_task_getsecid - get the secid of the task
1000  * @p: the object task
1001  * @secid: where to put the result
1002  *
1003  * Sets the secid to contain a u32 version of the smack label.
1004  */
1005 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1006 {
1007         *secid = smack_to_secid(p->security);
1008 }
1009
1010 /**
1011  * smack_task_setnice - Smack check on setting nice
1012  * @p: the task object
1013  * @nice: unused
1014  *
1015  * Return 0 if write access is permitted
1016  */
1017 static int smack_task_setnice(struct task_struct *p, int nice)
1018 {
1019         return smk_curacc(p->security, MAY_WRITE);
1020 }
1021
1022 /**
1023  * smack_task_setioprio - Smack check on setting ioprio
1024  * @p: the task object
1025  * @ioprio: unused
1026  *
1027  * Return 0 if write access is permitted
1028  */
1029 static int smack_task_setioprio(struct task_struct *p, int ioprio)
1030 {
1031         return smk_curacc(p->security, MAY_WRITE);
1032 }
1033
1034 /**
1035  * smack_task_getioprio - Smack check on reading ioprio
1036  * @p: the task object
1037  *
1038  * Return 0 if read access is permitted
1039  */
1040 static int smack_task_getioprio(struct task_struct *p)
1041 {
1042         return smk_curacc(p->security, MAY_READ);
1043 }
1044
1045 /**
1046  * smack_task_setscheduler - Smack check on setting scheduler
1047  * @p: the task object
1048  * @policy: unused
1049  * @lp: unused
1050  *
1051  * Return 0 if read access is permitted
1052  */
1053 static int smack_task_setscheduler(struct task_struct *p, int policy,
1054                                    struct sched_param *lp)
1055 {
1056         return smk_curacc(p->security, MAY_WRITE);
1057 }
1058
1059 /**
1060  * smack_task_getscheduler - Smack check on reading scheduler
1061  * @p: the task object
1062  *
1063  * Return 0 if read access is permitted
1064  */
1065 static int smack_task_getscheduler(struct task_struct *p)
1066 {
1067         return smk_curacc(p->security, MAY_READ);
1068 }
1069
1070 /**
1071  * smack_task_movememory - Smack check on moving memory
1072  * @p: the task object
1073  *
1074  * Return 0 if write access is permitted
1075  */
1076 static int smack_task_movememory(struct task_struct *p)
1077 {
1078         return smk_curacc(p->security, MAY_WRITE);
1079 }
1080
1081 /**
1082  * smack_task_kill - Smack check on signal delivery
1083  * @p: the task object
1084  * @info: unused
1085  * @sig: unused
1086  * @secid: identifies the smack to use in lieu of current's
1087  *
1088  * Return 0 if write access is permitted
1089  *
1090  * The secid behavior is an artifact of an SELinux hack
1091  * in the USB code. Someday it may go away.
1092  */
1093 static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1094                            int sig, u32 secid)
1095 {
1096         /*
1097          * Special cases where signals really ought to go through
1098          * in spite of policy. Stephen Smalley suggests it may
1099          * make sense to change the caller so that it doesn't
1100          * bother with the LSM hook in these cases.
1101          */
1102         if (info != SEND_SIG_NOINFO &&
1103             (is_si_special(info) || SI_FROMKERNEL(info)))
1104                 return 0;
1105         /*
1106          * Sending a signal requires that the sender
1107          * can write the receiver.
1108          */
1109         if (secid == 0)
1110                 return smk_curacc(p->security, MAY_WRITE);
1111         /*
1112          * If the secid isn't 0 we're dealing with some USB IO
1113          * specific behavior. This is not clean. For one thing
1114          * we can't take privilege into account.
1115          */
1116         return smk_access(smack_from_secid(secid), p->security, MAY_WRITE);
1117 }
1118
1119 /**
1120  * smack_task_wait - Smack access check for waiting
1121  * @p: task to wait for
1122  *
1123  * Returns 0 if current can wait for p, error code otherwise
1124  */
1125 static int smack_task_wait(struct task_struct *p)
1126 {
1127         int rc;
1128
1129         rc = smk_access(current->security, p->security, MAY_WRITE);
1130         if (rc == 0)
1131                 return 0;
1132
1133         /*
1134          * Allow the operation to succeed if either task
1135          * has privilege to perform operations that might
1136          * account for the smack labels having gotten to
1137          * be different in the first place.
1138          *
1139          * This breaks the strict subjet/object access
1140          * control ideal, taking the object's privilege
1141          * state into account in the decision as well as
1142          * the smack value.
1143          */
1144         if (capable(CAP_MAC_OVERRIDE) || __capable(p, CAP_MAC_OVERRIDE))
1145                 return 0;
1146
1147         return rc;
1148 }
1149
1150 /**
1151  * smack_task_to_inode - copy task smack into the inode blob
1152  * @p: task to copy from
1153  * inode: inode to copy to
1154  *
1155  * Sets the smack pointer in the inode security blob
1156  */
1157 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1158 {
1159         struct inode_smack *isp = inode->i_security;
1160         isp->smk_inode = p->security;
1161 }
1162
1163 /*
1164  * Socket hooks.
1165  */
1166
1167 /**
1168  * smack_sk_alloc_security - Allocate a socket blob
1169  * @sk: the socket
1170  * @family: unused
1171  * @priority: memory allocation priority
1172  *
1173  * Assign Smack pointers to current
1174  *
1175  * Returns 0 on success, -ENOMEM is there's no memory
1176  */
1177 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1178 {
1179         char *csp = current->security;
1180         struct socket_smack *ssp;
1181
1182         ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1183         if (ssp == NULL)
1184                 return -ENOMEM;
1185
1186         ssp->smk_in = csp;
1187         ssp->smk_out = csp;
1188         ssp->smk_packet[0] = '\0';
1189
1190         sk->sk_security = ssp;
1191
1192         return 0;
1193 }
1194
1195 /**
1196  * smack_sk_free_security - Free a socket blob
1197  * @sk: the socket
1198  *
1199  * Clears the blob pointer
1200  */
1201 static void smack_sk_free_security(struct sock *sk)
1202 {
1203         kfree(sk->sk_security);
1204 }
1205
1206 /**
1207  * smack_set_catset - convert a capset to netlabel mls categories
1208  * @catset: the Smack categories
1209  * @sap: where to put the netlabel categories
1210  *
1211  * Allocates and fills attr.mls.cat
1212  */
1213 static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1214 {
1215         unsigned char *cp;
1216         unsigned char m;
1217         int cat;
1218         int rc;
1219         int byte;
1220
1221         if (catset == 0)
1222                 return;
1223
1224         sap->flags |= NETLBL_SECATTR_MLS_CAT;
1225         sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1226         sap->attr.mls.cat->startbit = 0;
1227
1228         for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1229                 for (m = 0x80; m != 0; m >>= 1, cat++) {
1230                         if ((m & *cp) == 0)
1231                                 continue;
1232                         rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1233                                                           cat, GFP_ATOMIC);
1234                 }
1235 }
1236
1237 /**
1238  * smack_to_secattr - fill a secattr from a smack value
1239  * @smack: the smack value
1240  * @nlsp: where the result goes
1241  *
1242  * Casey says that CIPSO is good enough for now.
1243  * It can be used to effect.
1244  * It can also be abused to effect when necessary.
1245  * Appologies to the TSIG group in general and GW in particular.
1246  */
1247 static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1248 {
1249         struct smack_cipso cipso;
1250         int rc;
1251
1252         switch (smack_net_nltype) {
1253         case NETLBL_NLTYPE_CIPSOV4:
1254                 nlsp->domain = kstrdup(smack, GFP_ATOMIC);
1255                 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
1256
1257                 rc = smack_to_cipso(smack, &cipso);
1258                 if (rc == 0) {
1259                         nlsp->attr.mls.lvl = cipso.smk_level;
1260                         smack_set_catset(cipso.smk_catset, nlsp);
1261                 } else {
1262                         nlsp->attr.mls.lvl = smack_cipso_direct;
1263                         smack_set_catset(smack, nlsp);
1264                 }
1265                 break;
1266         default:
1267                 break;
1268         }
1269 }
1270
1271 /**
1272  * smack_netlabel - Set the secattr on a socket
1273  * @sk: the socket
1274  *
1275  * Convert the outbound smack value (smk_out) to a
1276  * secattr and attach it to the socket.
1277  *
1278  * Returns 0 on success or an error code
1279  */
1280 static int smack_netlabel(struct sock *sk)
1281 {
1282         struct socket_smack *ssp;
1283         struct netlbl_lsm_secattr secattr;
1284         int rc;
1285
1286         ssp = sk->sk_security;
1287         netlbl_secattr_init(&secattr);
1288         smack_to_secattr(ssp->smk_out, &secattr);
1289         rc = netlbl_sock_setattr(sk, &secattr);
1290         netlbl_secattr_destroy(&secattr);
1291
1292         return rc;
1293 }
1294
1295 /**
1296  * smack_inode_setsecurity - set smack xattrs
1297  * @inode: the object
1298  * @name: attribute name
1299  * @value: attribute value
1300  * @size: size of the attribute
1301  * @flags: unused
1302  *
1303  * Sets the named attribute in the appropriate blob
1304  *
1305  * Returns 0 on success, or an error code
1306  */
1307 static int smack_inode_setsecurity(struct inode *inode, const char *name,
1308                                    const void *value, size_t size, int flags)
1309 {
1310         char *sp;
1311         struct inode_smack *nsp = inode->i_security;
1312         struct socket_smack *ssp;
1313         struct socket *sock;
1314         int rc = 0;
1315
1316         if (value == NULL || size > SMK_LABELLEN)
1317                 return -EACCES;
1318
1319         sp = smk_import(value, size);
1320         if (sp == NULL)
1321                 return -EINVAL;
1322
1323         if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1324                 nsp->smk_inode = sp;
1325                 return 0;
1326         }
1327         /*
1328          * The rest of the Smack xattrs are only on sockets.
1329          */
1330         if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1331                 return -EOPNOTSUPP;
1332
1333         sock = SOCKET_I(inode);
1334         if (sock == NULL || sock->sk == NULL)
1335                 return -EOPNOTSUPP;
1336
1337         ssp = sock->sk->sk_security;
1338
1339         if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1340                 ssp->smk_in = sp;
1341         else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1342                 ssp->smk_out = sp;
1343                 rc = smack_netlabel(sock->sk);
1344                 if (rc != 0)
1345                         printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1346                                __func__, -rc);
1347         } else
1348                 return -EOPNOTSUPP;
1349
1350         return 0;
1351 }
1352
1353 /**
1354  * smack_socket_post_create - finish socket setup
1355  * @sock: the socket
1356  * @family: protocol family
1357  * @type: unused
1358  * @protocol: unused
1359  * @kern: unused
1360  *
1361  * Sets the netlabel information on the socket
1362  *
1363  * Returns 0 on success, and error code otherwise
1364  */
1365 static int smack_socket_post_create(struct socket *sock, int family,
1366                                     int type, int protocol, int kern)
1367 {
1368         if (family != PF_INET || sock->sk == NULL)
1369                 return 0;
1370         /*
1371          * Set the outbound netlbl.
1372          */
1373         return smack_netlabel(sock->sk);
1374 }
1375
1376 /**
1377  * smack_flags_to_may - convert S_ to MAY_ values
1378  * @flags: the S_ value
1379  *
1380  * Returns the equivalent MAY_ value
1381  */
1382 static int smack_flags_to_may(int flags)
1383 {
1384         int may = 0;
1385
1386         if (flags & S_IRUGO)
1387                 may |= MAY_READ;
1388         if (flags & S_IWUGO)
1389                 may |= MAY_WRITE;
1390         if (flags & S_IXUGO)
1391                 may |= MAY_EXEC;
1392
1393         return may;
1394 }
1395
1396 /**
1397  * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1398  * @msg: the object
1399  *
1400  * Returns 0
1401  */
1402 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1403 {
1404         msg->security = current->security;
1405         return 0;
1406 }
1407
1408 /**
1409  * smack_msg_msg_free_security - Clear the security blob for msg_msg
1410  * @msg: the object
1411  *
1412  * Clears the blob pointer
1413  */
1414 static void smack_msg_msg_free_security(struct msg_msg *msg)
1415 {
1416         msg->security = NULL;
1417 }
1418
1419 /**
1420  * smack_of_shm - the smack pointer for the shm
1421  * @shp: the object
1422  *
1423  * Returns a pointer to the smack value
1424  */
1425 static char *smack_of_shm(struct shmid_kernel *shp)
1426 {
1427         return (char *)shp->shm_perm.security;
1428 }
1429
1430 /**
1431  * smack_shm_alloc_security - Set the security blob for shm
1432  * @shp: the object
1433  *
1434  * Returns 0
1435  */
1436 static int smack_shm_alloc_security(struct shmid_kernel *shp)
1437 {
1438         struct kern_ipc_perm *isp = &shp->shm_perm;
1439
1440         isp->security = current->security;
1441         return 0;
1442 }
1443
1444 /**
1445  * smack_shm_free_security - Clear the security blob for shm
1446  * @shp: the object
1447  *
1448  * Clears the blob pointer
1449  */
1450 static void smack_shm_free_security(struct shmid_kernel *shp)
1451 {
1452         struct kern_ipc_perm *isp = &shp->shm_perm;
1453
1454         isp->security = NULL;
1455 }
1456
1457 /**
1458  * smack_shm_associate - Smack access check for shm
1459  * @shp: the object
1460  * @shmflg: access requested
1461  *
1462  * Returns 0 if current has the requested access, error code otherwise
1463  */
1464 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1465 {
1466         char *ssp = smack_of_shm(shp);
1467         int may;
1468
1469         may = smack_flags_to_may(shmflg);
1470         return smk_curacc(ssp, may);
1471 }
1472
1473 /**
1474  * smack_shm_shmctl - Smack access check for shm
1475  * @shp: the object
1476  * @cmd: what it wants to do
1477  *
1478  * Returns 0 if current has the requested access, error code otherwise
1479  */
1480 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1481 {
1482         char *ssp = smack_of_shm(shp);
1483         int may;
1484
1485         switch (cmd) {
1486         case IPC_STAT:
1487         case SHM_STAT:
1488                 may = MAY_READ;
1489                 break;
1490         case IPC_SET:
1491         case SHM_LOCK:
1492         case SHM_UNLOCK:
1493         case IPC_RMID:
1494                 may = MAY_READWRITE;
1495                 break;
1496         case IPC_INFO:
1497         case SHM_INFO:
1498                 /*
1499                  * System level information.
1500                  */
1501                 return 0;
1502         default:
1503                 return -EINVAL;
1504         }
1505
1506         return smk_curacc(ssp, may);
1507 }
1508
1509 /**
1510  * smack_shm_shmat - Smack access for shmat
1511  * @shp: the object
1512  * @shmaddr: unused
1513  * @shmflg: access requested
1514  *
1515  * Returns 0 if current has the requested access, error code otherwise
1516  */
1517 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1518                            int shmflg)
1519 {
1520         char *ssp = smack_of_shm(shp);
1521         int may;
1522
1523         may = smack_flags_to_may(shmflg);
1524         return smk_curacc(ssp, may);
1525 }
1526
1527 /**
1528  * smack_of_sem - the smack pointer for the sem
1529  * @sma: the object
1530  *
1531  * Returns a pointer to the smack value
1532  */
1533 static char *smack_of_sem(struct sem_array *sma)
1534 {
1535         return (char *)sma->sem_perm.security;
1536 }
1537
1538 /**
1539  * smack_sem_alloc_security - Set the security blob for sem
1540  * @sma: the object
1541  *
1542  * Returns 0
1543  */
1544 static int smack_sem_alloc_security(struct sem_array *sma)
1545 {
1546         struct kern_ipc_perm *isp = &sma->sem_perm;
1547
1548         isp->security = current->security;
1549         return 0;
1550 }
1551
1552 /**
1553  * smack_sem_free_security - Clear the security blob for sem
1554  * @sma: the object
1555  *
1556  * Clears the blob pointer
1557  */
1558 static void smack_sem_free_security(struct sem_array *sma)
1559 {
1560         struct kern_ipc_perm *isp = &sma->sem_perm;
1561
1562         isp->security = NULL;
1563 }
1564
1565 /**
1566  * smack_sem_associate - Smack access check for sem
1567  * @sma: the object
1568  * @semflg: access requested
1569  *
1570  * Returns 0 if current has the requested access, error code otherwise
1571  */
1572 static int smack_sem_associate(struct sem_array *sma, int semflg)
1573 {
1574         char *ssp = smack_of_sem(sma);
1575         int may;
1576
1577         may = smack_flags_to_may(semflg);
1578         return smk_curacc(ssp, may);
1579 }
1580
1581 /**
1582  * smack_sem_shmctl - Smack access check for sem
1583  * @sma: the object
1584  * @cmd: what it wants to do
1585  *
1586  * Returns 0 if current has the requested access, error code otherwise
1587  */
1588 static int smack_sem_semctl(struct sem_array *sma, int cmd)
1589 {
1590         char *ssp = smack_of_sem(sma);
1591         int may;
1592
1593         switch (cmd) {
1594         case GETPID:
1595         case GETNCNT:
1596         case GETZCNT:
1597         case GETVAL:
1598         case GETALL:
1599         case IPC_STAT:
1600         case SEM_STAT:
1601                 may = MAY_READ;
1602                 break;
1603         case SETVAL:
1604         case SETALL:
1605         case IPC_RMID:
1606         case IPC_SET:
1607                 may = MAY_READWRITE;
1608                 break;
1609         case IPC_INFO:
1610         case SEM_INFO:
1611                 /*
1612                  * System level information
1613                  */
1614                 return 0;
1615         default:
1616                 return -EINVAL;
1617         }
1618
1619         return smk_curacc(ssp, may);
1620 }
1621
1622 /**
1623  * smack_sem_semop - Smack checks of semaphore operations
1624  * @sma: the object
1625  * @sops: unused
1626  * @nsops: unused
1627  * @alter: unused
1628  *
1629  * Treated as read and write in all cases.
1630  *
1631  * Returns 0 if access is allowed, error code otherwise
1632  */
1633 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
1634                            unsigned nsops, int alter)
1635 {
1636         char *ssp = smack_of_sem(sma);
1637
1638         return smk_curacc(ssp, MAY_READWRITE);
1639 }
1640
1641 /**
1642  * smack_msg_alloc_security - Set the security blob for msg
1643  * @msq: the object
1644  *
1645  * Returns 0
1646  */
1647 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
1648 {
1649         struct kern_ipc_perm *kisp = &msq->q_perm;
1650
1651         kisp->security = current->security;
1652         return 0;
1653 }
1654
1655 /**
1656  * smack_msg_free_security - Clear the security blob for msg
1657  * @msq: the object
1658  *
1659  * Clears the blob pointer
1660  */
1661 static void smack_msg_queue_free_security(struct msg_queue *msq)
1662 {
1663         struct kern_ipc_perm *kisp = &msq->q_perm;
1664
1665         kisp->security = NULL;
1666 }
1667
1668 /**
1669  * smack_of_msq - the smack pointer for the msq
1670  * @msq: the object
1671  *
1672  * Returns a pointer to the smack value
1673  */
1674 static char *smack_of_msq(struct msg_queue *msq)
1675 {
1676         return (char *)msq->q_perm.security;
1677 }
1678
1679 /**
1680  * smack_msg_queue_associate - Smack access check for msg_queue
1681  * @msq: the object
1682  * @msqflg: access requested
1683  *
1684  * Returns 0 if current has the requested access, error code otherwise
1685  */
1686 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
1687 {
1688         char *msp = smack_of_msq(msq);
1689         int may;
1690
1691         may = smack_flags_to_may(msqflg);
1692         return smk_curacc(msp, may);
1693 }
1694
1695 /**
1696  * smack_msg_queue_msgctl - Smack access check for msg_queue
1697  * @msq: the object
1698  * @cmd: what it wants to do
1699  *
1700  * Returns 0 if current has the requested access, error code otherwise
1701  */
1702 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1703 {
1704         char *msp = smack_of_msq(msq);
1705         int may;
1706
1707         switch (cmd) {
1708         case IPC_STAT:
1709         case MSG_STAT:
1710                 may = MAY_READ;
1711                 break;
1712         case IPC_SET:
1713         case IPC_RMID:
1714                 may = MAY_READWRITE;
1715                 break;
1716         case IPC_INFO:
1717         case MSG_INFO:
1718                 /*
1719                  * System level information
1720                  */
1721                 return 0;
1722         default:
1723                 return -EINVAL;
1724         }
1725
1726         return smk_curacc(msp, may);
1727 }
1728
1729 /**
1730  * smack_msg_queue_msgsnd - Smack access check for msg_queue
1731  * @msq: the object
1732  * @msg: unused
1733  * @msqflg: access requested
1734  *
1735  * Returns 0 if current has the requested access, error code otherwise
1736  */
1737 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
1738                                   int msqflg)
1739 {
1740         char *msp = smack_of_msq(msq);
1741         int rc;
1742
1743         rc = smack_flags_to_may(msqflg);
1744         return smk_curacc(msp, rc);
1745 }
1746
1747 /**
1748  * smack_msg_queue_msgsnd - Smack access check for msg_queue
1749  * @msq: the object
1750  * @msg: unused
1751  * @target: unused
1752  * @type: unused
1753  * @mode: unused
1754  *
1755  * Returns 0 if current has read and write access, error code otherwise
1756  */
1757 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1758                         struct task_struct *target, long type, int mode)
1759 {
1760         char *msp = smack_of_msq(msq);
1761
1762         return smk_curacc(msp, MAY_READWRITE);
1763 }
1764
1765 /**
1766  * smack_ipc_permission - Smack access for ipc_permission()
1767  * @ipp: the object permissions
1768  * @flag: access requested
1769  *
1770  * Returns 0 if current has read and write access, error code otherwise
1771  */
1772 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
1773 {
1774         char *isp = ipp->security;
1775         int may;
1776
1777         may = smack_flags_to_may(flag);
1778         return smk_curacc(isp, may);
1779 }
1780
1781 /**
1782  * smack_d_instantiate - Make sure the blob is correct on an inode
1783  * @opt_dentry: unused
1784  * @inode: the object
1785  *
1786  * Set the inode's security blob if it hasn't been done already.
1787  */
1788 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
1789 {
1790         struct super_block *sbp;
1791         struct superblock_smack *sbsp;
1792         struct inode_smack *isp;
1793         char *csp = current->security;
1794         char *fetched;
1795         char *final;
1796         struct dentry *dp;
1797
1798         if (inode == NULL)
1799                 return;
1800
1801         isp = inode->i_security;
1802
1803         mutex_lock(&isp->smk_lock);
1804         /*
1805          * If the inode is already instantiated
1806          * take the quick way out
1807          */
1808         if (isp->smk_flags & SMK_INODE_INSTANT)
1809                 goto unlockandout;
1810
1811         sbp = inode->i_sb;
1812         sbsp = sbp->s_security;
1813         /*
1814          * We're going to use the superblock default label
1815          * if there's no label on the file.
1816          */
1817         final = sbsp->smk_default;
1818
1819         /*
1820          * This is pretty hackish.
1821          * Casey says that we shouldn't have to do
1822          * file system specific code, but it does help
1823          * with keeping it simple.
1824          */
1825         switch (sbp->s_magic) {
1826         case SMACK_MAGIC:
1827                 /*
1828                  * Casey says that it's a little embarassing
1829                  * that the smack file system doesn't do
1830                  * extended attributes.
1831                  */
1832                 final = smack_known_star.smk_known;
1833                 break;
1834         case PIPEFS_MAGIC:
1835                 /*
1836                  * Casey says pipes are easy (?)
1837                  */
1838                 final = smack_known_star.smk_known;
1839                 break;
1840         case DEVPTS_SUPER_MAGIC:
1841                 /*
1842                  * devpts seems content with the label of the task.
1843                  * Programs that change smack have to treat the
1844                  * pty with respect.
1845                  */
1846                 final = csp;
1847                 break;
1848         case SOCKFS_MAGIC:
1849                 /*
1850                  * Casey says sockets get the smack of the task.
1851                  */
1852                 final = csp;
1853                 break;
1854         case PROC_SUPER_MAGIC:
1855                 /*
1856                  * Casey says procfs appears not to care.
1857                  * The superblock default suffices.
1858                  */
1859                 break;
1860         case TMPFS_MAGIC:
1861                 /*
1862                  * Device labels should come from the filesystem,
1863                  * but watch out, because they're volitile,
1864                  * getting recreated on every reboot.
1865                  */
1866                 final = smack_known_star.smk_known;
1867                 /*
1868                  * No break.
1869                  *
1870                  * If a smack value has been set we want to use it,
1871                  * but since tmpfs isn't giving us the opportunity
1872                  * to set mount options simulate setting the
1873                  * superblock default.
1874                  */
1875         default:
1876                 /*
1877                  * This isn't an understood special case.
1878                  * Get the value from the xattr.
1879                  *
1880                  * No xattr support means, alas, no SMACK label.
1881                  * Use the aforeapplied default.
1882                  * It would be curious if the label of the task
1883                  * does not match that assigned.
1884                  */
1885                 if (inode->i_op->getxattr == NULL)
1886                         break;
1887                 /*
1888                  * Get the dentry for xattr.
1889                  */
1890                 if (opt_dentry == NULL) {
1891                         dp = d_find_alias(inode);
1892                         if (dp == NULL)
1893                                 break;
1894                 } else {
1895                         dp = dget(opt_dentry);
1896                         if (dp == NULL)
1897                                 break;
1898                 }
1899
1900                 fetched = smk_fetch(inode, dp);
1901                 if (fetched != NULL)
1902                         final = fetched;
1903
1904                 dput(dp);
1905                 break;
1906         }
1907
1908         if (final == NULL)
1909                 isp->smk_inode = csp;
1910         else
1911                 isp->smk_inode = final;
1912
1913         isp->smk_flags |= SMK_INODE_INSTANT;
1914
1915 unlockandout:
1916         mutex_unlock(&isp->smk_lock);
1917         return;
1918 }
1919
1920 /**
1921  * smack_getprocattr - Smack process attribute access
1922  * @p: the object task
1923  * @name: the name of the attribute in /proc/.../attr
1924  * @value: where to put the result
1925  *
1926  * Places a copy of the task Smack into value
1927  *
1928  * Returns the length of the smack label or an error code
1929  */
1930 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
1931 {
1932         char *cp;
1933         int slen;
1934
1935         if (strcmp(name, "current") != 0)
1936                 return -EINVAL;
1937
1938         cp = kstrdup(p->security, GFP_KERNEL);
1939         if (cp == NULL)
1940                 return -ENOMEM;
1941
1942         slen = strlen(cp);
1943         *value = cp;
1944         return slen;
1945 }
1946
1947 /**
1948  * smack_setprocattr - Smack process attribute setting
1949  * @p: the object task
1950  * @name: the name of the attribute in /proc/.../attr
1951  * @value: the value to set
1952  * @size: the size of the value
1953  *
1954  * Sets the Smack value of the task. Only setting self
1955  * is permitted and only with privilege
1956  *
1957  * Returns the length of the smack label or an error code
1958  */
1959 static int smack_setprocattr(struct task_struct *p, char *name,
1960                              void *value, size_t size)
1961 {
1962         char *newsmack;
1963
1964         if (!__capable(p, CAP_MAC_ADMIN))
1965                 return -EPERM;
1966
1967         /*
1968          * Changing another process' Smack value is too dangerous
1969          * and supports no sane use case.
1970          */
1971         if (p != current)
1972                 return -EPERM;
1973
1974         if (value == NULL || size == 0 || size >= SMK_LABELLEN)
1975                 return -EINVAL;
1976
1977         if (strcmp(name, "current") != 0)
1978                 return -EINVAL;
1979
1980         newsmack = smk_import(value, size);
1981         if (newsmack == NULL)
1982                 return -EINVAL;
1983
1984         p->security = newsmack;
1985         return size;
1986 }
1987
1988 /**
1989  * smack_unix_stream_connect - Smack access on UDS
1990  * @sock: one socket
1991  * @other: the other socket
1992  * @newsk: unused
1993  *
1994  * Return 0 if a subject with the smack of sock could access
1995  * an object with the smack of other, otherwise an error code
1996  */
1997 static int smack_unix_stream_connect(struct socket *sock,
1998                                      struct socket *other, struct sock *newsk)
1999 {
2000         struct inode *sp = SOCK_INODE(sock);
2001         struct inode *op = SOCK_INODE(other);
2002
2003         return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE);
2004 }
2005
2006 /**
2007  * smack_unix_may_send - Smack access on UDS
2008  * @sock: one socket
2009  * @other: the other socket
2010  *
2011  * Return 0 if a subject with the smack of sock could access
2012  * an object with the smack of other, otherwise an error code
2013  */
2014 static int smack_unix_may_send(struct socket *sock, struct socket *other)
2015 {
2016         struct inode *sp = SOCK_INODE(sock);
2017         struct inode *op = SOCK_INODE(other);
2018
2019         return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE);
2020 }
2021
2022 /**
2023  * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat
2024  *      pair to smack
2025  * @sap: netlabel secattr
2026  * @sip: where to put the result
2027  *
2028  * Copies a smack label into sip
2029  */
2030 static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2031 {
2032         char smack[SMK_LABELLEN];
2033         int pcat;
2034
2035         if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) {
2036                 /*
2037                  * If there are flags but no level netlabel isn't
2038                  * behaving the way we expect it to.
2039                  *
2040                  * Without guidance regarding the smack value
2041                  * for the packet fall back on the network
2042                  * ambient value.
2043                  */
2044                 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
2045                 return;
2046         }
2047         /*
2048          * Get the categories, if any
2049          */
2050         memset(smack, '\0', SMK_LABELLEN);
2051         if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2052                 for (pcat = -1;;) {
2053                         pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat,
2054                                                           pcat + 1);
2055                         if (pcat < 0)
2056                                 break;
2057                         smack_catset_bit(pcat, smack);
2058                 }
2059         /*
2060          * If it is CIPSO using smack direct mapping
2061          * we are already done. WeeHee.
2062          */
2063         if (sap->attr.mls.lvl == smack_cipso_direct) {
2064                 memcpy(sip, smack, SMK_MAXLEN);
2065                 return;
2066         }
2067         /*
2068          * Look it up in the supplied table if it is not a direct mapping.
2069          */
2070         smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2071         return;
2072 }
2073
2074 /**
2075  * smack_socket_sock_rcv_skb - Smack packet delivery access check
2076  * @sk: socket
2077  * @skb: packet
2078  *
2079  * Returns 0 if the packet should be delivered, an error code otherwise
2080  */
2081 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2082 {
2083         struct netlbl_lsm_secattr secattr;
2084         struct socket_smack *ssp = sk->sk_security;
2085         char smack[SMK_LABELLEN];
2086         int rc;
2087
2088         if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2089                 return 0;
2090
2091         /*
2092          * Translate what netlabel gave us.
2093          */
2094         memset(smack, '\0', SMK_LABELLEN);
2095         netlbl_secattr_init(&secattr);
2096         rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
2097         if (rc == 0)
2098                 smack_from_secattr(&secattr, smack);
2099         else
2100                 strncpy(smack, smack_net_ambient, SMK_MAXLEN);
2101         netlbl_secattr_destroy(&secattr);
2102         /*
2103          * Receiving a packet requires that the other end
2104          * be able to write here. Read access is not required.
2105          * This is the simplist possible security model
2106          * for networking.
2107          */
2108         return smk_access(smack, ssp->smk_in, MAY_WRITE);
2109 }
2110
2111 /**
2112  * smack_socket_getpeersec_stream - pull in packet label
2113  * @sock: the socket
2114  * @optval: user's destination
2115  * @optlen: size thereof
2116  * @len: max thereoe
2117  *
2118  * returns zero on success, an error code otherwise
2119  */
2120 static int smack_socket_getpeersec_stream(struct socket *sock,
2121                                           char __user *optval,
2122                                           int __user *optlen, unsigned len)
2123 {
2124         struct socket_smack *ssp;
2125         int slen;
2126         int rc = 0;
2127
2128         ssp = sock->sk->sk_security;
2129         slen = strlen(ssp->smk_packet) + 1;
2130
2131         if (slen > len)
2132                 rc = -ERANGE;
2133         else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2134                 rc = -EFAULT;
2135
2136         if (put_user(slen, optlen) != 0)
2137                 rc = -EFAULT;
2138
2139         return rc;
2140 }
2141
2142
2143 /**
2144  * smack_socket_getpeersec_dgram - pull in packet label
2145  * @sock: the socket
2146  * @skb: packet data
2147  * @secid: pointer to where to put the secid of the packet
2148  *
2149  * Sets the netlabel socket state on sk from parent
2150  */
2151 static int smack_socket_getpeersec_dgram(struct socket *sock,
2152                                          struct sk_buff *skb, u32 *secid)
2153
2154 {
2155         struct netlbl_lsm_secattr secattr;
2156         struct sock *sk;
2157         char smack[SMK_LABELLEN];
2158         int family = PF_INET;
2159         u32 s;
2160         int rc;
2161
2162         /*
2163          * Only works for families with packets.
2164          */
2165         if (sock != NULL) {
2166                 sk = sock->sk;
2167                 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2168                         return 0;
2169                 family = sk->sk_family;
2170         }
2171         /*
2172          * Translate what netlabel gave us.
2173          */
2174         memset(smack, '\0', SMK_LABELLEN);
2175         netlbl_secattr_init(&secattr);
2176         rc = netlbl_skbuff_getattr(skb, family, &secattr);
2177         if (rc == 0)
2178                 smack_from_secattr(&secattr, smack);
2179         netlbl_secattr_destroy(&secattr);
2180
2181         /*
2182          * Give up if we couldn't get anything
2183          */
2184         if (rc != 0)
2185                 return rc;
2186
2187         s = smack_to_secid(smack);
2188         if (s == 0)
2189                 return -EINVAL;
2190
2191         *secid = s;
2192         return 0;
2193 }
2194
2195 /**
2196  * smack_sock_graft - graft access state between two sockets
2197  * @sk: fresh sock
2198  * @parent: donor socket
2199  *
2200  * Sets the netlabel socket state on sk from parent
2201  */
2202 static void smack_sock_graft(struct sock *sk, struct socket *parent)
2203 {
2204         struct socket_smack *ssp;
2205         int rc;
2206
2207         if (sk == NULL)
2208                 return;
2209
2210         if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2211                 return;
2212
2213         ssp = sk->sk_security;
2214         ssp->smk_in = current->security;
2215         ssp->smk_out = current->security;
2216         ssp->smk_packet[0] = '\0';
2217
2218         rc = smack_netlabel(sk);
2219         if (rc != 0)
2220                 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
2221                        __func__, -rc);
2222 }
2223
2224 /**
2225  * smack_inet_conn_request - Smack access check on connect
2226  * @sk: socket involved
2227  * @skb: packet
2228  * @req: unused
2229  *
2230  * Returns 0 if a task with the packet label could write to
2231  * the socket, otherwise an error code
2232  */
2233 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2234                                    struct request_sock *req)
2235 {
2236         struct netlbl_lsm_secattr skb_secattr;
2237         struct socket_smack *ssp = sk->sk_security;
2238         char smack[SMK_LABELLEN];
2239         int rc;
2240
2241         if (skb == NULL)
2242                 return -EACCES;
2243
2244         memset(smack, '\0', SMK_LABELLEN);
2245         netlbl_secattr_init(&skb_secattr);
2246         rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr);
2247         if (rc == 0)
2248                 smack_from_secattr(&skb_secattr, smack);
2249         else
2250                 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
2251         netlbl_secattr_destroy(&skb_secattr);
2252         /*
2253          * Receiving a packet requires that the other end
2254          * be able to write here. Read access is not required.
2255          *
2256          * If the request is successful save the peer's label
2257          * so that SO_PEERCRED can report it.
2258          */
2259         rc = smk_access(smack, ssp->smk_in, MAY_WRITE);
2260         if (rc == 0)
2261                 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2262
2263         return rc;
2264 }
2265
2266 /*
2267  * Key management security hooks
2268  *
2269  * Casey has not tested key support very heavily.
2270  * The permission check is most likely too restrictive.
2271  * If you care about keys please have a look.
2272  */
2273 #ifdef CONFIG_KEYS
2274
2275 /**
2276  * smack_key_alloc - Set the key security blob
2277  * @key: object
2278  * @tsk: the task associated with the key
2279  * @flags: unused
2280  *
2281  * No allocation required
2282  *
2283  * Returns 0
2284  */
2285 static int smack_key_alloc(struct key *key, struct task_struct *tsk,
2286                            unsigned long flags)
2287 {
2288         key->security = tsk->security;
2289         return 0;
2290 }
2291
2292 /**
2293  * smack_key_free - Clear the key security blob
2294  * @key: the object
2295  *
2296  * Clear the blob pointer
2297  */
2298 static void smack_key_free(struct key *key)
2299 {
2300         key->security = NULL;
2301 }
2302
2303 /*
2304  * smack_key_permission - Smack access on a key
2305  * @key_ref: gets to the object
2306  * @context: task involved
2307  * @perm: unused
2308  *
2309  * Return 0 if the task has read and write to the object,
2310  * an error code otherwise
2311  */
2312 static int smack_key_permission(key_ref_t key_ref,
2313                                 struct task_struct *context, key_perm_t perm)
2314 {
2315         struct key *keyp;
2316
2317         keyp = key_ref_to_ptr(key_ref);
2318         if (keyp == NULL)
2319                 return -EINVAL;
2320         /*
2321          * If the key hasn't been initialized give it access so that
2322          * it may do so.
2323          */
2324         if (keyp->security == NULL)
2325                 return 0;
2326         /*
2327          * This should not occur
2328          */
2329         if (context->security == NULL)
2330                 return -EACCES;
2331
2332         return smk_access(context->security, keyp->security, MAY_READWRITE);
2333 }
2334 #endif /* CONFIG_KEYS */
2335
2336 /*
2337  * smack_secid_to_secctx - return the smack label for a secid
2338  * @secid: incoming integer
2339  * @secdata: destination
2340  * @seclen: how long it is
2341  *
2342  * Exists for networking code.
2343  */
2344 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2345 {
2346         char *sp = smack_from_secid(secid);
2347
2348         *secdata = sp;
2349         *seclen = strlen(sp);
2350         return 0;
2351 }
2352
2353 /*
2354  * smack_secctx_to_secid - return the secid for a smack label
2355  * @secdata: smack label
2356  * @seclen: how long result is
2357  * @secid: outgoing integer
2358  *
2359  * Exists for audit and networking code.
2360  */
2361 static int smack_secctx_to_secid(char *secdata, u32 seclen, u32 *secid)
2362 {
2363         *secid = smack_to_secid(secdata);
2364         return 0;
2365 }
2366
2367 /*
2368  * smack_release_secctx - don't do anything.
2369  * @key_ref: unused
2370  * @context: unused
2371  * @perm: unused
2372  *
2373  * Exists to make sure nothing gets done, and properly
2374  */
2375 static void smack_release_secctx(char *secdata, u32 seclen)
2376 {
2377 }
2378
2379 static struct security_operations smack_ops = {
2380         .ptrace =                       smack_ptrace,
2381         .capget =                       cap_capget,
2382         .capset_check =                 cap_capset_check,
2383         .capset_set =                   cap_capset_set,
2384         .capable =                      cap_capable,
2385         .syslog =                       smack_syslog,
2386         .settime =                      cap_settime,
2387         .vm_enough_memory =             cap_vm_enough_memory,
2388
2389         .bprm_apply_creds =             cap_bprm_apply_creds,
2390         .bprm_set_security =            cap_bprm_set_security,
2391         .bprm_secureexec =              cap_bprm_secureexec,
2392
2393         .sb_alloc_security =            smack_sb_alloc_security,
2394         .sb_free_security =             smack_sb_free_security,
2395         .sb_copy_data =                 smack_sb_copy_data,
2396         .sb_kern_mount =                smack_sb_kern_mount,
2397         .sb_statfs =                    smack_sb_statfs,
2398         .sb_mount =                     smack_sb_mount,
2399         .sb_umount =                    smack_sb_umount,
2400
2401         .inode_alloc_security =         smack_inode_alloc_security,
2402         .inode_free_security =          smack_inode_free_security,
2403         .inode_init_security =          smack_inode_init_security,
2404         .inode_link =                   smack_inode_link,
2405         .inode_unlink =                 smack_inode_unlink,
2406         .inode_rmdir =                  smack_inode_rmdir,
2407         .inode_rename =                 smack_inode_rename,
2408         .inode_permission =             smack_inode_permission,
2409         .inode_setattr =                smack_inode_setattr,
2410         .inode_getattr =                smack_inode_getattr,
2411         .inode_setxattr =               smack_inode_setxattr,
2412         .inode_post_setxattr =          smack_inode_post_setxattr,
2413         .inode_getxattr =               smack_inode_getxattr,
2414         .inode_removexattr =            smack_inode_removexattr,
2415         .inode_getsecurity =            smack_inode_getsecurity,
2416         .inode_setsecurity =            smack_inode_setsecurity,
2417         .inode_listsecurity =           smack_inode_listsecurity,
2418
2419         .file_permission =              smack_file_permission,
2420         .file_alloc_security =          smack_file_alloc_security,
2421         .file_free_security =           smack_file_free_security,
2422         .file_ioctl =                   smack_file_ioctl,
2423         .file_lock =                    smack_file_lock,
2424         .file_fcntl =                   smack_file_fcntl,
2425         .file_set_fowner =              smack_file_set_fowner,
2426         .file_send_sigiotask =          smack_file_send_sigiotask,
2427         .file_receive =                 smack_file_receive,
2428
2429         .task_alloc_security =          smack_task_alloc_security,
2430         .task_free_security =           smack_task_free_security,
2431         .task_post_setuid =             cap_task_post_setuid,
2432         .task_setpgid =                 smack_task_setpgid,
2433         .task_getpgid =                 smack_task_getpgid,
2434         .task_getsid =                  smack_task_getsid,
2435         .task_getsecid =                smack_task_getsecid,
2436         .task_setnice =                 smack_task_setnice,
2437         .task_setioprio =               smack_task_setioprio,
2438         .task_getioprio =               smack_task_getioprio,
2439         .task_setscheduler =            smack_task_setscheduler,
2440         .task_getscheduler =            smack_task_getscheduler,
2441         .task_movememory =              smack_task_movememory,
2442         .task_kill =                    smack_task_kill,
2443         .task_wait =                    smack_task_wait,
2444         .task_reparent_to_init =        cap_task_reparent_to_init,
2445         .task_to_inode =                smack_task_to_inode,
2446
2447         .ipc_permission =               smack_ipc_permission,
2448
2449         .msg_msg_alloc_security =       smack_msg_msg_alloc_security,
2450         .msg_msg_free_security =        smack_msg_msg_free_security,
2451
2452         .msg_queue_alloc_security =     smack_msg_queue_alloc_security,
2453         .msg_queue_free_security =      smack_msg_queue_free_security,
2454         .msg_queue_associate =          smack_msg_queue_associate,
2455         .msg_queue_msgctl =             smack_msg_queue_msgctl,
2456         .msg_queue_msgsnd =             smack_msg_queue_msgsnd,
2457         .msg_queue_msgrcv =             smack_msg_queue_msgrcv,
2458
2459         .shm_alloc_security =           smack_shm_alloc_security,
2460         .shm_free_security =            smack_shm_free_security,
2461         .shm_associate =                smack_shm_associate,
2462         .shm_shmctl =                   smack_shm_shmctl,
2463         .shm_shmat =                    smack_shm_shmat,
2464
2465         .sem_alloc_security =           smack_sem_alloc_security,
2466         .sem_free_security =            smack_sem_free_security,
2467         .sem_associate =                smack_sem_associate,
2468         .sem_semctl =                   smack_sem_semctl,
2469         .sem_semop =                    smack_sem_semop,
2470
2471         .netlink_send =                 cap_netlink_send,
2472         .netlink_recv =                 cap_netlink_recv,
2473
2474         .d_instantiate =                smack_d_instantiate,
2475
2476         .getprocattr =                  smack_getprocattr,
2477         .setprocattr =                  smack_setprocattr,
2478
2479         .unix_stream_connect =          smack_unix_stream_connect,
2480         .unix_may_send =                smack_unix_may_send,
2481
2482         .socket_post_create =           smack_socket_post_create,
2483         .socket_sock_rcv_skb =          smack_socket_sock_rcv_skb,
2484         .socket_getpeersec_stream =     smack_socket_getpeersec_stream,
2485         .socket_getpeersec_dgram =      smack_socket_getpeersec_dgram,
2486         .sk_alloc_security =            smack_sk_alloc_security,
2487         .sk_free_security =             smack_sk_free_security,
2488         .sock_graft =                   smack_sock_graft,
2489         .inet_conn_request =            smack_inet_conn_request,
2490  /* key management security hooks */
2491 #ifdef CONFIG_KEYS
2492         .key_alloc =                    smack_key_alloc,
2493         .key_free =                     smack_key_free,
2494         .key_permission =               smack_key_permission,
2495 #endif /* CONFIG_KEYS */
2496         .secid_to_secctx =              smack_secid_to_secctx,
2497         .secctx_to_secid =              smack_secctx_to_secid,
2498         .release_secctx =               smack_release_secctx,
2499 };
2500
2501 /**
2502  * smack_init - initialize the smack system
2503  *
2504  * Returns 0
2505  */
2506 static __init int smack_init(void)
2507 {
2508         printk(KERN_INFO "Smack:  Initializing.\n");
2509
2510         /*
2511          * Set the security state for the initial task.
2512          */
2513         current->security = &smack_known_floor.smk_known;
2514
2515         /*
2516          * Initialize locks
2517          */
2518         spin_lock_init(&smack_known_unset.smk_cipsolock);
2519         spin_lock_init(&smack_known_huh.smk_cipsolock);
2520         spin_lock_init(&smack_known_hat.smk_cipsolock);
2521         spin_lock_init(&smack_known_star.smk_cipsolock);
2522         spin_lock_init(&smack_known_floor.smk_cipsolock);
2523         spin_lock_init(&smack_known_invalid.smk_cipsolock);
2524
2525         /*
2526          * Register with LSM
2527          */
2528         if (register_security(&smack_ops))
2529                 panic("smack: Unable to register with kernel.\n");
2530
2531         return 0;
2532 }
2533
2534 /*
2535  * Smack requires early initialization in order to label
2536  * all processes and objects when they are created.
2537  */
2538 security_initcall(smack_init);
2539