From: Brian Gerst <brgerst@gmail.com>
To: linux-kernel@vger.kernel.org, x86@kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>, "H . Peter Anvin" <hpa@zytor.com>,
Peter Zijlstra <peterz@infradead.org>,
Linus Torvalds <torvalds@linuxfoundation.org>,
Brian Gerst <brgerst@gmail.com>, Michal Luczaj <mhal@rbox.co>
Subject: [PATCH 2/3] x86/ptrace: Reject system segements
Date: Wed, 13 Dec 2023 11:34:42 -0500 [thread overview]
Message-ID: <20231213163443.70490-3-brgerst@gmail.com> (raw)
In-Reply-To: <20231213163443.70490-1-brgerst@gmail.com>
Do not allow system segments (TSS and LDT) from being poked into segment
registers via ptrace. Loading these segments into a segment register
normally results in a general protection fault. But in the case of
ptrace, setting CS or SS to a system segment will cause IRET to fault.
This then results in the instruction decoder attempting to use the
invalid segment. This can be avoided by rejecting system segments in
PTRACE_SETREGS.
Signed-off-by: Brian Gerst <brgerst@gmail.com>
Reported-By: Michal Luczaj <mhal@rbox.co>
Link: https://lore.kernel.org/lkml/20231206004654.2986026-1-mhal@rbox.co/
---
arch/x86/include/asm/segment.h | 11 +++++++++++
arch/x86/kernel/ptrace.c | 12 ++----------
2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index a155843d0c37..ede1fa5aa4cc 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -359,6 +359,17 @@ static inline void __loadsegment_fs(unsigned short value)
#define savesegment(seg, value) \
asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
+/*
+ * Determines whether a value may be installed in a segment register.
+ */
+static inline bool valid_user_selector(u16 value)
+{
+ if (unlikely(!(value & SEGMENT_TI_MASK) && value >= (GDT_SYSTEM_START * 8)))
+ return false;
+
+ return likely(value == 0 || (value & SEGMENT_RPL_MASK) == USER_RPL);
+}
+
#endif /* !__ASSEMBLY__ */
#endif /* __KERNEL__ */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 095f04bdabdc..4c3a2278691e 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -162,14 +162,6 @@ const char *regs_query_register_name(unsigned int offset)
X86_EFLAGS_DF | X86_EFLAGS_OF | \
X86_EFLAGS_RF | X86_EFLAGS_AC))
-/*
- * Determines whether a value may be installed in a segment register.
- */
-static inline bool invalid_selector(u16 value)
-{
- return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
-}
-
#ifdef CONFIG_X86_32
#define FLAG_MASK FLAG_MASK_32
@@ -206,7 +198,7 @@ static int set_segment_reg(struct task_struct *task,
/*
* The value argument was already truncated to 16 bits.
*/
- if (invalid_selector(value))
+ if (!valid__user_selector(value))
return -EIO;
/*
@@ -296,7 +288,7 @@ static int set_segment_reg(struct task_struct *task,
/*
* The value argument was already truncated to 16 bits.
*/
- if (invalid_selector(value))
+ if (!valid_user_selector(value))
return -EIO;
/*
--
2.43.0
next prev parent reply other threads:[~2023-12-13 16:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-13 16:34 [PATCH 0/3] Reject setting system segments from userspace Brian Gerst
2023-12-13 16:34 ` [PATCH 1/3] x86: Move TSS and LDT to end of the GDT Brian Gerst
2023-12-13 18:51 ` Linus Torvalds
2023-12-13 19:08 ` Linus Torvalds
2023-12-16 18:24 ` Vegard Nossum
2023-12-16 18:40 ` Linus Torvalds
2023-12-17 21:09 ` H. Peter Anvin
2023-12-13 16:34 ` Brian Gerst [this message]
2023-12-13 16:34 ` [PATCH 3/3] x86/sigreturn: Reject system segements Brian Gerst
2023-12-13 18:54 ` Linus Torvalds
2023-12-17 21:07 ` H. Peter Anvin
2023-12-17 21:40 ` Linus Torvalds
2023-12-17 21:45 ` H. Peter Anvin
2023-12-18 8:31 ` Li, Xin3
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231213163443.70490-3-brgerst@gmail.com \
--to=brgerst@gmail.com \
--cc=bp@alien8.de \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhal@rbox.co \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linuxfoundation.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.