scripts/common: fix write_file when opened with mmap
PATCH 1/1 scripts/common: fix write_file when opened with mmap
Usage of bareboximd with -c option (Create checksum for FILE and write it to the crc32 tag) was broken. Possibly by applying 2154de1cf36c (bareboximd: Use mmap when possibly). The script fails with:
$ ./scripts/bareboximd -c images/<barebox-image.img>
Cannot write to images/<barebox-mage.img>: Bad address
CRC: write crc token to images/<barebox-image.img> failed: -14
Bad addressThis has to do with the usage of "mmap" and "open" with O_TRUNC flag which truncates the file length to 0. Writing to files fails with:
EFAULT (14) buf is outside your accessible address space. Remove the truncate flag and truncate manually after writing the data.
This fixes the bareboximd script, which is now again usable with -c option.
--- scripts/common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/common.c +6 -1
@@ -107,7 +107,7 @@ int write_file(const char *filename, const void *buf, size_t size)int fd, ret =0 ;int now; - fd =open (filename,O_WRONLY |O_TRUNC |O_CREAT , + fd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));
@@ -126,6 +126,11 @@ int write_file(const char *filename, const void *buf, size_t size) buf += now; } +if (ftruncate (fd, now) <0 ) { +fprintf (stderr,"Cannot truncate file: %s" ,strerror (errno)); + ret = -errno; + } + out:close (fd);
Signature · 7 lines
2.25.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Hi Andrej,
Quoted 9 lines · Andrej Picej
Usage of bareboximd with -c option (Create checksum for FILE and write it to the crc32 tag) was broken. Possibly by applying
2154de1cf36c(bareboximd: Use mmap when possibly). The script fails with:$ ./scripts/bareboximd -c images/<barebox-image.img> Cannot write to images/<barebox-mage.img>: Bad address CRC: write crc token to images/<barebox-image.img> failed: -14 Bad address
Quoted 7 lines
This has to do with the usage of "mmap" and "open" with
O_TRUNCflag which truncates the file length to 0. Writing to files fails with:
EFAULT (14) buf is outside your accessible address space. Remove the truncate flag and truncate manually after writing the data.This fixes the bareboximd script, which is now again usable with -c option.
That's a very unfortunate bug :(
Quoted 12 lines
Signed-off-by Anze Lesnik <[email protected]>Signed-off-by Andrej Picej <[email protected]>--- scripts/common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/common.c +0
-0
scripts/common.c int write_file(const char *filename, const void *buf, size_t size) · line 110
Quoted 4 lines
scripts/common.c +0
-1@@ -107,7 +107,7 @@ int write_file(const char *filename, const void *buf, size_t size)int fd, ret =0 ;int now; - fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,
I added a comment above this while applying to prevent people from "optimizing" this patch away.
scripts/common.c int write_file(const char *filename, const void *buf, size_t size) · line 132
Quoted 12 lines
scripts/common.c +5
-0+ fd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));@@ -126,6 +126,11 @@ int write_file(const char *filename, const void *buf, size_t size) buf += now; } +if (ftruncate (fd, now) <0 ) { +fprintf (stderr,"Cannot truncate file: %s" ,strerror (errno)); + ret = -errno; + }
truncating to 'now' is only correct when we were able to write all bytes at once. Otherwise we must truncate to the input size. I fixed this up to the patch below.
Sascha
-----------------------------8<----------------------------
Quoted 1 line
From
738601e1258c55953284ee10801b26b9977918c2Mon Sep 17 00:00:00 2001
From: Andrej Picej <[email protected]>
Date: Fri, 28 Jan 2022 09:00:33 +0100
Subject: [PATCH] scripts/common: fix write_file when opened with mmap
Usage of bareboximd with -c option (Create checksum for FILE and write it to the crc32 tag) was broken. Possibly by applying 2154de1cf36c (bareboximd: Use mmap when possibly). The script fails with:
$ ./scripts/bareboximd -c images/<barebox-image.img>
Cannot write to images/<barebox-mage.img>: Bad address
CRC: write crc token to images/<barebox-image.img> failed: -14
Bad addressThis has to do with the usage of "mmap" and "open" with O_TRUNC flag which truncates the file length to 0. Writing to files fails with:
EFAULT (14) buf is outside your accessible address space. Remove the truncate flag and truncate manually after writing the data.
This fixes the bareboximd script, which is now again usable with -c option.
--- scripts/common.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
scripts/common.c +11 -4
@@ -105,26 +105,33 @@ int write_file(const char *filename, const void *buf, size_t size) {int fd, ret =0 ;int now; +size_t left = size; - fd =open (filename,O_WRONLY |O_TRUNC |O_CREAT , +/* The same file may be mmapped currently, so can't use O_TRUNC here */ + fd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));return -errno; } -while (size) { - now =write (fd, buf, size); +while (left) { + now =write (fd, buf, left);if (now <0 ) {fprintf (stderr,"Cannot write to %s: %s\n" , filename,strerror (errno)); ret = -errno; goto out; } - size -= now; + left -= now; buf += now; } +if (ftruncate (fd, size) <0 ) { +fprintf (stderr,"Cannot truncate file: %s" ,strerror (errno)); + ret = -errno; + } + out:close (fd);
Signature · 12 lines
2.30.2 -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Hi Sascha,
Quoted 50 lines · Sascha Hauer
Hi Andrej,
Quoted 9 lines · Andrej Picej
Usage of bareboximd with -c option (Create checksum for FILE and write it to the crc32 tag) was broken. Possibly by applying
2154de1cf36c(bareboximd: Use mmap when possibly). The script fails with:$ ./scripts/bareboximd -c images/<barebox-image.img> Cannot write to images/<barebox-mage.img>: Bad address CRC: write crc token to images/<barebox-image.img> failed: -14 Bad addressQuoted 7 lines
This has to do with the usage of "mmap" and "open" with
O_TRUNCflag which truncates the file length to 0. Writing to files fails with:
EFAULT (14) buf is outside your accessible address space. Remove the truncate flag and truncate manually after writing the data.This fixes the bareboximd script, which is now again usable with -c option.
That's a very unfortunate bug :(
Quoted 12 lines
Signed-off-by Anze Lesnik <[email protected]>Signed-off-by Andrej Picej <[email protected]>--- scripts/common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/common.c +0
-0scripts/common.c int write_file(const char *filename, const void *buf, size_t size) · line 110
Quoted 4 lines
scripts/common.c +0
-1@@ -107,7 +107,7 @@ int write_file(const char *filename, const void *buf, size_t size)int fd, ret =0 ;int now; - fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,I added a comment above this while applying to prevent people from "optimizing" this patch away.
Quoted 8 lines
scripts/common.c +1
-0+ fd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));@@ -126,6 +126,11 @@ int write_file(const char *filename, const void *buf, size_t size) buf += now; }
Quoted 8 lines
scripts/common.c int write_file(const char *filename, const void *buf, size_t size) · line 133
Quoted 4 lines
scripts/common.c +4
-0+if (ftruncate (fd, now) <0 ) { +fprintf (stderr,"Cannot truncate file: %s" ,strerror (errno)); + ret = -errno; + }truncating to 'now' is only correct when we were able to write all bytes at once. Otherwise we must truncate to the input size. I fixed this up to the patch below.
That makes sense. Good thing you caught that.
Thanks.
Quoted 68 lines
Sascha
-----------------------------8<----------------------------
From
738601e1258c55953284ee10801b26b9977918c2Mon Sep 17 00:00:00 2001
From: Andrej Picej <[email protected]>
Date: Fri, 28 Jan 2022 09:00:33 +0100
Subject: [PATCH]scripts/common: fixwrite_filewhen opened with mmapUsage of bareboximd with -c option (Create checksum for FILE and write it to the crc32 tag) was broken. Possibly by applying
2154de1cf36c(bareboximd: Use mmap when possibly). The script fails with:$ ./scripts/bareboximd -c images/<barebox-image.img> Cannot write to images/<barebox-mage.img>: Bad address CRC: write crc token to images/<barebox-image.img> failed: -14 Bad addressThis has to do with the usage of "mmap" and "open" with
O_TRUNCflag which truncates the file length to 0. Writing to files fails with:
EFAULT (14) buf is outside your accessible address space. Remove the truncate flag and truncate manually after writing the data.This fixes the bareboximd script, which is now again usable with -c option.
Signed-off-by Anze Lesnik <[email protected]>Signed-off-by Andrej Picej <[email protected]>Signed-off-by Sascha Hauer <[email protected]>--- scripts/common.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)scripts/common.c +6
-4@@ -105,26 +105,33 @@ int write_file(const char *filename, const void *buf, size_t size) {int fd, ret =0 ;int now; +size_t left = size; - fd =open (filename,O_WRONLY |O_TRUNC |O_CREAT , +/* The same file may be mmapped currently, so can't use O_TRUNC here */ + fd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));return -errno; } -while (size) { - now =write (fd, buf, size); +while (left) { + now =write (fd, buf, left);if (now <0 ) {fprintf (stderr,"Cannot write to %s: %s\n" , filename,strerror (errno)); ret = -errno; goto out; } - size -= now; + left -= now; buf += now;
scripts/common.c int write_file(const char *filename, const void *buf, size_t size) · line 137
Quoted 10 lines
scripts/common.c +5
-0} +if (ftruncate (fd, size) <0 ) { +fprintf (stderr,"Cannot truncate file: %s" ,strerror (errno)); + ret = -errno; + } + out:close (fd);
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox