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