Skip to contentSkip to conversation

tools/xenstored: Harden corrupt()

PATCH In review Julien Grall 4 participants 8 messages Jun 23, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenstored: Harden corrupt()

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")
---
 tools/xenstore/xenstored_core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/xenstore/xenstored_core.c +5 -1
index fa733e714e9a..b6279bdfe229 100644
@@ -2065,7 +2065,11 @@ void corrupt(struct connection *conn, const char *fmt, ...)
 	va_end(arglist);
 
 	log("corruption detected by connection %i: err %s: %s",
-	    conn ? (int)conn->id : -1, strerror(saved_errno), str);
+	    conn ? (int)conn->id : -1, strerror(saved_errno),
+	    str ? str : "ENOMEM");
+
+	if (str)
+		talloc_free(str);
 
 	check_store();
 }
Signature · 1 line
2.32.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")
---
  tools/xenstore/xenstored_core.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
tools/xenstore/xenstored_core.c +0 -0
index fa733e714e9a..b6279bdfe229 100644

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const char *fmt, ...) · line 2071

Quoted 8 lines
tools/xenstore/xenstored_core.c +4 -1
@@ -2065,7 +2065,11 @@ void corrupt(struct connection *conn, const char *fmt, ...)
  	va_end(arglist);
 
  	log("corruption detected by connection %i: err %s: %s",
-	    conn ? (int)conn->id : -1, strerror(saved_errno), str);
+	    conn ? (int)conn->id : -1, strerror(saved_errno),
+	    str ? str : "ENOMEM");
+
+	if (str)

No need for the "if". talloc_free() handles NULL quite fine.

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const char *fmt, ...) · line 2075

Quoted 4 lines
tools/xenstore/xenstored_core.c +1 -0
+		talloc_free(str);
 
  	check_store();
  }

With above fixed:

Juergen

#2 lore raw

Hi Juergen,

Quoted 20 lines · Juergen Gross
Quoted 19 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")

---
  tools/xenstore/xenstored_core.c | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 15 lines

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const · line 2071

Quoted 13 lines
tools/xenstore/xenstored_core.c +4 -1
index fa733e714e9a..b6279bdfe229 100644
@@ -2065,7 +2065,11 @@ void corrupt(struct connection *conn, const 
 char *fmt, ...)
       va_end(arglist);
       log("corruption detected by connection %i: err %s: %s",
-        conn ? (int)conn->id : -1, strerror(saved_errno), str);
+        conn ? (int)conn->id : -1, strerror(saved_errno),
+        str ? str : "ENOMEM");
+
+    if (str)

No need for the "if". talloc_free() handles NULL quite fine.

In my original approach, I wasn't checking "if (str)" when I looked at talloc_free(), I noticed it would return -1 (i.e. the memory wasn't freed) when str is NULL.

I also couldn't find any example in Xenstored where talloc_free() would be called with NULL. So I felt it was not a good idea to add one because talloc_free() technically return a "failure".

That said, I would not strongly argue to keep it. So I am happy to drop the check if that's what you want.

Quoted 8 lines

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const char *fmt, ...) · line 2075

Quoted 4 lines
tools/xenstore/xenstored_core.c +1 -0
+        talloc_free(str);
       check_store();
   }

With above fixed:

Thanks!

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw
In reply to Juergen Gross
Quoted 21 lines · Juergen Gross
Quoted 20 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic

ability to recover from store")

---
  tools/xenstore/xenstored_core.c | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const · line 2069

Quoted 12 lines
tools/xenstore/xenstored_core.c +2 -1
index fa733e714e9a..b6279bdfe229 100644
@@ -2065,7 +2065,11 @@ void corrupt(struct connection *conn, const
 char *fmt, ...)
       va_end(arglist);
         log("corruption detected by connection %i: err %s: %s",
-        conn ? (int)conn->id : -1, strerror(saved_errno), str);
+        conn ? (int)conn->id : -1, strerror(saved_errno),
+        str ? str : "ENOMEM");

str ?: "ENOMEM"

seeing as we use this idiom in a lot of places.

~Andrew

#4 lore raw

Hi Andrew,

Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Juergen Gross
Quoted 20 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic

ability to recover from store")

---
   tools/xenstore/xenstored_core.c | 6 +++++-
   1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
tools/xenstore/xenstored_core.c +0 -1
index fa733e714e9a..b6279bdfe229 100644
--- a/tools/xenstore/xenstored_core.c
Quoted 8 lines

tools/xenstore/xenstored_core.c void corrupt(struct connection *conn, const · line 2069

Quoted 8 lines

+++ b/tools/xenstore/xenstored_core.c

tools/xenstore/xenstored_core.c +2 -1
@@ -2065,7 +2065,11 @@ void corrupt(struct connection *conn, const
 char *fmt, ...)
       va_end(arglist);
         log("corruption detected by connection %i: err %s: %s",
-        conn ? (int)conn->id : -1, strerror(saved_errno), str);
+        conn ? (int)conn->id : -1, strerror(saved_errno),
+        str ? str : "ENOMEM");

str ?: "ENOMEM"

Sure. I have updated the patch and committed it.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw
In reply to Julien Grall
Quoted 8 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

Quoted 7 lines

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")

Is this something which would want queuing for backport?

Jan

#6 lore raw
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Quoted 4 lines
Quoted 2 lines
Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")

Is this something which would want queuing for backport?

I would say yes. There are a couple of more Xenstored patches I would consider for backporting:

fe9be76d880b tools/xenstore: fix error handling of check_store() b977929d3646 tools/xenstore: fix hashtable_expand() zeroing new area

Who is taking care of tools backport nowadays?

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw
Quoted 22 lines · Julien Grall
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, corrupt() is neither checking for allocation failure nor freeing the allocated memory.

Harden the code by printing ENOMEM if the allocation failed and free 'str' after the last use.

This is not considered to be a security issue because corrupt() should only be called when Xenstored thinks the database is corrupted. Note that the trigger (i.e. a guest reliably provoking the call) would be a security issue.

Quoted 4 lines
Quoted 2 lines
Fixes 06d17943f0cd ("Added a basic integrity checker, and some basic ability to recover from store")

Is this something which would want queuing for backport?

Quoted 7 lines

I would say yes. There are a couple of more Xenstored patches I would consider for backporting:

fe9be76d880b tools/xenstore: fix error handling of check_store() b977929d3646 tools/xenstore: fix hashtable_expand() zeroing new area

Who is taking care of tools backport nowadays?

I'm trying to, as long as they apply cleanly enough. But I'd prefer if rather sooner then later I could offload this again. And I'm not actively looking to spot backporting candidates there (unlike for the hypervisor, excluding Arm).

Jan