Commit Graph

78 Commits

Author SHA1 Message Date
Omar Polo 4d9d3093d4 resurrect landlock support
this time targetting ABI level 3; partially based on how claudio@
handled it in rpki-client.  Fun how this bit of code has come full
circle (gmid inspired what I wrote for got, which inspired what was
written for rpki-client, which has come back.)
2023-08-23 20:18:59 +00:00
Omar Polo 226f13ece0 add ability to log to files with log access <path> 2023-07-24 08:50:49 +00:00
Omar Polo eac9287d29 copyright years++ 2023-06-24 10:07:17 +00:00
Omar Polo c5ded53a8e sort pledge promises as per pledge(2) 2023-06-23 15:52:20 +00:00
Omar Polo b692d8bd5b drop `proc' pledge in the main process
unlike the name might suggest, proc_kill() doesn't use kill(2) so
proc is not needed.
2023-06-23 15:52:04 +00:00
Omar Polo 7604fc903a drop questionable #warning 2023-06-13 16:39:32 +00:00
Omar Polo 86693a33ab add a privsep crypto engine
Incorporate the OpenSMTPD' privsep crypto engine.  The idea behind
it is to never load the certificate' private keys in a networked
process, instead they are loaded in a separate process (the `crypto'
one) which signs payloads on the behalf of the server processes.
This way, we greatly reduce the risk of leaking the certificate'
private key should the server process be compromised.

This currently compiles only on LibreSSL (portable fix is in the
way).
2023-06-11 11:03:59 +00:00
Omar Polo 1962764c62 fix sandbox_server_process
it does the unveil(2)ing based on the first config, which breaks
config-reloading.
2023-06-09 10:40:08 +00:00
Omar Polo fc440833ad provide sandbox_main_process on !OpenBSD 2023-06-08 19:41:25 +00:00
Omar Polo c26f2460e4 rework the daemon to do fork+exec
It uses the 'common' proc.c from various OpenBSD-daemons.

gmid grew organically bit by bit and it was also the first place where I
tried to implement privsep.  It wasn't done very well, in fact the
parent process (that retains root privileges) just fork()s a generation
of servers, all sharing *exactly* the same address space.  No good!

Now, we fork() and re-exec() ourselves, so that each process has a fresh
address space.

Some features (require client ca for example) are temporarly disabled,
will be fixed in subsequent commits.  The "ge" program is also
temporarly disabled as it needs tweaks to do privsep too.
2023-06-08 13:59:31 +00:00
Omar Polo 2dd5994ae1 use fatal() in code used in the daemon 2023-06-06 11:52:43 +00:00
Omar Polo eae52ad493 switch to the more usual log.c 2023-06-06 11:46:40 +00:00
Omar Polo 281a8852b3 rename log.[ch] to logger.[ch] 2023-06-06 08:50:54 +00:00
Omar Polo df5058c919 provide a more usual fatal
fatal usually appends the error string.  Add 'fatalx' that doesn't.
Fix callers and move the prototypes to log.h
2023-06-05 17:07:52 +00:00
Omar Polo 1e0b974519 send capsicum/landlock/seccomp hack to Valhalla 2023-05-08 10:27:32 +00:00
Omar Polo 0b62f4842d drop landlock/seccomp and capsicum support
it reached a point where this stuff is not maintenable.  I'd like
to move forward with gmid, but the restriction of capsicum and the
linux environment at large that make landlock unusable (how can you
resolve DNS portably when under landlock?) -and don't get me started
on seccomp- makes it impossible for me to do any work.

So, I prefer removing the crap, resuming working on gmid by cleaning
stuff and consolidating the features, improving various things
etc... and then eventually see how to introduce some sandboxing
again on other systems.  Patches to resume sandboxing are, as always,
welcome!
2023-05-08 10:27:32 +00:00
Omar Polo b24c6fcc1c adjust pledge/unveil on OpenBSD
to connect to unix-domain sockets the `unix' pledge is needed and also
unveil "w".  gmid can't mutate files because it doesn't pledge `wpath'
nor `cpath'.
2022-11-27 10:04:39 +00:00
Omar Polo 534afd0ddc make the various strings in the config fixed-length
will help in future restructuring to have fixed-size objects.
2022-10-05 15:10:44 +00:00
Omar Polo 7600099513 optionally disable the sandbox on some systems
The FreeBSD and Linux' sandbox can't deal with `fastcgi' and `proxy'
configuration rules: new sockets needs to be opened and it's either
impossible (the former) or a huge pain in the arse (the latter).

