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