Skip to contentSkip to conversation

key: Unlink keys when revoking

PATCH In review Andrew Zaborowski 2 participants 5 messages Jan 17, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 key: Unlink keys when revoking

As an optimization unlink the key in l_key_free and l_keyring_free same as we do in l_key(ring)_free_norevoke, in addition to the actual revocation. This happens to release the quota spot used by the key much faster under some circumstances.

---
 ell/key.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
ell/key.c +15 -0
index 06c7a98..587ee05 100644
@@ -313,6 +313,20 @@ LIB_EXPORT void l_key_free(struct l_key *key)
 	if (unlikely(!key))
 		return;
 
+	/*
+	 * Unlinking the key from internal_keyring, while not necessary,
+	 * causes the kernel's key garbage collector to run much sooner
+	 * than if the key is only revoked, at least under some kernel
+	 * versions and when the key is not linked to any other ring.
+	 * In practice this helps the user processes stay under the
+	 * per-user key quota (kernel.keys.maxkeys) which is difficult
+	 * to control when there's no way to reclaim the spot occupied
+	 * by the freed key until the GC runs and not even a way to
+	 * wait for the GC.  That means that running many key
+	 * operations too fast may cause add_key to start failing
+	 * independent of how high the quota has been set.
+	 */
+	kernel_unlink_key(key->serial, internal_keyring);
 	kernel_revoke_key(key->serial);
 
 	l_free(key);
@@ -700,6 +714,7 @@ LIB_EXPORT void l_keyring_free(struct l_keyring *keyring)
 	if (unlikely(!keyring))
 		return;
 
+	kernel_unlink_key(keyring->serial, internal_keyring);
 	kernel_revoke_key(keyring->serial);
 
 	l_free(keyring);
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/4 tls: Fix error check typo

---
 ell/tls-suites.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ell/tls-suites.c +1 -1
index d99f08b..1c3976e 100644
@@ -1063,7 +1063,7 @@ static void tls_handle_dhe_client_key_xchg(struct l_tls *tls,
 	}
 
 	params->public = l_key_new(L_KEY_RAW, buf, public_len);
-	if (!params->prime) {
+	if (!params->public) {
 		TLS_DISCONNECT(TLS_ALERT_INTERNAL_ERROR, 0, "l_key_new failed");
 		return;
 	}
Signature · 1 line
2.19.1
#2 lore raw

PATCH 3/4 unit: Drop list of cipher suites from test-tls

In reply to Andrew Zaborowski

Drop the incomplete list of cipher suite names to test and use the authoritative array of suites declared in tls-private.h.

---
 unit/test-tls.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
unit/test-tls.c +4 -14
index b372474..ebca9aa 100644
@@ -620,18 +620,7 @@ static void test_tls_suite_test(const void *data)
 
 int main(int argc, char *argv[])
 {
-	const char *suite_names[] = {
-		"TLS_RSA_WITH_RC4_128_MD5",
-		"TLS_RSA_WITH_RC4_128_SHA",
-		"TLS_RSA_WITH_3DES_EDE_CBC_SHA",
-		"TLS_RSA_WITH_AES_128_CBC_SHA",
-		"TLS_RSA_WITH_AES_256_CBC_SHA",
-		"TLS_RSA_WITH_AES_128_CBC_SHA256",
-		"TLS_RSA_WITH_AES_256_CBC_SHA256",
-		"TLS_RSA_WITH_AES_128_GCM_SHA256",
-		"TLS_RSA_WITH_AES_256_GCM_SHA384",
-		NULL
-	}, **name;
+	unsigned int i;
 
 	l_test_init(&argc, &argv);
 
@@ -699,8 +688,9 @@ int main(int argc, char *argv[])
 	l_test_add("TLS connection version mismatch",
 			test_tls_version_mismatch_test, NULL);
 
-	for (name = suite_names; *name; name++)
-		l_test_add(*name, test_tls_suite_test, *name);
+	for (i = 0; tls_cipher_suite_pref[i]; i++)
+		l_test_add(tls_cipher_suite_pref[i]->name, test_tls_suite_test,
+				tls_cipher_suite_pref[i]->name);
 
 done:
 	return l_test_run();
Signature · 1 line
2.19.1
#3 lore raw

PATCH 4/4 unit: Add warning about key quota setting in test-tls

In reply to Andrew Zaborowski
---
 unit/test-tls.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
unit/test-tls.c +25 -0
index ebca9aa..a69e708 100644
@@ -26,6 +26,7 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <unistd.h>
 
 #include <ell/ell.h>
 
@@ -618,9 +619,26 @@ static void test_tls_suite_test(const void *data)
 	test_tls_with_ver(&test, 0, 0);
 }
 
+static int read_int_from_file(const char *path)
+{
+	int ret;
+	FILE *file;
+
+	file = fopen(path, "r");
+	if (!file)
+		return 0;
+
+	if (fscanf(file, "%i", &ret) < 1)
+		ret = 0;
+
+	fclose(file);
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	unsigned int i;
+	int maxkeys;
 
 	l_test_init(&argc, &argv);
 
@@ -667,6 +685,13 @@ int main(int argc, char *argv[])
 		goto done;
 	}
 
+	maxkeys = read_int_from_file(getuid() > 0 ?
+					"/proc/sys/kernel/keys/maxkeys" :
+					"/proc/sys/kernel/keys/root_maxkeys");
+	if (maxkeys && maxkeys < 2000)
+		printf("Running sysctl kernel.keys.%s=2000 is recommended\n",
+			getuid() > 0 ? "maxkeys" : "root_maxkeys");
+
 	l_test_add("TLS connection no auth", test_tls_test,
 			&tls_conn_test_no_auth);
 	l_test_add("TLS connection server auth", test_tls_test,
Signature · 1 line
2.19.1
#4 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 8 lines · Andrew Zaborowski

As an optimization unlink the key in l_key_free and l_keyring_free same as we do in l_key(ring)_free_norevoke, in addition to the actual revocation. This happens to release the quota spot used by the key much faster under some circumstances.

---
  ell/key.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)

All applied, thanks.

Regards,
-Denis