Subject: [PATCH 2628/2944] x86/ptrace: Document FSBASE and GSBASE ABI oddities

From: Andy Lutomirski <[email-masked]>
Date: Mon, 22 Mar 2021 10:26:47 +0800

to #30648216

commit 56f2ab41b652251f336a0f471b1033afeaedd161 upstream.

Signed-off-by: Andy Lutomirski <[email-masked]>
Cc: Borislav Petkov <[email-masked]>
Cc: Peter Zijlstra <[email-masked]>
Cc: Thomas Gleixner <[email-masked]>
Cc: Linus Torvalds <[email-masked]>
Signed-off-by: Ingo Molnar <[email-masked]>
Signed-off-by: Ethan Zhao <[email-masked]>
Signed-off-by: Zelin Deng <[email-masked]>
Reviewed-by: Artie Ding <[email-masked]>
---
 arch/x86/kernel/ptrace.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 975bdb5..d62f78c 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -304,6 +304,20 @@ static int set_segment_reg(struct task_struct *task,
 	if (invalid_selector(value))
 		return -EIO;
 
+	/*
+	 * This function has some ABI oddities.
+	 *
+	 * A 32-bit ptracer probably expects that writing FS or GS will change
+	 * FSBASE or GSBASE respectively.  In the absence of FSGSBASE support,
+	 * this code indeed has that effect.  When FSGSBASE is added, this
+	 * will require a special case.
+	 *
+	 * For existing 64-bit ptracers, writing FS or GS *also* currently
+	 * changes the base if the selector is nonzero the next time the task
+	 * is run.  This behavior may not be needed, and trying to preserve it
+	 * when FSGSBASE is added would be complicated at best.
+	 */
+
 	switch (offset) {
 	case offsetof(struct user_regs_struct,fs):
 		task->thread.fsindex = value;
@@ -401,6 +415,9 @@ static int putreg(struct task_struct *child,
 		 * When changing the FS base, use do_arch_prctl_64()
 		 * to set the index to zero and to set the base
 		 * as requested.
+		 *
+		 * NB: This behavior is nonsensical and likely needs to
+		 * change when FSGSBASE support is added.
 		 */
 		if (child->thread.fsbase != value)
 			return do_arch_prctl_64(child, ARCH_SET_FS, value);
-- 
1.8.3.1