Re: [PATCH] ptrace: add PTRACE_GET_RSEQ_CONFIGURATION request

From: Mathieu Desnoyers
Date: Mon Feb 22 2021 - 09:54:22 EST




----- On Feb 22, 2021, at 5:04 AM, Piotr Figiel figiel@xxxxxxxxxx wrote:

> For userspace checkpoint and restore (C/R) a way of getting process state
> containing RSEQ configuration is needed.
>
> There are two ways this information is going to be used:
> - to re-enable RSEQ for threads which had it enabled before C/R
> - to detect if a thread was in a critical section during C/R
>
> Since C/R preserves TLS memory and addresses RSEQ ABI will be restored
> using the address registered before C/R.
>
> Detection whether the thread is in a critical section during C/R is needed
> to enforce behavior of RSEQ abort during C/R. Attaching with ptrace()
> before registers are dumped itself doesn't cause RSEQ abort.
> Restoring the instruction pointer within the critical section is
> problematic because rseq_cs may get cleared before the control is passed
> to the migrated application code leading to RSEQ invariants not being
> preserved. C/R code will use RSEQ ABI address to find the abort handler
> to which the instruction pointer needs to be set.
>
> To achieve above goals expose the RSEQ ABI address and the signature value
> with the new ptrace request PTRACE_GET_RSEQ_CONFIGURATION.
>
> This new ptrace request can also be used by debuggers so they are aware
> of stops within restartable sequences in progress.
>
> Signed-off-by: Piotr Figiel <figiel@xxxxxxxxxx>
> Reviewed-by: Michal Miroslaw <emmir@xxxxxxxxxx>
>
> ---
> include/uapi/linux/ptrace.h | 8 ++++++++
> kernel/ptrace.c | 23 +++++++++++++++++++++++
> 2 files changed, 31 insertions(+)
>
> diff --git a/include/uapi/linux/ptrace.h b/include/uapi/linux/ptrace.h
> index 83ee45fa634b..d54cf6b6ce7c 100644
> --- a/include/uapi/linux/ptrace.h
> +++ b/include/uapi/linux/ptrace.h
> @@ -102,6 +102,14 @@ struct ptrace_syscall_info {
> };
> };
>
> +#define PTRACE_GET_RSEQ_CONFIGURATION 0x420f
> +
> +struct ptrace_rseq_configuration {
> + __u64 rseq_abi_pointer;
> + __u32 signature;
> + __u32 pad;
> +};

I notice that other structures defined in this UAPI header are not packed as well.
Should we add an attribute packed on new structures ? It seems like it is
generally a safer course of action, even though each field is naturally aligned
here (there is no padding/hole in the structure).

> +
> /*
> * These values are stored in task->ptrace_message
> * by tracehook_report_syscall_* to describe the current syscall-stop.
> diff --git a/kernel/ptrace.c b/kernel/ptrace.c
> index 61db50f7ca86..a936af66cf6f 100644
> --- a/kernel/ptrace.c
> +++ b/kernel/ptrace.c
> @@ -31,6 +31,7 @@
> #include <linux/cn_proc.h>
> #include <linux/compat.h>
> #include <linux/sched/signal.h>
> +#include <linux/minmax.h>
>
> #include <asm/syscall.h> /* for syscall_get_* */
>
> @@ -779,6 +780,22 @@ static int ptrace_peek_siginfo(struct task_struct *child,
> return ret;
> }
>
> +#ifdef CONFIG_RSEQ
> +static long ptrace_get_rseq_configuration(struct task_struct *task,
> + unsigned long size, void __user *data)
> +{
> + struct ptrace_rseq_configuration conf = {
> + .rseq_abi_pointer = (u64)(uintptr_t)task->rseq,
> + .signature = task->rseq_sig,
> + };
> +
> + size = min_t(unsigned long, size, sizeof(conf));
> + if (copy_to_user(data, &conf, size))
> + return -EFAULT;
> + return size;

See other email about returning 0 here.

Thanks,

Mathieu

> +
> default:
> break;
> }
> --
> 2.30.0.617.g56c4b15f3c-goog

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com