* [PATCH 3.19 v3 0/2] x86, mpx: Instruction decoder fixes and hardening
@ 2015-01-13 2:11 Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 1/2] x86, mpx: Short-circuit the instruction decoder for unexpected opcodes Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 2/2] x86: Enforce MAX_INSN_SIZE in the instruction decoder Andy Lutomirski
0 siblings, 2 replies; 3+ messages in thread
From: Andy Lutomirski @ 2015-01-13 2:11 UTC (permalink / raw)
To: x86, linux-kernel, Dave Hansen; +Cc: Masami Hiramatsu, Andy Lutomirski
Hi all-
Sorry, this is later than I had hoped to send this. This series has
three minor fixes for mpx and the instruction decoder.
Changes from v2:
- Dropped patch 1 (fixed in tip separately)
- Fixed comment typoes in patch 2 (noticed by Dave)
Changes from v1:
- Dropped the TIF_IA32 change -- let's defer that until at least 3.20.
- Fixed the MPX decode short-circuit. v1 was buggy.
- Patch 3 is new. It fixes a minor regression from the MPX work.
Andy Lutomirski (2):
x86, mpx: Short-circuit the instruction decoder for unexpected opcodes
x86: Enforce MAX_INSN_SIZE in the instruction decoder
arch/x86/lib/insn.c | 7 +++++++
arch/x86/mm/mpx.c | 25 ++++++++++++++++---------
2 files changed, 23 insertions(+), 9 deletions(-)
--
2.1.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 3.19 v3 1/2] x86, mpx: Short-circuit the instruction decoder for unexpected opcodes
2015-01-13 2:11 [PATCH 3.19 v3 0/2] x86, mpx: Instruction decoder fixes and hardening Andy Lutomirski
@ 2015-01-13 2:11 ` Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 2/2] x86: Enforce MAX_INSN_SIZE in the instruction decoder Andy Lutomirski
1 sibling, 0 replies; 3+ messages in thread
From: Andy Lutomirski @ 2015-01-13 2:11 UTC (permalink / raw)
To: x86, linux-kernel, Dave Hansen; +Cc: Masami Hiramatsu, Andy Lutomirski
This reduces the degree to which we're exposing the instruction decoder
to malicious user code at very little complexity cost.
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
---
arch/x86/mm/mpx.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/arch/x86/mm/mpx.c b/arch/x86/mm/mpx.c
index 67ebf5751222..a73004330732 100644
--- a/arch/x86/mm/mpx.c
+++ b/arch/x86/mm/mpx.c
@@ -230,6 +230,22 @@ static int mpx_insn_decode(struct insn *insn,
*/
if (!nr_copied)
return -EFAULT;
+
+ /*
+ * We only _really_ need to decode bndcl/bndcn/bndcu
+ * Error out on anything else. Check this before decoding the
+ * instruction to reduce our exposure to intentionally bad code
+ * to some extent. Note that this shortcut can incorrectly return
+ * -EINVAL instead of -EFAULT under some circumstances. This
+ * discrepancy has no effect.
+ */
+ if (nr_copied < 2)
+ goto bad_opcode;
+ if (buf[0] != 0x0f)
+ goto bad_opcode;
+ if (buf[1] != 0x1a && buf[1] != 0x1b)
+ goto bad_opcode;
+
insn_init(insn, buf, nr_copied, x86_64);
insn_get_length(insn);
/*
@@ -244,15 +260,6 @@ static int mpx_insn_decode(struct insn *insn,
return -EFAULT;
insn_get_opcode(insn);
- /*
- * We only _really_ need to decode bndcl/bndcn/bndcu
- * Error out on anything else.
- */
- if (insn->opcode.bytes[0] != 0x0f)
- goto bad_opcode;
- if ((insn->opcode.bytes[1] != 0x1a) &&
- (insn->opcode.bytes[1] != 0x1b))
- goto bad_opcode;
return 0;
bad_opcode:
--
2.1.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 3.19 v3 2/2] x86: Enforce MAX_INSN_SIZE in the instruction decoder
2015-01-13 2:11 [PATCH 3.19 v3 0/2] x86, mpx: Instruction decoder fixes and hardening Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 1/2] x86, mpx: Short-circuit the instruction decoder for unexpected opcodes Andy Lutomirski
@ 2015-01-13 2:11 ` Andy Lutomirski
1 sibling, 0 replies; 3+ messages in thread
From: Andy Lutomirski @ 2015-01-13 2:11 UTC (permalink / raw)
To: x86, linux-kernel, Dave Hansen; +Cc: Masami Hiramatsu, Andy Lutomirski
The instruction decoder used to assume that the input buffer was
exactly MAX_INSN_SIZE bytes long. Now that the input buffer has
variable length, even if the input buffer is longer than
MAX_INSN_SIZE, we should still reject instructions longer than
MAX_INSN_SIZE, since a real CPU will reject them even if they're
otherwise valid.
Other than potentially confusing some of the decoder sanity checks,
I'm not aware of any actual problems that omitting this check would
cause.
It's worth noting that MAX_INSN_SIZE is incorrectly set to 16. This
patch doesn't change that. I'll submit a fix for that later.
Fixes: 6ba48ff46f76 x86: Remove arbitrary instruction size limit in instruction decoder
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
---
arch/x86/lib/insn.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c
index 2480978b31cc..4dc8252b6454 100644
--- a/arch/x86/lib/insn.c
+++ b/arch/x86/lib/insn.c
@@ -52,6 +52,13 @@
*/
void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
{
+ /*
+ * Instructions longer than 15 bytes are invalid even if the
+ * input buffer is long enough to hold them.
+ */
+ if (buf_len > MAX_INSN_SIZE)
+ buf_len = MAX_INSN_SIZE;
+
memset(insn, 0, sizeof(*insn));
insn->kaddr = kaddr;
insn->end_kaddr = kaddr + buf_len;
--
2.1.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-01-13 2:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-13 2:11 [PATCH 3.19 v3 0/2] x86, mpx: Instruction decoder fixes and hardening Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 1/2] x86, mpx: Short-circuit the instruction decoder for unexpected opcodes Andy Lutomirski
2015-01-13 2:11 ` [PATCH 3.19 v3 2/2] x86: Enforce MAX_INSN_SIZE in the instruction decoder Andy Lutomirski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome