* [PATCH] container_of: add type safety @ 2023-03-08 17:35 Michael S. Tsirkin 2023-03-08 17:57 ` Greg Kroah-Hartman 2023-03-08 17:57 ` Greg Kroah-Hartman 0 siblings, 2 replies; 4+ messages in thread From: Michael S. Tsirkin @ 2023-03-08 17:35 UTC (permalink / raw) To: linux-kernel Cc: Jason Gunthorpe, Sakari Ailus, Matthew Wilcox, Jason Gunthorpe, Andy Shevchenko, Rafael J . Wysocki, Greg Kroah-Hartman Using a wrong member in container_of will result in an error. No so for container_of_const - it is just a cast so will happily give you a wrong pointer. Use logic from container_of to add safety. Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Sakari Ailus <sakari.ailus@linux.intel.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Sakari Ailus <sakari.ailus@linux.intel.com> Cc: Rafael J. Wysocki <rafael@kernel.org> Link: https://lore.kernel.org/r/20221205121206.166576-1-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> --- include/linux/container_of.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/container_of.h b/include/linux/container_of.h index 1d898f9158b4..5d87faf72e0a 100644 --- a/include/linux/container_of.h +++ b/include/linux/container_of.h @@ -29,10 +29,13 @@ * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. */ -#define container_of_const(ptr, type, member) \ +#define container_of_const(ptr, type, member) ({ \ + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ + __same_type(*(ptr), void), \ + "pointer type mismatch in container_of()"); \ _Generic(ptr, \ const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ default: ((type *)container_of(ptr, type, member)) \ - ) + ); }) #endif /* _LINUX_CONTAINER_OF_H */ -- MST ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of: add type safety 2023-03-08 17:35 [PATCH] container_of: add type safety Michael S. Tsirkin @ 2023-03-08 17:57 ` Greg Kroah-Hartman 2023-03-08 20:58 ` Michael S. Tsirkin 2023-03-08 17:57 ` Greg Kroah-Hartman 1 sibling, 1 reply; 4+ messages in thread From: Greg Kroah-Hartman @ 2023-03-08 17:57 UTC (permalink / raw) To: Michael S. Tsirkin Cc: linux-kernel, Jason Gunthorpe, Sakari Ailus, Matthew Wilcox, Jason Gunthorpe, Andy Shevchenko, Rafael J . Wysocki On Wed, Mar 08, 2023 at 12:35:03PM -0500, Michael S. Tsirkin wrote: > Using a wrong member in container_of will result in an error. > No so for container_of_const - it is just a cast so will > happily give you a wrong pointer. > > Use logic from container_of to add safety. > > Cc: Jason Gunthorpe <jgg@ziepe.ca> > Cc: Sakari Ailus <sakari.ailus@linux.intel.com> > Cc: Matthew Wilcox (Oracle) <willy@infradead.org> > Cc: Jason Gunthorpe <jgg@nvidia.com> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > Cc: Sakari Ailus <sakari.ailus@linux.intel.com> > Cc: Rafael J. Wysocki <rafael@kernel.org> > Link: https://lore.kernel.org/r/20221205121206.166576-1-gregkh@linuxfoundation.org That's the wrong link, that's not this patch, that was an old patch. > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> I did not sign off on this. NEVER do that, you just made a legal statement in my name, why? Why did you not sign off on it? totally confused... > --- > include/linux/container_of.h | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > index 1d898f9158b4..5d87faf72e0a 100644 > --- a/include/linux/container_of.h > +++ b/include/linux/container_of.h > @@ -29,10 +29,13 @@ > * @type: the type of the container struct this is embedded in. > * @member: the name of the member within the struct. > */ > -#define container_of_const(ptr, type, member) \ > +#define container_of_const(ptr, type, member) ({ \ > + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ > + __same_type(*(ptr), void), \ > + "pointer type mismatch in container_of()"); \ Why is this needed because: > _Generic(ptr, \ > const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ > default: ((type *)container_of(ptr, type, member)) \ container_of() is used here, so shouldn't the assert trigger there if you get things wrong? So why is this change needed at all? thanks, greg k-h ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of: add type safety 2023-03-08 17:57 ` Greg Kroah-Hartman @ 2023-03-08 20:58 ` Michael S. Tsirkin 0 siblings, 0 replies; 4+ messages in thread From: Michael S. Tsirkin @ 2023-03-08 20:58 UTC (permalink / raw) To: Greg Kroah-Hartman Cc: linux-kernel, Jason Gunthorpe, Sakari Ailus, Matthew Wilcox, Jason Gunthorpe, Andy Shevchenko, Rafael J . Wysocki On Wed, Mar 08, 2023 at 06:57:13PM +0100, Greg Kroah-Hartman wrote: > On Wed, Mar 08, 2023 at 12:35:03PM -0500, Michael S. Tsirkin wrote: > > Using a wrong member in container_of will result in an error. > > No so for container_of_const - it is just a cast so will > > happily give you a wrong pointer. > > > > Use logic from container_of to add safety. > > > > Cc: Jason Gunthorpe <jgg@ziepe.ca> > > Cc: Sakari Ailus <sakari.ailus@linux.intel.com> > > Cc: Matthew Wilcox (Oracle) <willy@infradead.org> > > Cc: Jason Gunthorpe <jgg@nvidia.com> > > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > > Cc: Sakari Ailus <sakari.ailus@linux.intel.com> > > Cc: Rafael J. Wysocki <rafael@kernel.org> > > Link: https://lore.kernel.org/r/20221205121206.166576-1-gregkh@linuxfoundation.org > > That's the wrong link, that's not this patch, that was an old patch. > > > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > > I did not sign off on this. NEVER do that, you just made a legal > statement in my name, why? > > Why did you not sign off on it? > > totally confused... Ooops. I started with people who signed off on the original one and replaced S.O.B with a CC. Forgot to replace it for yours and then the script checking patches saw a signoff and was happy so I didn't notice I forgot to sign it myself ... However ... > > --- > > include/linux/container_of.h | 7 +++++-- > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > diff --git a/include/linux/container_of.h b/include/linux/container_of.h > > index 1d898f9158b4..5d87faf72e0a 100644 > > --- a/include/linux/container_of.h > > +++ b/include/linux/container_of.h > > @@ -29,10 +29,13 @@ > > * @type: the type of the container struct this is embedded in. > > * @member: the name of the member within the struct. > > */ > > -#define container_of_const(ptr, type, member) \ > > +#define container_of_const(ptr, type, member) ({ \ > > + static_assert(__same_type(*(ptr), ((type *)0)->member) || \ > > + __same_type(*(ptr), void), \ > > + "pointer type mismatch in container_of()"); \ > > Why is this needed because: > > > > _Generic(ptr, \ > > const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\ > > default: ((type *)container_of(ptr, type, member)) \ > > container_of() is used here, so shouldn't the assert trigger there if > you get things wrong? > > So why is this change needed at all? > > thanks, > > greg k-h Hmm. I think I was confused. Error did not seem to trigger but I tried again and it does. Ignore this please. Sorry about the noise. -- MST ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] container_of: add type safety 2023-03-08 17:35 [PATCH] container_of: add type safety Michael S. Tsirkin 2023-03-08 17:57 ` Greg Kroah-Hartman @ 2023-03-08 17:57 ` Greg Kroah-Hartman 1 sibling, 0 replies; 4+ messages in thread From: Greg Kroah-Hartman @ 2023-03-08 17:57 UTC (permalink / raw) To: Michael S. Tsirkin Cc: linux-kernel, Jason Gunthorpe, Sakari Ailus, Matthew Wilcox, Jason Gunthorpe, Andy Shevchenko, Rafael J . Wysocki On Wed, Mar 08, 2023 at 12:35:03PM -0500, Michael S. Tsirkin wrote: > Using a wrong member in container_of will result in an error. > No so for container_of_const - it is just a cast so will > happily give you a wrong pointer. > > Use logic from container_of to add safety. Also your subject line is wrong :( ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-03-08 20:59 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-03-08 17:35 [PATCH] container_of: add type safety Michael S. Tsirkin 2023-03-08 17:57 ` Greg Kroah-Hartman 2023-03-08 20:58 ` Michael S. Tsirkin 2023-03-08 17:57 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox