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