linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernel.h: fix new warnings for container_of()
@ 2017-06-20 20:09 Arnd Bergmann
  2017-06-20 22:43 ` Kees Cook
  2017-06-21  9:25 ` Ian Abbott
  0 siblings, 2 replies; 5+ messages in thread
From: Arnd Bergmann @ 2017-06-20 20:09 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Ian Abbott, Michal Nazarewicz, Kees Cook, Borislav Petkov,
	Johannes Berg, Peter Zijlstra, Alexander Potapenko,
	Arnd Bergmann, linux-kernel

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

This seems to happen for all structures that have a zero offset
between the member and the container structure, i.e. idential
pointers.

Reverting to an intermediate pointer avoids the warning, and using
a void pointer instead of the target type should also avoid
regressing on the previous patch again.

Fixes: mmotm ("kernel.h: handle pointers to arrays better in container_of()")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 include/linux/kernel.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d043adadcf33..bd6d96cf80b1 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -856,10 +856,11 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  *
  */
 #define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
 			 !__same_type(*(ptr), void),			\
 			 "pointer type mismatch in container_of()");	\
-	((type *)((char *)(ptr) - offsetof(type, member))); })
+	((type *)(__mptr - offsetof(type, member))); })
 
 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
-- 
2.9.0

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

* Re: [PATCH] kernel.h: fix new warnings for container_of()
  2017-06-20 20:09 [PATCH] kernel.h: fix new warnings for container_of() Arnd Bergmann
@ 2017-06-20 22:43 ` Kees Cook
  2017-06-20 22:46   ` Kees Cook
  2017-06-21  9:25 ` Ian Abbott
  1 sibling, 1 reply; 5+ messages in thread
From: Kees Cook @ 2017-06-20 22:43 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Andrew Morton, Ian Abbott, Michal Nazarewicz, Borislav Petkov,
	Johannes Berg, Peter Zijlstra, Alexander Potapenko, LKML

On Tue, Jun 20, 2017 at 1:09 PM, Arnd Bergmann <arnd@arndb.de> wrote:
> I see new warnings with gcc-7.0.1 with the modified container_of():
>
> fs/f2fs/dir.c: In function 'F2FS_I':
> fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

This is actually from the randstruct plugin, not native gcc. I'll go
update the message reporting to include a "randstruct: " which should
make this more obvious.

> This seems to happen for all structures that have a zero offset
> between the member and the container structure, i.e. idential
> pointers.
>
> Reverting to an intermediate pointer avoids the warning, and using
> a void pointer instead of the target type should also avoid
> regressing on the previous patch again.
>
> Fixes: mmotm ("kernel.h: handle pointers to arrays better in container_of()")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

Thanks!

I'll take this into the kspp tree so all the fixes are in the same place.

-Kees

-- 
Kees Cook
Pixel Security

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

* Re: [PATCH] kernel.h: fix new warnings for container_of()
  2017-06-20 22:43 ` Kees Cook
@ 2017-06-20 22:46   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2017-06-20 22:46 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Andrew Morton, Ian Abbott, Michal Nazarewicz, Borislav Petkov,
	Johannes Berg, Peter Zijlstra, Alexander Potapenko, LKML

On Tue, Jun 20, 2017 at 3:43 PM, Kees Cook <keescook@chromium.org> wrote:
> On Tue, Jun 20, 2017 at 1:09 PM, Arnd Bergmann <arnd@arndb.de> wrote:
>> I see new warnings with gcc-7.0.1 with the modified container_of():
>>
>> fs/f2fs/dir.c: In function 'F2FS_I':
>> fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'
>
> This is actually from the randstruct plugin, not native gcc. I'll go
> update the message reporting to include a "randstruct: " which should
> make this more obvious.
>
>> This seems to happen for all structures that have a zero offset
>> between the member and the container structure, i.e. idential
>> pointers.
>>
>> Reverting to an intermediate pointer avoids the warning, and using
>> a void pointer instead of the target type should also avoid
>> regressing on the previous patch again.
>>
>> Fixes: mmotm ("kernel.h: handle pointers to arrays better in container_of()")
>> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>
> Thanks!
>
> I'll take this into the kspp tree so all the fixes are in the same place.

Oh, nevermind, I see the Fixes is for what's already in -mm. :) And I
see akpm picked this up already. Sorry for the noise!

-Kees

-- 
Kees Cook
Pixel Security

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

* Re: [PATCH] kernel.h: fix new warnings for container_of()
  2017-06-20 20:09 [PATCH] kernel.h: fix new warnings for container_of() Arnd Bergmann
  2017-06-20 22:43 ` Kees Cook
@ 2017-06-21  9:25 ` Ian Abbott
  2017-06-21 10:15   ` Arnd Bergmann
  1 sibling, 1 reply; 5+ messages in thread
From: Ian Abbott @ 2017-06-21  9:25 UTC (permalink / raw)
  To: Arnd Bergmann, Andrew Morton
  Cc: Michal Nazarewicz, Kees Cook, Borislav Petkov, Johannes Berg,
	Peter Zijlstra, Alexander Potapenko, linux-kernel

On 20/06/17 21:09, Arnd Bergmann wrote:
> I see new warnings with gcc-7.0.1 with the modified container_of():
>
> fs/f2fs/dir.c: In function 'F2FS_I':
> fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

Is that actually a warning, or just informational?  In any case, it 
seems like a good idea to avoid it.

>
> This seems to happen for all structures that have a zero offset
> between the member and the container structure, i.e. idential
> pointers.
>
> Reverting to an intermediate pointer avoids the warning, and using
> a void pointer instead of the target type should also avoid
> regressing on the previous patch again.
>
> Fixes: mmotm ("kernel.h: handle pointers to arrays better in container_of()")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  include/linux/kernel.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d043adadcf33..bd6d96cf80b1 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -856,10 +856,11 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
>   *
>   */
>  #define container_of(ptr, type, member) ({				\
> +	void *__mptr = (void *)(ptr);					\
>  	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
>  			 !__same_type(*(ptr), void),			\
>  			 "pointer type mismatch in container_of()");	\
> -	((type *)((char *)(ptr) - offsetof(type, member))); })
> +	((type *)(__mptr - offsetof(type, member))); })
>
>  /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
>  #ifdef CONFIG_FTRACE_MCOUNT_RECORD
>

Acked-by: Ian Abbott <abbotti@mev.co.uk>

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti@mev.co.uk> )=-
-=(                          Web: http://www.mev.co.uk/  )=-

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

* Re: [PATCH] kernel.h: fix new warnings for container_of()
  2017-06-21  9:25 ` Ian Abbott
@ 2017-06-21 10:15   ` Arnd Bergmann
  0 siblings, 0 replies; 5+ messages in thread
From: Arnd Bergmann @ 2017-06-21 10:15 UTC (permalink / raw)
  To: Ian Abbott
  Cc: Andrew Morton, Michal Nazarewicz, Kees Cook, Borislav Petkov,
	Johannes Berg, Peter Zijlstra, Alexander Potapenko,
	Linux Kernel Mailing List

On Wed, Jun 21, 2017 at 11:25 AM, Ian Abbott <abbotti@mev.co.uk> wrote:
> On 20/06/17 21:09, Arnd Bergmann wrote:
>>
>> I see new warnings with gcc-7.0.1 with the modified container_of():
>>
>> fs/f2fs/dir.c: In function 'F2FS_I':
>> fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types:
>> 'struct f2fs_inode_info' and 'struct inode'
>
>
> Is that actually a warning, or just informational?  In any case, it seems
> like a good idea to avoid it.

It's informational, not a warning. I couldn't actually figure out what the
message is trying to tell me here. Maybe the message could also be
improved?

      Arnd

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

end of thread, other threads:[~2017-06-21 10:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-20 20:09 [PATCH] kernel.h: fix new warnings for container_of() Arnd Bergmann
2017-06-20 22:43 ` Kees Cook
2017-06-20 22:46   ` Kees Cook
2017-06-21  9:25 ` Ian Abbott
2017-06-21 10:15   ` Arnd Bergmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).