Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796153416; b=HqdNWG1/uq7MxU4PYOobuA/wpZU0AssvzzW8+qpiW1sgI7NhNNXokZAJPG+4eP9w374x 4bzzxlIk1EdwltzTa5Csvef97Ynt/adSHlSMk5prl73LttLM1z73I10dlH+ucRkDdpy4V iIz5HzC7Tu+VQfucoubN0oLMtQIu2sREufAMv2Q1KqToxf1X2RsRyOWl4S4mmzpirMfDm 03ZPhKuK8UWf+Exzh4WR/YnAqKNvXdC/3yNqsL6eFRRI0/RZa9Tl0biP3klZtbE4BJIxl u0+QBsv/c7CpYuCZOFD8ZWbknRQZDdHFliDZSpg2vzWNhoZiMkE3ZFnSCI0CETDgXvA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796153416; 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=UKPkIAYc5AOuypMtAnoufuVJSovCmM2bDWskrPAQFlQmhoVfR5cwYkbzlZ/Rw3s3c66f E3rFyf5iJbLG/6EGGV521zlcLi210w1DpfZr2tTi2NvWHzyWJie6boR7YEG97x1xHWM0C Px4kdHfV9GX2eSS40Mng6fjv4+kG/Rxhhi7LoQ2eyTIrJUgTgKZvx87Qu5bzXH8r5zM+S 9edOSxMlZBMtMbTgDJ4n/KeloW6yUAZr4KSqlvA3ez2sQsLTuvpJI+xrTTrrmqYO0BIwS oWzcFWa1GCx9OMdsriTNdPdSEMJR1cughu9c3u2PMITe7jYZSug/LC+IUINZsvb7zOQ== 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=CqPm6mwq; arc=none smtp.remote-ip=209.85.128.198 Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) (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 709C4606A68D for ; Tue, 1 Dec 2026 19:30:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 709C4606A68D 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=CqPm6mwq Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-8b501e92b97so121106097b3.1 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=vger.kernel.org; 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=CqPm6mwqgkAD3Ogx3gBozRLnaEKFB3RLgfSEmhxKkrVfH/VCHI4iDr6Z1CbHz/BotX BxMZlKTyI/WwHLB4A6ZY9Tf8zkG8jL7k+EC1Du/ElRTH3PxZZBBovVhQxL0ZNP8f6t/3 +ETCXrq/TFVdeMThtwFfmdr0ii3MbdlbrSoY7uouUGfn3Q+TcO6JcAM4+5Pelm0wrQSt phOWKkpucOl5KVOG08UjFEUS1aQImt7VSwbENEoLzXvYgKgGACyDgytKvt3xwrmKiN5u 1HxoorVEByqZXyJFVrF3grdDOibYylzGYCEoq9oNIv+DowiVYa8FavkxXSjftHCcsKlw KCpQ== 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=y4iUyznChYXaAOUf/QSCVkSd2WPONQxbbENCHUTCsLfscssbV3OCbZ47vG2/jpEdU0 j0nJnOAXFFiD4BZFAPhr6T6wdTZ8KCCtqZTmVk1IzdM5x+c970Hda1JIOwyTbTn4KxpW L7niJ1iWFWrbhfZSodt3K/MLR3ym8M3RG0sSp9EjYIs2AQGgcVQKC1uetSUmITVTvdTC kxOJUHM2au0RJnKHNxKVLg0Nbg3tqZwZkoKOIAnlg0Yp6v5dvuu44GJEX35n5WkdvV4R SlyfzkJI+o0e/i/uHwXWcMr072/3WHi/0emEGNGnz68Ry6pyx2GQl+eY2ciI1/4oiSBC vNiQ== X-Forwarded-Encrypted: i=1; AL+hErEi9luqaEPJab8nlSWlQH763fxIPb6VoG5xwvTEnjrpVIDIf7zqUnpNwcWpAGJSnVGYoscVIUfRjuLxLg==@vger.kernel.org X-Gm-Message-State: APuU4Z+HJdSnLR0Ln7cRutD922jXcvh6HtEQdwQdOWGPGB5sRQgqRHgZ 7xNVfSk+vhLBk/0AZWfgmS+r7PRrH8XQmo88eXMpboCt3Bqf28314XCO22IXVhnopbTyyfaI8Sr uQWpbbaEkQrZVO4oPJ6AJOy7Spfsp 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: linux-media@vger.kernel.org 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