OSDN Git Service

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