Skip to contentSkip to conversation

ltp: AF_ALG test helpers and a few regression tests

PATCH Superseded Eric Biggers 2 participants 15 messages Feb 21, 2019 – Mar 15, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ltp: AF_ALG test helpers and a few regression tests

Hello,

This series adds helper functions for testing AF_ALG (the userspace interface to algorithms in the Linux kernel's crypto API) to the Linux Test Project. It then adds a few sample regression tests.

I've had these patches sitting around for a while with the goal of also writing some "real" tests that test for more than these specific bugs, but haven't gotten around to it yet. And I now consider further improving the in-kernel crypto self-tests to be a higher priority. But I figured I'd send this out as-is in case people are interested in having this merged, e.g. maybe other people want to add AF_ALG tests to LTP too and the 'tst_af_alg' lib would be a good starting point.

Eric Biggers (6):
lib: add tst_af_alg lib
crypto/af_alg01: new regression test for hmac nesting bug crypto/af_alg02: new regression test for salsa20 empty message bug crypto/af_alg03: new regression test for rfc7539 hash alg validation crypto/af_alg04: new regression test for vmac race conditions crypto/af_alg05: new regression test for skcipher_walk error bug

 configure.ac                       |   1 +
 include/lapi/if_alg.h              |  40 ++++++++
 include/tst_af_alg.h               | 136 ++++++++++++++++++++++++++
 lib/tst_af_alg.c                   | 147 +++++++++++++++++++++++++++++
 runtest/cve                        |   2 +
 testcases/kernel/crypto/.gitignore |   5 +
 testcases/kernel/crypto/af_alg01.c |  79 ++++++++++++++++
 testcases/kernel/crypto/af_alg02.c |  29 ++++++
 testcases/kernel/crypto/af_alg03.c |  31 ++++++
 testcases/kernel/crypto/af_alg04.c |  56 +++++++++++
 testcases/kernel/crypto/af_alg05.c |  43 +++++++++
 11 files changed, 569 insertions(+)
 create mode 100644 include/lapi/if_alg.h
 create mode 100644 include/tst_af_alg.h
 create mode 100644 lib/tst_af_alg.c
 create mode 100644 testcases/kernel/crypto/af_alg01.c
 create mode 100644 testcases/kernel/crypto/af_alg02.c
 create mode 100644 testcases/kernel/crypto/af_alg03.c
 create mode 100644 testcases/kernel/crypto/af_alg04.c
 create mode 100644 testcases/kernel/crypto/af_alg05.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/6 lib: add tst_af_alg lib

From: Eric Biggers <[email protected]>

Add helper functions for creating and using AF_ALG sockets. AF_ALG is the userspace interface to algorithms in the Linux kernel's crypto API. See https://www.kernel.org/doc/html/latest/crypto/userspace-if.html for more information about this interface.

---
 configure.ac          |   1 +
 include/lapi/if_alg.h |  40 ++++++++++++
 include/tst_af_alg.h  | 136 ++++++++++++++++++++++++++++++++++++++
 lib/tst_af_alg.c      | 147 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 324 insertions(+)
 create mode 100644 include/lapi/if_alg.h
 create mode 100644 include/tst_af_alg.h
 create mode 100644 lib/tst_af_alg.c
configure.ac +1 -0
index caea34462f..229815694c 100644
@@ -40,6 +40,7 @@ AC_CHECK_HEADERS([ \
     linux/cryptouser.h \
     linux/genetlink.h \
     linux/keyctl.h \
+    linux/if_alg.h \
     linux/if_packet.h \
     linux/if_ether.h \
     linux/mempolicy.h \
include/lapi/if_alg.h new file +40 -0
new file mode 100644
index 0000000000..2fc5e7b5e3
@@ -0,0 +1,40 @@
+#ifndef IF_ALG_H__
+#define IF_ALG_H__
+
+#ifdef HAVE_LINUX_IF_ALG_H
+#  include <linux/if_alg.h>
+#else
+#  include <stdint.h>
+
+struct sockaddr_alg {
+	uint16_t	salg_family;
+	uint8_t		salg_type[14];
+	uint32_t	salg_feat;
+	uint32_t	salg_mask;
+	uint8_t		salg_name[64];
+};
+
+struct af_alg_iv {
+	uint32_t	ivlen;
+	uint8_t		iv[0];
+};
+
+/* Socket options */
+#define ALG_SET_KEY		1
+#define ALG_SET_IV		2
+#define ALG_SET_OP		3
+#define ALG_SET_AEAD_ASSOCLEN	4
+#define ALG_SET_AEAD_AUTHSIZE	5
+
+/* Operations */
+#define ALG_OP_DECRYPT		0
+#define ALG_OP_ENCRYPT		1
+
+#endif /* !HAVE_LINUX_IF_ALG_H */
+
+/* This isn't in any UAPI header */
+#ifndef SOL_ALG
+#  define SOL_ALG		279
+#endif
+
+#endif /* IF_ALG_H__ */
include/tst_af_alg.h new file +136 -0

136 lines not on this page · raw message

lib/tst_af_alg.c new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [LTP] [PATCH 1/6] lib: add tst_af_alg lib

Hi Eric,

Quoted 6 lines

From: Eric Biggers <[email protected]>

Add helper functions for creating and using AF_ALG sockets. AF_ALG is the userspace interface to algorithms in the Linux kernel's crypto API. See https://www.kernel.org/doc/html/latest/crypto/userspace-if.html for more information about this interface.

AF_ALG is not defined in old distros [1]:
/usr/src/ltp/lib/tst_af_alg.c: In function 'tst_alg_create':
/usr/src/ltp/lib/tst_af_alg.c:15: error: 'AF_ALG' undeclared (first use in this function)

We should add it (not sure whether to new lapi/if_alg.h or to lapi/socket.h).

Kind regards,
Petr

[1] https://api.travis-ci.org/v3/job/506663263/log.txt

#3 lore raw

PATCH 2/6 crypto/af_alg01: new regression test for hmac nesting bug

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

---
 runtest/cve                        |  1 +
 testcases/kernel/crypto/.gitignore |  1 +
 testcases/kernel/crypto/af_alg01.c | 79 ++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 testcases/kernel/crypto/af_alg01.c
runtest/cve +1 -0
index 8f38045e9a..f46c400cc4 100644
@@ -27,6 +27,7 @@ cve-2017-15299 request_key03 -b cve-2017-15299
 cve-2017-15537 ptrace07
 cve-2017-15649 fanout01
 cve-2017-15951 request_key03 -b cve-2017-15951
+cve-2017-17806 af_alg01
 cve-2017-17807 request_key04
 cve-2017-1000364 stack_clash
 cve-2017-5754 meltdown
testcases/kernel/crypto/.gitignore +1 -0
index 759592fbdf..998af17284 100644
@@ -1,2 +1,3 @@
+af_alg01
 pcrypt_aead01
 crypto_user01
testcases/kernel/crypto/af_alg01.c new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.20.1
#4 lore raw

Re: [LTP] [PATCH 2/6] crypto/af_alg01: new regression test for hmac nesting bug

Hi Eric,

testcases/kernel/crypto/af_alg01.c line 71

Quoted 12 lines
testcases/kernel/crypto/af_alg01.c new file +12 -0
+static void run(void)
+{
+	/* try several different unkeyed hash algorithms */
+	static const char * const hash_algs[] = {
+		"md5", "md5-generic",
+		"sha1", "sha1-generic",
+		"sha224", "sha224-generic",
+		"sha256", "sha256-generic",
+		"sha3-256", "sha3-256-generic",
+		"sha3-512", "sha3-512-generic",
+	};
+	size_t i;

FYI: We usually use struct tcase with .tcnt = ARRAY_SIZE(tcases) instead of for loop, but it's only a minor detail.

testcases/kernel/crypto/af_alg01.c line 79

Quoted 8 lines
testcases/kernel/crypto/af_alg01.c new file +8 -0
+
+	for (i = 0; i < ARRAY_SIZE(hash_algs); i++)
+		test_with_hash_alg(hash_algs[i]);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};

Kind regards,
Petr

#5 lore raw

PATCH 3/6 crypto/af_alg02: new regression test for salsa20 empty message bug

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

---
 runtest/cve                        |  1 +
 testcases/kernel/crypto/.gitignore |  1 +
 testcases/kernel/crypto/af_alg02.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 testcases/kernel/crypto/af_alg02.c
runtest/cve +1 -0
index f46c400cc4..031bcdc2a7 100644
@@ -27,6 +27,7 @@ cve-2017-15299 request_key03 -b cve-2017-15299
 cve-2017-15537 ptrace07
 cve-2017-15649 fanout01
 cve-2017-15951 request_key03 -b cve-2017-15951
+cve-2017-17805 af_alg02
 cve-2017-17806 af_alg01
 cve-2017-17807 request_key04
 cve-2017-1000364 stack_clash
testcases/kernel/crypto/.gitignore +1 -0
index 998af17284..dc79f3275b 100644
@@ -1,3 +1,4 @@
 af_alg01
+af_alg02
 pcrypt_aead01
 crypto_user01
testcases/kernel/crypto/af_alg02.c new file +29 -0
new file mode 100644
index 0000000000..a9e8204230
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2019 Google LLC
+ */
+
+/*
+ * Regression test for commit ecaaab564978 ("crypto: salsa20 - fix
+ * blkcipher_walk API usage"), or CVE-2017-17805.  This test verifies that an
+ * empty message can be encrypted with Salsa20 without crashing the kernel.
+ */
+
+#include "tst_test.h"
+#include "tst_af_alg.h"
+
+static void run(void)
+{
+	char buf[16];
+	int reqfd = tst_alg_setup_reqfd("skcipher", "salsa20", NULL, 16);
+
+	/* With the bug the kernel crashed here */
+	if (read(reqfd, buf, 16) == 0)
+		tst_res(TPASS, "Successfully \"encrypted\" an empty message");
+	else
+		tst_res(TBROK, "read() didn't return 0");
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
Signature · 1 line
2.20.1
#6 lore raw

PATCH 4/6 crypto/af_alg03: new regression test for rfc7539 hash alg validation

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

---
 testcases/kernel/crypto/.gitignore |  1 +
 testcases/kernel/crypto/af_alg03.c | 31 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 testcases/kernel/crypto/af_alg03.c
testcases/kernel/crypto/.gitignore +1 -0
index dc79f3275b..3e7936fc94 100644
@@ -1,4 +1,5 @@
 af_alg01
 af_alg02
+af_alg03
 pcrypt_aead01
 crypto_user01
testcases/kernel/crypto/af_alg03.c new file +31 -0
new file mode 100644
index 0000000000..240c52835f
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2019 Google LLC
+ */
+
+/*
+ * Regression test for commit e57121d08c38 ("crypto: chacha20poly1305 - validate
+ * the digest size").  This test verifies that the rfc7539 template can't be
+ * instantiated with a hash algorithm whose digest size is not 16 bytes.
+ */
+
+#include "tst_test.h"
+#include "tst_af_alg.h"
+
+static void run(void)
+{
+	tst_require_alg("aead", "rfc7539(chacha20,poly1305)");
+	tst_require_alg("hash", "sha256");
+
+	if (tst_have_alg("aead", "rfc7539(chacha20,sha256)")) {
+		tst_res(TFAIL,
+			"instantiated rfc7539 template with wrong digest size");
+	} else {
+		tst_res(TPASS,
+			"couldn't instantiate rfc7539 template with wrong digest size");
+	}
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
Signature · 1 line
2.20.1
#7 lore raw

Re: [LTP] [PATCH 4/6] crypto/af_alg03: new regression test for rfc7539 hash alg validation

Hi Eric,

you forget to add af_alg03 into runtest/cve.
The same applies to af_alg0{4,5}.

Kind regards,
Petr

#8 lore raw

Re: [LTP] [PATCH 4/6] crypto/af_alg03: new regression test for rfc7539 hash alg validation

Hi Petr,

Quoted 8 lines · Petr Vorel

Hi Eric,

you forget to add af_alg03 into runtest/cve.
The same applies to af_alg0{4,5}.

Kind regards,
Petr

As far as I know, there are no CVEs assigned for those bugs.

- Eric

#9 lore raw

Re: [LTP] [PATCH 4/6] crypto/af_alg03: new regression test for rfc7539 hash alg validation

Hi Eric,

Quoted 4 lines
Quoted 2 lines

you forget to add af_alg03 into runtest/cve.
The same applies to af_alg0{4,5}.

As far as I know, there are no CVEs assigned for those bugs.

Wouldn't be better to add them to runtest/cve (or other runtest file) just with filename? And rename them in runtest file once the CVEs are assigned?

Quoted 1 line

- Eric

Kind regards,
Petr

#10 lore raw

Re: [LTP] [PATCH 4/6] crypto/af_alg03: new regression test for rfc7539 hash alg validation

Quoted 5 lines · Petr Vorel

Hi Eric,

Quoted 2 lines
Quoted 2 lines

you forget to add af_alg03 into runtest/cve.
The same applies to af_alg0{4,5}.

Quoted 8 lines
Quoted 1 line

As far as I know, there are no CVEs assigned for those bugs.

Wouldn't be better to add them to runtest/cve (or other runtest file) just with filename? And rename them in runtest file once the CVEs are assigned?

Quoted 1 line

- Eric

Kind regards,
Petr

Oh, I missed that they're not in any runtest file at all. They all should be in runtest/crypto (in addition to the ones with CVEs being in runtest/cve).

- Eric

#11 lore raw

PATCH 5/6 crypto/af_alg04: new regression test for vmac race conditions

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

---
 testcases/kernel/crypto/.gitignore |  1 +
 testcases/kernel/crypto/af_alg04.c | 56 ++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 testcases/kernel/crypto/af_alg04.c
testcases/kernel/crypto/.gitignore +1 -0
index 3e7936fc94..df6b7e73cb 100644
@@ -1,5 +1,6 @@
 af_alg01
 af_alg02
 af_alg03
+af_alg04
 pcrypt_aead01
 crypto_user01
testcases/kernel/crypto/af_alg04.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.20.1
#12 lore raw

PATCH 6/6 crypto/af_alg05: new regression test for skcipher_walk error bug

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

---
 testcases/kernel/crypto/.gitignore |  1 +
 testcases/kernel/crypto/af_alg05.c | 43 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 testcases/kernel/crypto/af_alg05.c
testcases/kernel/crypto/.gitignore +1 -0
index df6b7e73cb..17faf3eefc 100644
@@ -2,5 +2,6 @@ af_alg01
 af_alg02
 af_alg03
 af_alg04
+af_alg05
 pcrypt_aead01
 crypto_user01
testcases/kernel/crypto/af_alg05.c new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.20.1
#13 lore raw
In reply to Eric Biggers
Quoted 34 lines · Eric Biggers

Hello,

This series adds helper functions for testing AF_ALG (the userspace interface to algorithms in the Linux kernel's crypto API) to the Linux Test Project. It then adds a few sample regression tests.

I've had these patches sitting around for a while with the goal of also writing some "real" tests that test for more than these specific bugs, but haven't gotten around to it yet. And I now consider further improving the in-kernel crypto self-tests to be a higher priority. But I figured I'd send this out as-is in case people are interested in having this merged, e.g. maybe other people want to add AF_ALG tests to LTP too and the 'tst_af_alg' lib would be a good starting point.

Eric Biggers (6):
lib: add tst_af_alg lib
crypto/af_alg01: new regression test for hmac nesting bug crypto/af_alg02: new regression test for salsa20 empty message bug crypto/af_alg03: new regression test for rfc7539 hash alg validation crypto/af_alg04: new regression test for vmac race conditions crypto/af_alg05: new regression test for skcipher_walk error bug

 configure.ac                       |   1 +
 include/lapi/if_alg.h              |  40 ++++++++
 include/tst_af_alg.h               | 136 ++++++++++++++++++++++++++
 lib/tst_af_alg.c                   | 147 +++++++++++++++++++++++++++++
 runtest/cve                        |   2 +
 testcases/kernel/crypto/.gitignore |   5 +
 testcases/kernel/crypto/af_alg01.c |  79 ++++++++++++++++
 testcases/kernel/crypto/af_alg02.c |  29 ++++++
 testcases/kernel/crypto/af_alg03.c |  31 ++++++
 testcases/kernel/crypto/af_alg04.c |  56 +++++++++++
 testcases/kernel/crypto/af_alg05.c |  43 +++++++++
 11 files changed, 569 insertions(+)
Quoted 12 lines
 create mode 100644 include/lapi/if_alg.h
 create mode 100644 include/tst_af_alg.h
 create mode 100644 lib/tst_af_alg.c
 create mode 100644 testcases/kernel/crypto/af_alg01.c
 create mode 100644 testcases/kernel/crypto/af_alg02.c
 create mode 100644 testcases/kernel/crypto/af_alg03.c
 create mode 100644 testcases/kernel/crypto/af_alg04.c
 create mode 100644 testcases/kernel/crypto/af_alg05.c
Signature · 1 line
2.20.1

Any comment on this from the LTP maintainers?

- Eric

Reviewed-by on the v1 series
#14 lore raw
In reply to Eric Biggers

Hi Eric,

apart from missing AF_ALG for old distros and missing record in runtest/cve for af_alg0[3-5] LGTM.
Sorry for delay with the review.

Kind regards,
Petr

Quoted 42 lines

Hello,

This series adds helper functions for testing AF_ALG (the userspace interface to algorithms in the Linux kernel's crypto API) to the Linux Test Project. It then adds a few sample regression tests.

I've had these patches sitting around for a while with the goal of also writing some "real" tests that test for more than these specific bugs, but haven't gotten around to it yet. And I now consider further improving the in-kernel crypto self-tests to be a higher priority. But I figured I'd send this out as-is in case people are interested in having this merged, e.g. maybe other people want to add AF_ALG tests to LTP too and the 'tst_af_alg' lib would be a good starting point.

Eric Biggers (6):
lib: add tst_af_alg lib
crypto/af_alg01: new regression test for hmac nesting bug crypto/af_alg02: new regression test for salsa20 empty message bug crypto/af_alg03: new regression test for rfc7539 hash alg validation crypto/af_alg04: new regression test for vmac race conditions crypto/af_alg05: new regression test for skcipher_walk error bug

 configure.ac                       |   1 +
 include/lapi/if_alg.h              |  40 ++++++++
 include/tst_af_alg.h               | 136 ++++++++++++++++++++++++++
 lib/tst_af_alg.c                   | 147 +++++++++++++++++++++++++++++
 runtest/cve                        |   2 +
 testcases/kernel/crypto/.gitignore |   5 +
 testcases/kernel/crypto/af_alg01.c |  79 ++++++++++++++++
 testcases/kernel/crypto/af_alg02.c |  29 ++++++
 testcases/kernel/crypto/af_alg03.c |  31 ++++++
 testcases/kernel/crypto/af_alg04.c |  56 +++++++++++
 testcases/kernel/crypto/af_alg05.c |  43 +++++++++
 11 files changed, 569 insertions(+)
 create mode 100644 include/lapi/if_alg.h
 create mode 100644 include/tst_af_alg.h
 create mode 100644 lib/tst_af_alg.c
 create mode 100644 testcases/kernel/crypto/af_alg01.c
 create mode 100644 testcases/kernel/crypto/af_alg02.c
 create mode 100644 testcases/kernel/crypto/af_alg03.c
 create mode 100644 testcases/kernel/crypto/af_alg04.c
 create mode 100644 testcases/kernel/crypto/af_alg05.c