From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#45198: 28.0.50; Sandbox mode Date: Sat, 17 Apr 2021 17:26:03 +0200 Message-ID: <5818DFAA-3A9C-4335-BAAF-1227A02C290A@acm.org> References: Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_2C9CC654-4040-4DAC-ADF8-2E07608D9C8C" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="555"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Philipp Stephani , =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= , Alan Third , Stefan Kangas , Stefan Monnier To: 45198@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Apr 17 17:27:14 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lXmqo-000AaK-As for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Apr 2021 17:27:14 +0200 Original-Received: from localhost ([::1]:38008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lXmqn-00029w-AS for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Apr 2021 11:27:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46910) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lXmqc-00029n-8u for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 11:27:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:60853) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lXmqc-0004bs-1q for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 11:27:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lXmqb-000359-So for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 11:27:01 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 17 Apr 2021 15:27:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45198 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 45198-submit@debbugs.gnu.org id=B45198.161867317111787 (code B ref 45198); Sat, 17 Apr 2021 15:27:01 +0000 Original-Received: (at 45198) by debbugs.gnu.org; 17 Apr 2021 15:26:11 +0000 Original-Received: from localhost ([127.0.0.1]:44166 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lXmpn-000342-FG for submit@debbugs.gnu.org; Sat, 17 Apr 2021 11:26:11 -0400 Original-Received: from mail235c50.megamailservers.eu ([91.136.10.245]:34146 helo=mail56c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lXmpl-00033t-9Z for 45198@debbugs.gnu.org; Sat, 17 Apr 2021 11:26:10 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1618673167; bh=E2VpJKBV344x9O4rNkAJi+JoCk2eganvHNpC15QtSCE=; h=From:Subject:Date:Cc:To:From; b=b2KB5S1cdvRA1opZnAQIpClKkORsnppAnkcLSnFw5w9fonDdGLj+N7RVBGmkNxWdS 2/2cEhL0dF1zCHHsfU4V7qLvCIzwhgnvO/aOoQhhCvs8G/xIo+egw/ECg1X8loD+YO tQbha+X2OYLUtumXogYMl02VoNakHsBOK2htSVhw= Feedback-ID: mattiase@acm.or Original-Received: from stanniol.lan (c-b952e353.032-75-73746f71.bbcust.telenor.se [83.227.82.185]) (authenticated bits=0) by mail56c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 13HFQ4DW025686; Sat, 17 Apr 2021 15:26:06 +0000 X-Mailer: Apple Mail (2.3445.104.17) X-CTCH-RefID: str=0001.0A742F15.607AFE0F.0039, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=FblJO626 c=1 sm=1 tr=0 a=von4qPfY+hyqc0zmWf0tYQ==:117 a=von4qPfY+hyqc0zmWf0tYQ==:17 a=M51BFTxLslgA:10 a=xcPf1q-u3qo2JXHEquoA:9 a=CjuIK1q_8ugA:10 a=1P0XLz_2t6KZdAnhmMkA:9 a=De_Ol2h6w80A:10 a=pHzHmUro8NiASowvMSCR:22 a=Ew2E2A-JSTLzCXPT_086:22 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:204217 Archived-At: --Apple-Mail=_2C9CC654-4040-4DAC-ADF8-2E07608D9C8C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Slightly updated patch for macOS. Obviously not nearly as fancy as the = seccomp one but for running something in batch mode that reads from = files and writes to stdout/stderr it should do. It works and can be pushed right away but it would be nice to have a = place to use it, for validation and for tuning the interface. Any plans = for that? --Apple-Mail=_2C9CC654-4040-4DAC-ADF8-2E07608D9C8C Content-Disposition: attachment; filename=darwin-sandbox.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="darwin-sandbox.diff" Content-Transfer-Encoding: 7bit diff --git a/lisp/subr.el b/lisp/subr.el index c2be26a15f..4994771c33 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6262,4 +6262,20 @@ internal--format-docstring-line This is intended for internal use only." (internal--fill-string-single-line (apply #'format string objects))) +(when (eq system-type 'darwin) + (defun sandbox-enter (dirs) + "Enter a sandbox only permitting reading files under DIRS. +DIRS is a list of directory names. Most other operations such as +writing files and network access are disallowed. +Existing open descriptors can still be used freely." + (darwin-sandbox-init + (concat "(version 1)\n" + "(deny default)\n" + ;; Emacs seems to need /dev/null; allowing it does no harm. + "(allow file-read* (path \"/dev/null\"))\n" + (mapconcat (lambda (dir) + (format "(allow file-read* (subpath %S))\n" dir)) + dirs "")))) + ) + ;;; subr.el ends here diff --git a/src/sysdep.c b/src/sysdep.c index d940acc4e0..b6c402ba33 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -4286,8 +4286,33 @@ str_collate (Lisp_Object s1, Lisp_Object s2, } #endif /* WINDOWSNT */ +#ifdef DARWIN_OS + +/* This function prototype is not in the platform header files. */ +int sandbox_init_with_parameters(const char *profile, + uint64_t flags, + const char *const parameters[], + char **errorbuf); + +DEFUN ("darwin-sandbox-init", Fdarwin_sandbox_init, Sdarwin_sandbox_init, + 1, 1, 0, + doc: /* Enter a sandbox whose permitted access is curtailed by PROFILE. +Already open descriptors can be used freely. */) + (Lisp_Object profile) +{ + char *err = NULL; + if (sandbox_init_with_parameters (SSDATA (profile), 0, NULL, &err) != 0) + error ("sandbox error: %s", err); + return Qnil; +} + +#endif /* DARWIN_OS */ + void syms_of_sysdep (void) { defsubr (&Sget_internal_run_time); +#ifdef DARWIN_OS + defsubr (&Sdarwin_sandbox_init); +#endif } diff --git a/test/src/emacs-tests.el b/test/src/emacs-tests.el index 09f9a248ef..a1dc7f3501 100644 --- a/test/src/emacs-tests.el +++ b/test/src/emacs-tests.el @@ -210,4 +210,74 @@ emacs-tests/bwrap/allows-stdout (should (eql status 0))) (should (equal (string-trim (buffer-string)) "Hi")))))) +(defun emacs-tests--darwin-run-sandboxed-emacs (sandbox-dirs body) + "Run Emacs and evaluate BODY, only allowing reads from SANDBOX-DIRS. +If SANDBOX-DIRS is `no-sandbox', then run without sandbox. +Return (EXIT-STATUS . OUTPUT), where OUTPUT is stderr and stdout." + (let ((emacs (expand-file-name invocation-name invocation-directory)) + (process-environment nil)) + (with-temp-buffer + (let* ((prog `(progn + ,@(and (not (eq sandbox-dirs 'no-sandbox)) + `((sandbox-enter ',sandbox-dirs))) + ,@body)) + (res (call-process emacs nil t nil + "--quick" "--batch" + (format "--eval=%S" prog)))) + (cons res (buffer-string)))))) + +(ert-deftest emacs-tests/darwin-sandbox () + (skip-unless (eq system-type 'darwin)) + (emacs-tests--with-temp-file test-file ("test") + (let ((some-text "abcdef") + (other-text "ghijkl") + (test-file (file-truename test-file))) ; resolve symlinks + (with-temp-buffer + (insert some-text) + (write-file test-file)) + + ;; Read the file without allowing its directory -- should fail. + (let ((res-out (emacs-tests--darwin-run-sandboxed-emacs + nil + `((find-file-literally ,test-file) + (message "OK: %s" (buffer-string)))))) + (ert-info ((cdr res-out) :prefix "output: ") + (should-not (equal (car res-out) 0)) + (should-not (string-search some-text (cdr res-out))))) + + ;; Read the file allowing its directory -- should succeed. + (let ((res-out (emacs-tests--darwin-run-sandboxed-emacs + (list (file-name-directory test-file)) + `((find-file-literally ,test-file) + (message "OK: %s" (buffer-string)))))) + (should (equal res-out (cons 0 (format "OK: %s\n" some-text))))) + + ;; Write to the file allowing directory reads -- should fail. + (let ((res-out (emacs-tests--darwin-run-sandboxed-emacs + (list (file-name-directory test-file)) + `((with-temp-buffer + (insert ,other-text) + (write-file ,test-file)))))) + (ert-info ((cdr res-out) :prefix "output: ") + (should-not (equal (car res-out) 0)) + ;; The file should be unchanged. + (let ((contents (with-temp-buffer + (insert-file-literally test-file) + (buffer-string)))) + (should (equal contents some-text))))) + + ;; Write to the file without sandbox -- should succeed. + (let ((res-out (emacs-tests--darwin-run-sandboxed-emacs + 'no-sandbox + `((with-temp-buffer + (insert ,other-text) + (write-file ,test-file)))))) + (ert-info ((cdr res-out) :prefix "output: ") + (should (equal (car res-out) 0)) + ;; The file should be changed. + (let ((contents (with-temp-buffer + (insert-file-literally test-file) + (buffer-string)))) + (should (equal contents other-text)))))))) + ;;; emacs-tests.el ends here --Apple-Mail=_2C9CC654-4040-4DAC-ADF8-2E07608D9C8C--