get_header_tar.c 14 KB

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