allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / security / selinux / ss / ebitmap.c
blobce492a6b38ed64110e2f8b0db68510df12281782
1 /*
2 * Implementation of the extensible bitmap type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
5 */
6 /*
7 * Updated: Hewlett-Packard <paul.moore@hp.com>
9 * Added support to import/export the NetLabel category bitmap
11 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <net/netlabel.h>
18 #include "ebitmap.h"
19 #include "policydb.h"
21 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
23 struct ebitmap_node *n1, *n2;
25 if (e1->highbit != e2->highbit)
26 return 0;
28 n1 = e1->node;
29 n2 = e2->node;
30 while (n1 && n2 &&
31 (n1->startbit == n2->startbit) &&
32 (n1->map == n2->map)) {
33 n1 = n1->next;
34 n2 = n2->next;
37 if (n1 || n2)
38 return 0;
40 return 1;
43 int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
45 struct ebitmap_node *n, *new, *prev;
47 ebitmap_init(dst);
48 n = src->node;
49 prev = NULL;
50 while (n) {
51 new = kzalloc(sizeof(*new), GFP_ATOMIC);
52 if (!new) {
53 ebitmap_destroy(dst);
54 return -ENOMEM;
56 new->startbit = n->startbit;
57 new->map = n->map;
58 new->next = NULL;
59 if (prev)
60 prev->next = new;
61 else
62 dst->node = new;
63 prev = new;
64 n = n->next;
67 dst->highbit = src->highbit;
68 return 0;
71 #ifdef CONFIG_NETLABEL
72 /**
73 * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap
74 * @ebmap: the ebitmap to export
75 * @catmap: the NetLabel category bitmap
77 * Description:
78 * Export a SELinux extensibile bitmap into a NetLabel category bitmap.
79 * Returns zero on success, negative values on error.
82 int ebitmap_netlbl_export(struct ebitmap *ebmap,
83 struct netlbl_lsm_secattr_catmap **catmap)
85 struct ebitmap_node *e_iter = ebmap->node;
86 struct netlbl_lsm_secattr_catmap *c_iter;
87 u32 cmap_idx;
89 /* This function is a much simpler because SELinux's MAPTYPE happens
90 * to be the same as NetLabel's NETLBL_CATMAP_MAPTYPE, if MAPTYPE is
91 * changed from a u64 this function will most likely need to be changed
92 * as well. It's not ideal but I think the tradeoff in terms of
93 * neatness and speed is worth it. */
95 if (e_iter == NULL) {
96 *catmap = NULL;
97 return 0;
100 c_iter = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
101 if (c_iter == NULL)
102 return -ENOMEM;
103 *catmap = c_iter;
104 c_iter->startbit = e_iter->startbit & ~(NETLBL_CATMAP_SIZE - 1);
106 while (e_iter != NULL) {
107 if (e_iter->startbit >=
108 (c_iter->startbit + NETLBL_CATMAP_SIZE)) {
109 c_iter->next = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
110 if (c_iter->next == NULL)
111 goto netlbl_export_failure;
112 c_iter = c_iter->next;
113 c_iter->startbit = e_iter->startbit &
114 ~(NETLBL_CATMAP_SIZE - 1);
116 cmap_idx = (e_iter->startbit - c_iter->startbit) /
117 NETLBL_CATMAP_MAPSIZE;
118 c_iter->bitmap[cmap_idx] = e_iter->map;
119 e_iter = e_iter->next;
122 return 0;
124 netlbl_export_failure:
125 netlbl_secattr_catmap_free(*catmap);
126 return -ENOMEM;
130 * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap
131 * @ebmap: the ebitmap to export
132 * @catmap: the NetLabel category bitmap
134 * Description:
135 * Import a NetLabel category bitmap into a SELinux extensibile bitmap.
136 * Returns zero on success, negative values on error.
139 int ebitmap_netlbl_import(struct ebitmap *ebmap,
140 struct netlbl_lsm_secattr_catmap *catmap)
142 struct ebitmap_node *e_iter = NULL;
143 struct ebitmap_node *emap_prev = NULL;
144 struct netlbl_lsm_secattr_catmap *c_iter = catmap;
145 u32 c_idx;
147 /* This function is a much simpler because SELinux's MAPTYPE happens
148 * to be the same as NetLabel's NETLBL_CATMAP_MAPTYPE, if MAPTYPE is
149 * changed from a u64 this function will most likely need to be changed
150 * as well. It's not ideal but I think the tradeoff in terms of
151 * neatness and speed is worth it. */
153 do {
154 for (c_idx = 0; c_idx < NETLBL_CATMAP_MAPCNT; c_idx++) {
155 if (c_iter->bitmap[c_idx] == 0)
156 continue;
158 e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC);
159 if (e_iter == NULL)
160 goto netlbl_import_failure;
161 if (emap_prev == NULL)
162 ebmap->node = e_iter;
163 else
164 emap_prev->next = e_iter;
165 emap_prev = e_iter;
167 e_iter->startbit = c_iter->startbit +
168 NETLBL_CATMAP_MAPSIZE * c_idx;
169 e_iter->map = c_iter->bitmap[c_idx];
171 c_iter = c_iter->next;
172 } while (c_iter != NULL);
173 if (e_iter != NULL)
174 ebmap->highbit = e_iter->startbit + MAPSIZE;
175 else
176 ebitmap_destroy(ebmap);
178 return 0;
180 netlbl_import_failure:
181 ebitmap_destroy(ebmap);
182 return -ENOMEM;
184 #endif /* CONFIG_NETLABEL */
186 int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2)
188 struct ebitmap_node *n1, *n2;
190 if (e1->highbit < e2->highbit)
191 return 0;
193 n1 = e1->node;
194 n2 = e2->node;
195 while (n1 && n2 && (n1->startbit <= n2->startbit)) {
196 if (n1->startbit < n2->startbit) {
197 n1 = n1->next;
198 continue;
200 if ((n1->map & n2->map) != n2->map)
201 return 0;
203 n1 = n1->next;
204 n2 = n2->next;
207 if (n2)
208 return 0;
210 return 1;
213 int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
215 struct ebitmap_node *n;
217 if (e->highbit < bit)
218 return 0;
220 n = e->node;
221 while (n && (n->startbit <= bit)) {
222 if ((n->startbit + MAPSIZE) > bit) {
223 if (n->map & (MAPBIT << (bit - n->startbit)))
224 return 1;
225 else
226 return 0;
228 n = n->next;
231 return 0;
234 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
236 struct ebitmap_node *n, *prev, *new;
238 prev = NULL;
239 n = e->node;
240 while (n && n->startbit <= bit) {
241 if ((n->startbit + MAPSIZE) > bit) {
242 if (value) {
243 n->map |= (MAPBIT << (bit - n->startbit));
244 } else {
245 n->map &= ~(MAPBIT << (bit - n->startbit));
246 if (!n->map) {
247 /* drop this node from the bitmap */
249 if (!n->next) {
251 * this was the highest map
252 * within the bitmap
254 if (prev)
255 e->highbit = prev->startbit + MAPSIZE;
256 else
257 e->highbit = 0;
259 if (prev)
260 prev->next = n->next;
261 else
262 e->node = n->next;
264 kfree(n);
267 return 0;
269 prev = n;
270 n = n->next;
273 if (!value)
274 return 0;
276 new = kzalloc(sizeof(*new), GFP_ATOMIC);
277 if (!new)
278 return -ENOMEM;
280 new->startbit = bit & ~(MAPSIZE - 1);
281 new->map = (MAPBIT << (bit - new->startbit));
283 if (!n)
284 /* this node will be the highest map within the bitmap */
285 e->highbit = new->startbit + MAPSIZE;
287 if (prev) {
288 new->next = prev->next;
289 prev->next = new;
290 } else {
291 new->next = e->node;
292 e->node = new;
295 return 0;
298 void ebitmap_destroy(struct ebitmap *e)
300 struct ebitmap_node *n, *temp;
302 if (!e)
303 return;
305 n = e->node;
306 while (n) {
307 temp = n;
308 n = n->next;
309 kfree(temp);
312 e->highbit = 0;
313 e->node = NULL;
314 return;
317 int ebitmap_read(struct ebitmap *e, void *fp)
319 int rc;
320 struct ebitmap_node *n, *l;
321 __le32 buf[3];
322 u32 mapsize, count, i;
323 __le64 map;
325 ebitmap_init(e);
327 rc = next_entry(buf, fp, sizeof buf);
328 if (rc < 0)
329 goto out;
331 mapsize = le32_to_cpu(buf[0]);
332 e->highbit = le32_to_cpu(buf[1]);
333 count = le32_to_cpu(buf[2]);
335 if (mapsize != MAPSIZE) {
336 printk(KERN_ERR "security: ebitmap: map size %u does not "
337 "match my size %Zd (high bit was %d)\n", mapsize,
338 MAPSIZE, e->highbit);
339 goto bad;
341 if (!e->highbit) {
342 e->node = NULL;
343 goto ok;
345 if (e->highbit & (MAPSIZE - 1)) {
346 printk(KERN_ERR "security: ebitmap: high bit (%d) is not a "
347 "multiple of the map size (%Zd)\n", e->highbit, MAPSIZE);
348 goto bad;
350 l = NULL;
351 for (i = 0; i < count; i++) {
352 rc = next_entry(buf, fp, sizeof(u32));
353 if (rc < 0) {
354 printk(KERN_ERR "security: ebitmap: truncated map\n");
355 goto bad;
357 n = kzalloc(sizeof(*n), GFP_KERNEL);
358 if (!n) {
359 printk(KERN_ERR "security: ebitmap: out of memory\n");
360 rc = -ENOMEM;
361 goto bad;
364 n->startbit = le32_to_cpu(buf[0]);
366 if (n->startbit & (MAPSIZE - 1)) {
367 printk(KERN_ERR "security: ebitmap start bit (%d) is "
368 "not a multiple of the map size (%Zd)\n",
369 n->startbit, MAPSIZE);
370 goto bad_free;
372 if (n->startbit > (e->highbit - MAPSIZE)) {
373 printk(KERN_ERR "security: ebitmap start bit (%d) is "
374 "beyond the end of the bitmap (%Zd)\n",
375 n->startbit, (e->highbit - MAPSIZE));
376 goto bad_free;
378 rc = next_entry(&map, fp, sizeof(u64));
379 if (rc < 0) {
380 printk(KERN_ERR "security: ebitmap: truncated map\n");
381 goto bad_free;
383 n->map = le64_to_cpu(map);
385 if (!n->map) {
386 printk(KERN_ERR "security: ebitmap: null map in "
387 "ebitmap (startbit %d)\n", n->startbit);
388 goto bad_free;
390 if (l) {
391 if (n->startbit <= l->startbit) {
392 printk(KERN_ERR "security: ebitmap: start "
393 "bit %d comes after start bit %d\n",
394 n->startbit, l->startbit);
395 goto bad_free;
397 l->next = n;
398 } else
399 e->node = n;
401 l = n;
405 rc = 0;
406 out:
407 return rc;
408 bad_free:
409 kfree(n);
410 bad:
411 if (!rc)
412 rc = -EINVAL;
413 ebitmap_destroy(e);
414 goto out;