From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>,
Fabian Pfitzner <f.pfitzner@pengutronix.de>
Subject: [PATCH v2025.09.y 13/49] state: guard against empty variable set in DT
Date: Fri, 19 Dec 2025 10:20:49 +0100 [thread overview]
Message-ID: <20251219101453.2806980-14-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251219101453.2806980-1-a.fatoum@pengutronix.de>
We always initialize state->variables, but there are two places in the
code that assume there is at least one entry already.
Change them to use list_first_entry_or_null/list_last_entry_or_null as
appropriate to catch this issue gracefully.
This should have only affected state nodes without children, which is
not a useful device tree description, but nevertheless we should handle
that somehow instead of reading uninitialized values that may trigger
a panic or other misbehavior.
Reported-by: Fabian Pfitzner <f.pfitzner@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20251103111624.1848136-1-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
(cherry picked from commit f1b549a97e503ccc227d2f103fa73351ebf3fca1)
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/state/backend_format_raw.c | 16 ++++++++++++++--
common/state/state.c | 8 +++++---
include/linux/list.h | 14 ++++++++++++++
3 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/common/state/backend_format_raw.c b/common/state/backend_format_raw.c
index 5fb38cd711da..b7b88dd5b94e 100644
--- a/common/state/backend_format_raw.c
+++ b/common/state/backend_format_raw.c
@@ -198,6 +198,18 @@ static int backend_format_raw_unpack(struct state_backend_format *format,
return ret;
}
+static inline size_t state_data_size(struct state *state)
+{
+ const struct state_variable *sv;
+
+ /* Make use of the fact that the list is sorted in ascending order */
+ sv = list_last_entry_or_null(&state->variables, struct state_variable, list);
+ if (!sv)
+ return 0;
+
+ return sv->start + sv->size;
+}
+
static int backend_format_raw_pack(struct state_backend_format *format,
struct state *state, void ** buf_out,
ssize_t * len_out)
@@ -216,8 +228,8 @@ static int backend_format_raw_pack(struct state_backend_format *format,
return ret;
}
- sv = list_last_entry(&state->variables, struct state_variable, list);
- size_data = sv->start + sv->size;
+ size_data = state_data_size(state);
+
size_full = size_data + sizeof(*header) + backend_raw->digest_length;
buf = xzalloc(size_full);
diff --git a/common/state/state.c b/common/state/state.c
index ac6cd6e57276..bafc07dfe751 100644
--- a/common/state/state.c
+++ b/common/state/state.c
@@ -405,10 +405,12 @@ int state_from_node(struct state *state, struct device_node *node, bool create)
if (create) {
const struct state_variable *sv;
- /* start with second entry */
- sv = list_first_entry(&state->variables, struct state_variable,
- list);
+ /* no variable = no variable overlap */
+ sv = list_first_entry_or_null(&state->variables, struct state_variable, list);
+ if (!sv)
+ return 0;
+ /* start with second entry */
list_for_each_entry_continue(sv, &state->variables, list) {
const struct state_variable *last_sv;
diff --git a/include/linux/list.h b/include/linux/list.h
index b90ea3e125d0..a036e3d07c07 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -529,6 +529,20 @@ static inline void list_splice_tail_init(struct list_head *list,
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
+/**
+ * list_last_entry_or_null - get the last element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note that if the list is empty, it returns NULL.
+ */
+#define list_last_entry_or_null(ptr, type, member) ({ \
+ struct list_head *head__ = (ptr); \
+ struct list_head *pos__ = READ_ONCE(head__->prev); \
+ pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
+})
+
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
--
2.47.3
next prev parent reply other threads:[~2025-12-19 10:39 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-19 9:20 [PATCH v2025.09.y 00/49] Backports for v2025.09.1 Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 01/49] mci: sdhci: am654: Use sdhci_wait_idle() Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 02/49] clk: rockchip: rk3588: Add PLL rate for 1500 MHz Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 03/49] ARM: i.MX6: configure AIPS registers only if trusted Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 04/49] ARM: dts: rockchip: Set initial CPU frequencies for RK3588 Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 05/49] ARM: dts: rockchip: Set CPLL frequency " Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 06/49] scripts: rockchip: rkimage: reinstate OpenSSL 1.1 compatibility Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 07/49] ARM: dts: am62lx: fix secondary core startup Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 08/49] ARM: i.MX: Fix the iomux DSE defines for imx8mp Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 09/49] virtio: Stop spamming debug buffer Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 10/49] clk: clk_set_parent: skip any operation if current and new parents are equal Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 11/49] range: fix corner cases when exclusive end is zero Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 12/49] mtd: peb: mtd_peb_read(): move error message to caller Ahmad Fatoum
2025-12-19 9:20 ` Ahmad Fatoum [this message]
2025-12-19 9:20 ` [PATCH v2025.09.y 14/49] fs: nfs: drop PROG_NFS special casing Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 15/49] fs: nfs: do not read past packets Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 16/49] ARM: i.MX8M: romapi: skip zero_page_access() if MMU is disabled Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 17/49] ARM: mmu: have zero_page_remappable() check MMU status Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 18/49] ARM: mmu: fix hang when reserved memory at start of RAM Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 19/49] clk: rockchip rk3588: configure CPLL in driver Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 20/49] poller: activate POLLER_WARN_OVERTIME only on third strike Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 21/49] bootm: fix boot override inheritance Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 22/49] commands: truncate: fix multiple file handling with relative sizes Ahmad Fatoum
2025-12-19 9:20 ` [PATCH v2025.09.y 23/49] fs: qemu_fw_cfg: make use of the automount Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 24/49] fs: qemu_fw_cfg: handle non-existent opt/org.barebox.env key gracefully Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 25/49] boards: qemu-virt: drop fitimage-pubkey.dts Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 26/49] pci: pci-tegra: fix null pointer assignation Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 27/49] sandbox: fix race with .old_version removal Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 28/49] net: designware-imx: don't set txclk when mode is (r)mii Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 29/49] mci: am654-sdhci: correctly configure SLOTTYPE_EMBEDDED Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 30/49] scripts: imx: Makefile.mingw64: add _GNU_SOURCE Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 31/49] scripts: imx: add pread implementation for mingw target Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 32/49] net: arp: collect context into new struct pending_arp Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 33/49] net: reset pending ARP state when request is done Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 34/49] test: populate qemu_bin, even if missing features Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 35/49] ARM: i.MX8M: Fix the RAM size calculation for DDR3 Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 36/49] test: exit directly when invoking QEMU states on non-QEMU targets Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 37/49] tlv: register_device() error handling Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 38/49] fs: fix automount when last filename component is a automountpoint Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 39/49] ARM: K3: am62l: don't put sysfwdata into 1st stage image Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 40/49] ARM: k3: make some firmware files SoC specific Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 41/49] ARM: k3: am62l: Add dedicated config option for OP-TEE Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 42/49] ARM: k3: am62l: fix dependencies on k3img input files Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 43/49] lib: base64: Fix out-of-bounds potential by respecting dst_len Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 44/49] param: make setting a parameter to the same string value a no-op Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 45/49] i2c: omap: fix probe on am335x Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 46/49] net: 9p: fix variable name in BAREBOX_MAGICVAR Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 47/49] pmdomain: fix collision in names when registering Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 48/49] i2c: implement i2c_unregister_device() Ahmad Fatoum
2025-12-19 9:21 ` [PATCH v2025.09.y 49/49] eeprom: at24: unregister devices in error path Ahmad Fatoum
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=20251219101453.2806980-14-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=f.pfitzner@pengutronix.de \
/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