Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * #pragma ident	"@(#)dtrace_subr.c	1.8	07/06/05 SMI"
 */

#include <stdarg.h>
#include <string.h>
#include <sys/malloc.h>
#include <sys/time.h>
#include <sys/dtrace.h>
#include <sys/dtrace_impl.h>
#include <sys/proc_internal.h>
#include <sys/vnode.h>
#include <kern/debug.h>
#include <kern/sched_prim.h>
#include <kern/task.h>

#if CONFIG_CSR
#include <sys/codesign.h>
#include <sys/csr.h>
#endif

/*
 * APPLE NOTE: Solaris proc_t is the struct.
 * Darwin's proc_t is a pointer to it.
 */
#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */


/* Copied from an arch specific dtrace_subr.c. */
int (*dtrace_fasttrap_probe_ptr)(struct regs *);

/*
 * Following DTrace hooks are taken from Solaris' dtrace_subr.c
 * They're assigned in dtrace.c but Darwin never calls them.
 */
void (*dtrace_cpu_init)(processorid_t);
int (*dtrace_modload)(struct kmod_info *, uint32_t);
int (*dtrace_modunload)(struct kmod_info *);
void (*dtrace_helpers_cleanup)(proc_t *);
void (*dtrace_helpers_fork)(proc_t *, proc_t *);
void (*dtrace_cpustart_init)(void);
void (*dtrace_cpustart_fini)(void);

void (*dtrace_debugger_init)(void);
void (*dtrace_debugger_fini)(void);

dtrace_vtime_state_t dtrace_vtime_active = 0;
dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;

void (*dtrace_fasttrap_fork_ptr)(proc_t *, proc_t *);
void (*dtrace_fasttrap_exec_ptr)(proc_t *);
void (*dtrace_fasttrap_exit_ptr)(proc_t *);

/*
 * This function is called by cfork() in the event that it appears that
 * there may be dtrace tracepoints active in the parent process's address
 * space. This first confirms the existence of dtrace tracepoints in the
 * parent process and calls into the fasttrap module to remove the
 * corresponding tracepoints from the child. By knowing that there are
 * existing tracepoints, and ensuring they can't be removed, we can rely
 * on the fasttrap module remaining loaded.
 */
void
dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
{
	if (dtrace_fasttrap_fork_ptr) {
		(*dtrace_fasttrap_fork_ptr)(p, cp);
	}
}


/*
 * DTrace wait for process execution
 *
 * This feature is using a list of entries, each entry containing a pointer
 * on a process description. The description is provided by a client, and it
 * contains the command we want to wait for along with a reserved space for
 * the caught process id.
 *
 * Once an awaited process has been spawned, it will be suspended before
 * notifying the client. Once the client has been back to userland, it's its
 * duty to resume the task.
 */

lck_mtx_t dtrace_procwaitfor_lock;

typedef struct dtrace_proc_awaited_entry {
	struct dtrace_procdesc			*pdesc;
	LIST_ENTRY(dtrace_proc_awaited_entry)	entries;
} dtrace_proc_awaited_entry_t;

LIST_HEAD(listhead, dtrace_proc_awaited_entry) dtrace_proc_awaited_head
	= LIST_HEAD_INITIALIZER(dtrace_proc_awaited_head);

void (*dtrace_proc_waitfor_exec_ptr)(proc_t*) = NULL;

static int
dtrace_proc_get_execpath(proc_t *p, char *buffer, int *maxlen)
{
	int err = 0, vid = 0;
	vnode_t tvp = NULLVP, nvp = NULLVP;

	ASSERT(p);
	ASSERT(buffer);
	ASSERT(maxlen);

	if ((tvp = p->p_textvp) == NULLVP)
		return ESRCH;

	vid = vnode_vid(tvp);
	if ((err = vnode_getwithvid(tvp, vid)) != 0)
		return err;

	if ((err = vn_getpath_fsenter(tvp, buffer, maxlen)) != 0)
		return err;
	vnode_put(tvp);

	if ((err = vnode_lookup(buffer, 0, &nvp, vfs_context_current())) != 0)
		return err;
	if (nvp != NULLVP)
		vnode_put(nvp);

	return 0;
}


static void
dtrace_proc_exec_notification(proc_t *p) {
	dtrace_proc_awaited_entry_t *entry, *tmp;
	static char execpath[MAXPATHLEN];

	ASSERT(p);
	ASSERT(p->p_pid != -1);
	ASSERT(current_task() != p->task);

	lck_mtx_lock(&dtrace_procwaitfor_lock);

	LIST_FOREACH_SAFE(entry, &dtrace_proc_awaited_head, entries, tmp) {
		/* By default consider we're using p_comm. */
		char *pname = p->p_comm;

		/* Already matched with another process. */
		if ((entry->pdesc->p_pid != -1))
			continue;

		/* p_comm is too short, use the execpath. */
		if (entry->pdesc->p_name_length >= MAXCOMLEN) {
			/*
			 * Retrieve the executable path. After the call, length contains
			 * the length of the string + 1.
			 */
			int length = sizeof(execpath);
			if (dtrace_proc_get_execpath(p, execpath, &length) != 0)
				continue;
			/* Move the cursor to the position after the last / */
			pname = &execpath[length - 1];
			while (pname != execpath && *pname != '/')
				pname--;
			pname = (*pname == '/') ? pname + 1 : pname;
		}

		if (!strcmp(entry->pdesc->p_name, pname)) {
			entry->pdesc->p_pid = p->p_pid;
			task_pidsuspend(p->task);
			wakeup(entry);
		}
	}

	lck_mtx_unlock(&dtrace_procwaitfor_lock);
}

