diff mbox series

[2/2] kbuild: forbid exported headers from including <stdint.h>, <stdbool.h>

Message ID 20220328172130.197319-2-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/2] agpgart.h: do not include <stdlib.h> from exported header | expand

Commit Message

Masahiro Yamada March 28, 2022, 5:21 p.m. UTC
Some UAPI headers included <stdlib.h>, like this:

  #ifndef __KERNEL__
  #include <stdlib.h>
  #endif

As it turned out, they just included it for no good reason.

After some fixes, now I can compile-test UAPI headers
(CONFIG_UAPI_HEADER_TEST=y) without <stdlib.h> included.

To avoid somebody getting it back again, this commit adds the dummy
header, usr/dummy-include/stdlib.h

I added $(srctree)/usr/dummy-include to the header search paths.
Because it is searched before the system directories, if someone
tries to include <stdlib.h>, they will see the error message.

While I am here, I also replaced $(objtree)/usr/include with $(obj), but
it is just a small refactoring.

If we achieve the situation where none of system headers is included
from exported kernel headers (i.e. kernel headers become self-contained),
we might be able to add -nostdinc, but that is much far from where we
stand now. (see many no-header-test lines in usr/include/Makefile)

As a realistic solution, you can forbid header inclusion individually by
putting a dummy header into usr/dummy-include/.

Currently, no header include <stdbool.h>. I put it as well before somebody
attempts to use it.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 usr/dummy-include/stdbool.h | 7 +++++++
 usr/dummy-include/stdlib.h  | 7 +++++++
 usr/include/Makefile        | 2 +-
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 usr/dummy-include/stdbool.h
 create mode 100644 usr/dummy-include/stdlib.h

Comments

Greg KH March 29, 2022, 6:04 a.m. UTC | #1
On Tue, Mar 29, 2022 at 02:21:30AM +0900, Masahiro Yamada wrote:
> Some UAPI headers included <stdlib.h>, like this:
> 
>   #ifndef __KERNEL__
>   #include <stdlib.h>
>   #endif
> 
> As it turned out, they just included it for no good reason.
> 
> After some fixes, now I can compile-test UAPI headers
> (CONFIG_UAPI_HEADER_TEST=y) without <stdlib.h> included.
> 
> To avoid somebody getting it back again, this commit adds the dummy
> header, usr/dummy-include/stdlib.h
> 
> I added $(srctree)/usr/dummy-include to the header search paths.
> Because it is searched before the system directories, if someone
> tries to include <stdlib.h>, they will see the error message.
> 
> While I am here, I also replaced $(objtree)/usr/include with $(obj), but
> it is just a small refactoring.
> 
> If we achieve the situation where none of system headers is included
> from exported kernel headers (i.e. kernel headers become self-contained),
> we might be able to add -nostdinc, but that is much far from where we
> stand now. (see many no-header-test lines in usr/include/Makefile)
> 
> As a realistic solution, you can forbid header inclusion individually by
> putting a dummy header into usr/dummy-include/.
> 
> Currently, no header include <stdbool.h>. I put it as well before somebody
> attempts to use it.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Nice work!

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Masahiro Yamada March 29, 2022, 7:16 a.m. UTC | #2
On Tue, Mar 29, 2022 at 3:04 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Tue, Mar 29, 2022 at 02:21:30AM +0900, Masahiro Yamada wrote:
> > Some UAPI headers included <stdlib.h>, like this:
> >
> >   #ifndef __KERNEL__
> >   #include <stdlib.h>
> >   #endif
> >
> > As it turned out, they just included it for no good reason.
> >
> > After some fixes, now I can compile-test UAPI headers
> > (CONFIG_UAPI_HEADER_TEST=y) without <stdlib.h> included.
> >
> > To avoid somebody getting it back again, this commit adds the dummy
> > header, usr/dummy-include/stdlib.h
> >
> > I added $(srctree)/usr/dummy-include to the header search paths.
> > Because it is searched before the system directories, if someone
> > tries to include <stdlib.h>, they will see the error message.
> >
> > While I am here, I also replaced $(objtree)/usr/include with $(obj), but
> > it is just a small refactoring.
> >
> > If we achieve the situation where none of system headers is included
> > from exported kernel headers (i.e. kernel headers become self-contained),
> > we might be able to add -nostdinc, but that is much far from where we
> > stand now. (see many no-header-test lines in usr/include/Makefile)
> >
> > As a realistic solution, you can forbid header inclusion individually by
> > putting a dummy header into usr/dummy-include/.
> >
> > Currently, no header include <stdbool.h>. I put it as well before somebody
> > attempts to use it.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
>
> Nice work!
>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>


I made a mistake in the patch subject.

The correct title should be:

    kbuild: forbid exported headers from including <stdlib.h>, <stdbool.h>

I will fix it in v2.



We cannot ban <stdint.h> for now because there are still some users.

A fix-up patch exists, but the fuse maintainer was opposed to it.
https://lore.kernel.org/lkml/20220318171405.2728855-1-cmllamas@google.com/
Greg KH March 29, 2022, 10:35 a.m. UTC | #3
On Tue, Mar 29, 2022 at 04:16:02PM +0900, Masahiro Yamada wrote:
> On Tue, Mar 29, 2022 at 3:04 PM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Tue, Mar 29, 2022 at 02:21:30AM +0900, Masahiro Yamada wrote:
> > > Some UAPI headers included <stdlib.h>, like this:
> > >
> > >   #ifndef __KERNEL__
> > >   #include <stdlib.h>
> > >   #endif
> > >
> > > As it turned out, they just included it for no good reason.
> > >
> > > After some fixes, now I can compile-test UAPI headers
> > > (CONFIG_UAPI_HEADER_TEST=y) without <stdlib.h> included.
> > >
> > > To avoid somebody getting it back again, this commit adds the dummy
> > > header, usr/dummy-include/stdlib.h
> > >
> > > I added $(srctree)/usr/dummy-include to the header search paths.
> > > Because it is searched before the system directories, if someone
> > > tries to include <stdlib.h>, they will see the error message.
> > >
> > > While I am here, I also replaced $(objtree)/usr/include with $(obj), but
> > > it is just a small refactoring.
> > >
> > > If we achieve the situation where none of system headers is included
> > > from exported kernel headers (i.e. kernel headers become self-contained),
> > > we might be able to add -nostdinc, but that is much far from where we
> > > stand now. (see many no-header-test lines in usr/include/Makefile)
> > >
> > > As a realistic solution, you can forbid header inclusion individually by
> > > putting a dummy header into usr/dummy-include/.
> > >
> > > Currently, no header include <stdbool.h>. I put it as well before somebody
> > > attempts to use it.
> > >
> > > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> >
> > Nice work!
> >
> > Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> 
> I made a mistake in the patch subject.
> 
> The correct title should be:
> 
>     kbuild: forbid exported headers from including <stdlib.h>, <stdbool.h>
> 
> I will fix it in v2.

Ah, missed that, yes.

> We cannot ban <stdint.h> for now because there are still some users.
> 
> A fix-up patch exists, but the fuse maintainer was opposed to it.
> https://lore.kernel.org/lkml/20220318171405.2728855-1-cmllamas@google.com/

It's not ok for one lone file to break the rules that all other uapi
header files follow.  I think that needs to be fixed, there is NOTHING
special about that one subsystem to justify this.

Uniformity is good, and should be followed, and I strongly think that
change needs to be accepted.

thanks,

greg k-h
diff mbox series

Patch

diff --git a/usr/dummy-include/stdbool.h b/usr/dummy-include/stdbool.h
new file mode 100644
index 000000000000..54ff9e9c90ac
--- /dev/null
+++ b/usr/dummy-include/stdbool.h
@@ -0,0 +1,7 @@ 
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _STDBOOL_H
+#define _STDBOOL_H
+
+#error "Please do not include <stdbool.h> from exported headers"
+
+#endif /* _STDBOOL_H */
diff --git a/usr/dummy-include/stdlib.h b/usr/dummy-include/stdlib.h
new file mode 100644
index 000000000000..e8c21888e371
--- /dev/null
+++ b/usr/dummy-include/stdlib.h
@@ -0,0 +1,7 @@ 
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+#error "Please do not include <stdlib.h> from exported headers"
+
+#endif /* _STDLIB_H */
diff --git a/usr/include/Makefile b/usr/include/Makefile
index fa9819e022b7..7740777b49f8 100644
--- a/usr/include/Makefile
+++ b/usr/include/Makefile
@@ -15,7 +15,7 @@  UAPI_CFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CFLAGS))
 # USERCFLAGS might contain sysroot location for CC.
 UAPI_CFLAGS += $(USERCFLAGS)
 
-override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
+override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I $(obj) -I $(srctree)/usr/dummy-include
 
 # The following are excluded for now because they fail to build.
 #