Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 350B5C433F5 for ; Tue, 29 Mar 2022 17:00:17 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0D70284029; Tue, 29 Mar 2022 18:59:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="T+bowEas"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2810684028; Tue, 29 Mar 2022 18:59:35 +0200 (CEST) Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 036FC8400A for ; Tue, 29 Mar 2022 18:59:31 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=38jpDYgYKBo0r9tB22x55x2v.t53B-s55A2z9A9.uv4E.uv@flex--ascull.bounces.google.com Received: by mail-ej1-x64a.google.com with SMTP id h22-20020a1709060f5600b006b11a2d3dcfso8482926ejj.4 for ; Tue, 29 Mar 2022 09:59:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=SdP0ZCEAweK2o6s+YEs1Ojj4P6plBcBVfkzsyeFoGvo=; b=T+bowEasAz1/tHWaiu9mb9TCFNHtcMLe/gvXmkh+OshREBzjnkq88KE8OnDky6+dga RYRhqiaPTCzyQXxQ4UJG4XVlYKufRzFHndVGR1F8FUPSKJdICPyP5zp60kI1xnyupOxn XoCemwzHsFlAbesnmAxsTnzs3N4mrWv/NK+ZlbmL5pKCqdy8d3IGOwA3lNQAYP1Sbf9F heLqJDNLPxlx0DTD4nHkT4kQ23eo2tJYyzFIfSd9YRqyMXXEKwtBwNV6PsrMoDe9f6K1 XOW925ccwfLwGWwHT/A7+9RI7RM/WvqTzPEEg0VweTq8Dt5F7QrHMWGfY6SrH8EwrpnX IoTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=SdP0ZCEAweK2o6s+YEs1Ojj4P6plBcBVfkzsyeFoGvo=; b=JS1mpq33g/5mrI5hRmWowHjJ7F8Q+Qz+YoVl5I0yvQrU4N9aOCYgge7EUXizyOkuea fS+s8fJgzkex2NLIY4aDlfxLGburwCjY3zzcBwfwJgC8cRm47wbGftuMdn3P7thOCH4S pDFcz50Dl2h70ZYi3T9khE2429lcepELuLy46JKKeGi2AbgbMtfTH0mlDtGfMwSgtNRs Grc1am6Xo052kf1xpdneT8TKFt6RVDPNeibFgyyPRghK7SOjf2h+faangr4D1ONkUKq+ iIWE3gOxpsejtkhuC28qGn8krYAe4EkNf+hcgETrQpMITfDBE9LF9Og9r/+eoMlba+nr 5MYg== X-Gm-Message-State: AOAM531C1p6Ftf/kj2hFntYW9hc8kckmzKWMGitqob7Zskt3EsMupqxy zPYa1jnYYx9ZTvvXdw8F9Mo2iupx0sSwytaX5okk5au5STeBPNLcjmaHtOdiam0gNJlq32CFErD 0CNzTGKfK+VIBHF3Id4iAKZtcutbr9bH0UkTX8QNit2mQwVdlu7zcNuIGbzE= X-Google-Smtp-Source: ABdhPJwy2JPPqkP+Q0GjL1wK28NcVCyaCafOpUpDuDv7jWmgXfVaCDy86WKt/7b8RavuOreTKNXt5vprebs= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a17:906:26c6:b0:6da:ecd5:fd48 with SMTP id u6-20020a17090626c600b006daecd5fd48mr35232887ejc.93.1648573170434; Tue, 29 Mar 2022 09:59:30 -0700 (PDT) Date: Tue, 29 Mar 2022 16:58:47 +0000 In-Reply-To: <20220329165900.1139885-1-ascull@google.com> Message-Id: <20220329165900.1139885-6-ascull@google.com> Mime-Version: 1.0 References: <20220329165900.1139885-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH v2 05/18] virtio: pci: Check virtio common config size From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, bmeng.cn@gmail.com, adelva@google.com, keirf@google.com, ptosi@google.com, Andrew Scull Content-Type: text/plain; charset="UTF-8" X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Check that the common config is at least as large as the struct it is expected to contain. Only then is it safe to cast the pointer and be safe from out-of-bounds accesses. Signed-off-by: Andrew Scull Reviewed-by: Bin Meng --- drivers/virtio/virtio_pci_modern.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index 7dd58aa0f4..2c1b0ebfce 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -480,6 +480,7 @@ static int virtio_pci_probe(struct udevice *udev) u16 subvendor; u8 revision; int common, notify, device; + u32 common_length; int offset; /* We only own devices >= 0x1040 and <= 0x107f: leave the rest. */ @@ -501,6 +502,13 @@ static int virtio_pci_probe(struct udevice *udev) return -ENODEV; } + offset = common + offsetof(struct virtio_pci_cap, length); + dm_pci_read_config32(udev, offset, &common_length); + if (common_length < sizeof(struct virtio_pci_common_cfg)) { + printf("(%s): virtio common config too small\n", udev->name); + return -EINVAL; + } + /* If common is there, notify should be too */ notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG); if (!notify) { -- 2.35.1.1021.g381101b075-goog