Smack: Abstract use of file security blob
[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_free_security - clear a file security blob
1500  * @file: the object
1501  *
1502  * The security blob for a file is a pointer to the master
1503  * label list, so no memory is freed.
1504  */
1505 static void smack_file_free_security(struct file *file)
1506 {
1507         file->f_security = NULL;
1508 }
1509
1510 /**
1511  * smack_file_ioctl - Smack check on ioctls
1512  * @file: the object
1513  * @cmd: what to do
1514  * @arg: unused
1515  *
1516  * Relies heavily on the correct use of the ioctl command conventions.
1517  *
1518  * Returns 0 if allowed, error code otherwise
1519  */
1520 static int smack_file_ioctl(struct file *file, unsigned int cmd,
1521                             unsigned long arg)
1522 {
1523         int rc = 0;
1524         struct smk_audit_info ad;
1525         struct inode *inode = file_inode(file);
1526
1527         if (unlikely(IS_PRIVATE(inode)))
1528                 return 0;
1529
1530         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1531         smk_ad_setfield_u_fs_path(&ad, file->f_path);
1532
1533         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1534                 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1535                 rc = smk_bu_file(file, MAY_WRITE, rc);
1536         }
1537
1538         if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
1539                 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1540                 rc = smk_bu_file(file, MAY_READ, rc);
1541         }
1542
1543         return rc;
1544 }
1545
1546 /**
1547  * smack_file_lock - Smack check on file locking
1548  * @file: the object
1549  * @cmd: unused
1550  *
1551  * Returns 0 if current has lock access, error code otherwise
1552  */
1553 static int smack_file_lock(struct file *file, unsigned int cmd)
1554 {
1555         struct smk_audit_info ad;
1556         int rc;
1557         struct inode *inode = file_inode(file);
1558
1559         if (unlikely(IS_PRIVATE(inode)))
1560                 return 0;
1561
1562         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1563         smk_ad_setfield_u_fs_path(&ad, file->f_path);
1564         rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1565         rc = smk_bu_file(file, MAY_LOCK, rc);
1566         return rc;
1567 }
1568
1569 /**
1570  * smack_file_fcntl - Smack check on fcntl
1571  * @file: the object
1572  * @cmd: what action to check
1573  * @arg: unused
1574  *
1575  * Generally these operations are harmless.
1576  * File locking operations present an obvious mechanism
1577  * for passing information, so they require write access.
1578  *
1579  * Returns 0 if current has access, error code otherwise
1580  */
1581 static int smack_file_fcntl(struct file *file, unsigned int cmd,
1582                             unsigned long arg)
1583 {
1584         struct smk_audit_info ad;
1585         int rc = 0;
1586         struct inode *inode = file_inode(file);
1587
1588         if (unlikely(IS_PRIVATE(inode)))
1589                 return 0;
1590
1591         switch (cmd) {
1592         case F_GETLK:
1593                 break;
1594         case F_SETLK:
1595         case F_SETLKW:
1596                 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1597                 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1598                 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
1599                 rc = smk_bu_file(file, MAY_LOCK, rc);
1600                 break;
1601         case F_SETOWN:
1602         case F_SETSIG:
1603                 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1604                 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1605                 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
1606                 rc = smk_bu_file(file, MAY_WRITE, rc);
1607                 break;
1608         default:
1609                 break;
1610         }
1611
1612         return rc;
1613 }
1614
1615 /**
1616  * smack_mmap_file :
1617  * Check permissions for a mmap operation.  The @file may be NULL, e.g.
1618  * if mapping anonymous memory.
1619  * @file contains the file structure for file to map (may be NULL).
1620  * @reqprot contains the protection requested by the application.
1621  * @prot contains the protection that will be applied by the kernel.
1622  * @flags contains the operational flags.
1623  * Return 0 if permission is granted.
1624  */
1625 static int smack_mmap_file(struct file *file,
1626                            unsigned long reqprot, unsigned long prot,
1627                            unsigned long flags)
1628 {
1629         struct smack_known *skp;
1630         struct smack_known *mkp;
1631         struct smack_rule *srp;
1632         struct task_smack *tsp;
1633         struct smack_known *okp;
1634         struct inode_smack *isp;
1635         struct superblock_smack *sbsp;
1636         int may;
1637         int mmay;
1638         int tmay;
1639         int rc;
1640
1641         if (file == NULL)
1642                 return 0;
1643
1644         if (unlikely(IS_PRIVATE(file_inode(file))))
1645                 return 0;
1646
1647         isp = file_inode(file)->i_security;
1648         if (isp->smk_mmap == NULL)
1649                 return 0;
1650         sbsp = file_inode(file)->i_sb->s_security;
1651         if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1652             isp->smk_mmap != sbsp->smk_root)
1653                 return -EACCES;
1654         mkp = isp->smk_mmap;
1655
1656         tsp = smack_cred(current_cred());
1657         skp = smk_of_current();
1658         rc = 0;
1659
1660         rcu_read_lock();
1661         /*
1662          * For each Smack rule associated with the subject
1663          * label verify that the SMACK64MMAP also has access
1664          * to that rule's object label.
1665          */
1666         list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1667                 okp = srp->smk_object;
1668                 /*
1669                  * Matching labels always allows access.
1670                  */
1671                 if (mkp->smk_known == okp->smk_known)
1672                         continue;
1673                 /*
1674                  * If there is a matching local rule take
1675                  * that into account as well.
1676                  */
1677                 may = smk_access_entry(srp->smk_subject->smk_known,
1678                                        okp->smk_known,
1679                                        &tsp->smk_rules);
1680                 if (may == -ENOENT)
1681                         may = srp->smk_access;
1682                 else
1683                         may &= srp->smk_access;
1684                 /*
1685                  * If may is zero the SMACK64MMAP subject can't
1686                  * possibly have less access.
1687                  */
1688                 if (may == 0)
1689                         continue;
1690
1691                 /*
1692                  * Fetch the global list entry.
1693                  * If there isn't one a SMACK64MMAP subject
1694                  * can't have as much access as current.
1695                  */
1696                 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1697                                         &mkp->smk_rules);
1698                 if (mmay == -ENOENT) {
1699                         rc = -EACCES;
1700                         break;
1701                 }
1702                 /*
1703                  * If there is a local entry it modifies the
1704                  * potential access, too.
1705                  */
1706                 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1707                                         &tsp->smk_rules);
1708                 if (tmay != -ENOENT)
1709                         mmay &= tmay;
1710
1711                 /*
1712                  * If there is any access available to current that is
1713                  * not available to a SMACK64MMAP subject
1714                  * deny access.
1715                  */
1716                 if ((may | mmay) != mmay) {
1717                         rc = -EACCES;
1718                         break;
1719                 }
1720         }
1721
1722         rcu_read_unlock();
1723
1724         return rc;
1725 }
1726
1727 /**
1728  * smack_file_set_fowner - set the file security blob value
1729  * @file: object in question
1730  *
1731  */
1732 static void smack_file_set_fowner(struct file *file)
1733 {
1734         struct smack_known **blob = smack_file(file);
1735
1736         *blob = smk_of_current();
1737 }
1738
1739 /**
1740  * smack_file_send_sigiotask - Smack on sigio
1741  * @tsk: The target task
1742  * @fown: the object the signal come from
1743  * @signum: unused
1744  *
1745  * Allow a privileged task to get signals even if it shouldn't
1746  *
1747  * Returns 0 if a subject with the object's smack could
1748  * write to the task, an error code otherwise.
1749  */
1750 static int smack_file_send_sigiotask(struct task_struct *tsk,
1751                                      struct fown_struct *fown, int signum)
1752 {
1753         struct smack_known **blob;
1754         struct smack_known *skp;
1755         struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
1756         const struct cred *tcred;
1757         struct file *file;
1758         int rc;
1759         struct smk_audit_info ad;
1760
1761         /*
1762          * struct fown_struct is never outside the context of a struct file
1763          */
1764         file = container_of(fown, struct file, f_owner);
1765
1766         /* we don't log here as rc can be overriden */
1767         blob = smack_file(file);
1768         skp = *blob;
1769         rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1770         rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1771
1772         rcu_read_lock();
1773         tcred = __task_cred(tsk);
1774         if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
1775                 rc = 0;
1776         rcu_read_unlock();
1777
1778         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1779         smk_ad_setfield_u_tsk(&ad, tsk);
1780         smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1781         return rc;
1782 }
1783
1784 /**
1785  * smack_file_receive - Smack file receive check
1786  * @file: the object
1787  *
1788  * Returns 0 if current has access, error code otherwise
1789  */
1790 static int smack_file_receive(struct file *file)
1791 {
1792         int rc;
1793         int may = 0;
1794         struct smk_audit_info ad;
1795         struct inode *inode = file_inode(file);
1796         struct socket *sock;
1797         struct task_smack *tsp;
1798         struct socket_smack *ssp;
1799
1800         if (unlikely(IS_PRIVATE(inode)))
1801                 return 0;
1802
1803         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1804         smk_ad_setfield_u_fs_path(&ad, file->f_path);
1805
1806         if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
1807                 sock = SOCKET_I(inode);
1808                 ssp = sock->sk->sk_security;
1809                 tsp = smack_cred(current_cred());
1810                 /*
1811                  * If the receiving process can't write to the
1812                  * passed socket or if the passed socket can't
1813                  * write to the receiving process don't accept
1814                  * the passed socket.
1815                  */
1816                 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1817                 rc = smk_bu_file(file, may, rc);
1818                 if (rc < 0)
1819                         return rc;
1820                 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1821                 rc = smk_bu_file(file, may, rc);
1822                 return rc;
1823         }
1824         /*
1825          * This code relies on bitmasks.
1826          */
1827         if (file->f_mode & FMODE_READ)
1828                 may = MAY_READ;
1829         if (file->f_mode & FMODE_WRITE)
1830                 may |= MAY_WRITE;
1831
1832         rc = smk_curacc(smk_of_inode(inode), may, &ad);
1833         rc = smk_bu_file(file, may, rc);
1834         return rc;
1835 }
1836
1837 /**
1838  * smack_file_open - Smack dentry open processing
1839  * @file: the object
1840  * @cred: task credential
1841  *
1842  * Set the security blob in the file structure.
1843  * Allow the open only if the task has read access. There are
1844  * many read operations (e.g. fstat) that you can do with an
1845  * fd even if you have the file open write-only.
1846  *
1847  * Returns 0
1848  */
1849 static int smack_file_open(struct file *file)
1850 {
1851         struct task_smack *tsp = smack_cred(file->f_cred);
1852         struct inode *inode = file_inode(file);
1853         struct smk_audit_info ad;
1854         int rc;
1855
1856         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1857         smk_ad_setfield_u_fs_path(&ad, file->f_path);
1858         rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
1859         rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
1860
1861         return rc;
1862 }
1863
1864 /*
1865  * Task hooks
1866  */
1867
1868 /**
1869  * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1870  * @new: the new credentials
1871  * @gfp: the atomicity of any memory allocations
1872  *
1873  * Prepare a blank set of credentials for modification.  This must allocate all
1874  * the memory the LSM module might require such that cred_transfer() can
1875  * complete without error.
1876  */
1877 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1878 {
1879         init_task_smack(smack_cred(cred), NULL, NULL);
1880         return 0;
1881 }
1882
1883
1884 /**
1885  * smack_cred_free - "free" task-level security credentials
1886  * @cred: the credentials in question
1887  *
1888  */
1889 static void smack_cred_free(struct cred *cred)
1890 {
1891         struct task_smack *tsp = smack_cred(cred);
1892         struct smack_rule *rp;
1893         struct list_head *l;
1894         struct list_head *n;
1895
1896         smk_destroy_label_list(&tsp->smk_relabel);
1897
1898         list_for_each_safe(l, n, &tsp->smk_rules) {
1899                 rp = list_entry(l, struct smack_rule, list);
1900                 list_del(&rp->list);
1901                 kfree(rp);
1902         }
1903 }
1904
1905 /**
1906  * smack_cred_prepare - prepare new set of credentials for modification
1907  * @new: the new credentials
1908  * @old: the original credentials
1909  * @gfp: the atomicity of any memory allocations
1910  *
1911  * Prepare a new set of credentials for modification.
1912  */
1913 static int smack_cred_prepare(struct cred *new, const struct cred *old,
1914                               gfp_t gfp)
1915 {
1916         struct task_smack *old_tsp = smack_cred(old);
1917         struct task_smack *new_tsp = smack_cred(new);
1918         int rc;
1919
1920         init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
1921
1922         rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1923         if (rc != 0)
1924                 return rc;
1925
1926         rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1927                                 gfp);
1928         return rc;
1929 }
1930
1931 /**
1932  * smack_cred_transfer - Transfer the old credentials to the new credentials
1933  * @new: the new credentials
1934  * @old: the original credentials
1935  *
1936  * Fill in a set of blank credentials from another set of credentials.
1937  */
1938 static void smack_cred_transfer(struct cred *new, const struct cred *old)
1939 {
1940         struct task_smack *old_tsp = smack_cred(old);
1941         struct task_smack *new_tsp = smack_cred(new);
1942
1943         new_tsp->smk_task = old_tsp->smk_task;
1944         new_tsp->smk_forked = old_tsp->smk_task;
1945         mutex_init(&new_tsp->smk_rules_lock);
1946         INIT_LIST_HEAD(&new_tsp->smk_rules);
1947
1948         /* cbs copy rule list */
1949 }
1950
1951 /**
1952  * smack_cred_getsecid - get the secid corresponding to a creds structure
1953  * @c: the object creds
1954  * @secid: where to put the result
1955  *
1956  * Sets the secid to contain a u32 version of the smack label.
1957  */
1958 static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
1959 {
1960         struct smack_known *skp;
1961
1962         rcu_read_lock();
1963         skp = smk_of_task(smack_cred(cred));
1964         *secid = skp->smk_secid;
1965         rcu_read_unlock();
1966 }
1967
1968 /**
1969  * smack_kernel_act_as - Set the subjective context in a set of credentials
1970  * @new: points to the set of credentials to be modified.
1971  * @secid: specifies the security ID to be set
1972  *
1973  * Set the security data for a kernel service.
1974  */
1975 static int smack_kernel_act_as(struct cred *new, u32 secid)
1976 {
1977         struct task_smack *new_tsp = smack_cred(new);
1978
1979         new_tsp->smk_task = smack_from_secid(secid);
1980         return 0;
1981 }
1982
1983 /**
1984  * smack_kernel_create_files_as - Set the file creation label in a set of creds
1985  * @new: points to the set of credentials to be modified
1986  * @inode: points to the inode to use as a reference
1987  *
1988  * Set the file creation context in a set of credentials to the same
1989  * as the objective context of the specified inode
1990  */
1991 static int smack_kernel_create_files_as(struct cred *new,
1992                                         struct inode *inode)
1993 {
1994         struct inode_smack *isp = inode->i_security;
1995         struct task_smack *tsp = smack_cred(new);
1996
1997         tsp->smk_forked = isp->smk_inode;
1998         tsp->smk_task = tsp->smk_forked;
1999         return 0;
2000 }
2001
2002 /**
2003  * smk_curacc_on_task - helper to log task related access
2004  * @p: the task object
2005  * @access: the access requested
2006  * @caller: name of the calling function for audit
2007  *
2008  * Return 0 if access is permitted
2009  */
2010 static int smk_curacc_on_task(struct task_struct *p, int access,
2011                                 const char *caller)
2012 {
2013         struct smk_audit_info ad;
2014         struct smack_known *skp = smk_of_task_struct(p);
2015         int rc;
2016
2017         smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
2018         smk_ad_setfield_u_tsk(&ad, p);
2019         rc = smk_curacc(skp, access, &ad);
2020         rc = smk_bu_task(p, access, rc);
2021         return rc;
2022 }
2023
2024 /**
2025  * smack_task_setpgid - Smack check on setting pgid
2026  * @p: the task object
2027  * @pgid: unused
2028  *
2029  * Return 0 if write access is permitted
2030  */
2031 static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2032 {
2033         return smk_curacc_on_task(p, MAY_WRITE, __func__);
2034 }
2035
2036 /**
2037  * smack_task_getpgid - Smack access check for getpgid
2038  * @p: the object task
2039  *
2040  * Returns 0 if current can read the object task, error code otherwise
2041  */
2042 static int smack_task_getpgid(struct task_struct *p)
2043 {
2044         return smk_curacc_on_task(p, MAY_READ, __func__);
2045 }
2046
2047 /**
2048  * smack_task_getsid - Smack access check for getsid
2049  * @p: the object task
2050  *
2051  * Returns 0 if current can read the object task, error code otherwise
2052  */
2053 static int smack_task_getsid(struct task_struct *p)
2054 {
2055         return smk_curacc_on_task(p, MAY_READ, __func__);
2056 }
2057
2058 /**
2059  * smack_task_getsecid - get the secid of the task
2060  * @p: the object task
2061  * @secid: where to put the result
2062  *
2063  * Sets the secid to contain a u32 version of the smack label.
2064  */
2065 static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2066 {
2067         struct smack_known *skp = smk_of_task_struct(p);
2068
2069         *secid = skp->smk_secid;
2070 }
2071
2072 /**
2073  * smack_task_setnice - Smack check on setting nice
2074  * @p: the task object
2075  * @nice: unused
2076  *
2077  * Return 0 if write access is permitted
2078  */
2079 static int smack_task_setnice(struct task_struct *p, int nice)
2080 {
2081         return smk_curacc_on_task(p, MAY_WRITE, __func__);
2082 }
2083
2084 /**
2085  * smack_task_setioprio - Smack check on setting ioprio
2086  * @p: the task object
2087  * @ioprio: unused
2088  *
2089  * Return 0 if write access is permitted
2090  */
2091 static int smack_task_setioprio(struct task_struct *p, int ioprio)
2092 {
2093         return smk_curacc_on_task(p, MAY_WRITE, __func__);
2094 }
2095
2096 /**
2097  * smack_task_getioprio - Smack check on reading ioprio
2098  * @p: the task object
2099  *
2100  * Return 0 if read access is permitted
2101  */
2102 static int smack_task_getioprio(struct task_struct *p)
2103 {
2104         return smk_curacc_on_task(p, MAY_READ, __func__);
2105 }
2106
2107 /**
2108  * smack_task_setscheduler - Smack check on setting scheduler
2109  * @p: the task object
2110  * @policy: unused
2111  * @lp: unused
2112  *
2113  * Return 0 if read access is permitted
2114  */
2115 static int smack_task_setscheduler(struct task_struct *p)
2116 {
2117         return smk_curacc_on_task(p, MAY_WRITE, __func__);
2118 }
2119
2120 /**
2121  * smack_task_getscheduler - Smack check on reading scheduler
2122  * @p: the task object
2123  *
2124  * Return 0 if read access is permitted
2125  */
2126 static int smack_task_getscheduler(struct task_struct *p)
2127 {
2128         return smk_curacc_on_task(p, MAY_READ, __func__);
2129 }
2130
2131 /**
2132  * smack_task_movememory - Smack check on moving memory
2133  * @p: the task object
2134  *
2135  * Return 0 if write access is permitted
2136  */
2137 static int smack_task_movememory(struct task_struct *p)
2138 {
2139         return smk_curacc_on_task(p, MAY_WRITE, __func__);
2140 }
2141
2142 /**
2143  * smack_task_kill - Smack check on signal delivery
2144  * @p: the task object
2145  * @info: unused
2146  * @sig: unused
2147  * @cred: identifies the cred to use in lieu of current's
2148  *
2149  * Return 0 if write access is permitted
2150  *
2151  */
2152 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
2153                            int sig, const struct cred *cred)
2154 {
2155         struct smk_audit_info ad;
2156         struct smack_known *skp;
2157         struct smack_known *tkp = smk_of_task_struct(p);
2158         int rc;
2159
2160         if (!sig)
2161                 return 0; /* null signal; existence test */
2162
2163         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2164         smk_ad_setfield_u_tsk(&ad, p);
2165         /*
2166          * Sending a signal requires that the sender
2167          * can write the receiver.
2168          */
2169         if (cred == NULL) {
2170                 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2171                 rc = smk_bu_task(p, MAY_DELIVER, rc);
2172                 return rc;
2173         }
2174         /*
2175          * If the cred isn't NULL we're dealing with some USB IO
2176          * specific behavior. This is not clean. For one thing
2177          * we can't take privilege into account.
2178          */
2179         skp = smk_of_task(smack_cred(cred));
2180         rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2181         rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
2182         return rc;
2183 }
2184
2185 /**
2186  * smack_task_to_inode - copy task smack into the inode blob
2187  * @p: task to copy from
2188  * @inode: inode to copy to
2189  *
2190  * Sets the smack pointer in the inode security blob
2191  */
2192 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2193 {
2194         struct inode_smack *isp = inode->i_security;
2195         struct smack_known *skp = smk_of_task_struct(p);
2196
2197         isp->smk_inode = skp;
2198         isp->smk_flags |= SMK_INODE_INSTANT;
2199 }
2200
2201 /*
2202  * Socket hooks.
2203  */
2204
2205 /**
2206  * smack_sk_alloc_security - Allocate a socket blob
2207  * @sk: the socket
2208  * @family: unused
2209  * @gfp_flags: memory allocation flags
2210  *
2211  * Assign Smack pointers to current
2212  *
2213  * Returns 0 on success, -ENOMEM is there's no memory
2214  */
2215 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2216 {
2217         struct smack_known *skp = smk_of_current();
2218         struct socket_smack *ssp;
2219
2220         ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2221         if (ssp == NULL)
2222                 return -ENOMEM;
2223
2224         /*
2225          * Sockets created by kernel threads receive web label.
2226          */
2227         if (unlikely(current->flags & PF_KTHREAD)) {
2228                 ssp->smk_in = &smack_known_web;
2229                 ssp->smk_out = &smack_known_web;
2230         } else {
2231                 ssp->smk_in = skp;
2232                 ssp->smk_out = skp;
2233         }
2234         ssp->smk_packet = NULL;
2235
2236         sk->sk_security = ssp;
2237
2238         return 0;
2239 }
2240
2241 /**
2242  * smack_sk_free_security - Free a socket blob
2243  * @sk: the socket
2244  *
2245  * Clears the blob pointer
2246  */
2247 static void smack_sk_free_security(struct sock *sk)
2248 {
2249 #ifdef SMACK_IPV6_PORT_LABELING
2250         struct smk_port_label *spp;
2251
2252         if (sk->sk_family == PF_INET6) {
2253                 rcu_read_lock();
2254                 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2255                         if (spp->smk_sock != sk)
2256                                 continue;
2257                         spp->smk_can_reuse = 1;
2258                         break;
2259                 }
2260                 rcu_read_unlock();
2261         }
2262 #endif
2263         kfree(sk->sk_security);
2264 }
2265
2266 /**
2267 * smack_ipv4host_label - check host based restrictions
2268 * @sip: the object end
2269 *
2270 * looks for host based access restrictions
2271 *
2272 * This version will only be appropriate for really small sets of single label
2273 * hosts.  The caller is responsible for ensuring that the RCU read lock is
2274 * taken before calling this function.
2275 *
2276 * Returns the label of the far end or NULL if it's not special.
2277 */
2278 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2279 {
2280         struct smk_net4addr *snp;
2281         struct in_addr *siap = &sip->sin_addr;
2282
2283         if (siap->s_addr == 0)
2284                 return NULL;
2285
2286         list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2287                 /*
2288                  * we break after finding the first match because
2289                  * the list is sorted from longest to shortest mask
2290                  * so we have found the most specific match
2291                  */
2292                 if (snp->smk_host.s_addr ==
2293                     (siap->s_addr & snp->smk_mask.s_addr))
2294                         return snp->smk_label;
2295
2296         return NULL;
2297 }
2298
2299 #if IS_ENABLED(CONFIG_IPV6)
2300 /*
2301  * smk_ipv6_localhost - Check for local ipv6 host address
2302  * @sip: the address
2303  *
2304  * Returns boolean true if this is the localhost address
2305  */
2306 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2307 {
2308         __be16 *be16p = (__be16 *)&sip->sin6_addr;
2309         __be32 *be32p = (__be32 *)&sip->sin6_addr;
2310
2311         if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2312             ntohs(be16p[7]) == 1)
2313                 return true;
2314         return false;
2315 }
2316
2317 /**
2318 * smack_ipv6host_label - check host based restrictions
2319 * @sip: the object end
2320 *
2321 * looks for host based access restrictions
2322 *
2323 * This version will only be appropriate for really small sets of single label
2324 * hosts.  The caller is responsible for ensuring that the RCU read lock is
2325 * taken before calling this function.
2326 *
2327 * Returns the label of the far end or NULL if it's not special.
2328 */
2329 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2330 {
2331         struct smk_net6addr *snp;
2332         struct in6_addr *sap = &sip->sin6_addr;
2333         int i;
2334         int found = 0;
2335
2336         /*
2337          * It's local. Don't look for a host label.
2338          */
2339         if (smk_ipv6_localhost(sip))
2340                 return NULL;
2341
2342         list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
2343                 /*
2344                  * If the label is NULL the entry has
2345                  * been renounced. Ignore it.
2346                  */
2347                 if (snp->smk_label == NULL)
2348                         continue;
2349                 /*
2350                 * we break after finding the first match because
2351                 * the list is sorted from longest to shortest mask
2352                 * so we have found the most specific match
2353                 */
2354                 for (found = 1, i = 0; i < 8; i++) {
2355                         if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2356                             snp->smk_host.s6_addr16[i]) {
2357                                 found = 0;
2358                                 break;
2359                         }
2360                 }
2361                 if (found)
2362                         return snp->smk_label;
2363         }
2364
2365         return NULL;
2366 }
2367 #endif /* CONFIG_IPV6 */
2368
2369 /**
2370  * smack_netlabel - Set the secattr on a socket
2371  * @sk: the socket
2372  * @labeled: socket label scheme
2373  *
2374  * Convert the outbound smack value (smk_out) to a
2375  * secattr and attach it to the socket.
2376  *
2377  * Returns 0 on success or an error code
2378  */
2379 static int smack_netlabel(struct sock *sk, int labeled)
2380 {
2381         struct smack_known *skp;
2382         struct socket_smack *ssp = sk->sk_security;
2383         int rc = 0;
2384
2385         /*
2386          * Usually the netlabel code will handle changing the
2387          * packet labeling based on the label.
2388          * The case of a single label host is different, because
2389          * a single label host should never get a labeled packet
2390          * even though the label is usually associated with a packet
2391          * label.
2392          */
2393         local_bh_disable();
2394         bh_lock_sock_nested(sk);
2395
2396         if (ssp->smk_out == smack_net_ambient ||
2397             labeled == SMACK_UNLABELED_SOCKET)
2398                 netlbl_sock_delattr(sk);
2399         else {
2400                 skp = ssp->smk_out;
2401                 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2402         }
2403
2404         bh_unlock_sock(sk);
2405         local_bh_enable();
2406
2407         return rc;
2408 }
2409
2410 /**
2411  * smack_netlbel_send - Set the secattr on a socket and perform access checks
2412  * @sk: the socket
2413  * @sap: the destination address
2414  *
2415  * Set the correct secattr for the given socket based on the destination
2416  * address and perform any outbound access checks needed.
2417  *
2418  * Returns 0 on success or an error code.
2419  *
2420  */
2421 static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2422 {
2423         struct smack_known *skp;
2424         int rc;
2425         int sk_lbl;
2426         struct smack_known *hkp;
2427         struct socket_smack *ssp = sk->sk_security;
2428         struct smk_audit_info ad;
2429
2430         rcu_read_lock();
2431         hkp = smack_ipv4host_label(sap);
2432         if (hkp != NULL) {
2433 #ifdef CONFIG_AUDIT
2434                 struct lsm_network_audit net;
2435
2436                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2437                 ad.a.u.net->family = sap->sin_family;
2438                 ad.a.u.net->dport = sap->sin_port;
2439                 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
2440 #endif
2441                 sk_lbl = SMACK_UNLABELED_SOCKET;
2442                 skp = ssp->smk_out;
2443                 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2444                 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2445         } else {
2446                 sk_lbl = SMACK_CIPSO_SOCKET;
2447                 rc = 0;
2448         }
2449         rcu_read_unlock();
2450         if (rc != 0)
2451                 return rc;
2452
2453         return smack_netlabel(sk, sk_lbl);
2454 }
2455
2456 #if IS_ENABLED(CONFIG_IPV6)
2457 /**
2458  * smk_ipv6_check - check Smack access
2459  * @subject: subject Smack label
2460  * @object: object Smack label
2461  * @address: address
2462  * @act: the action being taken
2463  *
2464  * Check an IPv6 access
2465  */
2466 static int smk_ipv6_check(struct smack_known *subject,
2467                                 struct smack_known *object,
2468                                 struct sockaddr_in6 *address, int act)
2469 {
2470 #ifdef CONFIG_AUDIT
2471         struct lsm_network_audit net;
2472 #endif
2473         struct smk_audit_info ad;
2474         int rc;
2475
2476 #ifdef CONFIG_AUDIT
2477         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2478         ad.a.u.net->family = PF_INET6;
2479         ad.a.u.net->dport = ntohs(address->sin6_port);
2480         if (act == SMK_RECEIVING)
2481                 ad.a.u.net->v6info.saddr = address->sin6_addr;
2482         else
2483                 ad.a.u.net->v6info.daddr = address->sin6_addr;
2484 #endif
2485         rc = smk_access(subject, object, MAY_WRITE, &ad);
2486         rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2487         return rc;
2488 }
2489 #endif /* CONFIG_IPV6 */
2490
2491 #ifdef SMACK_IPV6_PORT_LABELING
2492 /**
2493  * smk_ipv6_port_label - Smack port access table management
2494  * @sock: socket
2495  * @address: address
2496  *
2497  * Create or update the port list entry
2498  */
2499 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2500 {
2501         struct sock *sk = sock->sk;
2502         struct sockaddr_in6 *addr6;
2503         struct socket_smack *ssp = sock->sk->sk_security;
2504         struct smk_port_label *spp;
2505         unsigned short port = 0;
2506
2507         if (address == NULL) {
2508                 /*
2509                  * This operation is changing the Smack information
2510                  * on the bound socket. Take the changes to the port
2511                  * as well.
2512                  */
2513                 rcu_read_lock();
2514                 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2515                         if (sk != spp->smk_sock)
2516                                 continue;
2517                         spp->smk_in = ssp->smk_in;
2518                         spp->smk_out = ssp->smk_out;
2519                         rcu_read_unlock();
2520                         return;
2521                 }
2522                 /*
2523                  * A NULL address is only used for updating existing
2524                  * bound entries. If there isn't one, it's OK.
2525                  */
2526                 rcu_read_unlock();
2527                 return;
2528         }
2529
2530         addr6 = (struct sockaddr_in6 *)address;
2531         port = ntohs(addr6->sin6_port);
2532         /*
2533          * This is a special case that is safely ignored.
2534          */
2535         if (port == 0)
2536                 return;
2537
2538         /*
2539          * Look for an existing port list entry.
2540          * This is an indication that a port is getting reused.
2541          */
2542         rcu_read_lock();
2543         list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2544                 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2545                         continue;
2546                 if (spp->smk_can_reuse != 1) {
2547                         rcu_read_unlock();
2548                         return;
2549                 }
2550                 spp->smk_port = port;
2551                 spp->smk_sock = sk;
2552                 spp->smk_in = ssp->smk_in;
2553                 spp->smk_out = ssp->smk_out;
2554                 spp->smk_can_reuse = 0;
2555                 rcu_read_unlock();
2556                 return;
2557         }
2558         rcu_read_unlock();
2559         /*
2560          * A new port entry is required.
2561          */
2562         spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2563         if (spp == NULL)
2564                 return;
2565
2566         spp->smk_port = port;
2567         spp->smk_sock = sk;
2568         spp->smk_in = ssp->smk_in;
2569         spp->smk_out = ssp->smk_out;
2570         spp->smk_sock_type = sock->type;
2571         spp->smk_can_reuse = 0;
2572
2573         mutex_lock(&smack_ipv6_lock);
2574         list_add_rcu(&spp->list, &smk_ipv6_port_list);
2575         mutex_unlock(&smack_ipv6_lock);
2576         return;
2577 }
2578
2579 /**
2580  * smk_ipv6_port_check - check Smack port access
2581  * @sock: socket
2582  * @address: address
2583  *
2584  * Create or update the port list entry
2585  */
2586 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2587                                 int act)
2588 {
2589         struct smk_port_label *spp;
2590         struct socket_smack *ssp = sk->sk_security;
2591         struct smack_known *skp = NULL;
2592         unsigned short port;
2593         struct smack_known *object;
2594
2595         if (act == SMK_RECEIVING) {
2596                 skp = smack_ipv6host_label(address);
2597                 object = ssp->smk_in;
2598         } else {
2599                 skp = ssp->smk_out;
2600                 object = smack_ipv6host_label(address);
2601         }
2602
2603         /*
2604          * The other end is a single label host.
2605          */
2606         if (skp != NULL && object != NULL)
2607                 return smk_ipv6_check(skp, object, address, act);
2608         if (skp == NULL)
2609                 skp = smack_net_ambient;
2610         if (object == NULL)
2611                 object = smack_net_ambient;
2612
2613         /*
2614          * It's remote, so port lookup does no good.
2615          */
2616         if (!smk_ipv6_localhost(address))
2617                 return smk_ipv6_check(skp, object, address, act);
2618
2619         /*
2620          * It's local so the send check has to have passed.
2621          */
2622         if (act == SMK_RECEIVING)
2623                 return 0;
2624
2625         port = ntohs(address->sin6_port);
2626         rcu_read_lock();
2627         list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2628                 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2629                         continue;
2630                 object = spp->smk_in;
2631                 if (act == SMK_CONNECTING)
2632                         ssp->smk_packet = spp->smk_out;
2633                 break;
2634         }
2635         rcu_read_unlock();
2636
2637         return smk_ipv6_check(skp, object, address, act);
2638 }
2639 #endif /* SMACK_IPV6_PORT_LABELING */
2640
2641 /**
2642  * smack_inode_setsecurity - set smack xattrs
2643  * @inode: the object
2644  * @name: attribute name
2645  * @value: attribute value
2646  * @size: size of the attribute
2647  * @flags: unused
2648  *
2649  * Sets the named attribute in the appropriate blob
2650  *
2651  * Returns 0 on success, or an error code
2652  */
2653 static int smack_inode_setsecurity(struct inode *inode, const char *name,
2654                                    const void *value, size_t size, int flags)
2655 {
2656         struct smack_known *skp;
2657         struct inode_smack *nsp = inode->i_security;
2658         struct socket_smack *ssp;
2659         struct socket *sock;
2660         int rc = 0;
2661
2662         if (value == NULL || size > SMK_LONGLABEL || size == 0)
2663                 return -EINVAL;
2664
2665         skp = smk_import_entry(value, size);
2666         if (IS_ERR(skp))
2667                 return PTR_ERR(skp);
2668
2669         if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2670                 nsp->smk_inode = skp;
2671                 nsp->smk_flags |= SMK_INODE_INSTANT;
2672                 return 0;
2673         }
2674         /*
2675          * The rest of the Smack xattrs are only on sockets.
2676          */
2677         if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2678                 return -EOPNOTSUPP;
2679
2680         sock = SOCKET_I(inode);
2681         if (sock == NULL || sock->sk == NULL)
2682                 return -EOPNOTSUPP;
2683
2684         ssp = sock->sk->sk_security;
2685
2686         if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2687                 ssp->smk_in = skp;
2688         else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2689                 ssp->smk_out = skp;
2690                 if (sock->sk->sk_family == PF_INET) {
2691                         rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2692                         if (rc != 0)
2693                                 printk(KERN_WARNING
2694                                         "Smack: \"%s\" netlbl error %d.\n",
2695                                         __func__, -rc);
2696                 }
2697         } else
2698                 return -EOPNOTSUPP;
2699
2700 #ifdef SMACK_IPV6_PORT_LABELING
2701         if (sock->sk->sk_family == PF_INET6)
2702                 smk_ipv6_port_label(sock, NULL);
2703 #endif
2704
2705         return 0;
2706 }
2707
2708 /**
2709  * smack_socket_post_create - finish socket setup
2710  * @sock: the socket
2711  * @family: protocol family
2712  * @type: unused
2713  * @protocol: unused
2714  * @kern: unused
2715  *
2716  * Sets the netlabel information on the socket
2717  *
2718  * Returns 0 on success, and error code otherwise
2719  */
2720 static int smack_socket_post_create(struct socket *sock, int family,
2721                                     int type, int protocol, int kern)
2722 {
2723         struct socket_smack *ssp;
2724
2725         if (sock->sk == NULL)
2726                 return 0;
2727
2728         /*
2729          * Sockets created by kernel threads receive web label.
2730          */
2731         if (unlikely(current->flags & PF_KTHREAD)) {
2732                 ssp = sock->sk->sk_security;
2733                 ssp->smk_in = &smack_known_web;
2734                 ssp->smk_out = &smack_known_web;
2735         }
2736
2737         if (family != PF_INET)
2738                 return 0;
2739         /*
2740          * Set the outbound netlbl.
2741          */
2742         return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2743 }
2744
2745 /**
2746  * smack_socket_socketpair - create socket pair
2747  * @socka: one socket
2748  * @sockb: another socket
2749  *
2750  * Cross reference the peer labels for SO_PEERSEC
2751  *
2752  * Returns 0 on success, and error code otherwise
2753  */
2754 static int smack_socket_socketpair(struct socket *socka,
2755                                    struct socket *sockb)
2756 {
2757         struct socket_smack *asp = socka->sk->sk_security;
2758         struct socket_smack *bsp = sockb->sk->sk_security;
2759
2760         asp->smk_packet = bsp->smk_out;
2761         bsp->smk_packet = asp->smk_out;
2762
2763         return 0;
2764 }
2765
2766 #ifdef SMACK_IPV6_PORT_LABELING
2767 /**
2768  * smack_socket_bind - record port binding information.
2769  * @sock: the socket
2770  * @address: the port address
2771  * @addrlen: size of the address
2772  *
2773  * Records the label bound to a port.
2774  *
2775  * Returns 0
2776  */
2777 static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2778                                 int addrlen)
2779 {
2780         if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2781                 smk_ipv6_port_label(sock, address);
2782         return 0;
2783 }
2784 #endif /* SMACK_IPV6_PORT_LABELING */
2785
2786 /**
2787  * smack_socket_connect - connect access check
2788  * @sock: the socket
2789  * @sap: the other end
2790  * @addrlen: size of sap
2791  *
2792  * Verifies that a connection may be possible
2793  *
2794  * Returns 0 on success, and error code otherwise
2795  */
2796 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2797                                 int addrlen)
2798 {
2799         int rc = 0;
2800 #if IS_ENABLED(CONFIG_IPV6)
2801         struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2802 #endif
2803 #ifdef SMACK_IPV6_SECMARK_LABELING
2804         struct smack_known *rsp;
2805         struct socket_smack *ssp;
2806 #endif
2807
2808         if (sock->sk == NULL)
2809                 return 0;
2810
2811 #ifdef SMACK_IPV6_SECMARK_LABELING
2812         ssp = sock->sk->sk_security;
2813 #endif
2814
2815         switch (sock->sk->sk_family) {
2816         case PF_INET:
2817                 if (addrlen < sizeof(struct sockaddr_in))
2818                         return -EINVAL;
2819                 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2820                 break;
2821         case PF_INET6:
2822                 if (addrlen < sizeof(struct sockaddr_in6))
2823                         return -EINVAL;
2824 #ifdef SMACK_IPV6_SECMARK_LABELING
2825                 rsp = smack_ipv6host_label(sip);
2826                 if (rsp != NULL)
2827                         rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2828                                                 SMK_CONNECTING);
2829 #endif
2830 #ifdef SMACK_IPV6_PORT_LABELING
2831                 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2832 #endif
2833                 break;
2834         }
2835         return rc;
2836 }
2837
2838 /**
2839  * smack_flags_to_may - convert S_ to MAY_ values
2840  * @flags: the S_ value
2841  *
2842  * Returns the equivalent MAY_ value
2843  */
2844 static int smack_flags_to_may(int flags)
2845 {
2846         int may = 0;
2847
2848         if (flags & S_IRUGO)
2849                 may |= MAY_READ;
2850         if (flags & S_IWUGO)
2851                 may |= MAY_WRITE;
2852         if (flags & S_IXUGO)
2853                 may |= MAY_EXEC;
2854
2855         return may;
2856 }
2857
2858 /**
2859  * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2860  * @msg: the object
2861  *
2862  * Returns 0
2863  */
2864 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2865 {
2866         struct smack_known *skp = smk_of_current();
2867
2868         msg->security = skp;
2869         return 0;
2870 }
2871
2872 /**
2873  * smack_msg_msg_free_security - Clear the security blob for msg_msg
2874  * @msg: the object
2875  *
2876  * Clears the blob pointer
2877  */
2878 static void smack_msg_msg_free_security(struct msg_msg *msg)
2879 {
2880         msg->security = NULL;
2881 }
2882
2883 /**
2884  * smack_of_ipc - the smack pointer for the ipc
2885  * @isp: the object
2886  *
2887  * Returns a pointer to the smack value
2888  */
2889 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
2890 {
2891         return (struct smack_known *)isp->security;
2892 }
2893
2894 /**
2895  * smack_ipc_alloc_security - Set the security blob for ipc
2896  * @isp: the object
2897  *
2898  * Returns 0
2899  */
2900 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
2901 {
2902         struct smack_known *skp = smk_of_current();
2903
2904         isp->security = skp;
2905         return 0;
2906 }
2907
2908 /**
2909  * smack_ipc_free_security - Clear the security blob for ipc
2910  * @isp: the object
2911  *
2912  * Clears the blob pointer
2913  */
2914 static void smack_ipc_free_security(struct kern_ipc_perm *isp)
2915 {
2916         isp->security = NULL;
2917 }
2918
2919 /**
2920  * smk_curacc_shm : check if current has access on shm
2921  * @isp : the object
2922  * @access : access requested
2923  *
2924  * Returns 0 if current has the requested access, error code otherwise
2925  */
2926 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
2927 {
2928         struct smack_known *ssp = smack_of_ipc(isp);
2929         struct smk_audit_info ad;
2930         int rc;
2931
2932 #ifdef CONFIG_AUDIT
2933         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2934         ad.a.u.ipc_id = isp->id;
2935 #endif
2936         rc = smk_curacc(ssp, access, &ad);
2937         rc = smk_bu_current("shm", ssp, access, rc);
2938         return rc;
2939 }
2940
2941 /**
2942  * smack_shm_associate - Smack access check for shm
2943  * @isp: the object
2944  * @shmflg: access requested
2945  *
2946  * Returns 0 if current has the requested access, error code otherwise
2947  */
2948 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
2949 {
2950         int may;
2951
2952         may = smack_flags_to_may(shmflg);
2953         return smk_curacc_shm(isp, may);
2954 }
2955
2956 /**
2957  * smack_shm_shmctl - Smack access check for shm
2958  * @isp: the object
2959  * @cmd: what it wants to do
2960  *
2961  * Returns 0 if current has the requested access, error code otherwise
2962  */
2963 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
2964 {
2965         int may;
2966
2967         switch (cmd) {
2968         case IPC_STAT:
2969         case SHM_STAT:
2970         case SHM_STAT_ANY:
2971                 may = MAY_READ;
2972                 break;
2973         case IPC_SET:
2974         case SHM_LOCK:
2975         case SHM_UNLOCK:
2976         case IPC_RMID:
2977                 may = MAY_READWRITE;
2978                 break;
2979         case IPC_INFO:
2980         case SHM_INFO:
2981                 /*
2982                  * System level information.
2983                  */
2984                 return 0;
2985         default:
2986                 return -EINVAL;
2987         }
2988         return smk_curacc_shm(isp, may);
2989 }
2990
2991 /**
2992  * smack_shm_shmat - Smack access for shmat
2993  * @isp: the object
2994  * @shmaddr: unused
2995  * @shmflg: access requested
2996  *
2997  * Returns 0 if current has the requested access, error code otherwise
2998  */
2999 static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
3000                            int shmflg)
3001 {
3002         int may;
3003
3004         may = smack_flags_to_may(shmflg);
3005         return smk_curacc_shm(ipc, may);
3006 }
3007
3008 /**
3009  * smk_curacc_sem : check if current has access on sem
3010  * @isp : the object
3011  * @access : access requested
3012  *
3013  * Returns 0 if current has the requested access, error code otherwise
3014  */
3015 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
3016 {
3017         struct smack_known *ssp = smack_of_ipc(isp);
3018         struct smk_audit_info ad;
3019         int rc;
3020
3021 #ifdef CONFIG_AUDIT
3022         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3023         ad.a.u.ipc_id = isp->id;
3024 #endif
3025         rc = smk_curacc(ssp, access, &ad);
3026         rc = smk_bu_current("sem", ssp, access, rc);
3027         return rc;
3028 }
3029
3030 /**
3031  * smack_sem_associate - Smack access check for sem
3032  * @isp: the object
3033  * @semflg: access requested
3034  *
3035  * Returns 0 if current has the requested access, error code otherwise
3036  */
3037 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
3038 {
3039         int may;
3040
3041         may = smack_flags_to_may(semflg);
3042         return smk_curacc_sem(isp, may);
3043 }
3044
3045 /**
3046  * smack_sem_shmctl - Smack access check for sem
3047  * @isp: the object
3048  * @cmd: what it wants to do
3049  *
3050  * Returns 0 if current has the requested access, error code otherwise
3051  */
3052 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
3053 {
3054         int may;
3055
3056         switch (cmd) {
3057         case GETPID:
3058         case GETNCNT:
3059         case GETZCNT:
3060         case GETVAL:
3061         case GETALL:
3062         case IPC_STAT:
3063         case SEM_STAT:
3064         case SEM_STAT_ANY:
3065                 may = MAY_READ;
3066                 break;
3067         case SETVAL:
3068         case SETALL:
3069         case IPC_RMID:
3070         case IPC_SET:
3071                 may = MAY_READWRITE;
3072                 break;
3073         case IPC_INFO:
3074         case SEM_INFO:
3075                 /*
3076                  * System level information
3077                  */
3078                 return 0;
3079         default:
3080                 return -EINVAL;
3081         }
3082
3083         return smk_curacc_sem(isp, may);
3084 }
3085
3086 /**
3087  * smack_sem_semop - Smack checks of semaphore operations
3088  * @isp: the object
3089  * @sops: unused
3090  * @nsops: unused
3091  * @alter: unused
3092  *
3093  * Treated as read and write in all cases.
3094  *
3095  * Returns 0 if access is allowed, error code otherwise
3096  */
3097 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
3098                            unsigned nsops, int alter)
3099 {
3100         return smk_curacc_sem(isp, MAY_READWRITE);
3101 }
3102
3103 /**
3104  * smk_curacc_msq : helper to check if current has access on msq
3105  * @isp : the msq
3106  * @access : access requested
3107  *
3108  * return 0 if current has access, error otherwise
3109  */
3110 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
3111 {
3112         struct smack_known *msp = smack_of_ipc(isp);
3113         struct smk_audit_info ad;
3114         int rc;
3115
3116 #ifdef CONFIG_AUDIT
3117         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3118         ad.a.u.ipc_id = isp->id;
3119 #endif
3120         rc = smk_curacc(msp, access, &ad);
3121         rc = smk_bu_current("msq", msp, access, rc);
3122         return rc;
3123 }
3124
3125 /**
3126  * smack_msg_queue_associate - Smack access check for msg_queue
3127  * @isp: the object
3128  * @msqflg: access requested
3129  *
3130  * Returns 0 if current has the requested access, error code otherwise
3131  */
3132 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
3133 {
3134         int may;
3135
3136         may = smack_flags_to_may(msqflg);
3137         return smk_curacc_msq(isp, may);
3138 }
3139
3140 /**
3141  * smack_msg_queue_msgctl - Smack access check for msg_queue
3142  * @isp: the object
3143  * @cmd: what it wants to do
3144  *
3145  * Returns 0 if current has the requested access, error code otherwise
3146  */
3147 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
3148 {
3149         int may;
3150
3151         switch (cmd) {
3152         case IPC_STAT:
3153         case MSG_STAT:
3154         case MSG_STAT_ANY:
3155                 may = MAY_READ;
3156                 break;
3157         case IPC_SET:
3158         case IPC_RMID:
3159                 may = MAY_READWRITE;
3160                 break;
3161         case IPC_INFO:
3162         case MSG_INFO:
3163                 /*
3164                  * System level information
3165                  */
3166                 return 0;
3167         default:
3168                 return -EINVAL;
3169         }
3170
3171         return smk_curacc_msq(isp, may);
3172 }
3173
3174 /**
3175  * smack_msg_queue_msgsnd - Smack access check for msg_queue
3176  * @isp: the object
3177  * @msg: unused
3178  * @msqflg: access requested
3179  *
3180  * Returns 0 if current has the requested access, error code otherwise
3181  */
3182 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
3183                                   int msqflg)
3184 {
3185         int may;
3186
3187         may = smack_flags_to_may(msqflg);
3188         return smk_curacc_msq(isp, may);
3189 }
3190
3191 /**
3192  * smack_msg_queue_msgsnd - Smack access check for msg_queue
3193  * @isp: the object
3194  * @msg: unused
3195  * @target: unused
3196  * @type: unused
3197  * @mode: unused
3198  *
3199  * Returns 0 if current has read and write access, error code otherwise
3200  */
3201 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
3202                         struct task_struct *target, long type, int mode)
3203 {
3204         return smk_curacc_msq(isp, MAY_READWRITE);
3205 }
3206
3207 /**
3208  * smack_ipc_permission - Smack access for ipc_permission()
3209  * @ipp: the object permissions
3210  * @flag: access requested
3211  *
3212  * Returns 0 if current has read and write access, error code otherwise
3213  */
3214 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3215 {
3216         struct smack_known *iskp = ipp->security;
3217         int may = smack_flags_to_may(flag);
3218         struct smk_audit_info ad;
3219         int rc;
3220
3221 #ifdef CONFIG_AUDIT
3222         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3223         ad.a.u.ipc_id = ipp->id;
3224 #endif
3225         rc = smk_curacc(iskp, may, &ad);
3226         rc = smk_bu_current("svipc", iskp, may, rc);
3227         return rc;
3228 }
3229
3230 /**
3231  * smack_ipc_getsecid - Extract smack security id
3232  * @ipp: the object permissions
3233  * @secid: where result will be saved
3234  */
3235 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3236 {
3237         struct smack_known *iskp = ipp->security;
3238
3239         *secid = iskp->smk_secid;
3240 }
3241
3242 /**
3243  * smack_d_instantiate - Make sure the blob is correct on an inode
3244  * @opt_dentry: dentry where inode will be attached
3245  * @inode: the object
3246  *
3247  * Set the inode's security blob if it hasn't been done already.
3248  */
3249 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3250 {
3251         struct super_block *sbp;
3252         struct superblock_smack *sbsp;
3253         struct inode_smack *isp;
3254         struct smack_known *skp;
3255         struct smack_known *ckp = smk_of_current();
3256         struct smack_known *final;
3257         char trattr[TRANS_TRUE_SIZE];
3258         int transflag = 0;
3259         int rc;
3260         struct dentry *dp;
3261
3262         if (inode == NULL)
3263                 return;
3264
3265         isp = inode->i_security;
3266
3267         mutex_lock(&isp->smk_lock);
3268         /*
3269          * If the inode is already instantiated
3270          * take the quick way out
3271          */
3272         if (isp->smk_flags & SMK_INODE_INSTANT)
3273                 goto unlockandout;
3274
3275         sbp = inode->i_sb;
3276         sbsp = sbp->s_security;
3277         /*
3278          * We're going to use the superblock default label
3279          * if there's no label on the file.
3280          */
3281         final = sbsp->smk_default;
3282
3283         /*
3284          * If this is the root inode the superblock
3285          * may be in the process of initialization.
3286          * If that is the case use the root value out
3287          * of the superblock.
3288          */
3289         if (opt_dentry->d_parent == opt_dentry) {
3290                 switch (sbp->s_magic) {
3291                 case CGROUP_SUPER_MAGIC:
3292                 case CGROUP2_SUPER_MAGIC:
3293                         /*
3294                          * The cgroup filesystem is never mounted,
3295                          * so there's no opportunity to set the mount
3296                          * options.
3297                          */
3298                         sbsp->smk_root = &smack_known_star;
3299                         sbsp->smk_default = &smack_known_star;
3300                         isp->smk_inode = sbsp->smk_root;
3301                         break;
3302                 case TMPFS_MAGIC:
3303                         /*
3304                          * What about shmem/tmpfs anonymous files with dentry
3305                          * obtained from d_alloc_pseudo()?
3306                          */
3307                         isp->smk_inode = smk_of_current();
3308                         break;
3309                 case PIPEFS_MAGIC:
3310                         isp->smk_inode = smk_of_current();
3311                         break;
3312                 case SOCKFS_MAGIC:
3313                         /*
3314                          * Socket access is controlled by the socket
3315                          * structures associated with the task involved.
3316                          */
3317                         isp->smk_inode = &smack_known_star;
3318                         break;
3319                 default:
3320                         isp->smk_inode = sbsp->smk_root;
3321                         break;
3322                 }
3323                 isp->smk_flags |= SMK_INODE_INSTANT;
3324                 goto unlockandout;
3325         }
3326
3327         /*
3328          * This is pretty hackish.
3329          * Casey says that we shouldn't have to do
3330          * file system specific code, but it does help
3331          * with keeping it simple.
3332          */
3333         switch (sbp->s_magic) {
3334         case SMACK_MAGIC:
3335         case CGROUP_SUPER_MAGIC:
3336         case CGROUP2_SUPER_MAGIC:
3337                 /*
3338                  * Casey says that it's a little embarrassing
3339                  * that the smack file system doesn't do
3340                  * extended attributes.
3341                  *
3342                  * Cgroupfs is special
3343                  */
3344                 final = &smack_known_star;
3345                 break;
3346         case DEVPTS_SUPER_MAGIC:
3347                 /*
3348                  * devpts seems content with the label of the task.
3349                  * Programs that change smack have to treat the
3350                  * pty with respect.
3351                  */
3352                 final = ckp;
3353                 break;
3354         case PROC_SUPER_MAGIC:
3355                 /*
3356                  * Casey says procfs appears not to care.
3357                  * The superblock default suffices.
3358                  */
3359                 break;
3360         case TMPFS_MAGIC:
3361                 /*
3362                  * Device labels should come from the filesystem,
3363                  * but watch out, because they're volitile,
3364                  * getting recreated on every reboot.
3365                  */
3366                 final = &smack_known_star;
3367                 /*
3368                  * Fall through.
3369                  *
3370                  * If a smack value has been set we want to use it,
3371                  * but since tmpfs isn't giving us the opportunity
3372                  * to set mount options simulate setting the
3373                  * superblock default.
3374                  */
3375         default:
3376                 /*
3377                  * This isn't an understood special case.
3378                  * Get the value from the xattr.
3379                  */
3380
3381                 /*
3382                  * UNIX domain sockets use lower level socket data.
3383                  */
3384                 if (S_ISSOCK(inode->i_mode)) {
3385                         final = &smack_known_star;
3386                         break;
3387                 }
3388                 /*
3389                  * No xattr support means, alas, no SMACK label.
3390                  * Use the aforeapplied default.
3391                  * It would be curious if the label of the task
3392                  * does not match that assigned.
3393                  */
3394                 if (!(inode->i_opflags & IOP_XATTR))
3395                         break;
3396                 /*
3397                  * Get the dentry for xattr.
3398                  */
3399                 dp = dget(opt_dentry);
3400                 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3401                 if (!IS_ERR_OR_NULL(skp))
3402                         final = skp;
3403
3404                 /*
3405                  * Transmuting directory
3406                  */
3407                 if (S_ISDIR(inode->i_mode)) {
3408                         /*
3409                          * If this is a new directory and the label was
3410                          * transmuted when the inode was initialized
3411                          * set the transmute attribute on the directory
3412                          * and mark the inode.
3413                          *
3414                          * If there is a transmute attribute on the
3415                          * directory mark the inode.
3416                          */
3417                         if (isp->smk_flags & SMK_INODE_CHANGED) {
3418                                 isp->smk_flags &= ~SMK_INODE_CHANGED;
3419                                 rc = __vfs_setxattr(dp, inode,
3420                                         XATTR_NAME_SMACKTRANSMUTE,
3421                                         TRANS_TRUE, TRANS_TRUE_SIZE,
3422                                         0);
3423                         } else {
3424                                 rc = __vfs_getxattr(dp, inode,
3425                                         XATTR_NAME_SMACKTRANSMUTE, trattr,
3426                                         TRANS_TRUE_SIZE);
3427                                 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3428                                                        TRANS_TRUE_SIZE) != 0)
3429                                         rc = -EINVAL;
3430                         }
3431                         if (rc >= 0)
3432                                 transflag = SMK_INODE_TRANSMUTE;
3433                 }
3434                 /*
3435                  * Don't let the exec or mmap label be "*" or "@".
3436                  */
3437                 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3438                 if (IS_ERR(skp) || skp == &smack_known_star ||
3439                     skp == &smack_known_web)
3440                         skp = NULL;
3441                 isp->smk_task = skp;
3442
3443                 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3444                 if (IS_ERR(skp) || skp == &smack_known_star ||
3445                     skp == &smack_known_web)
3446                         skp = NULL;
3447                 isp->smk_mmap = skp;
3448
3449                 dput(dp);
3450                 break;
3451         }
3452
3453         if (final == NULL)
3454                 isp->smk_inode = ckp;
3455         else
3456                 isp->smk_inode = final;
3457
3458         isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3459
3460 unlockandout:
3461         mutex_unlock(&isp->smk_lock);
3462         return;
3463 }
3464
3465 /**
3466  * smack_getprocattr - Smack process attribute access
3467  * @p: the object task
3468  * @name: the name of the attribute in /proc/.../attr
3469  * @value: where to put the result
3470  *
3471  * Places a copy of the task Smack into value
3472  *
3473  * Returns the length of the smack label or an error code
3474  */
3475 static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3476 {
3477         struct smack_known *skp = smk_of_task_struct(p);
3478         char *cp;
3479         int slen;
3480
3481         if (strcmp(name, "current") != 0)
3482                 return -EINVAL;
3483
3484         cp = kstrdup(skp->smk_known, GFP_KERNEL);
3485         if (cp == NULL)
3486                 return -ENOMEM;
3487
3488         slen = strlen(cp);
3489         *value = cp;
3490         return slen;
3491 }
3492
3493 /**
3494  * smack_setprocattr - Smack process attribute setting
3495  * @name: the name of the attribute in /proc/.../attr
3496  * @value: the value to set
3497  * @size: the size of the value
3498  *
3499  * Sets the Smack value of the task. Only setting self
3500  * is permitted and only with privilege
3501  *
3502  * Returns the length of the smack label or an error code
3503  */
3504 static int smack_setprocattr(const char *name, void *value, size_t size)
3505 {
3506         struct task_smack *tsp = smack_cred(current_cred());
3507         struct cred *new;
3508         struct smack_known *skp;
3509         struct smack_known_list_elem *sklep;
3510         int rc;
3511
3512         if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3513                 return -EPERM;
3514
3515         if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3516                 return -EINVAL;
3517
3518         if (strcmp(name, "current") != 0)
3519                 return -EINVAL;
3520
3521         skp = smk_import_entry(value, size);
3522         if (IS_ERR(skp))
3523                 return PTR_ERR(skp);
3524
3525         /*
3526          * No process is ever allowed the web ("@") label
3527          * and the star ("*") label.
3528          */
3529         if (skp == &smack_known_web || skp == &smack_known_star)
3530                 return -EINVAL;
3531
3532         if (!smack_privileged(CAP_MAC_ADMIN)) {
3533                 rc = -EPERM;
3534                 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3535                         if (sklep->smk_label == skp) {
3536                                 rc = 0;
3537                                 break;
3538                         }
3539                 if (rc)
3540                         return rc;
3541         }
3542
3543         new = prepare_creds();
3544         if (new == NULL)
3545                 return -ENOMEM;
3546
3547         tsp = smack_cred(new);
3548         tsp->smk_task = skp;
3549         /*
3550          * process can change its label only once
3551          */
3552         smk_destroy_label_list(&tsp->smk_relabel);
3553
3554         commit_creds(new);
3555         return size;
3556 }
3557
3558 /**
3559  * smack_unix_stream_connect - Smack access on UDS
3560  * @sock: one sock
3561  * @other: the other sock
3562  * @newsk: unused
3563  *
3564  * Return 0 if a subject with the smack of sock could access
3565  * an object with the smack of other, otherwise an error code
3566  */
3567 static int smack_unix_stream_connect(struct sock *sock,
3568                                      struct sock *other, struct sock *newsk)
3569 {
3570         struct smack_known *skp;
3571         struct smack_known *okp;
3572         struct socket_smack *ssp = sock->sk_security;
3573         struct socket_smack *osp = other->sk_security;
3574         struct socket_smack *nsp = newsk->sk_security;
3575         struct smk_audit_info ad;
3576         int rc = 0;
3577 #ifdef CONFIG_AUDIT
3578         struct lsm_network_audit net;
3579 #endif
3580
3581         if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3582                 skp = ssp->smk_out;
3583                 okp = osp->smk_in;
3584 #ifdef CONFIG_AUDIT
3585                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3586                 smk_ad_setfield_u_net_sk(&ad, other);
3587 #endif
3588                 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3589                 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
3590                 if (rc == 0) {
3591                         okp = osp->smk_out;
3592                         skp = ssp->smk_in;
3593                         rc = smk_access(okp, skp, MAY_WRITE, &ad);
3594                         rc = smk_bu_note("UDS connect", okp, skp,
3595                                                 MAY_WRITE, rc);
3596                 }
3597         }
3598
3599         /*
3600          * Cross reference the peer labels for SO_PEERSEC.
3601          */
3602         if (rc == 0) {
3603                 nsp->smk_packet = ssp->smk_out;
3604                 ssp->smk_packet = osp->smk_out;
3605         }
3606
3607         return rc;
3608 }
3609
3610 /**
3611  * smack_unix_may_send - Smack access on UDS
3612  * @sock: one socket
3613  * @other: the other socket
3614  *
3615  * Return 0 if a subject with the smack of sock could access
3616  * an object with the smack of other, otherwise an error code
3617  */
3618 static int smack_unix_may_send(struct socket *sock, struct socket *other)
3619 {
3620         struct socket_smack *ssp = sock->sk->sk_security;
3621         struct socket_smack *osp = other->sk->sk_security;
3622         struct smk_audit_info ad;
3623         int rc;
3624
3625 #ifdef CONFIG_AUDIT
3626         struct lsm_network_audit net;
3627
3628         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3629         smk_ad_setfield_u_net_sk(&ad, other->sk);
3630 #endif
3631
3632         if (smack_privileged(CAP_MAC_OVERRIDE))
3633                 return 0;
3634
3635         rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3636         rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
3637         return rc;
3638 }
3639
3640 /**
3641  * smack_socket_sendmsg - Smack check based on destination host
3642  * @sock: the socket
3643  * @msg: the message
3644  * @size: the size of the message
3645  *
3646  * Return 0 if the current subject can write to the destination host.
3647  * For IPv4 this is only a question if the destination is a single label host.
3648  * For IPv6 this is a check against the label of the port.
3649  */
3650 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3651                                 int size)
3652 {
3653         struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
3654 #if IS_ENABLED(CONFIG_IPV6)
3655         struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
3656 #endif
3657 #ifdef SMACK_IPV6_SECMARK_LABELING
3658         struct socket_smack *ssp = sock->sk->sk_security;
3659         struct smack_known *rsp;
3660 #endif
3661         int rc = 0;
3662
3663         /*
3664          * Perfectly reasonable for this to be NULL
3665          */
3666         if (sip == NULL)
3667                 return 0;
3668
3669         switch (sock->sk->sk_family) {
3670         case AF_INET:
3671                 rc = smack_netlabel_send(sock->sk, sip);
3672                 break;
3673         case AF_INET6:
3674 #ifdef SMACK_IPV6_SECMARK_LABELING
3675                 rsp = smack_ipv6host_label(sap);
3676                 if (rsp != NULL)
3677                         rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3678                                                 SMK_CONNECTING);
3679 #endif
3680 #ifdef SMACK_IPV6_PORT_LABELING
3681                 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3682 #endif
3683                 break;
3684         }
3685         return rc;
3686 }
3687
3688 /**
3689  * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3690  * @sap: netlabel secattr
3691  * @ssp: socket security information
3692  *
3693  * Returns a pointer to a Smack label entry found on the label list.
3694  */
3695 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3696                                                 struct socket_smack *ssp)
3697 {
3698         struct smack_known *skp;
3699         int found = 0;
3700         int acat;
3701         int kcat;
3702
3703         if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3704                 /*
3705                  * Looks like a CIPSO packet.
3706                  * If there are flags but no level netlabel isn't
3707                  * behaving the way we expect it to.
3708                  *
3709                  * Look it up in the label table
3710                  * Without guidance regarding the smack value
3711                  * for the packet fall back on the network
3712                  * ambient value.
3713                  */
3714                 rcu_read_lock();
3715                 list_for_each_entry_rcu(skp, &smack_known_list, list) {
3716                         if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3717                                 continue;
3718                         /*
3719                          * Compare the catsets. Use the netlbl APIs.
3720                          */
3721                         if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3722                                 if ((skp->smk_netlabel.flags &
3723                                      NETLBL_SECATTR_MLS_CAT) == 0)
3724                                         found = 1;
3725                                 break;
3726                         }
3727                         for (acat = -1, kcat = -1; acat == kcat; ) {
3728                                 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3729                                                           acat + 1);
3730                                 kcat = netlbl_catmap_walk(
3731                                         skp->smk_netlabel.attr.mls.cat,
3732                                         kcat + 1);
3733                                 if (acat < 0 || kcat < 0)
3734                                         break;
3735                         }
3736                         if (acat == kcat) {
3737                                 found = 1;
3738                                 break;
3739                         }
3740                 }
3741                 rcu_read_unlock();
3742
3743                 if (found)
3744                         return skp;
3745
3746                 if (ssp != NULL && ssp->smk_in == &smack_known_star)
3747                         return &smack_known_web;
3748                 return &smack_known_star;
3749         }
3750         if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3751                 /*
3752                  * Looks like a fallback, which gives us a secid.
3753                  */
3754                 return smack_from_secid(sap->attr.secid);
3755         /*
3756          * Without guidance regarding the smack value
3757          * for the packet fall back on the network
3758          * ambient value.
3759          */
3760         return smack_net_ambient;
3761 }
3762
3763 #if IS_ENABLED(CONFIG_IPV6)
3764 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3765 {
3766         u8 nexthdr;
3767         int offset;
3768         int proto = -EINVAL;
3769         struct ipv6hdr _ipv6h;
3770         struct ipv6hdr *ip6;
3771         __be16 frag_off;
3772         struct tcphdr _tcph, *th;
3773         struct udphdr _udph, *uh;
3774         struct dccp_hdr _dccph, *dh;
3775
3776         sip->sin6_port = 0;
3777
3778         offset = skb_network_offset(skb);
3779         ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3780         if (ip6 == NULL)
3781                 return -EINVAL;
3782         sip->sin6_addr = ip6->saddr;
3783
3784         nexthdr = ip6->nexthdr;
3785         offset += sizeof(_ipv6h);
3786         offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3787         if (offset < 0)
3788                 return -EINVAL;
3789
3790         proto = nexthdr;
3791         switch (proto) {
3792         case IPPROTO_TCP:
3793                 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3794                 if (th != NULL)
3795                         sip->sin6_port = th->source;
3796                 break;
3797         case IPPROTO_UDP:
3798         case IPPROTO_UDPLITE:
3799                 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3800                 if (uh != NULL)
3801                         sip->sin6_port = uh->source;
3802                 break;
3803         case IPPROTO_DCCP:
3804                 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3805                 if (dh != NULL)
3806                         sip->sin6_port = dh->dccph_sport;
3807                 break;
3808         }
3809         return proto;
3810 }
3811 #endif /* CONFIG_IPV6 */
3812
3813 /**
3814  * smack_socket_sock_rcv_skb - Smack packet delivery access check
3815  * @sk: socket
3816  * @skb: packet
3817  *
3818  * Returns 0 if the packet should be delivered, an error code otherwise
3819  */
3820 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3821 {
3822         struct netlbl_lsm_secattr secattr;
3823         struct socket_smack *ssp = sk->sk_security;
3824         struct smack_known *skp = NULL;
3825         int rc = 0;
3826         struct smk_audit_info ad;
3827         u16 family = sk->sk_family;
3828 #ifdef CONFIG_AUDIT
3829         struct lsm_network_audit net;
3830 #endif
3831 #if IS_ENABLED(CONFIG_IPV6)
3832         struct sockaddr_in6 sadd;
3833         int proto;
3834
3835         if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3836                 family = PF_INET;
3837 #endif /* CONFIG_IPV6 */
3838
3839         switch (family) {
3840         case PF_INET:
3841 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3842                 /*
3843                  * If there is a secmark use it rather than the CIPSO label.
3844                  * If there is no secmark fall back to CIPSO.
3845                  * The secmark is assumed to reflect policy better.
3846                  */
3847                 if (skb && skb->secmark != 0) {
3848                         skp = smack_from_secid(skb->secmark);
3849                         goto access_check;
3850                 }
3851 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3852                 /*
3853                  * Translate what netlabel gave us.
3854                  */
3855                 netlbl_secattr_init(&secattr);
3856
3857                 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3858                 if (rc == 0)
3859                         skp = smack_from_secattr(&secattr, ssp);
3860                 else
3861                         skp = smack_net_ambient;
3862
3863                 netlbl_secattr_destroy(&secattr);
3864
3865 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3866 access_check:
3867 #endif
3868 #ifdef CONFIG_AUDIT
3869                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3870                 ad.a.u.net->family = family;
3871                 ad.a.u.net->netif = skb->skb_iif;
3872                 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3873 #endif
3874                 /*
3875                  * Receiving a packet requires that the other end
3876                  * be able to write here. Read access is not required.
3877                  * This is the simplist possible security model
3878                  * for networking.
3879                  */
3880                 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3881                 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
3882                                         MAY_WRITE, rc);
3883                 if (rc != 0)
3884                         netlbl_skbuff_err(skb, family, rc, 0);
3885                 break;
3886 #if IS_ENABLED(CONFIG_IPV6)
3887         case PF_INET6:
3888                 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3889                 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3890                     proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
3891                         break;
3892 #ifdef SMACK_IPV6_SECMARK_LABELING
3893                 if (skb && skb->secmark != 0)
3894                         skp = smack_from_secid(skb->secmark);
3895                 else
3896                         skp = smack_ipv6host_label(&sadd);
3897                 if (skp == NULL)
3898                         skp = smack_net_ambient;
3899 #ifdef CONFIG_AUDIT
3900                 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3901                 ad.a.u.net->family = family;
3902                 ad.a.u.net->netif = skb->skb_iif;
3903                 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3904 #endif /* CONFIG_AUDIT */
3905                 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3906                 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3907                                         MAY_WRITE, rc);
3908 #endif /* SMACK_IPV6_SECMARK_LABELING */
3909 #ifdef SMACK_IPV6_PORT_LABELING
3910                 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3911 #endif /* SMACK_IPV6_PORT_LABELING */
3912                 if (rc != 0)
3913                         icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3914                                         ICMPV6_ADM_PROHIBITED, 0);
3915                 break;
3916 #endif /* CONFIG_IPV6 */
3917         }
3918
3919         return rc;
3920 }
3921
3922 /**
3923  * smack_socket_getpeersec_stream - pull in packet label
3924  * @sock: the socket
3925  * @optval: user's destination
3926  * @optlen: size thereof
3927  * @len: max thereof
3928  *
3929  * returns zero on success, an error code otherwise
3930  */
3931 static int smack_socket_getpeersec_stream(struct socket *sock,
3932                                           char __user *optval,
3933                                           int __user *optlen, unsigned len)
3934 {
3935         struct socket_smack *ssp;
3936         char *rcp = "";
3937         int slen = 1;
3938         int rc = 0;
3939
3940         ssp = sock->sk->sk_security;
3941         if (ssp->smk_packet != NULL) {
3942                 rcp = ssp->smk_packet->smk_known;
3943                 slen = strlen(rcp) + 1;
3944         }
3945
3946         if (slen > len)
3947                 rc = -ERANGE;
3948         else if (copy_to_user(optval, rcp, slen) != 0)
3949                 rc = -EFAULT;
3950
3951         if (put_user(slen, optlen) != 0)
3952                 rc = -EFAULT;
3953
3954         return rc;
3955 }
3956
3957
3958 /**
3959  * smack_socket_getpeersec_dgram - pull in packet label
3960  * @sock: the peer socket
3961  * @skb: packet data
3962  * @secid: pointer to where to put the secid of the packet
3963  *
3964  * Sets the netlabel socket state on sk from parent
3965  */
3966 static int smack_socket_getpeersec_dgram(struct socket *sock,
3967                                          struct sk_buff *skb, u32 *secid)
3968
3969 {
3970         struct netlbl_lsm_secattr secattr;
3971         struct socket_smack *ssp = NULL;
3972         struct smack_known *skp;
3973         int family = PF_UNSPEC;
3974         u32 s = 0;      /* 0 is the invalid secid */
3975         int rc;
3976
3977         if (skb != NULL) {
3978                 if (skb->protocol == htons(ETH_P_IP))
3979                         family = PF_INET;
3980 #if IS_ENABLED(CONFIG_IPV6)
3981                 else if (skb->protocol == htons(ETH_P_IPV6))
3982                         family = PF_INET6;
3983 #endif /* CONFIG_IPV6 */
3984         }
3985         if (family == PF_UNSPEC && sock != NULL)
3986                 family = sock->sk->sk_family;
3987
3988         switch (family) {
3989         case PF_UNIX:
3990                 ssp = sock->sk->sk_security;
3991                 s = ssp->smk_out->smk_secid;
3992                 break;
3993         case PF_INET:
3994 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
3995                 s = skb->secmark;
3996                 if (s != 0)
3997                         break;
3998 #endif
3999                 /*
4000                  * Translate what netlabel gave us.
4001                  */
4002                 if (sock != NULL && sock->sk != NULL)
4003                         ssp = sock->sk->sk_security;
4004                 netlbl_secattr_init(&secattr);
4005                 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4006                 if (rc == 0) {
4007                         skp = smack_from_secattr(&secattr, ssp);
4008                         s = skp->smk_secid;
4009                 }
4010                 netlbl_secattr_destroy(&secattr);
4011                 break;
4012         case PF_INET6:
4013 #ifdef SMACK_IPV6_SECMARK_LABELING
4014                 s = skb->secmark;
4015 #endif
4016                 break;
4017         }
4018         *secid = s;
4019         if (s == 0)
4020                 return -EINVAL;
4021         return 0;
4022 }
4023
4024 /**
4025  * smack_sock_graft - Initialize a newly created socket with an existing sock
4026  * @sk: child sock
4027  * @parent: parent socket
4028  *
4029  * Set the smk_{in,out} state of an existing sock based on the process that
4030  * is creating the new socket.
4031  */
4032 static void smack_sock_graft(struct sock *sk, struct socket *parent)
4033 {
4034         struct socket_smack *ssp;
4035         struct smack_known *skp = smk_of_current();
4036
4037         if (sk == NULL ||
4038             (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4039                 return;
4040
4041         ssp = sk->sk_security;
4042         ssp->smk_in = skp;
4043         ssp->smk_out = skp;
4044         /* cssp->smk_packet is already set in smack_inet_csk_clone() */
4045 }
4046
4047 /**
4048  * smack_inet_conn_request - Smack access check on connect
4049  * @sk: socket involved
4050  * @skb: packet
4051  * @req: unused
4052  *
4053  * Returns 0 if a task with the packet label could write to
4054  * the socket, otherwise an error code
4055  */
4056 static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4057                                    struct request_sock *req)
4058 {
4059         u16 family = sk->sk_family;
4060         struct smack_known *skp;
4061         struct socket_smack *ssp = sk->sk_security;
4062         struct netlbl_lsm_secattr secattr;
4063         struct sockaddr_in addr;
4064         struct iphdr *hdr;
4065         struct smack_known *hskp;
4066         int rc;
4067         struct smk_audit_info ad;
4068 #ifdef CONFIG_AUDIT
4069         struct lsm_network_audit net;
4070 #endif
4071
4072 #if IS_ENABLED(CONFIG_IPV6)
4073         if (family == PF_INET6) {
4074                 /*
4075                  * Handle mapped IPv4 packets arriving
4076                  * via IPv6 sockets. Don't set up netlabel
4077                  * processing on IPv6.
4078                  */
4079                 if (skb->protocol == htons(ETH_P_IP))
4080                         family = PF_INET;
4081                 else
4082                         return 0;
4083         }
4084 #endif /* CONFIG_IPV6 */
4085
4086 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4087         /*
4088          * If there is a secmark use it rather than the CIPSO label.
4089          * If there is no secmark fall back to CIPSO.
4090          * The secmark is assumed to reflect policy better.
4091          */
4092         if (skb && skb->secmark != 0) {
4093                 skp = smack_from_secid(skb->secmark);
4094                 goto access_check;
4095         }
4096 #endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4097
4098         netlbl_secattr_init(&secattr);
4099         rc = netlbl_skbuff_getattr(skb, family, &secattr);
4100         if (rc == 0)
4101                 skp = smack_from_secattr(&secattr, ssp);
4102         else
4103                 skp = &smack_known_huh;
4104         netlbl_secattr_destroy(&secattr);
4105
4106 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4107 access_check:
4108 #endif
4109
4110 #ifdef CONFIG_AUDIT
4111         smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4112         ad.a.u.net->family = family;
4113         ad.a.u.net->netif = skb->skb_iif;
4114         ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4115 #endif
4116         /*
4117          * Receiving a packet requires that the other end be able to write
4118          * here. Read access is not required.
4119          */
4120         rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4121         rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4122         if (rc != 0)
4123                 return rc;
4124
4125         /*
4126          * Save the peer's label in the request_sock so we can later setup
4127          * smk_packet in the child socket so that SO_PEERCRED can report it.
4128          */
4129         req->peer_secid = skp->smk_secid;
4130
4131         /*
4132          * We need to decide if we want to label the incoming connection here
4133          * if we do we only need to label the request_sock and the stack will
4134          * propagate the wire-label to the sock when it is created.
4135          */
4136         hdr = ip_hdr(skb);
4137         addr.sin_addr.s_addr = hdr->saddr;
4138         rcu_read_lock();
4139         hskp = smack_ipv4host_label(&addr);
4140         rcu_read_unlock();
4141
4142         if (hskp == NULL)
4143                 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4144         else
4145                 netlbl_req_delattr(req);
4146
4147         return rc;
4148 }
4149
4150 /**
4151  * smack_inet_csk_clone - Copy the connection information to the new socket
4152  * @sk: the new socket
4153  * @req: the connection's request_sock
4154  *
4155  * Transfer the connection's peer label to the newly created socket.
4156  */
4157 static void smack_inet_csk_clone(struct sock *sk,
4158                                  const struct request_sock *req)
4159 {
4160         struct socket_smack *ssp = sk->sk_security;
4161         struct smack_known *skp;
4162
4163         if (req->peer_secid != 0) {
4164                 skp = smack_from_secid(req->peer_secid);
4165                 ssp->smk_packet = skp;
4166         } else
4167                 ssp->smk_packet = NULL;
4168 }
4169
4170 /*
4171  * Key management security hooks
4172  *
4173  * Casey has not tested key support very heavily.
4174  * The permission check is most likely too restrictive.
4175  * If you care about keys please have a look.
4176  */
4177 #ifdef CONFIG_KEYS
4178
4179 /**
4180  * smack_key_alloc - Set the key security blob
4181  * @key: object
4182  * @cred: the credentials to use
4183  * @flags: unused
4184  *
4185  * No allocation required
4186  *
4187  * Returns 0
4188  */
4189 static int smack_key_alloc(struct key *key, const struct cred *cred,
4190                            unsigned long flags)
4191 {
4192         struct smack_known *skp = smk_of_task(smack_cred(cred));
4193
4194         key->security = skp;
4195         return 0;
4196 }
4197
4198 /**
4199  * smack_key_free - Clear the key security blob
4200  * @key: the object
4201  *
4202  * Clear the blob pointer
4203  */
4204 static void smack_key_free(struct key *key)
4205 {
4206         key->security = NULL;
4207 }
4208
4209 /**
4210  * smack_key_permission - Smack access on a key
4211  * @key_ref: gets to the object
4212  * @cred: the credentials to use
4213  * @perm: requested key permissions
4214  *
4215  * Return 0 if the task has read and write to the object,
4216  * an error code otherwise
4217  */
4218 static int smack_key_permission(key_ref_t key_ref,
4219                                 const struct cred *cred, unsigned perm)
4220 {
4221         struct key *keyp;
4222         struct smk_audit_info ad;
4223         struct smack_known *tkp = smk_of_task(smack_cred(cred));
4224         int request = 0;
4225         int rc;
4226
4227         /*
4228          * Validate requested permissions
4229          */
4230         if (perm & ~KEY_NEED_ALL)
4231                 return -EINVAL;
4232
4233         keyp = key_ref_to_ptr(key_ref);
4234         if (keyp == NULL)
4235                 return -EINVAL;
4236         /*
4237          * If the key hasn't been initialized give it access so that
4238          * it may do so.
4239          */
4240         if (keyp->security == NULL)
4241                 return 0;
4242         /*
4243          * This should not occur
4244          */
4245         if (tkp == NULL)
4246                 return -EACCES;
4247
4248         if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4249                 return 0;
4250
4251 #ifdef CONFIG_AUDIT
4252         smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4253         ad.a.u.key_struct.key = keyp->serial;
4254         ad.a.u.key_struct.key_desc = keyp->description;
4255 #endif
4256         if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4257                 request |= MAY_READ;
4258         if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4259                 request |= MAY_WRITE;
4260         rc = smk_access(tkp, keyp->security, request, &ad);
4261         rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4262         return rc;
4263 }
4264
4265 /*
4266  * smack_key_getsecurity - Smack label tagging the key
4267  * @key points to the key to be queried
4268  * @_buffer points to a pointer that should be set to point to the
4269  * resulting string (if no label or an error occurs).
4270  * Return the length of the string (including terminating NUL) or -ve if
4271  * an error.
4272  * May also return 0 (and a NULL buffer pointer) if there is no label.
4273  */
4274 static int smack_key_getsecurity(struct key *key, char **_buffer)
4275 {
4276         struct smack_known *skp = key->security;
4277         size_t length;
4278         char *copy;
4279
4280         if (key->security == NULL) {
4281                 *_buffer = NULL;
4282                 return 0;
4283         }
4284
4285         copy = kstrdup(skp->smk_known, GFP_KERNEL);
4286         if (copy == NULL)
4287                 return -ENOMEM;
4288         length = strlen(copy) + 1;
4289
4290         *_buffer = copy;
4291         return length;
4292 }
4293
4294 #endif /* CONFIG_KEYS */
4295
4296 /*
4297  * Smack Audit hooks
4298  *
4299  * Audit requires a unique representation of each Smack specific
4300  * rule. This unique representation is used to distinguish the
4301  * object to be audited from remaining kernel objects and also
4302  * works as a glue between the audit hooks.
4303  *
4304  * Since repository entries are added but never deleted, we'll use
4305  * the smack_known label address related to the given audit rule as
4306  * the needed unique representation. This also better fits the smack
4307  * model where nearly everything is a label.
4308  */
4309 #ifdef CONFIG_AUDIT
4310
4311 /**
4312  * smack_audit_rule_init - Initialize a smack audit rule
4313  * @field: audit rule fields given from user-space (audit.h)
4314  * @op: required testing operator (=, !=, >, <, ...)
4315  * @rulestr: smack label to be audited
4316  * @vrule: pointer to save our own audit rule representation
4317  *
4318  * Prepare to audit cases where (@field @op @rulestr) is true.
4319  * The label to be audited is created if necessay.
4320  */
4321 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4322 {
4323         struct smack_known *skp;
4324         char **rule = (char **)vrule;
4325         *rule = NULL;
4326
4327         if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4328                 return -EINVAL;
4329
4330         if (op != Audit_equal && op != Audit_not_equal)
4331                 return -EINVAL;
4332
4333         skp = smk_import_entry(rulestr, 0);
4334         if (IS_ERR(skp))
4335                 return PTR_ERR(skp);
4336
4337         *rule = skp->smk_known;
4338
4339         return 0;
4340 }
4341
4342 /**
4343  * smack_audit_rule_known - Distinguish Smack audit rules
4344  * @krule: rule of interest, in Audit kernel representation format
4345  *
4346  * This is used to filter Smack rules from remaining Audit ones.
4347  * If it's proved that this rule belongs to us, the
4348  * audit_rule_match hook will be called to do the final judgement.
4349  */
4350 static int smack_audit_rule_known(struct audit_krule *krule)
4351 {
4352         struct audit_field *f;
4353         int i;
4354
4355         for (i = 0; i < krule->field_count; i++) {
4356                 f = &krule->fields[i];
4357
4358                 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4359                         return 1;
4360         }
4361
4362         return 0;
4363 }
4364
4365 /**
4366  * smack_audit_rule_match - Audit given object ?
4367  * @secid: security id for identifying the object to test
4368  * @field: audit rule flags given from user-space
4369  * @op: required testing operator
4370  * @vrule: smack internal rule presentation
4371  * @actx: audit context associated with the check
4372  *
4373  * The core Audit hook. It's used to take the decision of
4374  * whether to audit or not to audit a given object.
4375  */
4376 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4377                                   struct audit_context *actx)
4378 {
4379         struct smack_known *skp;
4380         char *rule = vrule;
4381
4382         if (unlikely(!rule)) {
4383                 WARN_ONCE(1, "Smack: missing rule\n");
4384                 return -ENOENT;
4385         }
4386
4387         if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4388                 return 0;
4389
4390         skp = smack_from_secid(secid);
4391
4392         /*
4393          * No need to do string comparisons. If a match occurs,
4394          * both pointers will point to the same smack_known
4395          * label.
4396          */
4397         if (op == Audit_equal)
4398                 return (rule == skp->smk_known);
4399         if (op == Audit_not_equal)
4400                 return (rule != skp->smk_known);
4401
4402         return 0;
4403 }
4404
4405 /*
4406  * There is no need for a smack_audit_rule_free hook.
4407  * No memory was allocated.
4408  */
4409
4410 #endif /* CONFIG_AUDIT */
4411
4412 /**
4413  * smack_ismaclabel - check if xattr @name references a smack MAC label
4414  * @name: Full xattr name to check.
4415  */
4416 static int smack_ismaclabel(const char *name)
4417 {
4418         return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4419 }
4420
4421
4422 /**
4423  * smack_secid_to_secctx - return the smack label for a secid
4424  * @secid: incoming integer
4425  * @secdata: destination
4426  * @seclen: how long it is
4427  *
4428  * Exists for networking code.
4429  */
4430 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4431 {
4432         struct smack_known *skp = smack_from_secid(secid);
4433
4434         if (secdata)
4435                 *secdata = skp->smk_known;
4436         *seclen = strlen(skp->smk_known);
4437         return 0;
4438 }
4439
4440 /**
4441  * smack_secctx_to_secid - return the secid for a smack label
4442  * @secdata: smack label
4443  * @seclen: how long result is
4444  * @secid: outgoing integer
4445  *
4446  * Exists for audit and networking code.
4447  */
4448 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4449 {
4450         struct smack_known *skp = smk_find_entry(secdata);
4451
4452         if (skp)
4453                 *secid = skp->smk_secid;
4454         else
4455                 *secid = 0;
4456         return 0;
4457 }
4458
4459 /*
4460  * There used to be a smack_release_secctx hook
4461  * that did nothing back when hooks were in a vector.
4462  * Now that there's a list such a hook adds cost.
4463  */
4464
4465 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4466 {
4467         return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4468 }
4469
4470 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4471 {
4472         return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4473 }
4474
4475 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4476 {
4477         struct smack_known *skp = smk_of_inode(inode);
4478
4479         *ctx = skp->smk_known;
4480         *ctxlen = strlen(skp->smk_known);
4481         return 0;
4482 }
4483
4484 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4485 {
4486
4487         struct task_smack *tsp;
4488         struct smack_known *skp;
4489         struct inode_smack *isp;
4490         struct cred *new_creds = *new;
4491
4492         if (new_creds == NULL) {
4493                 new_creds = prepare_creds();
4494                 if (new_creds == NULL)
4495                         return -ENOMEM;
4496         }
4497
4498         tsp = smack_cred(new_creds);
4499
4500         /*
4501          * Get label from overlay inode and set it in create_sid
4502          */
4503         isp = d_inode(dentry->d_parent)->i_security;
4504         skp = isp->smk_inode;
4505         tsp->smk_task = skp;
4506         *new = new_creds;
4507         return 0;
4508 }
4509
4510 static int smack_inode_copy_up_xattr(const char *name)
4511 {
4512         /*
4513          * Return 1 if this is the smack access Smack attribute.
4514          */
4515         if (strcmp(name, XATTR_NAME_SMACK) == 0)
4516                 return 1;
4517
4518         return -EOPNOTSUPP;
4519 }
4520
4521 static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4522                                         struct qstr *name,
4523                                         const struct cred *old,
4524                                         struct cred *new)
4525 {
4526         struct task_smack *otsp = smack_cred(old);
4527         struct task_smack *ntsp = smack_cred(new);
4528         struct inode_smack *isp;
4529         int may;
4530
4531         /*
4532          * Use the process credential unless all of
4533          * the transmuting criteria are met
4534          */
4535         ntsp->smk_task = otsp->smk_task;
4536
4537         /*
4538          * the attribute of the containing directory
4539          */
4540         isp = d_inode(dentry->d_parent)->i_security;
4541
4542         if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4543                 rcu_read_lock();
4544                 may = smk_access_entry(otsp->smk_task->smk_known,
4545                                        isp->smk_inode->smk_known,
4546                                        &otsp->smk_task->smk_rules);
4547                 rcu_read_unlock();
4548
4549                 /*
4550                  * If the directory is transmuting and the rule
4551                  * providing access is transmuting use the containing
4552                  * directory label instead of the process label.
4553                  */
4554                 if (may > 0 && (may & MAY_TRANSMUTE))
4555                         ntsp->smk_task = isp->smk_inode;
4556         }
4557         return 0;
4558 }
4559
4560 struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4561         .lbs_cred = sizeof(struct task_smack),
4562 };
4563
4564 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4565         LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4566         LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4567         LSM_HOOK_INIT(syslog, smack_syslog),
4568
4569         LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4570         LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4571         LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
4572         LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
4573         LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4574         LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4575
4576         LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4577
4578         LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4579         LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4580         LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4581         LSM_HOOK_INIT(inode_link, smack_inode_link),
4582         LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4583         LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4584         LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4585         LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4586         LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4587         LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4588         LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4589         LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4590         LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4591         LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4592         LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4593         LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4594         LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4595         LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
4596
4597         LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4598         LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4599         LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4600         LSM_HOOK_INIT(file_lock, smack_file_lock),
4601         LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4602         LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4603         LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4604         LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4605         LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4606         LSM_HOOK_INIT(file_receive, smack_file_receive),
4607
4608         LSM_HOOK_INIT(file_open, smack_file_open),
4609
4610         LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4611         LSM_HOOK_INIT(cred_free, smack_cred_free),
4612         LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4613         LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4614         LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
4615         LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4616         LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4617         LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4618         LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4619         LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4620         LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4621         LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4622         LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4623         LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4624         LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4625         LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4626         LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4627         LSM_HOOK_INIT(task_kill, smack_task_kill),
4628         LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
4629
4630         LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4631         LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
4632
4633         LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4634         LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
4635
4636         LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4637         LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
4638         LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4639         LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4640         LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4641         LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
4642
4643         LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4644         LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
4645         LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4646         LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4647         LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
4648
4649         LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4650         LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
4651         LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4652         LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4653         LSM_HOOK_INIT(sem_semop, smack_sem_semop),
4654
4655         LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
4656
4657         LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4658         LSM_HOOK_INIT(setprocattr, smack_setprocattr),
4659
4660         LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4661         LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
4662
4663         LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
4664         LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
4665 #ifdef SMACK_IPV6_PORT_LABELING
4666         LSM_HOOK_INIT(socket_bind, smack_socket_bind),
4667 #endif
4668         LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4669         LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4670         LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4671         LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4672         LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4673         LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4674         LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4675         LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4676         LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4677         LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4678
4679  /* key management security hooks */
4680 #ifdef CONFIG_KEYS
4681         LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4682         LSM_HOOK_INIT(key_free, smack_key_free),
4683         LSM_HOOK_INIT(key_permission, smack_key_permission),
4684         LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4685 #endif /* CONFIG_KEYS */
4686
4687  /* Audit hooks */
4688 #ifdef CONFIG_AUDIT
4689         LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4690         LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4691         LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4692 #endif /* CONFIG_AUDIT */
4693
4694         LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4695         LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4696         LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4697         LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4698         LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4699         LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4700         LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4701         LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4702         LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4703 };
4704
4705
4706 static __init void init_smack_known_list(void)
4707 {
4708         /*
4709          * Initialize rule list locks
4710          */
4711         mutex_init(&smack_known_huh.smk_rules_lock);
4712         mutex_init(&smack_known_hat.smk_rules_lock);
4713         mutex_init(&smack_known_floor.smk_rules_lock);
4714         mutex_init(&smack_known_star.smk_rules_lock);
4715         mutex_init(&smack_known_web.smk_rules_lock);
4716         /*
4717          * Initialize rule lists
4718          */
4719         INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4720         INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4721         INIT_LIST_HEAD(&smack_known_star.smk_rules);
4722         INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4723         INIT_LIST_HEAD(&smack_known_web.smk_rules);
4724         /*
4725          * Create the known labels list
4726          */
4727         smk_insert_entry(&smack_known_huh);
4728         smk_insert_entry(&smack_known_hat);
4729         smk_insert_entry(&smack_known_star);
4730         smk_insert_entry(&smack_known_floor);
4731         smk_insert_entry(&smack_known_web);
4732 }
4733
4734 /**
4735  * smack_init - initialize the smack system
4736  *
4737  * Returns 0
4738  */
4739 static __init int smack_init(void)
4740 {
4741         struct cred *cred = (struct cred *) current->cred;
4742         struct task_smack *tsp;
4743
4744         smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4745         if (!smack_inode_cache)
4746                 return -ENOMEM;
4747
4748         lsm_early_cred(cred);
4749
4750         /*
4751          * Set the security state for the initial task.
4752          */
4753         tsp = smack_cred(cred);
4754         init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4755
4756         /*
4757          * Register with LSM
4758          */
4759         security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4760         smack_enabled = 1;
4761
4762         pr_info("Smack:  Initializing.\n");
4763 #ifdef CONFIG_SECURITY_SMACK_NETFILTER
4764         pr_info("Smack:  Netfilter enabled.\n");
4765 #endif
4766 #ifdef SMACK_IPV6_PORT_LABELING
4767         pr_info("Smack:  IPv6 port labeling enabled.\n");
4768 #endif
4769 #ifdef SMACK_IPV6_SECMARK_LABELING
4770         pr_info("Smack:  IPv6 Netfilter enabled.\n");
4771 #endif
4772
4773         /* initialize the smack_known_list */
4774         init_smack_known_list();
4775
4776         return 0;
4777 }
4778
4779 /*
4780  * Smack requires early initialization in order to label
4781  * all processes and objects when they are created.
4782  */
4783 DEFINE_LSM(smack) = {
4784         .name = "smack",
4785         .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4786         .blobs = &smack_blob_sizes,
4787         .init = smack_init,
4788 };