get_header_tar.c 15 KB

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