Skip to contentSkip to conversation

Security-related fixes

PATCH In review Chuck Lever 2 participants 19 messages Dec 1 – Dec 2 Handshake upcall for transport layer security

lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

cover Security-related fixes

Address review findings from the new Claude security review tool.

Chuck Lever (9):
tlshd: Require the clientAuth key purpose on client certificates
tlshd: Require the serverAuth key purpose on server certificates
tlshd: Fail the handshake when a configured trust store is missing
tlshd: Accept only "psk" keys in the QUIC server PSK callback
tlshd: Match session tags only after the client certificate verifies
tlshd: Fix the direction of the certificate validity filters
tlshd: Re-arm select() on each pass of the QUIC handshake loop
tlshd: Link a configured keyring only when root owns it
tlshd: Validate DN strings as UTF-8 before glob matching

 src/tlshd/client.c  | 14 ++++++++++++--
 src/tlshd/config.c  | 10 ----------
 src/tlshd/keyring.c | 34 ++++++++++++++++++++++++++++++++++
 src/tlshd/quic.c    | 10 ++++++----
 src/tlshd/server.c  | 25 ++++++++++++-------------
 src/tlshd/tags.c    | 16 ++++++++++++++--
 6 files changed, 78 insertions(+), 31 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH 1/9 tlshd: Require the clientAuth key purpose on client certificates

tlshd_server_x509_verify_function() checks a client's certificate chain with gnutls_certificate_verify_peers3(). With a NULL hostname, that call hands GnuTLS no typed data at all, so GnuTLS does not check the extended key usage of the end-entity certificate. A certificate issued only for TLS server authentication passes, and tlshd reports the peer to the kernel as authenticated. When no server trust store is configured, the trust anchors are the system's public CAs, which now issue serverAuth-only certificates. Any holder of such a certificate is then accepted as an mTLS client by NFSD.

Pass GNUTLS_KP_TLS_WWW_CLIENT to gnutls_certificate_verify_peers(). GnuTLS then rejects an end-entity certificate whose extended key usage extension omits clientAuth and anyPurpose. A certificate that carries no extended key usage extension is still accepted, as RFC 5280 Section 4.2.1.12 permits.

Fixes 9d1b6a30d57c ("tlshd: Basic support for ServerHello")
---
 src/tlshd/server.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
