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