]> wimlib.net Git - wimlib/blob - src/security.c
to_leXX() -> cpu_to_leXX(), leXX_to_cpu()
[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  his 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         sd = MALLOC(sizeof(struct wim_security_data));
78         if (!sd) {
79                 ERROR("Out of memory");
80                 return WIMLIB_ERR_NOMEM;
81         }
82         sd->sizes       = NULL;
83         sd->descriptors = NULL;
84         sd->refcnt      = 1;
85
86         p = metadata_resource;
87         p = get_u32(p, &sd->total_length);
88         p = get_u32(p, (u32*)&sd->num_entries);
89
90         if (sd->num_entries > 0x7fffffff) {
91                 ERROR("Security data has too many entries!");
92                 ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
93                 goto out_free_sd;
94         }
95
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.
99          *
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;
109                 goto out_free_sd;
110         }
111
112         DEBUG("Reading security data: %u entries, length = %u",
113               sd->num_entries, sd->total_length);
114
115         if (sd->num_entries == 0) {
116                 /* No security data. */
117                 total_len = 8;
118                 goto out;
119         }
120
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;
128                 goto out_free_sd;
129         }
130         sd->sizes = MALLOC(sizes_size);
131         if (!sd->sizes) {
132                 ret = WIMLIB_ERR_NOMEM;
133                 goto out_free_sd;
134         }
135
136         /* Copy the sizes array in from the file data. */
137         p = get_bytes(p, sizes_size, sd->sizes);
138         array_le64_to_cpu(sd->sizes, sd->num_entries);
139
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 "
144                       "descriptors");
145                 ret = WIMLIB_ERR_NOMEM;
146                 goto out_free_sd;
147         }
148         total_len = size_no_descriptors;
149
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;
159                         goto out_free_sd;
160                 }
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;
167                         goto out_free_sd;
168                 }
169                 sd->descriptors[i] = MALLOC(sd->sizes[i]);
170                 if (!sd->descriptors[i]) {
171                         ERROR("Out of memory while allocating security "
172                               "descriptors");
173                         ret = WIMLIB_ERR_NOMEM;
174                         goto out_free_sd;
175                 }
176                 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
177                 empty_sacl_fixup(sd->descriptors[i], &sd->sizes[i]);
178         }
179 out:
180         sd->total_length = (u32)total_len;
181         *sd_p = sd;
182         return 0;
183 out_free_sd:
184         free_security_data(sd);
185         return ret;
186 }
187
188 /* 
189  * Writes security data to an in-memory buffer.
190  */
191 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
192 {
193         DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
194               "= %"PRIu32")", sd->total_length, sd->num_entries);
195
196         u32 aligned_length = (sd->total_length + 7) & ~7;
197
198         u8 *orig_p = p;
199         p = put_u32(p, aligned_length);
200         p = put_u32(p, sd->num_entries);
201
202         for (u32 i = 0; i < sd->num_entries; i++)
203                 p = put_u64(p, sd->sizes[i]);
204
205         for (u32 i = 0; i < sd->num_entries; i++)
206                 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
207
208         wimlib_assert(p - orig_p == sd->total_length);
209         p = put_zeroes(p, aligned_length - sd->total_length);
210
211         DEBUG("Successfully wrote security data.");
212         return p;
213 }
214
215 static void print_acl(const u8 *p, const char *type)
216 {
217         const ACL *acl = (const ACL*)p;
218         u8 revision = acl->revision;
219         u16 acl_size = le16_to_cpu(acl->acl_size);
220         u16 ace_count = le16_to_cpu(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);
225
226         p += sizeof(ACL);
227         for (uint i = 0; i < acl->ace_count; i++) {
228                 const ACEHeader *hdr = (const ACEHeader*)p;
229                 printf("        [ACE]\n");
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", le32_to_cpu(aaa->mask));
235                 printf("        SID start = %u\n", le32_to_cpu(aaa->sid_start));
236                 p += hdr->size;
237         }
238         putchar('\n');
239 }
240
241 static void print_sid(const u8 *p, const char *type)
242 {
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,
249                          sizeof(sid->identifier_authority));
250         putchar('\n');
251         for (uint i = 0; i < sid->sub_authority_count; i++)
252                 printf("    Subauthority %u = %u\n",
253                        i, le32_to_cpu(sid->sub_authority[i]));
254         putchar('\n');
255 }
256
257 static void print_security_descriptor(const u8 *p, u64 size)
258 {
259         const SecurityDescriptor *sd = (const SecurityDescriptor*)p;
260         u8 revision      = sd->revision;
261         u16 control      = le16_to_cpu(sd->security_descriptor_control);
262         u32 owner_offset = le32_to_cpu(sd->owner_offset);
263         u32 group_offset = le32_to_cpu(sd->group_offset);
264         u32 sacl_offset  = le32_to_cpu(sd->sacl_offset);
265         u32 dacl_offset  = le32_to_cpu(sd->dacl_offset);
266         printf("Revision = %u\n", revision);
267         printf("Security Descriptor Control = %#x\n", control);
268         printf("Owner offset = %u\n", owner_offset);
269         printf("Group offset = %u\n", group_offset);
270         printf("System ACL offset = %u\n", sacl_offset);
271         printf("Discretionary ACL offset = %u\n", dacl_offset);
272
273         if (sd->owner_offset != 0)
274                 print_sid(p + owner_offset, "Owner");
275         if (sd->group_offset != 0)
276                 print_sid(p + group_offset, "Group");
277         if (sd->sacl_offset != 0)
278                 print_acl(p + sacl_offset, "System");
279         if (sd->dacl_offset != 0)
280                 print_acl(p + dacl_offset, "Discretionary");
281 }
282
283 /* 
284  * Prints the security data for a WIM file.
285  */
286 void print_security_data(const struct wim_security_data *sd)
287 {
288         puts("[SECURITY DATA]");
289         printf("Length            = %"PRIu32" bytes\n", sd->total_length);
290         printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
291
292         for (u32 i = 0; i < sd->num_entries; i++) {
293                 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n", 
294                        i, sd->sizes[i]);
295                 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
296                 putchar('\n');
297         }
298         putchar('\n');
299 }
300
301 void free_security_data(struct wim_security_data *sd)
302 {
303         if (!sd)
304                 return;
305         wimlib_assert(sd->refcnt != 0);
306         if (--sd->refcnt == 0) {
307                 u8 **descriptors = sd->descriptors;
308                 u32 num_entries  = sd->num_entries;
309                 if (descriptors)
310                         while (num_entries--)
311                                 FREE(*descriptors++);
312                 FREE(sd->sizes);
313                 FREE(sd->descriptors);
314                 FREE(sd);
315         }
316 }