lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date:   Wed, 8 Mar 2023 12:35:03 -0500
From:   "Michael S. Tsirkin" <mst@...hat.com>
To:     linux-kernel@...r.kernel.org
Cc:     Jason Gunthorpe <jgg@...pe.ca>,
        Sakari Ailus <sakari.ailus@...ux.intel.com>,
        Matthew Wilcox <willy@...radead.org>,
        Jason Gunthorpe <jgg@...dia.com>,
        Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        "Rafael J . Wysocki" <rafael@...nel.org>,
        Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Subject: [PATCH] container_of: add type safety

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@...pe.ca>
Cc: Sakari Ailus <sakari.ailus@...ux.intel.com>
Cc: Matthew Wilcox (Oracle) <willy@...radead.org>
Cc: Jason Gunthorpe <jgg@...dia.com>
Cc: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Cc: Sakari Ailus <sakari.ailus@...ux.intel.com>
Cc: Rafael J. Wysocki <rafael@...nel.org>
Link: https://lore.kernel.org/r/20221205121206.166576-1-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@...uxfoundation.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