xref: /freebsd/lib/librtld_db/rtld_db.c (revision f5ce3f4ef562ea9fc4d8f9c13c268f48a5bacba7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 The FreeBSD Foundation
5  *
6  * This software was developed by Rui Paulo under sponsorship from the
7  * FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34 
35 #include <assert.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <machine/elf.h>
45 
46 #include <libelf.h>
47 #include <libproc.h>
48 #include <libprocstat.h>
49 #include <libutil.h>
50 
51 #include "rtld_db.h"
52 
53 static int _librtld_db_debug = 0;
54 #define DPRINTF(...) do {				\
55 	if (_librtld_db_debug) {			\
56 		fprintf(stderr, "librtld_db: DEBUG: ");	\
57 		fprintf(stderr, __VA_ARGS__);		\
58 	}						\
59 } while (0)
60 
61 void
62 rd_delete(rd_agent_t *rdap)
63 {
64 
65 	if (rdap->rda_procstat != NULL)
66 		procstat_close(rdap->rda_procstat);
67 	free(rdap);
68 }
69 
70 const char *
71 rd_errstr(rd_err_e rderr)
72 {
73 
74 	switch (rderr) {
75 	case RD_ERR:
76 		return "generic error";
77 	case RD_OK:
78 		return "no error";
79 	case RD_NOCAPAB:
80 		return "capability not supported";
81 	case RD_DBERR:
82 		return "database error";
83 	case RD_NOBASE:
84 		return "NOBASE";
85 	case RD_NOMAPS:
86 		return "NOMAPS";
87 	default:
88 		return "unknown error";
89 	}
90 }
91 
92 rd_err_e
93 rd_event_addr(rd_agent_t *rdap, rd_event_e event, rd_notify_t *notify)
94 {
95 	rd_err_e ret;
96 
97 	DPRINTF("%s rdap %p event %d notify %p\n", __func__, rdap, event,
98 	    notify);
99 
100 	ret = RD_OK;
101 	switch (event) {
102 	case RD_NONE:
103 		break;
104 	case RD_PREINIT:
105 		notify->type = RD_NOTIFY_BPT;
106 		notify->u.bptaddr = rdap->rda_preinit_addr;
107 		break;
108 	case RD_POSTINIT:
109 		notify->type = RD_NOTIFY_BPT;
110 		notify->u.bptaddr = rdap->rda_postinit_addr;
111 		break;
112 	case RD_DLACTIVITY:
113 		notify->type = RD_NOTIFY_BPT;
114 		notify->u.bptaddr = rdap->rda_dlactivity_addr;
115 		break;
116 	default:
117 		ret = RD_ERR;
118 		break;
119 	}
120 	return (ret);
121 }
122 
123 rd_err_e
124 rd_event_enable(rd_agent_t *rdap __unused, int onoff)
125 {
126 	DPRINTF("%s onoff %d\n", __func__, onoff);
127 
128 	return (RD_OK);
129 }
130 
131 rd_err_e
132 rd_event_getmsg(rd_agent_t *rdap __unused, rd_event_msg_t *msg)
133 {
134 	DPRINTF("%s\n", __func__);
135 
136 	msg->type = RD_POSTINIT;
137 	msg->u.state = RD_CONSISTENT;
138 
139 	return (RD_OK);
140 }
141 
142 rd_err_e
143 rd_init(int version)
144 {
145 	char *debug = NULL;
146 
147 	if (version == RD_VERSION) {
148 		debug = getenv("LIBRTLD_DB_DEBUG");
149 		_librtld_db_debug = debug ? atoi(debug) : 0;
150 		return (RD_OK);
151 	} else
152 		return (RD_NOCAPAB);
153 }
154 
155 rd_err_e
156 rd_loadobj_iter(rd_agent_t *rdap, rl_iter_f *cb, void *clnt_data)
157 {
158 	struct kinfo_vmentry *kves, *kve;
159 	const char *path;
160 	uint64_t fileid;
161 	rd_loadobj_t rdl;
162 	rd_err_e ret;
163 	uintptr_t base;
164 	uint32_t offset;
165 	int cnt, i;
166 
167 	DPRINTF("%s\n", __func__);
168 
169 	if ((kves = kinfo_getvmmap(proc_getpid(rdap->rda_php), &cnt)) == NULL) {
170 		warn("ERROR: kinfo_getvmmap() failed");
171 		return (RD_ERR);
172 	}
173 
174 	base = 0;
175 	fileid = 0;
176 	path = NULL;
177 	ret = RD_OK;
178 	for (i = 0; i < cnt; i++) {
179 		kve = &kves[i];
180 		/*
181 		 * Cache the base offset of the file mapping.  The kve_offset
182 		 * field gives the file offset of a particular mapping into the
183 		 * file, but we want the mapping offset relative to the base
184 		 * mapping.
185 		 */
186 		if (kve->kve_type == KVME_TYPE_VNODE) {
187 			if (kve->kve_vn_fileid != fileid) {
188 				base = kve->kve_start;
189 				fileid = kve->kve_vn_fileid;
190 			}
191 			path = kve->kve_path;
192 			offset = kve->kve_start - base;
193 		} else {
194 			path = NULL;
195 			offset = 0;
196 		}
197 		memset(&rdl, 0, sizeof(rdl));
198 		/*
199 		 * Map the kinfo_vmentry struct to the rd_loadobj structure.
200 		 */
201 		rdl.rdl_saddr = kve->kve_start;
202 		rdl.rdl_eaddr = kve->kve_end;
203 		rdl.rdl_offset = offset;
204 		if (kve->kve_protection & KVME_PROT_READ)
205 			rdl.rdl_prot |= RD_RDL_R;
206 		if (kve->kve_protection & KVME_PROT_WRITE)
207 			rdl.rdl_prot |= RD_RDL_W;
208 		if (kve->kve_protection & KVME_PROT_EXEC)
209 			rdl.rdl_prot |= RD_RDL_X;
210 		if (path != NULL)
211 			strlcpy(rdl.rdl_path, path, sizeof(rdl.rdl_path));
212 		if ((*cb)(&rdl, clnt_data) != 0) {
213 			ret = RD_ERR;
214 			break;
215 		}
216 	}
217 	free(kves);
218 	return (ret);
219 }
220 
221 void
222 rd_log(const int onoff)
223 {
224 	DPRINTF("%s\n", __func__);
225 
226 	(void)onoff;
227 }
228 
229 rd_agent_t *
230 rd_new(struct proc_handle *php)
231 {
232 	rd_agent_t *rdap;
233 
234 	rdap = malloc(sizeof(*rdap));
235 	if (rdap == NULL)
236 		return (NULL);
237 
238 	memset(rdap, 0, sizeof(rd_agent_t));
239 	rdap->rda_php = php;
240 	rdap->rda_procstat = procstat_open_sysctl();
241 
242 	if (rd_reset(rdap) != RD_OK) {
243 		rd_delete(rdap);
244 		rdap = NULL;
245 	}
246 	return (rdap);
247 }
248 
249 rd_err_e
250 rd_objpad_enable(rd_agent_t *rdap, size_t padsize)
251 {
252 	DPRINTF("%s\n", __func__);
253 
254 	(void)rdap;
255 	(void)padsize;
256 
257 	return (RD_ERR);
258 }
259 
260 rd_err_e
261 rd_plt_resolution(rd_agent_t *rdap, uintptr_t pc, struct proc *proc,
262     uintptr_t plt_base, rd_plt_info_t *rpi)
263 {
264 	DPRINTF("%s\n", __func__);
265 
266 	(void)rdap;
267 	(void)pc;
268 	(void)proc;
269 	(void)plt_base;
270 	(void)rpi;
271 
272 	return (RD_ERR);
273 }
274 
275 static int
276 rtld_syms(rd_agent_t *rdap, const char *rtldpath, u_long base)
277 {
278 	GElf_Shdr shdr;
279 	GElf_Sym sym;
280 	Elf *e;
281 	Elf_Data *data;
282 	Elf_Scn *scn;
283 	const char *symname;
284 	Elf64_Word strscnidx;
285 	int fd, i, ret;
286 
287 	ret = 1;
288 	e = NULL;
289 
290 	fd = open(rtldpath, O_RDONLY);
291 	if (fd < 0)
292 		goto err;
293 
294 	if (elf_version(EV_CURRENT) == EV_NONE)
295 		goto err;
296 	e = elf_begin(fd, ELF_C_READ, NULL);
297 	if (e == NULL)
298 		goto err;
299 
300 	scn = NULL;
301 	while ((scn = elf_nextscn(e, scn)) != NULL) {
302 		gelf_getshdr(scn, &shdr);
303 		if (shdr.sh_type == SHT_DYNSYM)
304 			break;
305 	}
306 	if (scn == NULL)
307 		goto err;
308 
309 	strscnidx = shdr.sh_link;
310 	data = elf_getdata(scn, NULL);
311 	if (data == NULL)
312 		goto err;
313 
314 	for (i = 0; gelf_getsym(data, i, &sym) != NULL; i++) {
315 		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
316 		    GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
317 			continue;
318 		symname = elf_strptr(e, strscnidx, sym.st_name);
319 		if (symname == NULL)
320 			continue;
321 
322 		if (strcmp(symname, "r_debug_state") == 0) {
323 			rdap->rda_preinit_addr = sym.st_value + base;
324 			rdap->rda_dlactivity_addr = sym.st_value + base;
325 		} else if (strcmp(symname, "_r_debug_postinit") == 0) {
326 			rdap->rda_postinit_addr = sym.st_value + base;
327 		}
328 	}
329 
330 	if (rdap->rda_preinit_addr != 0 &&
331 	    rdap->rda_postinit_addr != 0 &&
332 	    rdap->rda_dlactivity_addr != 0)
333 		ret = 0;
334 
335 err:
336 	if (e != NULL)
337 		(void)elf_end(e);
338 	if (fd >= 0)
339 		(void)close(fd);
340 	return (ret);
341 }
342 
343 rd_err_e
344 rd_reset(rd_agent_t *rdap)
345 {
346 	struct kinfo_proc *kp;
347 	struct kinfo_vmentry *kve;
348 	Elf_Auxinfo *auxv;
349 	const char *rtldpath;
350 	u_long base;
351 	rd_err_e rderr;
352 	int count, i;
353 
354 	kp = NULL;
355 	auxv = NULL;
356 	kve = NULL;
357 	rderr = RD_ERR;
358 
359 	kp = procstat_getprocs(rdap->rda_procstat, KERN_PROC_PID,
360 	    proc_getpid(rdap->rda_php), &count);
361 	if (kp == NULL)
362 		return (RD_ERR);
363 	assert(count == 1);
364 
365 	auxv = procstat_getauxv(rdap->rda_procstat, kp, &count);
366 	if (auxv == NULL)
367 		goto err;
368 
369 	base = 0;
370 	for (i = 0; i < count; i++) {
371 		if (auxv[i].a_type == AT_BASE) {
372 			base = auxv[i].a_un.a_val;
373 			break;
374 		}
375 	}
376 	if (i == count)
377 		goto err;
378 
379 	rtldpath = NULL;
380 	kve = procstat_getvmmap(rdap->rda_procstat, kp, &count);
381 	if (kve == NULL)
382 		goto err;
383 	for (i = 0; i < count; i++) {
384 		if (kve[i].kve_start == base) {
385 			rtldpath = kve[i].kve_path;
386 			break;
387 		}
388 	}
389 	if (i == count)
390 		goto err;
391 
392 	if (rtld_syms(rdap, rtldpath, base) != 0)
393 		goto err;
394 
395 	rderr = RD_OK;
396 
397 err:
398 	if (kve != NULL)
399 		procstat_freevmmap(rdap->rda_procstat, kve);
400 	if (auxv != NULL)
401 		procstat_freeauxv(rdap->rda_procstat, auxv);
402 	if (kp != NULL)
403 		procstat_freeprocs(rdap->rda_procstat, kp);
404 	return (rderr);
405 }
406