Skip to contentSkip to conversation

Futexes are broken on (at least) MIPS64/n32

PATCH Petr Malat 1 participant 1 message Jul 29, 2015

Subsystems (2)
lore
Share & tools
lore

Petr Malat

author
#0 lore raw

Hi All,
futexes doesn't work correctly on (at least) MIPS64/n32 if the value argument is not properly sign extended (glibc doesn't sign extend it properly).
According to the following discussion kernel shall be able to handle such a case (https://lkml.org/lkml/2007/6/4/456):

Quoted 6 lines · On Mon, 4 Jun 2007 17:04:12 -0700 "H. Peter Anvin" wrote:
Quoted 5 lines · On Mon, 4 Jun 2007 17:26:27 EST David Miller wrote:
Quoted 4 lines · On Mon, 4 Jun 2007 20:56:57 UTC Joseph S. Myers wrote:

What should the kernel syscall ABI be in such cases (any case where the syscall implementations expect arguments narrower than registers, so mainly 32-bit arguments on 64-bit platforms)? There are two obvious possibilities:

Quoted 8 lines
Quoted 5 lines

In general we've taken the stance that the syscall dispatch should create the proper calling environment for C code implementing the system calls, and this thus means properly sign and zero extending the arguments as expected by the C calling convention.

This is, in fact, rather fundamental (some ABIs don't require sign or zero extension, e.g. x86-64); otherwise libc's job becomes a whole lot harder.

Seems there isn't a generic solution addressing this in 2.6.32.67, 3.2.69 and 3.4.108 kernels. On newer kernels it's addressed by COMPAT_SYSCALL_DEFINE infrastructure.

For futexes it can be solved by cherry-picking the following commits (patch against 2.6.32.67 is attached):
468366138850f20543f1d4878028900672b23dae 90caf58dad77a4021e9dade5d051756cea2a2cd7
but as the issue can have potential security impact (see CVE-2009-0029), all syscalls shall be fixed, probably.

I've originally reported this issue to the glibc bugzilla:
https://sourceware.org/bugzilla/show_bug.cgi?id=18726
where you can find an example demonstrating the issue.

Cheers,
Petr

Cherry-pick
468366138850f20543f1d4878028900672b23dae 90caf58dad77a4021e9dade5d051756cea2a2cd7
to make futexes working even if the value argument hasn't been properly sign extended (glibc doesn't sign extend arguments on MIPS64/n32, see glibc bug #18726).

arch/s390/include/asm/compat.h +3 -0
index 82c882c..e4d864c 100644
@@ -7,6 +7,9 @@
 #include <linux/sched.h>
 #include <linux/thread_info.h>
 
+#define __TYPE_IS_PTR(t) (!__builtin_types_compatible_p(typeof(0?(t)0:0ULL), u64))
+#define __SC_DELOUSE(t,v) (t)(__TYPE_IS_PTR(t) ? ((v) & 0x7fffffff) : (v))
+
 #define PSW32_MASK_PER		0x40000000UL
 #define PSW32_MASK_DAT		0x04000000UL
 #define PSW32_MASK_IO		0x02000000UL
include/linux/compat.h +46 -0

52 lines not on this page · raw message

kernel/futex_compat.c +11 -9
index 2357165..c3e8e66 100644
@@ -10,6 +10,8 @@
 #include <linux/compat.h>
 #include <linux/nsproxy.h>
 #include <linux/futex.h>
+#include <linux/syscalls.h>
+#include <linux/compat.h>
 
 #include <asm/uaccess.h>
 
@@ -114,9 +116,9 @@ void compat_exit_robust_list(struct task_struct *curr)
 	}
 }
 
-asmlinkage long
-compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
-			   compat_size_t len)
+COMPAT_SYSCALL_DEFINE2(set_robust_list,
+		struct compat_robust_list_head __user *, head,
+		compat_size_t, len)
 {
 	if (!futex_cmpxchg_enabled)
 		return -ENOSYS;
@@ -129,9 +131,9 @@ compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
 	return 0;
 }
 
-asmlinkage long
-compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
-			   compat_size_t __user *len_ptr)
+COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
+			compat_uptr_t __user *, head_ptr,
+			compat_size_t __user *, len_ptr)
 {
 	struct compat_robust_list_head __user *head;
 	unsigned long ret;
@@ -170,9 +172,9 @@ err_unlock:
 	return ret;
 }
 
-asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
-		struct compat_timespec __user *utime, u32 __user *uaddr2,
-		u32 val3)
+COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
+		struct compat_timespec __user *, utime, u32 __user *, uaddr2,
+		u32, val3)
 {
 	struct timespec ts;
 	ktime_t t, *tp = NULL;