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