Introduce the initify gcc plugin
Subsystems (26)
v4 cover Introduce the initify gcc plugin
Hi,
This is a continuation of Emese Revfy's initify plugin upstreaming. This is based on her v3, but updated with various fixes from her github tree. Additionally, I split off the printf attribute fixes and sent those separately.
This is the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization. This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage. The initify gcc plugin can be useful for embedded systems.
Originally it was a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 7.0.
Changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just __func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSE config option)
* automatically discover init/exit functions and apply the __init or __exit attributes on them
You can find more about the changes here:
https://github.com/ephox-gcc-plugins/initify
This patch set is based on v4.9-rc2.
Some build statistics about the plugin:
On allyes config (amd64, gcc-6):
* 8412 initified strings
* 167 initified functions
On allmod config (i386, gcc-6):
* 8597 initified strings
* 159 initified functions
On allyes config (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 21746728 (0x14bd428) 21488680 (0x147e428) -258048
.init.data 1338376 (0x146c08) 1683016 (0x19ae48) +344640
.text 78270904 (0x4aa51b8) 78228280 (0x4a9ab38) -42624 .init.text 1184725 (0x1213d5) 1223257 (0x12aa59) +38532 .exit.data 104 (0x000068) 17760 (0x004560) +17656
.exit.text 174473 (0x02a989) 175763 (0x02ae93) +1290
FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 102936576 (0x622b000) 102678528 (0x61ec000) -258048
03 28680192 (0x1b5a000) 29081600 (0x1bbc000) +401408
00 .text .notes __ex_table .rodata __bug_table .pci_fixup .builtin_fw
.tracedata __ksymtab __ksymtab_gpl __ksymtab_strings __init_rodata __param __modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brk
On defconfig (amd64, gcc-6):
* 1957 initified strings
* 29 initified functions
On defconfig (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 2524240 (0x268450) 2462800 (0x259450) -61440
.init.data 560256 (0x088c80) 644000 (0x09d3a0) +83744
.text 9377367 (0x8f1657) 9373079 (0x8f0597) -4288
.init.text 438586 (0x06b13a) 441828 (0x06bde4) +3242
.exit.data 0 832 (0x000340) +832
.exit.text 8857 (0x002299) 8857 (0x002299) 0
FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 13398016 (0xcc7000) 13336576 (0xcb8000) -61440
03 2203648 (0x21a000) 2293760 (0x230000) +90112
00 .text .notes __ex_table .rodata __bug_table .pci_fixup .builtin_fw
.tracedata __ksymtab __ksymtab_gpl __ksymtab_strings __init_rodata __param __modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brk
One thing of note is that this plugin triggers false positive warnings from the modpost section mismatch detector. Further work is needed to deal with this.
Changed from v3:
* Refresh from Emese's latest version.
Changed from v2:
* Check all uses when walking a use-def chain.
* Check all uses of initialized local variables and initify them if they have only nocapture uses. Previously only uses in call arguments determined whether the initializer value could be initified.
* Handle the format gcc attribute from the plugin too.
* Verify nocapture parameters of calls. Track uses of these parameters and verify that all uses are not captured. Verify only the nocapture attribute (The format attribute should be verified too.).
* Fixed wrong indexing of function arguments.
* Fixed decl comparison. When comparing two decls the tree codes must be the same.
* Search capture uses of the return value. Use negative nocapture attribute parameter on a function argument to verify that the return value is not captured.
* Stop the search for capture uses if there is a cast to integer type.
* Removed unnecessary duplication hook.
* Handle cloned functions with a changed argument list.
* Check visited tree nodes to avoid an infinite loop.
* Add a new initify plugin option: enable_init_to_exit_moves. Move a function to the exit section if it is called by __init and __exit functions too.
* Added plugin option to disable the search of capture uses in nocapture functions. We must be able to disable verification of nocapture functions because there is a lot of asm code in the str* and mem* functions on i386.
* Added some more nocapture attributes.
* Added some more printf attributes.
* Added some unverified_nocapture attributes.
* Make is_kernel_rodata() nocapture.
* Added comment for the nocapture attribute from Kees.
Changes from v1:
* Removed unnecessary nocapture attributes from boot code (Reported-by: PaX Team <[email protected]>)
* Removed nocapture attributes from functions that return the marked parameter
(Reported-by: Rasmus Villemoes <[email protected]>)
* Added nocapture attribute to strlen()
* Updated gcc-common.h from PaX
* Don't forcibly constify initified string types this caused the size reduction of the .data section (initify_plugin.c)
* Added the section mismatch problem in the commit message
PATCH v4 1/4 gcc-plugins: Add the initify gcc plugin
From: Emese Revfy <[email protected]>
The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization. This plugin will teach the compiler to find more such code and data that can be freed after initialization.
The plugin runs in two passes. The first one tries to find all functions that can be become __init/__exit. The second one moves string constants (local variables and function string arguments marked by the nocapture attribute) only referenced in __init/__exit functions to the __initconst/__exitconst sections.
The result is a reduction in memory usage, since more code is moved into __init and freed during boot. This plugin can be useful for embedded systems.
If a function is called by __init and __exit functions as well then the plugin moves it to the __exit section. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.
The instrumentation pass of the latent_entropy plugin must run after the initify plugin to increase coverage.
--- arch/Kconfig | 38 + arch/x86/Kconfig | 1 + arch/x86/lib/Makefile | 4 + include/asm-generic/vmlinux.lds.h | 2 + include/linux/compiler-gcc.h | 14 + include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 9 + scripts/gcc-plugins/gcc-common.h | 41 + scripts/gcc-plugins/initify_plugin.c | 1883 ++++++++++++++++++++++++++++++++++ 9 files changed, 1996 insertions(+) create mode 100644 scripts/gcc-plugins/initify_plugin.c
arch/Kconfig +38 -0
@@ -401,6 +401,44 @@ config GCC_PLUGIN_LATENT_ENTROPY * https://grsecurity.net/ * https://pax.grsecurity.net/ +config GCC_PLUGIN_INITIFY + bool "Free more kernel memory after init" + depends on GCC_PLUGINS + help + The kernel has a mechanism to free up code and data memory that is + only used during kernel or module initialization. Enabling this + feature will teach the compiler to find more such code and data + that can be freed after initialization. + + This plugin currently triggers false positive warnings from the + section mismatch checker. + + This plugin is the part of grsecurity/PaX. More information at: + * https://grsecurity.net/ + * https://pax.grsecurity.net/ + +config HAVE_GCC_PLUGIN_INITIFY_INIT_EXIT + bool + depends on GCC_PLUGINS + help + Move functions to the exit section if they are called by __init and + __exit functions too. + +config GCC_PLUGIN_INITIFY_INIT_EXIT + bool + depends on GCC_PLUGIN_INITIFY && HAVE_GCC_PLUGIN_INITIFY_INIT_EXIT + +config GCC_PLUGIN_INITIFY_VERBOSE + bool "Verbose" + depends on GCC_PLUGINS + depends on GCC_PLUGIN_INITIFY + help + Print all initified strings and all functions which should be + __init/__exit. + Note that the candidates identified for __init/__exit markings + depend on the current kernel configuration and thus should be verified + manually before the source code is patched. + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/Kconfig +1 -0
@@ -113,6 +113,7 @@ config X86 select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS + select HAVE_GCC_PLUGIN_INITIFY_INIT_EXIT if GCC_PLUGINS select HAVE_GENERIC_DMA_COHERENT if X86_32 select HAVE_HW_BREAKPOINT select HAVE_IDE
arch/x86/lib/Makefile +4 -0
@@ -29,6 +29,10 @@ lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o ifeq ($(CONFIG_X86_32),y) + CFLAGS_strstr_32.o += $(INITIFY_DISABLE_VERIFIY_NOCAPTURE_FUNCTIONS) + CFLAGS_string_32.o += $(INITIFY_DISABLE_VERIFIY_NOCAPTURE_FUNCTIONS) + CFLAGS_memcpy_32.o += $(INITIFY_DISABLE_VERIFIY_NOCAPTURE_FUNCTIONS) + obj-y += atomic64_32.o lib-y += atomic64_cx8_32.o lib-y += checksum_32.o
include/asm-generic/vmlinux.lds.h +2 -0
@@ -551,6 +551,7 @@ MEM_DISCARD(init.data) \ KERNEL_CTORS() \MCOUNT_REC () \ + *(.init.rodata.str) \ *(.init.rodata) \ FTRACE_EVENTS() \TRACE_SYSCALLS () \
@@ -578,6 +579,7 @@ *(.fini_array) \ *(.dtors) \MEM_DISCARD (exit.data) \ + *(.exit.rodata.str) \ MEM_DISCARD(exit.rodata)#define EXIT_TEXT \
include/linux/compiler-gcc.h +14 -0
@@ -196,6 +196,20 @@#endif /* +* The initify gcc-plugin attempts to identify const arguments that are only +* used during init (see __init and __exit), so they can be moved to the +* .init.rodata/.exit.rodata section. If an argument is passed to a non-init +* function, it must normally be assumed that such an argument has been +* captured by that function and may be used in the future when .init/.exit has +* been unmapped from memory. In order to identify functions that are confirmed +* to not capture their arguments, the __nocapture() attribute is used so that +* initify can better identify candidate variables. +*/ +#ifdef INITIFY_PLUGIN +#define __nocapture (...) __attribute__((nocapture(__VA_ARGS__))) +#endif + +/* * Mark a position in code as unreachable. This can be used to * suppress control flow warnings after asm blocks that transfer * control elsewhere.
include/linux/compiler.h +4 -0
@@ -433,6 +433,10 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s# define __latent_entropy#endif +#ifndef __nocapture +# define __nocapture (...) +#endif +/* * Tell gcc if a function is cold. The compiler will assume any path * directly leading to the call is unlikely.
scripts/Makefile.gcc-plugins +9 -0
@@ -12,6 +12,14 @@ ifdef CONFIG_GCC_PLUGINS DISABLE_LATENT_ENTROPY_PLUGIN += -fplugin-arg-latent_entropy_plugin-disable endif + gcc-plugin-$(CONFIG_GCC_PLUGIN_INITIFY) += initify_plugin.so + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_INITIFY) += -DINITIFY_PLUGIN -fplugin-arg-initify_plugin-search_init_exit_functions + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_INITIFY_VERBOSE)+= -fplugin-arg-initify_plugin-verbose -fplugin-arg-initify_plugin-print_missing_attr + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_INITIFY_INIT_EXIT) += -fplugin-arg-initify_plugin-enable_init_to_exit_moves + ifdef CONFIG_GCC_PLUGIN_INITIFY + INITIFY_DISABLE_VERIFIY_NOCAPTURE_FUNCTIONS += -fplugin-arg-initify_plugin-disable_verify_nocapture_functions + endif + ifdef CONFIG_GCC_PLUGIN_SANCOV ifeq ($(CFLAGS_KCOV),) # It is needed because of the gcc-plugin.sh and gcc version checks.
@@ -29,6 +37,7 @@ ifdef CONFIG_GCC_PLUGINS export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN + export INITIFY_DISABLE_VERIFIY_NOCAPTURE_FUNCTIONS ifneq ($(PLUGINCC),) # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
scripts/gcc-plugins/gcc-common.h +41 -0
65 lines not on this page · raw message
scripts/gcc-plugins/initify_plugin.c new file +1883 -0
1,883 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 2/4 util: Move type casts into is_kernel_rodata
In reply to Kees CookFrom: Emese Revfy <[email protected]>
This moves type casts into the is_kernel_rodata() function itself so that parameters can be marked as "nocapture" in the coming initify gcc plugin.
--- mm/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
mm/util.c +5 -5
@@ -17,10 +17,10 @@#include "internal.h" -static inline int is_kernel_rodata (unsigned long addr) +static inline int is_kernel_rodata (const void *addr) { -return addr >= (unsigned long )__start_rodata && - addr < (unsigned long )__end_rodata; +return (unsigned long )addr >= (unsigned long )__start_rodata && + (unsigned long )addr < (unsigned long )__end_rodata; }/**
@@ -31,7 +31,7 @@ static inline int is_kernel_rodata(unsigned long addr)*/ void kfree_const (const void *x) { -if (!is_kernel_rodata ((unsigned long )x)) +if (!is_kernel_rodata (x))kfree (x); }EXPORT_SYMBOL (kfree_const);
@@ -68,7 +68,7 @@ EXPORT_SYMBOL(kstrdup);*/ const char *kstrdup_const (const char *s,gfp_t gfp) { -if (is_kernel_rodata ((unsigned long )s)) +if (is_kernel_rodata (s))return s;return kstrdup (s, gfp);
Signature · 1 line
2.7.4
PATCH v4 3/4 initify: Mark functions with the __nocapture attribute
In reply to Kees CookFrom: Emese Revfy <[email protected]>
The nocapture gcc attribute can be on functions only. The attribute takes zero or more signed integer constants as parameters that specify the function parameters to initify when the passed arguments are of const char* type.
A negative attribute parameter value means that the corresponding function parameter is returned by the function and the passed argument will only be initified if the data flow of the returned value is not captured in the caller.
If no values are passed to the attribute then all function parameters are treated as nocapture.
If the marked parameter is a vararg then the plugin initifies all vararg arguments.
I couldn't test the arm, arm64 and powerpc architectures.
--- arch/arm/include/asm/string.h | 10 +++--- arch/arm64/include/asm/string.h | 25 ++++++------- arch/powerpc/include/asm/string.h | 19 +++++----- arch/x86/include/asm/string_32.h | 25 ++++++------- arch/x86/include/asm/string_64.h | 23 ++++++------ arch/x86/kernel/hpet.c | 2 +- drivers/acpi/acpica/acutils.h | 2 +- fs/char_dev.c | 2 +- fs/ntfs/debug.h | 6 ++-- fs/ocfs2/cluster/masklog.h | 2 +- include/acpi/acpixf.h | 2 +- include/asm-generic/bug.h | 7 ++-- include/drm/drmP.h | 2 +- include/linux/audit.h | 5 +-- include/linux/fs.h | 8 +++-- include/linux/printk.h | 2 +- include/linux/ratelimit.h | 3 +- include/linux/string.h | 75 ++++++++++++++++++++------------------- lib/vsprintf.c | 6 ++-- mm/util.c | 2 +- security/integrity/integrity.h | 2 +- 21 files changed, 121 insertions(+), 109 deletions(-)
arch/arm/include/asm/string.h +5 -5
@@ -7,19 +7,19 @@*/ #define __HAVE_ARCH_STRRCHR -extern char *strrchr (const char * s,int c); +extern char *strrchr (const char * s,int c)__nocapture (-1 );#define __HAVE_ARCH_STRCHR -extern char *strchr (const char * s,int c); +extern char *strchr (const char * s,int c)__nocapture (-1 );#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMMOVE -extern void *memmove (void *,const void *,__kernel_size_t ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *,int ,__kernel_size_t ); +extern void *memchr (const void *,int ,__kernel_size_t )__nocapture (-1 );#define __HAVE_ARCH_MEMSETextern void *memset (void *,int ,__kernel_size_t );
arch/arm64/include/asm/string.h +13 -12
@@ -17,40 +17,41 @@#define __ASM_STRING_H#define __HAVE_ARCH_STRRCHR -extern char *strrchr (const char *,int c); +extern char *strrchr (const char *,int c)__nocapture (-1 );#define __HAVE_ARCH_STRCHR -extern char *strchr (const char *,int c); +extern char *strchr (const char *,int c)__nocapture (-1 );#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *,const char *); +extern int strcmp(const char *, const char *) __nocapture();#define __HAVE_ARCH_STRNCMP -extern int strncmp (const char *,const char *,__kernel_size_t ); +extern int +strncmp(const char *, const char *, __kernel_size_t) __nocapture(1, 2);#define __HAVE_ARCH_STRLEN -extern __kernel_size_t strlen (const char *); +extern __kernel_size_t strlen(const char *) __nocapture(1);#define __HAVE_ARCH_STRNLEN -extern __kernel_size_t strnlen (const char *,__kernel_size_t ); +extern __kernel_size_t strnlen(const char *, __kernel_size_t) __nocapture(1);#define __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); -extern void *__memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *__memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMMOVE -extern void *memmove (void *,const void *,__kernel_size_t ); -extern void *__memmove (void *,const void *,__kernel_size_t ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *__memmove (void *,const void *,__kernel_size_t )__nocapture (2 );#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *,int ,__kernel_size_t ); +extern void *memchr (const void *,int ,__kernel_size_t )__nocapture (-1 );#define __HAVE_ARCH_MEMSETextern void *memset (void *,int ,__kernel_size_t );extern void *__memset (void *,int ,__kernel_size_t );#define __HAVE_ARCH_MEMCMP -extern int memcmp (const void *,const void *,size_t ); +extern int memcmp(const void *, const void *, size_t) __nocapture(1, 2);#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)
arch/powerpc/include/asm/string.h +10 -9
@@ -11,16 +11,17 @@#define __HAVE_ARCH_MEMCMP#define __HAVE_ARCH_MEMCHR -extern char *strcpy (char *,const char *); -extern char *strncpy (char *,const char *,__kernel_size_t ); -extern __kernel_size_t strlen (const char *); -extern int strcmp (const char *,const char *); -extern int strncmp (const char *,const char *,__kernel_size_t ); -extern char *strcat (char *,const char *); +extern char *strcpy (char *,const char *)__nocapture (2 ); +extern char *strncpy (char *,const char *,__kernel_size_t )__nocapture (2 ); +extern __kernel_size_t strlen(const char *) __nocapture(1); +extern int strcmp(const char *,const char *) __nocapture(); +extern int +strncmp(const char *, const char *, __kernel_size_t) __nocapture(1, 2); +extern char *strcat (char *,const char *)__nocapture (2 );extern void *memset (void *,int ,__kernel_size_t ); -extern void *memcpy (void *,const void *,__kernel_size_t ); -extern void *memmove (void *,const void *,__kernel_size_t ); -extern int memcmp (const void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 ); +extern int memcmp(const void *,const void *,__kernel_size_t) __nocapture(1, 2);extern void *memchr (const void *,int ,__kernel_size_t );#endif /* __KERNEL__ */
arch/x86/include/asm/string_32.h +13 -12
@@ -6,28 +6,29 @@/* Let gcc decide whether to inline or use the out of line functions */ #define __HAVE_ARCH_STRCPY -extern char *strcpy (char *dest,const char *src); +extern char *strcpy (char *dest,const char *src)__nocapture (2 );#define __HAVE_ARCH_STRNCPY -extern char *strncpy (char *dest,const char *src,size_t count); +extern char *strncpy (char *dest,const char *src,size_t count)__nocapture (2 );#define __HAVE_ARCH_STRCAT -extern char *strcat (char *dest,const char *src); +extern char *strcat (char *dest,const char *src)__nocapture (2 );#define __HAVE_ARCH_STRNCAT -extern char *strncat (char *dest,const char *src,size_t count); +extern char *strncat (char *dest,const char *src,size_t count)__nocapture (2 );#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *cs,const char *ct); +extern int strcmp(const char *cs, const char *ct) __nocapture();#define __HAVE_ARCH_STRNCMP -extern int strncmp (const char *cs,const char *ct,size_t count); +extern int +strncmp(const char *cs, const char *ct, size_t count) __nocapture(1, 2);#define __HAVE_ARCH_STRCHR -extern char *strchr (const char *s,int c); +extern char *strchr (const char *s,int c)__nocapture (-1 );#define __HAVE_ARCH_STRLEN -extern size_t strlen (const char *s); +extern size_t strlen(const char *s) __nocapture(1); static __always_inline void *__memcpy(void *to, const void *from, size_t n) {
@@ -197,12 +198,12 @@ static inline void *__memcpy3d(void *to, const void *from, size_t len)#endif #define __HAVE_ARCH_MEMMOVE -void *memmove (void *dest,const void *src,size_t n); +void *memmove (void *dest,const void *src,size_t n)__nocapture (2 );#define memcmp __builtin_memcmp#define __HAVE_ARCH_MEMCHR -extern void *memchr (const void *cs,int c,size_t count); +extern void *memchr (const void *cs,int c,size_t count)__nocapture (-1 );static inline void *__memset_generic (void *s,char c,size_t count) {
@@ -243,11 +244,11 @@ void *__constant_c_memset(void *s, unsigned long c, size_t count)/* Added by Gertjan van Wingerde to make minix and sysv module work */ #define __HAVE_ARCH_STRNLEN -extern size_t strnlen (const char *s,size_t count); +extern size_t strnlen(const char *s, size_t count) __nocapture(1);/* end of additional stuff */ #define __HAVE_ARCH_STRSTR -extern char *strstr (const char *cs,const char *ct); +extern char *strstr (const char *cs,const char *ct)__nocapture (-1 ,2 );/* * This looks horribly ugly, but the compiler can optimize it totally,
arch/x86/include/asm/string_64.h +12 -11
@@ -28,8 +28,8 @@ static __always_inline void *__inline_memcpy(void *to, const void *from, size_tfunction. */ #define __HAVE_ARCH_MEMCPY 1 -extern void *memcpy (void *to,const void *from,size_t len); -extern void *__memcpy (void *to,const void *from,size_t len); +extern void *memcpy (void *to,const void *from,size_t len)__nocapture (2 ); +extern void *__memcpy (void *to,const void *from,size_t len)__nocapture (2 );#ifndef CONFIG_KMEMCHECK #if (__GNUC__ ==4 && __GNUC_MINOR__ <3 ) || __GNUC__ <4
@@ -57,14 +57,14 @@ void *memset(void *s, int c, size_t n);void *__memset (void *s,int c,size_t n);#define __HAVE_ARCH_MEMMOVE -void *memmove (void *dest,const void *src,size_t count); -void *__memmove (void *dest,const void *src,size_t count); +void *memmove (void *dest,const void *src,size_t count)__nocapture (2 ); +void *__memmove (void *dest,const void *src,size_t count)__nocapture (2 ); -int memcmp (const void *cs,const void *ct,size_t count); -size_t strlen (const char *s); -char *strcpy (char *dest,const char *src); -char *strcat (char *dest,const char *src); -int strcmp (const char *cs,const char *ct); +int memcmp(const void *cs, const void *ct, size_t count) __nocapture(1, 2); +size_t strlen(const char *s) __nocapture(1); +char *strcpy (char *dest,const char *src)__nocapture (2 ); +char *strcat (char *dest,const char *src)__nocapture (2 ); +int strcmp(const char *cs, const char *ct) __nocapture(1, 2);#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__)
@@ -79,7 +79,8 @@ int strcmp(const char *cs, const char *ct);#define memset (s, c, n) __memset(s, c, n)#endif -__must_check int memcpy_mcsafe_unrolled (void *dst,const void *src,size_t cnt); +__must_check __nocapture(2) int +memcpy_mcsafe_unrolled (void *dst,const void *src,size_t cnt);DECLARE_STATIC_KEY_FALSE (mcsafe_key);/**
@@ -96,7 +97,7 @@ DECLARE_STATIC_KEY_FALSE(mcsafe_key);* * Return 0 for success, -EFAULT for fail */ -static __always_inline __must_check int +static __always_inline __must_check __nocapture(2) int memcpy_mcsafe(void *dst, const void *src, size_t cnt) {#ifdef CONFIG_X86_MCE
arch/x86/kernel/hpet.c +1 -1
@@ -136,7 +136,7 @@ int is_hpet_enabled(void) }EXPORT_SYMBOL_GPL (is_hpet_enabled); -static void _hpet_print_config (const char *function,int line) +static void __nocapture(1) _hpet_print_config(const char *function, int line) { u32 i, timers, l, h; printk(KERN_INFO "hpet: %s(%d):\n", function, line);
drivers/acpi/acpica/acutils.h +1 -1
@@ -289,7 +289,7 @@ void acpi_ut_init_stack_ptr_trace(void);void acpi_ut_track_stack_ptr (void ); -void +__nocapture(2) voidacpi_ut_trace (u32 line_number,const char *function_name,const char *module_name,u32 component_id);
fs/char_dev.c +1 -1
@@ -70,7 +70,7 @@ void chrdev_show(struct seq_file *f, off_t offset)* * Returns a -ve errno on failure. */ -static struct char_device_struct * +static __nocapture(4) struct char_device_struct * __register_chrdev_region(unsigned int major, unsigned int baseminor, int minorct, const char *name) {
fs/ntfs/debug.h +3 -3
@@ -30,7 +30,7 @@extern int debug_msgs; -extern __printf(4, 5) +extern __printf(4, 5) __nocapture(3) void __ntfs_debug(const char *file, int line, const char *function, const char *format, ...); /**
@@ -58,12 +58,12 @@ do { \ #endif /* !DEBUG */ -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1) void __ntfs_warning(const char *function, const struct super_block *sb, const char *fmt, ...); #define ntfs_warning(sb, f, a...) __ntfs_warning(__func__, sb, f, ##a) -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1) void __ntfs_error(const char *function, const struct super_block *sb, const char *fmt, ...); #define ntfs_error(sb, f, a...) __ntfs_error(__func__, sb, f, ##a)
fs/ocfs2/cluster/masklog.h +1 -1
@@ -162,7 +162,7 @@ extern struct mlog_bits mlog_and_bits, mlog_not_bits;#endif -__printf(4, 5) +__printf(4, 5) __nocapture(2)void __mlog_printk (const u64 *m,const char *func,int line,const char *fmt, ...);
include/acpi/acpixf.h +1 -1
@@ -929,7 +929,7 @@ ACPI_MSG_DEPENDENT_RETURN_VOID(ACPI_PRINTF_LIKE(3)/* * Debug output */ -ACPI_DBG_DEPENDENT_RETURN_VOID(ACPI_PRINTF_LIKE(6) +ACPI_DBG_DEPENDENT_RETURN_VOID(ACPI_PRINTF_LIKE(6) __nocapture(3) void ACPI_INTERNAL_VAR_XFACE acpi_debug_print(u32 requested_debug_level, u32 line_number,
include/asm-generic/bug.h +4 -3
@@ -62,13 +62,13 @@ struct bug_entry {* to provide better diagnostics. */ #ifndef __WARN_TAINT -extern __printf(3, 4) +extern __printf(3, 4) __nocapture(1) void warn_slowpath_fmt(const char *file, const int line, const char *fmt, ...); -extern __printf(4, 5) +extern __printf(4, 5) __nocapture(1) void warn_slowpath_fmt_taint(const char *file, const int line, unsigned taint, const char *fmt, ...); -extern void warn_slowpath_null(const char *file, const int line); +extern __nocapture(1) void warn_slowpath_null(const char *file, const int line); #define WANT_WARN_ON_SLOWPATH #define __WARN() warn_slowpath_null(__FILE__, __LINE__) #define __WARN_printf(arg...) warn_slowpath_fmt(__FILE__, __LINE__, arg)
@@ -84,6 +84,7 @@ extern void warn_slowpath_null(const char *file, const int line);/* used internally by panic.c */ struct warn_args ; +__nocapture(1, 0)void __warn (const char *file,int line,void *caller,unsigned taint,struct pt_regs *regs,struct warn_args *args);
include/drm/drmP.h +1 -1
@@ -136,7 +136,7 @@ struct dma_buf_attachment;#define DRM_UT_ATOMIC 0x10#define DRM_UT_VBL 0x20 -extern __printf(6, 7) +extern __printf(6, 7) __nocapture(4) void drm_dev_printk(const struct device *dev, const char *level, unsigned int category, const char *function_name, const char *prefix, const char *format, ...);
include/linux/audit.h +3 -2
@@ -135,7 +135,7 @@ extern void audit_log_n_hex(struct audit_buffer *ab, size_t len); extern void audit_log_n_string(struct audit_buffer *ab, const char *buf, -size_t n); + size_t n) __nocapture(2);extern void audit_log_n_untrustedstring (struct audit_buffer *ab,const char *string,size_t n);
@@ -552,7 +552,8 @@ static inline bool audit_loginuid_set(struct task_struct *tsk)return uid_valid (audit_get_loginuid (tsk)); } -static inline void audit_log_string (struct audit_buffer *ab,const char *buf) +static inline __nocapture(2) +void audit_log_string(struct audit_buffer *ab, const char *buf) { audit_log_n_string(ab, buf, strlen(buf)); }
include/linux/fs.h +5 -3
@@ -2456,13 +2456,15 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev,#define CHRDEV_MAJOR_HASH_SIZE 255/* Marks the bottom of the first segment of free char majors */ #define CHRDEV_MAJOR_DYN_END 234 -extern int alloc_chrdev_region (dev_t *,unsigned ,unsigned ,const char *); +extern int __nocapture(4) +alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *); extern int register_chrdev_region(dev_t, unsigned, const char *); extern int __register_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name, const struct file_operations *fops); -extern void __unregister_chrdev (unsigned int major,unsigned int baseminor, -unsigned int count,const char *name); +extern __nocapture(4) void __unregister_chrdev(unsigned int major, + unsigned int baseminor, unsigned int count, + const char *name); extern void unregister_chrdev_region(dev_t, unsigned); extern void chrdev_show(struct seq_file *,off_t);
include/linux/printk.h +1 -1
@@ -172,7 +172,7 @@ __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);* with all other unrelated printk_ratelimit() callsites. Instead use * printk_ratelimited() or plain old __ratelimit(). */ -extern int __printk_ratelimit (const char *func); +extern int __printk_ratelimit(const char *func) __nocapture(1);#define printk_ratelimit () __printk_ratelimit(__func__)extern bool printk_timed_ratelimit (unsigned long *caller_jiffies,unsigned int interval_msec);
include/linux/ratelimit.h +2 -1
@@ -72,7 +72,8 @@ ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)extern struct ratelimit_state printk_ratelimit_state; -extern int ___ratelimit (struct ratelimit_state *rs,const char *func); +extern __nocapture(2) +int ___ratelimit(struct ratelimit_state *rs, const char *func); #define __ratelimit(state) ___ratelimit(state, __func__) #ifdef CONFIG_PRINTK
include/linux/string.h +39 -36
161 lines not on this page · raw message
lib/vsprintf.c +3 -3
@@ -118,7 +118,7 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base) }EXPORT_SYMBOL (simple_strtoll); -static noinline_for_stack +static noinline_for_stack __nocapture(1) int skip_atoi(const char **s) { int i = 0;
@@ -1570,7 +1570,7 @@ int kptr_restrict __read_mostly;* function pointers are really function descriptors, which contain a * pointer to the real address. */ -static noinline_for_stack +static noinline_for_stack __nocapture(1) char *pointer(const char *fmt, char *buf, char *end, void *ptr, struct printf_spec spec) {
@@ -1749,7 +1749,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,* @precision: precision of a number * @qualifier: qualifier of a number (long, size_t, ...) */ -static noinline_for_stack +static noinline_for_stack __nocapture(1) int format_decode(const char *fmt, struct printf_spec *spec) { const char *start = fmt;
mm/util.c +1 -1
@@ -17,7 +17,7 @@#include "internal.h" -static inline int is_kernel_rodata (const void *addr) +static inline __nocapture(1) int is_kernel_rodata(const void *addr) { return (unsigned long)addr >= (unsigned long)__start_rodata && (unsigned long)addr < (unsigned long)__end_rodata;
security/integrity/integrity.h +1 -1
@@ -177,7 +177,7 @@ static inline void evm_load_x509(void)#ifdef CONFIG_INTEGRITY_AUDIT /* declarations */ -void integrity_audit_msg (int audit_msgno,struct inode *inode, +void __nocapture(4) integrity_audit_msg(int audit_msgno, struct inode *inode, const unsigned char *fname, const char *op, const char *cause, int result, int info);#else
Signature · 1 line
2.7.4
PATCH v4 4/4 initify: Mark functions with the __unverified_nocapture attribute
In reply to Kees CookFrom: Emese Revfy <[email protected]>
This attribute disables the compile data flow verification of the designated nocapture parameters of the function. Use it only on function parameters that are difficult for the plugin to analyze.
--- include/linux/compiler-gcc.h | 1 + include/linux/compiler.h | 4 ++++ lib/vsprintf.c | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-)
include/linux/compiler-gcc.h +1 -0
@@ -207,6 +207,7 @@*/ #ifdef INITIFY_PLUGIN #define __nocapture (...) __attribute__((nocapture(__VA_ARGS__))) +#define __unverified_nocapture (...) __attribute__((unverified_nocapture(__VA_ARGS__)))#endif /*
include/linux/compiler.h +4 -0
@@ -437,6 +437,10 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s# define __nocapture (...)#endif +#ifndef __unverified_nocapture +# define __unverified_nocapture (...) +#endif +/* * Tell gcc if a function is cold. The compiler will assume any path * directly leading to the call is unlikely.
lib/vsprintf.c +2 -2
@@ -118,7 +118,7 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base) }EXPORT_SYMBOL (simple_strtoll); -static noinline_for_stack __nocapture(1) +static noinline_for_stack __nocapture(1) __unverified_nocapture(1) int skip_atoi(const char **s) {int i =0 ;
@@ -1570,7 +1570,7 @@ int kptr_restrict __read_mostly;* function pointers are really function descriptors, which contain a * pointer to the real address. */ -static noinline_for_stack __nocapture(1) +static noinline_for_stack __nocapture(1) __unverified_nocapture(1) char *pointer(const char *fmt, char *buf, char *end, void *ptr,struct printf_spec spec) {
Signature · 1 line
2.7.4
Quoted 94 lines · Kees Cook
Hi,
This is a continuation of Emese Revfy's initify plugin upstreaming. This is based on her v3, but updated with various fixes from her github tree. Additionally, I split off the printf attribute fixes and sent those separately.
This is the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization. This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage. The initify gcc plugin can be useful for embedded systems.
Originally it was a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 7.0.
Changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just__func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSEconfig option)
* automatically discover init/exit functions and apply the__initor__exitattributes on themYou can find more about the changes here:
https://github.com/ephox-gcc-plugins/initifyThis patch set is based on v4.9-rc2.
Some build statistics about the plugin:
On allyes config (amd64, gcc-6):
* 8412 initified strings
* 167 initified functionsOn allmod config (i386, gcc-6):
* 8597 initified strings
* 159 initified functionsOn allyes config (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 21746728 (0x14bd428) 21488680 (0x147e428) -258048
.init.data 1338376 (0x146c08) 1683016 (0x19ae48) +344640.text 78270904 (0x4aa51b8) 78228280 (0x4a9ab38) -42624 .init.text 1184725 (0x1213d5) 1223257 (0x12aa59) +38532 .exit.data 104 (0x000068) 17760 (0x004560) +17656.exit.text 174473 (
0x02a989) 175763 (0x02ae93) +1290FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 102936576 (0x622b000) 102678528 (0x61ec000) -258048
03 28680192 (0x1b5a000) 29081600 (0x1bbc000) +40140800 .text .notes
__ex_table.rodata__bug_table.pci_fixup .builtin_fw
.tracedata__ksymtab__ksymtab_gpl__ksymtab_strings__init_rodata__param__modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brkOn defconfig (amd64, gcc-6):
* 1957 initified strings
* 29 initified functionsOn defconfig (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 2524240 (0x268450) 2462800 (0x259450) -61440
.init.data 560256 (0x088c80) 644000 (0x09d3a0) +83744
.text 9377367 (0x8f1657) 9373079 (0x8f0597) -4288
.init.text 438586 (0x06b13a) 441828 (0x06bde4) +3242
.exit.data 0 832 (0x000340) +832
.exit.text 8857 (0x002299) 8857 (0x002299) 0FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 13398016 (0xcc7000) 13336576 (0xcb8000) -61440
03 2203648 (0x21a000) 2293760 (0x230000) +9011200 .text .notes
__ex_table.rodata__bug_table.pci_fixup .builtin_fw
.tracedata__ksymtab__ksymtab_gpl__ksymtab_strings__init_rodata__param__modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brk
Quoted 3 lines
One thing of note is that this plugin triggers false positive warnings from the modpost section mismatch detector. Further work is needed to deal with this.
FWIW, it still seems to me that these aren't false positives:
WARNING: vmlinux.o(.text.unlikely+0x1b1): Section mismatch in reference from the function uncore_pci_exit.part.22() to the function .init.text:uncore_free_pcibus_map()
The function uncore_pci_exit.part.22() references
the function __init uncore_free_pcibus_map().
This is often because uncore_pci_exit.part.22 lacks a __init annotation or the annotation of uncore_free_pcibus_map is wrong.
This is complaining about arch/x86/events/intel/uncore.c:
__init intel_uncore_init() calls uncore_pci_exit(). __exit intel_uncore_exit() calls uncore_pci_exit().
uncore_pci_exit() is marked as "both" (which will resolve to __exit).
__init intel_uncore_init() calls uncore_free_pcibus_map(). uncore_pci_exit() calls uncore_free_pcibus_map().
uncore_free_pcibus_map() should be marked as "both", but it seems like it ends up marked only __init.
When I tried a build that looked only at -D MODULE being set, I got fewer warnings, but the modpost on builtins would still have warnings, but they complained about things in __exit calling __init, which for a builtin is fine: the __exit section will never be called...
I think the modular-build checking is better than the Kconfig approach. It just requires that the __exit sections for non-modules get discarded before the modpost runs to complain about it...
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Re: [PATCH v4 1/4] gcc-plugins: Add the initify gcc plugin
In reply to Kees CookQuoted 21 lines · Kees Cook
scripts/gcc-plugins/gcc-common.h +13
-0@@ -287,6 +287,26 @@ static inline struct cgraph_node *cgraph_next_function_with_gimple_body(struct creturn NULL ; } +static inline bool cgraph_for_node_and_aliases (cgraph_node_ptr node, +bool (*callback)(cgraph_node_ptr ,void *), +void *data,bool include_overwritable) +{ +cgraph_node_ptr alias; + +if (callback (node, data)) +return true; + +for (alias = node->same_body; alias; alias = alias->next) { +if (include_overwritable || +cgraph_function_body_availability (alias) >AVAIL_OVERWRITABLE ) + if (cgraph_for_node_and_aliases(alias, callback, data,
scripts/gcc-plugins/gcc-common.h static inline struct cgraph_node *cgraph_next_function_with_gimple_body(struct c · line 312
Quoted 10 lines
scripts/gcc-plugins/gcc-common.h +7
-0+ include_overwritable)) +return true; + } + +return false; +} +#define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) \ for ((node) = cgraph_first_function_with_gimple_body(); (node); \ (node) = cgraph_next_function_with_gimple_body(node))
this hunk above and...
Quoted 5 lines
scripts/gcc-plugins/gcc-common.h +1
-0@@ -674,6 +707,14 @@ static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node)return node->get_alias_target (); } +static inline bool cgraph_for_node_and_aliases(cgraph_node_ptr node,
scripts/gcc-plugins/gcc-common.h static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node) · line 720
Quoted 10 lines
scripts/gcc-plugins/gcc-common.h +7
-0+bool (*callback)(cgraph_node_ptr ,void *), + void *data, bool include_overwritable) +{ + return node->call_for_symbol_thunks_and_aliases(callback, data, +include_overwritable ); +} +static inline struct cgraph_node_hook_list *cgraph_add_function_insertion_hook (cgraph_node_hook hook,void *data) {return symtab->add_cgraph_insertion_hook (hook, data);
...this one aren't needed by any plugins upstream so maybe introduce them when the needed arises? and the whole patch against gcc-common.h would also conflict with the version i maintain and that you said you'd sync to so there's a decision to be made regarding how this will is to be maintained...
Re: [PATCH v4 1/4] gcc-plugins: Add the initify gcc plugin
Quoted 40 lines · PaX Team
Quoted 21 lines · Kees Cook
scripts/gcc-plugins/gcc-common.h +13
-0@@ -287,6 +287,26 @@ static inline struct cgraph_node *cgraph_next_function_with_gimple_body(struct creturn NULL ; } +static inline bool cgraph_for_node_and_aliases (cgraph_node_ptr node, +bool (*callback)(cgraph_node_ptr ,void *), +void *data,bool include_overwritable) +{ +cgraph_node_ptr alias; + +if (callback (node, data)) +return true; + +for (alias = node->same_body; alias; alias = alias->next) { +if (include_overwritable || +cgraph_function_body_availability (alias) >AVAIL_OVERWRITABLE ) + if (cgraph_for_node_and_aliases(alias, callback, data,scripts/gcc-plugins/gcc-common.h static inline struct cgraph_node *cgraph_next_function_with_gimple_body(struct c · line 312
Quoted 10 lines
scripts/gcc-plugins/gcc-common.h +7
-0+ include_overwritable)) +return true; + } + +return false; +} +#define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) \ for ((node) = cgraph_first_function_with_gimple_body(); (node); \ (node) = cgraph_next_function_with_gimple_body(node))this hunk above and...
Quoted 4 lines
scripts/gcc-plugins/gcc-common.h +0
-0@@ -674,6 +707,14 @@ static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node)return node->get_alias_target (); }
Quoted 14 lines
scripts/gcc-plugins/gcc-common.h static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node) · line 720
Quoted 11 lines
scripts/gcc-plugins/gcc-common.h +8
-0+static inline bool cgraph_for_node_and_aliases (cgraph_node_ptr node, +bool (*callback)(cgraph_node_ptr ,void *), +void *data,bool include_overwritable) +{ +return node->call_for_symbol_thunks_and_aliases (callback, data, + include_overwritable); +} +static inline struct cgraph_node_hook_list *cgraph_add_function_insertion_hook (cgraph_node_hook hook,void *data) {return symtab->add_cgraph_insertion_hook (hook, data);...this one aren't needed by any plugins upstream so maybe introduce them when the needed arises?
Hrm, sure. I was just going off of Emese's v3. (And this is partially an artifact of basing off of v4.9-rc2... I'll refresh it to v4.10-rc2 once it's out.)
Quoted 3 lines
and the whole patch against
gcc-common.hwould also conflict
with the version i maintain and that you said you'd sync to so there's a decision to be made regarding how this will is to be maintained...
What's easiest for you? I'm okay to carry "unused by upstream yet" functions and macros in gcc-common, though I don't like carrying lots of commented out stuff. :P
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Re: [PATCH v4 1/4] gcc-plugins: Add the initify gcc plugin
Quoted 11 lines · Kees Cook
Quoted 6 lines
Quoted 3 lines
static inline struct
cgraph_node_hook_list*cgraph_add_function_insertion_hook(cgraph_node_hook hook, void *data) {
returnsymtab->add_cgraph_insertion_hook(hook, data);...this one aren't needed by any plugins upstream so maybe introduce them when the needed arises?
Hrm, sure. I was just going off of Emese's v3. (And this is partially an artifact of basing off of v4.9-rc2... I'll refresh it to v4.10-rc2 once it's out.)
Quoted 7 lines
Quoted 3 lines
and the whole patch against
gcc-common.hwould also conflict
with the version i maintain and that you said you'd sync to so there's a decision to be made regarding how this will is to be maintained...What's easiest for you? I'm okay to carry "unused by upstream yet" functions and macros in gcc-common, though I don't like carrying lots of commented out stuff. :P
well, as i explained it the other day, my version has 'everything but the kitchen sink' because i use it for development which isn't necessarily what other projects need or want for themselves (e.g., consider the consequences of Arnd's recent call to reduce the number of supported gcc versions, if that goes anywhere above 4.5, gcc-common.h in linux can be further trimmed from my version). at the end of the day this is a policy call and i'm not the one to make it for linux ;).
Quoted 9 lines · On Fri, 16 Dec 2016 14:19:10 -0800 Kees Cook <[email protected]> wrote:
FWIW, it still seems to me that these aren't false positives:
WARNING:
vmlinux.o(.text.unlikely+0x1b1): Section mismatch in reference from the function uncore_pci_exit.part.22() to the function .init.text:uncore_free_pcibus_map()
The function uncore_pci_exit.part.22() references
the function__inituncore_free_pcibus_map().
This is often because uncore_pci_exit.part.22 lacks a__initannotation or the annotation ofuncore_free_pcibus_mapis wrong.
Hi,
I can't reproduce this section mismatch. Could you please send me your .config? Which gcc version did you use?
I used this tree: git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git initify-v4
Thanks
Signature · 1 line
Emese
Quoted 134 lines · Kees Cook
Hi,
This is a continuation of Emese Revfy's initify plugin upstreaming. This is based on her v3, but updated with various fixes from her github tree. Additionally, I split off the printf attribute fixes and sent those separately.
This is the initify gcc plugin. The kernel already has a mechanism to free up code and data memory that is only used during kernel or module initialization. This plugin will teach the compiler to find more such code and data that can be freed after initialization. It reduces memory usage. The initify gcc plugin can be useful for embedded systems.
Originally it was a CII project supported by the Linux Foundation.
This plugin is the part of grsecurity/PaX.
The plugin supports all gcc versions from 4.5 to 7.0.
Changes on top of the PaX version (since March 6.). These are the important ones:
* move all local strings to init.rodata.str and exit.rodata.str (not just__func__)
* report all initified strings and functions (GCC_PLUGIN_INITIFY_VERBOSEconfig option)
* automatically discover init/exit functions and apply the__initor__exitattributes on themYou can find more about the changes here:
https://github.com/ephox-gcc-plugins/initifyThis patch set is based on v4.9-rc2.
Some build statistics about the plugin:
On allyes config (amd64, gcc-6):
* 8412 initified strings
* 167 initified functionsOn allmod config (i386, gcc-6):
* 8597 initified strings
* 159 initified functionsOn allyes config (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 21746728 (0x14bd428) 21488680 (0x147e428) -258048
.init.data 1338376 (0x146c08) 1683016 (0x19ae48) +344640.text 78270904 (0x4aa51b8) 78228280 (0x4a9ab38) -42624 .init.text 1184725 (0x1213d5) 1223257 (0x12aa59) +38532 .exit.data 104 (0x000068) 17760 (0x004560) +17656.exit.text 174473 (
0x02a989) 175763 (0x02ae93) +1290FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 102936576 (0x622b000) 102678528 (0x61ec000) -258048
03 28680192 (0x1b5a000) 29081600 (0x1bbc000) +40140800 .text .notes
__ex_table.rodata__bug_table.pci_fixup .builtin_fw
.tracedata__ksymtab__ksymtab_gpl__ksymtab_strings__init_rodata__param__modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brkOn defconfig (amd64, gcc-6):
* 1957 initified strings
* 29 initified functionsOn defconfig (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 2524240 (0x268450) 2462800 (0x259450) -61440
.init.data 560256 (0x088c80) 644000 (0x09d3a0) +83744
.text 9377367 (0x8f1657) 9373079 (0x8f0597) -4288
.init.text 438586 (0x06b13a) 441828 (0x06bde4) +3242
.exit.data 0 832 (0x000340) +832
.exit.text 8857 (0x002299) 8857 (0x002299) 0FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 13398016 (0xcc7000) 13336576 (0xcb8000) -61440
03 2203648 (0x21a000) 2293760 (0x230000) +9011200 .text .notes
__ex_table.rodata__bug_table.pci_fixup .builtin_fw
.tracedata__ksymtab__ksymtab_gpl__ksymtab_strings__init_rodata__param__modver
03 .init.text .altinstr_aux .init.data .x86_cpu_dev.init
.parainstructions .altinstructions .altinstr_replacement .iommu_table .apicdrivers .exit.text .exit.data .smp_locks .bss .brkOne thing of note is that this plugin triggers false positive warnings from the modpost section mismatch detector. Further work is needed to deal with this.
Changed from v3:
* Refresh from Emese's latest version.Changed from v2:
* Check all uses when walking a use-def chain.
* Check all uses of initialized local variables and initify them if they have only nocapture uses. Previously only uses in call arguments determined whether the initializer value could be initified.
* Handle the format gcc attribute from the plugin too.
* Verify nocapture parameters of calls. Track uses of these parameters and verify that all uses are not captured. Verify only the nocapture attribute (The format attribute should be verified too.).
* Fixed wrong indexing of function arguments.
* Fixed decl comparison. When comparing two decls the tree codes must be the same.
* Search capture uses of the return value. Use negative nocapture attribute parameter on a function argument to verify that the return value is not captured.
* Stop the search for capture uses if there is a cast to integer type.
* Removed unnecessary duplication hook.
* Handle cloned functions with a changed argument list.
* Check visited tree nodes to avoid an infinite loop.
* Add a new initify plugin option:enable_init_to_exit_moves. Move a function to the exit section if it is called by__initand__exitfunctions too.
* Added plugin option to disable the search of capture uses in nocapture functions. We must be able to disable verification of nocapture functions because there is a lot of asm code in the str* and mem* functions on i386.
* Added some more nocapture attributes.
* Added some more printf attributes.
* Added someunverified_nocaptureattributes.
* Makeis_kernel_rodata()nocapture.
* Added comment for the nocapture attribute from Kees.
Quoted 13 lines
Changes from v1:
* Removed unnecessary nocapture attributes from boot code (Reported-by: PaX Team <[email protected]>)
* Removed nocapture attributes from functions that return the marked parameter
(Reported-by: Rasmus Villemoes <[email protected]>)
* Added nocapture attribute tostrlen()
* Updatedgcc-common.hfrom PaX
* Don't forcibly constify initified string types this caused the size reduction of the .data section (initify_plugin.c)
* Added the section mismatch problem in the commit message
I gave this a spin on arm64 and it seems to boot up and run okay with just the "select HAVE_GCC_PLUGIN_INITIFY_INIT_EXIT if GCC_PLUGINS" added for arm64. The patches could probably use more review but I think it would be good to just fold this in for arm64 for ease of testing.
Thanks,
Laura
Quoted 13 lines · Emese Revfy
Quoted 9 lines · On Fri, 16 Dec 2016 14:19:10 -0800 Kees Cook <[email protected]> wrote:
FWIW, it still seems to me that these aren't false positives:
WARNING:
vmlinux.o(.text.unlikely+0x1b1): Section mismatch in reference from the function uncore_pci_exit.part.22() to the function .init.text:uncore_free_pcibus_map()
The function uncore_pci_exit.part.22() references
the function__inituncore_free_pcibus_map().
This is often because uncore_pci_exit.part.22 lacks a__initannotation or the annotation ofuncore_free_pcibus_mapis wrong.
Quoted 3 lines
Hi,
I can't reproduce this section mismatch. Could you please send me your .config?
Attached.
Quoted 1 line
Which gcc version did you use?
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Quoted 1 line
I used this tree:
git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.gitinitify-v4
Yup, that's what I was building from.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 5 lines · Laura Abbott
I gave this a spin on arm64 and it seems to boot up and run okay with just the "select
HAVE_GCC_PLUGIN_INITIFY_INIT_EXITifGCC_PLUGINS" added for arm64. The patches could probably use more review but I think it would be good to just fold this in for arm64 for ease of testing.
Thanks! I've added this to arm64 for a v5 soon.
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 6 lines · On Tue, 3 Jan 2017 16:23:03 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 lines · Emese Revfy
Quoted 2 lines · On Fri, 16 Dec 2016 14:19:10 -0800 Kees Cook <[email protected]> wrote:
FWIW, it still seems to me that these aren't false positives:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
WARNING:
vmlinux.o(.text.unlikely+0x1b1): Section mismatch in reference from the function uncore_pci_exit.part.22() to the function .init.text:uncore_free_pcibus_map()
The function uncore_pci_exit.part.22() references
the function__inituncore_free_pcibus_map().
This is often because uncore_pci_exit.part.22 lacks a__initannotation or the annotation ofuncore_free_pcibus_mapis wrong.
Thanks for the report, you can find the fix here:
https://github.com/ephox-gcc-plugins/initify/commit/5d93a54badd9751b79ef67ce8a8b4081fea37547
Signature · 1 line
Emese
Quoted 17 lines · Emese Revfy
Quoted 6 lines · On Tue, 3 Jan 2017 16:23:03 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 lines · Emese Revfy
Quoted 2 lines · On Fri, 16 Dec 2016 14:19:10 -0800 Kees Cook <[email protected]> wrote:
FWIW, it still seems to me that these aren't false positives:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
WARNING:
vmlinux.o(.text.unlikely+0x1b1): Section mismatch in reference from the function uncore_pci_exit.part.22() to the function .init.text:uncore_free_pcibus_map()
The function uncore_pci_exit.part.22() references
the function__inituncore_free_pcibus_map().
This is often because uncore_pci_exit.part.22 lacks a__initannotation or the annotation ofuncore_free_pcibus_mapis wrong.
Quoted 2 lines
Thanks for the report, you can find the fix here:
https://github.com/ephox-gcc-plugins/initify/commit/5d93a54badd9751b79ef67ce8a8b4081fea37547
Awesome! Thanks, that eliminated almost all of the warnings. I'm still seeing this:
WARNING: vmlinux.o(.text+0x1087e7): Section mismatch in reference from the function rebind_subsystems() to the variable .init.rodata.str:__func__.4400
The function rebind_subsystems() references
the variable __initconst __func__.4400.
This is often because rebind_subsystems lacks a __initconst annotation or the annotation of __func__.4400 is wrong.
WARNING: vmlinux.o(.text+0x13789d): Section mismatch in reference from the function register_ftrace_export() to the variable .init.rodata.str:__func__.4251
The function register_ftrace_export() references
the variable __initconst __func__.4251.
This is often because register_ftrace_export lacks a __initconst annotation or the annotation of __func__.4251 is wrong.
WARNING: vmlinux.o(.text+0x139d24): Section mismatch in reference from the function tracepoint_printk_sysctl() to the variable .init.rodata.str:__func__.4251
The function tracepoint_printk_sysctl() references
the variable __initconst __func__.4251.
This is often because tracepoint_printk_sysctl lacks a __initconst annotation or the annotation of __func__.4251 is wrong.
I haven't taken a close look yet, but I assume these are from the const strings in these functions?
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 7 lines · On Tue, 10 Jan 2017 17:09:31 -0800 Kees Cook <[email protected]> wrote:
WARNING:
vmlinux.o(.text+0x1087e7): Section mismatch in reference from the functionrebind_subsystems()to the variable .init.rodata.str:__func__.4400
The functionrebind_subsystems()references
the variable__initconst__func__.4400.
This is often becauserebind_subsystemslacks a__initconstannotation or the annotation of __func__.4400 is wrong.
Thanks for the report, you can find the fix here:
https://github.com/ephox-gcc-plugins/initify/commit/25f34834e3373e067133bc5d39d42c50a3592d56
Signature · 1 line
Emese
Quoted 11 lines · Emese Revfy
Quoted 7 lines · On Tue, 10 Jan 2017 17:09:31 -0800 Kees Cook <[email protected]> wrote:
WARNING:
vmlinux.o(.text+0x1087e7): Section mismatch in reference from the functionrebind_subsystems()to the variable .init.rodata.str:__func__.4400
The functionrebind_subsystems()references
the variable__initconst__func__.4400.
This is often becauserebind_subsystemslacks a__initconstannotation or the annotation of __func__.4400 is wrong.
Quoted 2 lines
Thanks for the report, you can find the fix here:
https://github.com/ephox-gcc-plugins/initify/commit/25f34834e3373e067133bc5d39d42c50a3592d56
Awesome! I can confirm, it builds without warnings now. Thanks!
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 12 lines · Kees Cook
Quoted 11 lines · Emese Revfy
Quoted 7 lines · On Tue, 10 Jan 2017 17:09:31 -0800 Kees Cook <[email protected]> wrote:
WARNING:
vmlinux.o(.text+0x1087e7): Section mismatch in reference from the functionrebind_subsystems()to the variable .init.rodata.str:__func__.4400
The functionrebind_subsystems()references
the variable__initconst__func__.4400.
This is often becauserebind_subsystemslacks a__initconstannotation or the annotation of __func__.4400 is wrong.
Quoted 4 lines
Quoted 2 lines
Thanks for the report, you can find the fix here:
https://github.com/ephox-gcc-plugins/initify/commit/25f34834e3373e067133bc5d39d42c50a3592d56Awesome! I can confirm, it builds without warnings now. Thanks!
Hm, actually, with an "allyesconfig" build, I'm still seeing warnings (and possibly some nocapture verification failures).
Most look like this:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function clk_gate() to the variable .init.rodata.str:__func__.29708
The function clk_gate() references
the variable __initconst __func__.29708.
This is often because clk_gate lacks a __initconst annotation or the annotation of __func__.29708 is wrong.
And there's this (should KASAN be disabled for initify?)
mm/kasan/kasan.c: In function ‘memmove’:mm/kasan/kasan.c:346:7: warning: ‘memmove’ captures its 2 (‘src’) parameter, please remove it from the nocapture attribute.
void *memmove(void *dest, const void *src, size_t len)
^
mm/kasan/kasan.c: In function ‘memcpy’:mm/kasan/kasan.c:355:7: warning: ‘memcpy’ captures its 2 (‘src’) parameter, please remove it from the nocapture attribute.
void *memcpy(void *dest, const void *src, size_t len)
^
And ACPI:
drivers/acpi/acpica/utdebug.c: In function ‘acpi_debug_print’:drivers/acpi/acpica/utdebug.c:158:1: warning: ‘acpi_debug_print’ captures its 3 (‘function_name’) parameter, please remove it from the nocapture attribute.
acpi_debug_print(u32 requested_debug_level,
^
I used my initify v5 development tree, with the following patch, with "make allyesconfig":
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=for-next/gcc-plugin/initify
arch/Kconfig +0 -1
@@ -359,7 +359,6 @@ config HAVE_GCC_PLUGINS menuconfig GCC_PLUGINS bool "GCC plugins" depends on HAVE_GCC_PLUGINS - depends on !COMPILE_TEST help GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and
static analysis.
@@ -429,6 +428,7 @@ config GCC_PLUGIN_INITIFY
config GCC_PLUGIN_INITIFY_VERBOSE
bool "Report initification"
depends on GCC_PLUGIN_INITIFY
+ depends on !COMPILE_TEST
help
Print all initified strings and all functions which should be __init/__exit.
I'll see if acpi needs __noverified_nocapture ...
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 8 lines · On Thu, 12 Jan 2017 15:40:41 -0800 Kees Cook <[email protected]> wrote:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function
clk_gate()to the variable .init.rodata.str:__func__.29708
The functionclk_gate()references
the variable__initconst__func__.29708.
This is often becauseclk_gatelacks a__initconstannotation or the annotation of __func__.29708 is wrong.
Quoted 4 lines
I used my initify v5 development tree, with the following patch, with "make allyesconfig":
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=for-next/gcc-plugin/initify
Hi,
I can't reproduce this mismatch with allyesconfig. Could you please send me your .config?
Signature · 1 line
Emese
Quoted 11 lines · Emese Revfy
Quoted 8 lines · On Thu, 12 Jan 2017 15:40:41 -0800 Kees Cook <[email protected]> wrote:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function
clk_gate()to the variable .init.rodata.str:__func__.29708
The functionclk_gate()references
the variable__initconst__func__.29708.
This is often becauseclk_gatelacks a__initconstannotation or the annotation of __func__.29708 is wrong.
Quoted 8 lines
Quoted 4 lines
I used my initify v5 development tree, with the following patch, with "make allyesconfig":
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=for-next/gcc-plugin/initify
Hi,
I can't reproduce this mismatch with allyesconfig. Could you please send me your .config?
Sure, attached. (Though note this requires the patch from my earlier email to allow allyesconfig to work with the plugins.)
-Kees
Signature · 2 lines
Kees Cook Nexus Security
Quoted 7 lines · On Thu, 12 Jan 2017 15:40:41 -0800 Kees Cook <[email protected]> wrote:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function
clk_gate()to the variable .init.rodata.str:__func__.29708
The functionclk_gate()references
the variable__initconst__func__.29708.
This is often becauseclk_gatelacks a__initconstannotation or the annotation of __func__.29708 is wrong.
Thanks for the report, it will be fixed in the next grsec patch.
Signature · 1 line
Emese
Quoted 11 lines · Emese Revfy
Quoted 7 lines · On Thu, 12 Jan 2017 15:40:41 -0800 Kees Cook <[email protected]> wrote:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function
clk_gate()to the variable .init.rodata.str:__func__.29708
The functionclk_gate()references
the variable__initconst__func__.29708.
This is often becauseclk_gatelacks a__initconstannotation or the annotation of __func__.29708 is wrong.
Quoted 1 line
Thanks for the report, it will be fixed in the next grsec patch.
Very cool, thanks! What did the problem turn out to be?
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 12 lines · On Wed, 15 Feb 2017 11:27:06 -0800 Kees Cook <[email protected]> wrote:
Quoted 11 lines · Emese Revfy
Quoted 7 lines · On Thu, 12 Jan 2017 15:40:41 -0800 Kees Cook <[email protected]> wrote:
WARNING: drivers/clk/bcm/built-in.o(.text+0xec2): Section mismatch in reference from the function
clk_gate()to the variable .init.rodata.str:__func__.29708
The functionclk_gate()references
the variable__initconst__func__.29708.
This is often becauseclk_gatelacks a__initconstannotation or the annotation of __func__.29708 is wrong.
Quoted 3 lines
Quoted 1 line
Thanks for the report, it will be fixed in the next grsec patch.
Very cool, thanks! What did the problem turn out to be?
You can find it here:
https://github.com/ephox-gcc-plugins/initify/commit/73e857aa47bde7cc78ed865a008d1519893c0f54
Signature · 1 line
Emese