v15: drop task_access_ok variant, instead prefer to just untag the
selector address when validating the user pointer.
v14: implement task_access_ok variant for cross-task pointer checks
v13: sizeof consistency and cosmetic changes in patch 2
v12: split test into its own patch
change from padding a u8 to using a u64
casting issues
checkpatch.pl
[truncating version history]
Syscall user dispatch makes it possible to cleanly intercept system
calls from user-land. However, most transparent checkpoint software
presently leverages some combination of ptrace and system call
injection to place software in a ready-to-checkpoint state.
If Syscall User Dispatch is enabled at the time of being quiesced,
injected system calls will subsequently be interposed upon and
dispatched to the task's signal handler.
Patch summary:
- Refactor configuration setting interface to operate on a task
rather than current, so the set and error paths can be consolidated
- Untag the selector address when being set in order to enable an
untagged tracer to set a tagged tracee's syscall dispatch selector.
Otherwise an untagged tracer will always fail to set a tagged address.
- Implement a getter interface for Syscall User Dispatch config info.
To resume successfully, the checkpoint/resume software has to
save and restore this information. Presently this configuration
is write-only, with no way for C/R software to save it.
This was done in ptrace because syscall user dispatch is not part of
uapi. The syscall_user_dispatch_config structure was added to the
ptrace exports.
- Selftest for the new feature
Gregory Price (4):
syscall_user_dispatch: helper function to operate on given task
syscall user dispatch: untag selector addresses before access_ok
ptrace,syscall_user_dispatch: checkpoint/restore support for SUD
selftest,ptrace: Add selftest for syscall user dispatch config api
.../admin-guide/syscall-user-dispatch.rst | 4 +
include/linux/syscall_user_dispatch.h | 18 +++++
include/uapi/linux/ptrace.h | 29 +++++++
kernel/entry/syscall_user_dispatch.c | 78 ++++++++++++++++---
kernel/ptrace.c | 9 +++
tools/testing/selftests/ptrace/.gitignore | 1 +
tools/testing/selftests/ptrace/Makefile | 2 +-
tools/testing/selftests/ptrace/get_set_sud.c | 72 +++++++++++++++++
8 files changed, 203 insertions(+), 10 deletions(-)
create mode 100644 tools/testing/selftests/ptrace/get_set_sud.c
--
2.39.1
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/[email protected]/
Signed-off-by: Gregory Price <[email protected]>
Suggested-by: Catalin Marinas <[email protected]>
---
kernel/entry/syscall_user_dispatch.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
index 22396b234854..16086226b41c 100644
--- a/kernel/entry/syscall_user_dispatch.c
+++ b/kernel/entry/syscall_user_dispatch.c
@@ -87,7 +87,18 @@ 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.
+ *
+ * The result of this is that a tagged tracer may be capable of setting
+ * an invalid address, and the tracee will SIGSEGV on the next syscall.
+ * This is equivalent to a task setting a bad selector (selector=0x1).
+ */
+ if (selector && !access_ok(untagged_addr(selector), sizeof(*selector)))
return -EFAULT;
break;
--
2.39.1
Implement ptrace getter/setter interface for syscall user dispatch.
These prctl settings are presently write-only, making it impossible to
implement transparent checkpoint/restore via software like CRIU.
'on_dispatch' field is not exposed because it is a kernel-internal
only field that cannot be 'true' when returning to userland.
Signed-off-by: Gregory Price <[email protected]>
Reviewed-by: Oleg Nesterov <[email protected]>
---
.../admin-guide/syscall-user-dispatch.rst | 4 ++
include/linux/syscall_user_dispatch.h | 18 ++++++++
include/uapi/linux/ptrace.h | 29 +++++++++++++
kernel/entry/syscall_user_dispatch.c | 42 +++++++++++++++++++
kernel/ptrace.c | 9 ++++
5 files changed, 102 insertions(+)
diff --git a/Documentation/admin-guide/syscall-user-dispatch.rst b/Documentation/admin-guide/syscall-user-dispatch.rst
index 60314953c728..f7648c08297e 100644
--- a/Documentation/admin-guide/syscall-user-dispatch.rst
+++ b/Documentation/admin-guide/syscall-user-dispatch.rst
@@ -73,6 +73,10 @@ thread-wide, without the need to invoke the kernel directly. selector
can be set to SYSCALL_DISPATCH_FILTER_ALLOW or SYSCALL_DISPATCH_FILTER_BLOCK.
Any other value should terminate the program with a SIGSYS.
+Additionally, a task's syscall user dispatch configuration can be peeked
+and poked via the PTRACE_(GET|SET)_SYSCALL_USER_DISPATCH_CONFIG ptrace
+requests. This is useful for checkpoint/restart software.
+
Security Notes
--------------
diff --git a/include/linux/syscall_user_dispatch.h b/include/linux/syscall_user_dispatch.h
index a0ae443fb7df..641ca8880995 100644
--- a/include/linux/syscall_user_dispatch.h
+++ b/include/linux/syscall_user_dispatch.h
@@ -22,6 +22,12 @@ int set_syscall_user_dispatch(unsigned long mode, unsigned long offset,
#define clear_syscall_work_syscall_user_dispatch(tsk) \
clear_task_syscall_work(tsk, SYSCALL_USER_DISPATCH)
+int syscall_user_dispatch_get_config(struct task_struct *task, unsigned long size,
+ void __user *data);
+
+int syscall_user_dispatch_set_config(struct task_struct *task, unsigned long size,
+ void __user *data);
+
#else
struct syscall_user_dispatch {};
@@ -35,6 +41,18 @@ static inline void clear_syscall_work_syscall_user_dispatch(struct task_struct *
{
}
+static inline int syscall_user_dispatch_get_config(struct task_struct *task,
+ unsigned long size, void __user *data)
+{
+ return -EINVAL;
+}
+
+static inline int syscall_user_dispatch_set_config(struct task_struct *task,
+ unsigned long size, void __user *data)
+{
+ return -EINVAL;
+}
+
#endif /* CONFIG_GENERIC_ENTRY */
#endif /* _SYSCALL_USER_DISPATCH_H */
diff --git a/include/uapi/linux/ptrace.h b/include/uapi/linux/ptrace.h
index 195ae64a8c87..1e77b02344c3 100644
--- a/include/uapi/linux/ptrace.h
+++ b/include/uapi/linux/ptrace.h
@@ -112,6 +112,35 @@ struct ptrace_rseq_configuration {
__u32 pad;
};
+#define PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG 0x4210
+#define PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG 0x4211
+
+/*
+ * struct ptrace_sud_config - Per-task configuration for SUD
+ * @mode: One of PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF
+ * @selector: Tracee's user virtual address of SUD selector
+ * @offset: SUD exclusion area (virtual address)
+ * @len: Length of SUD exclusion area
+ *
+ * Used to get/set the syscall user dispatch configuration for tracee.
+ * process. Selector is optional (may be NULL), and if invalid will produce
+ * a SIGSEGV in the tracee upon first access.
+ *
+ * If mode is PR_SYS_DISPATCH_ON, syscall dispatch will be enabled. If
+ * PR_SYS_DISPATCH_OFF, syscall dispatch will be disabled and all other
+ * parameters must be 0. The value in *selector (if not null), also determines
+ * whether syscall dispatch will occur.
+ *
+ * The SUD Exclusion area described by offset/len is the virtual address space
+ * from which syscalls will not produce a user dispatch.
+ */
+struct ptrace_sud_config {
+ __u64 mode;
+ __u64 selector;
+ __u64 offset;
+ __u64 len;
+};
+
/*
* These values are stored in task->ptrace_message
* by ptrace_stop to describe the current syscall-stop.
diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
index 16086226b41c..f0aa25e34622 100644
--- a/kernel/entry/syscall_user_dispatch.c
+++ b/kernel/entry/syscall_user_dispatch.c
@@ -4,6 +4,7 @@
*/
#include <linux/sched.h>
#include <linux/prctl.h>
+#include <linux/ptrace.h>
#include <linux/syscall_user_dispatch.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
@@ -124,3 +125,44 @@ int set_syscall_user_dispatch(unsigned long mode, unsigned long offset,
{
return task_set_syscall_user_dispatch(current, mode, offset, len, selector);
}
+
+int syscall_user_dispatch_get_config(struct task_struct *task, unsigned long size,
+ void __user *data)
+{
+ struct syscall_user_dispatch *sd = &task->syscall_dispatch;
+ struct ptrace_sud_config cfg;
+
+ if (size != sizeof(cfg))
+ return -EINVAL;
+
+ if (test_task_syscall_work(task, SYSCALL_USER_DISPATCH))
+ cfg.mode = PR_SYS_DISPATCH_ON;
+ else
+ cfg.mode = PR_SYS_DISPATCH_OFF;
+
+ cfg.offset = sd->offset;
+ cfg.len = sd->len;
+ cfg.selector = (__u64)(uintptr_t)sd->selector;
+
+ if (copy_to_user(data, &cfg, sizeof(cfg)))
+ return -EFAULT;
+
+ return 0;
+}
+
+int syscall_user_dispatch_set_config(struct task_struct *task, unsigned long size,
+ void __user *data)
+{
+ int rc;
+ struct ptrace_sud_config cfg;
+
+ if (size != sizeof(cfg))
+ return -EINVAL;
+
+ if (copy_from_user(&cfg, data, sizeof(cfg)))
+ return -EFAULT;
+
+ rc = task_set_syscall_user_dispatch(task, cfg.mode, cfg.offset, cfg.len,
+ (char __user *)(uintptr_t)cfg.selector);
+ return rc;
+}
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 54482193e1ed..d99376532b56 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -32,6 +32,7 @@
#include <linux/compat.h>
#include <linux/sched/signal.h>
#include <linux/minmax.h>
+#include <linux/syscall_user_dispatch.h>
#include <asm/syscall.h> /* for syscall_get_* */
@@ -1259,6 +1260,14 @@ int ptrace_request(struct task_struct *child, long request,
break;
#endif
+ case PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG:
+ ret = syscall_user_dispatch_set_config(child, addr, datavp);
+ break;
+
+ case PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG:
+ ret = syscall_user_dispatch_get_config(child, addr, datavp);
+ break;
+
default:
break;
}
--
2.39.1
Validate that the following new ptrace requests work as expected
* PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG
- returns the contents of task->syscall_dispatch if enabled
* PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG
- sets the contents of task->syscall_dispatch
Signed-off-by: Gregory Price <[email protected]>
---
tools/testing/selftests/ptrace/.gitignore | 1 +
tools/testing/selftests/ptrace/Makefile | 2 +-
tools/testing/selftests/ptrace/get_set_sud.c | 72 ++++++++++++++++++++
3 files changed, 74 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/ptrace/get_set_sud.c
diff --git a/tools/testing/selftests/ptrace/.gitignore b/tools/testing/selftests/ptrace/.gitignore
index 792318aaa30c..b7dde152e75a 100644
--- a/tools/testing/selftests/ptrace/.gitignore
+++ b/tools/testing/selftests/ptrace/.gitignore
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
get_syscall_info
+get_set_sud
peeksiginfo
vmaccess
diff --git a/tools/testing/selftests/ptrace/Makefile b/tools/testing/selftests/ptrace/Makefile
index 2f1f532c39db..33a36b73bcb9 100644
--- a/tools/testing/selftests/ptrace/Makefile
+++ b/tools/testing/selftests/ptrace/Makefile
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
CFLAGS += -std=c99 -pthread -iquote../../../../include/uapi -Wall
-TEST_GEN_PROGS := get_syscall_info peeksiginfo vmaccess
+TEST_GEN_PROGS := get_syscall_info peeksiginfo vmaccess get_set_sud
include ../lib.mk
diff --git a/tools/testing/selftests/ptrace/get_set_sud.c b/tools/testing/selftests/ptrace/get_set_sud.c
new file mode 100644
index 000000000000..5297b10d25c3
--- /dev/null
+++ b/tools/testing/selftests/ptrace/get_set_sud.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include "../kselftest_harness.h"
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/wait.h>
+#include <sys/syscall.h>
+#include <sys/prctl.h>
+
+#include "linux/ptrace.h"
+
+static int sys_ptrace(int request, pid_t pid, void *addr, void *data)
+{
+ return syscall(SYS_ptrace, request, pid, addr, data);
+}
+
+TEST(get_set_sud)
+{
+ struct ptrace_sud_config config;
+ pid_t child;
+ int ret = 0;
+ int status;
+
+ child = fork();
+ ASSERT_GE(child, 0);
+ if (child == 0) {
+ ASSERT_EQ(0, sys_ptrace(PTRACE_TRACEME, 0, 0, 0)) {
+ TH_LOG("PTRACE_TRACEME: %m");
+ }
+ kill(getpid(), SIGSTOP);
+ _exit(1);
+ }
+
+ waitpid(child, &status, 0);
+
+ memset(&config, 0xff, sizeof(config));
+ config.mode = PR_SYS_DISPATCH_ON;
+
+ ret = sys_ptrace(PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG, child,
+ (void *)sizeof(config), &config);
+
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(config.mode, PR_SYS_DISPATCH_OFF);
+ ASSERT_EQ(config.selector, 0);
+ ASSERT_EQ(config.offset, 0);
+ ASSERT_EQ(config.len, 0);
+
+ config.mode = PR_SYS_DISPATCH_ON;
+ config.selector = 0;
+ config.offset = 0x400000;
+ config.len = 0x1000;
+
+ ret = sys_ptrace(PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG, child,
+ (void *)sizeof(config), &config);
+
+ ASSERT_EQ(ret, 0);
+
+ memset(&config, 1, sizeof(config));
+ ret = sys_ptrace(PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG, child,
+ (void *)sizeof(config), &config);
+
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(config.mode, PR_SYS_DISPATCH_ON);
+ ASSERT_EQ(config.selector, 0);
+ ASSERT_EQ(config.offset, 0x400000);
+ ASSERT_EQ(config.len, 0x1000);
+
+ kill(child, SIGKILL);
+}
+
+TEST_HARNESS_MAIN
--
2.39.1
On Thu, Mar 30, 2023 at 05:21:22PM -0400, Gregory Price wrote:
> diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
> index 22396b234854..16086226b41c 100644
> --- a/kernel/entry/syscall_user_dispatch.c
> +++ b/kernel/entry/syscall_user_dispatch.c
> @@ -87,7 +87,18 @@ 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.
> + *
> + * The result of this is that a tagged tracer may be capable of setting
> + * an invalid address, and the tracee will SIGSEGV on the next syscall.
> + * This is equivalent to a task setting a bad selector (selector=0x1).
> + */
I'd drop the last paragraph above. Even without tagged pointers, a tracer
can set an invalid address (as you already mentioned) but the phrasing
some implies (to me) that if we did it differently, the tracer would not be
able to set an invalid pointer.
Either way,
Reviewed-by: Catalin Marinas <[email protected]>
--
Catalin
Catalin,
doesn't this mean that access_ok() on arm64 could use
untagged_addr(addr) unconditionally without any security risk?
On 03/30, Catalin Marinas wrote:
>
> On Thu, Mar 30, 2023 at 05:21:22PM -0400, Gregory Price wrote:
> > diff --git a/kernel/entry/syscall_user_dispatch.c b/kernel/entry/syscall_user_dispatch.c
> > index 22396b234854..16086226b41c 100644
> > --- a/kernel/entry/syscall_user_dispatch.c
> > +++ b/kernel/entry/syscall_user_dispatch.c
> > @@ -87,7 +87,18 @@ 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.
> > + *
> > + * The result of this is that a tagged tracer may be capable of setting
> > + * an invalid address, and the tracee will SIGSEGV on the next syscall.
> > + * This is equivalent to a task setting a bad selector (selector=0x1).
> > + */
>
> I'd drop the last paragraph above. Even without tagged pointers, a tracer
> can set an invalid address (as you already mentioned) but the phrasing
> some implies (to me) that if we did it differently, the tracer would not be
> able to set an invalid pointer.
>
> Either way,
>
> Reviewed-by: Catalin Marinas <[email protected]>
>
> --
> Catalin
>
On Tue, Apr 04, 2023 at 12:45:06PM +0200, Oleg Nesterov wrote:
> doesn't this mean that access_ok() on arm64 could use
> untagged_addr(addr) unconditionally without any security risk?
Yes, from the security perspective, but there are ABI implications.
Currently untagged_addr() in access_ok() is conditional on the user
process enabling the tagged address ABI (prctl() that sets a TIF flag).
The reason we did not enable this by default was a slight fear of
breaking the ABI since tagged pointers were not allowed at the syscall
boundary. It turned out that the fear was justified since the
unconditional untagged_addr() in brk() broke user space (see commit
dcde237319e6 "mm: Avoid creating virtual address aliases in
brk()/mmap()/mremap()"; the user was doing an sbrk(PY_SSIZE_T_MAX) and
bits 56 and higher were ignored by the kernel).
I'd be ok with untagging the address unconditionally in the arm64
access_ok() introduce another unaliased_access_ok() (I'm not good at
naming functions) that preserves the non-tagged behaviour and we use it
in brk/mmap/mremap().
This may actually be a good idea as an additional fix. If an application
enables the tagged address ABI we still have the address aliasing issue
for brk(). We get away with this since glibc, if MTE is enabled, stops
using brk() for heap in favour of mmap(PROT_MTE). But one may use hwasan
without full MTE and it would have a similar issue.
--
Catalin
On Tue, Apr 04, 2023 at 06:33:40PM +0100, Catalin Marinas wrote:
> On Tue, Apr 04, 2023 at 12:45:06PM +0200, Oleg Nesterov wrote:
> > doesn't this mean that access_ok() on arm64 could use
> > untagged_addr(addr) unconditionally without any security risk?
>
> Yes, from the security perspective, but there are ABI implications.
>
> Currently untagged_addr() in access_ok() is conditional on the user
> process enabling the tagged address ABI (prctl() that sets a TIF flag).
> The reason we did not enable this by default was a slight fear of
> breaking the ABI since tagged pointers were not allowed at the syscall
> boundary. It turned out that the fear was justified since the
> unconditional untagged_addr() in brk() broke user space (see commit
> dcde237319e6 "mm: Avoid creating virtual address aliases in
> brk()/mmap()/mremap()"; the user was doing an sbrk(PY_SSIZE_T_MAX) and
> bits 56 and higher were ignored by the kernel).
>
> I'd be ok with untagging the address unconditionally in the arm64
> access_ok() introduce another unaliased_access_ok() (I'm not good at
> naming functions) that preserves the non-tagged behaviour and we use it
> in brk/mmap/mremap().
Actually, I'm wrong here. There's no access_ok() check on the brk()
path. The unconditional untagged_addr() prior to dcde237319e6 messed up
the comparison between the old and new brk limit and shrank the heap
space for a process.
So, relaxing access_ok() to always do the untagging should not affect
the brk/mmap/mremap() cases.
--
Catalin
On 04/05, Catalin Marinas wrote:
>
> > Yes, from the security perspective, but there are ABI implications.
Thanks a lot for your explanations.
To be honest, I am still a bit confused... Will try to grep the relevant code.
But as for this patch, now I believe it is correct.
So, FWIW,
Acked-by: Oleg Nesterov <[email protected]>