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