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=1796153416; b=lZlzljmP5DqAjp+RJGRsILJgqX3WRDbX3cPnj6BHaG+z1shFa+6/9mrRK2WN5jQKojEG UMPMOx00+i1GrIKsxImw9R9N8niJVrlVfMNk/ow34He72rHDucOswn3hF2p9RuR7htX8u odC1Rj/uMFlIvhFHXnATOjlkcG2JlHqfAsQ+GrCakUSIiqJ9AAf7nscsbOSldHeGZ10Pd ceq9mcNo6TSnAe6ddBer5JOf/9VKkz9aQ8od0a9FwLZo4mQDtI2vKcRNmpsiH0zCONdmD mjJaKMjbq5gRHZOeSXwwIAWfgEgAUXbTkDwWzimQJGTrDvSFiiE3z9tRWxCrhRy2glw== 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=o4TYfygd3iZXFbDlIfILNX7ZWw0PlnsdOgLEwVHxbsGdoLJ3sVyTUKvSuDyU+CMbKwYl UACDZ43hM6CWTFKGzzxovtg7emAjDlCqMyU3LYQxz13lbMJ5AAKZEv+zuH5UofMgZ02fM PoriZ1m1m8ruh3aiXdT027X8Wc3AAqbWQlCtv9S/axhtfV6e6jO65+P5pxPrt3yXx4Zbx S5ylzYQRmk8uvXa+5DkoOicUWRVEJ+u2dcyvDZ/cMbuoa/1ltQOus0qjTvn9XOeYC9NsA 29AfWbnDOth2U0sJg2DrXlD9I//U1S/hkQd42PJLmZ+s3bhZIi9qopwfhlsF0ZeCx8A== 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.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 66A88606A699 for ; Tue, 1 Dec 2026 19:30:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 66A88606A699 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-f199.google.com with SMTP id 00721157ae682-8b501e92b97so121106107b3.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=cFxjrgwhMGiauxRkEQiEjzLau3eqYiUlxKq3EdrLjCM2U63BLkPcuJl6QrTsnlzNbw gjHKTrG4AJxrbERQXFiB4gi4Ov+XwW5lLmU+uKVbFDmZjaZu+N/nU3eGwysRhX6tetBq +9b2EGnii16VVXbRLT6MA221yM9+WZn2VbxKdK9iPCaTmyDoIp16WBPc4urBRWsNNnmM c5XH9I5D7eF9WPEazNKbR+tY6/ojUbHclAo+uhkwkeJXzX39/ApgQVeVK7O34ABtfIDF UOycXKwton/O9hDCLPCGAAACK2edOke6tTXvIUaIj6uWi8APazvwSWk1pNZsckhclXZK KDDw== X-Forwarded-Encrypted: i=1; AL+hErHtz3MSxoCvvcqfmJZAPBxAtSJCPmjN4T+OfPDS/zRn6x55SPoNX5ldBW4OPszcWOFbCuGyaJcDOxS2oX0=@vger.kernel.org X-Gm-Message-State: APuU4Z8glTYe0wcr4nfa6nsKzcyGmmAwxU6H5z9mxsB2eg1elH7l4d/S 1nfjZxsvTC/VaXgqPyGAD13PWgQ3WKKek57L+tRrhVKStZaqHPf326cv9dBOm36bTeHlZI10XOQ H/NHx7yPQZuCKPlH4bA2U48JnmTMV 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-kernel@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