Skip to content

Commit d0df0cd

Browse files
author
Baoquan He
committed
kexec_file: use SHA-256 library API instead of crypto_shash API
JIRA: https://issues.redhat.com/browse/RHEL-114162 Upstream Status: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git Conflict: there's conflict in line 776 of kernel/kexec_file.c. I haven't got the reason why it has conflict. Seems it's caused by upstream merging and the mess is produced. Have to edit it manually to grab that hunk. commit f7a667a Author: Eric Biggers <ebiggers@google.com> Date: Mon Apr 28 11:57:20 2025 -0700 kexec_file: use SHA-256 library API instead of crypto_shash API This user of SHA-256 does not support any other algorithm, so the crypto_shash abstraction provides no value. Just use the SHA-256 library API instead, which is much simpler and easier to use. Tested with '/sbin/kexec --kexec-file-syscall'. Link: https://lkml.kernel.org/r/20250428185721.844686-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com> Cc: Baoquan He <bhe@redhat.com> Cc: Vivek Goyal <vgoyal@redhat.com> Cc: Dave Young <dyoung@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Baoquan He <bhe@redhat.com>
1 parent 74ede9c commit d0df0cd

File tree

2 files changed

+16
-65
lines changed

2 files changed

+16
-65
lines changed

kernel/Kconfig.kexec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ config KEXEC
3838
config KEXEC_FILE
3939
bool "Enable kexec file based system call"
4040
depends on ARCH_SUPPORTS_KEXEC_FILE
41-
select CRYPTO
42-
select CRYPTO_SHA256
41+
select CRYPTO_LIB_SHA256
4342
select KEXEC_CORE
4443
help
4544
This is new version of kexec system call. This system call is

kernel/kexec_file.c

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <linux/list.h>
2020
#include <linux/fs.h>
2121
#include <linux/ima.h>
22-
#include <crypto/hash.h>
2322
#include <crypto/sha2.h>
2423
#include <linux/elf.h>
2524
#include <linux/elfcore.h>
@@ -736,11 +735,10 @@ int kexec_add_buffer(struct kexec_buf *kbuf)
736735
/* Calculate and store the digest of segments */
737736
static int kexec_calculate_store_digests(struct kimage *image)
738737
{
739-
struct crypto_shash *tfm;
740-
struct shash_desc *desc;
738+
struct sha256_state state;
741739
int ret = 0, i, j, zero_buf_sz, sha_region_sz;
742-
size_t desc_size, nullsz;
743-
char *digest;
740+
size_t nullsz;
741+
u8 digest[SHA256_DIGEST_SIZE];
744742
void *zero_buf;
745743
struct kexec_sha_region *sha_regions;
746744
struct purgatory_info *pi = &image->purgatory_info;
@@ -751,37 +749,12 @@ static int kexec_calculate_store_digests(struct kimage *image)
751749
zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
752750
zero_buf_sz = PAGE_SIZE;
753751

754-
tfm = crypto_alloc_shash("sha256", 0, 0);
755-
if (IS_ERR(tfm)) {
756-
ret = PTR_ERR(tfm);
757-
goto out;
758-
}
759-
760-
desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
761-
desc = kzalloc(desc_size, GFP_KERNEL);
762-
if (!desc) {
763-
ret = -ENOMEM;
764-
goto out_free_tfm;
765-
}
766-
767752
sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
768753
sha_regions = vzalloc(sha_region_sz);
769-
if (!sha_regions) {
770-
ret = -ENOMEM;
771-
goto out_free_desc;
772-
}
773-
774-
desc->tfm = tfm;
775-
776-
ret = crypto_shash_init(desc);
777-
if (ret < 0)
778-
goto out_free_sha_regions;
754+
if (!sha_regions)
755+
return -ENOMEM;
779756

780-
digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
781-
if (!digest) {
782-
ret = -ENOMEM;
783-
goto out_free_sha_regions;
784-
}
757+
sha256_init(&state);
785758

786759
for (j = i = 0; i < image->nr_segments; i++) {
787760
struct kexec_segment *ksegment;
@@ -807,10 +780,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
807780
if (check_ima_segment_index(image, i))
808781
continue;
809782

810-
ret = crypto_shash_update(desc, ksegment->kbuf,
811-
ksegment->bufsz);
812-
if (ret)
813-
break;
783+
sha256_update(&state, ksegment->kbuf, ksegment->bufsz);
814784

815785
/*
816786
* Assume rest of the buffer is filled with zero and
@@ -822,44 +792,26 @@ static int kexec_calculate_store_digests(struct kimage *image)
822792

823793
if (bytes > zero_buf_sz)
824794
bytes = zero_buf_sz;
825-
ret = crypto_shash_update(desc, zero_buf, bytes);
826-
if (ret)
827-
break;
795+
sha256_update(&state, zero_buf, bytes);
828796
nullsz -= bytes;
829797
}
830798

831-
if (ret)
832-
break;
833-
834799
sha_regions[j].start = ksegment->mem;
835800
sha_regions[j].len = ksegment->memsz;
836801
j++;
837802
}
838803

839-
if (!ret) {
840-
ret = crypto_shash_final(desc, digest);
841-
if (ret)
842-
goto out_free_digest;
843-
ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
844-
sha_regions, sha_region_sz, 0);
845-
if (ret)
846-
goto out_free_digest;
804+
sha256_final(&state, digest);
847805

848-
ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
849-
digest, SHA256_DIGEST_SIZE, 0);
850-
if (ret)
851-
goto out_free_digest;
852-
}
806+
ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
807+
sha_regions, sha_region_sz, 0);
808+
if (ret)
809+
goto out_free_sha_regions;
853810

854-
out_free_digest:
855-
kfree(digest);
811+
ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
812+
digest, SHA256_DIGEST_SIZE, 0);
856813
out_free_sha_regions:
857814
vfree(sha_regions);
858-
out_free_desc:
859-
kfree(desc);
860-
out_free_tfm:
861-
kfree(tfm);
862-
out:
863815
return ret;
864816
}
865817

0 commit comments

Comments
 (0)