c47527a
From: Andy Lutomirski <luto@amacapital.net>
c47527a
Date: Thu, 4 Dec 2014 16:48:16 -0800
c47527a
Subject: [PATCH] x86/tls: Validate TLS entries to protect espfix
c47527a
c47527a
Installing a 16-bit RW data segment into the GDT defeats espfix.
c47527a
AFAICT this will not affect glibc, Wine, or dosemu at all.
c47527a
c47527a
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
c47527a
Acked-by: H. Peter Anvin <hpa@zytor.com>
c47527a
Cc: stable@vger.kernel.org
c47527a
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
c47527a
Cc: Linus Torvalds <torvalds@linux-foundation.org>
c47527a
Cc: security@kernel.org <security@kernel.org>
c47527a
Cc: Willy Tarreau <w@1wt.eu>
c47527a
Signed-off-by: Ingo Molnar <mingo@kernel.org>
c47527a
---
c47527a
 arch/x86/kernel/tls.c | 23 +++++++++++++++++++++++
c47527a
 1 file changed, 23 insertions(+)
c47527a
c47527a
diff --git a/arch/x86/kernel/tls.c b/arch/x86/kernel/tls.c
c47527a
index f7fec09e3e3a..e7650bd71109 100644
c47527a
--- a/arch/x86/kernel/tls.c
c47527a
+++ b/arch/x86/kernel/tls.c
c47527a
@@ -27,6 +27,21 @@ static int get_free_idx(void)
c47527a
 	return -ESRCH;
c47527a
 }
c47527a
 
c47527a
+static bool tls_desc_okay(const struct user_desc *info)
c47527a
+{
c47527a
+	if (LDT_empty(info))
c47527a
+		return true;
c47527a
+
c47527a
+	/*
c47527a
+	 * espfix is required for 16-bit data segments, but espfix
c47527a
+	 * only works for LDT segments.
c47527a
+	 */
c47527a
+	if (!info->seg_32bit)
c47527a
+		return false;
c47527a
+
c47527a
+	return true;
c47527a
+}
c47527a
+
c47527a
 static void set_tls_desc(struct task_struct *p, int idx,
c47527a
 			 const struct user_desc *info, int n)
c47527a
 {
c47527a
@@ -66,6 +81,9 @@ int do_set_thread_area(struct task_struct *p, int idx,
c47527a
 	if (copy_from_user(&info, u_info, sizeof(info)))
c47527a
 		return -EFAULT;
c47527a
 
c47527a
+	if (!tls_desc_okay(&info))
c47527a
+		return -EINVAL;
c47527a
+
c47527a
 	if (idx == -1)
c47527a
 		idx = info.entry_number;
c47527a
 
c47527a
@@ -192,6 +210,7 @@ int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
c47527a
 {
c47527a
 	struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
c47527a
 	const struct user_desc *info;
c47527a
+	int i;
c47527a
 
c47527a
 	if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
c47527a
 	    (pos % sizeof(struct user_desc)) != 0 ||
c47527a
@@ -205,6 +224,10 @@ int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
c47527a
 	else
c47527a
 		info = infobuf;
c47527a
 
c47527a
+	for (i = 0; i < count / sizeof(struct user_desc); i++)
c47527a
+		if (!tls_desc_okay(info + i))
c47527a
+			return -EINVAL;
c47527a
+
c47527a
 	set_tls_desc(target,
c47527a
 		     GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
c47527a
 		     info, count / sizeof(struct user_desc));
c47527a
-- 
c47527a
2.1.0
c47527a