int
dtrace_proc_waitfor(dtrace_procdesc_t* pdesc) {
	dtrace_proc_awaited_entry_t entry;
	int res;

	ASSERT(pdesc);
	ASSERT(pdesc->p_name);

	/*
	 * Never trust user input, compute the length of the process name and ensure the
	 * string is null terminated.
	 */
	pdesc->p_name_length = strnlen(pdesc->p_name, sizeof(pdesc->p_name));
	if (pdesc->p_name_length >= (int) sizeof(pdesc->p_name))
		return -1;

	lck_mtx_lock(&dtrace_procwaitfor_lock);

	/* Initialize and insert the entry, then install the hook. */
	pdesc->p_pid = -1;
	entry.pdesc = pdesc;
	LIST_INSERT_HEAD(&dtrace_proc_awaited_head, &entry, entries);
	dtrace_proc_waitfor_exec_ptr = &dtrace_proc_exec_notification;

	/* Sleep until the process has been executed */
	res = msleep(&entry, &dtrace_procwaitfor_lock, PCATCH, "dtrace_proc_waitfor", NULL);

	/* Remove the entry and the hook if it is not needed anymore. */
	LIST_REMOVE(&entry, entries);
	if (LIST_EMPTY(&dtrace_proc_awaited_head))
		dtrace_proc_waitfor_exec_ptr = NULL;

	lck_mtx_unlock(&dtrace_procwaitfor_lock);

	return res;
}


typedef struct dtrace_invop_hdlr {
	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
	struct dtrace_invop_hdlr *dtih_next;
} dtrace_invop_hdlr_t;

dtrace_invop_hdlr_t *dtrace_invop_hdlr;

int
dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);

int
dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
{
	dtrace_invop_hdlr_t *hdlr;
	int rval;

	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
			return (rval);
	}

	return (0);
}

void
dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
{
	dtrace_invop_hdlr_t *hdlr;

	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
	hdlr->dtih_func = func;
	hdlr->dtih_next = dtrace_invop_hdlr;
	dtrace_invop_hdlr = hdlr;
}

void
dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
{
	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;

	for (;;) {
		if (hdlr == NULL)
			panic("attempt to remove non-existent invop handler");

		if (hdlr->dtih_func == func)
			break;

		prev = hdlr;
		hdlr = hdlr->dtih_next;
	}

	if (prev == NULL) {
		ASSERT(dtrace_invop_hdlr == hdlr);
		dtrace_invop_hdlr = hdlr->dtih_next;
	} else {
		ASSERT(dtrace_invop_hdlr != hdlr);
		prev->dtih_next = hdlr->dtih_next;
	}

	kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
}

static minor_t next_minor = 0;
static dtrace_state_t* dtrace_clients[DTRACE_NCLIENTS] = {NULL};


minor_t
dtrace_state_reserve(void)
{
	for (int i = 0; i < DTRACE_NCLIENTS; i++) {
		minor_t minor = atomic_add_32(&next_minor, 1) % DTRACE_NCLIENTS;
		if (dtrace_clients[minor] == NULL)
			return minor;
	}
	return 0;
}

dtrace_state_t*
dtrace_state_get(minor_t minor)
{
	ASSERT(minor < DTRACE_NCLIENTS);
	return dtrace_clients[minor];
}

dtrace_state_t*
dtrace_state_allocate(minor_t minor)
{
	dtrace_state_t *state = _MALLOC(sizeof(dtrace_state_t), M_TEMP, M_ZERO | M_WAITOK);
	if (dtrace_casptr(&dtrace_clients[minor], NULL, state) != NULL) {
		// We have been raced by another client for this number, abort
		_FREE(state, M_TEMP);
		return NULL;
	}
	return state;
}

void
dtrace_state_free(minor_t minor)
{
	dtrace_state_t *state = dtrace_clients[minor];
	dtrace_clients[minor] = NULL;
	_FREE(state, M_TEMP);
}



void
dtrace_restriction_policy_load(void)
{
}

/*
 * Check if DTrace has been restricted by the current security policy.
 */
boolean_t
dtrace_is_restricted(void)
{
#if CONFIG_CSR
	if (csr_check(CSR_ALLOW_UNRESTRICTED_DTRACE) != 0)
		return TRUE;
#endif

	return FALSE;
}

boolean_t
dtrace_are_restrictions_relaxed(void)
{
#if CONFIG_CSR
	if (csr_check(CSR_ALLOW_APPLE_INTERNAL) == 0)
		return TRUE;
#endif

	return FALSE;
}

boolean_t
dtrace_fbt_probes_restricted(void)
{

#if CONFIG_CSR
	if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed())
		return TRUE;
#endif

	return FALSE;
}

boolean_t
dtrace_sdt_probes_restricted(void)
{

	return FALSE;
}

/*
 * Check if the process can be attached.
 */
boolean_t
dtrace_can_attach_to_proc(proc_t *proc)
{
#pragma unused(proc)
	ASSERT(proc != NULL);

#if CONFIG_CSR
	if (cs_restricted(proc))
		return FALSE;
#endif

	return TRUE;
}