Skip to contentSkip to conversation

futex: Set USER_DS for the futex_detect_cmpxchg() test

PATCH In review Andy Lutomirski 6 participants 17 messages Sep 28, 2018 – Sep 29, 2018 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 futex: Set USER_DS for the futex_detect_cmpxchg() test

futex_detect_cmpxchg() checks whether cmpxchg is available by trying it on the NULL pointer and seeing what the error code is (EFAULT vs ENOSYS). This happens with KERNEL_DS set, which is impolite: while the NULL *user* pointer is definitely invalid when there is no user program running, the NULL *kernel* pointer seems more like a programming error than a safe place to do an intentionally-failing access. An upcoming hardening series I'm working on causes the existing code to OOPS, because it considers any failed uaccess with KERNEL_DS to be a sign of a bug.

Explicitly set USER_DS to avoid this problem.

Cc Martin Schwidefsky <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Finn Thain <[email protected]>
Cc Geert Uytterhoeven <[email protected]>

---

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

 kernel/futex.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/futex.c +4 -0
index 11fc3bb456d6..16bd3e72602a 100644
@@ -3593,6 +3593,7 @@ static void __init futex_detect_cmpxchg(void)
 {
 #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
 	u32 curval;
+	mm_segment_t old_seg;
 
 	/*
 	 * This will fail and we want it. Some arch implementations do
@@ -3604,8 +3605,11 @@ static void __init futex_detect_cmpxchg(void)
 	 * implementation, the non-functional ones will return
 	 * -ENOSYS.
 	 */
+	old_seg = get_fs();
+	set_fs(USER_DS);
 	if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
 		futex_cmpxchg_enabled = 1;
+	set_fs(old_seg);
 #endif
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 10 lines · Andy Lutomirski

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

I'm not entirely sure. Have to dig into the details. I assume S390 just can set it though.

Thanks,

tglx

Quoted 31 lines
 kernel/futex.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/futex.c +4 -0
index 11fc3bb456d6..16bd3e72602a 100644
@@ -3593,6 +3593,7 @@ static void __init futex_detect_cmpxchg(void)
 {
 #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
 	u32 curval;
+	mm_segment_t old_seg;
 
 	/*
 	 * This will fail and we want it. Some arch implementations do
@@ -3604,8 +3605,11 @@ static void __init futex_detect_cmpxchg(void)
 	 * implementation, the non-functional ones will return
 	 * -ENOSYS.
 	 */
+	old_seg = get_fs();
+	set_fs(USER_DS);
 	if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
 		futex_cmpxchg_enabled = 1;
+	set_fs(old_seg);
 #endif
 }
 
Signature · 1 line
2.17.1
#2 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Andy Lutomirski

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

Quoted 6 lines
Quoted 3 lines

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

I'm not entirely sure. Have to dig into the details. I assume S390 just can set it though.

x86 as well. It's supported from 486 onwards and we ripped out 386 years ago.

Thanks,

tglx

#3 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Andy Lutomirski

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

Quoted 6 lines
Quoted 3 lines

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

I'm not entirely sure. Have to dig into the details. I assume S390 just can set it though.

Not sure. My "[PATCH] futex: Switch to USER_DS for futex test"
(https://www.spinics.net/lists/stable/msg28846.html), which is basically the same
as this patch, broke s390, so it was never merged.

See "[BUG -next] "futex: switch to USER_DS for futex test" breaks s390"
(https://www.spinics.net/lists/linux-next/msg27902.html)

Heiko said:

| Martin and I discussed this today and we will change the s390 code so that
| it will also survive very early USER_DS accesses (without valid current->mm)
| since we also discovered a couple of other oddities in our code.

I don't know if that has happened, and whether it would work on s390 now.

Quoted 13 lines
Quoted 13 lines
 kernel/futex.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/futex.c +1 -0
index 11fc3bb456d6..16bd3e72602a 100644
@@ -3593,6 +3593,7 @@ static void __init futex_detect_cmpxchg(void)
 {
 #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
      u32 curval;
+     mm_segment_t old_seg;
 
Quoted 13 lines

kernel/futex.c static void __init futex_detect_cmpxchg(void) · line 3614

Quoted 13 lines
kernel/futex.c +3 -0
      /*
       * This will fail and we want it. Some arch implementations do
@@ -3604,8 +3605,11 @@ static void __init futex_detect_cmpxchg(void)
       * implementation, the non-functional ones will return
       * -ENOSYS.
       */
+     old_seg = get_fs();
+     set_fs(USER_DS);
      if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
              futex_cmpxchg_enabled = 1;
+     set_fs(old_seg);
 #endif
 }

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw
Quoted 25 lines · Geert Uytterhoeven
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Andy Lutomirski

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

Quoted 6 lines
Quoted 3 lines

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

I'm not entirely sure. Have to dig into the details. I assume S390 just can set it though.

Not sure. My "[PATCH] futex: Switch to USER_DS for futex test"
(https://www.spinics.net/lists/stable/msg28846.html), which is basically the same
as this patch, broke s390, so it was never merged.

See "[BUG -next] "futex: switch to USER_DS for futex test" breaks s390"
(https://www.spinics.net/lists/linux-next/msg27902.html)

Quoted 6 lines

Heiko said:

| Martin and I discussed this today and we will change the s390 code so that
| it will also survive very early USER_DS accesses (without valid current->mm)
| since we also discovered a couple of other oddities in our code.

I don't know if that has happened, and whether it would work on s390 now.

Duh yes, forgot about that one. But as S390 always has cmpxchg it simply can set HAVE_FUTEX_CMPXCHG which avoids the check completely.

Surely they want to fix the other oddities or have done so already :)

Thanks,

tglx

#5 lore raw
In reply to Geert Uytterhoeven
Quoted 26 lines · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

Hi Thomas,

Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Andy Lutomirski

I have a couple questions here:

- Is this actually okay on all architectures? That is, are there cases where we'll screw up if we fail a USER_DS access this early? s390 stands out as the obvious special case (where USER_DS is not than just a subset of KERNEL_DS), but s390 opts out.

Quoted 6 lines
Quoted 3 lines

- Why doesn't x86 set HAVE_FUTEX_CMPXCHG? Or do we still support some 32-bit configurations that don't have cmpxchg and don't know about it at compile time?

I'm not entirely sure. Have to dig into the details. I assume S390 just can set it though.

Not sure. My "[PATCH] futex: Switch to USER_DS for futex test"
(https://www.spinics.net/lists/stable/msg28846.html), which is basically the same
as this patch, broke s390, so it was never merged.

See "[BUG -next] "futex: switch to USER_DS for futex test" breaks s390"
(https://www.spinics.net/lists/linux-next/msg27902.html)

Quoted 6 lines

Heiko said:

| Martin and I discussed this today and we will change the s390 code so that
| it will also survive very early USER_DS accesses (without valid current->mm)
| since we also discovered a couple of other oddities in our code.

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
#6 lore raw
Quoted 23 lines · Martin Schwidefsky
Quoted 1 line · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Quoted 3 lines

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Duh. grep would have told me. -ENOTENOUGHCOFFEE

Thanks,

tglx

#7 lore raw
Quoted 28 lines · Thomas Gleixner
Quoted 25 lines · Martin Schwidefsky
Quoted 1 line · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Quoted 7 lines
Quoted 3 lines

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Duh. grep would have told me. -ENOTENOUGHCOFFEE

There’s another way to skin this cat: keep KERNEL_DS but pass a valid pointer. I don’t suppose you remember why you didn’t do that?

#8 lore raw
Quoted 30 lines · Andy Lutomirski
Quoted 30 lines · Thomas Gleixner
Quoted 28 lines · Martin Schwidefsky
Quoted 1 line · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Quoted 5 lines
Quoted 3 lines

Duh. grep would have told me. -ENOTENOUGHCOFFEE

There’s another way to skin this cat: keep KERNEL_DS but pass a valid pointer. I don’t suppose you remember why you didn’t do that?

IIRC, there was an issue with extra checks in some architectures when you handed in a kernel address spitting warnings or such. That's probably gone by now, but I can't tell for sure. At least the requirement to do runtime detection for x86 is gone. Don't know if any other architecture still has it.

Thanks,

tglx

#9 lore raw
In reply to Andy Lutomirski
Quoted 30 lines · On Fri, 28 Sep 2018 07:11:44 -0700 Andy Lutomirski <[email protected]> wrote:
Quoted 30 lines · Thomas Gleixner
Quoted 28 lines · Martin Schwidefsky
Quoted 1 line · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Quoted 6 lines
Quoted 3 lines

Duh. grep would have told me. -ENOTENOUGHCOFFEE

There’s another way to skin this cat: keep KERNEL_DS but pass a valid pointer. I don’t suppose you remember why you didn’t do that?

No, I don't remember. To use a valid kernel pointer with KERNEL_DS and then test for == 0 (vs -ENOSYS) imho should work.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
#10 lore raw
Quoted 38 lines · Martin Schwidefsky
Quoted 34 lines · On Fri, 28 Sep 2018 07:11:44 -0700 Andy Lutomirski <[email protected]> wrote:
Quoted 33 lines · Thomas Gleixner
Quoted 25 lines · Martin Schwidefsky
Quoted 1 line · On Fri, 28 Sep 2018 09:12:10 +0200 Geert Uytterhoeven <[email protected]>…

I don't know if that has happened, and whether it would work on s390 now.

commit 03b8c7b623c80af264c4c8d6111e5c6289933666

Author: Heiko Carstens <[email protected]>
Date:   Sun Mar 2 13:09:47 2014 +0100

futex: Allow architectures to skip futex_atomic_cmpxchg_inatomic() test

If an architecture has futex_atomic_cmpxchg_inatomic() implemented and there is no runtime check necessary, allow to skip the test within futex_init().

This allows to get rid of some code which would always give the same result, and also allows the compiler to optimize a couple of if statements away.

Quoted 3 lines

Heiko created the CONFIG_HAVE_FUTEX_CMPXCHG to get around this issue. We just skip the runtime check as well as arc, m68k and sh. Not sure about xtensa, the set it config option only for !MMU.

Duh. grep would have told me. -ENOTENOUGHCOFFEE

Quoted 6 lines
Quoted 2 lines

There’s another way to skin this cat: keep KERNEL_DS but pass a valid pointer. I don’t suppose you remember why you didn’t do that?

No, I don't remember. To use a valid kernel pointer with KERNEL_DS and then test for == 0 (vs -ENOSYS) imho should work.

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

#11 lore raw
Quoted 6 lines · Andy Lutomirski
Quoted 5 lines · Martin Schwidefsky
Quoted 2 lines · On Fri, 28 Sep 2018 07:11:44 -0700 Andy Lutomirski <[email protected]> wrote:

There’s another way to skin this cat: keep KERNEL_DS but pass a valid pointer. I don’t suppose you remember why you didn’t do that?

Quoted 7 lines
Quoted 3 lines

No, I don't remember. To use a valid kernel pointer with KERNEL_DS and then test for == 0 (vs -ENOSYS) imho should work.

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

That makes sense.

Thanks,

tglx

#12 lore raw
In reply to Andy Lutomirski
Quoted 3 lines · Andy Lutomirski

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

In case of xtensa availability of cmpxchg is known at build time.

Signature · 2 lines
Thanks.
-- Max
#13 lore raw
Quoted 6 lines · Max Filippov
Quoted 3 lines · Andy Lutomirski

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

In case of xtensa availability of cmpxchg is known at build time.

That makes it even simpler. Could you provide a patch which selects CONFIG_HAVE_FUTEX_CMPXCHG for the right set of CPUs please?

Thanks,

tglx

#14 lore raw
Quoted 11 lines · Thomas Gleixner
Quoted 8 lines · Max Filippov
Quoted 5 lines · Andy Lutomirski

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

Quoted 6 lines
Quoted 1 line

In case of xtensa availability of cmpxchg is known at build time.

That makes it even simpler. Could you provide a patch which selects CONFIG_HAVE_FUTEX_CMPXCHG for the right set of CPUs please?

I think that’s the wrong approach, since it won’t cover mips. How about adding this to mips and xtensa only:

static inline void arch_have_futex_cmpxchg(void) {...}; #define arch_have_futex_cmpxchg arch_have_futex_cmpxchg

And getting rid of the config option.

#15 lore raw
Quoted 18 lines · Andy Lutomirski
Quoted 11 lines · Thomas Gleixner
Quoted 8 lines · Max Filippov
Quoted 5 lines · Andy Lutomirski

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

Quoted 6 lines
Quoted 1 line

In case of xtensa availability of cmpxchg is known at build time.

That makes it even simpler. Could you provide a patch which selects CONFIG_HAVE_FUTEX_CMPXCHG for the right set of CPUs please?

Quoted 6 lines

I think that’s the wrong approach, since it won’t cover mips. How about adding this to mips and xtensa only:

static inline void arch_have_futex_cmpxchg(void) {...}; #define arch_have_futex_cmpxchg arch_have_futex_cmpxchg

And getting rid of the config option.

I'd rather do that, given that defining Kconfig entries that describe parts of xtensa configuration is somewhat awkward and redundant.

Signature · 2 lines
Thanks.
-- Max
#16 lore raw
Quoted 19 lines · Max Filippov
Quoted 18 lines · Andy Lutomirski
Quoted 9 lines · Thomas Gleixner
Quoted 8 lines · Max Filippov
Quoted 5 lines · Andy Lutomirski

There may be a much nicer solution. Unless I missed something, only mips and xtensa even have the possibility of cmpxchg being missing. We could just make those arches supply a futex-detecting helper.

Quoted 6 lines
Quoted 1 line

In case of xtensa availability of cmpxchg is known at build time.

That makes it even simpler. Could you provide a patch which selects CONFIG_HAVE_FUTEX_CMPXCHG for the right set of CPUs please?

I think that’s the wrong approach, since it won’t cover mips. How about adding this to mips and xtensa only:

Quoted 7 lines
Quoted 4 lines

static inline void arch_have_futex_cmpxchg(void) {...}; #define arch_have_futex_cmpxchg arch_have_futex_cmpxchg

And getting rid of the config option.

I'd rather do that, given that defining Kconfig entries that describe parts of xtensa configuration is somewhat awkward and redundant.

Fair enough.

Thanks,

tglx