Skip to content

Commit b78588c

Browse files
Peter Zijlstrasmb49
authored andcommitted
module: Provide EXPORT_SYMBOL_GPL_FOR_MODULES() helper
BugLink: https://bugs.launchpad.net/bugs/2122072 [ Upstream commit 707f853d7fa3ce323a6875487890c213e34d81a0 ] Helper macro to more easily limit the export of a symbol to a given list of modules. Eg: EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm"); will limit the use of said function to kvm.ko, any other module trying to use this symbol will refure to load (and get modpost build failures). Requested-by: Masahiro Yamada <masahiroy@kernel.org> Requested-by: Christoph Hellwig <hch@infradead.org> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Reviewed-by: Petr Pavlu <petr.pavlu@suse.com> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Stable-dep-of: cbe4134ea4bc ("fs: export anon_inode_make_secure_inode() and fix secretmem LSM bypass") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Noah Wager <noah.wager@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 3489d92 commit b78588c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Documentation/core-api/symbol-namespaces.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ kernel. As of today, modules that make use of symbols exported into namespaces,
2828
are required to import the namespace. Otherwise the kernel will, depending on
2929
its configuration, reject loading the module or warn about a missing import.
3030

31+
Additionally, it is possible to put symbols into a module namespace, strictly
32+
limiting which modules are allowed to use these symbols.
33+
3134
2. How to define Symbol Namespaces
3235
==================================
3336

@@ -83,6 +86,22 @@ unit as preprocessor statement. The above example would then read::
8386
within the corresponding compilation unit before the #include for
8487
<linux/export.h>. Typically it's placed before the first #include statement.
8588

89+
2.3 Using the EXPORT_SYMBOL_GPL_FOR_MODULES() macro
90+
===================================================
91+
92+
Symbols exported using this macro are put into a module namespace. This
93+
namespace cannot be imported.
94+
95+
The macro takes a comma separated list of module names, allowing only those
96+
modules to access this symbol. Simple tail-globs are supported.
97+
98+
For example:
99+
100+
EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm,kvm-*")
101+
102+
will limit usage of this symbol to modules whoes name matches the given
103+
patterns.
104+
86105
3. How to use Symbols exported in Namespaces
87106
============================================
88107

@@ -154,3 +173,6 @@ in-tree modules::
154173
You can also run nsdeps for external module builds. A typical usage is::
155174

156175
$ make -C <path_to_kernel_src> M=$PWD nsdeps
176+
177+
Note: it will happily generate an import statement for the module namespace;
178+
which will not work and generates build and runtime failures.

include/linux/export.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
.long sym
2525
#endif
2626

27-
#define ___EXPORT_SYMBOL(sym, license, ns) \
27+
/*
28+
* LLVM integrated assembler cam merge adjacent string literals (like
29+
* C and GNU-as) passed to '.ascii', but not to '.asciz' and chokes on:
30+
*
31+
* .asciz "MODULE_" "kvm" ;
32+
*/
33+
#define ___EXPORT_SYMBOL(sym, license, ns...) \
2834
.section ".export_symbol","a" ASM_NL \
2935
__export_symbol_##sym: ASM_NL \
3036
.asciz license ASM_NL \
31-
.asciz ns ASM_NL \
37+
.ascii ns "\0" ASM_NL \
3238
__EXPORT_SYMBOL_REF(sym) ASM_NL \
3339
.previous
3440

@@ -85,4 +91,6 @@
8591
#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", ns)
8692
#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "GPL", ns)
8793

94+
#define EXPORT_SYMBOL_GPL_FOR_MODULES(sym, mods) __EXPORT_SYMBOL(sym, "GPL", "module:" mods)
95+
8896
#endif /* _LINUX_EXPORT_H */

0 commit comments

Comments
 (0)