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