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 BC9A6C433F5 for ; Tue, 29 Mar 2022 17:00:42 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 614C284056; Tue, 29 Mar 2022 19:00:23 +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="L7/rjiov"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5B92584025; Tue, 29 Mar 2022 18:59:39 +0200 (CEST) Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) (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 191A083FE4 for ; Tue, 29 Mar 2022 18:59:33 +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=39DpDYgYKBo8tBvD44z77z4x.v75D-u77C41BCB.wx6G.wx@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id 9-20020a05600c240900b0038c99b98f6fso1505074wmp.0 for ; Tue, 29 Mar 2022 09:59:33 -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=uPjtXgmk/qNdkLEuHpQsGBZwBLLsDTmlR4SnjFq09t0=; b=L7/rjiovKuipLR3nZf6Tq6EV1VwVhuNyjIAi/0xwFRY7HSKE0v+JXperM2ryD3hqex Ulnxc+eQIryHo1j/3b1THhgazX+iLEY6HDAyiEb6pvasxQJeLrDotwYT8TG/imG3B0hR Z4YoN9/hXRoVE8+JssjXEITXF5hDPiwYnwO59wlSEqNJD62K+5BHAsSTPZ2ugOApEvv5 q4BRaBMwoCN+5oRSdX+zP1Y7o52qcZY2ryr16hyOEnhZYLi99LQwFTeJ0CYYhGJCbrzt RFZKUckhLWTiIT9gGeF5SR7X431hRh3J3YRvfx/O6sJCkhQ8jDmnC49e4os2SNkAIZAy EUrA== 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=uPjtXgmk/qNdkLEuHpQsGBZwBLLsDTmlR4SnjFq09t0=; b=eVonxHTfaTuDQAoVCdOtVkXIVSSIuFFij3hUG+SJpY0+NEnDW7zRdEVkrt49uyF7zN tO0309WOLzdwBmrRuEQpracZ5nkJjS5HGBrIoGxmFIO6GaDqz2+QDpzwn0t/IfKPk3Ez EkmDvIdjVR7R0J3GFPSJwb12Htvz+frl4q+ADRpZ3uz5hre3+lPz+p4SWWaFPIYQClqx hyju/BWUBA5Cd1ZfAqkb6THHHqJgpy/FuNrIDsK1p/qBh+2mRHqS5PczwIJKRgZIEmfy o1q4swWEly8jyYVGTpKez8LDsE0uCzalaNqBgMIjdSARQRprnpEjmvXziJkXh4o4feXW qmPw== X-Gm-Message-State: AOAM531MpDOPhI/bFvmHJU/L1ibyhVRKd4w6HvxEPh2FSwWKnCWHNp8I riHzFzpoU1JPZXQZi18B7ws+btE0oP+ZT9vG2LKDy+PZhCUQqkiIDPl14jT/1xUr+z/jrntTrEZ L9z4EyaEslsFCed11Z6w03NOVwTPKiEXYJkhttfuV6JbUog46ntJmD2aYpV0= X-Google-Smtp-Source: ABdhPJzrIq9wy9fnmV4KBX5SJQ7EgA7TsAiYE/ZN5kWwUuyEPuKgLNgFKCWRE9/44iQRd26bM1/ojQh9m2U= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a05:600c:2c49:b0:38c:6b2d:39fc with SMTP id r9-20020a05600c2c4900b0038c6b2d39fcmr128819wmg.33.1648573172639; Tue, 29 Mar 2022 09:59:32 -0700 (PDT) Date: Tue, 29 Mar 2022 16:58:48 +0000 In-Reply-To: <20220329165900.1139885-1-ascull@google.com> Message-Id: <20220329165900.1139885-7-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 06/18] virtio: pci: Check virtio capability is in bounds 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 Ensure the virtio PCI capabilities are contained within the bounds of the device's configuration space. The expected size of the capability is passed when searching for the capability to enforce this check. Signed-off-by: Andrew Scull --- drivers/virtio/virtio_pci_modern.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index 2c1b0ebfce..6fe5e76572 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -397,18 +397,27 @@ static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq) * * @udev: the transport device * @cfg_type: the VIRTIO_PCI_CAP_* value we seek + * @cap_size: expected size of the capability * * Return: offset of the configuration structure */ -static int virtio_pci_find_capability(struct udevice *udev, u8 cfg_type) +static int virtio_pci_find_capability(struct udevice *udev, u8 cfg_type, + size_t cap_size) { int pos; int offset; u8 type, bar; + assert(cap_size >= sizeof(struct virtio_pci_cap)); + assert(cap_size <= PCI_CFG_SPACE_SIZE); + for (pos = dm_pci_find_capability(udev, PCI_CAP_ID_VNDR); pos > 0; pos = dm_pci_find_next_capability(udev, pos, PCI_CAP_ID_VNDR)) { + /* Ensure the capability is within bounds */ + if (PCI_CFG_SPACE_SIZE - cap_size < pos) + return 0; + offset = pos + offsetof(struct virtio_pci_cap, cfg_type); dm_pci_read_config8(udev, offset, &type); offset = pos + offsetof(struct virtio_pci_cap, bar); @@ -496,7 +505,8 @@ static int virtio_pci_probe(struct udevice *udev) uc_priv->vendor = subvendor; /* Check for a common config: if not, use legacy mode (bar 0) */ - common = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_COMMON_CFG); + common = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_COMMON_CFG, + sizeof(struct virtio_pci_cap)); if (!common) { printf("(%s): leaving for legacy driver\n", udev->name); return -ENODEV; @@ -510,7 +520,8 @@ static int virtio_pci_probe(struct udevice *udev) } /* If common is there, notify should be too */ - notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG); + notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG, + sizeof(struct virtio_pci_notify_cap)); if (!notify) { printf("(%s): missing capabilities %i/%i\n", udev->name, common, notify); @@ -524,7 +535,8 @@ static int virtio_pci_probe(struct udevice *udev) * Device capability is only mandatory for devices that have * device-specific configuration. */ - device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG); + device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG, + sizeof(struct virtio_pci_cap)); if (device) { offset = device + offsetof(struct virtio_pci_cap, length); dm_pci_read_config32(udev, offset, &priv->device_len); -- 2.35.1.1021.g381101b075-goog