Skip to contentSkip to conversation

do not export kernel's NULL #define to userspace

PATCH Superseded Lubos Lunak 2 participants 5 messages Mar 21, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 do not export kernel's NULL #define to userspace

GCC's NULL is actually __null, which allows detecting some questionable NULL usage and warn about it. Moreover each platform/compiler should have its own stddef.h anyway (which is different from linux/stddef.h). So there's no good reason to leak kernel's NULL to userspace and override what the compiler provides.

---
 include/linux/stddef.h |    8 ++------
 1 files changed, 2 insertions(+), 6 deletions(-)
include/linux/stddef.h +2 -6
index 6a40c76..1747b67 100644
@@ -3,14 +3,10 @@
 
 #include <linux/compiler.h>
 
+#ifdef __KERNEL__
+
 #undef NULL
-#if defined(__cplusplus)
-#define NULL 0
-#else
 #define NULL ((void *)0)
-#endif
-
-#ifdef __KERNEL__
 
 enum {
 	false	= 0,
Signature · 4 lines
1.7.3.4
-- 
 Lubos Lunak
 [email protected]
#1 lore raw
Quoted 7 lines · Lubos Lunak

GCC's NULL is actually __null, which allows detecting some questionable NULL usage and warn about it. Moreover each platform/compiler should have its own stddef.h anyway (which is different from linux/stddef.h). So there's no good reason to leak kernel's NULL to userspace and override what the compiler provides.

Yes, this looks like a bug, but I'm not sure that the solution is sufficient. Have you checked the other exported header files for whether they use NULL after including linux/stddef.h?

If so, we might have to replace it with a __KERNEL_NULL constant or something, like we do for the stuff in linux/types.h, so we don't accidentally break user applications that rely on the header files to be self-contained.

I think there is at least a NULL usage in linux/wireless.h and some netfilter headers.

Arnd

#2 lore raw
Quoted 7 lines · Arnd Bergmann
Quoted 6 lines · Lubos Lunak

GCC's NULL is actually __null, which allows detecting some questionable NULL usage and warn about it. Moreover each platform/compiler should have its own stddef.h anyway (which is different from linux/stddef.h). So there's no good reason to leak kernel's NULL to userspace and override what the compiler provides.

Quoted 5 lines
Quoted 1 line

Yes, this looks like a bug, but I'm not sure that the solution is sufficient. Have you checked the other exported header files for whether they use NULL after including linux/stddef.h?

I have checked that the kernel builds with this change, and I have checked
that the whole of LibreOffice compiles after the change. I'm not sure what you mean exactly, are you asking me to manually inspect everything under include/linux for NULL usage?

Quoted 7 lines

If so, we might have to replace it with a __KERNEL_NULL constant or something, like we do for the stuff in linux/types.h, so we don't accidentally break user applications that rely on the header files to be self-contained.

I think there is at least a NULL usage in linux/wireless.h and some netfilter headers.

I see. How about the attached patch then?

Signature · 2 lines
 Lubos Lunak
 [email protected]

From 9be4118ed1bc6221d4f2a73cad69de33285b0b2a Mon Sep 17 00:00:00 2001
From: Lubos Lunak <[email protected]>
Date: Wed, 21 Mar 2012 14:04:31 +0100
Subject: [PATCH] do not export kernel's NULL #define to userspace
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC's NULL is actually __null, which allows detecting some questionable NULL usage and warn about it. Moreover each platform/compiler should have its own stddef.h anyway (which is different from linux/stddef.h). So there's no good reason to leak kernel's NULL to userspace and override what the compiler provides. Include <stddef.h> in that case to make sure linux headers are self-contained in userspace as well.

---
 include/linux/stddef.h |   10 ++++------
 1 files changed, 4 insertions(+), 6 deletions(-)
include/linux/stddef.h +4 -6
index 6a40c76..1db853b 100644
@@ -3,14 +3,10 @@
 
 #include <linux/compiler.h>
 
+#ifdef __KERNEL__
+
 #undef NULL
-#if defined(__cplusplus)
-#define NULL 0
-#else
 #define NULL ((void *)0)
-#endif
-
-#ifdef __KERNEL__
 
 enum {
 	false	= 0,
@@ -23,6 +19,8 @@ enum {
 #else
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
+#else /* __KERNEL__ */
+#include <stddef.h>
 #endif /* __KERNEL__ */
 
 #endif
Signature · 1 line
1.7.3.4
#3 lore raw
Quoted 9 lines · Lubos Lunak
Quoted 7 lines

If so, we might have to replace it with a __KERNEL_NULL constant or something, like we do for the stuff in linux/types.h, so we don't accidentally break user applications that rely on the header files to be self-contained.

I think there is at least a NULL usage in linux/wireless.h and some netfilter headers.

I see. How about the attached patch then?

Strictly speaking, you should not include standard headers from kernel provided headers, and the problems would be similar to those before your patch: anyone who currently doesn't include <stddef.h> but has their own definition of NULL will still get a conflict from including a kernel header that includes <linux/stddef.h>.

Arnd

#4 lore raw
Quoted 9 lines · Arnd Bergmann
Quoted 8 lines · Lubos Lunak
Quoted 7 lines

If so, we might have to replace it with a __KERNEL_NULL constant or something, like we do for the stuff in linux/types.h, so we don't accidentally break user applications that rely on the header files to be self-contained.

I think there is at least a NULL usage in linux/wireless.h and some netfilter headers.

Quoted 7 lines
Quoted 1 line

I see. How about the attached patch then?

Strictly speaking, you should not include standard headers from kernel provided headers, and the problems would be similar to those before your patch: anyone who currently doesn't include <stddef.h> but has their own definition of NULL will still get a conflict from including a kernel header that includes <linux/stddef.h>.

I guess I should point out that I'm not a kernel developer, I simply want to
fix the problem that kernel headers redefine NULL to something suboptimal in userspace. So I don't know what the requirements on the headers are from the kernel side (and I wonder why you need your own NULL in the kernel when it comes with the compiler).

Let me provide one more patch then, which only surrounds the NULL definition
by #ifndef NULL instead of bluntly doing #undef NULL. That way kernel should keep using this NULL definition, while it won't be forced in userspace.

Signature · 2 lines
 Lubos Lunak
 [email protected]

From 96e2b6caa5c52daade79635270ce96ce764fcd31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <[email protected]>
Date: Wed, 21 Mar 2012 19:32:02 +0100
Subject: [PATCH] do not redefine userspace's NULL #define
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC's NULL is actually __null, which allows detecting some questionable NULL usage and warn about it. Moreover each platform/compiler should have its own stddef.h anyway (which is different from linux/stddef.h). So there's no good reason to override what the compiler provides. Keep the #define conditionally, in order to keep the headers self-contained.

---
 include/linux/stddef.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
include/linux/stddef.h +2 -1
index 6a40c76..ce225a9 100644
@@ -3,12 +3,13 @@
 
 #include <linux/compiler.h>
 
-#undef NULL
+#ifndef NULL
 #if defined(__cplusplus)
 #define NULL 0
 #else
 #define NULL ((void *)0)
 #endif
+#endif
 
 #ifdef __KERNEL__
 
Signature · 1 line
1.7.7