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