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