4 * Read the security data from the WIM. Doing anything with the security data
5 * is not yet implemented other than printing some information about it.
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "wimlib_internal.h"
32 * Reads the security data from the metadata resource.
34 * @metadata_resource: An array that contains the uncompressed metadata
35 * resource for the WIM file.
36 * @metadata_resource_len: The length of @metadata_resource. It MUST be at
38 * @sd_p: A pointer to a pointer to a wim_security_data structure that
39 * will be filled in with a pointer to a new wim_security_data
40 * structure on success.
42 * Note: There is no `offset' argument because the security data is located at
43 * the beginning of the metadata resource.
45 int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
46 struct wim_security_data **sd_p)
48 struct wim_security_data *sd;
53 sd = MALLOC(sizeof(struct wim_security_data));
55 ERROR("Out of memory");
56 return WIMLIB_ERR_NOMEM;
59 sd->descriptors = NULL;
62 p = metadata_resource;
63 p = get_u32(p, &sd->total_length);
64 p = get_u32(p, &sd->num_entries);
66 /* Verify the listed total length of the security data is big enough to
67 * include the sizes array, verify that the file data is big enough to
68 * include it as well, then allocate the array of sizes.
70 * Note: The total length of the security data must fit in a 32-bit
71 * integer, even though each security descriptor size is a 64-bit
72 * integer. This is stupid, and we need to be careful not to actually
73 * let the security descriptor sizes be over 0xffffffff. */
74 if ((u64)sd->total_length > metadata_resource_len) {
75 ERROR("Security data total length (%u) is bigger than the "
76 "metadata resource length (%"PRIu64")",
77 sd->total_length, metadata_resource_len);
78 ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
82 DEBUG("Reading security data: %u entries, length = %u",
83 sd->num_entries, sd->total_length);
85 if (sd->num_entries == 0) {
86 /* No security data. */
91 u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
92 u64 size_no_descriptors = 8 + sizes_size;
93 if (size_no_descriptors > (u64)sd->total_length) {
94 ERROR("Security data total length of %u is too short because "
95 "there must be at least %"PRIu64" bytes of security data",
96 sd->total_length, 8 + sizes_size);
97 ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
100 sd->sizes = MALLOC(sizes_size);
102 ret = WIMLIB_ERR_NOMEM;
106 /* Copy the sizes array in from the file data. */
107 p = get_bytes(p, sizes_size, sd->sizes);
108 array_to_le64(sd->sizes, sd->num_entries);
110 /* Allocate the array of pointers to descriptors, and read them in. */
111 sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
112 if (!sd->descriptors) {
113 ERROR("Out of memory while allocating security "
115 ret = WIMLIB_ERR_NOMEM;
118 total_len = size_no_descriptors;
120 for (u32 i = 0; i < sd->num_entries; i++) {
121 /* Watch out for huge security descriptor sizes that could
122 * overflow the total length and wrap it around. */
123 if (total_len + sd->sizes[i] < total_len) {
124 ERROR("Caught overflow in security descriptor lengths "
125 "(current total length = %"PRIu64", security "
126 "descriptor size = %"PRIu64")",
127 total_len, sd->sizes[i]);
128 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
131 total_len += sd->sizes[i];
132 if (total_len > (u64)sd->total_length) {
133 ERROR("Security data total length of %u is too short "
134 "because there are at least %"PRIu64" bytes of "
135 "security data", sd->total_length, total_len);
136 ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
139 sd->descriptors[i] = MALLOC(sd->sizes[i]);
140 if (!sd->descriptors[i]) {
141 ERROR("Out of memory while allocating security "
143 ret = WIMLIB_ERR_NOMEM;
146 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
149 sd->total_length = (u32)total_len;
153 free_security_data(sd);
158 * Writes security data to an in-memory buffer.
160 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
162 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
163 "= %"PRIu32")", sd->total_length, sd->num_entries);
166 p = put_u32(p, sd->total_length);
167 p = put_u32(p, sd->num_entries);
169 for (u32 i = 0; i < sd->num_entries; i++)
170 p = put_u64(p, sd->sizes[i]);
172 for (u32 i = 0; i < sd->num_entries; i++)
173 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
175 wimlib_assert(p - orig_p == sd->total_length);
177 DEBUG("Successfully wrote security data.");
181 /* XXX We don't actually do anything with the ACL's yet besides being able to
182 * print a few things. It seems it would be a lot of work to have comprehensive
183 * support for all the weird flags and stuff, and Windows PE seems to be okay
184 * running from a WIM file that doesn't have any security data at all... */
186 static void print_acl(const u8 *p)
189 TO_LE16(acl->acl_size);
190 TO_LE16(acl->acl_count);
192 printf(" Revision = %u\n", acl->revision);
193 printf(" ACL Size = %u\n", acl->acl_size);
194 printf(" ACE Count = %u\n", acl->ace_count);
197 for (uint i = 0; i < acl->ace_count; i++) {
198 ACEHeader *hdr = (ACEHeader*)p;
200 printf(" ACE type = %d\n", hdr->type);
201 printf(" ACE flags = 0x%x\n", hdr->flags);
202 printf(" ACE size = %u\n", hdr->size);
203 AccessAllowedACE *aaa = (AccessAllowedACE*)hdr;
204 printf(" ACE mask = %x\n", to_le32(aaa->mask));
205 printf(" SID start = %u\n", to_le32(aaa->sid_start));
210 static void print_sid(const u8 *p)
214 printf(" Revision = %u\n", sid->revision);
215 printf(" Subauthority count = %u\n", sid->sub_authority_count);
216 printf(" Identifier authority = ");
217 print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority));
219 for (uint i = 0; i < sid->sub_authority_count; i++)
220 printf(" Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i]));
223 static void print_security_descriptor(const u8 *p, u64 size)
225 SecurityDescriptor *sd = (SecurityDescriptor*)p;
226 TO_LE16(sd->security_descriptor_control);
227 TO_LE32(sd->owner_offset);
228 TO_LE32(sd->group_offset);
229 TO_LE32(sd->sacl_offset);
230 TO_LE32(sd->dacl_offset);
231 printf("Revision = %u\n", sd->revision);
232 printf("Security Descriptor Control = %u\n", sd->security_descriptor_control);
233 printf("Owner offset = %u\n", sd->owner_offset);
234 printf("Group offset = %u\n", sd->group_offset);
235 printf("System ACL offset = %u\n", sd->sacl_offset);
236 printf("Discretionary ACL offset = %u\n", sd->dacl_offset);
238 if (sd->owner_offset != 0)
239 print_sid(p + sd->owner_offset);
240 if (sd->group_offset != 0)
241 print_sid(p + sd->group_offset);
242 if (sd->sacl_offset != 0)
243 print_acl(p + sd->sacl_offset);
244 if (sd->dacl_offset != 0)
245 print_acl(p + sd->dacl_offset);
249 * Prints the security data for a WIM file.
251 void print_security_data(const struct wim_security_data *sd)
253 puts("[SECURITY DATA]");
254 printf("Length = %"PRIu32" bytes\n", sd->total_length);
255 printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
257 for (u32 i = 0; i < sd->num_entries; i++) {
258 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n",
260 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
266 void free_security_data(struct wim_security_data *sd)
270 wimlib_assert(sd->refcnt >= 1);
271 if (--sd->refcnt == 0) {
272 u8 **descriptors = sd->descriptors;
273 u32 num_entries = sd->num_entries;
275 while (num_entries--)
276 FREE(*descriptors++);
278 FREE(sd->descriptors);