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