7e800c64191273abf305c50b35da7d241741655e
[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         /*
2149          * Sockets created by kernel threads receive web label.
2150          */
2151         if (unlikely(current->flags & PF_KTHREAD)) {
2152                 ssp->smk_in = &smack_known_web;
2153                 ssp->smk_out = &smack_known_web;
2154         } else {
2155                 ssp->smk_in = skp;
2156                 ssp->smk_out = skp;
2157         }
2158         ssp->smk_packet = NULL;
2159
2160         sk->sk_security = ssp;
2161
2162         return 0;
2163 }
2164
2165 /**
2166  * smack_sk_free_security - Free a socket blob
2167  * @sk: the socket
2168  *
2169  * Clears the blob pointer
2170  */
2171 static void smack_sk_free_security(struct sock *sk)
2172 {
2173         kfree(sk->sk_security);
2174 }
2175
2176 /**
2177 * smack_host_label - check host based restrictions
2178 * @sip: the object end
2179 *
2180 * looks for host based access restrictions
2181 *
2182 * This version will only be appropriate for really small sets of single label
2183 * hosts.  The caller is responsible for ensuring that the RCU read lock is
2184 * taken before calling this function.
2185 *
2186 * Returns the label of the far end or NULL if it's not special.
2187 */
2188 static char *smack_host_label(struct sockaddr_in *sip)
2189 {
2190         struct smk_netlbladdr *snp;
2191         struct in_addr *siap = &sip->sin_addr;
2192
2193         if (siap->s_addr == 0)
2194                 return NULL;
2195
2196         list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2197                 /*
2198                 * we break after finding the first match because
2199                 * the list is sorted from longest to shortest mask
2200                 * so we have found the most specific match
2201                 */
2202                 if ((&snp->smk_host.sin_addr)->s_addr ==
2203                     (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2204                         /* we have found the special CIPSO option */
2205                         if (snp->smk_label == smack_cipso_option)
2206                                 return NULL;
2207                         return snp->smk_label;
2208                 }
2209
2210         return NULL;
2211 }
2212
2213 /**
2214  * smack_netlabel - Set the secattr on a socket
2215  * @sk: the socket
2216  * @labeled: socket label scheme
2217  *
2218  * Convert the outbound smack value (smk_out) to a
2219  * secattr and attach it to the socket.
2220  *
2221  * Returns 0 on success or an error code
2222  */
2223 static int smack_netlabel(struct sock *sk, int labeled)
2224 {
2225         struct smack_known *skp;
2226         struct socket_smack *ssp = sk->sk_security;
2227         int rc = 0;
2228
2229         /*
2230          * Usually the netlabel code will handle changing the
2231          * packet labeling based on the label.
2232          * The case of a single label host is different, because
2233          * a single label host should never get a labeled packet
2234          * even though the label is usually associated with a packet
2235          * label.
2236          */
2237         local_bh_disable();
2238         bh_lock_sock_nested(sk);
2239
2240         if (ssp->smk_out == smack_net_ambient ||
2241             labeled == SMACK_UNLABELED_SOCKET)
2242                 netlbl_sock_delattr(sk);
2243         else {
2244                 skp = ssp->smk_out;
2245                 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2246         }
2247
2248         bh_unlock_sock(sk);
2249         local_bh_enable();
2250
2251         return rc;
2252 }
2253
2254 /**
2255  * smack_netlbel_send - Set the secattr on a socket and perform access checks
2256  * @sk: the socket
2257  * @sap: the destination address
2258  *
2259  * Set the correct secattr for the given socket based on the destination
2260  * address and perform any outbound access checks needed.
2261  *
2262  * Returns 0 on success or an error code.
2263  *
2264  */
2265 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2266 {
2267         struct smack_known *skp;
2268         int rc;
2269         int sk_lbl;
2270         char *hostsp;
2271         struct socket_smack *ssp = sk->sk_security;
2272         struct smk_audit_info ad;
2273
2274         rcu_read_lock();
2275         hostsp = smack_host_label(sap);
2276         if (hostsp != NULL) {
2277 #ifdef CONFIG_AUDIT
2278                 struct lsm_network_audit net;
2279
2280                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2281                 ad.a.u.net->family = sap->sin_family;
2282                 ad.a.u.net->dport = sap->sin_port;
2283                 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2284 #endif
2285                 sk_lbl = SMACK_UNLABELED_SOCKET;
2286                 skp = ssp->smk_out;
2287                 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
2288                 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
2289         } else {
2290                 sk_lbl = SMACK_CIPSO_SOCKET;
2291                 rc = 0;
2292         }
2293         rcu_read_unlock();
2294         if (rc != 0)
2295                 return rc;
2296
2297         return smack_netlabel(sk, sk_lbl);
2298 }
2299
2300 /**
2301  * smk_ipv6_port_label - Smack port access table management
2302  * @sock: socket
2303  * @address: address
2304  *
2305  * Create or update the port list entry
2306  */
2307 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2308 {
2309         struct sock *sk = sock->sk;
2310         struct sockaddr_in6 *addr6;
2311         struct socket_smack *ssp = sock->sk->sk_security;
2312         struct smk_port_label *spp;
2313         unsigned short port = 0;
2314
2315         if (address == NULL) {
2316                 /*
2317                  * This operation is changing the Smack information
2318                  * on the bound socket. Take the changes to the port
2319                  * as well.
2320                  */
2321                 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2322                         if (sk != spp->smk_sock)
2323                                 continue;
2324                         spp->smk_in = ssp->smk_in;
2325                         spp->smk_out = ssp->smk_out;
2326                         return;
2327                 }
2328                 /*
2329                  * A NULL address is only used for updating existing
2330                  * bound entries. If there isn't one, it's OK.
2331                  */
2332                 return;
2333         }
2334
2335         addr6 = (struct sockaddr_in6 *)address;
2336         port = ntohs(addr6->sin6_port);
2337         /*
2338          * This is a special case that is safely ignored.
2339          */
2340         if (port == 0)
2341                 return;
2342
2343         /*
2344          * Look for an existing port list entry.
2345          * This is an indication that a port is getting reused.
2346          */
2347         list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2348                 if (spp->smk_port != port)
2349                         continue;
2350                 spp->smk_port = port;
2351                 spp->smk_sock = sk;
2352                 spp->smk_in = ssp->smk_in;
2353                 spp->smk_out = ssp->smk_out;
2354                 return;
2355         }
2356
2357         /*
2358          * A new port entry is required.
2359          */
2360         spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2361         if (spp == NULL)
2362                 return;
2363
2364         spp->smk_port = port;
2365         spp->smk_sock = sk;
2366         spp->smk_in = ssp->smk_in;
2367         spp->smk_out = ssp->smk_out;
2368
2369         list_add(&spp->list, &smk_ipv6_port_list);
2370         return;
2371 }
2372
2373 /**
2374  * smk_ipv6_port_check - check Smack port access
2375  * @sock: socket
2376  * @address: address
2377  *
2378  * Create or update the port list entry
2379  */
2380 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2381                                 int act)
2382 {
2383         __be16 *bep;
2384         __be32 *be32p;
2385         struct smk_port_label *spp;
2386         struct socket_smack *ssp = sk->sk_security;
2387         struct smack_known *skp;
2388         unsigned short port = 0;
2389         char *object;
2390         struct smk_audit_info ad;
2391         int rc;
2392 #ifdef CONFIG_AUDIT
2393         struct lsm_network_audit net;
2394 #endif
2395
2396         if (act == SMK_RECEIVING) {
2397                 skp = smack_net_ambient;
2398                 object = ssp->smk_in->smk_known;
2399         } else {
2400                 skp = ssp->smk_out;
2401                 object = smack_net_ambient->smk_known;
2402         }
2403
2404         /*
2405          * Get the IP address and port from the address.
2406          */
2407         port = ntohs(address->sin6_port);
2408         bep = (__be16 *)(&address->sin6_addr);
2409         be32p = (__be32 *)(&address->sin6_addr);
2410
2411         /*
2412          * It's remote, so port lookup does no good.
2413          */
2414         if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2415                 goto auditout;
2416
2417         /*
2418          * It's local so the send check has to have passed.
2419          */
2420         if (act == SMK_RECEIVING) {
2421                 skp = &smack_known_web;
2422                 goto auditout;
2423         }
2424
2425         list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2426                 if (spp->smk_port != port)
2427                         continue;
2428                 object = spp->smk_in->smk_known;
2429                 if (act == SMK_CONNECTING)
2430                         ssp->smk_packet = spp->smk_out;
2431                 break;
2432         }
2433
2434 auditout:
2435
2436 #ifdef CONFIG_AUDIT
2437         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2438         ad.a.u.net->family = sk->sk_family;
2439         ad.a.u.net->dport = port;
2440         if (act == SMK_RECEIVING)
2441                 ad.a.u.net->v6info.saddr = address->sin6_addr;
2442         else
2443                 ad.a.u.net->v6info.daddr = address->sin6_addr;
2444 #endif
2445         rc = smk_access(skp, object, MAY_WRITE, &ad);
2446         rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2447         return rc;
2448 }
2449
2450 /**
2451  * smack_inode_setsecurity - set smack xattrs
2452  * @inode: the object
2453  * @name: attribute name
2454  * @value: attribute value
2455  * @size: size of the attribute
2456  * @flags: unused
2457  *
2458  * Sets the named attribute in the appropriate blob
2459  *
2460  * Returns 0 on success, or an error code
2461  */
2462 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2463                                    const void *value, size_t size, int flags)
2464 {
2465         struct smack_known *skp;
2466         struct inode_smack *nsp = inode->i_security;
2467         struct socket_smack *ssp;
2468         struct socket *sock;
2469         int rc = 0;
2470
2471         if (value == NULL || size > SMK_LONGLABEL || size == 0)
2472                 return -EACCES;
2473
2474         skp = smk_import_entry(value, size);
2475         if (skp == NULL)
2476                 return -EINVAL;
2477
2478         if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2479                 nsp->smk_inode = skp->smk_known;
2480                 nsp->smk_flags |= SMK_INODE_INSTANT;
2481                 return 0;
2482         }
2483         /*
2484          * The rest of the Smack xattrs are only on sockets.
2485          */
2486         if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2487                 return -EOPNOTSUPP;
2488
2489         sock = SOCKET_I(inode);
2490         if (sock == NULL || sock->sk == NULL)
2491                 return -EOPNOTSUPP;
2492
2493         ssp = sock->sk->sk_security;
2494
2495         if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2496                 ssp->smk_in = skp;
2497         else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2498                 ssp->smk_out = skp;
2499                 if (sock->sk->sk_family == PF_INET) {
2500                         rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2501                         if (rc != 0)
2502                                 printk(KERN_WARNING
2503                                         "Smack: \"%s\" netlbl error %d.\n",
2504                                         __func__, -rc);
2505                 }
2506         } else
2507                 return -EOPNOTSUPP;
2508
2509         if (sock->sk->sk_family == PF_INET6)
2510                 smk_ipv6_port_label(sock, NULL);
2511
2512         return 0;
2513 }
2514
2515 /**
2516  * smack_socket_post_create - finish socket setup
2517  * @sock: the socket
2518  * @family: protocol family
2519  * @type: unused
2520  * @protocol: unused
2521  * @kern: unused
2522  *
2523  * Sets the netlabel information on the socket
2524  *
2525  * Returns 0 on success, and error code otherwise
2526  */
2527 static int smack_socket_post_create(struct socket *sock, int family,
2528                                     int type, int protocol, int kern)
2529 {
2530         struct socket_smack *ssp;
2531
2532         if (sock->sk == NULL)
2533                 return 0;
2534
2535         /*
2536          * Sockets created by kernel threads receive web label.
2537          */
2538         if (unlikely(current->flags & PF_KTHREAD)) {
2539                 ssp = sock->sk->sk_security;
2540                 ssp->smk_in = &smack_known_web;
2541         }
2542         if (family != PF_INET)
2543                 return 0;
2544         /*
2545          * Set the outbound netlbl.
2546          */
2547         return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2548 }
2549
2550 /**
2551  * smack_socket_bind - record port binding information.
2552  * @sock: the socket
2553  * @address: the port address
2554  * @addrlen: size of the address
2555  *
2556  * Records the label bound to a port.
2557  *
2558  * Returns 0
2559  */
2560 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2561                                 int addrlen)
2562 {
2563         if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2564                 smk_ipv6_port_label(sock, address);
2565
2566         return 0;
2567 }
2568
2569 /**
2570  * smack_socket_connect - connect access check
2571  * @sock: the socket
2572  * @sap: the other end
2573  * @addrlen: size of sap
2574  *
2575  * Verifies that a connection may be possible
2576  *
2577  * Returns 0 on success, and error code otherwise
2578  */
2579 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2580                                 int addrlen)
2581 {
2582         int rc = 0;
2583
2584         if (sock->sk == NULL)
2585                 return 0;
2586
2587         switch (sock->sk->sk_family) {
2588         case PF_INET:
2589                 if (addrlen < sizeof(struct sockaddr_in))
2590                         return -EINVAL;
2591                 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2592                 break;
2593         case PF_INET6:
2594                 if (addrlen < sizeof(struct sockaddr_in6))
2595                         return -EINVAL;
2596                 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2597                                                 SMK_CONNECTING);
2598                 break;
2599         }
2600         return rc;
2601 }
2602
2603 /**
2604  * smack_flags_to_may - convert S_ to MAY_ values
2605  * @flags: the S_ value
2606  *
2607  * Returns the equivalent MAY_ value
2608  */
2609 static int smack_flags_to_may(int flags)
2610 {
2611         int may = 0;
2612
2613         if (flags & S_IRUGO)
2614                 may |= MAY_READ;
2615         if (flags & S_IWUGO)
2616                 may |= MAY_WRITE;
2617         if (flags & S_IXUGO)
2618                 may |= MAY_EXEC;
2619
2620         return may;
2621 }
2622
2623 /**
2624  * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2625  * @msg: the object
2626  *
2627  * Returns 0
2628  */
2629 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2630 {
2631         struct smack_known *skp = smk_of_current();
2632
2633         msg->security = skp->smk_known;
2634         return 0;
2635 }
2636
2637 /**
2638  * smack_msg_msg_free_security - Clear the security blob for msg_msg
2639  * @msg: the object
2640  *
2641  * Clears the blob pointer
2642  */
2643 static void smack_msg_msg_free_security(struct msg_msg *msg)
2644 {
2645         msg->security = NULL;
2646 }
2647
2648 /**
2649  * smack_of_shm - the smack pointer for the shm
2650  * @shp: the object
2651  *
2652  * Returns a pointer to the smack value
2653  */
2654 static char *smack_of_shm(struct shmid_kernel *shp)
2655 {
2656         return (char *)shp->shm_perm.security;
2657 }
2658
2659 /**
2660  * smack_shm_alloc_security - Set the security blob for shm
2661  * @shp: the object
2662  *
2663  * Returns 0
2664  */
2665 static int smack_shm_alloc_security(struct shmid_kernel *shp)
2666 {
2667         struct kern_ipc_perm *isp = &shp->shm_perm;
2668         struct smack_known *skp = smk_of_current();
2669
2670         isp->security = skp->smk_known;
2671         return 0;
2672 }
2673
2674 /**
2675  * smack_shm_free_security - Clear the security blob for shm
2676  * @shp: the object
2677  *
2678  * Clears the blob pointer
2679  */
2680 static void smack_shm_free_security(struct shmid_kernel *shp)
2681 {
2682         struct kern_ipc_perm *isp = &shp->shm_perm;
2683
2684         isp->security = NULL;
2685 }
2686
2687 /**
2688  * smk_curacc_shm : check if current has access on shm
2689  * @shp : the object
2690  * @access : access requested
2691  *
2692  * Returns 0 if current has the requested access, error code otherwise
2693  */
2694 static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2695 {
2696         char *ssp = smack_of_shm(shp);
2697         struct smk_audit_info ad;
2698         int rc;
2699
2700 #ifdef CONFIG_AUDIT
2701         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2702         ad.a.u.ipc_id = shp->shm_perm.id;
2703 #endif
2704         rc = smk_curacc(ssp, access, &ad);
2705         rc = smk_bu_current("shm", ssp, access, rc);
2706         return rc;
2707 }
2708
2709 /**
2710  * smack_shm_associate - Smack access check for shm
2711  * @shp: the object
2712  * @shmflg: access requested
2713  *
2714  * Returns 0 if current has the requested access, error code otherwise
2715  */
2716 static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2717 {
2718         int may;
2719
2720         may = smack_flags_to_may(shmflg);
2721         return smk_curacc_shm(shp, may);
2722 }
2723
2724 /**
2725  * smack_shm_shmctl - Smack access check for shm
2726  * @shp: the object
2727  * @cmd: what it wants to do
2728  *
2729  * Returns 0 if current has the requested access, error code otherwise
2730  */
2731 static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2732 {
2733         int may;
2734
2735         switch (cmd) {
2736         case IPC_STAT:
2737         case SHM_STAT:
2738                 may = MAY_READ;
2739                 break;
2740         case IPC_SET:
2741         case SHM_LOCK:
2742         case SHM_UNLOCK:
2743         case IPC_RMID:
2744                 may = MAY_READWRITE;
2745                 break;
2746         case IPC_INFO:
2747         case SHM_INFO:
2748                 /*
2749                  * System level information.
2750                  */
2751                 return 0;
2752         default:
2753                 return -EINVAL;
2754         }
2755         return smk_curacc_shm(shp, may);
2756 }
2757
2758 /**
2759  * smack_shm_shmat - Smack access for shmat
2760  * @shp: the object
2761  * @shmaddr: unused
2762  * @shmflg: access requested
2763  *
2764  * Returns 0 if current has the requested access, error code otherwise
2765  */
2766 static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2767                            int shmflg)
2768 {
2769         int may;
2770
2771         may = smack_flags_to_may(shmflg);
2772         return smk_curacc_shm(shp, may);
2773 }
2774
2775 /**
2776  * smack_of_sem - the smack pointer for the sem
2777  * @sma: the object
2778  *
2779  * Returns a pointer to the smack value
2780  */
2781 static char *smack_of_sem(struct sem_array *sma)
2782 {
2783         return (char *)sma->sem_perm.security;
2784 }
2785
2786 /**
2787  * smack_sem_alloc_security - Set the security blob for sem
2788  * @sma: the object
2789  *
2790  * Returns 0
2791  */
2792 static int smack_sem_alloc_security(struct sem_array *sma)
2793 {
2794         struct kern_ipc_perm *isp = &sma->sem_perm;
2795         struct smack_known *skp = smk_of_current();
2796
2797         isp->security = skp->smk_known;
2798         return 0;
2799 }
2800
2801 /**
2802  * smack_sem_free_security - Clear the security blob for sem
2803  * @sma: the object
2804  *
2805  * Clears the blob pointer
2806  */
2807 static void smack_sem_free_security(struct sem_array *sma)
2808 {
2809         struct kern_ipc_perm *isp = &sma->sem_perm;
2810
2811         isp->security = NULL;
2812 }
2813
2814 /**
2815  * smk_curacc_sem : check if current has access on sem
2816  * @sma : the object
2817  * @access : access requested
2818  *
2819  * Returns 0 if current has the requested access, error code otherwise
2820  */
2821 static int smk_curacc_sem(struct sem_array *sma, int access)
2822 {
2823         char *ssp = smack_of_sem(sma);
2824         struct smk_audit_info ad;
2825         int rc;
2826
2827 #ifdef CONFIG_AUDIT
2828         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2829         ad.a.u.ipc_id = sma->sem_perm.id;
2830 #endif
2831         rc = smk_curacc(ssp, access, &ad);
2832         rc = smk_bu_current("sem", ssp, access, rc);
2833         return rc;
2834 }
2835
2836 /**
2837  * smack_sem_associate - Smack access check for sem
2838  * @sma: the object
2839  * @semflg: access requested
2840  *
2841  * Returns 0 if current has the requested access, error code otherwise
2842  */
2843 static int smack_sem_associate(struct sem_array *sma, int semflg)
2844 {
2845         int may;
2846
2847         may = smack_flags_to_may(semflg);
2848         return smk_curacc_sem(sma, may);
2849 }
2850
2851 /**
2852  * smack_sem_shmctl - Smack access check for sem
2853  * @sma: the object
2854  * @cmd: what it wants to do
2855  *
2856  * Returns 0 if current has the requested access, error code otherwise
2857  */
2858 static int smack_sem_semctl(struct sem_array *sma, int cmd)
2859 {
2860         int may;
2861
2862         switch (cmd) {
2863         case GETPID:
2864         case GETNCNT:
2865         case GETZCNT:
2866         case GETVAL:
2867         case GETALL:
2868         case IPC_STAT:
2869         case SEM_STAT:
2870                 may = MAY_READ;
2871                 break;
2872         case SETVAL:
2873         case SETALL:
2874         case IPC_RMID:
2875         case IPC_SET:
2876                 may = MAY_READWRITE;
2877                 break;
2878         case IPC_INFO:
2879         case SEM_INFO:
2880                 /*
2881                  * System level information
2882                  */
2883                 return 0;
2884         default:
2885                 return -EINVAL;
2886         }
2887
2888         return smk_curacc_sem(sma, may);
2889 }
2890
2891 /**
2892  * smack_sem_semop - Smack checks of semaphore operations
2893  * @sma: the object
2894  * @sops: unused
2895  * @nsops: unused
2896  * @alter: unused
2897  *
2898  * Treated as read and write in all cases.
2899  *
2900  * Returns 0 if access is allowed, error code otherwise
2901  */
2902 static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2903                            unsigned nsops, int alter)
2904 {
2905         return smk_curacc_sem(sma, MAY_READWRITE);
2906 }
2907
2908 /**
2909  * smack_msg_alloc_security - Set the security blob for msg
2910  * @msq: the object
2911  *
2912  * Returns 0
2913  */
2914 static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2915 {
2916         struct kern_ipc_perm *kisp = &msq->q_perm;
2917         struct smack_known *skp = smk_of_current();
2918
2919         kisp->security = skp->smk_known;
2920         return 0;
2921 }
2922
2923 /**
2924  * smack_msg_free_security - Clear the security blob for msg
2925  * @msq: the object
2926  *
2927  * Clears the blob pointer
2928  */
2929 static void smack_msg_queue_free_security(struct msg_queue *msq)
2930 {
2931         struct kern_ipc_perm *kisp = &msq->q_perm;
2932
2933         kisp->security = NULL;
2934 }
2935
2936 /**
2937  * smack_of_msq - the smack pointer for the msq
2938  * @msq: the object
2939  *
2940  * Returns a pointer to the smack value
2941  */
2942 static char *smack_of_msq(struct msg_queue *msq)
2943 {
2944         return (char *)msq->q_perm.security;
2945 }
2946
2947 /**
2948  * smk_curacc_msq : helper to check if current has access on msq
2949  * @msq : the msq
2950  * @access : access requested
2951  *
2952  * return 0 if current has access, error otherwise
2953  */
2954 static int smk_curacc_msq(struct msg_queue *msq, int access)
2955 {
2956         char *msp = smack_of_msq(msq);
2957         struct smk_audit_info ad;
2958         int rc;
2959
2960 #ifdef CONFIG_AUDIT
2961         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2962         ad.a.u.ipc_id = msq->q_perm.id;
2963 #endif
2964         rc = smk_curacc(msp, access, &ad);
2965         rc = smk_bu_current("msq", msp, access, rc);
2966         return rc;
2967 }
2968
2969 /**
2970  * smack_msg_queue_associate - Smack access check for msg_queue
2971  * @msq: the object
2972  * @msqflg: access requested
2973  *
2974  * Returns 0 if current has the requested access, error code otherwise
2975  */
2976 static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2977 {
2978         int may;
2979
2980         may = smack_flags_to_may(msqflg);
2981         return smk_curacc_msq(msq, may);
2982 }
2983
2984 /**
2985  * smack_msg_queue_msgctl - Smack access check for msg_queue
2986  * @msq: the object
2987  * @cmd: what it wants to do
2988  *
2989  * Returns 0 if current has the requested access, error code otherwise
2990  */
2991 static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2992 {
2993         int may;
2994
2995         switch (cmd) {
2996         case IPC_STAT:
2997         case MSG_STAT:
2998                 may = MAY_READ;
2999                 break;
3000         case IPC_SET:
3001         case IPC_RMID:
3002                 may = MAY_READWRITE;
3003                 break;
3004         case IPC_INFO:
3005         case MSG_INFO:
3006                 /*
3007                  * System level information
3008                  */
3009                 return 0;
3010         default:
3011                 return -EINVAL;
3012         }
3013
3014         return smk_curacc_msq(msq, may);
3015 }
3016
3017 /**
3018  * smack_msg_queue_msgsnd - Smack access check for msg_queue
3019  * @msq: the object
3020  * @msg: unused
3021  * @msqflg: access requested
3022  *
3023  * Returns 0 if current has the requested access, error code otherwise
3024  */
3025 static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3026                                   int msqflg)
3027 {
3028         int may;
3029
3030         may = smack_flags_to_may(msqflg);
3031         return smk_curacc_msq(msq, may);
3032 }
3033
3034 /**
3035  * smack_msg_queue_msgsnd - Smack access check for msg_queue
3036  * @msq: the object
3037  * @msg: unused
3038  * @target: unused
3039  * @type: unused
3040  * @mode: unused
3041  *
3042  * Returns 0 if current has read and write access, error code otherwise
3043  */
3044 static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3045                         struct task_struct *target, long type, int mode)
3046 {
3047         return smk_curacc_msq(msq, MAY_READWRITE);
3048 }
3049
3050 /**
3051  * smack_ipc_permission - Smack access for ipc_permission()
3052  * @ipp: the object permissions
3053  * @flag: access requested
3054  *
3055  * Returns 0 if current has read and write access, error code otherwise
3056  */
3057 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3058 {
3059         char *isp = ipp->security;
3060         int may = smack_flags_to_may(flag);
3061         struct smk_audit_info ad;
3062         int rc;
3063
3064 #ifdef CONFIG_AUDIT
3065         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3066         ad.a.u.ipc_id = ipp->id;
3067 #endif
3068         rc = smk_curacc(isp, may, &ad);
3069         rc = smk_bu_current("svipc", isp, may, rc);
3070         return rc;
3071 }
3072
3073 /**
3074  * smack_ipc_getsecid - Extract smack security id
3075  * @ipp: the object permissions
3076  * @secid: where result will be saved
3077  */
3078 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3079 {
3080         char *smack = ipp->security;
3081
3082         *secid = smack_to_secid(smack);
3083 }
3084
3085 /**
3086  * smack_d_instantiate - Make sure the blob is correct on an inode
3087  * @opt_dentry: dentry where inode will be attached
3088  * @inode: the object
3089  *
3090  * Set the inode's security blob if it hasn't been done already.
3091  */
3092 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3093 {
3094         struct super_block *sbp;
3095         struct superblock_smack *sbsp;
3096         struct inode_smack *isp;
3097         struct smack_known *skp;
3098         struct smack_known *ckp = smk_of_current();
3099         char *final;
3100         char trattr[TRANS_TRUE_SIZE];
3101         int transflag = 0;
3102         int rc;
3103         struct dentry *dp;
3104
3105         if (inode == NULL)
3106                 return;
3107
3108         isp = inode->i_security;
3109
3110         mutex_lock(&isp->smk_lock);
3111         /*
3112          * If the inode is already instantiated
3113          * take the quick way out
3114          */
3115         if (isp->smk_flags & SMK_INODE_INSTANT)
3116                 goto unlockandout;
3117
3118         sbp = inode->i_sb;
3119         sbsp = sbp->s_security;
3120         /*
3121          * We're going to use the superblock default label
3122          * if there's no label on the file.
3123          */
3124         final = sbsp->smk_default;
3125
3126         /*
3127          * If this is the root inode the superblock
3128          * may be in the process of initialization.
3129          * If that is the case use the root value out
3130          * of the superblock.
3131          */
3132         if (opt_dentry->d_parent == opt_dentry) {
3133                 switch (sbp->s_magic) {
3134                 case CGROUP_SUPER_MAGIC:
3135                         /*
3136                          * The cgroup filesystem is never mounted,
3137                          * so there's no opportunity to set the mount
3138                          * options.
3139                          */
3140                         sbsp->smk_root = smack_known_star.smk_known;
3141                         sbsp->smk_default = smack_known_star.smk_known;
3142                         isp->smk_inode = sbsp->smk_root;
3143                         break;
3144                 case TMPFS_MAGIC:
3145                         /*
3146                          * What about shmem/tmpfs anonymous files with dentry
3147                          * obtained from d_alloc_pseudo()?
3148                          */
3149                         isp->smk_inode = smk_of_current()->smk_known;
3150                         break;
3151                 case PIPEFS_MAGIC:
3152                         isp->smk_inode = smk_of_current()->smk_known;
3153                         break;
3154                 default:
3155                         isp->smk_inode = sbsp->smk_root;
3156                         break;
3157                 }
3158                 isp->smk_flags |= SMK_INODE_INSTANT;
3159                 goto unlockandout;
3160         }
3161
3162         /*
3163          * This is pretty hackish.
3164          * Casey says that we shouldn't have to do
3165          * file system specific code, but it does help
3166          * with keeping it simple.
3167          */
3168         switch (sbp->s_magic) {
3169         case SMACK_MAGIC:
3170         case PIPEFS_MAGIC:
3171         case SOCKFS_MAGIC:
3172         case CGROUP_SUPER_MAGIC:
3173                 /*
3174                  * Casey says that it's a little embarrassing
3175                  * that the smack file system doesn't do
3176                  * extended attributes.
3177                  *
3178                  * Casey says pipes are easy (?)
3179                  *
3180                  * Socket access is controlled by the socket
3181                  * structures associated with the task involved.
3182                  *
3183                  * Cgroupfs is special
3184                  */
3185                 final = smack_known_star.smk_known;
3186                 break;
3187         case DEVPTS_SUPER_MAGIC:
3188                 /*
3189                  * devpts seems content with the label of the task.
3190                  * Programs that change smack have to treat the
3191                  * pty with respect.
3192                  */
3193                 final = ckp->smk_known;
3194                 break;
3195         case PROC_SUPER_MAGIC:
3196                 /*
3197                  * Casey says procfs appears not to care.
3198                  * The superblock default suffices.
3199                  */
3200                 break;
3201         case TMPFS_MAGIC:
3202                 /*
3203                  * Device labels should come from the filesystem,
3204                  * but watch out, because they're volitile,
3205                  * getting recreated on every reboot.
3206                  */
3207                 final = smack_known_star.smk_known;
3208                 /*
3209                  * No break.
3210                  *
3211                  * If a smack value has been set we want to use it,
3212                  * but since tmpfs isn't giving us the opportunity
3213                  * to set mount options simulate setting the
3214                  * superblock default.
3215                  */
3216         default:
3217                 /*
3218                  * This isn't an understood special case.
3219                  * Get the value from the xattr.
3220                  */
3221
3222                 /*
3223                  * UNIX domain sockets use lower level socket data.
3224                  */
3225                 if (S_ISSOCK(inode->i_mode)) {
3226                         final = smack_known_star.smk_known;
3227                         break;
3228                 }
3229                 /*
3230                  * No xattr support means, alas, no SMACK label.
3231                  * Use the aforeapplied default.
3232                  * It would be curious if the label of the task
3233                  * does not match that assigned.
3234                  */
3235                 if (inode->i_op->getxattr == NULL)
3236                         break;
3237                 /*
3238                  * Get the dentry for xattr.
3239                  */
3240                 dp = dget(opt_dentry);
3241                 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3242                 if (skp != NULL)
3243                         final = skp->smk_known;
3244
3245                 /*
3246                  * Transmuting directory
3247                  */
3248                 if (S_ISDIR(inode->i_mode)) {
3249                         /*
3250                          * If this is a new directory and the label was
3251                          * transmuted when the inode was initialized
3252                          * set the transmute attribute on the directory
3253                          * and mark the inode.
3254                          *
3255                          * If there is a transmute attribute on the
3256                          * directory mark the inode.
3257                          */
3258                         if (isp->smk_flags & SMK_INODE_CHANGED) {
3259                                 isp->smk_flags &= ~SMK_INODE_CHANGED;
3260                                 rc = inode->i_op->setxattr(dp,
3261                                         XATTR_NAME_SMACKTRANSMUTE,
3262                                         TRANS_TRUE, TRANS_TRUE_SIZE,
3263                                         0);
3264                         } else {
3265                                 rc = inode->i_op->getxattr(dp,
3266                                         XATTR_NAME_SMACKTRANSMUTE, trattr,
3267                                         TRANS_TRUE_SIZE);
3268                                 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3269                                                        TRANS_TRUE_SIZE) != 0)
3270                                         rc = -EINVAL;
3271                         }
3272                         if (rc >= 0)
3273                                 transflag = SMK_INODE_TRANSMUTE;
3274                 }
3275                 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3276                 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3277
3278                 dput(dp);
3279                 break;
3280         }
3281
3282         if (final == NULL)
3283                 isp->smk_inode = ckp->smk_known;
3284         else
3285                 isp->smk_inode = final;
3286
3287         isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3288
3289 unlockandout:
3290         mutex_unlock(&isp->smk_lock);
3291         return;
3292 }
3293
3294 /**
3295  * smack_getprocattr - Smack process attribute access
3296  * @p: the object task
3297  * @name: the name of the attribute in /proc/.../attr
3298  * @value: where to put the result
3299  *
3300  * Places a copy of the task Smack into value
3301  *
3302  * Returns the length of the smack label or an error code
3303  */
3304 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3305 {
3306         struct smack_known *skp = smk_of_task(task_security(p));
3307         char *cp;
3308         int slen;
3309
3310         if (strcmp(name, "current") != 0)
3311                 return -EINVAL;
3312
3313         cp = kstrdup(skp->smk_known, GFP_KERNEL);
3314         if (cp == NULL)
3315                 return -ENOMEM;
3316
3317         slen = strlen(cp);
3318         *value = cp;
3319         return slen;
3320 }
3321
3322 /**
3323  * smack_setprocattr - Smack process attribute setting
3324  * @p: the object task
3325  * @name: the name of the attribute in /proc/.../attr
3326  * @value: the value to set
3327  * @size: the size of the value
3328  *
3329  * Sets the Smack value of the task. Only setting self
3330  * is permitted and only with privilege
3331  *
3332  * Returns the length of the smack label or an error code
3333  */
3334 static int smack_setprocattr(struct task_struct *p, char *name,
3335                              void *value, size_t size)
3336 {
3337         struct task_smack *tsp = current_security();
3338         struct cred *new;
3339         struct smack_known *skp;
3340         struct smack_known_list_elem *sklep;
3341         int rc;
3342
3343         /*
3344          * Changing another process' Smack value is too dangerous
3345          * and supports no sane use case.
3346          */
3347         if (p != current)
3348                 return -EPERM;
3349
3350         if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3351                 return -EPERM;
3352
3353         if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3354                 return -EINVAL;
3355
3356         if (strcmp(name, "current") != 0)
3357                 return -EINVAL;
3358
3359         skp = smk_import_entry(value, size);
3360         if (skp == NULL)
3361                 return -EINVAL;
3362
3363         /*
3364          * No process is ever allowed the web ("@") label.
3365          */
3366         if (skp == &smack_known_web)
3367                 return -EPERM;
3368
3369         if (!smack_privileged(CAP_MAC_ADMIN)) {
3370                 rc = -EPERM;
3371                 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3372                         if (sklep->smk_label == skp) {
3373                                 rc = 0;
3374                                 break;
3375                         }
3376                 if (rc)
3377                         return rc;
3378         }
3379
3380         new = prepare_creds();
3381         if (new == NULL)
3382                 return -ENOMEM;
3383
3384         tsp = new->security;
3385         tsp->smk_task = skp;
3386         /*
3387          * process can change its label only once
3388          */
3389         smk_destroy_label_list(&tsp->smk_relabel);
3390
3391         commit_creds(new);
3392         return size;
3393 }
3394
3395 /**
3396  * smack_unix_stream_connect - Smack access on UDS
3397  * @sock: one sock
3398  * @other: the other sock
3399  * @newsk: unused
3400  *
3401  * Return 0 if a subject with the smack of sock could access
3402  * an object with the smack of other, otherwise an error code
3403  */
3404 static int smack_unix_stream_connect(struct sock *sock,
3405                                      struct sock *other, struct sock *newsk)
3406 {
3407         struct smack_known *skp;
3408         struct smack_known *okp;
3409         struct socket_smack *ssp = sock->sk_security;
3410         struct socket_smack *osp = other->sk_security;
3411         struct socket_smack *nsp = newsk->sk_security;
3412         struct smk_audit_info ad;
3413         int rc = 0;
3414 #ifdef CONFIG_AUDIT
3415         struct lsm_network_audit net;
3416 #endif
3417
3418         if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3419                 skp = ssp->smk_out;
3420                 okp = osp->smk_in;
3421 #ifdef CONFIG_AUDIT
3422                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3423                 smk_ad_setfield_u_net_sk(&ad, other);
3424 #endif
3425                 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3426                 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3427                                         MAY_WRITE, rc);
3428                 if (rc == 0) {
3429                         okp = osp->smk_out;
3430                         skp = ssp->smk_in;
3431                         rc = smk_access(okp, skp->smk_known, MAY_WRITE, &ad);
3432                         rc = smk_bu_note("UDS connect", okp, skp->smk_known,
3433                                                 MAY_WRITE, rc);
3434                 }
3435         }
3436
3437         /*
3438          * Cross reference the peer labels for SO_PEERSEC.
3439          */
3440         if (rc == 0) {
3441                 nsp->smk_packet = ssp->smk_out;
3442                 ssp->smk_packet = osp->smk_out;
3443         }
3444
3445         return rc;
3446 }
3447
3448 /**
3449  * smack_unix_may_send - Smack access on UDS
3450  * @sock: one socket
3451  * @other: the other socket
3452  *
3453  * Return 0 if a subject with the smack of sock could access
3454  * an object with the smack of other, otherwise an error code
3455  */
3456 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3457 {
3458         struct socket_smack *ssp = sock->sk->sk_security;
3459         struct socket_smack *osp = other->sk->sk_security;
3460         struct smack_known *skp;
3461         struct smk_audit_info ad;
3462         int rc;
3463
3464 #ifdef CONFIG_AUDIT
3465         struct lsm_network_audit net;
3466
3467         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3468         smk_ad_setfield_u_net_sk(&ad, other->sk);
3469 #endif
3470
3471         if (smack_privileged(CAP_MAC_OVERRIDE))
3472                 return 0;
3473
3474         skp = ssp->smk_out;
3475
3476         rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3477         rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3478                                 MAY_WRITE, rc);
3479         return rc;
3480 }
3481
3482 /**
3483  * smack_socket_sendmsg - Smack check based on destination host
3484  * @sock: the socket
3485  * @msg: the message
3486  * @size: the size of the message
3487  *
3488  * Return 0 if the current subject can write to the destination host.
3489  * For IPv4 this is only a question if the destination is a single label host.
3490  * For IPv6 this is a check against the label of the port.
3491  */
3492 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3493                                 int size)
3494 {
3495         struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3496         struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3497         int rc = 0;
3498
3499         /*
3500          * Perfectly reasonable for this to be NULL
3501          */
3502         if (sip == NULL)
3503                 return 0;
3504
3505         switch (sock->sk->sk_family) {
3506         case AF_INET:
3507                 rc = smack_netlabel_send(sock->sk, sip);
3508                 break;
3509         case AF_INET6:
3510                 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3511                 break;
3512         }
3513         return rc;
3514 }
3515
3516 /**
3517  * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3518  * @sap: netlabel secattr
3519  * @ssp: socket security information
3520  *
3521  * Returns a pointer to a Smack label entry found on the label list.
3522  */
3523 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3524                                                 struct socket_smack *ssp)
3525 {
3526         struct smack_known *skp;
3527         int found = 0;
3528         int acat;
3529         int kcat;
3530
3531         if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3532                 /*
3533                  * Looks like a CIPSO packet.
3534                  * If there are flags but no level netlabel isn't
3535                  * behaving the way we expect it to.
3536                  *
3537                  * Look it up in the label table
3538                  * Without guidance regarding the smack value
3539                  * for the packet fall back on the network
3540                  * ambient value.
3541                  */
3542                 rcu_read_lock();
3543                 list_for_each_entry(skp, &smack_known_list, list) {
3544                         if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3545                                 continue;
3546                         /*
3547                          * Compare the catsets. Use the netlbl APIs.
3548                          */
3549                         if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3550                                 if ((skp->smk_netlabel.flags &
3551                                      NETLBL_SECATTR_MLS_CAT) == 0)
3552                                         found = 1;
3553                                 break;
3554                         }
3555                         for (acat = -1, kcat = -1; acat == kcat; ) {
3556                                 acat = netlbl_secattr_catmap_walk(
3557                                         sap->attr.mls.cat, acat + 1);
3558                                 kcat = netlbl_secattr_catmap_walk(
3559                                         skp->smk_netlabel.attr.mls.cat,
3560                                         kcat + 1);
3561                                 if (acat < 0 || kcat < 0)
3562                                         break;
3563                         }
3564                         if (acat == kcat) {
3565                                 found = 1;
3566                                 break;
3567                         }
3568                 }
3569                 rcu_read_unlock();
3570
3571                 if (found)
3572                         return skp;
3573
3574                 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3575                         return &smack_known_web;
3576                 return &smack_known_star;
3577         }
3578         if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3579                 /*
3580                  * Looks like a fallback, which gives us a secid.
3581                  */
3582                 skp = smack_from_secid(sap->attr.secid);
3583                 /*
3584                  * This has got to be a bug because it is
3585                  * impossible to specify a fallback without
3586                  * specifying the label, which will ensure
3587                  * it has a secid, and the only way to get a
3588                  * secid is from a fallback.
3589                  */
3590                 BUG_ON(skp == NULL);
3591                 return skp;
3592         }
3593         /*
3594          * Without guidance regarding the smack value
3595          * for the packet fall back on the network
3596          * ambient value.
3597          */
3598         return smack_net_ambient;
3599 }
3600
3601 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3602 {
3603         u8 nexthdr;
3604         int offset;
3605         int proto = -EINVAL;
3606         struct ipv6hdr _ipv6h;
3607         struct ipv6hdr *ip6;
3608         __be16 frag_off;
3609         struct tcphdr _tcph, *th;
3610         struct udphdr _udph, *uh;
3611         struct dccp_hdr _dccph, *dh;
3612
3613         sip->sin6_port = 0;
3614
3615         offset = skb_network_offset(skb);
3616         ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3617         if (ip6 == NULL)
3618                 return -EINVAL;
3619         sip->sin6_addr = ip6->saddr;
3620
3621         nexthdr = ip6->nexthdr;
3622         offset += sizeof(_ipv6h);
3623         offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3624         if (offset < 0)
3625                 return -EINVAL;
3626
3627         proto = nexthdr;
3628         switch (proto) {
3629         case IPPROTO_TCP:
3630                 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3631                 if (th != NULL)
3632                         sip->sin6_port = th->source;
3633                 break;
3634         case IPPROTO_UDP:
3635                 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3636                 if (uh != NULL)
3637                         sip->sin6_port = uh->source;
3638                 break;
3639         case IPPROTO_DCCP:
3640                 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3641                 if (dh != NULL)
3642                         sip->sin6_port = dh->dccph_sport;
3643                 break;
3644         }
3645         return proto;
3646 }
3647
3648 /**
3649  * smack_socket_sock_rcv_skb - Smack packet delivery access check
3650  * @sk: socket
3651  * @skb: packet
3652  *
3653  * Returns 0 if the packet should be delivered, an error code otherwise
3654  */
3655 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3656 {
3657         struct netlbl_lsm_secattr secattr;
3658         struct socket_smack *ssp = sk->sk_security;
3659         struct smack_known *skp;
3660         struct sockaddr_in6 sadd;
3661         int rc = 0;
3662         struct smk_audit_info ad;
3663 #ifdef CONFIG_AUDIT
3664         struct lsm_network_audit net;
3665 #endif
3666         switch (sk->sk_family) {
3667         case PF_INET:
3668                 /*
3669                  * Translate what netlabel gave us.
3670                  */
3671                 netlbl_secattr_init(&secattr);
3672
3673                 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3674                 if (rc == 0)
3675                         skp = smack_from_secattr(&secattr, ssp);
3676                 else
3677                         skp = smack_net_ambient;
3678
3679                 netlbl_secattr_destroy(&secattr);
3680
3681 #ifdef CONFIG_AUDIT
3682                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3683                 ad.a.u.net->family = sk->sk_family;
3684                 ad.a.u.net->netif = skb->skb_iif;
3685                 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3686 #endif
3687                 /*
3688                  * Receiving a packet requires that the other end
3689                  * be able to write here. Read access is not required.
3690                  * This is the simplist possible security model
3691                  * for networking.
3692                  */
3693
3694                 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3695                 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3696                                         MAY_WRITE, rc);
3697
3698                 if (rc != 0)
3699                         netlbl_skbuff_err(skb, rc, 0);
3700                 break;
3701         case PF_INET6:
3702                 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3703                 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3704                         rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3705                 else
3706                         rc = 0;
3707                 break;
3708         }
3709         return rc;
3710 }
3711
3712 /**
3713  * smack_socket_getpeersec_stream - pull in packet label
3714  * @sock: the socket
3715  * @optval: user's destination
3716  * @optlen: size thereof
3717  * @len: max thereof
3718  *
3719  * returns zero on success, an error code otherwise
3720  */
3721 static int smack_socket_getpeersec_stream(struct socket *sock,
3722                                           char __user *optval,
3723                                           int __user *optlen, unsigned len)
3724 {
3725         struct socket_smack *ssp;
3726         char *rcp = "";
3727         int slen = 1;
3728         int rc = 0;
3729
3730         ssp = sock->sk->sk_security;
3731         if (ssp->smk_packet != NULL) {
3732                 rcp = ssp->smk_packet->smk_known;
3733                 slen = strlen(rcp) + 1;
3734         }
3735
3736         if (slen > len)
3737                 rc = -ERANGE;
3738         else if (copy_to_user(optval, rcp, slen) != 0)
3739                 rc = -EFAULT;
3740
3741         if (put_user(slen, optlen) != 0)
3742                 rc = -EFAULT;
3743
3744         return rc;
3745 }
3746
3747
3748 /**
3749  * smack_socket_getpeersec_dgram - pull in packet label
3750  * @sock: the peer socket
3751  * @skb: packet data
3752  * @secid: pointer to where to put the secid of the packet
3753  *
3754  * Sets the netlabel socket state on sk from parent
3755  */
3756 static int smack_socket_getpeersec_dgram(struct socket *sock,
3757                                          struct sk_buff *skb, u32 *secid)
3758
3759 {
3760         struct netlbl_lsm_secattr secattr;
3761         struct socket_smack *ssp = NULL;
3762         struct smack_known *skp;
3763         int family = PF_UNSPEC;
3764         u32 s = 0;      /* 0 is the invalid secid */
3765         int rc;
3766
3767         if (skb != NULL) {
3768                 if (skb->protocol == htons(ETH_P_IP))
3769                         family = PF_INET;
3770                 else if (skb->protocol == htons(ETH_P_IPV6))
3771                         family = PF_INET6;
3772         }
3773         if (family == PF_UNSPEC && sock != NULL)
3774                 family = sock->sk->sk_family;
3775
3776         if (family == PF_UNIX) {
3777                 ssp = sock->sk->sk_security;
3778                 s = ssp->smk_out->smk_secid;
3779         } else if (family == PF_INET || family == PF_INET6) {
3780                 /*
3781                  * Translate what netlabel gave us.
3782                  */
3783                 if (sock != NULL && sock->sk != NULL)
3784                         ssp = sock->sk->sk_security;
3785                 netlbl_secattr_init(&secattr);
3786                 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3787                 if (rc == 0) {
3788                         skp = smack_from_secattr(&secattr, ssp);
3789                         s = skp->smk_secid;
3790                 }
3791                 netlbl_secattr_destroy(&secattr);
3792         }
3793         *secid = s;
3794         if (s == 0)
3795                 return -EINVAL;
3796         return 0;
3797 }
3798
3799 /**
3800  * smack_sock_graft - Initialize a newly created socket with an existing sock
3801  * @sk: child sock
3802  * @parent: parent socket
3803  *
3804  * Set the smk_{in,out} state of an existing sock based on the process that
3805  * is creating the new socket.
3806  */
3807 static void smack_sock_graft(struct sock *sk, struct socket *parent)
3808 {
3809         struct socket_smack *ssp;
3810         struct smack_known *skp = smk_of_current();
3811
3812         if (sk == NULL ||
3813             (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
3814                 return;
3815
3816         ssp = sk->sk_security;
3817         ssp->smk_in = skp;
3818         ssp->smk_out = skp;
3819         /* cssp->smk_packet is already set in smack_inet_csk_clone() */
3820 }
3821
3822 /**
3823  * smack_inet_conn_request - Smack access check on connect
3824  * @sk: socket involved
3825  * @skb: packet
3826  * @req: unused
3827  *
3828  * Returns 0 if a task with the packet label could write to
3829  * the socket, otherwise an error code
3830  */
3831 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3832                                    struct request_sock *req)
3833 {
3834         u16 family = sk->sk_family;
3835         struct smack_known *skp;
3836         struct socket_smack *ssp = sk->sk_security;
3837         struct netlbl_lsm_secattr secattr;
3838         struct sockaddr_in addr;
3839         struct iphdr *hdr;
3840         char *hsp;
3841         int rc;
3842         struct smk_audit_info ad;
3843 #ifdef CONFIG_AUDIT
3844         struct lsm_network_audit net;
3845 #endif
3846
3847         if (family == PF_INET6) {
3848                 /*
3849                  * Handle mapped IPv4 packets arriving
3850                  * via IPv6 sockets. Don't set up netlabel
3851                  * processing on IPv6.
3852                  */
3853                 if (skb->protocol == htons(ETH_P_IP))
3854                         family = PF_INET;
3855                 else
3856                         return 0;
3857         }
3858
3859         netlbl_secattr_init(&secattr);
3860         rc = netlbl_skbuff_getattr(skb, family, &secattr);
3861         if (rc == 0)
3862                 skp = smack_from_secattr(&secattr, ssp);
3863         else
3864                 skp = &smack_known_huh;
3865         netlbl_secattr_destroy(&secattr);
3866
3867 #ifdef CONFIG_AUDIT
3868         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3869         ad.a.u.net->family = family;
3870         ad.a.u.net->netif = skb->skb_iif;
3871         ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3872 #endif
3873         /*
3874          * Receiving a packet requires that the other end be able to write
3875          * here. Read access is not required.
3876          */
3877
3878         rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
3879         rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3880                                 MAY_WRITE, rc);
3881         if (rc != 0)
3882                 return rc;
3883
3884         /*
3885          * Save the peer's label in the request_sock so we can later setup
3886          * smk_packet in the child socket so that SO_PEERCRED can report it.
3887          */
3888         req->peer_secid = skp->smk_secid;
3889
3890         /*
3891          * We need to decide if we want to label the incoming connection here
3892          * if we do we only need to label the request_sock and the stack will
3893          * propagate the wire-label to the sock when it is created.
3894          */
3895         hdr = ip_hdr(skb);
3896         addr.sin_addr.s_addr = hdr->saddr;
3897         rcu_read_lock();
3898         hsp = smack_host_label(&addr);
3899         rcu_read_unlock();
3900
3901         if (hsp == NULL)
3902                 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3903         else
3904                 netlbl_req_delattr(req);
3905
3906         return rc;
3907 }
3908
3909 /**
3910  * smack_inet_csk_clone - Copy the connection information to the new socket
3911  * @sk: the new socket
3912  * @req: the connection's request_sock
3913  *
3914  * Transfer the connection's peer label to the newly created socket.
3915  */
3916 static void smack_inet_csk_clone(struct sock *sk,
3917                                  const struct request_sock *req)
3918 {
3919         struct socket_smack *ssp = sk->sk_security;
3920         struct smack_known *skp;
3921
3922         if (req->peer_secid != 0) {
3923                 skp = smack_from_secid(req->peer_secid);
3924                 ssp->smk_packet = skp;
3925         } else
3926                 ssp->smk_packet = NULL;
3927 }
3928
3929 /*
3930  * Key management security hooks
3931  *
3932  * Casey has not tested key support very heavily.
3933  * The permission check is most likely too restrictive.
3934  * If you care about keys please have a look.
3935  */
3936 #ifdef CONFIG_KEYS
3937
3938 /**
3939  * smack_key_alloc - Set the key security blob
3940  * @key: object
3941  * @cred: the credentials to use
3942  * @flags: unused
3943  *
3944  * No allocation required
3945  *
3946  * Returns 0
3947  */
3948 static int smack_key_alloc(struct key *key, const struct cred *cred,
3949                            unsigned long flags)
3950 {
3951         struct smack_known *skp = smk_of_task(cred->security);
3952
3953         key->security = skp->smk_known;
3954         return 0;
3955 }
3956
3957 /**
3958  * smack_key_free - Clear the key security blob
3959  * @key: the object
3960  *
3961  * Clear the blob pointer
3962  */
3963 static void smack_key_free(struct key *key)
3964 {
3965         key->security = NULL;
3966 }
3967
3968 /*
3969  * smack_key_permission - Smack access on a key
3970  * @key_ref: gets to the object
3971  * @cred: the credentials to use
3972  * @perm: unused
3973  *
3974  * Return 0 if the task has read and write to the object,
3975  * an error code otherwise
3976  */
3977 static int smack_key_permission(key_ref_t key_ref,
3978                                 const struct cred *cred, key_perm_t perm)
3979 {
3980         struct key *keyp;
3981         struct smk_audit_info ad;
3982         struct smack_known *tkp = smk_of_task(cred->security);
3983         int rc;
3984
3985         keyp = key_ref_to_ptr(key_ref);
3986         if (keyp == NULL)
3987                 return -EINVAL;
3988         /*
3989          * If the key hasn't been initialized give it access so that
3990          * it may do so.
3991          */
3992         if (keyp->security == NULL)
3993                 return 0;
3994         /*
3995          * This should not occur
3996          */
3997         if (tkp == NULL)
3998                 return -EACCES;
3999 #ifdef CONFIG_AUDIT
4000         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4001         ad.a.u.key_struct.key = keyp->serial;
4002         ad.a.u.key_struct.key_desc = keyp->description;
4003 #endif
4004         rc = smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
4005         rc = smk_bu_note("key access", tkp, keyp->security, MAY_READWRITE, rc);
4006         return rc;
4007 }
4008 #endif /* CONFIG_KEYS */
4009
4010 /*
4011  * Smack Audit hooks
4012  *
4013  * Audit requires a unique representation of each Smack specific
4014  * rule. This unique representation is used to distinguish the
4015  * object to be audited from remaining kernel objects and also
4016  * works as a glue between the audit hooks.
4017  *
4018  * Since repository entries are added but never deleted, we'll use
4019  * the smack_known label address related to the given audit rule as
4020  * the needed unique representation. This also better fits the smack
4021  * model where nearly everything is a label.
4022  */
4023 #ifdef CONFIG_AUDIT
4024
4025 /**
4026  * smack_audit_rule_init - Initialize a smack audit rule
4027  * @field: audit rule fields given from user-space (audit.h)
4028  * @op: required testing operator (=, !=, >, <, ...)
4029  * @rulestr: smack label to be audited
4030  * @vrule: pointer to save our own audit rule representation
4031  *
4032  * Prepare to audit cases where (@field @op @rulestr) is true.
4033  * The label to be audited is created if necessay.
4034  */
4035 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4036 {
4037         char **rule = (char **)vrule;
4038         *rule = NULL;
4039
4040         if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4041                 return -EINVAL;
4042
4043         if (op != Audit_equal && op != Audit_not_equal)
4044                 return -EINVAL;
4045
4046         *rule = smk_import(rulestr, 0);
4047
4048         return 0;
4049 }
4050
4051 /**
4052  * smack_audit_rule_known - Distinguish Smack audit rules
4053  * @krule: rule of interest, in Audit kernel representation format
4054  *
4055  * This is used to filter Smack rules from remaining Audit ones.
4056  * If it's proved that this rule belongs to us, the
4057  * audit_rule_match hook will be called to do the final judgement.
4058  */
4059 static int smack_audit_rule_known(struct audit_krule *krule)
4060 {
4061         struct audit_field *f;
4062         int i;
4063
4064         for (i = 0; i < krule->field_count; i++) {
4065                 f = &krule->fields[i];
4066
4067                 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4068                         return 1;
4069         }
4070
4071         return 0;
4072 }
4073
4074 /**
4075  * smack_audit_rule_match - Audit given object ?
4076  * @secid: security id for identifying the object to test
4077  * @field: audit rule flags given from user-space
4078  * @op: required testing operator
4079  * @vrule: smack internal rule presentation
4080  * @actx: audit context associated with the check
4081  *
4082  * The core Audit hook. It's used to take the decision of
4083  * whether to audit or not to audit a given object.
4084  */
4085 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4086                                   struct audit_context *actx)
4087 {
4088         struct smack_known *skp;
4089         char *rule = vrule;
4090
4091         if (!rule) {
4092                 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
4093                           "Smack: missing rule\n");
4094                 return -ENOENT;
4095         }
4096
4097         if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4098                 return 0;
4099
4100         skp = smack_from_secid(secid);
4101
4102         /*
4103          * No need to do string comparisons. If a match occurs,
4104          * both pointers will point to the same smack_known
4105          * label.
4106          */
4107         if (op == Audit_equal)
4108                 return (rule == skp->smk_known);
4109         if (op == Audit_not_equal)
4110                 return (rule != skp->smk_known);
4111
4112         return 0;
4113 }
4114
4115 /**
4116  * smack_audit_rule_free - free smack rule representation
4117  * @vrule: rule to be freed.
4118  *
4119  * No memory was allocated.
4120  */
4121 static void smack_audit_rule_free(void *vrule)
4122 {
4123         /* No-op */
4124 }
4125
4126 #endif /* CONFIG_AUDIT */
4127
4128 /**
4129  * smack_secid_to_secctx - return the smack label for a secid
4130  * @secid: incoming integer
4131  * @secdata: destination
4132  * @seclen: how long it is
4133  *
4134  * Exists for networking code.
4135  */
4136 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4137 {
4138         struct smack_known *skp = smack_from_secid(secid);
4139
4140         if (secdata)
4141                 *secdata = skp->smk_known;
4142         *seclen = strlen(skp->smk_known);
4143         return 0;
4144 }
4145
4146 /**
4147  * smack_secctx_to_secid - return the secid for a smack label
4148  * @secdata: smack label
4149  * @seclen: how long result is
4150  * @secid: outgoing integer
4151  *
4152  * Exists for audit and networking code.
4153  */
4154 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4155 {
4156         *secid = smack_to_secid(secdata);
4157         return 0;
4158 }
4159
4160 /**
4161  * smack_release_secctx - don't do anything.
4162  * @secdata: unused
4163  * @seclen: unused
4164  *
4165  * Exists to make sure nothing gets done, and properly
4166  */
4167 static void smack_release_secctx(char *secdata, u32 seclen)
4168 {
4169 }
4170
4171 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4172 {
4173         return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4174 }
4175
4176 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4177 {
4178         return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4179 }
4180
4181 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4182 {
4183         int len = 0;
4184         len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4185
4186         if (len < 0)
4187                 return len;
4188         *ctxlen = len;
4189         return 0;
4190 }
4191
4192 struct security_operations smack_ops = {
4193         .name =                         "smack",
4194
4195         .ptrace_access_check =          smack_ptrace_access_check,
4196         .ptrace_traceme =               smack_ptrace_traceme,
4197         .syslog =                       smack_syslog,
4198
4199         .sb_alloc_security =            smack_sb_alloc_security,
4200         .sb_free_security =             smack_sb_free_security,
4201         .sb_copy_data =                 smack_sb_copy_data,
4202         .sb_kern_mount =                smack_sb_kern_mount,
4203         .sb_statfs =                    smack_sb_statfs,
4204
4205         .bprm_set_creds =               smack_bprm_set_creds,
4206         .bprm_committing_creds =        smack_bprm_committing_creds,
4207         .bprm_secureexec =              smack_bprm_secureexec,
4208
4209         .inode_alloc_security =         smack_inode_alloc_security,
4210         .inode_free_security =          smack_inode_free_security,
4211         .inode_init_security =          smack_inode_init_security,
4212         .inode_link =                   smack_inode_link,
4213         .inode_unlink =                 smack_inode_unlink,
4214         .inode_rmdir =                  smack_inode_rmdir,
4215         .inode_rename =                 smack_inode_rename,
4216         .inode_permission =             smack_inode_permission,
4217         .inode_setattr =                smack_inode_setattr,
4218         .inode_getattr =                smack_inode_getattr,
4219         .inode_setxattr =               smack_inode_setxattr,
4220         .inode_post_setxattr =          smack_inode_post_setxattr,
4221         .inode_getxattr =               smack_inode_getxattr,
4222         .inode_removexattr =            smack_inode_removexattr,
4223         .inode_getsecurity =            smack_inode_getsecurity,
4224         .inode_setsecurity =            smack_inode_setsecurity,
4225         .inode_listsecurity =           smack_inode_listsecurity,
4226         .inode_getsecid =               smack_inode_getsecid,
4227
4228         .file_permission =              smack_file_permission,
4229         .file_alloc_security =          smack_file_alloc_security,
4230         .file_free_security =           smack_file_free_security,
4231         .file_ioctl =                   smack_file_ioctl,
4232         .file_lock =                    smack_file_lock,
4233         .file_fcntl =                   smack_file_fcntl,
4234         .mmap_file =                    smack_mmap_file,
4235         .mmap_addr =                    cap_mmap_addr,
4236         .file_set_fowner =              smack_file_set_fowner,
4237         .file_send_sigiotask =          smack_file_send_sigiotask,
4238         .file_receive =                 smack_file_receive,
4239
4240         .file_open =                    smack_file_open,
4241
4242         .cred_alloc_blank =             smack_cred_alloc_blank,
4243         .cred_free =                    smack_cred_free,
4244         .cred_prepare =                 smack_cred_prepare,
4245         .cred_transfer =                smack_cred_transfer,
4246         .kernel_act_as =                smack_kernel_act_as,
4247         .kernel_create_files_as =       smack_kernel_create_files_as,
4248         .task_setpgid =                 smack_task_setpgid,
4249         .task_getpgid =                 smack_task_getpgid,
4250         .task_getsid =                  smack_task_getsid,
4251         .task_getsecid =                smack_task_getsecid,
4252         .task_setnice =                 smack_task_setnice,
4253         .task_setioprio =               smack_task_setioprio,
4254         .task_getioprio =               smack_task_getioprio,
4255         .task_setscheduler =            smack_task_setscheduler,
4256         .task_getscheduler =            smack_task_getscheduler,
4257         .task_movememory =              smack_task_movememory,
4258         .task_kill =                    smack_task_kill,
4259         .task_wait =                    smack_task_wait,
4260         .task_to_inode =                smack_task_to_inode,
4261
4262         .ipc_permission =               smack_ipc_permission,
4263         .ipc_getsecid =                 smack_ipc_getsecid,
4264
4265         .msg_msg_alloc_security =       smack_msg_msg_alloc_security,
4266         .msg_msg_free_security =        smack_msg_msg_free_security,
4267
4268         .msg_queue_alloc_security =     smack_msg_queue_alloc_security,
4269         .msg_queue_free_security =      smack_msg_queue_free_security,
4270         .msg_queue_associate =          smack_msg_queue_associate,
4271         .msg_queue_msgctl =             smack_msg_queue_msgctl,
4272         .msg_queue_msgsnd =             smack_msg_queue_msgsnd,
4273         .msg_queue_msgrcv =             smack_msg_queue_msgrcv,
4274
4275         .shm_alloc_security =           smack_shm_alloc_security,
4276         .shm_free_security =            smack_shm_free_security,
4277         .shm_associate =                smack_shm_associate,
4278         .shm_shmctl =                   smack_shm_shmctl,
4279         .shm_shmat =                    smack_shm_shmat,
4280
4281         .sem_alloc_security =           smack_sem_alloc_security,
4282         .sem_free_security =            smack_sem_free_security,
4283         .sem_associate =                smack_sem_associate,
4284         .sem_semctl =                   smack_sem_semctl,
4285         .sem_semop =                    smack_sem_semop,
4286
4287         .d_instantiate =                smack_d_instantiate,
4288
4289         .getprocattr =                  smack_getprocattr,
4290         .setprocattr =                  smack_setprocattr,
4291
4292         .unix_stream_connect =          smack_unix_stream_connect,
4293         .unix_may_send =                smack_unix_may_send,
4294
4295         .socket_post_create =           smack_socket_post_create,
4296         .socket_bind =                  smack_socket_bind,
4297         .socket_connect =               smack_socket_connect,
4298         .socket_sendmsg =               smack_socket_sendmsg,
4299         .socket_sock_rcv_skb =          smack_socket_sock_rcv_skb,
4300         .socket_getpeersec_stream =     smack_socket_getpeersec_stream,
4301         .socket_getpeersec_dgram =      smack_socket_getpeersec_dgram,
4302         .sk_alloc_security =            smack_sk_alloc_security,
4303         .sk_free_security =             smack_sk_free_security,
4304         .sock_graft =                   smack_sock_graft,
4305         .inet_conn_request =            smack_inet_conn_request,
4306         .inet_csk_clone =               smack_inet_csk_clone,
4307
4308  /* key management security hooks */
4309 #ifdef CONFIG_KEYS
4310         .key_alloc =                    smack_key_alloc,
4311         .key_free =                     smack_key_free,
4312         .key_permission =               smack_key_permission,
4313 #endif /* CONFIG_KEYS */
4314
4315  /* Audit hooks */
4316 #ifdef CONFIG_AUDIT
4317         .audit_rule_init =              smack_audit_rule_init,
4318         .audit_rule_known =             smack_audit_rule_known,
4319         .audit_rule_match =             smack_audit_rule_match,
4320         .audit_rule_free =              smack_audit_rule_free,
4321 #endif /* CONFIG_AUDIT */
4322
4323         .secid_to_secctx =              smack_secid_to_secctx,
4324         .secctx_to_secid =              smack_secctx_to_secid,
4325         .release_secctx =               smack_release_secctx,
4326         .inode_notifysecctx =           smack_inode_notifysecctx,
4327         .inode_setsecctx =              smack_inode_setsecctx,
4328         .inode_getsecctx =              smack_inode_getsecctx,
4329 };
4330
4331
4332 static __init void init_smack_known_list(void)
4333 {
4334         /*
4335          * Initialize rule list locks
4336          */
4337         mutex_init(&smack_known_huh.smk_rules_lock);
4338         mutex_init(&smack_known_hat.smk_rules_lock);
4339         mutex_init(&smack_known_floor.smk_rules_lock);
4340         mutex_init(&smack_known_star.smk_rules_lock);
4341         mutex_init(&smack_known_invalid.smk_rules_lock);
4342         mutex_init(&smack_known_web.smk_rules_lock);
4343         /*
4344          * Initialize rule lists
4345          */
4346         INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4347         INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4348         INIT_LIST_HEAD(&smack_known_star.smk_rules);
4349         INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4350         INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4351         INIT_LIST_HEAD(&smack_known_web.smk_rules);
4352         /*
4353          * Create the known labels list
4354          */
4355         smk_insert_entry(&smack_known_huh);
4356         smk_insert_entry(&smack_known_hat);
4357         smk_insert_entry(&smack_known_star);
4358         smk_insert_entry(&smack_known_floor);
4359         smk_insert_entry(&smack_known_invalid);
4360         smk_insert_entry(&smack_known_web);
4361 }
4362
4363 /* KMEM caches for fast and thrifty allocations */
4364 struct kmem_cache *smack_rule_cache;
4365 struct kmem_cache *smack_master_list_cache;
4366
4367 /**
4368  * smack_init - initialize the smack system
4369  *
4370  * Returns 0
4371  */
4372 static __init int smack_init(void)
4373 {
4374         struct cred *cred;
4375         struct task_smack *tsp;
4376
4377         if (!security_module_enable(&smack_ops))
4378                 return 0;
4379
4380         smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4381         if (!smack_rule_cache)
4382                 return -ENOMEM;
4383
4384         smack_master_list_cache = KMEM_CACHE(smack_master_list, 0);
4385         if (!smack_master_list_cache) {
4386                 kmem_cache_destroy(smack_rule_cache);
4387                 return -ENOMEM;
4388         }
4389
4390         smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4391         if (!smack_inode_cache) {
4392                 kmem_cache_destroy(smack_master_list_cache);
4393                 kmem_cache_destroy(smack_rule_cache);
4394                 return -ENOMEM;
4395         }
4396
4397         tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4398                                 GFP_KERNEL);
4399         if (tsp == NULL) {
4400                 kmem_cache_destroy(smack_master_list_cache);
4401                 kmem_cache_destroy(smack_rule_cache);
4402                 kmem_cache_destroy(smack_inode_cache);
4403                 return -ENOMEM;
4404         }
4405
4406         printk(KERN_INFO "Smack:  Initializing.\n");
4407
4408         /*
4409          * Set the security state for the initial task.
4410          */
4411         cred = (struct cred *) current->cred;
4412         cred->security = tsp;
4413
4414         /* initialize the smack_known_list */
4415         init_smack_known_list();
4416
4417         /*
4418          * Register with LSM
4419          */
4420         if (register_security(&smack_ops))
4421                 panic("smack: Unable to register with kernel.\n");
4422
4423         return 0;
4424 }
4425
4426 /*
4427  * Smack requires early initialization in order to label
4428  * all processes and objects when they are created.
4429  */
4430 security_initcall(smack_init);