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"
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 his ensures that that the security descriptors pass
41 * the validation in libntfs-3g.
43 static void empty_sacl_fixup(char *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 = 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;
77 sd = MALLOC(sizeof(struct wim_security_data));
79 ERROR("Out of memory");
80 return WIMLIB_ERR_NOMEM;
83 sd->descriptors = NULL;
86 p = metadata_resource;
87 p = get_u32(p, &sd->total_length);
88 p = get_u32(p, (u32*)&sd->num_entries);
90 if (sd->num_entries > 0x7fffffff) {
91 ERROR("Security data has too many entries!");
92 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
96 /* Verify the listed total length of the security data is big enough to
97 * include the sizes array, verify that the file data is big enough to
98 * include it as well, then allocate the array of sizes.
100 * Note: The total length of the security data must fit in a 32-bit
101 * integer, even though each security descriptor size is a 64-bit
102 * integer. This is stupid, and we need to be careful not to actually
103 * let the security descriptor sizes be over 0xffffffff. */
104 if ((u64)sd->total_length > metadata_resource_len) {
105 ERROR("Security data total length (%u) is bigger than the "
106 "metadata resource length (%"PRIu64")",
107 sd->total_length, metadata_resource_len);
108 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
112 DEBUG("Reading security data: %u entries, length = %u",
113 sd->num_entries, sd->total_length);
115 if (sd->num_entries == 0) {
116 /* No security data. */
121 u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
122 u64 size_no_descriptors = 8 + sizes_size;
123 if (size_no_descriptors > (u64)sd->total_length) {
124 ERROR("Security data total length of %u is too short because "
125 "there must be at least %"PRIu64" bytes of security data",
126 sd->total_length, 8 + sizes_size);
127 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
130 sd->sizes = MALLOC(sizes_size);
132 ret = WIMLIB_ERR_NOMEM;
136 /* Copy the sizes array in from the file data. */
137 p = get_bytes(p, sizes_size, sd->sizes);
138 array_to_le64(sd->sizes, sd->num_entries);
140 /* Allocate the array of pointers to descriptors, and read them in. */
141 sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
142 if (!sd->descriptors) {
143 ERROR("Out of memory while allocating security "
145 ret = WIMLIB_ERR_NOMEM;
148 total_len = size_no_descriptors;
150 for (u32 i = 0; i < sd->num_entries; i++) {
151 /* Watch out for huge security descriptor sizes that could
152 * overflow the total length and wrap it around. */
153 if (total_len + sd->sizes[i] < total_len) {
154 ERROR("Caught overflow in security descriptor lengths "
155 "(current total length = %"PRIu64", security "
156 "descriptor size = %"PRIu64")",
157 total_len, sd->sizes[i]);
158 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
161 total_len += sd->sizes[i];
162 if (total_len > (u64)sd->total_length) {
163 ERROR("Security data total length of %u is too short "
164 "because there are at least %"PRIu64" bytes of "
165 "security data", sd->total_length, total_len);
166 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
169 sd->descriptors[i] = MALLOC(sd->sizes[i]);
170 if (!sd->descriptors[i]) {
171 ERROR("Out of memory while allocating security "
173 ret = WIMLIB_ERR_NOMEM;
176 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
177 empty_sacl_fixup(sd->descriptors[i], &sd->sizes[i]);
180 sd->total_length = (u32)total_len;
184 free_security_data(sd);
189 * Writes security data to an in-memory buffer.
191 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
193 DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
194 "= %"PRIu32")", sd->total_length, sd->num_entries);
196 u32 aligned_length = (sd->total_length + 7) & ~7;
199 p = put_u32(p, aligned_length);
200 p = put_u32(p, sd->num_entries);
202 for (u32 i = 0; i < sd->num_entries; i++)
203 p = put_u64(p, sd->sizes[i]);
205 for (u32 i = 0; i < sd->num_entries; i++)
206 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
208 wimlib_assert(p - orig_p == sd->total_length);
209 p = put_zeroes(p, aligned_length - sd->total_length);
211 DEBUG("Successfully wrote security data.");
215 static void print_acl(const u8 *p, const char *type)
217 const ACL *acl = (const ACL*)p;
218 u8 revision = acl->revision;
219 u16 acl_size = to_le16(acl->acl_size);
220 u16 ace_count = to_le16(acl->ace_count);
221 printf(" [%s ACL]\n", type);
222 printf(" Revision = %u\n", revision);
223 printf(" ACL Size = %u\n", acl_size);
224 printf(" ACE Count = %u\n", ace_count);
227 for (uint i = 0; i < acl->ace_count; i++) {
228 const ACEHeader *hdr = (const ACEHeader*)p;
230 printf(" ACE type = %d\n", hdr->type);
231 printf(" ACE flags = 0x%x\n", hdr->flags);
232 printf(" ACE size = %u\n", hdr->size);
233 const AccessAllowedACE *aaa = (const AccessAllowedACE*)hdr;
234 printf(" ACE mask = %x\n", to_le32(aaa->mask));
235 printf(" SID start = %u\n", to_le32(aaa->sid_start));
241 static void print_sid(const u8 *p, const char *type)
243 const SID *sid = (const SID*)p;
244 printf(" [%s SID]\n", type);
245 printf(" Revision = %u\n", sid->revision);
246 printf(" Subauthority count = %u\n", sid->sub_authority_count);
247 printf(" Identifier authority = ");
248 print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority));
250 for (uint i = 0; i < sid->sub_authority_count; i++)
251 printf(" Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i]));
255 static void print_security_descriptor(const u8 *p, u64 size)
257 const SecurityDescriptor *sd = (const SecurityDescriptor*)p;
258 u8 revision = sd->revision;
259 u16 control = to_le16(sd->security_descriptor_control);
260 u32 owner_offset = to_le32(sd->owner_offset);
261 u32 group_offset = to_le32(sd->group_offset);
262 u32 sacl_offset = to_le32(sd->sacl_offset);
263 u32 dacl_offset = to_le32(sd->dacl_offset);
264 printf("Revision = %u\n", revision);
265 printf("Security Descriptor Control = %#x\n", control);
266 printf("Owner offset = %u\n", owner_offset);
267 printf("Group offset = %u\n", group_offset);
268 printf("System ACL offset = %u\n", sacl_offset);
269 printf("Discretionary ACL offset = %u\n", dacl_offset);
271 if (sd->owner_offset != 0)
272 print_sid(p + owner_offset, "Owner");
273 if (sd->group_offset != 0)
274 print_sid(p + group_offset, "Group");
275 if (sd->sacl_offset != 0)
276 print_acl(p + sacl_offset, "System");
277 if (sd->dacl_offset != 0)
278 print_acl(p + dacl_offset, "Discretionary");
282 * Prints the security data for a WIM file.
284 void print_security_data(const struct wim_security_data *sd)
286 puts("[SECURITY DATA]");
287 printf("Length = %"PRIu32" bytes\n", sd->total_length);
288 printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
290 for (u32 i = 0; i < sd->num_entries; i++) {
291 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n",
293 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
299 void free_security_data(struct wim_security_data *sd)
303 wimlib_assert(sd->refcnt != 0);
304 if (--sd->refcnt == 0) {
305 u8 **descriptors = sd->descriptors;
306 u32 num_entries = sd->num_entries;
308 while (num_entries--)
309 FREE(*descriptors++);
311 FREE(sd->descriptors);