src/tlshd/server.c +5 -5
index 10a63c2a6a63..4cb97b03c4a1 100644
@@ -330,11 +330,15 @@ static int tlshd_server_get_truststore(gnutls_certificate_credentials_t cred)
 static int tlshd_server_x509_verify_function(gnutls_session_t session,
 					     struct tlshd_handshake_parms *parms)
 {
+	gnutls_typed_vdata_st data = {
+		.type	= GNUTLS_DT_KEY_PURPOSE_OID,
+		.data	= (void *)GNUTLS_KP_TLS_WWW_CLIENT,
+	};
 	const gnutls_datum_t *peercerts;
 	unsigned int i, status, num_peercerts;
 	int ret;
 
-	ret = gnutls_certificate_verify_peers3(session, NULL, &status);
+	ret = gnutls_certificate_verify_peers(session, &data, 1, &status);
 	switch (ret) {
 	case GNUTLS_E_SUCCESS:
 		break;
@@ -350,10 +354,6 @@ static int tlshd_server_x509_verify_function(gnutls_session_t session,
 		goto certificate_error;
 	}
 
-	/* To do: Examine extended key usage information here, if we want
-	 * to get picky. Kernel would have to tell us what to look for
-	 * via a netlink attribute. */
-
 	peercerts = gnutls_certificate_get_peers(session, &num_peercerts);
 	if (!peercerts || num_peercerts == 0) {
 		tlshd_log_debug("The peer cert list is empty.");
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 1/9
#2 lore raw

Re: [PATCH v1 1/9] tlshd: Require the clientAuth key purpose on client certificates

Quoted 10 lines · Chuck Lever

tlshd_server_x509_verify_function() checks a client's certificate chain with gnutls_certificate_verify_peers3(). With a NULL hostname, that call hands GnuTLS no typed data at all, so GnuTLS does not check the extended key usage of the end-entity certificate. A certificate issued only for TLS server authentication passes, and tlshd reports the peer to the kernel as authenticated. When no server trust store is configured, the trust anchors are the system's public CAs, which now issue serverAuth-only certificates. Any holder of such a certificate is then accepted as an mTLS client by NFSD.

Quoted 8 lines

Pass GNUTLS_KP_TLS_WWW_CLIENT to gnutls_certificate_verify_peers(). GnuTLS then rejects an end-entity certificate whose extended key usage extension omits clientAuth and anyPurpose. A certificate that carries no extended key usage extension is still accepted, as RFC 5280 Section 4.2.1.12 permits.

Fixes 9d1b6a30d57c ("tlshd: Basic support for ServerHello")

Alistair

Quoted 45 lines

---
 src/tlshd/server.c | 10 +++++-----

 1 file changed, 5 insertions(+), 5 deletions(-)
src/tlshd/server.c +0 -0
index 10a63c2a6a63..4cb97b03c4a1 100644
@@ -330,11 +330,15 @@ static int
 tlshd_server_get_truststore(gnutls_certificate_credentials_t cred)

 static int tlshd_server_x509_verify_function(gnutls_session_t session,
       struct tlshd_handshake_parms *parms)
 {

src/tlshd/server.c +4 -0
+	gnutls_typed_vdata_st data = {
+		.type	= GNUTLS_DT_KEY_PURPOSE_OID,
+		.data	= (void *)GNUTLS_KP_TLS_WWW_CLIENT,
+	};

  const gnutls_datum_t *peercerts;
  unsigned int i, status, num_peercerts;
  int ret;

src/tlshd/server.c +0 -1
-	ret = gnutls_certificate_verify_peers3(session, NULL,

&status);

src/tlshd/server.c +1 -0
+	ret = gnutls_certificate_verify_peers(session, &data, 1,

&status);
  switch (ret) {
  case GNUTLS_E_SUCCESS:
  break;

src/tlshd/server.c +0 -0
@@ -350,10 +354,6 @@ static int
 tlshd_server_x509_verify_function(gnutls_session_t session,
  		goto certificate_error;
  	}

src/tlshd/server.c +0 -1
-	/* To do: Examine extended key usage information here, if we

want

src/tlshd/server.c +0 -1
-	 * to get picky. Kernel would have to tell us what to look

for

src/tlshd/server.c +0 -2
-	 * via a netlink attribute. */
-

  peercerts = gnutls_certificate_get_peers(session, &num_peercerts);
  if (!peercerts || num_peercerts == 0) { tlshd_log_debug("The peer cert list is empty.");

author maintainer
#3 lore raw

PATCH 2/9 tlshd: Require the serverAuth key purpose on server certificates

In reply to Chuck Lever

tlshd_client_verify_peer() checks the server's certificate chain with gnutls_certificate_verify_peers3(), which passes GnuTLS the peer's hostname and nothing else. GnuTLS therefore does not check the extended key usage of the end-entity certificate, and a certificate issued only for TLS client authentication is accepted as a server certificate for a name it covers.

Pass GNUTLS_KP_TLS_WWW_SERVER alongside the hostname. GnuTLS then rejects an end-entity certificate whose extended key usage extension omits serverAuth and anyPurpose. A certificate that carries no extended key usage extension is still accepted.

Fixes 26b539227f24 ("tlshd: Add client-side certificate verification")
---
 src/tlshd/client.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
src/tlshd/client.c +12 -2
index b85b54c77269..eb4f9eacdda0 100644
@@ -368,6 +368,16 @@ tlshd_x509_retrieve_key_cb(gnutls_session_t session,
 static int tlshd_client_verify_peer(gnutls_session_t session,
 				    struct tlshd_handshake_parms *parms)
 {
+	gnutls_typed_vdata_st data[2] = {
+		{
+			.type	= GNUTLS_DT_KEY_PURPOSE_OID,
+			.data	= (void *)GNUTLS_KP_TLS_WWW_SERVER,
+		},
+		{
+			.type	= GNUTLS_DT_DNS_HOSTNAME,
+			.data	= (void *)parms->peername,
+		},
+	};
 	unsigned int status;
 	int ret, dane;
 
@@ -377,8 +387,8 @@ static int tlshd_client_verify_peer(gnutls_session_t session,
 	if (dane > 0)
 		return GNUTLS_E_SUCCESS;
 
-	ret = gnutls_certificate_verify_peers3(session, parms->peername,
-					       &status);
+	ret = gnutls_certificate_verify_peers(session, data,
+					      parms->peername ? 2 : 1, &status);
 	if (ret != GNUTLS_E_SUCCESS) {
 		tlshd_log_gnutls_error(ret);
 		return GNUTLS_E_CERTIFICATE_ERROR;
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 2/9
#4 lore raw

Re: [PATCH v1 2/9] tlshd: Require the serverAuth key purpose on server certificates

Quoted 7 lines · Chuck Lever

tlshd_client_verify_peer() checks the server's certificate chain with gnutls_certificate_verify_peers3(), which passes GnuTLS the peer's hostname and nothing else. GnuTLS therefore does not check the extended key usage of the end-entity certificate, and a certificate issued only for TLS client authentication is accepted as a server certificate for a name it covers.

Quoted 8 lines

Pass GNUTLS_KP_TLS_WWW_SERVER alongside the hostname. GnuTLS then rejects an end-entity certificate whose extended key usage extension omits serverAuth and anyPurpose. A certificate that carries no extended key usage extension is still accepted.

Fixes 26b539227f24 ("tlshd: Add client-side certificate

verification")

Alistair

Quoted 41 lines

---
 src/tlshd/client.c | 14 ++++++++++++--

 1 file changed, 12 insertions(+), 2 deletions(-)
src/tlshd/client.c +0 -0
index b85b54c77269..eb4f9eacdda0 100644
@@ -368,6 +368,16 @@ tlshd_x509_retrieve_key_cb(gnutls_session_t
 session,
  static int tlshd_client_verify_peer(gnutls_session_t session,

      struct tlshd_handshake_parms *parms)
 {

src/tlshd/client.c +10 -0
+	gnutls_typed_vdata_st data[2] = {
+		{
+			.type	= GNUTLS_DT_KEY_PURPOSE_OID,
+			.data	= (void *)GNUTLS_KP_TLS_WWW_SERVER,
+		},
+		{
+			.type	= GNUTLS_DT_DNS_HOSTNAME,
+			.data	= (void *)parms->peername,
+		},
+	};

  unsigned int status;
  int ret, dane;

src/tlshd/client.c +0 -0
@@ -377,8 +387,8 @@ static int
 tlshd_client_verify_peer(gnutls_session_t session,
  	if (dane > 0)
  		return GNUTLS_E_SUCCESS;

src/tlshd/client.c +0 -1
-	ret = gnutls_certificate_verify_peers3(session, parms-
Quoted 1 line

peername,

src/tlshd/client.c +2 -1
-					       &status);
+	ret = gnutls_certificate_verify_peers(session, data,
+					      parms->peername ? 2 :

1, &status);
  if (ret != GNUTLS_E_SUCCESS) { tlshd_log_gnutls_error(ret); return GNUTLS_E_CERTIFICATE_ERROR;

author maintainer
#5 lore raw

PATCH 3/9 tlshd: Fail the handshake when a configured trust store is missing

In reply to Chuck Lever

tlshd_config_get_truststore() returns false when the configured x509.truststore file does not exist, which is the same result it returns when the option is unset. Both callers then load the system trust store in its place. On the server side, a typo in the path or a file removed after configuration turns client authentication over to every public CA, and the only sign is a debug-level message. tlshd_config_get_crl() treats a missing x509.crl file the same way, so revocation checking stops without notice.

Drop the access() checks so both functions return the configured pathname whether or not the file exists. GnuTLS then fails to load the missing file, and the handshake fails. Log that failure on the server side, as the client side already does. An unset option still selects the system trust store.

Fixes ad57063880e1 ("config: supply meaningful error for non-existing pathnames")
Fixes 88a93dc33f41 ("tlshd: Add server-side CRL checking")
Fixes 9ab34c1b1054 ("Add client-side CRL checking")
---
 src/tlshd/config.c | 10 ----------
 src/tlshd/server.c |  4 +++-
 2 files changed, 3 insertions(+), 11 deletions(-)
src/tlshd/config.c +0 -10
index b1fdd0c0e1b2..e62830daec63 100644
@@ -465,11 +465,6 @@ bool tlshd_config_get_truststore(int peer_type, char **bundle)
 					 "x509.truststore", NULL);
 	if (!pathname)
 		return false;
-	if (access(pathname, F_OK)) {
-		tlshd_log_debug("tlshd cannot access \"%s\"", pathname);
-		g_free(pathname);
-		return false;
-	}
 
 	*bundle = strdup(pathname);
 	g_free(pathname);
@@ -502,11 +497,6 @@ bool tlshd_config_get_crl(int peer_type, char **result)
 					 "x509.crl", NULL);
 	if (!pathname)
 		return false;
-	if (access(pathname, F_OK)) {
-		tlshd_log_debug("tlshd cannot access \"%s\"", pathname);
-		g_free(pathname);
-		return false;
-	}
 
 	*result = strdup(pathname);
 	g_free(pathname);
src/tlshd/server.c +3 -1
index 4cb97b03c4a1..341b047cc894 100644
@@ -294,8 +294,10 @@ static int tlshd_server_get_truststore(gnutls_certificate_credentials_t cred)
 		free(pathname);
 	} else
 		ret = gnutls_certificate_set_x509_system_trust(cred);
-	if (ret < 0)
+	if (ret < 0) {
+		tlshd_log_gnutls_error(ret);
 		return ret;
+	}
 	tlshd_log_debug("System trust: Loaded %d certificate(s).", ret);
 
 	if (tlshd_config_get_crl(PEER_TYPE_SERVER, &pathname)) {
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 3/9
#6 lore raw

Re: [PATCH v1 3/9] tlshd: Fail the handshake when a configured trust store is missing

Quoted 15 lines · Chuck Lever

tlshd_config_get_truststore() returns false when the configured x509.truststore file does not exist, which is the same result it returns when the option is unset. Both callers then load the system trust store in its place. On the server side, a typo in the path or a file removed after configuration turns client authentication over to every public CA, and the only sign is a debug-level message. tlshd_config_get_crl() treats a missing x509.crl file the same way, so revocation checking stops without notice.

Drop the access() checks so both functions return the configured pathname whether or not the file exists. GnuTLS then fails to load the missing file, and the handshake fails. Log that failure on the server side, as the client side already does. An unset option still selects the system trust store.

Quoted 5 lines
Fixes ad57063880e1 ("config: supply meaningful error for non-

existing pathnames")

Fixes 88a93dc33f41 ("tlshd: Add server-side CRL checking")
Fixes 9ab34c1b1054 ("Add client-side CRL checking")

Alistair

Quoted 56 lines

---
 src/tlshd/config.c | 10 ----------
 src/tlshd/server.c |  4 +++-

 2 files changed, 3 insertions(+), 11 deletions(-)
src/tlshd/config.c +0 -5
index b1fdd0c0e1b2..e62830daec63 100644
@@ -465,11 +465,6 @@ bool tlshd_config_get_truststore(int peer_type,
 char **bundle)
  					 "x509.truststore", NULL);
  	if (!pathname)
  		return false;
-	if (access(pathname, F_OK)) {
-		tlshd_log_debug("tlshd cannot access \"%s\"",
 pathname);
-		g_free(pathname);
-		return false;
-	}


  *bundle = strdup(pathname);
  g_free(pathname);

src/tlshd/config.c +0 -5
@@ -502,11 +497,6 @@ bool tlshd_config_get_crl(int peer_type, char
 **result)
  					 "x509.crl", NULL);
  	if (!pathname)
  		return false;
-	if (access(pathname, F_OK)) {
-		tlshd_log_debug("tlshd cannot access \"%s\"",
 pathname);
-		g_free(pathname);
-		return false;
-	}


  *result = strdup(pathname);
  g_free(pathname);

src/tlshd/server.c +0 -0
index 4cb97b03c4a1..341b047cc894 100644
@@ -294,8 +294,10 @@ static int
 tlshd_server_get_truststore(gnutls_certificate_credentials_t cred)
  		free(pathname);
  	} else

  ret =
gnutls_certificate_set_x509_system_trust(cred);

src/tlshd/server.c +2 -1
-	if (ret < 0)
+	if (ret < 0) {
+		tlshd_log_gnutls_error(ret);

  return ret;

src/tlshd/server.c +1 -0
+	}

  tlshd_log_debug("System trust: Loaded %d certificate(s).", ret);

  if (tlshd_config_get_crl(PEER_TYPE_SERVER, &pathname)) {

author maintainer
#7 lore raw

PATCH 4/9 tlshd: Accept only "psk" keys in the QUIC server PSK callback

In reply to Chuck Lever

When no "psk" key matches the identity a QUIC client offers, tlshd_quic_server_psk_cb() searches for a "user" key with that description and uses its payload as the PSK. Keys of type "user" are not provisioned as PSKs, and some of them carry public data. nfstlskey stores each certificate as a "user" key named "<identity>:cert", and systemd puts an "invocation_id" key in the session keyring of every service it starts. A client that offers one of those descriptions as its identity, with the public payload as the key, completes the handshake, and tlshd reports the key's serial number to the kernel as the authenticated peer.

Search only for keys of type TLS_DEFAULT_PSK_TYPE, as tlshd_server_psk_cb() already does on the TCP path.

Fixes 43a15fed2f33 ("tlshd: add support for quic handshake")
---
 src/tlshd/server.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
src/tlshd/server.c +1 -6
index 341b047cc894..ab6d7b3b93c2 100644
@@ -727,17 +727,12 @@ static int tlshd_quic_server_psk_cb(gnutls_session_t session, const char *userna
 	key_serial_t psk;
 	long ret;
 
-	ret = tlshd_server_psk_search("psk", username);
-	if (ret >= 0)
-		goto found;
-
-	ret = tlshd_server_psk_search("user", username);
+	ret = tlshd_server_psk_search(TLS_DEFAULT_PSK_TYPE, username);
 	if (ret < 0) {
 		tlshd_log_error("key search error %d %s", errno, username);
 		return -1;
 	}
 
-found:
 	psk = (key_serial_t)ret;
 	if (!tlshd_keyring_get_psk_key(psk, key)) {
 		tlshd_log_error("key load error %d", errno);
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 4/9
#8 lore raw

Re: [PATCH v1 4/9] tlshd: Accept only "psk" keys in the QUIC server PSK callback

Quoted 11 lines · Chuck Lever

When no "psk" key matches the identity a QUIC client offers, tlshd_quic_server_psk_cb() searches for a "user" key with that description and uses its payload as the PSK. Keys of type "user" are not provisioned as PSKs, and some of them carry public data. nfstlskey stores each certificate as a "user" key named "<identity>:cert", and systemd puts an "invocation_id" key in the session keyring of every service it starts. A client that offers one of those descriptions as its identity, with the public payload as the key, completes the handshake, and tlshd reports the key's serial number to the kernel as the authenticated peer.

Quoted 5 lines

Search only for keys of type TLS_DEFAULT_PSK_TYPE, as tlshd_server_psk_cb() already does on the TCP path.

Fixes 43a15fed2f33 ("tlshd: add support for quic handshake")

Alistair

Quoted 30 lines

---
 src/tlshd/server.c | 7 +------

 1 file changed, 1 insertion(+), 6 deletions(-)
src/tlshd/server.c +0 -0
index 341b047cc894..ab6d7b3b93c2 100644
@@ -727,17 +727,12 @@ static int

tlshd_quic_server_psk_cb(gnutls_session_t session, const char *userna key_serial_t psk;
  long ret;

src/tlshd/server.c +1 -5
-	ret = tlshd_server_psk_search("psk", username);
-	if (ret >= 0)
-		goto found;
-
-	ret = tlshd_server_psk_search("user", username);
+	ret = tlshd_server_psk_search(TLS_DEFAULT_PSK_TYPE,

username);
  if (ret < 0) {
  tlshd_log_error("key search error %d %s", errno, username);
  return -1;
  }

-found:
  psk = (key_serial_t)ret;
  if (!tlshd_keyring_get_psk_key(psk, key)) { tlshd_log_error("key load error %d", errno);

author maintainer
#9 lore raw

PATCH 5/9 tlshd: Match session tags only after the client certificate verifies

In reply to Chuck Lever

tlshd_tls13_server_x509_handshake() calls tlshd_tags_match_session() after every handshake. The server only requests a client certificate, so a client may complete the handshake without one. Every filter then fails to match, because there is no certificate to examine, and a tag whose filters are all negated, or that has no filters at all, is marked as matched. tls-session-tags(7) describes tags as the server's recognition of a peer whose certificate is trusted, but an anonymous client receives them.

Match tags only when the handshake succeeded and the verify function recorded a peer ID, which it does only for a verified certificate.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined TLS session tags")
---
 src/tlshd/server.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/tlshd/server.c +3 -1
index ab6d7b3b93c2..206fb9a45401 100644
@@ -487,7 +487,9 @@ static void tlshd_tls13_server_x509_handshake(struct tlshd_handshake_parms *parm
 		}
 	}
 
-	tlshd_tags_match_session(session);
+	/* Assign tags only to a peer whose certificate was verified */
+	if (!parms->session_status && parms->remote_peerids->len)
+		tlshd_tags_match_session(session);
 
 out_deinit_session:
 	gnutls_deinit(session);
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 5/9
#10 lore raw

Re: [PATCH v1 5/9] tlshd: Match session tags only after the client certificate verifies

Quoted 9 lines · Chuck Lever

tlshd_tls13_server_x509_handshake() calls tlshd_tags_match_session() after every handshake. The server only requests a client certificate, so a client may complete the handshake without one. Every filter then fails to match, because there is no certificate to examine, and a tag whose filters are all negated, or that has no filters at all, is marked as matched. tls-session-tags(7) describes tags as the server's recognition of a peer whose certificate is trusted, but an anonymous client receives them.

Quoted 6 lines

Match tags only when the handshake succeeded and the verify function recorded a peer ID, which it does only for a verified certificate.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined

TLS session tags")

Alistair

Quoted 21 lines

---
 src/tlshd/server.c | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)
src/tlshd/server.c +0 -0
index ab6d7b3b93c2..206fb9a45401 100644
@@ -487,7 +487,9 @@ static void

tlshd_tls13_server_x509_handshake(struct tlshd_handshake_parms *parm }
  }

src/tlshd/server.c +1 -1
-	tlshd_tags_match_session(session);
+	/* Assign tags only to a peer whose certificate was verified

*/

src/tlshd/server.c +2 -0
+	if (!parms->session_status && parms->remote_peerids->len)
+		tlshd_tags_match_session(session);


 out_deinit_session:
  gnutls_deinit(session);

author maintainer
#11 lore raw

PATCH 6/9 tlshd: Fix the direction of the certificate validity filters

In reply to Chuck Lever

tls-session-tags(7) documents that an x509.tbs.validity.notBefore filter matches when the certificate's activation time is equal to or later than the threshold, and that an x509.tbs.validity.notAfter filter matches when the expiration time is equal to or earlier than the threshold. Both match functions compare in the opposite direction. A notBefore filter written to admit only certificates issued after a CA key rotation instead admits only those issued before it, and a notAfter filter meant to admit short-lived certificates admits only long-lived ones.

Reverse both comparisons to match the documented behavior.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined TLS session tags")
---
 src/tlshd/tags.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/tlshd/tags.c +2 -2
index 0491ca62b0d7..cd0004fce4f8 100644
@@ -1559,7 +1559,7 @@ tlshd_tags_filter_type_match_x509_tbs_validity_notbefore(struct tlshd_tags_filte
 		goto deinit;
 	}
 
-	res = (filter->fi_time >= activation_time);
+	res = (activation_time >= filter->fi_time);
 
 	tlshd_log_debug("Filter '%s' %s validity.notBefore '%s'",
 			filter->fi_name,
@@ -1595,7 +1595,7 @@ tlshd_tags_filter_type_match_x509_tbs_validity_notafter(struct tlshd_tags_filter
 		goto deinit;
 	}
 
-	res = (filter->fi_time <= expiration_time);
+	res = (expiration_time <= filter->fi_time);
 
 	tlshd_log_debug("Filter '%s' %s validity.notAfter '%s'",
 			filter->fi_name,
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 6/9
#12 lore raw

Re: [PATCH v1 6/9] tlshd: Fix the direction of the certificate validity filters

Quoted 10 lines · Chuck Lever

tls-session-tags(7) documents that an x509.tbs.validity.notBefore filter matches when the certificate's activation time is equal to or later than the threshold, and that an x509.tbs.validity.notAfter filter matches when the expiration time is equal to or earlier than the threshold. Both match functions compare in the opposite direction. A notBefore filter written to admit only certificates issued after a CA key rotation instead admits only those issued before it, and a notAfter filter meant to admit short-lived certificates admits only long-lived ones.

Quoted 5 lines

Reverse both comparisons to match the documented behavior.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined

TLS session tags")

Alistair

Quoted 30 lines

---
 src/tlshd/tags.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
src/tlshd/tags.c +0 -0
index 0491ca62b0d7..cd0004fce4f8 100644
@@ -1559,7 +1559,7 @@

tlshd_tags_filter_type_match_x509_tbs_validity_notbefore(struct tlshd_tags_filte
  goto deinit;
  }

src/tlshd/tags.c +1 -1
-	res = (filter->fi_time >= activation_time);
+	res = (activation_time >= filter->fi_time);


  tlshd_log_debug("Filter '%s' %s validity.notBefore '%s'", filter->fi_name,

src/tlshd/tags.c +0 -0
@@ -1595,7 +1595,7 @@

tlshd_tags_filter_type_match_x509_tbs_validity_notafter(struct tlshd_tags_filter
  goto deinit;
  }

src/tlshd/tags.c +1 -1
-	res = (filter->fi_time <= expiration_time);
+	res = (expiration_time <= filter->fi_time);


  tlshd_log_debug("Filter '%s' %s validity.notAfter '%s'", filter->fi_name,

author maintainer
#13 lore raw

PATCH 7/9 tlshd: Re-arm select() on each pass of the QUIC handshake loop

In reply to Chuck Lever

tlshd_quic_start_handshake() initializes readfds and tv once, before its receive loop. On Linux, select() overwrites tv with the time remaining and clears the socket from readfds when it times out. Once a total of one second has elapsed waiting for the peer, every later call has an empty set and a zero timeout and returns at once. The loop then spins on EAGAIN from recvmsg() until the handshake timer fires or the socket fails, so a peer that stalls mid-handshake keeps a tlshd child at 100% CPU.

Reset readfds and tv before each call to select().

Fixes 43a15fed2f33 ("tlshd: add support for quic handshake")
---
 src/tlshd/quic.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/tlshd/quic.c +6 -4
index 25813e7385bd..f845b21540f6 100644
@@ -741,13 +741,10 @@ static void tlshd_quic_recv_session_ticket(struct tlshd_quic_conn *conn)
 void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn)
 {
 	int ret, sockfd = conn->parms->sockfd;
-	struct timeval tv = {1, 0};
 	struct tlshd_quic_msg *msg;
+	struct timeval tv;
 	fd_set readfds;
 
-	FD_ZERO(&readfds);
-	FD_SET(sockfd, &readfds);
-
 	if (tlshd_quic_session_configure(conn)) {
 		conn->errcode = EACCES;
 		return;
@@ -775,6 +772,11 @@ void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn)
 	}
 
 	while (!quic_handshake_completed(conn)) {
+		/* select() modifies readfds and tv on return */
+		FD_ZERO(&readfds);
+		FD_SET(sockfd, &readfds);
+		tv.tv_sec = 1;
+		tv.tv_usec = 0;
 		ret = select(sockfd + 1, &readfds, NULL,  NULL, &tv);
 		if (ret < 0) {
 			conn->errcode = errno;
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 7/9
#14 lore raw

Re: [PATCH v1 7/9] tlshd: Re-arm select() on each pass of the QUIC handshake loop

Quoted 9 lines · Chuck Lever

tlshd_quic_start_handshake() initializes readfds and tv once, before its receive loop. On Linux, select() overwrites tv with the time remaining and clears the socket from readfds when it times out. Once a total of one second has elapsed waiting for the peer, every later call has an empty set and a zero timeout and returns at once. The loop then spins on EAGAIN from recvmsg() until the handshake timer fires or the socket fails, so a peer that stalls mid-handshake keeps a tlshd child at 100% CPU.

Quoted 4 lines

Reset readfds and tv before each call to select().

Fixes 43a15fed2f33 ("tlshd: add support for quic handshake")

Alistair

Quoted 38 lines

---
 src/tlshd/quic.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
src/tlshd/quic.c +1 -1
index 25813e7385bd..f845b21540f6 100644
@@ -741,13 +741,10 @@ static void
 tlshd_quic_recv_session_ticket(struct tlshd_quic_conn *conn)
  void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn)
  {
  	int ret, sockfd = conn->parms->sockfd;
-	struct timeval tv = {1, 0};
  	struct tlshd_quic_msg *msg;
+	struct timeval tv;
  	fd_set readfds;

src/tlshd/quic.c +0 -3
-	FD_ZERO(&readfds);
-	FD_SET(sockfd, &readfds);
-

  if (tlshd_quic_session_configure(conn)) {
  conn->errcode = EACCES;
  return;

src/tlshd/quic.c +0 -0
@@ -775,6 +772,11 @@ void tlshd_quic_start_handshake(struct
 tlshd_quic_conn *conn)
  	}


  while (!quic_handshake_completed(conn)) {

src/tlshd/quic.c +5 -0
+		/* select() modifies readfds and tv on return */
+		FD_ZERO(&readfds);
+		FD_SET(sockfd, &readfds);
+		tv.tv_sec = 1;
+		tv.tv_usec = 0;

  ret = select(sockfd + 1, &readfds, NULL,  NULL, &tv);
  if (ret < 0) {
  conn->errcode = errno;

author maintainer
#15 lore raw

PATCH 8/9 tlshd: Link a configured keyring only when root owns it

In reply to Chuck Lever

tlshd_keyring_link() looks up each keyring named by the keyrings option in [authenticate] by type and description, then links the result into the session keyring that every handshake child shares. find_key_by_type_and_desc() falls back to scanning /proc/keys, and any local user can create a keyring with a chosen description and grant others permission to link it. If tlshd's own keyrings do not reach the administrator's keyring, the scan can return the local user's keyring instead. The server PSK callbacks then search that keyring for the identity a client offers, so a PSK planted there authenticates a remote peer.

Refuse to link a keyring that uid 0 does not own. Keyrings whose names start with '.' skip the check, because add_key() and keyctl_join_session_keyring() refuse such names from user space. The kernel creates .nfs with the owner of the network namespace's user namespace as its owner, and tlshd does not always see that owner as uid 0.

Fixes a70f14354543 ("keyring: merge keyring lookup in tlshd_keyring_link_session()")
---
 src/tlshd/keyring.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
src/tlshd/keyring.c +34 -0
index dd65d0c5b178..570540b43eeb 100644
@@ -245,6 +245,34 @@ out:
 	return serial;
 }
 
+/**
+ * @brief Check that root owns a key
+ * @param[in]     serial  serial number of the key to check
+ *
+ * Any user can create a keyring with a given description, so a
+ * keyring found by name is trusted only when root owns it.
+ *
+ * @retval true   The key is owned by uid 0
+ * @retval false  The key is owned by another user, or the check failed
+ */
+static bool tlshd_keyring_owned_by_root(key_serial_t serial)
+{
+	unsigned long uid;
+	char *desc, *p;
+	bool ret = false;
+
+	/* The description has the form "type;uid;gid;perm;description" */
+	if (keyctl_describe_alloc(serial, &desc) < 0)
+		return false;
+	p = strchr(desc, ';');
+	if (p) {
+		uid = strtoul(p + 1, &p, 10);
+		ret = (*p == ';' && uid == 0);
+	}
+	free(desc);
+	return ret;
+}
+
 /**
  * @brief Link a named keyring into one of the caller's keyrings
  * @param[in]     keyring  name of the keyring to be linked
@@ -272,6 +300,12 @@ int tlshd_keyring_link(const char *keyring, key_serial_t dest)
 		tlshd_log_debug("Failed to find keyring '%s'\n", keyring);
 		return -1;
 	}
+	/* User space cannot create a keyring whose name starts with '.' */
+	if (keyring[0] != '.' && !tlshd_keyring_owned_by_root(serial)) {
+		tlshd_log_error("Keyring '%s' (%lx) is not owned by root\n",
+				keyring, serial);
+		return -1;
+	}
 
 	ret = keyctl_link(serial, dest);
 	if (ret < 0) {
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 8/9
#16 lore raw

Re: [PATCH v1 8/9] tlshd: Link a configured keyring only when root owns it

Quoted 18 lines · Chuck Lever

tlshd_keyring_link() looks up each keyring named by the keyrings option in [authenticate] by type and description, then links the result into the session keyring that every handshake child shares. find_key_by_type_and_desc() falls back to scanning /proc/keys, and any local user can create a keyring with a chosen description and grant others permission to link it. If tlshd's own keyrings do not reach the administrator's keyring, the scan can return the local user's keyring instead. The server PSK callbacks then search that keyring for the identity a client offers, so a PSK planted there authenticates a remote peer.

Refuse to link a keyring that uid 0 does not own. Keyrings whose names start with '.' skip the check, because add_key() and keyctl_join_session_keyring() refuse such names from user space. The kernel creates .nfs with the owner of the network namespace's user namespace as its owner, and tlshd does not always see that owner as uid 0.

Quoted 3 lines
Fixes a70f14354543 ("keyring: merge keyring lookup in

tlshd_keyring_link_session()")

Alistair

Quoted 63 lines

---
 src/tlshd/keyring.c | 34 ++++++++++++++++++++++++++++++++++

 1 file changed, 34 insertions(+)
src/tlshd/keyring.c +0 -0
index dd65d0c5b178..570540b43eeb 100644
@@ -245,6 +245,34 @@ out:
  	return serial;
  }


+/**
+ * @brief Check that root owns a key
+ * @param[in]     serial  serial number of the key to check
+ *
+ * Any user can create a keyring with a given description, so a
+ * keyring found by name is trusted only when root owns it.
+ *
+ * @retval true   The key is owned by uid 0
+ * @retval false  The key is owned by another user, or the check failed
+ */

src/tlshd/keyring.c +7 -0
+static bool tlshd_keyring_owned_by_root(key_serial_t serial)
+{
+	unsigned long uid;
+	char *desc, *p;
+	bool ret = false;
+
+	/* The description has the form

"type;uid;gid;perm;description" */

src/tlshd/keyring.c +11 -0
+	if (keyctl_describe_alloc(serial, &desc) < 0)
+		return false;
+	p = strchr(desc, ';');
+	if (p) {
+		uid = strtoul(p + 1, &p, 10);
+		ret = (*p == ';' && uid == 0);
+	}
+	free(desc);
+	return ret;
+}
+

 /**
  * @brief Link a named keyring into one of the caller's keyrings
  * @param[in]     keyring  name of the keyring to be linked

src/tlshd/keyring.c +1 -0
@@ -272,6 +300,12 @@ int tlshd_keyring_link(const char *keyring,
 key_serial_t dest)
  		tlshd_log_debug("Failed to find keyring '%s'\n",
 keyring);
  		return -1;
  	}
+	/* User space cannot create a keyring whose name starts with

'.' */

src/tlshd/keyring.c +1 -0
+	if (keyring[0] != '.' &&

!tlshd_keyring_owned_by_root(serial)) {

src/tlshd/keyring.c +1 -0
+		tlshd_log_error("Keyring '%s' (%lx) is not owned by

root\n",

src/tlshd/keyring.c +3 -0
+				keyring, serial);
+		return -1;
+	}


  ret = keyctl_link(serial, dest);
  if (ret < 0) {

author maintainer
#17 lore raw

PATCH 9/9 tlshd: Validate DN strings as UTF-8 before glob matching

In reply to Chuck Lever

The subject and issuer filters pass the DN that gnutls_x509_crt_get_dn3() renders straight to g_pattern_spec_match(), which takes a UTF-8 string. GnuTLS copies the bytes of a UTF8String or PrintableString attribute through without validating them, so a certificate can present a DN that ends in the lead byte of a multibyte sequence.

GLib advances through the string by the length that each lead byte declares. For a pattern with wildcards at both ends, such as "*,OU=Example,*", g_utf8_next_char() steps over the terminating NUL and the matcher reads past the end of the DN buffer. For a pattern whose last wildcard is nearer the end, such as "CN=*,O=Example", g_pattern_spec_match() first reverses the DN with g_utf8_strreverse(). That function fails an assertion and aborts the process, or, in a GLib built with G_DISABLE_ASSERT, writes up to five bytes before the start of its result buffer.

Treat a DN that is not valid UTF-8 as not matching the filter.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined TLS session tags")
---
 src/tlshd/tags.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
src/tlshd/tags.c +12 -0
index cd0004fce4f8..1863f4b34510 100644
@@ -1515,6 +1515,12 @@ tlshd_tags_filter_type_match_x509_tbs_issuer(struct tlshd_tags_filter *filter,
 		tlshd_log_gnutls_error(ret);
 		goto deinit;
 	}
+	if (!g_utf8_validate((const gchar *)dn.data, dn.size, NULL)) {
+		tlshd_log_debug("Filter '%s' did not match: issuer is not valid UTF-8",
+				filter->fi_name);
+		gnutls_free(dn.data);
+		goto deinit;
+	}
 
 #ifdef HAVE_GLIB_G_PATTERN_SPEC_MATCH
 	res = g_pattern_spec_match(filter->fi_pattern_spec, dn.size,
@@ -1631,6 +1637,12 @@ tlshd_tags_filter_type_match_x509_tbs_subject(struct tlshd_tags_filter *filter,
 		tlshd_log_gnutls_error(ret);
 		goto deinit;
 	}
+	if (!g_utf8_validate((const gchar *)dn.data, dn.size, NULL)) {
+		tlshd_log_debug("Filter '%s' did not match: subject is not valid UTF-8",
+				filter->fi_name);
+		gnutls_free(dn.data);
+		goto deinit;
+	}
 
 #ifdef HAVE_GLIB_G_PATTERN_SPEC_MATCH
 	res = g_pattern_spec_match(filter->fi_pattern_spec, dn.size,
Signature · 1 line
2.56.0
Reviewed-by Alistair Francis on v1 9/9
#18 lore raw

Re: [PATCH v1 9/9] tlshd: Validate DN strings as UTF-8 before glob matching

Quoted 17 lines · Chuck Lever

The subject and issuer filters pass the DN that gnutls_x509_crt_get_dn3() renders straight to g_pattern_spec_match(), which takes a UTF-8 string. GnuTLS copies the bytes of a UTF8String or PrintableString attribute through without validating them, so a certificate can present a DN that ends in the lead byte of a multibyte sequence.

GLib advances through the string by the length that each lead byte declares. For a pattern with wildcards at both ends, such as "*,OU=Example,*", g_utf8_next_char() steps over the terminating NUL and the matcher reads past the end of the DN buffer. For a pattern whose last wildcard is nearer the end, such as "CN=*,O=Example", g_pattern_spec_match() first reverses the DN with g_utf8_strreverse(). That function fails an assertion and aborts the process, or, in a GLib built with G_DISABLE_ASSERT, writes up to five bytes before the start of its result buffer.

Quoted 5 lines

Treat a DN that is not valid UTF-8 as not matching the filter.

Fixes f11519ab4a45 ("tlshd: Match ingress certificates with defined

TLS session tags")

Alistair

Quoted 42 lines

---
 src/tlshd/tags.c | 12 ++++++++++++

 1 file changed, 12 insertions(+)
src/tlshd/tags.c +0 -0
index cd0004fce4f8..1863f4b34510 100644
@@ -1515,6 +1515,12 @@

tlshd_tags_filter_type_match_x509_tbs_issuer(struct tlshd_tags_filter *filter,
  tlshd_log_gnutls_error(ret);
  goto deinit;
  }

src/tlshd/tags.c +1 -0
+	if (!g_utf8_validate((const gchar *)dn.data, dn.size, NULL))

{

src/tlshd/tags.c +1 -0
+		tlshd_log_debug("Filter '%s' did not match: issuer

is not valid UTF-8",

src/tlshd/tags.c +4 -0
+				filter->fi_name);
+		gnutls_free(dn.data);
+		goto deinit;
+	}


 #ifdef HAVE_GLIB_G_PATTERN_SPEC_MATCH
  res = g_pattern_spec_match(filter->fi_pattern_spec, dn.size,

src/tlshd/tags.c +0 -0
@@ -1631,6 +1637,12 @@

tlshd_tags_filter_type_match_x509_tbs_subject(struct tlshd_tags_filter *filter, tlshd_log_gnutls_error(ret); goto deinit;
  }

src/tlshd/tags.c +1 -0
+	if (!g_utf8_validate((const gchar *)dn.data, dn.size, NULL))

{

src/tlshd/tags.c +1 -0
+		tlshd_log_debug("Filter '%s' did not match: subject

is not valid UTF-8",

src/tlshd/tags.c +4 -0
+				filter->fi_name);
+		gnutls_free(dn.data);
+		goto deinit;
+	}


 #ifdef HAVE_GLIB_G_PATTERN_SPEC_MATCH
  res = g_pattern_spec_match(filter->fi_pattern_spec, dn.size,