Module Name:    src
Committed By:   christos
Date:           Thu Jan 23 12:08:12 UTC 2025

Modified Files:
        src/lib/libexecinfo: backtrace.3 backtrace.c execinfo.h

Log Message:
Add backtrace_sandbox_{init,fini} for backtrace use in sandboxes.
(Kyle Evans @ FreeBSD)


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/lib/libexecinfo/backtrace.3
cvs rdiff -u -r1.8 -r1.9 src/lib/libexecinfo/backtrace.c
cvs rdiff -u -r1.4 -r1.5 src/lib/libexecinfo/execinfo.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libexecinfo/backtrace.3
diff -u src/lib/libexecinfo/backtrace.3:1.10 src/lib/libexecinfo/backtrace.3:1.11
--- src/lib/libexecinfo/backtrace.3:1.10	Sun Oct 22 10:07:03 2017
+++ src/lib/libexecinfo/backtrace.3	Thu Jan 23 07:08:12 2025
@@ -1,4 +1,4 @@
-.\"	$NetBSD: backtrace.3,v 1.10 2017/10/22 14:07:03 abhinav Exp $
+.\"	$NetBSD: backtrace.3,v 1.11 2025/01/23 12:08:12 christos Exp $
 .\"
 .\" Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd November 5, 2015
+.Dd January 22, 2025
 .Dt BACKTRACE 3
 .Os
 .Sh NAME
@@ -43,6 +43,10 @@
 .In execinfo.h
 .Ft size_t
 .Fn backtrace "void **addrlist" "size_t len"
+.Ft "int"
+.Fn backtrace_sandbox_init "void"
+.Ft "void"
+.Fn backtrace_sandbox_fini "void"
 .Ft "char **"
 .Fn backtrace_symbols "void * const *addrlist" "size_t len"
 .Ft int
@@ -64,6 +68,20 @@ The number of frames found (which can be
 is returned.
 .Pp
 The
+.Fn backtrace_sandbox_init
+and
+.Fn backtrace_sandbox_fini
+functions are intended to enable sandbox usage of
+.Nm .
+The
+.Fn backtrace_sandbox_init
+function must be called before the sandbox is entered, and will acquire any
+resources needed to function in a sandbox.
+The
+.Fn backtrace_sandbox_fini
+function will release the resources it acquired.
+.Pp
+The
 .Fn backtrace_symbols_fmt
 function takes an array of previously filled addresses from
 .Fn backtrace

Index: src/lib/libexecinfo/backtrace.c
diff -u src/lib/libexecinfo/backtrace.c:1.8 src/lib/libexecinfo/backtrace.c:1.9
--- src/lib/libexecinfo/backtrace.c:1.8	Sat Jun 25 02:51:37 2022
+++ src/lib/libexecinfo/backtrace.c	Thu Jan 23 07:08:12 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: backtrace.c,v 1.8 2022/06/25 06:51:37 skrll Exp $	*/
+/*	$NetBSD: backtrace.c,v 1.9 2025/01/23 12:08:12 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: backtrace.c,v 1.8 2022/06/25 06:51:37 skrll Exp $");
+__RCSID("$NetBSD: backtrace.c,v 1.9 2025/01/23 12:08:12 christos Exp $");
 
 #include <sys/param.h>
 #include <assert.h>
@@ -55,6 +55,8 @@ __RCSID("$NetBSD: backtrace.c,v 1.8 2022
 #define SELF	"/proc/curproc/file"
 #endif
 
+static int self_fd = -1;
+
 static int
 open_self(int flags)
 {
@@ -73,6 +75,23 @@ open_self(int flags)
 	return open(pathname, flags);
 }
 
+int
+backtrace_sandbox_init(void)
+{
+
+	if (self_fd == -1)
+		self_fd = open_self(O_RDONLY);
+	return self_fd >= 0 ? 0 : -1;
+}
+
+void
+backtrace_sandbox_fini(void)
+{
+	assert(self_fd >= 0);
+
+	close(self_fd);
+	self_fd = -1;
+}
 
 static int __printflike(4, 5)
 rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
@@ -182,9 +201,9 @@ backtrace_symbols_fmt(void *const *trace
 	static const size_t slen = sizeof(char *) + 64;	/* estimate */
 	char *ptr;
 	symtab_t *st;
-	int fd;
+	int fd = self_fd;
 
-	if ((fd = open_self(O_RDONLY)) != -1)
+	if (fd != -1 || (fd = open_self(O_RDONLY)) != -1)
 		st = symtab_create(fd, -1, STT_FUNC);
 	else
 		st = NULL;
@@ -216,7 +235,7 @@ backtrace_symbols_fmt(void *const *trace
 
 out:
 	symtab_destroy(st);
-	if (fd != -1)
+	if (fd != -1 && fd != self_fd)
 		(void)close(fd);
 
 	return (void *)ptr;

Index: src/lib/libexecinfo/execinfo.h
diff -u src/lib/libexecinfo/execinfo.h:1.4 src/lib/libexecinfo/execinfo.h:1.5
--- src/lib/libexecinfo/execinfo.h:1.4	Wed Aug 23 08:24:59 2023
+++ src/lib/libexecinfo/execinfo.h	Thu Jan 23 07:08:12 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: execinfo.h,v 1.4 2023/08/23 12:24:59 rin Exp $	*/
+/*	$NetBSD: execinfo.h,v 1.5 2025/01/23 12:08:12 christos Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -41,6 +41,8 @@ typedef _BSD_SIZE_T_    size_t;
 #endif
 
 __BEGIN_DECLS
+int backtrace_sandbox_init(void);
+void backtrace_sandbox_fini(void);
 size_t backtrace(void **, size_t);
 char **backtrace_symbols(void *const *, size_t);
 int backtrace_symbols_fd(void *const *, size_t, int);

Reply via email to