From 154532d71c447e73c70ec06d26628bb6919b9a5a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 27 Jun 2019 14:21:54 +0200 Subject: Relax tar header parser to accept pre-posix formats Signed-off-by: David Oberhollenzer --- include/tar.h | 10 ++++++++++ lib/tar/read_header.c | 42 ++++++++++++++++++++++++++++++++---------- tests/tar_formats.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/include/tar.h b/include/tar.h index a39d7c9..3b7c8eb 100644 --- a/include/tar.h +++ b/include/tar.h @@ -6,6 +6,13 @@ #include #include +typedef enum { + ETV_UNKNOWN = 0, + ETV_V7_UNIX, + ETV_PRE_POSIX, + ETV_POSIX, +} E_TAR_VERSION; + typedef struct { char name[100]; char mode[8]; @@ -46,6 +53,9 @@ typedef struct { #define TAR_MAGIC "ustar" #define TAR_VERSION "00" +#define TAR_MAGIC_OLD "ustar " +#define TAR_VERSION_OLD " " + /* Returns < 0 on failure, > 0 if cannot encode, 0 on success. Prints error/warning messages to stderr. diff --git a/lib/tar/read_header.c b/lib/tar/read_header.c index ec48a3e..01e4543 100644 --- a/lib/tar/read_header.c +++ b/lib/tar/read_header.c @@ -25,6 +25,11 @@ static int read_octal(const char *str, int digits, uint64_t *out) { uint64_t result = 0; + while (digits > 0 && isspace(*str)) { + ++str; + --digits; + } + while (digits > 0 && *str >= '0' && *str <= '7') { if (result > 0x1FFFFFFFFFFFFFFFUL) { fputs("numeric overflow parsing tar header\n", stderr); @@ -43,6 +48,11 @@ static int read_binary(const char *str, int digits, uint64_t *out) { uint64_t x, result = 0; + while (digits > 0 && isspace(*str)) { + ++str; + --digits; + } + while (digits > 0) { if (result > 0x00FFFFFFFFFFFFFFUL) { fputs("numeric overflow parsing tar header\n", stderr); @@ -96,15 +106,24 @@ static bool is_checksum_valid(const tar_header_t *hdr) return chksum == ref; } -static bool is_magic_valid(const tar_header_t *hdr) +static E_TAR_VERSION check_version(const tar_header_t *hdr) { - if (memcmp(hdr->magic, TAR_MAGIC, sizeof(hdr->magic)) != 0) - return false; + char buffer[sizeof(hdr->magic) + sizeof(hdr->version)]; - if (memcmp(hdr->version, TAR_VERSION, sizeof(hdr->version)) != 0) - return false; + memset(buffer, '\0', sizeof(buffer)); + if (memcmp(hdr->magic, buffer, sizeof(hdr->magic)) == 0 && + memcmp(hdr->version, buffer, sizeof(hdr->version)) == 0) + return ETV_V7_UNIX; + + if (memcmp(hdr->magic, TAR_MAGIC, sizeof(hdr->magic)) == 0 && + memcmp(hdr->version, TAR_VERSION, sizeof(hdr->version)) == 0) + return ETV_POSIX; + + if (memcmp(hdr->magic, TAR_MAGIC_OLD, sizeof(hdr->magic)) == 0 && + memcmp(hdr->version, TAR_VERSION_OLD, sizeof(hdr->version)) == 0) + return ETV_PRE_POSIX; - return true; + return ETV_UNKNOWN; } static int pax_read_decimal(const char *str, uint64_t *out) @@ -232,13 +251,14 @@ fail: } static int decode_header(const tar_header_t *hdr, unsigned int set_by_pax, - tar_header_decoded_t *out) + tar_header_decoded_t *out, E_TAR_VERSION version) { uint64_t field; size_t count; if (!(set_by_pax & PAX_NAME)) { - if (hdr->prefix[0] != '\0') { + if (hdr->tail.posix.prefix[0] != '\0' && + version == ETV_POSIX) { count = strlen(hdr->name) + 1; count += strlen(hdr->prefix) + 1; @@ -356,6 +376,7 @@ int read_header(int fd, tar_header_decoded_t *out) { unsigned int set_by_pax = 0; bool prev_was_zero = false; + E_TAR_VERSION version; uint64_t pax_size; tar_header_t hdr; int ret; @@ -379,8 +400,9 @@ int read_header(int fd, tar_header_decoded_t *out) } prev_was_zero = false; + version = check_version(&hdr); - if (!is_magic_valid(&hdr)) + if (version == ETV_UNKNOWN) goto fail_magic; if (!is_checksum_valid(&hdr)) @@ -398,7 +420,7 @@ int read_header(int fd, tar_header_decoded_t *out) break; } - if (decode_header(&hdr, set_by_pax, out)) + if (decode_header(&hdr, set_by_pax, out, version)) goto fail; return 0; diff --git a/tests/tar_formats.c b/tests/tar_formats.c index e09730b..58cd223 100644 --- a/tests/tar_formats.c +++ b/tests/tar_formats.c @@ -68,5 +68,39 @@ int main(void) clear_header(&hdr); close(fd); + fd = open_read("format-acceptance/ustar-pre-posix.tar"); + assert(read_header(fd, &hdr) == 0); + assert(hdr.sb.st_mode == (S_IFREG | 0644)); + assert(hdr.sb.st_uid == 01750); + assert(hdr.sb.st_gid == 01750); + assert(hdr.sb.st_size == 5); + assert(hdr.sb.st_mtime == 1542905892); + assert(hdr.sb.st_atime == 1542905892); + assert(hdr.sb.st_ctime == 1542905892); + assert(strcmp(hdr.name, "input.txt") == 0); + assert(!hdr.unknown_record); + assert(read_retry(fd, buffer, 5) == 5); + buffer[5] = '\0'; + assert(strcmp(buffer, "test\n") == 0); + clear_header(&hdr); + close(fd); + + fd = open_read("format-acceptance/v7.tar"); + assert(read_header(fd, &hdr) == 0); + assert(hdr.sb.st_mode == (S_IFREG | 0644)); + assert(hdr.sb.st_uid == 01750); + assert(hdr.sb.st_gid == 01750); + assert(hdr.sb.st_size == 5); + assert(hdr.sb.st_mtime == 1542905892); + assert(hdr.sb.st_atime == 1542905892); + assert(hdr.sb.st_ctime == 1542905892); + assert(strcmp(hdr.name, "input.txt") == 0); + assert(!hdr.unknown_record); + assert(read_retry(fd, buffer, 5) == 5); + buffer[5] = '\0'; + assert(strcmp(buffer, "test\n") == 0); + clear_header(&hdr); + close(fd); + return EXIT_SUCCESS; } -- cgit v1.2.3