From mboxrd@z Thu Jan 1 00:00:00 1970 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 9389AC433EF for ; Wed, 13 Apr 2022 14:24:09 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9B6E283E52; Wed, 13 Apr 2022 16:22:26 +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="d6zICJ/L"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8801A83E0A; Wed, 13 Apr 2022 16:22:13 +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 701F783E1F for ; Wed, 13 Apr 2022 16:22:10 +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=3ktxWYgYKBr8fxhzqqlttlqj.htrz-gttyqnxyx.ijs2.ij@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id p31-20020a05600c1d9f00b0038ed0964a90so849402wms.4 for ; Wed, 13 Apr 2022 07:22:10 -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=bHSsuQD10+ooMgqHDy4pp2AzlSYHP2ZC/daOdzoDK/Y=; b=d6zICJ/L+9dj9a/Cx1YIGD3g1Fdy58fTSmltvmpoTccsEqu4lTOEvGGgewJQE3PNa7 CvggJIhb2YQAkSoJZflOzr+HDPXtviO0Mn5EgdTeFv9ZFFdheENI4QY/0xsZgjqWVpDN T7urUVghCCnPIfnDmoMe89rE/zaaPbXoNKDKqfsKRX7oudKfN5o0gWWUWlnYWI9ta40r xwKvITxBDZqEDh5K+hMoOk4P/W68MvtVmPEyOYDYDT2prxeZrQg6nRXefC6sj4PdxRrj wqWY3MbR9uIHaoWXDlkdC+SScq2OqmVQVJ0w0Y4Q083NUzgAjd3Yv+7x/yx79w9RdbFw ZaQQ== 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=bHSsuQD10+ooMgqHDy4pp2AzlSYHP2ZC/daOdzoDK/Y=; b=L8ZE/af6Fk0YTfojK6d5VBzQnCky0dTGVOIMF8sUe1nFZ3NBRj0+cVes7looFHRtnL STYQZaYWy6NcJGKX0Ec4Yc14t3qzYwCqPqnNZBu3xSOo6v+Cle7+DBNtKcTNlBckvI8B PBGEIK5YsiCGZC1QVeKw6I/jIqhRm5MJ23gYl1MWVpBUbBfSmhuYCPR4RW4S6R7py6Od wOEkg7mD8ilwWvNH0ZJC4bBWMQkjGIM5rU1opF/V1DN7EQCIpVK+XxNpYxfoP1c+bsFN ylNlrGPgEY+IMCOeXdjo/DkRL+Ix0EKj6imXDzSSeWjzTmtOaIVtQacPasNBMmLiIqSD igew== X-Gm-Message-State: AOAM531T4dBVEcw1dGQ3W5JY3XwGCVVGCsAiir9sDDyJefg0jkxIUaBU qhosvCJw0nGFXClY9XjE9ihPR+Cny4tBVtxx0yi0iSiAEOLD40cBR10IFONndkE8CD+9e1r7GpA 9VJC7bs3ipACKIcYgVyBpaarDjBEJHG66ol4HFfL4fc2Q3JDvHlSrgOwoB+s= X-Google-Smtp-Source: ABdhPJxtptezGWxKKneH5CCyDtc+F6AZFjycfy1ocPrWnmjfj/F2Im2Ui7z8XnwdvuZtdEWGDFklrvV4zgM= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:6a01:0:b0:37f:1b18:6b17 with SMTP id f1-20020a1c6a01000000b0037f1b186b17mr8748410wmc.146.1649859730023; Wed, 13 Apr 2022 07:22:10 -0700 (PDT) Date: Wed, 13 Apr 2022 14:21:37 +0000 In-Reply-To: <20220413142137.560987-1-ascull@google.com> Message-Id: <20220413142137.560987-13-ascull@google.com> Mime-Version: 1.0 References: <20220413142137.560987-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH v2 12/12] test: dm: virtio_rng: Test virtio-rng with faked device From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, trini@konsulko.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 Add a regression test for virtio-rng reading beyond the end of its buffer if the virtio device provides an invalid length. Signed-off-by: Andrew Scull Reviewed-by: Simon Glass --- test/dm/Makefile | 1 + test/dm/virtio_rng.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/dm/virtio_rng.c diff --git a/test/dm/Makefile b/test/dm/Makefile index 6c467b145e..ba08c19515 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -109,6 +109,7 @@ obj-$(CONFIG_DM_VIDEO) += video.o ifeq ($(CONFIG_VIRTIO_SANDBOX),y) obj-y += virtio.o obj-$(CONFIG_VIRTIO_RNG) += virtio_device.o +obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o endif ifeq ($(CONFIG_WDT_GPIO)$(CONFIG_WDT_SANDBOX),yy) obj-y += wdt.o diff --git a/test/dm/virtio_rng.c b/test/dm/virtio_rng.c new file mode 100644 index 0000000000..ff5646b4e1 --- /dev/null +++ b/test/dm/virtio_rng.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2022 Google, Inc. + * Written by Andrew Scull + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* This is a brittle means of getting access to the virtqueue */ +struct virtio_rng_priv { + struct virtqueue *rng_vq; +}; + +/* Test the virtio-rng driver validates the used size */ +static int dm_test_virtio_rng_check_len(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + struct virtio_rng_priv *priv; + u8 buffer[16]; + + /* check probe success */ + ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus)); + ut_assertnonnull(bus); + + /* check the child virtio-rng device is bound */ + ut_assertok(device_find_first_child(bus, &dev)); + ut_assertnonnull(dev); + + /* probe the virtio-rng driver */ + ut_assertok(device_probe(dev)); + + /* simulate the device returning the buffer with too much data */ + priv = dev_get_priv(dev); + priv->rng_vq->vring.used->idx = 1; + priv->rng_vq->vring.used->ring[0].id = 0; + priv->rng_vq->vring.used->ring[0].len = U32_MAX; + + /* check the driver gracefully handles the error */ + ut_asserteq(-EIO, dm_rng_read(dev, buffer, sizeof(buffer))); + + return 0; +} +DM_TEST(dm_test_virtio_rng_check_len, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); -- 2.35.1.1178.g4f1659d476-goog