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