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