Linux Hardening
 help / color / mirror / Atom feed
* [PATCH v2] drm/ttm: Silence randstruct warning about casting struct file
@ 2025-05-02  0:24 Kees Cook
  2025-05-02  2:13 ` Matthew Brost
  0 siblings, 1 reply; 11+ messages in thread
From: Kees Cook @ 2025-05-02  0:24 UTC (permalink / raw)
  To: Thomas Hellström
  Cc: Kees Cook, Matthew Brost, Christian Koenig, Somalapuram Amaranath,
	Huang Rui, Matthew Auld, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter, dri-devel,
	linux-kernel, linux-hardening

Casting through a "void *" isn't sufficient to convince the randstruct
GCC plugin that the result is intentional. Instead operate through an
explicit union to silence the warning:

drivers/gpu/drm/ttm/ttm_backup.c: In function 'ttm_file_to_backup':
drivers/gpu/drm/ttm/ttm_backup.c:21:16: note: randstruct: casting between randomized structure pointer types (ssa): 'struct ttm_backup' and 'struct file'
   21 |         return (void *)file;
      |                ^~~~~~~~~~~~

Suggested-by: Matthew Brost <matthew.brost@intel.com>
Fixes: e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")
Signed-off-by: Kees Cook <kees@kernel.org>
---
 v2: use struct and container_of (matthew)
 v1: https://lore.kernel.org/all/20250501195859.work.107-kees@kernel.org/
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Christian Koenig <christian.koenig@amd.com>
Cc: Somalapuram Amaranath <Amaranath.Somalapuram@amd.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Cc: <dri-devel@lists.freedesktop.org>
---
 drivers/gpu/drm/ttm/ttm_backup.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 93c007f18855..60cff6c60db4 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -9,16 +9,21 @@
 
 /*
  * Casting from randomized struct file * to struct ttm_backup * is fine since
- * struct ttm_backup is never defined nor dereferenced.
+ * struct ttm_backup is never defined nor dereferenced. Use a single-member
+ * struct to avoid cast warnings.
  */
+struct ttm_backup {
+	struct file file;
+};
+
 static struct file *ttm_backup_to_file(struct ttm_backup *backup)
 {
-	return (void *)backup;
+	return &backup->file;
 }
 
 static struct ttm_backup *ttm_file_to_backup(struct file *file)
 {
-	return (void *)file;
+	return container_of(file, struct ttm_backup, file);
 }
 
 /*
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-05-02 10:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-02  0:24 [PATCH v2] drm/ttm: Silence randstruct warning about casting struct file Kees Cook
2025-05-02  2:13 ` Matthew Brost
2025-05-02  2:24   ` Kees Cook
2025-05-02  2:34   ` Al Viro
2025-05-02  2:46     ` Kees Cook
2025-05-02  4:26     ` Matthew Brost
2025-05-02  4:31       ` Al Viro
2025-05-02  4:52         ` Matthew Brost
2025-05-02  5:33           ` Al Viro
2025-05-02  7:49             ` Christian König
2025-05-02 10:44               ` Thomas Hellström

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox