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