Commit Graph

681 Commits

Author SHA1 Message Date
Omar Polo 9bb2f62e24 tweak landlock comment 2021-10-08 15:55:48 +00:00
Omar Polo 807869c14e print the error too if we can't open a directory
It's not intuitive to print

	open ... for domain xyz

it doesn't convey that the open failed.

now it appends the error string, at least the user can understand that
something went wrong.

reported by cage on irc, thanks!
2021-10-07 17:19:45 +00:00
Omar Polo 492a274fd7 add compat for sys/tree.h 2021-10-07 11:36:25 +00:00
Omar Polo 207b3e80d8 Store clients inside a splay tree
From day one we've been using a static array of client struct to hold
the clients data.  This has variuos drawbacks, among which:

 * reuse of the storage  ("shades of heartbleed")
 * maximum fixed amount of clients connected at the same time
 * bugs are harder to debug

The last point in particular is important because if we mess the client
ids, or try to execute some functions (e.g. the various fcgi_*) after a
client has been disconnected, it's harder to "see" this "use after
free"-tier kind of bug.

Now I'm using a splay tree to hold the data about the live connections.
Each client' data is managed by malloc.  If we try to access a client
data after the disconnection we'll probably crash with a SIGSEGV and
find the bug is more easy.  

Performance-wise the connection phase should be faster since we don't
have to loop anymore to find an empty spot in the clients array, but
some operations could be slightly slower (compare the O(1) access in an
array with a SPLAY_FIND operation -- still be faster than O(n) thought.)
2021-10-07 11:20:34 +00:00
Omar Polo 4cd2520965 one FastCGI connection per client
FastCGI is designed to multiplex requests over a single connection, so
ideally the server can open only one connection per worker to the
FastCGI application and that's that.

Doing this kind of multiplexing makes the code harder to follow and
easier to break/leak etc on the gmid side however.  OpenBSD' httpd
seems to open one connection per client, so why can't we too?

One connection per request is still way better (lighter) than using
CGI, and we can avoid all the pitfalls of the multiplexing (keeping
track of "live ids", properly shut down etc...)
2021-10-07 10:47:02 +00:00
Omar Polo 3096da4ef4 allow to run only a subset of the runtime tests
with
	make TESTS='test_1 test_2 ...' regress

now it's possible to run only that specified subset of tests.  It's
really useful during debugging :)
2021-10-07 08:55:44 +00:00
Omar Polo e4daebe44a plug a memory leak
c->req is set in client_read but never deallocated
2021-10-06 17:38:37 +00:00
Omar Polo 807a80cb9e fmt 2021-10-06 16:36:31 +00:00
Omar Polo b4c6cd9768 add the upload target to ease publishing the site 2021-10-04 13:01:41 +00:00
Omar Polo 9212cf1ba9 [gemini] tweak the contrib page
I find it more readable with some empty lines here and there
2021-10-04 12:57:46 +00:00
Omar Polo eb82dcfbf4 improve the service file usage instructions
Thanks Martin for providing these information :)
2021-10-04 12:56:11 +00:00
Omar Polo 12866f1911 add targets to serve the site locally 2021-10-04 12:54:46 +00:00
Omar Polo ae6870fa3b import the capsule/website 2021-10-04 10:42:35 +00:00
Omar Polo 568419b2c1 add .cirrus.yml
Add a cirrus CI config file that runs the regression suite on linux
amd64/aarch64 and on freebsd.
2021-10-04 10:05:34 +00:00
Omar Polo 6e0f14d51e re-add sha script; it's used in the Makefile
While there, use it in the tests too
2021-10-04 09:40:05 +00:00
Omar Polo 2072343d6b sync changelog 2021-10-04 09:35:17 +00:00
Omar Polo 260becda9c reduced the timeout time for single checks 2021-10-04 09:34:39 +00:00
Omar Polo d046e4d6b5 copy only `len' bytes, not the whole buffer
We ended up copying too much data from the fastcgi process.
2021-10-04 09:31:43 +00:00
Omar Polo 4a2a525d7c allow running only specific tests
It's now possible to run only a subset of the tests with:

	./runtime test1 test2 ...
2021-10-04 09:30:18 +00:00
Omar Polo c1272f63e4 sync 2021-10-04 09:08:21 +00:00
Omar Polo 99c91b4a51 remove unused script 2021-10-04 09:07:42 +00:00
Omar Polo 176179b2a9 rework the regression suite
The tests are still there, the suite is equivalent to the old one, but
this one is better structured.

The biggest annoyance I had with the old one was that it wasn't
straightforward to test only a specific set of tests.  It's still
impossible, but it's way easier to do it now.

This extract all the tests to their own functions.  It's overall
better in all possible regards.
2021-10-04 09:04:33 +00:00
Omar Polo c28994868e update clean target 2021-10-02 19:35:30 +00:00
Omar Polo a49800c86a sync 2021-10-02 17:21:26 +00:00
Omar Polo acafce5b7d libevent2 fix: unfreeze the client evbuffer
libevent2 has this concept of "freezeness" of a buffer.  It's a way to
avoid accidentally write/remove data from the wrong "edge" of the
buffer.  The client_tls_{read,write} functions need to add/drain data
from the opposite edge, hence the need for the unfreeze call.

This is the minimum change in order to work on libevent2 too.  Another
way would be to define evbuffer_{un,}freeze as NOP on libevent 1, but
it's ugly IMHO.
2021-10-02 17:20:56 +00:00
Omar Polo efb6210d77 improve libevent2 handling
* add configure check
* change the way the headers are required (copied from tmux)
2021-10-02 17:20:56 +00:00
Omar Polo d0071d8321 verbose logging for getnameinfo 2021-10-02 17:20:56 +00:00
Omar Polo 827cc37cff update tests
* we don't add a space before the lang anymore
* we're more strict in CGI handling: the `invalid' CGI script now
  triggers a 42 CGI ERROR
2021-10-02 17:20:56 +00:00
Omar Polo fa0299a26d drop now unused trim_req_iri 2021-10-02 17:20:56 +00:00
Omar Polo efe7d18029 new I/O handling on top of bufferevents
This is a big change in how gmid handles I/O.  Initially we used a
hand-written loop over poll(2), that then was evolved into something
powered by libevent basic API.  This meant that there were a lot of
small "asynchronous" function that did one step, eventually scheduling
the re-execution, that called each others in a chain.

The new implementation revolves completely around libevent'
bufferevents.  It's more clear, as everything is implemented around the
client_read and client_write functions.

There is still space for improvements, like adding timeouts for one, but
it's solid enough to be committed as is and then further improved.
2021-10-02 17:20:56 +00:00
Omar Polo 403c422041 [cgi] switch from pipe(2) to socketpair(2)
We can't use normal pipe(2)s with libevent in some cases.  Switch to
socketpair(2), which doesn't have the same problem.

This has the drawback that it doesn't prevent the CGI script from
reading stdout, for instance.  (sockets are two-way, pipes only one-way)
2021-10-02 17:20:56 +00:00
Omar Polo b618111a68 log more details for FastCGI errors
add the reported request id if there's a mismatch and both the gai error
and the errno value if getnameinfo fails.
2021-10-02 17:20:10 +00:00
Omar Polo 5f37f9c20d simplify error check 2021-10-02 17:20:10 +00:00
Omar Polo c016b65ca9 typo 2021-10-02 17:20:10 +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 2a44a2ab6e sync changelog 2021-09-26 17:00:16 +00:00
Omar Polo 741b69be96 fastcgi completely asynchronous
This changes the fastcgi implementation from a blocking I/O to an
async implementation on top of libevent' bufferevents.

Should improve the responsiveness of gmid especially when using remote
fastcgi applications.
2021-09-26 17:00:07 +00:00
Omar Polo 83fe545a2b initialize mbufhead 2021-09-26 16:43:19 +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 775ef04f82 mention the thanks to cage for the bugfix
Since I was in a hurry, I forgot to mention it in the tag message :/
2021-09-24 14:21:16 +00:00
Omar Polo 0d9a5b7a18 sync release 2021-09-24 11:16:32 +00:00
Omar Polo 3571854e94 fix possible out-of-bound access
While computing the parent directory it an out-of-bound access can
occur, which usually means the server process dies.

In particular, it can be triggered by making a request for a
non-existent file in the root of a virtual host if the path matches
the `cgi` pattern.

Thanks cage for helping in debugging!
2021-09-24 10:48:51 +00:00
Omar Polo 353e3c8ebe style 2021-09-24 08:16:28 +00:00
Omar Polo e15fc95736 change struct initialization
makes more explicit which fields we're setting.

(and kill an extra empty line)
2021-09-24 08:12:40 +00:00
Omar Polo 81e0f00078 fmt 2021-09-24 08:10:07 +00:00
Omar Polo df0c2926cc use memset(3) rather than bzero(3)
There's no difference, but bzero(3) says

STANDARDS
     The bzero() function conforms to the X/Open System Interfaces option of
     the IEEE Std 1003.1-2004 (“POSIX.1”) specification.  It was removed from
     the standard in IEEE Std 1003.1-2008 (“POSIX.1”), which recommends using
     memset(3) instead.

so here we are.
2021-09-24 08:08:49 +00:00
Omar Polo a91ad7f2ff drop unnecessary bzero
the whole struct client is already memset'd to 0 in do_accept.
handle_handshake doesn't touch the request or iri buffer in the code
path that leads to handle_open_conn.  (It does so in the error router
alone.)
2021-09-24 08:08:49 +00:00
Omar Polo 79288c8b60 making more explicit the case of missing SNI
Missing SNI (i.e. servname == NULL) is already handled correctly.
puny_decode refuses to work on NULL servname, c->domain is still the
empty string and everything flows as expected towards the error at the
end.  However, it's better to bail out early and make more explicit
how the case of missing SNI is handled.
2021-09-24 07:40:24 +00:00