Skip to content

Commit

Permalink
core: Fix large packet buffer overrun
Browse files Browse the repository at this point in the history
The astlpc binding allows negotiation of Tx/Rx region sizes, but the
packet accumulator assumed packet sizes were at most 4096 bytes.  Avoid
buffer overflow by allocating at least the length of the inbound packet
if we have not yet initialised the packet buffer.

Fixes:

=================================================================
==42296==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x621000002500 at pc 0x7ff8a22235ce bp 0x7ffd47469750 sp 0x7ffd47468ef8
WRITE of size 8192 at 0x621000002500 thread T0
    #0 0x7ff8a22235cd in __interceptor_memcpy (/usr/lib/x86_64-linux-gnu/libasan.so.6+0x3a5cd)
    #1 0x7ff8a21ac78b in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34
    #2 0x7ff8a21ac78b in mctp_msg_ctx_add_pkt /home/andrew/src/openbmc/libmctp/core.c:237
    #3 0x7ff8a21af245 in mctp_bus_rx /home/andrew/src/openbmc/libmctp/core.c:495
    #4 0x56458d3f9648 in mctp_astlpc_rx_start astlpc.c:813
    #5 0x56458d3f9648 in mctp_astlpc_poll astlpc.c:931
    #6 0x56458d3fc1f4 in astlpc_test_send_large_packet tests/test_astlpc.c:1111
    #7 0x56458d3efc86 in main tests/test_astlpc.c:1185
    #8 0x7ff8a165dcb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)
    #9 0x56458d3efe7d in _start (/home/andrew/src/openbmc/libmctp/tests/.libs/test_astlpc+0x17e7d)

0x621000002500 is located 0 bytes to the right of 4096-byte region [0x621000001500,0x621000002500)
allocated by thread T0 here:
    #0 0x7ff8a22998d0 in __interceptor_realloc (/usr/lib/x86_64-linux-gnu/libasan.so.6+0xb08d0)
    #1 0x7ff8a21b0533 in __mctp_realloc /home/andrew/src/openbmc/libmctp/alloc.c:48

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.6+0x3a5cd) in __interceptor_memcpy
Shadow bytes around the buggy address:
  0x0c427fff8450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c427fff84a0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff84b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff84c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff84d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff84e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff84f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==42296==ABORTING

Signed-off-by: Andrew Jeffery <[email protected]>
Change-Id: I9d39090cb9246ec2f6c06942d4f2a91fe0df0202
  • Loading branch information
amboar committed Nov 6, 2020
1 parent 5a50891 commit c2b833e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "libmctp-alloc.h"
#include "libmctp-log.h"
#include "libmctp-cmds.h"
#include "range.h"

/* Internal data structures */

Expand Down Expand Up @@ -214,7 +215,7 @@ static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,

/* @todo: finer-grained allocation */
if (!ctx->buf_alloc_size) {
new_alloc_size = 4096;
new_alloc_size = MAX(len, 4096UL);
} else {
new_alloc_size = ctx->buf_alloc_size * 2;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/test_astlpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/random.h>

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Expand Down Expand Up @@ -1070,6 +1071,64 @@ static void astlpc_test_negotiate_mtu_low_high(void)
free(lpc_mem);
}

static void astlpc_test_send_large_packet(void)
{
struct astlpc_endpoint *bmc, *host;
struct astlpc_test ctx;
uint8_t kcs[2] = { 0 };
void *lpc_mem;
int rc;

host = &ctx.host;
bmc = &ctx.bmc;

/* Test harness initialisation */
lpc_mem = calloc(1, 1 * 1024 * 1024);
assert(lpc_mem);

/* BMC initialisation */
rc = endpoint_init(bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 8192, &kcs,
lpc_mem);
assert(!rc);

/* Host initialisation */
rc = endpoint_init(host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, 8192, &kcs,
lpc_mem);
assert(!rc);

ctx.count = 0;
mctp_set_rx_all(bmc->mctp, rx_message, &ctx);

rc = mctp_astlpc_poll(bmc->astlpc);
assert(rc == 0);

rc = mctp_astlpc_poll(host->astlpc);
assert(rc == 0);

ctx.msg = malloc(2 * MCTP_BODY_SIZE(8192));
assert(ctx.msg);

memset(ctx.msg, 0x5a, 2 * MCTP_BODY_SIZE(8192));

rc = mctp_message_tx(host->mctp, 8, ctx.msg, 2 * MCTP_BODY_SIZE(8192));
assert(rc == 0);
rc = mctp_astlpc_poll(bmc->astlpc);
assert(rc == 0);
rc = mctp_astlpc_poll(host->astlpc);
assert(rc == 0);
rc = mctp_astlpc_poll(bmc->astlpc);
assert(rc == 0);
rc = mctp_astlpc_poll(host->astlpc);
assert(rc == 0);

assert(ctx.count == 1);

free(ctx.msg);
endpoint_destroy(host);
endpoint_destroy(bmc);
free(lpc_mem);
}

/* clang-format off */
#define TEST_CASE(test) { #test, test }
static const struct {
Expand Down Expand Up @@ -1109,6 +1168,7 @@ static const struct {
TEST_CASE(astlpc_test_buffers_bad_host_init),
TEST_CASE(astlpc_test_negotiate_increased_mtu),
TEST_CASE(astlpc_test_negotiate_mtu_low_high),
TEST_CASE(astlpc_test_send_large_packet),
};
/* clang-format on */

Expand Down

0 comments on commit c2b833e

Please sign in to comment.