Subject: Re: bug#45198: 28.0.50; Sandbox mode

From: Philipp Stephani <[email-masked]>
Date: Thu, 17 Dec 2020 14:08:04 +0100

Am Mo., 14. Dez. 2020 um 16:59 Uhr schrieb Mattias Engdegård <[email-masked]>:
>
> 14 dec. 2020 kl. 14.44 skrev Philipp Stephani <[email-masked]>:
>
> > Yes, it's not strictly required (as in, seccomp and unshare nominally
> > work at any point), though I think enabling sandboxing while user code
> > has already run can have confusing/unanticipated consequences. For
> > example, other threads might already be running in parallel, and they
> > would then suddenly be blocked from making some syscalls, potentially
> > in the middle of a critical section or similar.
>
> There shouldn't be many threads running in non-interactive mode,

Dynamic libraries tend to start threads for background work, so while
there aren't that many, they still exist.

> and those that are must be expected to work with the added restrictions because why should they be exempt and what are they doing that we want to forbid anyway? It seems a bit far-fetched and probably not an immediate concern.

Libraries (and Emacs itself) can do arbitrary background processing as
an implementation detail, so we'd need to take that into account. I
agree though that this isn't a problem in practice, and, if at all,
requires some adjustments to the policy.
Just do give an example:
https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_create.c;h=bad4e57a845bd3148ad634acaaccbea08b04dbbd;hb=HEAD#l393
assumes that set_robust_list will work if it worked once. In the case
of an Emacs sandbox, threads are started before entering main (through
dynamic initialization and dynamic linking), so the function assumes
that set_robust_list works. (In that case we can just allow
set_robust_list as it's not dangerous.)

>
> That said, it is very much an implementation matter -- the run-function-in-sandbox Lisp interface seems better than the original enter-sandbox because we get more ways to write the code. Thanks for proposing it!

Sure, I also don't mind adding a load-seccomp-filter function for
post-main invocation. Right now I believe it's not needed though.

>
> > For example, to achieve some amount of capability-based
> > security, you'd open files before sandboxing and then forbid the open
> > syscall, but that's not really possible with the current Emacs API
> > (which doesn't provide any access to open files).
>
> Well, almost -- elisp processes serve some of the purposes of open file descriptors, at least for pipes and sockets.
>
> Is it really is practical to restrict file-system visibility? A spawned byte-compiler will need to read almost arbitrary elisp files (autoload, 'require' calls) whose exact names are only known at runtime. Were you planning to build a name-space from a skeleton populated by load-path mounts?

I haven't tried this out yet, but allowing reads from load-path
entries plus the installation directory should be fine.

>
> My initial thought was simply inhibit pretty much everything except reading files and writing to already open descriptors (or just stdout/stderr), on the grounds that while it would enable an adversary to read anything, exfiltration would be difficult.

Yes, but see my other comment: restricting an open policy after the
fact is much harder than opening up an initially-restrictive one, so
I'd really start with a restrictive one (no file reading allowed
except for allowed directories and files).

>
> (Some side-channels may be worth thinking about: if the machine cannot trust its file servers, it is possible to exfiltrate data to an already compromised server merely by reading. But then there are probably more direct approaches.)
>
> > Even on Unix, a fork that's not immediately followed by an exec or
> > exit tends to not work any more. Lots of libraries nowadays assume
> > that the "weird in-between state" after a fork doesn't exist
> > permanently, and only a small number of async-signal-safe syscalls are
> > guaranteed to work between fork and exec.
>
> Yes, and I'm aware of the difficulties but wouldn't dismiss it out of hand since the gains are attractive. The main trouble stems from fork only bringing the calling thread into the new process, which may cause deadlock if those threads were holding locks which the forked process goes on to acquire later on. (pthread_atfork is supposed to be used by threaded libraries but typically isn't.)

Yes, and because libraries can and do start arbitrary threads, this
issue can't really be mitigated and makes fork without exec extremely
unsafe and largely useless.
The gains are largely realized using threads these days.

>
> It does work given some care (and I have done so in the past to good effect); it's mainly a matter of not touching anything that you don't want to use anyway such as GUI frameworks. In Emacs, this would be done in some sort of become_noninteractive function which ensures that future program flow will not involve any GUI code whatsoever.

I don't think that's enough, even linking against some libraries
already causes background threads to spin up.

>
> Let's see what latency we get from spawning a typically overloaded Emacs configuration first.
>

I'd think that we'd always run the sandboxed Emacs with --quick
--batch and an empty environment (to provide for some reproducibility
and avoid LD_PRELOAD attacks etc.), and then startup tends to be fast
enough (emacs -Q -batch takes ~50 ms on my system).