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