Skip to contentSkip to conversation

arm: ptrace: fix syscall modification under PTRACE_O_TRACESECCOMP

PATCH In review Kees Cook 4 participants 18 messages Jun 18, 2014 – Jul 4, 2014

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 arm: ptrace: fix syscall modification under PTRACE_O_TRACESECCOMP

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

The x86 code handles this (it expects orig_ax to always be the desired syscall). In the ARM case, this means we should not be touching current_thread_info()->syscall after its initial assignment. All failures should result in a -1 syscall, though.

Based on patch by Ricky Zhou.

Cc Ricky Zhou <[email protected]>
Cc stable at vger.kernel.org
---
 arch/arm/kernel/ptrace.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
arch/arm/kernel/ptrace.c +13 -7
index 0dd3b79b15c3..97bd95f6aa01 100644
@@ -911,6 +911,7 @@ enum ptrace_syscall_dir {
 static int tracehook_report_syscall(struct pt_regs *regs,
 				    enum ptrace_syscall_dir dir)
 {
+	int ret = 0;
 	unsigned long ip;
 
 	/*
@@ -923,30 +924,35 @@ static int tracehook_report_syscall(struct pt_regs *regs,
 	if (dir == PTRACE_SYSCALL_EXIT)
 		tracehook_report_syscall_exit(regs, 0);
 	else if (tracehook_report_syscall_entry(regs))
-		current_thread_info()->syscall = -1;
+		ret = -1;
 
 	regs->ARM_ip = ip;
-	return current_thread_info()->syscall;
+	return ret;
 }
 
 asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
 {
+	int ret = 0;
+
+	/* set up syscall, which may be changed in secure_computing */
 	current_thread_info()->syscall = scno;
 
 	/* Do the secure computing check first; failures should be fast. */
 	if (secure_computing(scno) == -1)
 		return -1;
 
-	if (test_thread_flag(TIF_SYSCALL_TRACE))
-		scno = tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
+	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
+	    tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER))
+		ret = -1;
 
 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
-		trace_sys_enter(regs, scno);
+		trace_sys_enter(regs, current_thread_info()->syscall);
 
-	audit_syscall_entry(AUDIT_ARCH_ARM, scno, regs->ARM_r0, regs->ARM_r1,
+	audit_syscall_entry(AUDIT_ARCH_ARM, current_thread_info()->syscall,
+			    regs->ARM_r0, regs->ARM_r1,
 			    regs->ARM_r2, regs->ARM_r3);
 
-	return scno;
+	return ret ?: current_thread_info()->syscall;
 }
 
 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 11 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

Quoted 4 lines

The x86 code handles this (it expects orig_ax to always be the desired syscall). In the ARM case, this means we should not be touching current_thread_info()->syscall after its initial assignment. All failures should result in a -1 syscall, though.

This looks sensible.

--Andy

#2 lore raw
In reply to Kees Cook

Hi Kees,

I'm struggling to see the bug in the current code, so apologies if my questions aren't helpful.

Quoted 10 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

I don't think I understand what you mean by `the code must respect the changed syscall'. The current code does indeed issue the new syscall, so are you more concerned with secure_computing changing ->syscall, then the debugger can't see the new syscall when it sees the trap from tracehook? Are these even supposed to inter-operate?

Quoted 4 lines

The x86 code handles this (it expects orig_ax to always be the desired syscall). In the ARM case, this means we should not be touching current_thread_info()->syscall after its initial assignment. All failures should result in a -1 syscall, though.

The only time we explicitly touch ->syscall is when we're aborting the call (i.e. writing -1), which I think is fine.

Will

author
#3 lore raw
Quoted 17 lines · Will Deacon

Hi Kees,

I'm struggling to see the bug in the current code, so apologies if my questions aren't helpful.

Quoted 10 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

Quoted 5 lines

I don't think I understand what you mean by `the code must respect the changed syscall'. The current code does indeed issue the new syscall, so are you more concerned with secure_computing changing ->syscall, then the debugger can't see the new syscall when it sees the trap from tracehook? Are these even supposed to inter-operate?

The problem is the use of "scno" in the call -- it results in ignoring the value that may be set up in ->syscall by a tracer:

syscall_trace_enter(regs, scno):

    current_thread_info()->syscall = scno;
    secure_computing(scno):
        [block on ptrace]
        [ptracer changes current_thread_info()->syscall vis PTRACE_SET_SYSCALL]
    ...
    return scno;

This means the tracer's changed syscall value will be ignored (syscall_trace_enter returns original "scno" instead of actual current_thread_info()->syscall). In the original code, failure cases were propagated correctly, but not tracer-induced changes.

Is that more clear? It's not an obvious state (due to the external modification of process state during the ptrace blocking). I've also got tests for this, if that's useful to further illustrate:

https://github.com/kees/seccomp/commit/bd24e174593f79784b97178b583f17e0ea9d2aa7

Quoted 8 lines
Quoted 5 lines

The x86 code handles this (it expects orig_ax to always be the desired syscall). In the ARM case, this means we should not be touching current_thread_info()->syscall after its initial assignment. All failures should result in a -1 syscall, though.

The only time we explicitly touch ->syscall is when we're aborting the call (i.e. writing -1), which I think is fine.

That was the error path, which was okay. This rearranges things to both handle error conditions and tracer changes.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#4 lore raw
Quoted 38 lines · Kees Cook
Quoted 15 lines · Will Deacon

I'm struggling to see the bug in the current code, so apologies if my questions aren't helpful.

Quoted 10 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

Quoted 5 lines

I don't think I understand what you mean by `the code must respect the changed syscall'. The current code does indeed issue the new syscall, so are you more concerned with secure_computing changing ->syscall, then the debugger can't see the new syscall when it sees the trap from tracehook? Are these even supposed to inter-operate?

The problem is the use of "scno" in the call -- it results in ignoring the value that may be set up in ->syscall by a tracer:

syscall_trace_enter(regs, scno):

    current_thread_info()->syscall = scno;
    secure_computing(scno):
        [block on ptrace]
        [ptracer changes current_thread_info()->syscall vis PTRACE_SET_SYSCALL]
    ...
    return scno;

This means the tracer's changed syscall value will be ignored (syscall_trace_enter returns original "scno" instead of actual current_thread_info()->syscall). In the original code, failure cases were propagated correctly, but not tracer-induced changes.

Quoted 5 lines

Is that more clear? It's not an obvious state (due to the external modification of process state during the ptrace blocking). I've also got tests for this, if that's useful to further illustrate:

https://github.com/kees/seccomp/commit/bd24e174593f79784b97178b583f17e0ea9d2aa7

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Will

--->8

arch/arm/kernel/ptrace.c +4 -3
index 0dd3b79b15c3..0c27ed6f3f23 100644
@@ -908,7 +908,7 @@ enum ptrace_syscall_dir {
        PTRACE_SYSCALL_EXIT,
 };
 
-static int tracehook_report_syscall(struct pt_regs *regs,
+static void tracehook_report_syscall(struct pt_regs *regs,
                                    enum ptrace_syscall_dir dir)
 {
        unsigned long ip;
@@ -926,7 +926,6 @@ static int tracehook_report_syscall(struct pt_regs *regs,
                current_thread_info()->syscall = -1;
 
        regs->ARM_ip = ip;
-       return current_thread_info()->syscall;
 }
 
 asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
@@ -938,7 +937,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
                return -1;
 
        if (test_thread_flag(TIF_SYSCALL_TRACE))
-               scno = tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
+               tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
+
+       scno = current_thread_info()->syscall;
 
        if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
                trace_sys_enter(regs, scno);
author
#5 lore raw
Quoted 45 lines · Will Deacon
Quoted 38 lines · Kees Cook
Quoted 15 lines · Will Deacon

I'm struggling to see the bug in the current code, so apologies if my questions aren't helpful.

Quoted 10 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

Quoted 5 lines

I don't think I understand what you mean by `the code must respect the changed syscall'. The current code does indeed issue the new syscall, so are you more concerned with secure_computing changing ->syscall, then the debugger can't see the new syscall when it sees the trap from tracehook? Are these even supposed to inter-operate?

The problem is the use of "scno" in the call -- it results in ignoring the value that may be set up in ->syscall by a tracer:

syscall_trace_enter(regs, scno):

    current_thread_info()->syscall = scno;
    secure_computing(scno):
        [block on ptrace]
        [ptracer changes current_thread_info()->syscall vis PTRACE_SET_SYSCALL]
    ...
    return scno;

This means the tracer's changed syscall value will be ignored (syscall_trace_enter returns original "scno" instead of actual current_thread_info()->syscall). In the original code, failure cases were propagated correctly, but not tracer-induced changes.

Quoted 5 lines

Is that more clear? It's not an obvious state (due to the external modification of process state during the ptrace blocking). I've also got tests for this, if that's useful to further illustrate:

https://github.com/kees/seccomp/commit/bd24e174593f79784b97178b583f17e0ea9d2aa7

Quoted 7 lines

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Quoted 27 lines

Will

--->8

arch/arm/kernel/ptrace.c +1 -2
index 0dd3b79b15c3..0c27ed6f3f23 100644
@@ -908,7 +908,7 @@ enum ptrace_syscall_dir {
        PTRACE_SYSCALL_EXIT,
 };
 
-static int tracehook_report_syscall(struct pt_regs *regs,
+static void tracehook_report_syscall(struct pt_regs *regs,
                                    enum ptrace_syscall_dir dir)
 {
        unsigned long ip;
@@ -926,7 +926,6 @@ static int tracehook_report_syscall(struct pt_regs *regs,
                current_thread_info()->syscall = -1;
 
        regs->ARM_ip = ip;
-       return current_thread_info()->syscall;
 }
 
 asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)

arch/arm/kernel/ptrace.c asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno) · line 942

Quoted 7 lines
arch/arm/kernel/ptrace.c +3 -1
@@ -938,7 +937,9 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
                return -1;
 
        if (test_thread_flag(TIF_SYSCALL_TRACE))
-               scno = tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
+               tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
+
+       scno = current_thread_info()->syscall;

Perhaps it'd be worth adding a comment above this line just for people looking at this in the future. Something like:

/* secure_computing and tracehook_report_syscall may have changed syscall */

-Kees

Quoted 3 lines
arch/arm/kernel/ptrace.c +0 -0
        if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
                trace_sys_enter(regs, scno);
Signature · 2 lines
Kees Cook
Chrome OS Security
author Tested-by
#6 lore raw
Quoted 51 lines · Kees Cook
Quoted 50 lines · Will Deacon
Quoted 38 lines · Kees Cook
Quoted 15 lines · Will Deacon

I'm struggling to see the bug in the current code, so apologies if my questions aren't helpful.

Quoted 10 lines · Kees Cook

An x86 tracer wanting to change the syscall uses PTRACE_SETREGS (stored to regs->orig_ax), and an ARM tracer uses PTRACE_SET_SYSCALL (stored to current_thread_info()->syscall). When this happens, the syscall can change across the call to secure_computing(), since it may block on tracer notification, and the tracer can then make changes to the process, before we return from secure_computing(). This means the code must respect the changed syscall after the secure_computing() call in syscall_trace_enter(). The same is true for tracehook_report_syscall_entry() which may also block and change the syscall.

Quoted 5 lines

I don't think I understand what you mean by `the code must respect the changed syscall'. The current code does indeed issue the new syscall, so are you more concerned with secure_computing changing ->syscall, then the debugger can't see the new syscall when it sees the trap from tracehook? Are these even supposed to inter-operate?

The problem is the use of "scno" in the call -- it results in ignoring the value that may be set up in ->syscall by a tracer:

syscall_trace_enter(regs, scno):

    current_thread_info()->syscall = scno;
    secure_computing(scno):
        [block on ptrace]
        [ptracer changes current_thread_info()->syscall vis PTRACE_SET_SYSCALL]
    ...
    return scno;

This means the tracer's changed syscall value will be ignored (syscall_trace_enter returns original "scno" instead of actual current_thread_info()->syscall). In the original code, failure cases were propagated correctly, but not tracer-induced changes.

Quoted 5 lines

Is that more clear? It's not an obvious state (due to the external modification of process state during the ptrace blocking). I've also got tests for this, if that's useful to further illustrate:

https://github.com/kees/seccomp/commit/bd24e174593f79784b97178b583f17e0ea9d2aa7

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

Quoted 4 lines
Quoted 2 lines

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#7 lore raw
Quoted 7 lines · Kees Cook
Quoted 6 lines · Kees Cook
Quoted 5 lines · Will Deacon

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

Thanks, Kees. I'll post a patch shortly. I'll try and remember to keep an eye out for this when seccomp lands for arm64 too.

Will

author
#8 lore raw
Quoted 11 lines · Will Deacon
Quoted 10 lines · Kees Cook
Quoted 9 lines · Kees Cook
Quoted 7 lines · Will Deacon

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

Thanks, Kees. I'll post a patch shortly. I'll try and remember to keep an eye out for this when seccomp lands for arm64 too.

Great, thanks!

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#9 lore raw
Quoted 18 lines · Kees Cook
Quoted 17 lines · Will Deacon
Quoted 15 lines · Kees Cook
Quoted 10 lines · Kees Cook
Quoted 7 lines · Will Deacon

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

Quoted 8 lines
Quoted 2 lines

Thanks, Kees. I'll post a patch shortly. I'll try and remember to keep an eye out for this when seccomp lands for arm64 too.

Great, thanks!

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Will

#10 lore raw
Quoted 22 lines · Will Deacon
Quoted 21 lines · Kees Cook
Quoted 19 lines · Will Deacon
Quoted 7 lines · Kees Cook
Quoted 6 lines · Kees Cook
Quoted 5 lines · Will Deacon

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

Thanks, Kees. I'll post a patch shortly. I'll try and remember to keep an eye out for this when seccomp lands for arm64 too.

Quoted 8 lines
Quoted 5 lines

Great, thanks!

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Do I really have to repost my patch even without any changes since the last one? Just kidding. I will do so once I confirm the issue discussed here. (I've finally found out a user of my patch :)

-Takahiro AKASHI

Quoted 7 lines

Will

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#11 lore raw
In reply to Will Deacon

Hi Will,

Quoted 22 lines · Will Deacon
Quoted 21 lines · Kees Cook
Quoted 19 lines · Will Deacon
Quoted 7 lines · Kees Cook
Quoted 6 lines · Kees Cook
Quoted 5 lines · Will Deacon

Right, gotcha. Thanks for the explanation. I was confused, because tracehook_report_syscall does the right thing (returns current_thread_info()->syscall), but if we don't have TIF_SYSCALL_TRACE set, then updates during the secure_computing callback will be ignored.re

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

However, my fix to this is significantly smaller than your patch, so I fear I'm still missing something.

Oh, yes, that's much smaller. Nice! I will test this and report back.

Yup, I can confirm this works. Thanks!

Thanks, Kees. I'll post a patch shortly. I'll try and remember to keep an eye out for this when seccomp lands for arm64 too.

Quoted 8 lines
Quoted 5 lines

Great, thanks!

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

I took a quick look at the current implementation of ptrace. ptrace(PTRACE_GETREGSET/SETREGSET), eventually gpr_get/set(), handles only 'struct user_pt_regs', and we have no way to modify orig_x0 nor syscallno in 'struct pt_regs' directly. So it seems to me that we can't change a system call by ptrace().
Do I misunderstand anything?

-Takahiro AKASHI

Quoted 7 lines

Will

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#12 lore raw
Quoted 1 line · AKASHI Takahiro

Hi Will,

Hi Akashi,

Quoted 9 lines · Will Deacon
Quoted 7 lines · Kees Cook
Quoted 3 lines

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Quoted 6 lines

I took a quick look at the current implementation of ptrace. ptrace(PTRACE_GETREGSET/SETREGSET), eventually gpr_get/set(), handles only 'struct user_pt_regs', and we have no way to modify orig_x0 nor syscallno in 'struct pt_regs' directly.
So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

Anybody else have an opinion about this?

Will

#13 lore raw
Quoted 28 lines · Will Deacon
Quoted 1 line · AKASHI Takahiro

Hi Will,

Hi Akashi,

Quoted 9 lines · Will Deacon
Quoted 7 lines · Kees Cook
Quoted 3 lines

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Quoted 6 lines

I took a quick look at the current implementation of ptrace. ptrace(PTRACE_GETREGSET/SETREGSET), eventually gpr_get/set(), handles only 'struct user_pt_regs', and we have no way to modify orig_x0 nor syscallno in 'struct pt_regs' directly.
So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

Quoted 6 lines

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

w8 is a real register, right? On x86, at least orig_ax isn't a real register, so it's quite unlikely to conflict with hardware stuff.

On x86, the "user_struct" thing has nothing to do with any real kernel data structure, so it's extensible. Can you just add syscallno to it?

--Andy

#14 lore raw
Quoted 29 lines · Andy Lutomirski
Quoted 28 lines · Will Deacon
Quoted 9 lines · AKASHI Takahiro
Quoted 7 lines · Will Deacon
Quoted 3 lines · Kees Cook

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Quoted 6 lines

I took a quick look at the current implementation of ptrace. ptrace(PTRACE_GETREGSET/SETREGSET), eventually gpr_get/set(), handles only 'struct user_pt_regs', and we have no way to modify orig_x0 nor syscallno in 'struct pt_regs' directly.
So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

Quoted 5 lines
Quoted 2 lines

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

w8 is a real register, right? On x86, at least orig_ax isn't a real register, so it's quite unlikely to conflict with hardware stuff.

Yeah, w8 is the hardware register which the Linux ABI uses for the system call number. I was thinking We could allow the debugger/tracer to update the syscall number by updating that register, or do you see an issue with that? (other than tying us to the current ABI).

Quoted 2 lines

On x86, the "user_struct" thing has nothing to do with any real kernel data structure, so it's extensible. Can you just add syscallno to it?

I'm really not keen on changing user-facing structures like that. For example, KVM embeds user_pt_regs into kvm_regs.

We can add a new ptrace request if we have to.

Will

#15 lore raw
Quoted 33 lines · Will Deacon
Quoted 32 lines · Andy Lutomirski
Quoted 30 lines · Will Deacon
Quoted 9 lines · AKASHI Takahiro
Quoted 7 lines · Will Deacon
Quoted 3 lines · Kees Cook

What's the state of seccomp on arm64? I saw a series back in March, but nothing since then? It looked complete, but I haven't set up a test environment yet to verify.

I think Akashi was going to repost `real soon now' so we can include them for 3.17. He missed the merge window last time around.

Quoted 6 lines

I took a quick look at the current implementation of ptrace. ptrace(PTRACE_GETREGSET/SETREGSET), eventually gpr_get/set(), handles only 'struct user_pt_regs', and we have no way to modify orig_x0 nor syscallno in 'struct pt_regs' directly.
So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

Quoted 7 lines
Quoted 2 lines

w8 is a real register, right? On x86, at least orig_ax isn't a real register, so it's quite unlikely to conflict with hardware stuff.

Yeah, w8 is the hardware register which the Linux ABI uses for the system call number. I was thinking We could allow the debugger/tracer to update the syscall number by updating that register, or do you see an issue with that? (other than tying us to the current ABI).

Not immediately, but I'm not super-familiar with ptrace.

Is w8 clobbered or otherwise changed by syscalls? Using w8 for this has the odd effect that tracers can't force a return with a specific value of w8 without executing the corresponding syscall. If that's a meaningful limitation, then presumably some other channel should be used.

Quoted 6 lines
Quoted 3 lines

On x86, the "user_struct" thing has nothing to do with any real kernel data structure, so it's extensible. Can you just add syscallno to it?

I'm really not keen on changing user-facing structures like that. For example, KVM embeds user_pt_regs into kvm_regs.

Fair enough.

Quoted 4 lines

We can add a new ptrace request if we have to.

Will

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#16 lore raw
Quoted 29 lines · Andy Lutomirski
Quoted 20 lines · Will Deacon
Quoted 19 lines · Andy Lutomirski
Quoted 17 lines · Will Deacon
Quoted 2 lines · AKASHI Takahiro

So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

Quoted 7 lines
Quoted 2 lines

w8 is a real register, right? On x86, at least orig_ax isn't a real register, so it's quite unlikely to conflict with hardware stuff.

Yeah, w8 is the hardware register which the Linux ABI uses for the system call number. I was thinking We could allow the debugger/tracer to update the syscall number by updating that register, or do you see an issue with that? (other than tying us to the current ABI).

Quoted 7 lines

Not immediately, but I'm not super-familiar with ptrace.

Is w8 clobbered or otherwise changed by syscalls? Using w8 for this has the odd effect that tracers can't force a return with a specific value of w8 without executing the corresponding syscall. If that's a meaningful limitation, then presumably some other channel should be used.

Hmm, that's true. Currently w8 is preserved across a syscall by the kernel, so it would be pretty bizarre for somebody to try and modify it but I guess they could do it if they wanted to. However, they could just as easily modify it on the syscall return path and have the same effect...

Furthermore, glibc unconditionally emits a mov into w8 prior to the svc instruction, so from a user's perspective that register always contains the system call number.

FWIW: the role of w8 in the PCS is `Indirect result location register', so I'd expect it to be saved across the syscall anyway.

Will

#17 lore raw
Quoted 38 lines · Will Deacon
Quoted 29 lines · Andy Lutomirski
Quoted 20 lines · Will Deacon
Quoted 19 lines · Andy Lutomirski
Quoted 17 lines · Will Deacon
Quoted 2 lines · AKASHI Takahiro

So it seems to me that we can't change a system call by ptrace(). Do I misunderstand anything?

No, it looks like you have a point here. I don't think userspace has any business with orig_x0, but changing syscallno is certainly useful. I can think of two ways to fix this:

(1) Updating syscallno based on w8, but this ties us to the current ABI and could get messy if this register changes in the future.

(2) Adding a PTRACE_SET_SYSCALL request, like we have for arch/arm/, but that means adding arch-specific stuff to arch_ptrace (which currently goes straight to ptrace_request on arm64).

It looks like x86 uses orig_ax, which I *think* means we would go with
(1) above if we followed their lead.

Quoted 7 lines
Quoted 2 lines

w8 is a real register, right? On x86, at least orig_ax isn't a real register, so it's quite unlikely to conflict with hardware stuff.

Yeah, w8 is the hardware register which the Linux ABI uses for the system call number. I was thinking We could allow the debugger/tracer to update the syscall number by updating that register, or do you see an issue with that? (other than tying us to the current ABI).

Quoted 7 lines

Not immediately, but I'm not super-familiar with ptrace.

Is w8 clobbered or otherwise changed by syscalls? Using w8 for this has the odd effect that tracers can't force a return with a specific value of w8 without executing the corresponding syscall. If that's a meaningful limitation, then presumably some other channel should be used.

Quoted 8 lines

Hmm, that's true. Currently w8 is preserved across a syscall by the kernel, so it would be pretty bizarre for somebody to try and modify it but I guess they could do it if they wanted to. However, they could just as easily modify it on the syscall return path and have the same effect...

Furthermore, glibc unconditionally emits a mov into w8 prior to the svc instruction, so from a user's perspective that register always contains the system call number.

That means that, if someone uses a seccomp trace action to skip or emulate a syscall by writing -1 to w8, then user code will see an unexpected -1 in w8. I don't know how much this matters.

Quoted 5 lines

FWIW: the role of w8 in the PCS is `Indirect result location register', so I'd expect it to be saved across the syscall anyway.

Will

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC