From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philipp Newsgroups: gmane.emacs.bugs Subject: bug#45198: 28.0.50; Sandbox mode Date: Sat, 17 Apr 2021 21:42:31 +0200 Message-ID: References: <5818DFAA-3A9C-4335-BAAF-1227A02C290A@acm.org> <19511709-E42B-4ABD-9823-39EA08A79B1F@gmail.com> <83v98kvr7y.fsf@gnu.org> <9A5BCDF3-6543-46C0-AB56-2311392FC549@gmail.com> <78F41D0B-D2F6-444C-9B5C-9C50CFF2CFBD@acm.org> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4196"; mail-complaints-to="usenet@ciao.gmane.io" Cc: alan@idiocy.org, 45198@debbugs.gnu.org, stefan@marxist.se, =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= , Stefan Monnier To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Apr 17 21:44:40 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 1lXqrv-0000zR-Js for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Apr 2021 21:44:39 +0200 Original-Received: from localhost ([::1]:42410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lXqru-0003EO-H7 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Apr 2021 15:44:38 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39548) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lXqqM-0000ym-Sa for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 15:43:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:33141) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lXqqM-0000EE-Jk for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 15:43:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lXqqM-00019a-Fh for bug-gnu-emacs@gnu.org; Sat, 17 Apr 2021 15:43:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philipp Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 17 Apr 2021 19:43:02 +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.16186885664410 (code B ref 45198); Sat, 17 Apr 2021 19:43:02 +0000 Original-Received: (at 45198) by debbugs.gnu.org; 17 Apr 2021 19:42:46 +0000 Original-Received: from localhost ([127.0.0.1]:44687 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lXqq1-00018z-Pn for submit@debbugs.gnu.org; Sat, 17 Apr 2021 15:42:46 -0400 Original-Received: from mail-ed1-f47.google.com ([209.85.208.47]:33567) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lXqpz-00018m-I3 for 45198@debbugs.gnu.org; Sat, 17 Apr 2021 15:42:40 -0400 Original-Received: by mail-ed1-f47.google.com with SMTP id w18so36215541edc.0 for <45198@debbugs.gnu.org>; Sat, 17 Apr 2021 12:42:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=SUXfrS1TSswhuexYFf3ZyMUDHa1TSug4DSrNBXQ4asw=; b=gWb3r+q6hyuyq3dxa2KxDAui+zdWVaCDe+E+Bw4+BJul8KTJzQ26R8JTB7OfgXegLT W8KYtSe4YegFu5DBTqEBlDrWE7hRydxtK1gswqzjmSZX7NR2ivpA8cBXn9DTdLvpCQm1 pPlesHFUaFkmJPcDRNiE7EAXbHpf+k6vg5C6lehwxcz9r9xmzcG4szqYM1Am8iY/OPqc JynJHOf82VvPMwMjWlhzThsCgRzS0/TxLB5FeZDJ98FR/WKIn16R9HxA76Yv0YPkCRUw hjG8cZbsOm6zlvkt9TOTy6jOBpuuFo3u5+Z8M6Lx+8LNTbp4wg0GQS3CZRVaaU7wAYqB vrRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=SUXfrS1TSswhuexYFf3ZyMUDHa1TSug4DSrNBXQ4asw=; b=AXCWHnza3ARZ3PQVBw0CSC5VMuM7b4wykS8o5ZQ6nNJoBpGcQifahZklIjqUjvg+j8 nGYHiwA6u/mpfkRPu8AOJI/wCcT5MCvFJoeID/W2ijSOiyS7XH9X0HcQsej6OtYRtIi9 lih5PFARuoL0kmU5JzZnEx/ENTMRY3SPuc9J1mgslt1Fw+BWGSUK+5SzND0joHqxuaLl +h2+9gfOHq/UroNcbeO6SXoXBx6P+aGB7Iv6WFEa5tKRoaNitvuu+zCH+HlN7M2tQ8d1 aBgjJ4u53ixDCog1rvmSTaEeU0UT3DCi1usZjQ8ragIBk7tpafonkBl1saWgIu95u7M4 Aeww== X-Gm-Message-State: AOAM5304pXADQJvGhM8nt3OPMeKpKCyobRNdNCS+8MZWlbpnY9ejkgp2 rxtMSTiPoHkjSWunE52jmfk= X-Google-Smtp-Source: ABdhPJyEgAzczoacppgQR0YizxkGm4ZZr0jjAFIIk+eMEX0HkUjJ3BxxYRP25xC8ajpyEtgzGfBLxQ== X-Received: by 2002:a05:6402:4245:: with SMTP id g5mr17098282edb.306.1618688553556; Sat, 17 Apr 2021 12:42:33 -0700 (PDT) Original-Received: from philipps-macbook-pro.fritz.box (p57aafcaa.dip0.t-ipconnect.de. [87.170.252.170]) by smtp.gmail.com with ESMTPSA id d5sm3969607edt.49.2021.04.17.12.42.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Apr 2021 12:42:32 -0700 (PDT) In-Reply-To: <78F41D0B-D2F6-444C-9B5C-9C50CFF2CFBD@acm.org> X-Mailer: Apple Mail (2.3654.60.0.2.21) 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:204261 Archived-At: > Am 17.04.2021 um 20:59 schrieb Mattias Engdeg=C3=A5rd = : >=20 > 17 apr. 2021 kl. 20.21 skrev Stefan Monnier = : >=20 >>> Very reasonable. Or would you prefer having the sandboxing in = flymake? >>=20 >> AFAICT this question refers to a minor implementation detail ;-) >=20 > Of course, sorry. >=20 > Looks like I forgot to attach the updated patch in a previous message. = Here it is. >=20 > <0001-Add-macOS-sandboxing-bug-45198.patch> Looks generally fine, just a few minor comments. > diff --git a/lisp/subr.el b/lisp/subr.el > index c2be26a15f..196512c0c6 100644 > --- a/lisp/subr.el > +++ b/lisp/subr.el Should this maybe be in a platform-specific file such as ns-fns.el (like = dos-fns.el, w32-fns.el)? > @@ -6262,4 +6262,22 @@ internal--format-docstring-line > This is intended for internal use only." > (internal--fill-string-single-line (apply #'format string = objects))) > =20 > +(when (eq system-type 'darwin) > + (defun darwin-sandbox-enter (dirs) I don't think we use the "darwin-" prefix anywhere else in Emacs. Maybe = use a "ns-" prefix. Also I think such internal functions commonly have an "internal" piece = somewhere in their name, e.g. "ns-enter-sandbox-internal". > + "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. > + > +This is not a supported interface and is for internal use only." > + (darwin-sandbox-init Can you also refer to the documents listed below, so that readers aren't = wondering what this syntax means? > + (concat "(version 1)\n" Since this uses Lisp syntax, maybe use (prin1-to-string `(...))? > + "(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)) Are you sure that the string quoting syntaxes are compatible? We really = need to avoid injection attacks here. > + dirs "")))) > + ) > + > ;;; subr.el ends here > diff --git a/src/sysdep.c b/src/sysdep.c > index d940acc4e0..44e8b82bc6 100644 > --- a/src/sysdep.c > +++ b/src/sysdep.c > @@ -4286,8 +4286,40 @@ str_collate (Lisp_Object s1, Lisp_Object s2, > } > #endif /* WINDOWSNT */ > =20 > +#ifdef DARWIN_OS > + > +/* This function prototype is not in the platform header files. > + See = https://reverse.put.as/wp-content/uploads/2011/09/Apple-Sandbox-Guide-v1.0= .pdf > + and = https://chromium.googlesource.com/chromium/src/+/master/sandbox/mac/seatbe= lt_sandbox_design.md */ Thanks, I'd expect at least the Chromium reference to stick around. > +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, Similar comments about naming here; maybe call this = ns-init-sandbox-internal or so. > + 1, 1, 0, > + doc: /* Enter a sandbox whose permitted access is curtailed by = PROFILE. > +Already open descriptors can be used freely. > +PROFILE is a string in the macOS sandbox profile language, > +a set of rules in a Lisp-like syntax. I'd also refer to the Chromium document here, otherwise C-h f for this = function won't be very useful. > + > +This is not a supported interface and is for internal use only. */) > + (Lisp_Object profile) > +{ > + CHECK_STRING (profile); > + char *err =3D NULL; > + if (sandbox_init_with_parameters (SSDATA (profile), 0, NULL, &err) = !=3D 0) If you're using SSDATA, better check that the string doesn't contain = embedded null bytes. Also does this need to be encoded somehow? What happens if the string = contains non-Unicode characters like raw bytes? > + error ("sandbox error: %s", err); This looks like an error that clients could handle, so I'd signal a = specific error symbol here. > + 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 This should probably be in subr-tests.el since it tests a function in = subr.el. > index 09f9a248ef..c1a741c359 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")))))) > =20 > +(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)) > + `((darwin-sandbox-enter = ',sandbox-dirs))) > + ,@body)) > + (res (call-process emacs nil t nil > + "--quick" "--batch" > + (format "--eval=3D%S" prog)))) > + (cons res (buffer-string)))))) This is more of a personal style, feel free to ignore: I like tests that are somewhat repetitive but more decoupled and easier = to read better than tests with factored-out assertions. See e.g. the = arguments in = https://www.maaikebrinkhof.nl/2016/03/repetition-in-testing/ and = https://stackoverflow.com/a/130038. > + > +(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-contents-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-contents-literally = test-file) > + (buffer-string)))) > + (should (equal contents other-text)))))))) These should be four separate tests, as they test four separate things.=