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