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