tar: correctly skip (and warn about) pax headers.

plug memory leak. save 50 bytes.
Wooohooo! we finally can unpack kernel tarballs!
This commit is contained in:
Denis Vlasenko 2006-11-26 01:46:59 +00:00
parent 7725d0ff5c
commit b596335868
2 changed files with 35 additions and 20 deletions

View File

@ -40,11 +40,12 @@ static unsigned long long getOctal(char *str, int len)
bb_error_msg_and_die("corrupted octal value in tar header"); bb_error_msg_and_die("corrupted octal value in tar header");
return v; return v;
} }
#define GET_OCTAL(a) getOctal((a), sizeof(a))
void BUG_tar_header_size(void); void BUG_tar_header_size(void);
char get_header_tar(archive_handle_t *archive_handle) char get_header_tar(archive_handle_t *archive_handle)
{ {
static int end = 0; static int end;
file_header_t *file_header = archive_handle->file_header; file_header_t *file_header = archive_handle->file_header;
struct { struct {
@ -69,18 +70,17 @@ char get_header_tar(archive_handle_t *archive_handle)
} tar; } tar;
char *cp; char *cp;
int sum, i; int sum, i;
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
int parse_names; int parse_names;
#else
enum { parse_names = 1 };
#endif
if (sizeof(tar) != 512) if (sizeof(tar) != 512)
BUG_tar_header_size(); BUG_tar_header_size();
again:
/* Align header */ /* Align header */
data_align(archive_handle, 512); data_align(archive_handle, 512);
again_after_align:
xread(archive_handle->src_fd, &tar, 512); xread(archive_handle->src_fd, &tar, 512);
archive_handle->offset += 512; archive_handle->offset += 512;
@ -121,13 +121,12 @@ char get_header_tar(archive_handle_t *archive_handle)
bb_error_msg_and_die("invalid tar header checksum"); bb_error_msg_and_die("invalid tar header checksum");
} }
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS /* 0 is reserved for high perf file, treat as normal file */
parse_names = (tar.typeflag != 'L' && tar.typeflag != 'K'); if (!tar.typeflag) tar.typeflag = '0';
#endif parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7');
/* getOctal trashes subsequent field, therefore we call it /* getOctal trashes subsequent field, therefore we call it
* on fields in reverse order */ * on fields in reverse order */
#define GET_OCTAL(a) getOctal((a), sizeof(a))
if (tar.devmajor[0]) { if (tar.devmajor[0]) {
unsigned minor = GET_OCTAL(tar.devminor); unsigned minor = GET_OCTAL(tar.devminor);
unsigned major = GET_OCTAL(tar.devmajor); unsigned major = GET_OCTAL(tar.devmajor);
@ -147,8 +146,8 @@ char get_header_tar(archive_handle_t *archive_handle)
file_header->uid = GET_OCTAL(tar.uid); file_header->uid = GET_OCTAL(tar.uid);
/* Set bits 0-11 of the files mode */ /* Set bits 0-11 of the files mode */
file_header->mode = 07777 & GET_OCTAL(tar.mode); file_header->mode = 07777 & GET_OCTAL(tar.mode);
#undef GET_OCTAL
file_header->name = NULL;
if (!longname && parse_names) { if (!longname && parse_names) {
/* we trash mode[0] here, it's ok */ /* we trash mode[0] here, it's ok */
tar.name[sizeof(tar.name)] = '\0'; tar.name[sizeof(tar.name)] = '\0';
@ -158,6 +157,7 @@ char get_header_tar(archive_handle_t *archive_handle)
file_header->name = concat_path_file(tar.prefix, tar.name); file_header->name = concat_path_file(tar.prefix, tar.name);
} else } else
file_header->name = xstrdup(tar.name); file_header->name = xstrdup(tar.name);
/* FIXME: add check for /../ attacks */
} }
/* Set bits 12-15 of the files mode */ /* Set bits 12-15 of the files mode */
@ -168,8 +168,7 @@ char get_header_tar(archive_handle_t *archive_handle)
file_header->mode |= S_IFREG; file_header->mode |= S_IFREG;
break; break;
case '7': case '7':
/* Reserved for high performance files, treat as normal file */ /* case 0: */
case 0:
case '0': case '0':
#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
if (last_char_is(file_header->name, '/')) { if (last_char_is(file_header->name, '/')) {
@ -195,18 +194,24 @@ char get_header_tar(archive_handle_t *archive_handle)
break; break;
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
case 'L': case 'L':
/* paranoia: tar with several consecutive longnames */ /* free: paranoia: tar with several consecutive longnames */
free(longname); free(longname);
/* For paranoia reasons we allocate extra NUL char */
longname = xzalloc(file_header->size + 1); longname = xzalloc(file_header->size + 1);
/* We read ASCIZ string, including NUL */
xread(archive_handle->src_fd, longname, file_header->size); xread(archive_handle->src_fd, longname, file_header->size);
archive_handle->offset += file_header->size; archive_handle->offset += file_header->size;
return get_header_tar(archive_handle); /* return get_header_tar(archive_handle); */
/* gcc 4.1.1 didn't optimize it into jump */
/* so we will do it ourself, this also saves stack */
goto again;
case 'K': case 'K':
free(linkname); free(linkname);
linkname = xzalloc(file_header->size + 1); linkname = xzalloc(file_header->size + 1);
xread(archive_handle->src_fd, linkname, file_header->size); xread(archive_handle->src_fd, linkname, file_header->size);
archive_handle->offset += file_header->size; archive_handle->offset += file_header->size;
return get_header_tar(archive_handle); /* return get_header_tar(archive_handle); */
goto again;
case 'D': /* GNU dump dir */ case 'D': /* GNU dump dir */
case 'M': /* Continuation of multi volume archive */ case 'M': /* Continuation of multi volume archive */
case 'N': /* Old GNU for names > 100 characters */ case 'N': /* Old GNU for names > 100 characters */
@ -214,11 +219,19 @@ char get_header_tar(archive_handle_t *archive_handle)
case 'V': /* Volume header */ case 'V': /* Volume header */
#endif #endif
case 'g': /* pax global header */ case 'g': /* pax global header */
case 'x': /* pax extended header */ case 'x': { /* pax extended header */
bb_error_msg("ignoring extension type %c", tar.typeflag); off_t sz;
break; bb_error_msg("warning: skipping header '%c'", tar.typeflag);
sz = (file_header->size + 511) & ~(off_t)511;
archive_handle->offset += sz;
sz >>= 9; /* sz /= 512 but w/o contortions for signed div */
while (sz--)
xread(archive_handle->src_fd, &tar, 512);
/* return get_header_tar(archive_handle); */
goto again_after_align;
}
default: default:
bb_error_msg("unknown typeflag: 0x%x", tar.typeflag); bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag);
} }
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
@ -246,10 +259,12 @@ char get_header_tar(archive_handle_t *archive_handle)
llist_add_to(&(archive_handle->passed), file_header->name); llist_add_to(&(archive_handle->passed), file_header->name);
} else { } else {
data_skip(archive_handle); data_skip(archive_handle);
free(file_header->name);
} }
archive_handle->offset += file_header->size; archive_handle->offset += file_header->size;
free(file_header->link_name); free(file_header->link_name);
/* Do not free(file_header->name)! */
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -768,7 +768,7 @@ int tar_main(int argc, char **argv)
if (verboseFlag) tar_handle->action_header = header_verbose_list; if (verboseFlag) tar_handle->action_header = header_verbose_list;
if (verboseFlag == 1) tar_handle->action_header = header_list; if (verboseFlag == 1) tar_handle->action_header = header_list;
if ((opt & OPT_EXTRACT) && tar_handle->action_data != data_extract_to_stdout) if (opt & OPT_EXTRACT)
tar_handle->action_data = data_extract_all; tar_handle->action_data = data_extract_all;
if (opt & OPT_2STDOUT) if (opt & OPT_2STDOUT)