From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 6F02E3C585E for ; Sun, 14 Jun 2026 15:56:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781452589; cv=none; b=UOd3NodTDdYhmtbBNZRY8cPxniXDVw2xvjV/4NTxeX33ePr9ko9KusXLn4G4sFAgfk2VrHwR/fmDUQaM5cg84jDIOLQ+Dos3JQxk+0M/OMupOtizxXb2eoZZ8NM7YBbnDdY6GRxzQMvdwcT2AvbDtIWiIEEMaXtXdP1mbvWHMvI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781452589; c=relaxed/simple; bh=TorzO0Kmi+6Z78DjajRRlcLFA0WRiXVZ3ntY1sLvXUk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=niymxUolJM7+lDu36hwPwVYutMxwmPekY+18xwzcKzgWQoNSg/b+AjiOABNkfU66AzxbxeuRKWbl1DLLjCepBPVAwc4hkiou54z9ghmQHbxUExt5kSS/C1WbgocCM+fPAe19pielI2NJOGK+MYtwz/8EY5NraBzfUlpeqU4l6JA= 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=YzHS5xJr; arc=none smtp.client-ip=209.85.219.42 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="YzHS5xJr" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-8ce9df31840so21060306d6.1 for ; Sun, 14 Jun 2026 08:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781452583; x=1782057383; 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=8jz+BPfeJA3CKv3gaCxJRY7nP9NRgfQenpRYWfzzFvk=; b=YzHS5xJrysbePvOOtgMqs82hxlL7pC/wzuiV3YOxY391tQm82Pqs8Z5ur8TyrFylUj bk/XsBeUjzCqTq6uREGRWR0pjqHCYr7KlwpwWVBVL33BMs73B6fIPHh9u+3TknUVsJdB c7MCMohRCllNC4pfYcdOyqLBi4vwu4NdchUALM83efJIKmOx3eH4eJqgfY78KflT63Cp u9GzOmKufrB/qjA8M7bcPpMAeG41+aVmREqjNSoKr8XosuurwZX/CdELEsoXi2a1tGfa L8LxV2RYrlQqonSy91ssiS1lFm9q4rVUo/ftHWRgS2PBK0NpgDGQgai7CgdkLjyremrU lcjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781452583; x=1782057383; 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=8jz+BPfeJA3CKv3gaCxJRY7nP9NRgfQenpRYWfzzFvk=; b=pivemsaanrwf+eVvIaWRnxPVq38Bi4mGtdlCJhF5T5dEUCQyLo+G9p4JuSRi+JhlFe 3r7qxovAGohxqRC7A93J2M64HuN2i4AhCpYOontQmfuj7Qtrp+Z/VE+gXelSeQt6Z5GY gertFZ+4jobu2hjZUGMNZTa7h2mJ+n2rBXW0vL3Hi7Cy9eLFVK0Xb7FsOyGzrXRjMjD/ A6RRaUqQIhK6+jniv42J2txJ8u0RVGJ2m3Gr2sxj7oP4Xml308fmPnBJfQ5lhNf5FW0J /vYA0pZHqKIj6PY/JcS9EM6LQB9MRQTKENXMcAdl7V8r0gMRMlDWzBcN6mKtXHbWVLvz EISA== X-Forwarded-Encrypted: i=1; AFNElJ+W4lhfqnfGZW+QZCrVEDA8Cwf0p4tF5HyVNXPW26pLPpxEm9JHT5TU6up0NYyFW/x2QUaIxu2Sw9vaKDw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5jB7EMGsMDq8gWqk6j24+U5xWkXW4+dO0Bj9o6VyAiHo5tS4s pTHgWXSkzpqyGGOTCa0y5JEABCruVVrx7kWgyEx+tDKT7UlXkM9t/gFQ X-Gm-Gg: Acq92OGfxRW5AG1iehXwYaxX9KsOANrClGq4eqXkkMkOqlzfNMnqjmx1LKK8kojwM9a 9518JTAi0E/e4kGhNj3EC46DeIq7dPG0aRj8JnZd6oSaZJIXa8ybkZndTygcukkfyO2fGli1O8P 7B9rZexrt+va1o4y756RGN1arHTa9v3Rwf/pQwCwkeGb8hwxlJEAzGMMxJkXMCmIrwvkXPr68W+ VkoaLaAjlM0+MQSFMH5kw2R1Vi2/Q5vfY547LKOQPW6UJ09SUX36cNNh6zzFcXmFb1GsHfgQgtE kXM0WdnHP2vFKBprSOEMA/5fEZj65VwBTxgE7R0Ztl7bPoxK3Yh1RsDOG8mtHuew478qRiOZbDz cfx8zS3mjBJKdlrXM3aAqra5xz8bLTGAN6Fzxz9aKeOwV5rzntO2p54xt9k4jiaNxA9RD1eRlyJ FWG2TYUUjCIW3LQsJakNU+Ob5urDu25jgOnTFsCr2fUT77D+SuOrA+fVJ+OSGshp2mulgR5PvA4 X8t/gmiPET1YGd1Q1OTefWGU8trgG9NnPNB9nv/aL0= X-Received: by 2002:a05:6214:e48:b0:8be:3da0:bbac with SMTP id 6a1803df08f44-8d32b56ffe0mr182671126d6.3.1781452583338; Sun, 14 Jun 2026 08:56:23 -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 6a1803df08f44-8d30522cbeasm82008446d6.44.2026.06.14.08.56.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jun 2026 08:56:21 -0700 (PDT) From: Michael Bommarito To: Hans Verkuil , Mauro Carvalho Chehab , Sakari Ailus , Nicolas Dufresne 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 v2 6/6] media: v4l2-ctrls: add KUnit tests for compound control tile validation Date: Sun, 14 Jun 2026 11:56:08 -0400 Message-ID: <20260614155609.3107600-7-michael.bommarito@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260614155609.3107600-1-michael.bommarito@gmail.com> References: <20260614155609.3107600-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 are rejected with -EINVAL while in-range values pass, including the zero-initialised AV1 frame default that userspace and v4l2-compliance submit. 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 --- drivers/media/v4l2-core/Kconfig | 12 ++ .../media/v4l2-core/v4l2-ctrls-core-test.c | 130 ++++++++++++++++++ drivers/media/v4l2-core/v4l2-ctrls-core.c | 4 + 3 files changed, 146 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 d50ccac9733cc..04b15d860a0af 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 0000000000000..813872694eb46 --- /dev/null +++ b/drivers/media/v4l2-core/v4l2-ctrls-core-test.c @@ -0,0 +1,130 @@ +// 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 upper bounds. */ +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); + + /* + * Benign control: a zero-initialised frame (tile_cols == 0) must + * still pass. Userspace and v4l2-compliance set the zeroed default, + * and the divisor that a zero tile_cols would feed is guarded in the + * consuming driver rather than rejected here. + */ + KUNIT_EXPECT_EQ(test, + call_validate_compound(V4L2_CTRL_TYPE_AV1_FRAME, f, + sizeof(*f)), + 0); + + /* In range: a 1x1 tiling. */ + 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); + + /* Out of range: tile_rows past the array. */ + f->tile_info.tile_cols = 1; + f->tile_info.tile_rows = V4L2_AV1_MAX_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 58e2eb7002a19..5e86b6373a136 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