]> wimlib.net Git - wimlib/blob - src/security.c
Include _ntfs_valid_descr()
[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 static void print_acl(const u8 *p, const char *type)
182 {
183         ACL *acl = (ACL*)p;
184         TO_LE16(acl->acl_size);
185         TO_LE16(acl->acl_count);
186         printf("    [%s ACL]\n", type);
187         printf("    Revision = %u\n", acl->revision);
188         printf("    ACL Size = %u\n", acl->acl_size);
189         printf("    ACE Count = %u\n", acl->ace_count);
190
191         p += sizeof(ACL);
192         for (uint i = 0; i < acl->ace_count; i++) {
193                 ACEHeader *hdr = (ACEHeader*)p;
194                 printf("        [ACE]\n");
195                 printf("        ACE type  = %d\n", hdr->type);
196                 printf("        ACE flags = 0x%x\n", hdr->flags);
197                 printf("        ACE size  = %u\n", hdr->size);
198                 AccessAllowedACE *aaa = (AccessAllowedACE*)hdr;
199                 printf("        ACE mask = %x\n", to_le32(aaa->mask));
200                 printf("        SID start = %u\n", to_le32(aaa->sid_start));
201                 p += hdr->size;
202         }
203         putchar('\n');
204 }
205
206 static void print_sid(const u8 *p, const char *type)
207 {
208         SID *sid = (SID*)p;
209         printf("    [%s SID]\n", type);
210         printf("    Revision = %u\n", sid->revision);
211         printf("    Subauthority count = %u\n", sid->sub_authority_count);
212         printf("    Identifier authority = ");
213         print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority));
214         putchar('\n');
215         for (uint i = 0; i < sid->sub_authority_count; i++)
216                 printf("    Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i]));
217         putchar('\n');
218 }
219
220 static void print_security_descriptor(const u8 *p, u64 size)
221 {
222         SecurityDescriptor *sd = (SecurityDescriptor*)p;
223         TO_LE16(sd->security_descriptor_control);
224         TO_LE32(sd->owner_offset);
225         TO_LE32(sd->group_offset);
226         TO_LE32(sd->sacl_offset);
227         TO_LE32(sd->dacl_offset);
228         printf("Revision = %u\n", sd->revision);
229         printf("Security Descriptor Control = %#x\n", sd->security_descriptor_control);
230         printf("Owner offset = %u\n", sd->owner_offset);
231         printf("Group offset = %u\n", sd->group_offset);
232         printf("System ACL offset = %u\n", sd->sacl_offset);
233         printf("Discretionary ACL offset = %u\n", sd->dacl_offset);
234
235         if (sd->owner_offset != 0)
236                 print_sid(p + sd->owner_offset, "Owner");
237         if (sd->group_offset != 0)
238                 print_sid(p + sd->group_offset, "Group");
239         if (sd->sacl_offset != 0)
240                 print_acl(p + sd->sacl_offset, "System");
241         if (sd->dacl_offset != 0)
242                 print_acl(p + sd->dacl_offset, "Discretionary");
243 }
244
245 /* 
246  * Prints the security data for a WIM file.
247  */
248 void print_security_data(const struct wim_security_data *sd)
249 {
250         puts("[SECURITY DATA]");
251         printf("Length            = %"PRIu32" bytes\n", sd->total_length);
252         printf("Number of Entries = %"PRIu32"\n", sd->num_entries);
253
254         for (u32 i = 0; i < sd->num_entries; i++) {
255                 printf("[SecurityDescriptor %"PRIu32", length = %"PRIu64"]\n", 
256                        i, sd->sizes[i]);
257                 print_security_descriptor(sd->descriptors[i], sd->sizes[i]);
258                 putchar('\n');
259         }
260         putchar('\n');
261 }
262
263 void free_security_data(struct wim_security_data *sd)
264 {
265         if (!sd)
266                 return;
267         wimlib_assert(sd->refcnt != 0);
268         if (--sd->refcnt == 0) {
269                 u8 **descriptors = sd->descriptors;
270                 u32 num_entries  = sd->num_entries;
271                 if (descriptors)
272                         while (num_entries--)
273                                 FREE(*descriptors++);
274                 FREE(sd->sizes);
275                 FREE(sd->descriptors);
276                 FREE(sd);
277         }
278 }