mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/3] scripts/common: Do not mmap in read_file_2()
Date: Fri, 11 Feb 2022 10:42:28 +0100	[thread overview]
Message-ID: <20220211094230.1807262-1-s.hauer@pengutronix.de> (raw)

Using mmap() in read_file_2() leads to problems because there are
cases where the buffer returned from read_file_2() is modified and
then written back to the same file. This doesn't work when the
original file has been mmapped instead of being read into an allocated
buffer.

Using mmap() was introduced for a usecase where the system is very tight
in memory and bareboximd ran out of memory. Support for this usecase is
removed here, we'll bring it back in the next patch.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/common.c | 53 +++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/scripts/common.c b/scripts/common.c
index 154d6dffcb..3d07be3630 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -17,7 +17,7 @@
 int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
 {
 	off_t fsize;
-	ssize_t rsize;
+	ssize_t read_size, now;
 	int ret, fd;
 	void *buf;
 
@@ -37,8 +37,10 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	if (fsize < max_size)
-		max_size = fsize;
+	if (max_size < fsize)
+		read_size = max_size;
+	else
+		read_size = fsize;
 
 	if (lseek(fd, 0, SEEK_SET) == -1) {
 		fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno));
@@ -46,35 +48,30 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si
 		goto close;
 	}
 
-	buf = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	if (buf == MAP_FAILED ) {
-		buf = malloc(max_size);
-		if (!buf) {
-			fprintf(stderr, "Cannot allocate memory\n");
-			ret = -ENOMEM;
-			goto close;
-		}
-
-		*outbuf = buf;
+	buf = malloc(read_size);
+	if (!buf) {
+		fprintf(stderr, "Cannot allocate memory\n");
+		ret = -ENOMEM;
+		goto close;
+	}
 
-		while (*size < max_size) {
-			rsize = read(fd, buf, max_size - *size);
-			if (rsize == 0) {
-				ret = -EIO;
-				goto free;
-			}
+	*outbuf = buf;
 
-			if (rsize < 0) {
-				ret = -errno;
-				goto free;
-			}
+	while (read_size) {
+		now = read(fd, buf, read_size);
+		if (now == 0) {
+			ret = -EIO;
+			goto free;
+		}
 
-			buf += rsize;
-			*size += rsize;
+		if (now < 0) {
+			ret = -errno;
+			goto free;
 		}
-	} else {
-		*outbuf = buf;
-		*size = max_size;
+
+		buf += now;
+		*size += now;
+		read_size -= now;
 	}
 
 	ret = 0;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


             reply	other threads:[~2022-02-11  9:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11  9:42 Sascha Hauer [this message]
2022-02-11  9:42 ` [PATCH 2/3] scripts: bareboximd: Use mmap when possible Sascha Hauer
2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
2022-02-24 13:22   ` Andrej Picej
2022-02-28 10:16     ` Sascha Hauer
2022-02-28 11:40       ` Andrej Picej

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=20220211094230.1807262-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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