From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E99A83A6F11 for ; Sun, 14 Jun 2026 13:10:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781442622; cv=none; b=YbBFAchtg/MpujQDFhP5TIoA7b+lKbGy1cIOmZOPx1nqjiBJs3AHduQW5FhBcPLwG5egy97784Cv7wThKjO2pK6UYEhShmWjEnIVCL1civ3HZgspgsJG8gmESPKEgN9p4bn08k7kk2xgACEbsMJhuVpkJuf18knd66jVHIE7jQI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781442622; c=relaxed/simple; bh=4de0iH1hxmw0RrIZy9MhVw+IN2gYqEuXESsum1x+fww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PIsqvflWZ1ty/eJdzf9KZaJ+3Nt67xERvB8VN+D/ls5gfnh9qQA23XLsvDBl151H6xVFFYqtwBPJiK9esm2v4HR3Ywsg03gfSrarrytFL3e2zf6y8TJFTMQDGnFvHMgQ/y0OxeJxGte60NyGOaRAH4Aqjb2i8hRyFKX0wp5h2Hc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FrWWixoD; arc=none smtp.client-ip=209.85.222.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FrWWixoD" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-915b5ce94c7so266484385a.2 for ; Sun, 14 Jun 2026 06:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781442617; x=1782047417; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eJpu5i70KzzzFRdB2RQuMRBjZ2Ekssr7dUppZeDYSDo=; b=FrWWixoDPPxVlIZwS3Gz3Oy48H4VaMXClvFFLyiTMIcAXg/oBCg8U9q2ljpZH+wj+D MI5R3DnWdpih6Sc0G01EWMK9l1MwCTLZzrQs8DLaFHmqdh/+9iMWF76l0X2sMkZPtApF 70e7Et/K8b96TQxB4j1YkGAP+pYAQkHKl3vo8qp7vXdAEHi94Bb9hluVecxpK0wS5J3T UM33Vp/W9dT6GKiD5ABwQDqY7uFPdngF4StoYlltGXRZb3kYzBFWH5Xyt6nDRmdBKyIQ 8dwdr9wk/vI4ZnaQV3+xLyTzViKARHUVmUxb5wz6utSMj4B13+0pn4yMbEKKUXGKmEnD t3fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781442617; x=1782047417; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=eJpu5i70KzzzFRdB2RQuMRBjZ2Ekssr7dUppZeDYSDo=; b=E7MHpTWQu+qbn4yacgZJxywXi+WTivb4OkpIcfAOMh+lf8WznLajm1Ui+1fD/nnnQV PU5pzZbkF6wfTD2CqqHV8yei0PPfQL8AbyI3f3mOJM5dWSFkdn8PfdoV/w2eWRFhuc6K kHvvVvwLZ5sDwBpghZxSkbozYFOqShXAUGnZ3GMvPx/IjLziiyEPIM45Ax7YQ5kWaA2l j4kfFvbgJfRSTJSK2HSpHMPC4KbkyZS4He+3jp85v9V+n72LWve0v4wx3w/1Os2dY3WV HuGYMwqxfWOkFCy68zNKjzOsp4H+TKsus1Dy/PxoYP3YSdWu7zWUBpveQ403XduakMMy BUXw== X-Forwarded-Encrypted: i=1; AFNElJ8iZPNS8ksTZGprzOeqyIq75MU7Snq2WhKQ1G+7dfGQa3KhOpgsO7wF8R2KDmCHBhPOym2QImwc2Duj4ZY=@vger.kernel.org X-Gm-Message-State: AOJu0YyUghATWAqbaDzyIyjBzSSypkJ14V6Is8Cv6A99s0U90WWXpcdU azfd8XjhgtUdb6vvVu2y19+w16VkWv1AsD+7n9eR/LEHGb6QrzySsPiu X-Gm-Gg: Acq92OGSJUhinax/XymmCxct+3ABxBW83krNGvLb/giixQ5QkpfG4gL1WleRX087L/H ZBbOXGp4yZeXxwBLz78oLfM+ZNBSP+CY97rvajSK9PLrTtf6EvQBdjeERxt8wLLZ21xqeT/GTiJ we//o6xX666hmz2ojY5FA33vZOyBTPiZdDxClG5Am2gn5TtQcnEtZjXsPvcEogvjSTKKtv8qIMe /s5uqwept7ac7Fq/xUsEwOke7FjOriEuH1Jf+197rhz3PBpMapGVIgN992EjEQVMDpdb/SeSr9N AAkVNTo2m/OwlEAT6qtR/4pD7WeLd4gMCIJ4/o2s8ItMBGjzI8xy6DZUUdcnRoug46hAX6UZwGF FRHRBrohA2Hk/snB5fD4XSbzIjwQKt7doDPW0u75fb6VhXmuuRZn/+yhDouwPKPu4J5gYDfwIeV OfPPOro948u6LyLkM2L68ITcQpoKEEY6aHO8q7rmbklhuDShNUDaj3uAJ7VkzcFxWpJx/L6voJi 1GXD7xTISTNwv7tidOoBdwiYX+xaHcjaAQHiJmWkMI= X-Received: by 2002:a05:620a:414a:b0:915:6e30:5bd8 with SMTP id af79cd13be357-917ee4d8cf8mr1102847085a.11.1781442616705; Sun, 14 Jun 2026 06:10:16 -0700 (PDT) Received: from server0.tail6e7dd.ts.net (c-68-48-65-54.hsd1.mi.comcast.net. [68.48.65.54]) by smtp.gmail.com with ESMTPSA id af79cd13be357-91619f1b400sm752878985a.15.2026.06.14.06.10.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jun 2026 06:10:16 -0700 (PDT) From: Michael Bommarito To: Hans Verkuil , Mauro Carvalho Chehab , Sakari Ailus , Nicolas Dufresne , Sebastian Fricke Cc: Laurent Pinchart , Benjamin Gaignard , Detlev Casanova , Ezequiel Garcia , Yunfei Dong , Jonas Karlman , Heiko Stuebner , Kees Cook , linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] media: v4l2-ctrls: add KUnit tests for compound control tile validation Date: Sun, 14 Jun 2026 09:10:03 -0400 Message-ID: <20260614131003.2524025-7-michael.bommarito@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260614131003.2524025-1-michael.bommarito@gmail.com> References: <20260614131003.2524025-1-michael.bommarito@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Drive std_validate_compound() with crafted HEVC PPS and AV1 frame controls and assert that out-of-range tile counts and a zero AV1 tile_cols are rejected with -EINVAL while in-range values pass. The tests are #included at the end of v4l2-ctrls-core.c to reach the static helper, gated by CONFIG_V4L2_CTRLS_KUNIT_TEST. Signed-off-by: Michael Bommarito Assisted-by: Claude:claude-opus-4-8 --- The tests are #included at the end of v4l2-ctrls-core.c so they can reach the static helper, gated by CONFIG_V4L2_CTRLS_KUNIT_TEST. Run under KASAN on x86_64 they fail on the stock validator and pass after patch 1. drivers/media/v4l2-core/Kconfig | 12 ++ .../media/v4l2-core/v4l2-ctrls-core-test.c | 119 ++++++++++++++++++ drivers/media/v4l2-core/v4l2-ctrls-core.c | 4 + 3 files changed, 135 insertions(+) create mode 100644 drivers/media/v4l2-core/v4l2-ctrls-core-test.c diff --git a/drivers/media/v4l2-core/Kconfig b/drivers/media/v4l2-core/Kconfig index d50ccac..04b15d8 100644 --- a/drivers/media/v4l2-core/Kconfig +++ b/drivers/media/v4l2-core/Kconfig @@ -3,6 +3,18 @@ # Generic video config states # +config V4L2_CTRLS_KUNIT_TEST + bool "KUnit tests for V4L2 compound control validation" if !KUNIT_ALL_TESTS + depends on VIDEO_DEV && KUNIT=y + default KUNIT_ALL_TESTS + help + This builds KUnit tests for the stateless-codec compound control + validation in std_validate_compound(). They check that out-of-range + HEVC/AV1 tile counts and parameter-set / tile indices are rejected + before driver code consumes them as array indices or loop bounds. + + If unsure, say N. + config VIDEO_V4L2_I2C bool depends on I2C && VIDEO_DEV diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core-test.c b/drivers/media/v4l2-core/v4l2-ctrls-core-test.c new file mode 100644 index 0000000..6ec48fc --- /dev/null +++ b/drivers/media/v4l2-core/v4l2-ctrls-core-test.c @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * KUnit tests for HEVC/AV1 tile-count validation in std_validate_compound(). + * #included at the end of v4l2-ctrls-core.c to reach the static helper. + */ + +#include + +static int call_validate_compound(enum v4l2_ctrl_type type, void *payload, + u32 elem_size) +{ + struct v4l2_ctrl ctrl = { + .type = type, + .elem_size = elem_size, + }; + union v4l2_ctrl_ptr ptr = { .p = payload }; + + return std_validate_compound(&ctrl, 0, ptr); +} + +/* HEVC PPS: num_tile_columns_minus1 / num_tile_rows_minus1 bounds. */ +static void v4l2_ctrls_hevc_pps_tile_cols(struct kunit *test) +{ + struct v4l2_ctrl_hevc_pps *pps; + + pps = kunit_kzalloc(test, sizeof(*pps), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, pps); + + pps->flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED; + + /* In range: count == array capacity (minus1 == capacity - 1). */ + pps->num_tile_columns_minus1 = ARRAY_SIZE(pps->column_width_minus1) - 1; + pps->num_tile_rows_minus1 = ARRAY_SIZE(pps->row_height_minus1) - 1; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_HEVC_PPS, pps, + sizeof(*pps)), + 0); + + /* Out of range: one past the column array. */ + pps->num_tile_columns_minus1 = ARRAY_SIZE(pps->column_width_minus1); + pps->num_tile_rows_minus1 = 0; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_HEVC_PPS, pps, + sizeof(*pps)), + -EINVAL); + + /* Out of range: maximal attacker value. */ + pps->num_tile_columns_minus1 = 0xff; + pps->num_tile_rows_minus1 = 0xff; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_HEVC_PPS, pps, + sizeof(*pps)), + -EINVAL); +} + +/* AV1 frame: tile_cols / tile_rows bounds and non-zero requirement. */ +static void v4l2_ctrls_av1_frame_tile(struct kunit *test) +{ + struct v4l2_ctrl_av1_frame *f; + + f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, f); + + /* In range: a 1x1 tiling (a zeroed frame is otherwise valid). */ + f->tile_info.tile_cols = 1; + f->tile_info.tile_rows = 1; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + 0); + + /* In range: maximal legal tile_cols / tile_rows. */ + f->tile_info.tile_cols = V4L2_AV1_MAX_TILE_COLS; + f->tile_info.tile_rows = V4L2_AV1_MAX_TILE_ROWS; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + 0); + + /* Out of range: tile_cols past the array. */ + f->tile_info.tile_cols = V4L2_AV1_MAX_TILE_COLS + 1; + f->tile_info.tile_rows = 1; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + -EINVAL); + + /* Out of range: maximal attacker value. */ + f->tile_info.tile_cols = 0xff; + f->tile_info.tile_rows = 0xff; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + -EINVAL); + + /* Divide-by-zero guard: tile_cols == 0 must be rejected. */ + f->tile_info.tile_cols = 0; + f->tile_info.tile_rows = 1; + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + -EINVAL); +} + +static struct kunit_case v4l2_ctrls_test_cases[] = { + KUNIT_CASE(v4l2_ctrls_hevc_pps_tile_cols), + KUNIT_CASE(v4l2_ctrls_av1_frame_tile), + {} +}; + +static struct kunit_suite v4l2_ctrls_test_suite = { + .name = "v4l2-ctrls-compound", + .test_cases = v4l2_ctrls_test_cases, +}; + +kunit_test_suite(v4l2_ctrls_test_suite); + +MODULE_DESCRIPTION("KUnit tests for V4L2 stateless-codec compound control validation"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c index 25227d9..8debd0e 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-core.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c @@ -2851,3 +2851,7 @@ int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl, return hdl->error; } EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties); + +#if IS_ENABLED(CONFIG_V4L2_CTRLS_KUNIT_TEST) +#include "v4l2-ctrls-core-test.c" +#endif -- 2.53.0