]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: grpc: Fix several unaligned 32/64 bits accesses
authorFrederic Lecaille <flecaille@haproxy.com>
Mon, 15 Apr 2024 07:57:37 +0000 (09:57 +0200)
committerFrederic Lecaille <flecaille@haproxy.com>
Tue, 16 Apr 2024 05:37:28 +0000 (07:37 +0200)
There were several places in grpc and its dependency protobuf where unaligned
accesses were done. Read accesses to 32 (resp. 64) bits values should be performed
by read_u32() (resp. read_u64()).
Replace these unligned read accesses by correct calls to these functions.
Same fixes for doubles and floats.

Such unaligned read accesses could lead to crashes with bus errors on CPU
archictectures which do not fix them at run time.

This patch depends on this previous commit:
    861199fa71 MINOR: net_helper: Add support for floats/doubles.

Must be backported as far as 2.6.

include/haproxy/protobuf.h
src/sample.c

index 009bd13d70f58bf173c99605622d6a9a2a1c7837..512288b5ec34de243073dd995fcfd5ccbc30a34e 100644 (file)
@@ -365,13 +365,13 @@ int protobuf_smp_store_64bit(struct sample *smp, int type,
        case PBUF_T_64BIT_FIXED64:
        case PBUF_T_64BIT_SFIXED64:
                smp->data.type = SMP_T_SINT;
-               smp->data.u.sint = pbuf_le64toh(*(uint64_t *)pos);
+               smp->data.u.sint = pbuf_le64toh(read_u64(pos));
                smp->flags = SMP_F_VOL_TEST;
                break;
 
        case PBUF_T_64BIT_DOUBLE:
                smp->data.type = SMP_T_SINT;
-               smp->data.u.sint = pbuf_le64toh(*(double *)pos);
+               smp->data.u.sint = pbuf_le64toh(read_dbl(pos));
                smp->flags = SMP_F_VOL_TEST;
                break;
 
@@ -455,19 +455,19 @@ int protobuf_smp_store_32bit(struct sample *smp, int type,
 
        case PBUF_T_32BIT_FIXED32:
                smp->data.type = SMP_T_SINT;
-               smp->data.u.sint = pbuf_le32toh(*(uint32_t *)pos);
+               smp->data.u.sint = pbuf_le32toh(read_u32(pos));
                smp->flags = SMP_F_VOL_TEST;
                break;
 
        case PBUF_T_32BIT_SFIXED32:
                smp->data.type = SMP_T_SINT;
-               smp->data.u.sint = (int32_t)pbuf_le32toh(*(uint32_t *)pos);
+               smp->data.u.sint = (int32_t)pbuf_le32toh(read_u32(pos));
                smp->flags = SMP_F_VOL_TEST;
                break;
 
        case PBUF_T_32BIT_FLOAT:
                smp->data.type = SMP_T_SINT;
-               smp->data.u.sint = pbuf_le32toh(*(float *)pos);
+               smp->data.u.sint = pbuf_le32toh(read_flt(pos));
                smp->flags = SMP_F_VOL_TEST;
                break;
 
index 8f46d31b96124cad48870f4f899c6ba8b6328bb7..334782c1732a44146fe492f6b6ccdef1aa9aa79c 100644 (file)
@@ -3818,7 +3818,7 @@ static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void
        while (grpc_left > GRPC_MSG_HEADER_SZ) {
                size_t grpc_msg_len, left;
 
-               grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
+               grpc_msg_len = left = ntohl(read_u32(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
 
                pos += GRPC_MSG_HEADER_SZ;
                grpc_left -= GRPC_MSG_HEADER_SZ;