1 /*
2  * Test whether process_vm_readv and PTRACE_PEEKDATA work.
3  *
4  * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2017-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 
33 #include <errno.h>
34 #include <sys/ptrace.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/uio.h>
39 #include <sys/wait.h>
40 
41 #include "test_ucopy.h"
42 
43 #ifndef HAVE_PROCESS_VM_READV
44 
45 # include <asm/unistd.h>
46 # include "scno.h"
47 static ssize_t
strace_process_vm_readv(pid_t pid,const struct iovec * lvec,unsigned long liovcnt,const struct iovec * rvec,unsigned long riovcnt,unsigned long flags)48 strace_process_vm_readv(pid_t pid,
49 		 const struct iovec *lvec,
50 		 unsigned long liovcnt,
51 		 const struct iovec *rvec,
52 		 unsigned long riovcnt,
53 		 unsigned long flags)
54 {
55 	return syscall(__NR_process_vm_readv,
56 		       (long) pid, lvec, liovcnt, rvec, riovcnt, flags);
57 }
58 # define process_vm_readv strace_process_vm_readv
59 
60 #endif /* !HAVE_PROCESS_VM_READV */
61 
62 static bool
call_process_vm_readv(const int pid,long * const addr)63 call_process_vm_readv(const int pid, long *const addr)
64 {
65 	long data = 0;
66 
67 	const struct iovec local = {
68 		.iov_base = &data,
69 		.iov_len = sizeof(data)
70 	};
71 	const struct iovec remote = {
72 		.iov_base = addr,
73 		.iov_len = sizeof(*addr)
74 	};
75 
76 	return process_vm_readv(pid, &local, 1, &remote, 1, 0) == sizeof(data)
77 		&& data == 1;
78 }
79 
80 static bool
call_ptrace_peekdata(const int pid,long * const addr)81 call_ptrace_peekdata(const int pid, long *const addr)
82 {
83 	return ptrace(PTRACE_PEEKDATA, pid, addr, 0) == 1;
84 }
85 
86 static bool
test_ucopy(bool (* fn)(int pid,long * addr))87 test_ucopy(bool (*fn)(int pid, long *addr))
88 {
89 	static long data;
90 
91 	data = 0;
92 	bool rc = false;
93 	int saved = 0;
94 
95 	pid_t pid = fork();
96 	if (pid < 0)
97 		perror_msg_and_fail("fork");
98 
99 	if (!pid) {
100 		data = 1;
101 		if (ptrace(PTRACE_TRACEME, 0, 0, 0))
102 			perror_msg_and_fail("PTRACE_TRACEME");
103 		raise(SIGSTOP);
104 		_exit(0);
105 	}
106 
107 	for (;;) {
108 		int status, tracee;
109 
110 		errno = 0;
111 		tracee = wait(&status);
112 		if (tracee != pid) {
113 			if (errno == EINTR)
114 				continue;
115 			saved = errno;
116 			kill(pid, SIGKILL);
117 			errno = saved;
118 			perror_msg_and_fail("wait");
119 		}
120 		if (WIFEXITED(status)) {
121 			if (WEXITSTATUS(status) == 0)
122 				break;
123 			error_msg_and_fail("unexpected exit status %u",
124 					   WEXITSTATUS(status));
125 		}
126 		if (WIFSIGNALED(status))
127 			error_msg_and_fail("unexpected signal %u",
128 					   WTERMSIG(status));
129 		if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
130 			kill(pid, SIGKILL);
131 			error_msg_and_fail("unexpected wait status %x",
132 					   status);
133 		}
134 
135 		errno = 0;
136 		rc = fn(pid, &data);
137 		if (!rc)
138 			saved = errno;
139 
140 		if (ptrace(PTRACE_CONT, pid, 0, 0)) {
141 			saved = errno;
142 			kill(pid, SIGKILL);
143 			errno = saved;
144 			perror_msg_and_fail("PTRACE_CONT");
145 		}
146 	}
147 
148 	if (!rc)
149 		errno = saved;
150 	return rc;
151 }
152 
153 bool
test_process_vm_readv(void)154 test_process_vm_readv(void)
155 {
156 	return test_ucopy(call_process_vm_readv);
157 }
158 
159 bool
test_ptrace_peekdata(void)160 test_ptrace_peekdata(void)
161 {
162 	return test_ucopy(call_ptrace_peekdata);
163 }
164