| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-11-29 00:37:12 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-11-29 00:37:12 UTC |
| parent | b5069a40d401f31cb1661d2b8e6bd7dee34b80b4 |
| doc/man/kxc-cryptsetup.1 | +13 | -1 |
| doc/man/kxc-cryptsetup.1.pod | +14 | -0 |
| doc/man/kxc.1 | +7 | -1 |
| doc/man/kxc.1.pod | +7 | -0 |
| doc/quick_start.md | +13 | -2 |
| kxc/kxc.go | +16 | -1 |
| tests/run_tests | +35 | -3 |
diff --git a/doc/man/kxc-cryptsetup.1 b/doc/man/kxc-cryptsetup.1 index 826ba3e..b4c703f 100644 --- a/doc/man/kxc-cryptsetup.1 +++ b/doc/man/kxc-cryptsetup.1 @@ -58,7 +58,7 @@ .\" ======================================================================== .\" .IX Title "kxc-cryptsetup 1" -.TH kxc-cryptsetup 1 2019-08-10 "" "" +.TH kxc-cryptsetup 1 2026-11-29 "" "" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -94,6 +94,18 @@ Server certificate, used to validate the server. .IP \fI/etc/kxc/NAME.url\fR 8 .IX Item "/etc/kxc/NAME.url" Contains the URL to the key; usually in the form of \f(CW\*(C`kxd://server/name\*(C'\fR. +.SH NOTES +.IX Header "NOTES" +By default, cryptsetup tries to get the key only 3 times, and each try +usually fails within a few seconds if the server is not available. So we +recommend using the \f(CW\*(C`tries=0\*(C'\fR option in \fBcrypttab\fR\|(5), which makes +cryptsetup keep trying until it gets the key. This is useful when the server +can take longer to be available, for example if the client and server boot +at the same time after a power loss. +.PP +Note this also means the boot will not continue if the server never becomes +available. A large number like \f(CW\*(C`tries=100\*(C'\fR waits for a while but eventually +gives up. .SH CONTACT .IX Header "CONTACT" Main website <https://blitiri.com.ar/p/kxd>. diff --git a/doc/man/kxc-cryptsetup.1.pod b/doc/man/kxc-cryptsetup.1.pod index 550837a..0815cee 100644 --- a/doc/man/kxc-cryptsetup.1.pod +++ b/doc/man/kxc-cryptsetup.1.pod @@ -47,6 +47,20 @@ Contains the URL to the key; usually in the form of C<kxd://server/name>. =back +=head1 NOTES + +By default, cryptsetup tries to get the key only 3 times, and each try +usually fails within a few seconds if the server is not available. So we +recommend using the C<tries=0> option in L<crypttab(5)>, which makes +cryptsetup keep trying until it gets the key. This is useful when the server +can take longer to be available, for example if the client and server boot +at the same time after a power loss. + +Note this also means the boot will not continue if the server never becomes +available. A large number like C<tries=100> waits for a while but eventually +gives up. + + =head1 CONTACT L<Main website|https://blitiri.com.ar/p/kxd>. diff --git a/doc/man/kxc.1 b/doc/man/kxc.1 index 7ae0222..aa0beb6 100644 --- a/doc/man/kxc.1 +++ b/doc/man/kxc.1 @@ -58,7 +58,7 @@ .\" ======================================================================== .\" .IX Title "kxc 1" -.TH kxc 1 2019-08-10 "" "" +.TH kxc 1 2026-11-29 "" "" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -92,6 +92,12 @@ PAM format). .IP \fB\-\-server_cert\fR=\fIfile\fR 8 .IX Item "--server_cert=file" File containing valid server certificate (in PAM format). +.IP \fB\-\-timeout\fR=\fIduration\fR 8 +.IX Item "--timeout=duration" +Maximum time to wait for the key, including connecting to the server, and +the server running its hook. Connecting and the TLS handshake are also +limited to 10 seconds each. +Uses Go\*(Aqs duration syntax, like \f(CW\*(C`90s\*(C'\fR or \f(CW\*(C`2m\*(C'\fR (default: 2m). .SH CONTACT .IX Header "CONTACT" Main website <https://blitiri.com.ar/p/kxd>. diff --git a/doc/man/kxc.1.pod b/doc/man/kxc.1.pod index 25becde..a9fbf31 100644 --- a/doc/man/kxc.1.pod +++ b/doc/man/kxc.1.pod @@ -38,6 +38,13 @@ PAM format). File containing valid server certificate (in PAM format). +=item B<--timeout>=I<duration> + +Maximum time to wait for the key, including connecting to the server, and +the server running its hook. Connecting and the TLS handshake are also +limited to 10 seconds each. +Uses Go's duration syntax, like C<90s> or C<2m> (default: 2m). + =back diff --git a/doc/quick_start.md b/doc/quick_start.md index a64337c..fc7096b 100644 --- a/doc/quick_start.md +++ b/doc/quick_start.md @@ -84,8 +84,8 @@ In order to get kxc to be run automatically to fetch the key, we need to edit `/etc/crypttab` and tell it to use a keyscript: ``` -sda2_crypt UUID=blah-blah-blah sda2 luks,keyscript=/usr/bin/kxc-cryptsetup - ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +sda2_crypt UUID=blah-blah-blah sda2 luks,keyscript=/usr/bin/kxc-cryptsetup,tries=0 + ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^ ``` Note the `sda2` field corresponds to the name we've been passing around in @@ -93,6 +93,15 @@ previous sections. The `keyscript=/usr/bin/kxc-cryptsetup` option is our way of telling the cryptsetup infrastructure to use our script to fetch the key for this target. +The `tries=0` option makes cryptsetup keep trying until it gets the key +(by default, it gives up after 3 tries). This is useful when the client and +the server reboot at the same time (for example, after a power loss), and the +server takes longer to come back. On the other hand, if the server is never +available (or it no longer allows the client), the boot will not continue, +and you will need other means (like rescue media) to unlock the device with +one of your other keys. If you prefer to eventually give up, use a large +number instead, like `tries=100`. + You can test that this works by using: @@ -102,6 +111,8 @@ cryptdisks_start sda2_crypt ``` The second command should issue a request to your server to get the key. +Because of `tries=0`, if it can't get the key it will keep retrying, so use +Ctrl-C to stop it. Consider running `update-initramfs -u` if your device is the root device, or it is needed very early in the boot process. diff --git a/kxc/kxc.go b/kxc/kxc.go index 32969da..4565d8a 100644 --- a/kxc/kxc.go +++ b/kxc/kxc.go @@ -15,11 +15,13 @@ import ( "fmt" "io" "log" + "net" "net/http" "net/url" "os" "slices" "strings" + "time" ) const defaultPort = 19840 @@ -30,6 +32,8 @@ var clientCert = flag.String( "client_cert", "", "File containing the client certificate") var clientKey = flag.String( "client_key", "", "File containing the client private key") +var timeout = flag.Duration( + "timeout", 2*time.Minute, "Maximum time to wait for the key") func loadServerCerts() (*x509.CertPool, bool, error) { pemData, err := os.ReadFile(*serverCert) @@ -171,12 +175,23 @@ func main() { var err error flag.Parse() + // Bound connecting and the TLS handshake, so we fail quickly if the + // server is not reachable or not responding. + // We don't set a separate timeout for the response headers, because + // the server runs the hook before replying, which can take a while. + dialer := &net.Dialer{Timeout: 10 * time.Second} tr := &http.Transport{ - TLSClientConfig: makeTLSConf(), + TLSClientConfig: makeTLSConf(), + DialContext: dialer.DialContext, + TLSHandshakeTimeout: 10 * time.Second, } + // Overall timeout for the whole request, including reading the body. + // Without it, a server that stalls would make us (and the boot) hang + // forever. client := &http.Client{ Transport: tr, + Timeout: *timeout, } serverURL, err := extractURL(flag.Arg(0)) diff --git a/tests/run_tests b/tests/run_tests index 8b47eb5..cdd59e6 100755 --- a/tests/run_tests +++ b/tests/run_tests @@ -132,12 +132,13 @@ class Config: with open(key_path + "/email_to", "a") as efd: efd.write(email_to + "\n") - def call(self, server_cert, url): + def call(self, server_cert, url, extra_args=()): args = [ BINS + "/kxc", "--client_cert=%s/cert.pem" % self.path, "--client_key=%s/key.pem" % self.path, "--server_cert=%s" % server_cert, + *extra_args, url, ] try: @@ -283,14 +284,16 @@ class TestCase(unittest.TestCase): self.fail("Timeout waiting for the server") # pylint: disable=invalid-name - def assertClientFails(self, url, regexp, client=None, cert_path=None): + def assertClientFails( + self, url, regexp, client=None, cert_path=None, extra_args=() + ): if client is None: client = self.client if cert_path is None: cert_path = self.server.cert_path() try: - client.call(cert_path, url) + client.call(cert_path, url, extra_args) except subprocess.CalledProcessError as err: self.assertRegex(err.output.decode(), regexp) else: @@ -489,6 +492,35 @@ class CommonErrors(TestCase): ) +class ClientTimeouts(TestCase): + """Tests for the client timeouts.""" + + def assertClientTimesOut(self, url): + start = time.monotonic() + self.assertClientFails( + url, "Client.Timeout exceeded", extra_args=["--timeout=1s"] + ) + self.assertLess(time.monotonic() - start, 5) + + def test_slow_server(self): + """Server that takes too long to reply.""" + # The hook runs before the server replies, so a slow hook makes the + # server stall after the TLS handshake. + path = self.server.path + "/hook" + with open(path, "w") as hook: + hook.write("#!/bin/sh\nsleep 3\n") + os.chmod(path, 0o770) + + self.server.new_key("k1", allowed_clients=[self.client.cert()]) + self.assertClientTimesOut("kxd://localhost/k1") + + def test_stalled_handshake(self): + """Server that accepts the connection, but never replies.""" + with socket.create_server(("localhost", 0)) as sock: + port = sock.getsockname()[1] + self.assertClientTimesOut("kxd://localhost:%d/k1" % port) + + class Multiples(TestCase): """Tests for multiple clients and keys."""