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