From: Aristo Chen <aristo.chen@canonical.com>
To: u-boot@lists.denx.de
Cc: Aristo Chen <aristo.chen@canonical.com>
Subject: [PATCH v2 0/2] tools: mkimage: fix get_basename crash on paths with dotted directories
Date: Tue, 26 May 2026 07:03:31 +0000 [thread overview]
Message-ID: <20260526070336.23199-1-aristo.chen@canonical.com> (raw)
In-Reply-To: <20260521023503.29315-1-aristo.chen@canonical.com>
The get_basename() helper in tools/fit_image.c searches the entire input
path independently for the last '/' and the last '.'. When the last '.'
falls at an offset earlier than the last '/', for example "./mydt",
"a.b/c" or "sub.d/leaf", 'end' points before 'start' and the computed
length is negative. The size check uses signed comparison so the negative
value flows unchanged into memcpy() (cast to size_t there) and mkimage
segfaults during -f auto FIT generation. The helper is reached on every
auto-FIT build via the -b, --fit-tee and --fit-tfa-bl31 file arguments.
The first patch restricts the dot search to the substring that follows
the last slash, which is the minimal fix and preserves the existing
behaviour for typical inputs such as "arch/arm/dts/foo.dtb".
The second patch adds a parametrized sandbox test under
test/py/tests/test_fit_mkimage_validate.py that drives mkimage -f auto
with each of the crashing inputs ("./mydt", "./sub.d/leaf", "./a.b/c")
plus one control input ("./mydt.dtb"). The test reads the resulting
/images/fdt-1 description back from the produced FIT via fdtget to verify
get_basename()'s output matches the expected stripped basename.
Reproducer that previously segfaulted and now produces a valid image:
echo dummy > kernel.bin
echo dummy > ./mydt
./tools/mkimage -f auto -A arm -O linux -T kernel -C none \
-a 0x80000000 -e 0x80000000 -n test \
-d kernel.bin -b ./mydt out.itb
Verified by rebuilding tools/mkimage on master and running the command
above with each of the four parametrized inputs. The three crash triggers
all segfault before the fix and now produce the expected fdt-1
descriptions ("mydt", "leaf", "c"); the control input "./mydt.dtb"
continues to produce "mydt" as before.
Changes in v2:
Patch 1/2 is unchanged code-wise and picks up Quentin Schulz's
Reviewed-by from the v1 thread. Marek Vasut asked on that thread whether
basename(3) could replace the helper; the in-thread response noted that
get_basename() also strips the extension and therefore only half of it
overlaps with basename(3), and that the GNU vs POSIX basename() selection
in tools/ is implicit (it relies on _GNU_SOURCE being defined globally
and on <libgen.h> not being included), so v2 keeps the hand-rolled
approach.
Patch 2/2 now reads the fdt sub-image description back from the produced
FIT via fdtget instead of regex-matching mkimage's console output, and
invokes ./tools/mkimage rather than building an absolute path now that
the test runs with cwd=build_dir.
v1: https://patchwork.ozlabs.org/project/uboot/patch/20260521023503.29315-1-aristo.chen@canonical.com/
Aristo Chen (2):
tools: mkimage: fix get_basename crash on paths with dotted
directories
test/py: cover get_basename crash on paths with dotted directories
test/py/tests/test_fit_mkimage_validate.py | 57 ++++++++++++++++++++++
tools/fit_image.c | 10 +++-
2 files changed, 65 insertions(+), 2 deletions(-)
--
2.43.0
next prev parent reply other threads:[~2026-05-26 8:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-21 2:34 [PATCH v1 1/2] tools: mkimage: fix get_basename crash on paths with dotted directories Aristo Chen
2026-05-21 2:35 ` [PATCH v1 2/2] test/py: cover " Aristo Chen
2026-05-21 9:10 ` Quentin Schulz
2026-05-21 15:59 ` Aristo Chen
2026-05-21 9:12 ` [PATCH v1 1/2] tools: mkimage: fix " Quentin Schulz
2026-05-21 16:09 ` Marek Vasut
2026-05-21 16:12 ` Quentin Schulz
2026-05-21 16:17 ` Marek Vasut
2026-05-22 3:07 ` Aristo Chen
2026-05-26 7:03 ` Aristo Chen [this message]
2026-05-26 7:03 ` [PATCH v2 " Aristo Chen
2026-05-26 7:03 ` [PATCH v2 2/2] test/py: cover " Aristo Chen
2026-06-12 1:59 ` [PATCH v2 0/2] tools: mkimage: fix " Tom Rini
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=20260526070336.23199-1-aristo.chen@canonical.com \
--to=aristo.chen@canonical.com \
--cc=u-boot@lists.denx.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