drivers/gpu/drm/ttm/ttm_backup.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
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;
| ^~~~~~~~~~~~
Fixes: e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")
Signed-off-by: Kees Cook <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 | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 93c007f18855..626af1de562f 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -18,7 +18,13 @@ static struct file *ttm_backup_to_file(struct ttm_backup *backup)
static struct ttm_backup *ttm_file_to_backup(struct file *file)
{
- return (void *)file;
+ /* Explicit union instead of a cast to make randstruct ignore us. */
+ union {
+ struct file *file;
+ struct ttm_backup *backup;
+ } u;
+ u.file = file;
+ return u.backup;
}
/*
--
2.34.1
On Thu, May 01, 2025 at 12:59:03PM -0700, Kees Cook wrote:
> 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;
> | ^~~~~~~~~~~~
>
> Fixes: e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")
> Signed-off-by: Kees Cook <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>
What if we did something like this instead:
diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 93c007f18855..fe936a87c959 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -7,18 +7,22 @@
#include <linux/page-flags.h>
#include <linux/swap.h>
+struct ttm_backup {
+ struct file file;
+};
+
/*
* Casting from randomized struct file * to struct ttm_backup * is fine since
* struct ttm_backup is never defined nor dereferenced.
*/
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);
}
Matt
> 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 | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
> index 93c007f18855..626af1de562f 100644
> --- a/drivers/gpu/drm/ttm/ttm_backup.c
> +++ b/drivers/gpu/drm/ttm/ttm_backup.c
> @@ -18,7 +18,13 @@ static struct file *ttm_backup_to_file(struct ttm_backup *backup)
>
> static struct ttm_backup *ttm_file_to_backup(struct file *file)
> {
> - return (void *)file;
> + /* Explicit union instead of a cast to make randstruct ignore us. */
> + union {
> + struct file *file;
> + struct ttm_backup *backup;
> + } u;
> + u.file = file;
> + return u.backup;
> }
>
> /*
> --
> 2.34.1
>
On Thu, May 01, 2025 at 01:28:52PM -0700, Matthew Brost wrote:
> On Thu, May 01, 2025 at 12:59:03PM -0700, Kees Cook wrote:
> > 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;
> > | ^~~~~~~~~~~~
> >
> > Fixes: e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")
> > Signed-off-by: Kees Cook <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>
>
> What if we did something like this instead:
>
> diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
> index 93c007f18855..fe936a87c959 100644
> --- a/drivers/gpu/drm/ttm/ttm_backup.c
> +++ b/drivers/gpu/drm/ttm/ttm_backup.c
> @@ -7,18 +7,22 @@
> #include <linux/page-flags.h>
> #include <linux/swap.h>
>
> +struct ttm_backup {
> + struct file file;
> +};
> +
> /*
> * Casting from randomized struct file * to struct ttm_backup * is fine since
> * struct ttm_backup is never defined nor dereferenced.
> */
> 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);
> }
Yup, that works too! I'll send this as a v2.
--
Kees Cook
© 2016 - 2026 Red Hat, Inc.