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 2/2] environment: Do not use environment when overlapping with other partitions
Date: Fri, 21 Sep 2018 14:29:55 +0200	[thread overview]
Message-ID: <20180921122955.9513-2-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20180921122955.9513-1-s.hauer@pengutronix.de>

Environment partitions are usually specified with their hardcoded offset
and size, either in the device tree or the board file. These partitions
potentially overlap with other partitions read from the partition table.
Overlapping partitions for sure have bad effects. Be more friendly to our
users and warn them when such a situation occurs and stop using that
partition for storing the environment.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/startup.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 include/common.h | 10 ++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/common/startup.c b/common/startup.c
index 8553849cb3..f6bb1f1947 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -73,16 +73,62 @@ fs_initcall(mount_root);
 #endif
 
 #ifdef CONFIG_ENV_HANDLING
+static int check_overlap(const char *path)
+{
+	struct cdev *cenv, *cdisk, *cpart;
+
+	if (strncmp(path, "/dev/", 5))
+		return 0;
+
+	path = devpath_to_name(path);
+	cenv = cdev_by_name(path);
+	if (!cenv)
+		return -EINVAL;
+
+	cdisk = cenv->master;
+
+	if (!cdisk)
+		return 0;
+
+	list_for_each_entry(cpart, &cdisk->partitions, partition_entry) {
+		if (cpart == cenv) {
+			pr_info("Found self\n");
+			continue;
+		}
+
+		if (lregion_overlap(cpart->offset, cpart->size,
+				    cenv->offset, cenv->size))
+			goto conflict;
+	}
+
+	return 0;
+
+conflict:
+	pr_err("Environment partition (0x%08llx-0x%08llx) "
+		"overlaps with partition %s (0x%08llx-0x%08llx), not using it\n",
+		cenv->offset, cenv->offset + cenv->offset + cenv->size - 1,
+		cpart->name,
+		cpart->offset, cpart->offset + cpart->offset + cpart->size - 1);
+
+	return -EINVAL;
+}
+
 static int load_environment(void)
 {
 	const char *default_environment_path;
+	int ret;
 
 	default_environment_path = default_environment_path_get();
 
 	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT))
 		defaultenv_load("/env", 0);
 
-	envfs_load(default_environment_path, "/env", 0);
+	ret = check_overlap(default_environment_path);
+	if (ret)
+		default_environment_path_set(NULL);
+	else
+		envfs_load(default_environment_path, "/env", 0);
+
 	nvvar_load();
 
 	return 0;
diff --git a/include/common.h b/include/common.h
index abbe73fd36..f52c7e430c 100644
--- a/include/common.h
+++ b/include/common.h
@@ -157,4 +157,14 @@ static inline bool region_overlap(unsigned long starta, unsigned long lena,
 	return 1;
 }
 
+static inline bool lregion_overlap(loff_t starta, loff_t lena,
+				   loff_t startb, loff_t lenb)
+{
+	if (starta + lena <= startb)
+		return 0;
+	if (startb + lenb <= starta)
+		return 0;
+	return 1;
+}
+
 #endif	/* __COMMON_H_ */
-- 
2.19.0


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

  reply	other threads:[~2018-09-21 12:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-21 12:29 [PATCH 1/2] environment: Allow default env path to be NULL Sascha Hauer
2018-09-21 12:29 ` Sascha Hauer [this message]
2018-09-24 13:23   ` [PATCH 2/2] environment: Do not use environment when overlapping with other partitions Andrey Smirnov
2018-09-26  6:55     ` Sascha Hauer

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=20180921122955.9513-2-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