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