From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (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 A5CB3224B04 for ; Tue, 3 Mar 2026 17:02:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772557339; cv=none; b=ElOqHsMUHNQthVLHv667Y/10foILQ/Wa3diRrf1bzt0HnPQm9vZ/Q9gyYqDVzo8rLq/ZayPi0dD/qR8MqST8jLT7cYHCj3wudoAoFA4GeJdKu5Ivq7JMAWtoMNeUNi07ATOOczRinIhanbiDhbrLKnDEk+uEWDNoR2TAxdLEvf8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772557339; c=relaxed/simple; bh=BB82pqqX/duWqsqHb9QyQX0bkwd2e1gEc/j59935XkQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dgW5my6kurFU+i2d/cKyj1W+EZOHODF3CrZvqZMPnsgHBAHyshehjz+nAbmOlJ0RodIFEixELU+Vs/6R4wn6GlLRJeFlnE1fpKpD3ksj3Y8Dmxl4ML2OWiyZH6XdZ/Kq3ipBTC4qFpG0+uVc73EnnlDoYVUBvxCLF5srk7CXmSs= 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=yT+rKsO8; arc=none smtp.client-ip=80.241.56.171 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="yT+rKsO8" Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (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-201.mailbox.org (Postfix) with ESMTPS id 4fQMXw2kcpz9ty2; Tue, 3 Mar 2026 18:02:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1772557328; 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=TkFz1dpQpWZ/PnNaBEvKOgiDKplU1Wb7XSM1S4gYrzE=; b=yT+rKsO8vPRsxJgDSO0tTOvWUcPsQmgyI8HqhmI+NIrWuHPy9qMSCvE43Um8GEw9w8695e zkwaCMEEGgXVBeyWmBqoJ4KbmvyXmcFBHeMPkHIidrQ3sVhXzHjKd5ubhCca/NHJ6RW9nG 8htGC7yZaYa8Z8KaWOoKBBGZYER2pcQHCl25oBF5pNdXjl9LxygG0be75L66sEKECXOLdT tsMMw3mUj2A5Qo2ZquFzL28iXN6B7t3CunnWn1oh5KFiQUkg2qfVrkF06PIO31QhFRh5dH k+0PfkzJXuXFvrkkhLL/toWV1TjIQ6eXNRakZ8BJ/mpA14S85VnNmPTr5BH1/w== Message-ID: Date: Tue, 3 Mar 2026 18:02:04 +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: Julian Orth , Maarten Lankhorst Cc: =?UTF-8?Q?Christian_K=C3=B6nig?= , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Dmitry Osipenko , Rob Clark , 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> <5a816b1b-fae3-42d9-95eb-b1706a91d138@linux.intel.com> From: =?UTF-8?Q?Michel_D=C3=A4nzer?= Content-Language: en-CA In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-MBO-RS-ID: c7ac7d6a695afd57a12 X-MBO-RS-META: 956ruwp1f4m1npiopkwcrud7o58pzg5e On 3/3/26 16:21, Julian Orth wrote: > On Tue, Mar 3, 2026 at 4:15 PM Maarten Lankhorst > wrote: >> >> Hey, >> >> Den 2026-03-03 kl. 15:59, skrev Christian König: >>> 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. >> I know that in case of xe, even padding members are tested for being zero. For new code it's >> explicitly recommended to test to prevent running into undefined behavior. In some cases >> data may look valid, even if it's just random garbage from the stack. > > This isn't about padding fields. This is about a new field that was > added to the struct, increasing its size. Existing code could not have > zero-initialized that field except by using memset or something > equivalent. As long as the requirement to use memset is not > documented, requiring existing code to use memset is a breaking change > because code that didn't use memset always worked until the new field > was added. It's always been effectively mandatory, it's not a breaking change. -- Earthling Michel Dänzer \ GNOME / Xwayland / Mesa developer https://redhat.com \ Libre software enthusiast