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:   Thu, 10 Feb 2022 20:04:34 +0300
From:   Dmitry Baryshkov <dmitry.baryshkov@...aro.org>
To:     linux-kernel@...r.kernel.org,
        Andrew Morton <akpm@...ux-foundation.org>
Cc:     Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Subject: [PATCH] container_of.h: make container_of const-aware

container_of() macro has one major drawback. It does not check whether
the passed ptr has a const pointer, the result will always be a
non-const pointer. Use a _Generic() construct (supported since gcc 4.9
and Clang 3.0) to teach container_of that if converting a const pointer,
the returned pointer should also have the const modifier.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@...aro.org>
---
 include/linux/container_of.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 2f4944b791b8..269f64e27b09 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -19,7 +19,11 @@
 	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
 		      __same_type(*(ptr), void),			\
 		      "pointer type mismatch in container_of()");	\
-	((type *)(__mptr - offsetof(type, member))); })
+	_Generic((ptr),							\
+		const typeof(((type *)0)->member)*:			\
+			((const type *)(__mptr - offsetof(type, member))), \
+		default: ((type *)(__mptr - offsetof(type, member)))	\
+	); })
 
 /**
  * container_of_safe - cast a member of a structure out to the containing structure
@@ -35,6 +39,10 @@
 		      __same_type(*(ptr), void),			\
 		      "pointer type mismatch in container_of_safe()");	\
 	IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :			\
-		((type *)(__mptr - offsetof(type, member))); })
+	_Generic((ptr),							\
+		const typeof(((type *)0)->member)*:			\
+			((const type *)(__mptr - offsetof(type, member))), \
+		default: ((type *)(__mptr - offsetof(type, member)))	\
+	); })
 
 #endif	/* _LINUX_CONTAINER_OF_H */
-- 
2.34.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