From: "Dmitry V. Levin" <ldv@strace.io>
To: Charlie Jenkins <charlie@rivosinc.com>
Cc: "Celeste Liu" <uwu@coelacanthus.name>,
"Oleg Nesterov" <oleg@redhat.com>,
"Paul Walmsley" <paul.walmsley@sifive.com>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
"Eric Biederman" <ebiederm@xmission.com>,
"Kees Cook" <kees@kernel.org>, "Alexandre Ghiti" <alex@ghiti.fr>,
"Andrea Bolognani" <abologna@redhat.com>,
"Björn Töpel" <bjorn@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ron Economos" <re@w6rz.net>,
"Felix Yan" <felixonmars@archlinux.org>,
"Ruizhe Pan" <c141028@gmail.com>,
"Shiqi Zhang" <shiqi@isrc.iscas.ac.cn>,
"Guo Ren" <guoren@kernel.org>, "Yao Zi" <ziyao@disroot.org>,
"Han Gao" <gaohan@iscas.ac.cn>,
"Quan Zhou" <zhouquan@iscas.ac.cn>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH] riscv/ptrace: add new regset to get original a0 register
Date: Tue, 3 Dec 2024 14:19:48 +0200 [thread overview]
Message-ID: <20241203121948.GA18179@strace.io> (raw)
In-Reply-To: <Z06ZAP-_4J4-6aK_@ghost>
On Mon, Dec 02, 2024 at 09:37:04PM -0800, Charlie Jenkins wrote:
[...]
> +static void ptrace_test(int opt, int *result)
> +{
> + int status;
> + pid_t pid;
> + struct user_regs_struct regs;
> + struct iovec iov = {
> + .iov_base = ®s,
> + .iov_len = sizeof(regs),
> + };
> +
> + unsigned long orig_a0;
> + struct iovec a0_iov = {
> + .iov_base = &orig_a0,
> + .iov_len = sizeof(orig_a0),
> + };
> +
> + pid = fork();
> + if (pid == 0) {
> + /* Mark oneself being traced */
> + long val = ptrace(PTRACE_TRACEME, 0, 0, 0);
> + if (val)
> + perr_and_exit("failed to request for tracer to trace me: %ld\n", val);
> +
> + kill(getpid(), SIGSTOP);
> +
> + /* Perform exit syscall that will be intercepted */
> + exit(A0_OLD);
> + }
> +
> + if (pid < 0)
> + exit(1);
> +
> + if (waitpid(pid, &status, 0) != pid)
> + perr_and_exit("failed to wait for the tracee %d\n", pid);
> +
> + /* Stop at the entry point of the syscall */
> + resume_and_wait_tracee(pid, PTRACE_SYSCALL);
> +
> + /* Check tracee regs before the syscall */
> + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov))
> + perr_and_exit("failed to get tracee registers\n");
> + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov))
> + perr_and_exit("failed to get tracee registers\n");
> + if (orig_a0 != A0_OLD)
> + perr_and_exit("unexpected orig_a0: 0x%lx\n", orig_a0);
> +
> + /* Modify a0/orig_a0 for the syscall */
> + switch (opt) {
> + case A0_MODIFY:
> + regs.a0 = A0_NEW;
> + break;
Did you mean applying the modified user_regs_struct using PTRACE_SETREGSET?
If yes, then there should be an appropriate PTRACE_SETREGSET NT_PRSTATUS call.
If no, then regs is ignored, so why would you change it in the first place?
> + case ORIG_A0_MODIFY:
> + orig_a0 = A0_NEW;
> + break;
> + }
> +
> + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov))
> + perr_and_exit("failed to set tracee registers\n");
> +
> + /* Resume the tracee */
> + ptrace(PTRACE_CONT, pid, 0, 0);
> + if (waitpid(pid, &status, 0) != pid)
> + perr_and_exit("failed to wait for the tracee\n");
> +
> + *result = WEXITSTATUS(status);
> +}
--
ldv
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
WARNING: multiple messages have this Message-ID (diff)
From: "Dmitry V. Levin" <ldv@strace.io>
To: Charlie Jenkins <charlie@rivosinc.com>
Cc: "Celeste Liu" <uwu@coelacanthus.name>,
"Oleg Nesterov" <oleg@redhat.com>,
"Paul Walmsley" <paul.walmsley@sifive.com>,
"Palmer Dabbelt" <palmer@dabbelt.com>,
"Albert Ou" <aou@eecs.berkeley.edu>,
"Eric Biederman" <ebiederm@xmission.com>,
"Kees Cook" <kees@kernel.org>, "Alexandre Ghiti" <alex@ghiti.fr>,
"Andrea Bolognani" <abologna@redhat.com>,
"Björn Töpel" <bjorn@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ron Economos" <re@w6rz.net>,
"Felix Yan" <felixonmars@archlinux.org>,
"Ruizhe Pan" <c141028@gmail.com>,
"Shiqi Zhang" <shiqi@isrc.iscas.ac.cn>,
"Guo Ren" <guoren@kernel.org>, "Yao Zi" <ziyao@disroot.org>,
"Han Gao" <gaohan@iscas.ac.cn>,
"Quan Zhou" <zhouquan@iscas.ac.cn>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH] riscv/ptrace: add new regset to get original a0 register
Date: Tue, 3 Dec 2024 14:19:48 +0200 [thread overview]
Message-ID: <20241203121948.GA18179@strace.io> (raw)
In-Reply-To: <Z06ZAP-_4J4-6aK_@ghost>
On Mon, Dec 02, 2024 at 09:37:04PM -0800, Charlie Jenkins wrote:
[...]
> +static void ptrace_test(int opt, int *result)
> +{
> + int status;
> + pid_t pid;
> + struct user_regs_struct regs;
> + struct iovec iov = {
> + .iov_base = ®s,
> + .iov_len = sizeof(regs),
> + };
> +
> + unsigned long orig_a0;
> + struct iovec a0_iov = {
> + .iov_base = &orig_a0,
> + .iov_len = sizeof(orig_a0),
> + };
> +
> + pid = fork();
> + if (pid == 0) {
> + /* Mark oneself being traced */
> + long val = ptrace(PTRACE_TRACEME, 0, 0, 0);
> + if (val)
> + perr_and_exit("failed to request for tracer to trace me: %ld\n", val);
> +
> + kill(getpid(), SIGSTOP);
> +
> + /* Perform exit syscall that will be intercepted */
> + exit(A0_OLD);
> + }
> +
> + if (pid < 0)
> + exit(1);
> +
> + if (waitpid(pid, &status, 0) != pid)
> + perr_and_exit("failed to wait for the tracee %d\n", pid);
> +
> + /* Stop at the entry point of the syscall */
> + resume_and_wait_tracee(pid, PTRACE_SYSCALL);
> +
> + /* Check tracee regs before the syscall */
> + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov))
> + perr_and_exit("failed to get tracee registers\n");
> + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov))
> + perr_and_exit("failed to get tracee registers\n");
> + if (orig_a0 != A0_OLD)
> + perr_and_exit("unexpected orig_a0: 0x%lx\n", orig_a0);
> +
> + /* Modify a0/orig_a0 for the syscall */
> + switch (opt) {
> + case A0_MODIFY:
> + regs.a0 = A0_NEW;
> + break;
Did you mean applying the modified user_regs_struct using PTRACE_SETREGSET?
If yes, then there should be an appropriate PTRACE_SETREGSET NT_PRSTATUS call.
If no, then regs is ignored, so why would you change it in the first place?
> + case ORIG_A0_MODIFY:
> + orig_a0 = A0_NEW;
> + break;
> + }
> +
> + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov))
> + perr_and_exit("failed to set tracee registers\n");
> +
> + /* Resume the tracee */
> + ptrace(PTRACE_CONT, pid, 0, 0);
> + if (waitpid(pid, &status, 0) != pid)
> + perr_and_exit("failed to wait for the tracee\n");
> +
> + *result = WEXITSTATUS(status);
> +}
--
ldv
next prev parent reply other threads:[~2024-12-03 12:27 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-30 21:47 [PATCH] riscv/ptrace: add new regset to get original a0 register Celeste Liu
2024-11-30 21:47 ` Celeste Liu
2024-11-30 21:48 ` kernel test robot
2024-12-02 15:44 ` Björn Töpel
2024-12-02 15:44 ` Björn Töpel
2024-12-03 9:33 ` Celeste Liu
2024-12-03 9:33 ` Celeste Liu
2024-12-03 11:23 ` Björn Töpel
2024-12-03 11:23 ` Björn Töpel
2024-12-03 5:37 ` Charlie Jenkins
2024-12-03 5:37 ` Charlie Jenkins
2024-12-03 9:41 ` Celeste Liu
2024-12-03 9:41 ` Celeste Liu
2024-12-03 12:19 ` Dmitry V. Levin [this message]
2024-12-03 12:19 ` Dmitry V. Levin
2024-12-03 18:45 ` Charlie Jenkins
2024-12-03 18:45 ` Charlie Jenkins
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=20241203121948.GA18179@strace.io \
--to=ldv@strace.io \
--cc=abologna@redhat.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=bjorn@kernel.org \
--cc=c141028@gmail.com \
--cc=charlie@rivosinc.com \
--cc=ebiederm@xmission.com \
--cc=felixonmars@archlinux.org \
--cc=gaohan@iscas.ac.cn \
--cc=guoren@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=oleg@redhat.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=re@w6rz.net \
--cc=shiqi@isrc.iscas.ac.cn \
--cc=tglx@linutronix.de \
--cc=uwu@coelacanthus.name \
--cc=zhouquan@iscas.ac.cn \
--cc=ziyao@disroot.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.