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