]> wimlib.net Git - wimlib/blob - src/security.c
select_wim_image(): Set WIMLIB_NO_IMAGE on failure
[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 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 #include "wimlib_internal.h"
27 #include "buffer_io.h"
28 #include "security.h"
29
30 /*
31  * This is a hack to work around a problem in libntfs-3g.  libntfs-3g validates
32  * security descriptors with a function named ntfs_valid_descr().
33  * ntfs_valid_descr() considers a security descriptor that ends in a SACL
34  * (Sysetm Access Control List) with no ACE's (Access Control Entries) to be
35  * invalid.  However, a security descriptor like this exists in the Windows 7
36  * install.wim.  Here, security descriptors matching this pattern are modified
37  * to have no SACL.  This should make no difference since the SACL had no
38  * entries anyway; however this ensures that that the security descriptors pass
39  * the validation in libntfs-3g.
40  */
41 static void empty_sacl_fixup(u8 *descr, u64 *size_p)
42 {
43         if (*size_p >= sizeof(SecurityDescriptor)) {
44                 SecurityDescriptor *sd = (SecurityDescriptor*)descr;
45                 u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
46                 if (sacl_offset == *size_p - sizeof(ACL)) {
47                         sd->sacl_offset = cpu_to_le32(0);
48                         *size_p -= sizeof(ACL);
49                 }
50         }
51 }
52
53 /*
54  * Reads the security data from the metadata resource.
55  *
56  * @metadata_resource:  An array that contains the uncompressed metadata
57  *                              resource for the WIM file.
58  * @metadata_resource_len:      The length of @metadata_resource.  It must be at
59  *                              least 8 bytes.
60  * @sd_p:       A pointer to a pointer to a wim_security_data structure that
61  *              will be filled in with a pointer to a new wim_security_data
62  *              structure on success.
63  *
64  * Note: There is no `offset' argument because the security data is located at
65  * the beginning of the metadata resource.
66  */
67 int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
68                        struct wim_security_data **sd_p)
69 {
70         struct wim_security_data *sd;
71         const u8 *p;
72         int ret;
73         u64 total_len;
74
75         wimlib_assert(metadata_resource_len >= 8);
76
77         /*
78          * Sorry this function is excessively complicated--- I'm just being
79          * extremely careful about integer overflows.
80          */
81
82         sd = MALLOC(sizeof(struct wim_security_data));
83         if (!sd) {
84                 ERROR("Out of memory");
85                 return WIMLIB_ERR_NOMEM;
86         }
87         sd->sizes       = NULL;
88         sd->descriptors = NULL;
89         sd->refcnt      = 1;
90
91         p = metadata_resource;
92         p = get_u32(p, &sd->total_length);
93         p = get_u32(p, (u32*)&sd->num_entries);
94
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!");
102                 goto out_invalid_sd;
103         }
104
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.
108          *
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);
117                 goto out_invalid_sd;
118         }
119
120         DEBUG("Reading security data: %u entries, length = %u",
121               sd->num_entries, sd->total_length);
122
123         if (sd->num_entries == 0) {
124                 /* No security descriptors.  We allow the total_length field to
125                  * be either 8 (which is correct, since there are always 2
126                  * 32-bit integers) or 0. */
127                 if (sd->total_length != 0 && sd->total_length != 8) {
128                         ERROR("Invalid security data length (%u): expected 0 or 8",
129                               sd->total_length);
130                         goto out_invalid_sd;
131                 }
132                 sd->total_length = 8;
133                 goto out_return_sd;
134         }
135
136         u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
137         u64 size_no_descriptors = 8 + sizes_size;
138         if (size_no_descriptors > (u64)sd->total_length) {
139                 ERROR("Security data total length of %u is too short because "
140                       "there seem to be at least %"PRIu64" bytes of security data",
141                       sd->total_length, 8 + sizes_size);
142                 goto out_invalid_sd;
143         }
144
145         sd->sizes = MALLOC(sizes_size);
146         if (!sd->sizes) {
147                 ret = WIMLIB_ERR_NOMEM;
148                 goto out_free_sd;
149         }
150
151         /* Copy the sizes array in from the file data. */
152         p = get_bytes(p, sizes_size, sd->sizes);
153         array_le64_to_cpu(sd->sizes, sd->num_entries);
154
155         /* Allocate the array of pointers to descriptors, and read them in. */
156         sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
157         if (!sd->descriptors) {
158                 ERROR("Out of memory while allocating security "
159                       "descriptors");
160                 ret = WIMLIB_ERR_NOMEM;
161                 goto out_free_sd;
162         }
163         total_len = size_no_descriptors;
164
165         for (u32 i = 0; i < sd->num_entries; i++) {
166                 /* Watch out for huge security descriptor sizes that could
167                  * overflow the total length and wrap it around. */
168                 if (total_len + sd->sizes[i] < total_len) {
169                         ERROR("Caught overflow in security descriptor lengths "
170                               "(current total length = %"PRIu64", security "
171                               "descriptor size = %"PRIu64")",
172                               total_len, sd->sizes[i]);
173                         goto out_invalid_sd;
174                 }
175                 total_len += sd->sizes[i];
176                 /* This check ensures that the descriptor size fits in a 32 bit
177                  * integer.  Because if it didn't, the total length would come
178                  * out bigger than sd->total_length, which is a 32 bit integer.
179                  * */
180                 if (total_len > (u64)sd->total_length) {
181                         ERROR("Security data total length of %u is too short "
182                               "because there seem to be at least %"PRIu64" "
183                               "bytes of security data",
184                               sd->total_length, total_len);
185                         goto out_invalid_sd;
186                 }
187                 sd->descriptors[i] = MALLOC(sd->sizes[i]);
188                 if (!sd->descriptors[i]) {
189                         ERROR("Out of memory while allocating security "
190                               "descriptors");
191                         ret = WIMLIB_ERR_NOMEM;
192                         goto out_free_sd;
193                 }
194                 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
195                 empty_sacl_fixup(sd->descriptors[i], &sd->sizes[i]);
196         }
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);
201                 goto out_invalid_sd;
202         }
203         sd->total_length = total_len;
204 out_return_sd:
205         *sd_p = sd;
206         return 0;
207 out_invalid_sd:
208         ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
209 out_free_sd:
210         free_security_data(sd);
211         return ret;
212 }
213
214 /*
215  * Writes security data to an in-memory buffer.
216  */
217 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
218 {
219         DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
220               "= %"PRIu32")", sd->total_length, sd->num_entries);
221
222         u32 aligned_length = (sd->total_length + 7) & ~7;
223
224         u8 *orig_p = p;
225         p = put_u32(p, aligned_length);
226         p = put_u32(p, sd->num_entries);
227
228         for (u32 i = 0; i < sd->num_entries; i++)
229                 p = put_u64(p, sd->sizes[i]);
230
231         for (u32 i = 0; i < sd->num_entries; i++)
232                 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
233
234         wimlib_assert(p - orig_p == sd->total_length);
235         p = put_zeroes(p, aligned_length - sd->total_length);
236
237         DEBUG("Successfully wrote security data.");
238         return p;
239 }
240
241 static void print_acl(const u8 *p, const char *type)
242 {
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);
251
252         p += sizeof(ACL);
253         for (u16 i = 0; i < ace_count; i++) {
254                 const ACEHeader *hdr = (const ACEHeader*)p;
255                 printf("        [ACE]\n");
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));
262                 p += hdr->size;
263         }
264         putchar('\n');
265 }
266
267 static void print_sid(const u8 *p, const char *type)
268 {
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));
276         putchar('\n');
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]));
280         putchar('\n');
281 }
282
283 static void print_security_descriptor(const u8 *p, u64 size)
284 {
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);
298
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");
307 }
308
309 /*
310  * Prints the security data for a WIM file.
311  */
312 void print_security_data(const struct wim_security_data *sd)
313 {
314         wimlib_assert(sd != NULL);
315
316         puts("[SECURITY DATA]");
317         printf("Length            = %"PRIu32" bytes\n", sd->total_length);
318         printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
319
320         for (u32 i = 0; i < sd->num_entries; i++) {
321                 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n",
322                        i, sd->sizes[i]);
323                 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
324                 putchar('\n');
325         }
326         putchar('\n');
327 }
328
329 void free_security_data(struct wim_security_data *sd)
330 {
331         if (sd) {
332                 wimlib_assert(sd->refcnt != 0);
333                 if (--sd->refcnt == 0) {
334                         u8 **descriptors = sd->descriptors;
335                         u32 num_entries  = sd->num_entries;
336                         if (descriptors)
337                                 while (num_entries--)
338                                         FREE(*descriptors++);
339                         FREE(sd->sizes);
340                         FREE(sd->descriptors);
341                         FREE(sd);
342                 }
343         }
344 }