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