-
Notifications
You must be signed in to change notification settings - Fork 21
/
reparse.c
445 lines (392 loc) · 15.2 KB
/
reparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* reparse.c - Reparse point handling
*/
/*
* Copyright (C) 2012, 2013, 2015 Eric Biggers
*
* This file is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file; if not, see https://www.gnu.org/licenses/.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include "wimlib/alloca.h"
#include "wimlib/blob_table.h"
#include "wimlib/endianness.h"
#include "wimlib/encoding.h"
#include "wimlib/error.h"
#include "wimlib/guid.h"
#include "wimlib/inode.h"
#include "wimlib/reparse.h"
#include "wimlib/resource.h"
/*
* Reconstruct the header of a reparse point buffer. This is necessary because
* only reparse data is stored in WIM files. The reparse tag is instead stored
* in the on-disk WIM dentry, and the reparse data length is equal to the size
* of the blob in which the reparse data was stored, minus the size of a GUID
* (16 bytes) if the reparse tag does not have the "Microsoft" bit set.
*/
void
complete_reparse_point(struct reparse_buffer_disk *rpbuf,
const struct wim_inode *inode, u16 blob_size)
{
rpbuf->rptag = cpu_to_le32(inode->i_reparse_tag);
if (blob_size >= GUID_SIZE && !(inode->i_reparse_tag & 0x80000000))
blob_size -= GUID_SIZE;
rpbuf->rpdatalen = cpu_to_le16(blob_size);
rpbuf->rpreserved = cpu_to_le16(inode->i_rp_reserved);
}
/* Parse the buffer for a symbolic link or junction reparse point and fill in a
* 'struct link_reparse_point'. */
int
parse_link_reparse_point(const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
struct link_reparse_point *link)
{
u16 substitute_name_offset;
u16 print_name_offset;
const u8 *data;
link->rptag = le32_to_cpu(rpbuf->rptag);
/* Not a symbolic link or junction? */
if (link->rptag != WIM_IO_REPARSE_TAG_SYMLINK &&
link->rptag != WIM_IO_REPARSE_TAG_MOUNT_POINT)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
/* Is the buffer too small to be a symlink or a junction? */
if (rpbuflen < offsetof(struct reparse_buffer_disk, link.junction.data))
return WIMLIB_ERR_INVALID_REPARSE_DATA;
link->rpreserved = le16_to_cpu(rpbuf->rpreserved);
link->substitute_name_nbytes = le16_to_cpu(rpbuf->link.substitute_name_nbytes);
substitute_name_offset = le16_to_cpu(rpbuf->link.substitute_name_offset);
link->print_name_nbytes = le16_to_cpu(rpbuf->link.print_name_nbytes);
print_name_offset = le16_to_cpu(rpbuf->link.print_name_offset);
/* The names must be properly sized and aligned. */
if ((substitute_name_offset | print_name_offset |
link->substitute_name_nbytes | link->print_name_nbytes) & 1)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
if (link->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
if (rpbuflen < offsetof(struct reparse_buffer_disk, link.symlink.data))
return WIMLIB_ERR_INVALID_REPARSE_DATA;
link->symlink_flags = le32_to_cpu(rpbuf->link.symlink.flags);
data = rpbuf->link.symlink.data;
} else {
data = rpbuf->link.junction.data;
}
/* Verify that the names don't overflow the buffer. */
if ((data - (const u8 *)rpbuf) + substitute_name_offset +
link->substitute_name_nbytes > rpbuflen)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
if ((data - (const u8 *)rpbuf) + print_name_offset +
link->print_name_nbytes > rpbuflen)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
/* Save the name pointers. */
link->substitute_name = (utf16lechar *)&data[substitute_name_offset];
link->print_name = (utf16lechar *)&data[print_name_offset];
return 0;
}
/* Translate a 'struct link_reparse_point' into a reparse point buffer. */
int
make_link_reparse_point(const struct link_reparse_point *link,
struct reparse_buffer_disk *rpbuf, u16 *rpbuflen_ret)
{
u8 *data;
if (link->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
data = rpbuf->link.symlink.data;
else if (link->rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT)
data = rpbuf->link.junction.data;
else /* Callers should forbid this case, but check anyway. */
return WIMLIB_ERR_INVALID_REPARSE_DATA;
/* Check if the names are too long to fit in a reparse point. */
if ((data - (u8 *)rpbuf) + link->substitute_name_nbytes +
link->print_name_nbytes +
2 * sizeof(utf16lechar) > REPARSE_POINT_MAX_SIZE)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
rpbuf->rptag = cpu_to_le32(link->rptag);
rpbuf->rpreserved = cpu_to_le16(link->rpreserved);
rpbuf->link.substitute_name_offset = cpu_to_le16(0);
rpbuf->link.substitute_name_nbytes = cpu_to_le16(link->substitute_name_nbytes);
rpbuf->link.print_name_offset = cpu_to_le16(link->substitute_name_nbytes +
sizeof(utf16lechar));
rpbuf->link.print_name_nbytes = cpu_to_le16(link->print_name_nbytes);
if (link->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
rpbuf->link.symlink.flags = cpu_to_le32(link->symlink_flags);
/* We null-terminate the substitute and print names, although this isn't
* strictly necessary. Note that the nbytes fields do not include the
* null terminators. */
data = mempcpy(data, link->substitute_name, link->substitute_name_nbytes);
*(utf16lechar *)data = cpu_to_le16(0);
data += sizeof(utf16lechar);
data = mempcpy(data, link->print_name, link->print_name_nbytes);
*(utf16lechar *)data = cpu_to_le16(0);
data += sizeof(utf16lechar);
rpbuf->rpdatalen = cpu_to_le16(data - rpbuf->rpdata);
*rpbuflen_ret = data - (u8 *)rpbuf;
return 0;
}
/* UNIX symlink <=> Windows reparse point translation */
#ifndef _WIN32
/* Retrieve the inode's reparse point buffer into @rpbuf and @rpbuflen_ret.
* This gets the reparse data from @blob if specified, otherwise from the
* inode's reparse point stream. The inode's streams must be resolved. */
static int
wim_inode_get_reparse_point(const struct wim_inode *inode,
struct reparse_buffer_disk *rpbuf,
u16 *rpbuflen_ret,
const struct blob_descriptor *blob)
{
int ret;
u16 blob_size = 0;
if (!blob) {
const struct wim_inode_stream *strm;
strm = inode_get_unnamed_stream(inode, STREAM_TYPE_REPARSE_POINT);
if (strm)
blob = stream_blob_resolved(strm);
}
if (blob) {
if (blob->size > REPARSE_DATA_MAX_SIZE)
return WIMLIB_ERR_INVALID_REPARSE_DATA;
blob_size = blob->size;
ret = read_blob_into_buf(blob, rpbuf->rpdata);
if (ret)
return ret;
}
complete_reparse_point(rpbuf, inode, blob_size);
*rpbuflen_ret = REPARSE_DATA_OFFSET + blob_size;
return 0;
}
static void
copy(char **buf_p, size_t *bufsize_p, const char *src, size_t src_size)
{
size_t n = min(*bufsize_p, src_size);
memcpy(*buf_p, src, n);
*buf_p += n;
*bufsize_p -= n;
}
/*
* Get a UNIX-style symlink target from the WIM inode for a reparse point.
*
* @inode
* The inode from which to read the symlink. If not a symbolic link or
* junction reparse point, then -EINVAL will be returned.
* @buf
* Buffer into which to place the link target.
* @bufsize
* Available space in @buf, in bytes.
* @blob
* If not NULL, the blob from which to read the reparse data. Otherwise,
* the reparse data will be read from the reparse point stream of @inode.
* @altroot
* If @altroot_len != 0 and the link is an absolute link that was stored as
* "fixed", then prepend this path to the link target.
* @altroot_len
* Length of the @altroot string or 0.
*
* Similar to POSIX readlink(), this function writes as much of the symlink
* target as possible (up to @bufsize bytes) to @buf with no null terminator and
* returns the number of bytes written or a negative errno value on error. Note
* that the target is truncated and @bufsize is returned in the overflow case.
*/
int
wim_inode_readlink(const struct wim_inode *inode, char *buf, size_t bufsize,
const struct blob_descriptor *blob,
const char *altroot, size_t altroot_len)
{
struct reparse_buffer_disk rpbuf;
u16 rpbuflen;
struct link_reparse_point link;
char *target_buffer;
char *target;
size_t target_len;
char *buf_ptr;
bool rpfix_ok = false;
/* Not a symbolic link or junction? */
if (!inode_is_symlink(inode))
return -EINVAL;
/* Retrieve the native Windows "substitute name". */
if (wim_inode_get_reparse_point(inode, &rpbuf, &rpbuflen, blob))
return -EIO;
if (parse_link_reparse_point(&rpbuf, rpbuflen, &link))
return -EINVAL;
/* Translate the substitute name to a multibyte string. */
if (utf16le_to_tstr(link.substitute_name, link.substitute_name_nbytes,
&target_buffer, &target_len))
return -errno;
target = target_buffer;
/*
* The substitute name is a native Windows NT path. There are two cases:
*
* 1. The reparse point is a symlink (rptag=WIM_IO_REPARSE_TAG_SYMLINK)
* and SYMBOLIC_LINK_RELATIVE is set. Windows resolves the path
* relative to the directory containing the reparse point file. In
* this case, we just translate the path separators.
* 2. Otherwise, Windows resolves the path from the root of the Windows
* NT kernel object namespace. In this case, we attempt to strip the
* device name, in addition to translating the path separators; e.g.
* "\??\C:\Users\Public" is translated to "/Users/Public".
*
* Also in case (2) the link target may have been stored as "fixed",
* meaning that with the device portion stripped off it is effectively
* "relative to the root of the WIM image". If this is the case, and if
* the caller provided an alternate root directory, then rewrite the
* link to be relative to that directory.
*/
if (!link_is_relative_symlink(&link)) {
static const char *const nt_root_dirs[] = {
"\\??\\", "\\DosDevices\\", "\\Device\\",
};
for (size_t i = 0; i < ARRAY_LEN(nt_root_dirs); i++) {
size_t len = strlen(nt_root_dirs[i]);
if (!strncmp(target, nt_root_dirs[i], len)) {
char *p = target + len;
while (*p == '\\')
p++;
while (*p && *p != '\\')
p++;
target_len -= (p - target);
target = p;
break;
}
}
if (!(inode->i_rp_flags & WIM_RP_FLAG_NOT_FIXED))
rpfix_ok = true;
}
/* Translate backslashes (Windows NT path separator) to forward slashes
* (UNIX path separator). In addition, translate forwards slashes to
* backslashes; this enables lossless handling of UNIX symbolic link
* targets that contain the backslash character. */
for (char *p = target; *p; p++) {
if (*p == '\\')
*p = '/';
else if (*p == '/')
*p = '\\';
}
/* Copy as much of the link target as possible to the output buffer and
* return the number of bytes copied. */
buf_ptr = buf;
if (rpfix_ok && altroot_len != 0) {
copy(&buf_ptr, &bufsize, altroot, altroot_len);
} else if (target_len == 0) {
/* An absolute link target that was made relative to the same
* directory pointed to will end up empty if the original target
* did not have a trailing slash. Here, we are reading this
* adjusted link target without prefixing it. This usually
* doesn't happen, but if it does then we need to change it to
* "/" so that it is a valid target. */
target = "/";
target_len = 1;
}
copy(&buf_ptr, &bufsize, target, target_len);
FREE(target_buffer);
return buf_ptr - buf;
}
/* Given a UNIX-style symbolic link target, create a Windows-style reparse point
* buffer and assign it to the specified inode. */
int
wim_inode_set_symlink(struct wim_inode *inode, const char *_target,
struct blob_table *blob_table)
{
int ret;
utf16lechar *target;
size_t target_nbytes;
struct link_reparse_point link;
struct reparse_buffer_disk rpbuf;
u16 rpbuflen;
/* Translate the link target to UTF-16LE. */
ret = tstr_to_utf16le(_target, strlen(_target), &target, &target_nbytes);
if (ret)
return ret;
/* Translate forward slashes (UNIX path separator) to backslashes
* (Windows NT path separator). In addition, translate backslashes to
* forward slashes; this enables lossless handling of UNIX symbolic link
* targets that contain the backslash character. */
for (utf16lechar *p = target; *p; p++) {
if (*p == cpu_to_le16('/'))
*p = cpu_to_le16('\\');
else if (*p == cpu_to_le16('\\'))
*p = cpu_to_le16('/');
}
link.rptag = WIM_IO_REPARSE_TAG_SYMLINK;
link.rpreserved = 0;
/* Note: an absolute link that was rewritten to be relative to another
* directory is assumed to either be empty or to have a leading slash.
* See unix_relativize_link_target(). */
if (*target == cpu_to_le16('\\') || !*target) {
/*
* UNIX link target was absolute. In this case we represent the
* link as a symlink reparse point with SYMBOLIC_LINK_RELATIVE
* cleared. For this to work we need to assign it a path that
* can be resolved from the root of the Windows NT kernel object
* namespace. We do this by using "\??\C:" as a dummy prefix.
*
* Note that we could instead represent UNIX absolute links by
* setting SYMBOLIC_LINK_RELATIVE and then leaving the path
* backslash-prefixed like "\Users\Public". On Windows this is
* valid and denotes a path relative to the root of the
* filesystem on which the reparse point resides. The problem
* with this is that neither WIMGAPI nor wimlib (on Windows)
* will do "reparse point fixups" when extracting such links
* (modifying the link target to point into the actual
* extraction directory). So for the greatest cross-platform
* consistency, we have to use the fake C: drive approach.
*/
static const utf16lechar prefix[6] = {
cpu_to_le16('\\'),
cpu_to_le16('?'),
cpu_to_le16('?'),
cpu_to_le16('\\'),
cpu_to_le16('C'),
cpu_to_le16(':'),
};
/* Do not show \??\ in print name */
const size_t num_unprintable_chars = 4;
link.symlink_flags = 0;
link.substitute_name_nbytes = sizeof(prefix) + target_nbytes;
link.substitute_name = alloca(link.substitute_name_nbytes);
memcpy(link.substitute_name, prefix, sizeof(prefix));
memcpy(link.substitute_name + ARRAY_LEN(prefix), target, target_nbytes);
link.print_name_nbytes = link.substitute_name_nbytes -
(num_unprintable_chars * sizeof(utf16lechar));
link.print_name = link.substitute_name + num_unprintable_chars;
} else {
/* UNIX link target was relative. In this case we represent the
* link as a symlink reparse point with SYMBOLIC_LINK_RELATIVE
* set. This causes Windows to interpret the link relative to
* the directory containing the reparse point file. */
link.symlink_flags = SYMBOLIC_LINK_RELATIVE;
link.substitute_name_nbytes = target_nbytes;
link.substitute_name = target;
link.print_name_nbytes = target_nbytes;
link.print_name = target;
}
/* Generate the reparse buffer. */
ret = make_link_reparse_point(&link, &rpbuf, &rpbuflen);
if (ret)
goto out_free_target;
/* Save the reparse data with the inode. */
ret = WIMLIB_ERR_NOMEM;
if (!inode_add_stream_with_data(inode,
STREAM_TYPE_REPARSE_POINT,
NO_STREAM_NAME,
rpbuf.rpdata,
rpbuflen - REPARSE_DATA_OFFSET,
blob_table))
goto out_free_target;
/* The inode is now a reparse point. */
inode->i_reparse_tag = link.rptag;
inode->i_attributes &= ~FILE_ATTRIBUTE_NORMAL;
inode->i_attributes |= FILE_ATTRIBUTE_REPARSE_POINT;
ret = 0;
out_free_target:
FREE(target);
return ret;
}
#endif /* !_WIN32 */