]> wimlib.net Git - wimlib/blob - src/security.c
ec1590f6f09a7636ff9a29a929eaf0712ff71a1a
[wimlib] / src / security.c
1 /*
2  * security.c
3  *
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.
7  */
8
9 /*
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
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)
17  * any later version.
18  *
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
22  * details.
23  *
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/.
26  */
27
28 #include "wimlib_internal.h"
29 #include "io.h"
30 #include "security.h"
31
32 /*
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.
42  */
43 static void empty_sacl_fixup(char *descr, u64 *size_p)
44 {
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);
51                 }
52         }
53 }
54
55 /*
56  * Reads the security data from the metadata resource.
57  *
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
61  *                              least 8 bytes.
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.
65  *
66  * Note: There is no `offset' argument because the security data is located at
67  * the beginning of the metadata resource.
68  */
69 int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
70                        struct wim_security_data **sd_p)
71 {
72         struct wim_security_data *sd;
73         const u8 *p;
74         int ret;
75         u64 total_len;
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. */
125                 if (sd->total_length != 0 && sd->total_length != 8) {
126                         ERROR("Invalid security data length (%u): expected 0 or 8",
127                               sd->total_length);
128                         goto out_invalid_sd;
129                 }
130                 sd->total_length = 8;
131                 goto out_return_sd;
132         }
133
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);
140                 goto out_invalid_sd;
141         }
142         if (sizeof(size_t) < 8 && sizes_size > 0xffffffff) {
143                 ERROR("Too many security descriptors!");
144                 goto out_invalid_sd;
145         }
146         sd->sizes = MALLOC(sizes_size);
147         if (!sd->sizes) {
148                 ret = WIMLIB_ERR_NOMEM;
149                 goto out_free_sd;
150         }
151
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);
155
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 "
160                       "descriptors");
161                 ret = WIMLIB_ERR_NOMEM;
162                 goto out_free_sd;
163         }
164         total_len = size_no_descriptors;
165
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]);
174                         goto out_invalid_sd;
175                 }
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.
180                  * */
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);
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, 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 (uint i = 0; i < acl->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 (uint 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                 return;
333         wimlib_assert(sd->refcnt != 0);
334         if (--sd->refcnt == 0) {
335                 u8 **descriptors = sd->descriptors;
336                 u32 num_entries  = sd->num_entries;
337                 if (descriptors)
338                         while (num_entries--)
339                                 FREE(*descriptors++);
340                 FREE(sd->sizes);
341                 FREE(sd->descriptors);
342                 FREE(sd);
343         }
344 }