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