4 * Read and write the per-WIM-image table of security descriptors.
8 * Copyright (C) 2012, 2013, 2014 Eric Biggers
10 * This file is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option) any
15 * This file is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this file; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/assert.h"
29 #include "wimlib/avl_tree.h"
30 #include "wimlib/endianness.h"
31 #include "wimlib/error.h"
32 #include "wimlib/security.h"
33 #include "wimlib/sha1.h"
34 #include "wimlib/util.h"
36 struct wim_security_data_disk {
42 struct wim_security_data *
43 new_wim_security_data(void)
45 return CALLOC(1, sizeof(struct wim_security_data));
49 * Reads the security data from the metadata resource of a WIM image.
52 * Buffer containing an uncompressed WIM metadata resource.
54 * Length of the uncompressed metadata resource, in bytes.
56 * On success, a pointer to the resulting security data structure will be
59 * Note: There is no `offset' argument because the security data is located at
60 * the beginning of the metadata resource.
63 * WIMLIB_ERR_SUCCESS (0)
64 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
68 read_wim_security_data(const u8 *buf, size_t buf_len,
69 struct wim_security_data **sd_ret)
71 struct wim_security_data *sd;
75 u64 size_no_descriptors;
76 const struct wim_security_data_disk *sd_disk;
80 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
82 sd = new_wim_security_data();
86 sd_disk = (const struct wim_security_data_disk *)buf;
87 sd->total_length = le32_to_cpu(sd_disk->total_length);
88 sd->num_entries = le32_to_cpu(sd_disk->num_entries);
90 DEBUG("Reading security data: num_entries=%u, total_length=%u",
91 sd->num_entries, sd->total_length);
93 /* Length field of 0 is a special case that really means length
95 if (sd->total_length == 0)
98 /* The security_id field of each dentry is a signed 32-bit integer, so
99 * the possible indices into the security descriptors table are 0
100 * through 0x7fffffff. Which means 0x80000000 security descriptors
101 * maximum. Not like you should ever have anywhere close to that many
102 * security descriptors! */
103 if (sd->num_entries > 0x80000000)
106 /* Verify the listed total length of the security data is big enough to
107 * include the sizes array, verify that the file data is big enough to
108 * include it as well, then allocate the array of sizes.
110 * Note: The total length of the security data must fit in a 32-bit
111 * integer, even though each security descriptor size is a 64-bit
112 * integer. This is stupid, and we need to be careful not to actually
113 * let the security descriptor sizes be over 0xffffffff. */
114 if (sd->total_length > buf_len)
117 sizes_size = (u64)sd->num_entries * sizeof(u64);
118 size_no_descriptors = 8 + sizes_size;
119 if (size_no_descriptors > sd->total_length)
122 total_len = size_no_descriptors;
124 /* Return immediately if no security descriptors. */
125 if (sd->num_entries == 0)
126 goto out_align_total_length;
128 /* Allocate a new buffer for the sizes array */
129 sd->sizes = MALLOC(sizes_size);
133 /* Copy the sizes array into the new buffer */
134 for (u32 i = 0; i < sd->num_entries; i++) {
135 sd->sizes[i] = le64_to_cpu(sd_disk->sizes[i]);
136 if (sd->sizes[i] > 0xffffffff)
140 p = (const u8*)sd_disk + size_no_descriptors;
142 /* Allocate the array of pointers to the security descriptors, then read
143 * them into separate buffers. */
144 sd->descriptors = CALLOC(sd->num_entries, sizeof(sd->descriptors[0]));
145 if (!sd->descriptors)
148 for (u32 i = 0; i < sd->num_entries; i++) {
149 if (sd->sizes[i] == 0)
151 total_len += sd->sizes[i];
152 if (total_len > (u64)sd->total_length)
154 sd->descriptors[i] = memdup(p, sd->sizes[i]);
155 if (!sd->descriptors[i])
159 out_align_total_length:
160 total_len = ALIGN(total_len, 8);
161 sd->total_length = ALIGN(sd->total_length, 8);
162 if (total_len != sd->total_length) {
163 WARNING("Expected WIM security data total length of "
164 "%u bytes, but calculated %u bytes",
165 sd->total_length, (unsigned)total_len);
171 ERROR("WIM security data is invalid!");
172 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
175 ERROR("Out of memory while reading WIM security data!");
176 ret = WIMLIB_ERR_NOMEM;
178 free_wim_security_data(sd);
184 * Writes the security data for a WIM image to an in-memory buffer.
187 write_wim_security_data(const struct wim_security_data * restrict sd,
190 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
191 "= %"PRIu32")", sd->total_length, sd->num_entries);
194 struct wim_security_data_disk *sd_disk = (struct wim_security_data_disk*)p;
195 u32 num_entries = sd->num_entries;
197 sd_disk->total_length = cpu_to_le32(sd->total_length);
198 sd_disk->num_entries = cpu_to_le32(num_entries);
200 for (u32 i = 0; i < num_entries; i++)
201 sd_disk->sizes[i] = cpu_to_le64(sd->sizes[i]);
203 p = (u8*)&sd_disk->sizes[num_entries];
205 for (u32 i = 0; i < num_entries; i++)
206 p = mempcpy(p, sd->descriptors[i], sd->sizes[i]);
208 while ((uintptr_t)p & 7)
211 wimlib_assert(p - orig_p == sd->total_length);
213 DEBUG("Successfully wrote security data.");
218 free_wim_security_data(struct wim_security_data *sd)
221 u8 **descriptors = sd->descriptors;
222 u32 num_entries = sd->num_entries;
224 while (num_entries--)
225 FREE(*descriptors++);
227 FREE(sd->descriptors);
234 u8 hash[SHA1_HASH_SIZE];
235 struct avl_tree_node index_node;
238 #define SD_NODE(avl_node) \
239 avl_tree_entry(avl_node, struct sd_node, index_node)
242 free_sd_tree(struct avl_tree_node *node)
245 free_sd_tree(node->left);
246 free_sd_tree(node->right);
252 rollback_new_security_descriptors(struct wim_sd_set *sd_set)
254 struct wim_security_data *sd = sd_set->sd;
255 u8 **descriptors = sd->descriptors + sd_set->orig_num_entries;
256 u32 num_entries = sd->num_entries - sd_set->orig_num_entries;
257 while (num_entries--)
258 FREE(*descriptors++);
259 sd->num_entries = sd_set->orig_num_entries;
262 /* Frees a security descriptor index set. */
264 destroy_sd_set(struct wim_sd_set *sd_set)
266 free_sd_tree(sd_set->root);
270 _avl_cmp_nodes_by_hash(const struct avl_tree_node *n1,
271 const struct avl_tree_node *n2)
273 return hashes_cmp(SD_NODE(n1)->hash, SD_NODE(n2)->hash);
276 /* Inserts a new node into the security descriptor index tree. Returns true
277 * if successful (not a duplicate). */
279 insert_sd_node(struct wim_sd_set *set, struct sd_node *new)
281 return NULL == avl_tree_insert(&set->root, &new->index_node,
282 _avl_cmp_nodes_by_hash);
285 /* Returns the index of the security descriptor having a SHA1 message digest of
286 * @hash. If not found, return -1. */
288 lookup_sd(struct wim_sd_set *set, const u8 hash[SHA1_HASH_SIZE])
290 struct avl_tree_node *res;
291 struct sd_node dummy;
293 copy_hash(dummy.hash, hash);
294 res = avl_tree_lookup_node(set->root, &dummy.index_node,
295 _avl_cmp_nodes_by_hash);
298 return SD_NODE(res)->security_id;
302 * Adds a security descriptor to the indexed security descriptor set as well as
303 * the corresponding `struct wim_security_data', and returns the new security
304 * ID; or, if there is an existing security descriptor that is the same, return
305 * the security ID for it. If a new security descriptor cannot be allocated,
309 sd_set_add_sd(struct wim_sd_set *sd_set, const char *descriptor, size_t size)
311 u8 hash[SHA1_HASH_SIZE];
317 struct wim_security_data *sd;
320 sha1_buffer(descriptor, size, hash);
322 security_id = lookup_sd(sd_set, hash);
323 if (security_id >= 0) /* Identical descriptor already exists */
326 /* Need to add a new security descriptor */
329 new = MALLOC(sizeof(*new));
333 descr_copy = memdup(descriptor, size);
338 new->security_id = sd->num_entries;
339 copy_hash(new->hash, hash);
341 /* There typically are only a few dozen security descriptors in a
342 * directory tree, so expanding the array of security descriptors by
343 * only 1 extra space each time should not be a problem. */
344 descriptors = REALLOC(sd->descriptors,
345 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
348 sd->descriptors = descriptors;
349 sizes = REALLOC(sd->sizes,
350 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
354 sd->descriptors[sd->num_entries] = descr_copy;
355 sd->sizes[sd->num_entries] = size;
357 DEBUG("There are now %u security descriptors", sd->num_entries);
358 bret = insert_sd_node(sd_set, new);
360 security_id = new->security_id;
370 /* Initialize a `struct sd_set' mapping from SHA1 message digests of security
371 * descriptors to indices into the security descriptors table of the WIM image
374 init_sd_set(struct wim_sd_set *sd_set, struct wim_security_data *sd)
381 /* Remember the original number of security descriptors so that newly
382 * added ones can be rolled back if needed. */
383 sd_set->orig_num_entries = sd->num_entries;
384 for (u32 i = 0; i < sd->num_entries; i++) {
387 new = MALLOC(sizeof(struct sd_node));
389 ret = WIMLIB_ERR_NOMEM;
390 goto out_destroy_sd_set;
392 sha1_buffer(sd->descriptors[i], sd->sizes[i], new->hash);
393 new->security_id = i;
394 if (!insert_sd_node(sd_set, new))
395 FREE(new); /* Ignore duplicate security descriptor */
400 destroy_sd_set(sd_set);