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