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