a2e2cdfab4ef2004c4e2820ba79f4a438abc8194
[linux-flexiantxendom0-natty.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 #include <linux/lsm_audit.h>
24
25 /*
26  * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
27  * bigger than can be used, and 24 is the next lower multiple
28  * of 8, and there are too many issues if there isn't space set
29  * aside for the terminating null byte.
30  */
31 #define SMK_MAXLEN      23
32 #define SMK_LABELLEN    (SMK_MAXLEN+1)
33
34 struct superblock_smack {
35         char            *smk_root;
36         char            *smk_floor;
37         char            *smk_hat;
38         char            *smk_default;
39         int             smk_initialized;
40         spinlock_t      smk_sblock;     /* for initialization */
41 };
42
43 struct socket_smack {
44         char            *smk_out;                       /* outbound label */
45         char            *smk_in;                        /* inbound label */
46         char            smk_packet[SMK_LABELLEN];       /* TCP peer label */
47 };
48
49 /*
50  * Inode smack data
51  */
52 struct inode_smack {
53         char            *smk_inode;     /* label of the fso */
54         char            *smk_task;      /* label of the task */
55         struct mutex    smk_lock;       /* initialization lock */
56         int             smk_flags;      /* smack inode flags */
57 };
58
59 struct task_smack {
60         char            *smk_task;      /* label used for access control */
61         char            *smk_forked;    /* label when forked */
62 };
63
64 #define SMK_INODE_INSTANT       0x01    /* inode is instantiated */
65
66 /*
67  * A label access rule.
68  */
69 struct smack_rule {
70         struct list_head        list;
71         char                    *smk_subject;
72         char                    *smk_object;
73         int                     smk_access;
74 };
75
76 /*
77  * An entry in the table mapping smack values to
78  * CIPSO level/category-set values.
79  */
80 struct smack_cipso {
81         int     smk_level;
82         char    smk_catset[SMK_LABELLEN];
83 };
84
85 /*
86  * An entry in the table identifying hosts.
87  */
88 struct smk_netlbladdr {
89         struct list_head        list;
90         struct sockaddr_in      smk_host;       /* network address */
91         struct in_addr          smk_mask;       /* network mask */
92         char                    *smk_label;     /* label */
93 };
94
95 /*
96  * This is the repository for labels seen so that it is
97  * not necessary to keep allocating tiny chuncks of memory
98  * and so that they can be shared.
99  *
100  * Labels are never modified in place. Anytime a label
101  * is imported (e.g. xattrset on a file) the list is checked
102  * for it and it is added if it doesn't exist. The address
103  * is passed out in either case. Entries are added, but
104  * never deleted.
105  *
106  * Since labels are hanging around anyway it doesn't
107  * hurt to maintain a secid for those awkward situations
108  * where kernel components that ought to use LSM independent
109  * interfaces don't. The secid should go away when all of
110  * these components have been repaired.
111  *
112  * If there is a cipso value associated with the label it
113  * gets stored here, too. This will most likely be rare as
114  * the cipso direct mapping in used internally.
115  */
116 struct smack_known {
117         struct list_head        list;
118         char                    smk_known[SMK_LABELLEN];
119         u32                     smk_secid;
120         struct smack_cipso      *smk_cipso;
121         spinlock_t              smk_cipsolock; /* for changing cipso map */
122 };
123
124 /*
125  * Mount options
126  */
127 #define SMK_FSDEFAULT   "smackfsdef="
128 #define SMK_FSFLOOR     "smackfsfloor="
129 #define SMK_FSHAT       "smackfshat="
130 #define SMK_FSROOT      "smackfsroot="
131
132 #define SMACK_CIPSO_OPTION      "-CIPSO"
133
134 /*
135  * How communications on this socket are treated.
136  * Usually it's determined by the underlying netlabel code
137  * but there are certain cases, including single label hosts
138  * and potentially single label interfaces for which the
139  * treatment can not be known in advance.
140  *
141  * The possibility of additional labeling schemes being
142  * introduced in the future exists as well.
143  */
144 #define SMACK_UNLABELED_SOCKET  0
145 #define SMACK_CIPSO_SOCKET      1
146
147 /*
148  * smackfs magic number
149  * smackfs macic number
150  */
151 #define SMACK_MAGIC     0x43415d53 /* "SMAC" */
152
153 /*
154  * A limit on the number of entries in the lists
155  * makes some of the list administration easier.
156  */
157 #define SMACK_LIST_MAX  10000
158
159 /*
160  * CIPSO defaults.
161  */
162 #define SMACK_CIPSO_DOI_DEFAULT         3       /* Historical */
163 #define SMACK_CIPSO_DOI_INVALID         -1      /* Not a DOI */
164 #define SMACK_CIPSO_DIRECT_DEFAULT      250     /* Arbitrary */
165 #define SMACK_CIPSO_MAXCATVAL           63      /* Bigger gets harder */
166 #define SMACK_CIPSO_MAXLEVEL            255     /* CIPSO 2.2 standard */
167 #define SMACK_CIPSO_MAXCATNUM           239     /* CIPSO 2.2 standard */
168
169 /*
170  * Just to make the common cases easier to deal with
171  */
172 #define MAY_ANY         (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
173 #define MAY_ANYREAD     (MAY_READ | MAY_EXEC)
174 #define MAY_ANYWRITE    (MAY_WRITE | MAY_APPEND)
175 #define MAY_READWRITE   (MAY_READ | MAY_WRITE)
176 #define MAY_NOT         0
177
178 /*
179  * Number of access types used by Smack (rwxa)
180  */
181 #define SMK_NUM_ACCESS_TYPE 4
182
183 /*
184  * Smack audit data; is empty if CONFIG_AUDIT not set
185  * to save some stack
186  */
187 struct smk_audit_info {
188 #ifdef CONFIG_AUDIT
189         struct common_audit_data a;
190 #endif
191 };
192 /*
193  * These functions are in smack_lsm.c
194  */
195 struct inode_smack *new_inode_smack(char *);
196
197 /*
198  * These functions are in smack_access.c
199  */
200 int smk_access(char *, char *, int, struct smk_audit_info *);
201 int smk_curacc(char *, u32, struct smk_audit_info *);
202 int smack_to_cipso(const char *, struct smack_cipso *);
203 void smack_from_cipso(u32, char *, char *);
204 char *smack_from_secid(const u32);
205 char *smk_import(const char *, int);
206 struct smack_known *smk_import_entry(const char *, int);
207 u32 smack_to_secid(const char *);
208
209 /*
210  * Shared data.
211  */
212 extern int smack_cipso_direct;
213 extern char *smack_net_ambient;
214 extern char *smack_onlycap;
215 extern const char *smack_cipso_option;
216
217 extern struct smack_known smack_known_floor;
218 extern struct smack_known smack_known_hat;
219 extern struct smack_known smack_known_huh;
220 extern struct smack_known smack_known_invalid;
221 extern struct smack_known smack_known_star;
222 extern struct smack_known smack_known_web;
223
224 extern struct list_head smack_known_list;
225 extern struct list_head smack_rule_list;
226 extern struct list_head smk_netlbladdr_list;
227
228 extern struct security_operations smack_ops;
229
230 /*
231  * Stricly for CIPSO level manipulation.
232  * Set the category bit number in a smack label sized buffer.
233  */
234 static inline void smack_catset_bit(int cat, char *catsetp)
235 {
236         if (cat > SMK_LABELLEN * 8)
237                 return;
238
239         catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
240 }
241
242 /*
243  * Present a pointer to the smack label in an inode blob.
244  */
245 static inline char *smk_of_inode(const struct inode *isp)
246 {
247         struct inode_smack *sip = isp->i_security;
248         return sip->smk_inode;
249 }
250
251 /*
252  * Present a pointer to the smack label in an task blob.
253  */
254 static inline char *smk_of_task(const struct task_smack *tsp)
255 {
256         return tsp->smk_task;
257 }
258
259 /*
260  * Present a pointer to the forked smack label in an task blob.
261  */
262 static inline char *smk_of_forked(const struct task_smack *tsp)
263 {
264         return tsp->smk_forked;
265 }
266
267 /*
268  * Present a pointer to the smack label in the curren task blob.
269  */
270 static inline char *smk_of_current(void)
271 {
272         return smk_of_task(current_security());
273 }
274
275 /*
276  * logging functions
277  */
278 #define SMACK_AUDIT_DENIED 0x1
279 #define SMACK_AUDIT_ACCEPT 0x2
280 extern int log_policy;
281
282 void smack_log(char *subject_label, char *object_label,
283                 int request,
284                 int result, struct smk_audit_info *auditdata);
285
286 #ifdef CONFIG_AUDIT
287
288 /*
289  * some inline functions to set up audit data
290  * they do nothing if CONFIG_AUDIT is not set
291  *
292  */
293 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
294                                char type)
295 {
296         memset(a, 0, sizeof(*a));
297         a->a.type = type;
298         a->a.smack_audit_data.function = func;
299 }
300
301 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
302                                          struct task_struct *t)
303 {
304         a->a.u.tsk = t;
305 }
306 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
307                                                     struct dentry *d)
308 {
309         a->a.u.fs.path.dentry = d;
310 }
311 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
312                                                  struct vfsmount *m)
313 {
314         a->a.u.fs.path.mnt = m;
315 }
316 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
317                                               struct inode *i)
318 {
319         a->a.u.fs.inode = i;
320 }
321 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
322                                              struct path p)
323 {
324         a->a.u.fs.path = p;
325 }
326 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
327                                             struct sock *sk)
328 {
329         a->a.u.net.sk = sk;
330 }
331
332 #else /* no AUDIT */
333
334 static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
335                                char type)
336 {
337 }
338 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
339                                          struct task_struct *t)
340 {
341 }
342 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
343                                                     struct dentry *d)
344 {
345 }
346 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a,
347                                                  struct vfsmount *m)
348 {
349 }
350 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
351                                               struct inode *i)
352 {
353 }
354 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
355                                              struct path p)
356 {
357 }
358 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
359                                             struct sock *sk)
360 {
361 }
362 #endif
363
364 #endif  /* _SECURITY_SMACK_H */