From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D3473B5832 for ; Tue, 3 Mar 2026 15:29:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772551790; cv=none; b=CjXf3Z1B4Bwxt9V809QjcxZt2u0nrSdPI9cy9d7veYURtYFdAm4+2mxQSBzC3w/Wxz7ryIOgvgn98hBy0nRD23M0cdkE+O3++fxOCaQ6JcnJEQiOiWxtP21EOob0Jaw8qodUPLCyEgY4gCmd9HaGSt3Exsrr2MtzOxxXxwg5PUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772551790; c=relaxed/simple; bh=FLlFjX0xjyeyFcOnBlOeHYgsDSrtdWGURszqvpepVbc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=uiKP0kqCb9UIm2dNqsGqXJTv7p5vYGVEKuYw4s7VkhgBryFofRDghR8uo15T+9RrnvNDfITAY2ktD0CRK4RgFMuyu/NeS2gGr8YZ8qQF81KzwT2KGM55E5SNr7oaeXC9fy8Uet9kDIxyVC9MdKFmZXkf22rX3gnMlVunbsfle3A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org; spf=pass smtp.mailfrom=mailbox.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=FCcSq/zY; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mailbox.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="FCcSq/zY" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4fQKVJ4lC8z9vBx; Tue, 3 Mar 2026 16:29:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1772551784; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pM6lNqsskE7KRAVsbcEdawR3cIC0eusGPaA5IKKWiUM=; b=FCcSq/zY8HUNqE6/6wNjHi9mudJ1YuJjnHHQGEYFApLeUcKnfB5YkSjrLi/UXXAp8/PzEJ 04+P+4tgaykBrA/M8PNL6vKd6cTgsttz6xtyCsgoE+yTAq3Oc/GuFLF7OeQrt9AaqhTvR8 o8OFSfTOSHQbqC0K/HVLW3PtwbXB/cxjE5txQ1KfV3k+q9g9dlbUZjb+1Q39/EQtilAXk+ lvxZnJwuaetXhdhg+MBh4fv8X8JuF8p3b034TKs5vj9jW7pDUMpo8u5PMjAPzzcId0UQwG TcynDsutRTXXmnzSUQzfWAqyLZioQvxH9i4F3EjZTb2Vek+bTvAXUu558E6BMg== Message-ID: <2b75199f-b78a-4915-8e75-5d186f63f7c5@mailbox.org> Date: Tue, 3 Mar 2026 16:29:35 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH] drm/syncobj: Fix handle <-> fd ioctls with dirty stack To: =?UTF-8?Q?Christian_K=C3=B6nig?= , Maarten Lankhorst , Julian Orth , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Dmitry Osipenko , Rob Clark Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org References: <20260301-point-v1-1-21fc5fd98614@gmail.com> <3c969254-ed38-4b13-84b3-5afa365b04cb@amd.com> From: =?UTF-8?Q?Michel_D=C3=A4nzer?= Content-Language: en-CA In-Reply-To: <3c969254-ed38-4b13-84b3-5afa365b04cb@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-MBO-RS-ID: a6d17d73f2ec7df31ed X-MBO-RS-META: p49ack64wm7b6753dknkbui9359mtx3c On 3/3/26 15:59, Christian König wrote: > On 3/3/26 15:53, Maarten Lankhorst wrote: >> Hey, >> >> Den 2026-03-01 kl. 13:34, skrev Julian Orth: >>> Consider the following application: >>> >>> #include >>> #include >>> #include >>> #include >>> >>> int main(void) { >>> int fd = open("/dev/dri/renderD128", O_RDWR); >>> struct drm_syncobj_create arg1; >>> ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &arg1); >>> struct drm_syncobj_handle arg2; >>> memset(&arg2, 1, sizeof(arg2)); // simulate dirty stack >>> arg2.handle = arg1.handle; >>> arg2.flags = 0; >>> arg2.fd = 0; >>> arg2.pad = 0; >>> // arg2.point = 0; // userspace is required to set point to 0 >>> ioctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &arg2); >>> } >>> >>> The last ioctl returns EINVAL because args->point is not 0. However, >>> userspace developed against older kernel versions is not aware of the >>> new point field and might therefore not initialize it. >>> >>> The correct check would be >>> >>> if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_TIMELINE) >>> return -EINVAL; >>> >>> However, there might already be userspace that relies on this not >>> returning an error as long as point == 0. Therefore use the more lenient >>> check. >>> >>> Fixes: c2d3a7300695 ("drm/syncobj: Extend EXPORT_SYNC_FILE for timeline syncobjs") >>> Signed-off-by: Julian Orth >> >> I'm not convinced this is the correct fix. >> Userspace built before the change had the old size for drm_syncobj_create, >> the size is encoded into the ioctl, and zero extended as needed. >> >> See drivers/gpu/drm/drm_ioctl.c: >> out_size = in_size = _IOC_SIZE(cmd); >> ... >> if (ksize > in_size) >> memset(kdata + in_size, 0, ksize - in_size); >> >> This is a bug in a newly built app, and should be handled by explicitly zeroing >> the entire struct or using named initializers, and only setting specific members >> as required. >> >> In particular, apps built before the change will never encounter this bug. > > Yeah, I've realized that after pushing the patch as well. > > But I still think this patch is the right thing to do, because without requesting the functionality by setting the flag the point should clearly not have any effect at all. > > And when an application would have only explicitly assigned the fields known previously and then later been compiled with the new points field it would have failed. > > It is good practice to memset() structures given to the kernel so that all bytes are zero initialized, but it is not documented as mandatory as far as I know. Even though it may not be documented, it is in fact mandatory. Otherwise it's not possible to safely extend ioctl structs in general. -- Earthling Michel Dänzer \ GNOME / Xwayland / Mesa developer https://redhat.com \ Libre software enthusiast