Commit Graph

1415 Commits

Author SHA1 Message Date
Joris Vink 13f9921679 cleanup 2023-03-01 16:35:11 +01:00
Joris Vink 3e944e106b Change kore_realloc() behaviour a bit.
If kore_realloc() decides that a new block must be allocated it will
explicitly call the new kore_free_zero() function to erase the
contents of the old block once the move is done.
2023-03-01 16:29:06 +01:00
Daniel Melani a5342a0120 Remove libressl log message.
The feature has been removed.
2023-02-22 19:15:42 +01:00
Joris Vink 83d1ff1013 Get rid of the openssl libcrypto allocation hooks.
In practice this rarely works anyway as other libs can end up
allocating things before we even reach main() as demonstrated
on the discord channel earlier.
2023-01-25 08:00:45 +01:00
Joris Vink 9fec810ce8 add lua to built-ins log 2023-01-23 21:56:49 +01:00
Joris Vink 4718bae098 Initial lua runtime.
Works enough so one can do basic configuration and handle HTTP.
2023-01-21 23:41:35 +01:00
Joris Vink 2f5d274059 Rework runtime init a little bit.
It was hardcoded that if KORE_USE_PYTHON was defined we would
look at the passed argument on the command-line as the python
script or module to be run.

This won't work when adding more runtimes.

So instead call a kore_runtime_resolve() function that in
turn calls each available runtime its resolve function.

That resolve function will check if its a script / module
that it can load, and if so will load it.

This way we can remove all those KORE_USE_PYTHON blocks in the
Kore startup path and we pave the way for lua.
2023-01-16 21:00:01 +01:00
Joris Vink d21c0aab5f Call kore_tls_init() earlier at startup. 2023-01-06 10:54:07 +01:00
Joris Vink a421e7a9cd Add memory protection with KORE_MEM_GUARD.
When KORE_MEM_GUARD is set in the environment when Kore is started
it will enable a few memory protection techniques for all kore pools:

1) The metadata is placed away from the actual user pointer returned.

2) Each entry in a pool is placed in such a way that it is followed
   immediately by a guard page which has PROT_NONE. Accessing a guard
   page will cause an immediate crash.

3) Each entry is marked with PROT_NONE until it is allocated. Once it
   is returned to a pool it becomes PROT_NONE again, protecting against
   use after frees.

This commit also removes the magic goo from the mem facitilies such
as kore_malloc and friends and moves these as canaries into the kore
pool facilities instead.

Note that using this will increase memory pressure and decrease performance.

It is recommended to enable this during development to catch bugs.
2023-01-05 22:47:29 +01:00
Joris Vink 922ce7fefb Force OpenSSL to use Kore allocators.
LibreSSL does not support this, which is fair as its designed
for OpenBSD which has malloc and friends that do nice things.
2023-01-05 19:23:24 +01:00
Joris Vink 4ecd6d5603 Add kore_platform_random_uint32(). 2023-01-04 11:48:19 +01:00
Joris Vink 7b48959c32 Python HTTP API improvement: add req.headers()
Calling req.headers() will return all the request headers in
a dictionary to the caller.
2022-12-29 12:58:43 +01:00
Joris Vink 7a6753ca33 Force lowercase on internal HTTP header names. 2022-12-29 12:58:21 +01:00
Joris Vink 46af5aa11b more workarounds for curl 7.87 2022-12-28 16:05:14 +01:00
Joris Vink 7f27677904 work around curl 7.87 curl_easy_setopt() being broken. 2022-12-28 16:04:18 +01:00
Joris Vink f59ee8e29e constify another RSA pointer. 2022-12-28 15:55:19 +01:00
Joris Vink f82e4fe967 kill debug 2022-12-28 15:49:42 +01:00
Joris Vink 8b701a9f0e Change how we handle EVP_PKEY for privsep.
We used to just call EVP_PKEY_get1_RSA() and set the domain
and RSA_METHOD on that.

But with OpenSSL 3, the EVP_PKEY_get1_RSA() function returns a cached
copy of the internal provider struct and any changes we make are not
reflected back. So we can't use it to set the domain and custom method.

Instead just create our own EVP_PKEY from scratch, coupled with an
RSA key that contains just n and e from the public key.

