Skip to content

Commit 8c7b864

Browse files
authored
Merge pull request #762 from danielinux/fenrir-fixes-2026-04-29
Fenrir fixes
2 parents e44e6b2 + 971f0c6 commit 8c7b864

30 files changed

Lines changed: 1040 additions & 164 deletions

.github/workflows/test-build-riscv.yml

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818

1919
build:
2020
runs-on: ubuntu-24.04
21+
container:
22+
image: ghcr.io/wolfssl/wolfboot-ci-riscv:v1.0
2123
timeout-minutes: 30
2224
steps:
2325
- uses: actions/checkout@v4
@@ -31,69 +33,16 @@ jobs:
3133
path: freedom-e-sdk
3234
submodules: recursive
3335

34-
- name: Workaround for sources.list
36+
- name: Trust workspace
3537
run: |
36-
# Replace sources
38+
git config --global --add safe.directory "$GITHUB_WORKSPACE"
3739
38-
set -euxo pipefail
39-
40-
# Peek (what repos are active now)
41-
apt-cache policy
42-
grep -RInE '^(deb|Types|URIs)' /etc/apt || true
43-
44-
# Enable nullglob so *.list/*.sources that don't exist don't break sed
45-
shopt -s nullglob
46-
47-
echo "Replace sources.list (legacy)"
48-
sudo sed -i \
49-
-e "s|https\?://azure\.archive\.ubuntu\.com/ubuntu/?|http://mirror.arizona.edu/ubuntu/|g" \
50-
/etc/apt/sources.list || true
51-
52-
echo "Replace sources.list.d/*.list (legacy)"
53-
for f in /etc/apt/sources.list.d/*.list; do
54-
sudo sed -i \
55-
-e "s|https\?://azure\.archive\.ubuntu\.com/ubuntu/?|http://mirror.arizona.edu/ubuntu/|g" \
56-
"$f"
57-
done
58-
59-
echo "Replace sources.list.d/*.sources (deb822)"
60-
for f in /etc/apt/sources.list.d/*.sources; do
61-
sudo sed -i \
62-
-e "s|https\?://azure\.archive\.ubuntu\.com/ubuntu/?|http://mirror.arizona.edu/ubuntu/|g" \
63-
-e "s|https\?://azure\.archive\.ubuntu\.com|http://mirror.arizona.edu|g" \
64-
"$f"
65-
done
66-
67-
echo "Fix /etc/apt/apt-mirrors.txt (used by URIs: mirror+file:...)"
68-
if grep -qE '^[[:space:]]*https?://azure\.archive\.ubuntu\.com/ubuntu/?' /etc/apt/apt-mirrors.txt; then
69-
# Replace azure with our mirror (idempotent)
70-
sudo sed -i 's|https\?://azure\.archive\.ubuntu\.com/ubuntu/|http://mirror.arizona.edu/ubuntu/|g' /etc/apt/apt-mirrors.txt
71-
fi
72-
73-
# Peek (verify changes)
74-
grep -RIn "azure.archive.ubuntu.com" /etc/apt || true
75-
grep -RInE '^(deb|Types|URIs)' /etc/apt || true
76-
echo "--- apt-mirrors.txt ---"
77-
cat /etc/apt/apt-mirrors.txt || true
78-
79-
- name: Update repository
80-
run: sudo apt-get update -o Acquire::Retries=3
40+
- name: Add preinstalled RISC-V toolchain to PATH
41+
run: |
42+
echo "/opt/xpack-riscv-none-elf-gcc-15.2.0-1/bin" >> "$GITHUB_PATH"
8143
82-
# ============================================================
83-
# xPack RISC-V GCC — single toolchain with full multilib
84-
# supporting both rv32 and rv64 targets (including rv32imac/ilp32
85-
# and rv64imac/lp64 which riscv-collab nightly builds lack).
86-
# https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack
87-
# ============================================================
88-
- name: Download and install xPack RISC-V toolchain
44+
- name: Check RISC-V toolchain
8945
run: |
90-
XPACK_VER="15.2.0-1"
91-
XPACK_FILE="xpack-riscv-none-elf-gcc-${XPACK_VER}-linux-x64.tar.gz"
92-
wget -q "https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v${XPACK_VER}/${XPACK_FILE}"
93-
echo "aaaa8060c914851a3e5ee1ba82cc3d6f80972f90638a05c6e823a37557a33758 ${XPACK_FILE}" | sha256sum -c -
94-
tar -xf "${XPACK_FILE}"
95-
echo "$GITHUB_WORKSPACE/xpack-riscv-none-elf-gcc-${XPACK_VER}/bin" >> $GITHUB_PATH
96-
export PATH="$GITHUB_WORKSPACE/xpack-riscv-none-elf-gcc-${XPACK_VER}/bin:$PATH"
9746
riscv-none-elf-gcc --version
9847
riscv-none-elf-gcc -print-multi-lib | head -5
9948

hal/library.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ int main(int argc, const char* argv[])
191191
if (((void*)gImage) == NULL) {
192192
wolfBoot_printf("failed to malloc %zu bytes for image\n", sz);
193193
ret = -1;
194+
goto close_img;
194195
}
195196

196197
bread = fread((void*)gImage, 1, sz, img);
197198
if (bread != sz) {
198199
ret = -2;
199200
wolfBoot_printf("read %zu of %zu bytes from %s\n", bread, sz, argv[1]);
200201
}
202+
203+
close_img:
201204
fclose(img);
202205
} else {
203206
wolfBoot_printf("usage: %s image_file.bin\n", argv[0]);

include/gpt.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ struct gpt_part_info {
9999
uint16_t name[GPT_PART_NAME_SIZE];
100100
};
101101

102+
/**
103+
* @brief CRC32 context for GPT header and partition-array validation.
104+
*/
105+
struct gpt_crc32_ctx {
106+
uint32_t value;
107+
};
108+
102109
/**
103110
* @brief Check MBR for protective GPT partition entry.
104111
*
@@ -139,6 +146,32 @@ int gpt_parse_header(const uint8_t *sector, struct guid_ptable *hdr);
139146
int gpt_parse_partition(const uint8_t *entry_data, uint32_t entry_size,
140147
struct gpt_part_info *part);
141148

149+
/**
150+
* @brief Initialize a GPT CRC32 calculation.
151+
*
152+
* @param[out] ctx Pointer to CRC32 context.
153+
*/
154+
void gpt_crc32_init(struct gpt_crc32_ctx *ctx);
155+
156+
/**
157+
* @brief Accumulate bytes into a GPT CRC32 calculation.
158+
*
159+
* @param[in,out] ctx Pointer to CRC32 context.
160+
* @param[in] data Pointer to input bytes.
161+
* @param[in] len Number of bytes to process.
162+
*/
163+
void gpt_crc32_update(struct gpt_crc32_ctx *ctx, const uint8_t *data,
164+
uint32_t len);
165+
166+
/**
167+
* @brief Finalize a GPT CRC32 calculation.
168+
*
169+
* @param[in] ctx Pointer to CRC32 context.
170+
*
171+
* @return Final CRC32 value.
172+
*/
173+
uint32_t gpt_crc32_final(const struct gpt_crc32_ctx *ctx);
174+
142175
/**
143176
* @brief Compare UTF-16 partition name with ASCII string.
144177
*
@@ -152,4 +185,3 @@ int gpt_parse_partition(const uint8_t *entry_data, uint32_t entry_size,
152185
int gpt_part_name_eq(const uint16_t *utf16_name, const char *ascii_label);
153186

154187
#endif /* GPT_H */
155-

