Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: git@danielhodges.dev,bpf@vger.kernel.org,linux-crypto@vger.kernel.org,linux-kernel@vger.kernel.org,linux-kselftest@vger.kernel.org
Cc: ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,vadim.fedorenko@linux.dev,song@kernel.org,yatsenko@meta.com,martin.lau@linux.dev,eddyz87@gmail.com,haoluo@google.com,jolsa@kernel.org,john.fastabend@gmail.com,kpsingh@kernel.org,sdf@fomichev.me,yonghong.song@linux.dev,herbert@gondor.apana.org.au,davem@davemloft.net,git@danielhodges.dev,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,clm@meta.com,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v8 1/4] bpf: Add hash kfunc for cryptographic hashing
Date: Wed, 25 Feb 2026 21:06:39 +0000 (UTC)	[thread overview]
Message-ID: <bf515a9784c17b9b0dcfeff987e9cc7c328fc6d330cdb97b1cfe77448eafcc27@mail.kernel.org> (raw)
In-Reply-To: <20260225202935.31986-2-git@danielhodges.dev>

[-- Attachment #1: Type: text/plain, Size: 3896 bytes --]

> diff --git a/crypto/bpf_crypto_shash.c b/crypto/bpf_crypto_shash.c
> --- /dev/null
> +++ b/crypto/bpf_crypto_shash.c
> @@ -0,0 +1,96 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
> +#include <linux/types.h>
> +#include <linux/module.h>
> +#include <linux/bpf_crypto.h>
> +#include <crypto/hash.h>
> +
> +struct bpf_shash_ctx {
> +	struct crypto_shash *tfm;
> +	struct shash_desc desc;
> +};

[ ... ]

> +static void *bpf_crypto_shash_alloc_tfm(const char *algo)
> +{

[ ... ]

> +	ctx->tfm = tfm;
> +	ctx->desc.tfm = tfm;
> +
> +	return ctx;
> +}

[ ... ]

> +static int bpf_crypto_shash_hash(void *tfm, const u8 *data, u8 *out,
> +				 unsigned int len)
> +{
> +	struct bpf_shash_ctx *ctx = tfm;
> +
> +	return crypto_shash_digest(&ctx->desc, data, len, out);
> +}

bpf_crypto_shash_alloc_tfm() creates one bpf_shash_ctx per
bpf_crypto_ctx, with a single embedded shash_desc. Multiple BPF
programs can acquire references to the same bpf_crypto_ctx
concurrently via bpf_crypto_ctx_acquire() (which just does
refcount_inc_not_zero), and the hash kfunc is marked KF_RCU which
provides no mutual exclusion.

When two CPUs call bpf_crypto_hash() with the same context, both
end up in crypto_shash_digest() using the same desc:

  CPU0                           CPU1
  bpf_crypto_hash(ctx)           bpf_crypto_hash(ctx)
    bpf_crypto_shash_hash()        bpf_crypto_shash_hash()
      crypto_shash_digest(desc)      crypto_shash_digest(desc)
        alg->digest(desc, ...)         alg->digest(desc, ...)
        // writes desc->__ctx          // writes desc->__ctx
        memset(desc->__ctx, 0)         memset(desc->__ctx, 0)

crypto_shash_op_and_zero() in crypto/shash.c writes to
shash_desc_ctx(desc) via the algorithm callback and then zeroes it
with memset. Both CPUs race on this buffer, producing silently
corrupted hash output.

The existing encrypt/decrypt path does not have this problem because
crypto_lskcipher_encrypt() treats the tfm as read-only (the key
schedule is pre-computed) and the mutable IV state is passed
per-call through the siv dynptr.

Would it make sense to allocate a shash_desc per-call on the stack,
or use a per-cpu descriptor, to avoid this race?

> diff --git a/kernel/bpf/crypto.c b/kernel/bpf/crypto.c
> --- a/kernel/bpf/crypto.c
> +++ b/kernel/bpf/crypto.c

[ ... ]

> +#if IS_ENABLED(CONFIG_CRYPTO_HASH2)
> +/**
> + * bpf_crypto_hash() - Compute hash using configured context
> + * @ctx:	The crypto context being used. The ctx must be a trusted pointer.
> + * @data:	bpf_dynptr to the input data to hash. Must be a trusted pointer.
> + * @out:	bpf_dynptr to the output buffer. Must be a trusted pointer.
> + *
> + * Computes hash of the input data using the crypto context. The output buffer
> + * must be at least as large as the digest size of the hash algorithm.
> + */
> +__bpf_kfunc int bpf_crypto_hash(struct bpf_crypto_ctx *ctx,
> +				const struct bpf_dynptr *data,
> +				const struct bpf_dynptr *out)
> +{

[ ... ]

> +	return ctx->type->hash(ctx->tfm, data_ptr, out_ptr, data_len);
> +}
> +#endif /* CONFIG_CRYPTO_HASH2 */


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22414975518

AI-authorship-score: low
AI-authorship-explanation: Eight revision iterations with active reviewer engagement on lore.kernel.org and incremental changes addressing feedback are consistent with human-driven development.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: Data race on shared shash_desc in bpf_crypto_hash() produces silently corrupted cryptographic hash output when two CPUs use the same context concurrently, which could affect integrity checking and authentication use cases.

  reply	other threads:[~2026-02-25 21:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25 20:29 [PATCH bpf-next v8 0/4] Add cryptographic hash and signature verification kfuncs to BPF Daniel Hodges
2026-02-25 20:29 ` [PATCH bpf-next v8 1/4] bpf: Add hash kfunc for cryptographic hashing Daniel Hodges
2026-02-25 21:06   ` bot+bpf-ci [this message]
2026-02-25 20:29 ` [PATCH bpf-next v8 2/4] selftests/bpf: Add tests for bpf_crypto_hash kfunc Daniel Hodges
2026-02-25 20:29 ` [PATCH bpf-next v8 3/4] bpf: Add signature verification kfuncs Daniel Hodges
2026-02-25 21:06   ` bot+bpf-ci
2026-02-25 20:29 ` [PATCH bpf-next v8 4/4] selftests/bpf: Add tests for " Daniel Hodges

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bf515a9784c17b9b0dcfeff987e9cc7c328fc6d330cdb97b1cfe77448eafcc27@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=clm@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=git@danielhodges.dev \
    --cc=haoluo@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=vadim.fedorenko@linux.dev \
    --cc=yatsenko@meta.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox