Skip to contentSkip to conversation

testcases/kernel/crypto/crypto_user02.c: fix hmac candidate probing

PATCH Superseded Frank Ranner 2 participants 2 messages Aug 13 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 testcases/kernel/crypto/crypto_user02.c: fix hmac candidate probing

Commit a4be708b4 ("try non-generic hmac names first") added a list of plain algorithm names (e.g. "hmac(sha256)") that was probed by copying each string directly into cru_driver_name, the same way the pre-existing "-generic" fallback names were already being probed.

That approach relies on the newly (or already) registered algorithm's actual cra_driver_name being identical to the probed string, which is not guaranteed. On current kernels, "hmac(sha256)" is satisfied by a non-instance "hmac-sha256-lib" driver matched via the loose cra_name fallback in crypto_alg_match(), while template instantiation of "hmac" produces driver names such as "hmac-shash(sha256-generic)". Since CRYPTO_MSG_DELALG always requires an exact cru_driver_name match, every one of the new candidates fails to delete, and setup() silently falls through to the legacy "-generic" names, which are themselves absent on kernels that dropped the generic hash implementations. The net effect is that the test still reports "No viable algorithm found" on affected kernels, unchanged from before the commit.

Fix this by not guessing the driver name at all. Probe each candidate by algorithm name (cru_name) instead of driver name, which lets the kernel resolve it however it currently does, then use a new CRYPTO_MSG_GETALG request to ask the kernel what driver name actually satisfies that algorithm. Only that authoritative, kernel-reported driver name is used for the deletability check and for the later CRYPTO_MSG_DELALG calls in the actual race loop. The race loop's own CRYPTO_MSG_NEWALG calls also switch to adding by algorithm name, since re-instantiating the same driver on each iteration requires the template-parseable "hmac(...)" form rather than its resulting driver name.

This adds tst_crypto_get_alg() to the shared crypto netlink helpers, built directly on the existing low-level tst_netlink send/recv primitives, so any future crypto test needing to resolve a driver name can reuse it instead of re-deriving this logic.

Verified locally: on a live 7.0 kernel, all of the plain hmac(<hash>) candidates for sha1/sha224/sha256/sha384/md5 resolve to permanent "-lib"-style drivers and are correctly rejected as non-deletable, while hmac(sm3) resolves to a genuine "hmac(sm3-avx)" template instance and is correctly selected; the race loop then runs to completion cleanly. Rebuilding the pre-fix version of this file on the same kernel confirms it falls through every new candidate and only succeeds via the legacy "hmac(sm3-generic)" fallback, reproducing the reported failure mode.

Fixes a4be708b4 ("testcases/kernel/crypto/crypto_user02.c: try non-generic hmac names first")
---
 include/tst_crypto.h                    | 22 +++++++++
 lib/tst_crypto.c                        | 42 ++++++++++++++++
 testcases/kernel/crypto/crypto_user02.c | 65 ++++++++++++++++++-------
 3 files changed, 111 insertions(+), 18 deletions(-)
include/tst_crypto.h +22 -0
index 4511adf22..db71a5f3b 100644
@@ -54,4 +54,26 @@ int tst_crypto_add_alg(struct tst_netlink_context *ctx,
 int tst_crypto_del_alg(struct tst_netlink_context *ctx,
 	const struct crypto_user_alg *alg, unsigned int retries);
 
+/**
+ * tst_crypto_get_alg() - Look up the kernel's registration info for an alg.
+ *
+ * @ctx: Initialized netlink context
+ * @alg: On input, the algorithm to query, normally identified by cru_name.
+ *       On success it is overwritten with the kernel's authoritative view of
+ *       the matched algorithm, including its actual cru_driver_name.
+ *
+ * Sends a CRYPTO_MSG_GETALG request and parses the response. This is useful
+ * because the driver name that ends up registered for a given algorithm name
+ * is a kernel implementation detail: it can be satisfied by a permanent,
+ * non-instance driver (e.g. a "-lib" fast path) or by a freshly instantiated
+ * template, and the exact driver name string used for either has changed
+ * across kernel versions. Looking it up dynamically avoids depending on any
+ * particular naming convention.
+ *
+ * Return: On success it will return 0 otherwise it will return an inverted
+ * error code from the crypto layer.
+ */
+int tst_crypto_get_alg(struct tst_netlink_context *ctx,
+	struct crypto_user_alg *alg);
+
 #endif	/* TST_CRYPTO_H */
lib/tst_crypto.c +42 -0

45 lines not on this page · raw message

testcases/kernel/crypto/crypto_user02.c +47 -18

121 lines not on this page · raw message

Signature · 13 lines
2.53.0

---------------------------------------------------------------------
Intel Technology Poland sp. z o.o.
ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | Kapital zakladowy 200.000 PLN.
Spolka oswiadcza, ze posiada status duzego przedsiebiorcy w rozumieniu ustawy z dnia 8 marca 2013 r. o przeciwdzialaniu nadmiernym opoznieniom w transakcjach handlowych.

Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiekolwiek przegladanie lub rozpowszechnianie jest zabronione.
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). If you are not the intended recipient, please contact the sender and delete all copies; any review or distribution by others is strictly prohibited.


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Frank,

Quoted 3 lines · Frank Ranner

testcases/kernel/crypto/crypto_user02.c: fix hmac candidate probing

Fixes a4be708b4 ("testcases/kernel/crypto/crypto_user02.c: try non-generic hmac names first")

Medium: Could this use c05a44cf1 instead? The referenced a4be708b4 does not resolve to a commit in the local LTP history, while c05a44cf1 has the quoted subject.

testcases/kernel/crypto/crypto_user02.c static void setup(void) · line 107

Quoted 4 lines
testcases/kernel/crypto/crypto_user02.c +4 -0
+		/* find out which driver actually satisfies this name */
+		memset(&report, 0, sizeof(report));
+		strcpy(report.cru_name, ALGORITHM_CANDIDATES[i]);
+		if (tst_crypto_get_alg(ctx, &report) != 0)

Low: Could this comment be removed? It only restates the tst_crypto_get_alg() call, while LTP comments should explain why rather than narrate the code.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp