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