4 * Read and write the WIM security data. The security data is a table of
5 * security descriptors. Each WIM image has its own security data, but it's
6 * possible that an image's security data have no security descriptors.
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
29 #include "buffer_io.h"
33 * This is a hack to work around a problem in libntfs-3g. libntfs-3g validates
34 * security descriptors with a function named ntfs_valid_descr().
35 * ntfs_valid_descr() considers a security descriptor that ends in a SACL
36 * (Sysetm Access Control List) with no ACE's (Access Control Entries) to be
37 * invalid. However, a security descriptor like this exists in the Windows 7
38 * install.wim. Here, security descriptors matching this pattern are modified
39 * to have no SACL. This should make no difference since the SACL had no
40 * entries anyway; however this ensures that that the security descriptors pass
41 * the validation in libntfs-3g.
43 static void empty_sacl_fixup(u8 *descr, u64 *size_p)
45 if (*size_p >= sizeof(SecurityDescriptor)) {
46 SecurityDescriptor *sd = (SecurityDescriptor*)descr;
47 u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
48 if (sacl_offset == *size_p - sizeof(ACL)) {
49 sd->sacl_offset = cpu_to_le32(0);
50 *size_p -= sizeof(ACL);
56 * Reads the security data from the metadata resource.
58 * @metadata_resource: An array that contains the uncompressed metadata
59 * resource for the WIM file.
60 * @metadata_resource_len: The length of @metadata_resource. It MUST be at
62 * @sd_p: A pointer to a pointer to a wim_security_data structure that
63 * will be filled in with a pointer to a new wim_security_data
64 * structure on success.
66 * Note: There is no `offset' argument because the security data is located at
67 * the beginning of the metadata resource.
69 int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
70 struct wim_security_data **sd_p)
72 struct wim_security_data *sd;
78 * Sorry this function is excessively complicated--- I'm just being
79 * extremely careful about integer overflows.
82 sd = MALLOC(sizeof(struct wim_security_data));
84 ERROR("Out of memory");
85 return WIMLIB_ERR_NOMEM;
88 sd->descriptors = NULL;
91 p = metadata_resource;
92 p = get_u32(p, &sd->total_length);
93 p = get_u32(p, (u32*)&sd->num_entries);
95 /* The security_id field of each dentry is a signed 32-bit integer, so
96 * the possible indices into the security descriptors table are 0
97 * through 0x7fffffff. Which means 0x80000000 security descriptors
98 * maximum. Not like you should ever have anywhere close to that many
99 * security descriptors! */
100 if (sd->num_entries > 0x80000000) {
101 ERROR("Security data has too many entries!");
105 /* Verify the listed total length of the security data is big enough to
106 * include the sizes array, verify that the file data is big enough to
107 * include it as well, then allocate the array of sizes.
109 * Note: The total length of the security data must fit in a 32-bit
110 * integer, even though each security descriptor size is a 64-bit
111 * integer. This is stupid, and we need to be careful not to actually
112 * let the security descriptor sizes be over 0xffffffff. */
113 if ((u64)sd->total_length > metadata_resource_len) {
114 ERROR("Security data total length (%u) is bigger than the "
115 "metadata resource length (%"PRIu64")",
116 sd->total_length, metadata_resource_len);
120 DEBUG("Reading security data: %u entries, length = %u",
121 sd->num_entries, sd->total_length);
123 if (sd->num_entries == 0) {
124 /* No security descriptors. */
125 if (sd->total_length != 0 && sd->total_length != 8) {
126 ERROR("Invalid security data length (%u): expected 0 or 8",
130 sd->total_length = 8;
134 u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
135 u64 size_no_descriptors = 8 + sizes_size;
136 if (size_no_descriptors > (u64)sd->total_length) {
137 ERROR("Security data total length of %u is too short because "
138 "there must be at least %"PRIu64" bytes of security data",
139 sd->total_length, 8 + sizes_size);
142 if (sizeof(size_t) < 8 && sizes_size > 0xffffffff) {
143 ERROR("Too many security descriptors!");
146 sd->sizes = MALLOC(sizes_size);
148 ret = WIMLIB_ERR_NOMEM;
152 /* Copy the sizes array in from the file data. */
153 p = get_bytes(p, sizes_size, sd->sizes);
154 array_le64_to_cpu(sd->sizes, sd->num_entries);
156 /* Allocate the array of pointers to descriptors, and read them in. */
157 sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
158 if (!sd->descriptors) {
159 ERROR("Out of memory while allocating security "
161 ret = WIMLIB_ERR_NOMEM;
164 total_len = size_no_descriptors;
166 for (u32 i = 0; i < sd->num_entries; i++) {
167 /* Watch out for huge security descriptor sizes that could
168 * overflow the total length and wrap it around. */
169 if (total_len + sd->sizes[i] < total_len) {
170 ERROR("Caught overflow in security descriptor lengths "
171 "(current total length = %"PRIu64", security "
172 "descriptor size = %"PRIu64")",
173 total_len, sd->sizes[i]);
176 total_len += sd->sizes[i];
177 /* This check assures that the descriptor size fits in a 32 bit
178 * integer. Because if it didn't, the total length would come
179 * out bigger than sd->total_length, which is a 32 bit integer.
181 if (total_len > (u64)sd->total_length) {
182 ERROR("Security data total length of %u is too short "
183 "because there are at least %"PRIu64" bytes of "
184 "security data", sd->total_length, total_len);
187 sd->descriptors[i] = MALLOC(sd->sizes[i]);
188 if (!sd->descriptors[i]) {
189 ERROR("Out of memory while allocating security "
191 ret = WIMLIB_ERR_NOMEM;
194 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
195 empty_sacl_fixup(sd->descriptors[i], &sd->sizes[i]);
197 wimlib_assert(total_len <= 0xffffffff);
198 if (((total_len + 7) & ~7) != ((sd->total_length + 7) & ~7)) {
199 ERROR("Expected security data total length = %u, but "
200 "calculated %u", sd->total_length, (unsigned)total_len);
203 sd->total_length = total_len;
208 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
210 free_security_data(sd);
215 * Writes security data to an in-memory buffer.
217 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
219 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
220 "= %"PRIu32")", sd->total_length, sd->num_entries);
222 u32 aligned_length = (sd->total_length + 7) & ~7;
225 p = put_u32(p, aligned_length);
226 p = put_u32(p, sd->num_entries);
228 for (u32 i = 0; i < sd->num_entries; i++)
229 p = put_u64(p, sd->sizes[i]);
231 for (u32 i = 0; i < sd->num_entries; i++)
232 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
234 wimlib_assert(p - orig_p == sd->total_length);
235 p = put_zeroes(p, aligned_length - sd->total_length);
237 DEBUG("Successfully wrote security data.");
241 static void print_acl(const u8 *p, const char *type)
243 const ACL *acl = (const ACL*)p;
244 u8 revision = acl->revision;
245 u16 acl_size = le16_to_cpu(acl->acl_size);
246 u16 ace_count = le16_to_cpu(acl->ace_count);
247 printf(" [%s ACL]\n", type);
248 printf(" Revision = %u\n", revision);
249 printf(" ACL Size = %u\n", acl_size);
250 printf(" ACE Count = %u\n", ace_count);
253 for (u16 i = 0; i < ace_count; i++) {
254 const ACEHeader *hdr = (const ACEHeader*)p;
256 printf(" ACE type = %d\n", hdr->type);
257 printf(" ACE flags = 0x%x\n", hdr->flags);
258 printf(" ACE size = %u\n", hdr->size);
259 const AccessAllowedACE *aaa = (const AccessAllowedACE*)hdr;
260 printf(" ACE mask = %x\n", le32_to_cpu(aaa->mask));
261 printf(" SID start = %u\n", le32_to_cpu(aaa->sid_start));
267 static void print_sid(const u8 *p, const char *type)
269 const SID *sid = (const SID*)p;
270 printf(" [%s SID]\n", type);
271 printf(" Revision = %u\n", sid->revision);
272 printf(" Subauthority count = %u\n", sid->sub_authority_count);
273 printf(" Identifier authority = ");
274 print_byte_field(sid->identifier_authority,
275 sizeof(sid->identifier_authority));
277 for (u8 i = 0; i < sid->sub_authority_count; i++)
278 printf(" Subauthority %u = %u\n",
279 i, le32_to_cpu(sid->sub_authority[i]));
283 static void print_security_descriptor(const u8 *p, u64 size)
285 const SecurityDescriptor *sd = (const SecurityDescriptor*)p;
286 u8 revision = sd->revision;
287 u16 control = le16_to_cpu(sd->security_descriptor_control);
288 u32 owner_offset = le32_to_cpu(sd->owner_offset);
289 u32 group_offset = le32_to_cpu(sd->group_offset);
290 u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
291 u32 dacl_offset = le32_to_cpu(sd->dacl_offset);
292 printf("Revision = %u\n", revision);
293 printf("Security Descriptor Control = %#x\n", control);
294 printf("Owner offset = %u\n", owner_offset);
295 printf("Group offset = %u\n", group_offset);
296 printf("System ACL offset = %u\n", sacl_offset);
297 printf("Discretionary ACL offset = %u\n", dacl_offset);
299 if (sd->owner_offset != 0)
300 print_sid(p + owner_offset, "Owner");
301 if (sd->group_offset != 0)
302 print_sid(p + group_offset, "Group");
303 if (sd->sacl_offset != 0)
304 print_acl(p + sacl_offset, "System");
305 if (sd->dacl_offset != 0)
306 print_acl(p + dacl_offset, "Discretionary");
310 * Prints the security data for a WIM file.
312 void print_security_data(const struct wim_security_data *sd)
314 wimlib_assert(sd != NULL);
316 puts("[SECURITY DATA]");
317 printf("Length = %"PRIu32" bytes\n", sd->total_length);
318 printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
320 for (u32 i = 0; i < sd->num_entries; i++) {
321 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n",
323 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
329 void free_security_data(struct wim_security_data *sd)
332 wimlib_assert(sd->refcnt != 0);
333 if (--sd->refcnt == 0) {
334 u8 **descriptors = sd->descriptors;
335 u32 num_entries = sd->num_entries;
337 while (num_entries--)
338 FREE(*descriptors++);
340 FREE(sd->descriptors);