ARM: pass syscall return value to sys_exit tracepoint
Subsystems (2)
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
sys_exit tracepoint expects the syscall return value as a second argument, rather than syscall number.
--- arch/arm/kernel/ptrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/ptrace.c +1 -1
@@ -956,7 +956,7 @@ asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) { scno =ptrace_syscall_trace (regs, scno,PTRACE_SYSCALL_EXIT );if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs, regs->ARM_r0);audit_syscall_exit (regs);return scno; }
Signature · 1 line
1.7.10.4
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 13 lines · Andrew Gabbasov
sys_exittracepoint expects the syscall return value as a second argument, rather than syscall number.Signed-off-by Andrew Gabbasov <[email protected]>--- arch/arm/kernel/ptrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm/kernel/ptrace.c +0
-0
arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) · line 962
Quoted 8 lines
arch/arm/kernel/ptrace.c +1
-1@@ -956,7 +956,7 @@ asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) { scno =ptrace_syscall_trace (regs, scno,PTRACE_SYSCALL_EXIT );if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs, regs->ARM_r0);audit_syscall_exit (regs);return scno; }
It might be worth stashing the return value into a local variable prior to the ptrace_syscall_trace invocation, just in case a debugger decides to rewrite the child's registers.
Will
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 3 lines · Will Deacon
It might be worth stashing the return value into a local variable prior to the
ptrace_syscall_traceinvocation, just in case a debugger decides to rewrite the child's registers.
And if so, ensure that there's a comment there explaining it, otherwise the reasoning will get lost over time.
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 27 lines · Andrew Gabbasov
Quoted 13 lines
sys_exittracepoint expects the syscall return value as a second argument, rather than syscall number.Signed-off-by Andrew Gabbasov <[email protected]>--- arch/arm/kernel/ptrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm/kernel/ptrace.c +0
-0arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) · line 962
Quoted 8 lines
arch/arm/kernel/ptrace.c +1
-1@@ -956,7 +956,7 @@ asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) { scno =ptrace_syscall_trace (regs, scno,PTRACE_SYSCALL_EXIT );if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs, regs->ARM_r0);audit_syscall_exit (regs);return scno; }It might be worth stashing the return value into a local variable prior to the
ptrace_syscall_traceinvocation, just in case a debugger decides to rewrite the child's registers.
Quoted 1 line
Will
The other registers in "regs" structure are not saved around ptrace_syscall_trace, and both audit_syscall_exit and trace_sys_exit get the register values, potentially changed by a debugger. Does it make sense to save the isolated return value for trace_sys_exit call only and not to save other registers, passed, for example, to audit_syscall_exit function that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case of syscall_exit, as we rely on this for syscall_enter case?
Thanks,
Andrew
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 8 lines · Andrew
The other registers in "regs" structure are not saved around
ptrace_syscall_trace, and bothaudit_syscall_exitandtrace_sys_exitget the register values, potentially changed by a debugger. Does it make sense to save the isolated return value fortrace_sys_exitcall only and not to save other registers, passed, for example, toaudit_syscall_exitfunction that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case ofsyscall_exit, as we rely on this forsyscall_entercase?
Actually, why are we doing things in a different order to x86? If we assume that x86 has this stuff correctly ordered, then shouldn't we be following the sequence presented there?
x86 on entry does:
- secure_computing
- tracehook_report_syscall_entry
- trace_sys_enter
- audit_syscall_entry
and on exit:
- audit_syscall_exit
- trace_sys_exit
- tracehook_report_syscall_exit
We appear to be doing the _exact_ reverse ordering in our syscall exit path - why is that?
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 24 lines · Russell King - ARM Linux
Quoted 8 lines · Andrew
The other registers in "regs" structure are not saved around
ptrace_syscall_trace, and bothaudit_syscall_exitandtrace_sys_exitget the register values, potentially changed by a debugger. Does it make sense to save the isolated return value fortrace_sys_exitcall only and not to save other registers, passed, for example, toaudit_syscall_exitfunction that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case ofsyscall_exit, as we rely on this forsyscall_entercase?Actually, why are we doing things in a different order to x86? If we assume that x86 has this stuff correctly ordered, then shouldn't we be following the sequence presented there?
x86 on entry does:
-secure_computing
-tracehook_report_syscall_entry
-trace_sys_enter
-audit_syscall_entry
and on exit:
-audit_syscall_exit
-trace_sys_exit
-tracehook_report_syscall_exit
Quoted 2 lines
We appear to be doing the _exact_ reverse ordering in our syscall exit path - why is that?
I've just been looking at that and the problem stems around having ->syscall set for the current thread before calling the sys_exit tracepoint. The audit code should definitely be moved earlier.
Working on a patch...
Will
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 29 lines · Will Deacon
Quoted 24 lines · Russell King - ARM Linux
Quoted 8 lines · Andrew
The other registers in "regs" structure are not saved around
ptrace_syscall_trace, and bothaudit_syscall_exitandtrace_sys_exitget the register values, potentially changed by a debugger. Does it make sense to save the isolated return value fortrace_sys_exitcall only and not to save other registers, passed, for example, toaudit_syscall_exitfunction that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case ofsyscall_exit, as we rely on this forsyscall_entercase?Actually, why are we doing things in a different order to x86? If we assume that x86 has this stuff correctly ordered, then shouldn't we be following the sequence presented there?
x86 on entry does:
-secure_computing
-tracehook_report_syscall_entry
-trace_sys_enter
-audit_syscall_entry
and on exit:
-audit_syscall_exit
-trace_sys_exit
-tracehook_report_syscall_exitQuoted 2 lines
We appear to be doing the _exact_ reverse ordering in our syscall exit path - why is that?
Quoted 5 lines
I've just been looking at that and the problem stems around having ->syscall set for the current thread before calling the
sys_exittracepoint. The audit code should definitely be moved earlier.Working on a patch...
Ok, how about something like the following?
Will
--->8
commit 0109b339755b29f92733c1f257911615fc231727
Author: Will Deacon <[email protected]> Date: Tue Dec 4 11:47:14 2012 +0000
ARM: syscall: rework ordering in syscall_trace_exit
syscall_trace_exit is currently doing things back-to-front; invoking the audit hook *after* signalling the debugger, which presents an opportunity for the registers to be re-written by userspace in order to bypass auditing constaints.
This patch fixes the ordering by moving the audit code first and the tracehook code last. On the face of it, it looks like current_thread_info()->syscall may be incorrect for the sys_exit tracepoint, but that's actually not an issue because it will have been set during syscall entry and cannot have changed since then.
arch/arm/kernel/entry-common.S +0 -1
@@ -452,7 +452,6 @@ __sys_trace: __sys_trace_return: str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 - mov r1, scno mov r0, sp bl syscall_trace_exit b ret_slow_syscall
arch/arm/kernel/ptrace.c +14 -9
@@ -957,17 +957,22 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)return scno; } -asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) +asmlinkage void syscall_trace_exit(struct pt_regs *regs) { -current_thread_info ()->syscall = scno; - -if (test_thread_flag (TIF_SYSCALL_TRACE )) - scno =tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT ); +/* +* Audit the syscall before anything else, as a debugger may +* come in and change the current registers. +*/ +audit_syscall_exit (regs); +/* +* Note that we haven't updated the ->syscall field for the +* current thread. This isn't a problem because it will have +* been set on syscall entry and there hasn't been an opportunity +* for a PTRACE_SET_SYSCALL since then. +*/ if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs,regs_return_value (regs)); -audit_syscall_exit (regs); - -return scno; +tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT ); }
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 101 lines · Will Deacon
Quoted 29 lines · Russell King - ARM Linux
Quoted 24 lines · Andrew
Quoted 8 lines
The other registers in "regs" structure are not saved around
ptrace_syscall_trace, and bothaudit_syscall_exitandtrace_sys_exitget the register values, potentially changed by a debugger. Does it make sense to save the isolated return value fortrace_sys_exitcall only and not to save other registers, passed, for example, toaudit_syscall_exitfunction that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case ofsyscall_exit, as we rely on this forsyscall_entercase?Actually, why are we doing things in a different order to x86? If we assume that x86 has this stuff correctly ordered, then shouldn't we be following the sequence presented there?
x86 on entry does:
-secure_computing
-tracehook_report_syscall_entry
-trace_sys_enter
-audit_syscall_entry
and on exit:
-audit_syscall_exit
-trace_sys_exit
-tracehook_report_syscall_exitQuoted 2 lines
We appear to be doing the _exact_ reverse ordering in our syscall exit path - why is that?
Quoted 5 lines
I've just been looking at that and the problem stems around having ->syscall set for the current thread before calling the
sys_exittracepoint. The audit code should definitely be moved earlier.Working on a patch...
Ok, how about something like the following?
Will
--->8
commit
0109b339755b29f92733c1f257911615fc231727Author: Will Deacon <[email protected]> Date: Tue Dec 4 11:47:14 2012 +0000ARM: syscall: rework ordering in
syscall_trace_exit
syscall_trace_exitis currently doing things back-to-front; invoking
the audit hook *after* signalling the debugger, which presents an opportunity for the registers to be re-written by userspace in order to bypass auditing constaints.This patch fixes the ordering by moving the audit code first and the tracehook code last. On the face of it, it looks like current_thread_info()->syscall may be incorrect for the
sys_exittracepoint, but that's actually not an issue because it will have been set during syscall entry and cannot have changed since then.Reported-by Andrew Gabbasov <[email protected]>Signed-off-by Will Deacon <[email protected]>arch/arm/kernel/entry-common.S +0
-1@@ -452,7 +452,6 @@ __sys_trace: __sys_trace_return: str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 - mov r1, scno mov r0, sp bl syscall_trace_exit b ret_slow_syscallarch/arm/kernel/ptrace.c +11
-5@@ -957,17 +957,22 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)return scno; } -asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) +asmlinkage void syscall_trace_exit(struct pt_regs *regs) { -current_thread_info ()->syscall = scno; - -if (test_thread_flag (TIF_SYSCALL_TRACE )) - scno =tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT ); +/* +* Audit the syscall before anything else, as a debugger may +* come in and change the current registers. +*/ +audit_syscall_exit (regs); +/* +* Note that we haven't updated the ->syscall field for the +* current thread. This isn't a problem because it will have +* been set on syscall entry and there hasn't been an opportunity +* for a PTRACE_SET_SYSCALL since then.
arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 978
Quoted 10 lines
arch/arm/kernel/ptrace.c +3
-4+*/ if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs,regs_return_value (regs)); -audit_syscall_exit (regs); - -return scno; +tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT ); }
Hi Will,
Thank you for the patch, I was thinking about something like this too, but was in a doubt that reverse order could be for a purpose ;-)
Minor question: what is the version this patch is against? The latest version in Linus'es tree does not have "current_thread_info()->syscall = scno;" at the beginning of syscall_trace_exit. Or is it just a "proof of concept" patch and will be modified before merging to the source?
Thanks,
Andrew
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 1 line · Andrew
Hi Will,
Hello Andrew,
Quoted 2 lines
Thank you for the patch, I was thinking about something like this too, but was in a doubt that reverse order could be for a purpose ;-)
Nope, it think it was an oversight when the entry path was cleaned up.
Quoted 4 lines
Minor question: what is the version this patch is against? The latest version in Linus'es tree does not have "current_thread_info()->syscall = scno;" at the beginning of
syscall_trace_exit. Or is it just a "proof of concept" patch and will be modified before merging to the source?
It's based on what Russell has queued for 3.8 (there is some seccomp stuff in there). If you're happy with the patch, I can stick it in the patch system once I've given it some more testing.
Will
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 15 lines · Andrew
Quoted 1 line
Hi Will,
Hello Andrew,
Quoted 2 lines
Thank you for the patch, I was thinking about something like this too, but was in a doubt that reverse order could be for a purpose ;-)
Nope, it think it was an oversight when the entry path was cleaned up.
Quoted 4 lines
Minor question: what is the version this patch is against? The latest version in Linus'es tree does not have "current_thread_info()->syscall = scno;" at the beginning of
syscall_trace_exit. Or is it just a "proof of concept" patch and will be modified before merging to the source?
Quoted 5 lines
It's based on what Russell has queued for 3.8 (there is some seccomp stuff in there). If you're happy with the patch, I can stick it in the patch system once I've given it some more testing.
Will
Yes, the patch looks good to me.
Thanks.
Andrew
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
In reply to Will DeaconQuoted 53 lines · Will Deacon
Quoted 29 lines · Will Deacon
Quoted 24 lines · Russell King - ARM Linux
Quoted 8 lines · Andrew
The other registers in "regs" structure are not saved around
ptrace_syscall_trace, and bothaudit_syscall_exitandtrace_sys_exitget the register values, potentially changed by a debugger. Does it make sense to save the isolated return value fortrace_sys_exitcall only and not to save other registers, passed, for example, toaudit_syscall_exitfunction that takes the return value from regs structure? Isn't it a reasonable assumption that a debugger will preserve important register values (or intentionally change them for some purpose) in case ofsyscall_exit, as we rely on this forsyscall_entercase?Actually, why are we doing things in a different order to x86? If we assume that x86 has this stuff correctly ordered, then shouldn't we be following the sequence presented there?
x86 on entry does:
-secure_computing
-tracehook_report_syscall_entry
-trace_sys_enter
-audit_syscall_entry
and on exit:
-audit_syscall_exit
-trace_sys_exit
-tracehook_report_syscall_exitQuoted 2 lines
We appear to be doing the _exact_ reverse ordering in our syscall exit path - why is that?
Quoted 5 lines
I've just been looking at that and the problem stems around having ->syscall set for the current thread before calling the
sys_exittracepoint. The audit code should definitely be moved earlier.Working on a patch...
Ok, how about something like the following?
Will
--->8
commit
0109b339755b29f92733c1f257911615fc231727Author: Will Deacon <[email protected]> Date: Tue Dec 4 11:47:14 2012 +0000ARM: syscall: rework ordering in
syscall_trace_exit
syscall_trace_exitis currently doing things back-to-front; invoking the audit hook *after* signalling the debugger, which presents an opportunity for the registers to be re-written by userspace in order to bypass auditing constaints.
Quoted 8 lines
This patch fixes the ordering by moving the audit code first and the tracehook code last. On the face of it, it looks like current_thread_info()->syscall may be incorrect for the
sys_exittracepoint, but that's actually not an issue because it will have been set during syscall entry and cannot have changed since then.Reported-by Andrew Gabbasov <[email protected]>Signed-off-by Will Deacon <[email protected]>
My board locks up upon entering userspace with this applied.
Quoted 17 lines
arch/arm/kernel/entry-common.S +0
-1@@ -452,7 +452,6 @@ __sys_trace: __sys_trace_return: str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 - mov r1, scno mov r0, sp bl syscall_trace_exit b ret_slow_syscallarch/arm/kernel/ptrace.c +0
-0
arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 965
Quoted 10 lines
arch/arm/kernel/ptrace.c +1
-5@@ -957,17 +957,22 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)return scno; } -asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno) +asmlinkage void syscall_trace_exit(struct pt_regs *regs) { -current_thread_info ()->syscall = scno; - -if (test_thread_flag (TIF_SYSCALL_TRACE )) - scno =tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT );
I see here we used to test TIF_SYSCALL_TRACE...
Quoted 10 lines
arch/arm/kernel/ptrace.c +9
-0+/* +* Audit the syscall before anything else, as a debugger may +* come in and change the current registers. +*/ +audit_syscall_exit (regs); +/* +* Note that we haven't updated the ->syscall field for the +* current thread. This isn't a problem because it will have +* been set on syscall entry and there hasn't been an opportunity
arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 977
Quoted 10 lines
arch/arm/kernel/ptrace.c +4
-4+* for a PTRACE_SET_SYSCALL since then. +*/ if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs,regs_return_value (regs)); -audit_syscall_exit (regs); - -return scno; +tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT );
And here we don't. Restoring the test makes boot complete, and audit and tracing seem to be happy afterwards.
With the test restored,
Thanks,
Mark.
PATCH 1/1 ARM: pass syscall return value to sys_exit tracepoint
Quoted 34 lines · Mark Rutland
arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 965
Quoted 6 lines · Will Deacon
arch/arm/kernel/ptrace.c +1
-4+asmlinkage void syscall_trace_exit(struct pt_regs *regs) { -current_thread_info ()->syscall = scno; - -if (test_thread_flag (TIF_SYSCALL_TRACE )) - scno =tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT );I see here we used to test
TIF_SYSCALL_TRACE...Quoted 10 lines
arch/arm/kernel/ptrace.c +9
-0+/* +* Audit the syscall before anything else, as a debugger may +* come in and change the current registers. +*/ +audit_syscall_exit (regs); +/* +* Note that we haven't updated the ->syscall field for the +* current thread. This isn't a problem because it will have +* been set on syscall entry and there hasn't been an opportunity arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 977
Quoted 10 lines
arch/arm/kernel/ptrace.c +4
-4+* for a PTRACE_SET_SYSCALL since then. +*/ if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs, scno); +trace_sys_exit (regs,regs_return_value (regs)); -audit_syscall_exit (regs); - -return scno; +tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT );And here we don't. Restoring the test makes boot complete, and audit and tracing seem to be happy afterwards.
Quoted 2 lines
With the test restored,
Tested-by Mark Rutland <[email protected]>
Cheers Mark, I'll fix this oversight and stick it in the patch system with your tag.
Will