4 * Read and write the per-WIM-image table of security descriptors.
8 * Copyright (C) 2012, 2013, 2014 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/assert.h"
31 #include "wimlib/avl_tree.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/security.h"
35 #include "wimlib/sha1.h"
36 #include "wimlib/util.h"
38 struct wim_security_data_disk {
44 struct wim_security_data *
45 new_wim_security_data(void)
47 return CALLOC(1, sizeof(struct wim_security_data));
51 * Reads the security data from the metadata resource of a WIM image.
54 * Buffer containing an uncompressed WIM metadata resource.
56 * Length of the uncompressed metadata resource, in bytes.
58 * On success, a pointer to the resulting security data structure will be
61 * Note: There is no `offset' argument because the security data is located at
62 * the beginning of the metadata resource.
65 * WIMLIB_ERR_SUCCESS (0)
66 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
70 read_wim_security_data(const u8 *buf, size_t buf_len,
71 struct wim_security_data **sd_ret)
73 struct wim_security_data *sd;
77 u64 size_no_descriptors;
78 const struct wim_security_data_disk *sd_disk;
82 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
84 sd = new_wim_security_data();
88 sd_disk = (const struct wim_security_data_disk *)buf;
89 sd->total_length = le32_to_cpu(sd_disk->total_length);
90 sd->num_entries = le32_to_cpu(sd_disk->num_entries);
92 DEBUG("Reading security data: num_entries=%u, total_length=%u",
93 sd->num_entries, sd->total_length);
95 /* Length field of 0 is a special case that really means length
97 if (sd->total_length == 0)
100 /* The security_id field of each dentry is a signed 32-bit integer, so
101 * the possible indices into the security descriptors table are 0
102 * through 0x7fffffff. Which means 0x80000000 security descriptors
103 * maximum. Not like you should ever have anywhere close to that many
104 * security descriptors! */
105 if (sd->num_entries > 0x80000000)
108 /* Verify the listed total length of the security data is big enough to
109 * include the sizes array, verify that the file data is big enough to
110 * include it as well, then allocate the array of sizes.
112 * Note: The total length of the security data must fit in a 32-bit
113 * integer, even though each security descriptor size is a 64-bit
114 * integer. This is stupid, and we need to be careful not to actually
115 * let the security descriptor sizes be over 0xffffffff. */
116 if (sd->total_length > buf_len)
119 sizes_size = (u64)sd->num_entries * sizeof(u64);
120 size_no_descriptors = 8 + sizes_size;
121 if (size_no_descriptors > sd->total_length)
124 total_len = size_no_descriptors;
126 /* Return immediately if no security descriptors. */
127 if (sd->num_entries == 0)
128 goto out_align_total_length;
130 /* Allocate a new buffer for the sizes array */
131 sd->sizes = MALLOC(sizes_size);
135 /* Copy the sizes array into the new buffer */
136 for (u32 i = 0; i < sd->num_entries; i++) {
137 sd->sizes[i] = le64_to_cpu(sd_disk->sizes[i]);
138 if (sd->sizes[i] > 0xffffffff)
142 p = (const u8*)sd_disk + size_no_descriptors;
144 /* Allocate the array of pointers to the security descriptors, then read
145 * them into separate buffers. */
146 sd->descriptors = CALLOC(sd->num_entries, sizeof(sd->descriptors[0]));
147 if (!sd->descriptors)
150 for (u32 i = 0; i < sd->num_entries; i++) {
151 if (sd->sizes[i] == 0)
153 total_len += sd->sizes[i];
154 if (total_len > (u64)sd->total_length)
156 sd->descriptors[i] = memdup(p, sd->sizes[i]);
157 if (!sd->descriptors[i])
161 out_align_total_length:
162 total_len = (total_len + 7) & ~7;
163 sd->total_length = (sd->total_length + 7) & ~7;
164 if (total_len != sd->total_length) {
165 WARNING("Expected WIM security data total length of "
166 "%u bytes, but calculated %u bytes",
167 sd->total_length, (unsigned)total_len);
173 ERROR("WIM security data is invalid!");
174 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
177 ERROR("Out of memory while reading WIM security data!");
178 ret = WIMLIB_ERR_NOMEM;
180 free_wim_security_data(sd);
186 * Writes the security data for a WIM image to an in-memory buffer.
189 write_wim_security_data(const struct wim_security_data * restrict sd,
192 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
193 "= %"PRIu32")", sd->total_length, sd->num_entries);
196 struct wim_security_data_disk *sd_disk = (struct wim_security_data_disk*)p;
197 u32 num_entries = sd->num_entries;
199 sd_disk->total_length = cpu_to_le32(sd->total_length);
200 sd_disk->num_entries = cpu_to_le32(num_entries);
202 for (u32 i = 0; i < num_entries; i++)
203 sd_disk->sizes[i] = cpu_to_le64(sd->sizes[i]);
205 p = (u8*)&sd_disk->sizes[num_entries];
207 for (u32 i = 0; i < num_entries; i++)
208 p = mempcpy(p, sd->descriptors[i], sd->sizes[i]);
210 while ((uintptr_t)p & 7)
213 wimlib_assert(p - orig_p == sd->total_length);
215 DEBUG("Successfully wrote security data.");
220 free_wim_security_data(struct wim_security_data *sd)
223 u8 **descriptors = sd->descriptors;
224 u32 num_entries = sd->num_entries;
226 while (num_entries--)
227 FREE(*descriptors++);
229 FREE(sd->descriptors);
236 u8 hash[SHA1_HASH_SIZE];
237 struct avl_tree_node index_node;
240 #define SD_NODE(avl_node) \
241 avl_tree_entry(avl_node, struct sd_node, index_node)
244 free_sd_tree(struct avl_tree_node *node)
247 free_sd_tree(node->left);
248 free_sd_tree(node->right);
254 rollback_new_security_descriptors(struct wim_sd_set *sd_set)
256 struct wim_security_data *sd = sd_set->sd;
257 u8 **descriptors = sd->descriptors + sd_set->orig_num_entries;
258 u32 num_entries = sd->num_entries - sd_set->orig_num_entries;
259 while (num_entries--)
260 FREE(*descriptors++);
261 sd->num_entries = sd_set->orig_num_entries;
264 /* Frees a security descriptor index set. */
266 destroy_sd_set(struct wim_sd_set *sd_set)
268 free_sd_tree(sd_set->root);
272 _avl_cmp_nodes_by_hash(const struct avl_tree_node *n1,
273 const struct avl_tree_node *n2)
275 return hashes_cmp(SD_NODE(n1)->hash, SD_NODE(n2)->hash);
278 /* Inserts a new node into the security descriptor index tree. Returns true
279 * if successful (not a duplicate). */
281 insert_sd_node(struct wim_sd_set *set, struct sd_node *new)
283 return NULL == avl_tree_insert(&set->root, &new->index_node,
284 _avl_cmp_nodes_by_hash);
287 /* Returns the index of the security descriptor having a SHA1 message digest of
288 * @hash. If not found, return -1. */
290 lookup_sd(struct wim_sd_set *set, const u8 hash[SHA1_HASH_SIZE])
292 struct avl_tree_node *res;
293 struct sd_node dummy;
295 copy_hash(dummy.hash, hash);
296 res = avl_tree_lookup_node(set->root, &dummy.index_node,
297 _avl_cmp_nodes_by_hash);
300 return SD_NODE(res)->security_id;
304 * Adds a security descriptor to the indexed security descriptor set as well as
305 * the corresponding `struct wim_security_data', and returns the new security
306 * ID; or, if there is an existing security descriptor that is the same, return
307 * the security ID for it. If a new security descriptor cannot be allocated,
311 sd_set_add_sd(struct wim_sd_set *sd_set, const char *descriptor, size_t size)
313 u8 hash[SHA1_HASH_SIZE];
319 struct wim_security_data *sd;
322 sha1_buffer(descriptor, size, hash);
324 security_id = lookup_sd(sd_set, hash);
325 if (security_id >= 0) /* Identical descriptor already exists */
328 /* Need to add a new security descriptor */
331 new = MALLOC(sizeof(*new));
335 descr_copy = memdup(descriptor, size);
340 new->security_id = sd->num_entries;
341 copy_hash(new->hash, hash);
343 /* There typically are only a few dozen security descriptors in a
344 * directory tree, so expanding the array of security descriptors by
345 * only 1 extra space each time should not be a problem. */
346 descriptors = REALLOC(sd->descriptors,
347 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
350 sd->descriptors = descriptors;
351 sizes = REALLOC(sd->sizes,
352 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
356 sd->descriptors[sd->num_entries] = descr_copy;
357 sd->sizes[sd->num_entries] = size;
359 DEBUG("There are now %u security descriptors", sd->num_entries);
360 bret = insert_sd_node(sd_set, new);
362 security_id = new->security_id;
372 /* Initialize a `struct sd_set' mapping from SHA1 message digests of security
373 * descriptors to indices into the security descriptors table of the WIM image
376 init_sd_set(struct wim_sd_set *sd_set, struct wim_security_data *sd)
383 /* Remember the original number of security descriptors so that newly
384 * added ones can be rolled back if needed. */
385 sd_set->orig_num_entries = sd->num_entries;
386 for (u32 i = 0; i < sd->num_entries; i++) {
389 new = MALLOC(sizeof(struct sd_node));
391 ret = WIMLIB_ERR_NOMEM;
392 goto out_destroy_sd_set;
394 sha1_buffer(sd->descriptors[i], sd->sizes[i], new->hash);
395 new->security_id = i;
396 if (!insert_sd_node(sd_set, new))
397 FREE(new); /* Ignore duplicate security descriptor */
402 destroy_sd_set(sd_set);