mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master] nv: fix use-after-free when clearing from shell
@ 2020-11-12 17:19 Ahmad Fatoum
  2020-11-13  7:35 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2020-11-12 17:19 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum, Holger Assmann

From: Ahmad Fatoum <ahmad@a3f.at>

When we use hush to set the same nv.var twice to the empty string:

  $ nv.user=
  $ nv.user=

nv_set is called twice with a NULL val argument leading
to a double free and accompanied memory corruption.

Reorder the code, so p->value is freed just once.

Fixes: fa4c41ba60af ("nvvar: when setting a nvvar to NULL just free the content")
Cc: Holger Assmann <has@pengutronix.de>
Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 common/globalvar.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/common/globalvar.c b/common/globalvar.c
index 60793d7a30c6..a55b38b00fd5 100644
--- a/common/globalvar.c
+++ b/common/globalvar.c
@@ -179,16 +179,12 @@ static int nv_set(struct device_d *dev, struct param_d *p, const char *name, con
 {
 	int ret;
 
-	if (!val) {
-		if (p)
-			free(p->value);
-		return 0;
+	if (val) {
+		ret = dev_set_param(&global_device, name, val);
+		if (ret)
+			return ret;
 	}
 
-	ret = dev_set_param(&global_device, name, val);
-	if (ret)
-		return ret;
-
 	if (p) {
 		free(p->value);
 		p->value = xstrdup(val);
-- 
2.28.0


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

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

end of thread, other threads:[~2020-11-13  7:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12 17:19 [PATCH master] nv: fix use-after-free when clearing from shell Ahmad Fatoum
2020-11-13  7:35 ` Sascha Hauer

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