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 D34C3C433F5 for ; Mon, 16 May 2022 10:44:38 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 456EE8427E; Mon, 16 May 2022 12:43:57 +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="lYfA7KsQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EE97E84077; Mon, 16 May 2022 12:42:46 +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 CC8CF8425F for ; Mon, 16 May 2022 12:42:17 +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=3iSqCYgYKBgIcuewnniqqing.eqow-dqqvnkuvu.fgpz.fg@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id h6-20020a7bc926000000b0039470bcb9easo5457335wml.1 for ; Mon, 16 May 2022 03:42:17 -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=/AY3c4rbZFixBpn43yY+h7X/eCc/gqDJx9GU1xDkoJw=; b=lYfA7KsQmvxzhwuC2vwqknTmD1MyO4t1QUBS7WIW+EvcyndIBjDrncSPow43bicGNH TT4Gc0OIjwP7WjJ2NNzAAhstDobFKlfdSvCcQzct88tEBtepsbSyERDvaRMS1oj8x7PE //G59O26TUV2uXxLP77v+rhtQPuCsTbi49mk/DeJd8JvJI4TPUw2PARjMLMn3z12wbYy ykF4ykGwMbGvKS0l/zPzLQG6MXvd2iGQRr4wDJPoRpVe5Yhm0OY3Ev3QD+/+GK3ih6ye 7fXDEyp+/O7DQ4GbD2GQP9LrTafef0JihmiwNq833XRzX1VaJk107wwCh7115TrSb3YL TTCw== 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=/AY3c4rbZFixBpn43yY+h7X/eCc/gqDJx9GU1xDkoJw=; b=H2PKjpJn67khh9Hc9nwdO7f7GCIUU0szxp/1POJpznxex7EJOwxzl4GmzBtVR69Z+J UcvvF79lkDrg/DWLR6bXZ5OUp8ClSZ5O6zGSF2PcW/JwQeeMHcPuWeGR9FTK8xUDQfZC wQ5GrNeMw2qJ6dOuRYMmPQD/fhpqADDQDSG5rp9b2VjZA2NuARghTwAu5ocm/jqjgN7S Z9Vd3/pNtsdAto1KCKwE0ZQKEADej/g+3N2QSk8sQ35I0urNXCwLoeJsYyE3RcSl0jk8 hCLVJIujhvaWC/7TOodS5r/FT85W9zt7HOQ6Xs4BgMT9koyvWJd2JyJmTHBogXRL8lpL a12w== X-Gm-Message-State: AOAM533NLo+qW+PxtQIzLeCFhKT/xags/7F+zhqYcSoI5Z2/8G7yssri qSRmW8mdCECueTr6Stn5dwcmZKqWZ3NMVxeTod5HlfV6PIKLN5v80j8/aP+EO/kinoCm/C8eK6s y2OXUSgOgNZPM7YuSy21DtfLyxUr2caiTf02BmeYWksZpymhlB+f0Aq6jZaY= X-Google-Smtp-Source: ABdhPJyijVQqFgIwDKR4t0xcxuciYSVSc/qwgu9z6RGsywT7DAjbiZ6LpYR4KGYj0Gy2RxwyAdCbTgHFp74= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a7b:c8c7:0:b0:394:9e02:baa with SMTP id f7-20020a7bc8c7000000b003949e020baamr25733514wml.26.1652697737483; Mon, 16 May 2022 03:42:17 -0700 (PDT) Date: Mon, 16 May 2022 10:41:40 +0000 In-Reply-To: <20220516104140.1047229-1-ascull@google.com> Message-Id: <20220516104140.1047229-13-ascull@google.com> Mime-Version: 1.0 References: <20220516104140.1047229-1-ascull@google.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog Subject: [PATCH v3 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, bmeng.cn@gmail.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 809f0f239f..caea52f4e2 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -110,6 +110,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.36.0.550.gb090851708-goog