mkimage (kwbimage) unmapping more bytes than it maps
Hi,
Mark and I have been looking at a problem we've been experiencing on our build system.
We're running a number of similarly configured makers and seemingly randomly one will fail to build one of our kirkwood based targets (specifically the SBx81LIFXCAT that was recently added) with a segfault in mkimage. When I tried to run the same build it would work fine on my PC (in theory the same base install as the makers).
I'd been blaming specific makers as having bad RAM (these things get hammered so we've had them die before) but Mark managed to track it down to mmap and munmap disagreeing on the size of the area mapped. You can see this by running strace on mkimage
strace -y ./tools/mkimage -n ./arch/arm/mach-mvebu/kwbimage.cfg -T kwbimage -a 0x00800000 -e 0x00800000 -d u-boot.img u-boot-spl.kwb
...mmap(NULL, 641024, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0) = 0x7f1b5ce3e000
...munmap(0x7f1b5ce3e000, 641028) = 0
Note that the size has been incremented by 4. Most of the time this is harmless but on our build system the size of the image worked out to an exact multiple of the page size so the munmap ended up spilling over and unmapping another page which depending on the in-memory layout of the process could result in a segfault.
This extra 4 bytes comes from kwbimage.c. Towards the end of kwbimage_set_header() we have
checksum = cpu_to_le32(image_checksum32((uint32_t *)ptr, sbuf->st_size)); size = write(ifd, &checksum, sizeof(uint32_t));
...sbuf->st_size += sizeof(uint32_t);
The st_size increment seems like a leftover from commit 4acd2d24b651 ("tools: kwbimage: Add image version 1 support for Armada XP / 370") as aside from the munmap in mkimage nothing else uses it.
This is where Mark and I disagree. A minimal change would simply to remove the increment of st_size from kwbimage.c. But a more robust fix would be to have mkimage not trust sbuf after it's been passed to tparams->set_header.
Is there are preference for how to handle this? I'll follow up this mail with Marks proposed fix.
PATCH 1/1 tools: mkimage: Ensure munmap unmaps the same length that was mapped
From: Mark Tomlinson <[email protected]>
The set_header call in kwbimage.c adds a checksum to the end of the image in addition to setting up the header. It 'helpfully' updates the st_size to match the fact that the file is now longer. However, mkimage uses this length in the munmap call. This can lead to unmapping an extra page, of perhaps required data. When this happens, a SEGV can occur.
To prevent this from happening, the munmap call now uses the same length that was passed to mmap. This could also have been fixed by not changing the length in kwbimage.c, however changing it in the main file means that other plugins will also not fall for the same trap.
---
tools/mkimage.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/mkimage.c +4 -2
@@ -318,6 +318,7 @@ int main(int argc, char **argv)struct image_type_params *tparams =NULL ;int pad_len =0 ;int dfd; +size_t map_len; params.cmdname = *argv; params.addr =0 ;
@@ -576,7 +577,8 @@ int main(int argc, char **argv) } params.file_size = sbuf.st_size; - ptr =mmap (0 , sbuf.st_size,PROT_READ |PROT_WRITE ,MAP_SHARED , ifd,0 ); + map_len = sbuf.st_size; + ptr =mmap (0 , map_len,PROT_READ |PROT_WRITE ,MAP_SHARED , ifd,0 );if (ptr ==MAP_FAILED ) {fprintf (stderr,"%s: Can't map %s: %s\n" , params.cmdname, params.imagefile,strerror (errno));
@@ -600,7 +602,7 @@ int main(int argc, char **argv) params.cmdname, tparams->name); } - (void )munmap ((void *)ptr, sbuf.st_size); + (void )munmap ((void *)ptr, map_len);/* We're a bit of paranoid */ #if defined(_POSIX_SYNCHRONIZED_IO) && \
Signature · 1 line
2.18.0
[U-Boot] tools: mkimage: Ensure munmap unmaps the same length that was mapped
Quoted 13 lines · Chris Packham
From: Mark Tomlinson <[email protected]>
The
set_headercall inkwbimage.cadds a checksum to the end of the image in addition to setting up the header. It 'helpfully' updates thest_sizeto match the fact that the file is now longer. However, mkimage uses this length in the munmap call. This can lead to unmapping an extra page, of perhaps required data. When this happens, a SEGV can occur.To prevent this from happening, the munmap call now uses the same length that was passed to mmap. This could also have been fixed by not changing the length in
kwbimage.c, however changing it in the main file means that other plugins will also not fall for the same trap.
Quoted 4 lines
Signed-off-by Mark Tomlinson <[email protected]>Signed-off-by Chris Packham <[email protected]>[cp: resolve checkpatch complaints]Tested-by Chris Packham <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: not available URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180911/60a2fd0c/attachment.sig>