[PATCH RESEND] wireless: Use offsetof instead of custom macro.

4 views
Skip to first unread message

Pi-Hsun Shih

unread,
Nov 27, 2019, 10:42:00 PM11/27/19
to Pi-Hsun Shih, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
Use offsetof to calculate offset of a field to take advantage of
compiler built-in version when possible, and avoid UBSAN warning when
compiling with Clang:

==================================================================
UBSAN: Undefined behaviour in net/wireless/wext-core.c:525:14
member access within null pointer of type 'struct iw_point'
CPU: 3 PID: 165 Comm: kworker/u16:3 Tainted: G S W 4.19.23 #43
Workqueue: cfg80211 __cfg80211_scan_done [cfg80211]
Call trace:
dump_backtrace+0x0/0x194
show_stack+0x20/0x2c
__dump_stack+0x20/0x28
dump_stack+0x70/0x94
ubsan_epilogue+0x14/0x44
ubsan_type_mismatch_common+0xf4/0xfc
__ubsan_handle_type_mismatch_v1+0x34/0x54
wireless_send_event+0x3cc/0x470
___cfg80211_scan_done+0x13c/0x220 [cfg80211]
__cfg80211_scan_done+0x28/0x34 [cfg80211]
process_one_work+0x170/0x35c
worker_thread+0x254/0x380
kthread+0x13c/0x158
ret_from_fork+0x10/0x18
===================================================================

Signed-off-by: Pi-Hsun Shih <pih...@chromium.org>
---
include/uapi/linux/wireless.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/include/uapi/linux/wireless.h b/include/uapi/linux/wireless.h
index 86eca3208b6b..f259cca5cc2b 100644
--- a/include/uapi/linux/wireless.h
+++ b/include/uapi/linux/wireless.h
@@ -1090,8 +1090,7 @@ struct iw_event {
/* iw_point events are special. First, the payload (extra data) come at
* the end of the event, so they are bigger than IW_EV_POINT_LEN. Second,
* we omit the pointer, so start at an offset. */
-#define IW_EV_POINT_OFF (((char *) &(((struct iw_point *) NULL)->length)) - \
- (char *) NULL)
+#define IW_EV_POINT_OFF offsetof(struct iw_point, length)
#define IW_EV_POINT_LEN (IW_EV_LCP_LEN + sizeof(struct iw_point) - \
IW_EV_POINT_OFF)


base-commit: 1875ff320f14afe21731a6e4c7b46dd33e45dfaa
--
2.24.0.393.g34dc348eaf-goog

Joe Perches

unread,
Nov 27, 2019, 10:54:23 PM11/27/19
to Pi-Hsun Shih, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
On Thu, 2019-11-28 at 11:39 +0800, Pi-Hsun Shih wrote:
> Use offsetof to calculate offset of a field to take advantage of
> compiler built-in version when possible, and avoid UBSAN warning when
> compiling with Clang:
[]
> diff --git a/include/uapi/linux/wireless.h b/include/uapi/linux/wireless.h
[]
> @@ -1090,8 +1090,7 @@ struct iw_event {
> /* iw_point events are special. First, the payload (extra data) come at
> * the end of the event, so they are bigger than IW_EV_POINT_LEN. Second,
> * we omit the pointer, so start at an offset. */
> -#define IW_EV_POINT_OFF (((char *) &(((struct iw_point *) NULL)->length)) - \
> - (char *) NULL)
> +#define IW_EV_POINT_OFF offsetof(struct iw_point, length)
> #define IW_EV_POINT_LEN (IW_EV_LCP_LEN + sizeof(struct iw_point) - \
> IW_EV_POINT_OFF)

This is uapi. Is offsetof guaranteed to be available?

Perhaps this is better without using another macro

#define IW_EV_POINT_OFF ((size_t)&((struct iw_point *)NULL)->length)

Pi-Hsun Shih

unread,
Nov 28, 2019, 11:05:24 PM11/28/19
to Joe Perches, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
On Thu, Nov 28, 2019 at 11:54 AM Joe Perches <j...@perches.com> wrote:
>
> On Thu, 2019-11-28 at 11:39 +0800, Pi-Hsun Shih wrote:
> > Use offsetof to calculate offset of a field to take advantage of
> > compiler built-in version when possible, and avoid UBSAN warning when
> > compiling with Clang:
> []
> > diff --git a/include/uapi/linux/wireless.h b/include/uapi/linux/wireless.h
> []
> > @@ -1090,8 +1090,7 @@ struct iw_event {
> > /* iw_point events are special. First, the payload (extra data) come at
> > * the end of the event, so they are bigger than IW_EV_POINT_LEN. Second,
> > * we omit the pointer, so start at an offset. */
> > -#define IW_EV_POINT_OFF (((char *) &(((struct iw_point *) NULL)->length)) - \
> > - (char *) NULL)
> > +#define IW_EV_POINT_OFF offsetof(struct iw_point, length)
> > #define IW_EV_POINT_LEN (IW_EV_LCP_LEN + sizeof(struct iw_point) - \
> > IW_EV_POINT_OFF)
>
> This is uapi. Is offsetof guaranteed to be available?

offsetof is already used in other uapi headers
(include/uapi/linux/fuse.h FUSE_NAME_OFFSET).

