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 C6346C433F5 for ; Tue, 29 Mar 2022 17:00:28 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EBB2B84042; Tue, 29 Mar 2022 19:00:00 +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="HGSMugy4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F1B7D84028; Tue, 29 Mar 2022 18:59:32 +0200 (CEST) Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) (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 0120783FE4 for ; Tue, 29 Mar 2022 18:59:28 +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=37zpDYgYKBooo6q8zzu22uzs.q208-p227zw676.rs1B.rs@flex--ascull.bounces.google.com Received: by mail-wr1-x449.google.com with SMTP id 71-20020adf82cd000000b00203dc43d216so5227482wrc.22 for ; Tue, 29 Mar 2022 09:59:28 -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=SIGKAJFeGieFehFpCDCQFOSHdMNcp2qNy54VwXel6Q8=; b=HGSMugy4JZYFSP+z5T37CWTwgFUhnp80ILZf8YWKlf5QGu85qsdoEJHWpVBVSnOM5t SGUIf+21h00r9b2j7t8o0/9h2OHkxDJXjRjnPzuE/tL1S8YE1MTcciaF5mLLA+fFxyDm 8ioz3iiycltzKySAB/tFpuTtRpHLYDD4p457RMbQLEHfm7kBM7U05WCg/CVDcgL8oKWQ nqSzCKVxPmUvhNNeWTDNhrNE22GkDBB3cSmPBerDTNyUhLT7M1wcsDF1KXHhCoek93+5 ok8sxG55ilqGJ8yHwtMJ0STwK2NT0QR5SsDJVrMwOUAwgGA1wt+8NIbL9VLmAJe4ErYj yp5A== 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=SIGKAJFeGieFehFpCDCQFOSHdMNcp2qNy54VwXel6Q8=; b=Ejzm6Vg1ZXSFmzOwGWOKtiXCnPmx9qBgdCn3VYCdXst+YUPqsMwT+QT7OkhES/dzlz ArVwZbovhzlXlGxlNIeA5dabioMQorlEaHyd29yxxabGjW2uZ+vAFVJ2gz12EZHqWOhN k0RbjSy0HiqhuE6TESfu9wpI5AIbIQEykO2FGyL5kSzw4p/JjQ9zoBgT/ypFFjelojir mKSSPcbYZXDgVQZC6a7V3xcx0iLwFTskViroBnOs2vHCxslqdDGq2g8d1NJAo463Yy3N KdrcQSHGiY2PT3jQrhXeOmuHw8C3y4MyykZ9+oVpXaytraxS1Hl7y4aWY5S0y1bM/hCc KY2A== X-Gm-Message-State: AOAM532XuCGQAstbHwx9KGtcfkD/+gTitjwQ56g1RPjJVlgD6jDgbDCX HdCkQ+JRtXyv60JAT3f7f31+3mfDWgQLfatsvARlWHFhhspTDdG+ODx96YVIn1ZUGThPArLpUc6 LOZjLGC2CPPQaVKDZuNmFgPHXyEjYRihb4Y3AtZR4qvKhuK2VfXLIqGWnJxo= X-Google-Smtp-Source: ABdhPJx24maUDHCTVTAnFtQc/nSQDIvKqtkr+/Vk5nZ9So0x1Q9iDK3adod0VvI8atl9BitRtHQ9XEVRGkE= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a05:600c:3c89:b0:37f:aacb:cac7 with SMTP id bg9-20020a05600c3c8900b0037faacbcac7mr178080wmb.1.1648573167906; Tue, 29 Mar 2022 09:59:27 -0700 (PDT) Date: Tue, 29 Mar 2022 16:58:46 +0000 In-Reply-To: <20220329165900.1139885-1-ascull@google.com> Message-Id: <20220329165900.1139885-5-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 04/18] virtio: pci: Bounds check notification writes 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 Make sure virtio notifications are written within their allocated buffer. Signed-off-by: Andrew Scull Reviewed-by: Bin Meng --- drivers/virtio/virtio_pci_modern.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index bcf9f18997..7dd58aa0f4 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -94,6 +94,7 @@ * * @common: pci transport device common register block base * @notify_base: pci transport device notify register block base + * @notify_len: pci transport device notify register block length * @device: pci transport device device-specific register block base * @device_len: pci transport device device-specific register block length * @notify_offset_multiplier: multiply queue_notify_off by this value @@ -101,6 +102,7 @@ struct virtio_pci_priv { struct virtio_pci_common_cfg __iomem *common; void __iomem *notify_base; + u32 notify_len; void __iomem *device; u32 device_len; u32 notify_offset_multiplier; @@ -372,12 +374,20 @@ static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq) /* get offset of notification word for this vq */ off = ioread16(&priv->common->queue_notify_off); + /* + * Check the effective offset is in bounds and leaves space for the + * notification, which is just a single 16-bit value since + * VIRTIO_F_NOTIFICATION_DATA isn't negotiated by the drivers. + */ + off *= priv->notify_offset_multiplier; + if (off > priv->notify_len - sizeof(u16)) + return -EIO; + /* * We write the queue's selector into the notification register * to signal the other end */ - iowrite16(vq->index, - priv->notify_base + off * priv->notify_offset_multiplier); + iowrite16(vq->index, priv->notify_base + off); return 0; } @@ -499,6 +509,9 @@ static int virtio_pci_probe(struct udevice *udev) return -EINVAL; } + offset = notify + offsetof(struct virtio_pci_cap, length); + dm_pci_read_config32(udev, offset, &priv->notify_len); + /* * Device capability is only mandatory for devices that have * device-specific configuration. -- 2.35.1.1021.g381101b075-goog