Module Name: src
Committed By: christos
Date: Thu Jul 2 03:47:54 UTC 2015
Modified Files:
src/sys/kern: sys_process.c
src/sys/sys: ptrace.h
Log Message:
Support PIOD_READ_AUXV so that gdb can handle PIE binaries. From OpenBSD.
To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/kern/sys_process.c
cvs rdiff -u -r1.45 -r1.46 src/sys/sys/ptrace.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/kern/sys_process.c
diff -u src/sys/kern/sys_process.c:1.165 src/sys/kern/sys_process.c:1.166
--- src/sys/kern/sys_process.c:1.165 Sun Nov 23 21:34:04 2014
+++ src/sys/kern/sys_process.c Wed Jul 1 23:47:54 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: sys_process.c,v 1.165 2014/11/24 02:34:04 christos Exp $ */
+/* $NetBSD: sys_process.c,v 1.166 2015/07/02 03:47:54 christos Exp $ */
/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -118,7 +118,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.165 2014/11/24 02:34:04 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.166 2015/07/02 03:47:54 christos Exp $");
#include "opt_ptrace.h"
#include "opt_ktrace.h"
@@ -127,6 +127,7 @@ __KERNEL_RCSID(0, "$NetBSD: sys_process.
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
+#include <sys/exec.h>
#include <sys/ptrace.h>
#include <sys/uio.h>
#include <sys/ras.h>
@@ -148,6 +149,9 @@ __KERNEL_RCSID(0, "$NetBSD: sys_process.
# endif
static kauth_listener_t ptrace_listener;
+#ifdef PTRACE
+static int process_auxv_offset(struct proc *, struct uio *);
+#endif
static int
ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
@@ -531,6 +535,14 @@ sys_ptrace(struct lwp *l, const struct s
error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
if (error)
break;
+
+ iov.iov_base = piod.piod_addr;
+ iov.iov_len = piod.piod_len;
+ uio.uio_iov = &iov;
+ uio.uio_iovcnt = 1;
+ uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
+ uio.uio_resid = piod.piod_len;
+
switch (piod.piod_op) {
case PIOD_READ_D:
case PIOD_READ_I:
@@ -546,6 +558,19 @@ sys_ptrace(struct lwp *l, const struct s
}
uio.uio_rw = UIO_WRITE;
break;
+ case PIOD_READ_AUXV:
+ req = PT_READ_D;
+ uio.uio_rw = UIO_READ;
+ tmp = t->p_execsw->es_arglen * sizeof(char *);
+ if (uio.uio_offset > tmp)
+ return EIO;
+ if (uio.uio_resid > tmp - uio.uio_offset)
+ uio.uio_resid = tmp - uio.uio_offset;
+ piod.piod_len = iov.iov_len = uio.uio_resid;
+ error = process_auxv_offset(t, &uio);
+ if (error)
+ return error;
+ break;
default:
error = EINVAL;
break;
@@ -555,12 +580,6 @@ sys_ptrace(struct lwp *l, const struct s
error = proc_vmspace_getref(l->l_proc, &vm);
if (error)
break;
- iov.iov_base = piod.piod_addr;
- iov.iov_len = piod.piod_len;
- uio.uio_iov = &iov;
- uio.uio_iovcnt = 1;
- uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
- uio.uio_resid = piod.piod_len;
uio.uio_vmspace = vm;
error = process_domem(l, lt, &uio);
@@ -1138,3 +1157,31 @@ process_stoptrace(void)
mutex_exit(p->p_lock);
}
#endif /* KTRACE || PTRACE */
+
+#ifdef PTRACE
+static int
+process_auxv_offset(struct proc *p, struct uio *uio)
+{
+ struct ps_strings pss;
+ int error;
+ off_t off = (off_t)p->p_psstrp;
+
+ if ((error = copyin_psstrings(p, &pss)) != 0)
+ return error;
+
+ if (pss.ps_envstr == NULL)
+ return EIO;
+
+ uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
+#ifdef __MACHINE_STACK_GROWS_UP
+ if (uio->uio_offset < off)
+ return EIO;
+#else
+ if (uio->uio_offset > off)
+ return EIO;
+ if ((uio->uio_offset + uio->uio_resid) > off)
+ uio->uio_resid = off - uio->uio_offset;
+#endif
+ return 0;
+}
+#endif
Index: src/sys/sys/ptrace.h
diff -u src/sys/sys/ptrace.h:1.45 src/sys/sys/ptrace.h:1.46
--- src/sys/sys/ptrace.h:1.45 Fri Jan 3 19:10:03 2014
+++ src/sys/sys/ptrace.h Wed Jul 1 23:47:54 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: ptrace.h,v 1.45 2014/01/04 00:10:03 dsl Exp $ */
+/* $NetBSD: ptrace.h,v 1.46 2015/07/02 03:47:54 christos Exp $ */
/*-
* Copyright (c) 1984, 1993
@@ -104,6 +104,7 @@ struct ptrace_io_desc {
#define PIOD_WRITE_D 2 /* write to D spcae */
#define PIOD_READ_I 3 /* read from I space */
#define PIOD_WRITE_I 4 /* write to I space */
+#define PIOD_READ_AUXV 5 /* Read from aux array */
/*
* Argument structure for PT_LWPINFO.