Also offsetof is also defined back in C89 standard (in stddef.h), so
it should be widely available and should be fine to use here?
(Should I add a #ifndef __KERNEL__ #include <stddef.h> to the file?)

>
> Perhaps this is better without using another macro
>
> #define IW_EV_POINT_OFF ((size_t)&((struct iw_point *)NULL)->length)

Clang UBSAN would still complain about this, since it's the same
pattern as the original one.

>

Nick Desaulniers

unread,
Dec 2, 2019, 12:27:48 PM12/2/19
to Pi-Hsun Shih, Joe Perches, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
On Thu, Nov 28, 2019 at 8:05 PM Pi-Hsun Shih <pih...@chromium.org> wrote:
>
> On Thu, Nov 28, 2019 at 11:54 AM Joe Perches <j...@perches.com> wrote:
> >
> > On Thu, 2019-11-28 at 11:39 +0800, Pi-Hsun Shih wrote:
> > > Use offsetof to calculate offset of a field to take advantage of
> > > compiler built-in version when possible, and avoid UBSAN warning when
> > > compiling with Clang:
> > []
> > > diff --git a/include/uapi/linux/wireless.h b/include/uapi/linux/wireless.h
> > []
> > > @@ -1090,8 +1090,7 @@ struct iw_event {
> > > /* iw_point events are special. First, the payload (extra data) come at
> > > * the end of the event, so they are bigger than IW_EV_POINT_LEN. Second,
> > > * we omit the pointer, so start at an offset. */
> > > -#define IW_EV_POINT_OFF (((char *) &(((struct iw_point *) NULL)->length)) - \
> > > - (char *) NULL)
> > > +#define IW_EV_POINT_OFF offsetof(struct iw_point, length)
> > > #define IW_EV_POINT_LEN (IW_EV_LCP_LEN + sizeof(struct iw_point) - \
> > > IW_EV_POINT_OFF)
> >
> > This is uapi. Is offsetof guaranteed to be available?
>
> offsetof is already used in other uapi headers
> (include/uapi/linux/fuse.h FUSE_NAME_OFFSET).
>
> Also offsetof is also defined back in C89 standard (in stddef.h), so
> it should be widely available and should be fine to use here?
> (Should I add a #ifndef __KERNEL__ #include <stddef.h> to the file?)

Yes, please, otherwise userspace could have a
-Wimplicit-function-definition warning from including this header,
since it would look like a function call to a previously undeclared
function.

Actually, it looks like include/uapi/linux/posix_types.h includes it
unconditionally, and many other headers under include/uapi/ include
include/uapi/linux/posix_types.h (unconditionally). So it may be ok
to just include stddef.h unconditionally, but please do so in addition
to the current diff you have.

--
Thanks,
~Nick Desaulniers

Pi-Hsun Shih

unread,
Dec 4, 2019, 3:13:17 AM12/4/19
to Pi-Hsun Shih, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
Use offsetof to calculate offset of a field to take advantage of
compiler built-in version when possible, and avoid UBSAN warning when
compiling with Clang:

==================================================================
UBSAN: Undefined behaviour in net/wireless/wext-core.c:525:14
member access within null pointer of type 'struct iw_point'
CPU: 3 PID: 165 Comm: kworker/u16:3 Tainted: G S W 4.19.23 #43
Workqueue: cfg80211 __cfg80211_scan_done [cfg80211]
Call trace:
dump_backtrace+0x0/0x194
show_stack+0x20/0x2c
__dump_stack+0x20/0x28
dump_stack+0x70/0x94
ubsan_epilogue+0x14/0x44
ubsan_type_mismatch_common+0xf4/0xfc
__ubsan_handle_type_mismatch_v1+0x34/0x54
wireless_send_event+0x3cc/0x470
___cfg80211_scan_done+0x13c/0x220 [cfg80211]
__cfg80211_scan_done+0x28/0x34 [cfg80211]
process_one_work+0x170/0x35c
worker_thread+0x254/0x380
kthread+0x13c/0x158
ret_from_fork+0x10/0x18
===================================================================

Signed-off-by: Pi-Hsun Shih <pih...@chromium.org>
---

Change since v1:
* Add #include <stddef.h>

---
include/uapi/linux/wireless.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/include/uapi/linux/wireless.h b/include/uapi/linux/wireless.h
index 86eca3208b6b..a2c006a364e0 100644
--- a/include/uapi/linux/wireless.h
+++ b/include/uapi/linux/wireless.h
@@ -74,6 +74,8 @@
#include <linux/socket.h> /* for "struct sockaddr" et al */
#include <linux/if.h> /* for IFNAMSIZ and co... */

+#include <stddef.h> /* for offsetof */
+
/***************************** VERSION *****************************/
/*
* This constant is used to know the availability of the wireless
@@ -1090,8 +1092,7 @@ struct iw_event {
/* iw_point events are special. First, the payload (extra data) come at
* the end of the event, so they are bigger than IW_EV_POINT_LEN. Second,
* we omit the pointer, so start at an offset. */
-#define IW_EV_POINT_OFF (((char *) &(((struct iw_point *) NULL)->length)) - \
- (char *) NULL)
+#define IW_EV_POINT_OFF offsetof(struct iw_point, length)
#define IW_EV_POINT_LEN (IW_EV_LCP_LEN + sizeof(struct iw_point) - \
IW_EV_POINT_OFF)


base-commit: c5db92909beddadddb705b92d3388ea50b01e1a2
--
2.24.0.393.g34dc348eaf-goog

Nick Desaulniers

unread,
Dec 4, 2019, 12:02:43 PM12/4/19
to Pi-Hsun Shih, linux-w...@vger.kernel.org, Johannes Berg, open list, open list:CLANG/LLVM BUILD SUPPORT
Thanks for following up on the feedback.
Reviewed-by: Nick Desaulniers <ndesau...@google.com>
> --
> You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to clang-built-li...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20191204081307.138765-1-pihsun%40chromium.org.



--
Thanks,
~Nick Desaulniers
Reply all
Reply to author
Forward
0 new messages