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