ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / security / selinux / ss / policydb.h
1 /*
2  * A policy database (policydb) specifies the
3  * configuration data for the security policy.
4  *
5  * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
6  */
7
8 /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
9  *
10  *      Added conditional policy language extensions
11  *
12  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
13  *      This program is free software; you can redistribute it and/or modify
14  *      it under the terms of the GNU General Public License as published by
15  *      the Free Software Foundation, version 2.
16  */
17
18 #ifndef _SS_POLICYDB_H_
19 #define _SS_POLICYDB_H_
20
21 #include "symtab.h"
22 #include "avtab.h"
23 #include "sidtab.h"
24 #include "context.h"
25 #include "constraint.h"
26
27 /*
28  * A datum type is defined for each kind of symbol
29  * in the configuration data:  individual permissions,
30  * common prefixes for access vectors, classes,
31  * users, roles, types, sensitivities, categories, etc.
32  */
33
34 /* Permission attributes */
35 struct perm_datum {
36         u32 value;              /* permission bit + 1 */
37 #ifdef CONFIG_SECURITY_SELINUX_MLS
38 #define MLS_BASE_READ    1      /* MLS base permission `read' */
39 #define MLS_BASE_WRITE   2      /* MLS base permission `write' */
40 #define MLS_BASE_READBY  4      /* MLS base permission `readby' */
41 #define MLS_BASE_WRITEBY 8      /* MLS base permission `writeby' */
42         u32 base_perms;         /* MLS base permission mask */
43 #endif
44 };
45
46 /* Attributes of a common prefix for access vectors */
47 struct common_datum {
48         u32 value;                      /* internal common value */
49         struct symtab permissions;      /* common permissions */
50 };
51
52 /* Class attributes */
53 struct class_datum {
54         u32 value;                      /* class value */
55         char *comkey;                   /* common name */
56         struct common_datum *comdatum;  /* common datum */
57         struct symtab permissions;      /* class-specific permission symbol table */
58         struct constraint_node *constraints;    /* constraints on class permissions */
59 #ifdef CONFIG_SECURITY_SELINUX_MLS
60         struct mls_perms mlsperms;      /* MLS base permission masks */
61 #endif
62 };
63
64 /* Role attributes */
65 struct role_datum {
66         u32 value;                      /* internal role value */
67         struct ebitmap dominates;       /* set of roles dominated by this role */
68         struct ebitmap types;           /* set of authorized types for role */
69 };
70
71 struct role_trans {
72         u32 role;               /* current role */
73         u32 type;               /* program executable type */
74         u32 new_role;           /* new role */
75         struct role_trans *next;
76 };
77
78 struct role_allow {
79         u32 role;               /* current role */
80         u32 new_role;           /* new role */
81         struct role_allow *next;
82 };
83
84 /* Type attributes */
85 struct type_datum {
86         u32 value;              /* internal type value */
87         unsigned char primary;  /* primary name? */
88 };
89
90 /* User attributes */
91 struct user_datum {
92         u32 value;                      /* internal user value */
93         struct ebitmap roles;           /* set of authorized roles for user */
94 #ifdef CONFIG_SECURITY_SELINUX_MLS
95         struct mls_range_list *ranges;  /* list of authorized MLS ranges for user */
96 #endif
97 };
98
99
100 #ifdef CONFIG_SECURITY_SELINUX_MLS
101 /* Sensitivity attributes */
102 struct level_datum {
103         struct mls_level *level;        /* sensitivity and associated categories */
104         unsigned char isalias;  /* is this sensitivity an alias for another? */
105 };
106
107 /* Category attributes */
108 struct cat_datum {
109         u32 value;              /* internal category bit + 1 */
110         unsigned char isalias;  /* is this category an alias for another? */
111 };
112 #endif
113
114 /* Boolean data type */
115 struct cond_bool_datum {
116         __u32 value;            /* internal type value */
117         int state;
118 };
119
120 struct cond_node;
121
122 /*
123  * The configuration data includes security contexts for
124  * initial SIDs, unlabeled file systems, TCP and UDP port numbers,
125  * network interfaces, and nodes.  This structure stores the
126  * relevant data for one such entry.  Entries of the same kind
127  * (e.g. all initial SIDs) are linked together into a list.
128  */
129 struct ocontext {
130         union {
131                 char *name;     /* name of initial SID, fs, netif, fstype, path */
132                 struct {
133                         u8 protocol;
134                         u16 low_port;
135                         u16 high_port;
136                 } port;         /* TCP or UDP port information */
137                 struct {
138                         u32 addr;
139                         u32 mask;
140                 } node;         /* node information */
141                 struct {
142                         u32 addr[4];
143                         u32 mask[4];
144                 } node6;        /* IPv6 node information */
145         } u;
146         union {
147                 u32 sclass;  /* security class for genfs */
148                 u32 behavior;  /* labeling behavior for fs_use */
149         } v;
150         struct context context[2];      /* security context(s) */
151         u32 sid[2];     /* SID(s) */
152         struct ocontext *next;
153 };
154
155 struct genfs {
156         char *fstype;
157         struct ocontext *head;
158         struct genfs *next;
159 };
160
161 /* symbol table array indices */
162 #define SYM_COMMONS 0
163 #define SYM_CLASSES 1
164 #define SYM_ROLES   2
165 #define SYM_TYPES   3
166 #define SYM_USERS   4
167 #ifdef CONFIG_SECURITY_SELINUX_MLS
168 #define SYM_LEVELS  5
169 #define SYM_CATS    6
170 #define SYM_BOOLS   7
171 #define SYM_NUM     8
172 #else
173 #define SYM_BOOLS   5
174 #define SYM_NUM     6
175 #endif
176
177 /* object context array indices */
178 #define OCON_ISID  0    /* initial SIDs */
179 #define OCON_FS    1    /* unlabeled file systems */
180 #define OCON_PORT  2    /* TCP and UDP port numbers */
181 #define OCON_NETIF 3    /* network interfaces */
182 #define OCON_NODE  4    /* nodes */
183 #define OCON_FSUSE 5    /* fs_use */
184 #define OCON_NODE6 6    /* IPv6 nodes */
185 #define OCON_NUM   7
186
187 /* The policy database */
188 struct policydb {
189         /* symbol tables */
190         struct symtab symtab[SYM_NUM];
191 #define p_commons symtab[SYM_COMMONS]
192 #define p_classes symtab[SYM_CLASSES]
193 #define p_roles symtab[SYM_ROLES]
194 #define p_types symtab[SYM_TYPES]
195 #define p_users symtab[SYM_USERS]
196 #define p_levels symtab[SYM_LEVELS]
197 #define p_cats symtab[SYM_CATS]
198 #define p_bools symtab[SYM_BOOLS]
199
200         /* symbol names indexed by (value - 1) */
201         char **sym_val_to_name[SYM_NUM];
202 #define p_common_val_to_name sym_val_to_name[SYM_COMMONS]
203 #define p_class_val_to_name sym_val_to_name[SYM_CLASSES]
204 #define p_role_val_to_name sym_val_to_name[SYM_ROLES]
205 #define p_type_val_to_name sym_val_to_name[SYM_TYPES]
206 #define p_user_val_to_name sym_val_to_name[SYM_USERS]
207 #define p_sens_val_to_name sym_val_to_name[SYM_LEVELS]
208 #define p_cat_val_to_name sym_val_to_name[SYM_CATS]
209 #define p_bool_val_to_name sym_val_to_name[SYM_BOOLS]
210
211         /* class, role, and user attributes indexed by (value - 1) */
212         struct class_datum **class_val_to_struct;
213         struct role_datum **role_val_to_struct;
214         struct user_datum **user_val_to_struct;
215
216         /* type enforcement access vectors and transitions */
217         struct avtab te_avtab;
218
219         /* role transitions */
220         struct role_trans *role_tr;
221
222         /* bools indexed by (value - 1) */
223         struct cond_bool_datum **bool_val_to_struct;
224         /* type enforcement conditional access vectors and transitions */
225         struct avtab te_cond_avtab;
226         /* linked list indexing te_cond_avtab by conditional */
227         struct cond_node* cond_list;
228
229         /* role allows */
230         struct role_allow *role_allow;
231
232         /* security contexts of initial SIDs, unlabeled file systems,
233            TCP or UDP port numbers, network interfaces and nodes */
234         struct ocontext *ocontexts[OCON_NUM];
235
236         /* security contexts for files in filesystems that cannot support
237            a persistent label mapping or use another
238            fixed labeling behavior. */
239         struct genfs *genfs;
240
241 #ifdef CONFIG_SECURITY_SELINUX_MLS
242         /* number of legitimate MLS levels */
243         u32 nlevels;
244
245         struct ebitmap trustedreaders;
246         struct ebitmap trustedwriters;
247         struct ebitmap trustedobjects;
248 #endif
249 };
250
251 extern int policydb_init(struct policydb *p);
252 extern int policydb_index_classes(struct policydb *p);
253 extern int policydb_index_others(struct policydb *p);
254 extern int constraint_expr_destroy(struct constraint_expr *expr);
255 extern void policydb_destroy(struct policydb *p);
256 extern int policydb_load_isids(struct policydb *p, struct sidtab *s);
257 extern int policydb_context_isvalid(struct policydb *p, struct context *c);
258 extern int policydb_read(struct policydb *p, void *fp);
259
260 #define PERM_SYMTAB_SIZE 32
261
262 #define POLICYDB_CONFIG_MLS    1
263
264 #define OBJECT_R "object_r"
265 #define OBJECT_R_VAL 1
266
267 #define POLICYDB_MAGIC SELINUX_MAGIC
268 #define POLICYDB_STRING "SE Linux"
269
270 struct policy_file {
271         char *data;
272         size_t len;
273 };
274
275 static inline void *next_entry(struct policy_file *fp, size_t bytes)
276 {
277         void *buf;
278
279         if (bytes > fp->len)
280                 return NULL;
281
282         buf = fp->data;
283         fp->data += bytes;
284         fp->len -= bytes;
285         return buf;
286 }
287
288 #endif  /* _SS_POLICYDB_H_ */
289