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

Dmitry Safonov dima at arista.com
Sun Nov 10 01:20:34 MSK 2019


>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 at arista.com>
---
 compel/src/lib/ptrace.c | 6 ++++++
 1 file changed, 6 insertions(+)

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;
-- 
2.24.0



More information about the CRIU mailing list