netlabel: Cleanup the Smack/NetLabel code to fix incoming TCP connections
[pandora-kernel.git] / security / smack / smack.h
1 /*
2  * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3  *
4  *      This program is free software; you can redistribute it and/or modify
5  *      it under the terms of the GNU General Public License as published by
6  *      the Free Software Foundation, version 2.
7  *
8  * Author:
9  *      Casey Schaufler <casey@schaufler-ca.com>
10  *
11  */
12
13 #ifndef _SECURITY_SMACK_H
14 #define _SECURITY_SMACK_H
15
16 #include <linux/capability.h>
17 #include <linux/spinlock.h>
18 #include <linux/security.h>
19 #include <linux/in.h>
20 #include <net/netlabel.h>
21 #include <linux/list.h>
22 #include <linux/rculist.h>
23
24 /*
25  * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
26  * bigger than can be used, and 24 is the next lower multiple
27  * of 8, and there are too many issues if there isn't space set
28  * aside for the terminating null byte.
29  */
30 #define SMK_MAXLEN      23
31 #define SMK_LABELLEN    (SMK_MAXLEN+1)
32
33 struct superblock_smack {
34         char            *smk_root;
35         char            *smk_floor;
36         char            *smk_hat;
37         char            *smk_default;
38         int             smk_initialized;
39         spinlock_t      smk_sblock;     /* for initialization */
40 };
41
42 struct socket_smack {
43         char            *smk_out;                       /* outbound label */
44         char            *smk_in;                        /* inbound label */
45         char            smk_packet[SMK_LABELLEN];       /* TCP peer label */
46 };
47
48 /*
49  * Inode smack data
50  */
51 struct inode_smack {
52         char            *smk_inode;     /* label of the fso */
53         struct mutex    smk_lock;       /* initialization lock */
54         int             smk_flags;      /* smack inode flags */
55 };
56
57 #define SMK_INODE_INSTANT       0x01    /* inode is instantiated */
58
59 /*
60  * A label access rule.
61  */
62 struct smack_rule {
63         struct list_head        list;
64         char                    *smk_subject;
65         char                    *smk_object;
66         int                     smk_access;
67 };
68
69 /*
70  * An entry in the table mapping smack values to
71  * CIPSO level/category-set values.
72  */
73 struct smack_cipso {
74         int     smk_level;
75         char    smk_catset[SMK_LABELLEN];
76 };
77
78 /*
79  * An entry in the table identifying hosts.
80  */
81 struct smk_netlbladdr {
82         struct list_head        list;
83         struct sockaddr_in      smk_host;       /* network address */
84         struct in_addr          smk_mask;       /* network mask */
85         char                    *smk_label;     /* label */
86 };
87
88 /*
89  * This is the repository for labels seen so that it is
90  * not necessary to keep allocating tiny chuncks of memory
91  * and so that they can be shared.
92  *
93  * Labels are never modified in place. Anytime a label
94  * is imported (e.g. xattrset on a file) the list is checked
95  * for it and it is added if it doesn't exist. The address
96  * is passed out in either case. Entries are added, but
97  * never deleted.
98  *
99  * Since labels are hanging around anyway it doesn't
100  * hurt to maintain a secid for those awkward situations
101  * where kernel components that ought to use LSM independent
102  * interfaces don't. The secid should go away when all of
103  * these components have been repaired.
104  *
105  * If there is a cipso value associated with the label it
106  * gets stored here, too. This will most likely be rare as
107  * the cipso direct mapping in used internally.
108  */
109 struct smack_known {
110         struct list_head        list;
111         char                    smk_known[SMK_LABELLEN];
112         u32                     smk_secid;
113         struct smack_cipso      *smk_cipso;
114         spinlock_t              smk_cipsolock; /* for changing cipso map */
115 };
116
117 /*
118  * Mount options
119  */
120 #define SMK_FSDEFAULT   "smackfsdef="
121 #define SMK_FSFLOOR     "smackfsfloor="
122 #define SMK_FSHAT       "smackfshat="
123 #define SMK_FSROOT      "smackfsroot="
124
125 /*
126  * xattr names
127  */
128 #define XATTR_SMACK_SUFFIX      "SMACK64"
129 #define XATTR_SMACK_IPIN        "SMACK64IPIN"
130 #define XATTR_SMACK_IPOUT       "SMACK64IPOUT"
131 #define XATTR_NAME_SMACK        XATTR_SECURITY_PREFIX XATTR_SMACK_SUFFIX
132 #define XATTR_NAME_SMACKIPIN    XATTR_SECURITY_PREFIX XATTR_SMACK_IPIN
133 #define XATTR_NAME_SMACKIPOUT   XATTR_SECURITY_PREFIX XATTR_SMACK_IPOUT
134
135 /*
136  * How communications on this socket are treated.
137  * Usually it's determined by the underlying netlabel code
138  * but there are certain cases, including single label hosts
139  * and potentially single label interfaces for which the
140  * treatment can not be known in advance.
141  *
142  * The possibility of additional labeling schemes being
143  * introduced in the future exists as well.
144  */
145 #define SMACK_UNLABELED_SOCKET  0
146 #define SMACK_CIPSO_SOCKET      1
147
148 /*
149  * smackfs magic number
150  * smackfs macic number
151  */
152 #define SMACK_MAGIC     0x43415d53 /* "SMAC" */
153
154 /*
155  * A limit on the number of entries in the lists
156  * makes some of the list administration easier.
157  */
158 #define SMACK_LIST_MAX  10000
159
160 /*
161  * CIPSO defaults.
162  */
163 #define SMACK_CIPSO_DOI_DEFAULT         3       /* Historical */
164 #define SMACK_CIPSO_DOI_INVALID         -1      /* Not a DOI */
165 #define SMACK_CIPSO_DIRECT_DEFAULT      250     /* Arbitrary */
166 #define SMACK_CIPSO_MAXCATVAL           63      /* Bigger gets harder */
167 #define SMACK_CIPSO_MAXLEVEL            255     /* CIPSO 2.2 standard */
168 #define SMACK_CIPSO_MAXCATNUM           239     /* CIPSO 2.2 standard */
169
170 /*
171  * Just to make the common cases easier to deal with
172  */
173 #define MAY_ANY         (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
174 #define MAY_ANYREAD     (MAY_READ | MAY_EXEC)
175 #define MAY_ANYWRITE    (MAY_WRITE | MAY_APPEND)
176 #define MAY_READWRITE   (MAY_READ | MAY_WRITE)
177 #define MAY_NOT         0
178
179 /*
180  * These functions are in smack_lsm.c
181  */
182 struct inode_smack *new_inode_smack(char *);
183
184 /*
185  * These functions are in smack_access.c
186  */
187 int smk_access(char *, char *, int);
188 int smk_curacc(char *, u32);
189 int smack_to_cipso(const char *, struct smack_cipso *);
190 void smack_from_cipso(u32, char *, char *);
191 char *smack_from_secid(const u32);
192 char *smk_import(const char *, int);
193 struct smack_known *smk_import_entry(const char *, int);
194 u32 smack_to_secid(const char *);
195
196 /*
197  * Shared data.
198  */
199 extern int smack_cipso_direct;
200 extern char *smack_net_ambient;
201 extern char *smack_onlycap;
202
203 extern struct smack_known smack_known_floor;
204 extern struct smack_known smack_known_hat;
205 extern struct smack_known smack_known_huh;
206 extern struct smack_known smack_known_invalid;
207 extern struct smack_known smack_known_star;
208 extern struct smack_known smack_known_web;
209
210 extern struct list_head smack_known_list;
211 extern struct list_head smack_rule_list;
212 extern struct list_head smk_netlbladdr_list;
213
214 extern struct security_operations smack_ops;
215
216 /*
217  * Stricly for CIPSO level manipulation.
218  * Set the category bit number in a smack label sized buffer.
219  */
220 static inline void smack_catset_bit(int cat, char *catsetp)
221 {
222         if (cat > SMK_LABELLEN * 8)
223                 return;
224
225         catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
226 }
227
228 /*
229  * Present a pointer to the smack label in an inode blob.
230  */
231 static inline char *smk_of_inode(const struct inode *isp)
232 {
233         struct inode_smack *sip = isp->i_security;
234         return sip->smk_inode;
235 }
236
237 #endif  /* _SECURITY_SMACK_H */