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