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