The sandbox is still always used in case only static files are served.
2022-09-06 16:40:38 +00:00
Omar Polo 1ab7c96bb3 gc sandbox_executor_process 2022-09-06 16:24:45 +00:00
Omar Polo d29a2ee224 get rid of the CGI support
I really want to get rid of the `executor' process hack for CGI scripts
and its escalation to allow fastcgi and proxying to work on non-OpenBSD.

This drops the CGI support and the `executor' process entirely and is
the first step towards gmid 2.0.  It also allows to have more secure
defaults.

On non-OpenBSD systems this means that the sandbox will be deactivated
as soon as fastcgi or proxying are used: you can't open sockets under
FreeBSD' capsicum(4) and I don't want to go thru the pain of making it
work under linux' seccomp/landlock.  Patches are always welcome however.

For folks using CGI scripts (hey, I'm one of you!) not all hope is lost:
fcgiwrap or OpenBSD' slowcgi(8) are ways to run CGI scripts as they were
FastCGI applications.

fixes for the documentation and to the non-OpenBSD sandboxes will
follow.
2022-09-06 16:11:09 +00:00
Omar Polo e5d82d9472 const-ify some tables
matches found with

	% grep -R '=[	 ]*{' . | fgrep -v const
2022-03-19 11:02:42 +00:00
Omar Polo 4f0e893cd3 tightens seccomp filter: allow only openat(O_RDONLY)
be more strict and allow an openat only with the O_RDONLY flag.  This
is kind of redundant with landlock, but still good to have.  Landlock
is not yet widely available and won't kill the process upon policy
violation; furthermore, landlock can be disabled at boot time.

tested on GNU and musl libc on arch and alpine amd64.
2022-02-13 16:20:27 +00:00
Omar Polo 94c5f99ab0 sort syscalls in seccomp filter 2022-02-13 15:32:10 +00:00
Tobias Berger d0e0be1e43 Allow Arch-Armv7 syscalls in sandbox.c 2022-02-13 15:29:33 +01:00
Omar Polo 98c6f8de41 fix landlock usage
Mickaël Salaün, the landlock author, pointed out the same error on the
got implementation.  The assumption that not listed access
capabilities are implicitly denied is completely wrong:

> In a nutshell, the ruleset's handled_access_fs is required for
> backward and forward compatibility (i.e. the kernel and user space may
> not know each other's supported restrictions), hence the need to be
> explicit about the denied-by-default access rights.
2022-02-10 22:29:51 +00:00
Max 63bf54b646 [seccomp] allow ugetrlimit(2), needed by glibc on armv7l 2021-12-11 09:08:50 +00:00
Omar Polo 4842c72d9f fmt 2021-10-18 10:05:55 +00:00
Omar Polo 5eb3fc905f don't work around a missing -Wno-unused-parameter
It's been there for a long time, and it's frankly annoying to pretend
to use parameters.  Most of the time, they're there to satisfy an
interface and nothings more.
2021-10-09 18:54:41 +00:00
Omar Polo f7ee799023 enforce PR_SET_NO_NEW_PRIVS in the logger process
otherwise landlock will refuse to enable itself and the logger process
dies.
2021-10-02 17:20:10 +00:00
Omar Polo 0c66b6ad55 forgot include 2021-09-26 20:01:32 +00:00
Omar Polo 6f27d2595a [seccomp] allow ioctl(FIONREAD)
it's needed by bufferevent_read
2021-09-26 20:00:38 +00:00
Omar Polo cb28978f0a refactor landlock
refactor the landlock-related code into something more manageable.
The only real difference is that before the logger process would try
to landlock itself to "/" without perms, something that landlock
doesn't support (now it enables landlock and then restrict itself,
which is the correct move.)
2021-09-25 08:47:29 +00:00
Omar Polo b0be065390 landlock the logger process too
Disallow everything landlock can handle.  The logger process doesn't
need any fs access (on OpenBSD it runs with pledge("stdio recvfd")).
2021-09-19 17:08:12 +00:00
Omar Polo 0ea22af280 add helper function gmid_create_landlock_rs 2021-09-19 17:08:12 +00:00
Omar Polo 3499ce5a9a landlock the server process
Trying to implement some landlock policies (rules?) where possible.
The server process is, of course, the most dangerous process so start
with that.

The following should be equivalent to the unveil(2) call on OpenBSD:
allows only to read files and directories inside the vhost roots.

I'm assuming seccomp is enabled so I'm not trying to disallow actions
such as LANDLOCK_ACCESS_FS_EXECUTE or LANDLOCK_ACCESS_FS_REMOVE_FILE
which require syscalls that are already disallowed.  I'm only trying
to limit the damage that the currently allowed system calls can do.
e.g. since write(2) is allowed, gmid could modify *any* file it has
access to; this is now forbidden by landlock.

There are still too many #ifdefs for my tastes, but it's still better
than the seccomp code.
2021-09-19 17:08:12 +00:00
Omar Polo a8e1e8d738 typo
Since there was 0 reports in a month can I assume it's not actually
used anywhere?
2021-07-23 06:55:32 +00:00
Omar Polo 21f7d24699 allow fstat64
used by glibc on aarch64.

Found and tested by pine, thanks!
2021-07-23 06:50:30 +00:00
Omar Polo a5d822e542 typo 2021-07-09 08:12:05 +00:00
Omar Polo a8a1f43921 style(9)-ify 2021-07-07 09:46:37 +00:00
Omar Polo 4aa1dd553a typo 2021-07-03 18:30:07 +00:00
Omar Polo b24021d4a2 fix seccomp filter for ppc64le
before we matched ppc64le as ppc64 (which is big ending I presume), so
the seccomp filter would always kill gmid

#4 related
2021-07-03 17:38:50 +00:00
Omar Polo 8bb8cf2ad4 configure: add --disable-sandbox
Calling `configure' with --disable-sandbox will disable the sandbox
support *completely* at compile time.  gmid will still complain at
compile time and during the startup.

Users shouldn't disable the sandbox if possible, but instead report
problem upstream so they get fixed (hopefully.)

#4 related
2021-07-02 09:26:59 +00:00
Omar Polo 137def5ff4 reworked seccomp filter
* SECCOMP_AUDIT_ARCH extended to support more architectures
* relax fcntl policy: allow the syscall regardless of the flags
* wrap every syscall in a ifdef, and add some (statx, fcntl64, ...)
  used in x86

Some bits were taken from dhcpcd[0], thanks!

#4 related


[0]: https://roy.marples.name/git/dhcpcd/blob/HEAD:/src/privsep-linux.c
2021-07-02 09:11:40 +00:00
Omar Polo e952c5052a allow sending fd to log on to the logger process
the logger process now can receive a file descriptor to write logs
to.  At the moment the logic is simple, if it receives a file it logs
there, otherwise it logs to syslog.  This will allow to log on custom
log files.
2021-06-15 08:06:10 +00:00
Omar Polo 8ad1c57024 fastcgi: a first implementation
Not production-ready yet, but it's a start.

This adds a third ``backend'' for gmid: until now there it served
local files or CGI scripts, now FastCGI applications too.

FastCGI is meant to be an improvement over CGI: instead of exec'ing a
script for every request, it allows to open a single connection to an
``application'' and send the requests/receive the responses over that
socket using a simple binary protocol.

At the moment gmid supports three different methods of opening a
fastcgi connection:

 - local unix sockets, with: fastcgi "/path/to/sock"
 - network sockets, with: fastcgi tcp "host" [port]
   port defaults to 9000 and can be either a string or a number
 - subprocess, with: fastcgi spawn "/path/to/program"
   the fastcgi protocol is done over the executed program stdin

of these, the last is only for testing and may be removed in the
future.

P.S.: the fastcgi rule is per-location of course :)
2021-05-09 18:23:36 +00:00
Omar Polo fdea6aa0bc allow ``root'' rule to be specified per-location block 2021-04-30 17:16:34 +00:00
Omar Polo b8e64ccd44 list instead of fixed-size array for vhosts and locations
saves some bytes of memory and removes the limit on the maximum number
of vhosts and location blocks.
2021-03-31 16:32:18 +00:00
Omar Polo e3d81f49cc [seccomp] allow prlimit64
it's needed by getdtablesize, at least on glibc
2021-03-20 09:24:44 +00:00
Omar Polo 62e001b067 move all sandbox-related code to sandbox.c
while there, add capsicum for the logger process
2021-03-20 08:42:08 +00:00