mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* Fix some longstanding environment bugs
@ 2014-04-09  7:39 Sascha Hauer
  2014-04-09  7:39 ` [PATCH 1/4] unlink_recursive: return negative error value Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-04-09  7:39 UTC (permalink / raw)
  To: barebox

When an environment doesn't fit into the environment partition saveenv
still returned success. Also loading this corrupted partition resulted
in success. Fix this.

----------------------------------------------------------------
Sascha Hauer (4):
      unlink_recursive: return negative error value
      loadenv: ignore -ENOENT when removing /env
      saveenv: Properly detect write errors
      loadenv: detect truncated environment files

 commands/loadenv.c     |  2 +-
 common/environment.c   | 38 ++++++++++++++++++++++++++++----------
 lib/unlink-recursive.c |  2 +-
 3 files changed, 30 insertions(+), 12 deletions(-)

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] unlink_recursive: return negative error value
  2014-04-09  7:39 Fix some longstanding environment bugs Sascha Hauer
@ 2014-04-09  7:39 ` Sascha Hauer
  2014-04-09  7:39 ` [PATCH 2/4] loadenv: ignore -ENOENT when removing /env Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-04-09  7:39 UTC (permalink / raw)
  To: barebox

In case of an error unlink_recursive returns errno which is positive.
Return -errno instead.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/unlink-recursive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/unlink-recursive.c b/lib/unlink-recursive.c
index 8f7812f..78dc015 100644
--- a/lib/unlink-recursive.c
+++ b/lib/unlink-recursive.c
@@ -53,5 +53,5 @@ int unlink_recursive(const char *path, char **failedpath)
 	if (!ret && failedpath)
 		*failedpath = unlink_recursive_failedpath;
 
-	return ret ? 0 : errno;
+	return ret ? 0 : -errno;
 }
-- 
1.9.1


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] loadenv: ignore -ENOENT when removing /env
  2014-04-09  7:39 Fix some longstanding environment bugs Sascha Hauer
  2014-04-09  7:39 ` [PATCH 1/4] unlink_recursive: return negative error value Sascha Hauer
@ 2014-04-09  7:39 ` Sascha Hauer
  2014-04-09  7:39 ` [PATCH 3/4] saveenv: Properly detect write errors Sascha Hauer
  2014-04-09  7:39 ` [PATCH 4/4] loadenv: detect truncated environment files Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-04-09  7:39 UTC (permalink / raw)
  To: barebox

With the -s option loadenv first removes /env. Ignore it when this
directory does not exist.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/loadenv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/loadenv.c b/commands/loadenv.c
index 7b93e86..a39ca8c 100644
--- a/commands/loadenv.c
+++ b/commands/loadenv.c
@@ -80,7 +80,7 @@ static int do_loadenv(int argc, char *argv[])
 		int ret;
 
 		ret = unlink_recursive(dirname, NULL);
-		if (ret) {
+		if (ret && ret != -ENOENT) {
 			eprintf("cannot remove %s: %s\n", dirname,
 					strerror(-ret));
 			return 1;
-- 
1.9.1


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] saveenv: Properly detect write errors
  2014-04-09  7:39 Fix some longstanding environment bugs Sascha Hauer
  2014-04-09  7:39 ` [PATCH 1/4] unlink_recursive: return negative error value Sascha Hauer
  2014-04-09  7:39 ` [PATCH 2/4] loadenv: ignore -ENOENT when removing /env Sascha Hauer
@ 2014-04-09  7:39 ` Sascha Hauer
  2014-04-09  7:39 ` [PATCH 4/4] loadenv: detect truncated environment files Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-04-09  7:39 UTC (permalink / raw)
  To: barebox

The return value check of the write call is completely bogus. We
check if we have written at minimum sizeof(struct envfs_super) bytes
instead of all bytes. Properly check for all bytes written instead
and allow write to write less bytes than requested.
Do not use write_full because this file is compiled for userspace
aswell.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/environment.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/common/environment.c b/common/environment.c
index 9f4e098..bf813b4 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -201,11 +201,16 @@ int envfs_save(const char *filename, const char *dirname)
 		goto out1;
 	}
 
-	if (write(envfd, buf, size  + sizeof(struct envfs_super)) <
-			sizeof(struct envfs_super)) {
-		perror("write");
-		ret = -1;	/* FIXME */
-		goto out;
+	size += sizeof(struct envfs_super);
+
+	while (size) {
+		ssize_t now = write(envfd, buf, size);
+		if (now < 0) {
+			ret = -errno;
+			goto out;
+		}
+
+		size -= now;
 	}
 
 	ret = 0;
-- 
1.9.1


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] loadenv: detect truncated environment files
  2014-04-09  7:39 Fix some longstanding environment bugs Sascha Hauer
                   ` (2 preceding siblings ...)
  2014-04-09  7:39 ` [PATCH 3/4] saveenv: Properly detect write errors Sascha Hauer
@ 2014-04-09  7:39 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2014-04-09  7:39 UTC (permalink / raw)
  To: barebox

Properly detect when an environment file is truncated. This can happen
when a previous saveenv failed because the environment partition is too
small.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/environment.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/common/environment.c b/common/environment.c
index bf813b4..abd69c5 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -409,11 +409,24 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
 		goto out;
 
 	buf = xmalloc(size);
-	ret = read(envfd, buf, size);
-	if (ret < size) {
-		perror("read");
-		ret = -errno;
-		goto out;
+
+	while (size) {
+		ssize_t now;
+
+		now = read(envfd, buf, size);
+		if (now < 0) {
+			perror("read");
+			ret = -errno;
+			goto out;
+		}
+
+		if (!now) {
+			printf("%s: premature end of file\n", filename);
+			ret = -EINVAL;
+			goto out;
+		}
+
+		size -= now;
 	}
 
 	ret = envfs_check_data(&super, buf, size);
-- 
1.9.1


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-04-09  7:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-09  7:39 Fix some longstanding environment bugs Sascha Hauer
2014-04-09  7:39 ` [PATCH 1/4] unlink_recursive: return negative error value Sascha Hauer
2014-04-09  7:39 ` [PATCH 2/4] loadenv: ignore -ENOENT when removing /env Sascha Hauer
2014-04-09  7:39 ` [PATCH 3/4] saveenv: Properly detect write errors Sascha Hauer
2014-04-09  7:39 ` [PATCH 4/4] loadenv: detect truncated environment files Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox