Skip to contentSkip to conversation

x86: correct fpu emulation access to ldt

PATCH Superseded Juergen Gross 2 participants 6 messages Aug 4, 2015 – Aug 6, 2015 x86

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: correct fpu emulation access to ldt

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Adapt the x86 fpu emulation to use that new structure.

---
 arch/x86/math-emu/fpu_entry.c   | 1 +
 arch/x86/math-emu/fpu_system.h  | 2 +-
 arch/x86/math-emu/get_address.c | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/math-emu/fpu_entry.c +1 -0
index f37e84a..97820c24 100644
@@ -30,6 +30,7 @@
 #include <asm/uaccess.h>
 #include <asm/traps.h>
 #include <asm/desc.h>
+#include <asm/mmu_context.h>
 #include <asm/user.h>
 #include <asm/fpu/internal.h>
 
arch/x86/math-emu/fpu_system.h +1 -1
index 9ccecb6..7c7af3a 100644
@@ -18,7 +18,7 @@
 
 /* s is always from a cpu register, and the cpu does bounds checking
  * during register load --> no further bounds checks needed */
-#define LDT_DESCRIPTOR(s)	(((struct desc_struct *)current->mm->context.ldt)[(s) >> 3])
+#define LDT_DESCRIPTOR(s)	(current->mm->context.ldt->entries[(s) >> 3])
 #define SEG_D_SIZE(x)		((x).b & (3 << 21))
 #define SEG_G_BIT(x)		((x).b & (1 << 23))
 #define SEG_GRANULARITY(x)	(((x).b & (1 << 23)) ? 4096 : 1)
arch/x86/math-emu/get_address.c +1 -0
index 6ef5e99..4f83d09 100644
@@ -21,6 +21,7 @@
 
 #include <asm/uaccess.h>
 #include <asm/desc.h>
+#include <asm/mmu_context.h>
 
 #include "fpu_system.h"
 #include "exception.h"
Signature · 1 line
2.1.4
#1 lore raw
Quoted 6 lines · Juergen Gross

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Adapt the x86 fpu emulation to use that new structure.

Whoops!

Does this need to Cc: stable?

Also, want to make it slightly fancier so we can drop the dependency on CONFIG_MODIFY_LDT_SYSCALL?

--Andy

#2 lore raw
Quoted 11 lines · Andy Lutomirski
Quoted 6 lines · Juergen Gross

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Adapt the x86 fpu emulation to use that new structure.

Whoops!

Does this need to Cc: stable?

Probably.

Quoted 2 lines

Also, want to make it slightly fancier so we can drop the dependency on CONFIG_MODIFY_LDT_SYSCALL?

Something like:

-#define LDT_DESCRIPTOR(s) (((struct desc_struct *)current->mm->context.ldt)[(s) >> 3]) +#ifdef CONFIG_MODIFY_LDT_SYSCALL
+#define LDT_DESCRIPTOR(s) (current->mm->context.ldt->entries[(s) >> 3]) +#else
+#define LDT_DESCRIPTOR(s) ((struct desc_struct){{{ .a = 0, .b = 0, }}}) +#endif

I'd need to specify the corresponding patch as a prerequisite for stable I guess? How to do this before it is picked by Linus?

Juergen

#3 lore raw
Quoted 27 lines · Juergen Gross
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Juergen Gross

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Quoted 8 lines
Quoted 3 lines

Adapt the x86 fpu emulation to use that new structure.

Whoops!

Does this need to Cc: stable?

Probably.

Quoted 2 lines

Also, want to make it slightly fancier so we can drop the dependency on CONFIG_MODIFY_LDT_SYSCALL?

Something like:

-#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

Review

Quoted 4 lines
(quoted diff) +4 -0
+#ifdef CONFIG_MODIFY_LDT_SYSCALL
+#define LDT_DESCRIPTOR(s) (current->mm->context.ldt->entries[(s) >> 3])
+#else
+#define LDT_DESCRIPTOR(s) ((struct desc_struct){{{ .a = 0, .b = 0, }}})

Careful! I think that akpm uses some ancient gcc version that can't compile that. Maybe have a global empty segment somewhere that this returns, or just ifdef out the two call sites.

Also, I don't believe this for a second:

/* s is always from a cpu register, and the cpu does bounds checking
* during register load --> no further bounds checks needed */
#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

"What the comment means is that s always came from a cpu register at some point in the recent past (assuming that no lazy segment save logic is in effect) and we cross our fingers and hope that we never end up accessing out of bounds if the LDT isn't the same as it was at the time of the fault we're handling."

Sigh.

Maybe the best approach would be to replace LDT_DESCRIPTOR with an actual function that returns a struct desc_struct. If it's out of bounds or !CONFIG_MODIFY_LDT_SYSCALL, return zeros. Otherwise return the descriptor.

Quoted 4 lines

+#endif

I'd need to specify the corresponding patch as a prerequisite for stable I guess? How to do this before it is picked by Linus?

Send a v2 with Cc: [email protected] # [commit hash you depend on]. Presumably Ingo will pick it up, not Linus.

--Andy

#4 lore raw
Quoted 50 lines · Andy Lutomirski
Quoted 27 lines · Juergen Gross
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Juergen Gross

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Quoted 8 lines
Quoted 3 lines

Adapt the x86 fpu emulation to use that new structure.

Whoops!

Does this need to Cc: stable?

Probably.

Quoted 2 lines

Also, want to make it slightly fancier so we can drop the dependency on CONFIG_MODIFY_LDT_SYSCALL?

Something like:

-#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

Review

Quoted 4 lines
(quoted diff) +4 -0
+#ifdef CONFIG_MODIFY_LDT_SYSCALL
+#define LDT_DESCRIPTOR(s) (current->mm->context.ldt->entries[(s) >> 3])
+#else
+#define LDT_DESCRIPTOR(s) ((struct desc_struct){{{ .a = 0, .b = 0, }}})

Careful! I think that akpm uses some ancient gcc version that can't compile that. Maybe have a global empty segment somewhere that this returns, or just ifdef out the two call sites.

Also, I don't believe this for a second:

/* s is always from a cpu register, and the cpu does bounds checking
* during register load --> no further bounds checks needed */
#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

"What the comment means is that s always came from a cpu register at some point in the recent past (assuming that no lazy segment save logic is in effect) and we cross our fingers and hope that we never end up accessing out of bounds if the LDT isn't the same as it was at the time of the fault we're handling."

Quoted 6 lines

Sigh.

Maybe the best approach would be to replace LDT_DESCRIPTOR with an actual function that returns a struct desc_struct. If it's out of bounds or !CONFIG_MODIFY_LDT_SYSCALL, return zeros. Otherwise return the descriptor.

Yeah, seems to be the better approach.

Quoted 8 lines
Quoted 5 lines

+#endif

I'd need to specify the corresponding patch as a prerequisite for stable I guess? How to do this before it is picked by Linus?

Send a v2 with Cc: [email protected] # [commit hash you depend on]. Presumably Ingo will pick it up, not Linus.

I know how to specify a prerequisite. I just wasn't sure which commit hash to use, as up to now I've only one from your tree and I guessed that wouldn't do it.

Juergen

#5 lore raw
Quoted 78 lines · Juergen Gross
Quoted 58 lines · Andy Lutomirski
Quoted 33 lines · Juergen Gross
Quoted 10 lines · Andy Lutomirski
Quoted 7 lines · Juergen Gross

Commit 14805442532c ("x86/ldt: Make modify_ldt synchronous") introduced a new struct ldt_struct anchored at mm->context.ldt.

Adapt the x86 fpu emulation to use that new structure.

Quoted 7 lines
Quoted 1 line

Whoops!

Does this need to Cc: stable?

Probably.

Quoted 2 lines

Also, want to make it slightly fancier so we can drop the dependency on CONFIG_MODIFY_LDT_SYSCALL?

Something like:

-#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

Review

Quoted 4 lines
(quoted diff) +4 -0
+#ifdef CONFIG_MODIFY_LDT_SYSCALL
+#define LDT_DESCRIPTOR(s) (current->mm->context.ldt->entries[(s) >> 3])
+#else
+#define LDT_DESCRIPTOR(s) ((struct desc_struct){{{ .a = 0, .b = 0, }}})

Careful! I think that akpm uses some ancient gcc version that can't compile that. Maybe have a global empty segment somewhere that this returns, or just ifdef out the two call sites.

Also, I don't believe this for a second:

/* s is always from a cpu register, and the cpu does bounds checking
* during register load --> no further bounds checks needed */
#define LDT_DESCRIPTOR(s) (((struct desc_struct
*)current->mm->context.ldt)[(s) >> 3])

"What the comment means is that s always came from a cpu register at some point in the recent past (assuming that no lazy segment save logic is in effect) and we cross our fingers and hope that we never end up accessing out of bounds if the LDT isn't the same as it was at the time of the fault we're handling."

Quoted 6 lines

Sigh.

Maybe the best approach would be to replace LDT_DESCRIPTOR with an actual function that returns a struct desc_struct. If it's out of bounds or !CONFIG_MODIFY_LDT_SYSCALL, return zeros. Otherwise return the descriptor.

Yeah, seems to be the better approach.

Quoted 7 lines
Quoted 5 lines

+#endif

I'd need to specify the corresponding patch as a prerequisite for stable I guess? How to do this before it is picked by Linus?

Quoted 7 lines
Quoted 2 lines

Send a v2 with Cc: [email protected] # [commit hash you depend on]. Presumably Ingo will pick it up, not Linus.

I know how to specify a prerequisite. I just wasn't sure which commit hash to use, as up to now I've only one from your tree and I guessed that wouldn't do it.

Gotcha. I thought it was a strange question, and I obviously misunderstood.

Use:

https://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?h=x86/urgent&id=37868fe113ff2ba814b3b4eb12df214df555f8dc

if you haven't spotted it yet.

--Andy