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