4 * Read and write the per-WIM-image table of security descriptors.
8 * Copyright (C) 2012, 2013 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/.
26 #include "wimlib_internal.h"
27 #include "buffer_io.h"
30 /* At the start of each type of access control entry. */
32 /* enum ace_type, specifies what type of ACE this is. */
35 /* bitwise OR of the inherit ACE flags #defined above */
38 /* Size of the access control entry. */
42 /* Grants rights to a user or group */
49 /* Denies rights to a user or group */
63 /* Header of an access control list. */
65 /* ACL_REVISION or ACL_REVISION_DS */
71 /* Total size of the ACL, including all access control entries */
74 /* Number of access control entry structures that follow the ACL
82 /* A structure used to identify users or groups. */
87 u8 sub_authority_count;
89 /* Identifies the authority that issued the SID. Can be, but does not
90 * have to be, one of enum sid_authority_value */
91 u8 identifier_authority[6];
102 /* Example: 0x4149 */
103 u16 security_descriptor_control;
105 /* Offset of a SID structure in the security descriptor. */
109 /* Offset of a SID structure in the security descriptor. */
113 /* Offset of an ACL structure in the security descriptor. */
118 /* Offset of an ACL structure in the security descriptor. */
119 /* Discretionary ACL. */
122 } SecurityDescriptor;
125 * This is a hack to work around a problem in libntfs-3g. libntfs-3g validates
126 * security descriptors with a function named ntfs_valid_descr().
127 * ntfs_valid_descr() considers a security descriptor that ends in a SACL
128 * (Sysetm Access Control List) with no ACE's (Access Control Entries) to be
129 * invalid. However, a security descriptor like this exists in the Windows 7
130 * install.wim. Here, security descriptors matching this pattern are modified
131 * to have no SACL. This should make no difference since the SACL had no
132 * entries anyway; however this ensures that that the security descriptors pass
133 * the validation in libntfs-3g.
136 empty_sacl_fixup(u8 *descr, u64 *size_p)
138 /* No-op if no NTFS-3g support, or if NTFS-3g is version 2013 or later
140 #if defined(WITH_NTFS_3G) && !defined(HAVE_NTFS_MNT_RDONLY)
141 if (*size_p >= sizeof(SecurityDescriptor)) {
142 SecurityDescriptor *sd = (SecurityDescriptor*)descr;
143 u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
144 if (sacl_offset == *size_p - sizeof(ACL)) {
145 sd->sacl_offset = cpu_to_le32(0);
146 *size_p -= sizeof(ACL);
153 * Reads the security data from the metadata resource.
155 * @metadata_resource: An array that contains the uncompressed metadata
156 * resource for the WIM file.
157 * @metadata_resource_len: The length of @metadata_resource. It must be at
159 * @sd_p: A pointer to a pointer to a wim_security_data structure that
160 * will be filled in with a pointer to a new wim_security_data
161 * structure on success.
163 * Note: There is no `offset' argument because the security data is located at
164 * the beginning of the metadata resource.
167 read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
168 struct wim_security_data **sd_p)
170 struct wim_security_data *sd;
175 wimlib_assert(metadata_resource_len >= 8);
178 * Sorry this function is excessively complicated--- I'm just being
179 * extremely careful about integer overflows.
182 sd = MALLOC(sizeof(struct wim_security_data));
184 ERROR("Out of memory");
185 return WIMLIB_ERR_NOMEM;
188 sd->descriptors = NULL;
190 p = metadata_resource;
191 p = get_u32(p, &sd->total_length);
192 p = get_u32(p, (u32*)&sd->num_entries);
194 /* The security_id field of each dentry is a signed 32-bit integer, so
195 * the possible indices into the security descriptors table are 0
196 * through 0x7fffffff. Which means 0x80000000 security descriptors
197 * maximum. Not like you should ever have anywhere close to that many
198 * security descriptors! */
199 if (sd->num_entries > 0x80000000) {
200 ERROR("Security data has too many entries!");
204 /* Verify the listed total length of the security data is big enough to
205 * include the sizes array, verify that the file data is big enough to
206 * include it as well, then allocate the array of sizes.
208 * Note: The total length of the security data must fit in a 32-bit
209 * integer, even though each security descriptor size is a 64-bit
210 * integer. This is stupid, and we need to be careful not to actually
211 * let the security descriptor sizes be over 0xffffffff. */
212 if ((u64)sd->total_length > metadata_resource_len) {
213 ERROR("Security data total length (%u) is bigger than the "
214 "metadata resource length (%"PRIu64")",
215 sd->total_length, metadata_resource_len);
219 DEBUG("Reading security data: %u entries, length = %u",
220 sd->num_entries, sd->total_length);
222 if (sd->num_entries == 0) {
223 /* No security descriptors. We allow the total_length field to
224 * be either 8 (which is correct, since there are always 2
225 * 32-bit integers) or 0. */
226 if (sd->total_length != 0 && sd->total_length != 8) {
227 ERROR("Invalid security data length (%u): expected 0 or 8",
231 sd->total_length = 8;
235 u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
236 u64 size_no_descriptors = 8 + sizes_size;
237 if (size_no_descriptors > (u64)sd->total_length) {
238 ERROR("Security data total length of %u is too short because "
239 "there seem to be at least %"PRIu64" bytes of security data",
240 sd->total_length, 8 + sizes_size);
244 sd->sizes = MALLOC(sizes_size);
246 ret = WIMLIB_ERR_NOMEM;
250 /* Copy the sizes array in from the file data. */
251 p = get_bytes(p, sizes_size, sd->sizes);
252 array_le64_to_cpu(sd->sizes, sd->num_entries);
254 /* Allocate the array of pointers to descriptors, and read them in. */
255 sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
256 if (!sd->descriptors) {
257 ERROR("Out of memory while allocating security "
259 ret = WIMLIB_ERR_NOMEM;
262 total_len = size_no_descriptors;
264 for (u32 i = 0; i < sd->num_entries; i++) {
265 /* Watch out for huge security descriptor sizes that could
266 * overflow the total length and wrap it around. */
267 if (total_len + sd->sizes[i] < total_len) {
268 ERROR("Caught overflow in security descriptor lengths "
269 "(current total length = %"PRIu64", security "
270 "descriptor size = %"PRIu64")",
271 total_len, sd->sizes[i]);
274 total_len += sd->sizes[i];
275 /* This check ensures that the descriptor size fits in a 32 bit
276 * integer. Because if it didn't, the total length would come
277 * out bigger than sd->total_length, which is a 32 bit integer.
279 if (total_len > (u64)sd->total_length) {
280 ERROR("Security data total length of %u is too short "
281 "because there seem to be at least %"PRIu64" "
282 "bytes of security data",
283 sd->total_length, total_len);
286 sd->descriptors[i] = MALLOC(sd->sizes[i]);
287 if (!sd->descriptors[i]) {
288 ERROR("Out of memory while allocating security "
290 ret = WIMLIB_ERR_NOMEM;
293 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
294 empty_sacl_fixup(sd->descriptors[i], &sd->sizes[i]);
296 wimlib_assert(total_len <= 0xffffffff);
297 if (((total_len + 7) & ~7) != ((sd->total_length + 7) & ~7)) {
298 ERROR("Expected security data total length = %u, but "
299 "calculated %u", sd->total_length, (unsigned)total_len);
302 sd->total_length = total_len;
307 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
309 free_security_data(sd);
314 * Writes security data to an in-memory buffer.
317 write_security_data(const struct wim_security_data *sd, u8 *p)
319 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
320 "= %"PRIu32")", sd->total_length, sd->num_entries);
322 u32 aligned_length = (sd->total_length + 7) & ~7;
325 p = put_u32(p, aligned_length);
326 p = put_u32(p, sd->num_entries);
328 for (u32 i = 0; i < sd->num_entries; i++)
329 p = put_u64(p, sd->sizes[i]);
331 for (u32 i = 0; i < sd->num_entries; i++)
332 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
334 wimlib_assert(p - orig_p == sd->total_length);
335 p = put_zeroes(p, aligned_length - sd->total_length);
337 DEBUG("Successfully wrote security data.");
342 print_acl(const void *p, const tchar *type)
345 u8 revision = acl->revision;
346 u16 acl_size = le16_to_cpu(acl->acl_size);
347 u16 ace_count = le16_to_cpu(acl->ace_count);
348 tprintf(T(" [%"TS" ACL]\n"), type);
349 tprintf(T(" Revision = %u\n"), revision);
350 tprintf(T(" ACL Size = %u\n"), acl_size);
351 tprintf(T(" ACE Count = %u\n"), ace_count);
354 for (u16 i = 0; i < ace_count; i++) {
355 const ACEHeader *hdr = p;
356 tprintf(T(" [ACE]\n"));
357 tprintf(T(" ACE type = %d\n"), hdr->type);
358 tprintf(T(" ACE flags = 0x%x\n"), hdr->flags);
359 tprintf(T(" ACE size = %u\n"), hdr->size);
360 const AccessAllowedACE *aaa = (const AccessAllowedACE*)hdr;
361 tprintf(T(" ACE mask = %x\n"), le32_to_cpu(aaa->mask));
362 tprintf(T(" SID start = %u\n"), le32_to_cpu(aaa->sid_start));
369 print_sid(const void *p, const tchar *type)
372 tprintf(T(" [%"TS" SID]\n"), type);
373 tprintf(T(" Revision = %u\n"), sid->revision);
374 tprintf(T(" Subauthority count = %u\n"), sid->sub_authority_count);
375 tprintf(T(" Identifier authority = "));
376 print_byte_field(sid->identifier_authority,
377 sizeof(sid->identifier_authority), stdout);
379 for (u8 i = 0; i < sid->sub_authority_count; i++) {
380 tprintf(T(" Subauthority %u = %u\n"),
381 i, le32_to_cpu(sid->sub_authority[i]));
387 print_security_descriptor(const void *p, u64 size)
389 const SecurityDescriptor *sd = p;
391 u8 revision = sd->revision;
392 u16 control = le16_to_cpu(sd->security_descriptor_control);
393 u32 owner_offset = le32_to_cpu(sd->owner_offset);
394 u32 group_offset = le32_to_cpu(sd->group_offset);
395 u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
396 u32 dacl_offset = le32_to_cpu(sd->dacl_offset);
397 tprintf(T("Revision = %u\n"), revision);
398 tprintf(T("Security Descriptor Control = %#x\n"), control);
399 tprintf(T("Owner offset = %u\n"), owner_offset);
400 tprintf(T("Group offset = %u\n"), group_offset);
401 tprintf(T("System ACL offset = %u\n"), sacl_offset);
402 tprintf(T("Discretionary ACL offset = %u\n"), dacl_offset);
404 if (sd->owner_offset != 0)
405 print_sid(p + owner_offset, T("Owner"));
406 if (sd->group_offset != 0)
407 print_sid(p + group_offset, T("Group"));
408 if (sd->sacl_offset != 0)
409 print_acl(p + sacl_offset, T("System"));
410 if (sd->dacl_offset != 0)
411 print_acl(p + dacl_offset, T("Discretionary"));
415 * Prints the security data for a WIM file.
418 print_security_data(const struct wim_security_data *sd)
420 wimlib_assert(sd != NULL);
422 tputs(T("[SECURITY DATA]"));
423 tprintf(T("Length = %"PRIu32" bytes\n"), sd->total_length);
424 tprintf(T("Number of Entries = %"PRIu32"\n"), sd->num_entries);
426 for (u32 i = 0; i < sd->num_entries; i++) {
427 tprintf(T("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n"),
429 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
436 free_security_data(struct wim_security_data *sd)
439 u8 **descriptors = sd->descriptors;
440 u32 num_entries = sd->num_entries;
442 while (num_entries--)
443 FREE(*descriptors++);
445 FREE(sd->descriptors);
452 u8 hash[SHA1_HASH_SIZE];
453 struct rb_node rb_node;
457 free_sd_tree(struct rb_node *node)
460 free_sd_tree(node->rb_left);
461 free_sd_tree(node->rb_right);
462 FREE(container_of(node, struct sd_node, rb_node));
466 /* Frees a security descriptor index set. */
468 destroy_sd_set(struct sd_set *sd_set)
470 free_sd_tree(sd_set->rb_root.rb_node);
473 /* Inserts a a new node into the security descriptor index tree. */
475 insert_sd_node(struct sd_set *set, struct sd_node *new)
477 struct rb_root *root = &set->rb_root;
478 struct rb_node **p = &(root->rb_node);
479 struct rb_node *rb_parent = NULL;
482 struct sd_node *this = container_of(*p, struct sd_node, rb_node);
483 int cmp = hashes_cmp(new->hash, this->hash);
487 p = &((*p)->rb_left);
489 p = &((*p)->rb_right);
491 wimlib_assert(0); /* Duplicate SHA1 message digest */
493 rb_link_node(&new->rb_node, rb_parent, p);
494 rb_insert_color(&new->rb_node, root);
497 /* Returns the index of the security descriptor having a SHA1 message digest of
498 * @hash. If not found, return -1. */
500 lookup_sd(struct sd_set *set, const u8 hash[SHA1_HASH_SIZE])
502 struct rb_node *node = set->rb_root.rb_node;
505 struct sd_node *sd_node = container_of(node, struct sd_node, rb_node);
506 int cmp = hashes_cmp(hash, sd_node->hash);
508 node = node->rb_left;
510 node = node->rb_right;
512 return sd_node->security_id;
518 * Adds a security descriptor to the indexed security descriptor set as well as
519 * the corresponding `struct wim_security_data', and returns the new security
520 * ID; or, if there is an existing security descriptor that is the same, return
521 * the security ID for it. If a new security descriptor cannot be allocated,
525 sd_set_add_sd(struct sd_set *sd_set, const char descriptor[], size_t size)
527 u8 hash[SHA1_HASH_SIZE];
533 struct wim_security_data *sd;
535 sha1_buffer((const u8*)descriptor, size, hash);
537 security_id = lookup_sd(sd_set, hash);
538 if (security_id >= 0) /* Identical descriptor already exists */
541 /* Need to add a new security descriptor */
542 new = MALLOC(sizeof(*new));
545 descr_copy = MALLOC(size);
551 memcpy(descr_copy, descriptor, size);
552 new->security_id = sd->num_entries;
553 copy_hash(new->hash, hash);
555 /* There typically are only a few dozen security descriptors in a
556 * directory tree, so expanding the array of security descriptors by
557 * only 1 extra space each time should not be a problem. */
558 descriptors = REALLOC(sd->descriptors,
559 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
562 sd->descriptors = descriptors;
563 sizes = REALLOC(sd->sizes,
564 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
568 sd->descriptors[sd->num_entries] = descr_copy;
569 sd->sizes[sd->num_entries] = size;
571 DEBUG("There are now %d security descriptors", sd->num_entries);
572 sd->total_length += size + sizeof(sd->sizes[0]);
573 insert_sd_node(sd_set, new);
574 return new->security_id;