Introduce the initify gcc plugin
Subsystems (12)
v2 cover Introduce the initify gcc plugin
I would like to introduce 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.
It is 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 6.0.
I made some 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 the "Add support for complex gcc plugins that don't fit in a single file" patch set
(https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next.git#initify
HEAD: e08eda5d85f7f52641640).
Some statistics about the plugin:
On allyes config (amd64, gcc-6):
* 7765 initified strings
* 231 initified functions
On allmod config (i386, gcc-6):
* 9177 initified strings
* 288 initified functions
On allyes config (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 19054824 (0x122c0e8) 18841832 (0x11f80e8) -212992
.init.data 1273768 (0x136fa8) 1583496 (0x182988) +309728
.text 46126301 (0x2bfd4dd) 46099805 (0x2bf6d5d) -26496
.init.text 836339 (0x0cc2f3) 855952 (0x0d0f90) +19613 .exit.data 104 (0x000068) 16736 (0x004160) +16632 .exit.text 125511 (0x01ea47) 133267 (0x020893) +7756
FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 67727360 (0x4097000) 65417216 (0x3e63000) -2310144
03 2453504 (0x257000) 2809856 (0x2ae000) +356352
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):
* 2044 initified strings
* 43 initified functions
On defconfig (amd64, gcc-6):
section vanilla vanilla + initify change
-----------------------------------------------------------------------
.rodata 2466736 (0x25a3b0) 2405296 (0x24b3b0) -61440
.init.data 564656 (0x089db0) 645616 (0x09d9f0) +80960
.text 9364727 (0x8ee4f7) 9360823 (0x8ed5b7) -3904
.init.text 436097 (0x06a781) 439161 (0x06b379) +3064
.exit.data 0 864 (0x000360) +864
.exit.text 8806 (0x002266) 9824 (0x002660) +1018
FileSiz (vanilla) FileSiz (vanilla + initify) change
------------------------------------------------------------------------
00 13340672 (0xcb9000) 13279232 (0xcaa000) -61440
03 1097728 (0x10c000) 1183744 (0x121000) +86016
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
Emese Revfy (3):
Add the initify gcc plugin
Mark functions with the __nocapture attribute
Constify some function parameters
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
--- arch/Kconfig | 23 + arch/arm/include/asm/string.h | 4 +- arch/arm64/include/asm/string.h | 19 +- arch/powerpc/include/asm/string.h | 19 +- arch/x86/include/asm/string_32.h | 21 +- arch/x86/include/asm/string_64.h | 20 +- arch/x86/kernel/hpet.c | 2 +- drivers/isdn/hisax/config.c | 4 +- drivers/isdn/hisax/hisax.h | 4 +- include/asm-generic/bug.h | 6 +- include/asm-generic/vmlinux.lds.h | 2 + include/linux/compiler-gcc.h | 10 +- include/linux/compiler.h | 4 + include/linux/fs.h | 5 +- include/linux/printk.h | 2 +- include/linux/string.h | 63 +- scripts/Makefile.gcc-plugins | 4 + scripts/gcc-plugins/gcc-common.h | 41 ++ scripts/gcc-plugins/initify_plugin.c | 1183 ++++++++++++++++++++++++++++++++++ 19 files changed, 1353 insertions(+), 83 deletions(-)
PATCH v2 1/3 Add 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 has 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.
It reduces memory usage. 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 | 23 + include/asm-generic/vmlinux.lds.h | 2 + scripts/Makefile.gcc-plugins | 4 + scripts/gcc-plugins/gcc-common.h | 41 ++ scripts/gcc-plugins/initify_plugin.c | 1183 ++++++++++++++++++++++++++++++++++ 5 files changed, 1253 insertions(+) create mode 100644 scripts/gcc-plugins/initify_plugin.c
arch/Kconfig +23 -0
@@ -417,6 +417,29 @@ 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 is the part of grsecurity/PaX. More information at: + * https://grsecurity.net/ + * https://pax.grsecurity.net/ + +config GCC_PLUGIN_INITIFY_VERBOSE + bool "Verbose" + 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
include/asm-generic/vmlinux.lds.h +2 -0
@@ -531,6 +531,7 @@ MEM_DISCARD(init.data) \ KERNEL_CTORS() \MCOUNT_REC () \ + *(.init.rodata.str) \ *(.init.rodata) \ FTRACE_EVENTS() \TRACE_SYSCALLS () \
@@ -555,6 +556,7 @@#define EXIT_DATA \ *(.exit.data) \ MEM_DISCARD(exit.data) \ + *(.exit.rodata.str) \ MEM_DISCARD(exit.rodata)#define EXIT_TEXT \
scripts/Makefile.gcc-plugins +4 -0
@@ -12,6 +12,10 @@ 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 + ifdef CONFIG_GCC_PLUGIN_SANCOV ifeq ($(CFLAGS_KCOV),) # It is needed because of the gcc-plugin.sh and gcc version checks.
scripts/gcc-plugins/gcc-common.h +41 -0
65 lines not on this page · raw message
scripts/gcc-plugins/initify_plugin.c new file +1183 -0
1,183 lines not on this page · raw message
Signature · 1 line
2.8.1
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 11 lines · Emese Revfy
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 has 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.
It reduces memory usage. This plugin can be useful for embedded systems.
Quoted 3 lines
If a function is called by
__initand__exitfunctions as well then the plugin moves it to the__exitsection. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.
Should the mismatch checker be updated to recognize this case? Without the plugin, I assume these kinds of functions would only ever be marked for __exit? If so, should the plugin strip the __init marking and only add __exit?
Quoted 4 lines
The instrumentation pass of the
latent_entropyplugin must run after the initify plugin to increase coverage.Signed-off-by Emese Revfy <[email protected]>
Thanks for sending this! I'll get it added to my tree for some 0day build testing, and then get it into my -next tree.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 3 lines · On Tue, 12 Jul 2016 15:45:56 -0400 Kees Cook <[email protected]> wrote:
On Mon, Jul 4, 2016 at 7:40 PM, Emese Revfy <[email protected]> wrote:
Thanks for sending this! I'll get it added to my tree for some 0day build testing, and then get it into my -next tree.
I'll send a new patch set soon. I'm working on some fixes and
I would like to implement the handling of return uses to increase the coverage (which decreases because of the bug fixes :) ).
So please hold off with testing until the next patch.
Signature · 1 line
Emese
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 11 lines · Emese Revfy
Quoted 3 lines · On Tue, 12 Jul 2016 15:45:56 -0400 Kees Cook <[email protected]> wrote:
On Mon, Jul 4, 2016 at 7:40 PM, Emese Revfy <[email protected]> wrote:
Thanks for sending this! I'll get it added to my tree for some 0day build testing, and then get it into my -next tree.I'll send a new patch set soon. I'm working on some fixes and
I would like to implement the handling of return uses to increase the coverage (which decreases because of the bug fixes :) ).
So please hold off with testing until the next patch.
Okay, cool. I'll just do some simple tests with what I have so I can feel like I understand its intended use, etc. :)
One change I made was to add this comment in compiler-gcc.h for people that become curious about __nocapture and go looking for its definition (please feel free to adjust for accuracy, etc):
/*
* The initify gcc-plugin attempts to identify const arguments that are only
* used during init (see __init), so they can be moved to the .init.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 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
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 11 lines · On Tue, 12 Jul 2016 16:05:45 -0400 Kees Cook <[email protected]> wrote:
/*
* The initify gcc-plugin attempts to identify const arguments that are only
* used during init (see__init), so they can be moved to the .init.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 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.
*/
#ifdefINITIFY_PLUGIN
Quoted 2 lines
# define __nocapture(...) __attribute__((nocapture(__VA_ARGS__))) #endif
Thanks, I'll take it in the next patch set with some additions (the attribute also handles __exit functions and the plugin does other things e.g., it can identify candidate init/exit functions and move them automatically to init.text/exit.text).
Signature · 1 line
Emese
Russell King - ARM Linux
maintainerRe: [PATCH v2 1/3] Add the initify gcc plugin
In reply to Kees CookQuoted 9 lines · Kees Cook
Quoted 3 lines · Emese Revfy
If a function is called by
__initand__exitfunctions as well then the plugin moves it to the__exitsection. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.Should the mismatch checker be updated to recognize this case? Without the plugin, I assume these kinds of functions would only ever be marked for
__exit? If so, should the plugin strip the__initmarking and only add__exit?
That sounds like a problem for architectures that still discard the __exit section at link time to reduce the size of the linked kernel image - though, obviously, if using the plugin results in a smaller kernel image _with_ the exit sections, then there's a net benefit size-wise.
Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up according to speedtest.net.
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 11 lines · Russell King - ARM Linux
Quoted 9 lines · Kees Cook
Quoted 3 lines · Emese Revfy
If a function is called by
__initand__exitfunctions as well then the plugin moves it to the__exitsection. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.Should the mismatch checker be updated to recognize this case? Without the plugin, I assume these kinds of functions would only ever be marked for
__exit? If so, should the plugin strip the__initmarking and only add__exit?
Quoted 5 lines
That sounds like a problem for architectures that still discard the
__exitsection at link time to reduce the size of the linked kernel image - though, obviously, if using the plugin results in a smaller kernel image _with_ the exit sections, then there's a net benefit size-wise.
Ah right, __exit is dropped for non-modular builds. So, for "both __init and __exit" it sounds like the behavior depends on the build:
- if modular: remove __init marking (since we need it after init)
- if non-modular: remove __exit marking (since we'll never call exit)
Is this something the build itself (rather than the plugin) could notice and fix up? Hmmm
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 10 lines · On Tue, 12 Jul 2016 18:38:47 -0400 Kees Cook <[email protected]> wrote:
Quoted 7 lines · Russell King - ARM Linux
Quoted 1 line · Kees Cook
On Mon, Jul 4, 2016 at 7:40 PM, Emese Revfy <[email protected]> wrote:
That sounds like a problem for architectures that still discard the
__exitsection at link time to reduce the size of the linked kernel image - though, obviously, if using the plugin results in a smaller kernel image _with_ the exit sections, then there's a net benefit size-wise.
Quoted 5 lines
Ah right,
__exitis dropped for non-modular builds. So, for "both__initand__exit" it sounds like the behavior depends on the build:- if modular: remove
__initmarking (since we need it after init)
- if non-modular: remove__exitmarking (since we'll never call exit)
When gcc compiles vmlinux these functions should be in __init and when it compiles *.ko then they can be in __exit. I have no time to do this now but I added it to my todo list.
The temporary fix can be that I enable this section move only on x86 (on other archs it will decrase the coverage).
Signature · 1 line
Emese
Re: [PATCH v2 1/3] Add the initify gcc plugin
In reply to Kees CookQuoted 9 lines · On Tue, 12 Jul 2016 15:45:56 -0400 Kees Cook <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
If a function is called by
__initand__exitfunctions as well then the plugin moves it to the__exitsection. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.Should the mismatch checker be updated to recognize this case? Without the plugin, I assume these kinds of functions would only ever be marked for
__exit? If so, should the plugin strip the__initmarking and only add__exit?
I don't modify the existing attributes. I just add a new __init/__exit when a function hasn't a section attribute yet.
There are three cases:
* when the function is called only by __init functions then the plugin adds the __init attribute
* when the function is called only by __exit functions then the plugin adds the __exit attribute
* when the function is called by __init and __exit functions too then the plugin adds the __exit attribute.
The last case causes the false positive(?) message of the section mismatch.
Signature · 1 line
Emese
Re: [PATCH v2 1/3] Add the initify gcc plugin
Quoted 13 lines · Emese Revfy
Quoted 9 lines · On Tue, 12 Jul 2016 15:45:56 -0400 Kees Cook <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
If a function is called by
__initand__exitfunctions as well then the plugin moves it to the__exitsection. This causes false positive section mismatch errors/warnings that I don't know how to handle yet.Should the mismatch checker be updated to recognize this case? Without the plugin, I assume these kinds of functions would only ever be marked for
__exit? If so, should the plugin strip the__initmarking and only add__exit?
Quoted 10 lines
I don't modify the existing attributes. I just add a new __init/__exit when a function hasn't a section attribute yet.
There are three cases:
* when the function is called only by__initfunctions then the plugin adds the__initattribute
* when the function is called only by__exitfunctions then the plugin adds the__exitattribute
* when the function is called by__initand__exitfunctions too then the plugin adds the__exitattribute.
The last case causes the false positive(?) message of the section mismatch.
In the latter case, how does the linker actually choose where to put such a function?
For a modular build, if it puts it in .init, it will be missing during exit. If it puts it in .exit, that seems correct.
For a non-modular build, if it puts it in .init, this is correct. If it puts it in .exit, it may be missing for init because the exit section may have already been removed at final link time.
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v2 2/3 Mark functions with the __nocapture attribute
In reply to Emese RevfyThe nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify. If the marked argument 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 | 4 +-- arch/arm64/include/asm/string.h | 19 ++++++------ arch/powerpc/include/asm/string.h | 19 ++++++------ arch/x86/include/asm/string_32.h | 21 ++++++------- arch/x86/include/asm/string_64.h | 20 ++++++------- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 ++-- include/linux/compiler-gcc.h | 10 +++++-- include/linux/compiler.h | 4 +++ include/linux/fs.h | 5 ++-- include/linux/printk.h | 2 +- include/linux/string.h | 63 ++++++++++++++++++++------------------- 12 files changed, 96 insertions(+), 79 deletions(-)
arch/arm/include/asm/string.h +2 -2
@@ -13,10 +13,10 @@ extern char * strrchr(const char * s, int c);extern char *strchr (const char * s,int c);#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_MEMCHRextern void *memchr (const void *,int ,__kernel_size_t );
arch/arm64/include/asm/string.h +10 -9
@@ -23,24 +23,25 @@ extern char *strrchr(const char *, int c);extern char *strchr (const char *,int c);#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *,const char *); +extern int strcmp(const char *, const char *) __nocapture(1, 2);#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_MEMCHRextern void *memchr (const void *,int ,__kernel_size_t );
@@ -50,7 +51,7 @@ extern 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(1, 2); +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 +11 -10
@@ -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(1, 2);#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_STRCHRextern char *strchr (const char *s,int c);#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,7 +198,7 @@ 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
@@ -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 (2 );/* * This looks horribly ugly, but the compiler can optimize it totally,
arch/x86/include/asm/string_64.h +10 -10
@@ -27,8 +27,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
@@ -56,14 +56,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__)
@@ -89,7 +89,7 @@ int strcmp(const char *cs, const char *ct);* * Return 0 for success, -EFAULT for fail */ -int memcpy_mcsafe (void *dst,const void *src,size_t cnt); +int memcpy_mcsafe(void *dst, const void *src, size_t cnt) __nocapture(2);#endif /* __KERNEL__ */
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);
include/asm-generic/bug.h +3 -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)
include/linux/compiler-gcc.h +8 -2
@@ -116,8 +116,10 @@*/ #define __pure __attribute__((pure))#define __aligned (x) __attribute__((aligned(x))) -#define __printf (a, b) __attribute__((format(printf, a, b))) -#define __scanf (a, b) __attribute__((format(scanf, a, b))) +#define __printf (a, b) __attribute__((format(printf, a, b))) \ + __nocapture(a, b) +#define __scanf (a, b) __attribute__((format(scanf, a, b))) \ + __nocapture(a, b)#define __attribute_const__ __attribute__((__const__))#define __maybe_unused __attribute__((unused))#define __always_unused __attribute__((unused))
@@ -193,6 +195,10 @@# define __latent_entropy __attribute__((latent_entropy))#endif +#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
include/linux/compiler.h +4 -0
@@ -412,6 +412,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.
include/linux/fs.h +3 -2
@@ -2476,8 +2476,9 @@ 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
@@ -163,7 +163,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/string.h +33 -30
138 lines not on this page · raw message
Signature · 1 line
2.8.1
Re: [PATCH v2 2/3] Mark functions with the __nocapture attribute
Quoted 5 lines · Emese Revfy
The nocapture gcc attribute can be on functions only.
The attribute takes one or more unsigned integer constants as parameters that specify the function argument(s) of const char* type to initify. If the marked argument is a vararg then the plugin initifies all vararg arguments.
Why is this called "nocapture"? Not captured by what? It seems like it means "initify this if possible". Am I misunderstanding its purpose?
-Kees
Quoted 1 line
I couldn't test the arm, arm64 and powerpc architectures.
Has anyone had a chance to try this out on these architectures? I'll be testing arm shortly...
-Kees
Quoted 477 lines
Signed-off-by Emese Revfy <[email protected]>--- arch/arm/include/asm/string.h | 4 +-- arch/arm64/include/asm/string.h | 19 ++++++------ arch/powerpc/include/asm/string.h | 19 ++++++------ arch/x86/include/asm/string_32.h | 21 ++++++------- arch/x86/include/asm/string_64.h | 20 ++++++------- arch/x86/kernel/hpet.c | 2 +- include/asm-generic/bug.h | 6 ++-- include/linux/compiler-gcc.h | 10 +++++-- include/linux/compiler.h | 4 +++ include/linux/fs.h | 5 ++-- include/linux/printk.h | 2 +- include/linux/string.h | 63 ++++++++++++++++++++------------------- 12 files changed, 96 insertions(+), 79 deletions(-)arch/arm/include/asm/string.h +2
-2@@ -13,10 +13,10 @@ extern char * strrchr(const char * s, int c);extern char *strchr (const char * s,int c);#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_MEMCHRextern void *memchr (const void *,int ,__kernel_size_t );arch/arm64/include/asm/string.h +10
-9@@ -23,24 +23,25 @@ extern char *strrchr(const char *, int c);extern char *strchr (const char *,int c);#define __HAVE_ARCH_STRCMP -extern int strcmp (const char *,const char *); +extern int strcmp(const char *, const char *) __nocapture(1, 2);#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_MEMCHRextern void *memchr (const void *,int ,__kernel_size_t );@@ -50,7 +51,7 @@ extern 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(1, 2); +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 +11
-10@@ -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(1, 2);#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_STRCHRextern char *strchr (const char *s,int c);#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,7 +198,7 @@ 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@@ -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 (2 );/* * This looks horribly ugly, but the compiler can optimize it totally, arch/x86/include/asm/string_64.h +10
-10@@ -27,8 +27,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 @@ -56,14 +56,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__)@@ -89,7 +89,7 @@ int strcmp(const char *cs, const char *ct);* * Return 0 for success, -EFAULT for fail */ -int memcpy_mcsafe (void *dst,const void *src,size_t cnt); +int memcpy_mcsafe(void *dst, const void *src, size_t cnt) __nocapture(2);#endif /* __KERNEL__ */ 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);include/asm-generic/bug.h +3
-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)include/linux/compiler-gcc.h +8
-2@@ -116,8 +116,10 @@*/ #define __pure __attribute__((pure))#define __aligned (x) __attribute__((aligned(x))) -#define __printf (a, b) __attribute__((format(printf, a, b))) -#define __scanf (a, b) __attribute__((format(scanf, a, b))) +#define __printf (a, b) __attribute__((format(printf, a, b))) \ + __nocapture(a, b) +#define __scanf (a, b) __attribute__((format(scanf, a, b))) \ + __nocapture(a, b)#define __attribute_const__ __attribute__((__const__))#define __maybe_unused __attribute__((unused))#define __always_unused __attribute__((unused))@@ -193,6 +195,10 @@# define __latent_entropy __attribute__((latent_entropy))#endif +#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 include/linux/compiler.h +4
-0@@ -412,6 +412,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. include/linux/fs.h +3
-2@@ -2476,8 +2476,9 @@ 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@@ -163,7 +163,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/string.h +33
-31@@ -18,37 +18,38 @@ extern void *memdup_user_nul(const void __user *, size_t);#include <asm/string.h> #ifndef __HAVE_ARCH_STRCPY -extern char *strcpy (char *,const char *); +extern char *strcpy (char *,const char *)__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRNCPY -extern char *strncpy (char *,const char *,__kernel_size_t ); +extern char *strncpy (char *,const char *,__kernel_size_t )__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRLCPY -size_t strlcpy (char *,const char *,size_t ); +size_t strlcpy(char *, const char *, size_t) __nocapture(2);#endif #ifndef __HAVE_ARCH_STRSCPY -ssize_t __must_check strscpy (char *,const char *,size_t ); +ssize_t __must_check strscpy(char *, const char *, size_t) __nocapture(2);#endif #ifndef __HAVE_ARCH_STRCAT -extern char *strcat (char *,const char *); +extern char *strcat (char *,const char *)__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRNCAT -extern char *strncat (char *,const char *,__kernel_size_t ); +extern char *strncat (char *,const char *,__kernel_size_t )__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRLCAT -extern size_t strlcat (char *,const char *,__kernel_size_t ); +extern size_t strlcat(char *, const char *, __kernel_size_t) __nocapture(2);#endif #ifndef __HAVE_ARCH_STRCMP -extern int strcmp (const char *,const char *); +extern int strcmp(const char *,const char *) __nocapture(1, 2);#endif #ifndef __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);#endif #ifndef __HAVE_ARCH_STRCASECMP -extern int strcasecmp (const char *s1,const char *s2); +extern int strcasecmp(const char *s1, const char *s2) __nocapture(1, 2);#endif #ifndef __HAVE_ARCH_STRNCASECMP -extern int strncasecmp (const char *s1,const char *s2,size_t n); +extern int +strncasecmp(const char *s1, const char *s2, size_t n) __nocapture(1, 2);#endif #ifndef __HAVE_ARCH_STRCHRextern char *strchr (const char *,int );@@ -72,44 +73,44 @@ static inline __must_check char *strstrip(char *str) }#ifndef __HAVE_ARCH_STRSTR -extern char *strstr (const char *,const char *); +extern char *strstr (const char *,const char *)__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRNSTR -extern char *strnstr (const char *,const char *,size_t ); +extern char *strnstr (const char *,const char *,size_t )__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRLEN -extern __kernel_size_t strlen (const char *); +extern __kernel_size_t strlen(const char *) __nocapture(1);#endif #ifndef __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);#endif #ifndef __HAVE_ARCH_STRPBRK -extern char *strpbrk (const char *,const char *); +extern char *strpbrk (const char *,const char *)__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRSEP -extern char *strsep (char **,const char *); +extern char *strsep (char **,const char *)__nocapture (2 );#endif #ifndef __HAVE_ARCH_STRSPN -extern __kernel_size_t strspn (const char *,const char *); +extern __kernel_size_t strspn(const char *,const char *) __nocapture(1, 2);#endif #ifndef __HAVE_ARCH_STRCSPN -extern __kernel_size_t strcspn (const char *,const char *); +extern __kernel_size_t strcspn(const char *,const char *) __nocapture(1, 2);#endif #ifndef __HAVE_ARCH_MEMSETextern void *memset (void *,int ,__kernel_size_t );#endif #ifndef __HAVE_ARCH_MEMCPY -extern void *memcpy (void *,const void *,__kernel_size_t ); +extern void *memcpy (void *,const void *,__kernel_size_t )__nocapture (2 );#endif #ifndef __HAVE_ARCH_MEMMOVE -extern void *memmove (void *,const void *,__kernel_size_t ); +extern void *memmove (void *,const void *,__kernel_size_t )__nocapture (2 );#endif #ifndef __HAVE_ARCH_MEMSCANextern void *memscan (void *,int ,__kernel_size_t );#endif #ifndef __HAVE_ARCH_MEMCMP -extern int memcmp (const void *,const void *,__kernel_size_t ); +extern int memcmp(const void *,const void *,__kernel_size_t) __nocapture(1, 2);#endif #ifndef __HAVE_ARCH_MEMCHRextern void *memchr (const void *,int ,__kernel_size_t );@@ -119,16 +120,16 @@ char *strreplace(char *s, char old, char new);extern void kfree_const (const void *x); -extern char *kstrdup (const char *s,gfp_t gfp) __malloc; -extern const char *kstrdup_const (const char *s,gfp_t gfp); -extern char *kstrndup (const char *s,size_t len,gfp_t gfp); -extern void *kmemdup (const void *src,size_t len,gfp_t gfp); +extern char *kstrdup (const char *s,gfp_t gfp) __malloc__nocapture (1 ); +extern const char *kstrdup_const (const char *s,gfp_t gfp)__nocapture (1 ); +extern char *kstrndup (const char *s,size_t len,gfp_t gfp)__nocapture (1 ); +extern void *kmemdup (const void *src,size_t len,gfp_t gfp)__nocapture (1 );extern char **argv_split (gfp_t gfp,const char *str,int *argcp);extern void argv_free (char **argv); -extern bool sysfs_streq (const char *s1,const char *s2); -extern int kstrtobool (const char *s,bool *res); +extern bool sysfs_streq(const char *s1, const char *s2) __nocapture(1, 2); +extern int kstrtobool(const char *s, bool *res) __nocapture(1);static inline int strtobool (const char *s,bool *res) {return kstrtobool (s, res);@@ -137,8 +138,10 @@ static inline int strtobool(const char *s, bool *res)int match_string (const char *const *array,size_t n,const char *string);#ifdef CONFIG_BINARY_PRINTF -int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args); -int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf); +int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, + va_list args) __nocapture(3); +int bstr_printf(char *buf, size_t size, const char *fmt, + const u32 *bin_buf) __nocapture(3); int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);#endif --2.8.1
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [kernel-hardening] Re: [PATCH v2 2/3] Mark functions with the __nocapture attribute
Quoted 11 lines · Kees Cook
Quoted 8 lines · Emese Revfy
The nocapture gcc attribute can be on functions only. The attribute takes one or more unsigned integer constants as parameters
that specify the function argument(s) of const char* type to initify.
If the marked argument is a vararg then the plugin initifies all vararg arguments.
Quoted 3 lines
Why is this called "nocapture"? Not captured by what? It seems like it
means "initify this if possible". Am I misunderstanding its purpose?
It means they don't escape via that function, i.e. they aren't stored anywhere to be used in any way after the call.
Re: [kernel-hardening] Re: [PATCH v2 2/3] Mark functions with the __nocapture attribute
Quoted 12 lines · Daniel Micay
Quoted 11 lines · Kees Cook
Quoted 8 lines · Emese Revfy
The nocapture gcc attribute can be on functions only. The attribute takes one or more unsigned integer constants as parameters
that specify the function argument(s) of const char* type to initify.
If the marked argument is a vararg then the plugin initifies all vararg arguments.
Quoted 6 lines
Quoted 3 lines
Why is this called "nocapture"? Not captured by what? It seems like it
means "initify this if possible". Am I misunderstanding its purpose?It means they don't escape via that function, i.e. they aren't stored anywhere to be used in any way after the call.
Ah, those arguments are confirmed to not be "captured" by that function, leaving them available to be moved to .init. Got it, thanks!
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v2 3/3 Constify some function parameters
In reply to Emese RevfyInitify needs const pointer types, the initify plugin caught some __printf arguments that weren't const yet.
--- drivers/isdn/hisax/config.c | 4 ++-- drivers/isdn/hisax/hisax.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/isdn/hisax/config.c +2 -2
@@ -659,7 +659,7 @@ int jiftime(char *s, long mark)static u_char tmpbuf[HISAX_STATUS_BUFSIZE ]; -void VHiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, +void VHiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt,va_list args) {/* if head == NULL the fmt contains the full info */
@@ -729,7 +729,7 @@ void VHiSax_putstatus(struct IsdnCardState *cs, char *head, char *fmt, } } -void HiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, ...) +void HiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt, ...) {va_list args;
drivers/isdn/hisax/hisax.h +2 -2
@@ -1288,9 +1288,9 @@ int jiftime(char *s, long mark);int HiSax_command (isdn_ctrl *ic);int HiSax_writebuf_skb (int id,int chan,int ack,struct sk_buff *skb); __printf(3, 4) -void HiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, ...); +void HiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt, ...); __printf(3, 0) -void VHiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt,va_list args); +void VHiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt,va_list args);void HiSax_reportcard (int cardnr,int sel);int QuickHex (char *txt,u_char *p,int cnt);void LogFrame (struct IsdnCardState *cs,u_char *p,int size);
Signature · 1 line
2.8.1
kbuild test robot
build botRe: [PATCH v2 3/3] Constify some function parameters
url: https://github.com/0day-ci/linux/commits/Emese-Revfy/Introduce-the-initify-gcc-plugin/20160705-074117 config: sparc64-allyesconfig (attached as .config) compiler: sparc64-linux-gnu-gcc (Debian…
Hi,
[auto build test WARNING on next-20160704]
[cannot apply to tip/x86/core asm-generic/master v4.7-rc6 v4.7-rc5 v4.7-rc4 v4.7-rc6] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Emese-Revfy/Introduce-the-initify-gcc-plugin/20160705-074117
config: sparc64-allyesconfig (attached as .config)
compiler: sparc64-linux-gnu-gcc (Debian 5.3.1-8) 5.3.1 20160205
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=sparc64
All warnings (new ones prefixed by >>):
drivers/isdn/hisax/config.c: In function 'VHiSax_putstatus':
>> drivers/isdn/hisax/config.c:688:5: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
p = fmt;
^vim +/const +688 drivers/isdn/hisax/config.c
^1da177e Linus Torvalds 2005-04-16 672
^1da177e Linus Torvalds 2005-04-16 673 if (!cs) {
^1da177e Linus Torvalds 2005-04-16 674 printk(KERN_WARNING "HiSax: No CardStatus for message");
^1da177e Linus Torvalds 2005-04-16 675 return;
^1da177e Linus Torvalds 2005-04-16 676 }
^1da177e Linus Torvalds 2005-04-16 677 spin_lock_irqsave(&cs->statlock, flags);
^1da177e Linus Torvalds 2005-04-16 678 p = tmpbuf;
^1da177e Linus Torvalds 2005-04-16 679 if (head) {
^1da177e Linus Torvalds 2005-04-16 680 p += jiftime(p, jiffies);
^1da177e Linus Torvalds 2005-04-16 681 p += sprintf(p, " %s", head);
^1da177e Linus Torvalds 2005-04-16 682 p += vsprintf(p, fmt, args);
^1da177e Linus Torvalds 2005-04-16 683 *p++ = '\n';
^1da177e Linus Torvalds 2005-04-16 684 *p = 0;
^1da177e Linus Torvalds 2005-04-16 685 len = p - tmpbuf;
^1da177e Linus Torvalds 2005-04-16 686 p = tmpbuf;
^1da177e Linus Torvalds 2005-04-16 687 } else {
^1da177e Linus Torvalds 2005-04-16 @688 p = fmt;
^1da177e Linus Torvalds 2005-04-16 689 len = strlen(fmt);
^1da177e Linus Torvalds 2005-04-16 690 }
^1da177e Linus Torvalds 2005-04-16 691 if (len > HISAX_STATUS_BUFSIZE) {
^1da177e Linus Torvalds 2005-04-16 692 spin_unlock_irqrestore(&cs->statlock, flags);
^1da177e Linus Torvalds 2005-04-16 693 printk(KERN_WARNING "HiSax: status overflow %d/%d\n",
^1da177e Linus Torvalds 2005-04-16 694 len, HISAX_STATUS_BUFSIZE);
^1da177e Linus Torvalds 2005-04-16 695 return;
^1da177e Linus Torvalds 2005-04-16 696 }:::::: The code at line 688 was first introduced by commit :::::: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 Linux-2.6.12-rc2
:::::: TO: Linus Torvalds <[email protected]> :::::: CC: Linus Torvalds <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH v2 3/3] Constify some function parameters
Quoted 5 lines · On Tue, 5 Jul 2016 07:58:04 +0800 kbuild test robot <[email protected]> wrote:
All warnings (new ones prefixed by >>):
drivers/isdn/hisax/config.c: In function 'VHiSax_putstatus':Quoted 1 line
Quoted 1 line
drivers/isdn/hisax/config.c:688:5: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]p = fmt;
Hi,
Thanks for the report, I resent "[PATCH v2 3/3] Constify some function parameters" with the fix.
Signature · 1 line
Emese
PATCH v2 3/3 Constify some function parameters
In reply to Emese RevfyInitify needs const pointer types, the initify plugin caught some __printf arguments that weren't const yet.
--- drivers/isdn/hisax/config.c | 16 ++++++++-------- drivers/isdn/hisax/hisax.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-)
drivers/isdn/hisax/config.c +8 -8
@@ -659,7 +659,7 @@ int jiftime(char *s, long mark)static u_char tmpbuf[HISAX_STATUS_BUFSIZE ]; -void VHiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, +void VHiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt,va_list args) {/* if head == NULL the fmt contains the full info */
@@ -669,23 +669,24 @@ void VHiSax_putstatus(struct IsdnCardState *cs, char *head, char *fmt,u_char *p;isdn_ctrl ic;int len; +const u_char *data;if (!cs) {printk (KERN_WARNING "HiSax: No CardStatus for message" );return ; }spin_lock_irqsave (&cs->statlock, flags); - p = tmpbuf;if (head) { + p = tmpbuf; p +=jiftime (p, jiffies); p +=sprintf (p," %s" , head); p +=vsprintf (p, fmt, args); *p++ ='\n' ; *p =0 ; len = p - tmpbuf; - p = tmpbuf; + data = tmpbuf; }else { - p = fmt; + data = fmt; len =strlen (fmt); }if (len >HISAX_STATUS_BUFSIZE ) {
@@ -699,13 +700,12 @@ void VHiSax_putstatus(struct IsdnCardState *cs, char *head, char *fmt,if (i >= len) i = len; len -= i; -memcpy (cs->status_write, p, i); +memcpy (cs->status_write, data, i); cs->status_write += i;if (cs->status_write > cs->status_end) cs->status_write = cs->status_buf; - p += i;if (len) { -memcpy (cs->status_write, p, len); +memcpy (cs->status_write, data + i, len); cs->status_write += len; }#ifdef KERNELSTACK_DEBUG
@@ -729,7 +729,7 @@ void VHiSax_putstatus(struct IsdnCardState *cs, char *head, char *fmt, } } -void HiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, ...) +void HiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt, ...) {va_list args;
drivers/isdn/hisax/hisax.h +2 -2
@@ -1288,9 +1288,9 @@ int jiftime(char *s, long mark);int HiSax_command (isdn_ctrl *ic);int HiSax_writebuf_skb (int id,int chan,int ack,struct sk_buff *skb); __printf(3, 4) -void HiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt, ...); +void HiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt, ...); __printf(3, 0) -void VHiSax_putstatus (struct IsdnCardState *cs,char *head,char *fmt,va_list args); +void VHiSax_putstatus (struct IsdnCardState *cs,char *head,const char *fmt,va_list args);void HiSax_reportcard (int cardnr,int sel);int QuickHex (char *txt,u_char *p,int cnt);void LogFrame (struct IsdnCardState *cs,u_char *p,int size);
Signature · 1 line
2.8.1