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