read-cache: avoid misaligned reads in index v4

The process for reading the index into memory from disk is to first read its contents into a single memory-mapped file buffer (type 'char *'), then sequentially convert each on-disk index entry into a corresponding incore 'cache_entry'. To access the contents of the on-disk entry for processing, a moving pointer within the memory-mapped file is cast to type 'struct ondisk_cache_entry *'. In index v4, the entries in the on-disk index file are written *without* aligning their first byte to a 4-byte boundary; entries are a variable length (depending on the entry name and whether or not extended flags are used). As a result, casting the 'char *' buffer pointer to 'struct ondisk_cache_entry *' then accessing its contents in a 'SANITIZE=undefined' build can trigger the following error: read-cache.c:1886:46: runtime error: member access within misaligned address <address> for type 'struct ondisk_cache_entry', which requires 4 byte alignment Avoid this error by reading fields directly from the 'char *' buffer, using the 'offsetof' individual fields in 'struct ondisk_cache_entry'. Additionally, add documentation describing why the new approach avoids the misaligned address error, as well as advice on how to improve the implementation in the future. Reported-by: Jeff King <peff@peff.net> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Sep 28, 2022 at 17:19 UTC 4a6ed30f96c0313dc55fc052707b33d4d3518912
1 file changed +42 -19
read-cache.c
+42 -19
@@ -1873,9 +1873,20 @@ static int read_index_extension(struct index_state *istate,
1873 return 0;
1874 }
1875
1876 +/*
1877 + * Parses the contents of the cache entry contained within the 'ondisk' buffer
1878 + * into a new incore 'cache_entry'.
1879 + *
1880 + * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in
1881 + * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access
1882 + * its members. Instead, we use the byte offsets of members within the struct to
1883 + * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all
1884 + * read from an unaligned memory buffer) should read from the 'ondisk' buffer
1885 + * into the corresponding incore 'cache_entry' members.
1886 + */
1887 static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1888 unsigned int version,
1878 - struct ondisk_cache_entry *ondisk,
1889 + const char *ondisk,
1890 unsigned long *ent_size,
1891 const struct cache_entry *previous_ce)
1892 {
@@ -1883,7 +1894,7 @@ static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1894 size_t len;
1895 const char *name;
1896 const unsigned hashsz = the_hash_algo->rawsz;
1886 - const uint16_t *flagsp = (const uint16_t *)(ondisk->data + hashsz);
1897 + const char *flagsp = ondisk + offsetof(struct ondisk_cache_entry, data) + hashsz;
1898 unsigned int flags;
1899 size_t copy_len = 0;
1900 /*
@@ -1901,15 +1912,15 @@ static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1912
1913 if (flags & CE_EXTENDED) {
1914 int extended_flags;
1904 - extended_flags = get_be16(flagsp + 1) << 16;
1915 + extended_flags = get_be16(flagsp + sizeof(uint16_t)) << 16;
1916 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1917 if (extended_flags & ~CE_EXTENDED_FLAGS)
1918 die(_("unknown index entry format 0x%08x"), extended_flags);
1919 flags |= extended_flags;
1909 - name = (const char *)(flagsp + 2);
1920 + name = (const char *)(flagsp + 2 * sizeof(uint16_t));
1921 }
1922 else
1912 - name = (const char *)(flagsp + 1);
1923 + name = (const char *)(flagsp + sizeof(uint16_t));
1924
1925 if (expand_name_field) {
1926 const unsigned char *cp = (const unsigned char *)name;
@@ -1935,20 +1946,32 @@ static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1946
1947 ce = mem_pool__ce_alloc(ce_mem_pool, len);
1948
1938 - ce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec);
1939 - ce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec);
1940 - ce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec);
1941 - ce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec);
1942 - ce->ce_stat_data.sd_dev = get_be32(&ondisk->dev);
1943 - ce->ce_stat_data.sd_ino = get_be32(&ondisk->ino);
1944 - ce->ce_mode = get_be32(&ondisk->mode);
1945 - ce->ce_stat_data.sd_uid = get_be32(&ondisk->uid);
1946 - ce->ce_stat_data.sd_gid = get_be32(&ondisk->gid);
1947 - ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
1949 + /*
1950 + * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced
1951 + * with something more akin to 'load_bitmap_entries_v1()'s use of
1952 + * 'read_be16'/'read_be32'. For consistency with the corresponding
1953 + * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this
1954 + * should be done at the same time as removing references to
1955 + * 'ondisk_cache_entry' there.
1956 + */
1957 + ce->ce_stat_data.sd_ctime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime)
1958 + + offsetof(struct cache_time, sec));
1959 + ce->ce_stat_data.sd_mtime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime)
1960 + + offsetof(struct cache_time, sec));
1961 + ce->ce_stat_data.sd_ctime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime)
1962 + + offsetof(struct cache_time, nsec));
1963 + ce->ce_stat_data.sd_mtime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime)
1964 + + offsetof(struct cache_time, nsec));
1965 + ce->ce_stat_data.sd_dev = get_be32(ondisk + offsetof(struct ondisk_cache_entry, dev));
1966 + ce->ce_stat_data.sd_ino = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ino));
1967 + ce->ce_mode = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mode));
1968 + ce->ce_stat_data.sd_uid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, uid));
1969 + ce->ce_stat_data.sd_gid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, gid));
1970 + ce->ce_stat_data.sd_size = get_be32(ondisk + offsetof(struct ondisk_cache_entry, size));
1971 ce->ce_flags = flags & ~CE_NAMEMASK;
1972 ce->ce_namelen = len;
1973 ce->index = 0;
1951 - oidread(&ce->oid, ondisk->data);
1974 + oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data));
1975
1976 if (expand_name_field) {
1977 if (copy_len)
@@ -2117,12 +2140,12 @@ static unsigned long load_cache_entry_block(struct index_state *istate,
2140 unsigned long src_offset = start_offset;
2141
2142 for (i = offset; i < offset + nr; i++) {
2120 - struct ondisk_cache_entry *disk_ce;
2143 struct cache_entry *ce;
2144 unsigned long consumed;
2145
2124 - disk_ce = (struct ondisk_cache_entry *)(mmap + src_offset);
2125 - ce = create_from_disk(ce_mem_pool, istate->version, disk_ce, &consumed, previous_ce);
2146 + ce = create_from_disk(ce_mem_pool, istate->version,
2147 + mmap + src_offset,
2148 + &consumed, previous_ce);
2149 set_index_entry(istate, i, ce);
2150
2151 src_offset += consumed;