Works with both 1.1.x and 3.0.x.
2022-12-28 15:43:56 +01:00
Joris Vink 17b6f3bbc6 Disable deprecated warnings for OpenSSL 3.
Until the replacement is done, make sure Kore builds against OpenSSL 3
so it can be used as most distros made the move towards it.
2022-12-28 11:09:15 +01:00
Joris Vink 369fc4fa01 Add newfstatat in common Kore seccomp whitelist. 2022-11-02 22:13:10 +01:00
Joris Vink dd93790d80 Improve kore.proc in the Python API.
Allow passing of an env keyword, allowing you to set environment variables
that may be required by the subprocess.

The env keyword must be a list with correctly formed environment variables.

eg:

proc = kore.proc("/bin/myproc",
    env=[
        "LD_LIBRARY_PATH=/my/path"
    ]
)
2022-10-19 08:04:16 +02:00
Joris Vink eab8dcd9ac Constify in parameter for kore_strip_chars() 2022-10-19 07:34:14 +02:00
Joris Vink f34d21caa7 Hack around some hidden Python symbols.
The _PyInterpreterFrame_GetLine() is hidden in dynamic libs so
roll our own variant of it.

Shuffle the old code so we always end up calling python_resolve_frame_line()
no matter the Python version.
2022-09-26 08:48:29 +02:00
Joris Vink d7cef82362 Python improvements: Rework corotracing for 3.11.
In the upcoming Python 3.11 release the PyCoroObject no longer
has a full PyFrameObject, but instead their internal frame
struct _PyInterpreterFrame. Use that when we are building
against 3.11 or higher so we can still provide useful tracing
functionality (and so that it builds).
2022-09-25 00:29:08 +02:00
Joris Vink 783cc6cd4c Send a 413 if header_recv buffer is full.
Kore used to just stall the connection until the timeout kicked
in, but if no proper headers were received by the time the header
buffer is full we should just error out.

While here, use s_off for the inital length check.
2022-09-07 12:41:38 +02:00
Joris Vink 80db912c34 Improve CRL reloading.
When a CRL is reloaded, disconnect any TLS clients that authenticated
with a certificate that is now in the CRL.

Improve log messages while here.
2022-09-05 10:59:28 +02:00
Joris Vink 0de5d5e4c7 Use a more structured log format. 2022-09-05 10:59:06 +02:00
Joris Vink 5dcf3d1550 Fix a problem with http_timeout when a body is sent.
When receiving an HTTP body, Kore never reset http_timeout once
the transfer was done.

This can result in a 408 being thrown by Kore while a request is
activity running.
2022-09-01 12:09:15 +02:00
Joris Vink b3c5ce8375 Demote log message to notice in tls_none backend. 2022-08-26 10:23:13 +02:00
Joris Vink c0d6657c26 Fix a log message regarding worker count.
The worker_count is incremented by 2 earlier to account for keymgr/acme
but aren't actually workers that should count towards CPU pinning.

So adjust the count when comparing to cpu_count when logging that there
are more workers than cpus.
2022-08-26 10:19:26 +02:00
Joris Vink 11cf2075a2 Get rid of PRI_TIME_T defines locally.
Instead do an ugly, use PRId64 and cast the time_t to a signed 64-bit integer.
This'll work on all platforms regardless of how they defined time_t.
2022-08-22 13:17:45 +02:00
Joris Vink 139348c2b7 change PRI_TIME_T to ldd on BSD platforms 2022-08-19 17:00:33 +02:00
Joris Vink a2d312d0a0 kore_debug() has been unused for years.
Kill all useless messages, convert useful ones into kore_log() instead.
2022-08-18 15:20:55 +02:00
Joris Vink 52ff37c5be Support crls for kore.domain() in Python.
The crl keyword is parsed when the client_verify keyword has been set.

eg:

kore.domain("kore.io", cert="cert.pem", key="key.pem",
    client_verify="cacert.pem", verify_depth=1, crl="crl.pem")
