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