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