[PATCH v16 2/4] syscall user dispatch: untag selector addresses before access_ok

Gregory Price posted 4 patches 2 years, 5 months ago
[PATCH v16 2/4] syscall user dispatch: untag selector addresses before access_ok
Posted by Gregory Price 2 years, 5 months ago
This is a preparatory patch for enabling checkpoint/restart of tasks
utilizing syscall user dispatch via ptrace.

To support checkpoint/restart, ptrace must be able to set the selector
of the tracee.  The selector is a user pointer that may be subject to
memory tagging extensions on some architectures (namely ARM MTE).

access_ok will clear memory tags for tagged addresses on tasks where
memory tagging is enabled.  However, to allow ptrace to set a task's
selector when tracer and tracee are not both tagged or untagged,
the selector address must be untagged when calling access_ok.

Since access_ok utilizes current to determine whether or not to untag
an address, an untagged tracer will always fail to restore a tagged
address in a tagged tracee.  This patch will resolve this issue.

The result of this is that a tagged tracer may be capable of setting
an invalid address, which will cause the tracee to SIGSEGV on next
syscall.  This is equivalent to the tracee setting a bad selector
address (such as selector=0x1).  This is preferable to the alternative
of creating a task_access_ok variant, and is consistent with other
operations which change tracee pointers via ptrace.

For more information, see:
https://lore.kernel.org/all/ZCWXE04nLZ4pXEtM@arm.com/

Signed-off-by: Gregory Price <gregory.price@memverge.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
---
 kernel/entry/syscall_user_dispatch.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
index 22396b234854..424f24350f8b 100644
--- a/kernel/entry/syscall_user_dispatch.c
+++ b/kernel/entry/syscall_user_dispatch.c
@@ -87,7 +87,14 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lon
 		if (offset && offset + len <= offset)
 			return -EINVAL;
 
-		if (selector && !access_ok(selector, sizeof(*selector)))
+		/*
+		 * access_ok will clear memory tags for tagged addresses on tasks where
+		 * memory tagging is enabled.  To enable a tracer to set a tracee's
+		 * selector not in the same tagging state, the selector address must be
+		 * untagged for access_ok, otherwise an untagged tracer will always fail
+		 * to set a tagged tracee's selector.
+		 */
+		if (selector && !access_ok(untagged_addr(selector), sizeof(*selector)))
 			return -EFAULT;
 
 		break;
-- 
2.39.1
[tip: core/entry] syscall_user_dispatch: Untag selector address before access_ok()
Posted by tip-bot2 for Gregory Price 2 years, 4 months ago
The following commit has been merged into the core/entry branch of tip:

Commit-ID:     463b7715e7ce367fce89769c5d85e31595715ee1
Gitweb:        https://git.kernel.org/tip/463b7715e7ce367fce89769c5d85e31595715ee1
Author:        Gregory Price <gourry.memverge@gmail.com>
AuthorDate:    Fri, 07 Apr 2023 13:18:32 -04:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Sun, 16 Apr 2023 14:23:07 +02:00

syscall_user_dispatch: Untag selector address before access_ok()

To support checkpoint/restart, ptrace must be able to set the selector
of the tracee.  The selector is a user pointer that may be subject to
memory tagging extensions on some architectures (namely ARM MTE).

access_ok() clears memory tags for tagged addresses if the current task has
memory tagging enabled.

This obviously fails when ptrace modifies the selector of a tracee when
tracer and tracee do not have the same memory tagging enabled state.

Solve this by untagging the selector address before handing it to
access_ok(), like other ptrace functions which modify tracee pointers do.

Obviously a tracer can set an invalid selector address for the tracee, but
that's independent of tagging and a general capability of the tracer.

Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Gregory Price <gregory.price@memverge.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lore.kernel.org/all/ZCWXE04nLZ4pXEtM@arm.com/
Link: https://lore.kernel.org/r/20230407171834.3558-3-gregory.price@memverge.com

---
 kernel/entry/syscall_user_dispatch.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
index 22396b2..7f2add4 100644
--- a/kernel/entry/syscall_user_dispatch.c
+++ b/kernel/entry/syscall_user_dispatch.c
@@ -87,7 +87,16 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lon
 		if (offset && offset + len <= offset)
 			return -EINVAL;
 
-		if (selector && !access_ok(selector, sizeof(*selector)))
+		/*
+		 * access_ok() will clear memory tags for tagged addresses
+		 * if current has memory tagging enabled.
+
+		 * To enable a tracer to set a tracees selector the
+		 * selector address must be untagged for access_ok(),
+		 * otherwise an untagged tracer will always fail to set a
+		 * tagged tracees selector.
+		 */
+		if (selector && !access_ok(untagged_addr(selector), sizeof(*selector)))
 			return -EFAULT;
 
 		break;