get_header_tar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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) + (unsigned char) *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. xfunc_error_retval = 0;
  184. short_read:
  185. bb_error_msg_and_die("short read");
  186. }
  187. if (i != 512) {
  188. IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
  189. goto 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;
  205. }
  206. archive_handle->tar__end = 1;
  207. return EXIT_SUCCESS;
  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. char FAST_FUNC (*get_header_ptr)(archive_handle_t *);
  218. uint16_t magic2;
  219. autodetect:
  220. magic2 = *(bb__aliased_uint16_t*)tar.name;
  221. /* tar gz/bz autodetect: check for gz/bz2 magic.
  222. * If we see the magic, and it is the very first block,
  223. * we can switch to get_header_tar_gz/bz2/lzma().
  224. * Needs seekable fd. I wish recv(MSG_PEEK) works
  225. * on any fd... */
  226. # if ENABLE_FEATURE_SEAMLESS_GZ
  227. if (magic2 == GZIP_MAGIC) {
  228. get_header_ptr = get_header_tar_gz;
  229. } else
  230. # endif
  231. # if ENABLE_FEATURE_SEAMLESS_BZ2
  232. if (magic2 == BZIP2_MAGIC
  233. && tar.name[2] == 'h' && isdigit(tar.name[3])
  234. ) { /* bzip2 */
  235. get_header_ptr = get_header_tar_bz2;
  236. } else
  237. # endif
  238. # if ENABLE_FEATURE_SEAMLESS_XZ
  239. //TODO: if (magic2 == XZ_MAGIC1)...
  240. //else
  241. # endif
  242. goto err;
  243. /* Two different causes for lseek() != 0:
  244. * unseekable fd (would like to support that too, but...),
  245. * or not first block (false positive, it's not .gz/.bz2!) */
  246. if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
  247. goto err;
  248. while (get_header_ptr(archive_handle) == EXIT_SUCCESS)
  249. continue;
  250. return EXIT_FAILURE;
  251. err:
  252. #endif /* FEATURE_TAR_AUTODETECT */
  253. bb_error_msg_and_die("invalid tar magic");
  254. }
  255. /* Do checksum on headers.
  256. * POSIX says that checksum is done on unsigned bytes, but
  257. * Sun and HP-UX gets it wrong... more details in
  258. * GNU tar source. */
  259. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  260. sum_s = ' ' * sizeof(tar.chksum);
  261. #endif
  262. sum_u = ' ' * sizeof(tar.chksum);
  263. for (i = 0; i < 148; i++) {
  264. sum_u += ((unsigned char*)&tar)[i];
  265. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  266. sum_s += ((signed char*)&tar)[i];
  267. #endif
  268. }
  269. for (i = 156; i < 512; i++) {
  270. sum_u += ((unsigned char*)&tar)[i];
  271. #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY
  272. sum_s += ((signed char*)&tar)[i];
  273. #endif
  274. }
  275. /* This field does not need special treatment (getOctal) */
  276. {
  277. char *endp; /* gcc likes temp var for &endp */
  278. sum = strtoul(tar.chksum, &endp, 8);
  279. if ((*endp != '\0' && *endp != ' ')
  280. || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum))
  281. ) {
  282. bb_error_msg_and_die("invalid tar header checksum");
  283. }
  284. }
  285. /* don't use xstrtoul, tar.chksum may have leading spaces */
  286. sum = strtoul(tar.chksum, NULL, 8);
  287. if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) {
  288. bb_error_msg_and_die("invalid tar header checksum");
  289. }
  290. /* 0 is reserved for high perf file, treat as normal file */
  291. if (!tar.typeflag) tar.typeflag = '0';
  292. parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
  293. /* getOctal trashes subsequent field, therefore we call it
  294. * on fields in reverse order */
  295. if (tar.devmajor[0]) {
  296. char t = tar.prefix[0];
  297. /* we trash prefix[0] here, but we DO need it later! */
  298. unsigned minor = GET_OCTAL(tar.devminor);
  299. unsigned major = GET_OCTAL(tar.devmajor);
  300. file_header->device = makedev(major, minor);
  301. tar.prefix[0] = t;
  302. }
  303. file_header->link_target = NULL;
  304. if (!p_linkname && parse_names && tar.linkname[0]) {
  305. file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname));
  306. /* FIXME: what if we have non-link object with link_target? */
  307. /* Will link_target be free()ed? */
  308. }
  309. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  310. file_header->tar__uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL;
  311. file_header->tar__gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL;
  312. #endif
  313. file_header->mtime = GET_OCTAL(tar.mtime);
  314. file_header->size = GET_OCTAL(tar.size);
  315. file_header->gid = GET_OCTAL(tar.gid);
  316. file_header->uid = GET_OCTAL(tar.uid);
  317. /* Set bits 0-11 of the files mode */
  318. file_header->mode = 07777 & GET_OCTAL(tar.mode);
  319. file_header->name = NULL;
  320. if (!p_longname && parse_names) {
  321. /* we trash mode[0] here, it's ok */
  322. //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain
  323. tar.mode[0] = '\0';
  324. if (tar.prefix[0]) {
  325. /* and padding[0] */
  326. //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain
  327. tar.padding[0] = '\0';
  328. file_header->name = concat_path_file(tar.prefix, tar.name);
  329. } else
  330. file_header->name = xstrdup(tar.name);
  331. }
  332. /* Set bits 12-15 of the files mode */
  333. /* (typeflag was not trashed because chksum does not use getOctal) */
  334. switch (tar.typeflag) {
  335. case '1': /* hardlink */
  336. /* we mark hardlinks as regular files with zero size and a link name */
  337. file_header->mode |= S_IFREG;
  338. /* on size of link fields from star(4)
  339. * ... For tar archives written by pre POSIX.1-1988
  340. * implementations, the size field usually contains the size of
  341. * the file and needs to be ignored as no data may follow this
  342. * header type. For POSIX.1- 1988 compliant archives, the size
  343. * field needs to be 0. For POSIX.1-2001 compliant archives,
  344. * the size field may be non zero, indicating that file data is
  345. * included in the archive.
  346. * i.e; always assume this is zero for safety.
  347. */
  348. goto size0;
  349. case '7':
  350. /* case 0: */
  351. case '0':
  352. #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
  353. if (last_char_is(file_header->name, '/')) {
  354. goto set_dir;
  355. }
  356. #endif
  357. file_header->mode |= S_IFREG;
  358. break;
  359. case '2':
  360. file_header->mode |= S_IFLNK;
  361. /* have seen tarballs with size field containing
  362. * the size of the link target's name */
  363. size0:
  364. file_header->size = 0;
  365. break;
  366. case '3':
  367. file_header->mode |= S_IFCHR;
  368. goto size0; /* paranoia */
  369. case '4':
  370. file_header->mode |= S_IFBLK;
  371. goto size0;
  372. case '5':
  373. IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:)
  374. file_header->mode |= S_IFDIR;
  375. goto size0;
  376. case '6':
  377. file_header->mode |= S_IFIFO;
  378. goto size0;
  379. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  380. case 'L':
  381. /* free: paranoia: tar with several consecutive longnames */
  382. free(p_longname);
  383. /* For paranoia reasons we allocate extra NUL char */
  384. p_longname = xzalloc(file_header->size + 1);
  385. /* We read ASCIZ string, including NUL */
  386. xread(archive_handle->src_fd, p_longname, file_header->size);
  387. archive_handle->offset += file_header->size;
  388. /* return get_header_tar(archive_handle); */
  389. /* gcc 4.1.1 didn't optimize it into jump */
  390. /* so we will do it ourself, this also saves stack */
  391. goto again;
  392. case 'K':
  393. free(p_linkname);
  394. p_linkname = xzalloc(file_header->size + 1);
  395. xread(archive_handle->src_fd, p_linkname, file_header->size);
  396. archive_handle->offset += file_header->size;
  397. /* return get_header_tar(archive_handle); */
  398. goto again;
  399. case 'D': /* GNU dump dir */
  400. case 'M': /* Continuation of multi volume archive */
  401. case 'N': /* Old GNU for names > 100 characters */
  402. case 'S': /* Sparse file */
  403. case 'V': /* Volume header */
  404. #endif
  405. case 'g': /* pax global header */
  406. case 'x': { /* pax extended header */
  407. if ((uoff_t)file_header->size > 0xfffff) /* paranoia */
  408. goto skip_ext_hdr;
  409. process_pax_hdr(archive_handle, file_header->size, (tar.typeflag == 'g'));
  410. goto again_after_align;
  411. }
  412. skip_ext_hdr:
  413. {
  414. off_t sz;
  415. bb_error_msg("warning: skipping header '%c'", tar.typeflag);
  416. sz = (file_header->size + 511) & ~(off_t)511;
  417. archive_handle->offset += sz;
  418. sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
  419. while (sz--)
  420. xread(archive_handle->src_fd, &tar, 512);
  421. /* return get_header_tar(archive_handle); */
  422. goto again_after_align;
  423. }
  424. default:
  425. bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
  426. }
  427. #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
  428. if (p_longname) {
  429. file_header->name = p_longname;
  430. p_longname = NULL;
  431. }
  432. if (p_linkname) {
  433. file_header->link_target = p_linkname;
  434. p_linkname = NULL;
  435. }
  436. #endif
  437. /* Everything up to and including last ".." component is stripped */
  438. overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
  439. /* Strip trailing '/' in directories */
  440. /* Must be done after mode is set as '/' is used to check if it's a directory */
  441. cp = last_char_is(file_header->name, '/');
  442. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  443. archive_handle->action_header(/*archive_handle->*/ file_header);
  444. /* Note that we kill the '/' only after action_header() */
  445. /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
  446. if (cp)
  447. *cp = '\0';
  448. archive_handle->action_data(archive_handle);
  449. if (archive_handle->accept || archive_handle->reject)
  450. llist_add_to(&archive_handle->passed, file_header->name);
  451. else /* Caller isn't interested in list of unpacked files */
  452. free(file_header->name);
  453. } else {
  454. data_skip(archive_handle);
  455. free(file_header->name);
  456. }
  457. archive_handle->offset += file_header->size;
  458. free(file_header->link_target);
  459. /* Do not free(file_header->name)!
  460. * It might be inserted in archive_handle->passed - see above */
  461. #if ENABLE_FEATURE_TAR_UNAME_GNAME
  462. free(file_header->tar__uname);
  463. free(file_header->tar__gname);
  464. #endif
  465. return EXIT_SUCCESS;
  466. }