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