]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Fix runtime LSAN/ASAN error in fr_in6addr_mask()
authorJorge Pereira <jpereiran@gmail.com>
Tue, 14 Mar 2023 18:27:21 +0000 (15:27 -0300)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 17 Mar 2023 13:24:35 +0000 (09:24 -0400)
Such error:

src/lib/misc.c:1266:34: runtime error: load of misaligned address 0x00016f8b1d54 for type 'const uint64_t' (aka 'const unsigned long long'), which requires 8 byte alignment
0x00016f8b1d54: note: pointer points here
  e0 89 29 05 00 00 00 00  00 00 00 00 00 00 ff ff  cb 00 71 00 00 00 00 00  e0 89 29 05 01 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/lib/misc.c:1266:34 in

src/lib/misc.c

index 36378ec40c9387da9b4c4c1081aeaa93e59a7142..b80b9ce468528f05b4dc2af26250431583d5282e 100644 (file)
@@ -1254,6 +1254,7 @@ struct in_addr fr_inaddr_mask(struct in_addr const *ipaddr, uint8_t prefix)
 struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix)
 {
        uint64_t const *p = (uint64_t const *) ipaddr;
+       uint64_t addr;                                  /* Needed for alignment */
        uint64_t ret[2], *o = ret;
 
        if (prefix > 128) prefix = 128;
@@ -1263,14 +1264,17 @@ struct in6_addr fr_in6addr_mask(struct in6_addr const *ipaddr, uint8_t prefix)
 
        if (prefix >= 64) {
                prefix -= 64;
-               *o++ = 0xffffffffffffffffULL & *p++;    /* lhs portion masked */
+               memcpy(&addr, p, sizeof(addr));         /* Needed for aligned access (ubsan) */
+               *o++ = 0xffffffffffffffffULL & addr;    /* lhs portion masked */
+               p++;
        } else {
                ret[1] = 0;                             /* rhs portion zeroed */
        }
 
        /* Max left shift is 63 else we get overflow */
        if (prefix > 0) {
-               *o = htonll(~((uint64_t)(0x0000000000000001ULL << (64 - prefix)) - 1)) & *p;
+               memcpy(&addr, p, sizeof(addr));         /* Needed for aligned access (ubsan) */
+               *o = htonll(~((uint64_t)(0x0000000000000001ULL << (64 - prefix)) - 1)) & addr;
        } else {
                *o = 0;
        }