samples/ftrace: mark my_tramp[12]? global
PATCH 1/1 samples/ftrace: mark my_tramp[12]? global
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
--- samples/ftrace/ftrace-direct-modify.c | 2 ++ samples/ftrace/ftrace-direct-too.c | 1 + samples/ftrace/ftrace-direct.c | 1 + 3 files changed, 4 insertions(+)
samples/ftrace/ftrace-direct-modify.c +2 -0
@@ -21,6 +21,7 @@ static unsigned long my_ip = (unsigned long)schedule; asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp1, @function\n" +" .globl my_tramp1\n" " my_tramp1:" " pushq %rbp\n" " movq %rsp, %rbp\n"
@@ -29,6 +30,7 @@ asm (" .size my_tramp1, .-my_tramp1\n" " ret\n" " .type my_tramp2, @function\n" +" .globl my_tramp2\n" " my_tramp2:" " pushq %rbp\n" " movq %rsp, %rbp\n"
samples/ftrace/ftrace-direct-too.c +1 -0
@@ -16,6 +16,7 @@ extern void my_tramp(void *); asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp, @function\n" +" .globl my_tramp\n" " my_tramp:" " pushq %rbp\n" " movq %rsp, %rbp\n"
samples/ftrace/ftrace-direct.c +1 -0
@@ -14,6 +14,7 @@ extern void my_tramp(void *); asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp, @function\n" +" .globl my_tramp\n" " my_tramp:" " pushq %rbp\n" " movq %rsp, %rbp\n"
base-commit: 585e5b17b92dead8a3aca4e3c9876fbca5f7e0ba
Signature · 1 line
2.29.2.299.gdc1121823c-goog
Quoted 6 lines · Sami Tolvanen
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
Signed-off-by Sami Tolvanen <[email protected]>
Signature · 1 line
Kees Cook
Steven Rostedt
maintainerQuoted 5 lines · On Fri, 13 Nov 2020 10:34:14 -0800 Sami Tolvanen <[email protected]>…
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
Actually, since that function is not really global, would it work if you removed the "extern" from the my_tramp declaration?
In other words, is there a way to tell C that a function is declared in an inline assembly block?
-- Steve
Quoted 53 lines
Signed-off-by Sami Tolvanen <[email protected]>--- samples/ftrace/ftrace-direct-modify.c | 2 ++ samples/ftrace/ftrace-direct-too.c | 1 + samples/ftrace/ftrace-direct.c | 1 + 3 files changed, 4 insertions(+)samples/ftrace/ftrace-direct-modify.c +2
-0@@ -21,6 +21,7 @@ static unsigned long my_ip = (unsigned long)schedule; asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp1, @function\n" +" .globl my_tramp1\n" " my_tramp1:" " pushq %rbp\n" " movq %rsp, %rbp\n"@@ -29,6 +30,7 @@ asm (" .size my_tramp1, .-my_tramp1\n" " ret\n" " .type my_tramp2, @function\n" +" .globl my_tramp2\n" " my_tramp2:" " pushq %rbp\n" " movq %rsp, %rbp\n" samples/ftrace/ftrace-direct-too.c +1
-0@@ -16,6 +16,7 @@ extern void my_tramp(void *); asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp, @function\n" +" .globl my_tramp\n" " my_tramp:" " pushq %rbp\n" " movq %rsp, %rbp\n"samples/ftrace/ftrace-direct.c +1
-0@@ -14,6 +14,7 @@ extern void my_tramp(void *); asm (" .pushsection .text, \"ax\", @progbits\n" " .type my_tramp, @function\n" +" .globl my_tramp\n" " my_tramp:" " pushq %rbp\n" " movq %rsp, %rbp\n"base-commit:
585e5b17b92dead8a3aca4e3c9876fbca5f7e0ba
Quoted 4 lines · Steven Rostedt
On Fri, 13 Nov 2020 10:34:14 -0800
Sami Tolvanen <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
Actually, since that function is not really global, would it work if you removed the "extern" from the
my_trampdeclaration?
Unfortunately not, removing the "extern" doesn't seem to change anything.
Quoted 2 lines
In other words, is there a way to tell C that a function is declared in an inline assembly block?
I'm not sure if there's a way to tell C that a static function is declared in inline assembly. At least I couldn't find a way that would make the compiler happy.
Sami
Steven Rostedt
maintainerQuoted 14 lines · On Mon, 16 Nov 2020 12:10:10 -0800 Sami Tolvanen <[email protected]>…
Quoted 4 lines · Steven Rostedt
On Fri, 13 Nov 2020 10:34:14 -0800
Sami Tolvanen <[email protected]> wrote:Quoted 8 lines
Quoted 5 lines
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
Actually, since that function is not really global, would it work if you removed the "extern" from the
my_trampdeclaration?
Quoted 8 lines
Unfortunately not, removing the "extern" doesn't seem to change anything.
Quoted 2 lines
In other words, is there a way to tell C that a function is declared in an inline assembly block?
I'm not sure if there's a way to tell C that a static function is declared in inline assembly. At least I couldn't find a way that would make the compiler happy.
I'm trying to see the warning. What option makes clang trigger a warning on this?
From user space, I'm just using the following file:
#include <stdio.h>
void my_direct_func(char *str)
{
printf("%s\n", str);
}int test(char *str);
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type test, @function\n"
" test:"
" pushq %rbp\n"
" movq %rsp, %rbp\n"
" pushq %rdi\n"
" call my_direct_func\n"
" popq %rdi\n"
" leave\n"
" ret\n"
" .size test, .-test\n"
" .popsection\n"
);
int main (int argc, char **argv)
{
test("hello");
return 0;
}Quoted 23 lines · Steven Rostedt
Quoted 19 lines · On Mon, 16 Nov 2020 12:10:10 -0800 Sami Tolvanen <[email protected]>…
Quoted 4 lines · Steven Rostedt
On Fri, 13 Nov 2020 10:34:14 -0800
Sami Tolvanen <[email protected]> wrote:Quoted 8 lines
Quoted 5 lines
my_tramp[12]? are declared as global functions in C, but they are not marked global in the inline assembly definition. This mismatch confuses Clang's Control-Flow Integrity checking. Fix the definitions by adding .globl.
Actually, since that function is not really global, would it work if you removed the "extern" from the
my_trampdeclaration?Unfortunately not, removing the "extern" doesn't seem to change anything.
Quoted 2 lines
In other words, is there a way to tell C that a function is declared in an inline assembly block?
Quoted 6 lines
Quoted 3 lines
I'm not sure if there's a way to tell C that a static function is declared in inline assembly. At least I couldn't find a way that would make the compiler happy.
I'm trying to see the warning. What option makes clang trigger a warning on this?
Clang doesn't warn about this as we're building a module, it just generates a reference to a non-existing global "my_tramp" symbol, because the one defined in inline assembly has a local binding:
$ readelf --symbols --wide ftrace-direct.lto.o | grep my_tramp
16: 0000000000000000 13 FUNC LOCAL DEFAULT 1 my_tramp
33: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND my_tramp
42: 0000000000000000 8 FUNC GLOBAL HIDDEN 8 my_tramp.cfi_jtThis would prevent the module from loading, which modpost catches:
ERROR: modpost: "my_tramp" [samples/ftrace/ftrace-direct.ko] undefined!
Quoted 1 line
From user space, I'm just using the following file:
As this error happens only with Control-Flow Integrity, we need to take the address of the "test" function to force the compiler to generate a jump table entry for it. Here's a slightly tweaked stand-alone reproducer:
Sami
Steven Rostedt
maintainerQuoted 13 lines · On Mon, 16 Nov 2020 13:10:57 -0800 Sami Tolvanen <[email protected]>…
Clang doesn't warn about this as we're building a module, it just generates a reference to a non-existing global "
my_tramp" symbol, because the one defined in inline assembly has a local binding:$ readelf --symbols --wide ftrace-direct.lto.o | grep my_tramp 16: 0000000000000000 13 FUNC LOCAL DEFAULT 1 my_tramp 33: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND my_tramp 42: 0000000000000000 8 FUNC GLOBAL HIDDEN 8 my_tramp.cfi_jtThis would prevent the module from loading, which modpost catches:
ERROR: modpost: "
my_tramp" [samples/ftrace/ftrace-direct.ko] undefined!
Quoted 8 lines
Quoted 1 line
From user space, I'm just using the following file:
As this error happens only with Control-Flow Integrity, we need to take the address of the "test" function to force the compiler to generate a jump table entry for it. Here's a slightly tweaked stand-alone reproducer:
Thanks, we don't need to look more into this. It was mostly my curiosity to find a way to have the compiler know about a function declared statically in inline assembly. Maybe I'm asking for too much ;-)
I'll take your original patch. Does it need to go to stable, or is this not that big of an issue to allow it to be added in the next merge window?
-- Steve
Quoted 24 lines · Steven Rostedt
Quoted 20 lines · On Mon, 16 Nov 2020 13:10:57 -0800 Sami Tolvanen <[email protected]>…
Clang doesn't warn about this as we're building a module, it just generates a reference to a non-existing global "
my_tramp" symbol, because the one defined in inline assembly has a local binding:$ readelf --symbols --wide ftrace-direct.lto.o | grep my_tramp 16: 0000000000000000 13 FUNC LOCAL DEFAULT 1 my_tramp 33: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND my_tramp 42: 0000000000000000 8 FUNC GLOBAL HIDDEN 8 my_tramp.cfi_jtThis would prevent the module from loading, which modpost catches:
ERROR: modpost: "
my_tramp" [samples/ftrace/ftrace-direct.ko] undefined!Quoted 1 line
From user space, I'm just using the following file:
As this error happens only with Control-Flow Integrity, we need to take the address of the "test" function to force the compiler to generate a jump table entry for it. Here's a slightly tweaked stand-alone reproducer:
Quoted 8 lines
Quoted 1 line
Thanks, we don't need to look more into this. It was mostly my curiosity to find a way to have the compiler know about a function declared statically in inline assembly. Maybe I'm asking for too much ;-)
I'll take your original patch. Does it need to go to stable, or is this not that big of an issue to allow it to be added in the next merge window?
Thanks! This is definitely not a big issue, it just fixes the allyesconfig build with CFI, and it's not needed in -stable.
Sami