2022-08-18 10:43:10 +02:00
Joris Vink 773790c18b fix more fallout from printf format attributes. 2022-08-17 13:19:25 +02:00
Daniel Fahlgren 0dc4d1adaa Remove unused define 2022-08-17 13:16:27 +02:00
Daniel Fahlgren 2211bb8f97 Some platforms doesn't have a /tmp/ directory where temporary files can be
stored. Make it possible to override that location compile time.
2022-08-17 13:16:25 +02:00
Daniel Fahlgren 5ddfc2131c Constify filemap_entry argument to filemap_serve() 2022-08-17 13:16:21 +02:00
Daniel Fahlgren 5e21de3a4c Add printf format attributes and fix fallout 2022-08-17 13:16:18 +02:00
Joris Vink 25ea10fea7 Be better. 2022-08-10 10:20:18 +02:00
Joris Vink 73be741bfd Allow authenticators on filemaps.
This commit introduces the ability to add authenticators to filemaps.
Just like in normal routes, the authenticators will be resolved first
before allowing access to the filemap entries.

Configuration wise, the authenticator is an optional value after the
filemap config directive:

	filemap / webroot myauth

In the Python API you can now pass the authenticator for a filemap entry
but turning the value of the filemap into a tuple with the first entry
being the path and the second being the auth dict:

	AUTH AUTH={
	    "type": "cookie",
	    "value": "cookiename",
	    "redirect": "/auth/",
	    "verify": verify_cookie
	}

	domain.filemaps({
	    "/css/": "webroot/css",
	    "/secret/": ("webroot/secret", AUTH)
	})
2022-08-10 10:13:01 +02:00
Joris Vink e2fcedfaec Differentiate between normal shutdown and fatal.
The parent process never differentiated between a worker process
asking for a shutdown or a worker process calling fatalx() when
it came to its exit code.

I made some changes here so the parent process will exit with
an exit code 1 if anything worker related went wrong (fatalx/death policy).
2022-08-08 11:02:27 +02:00
Joris Vink 7316f372d1 If no result was available, use Py_None. 2022-08-04 13:19:02 +02:00
Joris Vink fd94fba3c7 Python 3.10 improvements.
The coroutines results are now relayed back via PyIter_Send() and
no longer obtainable via _PyGen_FetchStopIterationValue().

This means that our kore.gather() would not be able to return any
values from any of the coroutines it governed.

Fix this by saving the object returned in PyIter_Send() and using it
later in pygather_reap_coro().
2022-08-04 09:56:41 +02:00
Joris Vink 30c8652534 More Python 3.10.x changes.
Handle PYGEN_RETURN in case our prerequest handlers return actual
values instead of None.

Mimic PySendResult for older versions for now.
2022-07-14 11:48:32 +02:00
Joris Vink f6c54042c5 Improve Python 3.10.x support.
Python 3.10.x its PyIter_Send() will return PYGEN_ERROR if the
generator returned (as opposed to yielding) if the result returned
is Py_None and no argument was given to PyIter_Send(). Which is counter
intuitive since it seems it should give us a PYGEN_RETURN as per its
documentation.

It also no longer sets PyExc_StopIteration in this case so we cannot depend
on that anymore to figure out if a coroutine has finished.

This caused Kore to respond with a 500 immediately after coroutines
ran to completion as it was looking for the PyExc_StopIteration exception.

Fix this by simply doing another check if an exception happened before
we enter the code path where Kore would spew the 500.
2022-07-14 10:56:27 +02:00
Joris Vink f7a76f7e96 Improvements for kore.recvmsg() in Python.
The cmsghdr contains a length (cmsg_len) which indicates the length
of the data in combination with the cmsghdr length itself.

Remove the length of the cmsghdr before passing it back up to callers
so they don't need to bother with it.

This also fixes a mistake where we ended up copying extra data
from the ancdata buffer that was unintended.
2022-04-22 17:01:06 +02:00
Joris Vink 3516d50be3 adjust log messages 2022-04-20 11:10:01 +02:00
Joris Vink dcdfce5763 Unbreak x509dict.
I accidentally swapped issuer and subject while constructing it.
2022-04-20 11:06:10 +02:00
Joris Vink 38d7a5f88d Fix handling kore_tls_connection_accept() return codes.
When this code was moved from src/connection.c into src/tls_openssl.c
a return wouldn't break us out from kore_connection_handle() as
previously expected.

This ment that Kore would move the connection into established state
immediately even if SSL_accept() needed to read more.

This broke TLS client authentication as Kore its belts and suspenders
kept throwing a 403 due to the code not properly obtaining the client
certificate when expected.
2022-03-21 12:23:38 +01:00