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