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