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