sandbox for OS X

Damien Miller djm at mindrot.org
Fri Jun 24 09:45:30 EST 2011


On Thu, 23 Jun 2011, Damien Miller wrote:

> 
> Hi,
> 
> The systrace and rlimit sandboxes have been committed and will be in
> snapshots dated 20110623 and later. This diff adds support for
> pre-auth privsep sandboxing using the OS X sandbox_init(3) service.
> 
> It's a bit disappointing that the OS X developers chose such as
> namespace-polluting header and function names "sandbox.h",
> "sandbox_init()", etc. It already forced me to rename a header in
> OpenSSH.
> 
> Anyway, the OS X sandbox  uses the strictest of the canned policies:
> "kSBXProfilePureComputation". It passes regress tests and seems to
> deny calls to fork() as expected. Barring objections, I'll commit
> this soon - please test.

Markus points out that, despite its name, the kSBXProfilePureComputation
sandbox actually allows the sandboxed process to create sockets (WTH?).
So here is a revised version of the diff that uses setrlimit() to
prevent that too.

-d

Index: ChangeLog
===================================================================
RCS file: /var/cvs/openssh/ChangeLog,v
retrieving revision 1.5912
diff -u -p -r1.5912 ChangeLog
--- ChangeLog	23 Jun 2011 09:45:51 -0000	1.5912
+++ ChangeLog	23 Jun 2011 23:41:14 -0000
@@ -1,3 +1,7 @@
+20110624
+ - (djm) [configure.ac Makefile.in sandbox-darwin.c] Add a sandbox for
+   Darwin/OS X using sandbox_init() + setrlimit()
+
 20110623
  - OpenBSD CVS Sync
    - djm at cvs.openbsd.org 2011/06/22 21:47:28
Index: Makefile.in
===================================================================
RCS file: /var/cvs/openssh/Makefile.in,v
retrieving revision 1.323
diff -u -p -r1.323 Makefile.in
--- Makefile.in	22 Jun 2011 22:30:03 -0000	1.323
+++ Makefile.in	23 Jun 2011 22:33:38 -0000
@@ -90,7 +90,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passw
 	loginrec.o auth-pam.o auth-shadow.o auth-sia.o md5crypt.o \
 	sftp-server.o sftp-common.o \
 	roaming_common.o roaming_serv.o \
-	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o
+	sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o
 
 MANPAGES	= moduli.5.out scp.1.out ssh-add.1.out ssh-agent.1.out ssh-keygen.1.out ssh-keyscan.1.out ssh.1.out sshd.8.out sftp-server.8.out sftp.1.out ssh-keysign.8.out ssh-pkcs11-helper.8.out sshd_config.5.out ssh_config.5.out
 MANPAGES_IN	= moduli.5 scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh-keyscan.1 ssh.1 sshd.8 sftp-server.8 sftp.1 ssh-keysign.8 ssh-pkcs11-helper.8 sshd_config.5 ssh_config.5
Index: configure.ac
===================================================================
RCS file: /var/cvs/openssh/configure.ac,v
retrieving revision 1.477
diff -u -p -r1.477 configure.ac
--- configure.ac	22 Jun 2011 22:30:03 -0000	1.477
+++ configure.ac	23 Jun 2011 22:33:38 -0000
@@ -525,6 +525,8 @@ main() { if (NSVersionOfRunTimeLibrary("
 	AC_DEFINE([SPT_TYPE], [SPT_REUSEARGV],
 		[Define to a Set Process Title type if your system is
 		supported by bsd-setproctitle.c])
+	AC_CHECK_FUNCS([sandbox_init])
+	AC_CHECK_HEADERS([sandbox.h])
 	;;
 *-*-dragonfly*)
 	SSHDLIBS="$SSHDLIBS -lcrypt"
@@ -2487,6 +2489,11 @@ if test "x$sandbox_arg" = "xsystrace" ||
    ( test -z "$sandbox_arg" && test "x$have_systr_policy_kill" = "x1" ) ; then
 	SANDBOX_STYLE="systrace"
 	AC_DEFINE([SANDBOX_SYSTRACE], [1], [Sandbox using systrace(4)])
+elif test "x$sandbox_arg" = "xdarwin" || \
+     ( test -z "$sandbox_arg" && test "x$ac_cv_func_sandbox_init" = "xyes" && \
+       test "x$ac_cv_header_sandbox_h" = "xyes") ; then
+	SANDBOX_STYLE="darwin"
+	AC_DEFINE([SANDBOX_DARWIN], [1], [Sandbox using Darwin sandbox_init(3)])
 elif test "x$sandbox_arg" = "xrlimit" || \
      ( test -z "$sandbox_arg" && test "x$ac_cv_func_setrlimit" = "xyes" ) ; then
 	SANDBOX_STYLE="rlimit"
Index: sandbox-darwin.c
===================================================================
RCS file: sandbox-darwin.c
diff -N sandbox-darwin.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sandbox-darwin.c	23 Jun 2011 22:36:07 -0000
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2011 Damien Miller <djm at mindrot.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "includes.h"
+
+#ifdef SANDBOX_DARWIN
+
+#include <sys/types.h>
+
+#include <sandbox.h>
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "log.h"
+#include "sandbox.h"
+#include "xmalloc.h"
+
+/* Darwin/OS X sandbox */
+
+struct ssh_sandbox {
+	pid_t child_pid;
+};
+
+struct ssh_sandbox *
+ssh_sandbox_init(void)
+{
+	struct ssh_sandbox *box;
+
+	/*
+	 * Strictly, we don't need to maintain any state here but we need
+	 * to return non-NULL to satisfy the API.
+	 */
+	debug3("%s: preparing Darwin sandbox", __func__);
+	box = xcalloc(1, sizeof(*box));
+	box->child_pid = 0;
+
+	return box;
+}
+
+void
+ssh_sandbox_child(struct ssh_sandbox *box)
+{
+	char *errmsg;
+	struct rlimit rl_zero;
+
+	debug3("%s: starting Darwin sandbox", __func__);
+	if (sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED,
+	    &errmsg) == -1)
+		fatal("%s: sandbox_init: %s", __func__, errmsg);
+
+	/*
+	 * The kSBXProfilePureComputation still allows sockets, so
+	 * we must disable these using rlimit.
+	 */
+	rl_zero.rlim_cur = rl_zero.rlim_max = 0;
+	if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
+		fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
+			__func__, strerror(errno));
+	if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
+		fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
+			__func__, strerror(errno));
+	if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
+		fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
+			__func__, strerror(errno));
+}
+
+void
+ssh_sandbox_parent_finish(struct ssh_sandbox *box)
+{
+	free(box);
+	debug3("%s: finished", __func__);
+}
+
+void
+ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
+{
+	box->child_pid = child_pid;
+}
+
+#endif /* SANDBOX_DARWIN */


More information about the openssh-unix-dev mailing list