testcases/kernel/crypto/crypto_user02.c: fix hmac candidate probing
PATCH v2 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.
--- include/tst_crypto.h | 22 +++++++++ lib/tst_crypto.c | 42 ++++++++++++++++ testcases/kernel/crypto/crypto_user02.c | 64 ++++++++++++++++++------- 3 files changed, 110 insertions(+), 18 deletions(-)
include/tst_crypto.h +22 -0
@@ -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 +46 -18
120 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
Hi Frank,
testcases/kernel/crypto/crypto_user02.c static void setup(void) · line 110
Quoted 13 lines · Frank Ranner
testcases/kernel/crypto/crypto_user02.c: fix hmac candidate probingtestcases/kernel/crypto/crypto_user02.c +8
-0+ rc =tst_crypto_add_alg (ctx, &alg); +if (rc !=0 && rc != -EEXIST ) +continue ; + +memset (&report,0 ,sizeof (report)); +strcpy (report.cru_name,ALGORITHM_CANDIDATES [i]); +if (tst_crypto_get_alg (ctx, &report) !=0 )continue ;/* it also has to be deletable */ + rc =tst_crypto_del_alg (ctx, &report,1000 );
Could -EEXIST candidates be skipped here? If this result refers to an already-registered, unused template instance, GETALG selects it and DELALG removes global crypto state that the test did not create. Such instances remain registered after their users release them and are deletable once the reference count permits it, so cleanup does not restore the original state.
This can also leave another implementation with the same cru_name. The race loop then gets -EEXIST from every NEWALG request, creates no larval, accepts -ENOENT from the fixed-driver DELALG request, and falsely passes. Could setup require successful creation rather than accepting an existing registration?
Quoted 1 line
Commit
a4be708b4("try non-generic hmac names first") added a list of
Could this use c05a44cf instead? a4be708b4 does not resolve in the local LTP history, while the Fixes tag identifies the same subject as c05a44cf.
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
Quoted 44 lines · Frank Ranner
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 intocru_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_namebeing 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 loosecra_namefallback incrypto_alg_match(), while template instantiation of "hmac" produces driver names such as "hmac-shash(sha256-generic)". SinceCRYPTO_MSG_DELALGalways requires an exactcru_driver_namematch, every one of the new candidates fails to delete, andsetup()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 newCRYPTO_MSG_GETALGrequest 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 laterCRYPTO_MSG_DELALGcalls in the actual race loop. The race loop's ownCRYPTO_MSG_NEWALGcalls 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-leveltst_netlinksend/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.
Quoted 2 lines
Fixes c05a44cf ("testcases/kernel/crypto/crypto_user02.c: try non-generic hmac names first")Signed-off-by Frank Ranner <[email protected]>
This misses the point of what I said in my last response. In current mainline kernels, if you request "hmac(sha256)" you get a non-template-based algorithm with name="hmac(sha256)", driver_name="hmac-sha256-lib". With "hmac(sm3)" you do still usually get a template-based algorithm, but that's just because the direct support for HMAC-SM3 hasn't been implemented yet outside of a few random hardware-specific drivers. To actually get the template instance now you would need to request "hmac(sha256-lib)", etc.
At the same time, there's zero reason for userspace to do that other than verifying that these pointless UAPIs don't cause a kernel crash. Honestly we should consider just deleting this test, as it is testing implementation details of legacy stuff that is just causing confusion.
- Eric
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp