From patchwork Fri Feb 28 17:47:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 869288 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp932405wrz; Fri, 28 Feb 2025 09:49:29 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU2nWMQrPdg8X5Nyu9xIFX8CUSKTCKwl8QCvZybCy4+i2f6VnUz1n+ePKNMeCbFUU4eK8To1g==@linaro.org X-Google-Smtp-Source: AGHT+IGcHmqndFQ3XAcOKPXue6GKwCrAdeD0QOXPWfn12YPrwAOMSTmNouAtRx/uwZWNAc6k/GAf X-Received: by 2002:a05:620a:4549:b0:7c0:b0b7:493d with SMTP id af79cd13be357-7c39c499702mr641808885a.11.1740764969627; Fri, 28 Feb 2025 09:49:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740764969; cv=none; d=google.com; s=arc-20240605; b=k/pDzaJaGcJ783326rPtTPx2ckmg9KrSO1vydHYojifariVJ3apXIRncNreULAF7XY t5cB1izrz4ZcinRUcFWDGRrgh3bIO9xY4+sEKI3tyVhtyRkEO3VafBzIuqM1Sa6DBWX5 3AYDyxyXm/ctqOMN1PNaIbj6r3nwgZt1W47CCw58COo5TVB5IbLT1KT0hmDpakdp3L2R Yc3hK3omGsmP+0OxzI90A0LR3gbzVzdGAJtizyFFgLBxrP1TJw6DMNy88SMuSImMakfb BTkzvQkztcE33DgzhFay+oNLO9iVZPBgmkidfZ9lBY6/pSUs2SCgTV2Xny11szDC/4a3 8b4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eN07gCc2oP06ECRd8sreJOokFNA6cGgOnl4Jc0WaVu4=; fh=BmoYN8PU6hM6Ugy/aN+KwCx5nqOEFbNcYKccUa8g/kc=; b=iy3LRmey/5uxNk4+vSIX8E5PEivJbITsk0igsI/q7TuCK839xNCubYf4GWiEWyUAls reUr5NMaYag4JKOdYJYMyETPaoDdb2iT3UzYB9a6ybejmhNawEk9mymDVIGGaT2cfTUV I7JXN68V1ZA9ZsqujqcvwZBsx4ghqnTYJ/2qKpivwqF3IJYETVWaH/xKEgcy0fOqlXJd NOBHEM+0mHe3fqPINgdNq2X9Cjm7+sGGpC2MJ7XLreEQKREUt9cIMb4cEdGm8ZsztsB5 kBWc5FRe6c1zJPcCb6EqT+bp0Uq8dwZ7AWfO1MK0WQyR4QeUQd2CSz7CunXlsimkBRdo UH8A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=w4k8A0Ar; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7c36ff6c4fcsi355693385a.275.2025.02.28.09.49.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Feb 2025 09:49:29 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=w4k8A0Ar; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1to4TT-0007g1-Bo; Fri, 28 Feb 2025 12:48:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1to4T7-0007R9-Br for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:13 -0500 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1to4T1-0005Mc-NJ for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:10 -0500 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-4398738217aso21724025e9.3 for ; Fri, 28 Feb 2025 09:48:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1740764886; x=1741369686; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eN07gCc2oP06ECRd8sreJOokFNA6cGgOnl4Jc0WaVu4=; b=w4k8A0ArvYP84JD1o+L4/HEwueeIZ4o9XETaih4MKeiD2BFqS//Zi339KMooC4Hp9q aeZmHIGZT8pwWNRfTsuWO8gs8pZg1sTX9gesYtSHUa/NSFZ8bzW2g7FyXIDLjr2HMW9t MwQ3dPhoqjKhGpjM8sCYiEA4iSqNxHKhUPDMVew6tqYv5wfABuKarMJ+Di3Ph3K60UvY W/+8rSqLiVyX0o9NrbXYRCE93ugzGbyQe6EVv3mQ8EvORRSHnlTlDzchiJZuajYUk/EC Arg1HqGUKkTeDequbHZTaoQ8vNAhivPsQX+WIjQcz+XE9/eltyerghJ0IJk22dsnqJ91 lNDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740764886; x=1741369686; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eN07gCc2oP06ECRd8sreJOokFNA6cGgOnl4Jc0WaVu4=; b=SeZ/5Sa7wJ1xTyku2iN0KT6BxuPTh1NtX9PeChWhjMwgMRqLhP2U6TFv21na3MFRs5 BrpLjTpRp5hmZuihQDd2FklgqoUM6R/6yZGEMrc7S/3NxjELhka657i+o7LmlIyrXOml ZB1c2uxhU8aL0zLrdkFU0JxAkj7udyrrUm+6ODl2uMkDJ7EJm7J4ImTyW+SRP1FT1WFr e7SXcCDGG87wpyknZGopNvTutO63FzLbDk10PWpILgjzh3BmFg2JlE1iVpx9WBSINuIs BtxNVB0sXucZcWBH5eTtr9qMhpsQzzf8oA0BZLel3wd4cloLYo8lszRKvsoouWn+lz3J +MbQ== X-Forwarded-Encrypted: i=1; AJvYcCXJ5FGETEWdSoRiJcZhw465WZcz2qSSJxePxHfqy/NbAqKkZMAw4xVq/j8L88dsvDooNQBhvtxTUxGx@nongnu.org X-Gm-Message-State: AOJu0YyT4Ke0RuVxhB68ah2IESZXptasve+e0vY0n0hn807DOU42yaYZ TY6sKppdi2rOgOItjMle/ZtG4ibm/CO0Lw0BroykS3DKbUJ5U9gZjXXOueyERlwZxs3MF5zCGa+ 1 X-Gm-Gg: ASbGncsJinGwE0OndsHjFWMp/qI8Epo30gf/R94+dIBNlVzCRqQHDtJWnEn6yyY9qyD mJ227f5DuUEEOw0lJ/hHrqvuu4ekWMl8/G9ks4fLZDfQShIJTZDMuERE2Y2GdIXJJ8OY5v4ein7 PIqjqHzBphUUEF/PjVz1fT0tAzg64QsJ7inB51qTvFhDu0xbyOi0gCVRF7SXwPVobXWof1Owgdr U0OhJwf09qr6lkN7GW7legygl1jDN1zw5qYdOdghT3Z/8QSVNATYJeH/Ed/7/Jy8nLmI9DxH7J9 GHBFET1dtR62ptP6MIt12/x9LFdR/n2S X-Received: by 2002:a05:600c:1d82:b0:439:9543:9488 with SMTP id 5b1f17b1804b1-43ba6727b65mr33765805e9.21.1740764885856; Fri, 28 Feb 2025 09:48:05 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-390e4795da5sm5979556f8f.15.2025.02.28.09.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2025 09:48:04 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Jason Wang Subject: [PATCH 1/3] hw/net/smc91c111: Sanitize packet numbers Date: Fri, 28 Feb 2025 17:47:59 +0000 Message-ID: <20250228174802.1945417-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250228174802.1945417-1-peter.maydell@linaro.org> References: <20250228174802.1945417-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The smc91c111 uses packet numbers as an index into its internal s->data[][] array. Valid packet numbers are between 0 and 3, but the code does not generally check this, and there are various places where the guest can hand us an arbitrary packet number and cause an out-of-bounds access to the data array. Add validation of packet numbers. The datasheet is not very helpful about how guest errors like this should be handled: it says nothing on the subject, and none of the documented error conditions are relevant. We choose to log the situation with LOG_GUEST_ERROR and silently ignore the attempted operation. In the places where we are about to access the data[][] array using a packet number and we know the number is valid because we got it from somewhere that has already validated, we add an assert() to document that belief. Cc: qemu-stable@nongnu.org Signed-off-by: Peter Maydell --- hw/net/smc91c111.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/hw/net/smc91c111.c b/hw/net/smc91c111.c index 0e13dfa18b2..2295c6acf25 100644 --- a/hw/net/smc91c111.c +++ b/hw/net/smc91c111.c @@ -118,6 +118,11 @@ static const VMStateDescription vmstate_smc91c111 = { #define RS_TOOSHORT 0x0400 #define RS_MULTICAST 0x0001 +static inline bool packetnum_valid(int packet_num) +{ + return packet_num >= 0 && packet_num < NUM_PACKETS; +} + /* Update interrupt status. */ static void smc91c111_update(smc91c111_state *s) { @@ -218,6 +223,17 @@ static void smc91c111_pop_tx_fifo_done(smc91c111_state *s) /* Release the memory allocated to a packet. */ static void smc91c111_release_packet(smc91c111_state *s, int packet) { + if (!packetnum_valid(packet)) { + /* + * Data sheet doesn't document behaviour in this guest error + * case, and there is no error status register to report it. + * Log and ignore the attempt. + */ + qemu_log_mask(LOG_GUEST_ERROR, + "smc91c111: attempt to release invalid packet %d\n", + packet); + return; + } s->allocated &= ~(1 << packet); if (s->tx_alloc == 0x80) smc91c111_tx_alloc(s); @@ -239,6 +255,8 @@ static void smc91c111_do_tx(smc91c111_state *s) return; for (i = 0; i < s->tx_fifo_len; i++) { packetnum = s->tx_fifo[i]; + /* queue_tx checked the packet number was valid */ + assert(packetnum_valid(packetnum)); p = &s->data[packetnum][0]; /* Set status word. */ *(p++) = 0x01; @@ -287,6 +305,17 @@ static void smc91c111_do_tx(smc91c111_state *s) /* Add a packet to the TX FIFO. */ static void smc91c111_queue_tx(smc91c111_state *s, int packet) { + if (!packetnum_valid(packet)) { + /* + * Datasheet doesn't document behaviour in this error case, and + * there's no error status register we could report it in. + * Log and ignore. + */ + qemu_log_mask(LOG_GUEST_ERROR, + "smc91c111: attempt to queue invalid packet %d\n", + packet); + return; + } if (s->tx_fifo_len == NUM_PACKETS) return; s->tx_fifo[s->tx_fifo_len++] = packet; @@ -457,6 +486,13 @@ static void smc91c111_writeb(void *opaque, hwaddr offset, n = s->rx_fifo[0]; else n = s->packet_num; + if (!packetnum_valid(n)) { + /* Datasheet doesn't document what to do here */ + qemu_log_mask(LOG_GUEST_ERROR, + "smc91c111: attempt to write data to invalid packet %d\n", + n); + return; + } p = s->ptr & 0x07ff; if (s->ptr & 0x4000) { s->ptr = (s->ptr & 0xf800) | ((s->ptr + 1) & 0x7ff); @@ -605,6 +641,13 @@ static uint32_t smc91c111_readb(void *opaque, hwaddr offset) n = s->rx_fifo[0]; else n = s->packet_num; + if (!packetnum_valid(n)) { + /* Datasheet doesn't document what to do here */ + qemu_log_mask(LOG_GUEST_ERROR, + "smc91c111: attempt to read data from invalid packet %d\n", + n); + return 0; + } p = s->ptr & 0x07ff; if (s->ptr & 0x4000) { s->ptr = (s->ptr & 0xf800) | ((s->ptr + 1) & 0x07ff); @@ -713,6 +756,8 @@ static ssize_t smc91c111_receive(NetClientState *nc, const uint8_t *buf, size_t return -1; s->rx_fifo[s->rx_fifo_len++] = packetnum; + /* allocate_packet() will not hand us back an invalid packet number */ + assert(packetnum_valid(packetnum)); p = &s->data[packetnum][0]; /* ??? Multicast packets? */ status = 0; From patchwork Fri Feb 28 17:48:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 869290 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp932480wrz; Fri, 28 Feb 2025 09:49:42 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXDl1pm54VsRTEHAqLF5W56fLRtb9UztxhnGNT82WP56dBkK2yr/uas7pzkV+SvWOYMLKHKqA==@linaro.org X-Google-Smtp-Source: AGHT+IHl5xklLCBs+m2/u3EZGgUodL1qd7utN6ppbanumPIfn73WUhwhYCtjSDxMgI/3csyQURHx X-Received: by 2002:ac8:5841:0:b0:471:f272:9862 with SMTP id d75a77b69052e-474bc0e1ad0mr64235101cf.41.1740764982103; Fri, 28 Feb 2025 09:49:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740764982; cv=none; d=google.com; s=arc-20240605; b=TYbyUueamugTZiYi3535VbZ5plTLUHXXe7Q2I/s1kCmbPL197VnGddLwHMRosAE426 NMyfvahGr3BQbsjiMBt2gMDgi2L2FOL78lFku7zWnYvDH4QP2XVdwu7/Q+Nc1ei6s93S Y/wBVZW6QiXatz9VNjSXjce31R5OZEZrtxtcety0dgUIy8VrO3YcF0J8ch+4/pRVeNBe AjtEEyTyjM/LtDrIIhueYGHSH6aIG2bSnKzda2vXN6z/kPic3uzIIqB49aCaQnfIqGHO tKwyxBB3ITkteIIEJX13ITE8kxQNG/3MRXtr/dAxkqb67kgayX4CFLm3wGO2JBSCoWbI MiPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bK4QMP2l/mN25359h/sPhHBhlHOYz8CuG9/oUAWNxfg=; fh=xW6vQQeIwOkDnx8JQItVO2X11dSri8i+qJRUeLGwd8k=; b=gyaqdX4Z+Bl0g0efWwAeSHLh3wMH0GKiUWvWxEuCOFXG6EgKyTSgnZOtkCR3inOKKy s4urAQkOE7lzqmxnhQSuWXSQOt7CmCdBpGKLqVPCdRKZ5XSiWoAWtZNMclcCkxlvnHlB xx7GlHEYBcOyan/yULN6T3BEodFi/Rob8Zs6MgE1SSMypls05Hxqhb+7D0HlNkK9cNXj fvfAogiNRQCLsuyl37lnHMiMCUlBZJCcFKnOwdYNmOhTGWAoMRZiZkLYmMwqq5EepAZk rMDyxJhiibcwsghlxXCzbSpWXZ1BiXMDNSo+MMTr6oyhc6isXAfXgAg1QqAeDF4o+7Du zYbQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sw+Ks4yz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-474725445bbsi35275371cf.620.2025.02.28.09.49.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Feb 2025 09:49:42 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sw+Ks4yz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1to4TX-0007q0-3A; Fri, 28 Feb 2025 12:48:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1to4TB-0007VQ-7t for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:17 -0500 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1to4T8-0005NE-7Q for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:16 -0500 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-439a4dec9d5so26072755e9.0 for ; Fri, 28 Feb 2025 09:48:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1740764888; x=1741369688; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bK4QMP2l/mN25359h/sPhHBhlHOYz8CuG9/oUAWNxfg=; b=sw+Ks4yzndvavcUuaKIA8jcek9EtBFjwE2jeOYr9w0MdiwDFDExBkbGT++Ye1wsrRJ 1x5TJSpyHZITrR6HX4ymq2qSxdcCOb9AXsIChXUKb3am99FCBzO8eO+rvflDrt31rl0d Lszhx3ytxBde4DZqXZ8MlRATkwL1gFrUaMtylIFO8GDV8YdCDF17RbVmxeEbDjVQFZdN MIaUZgqctt9FXQNOJd5M0MI2F+7UVf/5p0dsK1GX5W6UhLIE3C2QRkqnPqQ4q9nI1YR3 q9lGTlnsW/zVutDosijHjMh9ohDmVXS4sHCeI1qHm0f/V4Shujnv77CXpSbkSdUTBjei m3wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740764888; x=1741369688; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bK4QMP2l/mN25359h/sPhHBhlHOYz8CuG9/oUAWNxfg=; b=bGBkFR8ajNVHkCo0COv+gjnLl38sS8//aQpfabTJEK8n9vrjpGBIU7g+2pLj8kA0qg iQ9SgULCQ5Ff/XiR99ntn030eP7JuHOlxT05zL3lW11oCf125jM69Wd7PVSE/GkNLqtx SKgOiNykBTcEehnaqK5nWebaSQwEMVwH0P2qWeyFs141BD84xb+vAm/rQXK1HA2liS7M MsLWZ82zII6SSqhquVRB6gO1KhEOSbSAKfGQpa8BXPx64O3pC6ep7wcLu2XOOYzJ7MpE YG2Mn3ydPnQZAGs2zPolDKIv5tQ6v93QQhe6Ik5Yld9718YNSDn63gBGdrlqKZavl82n sW2Q== X-Forwarded-Encrypted: i=1; AJvYcCUooZXYCLBp8bc6dte1g+PDEn8eELDyL483UhF3fcaPoCIF4DxQK0vsZEvYdDaPT93ahJZL1ExJlAE1@nongnu.org X-Gm-Message-State: AOJu0YwO4TOE1GihLeqJicdNQaR6Z8syRVGszY0H4bZWCnJcKs+1evbR 3EY6ixaUQ3Ly8qcYPnCuIEusH+mhsGvmvHS24jsKvFo/atSNCSqhF9FrMRSAC5M= X-Gm-Gg: ASbGnctFxxu1tvazD6YoQPTfWpKZHLkYPaOgbuCJfT+7FtKySADuhrI8OORLx8Wp9Qt Yu3YitwymaZ6pvLK9Z2OljDVccsn0Dd2023RCJtOV83/2tN8GuRoQf8AUpc/W/KWcM40Ne1uyyx Wn40oM0ImNnBI08Om8kJ6vBqa4Gd6Zug2t1fmptDQ5x6Hhk8Viuqz4i0kQOEegqOmpw3ravbhGy hxbL3k9f15R6IbhQAFkf+9QKiLfOuupu/cDu5flkOCDgBKO7eU3jfF+mLHxDQVzBALkTsL3ysR/ Z4Y/LkUK/8k0A5wjOTgpfu0Vc0fP2AQ9 X-Received: by 2002:a05:600c:1c1f:b0:439:9eba:93bb with SMTP id 5b1f17b1804b1-43ba67049b8mr48330735e9.18.1740764887948; Fri, 28 Feb 2025 09:48:07 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-390e4795da5sm5979556f8f.15.2025.02.28.09.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2025 09:48:06 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Jason Wang Subject: [PATCH 2/3] hw/net/smc91c111: Sanitize packet length on tx Date: Fri, 28 Feb 2025 17:48:00 +0000 Message-ID: <20250228174802.1945417-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250228174802.1945417-1-peter.maydell@linaro.org> References: <20250228174802.1945417-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x32b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org When the smc91c111 transmits a packet, it must read a control byte which is at the end of the data area and CRC. However, we don't sanitize the length field in the packet buffer, so if the guest sets the length field to something large we will try to read past the end of the packet data buffer when we access the control byte. As usual, the datasheet says nothing about the behaviour of the hardware if the guest misprograms it in this way. It says only that the maximum valid length is 2048 bytes. We choose to log the guest error and silently drop the packet. This requires us to factor out the "mark the tx packet as complete" logic, so we can call it for this "drop packet" case as well as at the end of the loop when we send a valid packet. Cc: qemu-stable@nongnu.org Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2742 Signed-off-by: Peter Maydell --- hw/net/smc91c111.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/hw/net/smc91c111.c b/hw/net/smc91c111.c index 2295c6acf25..23ca99f926a 100644 --- a/hw/net/smc91c111.c +++ b/hw/net/smc91c111.c @@ -22,6 +22,13 @@ /* Number of 2k memory pages available. */ #define NUM_PACKETS 4 +/* + * Maximum size of a data frame, including the leading status word + * and byte count fields and the trailing CRC, last data byte + * and control byte (per figure 8-1 in the Microchip Technology + * LAN91C111 datasheet). + */ +#define MAX_PACKET_SIZE 2048 #define TYPE_SMC91C111 "smc91c111" OBJECT_DECLARE_SIMPLE_TYPE(smc91c111_state, SMC91C111) @@ -240,6 +247,16 @@ static void smc91c111_release_packet(smc91c111_state *s, int packet) smc91c111_flush_queued_packets(s); } +static void smc91c111_complete_tx_packet(smc91c111_state *s, int packetnum) +{ + if (s->ctr & CTR_AUTO_RELEASE) { + /* Race? */ + smc91c111_release_packet(s, packetnum); + } else if (s->tx_fifo_done_len < NUM_PACKETS) { + s->tx_fifo_done[s->tx_fifo_done_len++] = packetnum; + } +} + /* Flush the TX FIFO. */ static void smc91c111_do_tx(smc91c111_state *s) { @@ -263,6 +280,17 @@ static void smc91c111_do_tx(smc91c111_state *s) *(p++) = 0x40; len = *(p++); len |= ((int)*(p++)) << 8; + if (len >= MAX_PACKET_SIZE) { + /* + * Datasheet doesn't say what to do here, and there is no + * relevant tx error condition listed. Log, and drop the packet. + */ + qemu_log_mask(LOG_GUEST_ERROR, + "smc91c111: tx packet with bad length %d, dropping\n", + len); + smc91c111_complete_tx_packet(s, packetnum); + continue; + } len -= 6; control = p[len + 1]; if (control & 0x20) @@ -291,11 +319,7 @@ static void smc91c111_do_tx(smc91c111_state *s) } } #endif - if (s->ctr & CTR_AUTO_RELEASE) - /* Race? */ - smc91c111_release_packet(s, packetnum); - else if (s->tx_fifo_done_len < NUM_PACKETS) - s->tx_fifo_done[s->tx_fifo_done_len++] = packetnum; + smc91c111_complete_tx_packet(s, packetnum); qemu_send_packet(qemu_get_queue(s->nic), p, len); } s->tx_fifo_len = 0; From patchwork Fri Feb 28 17:48:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 869289 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1561:b0:38f:210b:807b with SMTP id 1csp932482wrz; Fri, 28 Feb 2025 09:49:42 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXrD1MvXtowrGqIUluRcSG8ZRKy5iOlxSv+2TLse0xDilHKYu9eldPtbAQYaq4JXTuU5ECw9A==@linaro.org X-Google-Smtp-Source: AGHT+IGorQTEzPH1sKsf5mCq7VdUW+pBNZ4XDe0ntLV5acc9KsN4HngcCAtV7o8sl+ZSE6WbS1Cg X-Received: by 2002:ac8:5741:0:b0:472:3ae:eab4 with SMTP id d75a77b69052e-474bc08454fmr56967141cf.20.1740764982410; Fri, 28 Feb 2025 09:49:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1740764982; cv=none; d=google.com; s=arc-20240605; b=WNmLSW5OLbM9Fvf6Bue9apyNC6GTTW1yV44HBakTne0DWnrGOLybOx3zUV0ANgHYE0 blozsHxogQacT4+Lzpxe2LJ9mFfm1l7gtELH/q3uMcMym8GyjsbuS42aPQU4MCumDtx9 Vl3yVXnCJaKIC+leqq3H6As5UZL3eIJuSmorDjNp/uXQ0x9nXODMce4fLrjKXHAUyqZR 1SJaEfH+zeHK34vb5BXx8IHMVR7bDbZHyyws4mB/vZmdUxzCISXc5JWcy4E1HqYwWEXz bU+iwn6CV9EiVzxa6qXy9bYvDmZUPAAklyXeM0sdCLYv9345w+yNagG7fhoTy4KYXkeV b2yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IrK81ur78/kd+G6F3kTwFNDWhB4gkURHyE9BpPtQ7cw=; fh=RLZtwVazNnTrgPwjdMdjNN6ndINMPPOoZilJFcijy0Y=; b=cXuUyAuzYzQVuv0JilA+gVD3CXhsxueU2MLMl6BLnBZhTHWvWqH6m5aA68EBGGHQoS 76W7N5nq5T3bQtg9f29PelbAWhffX07eaB3Yu0vdjwpeEOKz0AwxZnagotGC1E3CzBqa B8wSpD2vSi8pmZpcmESkpZYxsMmLRha+aLZOPaOEpAjwOUCC9VrCN+jUAQvl8jRhzZlq Zcz+uz0CIzAOANAKnTT+v2CgoE4DhXAJa9o08d0GHHeI1bPG6pKF+OPvzz423mZj3LUv tOuL/n+tN/MAEBADvWlecABfGq/etIRXOb6EDHiUyCSBU7VrLw8F4Gbf7Fggr4hLtlXb kZpQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QBOc97TG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-4746919e5a8si36841911cf.18.2025.02.28.09.49.42 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Feb 2025 09:49:42 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QBOc97TG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1to4TV-0007nQ-TK; Fri, 28 Feb 2025 12:48:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1to4TA-0007VJ-Ry for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:17 -0500 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1to4T7-0005Nw-WC for qemu-devel@nongnu.org; Fri, 28 Feb 2025 12:48:16 -0500 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-38f6475f747so1230617f8f.3 for ; Fri, 28 Feb 2025 09:48:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1740764889; x=1741369689; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IrK81ur78/kd+G6F3kTwFNDWhB4gkURHyE9BpPtQ7cw=; b=QBOc97TGU8coZO044vEU6iTAKItSwICwtQISKtLa2GOW+8OnQ6+ul9FvydAtewPPsp msjB1qbA6ctTPeyqdwjcj6qHTmoibwd/UnejUaXOmmnMxDHSHbWH3teOm2kE0U/0OzI/ xk/G6guV21LwMdpWFz+nxnJl8eIdw9lHQQSogQC7IB6mgyR79XRNqghRcx6EWnHc9G/O KA2MqBta7ERHat740P5iILxL+eQNIX+UkhpzxqlOqNBqmV9dzOwBfDJwb0cT0K4IO5Wj /8z/HY5YLKQ8SrxD/37jr8yAuGqjjnLLzU1hiV4wiA1E8olV7b1bT1wjVa+OwtXgU29c zADw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740764889; x=1741369689; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IrK81ur78/kd+G6F3kTwFNDWhB4gkURHyE9BpPtQ7cw=; b=BJOVfxHvQjbpQm8GmHfwFNy/vOQ5jc2hCX2qTUHabh439bGoYQmliCZiog/Peq/DLF ua1FDYp+SrSO/6JWaAG5gTtHQ4CA3/BC0qXQnHfGsbT8zoG8UkKtYLq/EKM65mRq/W2V 4oUbuBFtZxLg6kc+71/gEnwMlx57wvJuXsXOsR0j9u5NYobGRDFGj2+Iu7XaBxWqf5EU HcWOxqgqDeENpuBTgH5WEMu+eDZMmX+KMup1FLWe9MOGOjEiWuYabdwS/cw5HfPesu0Y eICMrnQrYcsxP0m1xSRDnM3QUkz71PSU9mNtn8hWhi+8lggoqKq1KgZXXRUYyztroxKU l0kg== X-Forwarded-Encrypted: i=1; AJvYcCX+M5CTUyBKCBdMXBo5hlaMxodv0WtELUVPXK0hVuphpk1eIk7or6MkizudTrwgF7BYWyoIABH24zvN@nongnu.org X-Gm-Message-State: AOJu0YyQ67+T5kFz7KkcBpTrZZiHc1kF3TFz+T9pn02/USP+JeBpiSx/ oDzgExqmUj/J5iAGP/d1V73eCWQO7TUm7cFNkc3BqatL5l10jtQZ2fj/E4xyf6k= X-Gm-Gg: ASbGncvMRM2YvUqAhaw+FgIN8jj2wIdwhKMV67TbIzECZ75tbdrm2vUHyUrztIygV9I xashbtBoBYuIrI7n/Hb2Z8wHDLawq8yMCPRDCufgnrI65hQNX2J4PUz98DivvSt4kp5LpQJtANI uVxT6fq0YbxiJJqFmkVyrarxrNMlpX1j90ycK2UYDM/WXwEB5hXitiZxTwWx3TbLt3XMXmXTGiT kFaVa1z2ZxEUJ95xmQQDYjkqZlW7Kb+ZgZ4qjL5JjMz3CQDjsyiRpl4gBeYqCaO2g48QvO8TYgn uKhmiAmcHEXSz8A64Z9uMfrT8lhcLf/n X-Received: by 2002:a05:6000:401f:b0:38f:38eb:fcfc with SMTP id ffacd0b85a97d-390ec7c7fefmr3526755f8f.7.1740764889367; Fri, 28 Feb 2025 09:48:09 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-390e4795da5sm5979556f8f.15.2025.02.28.09.48.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Feb 2025 09:48:08 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Jason Wang Subject: [PATCH 3/3] hw/net/smc91c111: Use MAX_PACKET_SIZE instead of magic numbers Date: Fri, 28 Feb 2025 17:48:01 +0000 Message-ID: <20250228174802.1945417-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250228174802.1945417-1-peter.maydell@linaro.org> References: <20250228174802.1945417-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::433; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x433.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Now we have a constant for the maximum packet size, we can use it to replace various hardcoded 2048 values. Signed-off-by: Peter Maydell --- hw/net/smc91c111.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hw/net/smc91c111.c b/hw/net/smc91c111.c index 23ca99f926a..4a8f867d96e 100644 --- a/hw/net/smc91c111.c +++ b/hw/net/smc91c111.c @@ -58,7 +58,7 @@ struct smc91c111_state { int tx_fifo_done_len; int tx_fifo_done[NUM_PACKETS]; /* Packet buffer memory. */ - uint8_t data[NUM_PACKETS][2048]; + uint8_t data[NUM_PACKETS][MAX_PACKET_SIZE]; uint8_t int_level; uint8_t int_mask; MemoryRegion mmio; @@ -86,7 +86,8 @@ static const VMStateDescription vmstate_smc91c111 = { VMSTATE_INT32_ARRAY(rx_fifo, smc91c111_state, NUM_PACKETS), VMSTATE_INT32(tx_fifo_done_len, smc91c111_state), VMSTATE_INT32_ARRAY(tx_fifo_done, smc91c111_state, NUM_PACKETS), - VMSTATE_BUFFER_UNSAFE(data, smc91c111_state, 0, NUM_PACKETS * 2048), + VMSTATE_BUFFER_UNSAFE(data, smc91c111_state, 0, + NUM_PACKETS * MAX_PACKET_SIZE), VMSTATE_UINT8(int_level, smc91c111_state), VMSTATE_UINT8(int_mask, smc91c111_state), VMSTATE_END_OF_LIST() @@ -773,8 +774,9 @@ static ssize_t smc91c111_receive(NetClientState *nc, const uint8_t *buf, size_t if (crc) packetsize += 4; /* TODO: Flag overrun and receive errors. */ - if (packetsize > 2048) + if (packetsize > MAX_PACKET_SIZE) { return -1; + } packetnum = smc91c111_allocate_packet(s); if (packetnum == 0x80) return -1;