]> wimlib.net Git - wimlib/blob - src/security.c
More timestamp changes: Set timestamp on extracted files
[wimlib] / src / security.c
1 /*
2  * security.c
3  *
4  * Read the security data from the WIM.  Doing anything with the security data
5  * is not yet implemented other than printing some information about it.
6  */
7
8 /*
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free
15  * Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "io.h"
29 #include "security.h"
30
31 /* 
32  * Reads the security data from the metadata resource.
33  *
34  * @metadata_resource:  An array that contains the uncompressed metadata
35  *                              resource for the WIM file.
36  * @metadata_resource_len:      The length of @metadata_resource.  It MUST be at
37  *                              least 8 bytes.
38  * @sd_p:       A pointer to a pointer to a wim_security_data structure that
39  *              will be filled in with a pointer to a new wim_security_data
40  *              structure on success.
41  *
42  * Note: There is no `offset' argument because the security data is located at
43  * the beginning of the metadata resource.
44  */
45 int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
46                        struct wim_security_data **sd_p)
47 {
48         struct wim_security_data *sd;
49         const u8 *p;
50         int ret;
51         u64 total_len;
52
53         sd = MALLOC(sizeof(struct wim_security_data));
54         if (!sd) {
55                 ERROR("Out of memory");
56                 return WIMLIB_ERR_NOMEM;
57         }
58         sd->sizes       = NULL;
59         sd->descriptors = NULL;
60         sd->refcnt      = 1;
61
62         p = metadata_resource;
63         p = get_u32(p, &sd->total_length);
64         p = get_u32(p, &sd->num_entries);
65
66         /* Verify the listed total length of the security data is big enough to
67          * include the sizes array, verify that the file data is big enough to
68          * include it as well, then allocate the array of sizes.
69          *
70          * Note: The total length of the security data must fit in a 32-bit
71          * integer, even though each security descriptor size is a 64-bit
72          * integer.  This is stupid, and we need to be careful not to actually
73          * let the security descriptor sizes be over 0xffffffff.  */
74         if ((u64)sd->total_length > metadata_resource_len) {
75                 ERROR("Security data total length (%u) is bigger than the "
76                       "metadata resource length (%"PRIu64")",
77                       sd->total_length, metadata_resource_len);
78                 ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
79                 goto out_free_sd;
80         }
81
82         DEBUG("Reading security data: %u entries, length = %u",
83               sd->num_entries, sd->total_length);
84
85         if (sd->num_entries == 0) {
86                 /* No security data. */
87                 total_len = 8;
88                 goto out;
89         }
90
91         u64 sizes_size = (u64)sd->num_entries * sizeof(u64);
92         u64 size_no_descriptors = 8 + sizes_size;
93         if (size_no_descriptors > (u64)sd->total_length) {
94                 ERROR("Security data total length of %u is too short because "
95                       "there must be at least %"PRIu64" bytes of security data",
96                       sd->total_length, 8 + sizes_size);
97                 ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
98                 goto out_free_sd;
99         }
100         sd->sizes = MALLOC(sizes_size);
101         if (!sd->sizes) {
102                 ret = WIMLIB_ERR_NOMEM;
103                 goto out_free_sd;
104         }
105
106         /* Copy the sizes array in from the file data. */
107         p = get_bytes(p, sizes_size, sd->sizes);
108         array_to_le64(sd->sizes, sd->num_entries);
109
110         /* Allocate the array of pointers to descriptors, and read them in. */
111         sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
112         if (!sd->descriptors) {
113                 ERROR("Out of memory while allocating security "
114                       "descriptors");
115                 ret = WIMLIB_ERR_NOMEM;
116                 goto out_free_sd;
117         }
118         total_len = size_no_descriptors;
119
120         for (u32 i = 0; i < sd->num_entries; i++) {
121                 /* Watch out for huge security descriptor sizes that could
122                  * overflow the total length and wrap it around. */
123                 if (total_len + sd->sizes[i] < total_len) {
124                         ERROR("Caught overflow in security descriptor lengths "
125                               "(current total length = %"PRIu64", security "
126                               "descriptor size = %"PRIu64")",
127                               total_len, sd->sizes[i]);
128                         ret = WIMLIB_ERR_INVALID_SECURITY_DATA;
129                         goto out_free_sd;
130                 }
131                 total_len += sd->sizes[i];
132                 if (total_len > (u64)sd->total_length) {
133                         ERROR("Security data total length of %u is too short "
134                               "because there are at least %"PRIu64" bytes of "
135                               "security data", sd->total_length, total_len);
136                         ret = WIMLIB_ERR_INVALID_RESOURCE_SIZE;
137                         goto out_free_sd;
138                 }
139                 sd->descriptors[i] = MALLOC(sd->sizes[i]);
140                 if (!sd->descriptors[i]) {
141                         ERROR("Out of memory while allocating security "
142                               "descriptors");
143                         ret = WIMLIB_ERR_NOMEM;
144                         goto out_free_sd;
145                 }
146                 p = get_bytes(p, sd->sizes[i], sd->descriptors[i]);
147         }
148 out:
149         sd->total_length = (u32)total_len;
150         *sd_p = sd;
151         return 0;
152 out_free_sd:
153         free_security_data(sd);
154         return ret;
155 }
156
157 /* 
158  * Writes security data to an in-memory buffer.
159  */
160 u8 *write_security_data(const struct wim_security_data *sd, u8 *p)
161 {
162         DEBUG("Writing security data (total_length = %"PRIu32", num_entries "
163               "= %"PRIu32")", sd->total_length, sd->num_entries);
164
165         u8 *orig_p = p;
166         p = put_u32(p, sd->total_length);
167         p = put_u32(p, sd->num_entries);
168
169         for (u32 i = 0; i < sd->num_entries; i++)
170                 p = put_u64(p, sd->sizes[i]);
171
172         for (u32 i = 0; i < sd->num_entries; i++)
173                 p = put_bytes(p, sd->sizes[i], sd->descriptors[i]);
174
175         wimlib_assert(p - orig_p == sd->total_length);
176
177         DEBUG("Successfully wrote security data.");
178         return p;
179 }
180
181 /* XXX We don't actually do anything with the ACL's yet besides being able to
182  * print a few things.  It seems it would be a lot of work to have comprehensive
183  * support for all the weird flags and stuff, and Windows PE seems to be okay
184  * running from a WIM file that doesn't have any security data at all...  */
185
186 static void print_acl(const u8 *p)
187 {
188         ACL *acl = (ACL*)p;
189         TO_LE16(acl->acl_size);
190         TO_LE16(acl->acl_count);
191         printf("    [ACL]\n");
192         printf("    Revision = %u\n", acl->revision);
193         printf("    ACL Size = %u\n", acl->acl_size);
194         printf("    ACE Count = %u\n", acl->ace_count);
195
196         p += sizeof(ACL);
197         for (uint i = 0; i < acl->ace_count; i++) {
198                 ACEHeader *hdr = (ACEHeader*)p;
199                 printf("        [ACE]\n");
200                 printf("        ACE type  = %d\n", hdr->type);
201                 printf("        ACE flags = 0x%x\n", hdr->flags);
202                 printf("        ACE size  = %u\n", hdr->size);
203                 AccessAllowedACE *aaa = (AccessAllowedACE*)hdr;
204                 printf("        ACE mask = %x\n", to_le32(aaa->mask));
205                 printf("        SID start = %u\n", to_le32(aaa->sid_start));
206                 p += hdr->size;
207         }
208 }
209
210 static void print_sid(const u8 *p)
211 {
212         SID *sid = (SID*)p;
213         printf("    [SID]\n");
214         printf("    Revision = %u\n", sid->revision);
215         printf("    Subauthority count = %u\n", sid->sub_authority_count);
216         printf("    Identifier authority = ");
217         print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority));
218         putchar('\n');
219         for (uint i = 0; i < sid->sub_authority_count; i++)
220                 printf("    Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i]));
221 }
222
223 static void print_security_descriptor(const u8 *p, u64 size)
224 {
225         SecurityDescriptor *sd = (SecurityDescriptor*)p;
226         TO_LE16(sd->security_descriptor_control);
227         TO_LE32(sd->owner_offset);
228         TO_LE32(sd->group_offset);
229         TO_LE32(sd->sacl_offset);
230         TO_LE32(sd->dacl_offset);
231         printf("Revision = %u\n", sd->revision);
232         printf("Security Descriptor Control = %u\n", sd->security_descriptor_control);
233         printf("Owner offset = %u\n", sd->owner_offset);
234         printf("Group offset = %u\n", sd->group_offset);
235         printf("System ACL offset = %u\n", sd->sacl_offset);
236         printf("Discretionary ACL offset = %u\n", sd->dacl_offset);
237
238         if (sd->owner_offset != 0)
239                 print_sid(p + sd->owner_offset);
240         if (sd->group_offset != 0)
241                 print_sid(p + sd->group_offset);
242         if (sd->sacl_offset != 0)
243                 print_acl(p + sd->sacl_offset);
244         if (sd->dacl_offset != 0)
245                 print_acl(p + sd->dacl_offset);
246 }
247
248 /* 
249  * Prints the security data for a WIM file.
250  */
251 void print_security_data(const struct wim_security_data *sd)
252 {
253         puts("[SECURITY DATA]");
254         printf("Length            = %"PRIu32" bytes\n", sd->total_length);
255         printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
256
257         for (u32 i = 0; i < sd->num_entries; i++) {
258                 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n", 
259                        i, sd->sizes[i]);
260                 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
261                 putchar('\n');
262         }
263         putchar('\n');
264 }
265
266 void free_security_data(struct wim_security_data *sd)
267 {
268         if (!sd)
269                 return;
270         wimlib_assert(sd->refcnt != 0);
271         if (--sd->refcnt == 0) {
272                 u8 **descriptors = sd->descriptors;
273                 u32 num_entries  = sd->num_entries;
274                 if (descriptors)
275                         while (num_entries--)
276                                 FREE(*descriptors++);
277                 FREE(sd->sizes);
278                 FREE(sd->descriptors);
279                 FREE(sd);
280         }
281 }