src/arm_tee_psa_ipc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <psa/crypto.h>
2929
#include <psa/error.h>
3030

31+
#include <wolfssl/wolfcrypt/memory.h>
3132
#include <wolfssl/wolfcrypt/types.h>
3233
#include <wolfboot/arm_tee_api.h>
3334
#include <wolfboot/dice.h>
@@ -240,6 +241,7 @@ static struct wolfboot_ps_entry *wolfboot_ps_alloc(psa_storage_uid_t uid)
240241
{
241242
for (size_t i = 0; i < WOLFBOOT_PS_MAX_ENTRIES; i++) {
242243
if (!g_ps_entries[i].in_use) {
244+
wc_ForceZero(g_ps_entries[i].data, sizeof(g_ps_entries[i].data));
243245
g_ps_entries[i].in_use = 1;
244246
g_ps_entries[i].uid = uid;
245247
g_ps_entries[i].size = 0;
@@ -844,6 +846,7 @@ int32_t arm_tee_psa_call(psa_handle_t handle, int32_t type,
844846
if (entry == NULL) {
845847
return PSA_ERROR_DOES_NOT_EXIST;
846848
}
849+
wc_ForceZero(entry->data, sizeof(entry->data));
847850
entry->in_use = 0;
848851
entry->uid = 0;
849852
entry->size = 0;

src/delta.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
370370
pb+= BLOCK_HDR_SIZE;
371371
ctx->off_b += BLOCK_HDR_SIZE;
372372
while ((pb < pb_limit) &&
373+
(match_len < UINT16_MAX) &&
373374
(ctx->off_b < ctx->size_b) &&
374375
(*pb == *(ctx->src_b + ctx->off_b))) {
375376
/* Extend match as long as the areas have the

src/disk.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ int disk_open(int drv)
118118
uint32_t i;
119119
uint32_t n_parts = 0;
120120
uint32_t gpt_lba = 0;
121+
uint32_t chunk = 0;
122+
uint64_t array_addr = 0;
123+
uint64_t bytes_left = 0;
121124
uint8_t sector[GPT_SECTOR_SIZE] XALIGNED(4);
125+
struct gpt_crc32_ctx part_crc;
122126

123127
if ((drv < 0) || (drv >= MAX_DISKS)) {
124128
wolfBoot_printf("Attempting to access invalid drive %d\r\n", drv);
@@ -162,6 +166,34 @@ int disk_open(int drv)
162166
wolfBoot_printf("Valid GPT partition table\r\n");
163167
wolfBoot_printf("Max number of partitions: %d\r\n", ptable.n_part);
164168

169+
array_addr = ptable.start_array * GPT_SECTOR_SIZE;
170+
bytes_left = (uint64_t)ptable.n_part * ptable.array_sz;
171+
172+
gpt_crc32_init(&part_crc);
173+
while (bytes_left > 0) {
174+
chunk = GPT_SECTOR_SIZE;
175+
176+
if (bytes_left < chunk) {
177+
chunk = (uint32_t)bytes_left;
178+
}
179+
180+
r = disk_read(drv, array_addr, chunk, sector);
181+
if (r < 0) {
182+
Drives[drv].is_open = 0;
183+
return -1;
184+
}
185+
186+
gpt_crc32_update(&part_crc, sector, chunk);
187+
array_addr += chunk;
188+
bytes_left -= chunk;
189+
}
190+
191+
if (gpt_crc32_final(&part_crc) != ptable.part_crc) {
192+
wolfBoot_printf("Invalid GPT partition entry array CRC\r\n");
193+
Drives[drv].is_open = 0;
194+
return -1;
195+
}
196+
165197
n_parts = ptable.n_part;
166198
if (n_parts > MAX_PARTITIONS)
167199
n_parts = MAX_PARTITIONS;

src/gpt.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,39 @@
3434

3535
#include "gpt.h"
3636

37-
static uint32_t gpt_crc32(const uint8_t *data, uint32_t len)
37+
void gpt_crc32_init(struct gpt_crc32_ctx *ctx)
38+
{
39+
if (ctx != NULL) {
40+
ctx->value = 0xFFFFFFFFU;
41+
}
42+
}
43+
44+
void gpt_crc32_update(struct gpt_crc32_ctx *ctx, const uint8_t *data,
45+
uint32_t len)
3846
{
39-
uint32_t crc = 0xFFFFFFFFU;
4047
uint32_t i;
4148
uint32_t j;
4249

50+
if (ctx == NULL || data == NULL) {
51+
return;
52+
}
53+
4354
for (i = 0; i < len; i++) {
44-
crc ^= data[i];
55+
ctx->value ^= data[i];
4556
for (j = 0; j < 8; j++) {
46-
uint32_t mask = -(crc & 1U);
47-
crc = (crc >> 1) ^ (0xEDB88320U & mask);
57+
uint32_t mask = -(ctx->value & 1U);
58+
ctx->value = (ctx->value >> 1) ^ (0xEDB88320U & mask);
4859
}
4960
}
61+
}
62+
63+
uint32_t gpt_crc32_final(const struct gpt_crc32_ctx *ctx)
64+
{
65+
if (ctx == NULL) {
66+
return 0;
67+
}
5068

51-
return ~crc;
69+
return ~ctx->value;
5270
}
5371

5472
/**
@@ -116,6 +134,7 @@ int gpt_parse_header(const uint8_t *sector, struct guid_ptable *hdr)
116134
{
117135
const struct guid_ptable *src;
118136
struct guid_ptable tmp;
137+
struct gpt_crc32_ctx crc;
119138

120139
if (sector == NULL || hdr == NULL) {
121140
return -1;
@@ -134,7 +153,9 @@ int gpt_parse_header(const uint8_t *sector, struct guid_ptable *hdr)
134153

135154
memcpy(&tmp, src, sizeof(tmp));
136155
tmp.hdr_crc32 = 0;
137-
if (gpt_crc32((const uint8_t *)&tmp, src->hdr_size) != src->hdr_crc32) {
156+
gpt_crc32_init(&crc);
157+
gpt_crc32_update(&crc, (const uint8_t *)&tmp, src->hdr_size);
158+
if (gpt_crc32_final(&crc) != src->hdr_crc32) {
138159
return -1;
139160
}
140161

src/libwolfboot.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@
6565
#ifdef EXT_ENCRYPTED
6666
static int encrypt_key_is_erased(const uint8_t *key, uint32_t len)
6767
{
68+
const volatile uint8_t *vkey = key;
6869
uint8_t diff = 0;
6970
uint32_t i;
7071

7172
for (i = 0; i < len; i++)
72-
diff |= key[i] ^ FLASH_BYTE_ERASED;
73+
diff |= vkey[i] ^ FLASH_BYTE_ERASED;
7374

7475
return diff == 0;
7576
}
@@ -85,13 +86,14 @@ static int fallback_iv_forced = 0;
8586

8687
static int encrypt_key_is_valid(const uint8_t *key, uint32_t len)
8788
{
89+
const volatile uint8_t *vkey = key;
8890
uint8_t has_one = 0;
8991
uint8_t has_zero = 0;
9092
uint32_t i;
9193

9294
for (i = 0; i < len; i++) {
93-
has_one |= key[i];
94-
has_zero |= (uint8_t)~key[i];
95+
has_one |= vkey[i];
96+
has_zero |= (uint8_t)~vkey[i];
9597
}
9698

9799
return (has_one != 0) && (has_zero != 0);

src/multiboot.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,10 @@ static void mb2_dump_header(void* mbHeader) {
351351
MB2_DEBUG_PRINTF("Checksum: 0x%x\r\n", header->checksum);
352352

353353
tags = (uint8_t*)header + sizeof(*header);
354-
if (header->header_length < sizeof(struct mb2_header))
354+
if (header->header_length < sizeof(struct mb2_header)) {
355355
MB2_DEBUG_PRINTF("Invalid header length\r\n");
356+
return;
357+
}
356358
mb2_dump_tags(tags, header->header_length - sizeof(*header));
357359
}
358360
#endif /* DEBUG_MB2 */

0 commit comments

Comments
 (0)