]> wimlib.net Git - wimlib/blob - src/security.c
read_wim_lookup_table(): Allow multiple "subpacks" per packed run
[wimlib] / src / security.c
1 /*
2  * security.c
3  *
4  * Read and write the per-WIM-image table of security descriptors.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013, 2014 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
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"
37
38 struct wim_security_data_disk {
39         le32 total_length;
40         le32 num_entries;
41         le64 sizes[];
42 } _packed_attribute;
43
44 struct wim_security_data *
45 new_wim_security_data(void)
46 {
47         return CALLOC(1, sizeof(struct wim_security_data));
48 }
49
50 /*
51  * Reads the security data from the metadata resource of a WIM image.
52  *
53  * @metadata_resource:  An array that contains the uncompressed metadata
54  *                              resource for the WIM image.
55  * @metadata_resource_len:      The length of @metadata_resource.
56  * @sd_ret:     A pointer to a pointer to a wim_security_data structure that
57  *              will be filled in with a pointer to a new wim_security_data
58  *              structure containing the security data on success.
59  *
60  * Note: There is no `offset' argument because the security data is located at
61  * the beginning of the metadata resource.
62  *
63  * Return values:
64  *      WIMLIB_ERR_SUCCESS (0)
65  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
66  *      WIMLIB_ERR_NOMEM
67  */
68 int
69 read_wim_security_data(const u8 metadata_resource[], size_t metadata_resource_len,
70                        struct wim_security_data **sd_ret)
71 {
72         struct wim_security_data *sd;
73         int ret;
74         u64 total_len;
75         u64 sizes_size;
76         u64 size_no_descriptors;
77         const struct wim_security_data_disk *sd_disk;
78         const u8 *p;
79
80         if (metadata_resource_len < 8)
81                 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
82
83         sd = new_wim_security_data();
84         if (!sd)
85                 goto out_of_memory;
86
87         sd_disk = (const struct wim_security_data_disk*)metadata_resource;
88         sd->total_length = le32_to_cpu(sd_disk->total_length);
89         sd->num_entries = le32_to_cpu(sd_disk->num_entries);
90
91         DEBUG("Reading security data: num_entries=%u, total_length=%u",
92               sd->num_entries, sd->total_length);
93
94         /* Length field of 0 is a special case that really means length
95          * of 8. */
96         if (sd->total_length == 0)
97                 sd->total_length = 8;
98
99         /* The security_id field of each dentry is a signed 32-bit integer, so
100          * the possible indices into the security descriptors table are 0
101          * through 0x7fffffff.  Which means 0x80000000 security descriptors
102          * maximum.  Not like you should ever have anywhere close to that many
103          * security descriptors! */
104         if (sd->num_entries > 0x80000000)
105                 goto out_invalid_sd;
106
107         /* Verify the listed total length of the security data is big enough to
108          * include the sizes array, verify that the file data is big enough to
109          * include it as well, then allocate the array of sizes.
110          *
111          * Note: The total length of the security data must fit in a 32-bit
112          * integer, even though each security descriptor size is a 64-bit
113          * integer.  This is stupid, and we need to be careful not to actually
114          * let the security descriptor sizes be over 0xffffffff.  */
115         if (sd->total_length > metadata_resource_len)
116                 goto out_invalid_sd;
117
118         sizes_size = (u64)sd->num_entries * sizeof(u64);
119         size_no_descriptors = 8 + sizes_size;
120         if (size_no_descriptors > sd->total_length)
121                 goto out_invalid_sd;
122
123         total_len = size_no_descriptors;
124
125         /* Return immediately if no security descriptors. */
126         if (sd->num_entries == 0)
127                 goto out_align_total_length;
128
129         /* Allocate a new buffer for the sizes array */
130         sd->sizes = MALLOC(sizes_size);
131         if (!sd->sizes)
132                 goto out_of_memory;
133
134         /* Copy the sizes array into the new buffer */
135         for (u32 i = 0; i < sd->num_entries; i++) {
136                 sd->sizes[i] = le64_to_cpu(sd_disk->sizes[i]);
137                 if (sd->sizes[i] > 0xffffffff)
138                         goto out_invalid_sd;
139         }
140
141         p = (const u8*)sd_disk + size_no_descriptors;
142
143         /* Allocate the array of pointers to the security descriptors, then read
144          * them into separate buffers. */
145         sd->descriptors = CALLOC(sd->num_entries, sizeof(sd->descriptors[0]));
146         if (!sd->descriptors)
147                 goto out_of_memory;
148
149         for (u32 i = 0; i < sd->num_entries; i++) {
150                 if (sd->sizes[i] == 0)
151                         continue;
152                 total_len += sd->sizes[i];
153                 if (total_len > (u64)sd->total_length)
154                         goto out_invalid_sd;
155                 sd->descriptors[i] = memdup(p, sd->sizes[i]);
156                 if (!sd->descriptors[i])
157                         goto out_of_memory;
158                 p += sd->sizes[i];
159         }
160 out_align_total_length:
161         total_len = (total_len + 7) & ~7;
162         sd->total_length = (sd->total_length + 7) & ~7;
163         if (total_len != sd->total_length) {
164                 WARNING("Expected WIM security data total length of "
165                         "%u bytes, but calculated %u bytes",
166                         sd->total_length, (unsigned)total_len);
167         }
168         *sd_ret = sd;
169         ret = 0;
170         goto out;
171 out_invalid_sd:
172         ERROR("WIM security data is invalid!");
173         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
174         goto out_free_sd;
175 out_of_memory:
176         ERROR("Out of memory while reading WIM security data!");
177         ret = WIMLIB_ERR_NOMEM;
178 out_free_sd:
179         free_wim_security_data(sd);
180 out:
181         return ret;
182 }
183
184 /*
185  * Writes the security data for a WIM image to an in-memory buffer.
186  */
187 u8 *
188 write_wim_security_data(const struct wim_security_data * restrict sd,
189                         u8 * restrict p)
190 {
191         DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
192               "= %"PRIu32")", sd->total_length, sd->num_entries);
193
194         u8 *orig_p = p;
195         struct wim_security_data_disk *sd_disk = (struct wim_security_data_disk*)p;
196         u32 num_entries = sd->num_entries;
197
198         sd_disk->total_length = cpu_to_le32(sd->total_length);
199         sd_disk->num_entries = cpu_to_le32(num_entries);
200
201         for (u32 i = 0; i < num_entries; i++)
202                 sd_disk->sizes[i] = cpu_to_le64(sd->sizes[i]);
203
204         p = (u8*)&sd_disk->sizes[num_entries];
205
206         for (u32 i = 0; i < num_entries; i++)
207                 p = mempcpy(p, sd->descriptors[i], sd->sizes[i]);
208
209         while ((uintptr_t)p & 7)
210                 *p++ = 0;
211
212         wimlib_assert(p - orig_p == sd->total_length);
213
214         DEBUG("Successfully wrote security data.");
215         return p;
216 }
217
218 void
219 free_wim_security_data(struct wim_security_data *sd)
220 {
221         if (sd) {
222                 u8 **descriptors = sd->descriptors;
223                 u32 num_entries  = sd->num_entries;
224                 if (descriptors)
225                         while (num_entries--)
226                                 FREE(*descriptors++);
227                 FREE(sd->sizes);
228                 FREE(sd->descriptors);
229                 FREE(sd);
230         }
231 }
232
233 struct sd_node {
234         int security_id;
235         u8 hash[SHA1_HASH_SIZE];
236         struct avl_tree_node index_node;
237 };
238
239 #define SD_NODE(avl_node) \
240         avl_tree_entry(avl_node, struct sd_node, index_node)
241
242 static void
243 free_sd_tree(struct avl_tree_node *node)
244 {
245         if (node) {
246                 free_sd_tree(node->left);
247                 free_sd_tree(node->right);
248                 FREE(SD_NODE(node));
249         }
250 }
251
252 void
253 rollback_new_security_descriptors(struct wim_sd_set *sd_set)
254 {
255         struct wim_security_data *sd = sd_set->sd;
256         u8 **descriptors = sd->descriptors + sd_set->orig_num_entries;
257         u32 num_entries  = sd->num_entries - sd_set->orig_num_entries;
258         while (num_entries--)
259                 FREE(*descriptors++);
260         sd->num_entries = sd_set->orig_num_entries;
261 }
262
263 /* Frees a security descriptor index set. */
264 void
265 destroy_sd_set(struct wim_sd_set *sd_set)
266 {
267         free_sd_tree(sd_set->root);
268 }
269
270 static int
271 _avl_cmp_nodes_by_hash(const struct avl_tree_node *n1,
272                        const struct avl_tree_node *n2)
273 {
274         return hashes_cmp(SD_NODE(n1)->hash, SD_NODE(n2)->hash);
275 }
276
277 /* Inserts a a new node into the security descriptor index tree.  Returns true
278  * if successful (not a duplicate).  */
279 static bool
280 insert_sd_node(struct wim_sd_set *set, struct sd_node *new)
281 {
282         return NULL == avl_tree_insert(&set->root, &new->index_node,
283                                        _avl_cmp_nodes_by_hash);
284 }
285
286 /* Returns the index of the security descriptor having a SHA1 message digest of
287  * @hash.  If not found, return -1. */
288 static int
289 lookup_sd(struct wim_sd_set *set, const u8 hash[SHA1_HASH_SIZE])
290 {
291         struct avl_tree_node *res;
292         struct sd_node dummy;
293
294         copy_hash(dummy.hash, hash);
295         res = avl_tree_lookup_node(set->root, &dummy.index_node,
296                                    _avl_cmp_nodes_by_hash);
297         if (!res)
298                 return -1;
299         return SD_NODE(res)->security_id;
300 }
301
302 /*
303  * Adds a security descriptor to the indexed security descriptor set as well as
304  * the corresponding `struct wim_security_data', and returns the new security
305  * ID; or, if there is an existing security descriptor that is the same, return
306  * the security ID for it.  If a new security descriptor cannot be allocated,
307  * return -1.
308  */
309 int
310 sd_set_add_sd(struct wim_sd_set *sd_set, const char *descriptor, size_t size)
311 {
312         u8 hash[SHA1_HASH_SIZE];
313         int security_id;
314         struct sd_node *new;
315         u8 **descriptors;
316         u64 *sizes;
317         u8 *descr_copy;
318         struct wim_security_data *sd;
319         bool bret;
320
321         sha1_buffer(descriptor, size, hash);
322
323         security_id = lookup_sd(sd_set, hash);
324         if (security_id >= 0) /* Identical descriptor already exists */
325                 goto out;
326
327         /* Need to add a new security descriptor */
328         security_id = -1;
329
330         new = MALLOC(sizeof(*new));
331         if (!new)
332                 goto out;
333
334         descr_copy = memdup(descriptor, size);
335         if (!descr_copy)
336                 goto out_free_node;
337
338         sd = sd_set->sd;
339         new->security_id = sd->num_entries;
340         copy_hash(new->hash, hash);
341
342         /* There typically are only a few dozen security descriptors in a
343          * directory tree, so expanding the array of security descriptors by
344          * only 1 extra space each time should not be a problem. */
345         descriptors = REALLOC(sd->descriptors,
346                               (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
347         if (!descriptors)
348                 goto out_free_descr;
349         sd->descriptors = descriptors;
350         sizes = REALLOC(sd->sizes,
351                         (sd->num_entries + 1) * sizeof(sd->sizes[0]));
352         if (!sizes)
353                 goto out_free_descr;
354         sd->sizes = sizes;
355         sd->descriptors[sd->num_entries] = descr_copy;
356         sd->sizes[sd->num_entries] = size;
357         sd->num_entries++;
358         DEBUG("There are now %u security descriptors", sd->num_entries);
359         bret = insert_sd_node(sd_set, new);
360         wimlib_assert(bret);
361         security_id = new->security_id;
362         goto out;
363 out_free_descr:
364         FREE(descr_copy);
365 out_free_node:
366         FREE(new);
367 out:
368         return security_id;
369 }
370
371 /* Initialize a `struct sd_set' mapping from SHA1 message digests of security
372  * descriptors to indices into the security descriptors table of the WIM image
373  * (security IDs).  */
374 int
375 init_sd_set(struct wim_sd_set *sd_set, struct wim_security_data *sd)
376 {
377         int ret;
378
379         sd_set->sd = sd;
380         sd_set->root = NULL;
381
382         /* Remember the original number of security descriptors so that newly
383          * added ones can be rolled back if needed. */
384         sd_set->orig_num_entries = sd->num_entries;
385         for (u32 i = 0; i < sd->num_entries; i++) {
386                 struct sd_node *new;
387
388                 new = MALLOC(sizeof(struct sd_node));
389                 if (!new) {
390                         ret = WIMLIB_ERR_NOMEM;
391                         goto out_destroy_sd_set;
392                 }
393                 sha1_buffer(sd->descriptors[i], sd->sizes[i], new->hash);
394                 new->security_id = i;
395                 if (!insert_sd_node(sd_set, new))
396                         FREE(new); /* Ignore duplicate security descriptor */
397         }
398         ret = 0;
399         goto out;
400 out_destroy_sd_set:
401         destroy_sd_set(sd_set);
402 out:
403         return ret;
404 }