mctp: serial: reject zero-length frames to prevent rx buffer overflow
Subsystems (4)
PATCH 1/1 mctp: serial: reject zero-length frames to prevent rx buffer overflow
The MCTP serial receive state machine reads a frame length byte in mctp_serial_push_header() case 2 and validates it upper-bound-only:
if (c > MCTP_SERIAL_FRAME_MTU) {
dev->rxstate = STATE_ERR;
} else {
dev->rxlen = c;
dev->rxpos = 0;
dev->rxstate = STATE_DATA;
...
}A length of zero passes this check, so rxlen is set to 0 and the state machine advances to STATE_DATA. In mctp_serial_push() STATE_DATA, the incoming byte is stored and rxpos incremented before the terminator is tested:
dev->rxbuf[dev->rxpos] = c;
dev->rxpos++;
dev->rxstate = STATE_DATA;
if (dev->rxpos == dev->rxlen) {
dev->rxpos = 0;
dev->rxstate = STATE_TRAILER;
}With rxlen == 0 the "rxpos == rxlen" terminator can never fire (rxpos is already 1 on the first data byte), so subsequent bytes are written past the end of the fixed 74-byte rxbuf, which is the last member of the netdev private area. Every following data byte is an attacker-controlled 1-byte out-of-bounds heap write, and the overflow continues until a frame (0x7e) or escape byte resets the parser -- effectively unbounded.
Reaching this requires CAP_NET_ADMIN to attach the N_MCTP line discipline and bring the resulting mctpserialN netdev up, after which the bytes arrive via the tty receive path.
Reject a zero-length frame in the header parser, matching the existing upper-bound rejection.
KASAN, on a frame of 0x7e 0x01 0x00 followed by data bytes:
UBSAN: array-index-out-of-bounds in drivers/net/mctp/mctp-serial.c:370 index 74 is out of range for type 'u8 [74]' BUG: KASAN: slab-out-of-bounds in mctp_serial_tty_receive_buf Write of size 1 at addr ... by task kworker/u16:0 mctp_serial_tty_receive_buf tty_ldisc_receive_buf flush_to_ldisc Allocated by task 152: alloc_netdev_mqs mctp_serial_open
Found by 0sec (https://0sec.ai).
--- drivers/net/mctp/mctp-serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/mctp/mctp-serial.c +1 -1
@@ -313,7 +313,7 @@ static void mctp_serial_push_header(struct mctp_serial *dev, u8 c) }break ;case 2 : -if (c >MCTP_SERIAL_FRAME_MTU ) { +if (c ==0 || c >MCTP_SERIAL_FRAME_MTU ) { dev->rxstate =STATE_ERR ; }else { dev->rxlen = c;
Signature · 1 line
2.43.0
Jeremy Kerr
maintainerHi Doruk,
Thanks for the report. The analysis looks solid, but I do have a recommendation for a different fix. One comment inline too.
Quoted 49 lines
The MCTP serial receive state machine reads a frame length byte in
mctp_serial_push_header()case 2 and validates it upper-bound-only:if (c > MCTP_SERIAL_FRAME_MTU) { dev->rxstate = STATE_ERR; } else { dev->rxlen = c; dev->rxpos = 0; dev->rxstate = STATE_DATA; ... }A length of zero passes this check, so rxlen is set to 0 and the state machine advances to
STATE_DATA. Inmctp_serial_push()STATE_DATA, the incoming byte is stored and rxpos incremented before the terminator is tested:dev->rxbuf[dev->rxpos] = c; dev->rxpos++; dev->rxstate = STATE_DATA; if (dev->rxpos == dev->rxlen) { dev->rxpos = 0; dev->rxstate = STATE_TRAILER; }With rxlen == 0 the "rxpos == rxlen" terminator can never fire (rxpos is already 1 on the first data byte), so subsequent bytes are written past the end of the fixed 74-byte rxbuf, which is the last member of the netdev private area. Every following data byte is an attacker-controlled 1-byte out-of-bounds heap write, and the overflow continues until a frame (
0x7e) or escape byte resets the parser -- effectively unbounded.Reaching this requires
CAP_NET_ADMINto attach theN_MCTPline discipline and bring the resulting mctpserialN netdev up, after which the bytes arrive via the tty receive path.Reject a zero-length frame in the header parser, matching the existing upper-bound rejection.
KASAN, on a frame of
0x7e0x010x00followed by data bytes:UBSAN: array-index-out-of-bounds in drivers/net/mctp/mctp-serial.c:370 index 74 is out of range for type 'u8 [74]' BUG: KASAN: slab-out-of-bounds in mctp_serial_tty_receive_buf Write of size 1 at addr ... by task kworker/u16:0 mctp_serial_tty_receive_buf tty_ldisc_receive_buf flush_to_ldisc Allocated by task 152:
Quoted 7 lines
alloc_netdev_mqs
mctp_serial_openFound by 0sec (https://0sec.ai).
Fixes a0c2ccd9b5ad ("mctp: Add MCTP-over-serial transport binding")Assisted-by 0sec
I assume this needs to be in the Assisted-by format.
Quoted 15 lines
Signed-off-by Doruk Tan Ozturk <[email protected]>---
drivers/net/mctp/mctp-serial.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/mctp/mctp-serial.c +0
-0@@ -313,7 +313,7 @@ static void mctp_serial_push_header(struct mctp_serial *dev, u8 c) } break;case 2:
drivers/net/mctp/mctp-serial.c +1
-1-if (c >MCTP_SERIAL_FRAME_MTU ) { +if (c ==0 || c >MCTP_SERIAL_FRAME_MTU ) {
Quoted 3 lines
dev->rxstate=STATE_ERR;
} else {
dev->rxlen= c;
We probably want to advance directly to STATE_TRAILER instead, in order to consume the trailer and framing bytes for cases when it's a legitimate (although somewhat useless) zero-length frame.
Perhaps:
drivers/net/mctp/mctp-serial.c +1 -1
@@ -318,7 +318,7 @@ static void mctp_serial_push_header(struct mctp_serial *dev, u8 c) }else { dev->rxlen = c; dev->rxpos =0 ; - dev->rxstate =STATE_DATA ; + dev->rxstate = c >0 ?STATE_DATA :STATE_TRAILER ; dev->rxfcs =crc_ccitt_byte (dev->rxfcs, c); }break ;
We'll still land in STATE_ERROR on incorrect framing, but just during trailer parse instead.
We could then add an early error path rather than creating a zero-length skb (which then gets rejected by the MCTP core), but that would be best done separately.
Cheers,
Jeremy