1 /* vi: set sw=4 ts=4: */
2 /* Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 * In privileged mode if uname and gname map to a uid and gid then use the
6 * mapped value instead of the uid/gid values in tar header
9 * GNU tar and star man pages,
10 * Opengroup's ustar interchange format,
11 * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
15 #include "bb_archive.h"
17 typedef uint32_t aliased_uint32_t FIX_ALIASING;
18 typedef off_t aliased_off_t FIX_ALIASING;
20 /* NB: _DESTROYS_ str[len] character! */
21 static unsigned long long getOctal(char *str, int len)
25 /* NB: leading spaces are allowed. Using strtoull to handle that.
26 * The downside is that we accept e.g. "-123" too :(
29 v = strtoull(str, &end, 8);
30 /* std: "Each numeric field is terminated by one or more
31 * <space> or NUL characters". We must support ' '! */
32 if (*end != '\0' && *end != ' ') {
33 int8_t first = str[0];
35 bb_error_msg_and_die("corrupted octal value in tar header");
37 * GNU tar uses "base-256 encoding" for very large numbers.
38 * Encoding is binary, with highest bit always set as a marker
39 * and sign in next-highest bit:
41 * bf ff .. ff - largest positive number
42 * ff ff .. ff - minus 1
43 * c0 00 .. 00 - smallest negative number
45 * Example of tar file with 8914993153 (0x213600001) byte file.
46 * Field starts at offset 7c:
47 * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
48 * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
50 * NB: tarballs with NEGATIVE unix times encoded that way were seen!
52 /* Sign-extend 7bit 'first' to 64bit 'v' (that is, using 6th bit as sign): */
54 first >>= 1; /* now 7th bit = 6th bit */
55 v = first; /* sign-extend 8 bits to 64 */
57 v = (v << 8) + (uint8_t) *++str;
61 #define GET_OCTAL(a) getOctal((a), sizeof(a))
63 /* "global" is 0 or 1 */
64 static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int global)
69 blk_sz = (sz + 511) & (~511);
70 p = buf = xmalloc(blk_sz + 1);
71 xread(archive_handle->src_fd, buf, blk_sz);
72 archive_handle->offset += blk_sz;
74 /* prevent bb_strtou from running off the buffer */
81 /* Every record has this format: "LEN NAME=VALUE\n" */
82 len = bb_strtou(p, &end, 10);
83 /* expect errno to be EINVAL, because the character
84 * following the digits should be a space
89 /** (int)sz < 0 - not good enough for huge malicious VALUE of 2^32-1 */
90 (int)(sz|len) < 0 /* this works */
95 bb_error_msg("malformed extended header, skipped");
96 // More verbose version:
97 //bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
98 // archive_handle->offset - (sz + len));
101 /* overwrite the terminating newline with NUL
102 * (we do not bother to check that it *was* a newline)
107 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
108 if (!global && is_prefixed_with(value, "path=")) {
109 value += sizeof("path=") - 1;
110 free(archive_handle->tar__longname);
111 archive_handle->tar__longname = xstrdup(value);
116 #if ENABLE_FEATURE_TAR_SELINUX
117 /* Scan for SELinux contexts, via "RHT.security.selinux" keyword.
118 * This is what Red Hat's patched version of tar uses.
120 # define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
121 if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) {
122 value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
123 free(archive_handle->tar__sctx[global]);
124 archive_handle->tar__sctx[global] = xstrdup(value);
133 char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
135 file_header_t *file_header = archive_handle->file_header;
136 struct tar_header_t tar;
139 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
144 /* Our "private data" */
145 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
146 # define p_longname (archive_handle->tar__longname)
147 # define p_linkname (archive_handle->tar__linkname)
149 # define p_longname 0
150 # define p_linkname 0
153 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX
157 data_align(archive_handle, 512);
161 #if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
162 /* to prevent misdetection of bz2 sig */
163 *(aliased_uint32_t*)&tar = 0;
164 i = full_read(archive_handle->src_fd, &tar, 512);
165 /* If GNU tar sees EOF in above read, it says:
166 * "tar: A lone zero block at N", where N = kilobyte
167 * where EOF was met (not EOF block, actual EOF!),
168 * and exits with EXIT_SUCCESS.
169 * We will mimic exit(EXIT_SUCCESS), although we will not mimic
170 * the message and we don't check whether we indeed
171 * saw zero block directly before this. */
173 bb_error_msg("short read");
174 /* this merely signals end of archive, not exit(1): */
178 IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
179 bb_error_msg_and_die("short read");
184 xread(archive_handle->src_fd, &tar, i);
186 archive_handle->offset += i;
188 /* If there is no filename its an empty header */
189 if (tar.name[0] == 0 && tar.prefix[0] == 0) {
190 if (archive_handle->tar__end) {
191 /* Second consecutive empty header - end of archive.
192 * Read until the end to empty the pipe from gz or bz2
194 while (full_read(archive_handle->src_fd, &tar, 512) == 512)
196 return EXIT_FAILURE; /* "end of archive" */
198 archive_handle->tar__end = 1;
199 return EXIT_SUCCESS; /* "decoded one header" */
201 archive_handle->tar__end = 0;
203 /* Check header has valid magic, "ustar" is for the proper tar,
204 * five NULs are for the old tar format */
205 if (!is_prefixed_with(tar.magic, "ustar")
206 && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
207 || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
209 #if ENABLE_FEATURE_TAR_AUTODETECT
211 /* Two different causes for lseek() != 0:
212 * unseekable fd (would like to support that too, but...),
213 * or not first block (false positive, it's not .gz/.bz2!) */
214 if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
216 if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
218 bb_error_msg_and_die("invalid tar magic");
219 archive_handle->offset = 0;
220 goto again_after_align;
222 bb_error_msg_and_die("invalid tar magic");
225 /* Do checksum on headers.
226 * POSIX says that checksum is done on unsigned bytes, but
227 * Sun and HP-UX gets it wrong... more details in
229 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
230 sum_s = ' ' * sizeof(tar.chksum);
232 sum_u = ' ' * sizeof(tar.chksum);
233 for (i = 0; i < 148; i++) {
234 sum_u += ((unsigned char*)&tar)[i];
235 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
236 sum_s += ((signed char*)&tar)[i];
239 for (i = 156; i < 512; i++) {
240 sum_u += ((unsigned char*)&tar)[i];
241 #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
242 sum_s += ((signed char*)&tar)[i];
245 /* This field does not need special treatment (getOctal) */
247 char *endp; /* gcc likes temp var for &endp */
248 sum = strtoul(tar.chksum, &endp, 8);
249 if ((*endp != '\0' && *endp != ' ')
250 || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
252 bb_error_msg_and_die("invalid tar header checksum");
255 /* don't use xstrtoul, tar.chksum may have leading spaces */
256 sum = strtoul(tar.chksum, NULL, 8);
257 if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
258 bb_error_msg_and_die("invalid tar header checksum");
261 /* 0 is reserved for high perf file, treat as normal file */
262 if (!tar.typeflag) tar.typeflag = '0';
263 parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
265 /* getOctal trashes subsequent field, therefore we call it
266 * on fields in reverse order */
267 if (tar.devmajor[0]) {
268 char t = tar.prefix[0];
269 /* we trash prefix[0] here, but we DO need it later! */
270 unsigned minor = GET_OCTAL(tar.devminor);
271 unsigned major = GET_OCTAL(tar.devmajor);
272 file_header->device = makedev(major, minor);
275 file_header->link_target = NULL;
276 if (!p_linkname && parse_names && tar.linkname[0]) {
277 file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
278 /* FIXME: what if we have non-link object with link_target? */
279 /* Will link_target be free()ed? */
281 #if ENABLE_FEATURE_TAR_UNAME_GNAME
282 file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
283 file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
285 file_header->mtime = GET_OCTAL(tar.mtime);
286 file_header->size = GET_OCTAL(tar.size);
287 file_header->gid = GET_OCTAL(tar.gid);
288 file_header->uid = GET_OCTAL(tar.uid);
289 /* Set bits 0-11 of the files mode */
290 file_header->mode = 07777 & GET_OCTAL(tar.mode);
292 file_header->name = NULL;
293 if (!p_longname && parse_names) {
294 /* we trash mode[0] here, it's ok */
295 //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
299 //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
300 tar.padding[0] = '\0';
301 file_header->name = concat_path_file(tar.prefix, tar.name);
303 file_header->name = xstrdup(tar.name);
306 /* Set bits 12-15 of the files mode */
307 /* (typeflag was not trashed because chksum does not use getOctal) */
308 switch (tar.typeflag) {
309 case '1': /* hardlink */
310 /* we mark hardlinks as regular files with zero size and a link name */
311 file_header->mode |= S_IFREG;
312 /* on size of link fields from star(4)
313 * ... For tar archives written by pre POSIX.1-1988
314 * implementations, the size field usually contains the size of
315 * the file and needs to be ignored as no data may follow this
316 * header type. For POSIX.1- 1988 compliant archives, the size
317 * field needs to be 0. For POSIX.1-2001 compliant archives,
318 * the size field may be non zero, indicating that file data is
319 * included in the archive.
320 * i.e; always assume this is zero for safety.
326 #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
327 if (last_char_is(file_header->name, '/')) {
331 file_header->mode |= S_IFREG;
334 file_header->mode |= S_IFLNK;
335 /* have seen tarballs with size field containing
336 * the size of the link target's name */
338 file_header->size = 0;
341 file_header->mode |= S_IFCHR;
342 goto size0; /* paranoia */
344 file_header->mode |= S_IFBLK;
347 IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
348 file_header->mode |= S_IFDIR;
351 file_header->mode |= S_IFIFO;
353 case 'g': /* pax global header */
354 case 'x': { /* pax extended header */
355 if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
357 process_pax_hdr(archive_handle, file_header->size, (tar.typeflag == 'g'));
358 goto again_after_align;
359 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
360 /* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */
362 /* free: paranoia: tar with several consecutive longnames */
364 /* For paranoia reasons we allocate extra NUL char */
365 p_longname = xzalloc(file_header->size + 1);
366 /* We read ASCIZ string, including NUL */
367 xread(archive_handle->src_fd, p_longname, file_header->size);
368 archive_handle->offset += file_header->size;
369 /* return get_header_tar(archive_handle); */
370 /* gcc 4.1.1 didn't optimize it into jump */
371 /* so we will do it ourself, this also saves stack */
375 p_linkname = xzalloc(file_header->size + 1);
376 xread(archive_handle->src_fd, p_linkname, file_header->size);
377 archive_handle->offset += file_header->size;
378 /* return get_header_tar(archive_handle); */
381 * case 'S': // Sparse file
382 * Was seen in the wild. Not supported (yet?).
383 * See https://www.gnu.org/software/tar/manual/html_section/tar_92.html
384 * for the format. (An "Old GNU Format" was seen, not PAX formats).
386 // case 'D': /* GNU dump dir */
387 // case 'M': /* Continuation of multi volume archive */
388 // case 'N': /* Old GNU for names > 100 characters */
389 // case 'V': /* Volume header */
395 bb_error_msg("warning: skipping header '%c'", tar.typeflag);
396 sz = (file_header->size + 511) & ~(off_t)511;
397 archive_handle->offset += sz;
398 sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
400 xread(archive_handle->src_fd, &tar, 512);
401 /* return get_header_tar(archive_handle); */
402 goto again_after_align;
405 bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
408 #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
410 file_header->name = p_longname;
414 file_header->link_target = p_linkname;
419 /* Everything up to and including last ".." component is stripped */
420 overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
421 //TODO: do the same for file_header->link_target?
423 /* Strip trailing '/' in directories */
424 /* Must be done after mode is set as '/' is used to check if it's a directory */
425 cp = last_char_is(file_header->name, '/');
427 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
428 archive_handle->action_header(/*archive_handle->*/ file_header);
429 /* Note that we kill the '/' only after action_header() */
430 /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
433 archive_handle->action_data(archive_handle);
434 if (archive_handle->accept || archive_handle->reject
435 || (archive_handle->ah_flags & ARCHIVE_REMEMBER_NAMES)
437 llist_add_to(&archive_handle->passed, file_header->name);
438 } else /* Caller isn't interested in list of unpacked files */
439 free(file_header->name);
441 data_skip(archive_handle);
442 free(file_header->name);
444 archive_handle->offset += file_header->size;
446 free(file_header->link_target);
447 /* Do not free(file_header->name)!
448 * It might be inserted in archive_handle->passed - see above */
449 #if ENABLE_FEATURE_TAR_UNAME_GNAME
450 free(file_header->tar__uname);
451 free(file_header->tar__gname);
453 return EXIT_SUCCESS; /* "decoded one header" */