get_header_tar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. *
  5. * FIXME:
  6. * In privileged mode if uname and gname map to a uid and gid then use the
  7. * mapped value instead of the uid/gid values in tar header
  8. *
  9. * References:
  10. * GNU tar and star man pages,
  11. * Opengroup's ustar interchange format,
  12. * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
  13. */
  14. #include "libbb.h"
  15. #include "bb_archive.h"
  16. typedef uint32_t aliased_uint32_t FIX_ALIASING;
  17. typedef off_t aliased_off_t FIX_ALIASING;
  18. /* NB: _DESTROYS_ str[len] character! */
  19. static unsigned long long getOctal(char *str, int len)
  20. {
  21. unsigned long long v;
  22. char *end;
  23. /* NB: leading spaces are allowed. Using strtoull to handle that.
  24. * The downside is that we accept e.g. "-123" too :(
  25. */
  26. str[len] = '\0';
  27. v = strtoull(str, &end, 8);
  28. /* std: "Each numeric field is terminated by one or more
  29. * <space> or NUL characters". We must support ' '! */
  30. if (*end != '\0' && *end != ' ') {
  31. int8_t first = str[0];
  32. if (!(first & 0x80))
  33. bb_simple_error_msg_and_die("corrupted octal value in tar header");
  34. /*
  35. * GNU tar uses "base-256 encoding" for very large numbers.
  36. * Encoding is binary, with highest bit always set as a marker
  37. * and sign in next-highest bit:
  38. * 80 00 .. 00 - zero
  39. * bf ff .. ff - largest positive number
  40. * ff ff .. ff - minus 1
  41. * c0 00 .. 00 - smallest negative number
  42. *
  43. * Example of tar file with 8914993153 (0x213600001) byte file.
  44. * Field starts at offset 7c:
  45. * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....|
  46. * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336|
  47. *
  48. * NB: tarballs with NEGATIVE unix times encoded that way were seen!
  49. */
  50. /* Sign-extend 7bit 'first' to 64bit 'v' (that is, using 6th bit as sign): */
  51. first <<= 1;
  52. first >>= 1; /* now 7th bit = 6th bit */
  53. v = first; /* sign-extend 8 bits to 64 */
  54. while (--len != 0)
  55. v = (v << 8) + (uint8_t) *++str;
  56. }
  57. return v;
  58. }
  59. #define GET_OCTAL(a) getOctal((a), sizeof(a))
  60. #define TAR_EXTD (ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX)
  61. #if !TAR_EXTD
  62. #define process_pax_hdr(archive_handle, sz, global) \
  63. process_pax_hdr(archive_handle, sz)
  64. #endif
  65. /* "global" is 0 or 1 */
  66. static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int global)
  67. {
  68. #if !TAR_EXTD
  69. unsigned blk_sz = (sz + 511) & (~511);
  70. seek_by_read(archive_handle->src_fd, blk_sz);
  71. #else
  72. unsigned blk_sz = (sz + 511) & (~511);
  73. char *buf, *p;
  74. p = buf = xmalloc(blk_sz + 1);
  75. xread(archive_handle->src_fd, buf, blk_sz);
  76. archive_handle->offset += blk_sz;
  77. /* prevent bb_strtou from running off the buffer */
  78. buf[sz] = '\0';
  79. while (sz != 0) {
  80. char *end, *value;
  81. unsigned len;
  82. /* Every record has this format: "LEN NAME=VALUE\n" */
  83. len = bb_strtou(p, &end, 10);
  84. /* expect errno to be EINVAL, because the character
  85. * following the digits should be a space
  86. */
  87. p += len;
  88. sz -= len;
  89. if (
  90. /** (int)sz < 0 - not good enough for huge malicious VALUE of 2^32-1 */
  91. (int)(sz|len) < 0 /* this works */
  92. || len == 0
  93. || errno != EINVAL
  94. || *end != ' '
  95. ) {
  96. bb_simple_error_msg("malformed extended header, skipped");
  97. // More verbose version:
  98. //bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
  99. // archive_handle->offset - (sz + len));
  100. break;
  101. }
  102. /* overwrite the terminating newline with NUL
  103. * (we do not bother to check that it *was* a newline)
  104. */
  105. p[-1] = '\0';
  106. value = end + 1;
  107. # if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  108. if (!global) {
  109. if (is_prefixed_with(value, "path=")) {
  110. value += sizeof("path=") - 1;
  111. free(archive_handle->tar__longname);
  112. archive_handle->tar__longname = xstrdup(value);
  113. continue;
  114. }
  115. if (is_prefixed_with(value, "linkpath=")) {
  116. value += sizeof("linkpath=") - 1;
  117. free(archive_handle->tar__linkname);
  118. archive_handle->tar__linkname = xstrdup(value);
  119. continue;
  120. }
  121. }
  122. # endif
  123. # if ENABLE_FEATURE_TAR_SELINUX
  124. /* Scan for SELinux contexts, via "RHT.security.selinux" keyword.
  125. * This is what Red Hat's patched version of tar uses.
  126. */
  127. # define SELINUX_CONTEXT_KEYWORD "RHT.security.selinux"
  128. if (is_prefixed_with(value, SELINUX_CONTEXT_KEYWORD"=")) {
  129. value += sizeof(SELINUX_CONTEXT_KEYWORD"=") - 1;
  130. free(archive_handle->tar__sctx[global]);
  131. archive_handle->tar__sctx[global] = xstrdup(value);
  132. continue;
  133. }
  134. # endif
  135. }
  136. free(buf);
  137. #endif
  138. }
  139. char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
  140. {
  141. file_header_t *file_header = archive_handle->file_header;
  142. struct tar_header_t tar;
  143. char *cp;
  144. int tar_typeflag; /* can be "char", "int" seems give smaller code */
  145. int i, sum_u, sum;
  146. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  147. int sum_s;
  148. #endif
  149. int parse_names;
  150. /* Our "private data" */
  151. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  152. # define p_longname (archive_handle->tar__longname)
  153. # define p_linkname (archive_handle->tar__linkname)
  154. #else
  155. # define p_longname 0
  156. # define p_linkname 0
  157. #endif
  158. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS || ENABLE_FEATURE_TAR_SELINUX
  159. again:
  160. #endif
  161. /* Align header */
  162. data_align(archive_handle, 512);
  163. again_after_align:
  164. #if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT
  165. /* to prevent misdetection of bz2 sig */
  166. *(aliased_uint32_t*)&tar = 0;
  167. i = full_read(archive_handle->src_fd, &tar, 512);
  168. /* If GNU tar sees EOF in above read, it says:
  169. * "tar: A lone zero block at N", where N = kilobyte
  170. * where EOF was met (not EOF block, actual EOF!),
  171. * and exits with EXIT_SUCCESS.
  172. * We will mimic exit(EXIT_SUCCESS), although we will not mimic
  173. * the message and we don't check whether we indeed
  174. * saw zero block directly before this. */
  175. if (i == 0) {
  176. /* GNU tar 1.29 will be silent if tar archive ends abruptly
  177. * (if there are no zero blocks at all, and last read returns zero,
  178. * not short read 0 < len < 512). Complain only if
  179. * the very first read fails. Grrr.
  180. */
  181. if (archive_handle->offset == 0)
  182. bb_simple_error_msg("short read");
  183. /* this merely signals end of archive, not exit(1): */
  184. return EXIT_FAILURE;
  185. }
  186. if (i != 512) {
  187. IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
  188. bb_simple_error_msg_and_die("short read");
  189. }
  190. #else
  191. i = 512;
  192. xread(archive_handle->src_fd, &tar, i);
  193. #endif
  194. archive_handle->offset += i;
  195. /* If there is no filename its an empty header */
  196. if (tar.name[0] == 0 && tar.prefix[0] == 0
  197. /* Have seen a tar archive with pax 'x' header supplying UTF8 filename,
  198. * with actual file having all name fields NUL-filled. Check this: */
  199. && !p_longname
  200. ) {
  201. if (archive_handle->tar__end) {
  202. /* Second consecutive empty header - end of archive.
  203. * Read until the end to empty the pipe from gz or bz2
  204. */
  205. while (full_read(archive_handle->src_fd, &tar, 512) == 512)
  206. continue;
  207. return EXIT_FAILURE; /* "end of archive" */
  208. }
  209. archive_handle->tar__end = 1;
  210. return EXIT_SUCCESS; /* "decoded one header" */
  211. }
  212. archive_handle->tar__end = 0;
  213. /* Check header has valid magic, "ustar" is for the proper tar,
  214. * five NULs are for the old tar format */
  215. if (!is_prefixed_with(tar.magic, "ustar")
  216. && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  217. || memcmp(tar.magic, "\0\0\0\0", 5) != 0)
  218. ) {
  219. #if ENABLE_FEATURE_TAR_AUTODETECT
  220. autodetect:
  221. /* Two different causes for lseek() != 0:
  222. * unseekable fd (would like to support that too, but...),
  223. * or not first block (false positive, it's not .gz/.bz2!) */
  224. if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
  225. goto err;
  226. if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
  227. err:
  228. bb_simple_error_msg_and_die("invalid tar magic");
  229. archive_handle->offset = 0;
  230. goto again_after_align;
  231. #endif
  232. bb_simple_error_msg_and_die("invalid tar magic");
  233. }
  234. /* Do checksum on headers.
  235. * POSIX says that checksum is done on unsigned bytes, but
  236. * Sun and HP-UX gets it wrong... more details in
  237. * GNU tar source. */
  238. sum_u = ' ' * sizeof(tar.chksum);
  239. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  240. sum_s = sum_u;
  241. #endif
  242. for (i = 0; i < 148; i++) {
  243. sum_u += ((unsigned char*)&tar)[i];
  244. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  245. sum_s += ((signed char*)&tar)[i];
  246. #endif
  247. }
  248. for (i = 156; i < 512; i++) {
  249. sum_u += ((unsigned char*)&tar)[i];
  250. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  251. sum_s += ((signed char*)&tar)[i];
  252. #endif
  253. }
  254. /* Most tarfiles have tar.chksum NUL or space terminated, but
  255. * github.com decided to be "special" and have unterminated field:
  256. * 0090: 30343300 30303031 33323731 30000000 |043.000132710...|
  257. * ^^^^^^^^|
  258. * Need to use GET_OCTAL. This overwrites tar.typeflag ---+
  259. * (the '0' char immediately after chksum in example above) with NUL.
  260. */
  261. tar_typeflag = (uint8_t)tar.typeflag; /* save it */
  262. sum = GET_OCTAL(tar.chksum);
  263. if (sum_u != sum
  264. IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)
  265. ) {
  266. bb_simple_error_msg_and_die("invalid tar header checksum");
  267. }
  268. /* GET_OCTAL trashes subsequent field, therefore we call it
  269. * on fields in reverse order */
  270. if (tar.devmajor[0]) {
  271. char t = tar.prefix[0];
  272. /* we trash prefix[0] here, but we DO need it later! */
  273. unsigned minor = GET_OCTAL(tar.devminor);
  274. unsigned major = GET_OCTAL(tar.devmajor);
  275. file_header->device = makedev(major, minor);
  276. tar.prefix[0] = t;
  277. }
  278. /* 0 is reserved for high perf file, treat as normal file */
  279. if (tar_typeflag == '\0') tar_typeflag = '0';
  280. parse_names = (tar_typeflag >= '0' && tar_typeflag <= '7');
  281. file_header->link_target = NULL;
  282. if (!p_linkname && parse_names && tar.linkname[0]) {
  283. file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
  284. /* FIXME: what if we have non-link object with link_target? */
  285. /* Will link_target be free()ed? */
  286. }
  287. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  288. file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
  289. file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
  290. #endif
  291. file_header->mtime = GET_OCTAL(tar.mtime);
  292. file_header->size = GET_OCTAL(tar.size);
  293. file_header->gid = GET_OCTAL(tar.gid);
  294. file_header->uid = GET_OCTAL(tar.uid);
  295. /* Set bits 0-11 of the files mode */
  296. file_header->mode = 07777 & GET_OCTAL(tar.mode);
  297. file_header->name = NULL;
  298. if (!p_longname && parse_names) {
  299. /* we trash mode[0] here, it's ok */
  300. //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
  301. tar.mode[0] = '\0';
  302. if (tar.prefix[0]) {
  303. /* and padding[0] */
  304. //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
  305. tar.padding[0] = '\0';
  306. file_header->name = concat_path_file(tar.prefix, tar.name);
  307. } else
  308. file_header->name = xstrdup(tar.name);
  309. }
  310. /* Set bits 12-15 of the files mode */
  311. /* (typeflag was not trashed because chksum does not use getOctal) */
  312. switch (tar_typeflag) {
  313. case '1': /* hardlink */
  314. /* we mark hardlinks as regular files with zero size and a link name */
  315. file_header->mode |= S_IFREG;
  316. /* on size of link fields from star(4)
  317. * ... For tar archives written by pre POSIX.1-1988
  318. * implementations, the size field usually contains the size of
  319. * the file and needs to be ignored as no data may follow this
  320. * header type. For POSIX.1- 1988 compliant archives, the size
  321. * field needs to be 0. For POSIX.1-2001 compliant archives,
  322. * the size field may be non zero, indicating that file data is
  323. * included in the archive.
  324. * i.e; always assume this is zero for safety.
  325. */
  326. goto size0;
  327. case '7':
  328. /* case 0: */
  329. case '0':
  330. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  331. if (file_header->name && last_char_is(file_header->name, '/')) {
  332. goto set_dir;
  333. }
  334. #endif
  335. file_header->mode |= S_IFREG;
  336. break;
  337. case '2':
  338. file_header->mode |= S_IFLNK;
  339. /* have seen tarballs with size field containing
  340. * the size of the link target's name */
  341. size0:
  342. file_header->size = 0;
  343. break;
  344. case '3':
  345. file_header->mode |= S_IFCHR;
  346. goto size0; /* paranoia */
  347. case '4':
  348. file_header->mode |= S_IFBLK;
  349. goto size0;
  350. case '5':
  351. IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
  352. file_header->mode |= S_IFDIR;
  353. goto size0;
  354. case '6':
  355. file_header->mode |= S_IFIFO;
  356. goto size0;
  357. case 'g': /* pax global header */
  358. case 'x': { /* pax extended header */
  359. if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
  360. goto skip_ext_hdr;
  361. process_pax_hdr(archive_handle, file_header->size, (tar_typeflag == 'g'));
  362. goto again_after_align;
  363. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  364. /* See http://www.gnu.org/software/tar/manual/html_node/Extensions.html */
  365. case 'L':
  366. /* free: paranoia: tar with several consecutive longnames */
  367. free(p_longname);
  368. /* For paranoia reasons we allocate extra NUL char */
  369. p_longname = xzalloc(file_header->size + 1);
  370. /* We read ASCIZ string, including NUL */
  371. xread(archive_handle->src_fd, p_longname, file_header->size);
  372. archive_handle->offset += file_header->size;
  373. /* return get_header_tar(archive_handle); */
  374. /* gcc 4.1.1 didn't optimize it into jump */
  375. /* so we will do it ourself, this also saves stack */
  376. goto again;
  377. case 'K':
  378. free(p_linkname);
  379. p_linkname = xzalloc(file_header->size + 1);
  380. xread(archive_handle->src_fd, p_linkname, file_header->size);
  381. archive_handle->offset += file_header->size;
  382. /* return get_header_tar(archive_handle); */
  383. goto again;
  384. /*
  385. * case 'S': // Sparse file
  386. * Was seen in the wild. Not supported (yet?).
  387. * See https://www.gnu.org/software/tar/manual/html_section/tar_92.html
  388. * for the format. (An "Old GNU Format" was seen, not PAX formats).
  389. */
  390. // case 'D': /* GNU dump dir */
  391. // case 'M': /* Continuation of multi volume archive */
  392. // case 'N': /* Old GNU for names > 100 characters */
  393. case 'V': /* Volume header */
  394. ; /* Fall through to skip it */
  395. #endif
  396. }
  397. skip_ext_hdr:
  398. {
  399. off_t sz;
  400. bb_error_msg("warning: skipping header '%c'", tar_typeflag);
  401. sz = (file_header->size + 511) & ~(off_t)511;
  402. archive_handle->offset += sz;
  403. sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
  404. while (sz--)
  405. xread(archive_handle->src_fd, &tar, 512);
  406. /* return get_header_tar(archive_handle); */
  407. goto again_after_align;
  408. }
  409. default:
  410. bb_error_msg_and_die("unknown typeflag: 0x%x", tar_typeflag);
  411. }
  412. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  413. if (p_longname) {
  414. file_header->name = p_longname;
  415. p_longname = NULL;
  416. }
  417. if (p_linkname) {
  418. file_header->link_target = p_linkname;
  419. p_linkname = NULL;
  420. }
  421. #endif
  422. /* Everything up to and including last ".." component is stripped */
  423. overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
  424. //TODO: do the same for file_header->link_target?
  425. /* Strip trailing '/' in directories */
  426. /* Must be done after mode is set as '/' is used to check if it's a directory */
  427. cp = last_char_is(file_header->name, '/');
  428. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  429. archive_handle->action_header(/*archive_handle->*/ file_header);
  430. /* Note that we kill the '/' only after action_header() */
  431. /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
  432. if (cp)
  433. *cp = '\0';
  434. archive_handle->action_data(archive_handle);
  435. if (archive_handle->accept || archive_handle->reject
  436. || (archive_handle->ah_flags & ARCHIVE_REMEMBER_NAMES)
  437. ) {
  438. llist_add_to(&archive_handle->passed, file_header->name);
  439. } else /* Caller isn't interested in list of unpacked files */
  440. free(file_header->name);
  441. } else {
  442. data_skip(archive_handle);
  443. free(file_header->name);
  444. }
  445. 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);
  452. #endif
  453. return EXIT_SUCCESS; /* "decoded one header" */
  454. }