[01/12] lib/ptrace: Allow PTRACE_PEEKDATA with errno != 0

Submitted by Dmitry Safonov on Nov. 9, 2019, 10:20 p.m.

Details

Message ID 20191109222045.89507-2-dima@arista.com
State Accepted
Series "compel: Add __must_check"
Commit 2a90c5c31378616895545ca547738fd658d141fa
Headers show

Commit Message

Dmitry Safonov Nov. 9, 2019, 10:20 p.m.
From man ptrace:
> On error, all requests return -1, and errno is set appropriately.
> Since the value returned by a successful PTRACE_PEEK* request may be
> -1, the caller must clear errno before the call, and then check
> it afterward to determine whether or not an error occurred.

FWIW: if ptrace_peek_area() is called with (errno != 0) it may
false-fail if the data is (-1).

Signed-off-by: Dmitry Safonov <dima@arista.com>
---
 compel/src/lib/ptrace.c | 6 ++++++
 1 file changed, 6 insertions(+)

Patch hide | download patch | download mbox

diff --git a/compel/src/lib/ptrace.c b/compel/src/lib/ptrace.c
index 9142bac421b8..715e564df8e8 100644
--- a/compel/src/lib/ptrace.c
+++ b/compel/src/lib/ptrace.c
@@ -34,14 +34,20 @@  int ptrace_suspend_seccomp(pid_t pid)
 int ptrace_peek_area(pid_t pid, void *dst, void *addr, long bytes)
 {
 	unsigned long w;
+	int old_errno = errno;
+
 	if (bytes & (sizeof(long) - 1))
 		return -1;
+
+	errno = 0;
 	for (w = 0; w < bytes / sizeof(long); w++) {
 		unsigned long *d = dst, *a = addr;
+
 		d[w] = ptrace(PTRACE_PEEKDATA, pid, a + w, NULL);
 		if (d[w] == -1U && errno)
 			goto err;
 	}
+	errno = old_errno;
 	return 0;
 err:
 	return -2;