From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796153417; b=etYXrzh6b3zOUlQsyKAYNTF9TqaAyjMDgKji8TQ50Btoief46rmMEMQaRioVFFU224Gr jzCav9Y288cq0T+YYmaqQIvdMxcB4WqdeNbtKONGyJrvcMoY+ABqQgEkAAEkDlMnI311/ r6EcD1ha4yQ+Ld+GgqzJvNhV987r/MDm6dDyuzw8+0dmQu7f+TeYxIFx7JmAlHLeiLsyC cwb3Tnd+wVMSjasChjO5uhL8yVQrLuuteei7MwYTnJAtNLqg0DGeuP86A9LCr7H8an/oE IGpmzKJhteGtbMlwvfI/E+CP0I38Da2AYLiRh86EmtDHM0KDUrnw+pBcZumhkBon41Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796153417; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=v/bkOF3SmzKTo2hXk9xo0vIzHMvhPLQK3jC7BOTuyps=; b=pU50sOFEifnWsnTaAvlBEzW+Ghdy5b9mNTfmgdqcxU3EGm3BhBl1rGQBY79pP2u9y7lf 4xuNxNViuUgYmxFCKH2ixIzSN7PTkYufsKNJgQIJTbZ4GL2939/bYnwr+4iyZiVwVZshu ciz6fEHkMox3X4N79f4DN/5uogQz+3xU45wFxMEnRuDNvmiffPAEAc4oygXZhCBidV/fd IVBDRX9Mw4uLVTvkmCwZ5rZw5tJBi9HWNzzemPmmWPeVM5+8+XNh5lH1+POf5g2j3mZdn fs9LdrTLWjN5wcVYxEGofj594zUycz7IEbeBHCKcxrpL9dPv9j6mGfX6C0HLivEaRxA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--briandaniels.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="IVy/vgll"; arc=none smtp.remote-ip=209.85.128.199 Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 669D6606A683 for ; Tue, 1 Dec 2026 19:30:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 669D6606A683 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--briandaniels.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=IVy/vgll Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-8b3b74c495dso187499037b3.0 for ; Tue, 01 Dec 2026 11:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796153415; x=1796758215; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=v/bkOF3SmzKTo2hXk9xo0vIzHMvhPLQK3jC7BOTuyps=; b=IVy/vgllR11A67fhYqW2lvdjRjKqRLCFNIcDHQrtSxeZPJKDpeqryN721qWi40sHvX tjvA6KUe/tJoK5PurRptVwnD6/WKLchPhhjRFXGUSZbYR6pVuquMshhRzi8FWMrPwTqQ mU8HhGvbV4uYoNmuO22xAtVFfdeJ23mJg2G2srBAkWAzM7onuxxi6ojjt/W68yy+r+vf eNjtVsuPMhHzjOZ5ldOS/N/9WR3kwin3tD1dk6yIzC6eBjns1pjKt2BHfa8c6R0k/Dk2 jNDLpAsb7ieVDthExgPrVGR6LFPwtBZrHJj4Es5yJbp29oUg4pIK+ZNMrZKu+WCXjBPG 7aAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796153415; x=1796758215; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=v/bkOF3SmzKTo2hXk9xo0vIzHMvhPLQK3jC7BOTuyps=; b=eu24Jt1VI1y2sL6DZzzgaRVGc6A74F2f2KflckcpS9lLVShKUe3kpe1tVdoae9i1RQ J7/bhYF7ei1p+I8VtT3QPXnBKMgkuwm0QrhiaZHzBib1M7+hlCTjscuM2kfNPhSlqScC yEQc9FJfLGYWsod2s7GgKPFrLfNxNNrNgcZ98+ERvmfgRtwbOP4ha6Pq7rB0ZShOCs1y sxQbSKySF4afo4BaSDA1cWQSOBmBWI8PW57+jRCb+EX6J5GRO5LeXk4Q/BspC1o63R9m YcMdeFzSLLmliWQrXM3B2n3YOvyU7xCuKFbDKGRkt/pP02zN9vY2hvkhk5dzgVQ21iqw zojw== X-Forwarded-Encrypted: i=1; AL+hErEndd4qEdRXjsZlqMGz7DanmUWkM5WxbDEbYluE+pYc1hP/rxKbEeG40Uq37hGeRXovHUanYu3IIOZSBQWzaQ==@lists.linux.dev X-Gm-Message-State: APuU4Z+rq8V1nb60q2NLvjX74vopxOHXsIH0lMkfdxN1JrSjelR6Is+3 XhdCPuqyJiPkVY+Zwe0i3yfEsr30OR8zJUVat0CmmjU92FwHK0vC5eyWlAsgsUPfsf9767Qp/wY MbV/8/PrmIWYagwYbXdGP/Q/ldKPb X-Received: from ywdj16.prod.google.com ([2002:a05:690c:e310:b0:8a8:7912:4620]) (user=briandaniels job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:891:b0:8b4:cb03:c8d7 with SMTP id 00721157ae682-8b5204da8eemr139030097b3.78.1796153415197; Tue, 01 Dec 2026 11:30:15 -0800 (PST) Date: Tue, 1 Dec 2026 14:29:57 -0500 In-Reply-To: <20261201192957.790894-1-briandaniels@google.com> X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201192957.790894-1-briandaniels@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261201192957.790894-7-briandaniels@google.com> Subject: [PATCH v13 6/6] media: virtio: clear the size of legacy controls built on the stack From: Brian Daniels To: Mauro Carvalho Chehab Cc: adelva@google.com, aesteve@redhat.com, changyeon@google.com, daniel.almeida@collabora.com, eperezma@redhat.com, gnurou@gmail.com, gurchetansingh@google.com, hverkuil@xs4all.nl, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, mst@redhat.com, nicolas.dufresne@collabora.com, virtualization@lists.linux.dev, xuanzhuo@linux.alibaba.com, dbassey@redhat.com, laurent.pinchart@ideasonboard.com, Nick Rogers , Brian Daniels Content-Type: text/plain; charset="UTF-8" From: Nick Rogers A driver without a control handler receives VIDIOC_G_CTRL and VIDIOC_S_CTRL from the V4L2 core as a single extended control that the core builds on its own stack, with the control's size left uninitialized. virtio_media_send_ext_controls_ioctl() takes a nonzero size as a payload to copy from userspace, so these ioctls fail with -EINVAL whenever the stack is dirty. GStreamer's V4L2 encoders set their profile with VIDIOC_S_CTRL and cannot negotiate, and v4l2-ctl cannot read the MIN_BUFFERS controls. Legacy controls carry a 32-bit value and never a payload, and an array that comes from userspace is copied into an allocation, never onto the stack. Clear the size of each control when the array is on the stack. The core is being fixed to zero the structure; this keeps the driver working on kernels without that fix. Link: https://lore.kernel.org/all/20260923160936.33445-1-nick@getfieldwork.ai/ Signed-off-by: Nick Rogers Signed-off-by: Brian Daniels --- drivers/media/virtio/virtio_media_ioctls.c | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/drivers/media/virtio/virtio_media_ioctls.c b/drivers/media/virtio/virtio_media_ioctls.c index 99a6ef4f2..c1d5c9b16 100644 --- a/drivers/media/virtio/virtio_media_ioctls.c +++ b/drivers/media/virtio/virtio_media_ioctls.c @@ -6,6 +6,7 @@ * Copyright (c) 2024-2026 Google LLC. */ +#include #include #include #include @@ -593,11 +594,31 @@ static int virtio_media_querycap(struct file *file, void *fh, * Extended control ioctls are handled mostly identically. */ +/* + * VIDIOC_G_CTRL and VIDIOC_S_CTRL reach a driver without a control handler + * as one extended control the V4L2 core builds on its own stack, with its + * size left uninitialized. They carry a 32-bit value and never a payload, + * so a size there is stack garbage, which would be sent as a user pointer + * to copy and fail the ioctl. An array from userspace is never on the + * stack: the core copies it into an allocation. + */ +static void virtio_media_legacy_ctrl(struct v4l2_ext_controls *ctrls) +{ + u32 i; + + if (!ctrls->count || !object_is_on_stack(ctrls->controls)) + return; + for (i = 0; i < ctrls->count; i++) + ctrls->controls[i].size = 0; +} + static int virtio_media_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *ctrls) { struct v4l2_fh *vfh = file_to_v4l2_fh(file); + virtio_media_legacy_ctrl(ctrls); + return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_G_EXT_CTRLS, ctrls); } @@ -607,6 +628,8 @@ static int virtio_media_s_ext_ctrls(struct file *file, void *fh, { struct v4l2_fh *vfh = file_to_v4l2_fh(file); + virtio_media_legacy_ctrl(ctrls); + return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_S_EXT_CTRLS, ctrls); } @@ -616,6 +639,8 @@ static int virtio_media_try_ext_ctrls(struct file *file, void *fh, { struct v4l2_fh *vfh = file_to_v4l2_fh(file); + virtio_media_legacy_ctrl(ctrls); + return virtio_media_send_ext_controls_ioctl(vfh, VIDIOC_TRY_EXT_CTRLS, ctrls); } -- 2.56.1.911.g883d8bcb20-goog