Skip to contentSkip to conversation

ptrace: is_syscall_success: Add syscall return code handling for compat task

PATCH In review He Zhe 3 participants 7 messages Apr 14, 2021 – Apr 15, 2021 ptrace support

lore
Share & tools
lore

He Zhe

author
#0 lore raw

PATCH 2/2 ptrace: is_syscall_success: Add syscall return code handling for compat task

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

type=SYSCALL msg=audit(1611110715.887:582): arch=40000028 syscall=322 success=yes exit=4294967283

This patch forces the u32->s32->s64 for compat tasks.

---
 include/linux/ptrace.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/ptrace.h +3 -1
index b5ebf6c01292..bc3056fff8a6 100644
@@ -260,7 +260,9 @@ static inline void ptrace_release_task(struct task_struct *task)
  * is an error value.  On some systems like ia64 and powerpc they have different
  * indicators of success/failure and must define their own.
  */
-#define is_syscall_success(regs) (!IS_ERR_VALUE((unsigned long)(regs_return_value(regs))))
+#define is_syscall_success(regs) (!IS_ERR_VALUE(is_compat_task() ? \
+	(unsigned long)(s64)(s32)(regs_return_value(regs)) : \
+	(unsigned long)(regs_return_value(regs))))
 #endif
 
 /*
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Add audit maintainers...

Quoted 6 lines · He Zhe

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

Sorry, can understand. At least on x86_64 even the 32-bit syscall returns long, not u32.

Hmm. And afaics on x86 is_compat_task() is only defined if !CONFIG_COMPAT, so this patch looks wrong anyway.

Oleg.

Quoted 28 lines

type=SYSCALL msg=audit(1611110715.887:582): arch=40000028 syscall=322 success=yes exit=4294967283

This patch forces the u32->s32->s64 for compat tasks.

---
 include/linux/ptrace.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/ptrace.h +3 -1
index b5ebf6c01292..bc3056fff8a6 100644
@@ -260,7 +260,9 @@ static inline void ptrace_release_task(struct task_struct *task)
  * is an error value.  On some systems like ia64 and powerpc they have different
  * indicators of success/failure and must define their own.
  */
-#define is_syscall_success(regs) (!IS_ERR_VALUE((unsigned long)(regs_return_value(regs))))
+#define is_syscall_success(regs) (!IS_ERR_VALUE(is_compat_task() ? \
+	(unsigned long)(s64)(s32)(regs_return_value(regs)) : \
+	(unsigned long)(regs_return_value(regs))))
 #endif
 
 /*
Signature · 1 line
2.17.1
#2 lore raw

From: Oleg Nesterov

Quoted 12 lines

Sent: 14 April 2021 16:08

Add audit maintainers...

Quoted 6 lines · He Zhe

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

Quoted 5 lines

Sorry, can understand. At least on x86_64 even the 32-bit syscall returns long, not u32.

Hmm. And afaics on x86 is_compat_task() is only defined if !CONFIG_COMPAT, so this patch looks wrong anyway.

And, as with the other patch a x64_64 64bit process can make both types of 32bit system call - so it needs to depend on the system call entry type not any type of the task.

David

Quoted 27 lines

Oleg.

Quoted 24 lines

type=SYSCALL msg=audit(1611110715.887:582): arch=40000028 syscall=322 success=yes exit=4294967283

This patch forces the u32->s32->s64 for compat tasks.

---
 include/linux/ptrace.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/ptrace.h +3 -1
index b5ebf6c01292..bc3056fff8a6 100644
@@ -260,7 +260,9 @@ static inline void ptrace_release_task(struct task_struct *task)
  * is an error value.  On some systems like ia64 and powerpc they have different
  * indicators of success/failure and must define their own.
  */
-#define is_syscall_success(regs) (!IS_ERR_VALUE((unsigned long)(regs_return_value(regs))))
+#define is_syscall_success(regs) (!IS_ERR_VALUE(is_compat_task() ? \
+	(unsigned long)(s64)(s32)(regs_return_value(regs)) : \
+	(unsigned long)(regs_return_value(regs))))
 #endif
 
Quoted 4 lines
Quoted 4 lines
include/linux/ptrace.h +0 -1
 /*
--

2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

maintainer
#3 lore raw
Quoted 17 lines · David Laight

From: Oleg Nesterov

Quoted 15 lines

Sent: 14 April 2021 16:08

Add audit maintainers...

Quoted 6 lines · He Zhe

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

Sorry, can understand. At least on x86_64 even the 32-bit syscall returns long, not u32.

Quoted 6 lines
Quoted 2 lines

Hmm. And afaics on x86 is_compat_task() is only defined if !CONFIG_COMPAT, so this patch looks wrong anyway.

And, as with the other patch a x64_64 64bit process can make both types of 32bit system call - so it needs to depend on the system call entry type not any type of the task.

I don't understand... but iirc is_compat_task() used to check TS_COMPAT and this is what we need to detect the 32-bit syscall. But it looks deprecated, I think in_compat_syscall() should be used instead.

But this doesn't matter, I still can't understand the problem.

Oleg.

#4 lore raw

From: Oleg Nesterov

Quoted 23 lines

Sent: 14 April 2021 17:56

Quoted 20 lines · David Laight

From: Oleg Nesterov

Quoted 17 lines

Sent: 14 April 2021 16:08

Add audit maintainers...

Quoted 6 lines · He Zhe

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

Sorry, can understand. At least on x86_64 even the 32-bit syscall returns long, not u32.

Hmm. And afaics on x86 is_compat_task() is only defined if !CONFIG_COMPAT, so this patch looks wrong anyway.

Quoted 6 lines
Quoted 3 lines

And, as with the other patch a x64_64 64bit process can make both types of 32bit system call - so it needs to depend on the system call entry type not any type of the task.

I don't understand... but iirc is_compat_task() used to check TS_COMPAT and this is what we need to detect the 32-bit syscall.

Not on X86_64.
The syscall entry code sets the type on the current system call entry. So a process that is created as a 64bit process can make both i386 and x32 system calls as well as normal 64bit ones.

Quoted 2 lines

But it looks deprecated,
I think in_compat_syscall() should be used instead.

That does the right checks on x86.
Most code doesn't need to know about the subtle difference between normal 32bit calls and x32 ones.

Quoted 1 line

But this doesn't matter, I still can't understand the problem.

Dunno, I only know the 'fix' is borked.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#5 lore raw
In reply to Oleg Nesterov
Quoted 17 lines · Oleg Nesterov
Quoted 16 lines · David Laight

From: Oleg Nesterov

Quoted 15 lines

Sent: 14 April 2021 16:08

Add audit maintainers...

Quoted 5 lines · He Zhe

When 32-bit userspace application is running on 64-bit kernel, the 32-bit syscall return code would be changed from u32 to u64 in regs_return_value and then changed to s64. Hence the negative return code would be treated as a positive number and results in a non-error in, for example, audit like below.

Sorry, can understand. At least on x86_64 even the 32-bit syscall returns long, not u32.

Hmm. And afaics on x86 is_compat_task() is only defined if !CONFIG_COMPAT, so this patch looks wrong anyway.

Quoted 8 lines
Quoted 3 lines

And, as with the other patch a x64_64 64bit process can make both types of 32bit system call - so it needs to depend on the system call entry type not any type of the task.

I don't understand... but iirc is_compat_task() used to check TS_COMPAT and this is what we need to detect the 32-bit syscall. But it looks deprecated, I think in_compat_syscall() should be used instead.

But this doesn't matter, I still can't understand the problem.

Sorry for not enough clarification.

This was found on an arm64 kernel running with 32-bit user-space application. The arm64 version of regs_return_value returns unsigned long.

static inline unsigned long regs_return_value(struct pt_regs *regs) {
        return regs->regs[0];
}

But when the syscall fails, with -13 in my case, the return code has been saved as a 32 bit long negative number, 0x00000000FFFFFFF3, in regs[0] by the time regs_return_value gets called in audit_syscall_exit.

Then in audit_syscall_exit, the return value of regs_return_value is changed to a 64 bit signed long, from when on it is treated as a positive number.

Similarly in is_syscall_success, 0x00000000FFFFFFF3 would be out of error number range, resulting in a "success".

These two patches are to do the sign extension.

David, thanks, is_compat_syscall should be the right one to use. I didn't notice the difference between is_compat_syscall and is_compat_task and thought is_compat_task would be harmless to other architectures.

Zhe

Quoted 3 lines

Oleg.

maintainer
#6 lore raw
Quoted 10 lines · He Zhe
Quoted 3 lines · Oleg Nesterov

I think in_compat_syscall() should be used instead.

But this doesn't matter, I still can't understand the problem.

Sorry for not enough clarification.

This was found on an arm64 kernel running with 32-bit user-space application.

OK, but then I think you should add the arm64 version of is_syscall_success() into arch/arm4/include/asm/ptrace.h and do not touch the generic version ?

Something like arch/arm64/include/asm/syscall.h:syscall_get_error() which uses is_compat_thread(). Perhaps you can even do

	#define is_syscall_success(regs)	\
		(syscall_get_error(current, regs) == 0)

Oleg.