]> git.ipfire.org Git - thirdparty/git.git/commitdiff
index-pack, unpack-objects: use get_be32() for reading pack header
authorJeff King <peff@peff.net>
Sun, 19 Jan 2025 13:25:47 +0000 (08:25 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Jan 2025 16:42:56 +0000 (08:42 -0800)
Both of these commands read the incoming pack into a static unsigned
char buffer in BSS, and then parse it by casting the start of the buffer
to a struct pack_header. This can result in SIGBUS on some platforms if
the compiler doesn't place the buffer in a position that is properly
aligned for 4-byte integers.

This reportedly happens with unpack-objects (but not index-pack) on
sparc64 when compiled with clang (but not gcc). But we are definitely in
the wrong in both spots; since the buffer's type is unsigned char, we
can't depend on larger alignment. When it works it is only because we
are lucky.

We'll fix this by switching to get_be32() to read the headers (just like
the last few commits similarly switched us to put_be32() for writing
into the same buffer).

It would be nice to factor this out into a common helper function, but
the interface ends up quite awkward. Either the caller needs to hardcode
how many bytes we'll need, or it needs to pass us its fill()/use()
functions as pointers. So I've just fixed both spots in the same way;
this is not code that is likely to be repeated a third time (most of the
pack reading code uses an mmap'd buffer, which should be properly
aligned).

I did make one tweak to the shared code: our pack_version_ok() macro
expects us to pass the big-endian value we'd get by casting. We can
introduce a "native" variant which uses the host integer ordering.

Reported-by: Koakuma <koachan@protonmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/index-pack.c
builtin/unpack-objects.c
pack.h

index bab42dfc2a491abb09592659ec19ffdd30c93fc1..5f0ff1ce04a22ff96d8661bcd06145a311b291ce 100644 (file)
@@ -363,16 +363,18 @@ static const char *open_pack_file(const char *pack_name)
 
 static void parse_pack_header(void)
 {
-       struct pack_header *hdr = fill(sizeof(struct pack_header));
+       unsigned char *hdr = fill(sizeof(struct pack_header));
 
        /* Header consistency check */
-       if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
+       if (get_be32(hdr) != PACK_SIGNATURE)
                die(_("pack signature mismatch"));
-       if (!pack_version_ok(hdr->hdr_version))
+       hdr += 4;
+       if (!pack_version_ok_native(get_be32(hdr)))
                die(_("pack version %"PRIu32" unsupported"),
-                       ntohl(hdr->hdr_version));
+                   get_be32(hdr));
+       hdr += 4;
 
-       nr_objects = ntohl(hdr->hdr_entries);
+       nr_objects = get_be32(hdr);
        use(sizeof(struct pack_header));
 }
 
index 31614472749bf9784854aa81a3e9e1f9bb4008d4..fc3de6dac8913e32aa374df0fc845b04e705107b 100644 (file)
@@ -576,15 +576,16 @@ static void unpack_one(unsigned nr)
 static void unpack_all(void)
 {
        int i;
-       struct pack_header *hdr = fill(sizeof(struct pack_header));
+       unsigned char *hdr = fill(sizeof(struct pack_header));
 
-       nr_objects = ntohl(hdr->hdr_entries);
-
-       if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
+       if (get_be32(hdr) != PACK_SIGNATURE)
                die("bad pack file");
-       if (!pack_version_ok(hdr->hdr_version))
+       hdr += 4;
+       if (!pack_version_ok_native(get_be32(hdr)))
                die("unknown pack file version %"PRIu32,
-                       ntohl(hdr->hdr_version));
+                   get_be32(hdr));
+       hdr += 4;
+       nr_objects = get_be32(hdr);
        use(sizeof(struct pack_header));
 
        if (!quiet)
diff --git a/pack.h b/pack.h
index 3ab9e3f60c0b0341c3cb37e9026bdc217ba6aa97..33b42fdad597dcb53e0a196a6a23ded01234f0e9 100644 (file)
--- a/pack.h
+++ b/pack.h
@@ -13,7 +13,8 @@ struct repository;
  */
 #define PACK_SIGNATURE 0x5041434b      /* "PACK" */
 #define PACK_VERSION 2
-#define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3))
+#define pack_version_ok(v) pack_version_ok_native(ntohl(v))
+#define pack_version_ok_native(v) ((v) == 2 || (v) == 3)
 struct pack_header {
        uint32_t hdr_signature;
        uint32_t hdr_version;