Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of the extensible bitmap type. |
| 3 | * |
| 4 | * Author : Stephen Smalley, <sds@epoch.ncsc.mil> |
| 5 | */ |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 6 | /* |
| 7 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 8 | * |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 9 | * Added support to import/export the NetLabel category bitmap |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 10 | * |
| 11 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 |
| 12 | */ |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 13 | /* |
| 14 | * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com> |
| 15 | * Applied standard bit operations to improve bitmap scanning. |
| 16 | */ |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/errno.h> |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 21 | #include <net/netlabel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "ebitmap.h" |
| 23 | #include "policydb.h" |
| 24 | |
| 25 | int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2) |
| 26 | { |
| 27 | struct ebitmap_node *n1, *n2; |
| 28 | |
| 29 | if (e1->highbit != e2->highbit) |
| 30 | return 0; |
| 31 | |
| 32 | n1 = e1->node; |
| 33 | n2 = e2->node; |
| 34 | while (n1 && n2 && |
| 35 | (n1->startbit == n2->startbit) && |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 36 | !memcmp(n1->maps, n2->maps, EBITMAP_SIZE / 8)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | n1 = n1->next; |
| 38 | n2 = n2->next; |
| 39 | } |
| 40 | |
| 41 | if (n1 || n2) |
| 42 | return 0; |
| 43 | |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src) |
| 48 | { |
| 49 | struct ebitmap_node *n, *new, *prev; |
| 50 | |
| 51 | ebitmap_init(dst); |
| 52 | n = src->node; |
| 53 | prev = NULL; |
| 54 | while (n) { |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 55 | new = kzalloc(sizeof(*new), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (!new) { |
| 57 | ebitmap_destroy(dst); |
| 58 | return -ENOMEM; |
| 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | new->startbit = n->startbit; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 61 | memcpy(new->maps, n->maps, EBITMAP_SIZE / 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | new->next = NULL; |
| 63 | if (prev) |
| 64 | prev->next = new; |
| 65 | else |
| 66 | dst->node = new; |
| 67 | prev = new; |
| 68 | n = n->next; |
| 69 | } |
| 70 | |
| 71 | dst->highbit = src->highbit; |
| 72 | return 0; |
| 73 | } |
| 74 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 75 | #ifdef CONFIG_NETLABEL |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 76 | /** |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 77 | * ebitmap_netlbl_export - Export an ebitmap into a NetLabel category bitmap |
| 78 | * @ebmap: the ebitmap to export |
| 79 | * @catmap: the NetLabel category bitmap |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 80 | * |
| 81 | * Description: |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 82 | * Export a SELinux extensibile bitmap into a NetLabel category bitmap. |
| 83 | * Returns zero on success, negative values on error. |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 84 | * |
| 85 | */ |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 86 | int ebitmap_netlbl_export(struct ebitmap *ebmap, |
| 87 | struct netlbl_lsm_secattr_catmap **catmap) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 88 | { |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 89 | struct ebitmap_node *e_iter = ebmap->node; |
| 90 | struct netlbl_lsm_secattr_catmap *c_iter; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 91 | u32 cmap_idx, cmap_sft; |
| 92 | int i; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 93 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 94 | /* NetLabel's NETLBL_CATMAP_MAPTYPE is defined as an array of u64, |
| 95 | * however, it is not always compatible with an array of unsigned long |
| 96 | * in ebitmap_node. |
| 97 | * In addition, you should pay attention the following implementation |
| 98 | * assumes unsigned long has a width equal with or less than 64-bit. |
| 99 | */ |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 100 | |
| 101 | if (e_iter == NULL) { |
| 102 | *catmap = NULL; |
Paul Moore | bf0edf3 | 2006-10-11 19:10:48 -0400 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 106 | c_iter = netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 107 | if (c_iter == NULL) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 108 | return -ENOMEM; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 109 | *catmap = c_iter; |
| 110 | c_iter->startbit = e_iter->startbit & ~(NETLBL_CATMAP_SIZE - 1); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 111 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 112 | while (e_iter != NULL) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 113 | for (i = 0; i < EBITMAP_UNIT_NUMS; i++) { |
| 114 | unsigned int delta, e_startbit, c_endbit; |
| 115 | |
| 116 | e_startbit = e_iter->startbit + i * EBITMAP_UNIT_SIZE; |
| 117 | c_endbit = c_iter->startbit + NETLBL_CATMAP_SIZE; |
| 118 | if (e_startbit >= c_endbit) { |
| 119 | c_iter->next |
| 120 | = netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 121 | if (c_iter->next == NULL) |
| 122 | goto netlbl_export_failure; |
| 123 | c_iter = c_iter->next; |
| 124 | c_iter->startbit |
| 125 | = e_startbit & ~(NETLBL_CATMAP_SIZE - 1); |
| 126 | } |
| 127 | delta = e_startbit - c_iter->startbit; |
| 128 | cmap_idx = delta / NETLBL_CATMAP_MAPSIZE; |
| 129 | cmap_sft = delta % NETLBL_CATMAP_MAPSIZE; |
| 130 | c_iter->bitmap[cmap_idx] |
| 131 | |= e_iter->maps[cmap_idx] << cmap_sft; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 132 | } |
KaiGai Kohei | 6d2b685 | 2007-11-07 01:17:16 +0900 | [diff] [blame] | 133 | e_iter = e_iter->next; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 134 | } |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 135 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 136 | return 0; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 137 | |
| 138 | netlbl_export_failure: |
| 139 | netlbl_secattr_catmap_free(*catmap); |
| 140 | return -ENOMEM; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 144 | * ebitmap_netlbl_import - Import a NetLabel category bitmap into an ebitmap |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 145 | * @ebmap: the ebitmap to import |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 146 | * @catmap: the NetLabel category bitmap |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 147 | * |
| 148 | * Description: |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 149 | * Import a NetLabel category bitmap into a SELinux extensibile bitmap. |
| 150 | * Returns zero on success, negative values on error. |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 151 | * |
| 152 | */ |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 153 | int ebitmap_netlbl_import(struct ebitmap *ebmap, |
| 154 | struct netlbl_lsm_secattr_catmap *catmap) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 155 | { |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 156 | struct ebitmap_node *e_iter = NULL; |
| 157 | struct ebitmap_node *emap_prev = NULL; |
| 158 | struct netlbl_lsm_secattr_catmap *c_iter = catmap; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 159 | u32 c_idx, c_pos, e_idx, e_sft; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 160 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 161 | /* NetLabel's NETLBL_CATMAP_MAPTYPE is defined as an array of u64, |
| 162 | * however, it is not always compatible with an array of unsigned long |
| 163 | * in ebitmap_node. |
| 164 | * In addition, you should pay attention the following implementation |
| 165 | * assumes unsigned long has a width equal with or less than 64-bit. |
| 166 | */ |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 167 | |
| 168 | do { |
| 169 | for (c_idx = 0; c_idx < NETLBL_CATMAP_MAPCNT; c_idx++) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 170 | unsigned int delta; |
| 171 | u64 map = c_iter->bitmap[c_idx]; |
| 172 | |
| 173 | if (!map) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 174 | continue; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 175 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 176 | c_pos = c_iter->startbit |
| 177 | + c_idx * NETLBL_CATMAP_MAPSIZE; |
| 178 | if (!e_iter |
| 179 | || c_pos >= e_iter->startbit + EBITMAP_SIZE) { |
| 180 | e_iter = kzalloc(sizeof(*e_iter), GFP_ATOMIC); |
| 181 | if (!e_iter) |
| 182 | goto netlbl_import_failure; |
| 183 | e_iter->startbit |
| 184 | = c_pos - (c_pos % EBITMAP_SIZE); |
| 185 | if (emap_prev == NULL) |
| 186 | ebmap->node = e_iter; |
| 187 | else |
| 188 | emap_prev->next = e_iter; |
| 189 | emap_prev = e_iter; |
| 190 | } |
| 191 | delta = c_pos - e_iter->startbit; |
| 192 | e_idx = delta / EBITMAP_UNIT_SIZE; |
| 193 | e_sft = delta % EBITMAP_UNIT_SIZE; |
| 194 | while (map) { |
| 195 | e_iter->maps[e_idx++] |= map & (-1UL); |
KaiGai Kohei | 087feb9 | 2007-10-03 23:42:56 +0900 | [diff] [blame] | 196 | map = EBITMAP_SHIFT_UNIT_SIZE(map); |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 197 | } |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 198 | } |
| 199 | c_iter = c_iter->next; |
| 200 | } while (c_iter != NULL); |
| 201 | if (e_iter != NULL) |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 202 | ebmap->highbit = e_iter->startbit + EBITMAP_SIZE; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 203 | else |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 204 | ebitmap_destroy(ebmap); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 205 | |
| 206 | return 0; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 207 | |
| 208 | netlbl_import_failure: |
| 209 | ebitmap_destroy(ebmap); |
| 210 | return -ENOMEM; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 211 | } |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 212 | #endif /* CONFIG_NETLABEL */ |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2) |
| 215 | { |
| 216 | struct ebitmap_node *n1, *n2; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 217 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | if (e1->highbit < e2->highbit) |
| 220 | return 0; |
| 221 | |
| 222 | n1 = e1->node; |
| 223 | n2 = e2->node; |
| 224 | while (n1 && n2 && (n1->startbit <= n2->startbit)) { |
| 225 | if (n1->startbit < n2->startbit) { |
| 226 | n1 = n1->next; |
| 227 | continue; |
| 228 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 229 | for (i = 0; i < EBITMAP_UNIT_NUMS; i++) { |
| 230 | if ((n1->maps[i] & n2->maps[i]) != n2->maps[i]) |
| 231 | return 0; |
| 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | n1 = n1->next; |
| 235 | n2 = n2->next; |
| 236 | } |
| 237 | |
| 238 | if (n2) |
| 239 | return 0; |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int ebitmap_get_bit(struct ebitmap *e, unsigned long bit) |
| 245 | { |
| 246 | struct ebitmap_node *n; |
| 247 | |
| 248 | if (e->highbit < bit) |
| 249 | return 0; |
| 250 | |
| 251 | n = e->node; |
| 252 | while (n && (n->startbit <= bit)) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 253 | if ((n->startbit + EBITMAP_SIZE) > bit) |
| 254 | return ebitmap_node_get_bit(n, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | n = n->next; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value) |
| 262 | { |
| 263 | struct ebitmap_node *n, *prev, *new; |
| 264 | |
| 265 | prev = NULL; |
| 266 | n = e->node; |
| 267 | while (n && n->startbit <= bit) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 268 | if ((n->startbit + EBITMAP_SIZE) > bit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | if (value) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 270 | ebitmap_node_set_bit(n, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } else { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 272 | unsigned int s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 274 | ebitmap_node_clr_bit(n, bit); |
| 275 | |
| 276 | s = find_first_bit(n->maps, EBITMAP_SIZE); |
| 277 | if (s < EBITMAP_SIZE) |
| 278 | return 0; |
| 279 | |
| 280 | /* drop this node from the bitmap */ |
| 281 | if (!n->next) { |
| 282 | /* |
| 283 | * this was the highest map |
| 284 | * within the bitmap |
| 285 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (prev) |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 287 | e->highbit = prev->startbit |
| 288 | + EBITMAP_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | else |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 290 | e->highbit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 292 | if (prev) |
| 293 | prev->next = n->next; |
| 294 | else |
| 295 | e->node = n->next; |
| 296 | kfree(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | prev = n; |
| 301 | n = n->next; |
| 302 | } |
| 303 | |
| 304 | if (!value) |
| 305 | return 0; |
| 306 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 307 | new = kzalloc(sizeof(*new), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (!new) |
| 309 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 311 | new->startbit = bit - (bit % EBITMAP_SIZE); |
| 312 | ebitmap_node_set_bit(new, bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | if (!n) |
| 315 | /* this node will be the highest map within the bitmap */ |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 316 | e->highbit = new->startbit + EBITMAP_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | if (prev) { |
| 319 | new->next = prev->next; |
| 320 | prev->next = new; |
| 321 | } else { |
| 322 | new->next = e->node; |
| 323 | e->node = new; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | void ebitmap_destroy(struct ebitmap *e) |
| 330 | { |
| 331 | struct ebitmap_node *n, *temp; |
| 332 | |
| 333 | if (!e) |
| 334 | return; |
| 335 | |
| 336 | n = e->node; |
| 337 | while (n) { |
| 338 | temp = n; |
| 339 | n = n->next; |
| 340 | kfree(temp); |
| 341 | } |
| 342 | |
| 343 | e->highbit = 0; |
| 344 | e->node = NULL; |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | int ebitmap_read(struct ebitmap *e, void *fp) |
| 349 | { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 350 | struct ebitmap_node *n = NULL; |
| 351 | u32 mapunit, count, startbit, index; |
| 352 | u64 map; |
Alexey Dobriyan | b5bf6c5 | 2005-09-03 15:55:17 -0700 | [diff] [blame] | 353 | __le32 buf[3]; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 354 | int rc, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | ebitmap_init(e); |
| 357 | |
| 358 | rc = next_entry(buf, fp, sizeof buf); |
| 359 | if (rc < 0) |
| 360 | goto out; |
| 361 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 362 | mapunit = le32_to_cpu(buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | e->highbit = le32_to_cpu(buf[1]); |
| 364 | count = le32_to_cpu(buf[2]); |
| 365 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 366 | if (mapunit != sizeof(u64) * 8) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 367 | printk(KERN_ERR "SELinux: ebitmap: map size %u does not " |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 368 | "match my size %Zd (high bit was %d)\n", |
| 369 | mapunit, sizeof(u64) * 8, e->highbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | goto bad; |
| 371 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 372 | |
| 373 | /* round up e->highbit */ |
| 374 | e->highbit += EBITMAP_SIZE - 1; |
| 375 | e->highbit -= (e->highbit % EBITMAP_SIZE); |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (!e->highbit) { |
| 378 | e->node = NULL; |
| 379 | goto ok; |
| 380 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | for (i = 0; i < count; i++) { |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 383 | rc = next_entry(&startbit, fp, sizeof(u32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 385 | printk(KERN_ERR "SELinux: ebitmap: truncated map\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | goto bad; |
| 387 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 388 | startbit = le32_to_cpu(startbit); |
| 389 | |
| 390 | if (startbit & (mapunit - 1)) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 391 | printk(KERN_ERR "SELinux: ebitmap start bit (%d) is " |
KaiGai Kohei | 087feb9 | 2007-10-03 23:42:56 +0900 | [diff] [blame] | 392 | "not a multiple of the map unit size (%u)\n", |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 393 | startbit, mapunit); |
| 394 | goto bad; |
| 395 | } |
| 396 | if (startbit > e->highbit - mapunit) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 397 | printk(KERN_ERR "SELinux: ebitmap start bit (%d) is " |
KaiGai Kohei | 087feb9 | 2007-10-03 23:42:56 +0900 | [diff] [blame] | 398 | "beyond the end of the bitmap (%u)\n", |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 399 | startbit, (e->highbit - mapunit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | goto bad; |
| 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 403 | if (!n || startbit >= n->startbit + EBITMAP_SIZE) { |
| 404 | struct ebitmap_node *tmp; |
| 405 | tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); |
| 406 | if (!tmp) { |
| 407 | printk(KERN_ERR |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 408 | "SELinux: ebitmap: out of memory\n"); |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 409 | rc = -ENOMEM; |
| 410 | goto bad; |
| 411 | } |
| 412 | /* round down */ |
| 413 | tmp->startbit = startbit - (startbit % EBITMAP_SIZE); |
Eric Paris | 7696ee8 | 2008-04-18 17:38:30 -0400 | [diff] [blame] | 414 | if (n) |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 415 | n->next = tmp; |
Eric Paris | 7696ee8 | 2008-04-18 17:38:30 -0400 | [diff] [blame] | 416 | else |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 417 | e->node = tmp; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 418 | n = tmp; |
| 419 | } else if (startbit <= n->startbit) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 420 | printk(KERN_ERR "SELinux: ebitmap: start bit %d" |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 421 | " comes after start bit %d\n", |
| 422 | startbit, n->startbit); |
| 423 | goto bad; |
| 424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | rc = next_entry(&map, fp, sizeof(u64)); |
| 427 | if (rc < 0) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 428 | printk(KERN_ERR "SELinux: ebitmap: truncated map\n"); |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 429 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 431 | map = le64_to_cpu(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 433 | index = (startbit - n->startbit) / EBITMAP_UNIT_SIZE; |
| 434 | while (map) { |
KaiGai Kohei | 087feb9 | 2007-10-03 23:42:56 +0900 | [diff] [blame] | 435 | n->maps[index++] = map & (-1UL); |
| 436 | map = EBITMAP_SHIFT_UNIT_SIZE(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | ok: |
| 440 | rc = 0; |
| 441 | out: |
| 442 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | bad: |
| 444 | if (!rc) |
| 445 | rc = -EINVAL; |
| 446 | ebitmap_destroy(e); |
| 447 | goto out; |
| 448 | } |