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