From 7b83dce96aa065ebebf63daad1e7d61cd1ef7113 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 11 Jan 2023 21:02:37 +0100 Subject: [PATCH] feat: calculate header checksums and validate them against hdr->chksum Fixes #2. --- src/util.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/util.c b/src/util.c index 3a90a5c..e87bd19 100644 --- a/src/util.c +++ b/src/util.c @@ -196,6 +196,32 @@ void minitar_parse_metadata_from_tar_header(const struct tar_header* hdr, struct minitar_strlcpy(metadata->gname, hdr->gname, 32); } +uint32_t minitar_checksum_header(const struct tar_header* hdr) +{ + uint32_t sum = 0; + const uint8_t* ptr = (const uint8_t*)hdr; + + // Sum up all bytes in the header, as unsigned bytes... + while (ptr < (const uint8_t*)hdr + sizeof *hdr) + { + sum += *ptr; + ptr++; + } + + // except for the chksum field, which is treated as... + ptr = (const uint8_t*)hdr->chksum; + while (ptr < (const uint8_t*)hdr->chksum + sizeof hdr->chksum) + { + sum -= *ptr; + ptr++; + } + + // all blanks. + for (size_t i = 0; i < sizeof hdr->chksum; i++) { sum += ' '; } + + return sum; +} + int minitar_validate_header(const struct tar_header* hdr) { #ifdef MINITAR_IGNORE_UNSUPPORTED_TYPES @@ -205,6 +231,8 @@ int minitar_validate_header(const struct tar_header* hdr) hdr->typeflag != '3' && hdr->typeflag != '4' && hdr->typeflag != '5' && hdr->typeflag != '6') return 0; #endif + // FIXME: Warn on checksum mismatch unless header is all blanks? + if (minitar_checksum_header(hdr) != minitar_parse_octal(hdr->chksum)) return 0; return !strncmp(hdr->magic, "ustar", 5); }