Skip to contentSkip to conversation

tools/xenstored: Don't assume errno will not be overwritten in lu_arch()

PATCH In review Julien Grall 2 participants 4 messages Jul 29, 2021 – Jul 30, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenstored: Don't assume errno will not be overwritten in lu_arch()

From: Julien Grall <[email protected]>

At the moment, do_control_lu() will set errno to 0 before calling lu_arch() and then check errno. The expectation is nothing in lu_arch() will change the value unless there is an error.

However, per errno(3), a function that succeeds is allowed to change errno. In fact, syslog() will overwrite errno if the logs are rotated at the time it is called.

To prevent any further issue, errno is now always set before returning NULL.

Additionally, errno is only checked when returning NULL so the client can see the error message if there is any.

---
 tools/xenstore/xenstored_control.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/xenstore/xenstored_control.c +3 -2
index 6b68b79faac7..6fcb42095b59 100644
@@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void *ctx, struct connection *conn,
 	lu_status->kernel_size = size;
 	lu_status->kernel_off = 0;
 
+	errno = 0;
 	return NULL;
 }
 
@@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, struct connection *conn,
 	memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
 	lu_status->kernel_off += size;
 
+	errno = 0;
 	return NULL;
 }
 
@@ -798,9 +800,8 @@ static int do_control_lu(void *ctx, struct connection *conn,
 		if (!ret)
 			return errno;
 	} else {
-		errno = 0;
 		ret = lu_arch(ctx, conn, vec, num);
-		if (errno)
+		if (!ret && errno)
 			return errno;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 35 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, do_control_lu() will set errno to 0 before calling lu_arch() and then check errno. The expectation is nothing in lu_arch() will change the value unless there is an error.

However, per errno(3), a function that succeeds is allowed to change errno. In fact, syslog() will overwrite errno if the logs are rotated at the time it is called.

To prevent any further issue, errno is now always set before returning NULL.

Additionally, errno is only checked when returning NULL so the client can see the error message if there is any.

---
  tools/xenstore/xenstored_control.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
tools/xenstore/xenstored_control.c +1 -0
index 6b68b79faac7..6fcb42095b59 100644
@@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void *ctx, struct connection *conn,
  	lu_status->kernel_size = size;
  	lu_status->kernel_off = 0;
 
+	errno = 0;
  	return NULL;
  }
 

tools/xenstore/xenstored_control.c static const char *lu_binary_save(const void *ctx, struct connection *conn, · line 346

Quoted 7 lines
tools/xenstore/xenstored_control.c +1 -0
@@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, struct connection *conn,
  	memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
  	lu_status->kernel_off += size;
 
+	errno = 0;
  	return NULL;
  }
 

I forgot to update lu_binary(). I will respin the patch once I get some feedback.

Cheers,

Quoted 12 lines
tools/xenstore/xenstored_control.c +1 -2
@@ -798,9 +800,8 @@ static int do_control_lu(void *ctx, struct connection *conn,
  		if (!ret)
  			return errno;
  	} else {
-		errno = 0;
  		ret = lu_arch(ctx, conn, vec, num);
-		if (errno)
+		if (!ret && errno)
  			return errno;
  	}
 
Signature · 1 line
Julien Grall
Reviewed-by
#2 lore raw
Quoted 37 lines · Julien Grall
Quoted 34 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, do_control_lu() will set errno to 0 before calling lu_arch() and then check errno. The expectation is nothing in lu_arch() will change the value unless there is an error.

However, per errno(3), a function that succeeds is allowed to change errno. In fact, syslog() will overwrite errno if the logs are rotated at the time it is called.

To prevent any further issue, errno is now always set before returning NULL.

Additionally, errno is only checked when returning NULL so the client can see the error message if there is any.

---
  tools/xenstore/xenstored_control.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
tools/xenstore/xenstored_control.c +1 -0
index 6b68b79faac7..6fcb42095b59 100644
@@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void 
 *ctx, struct connection *conn,
       lu_status->kernel_size = size;
       lu_status->kernel_off = 0;
+    errno = 0;
       return NULL;
   }
@@ -339,6 +340,7 @@ static const char *lu_binary_save(const void *ctx, 
Quoted 8 lines

tools/xenstore/xenstored_control.c static const char *lu_binary_save(const void *ctx, struct connection *conn, · line 345

Quoted 6 lines
tools/xenstore/xenstored_control.c +1 -0
 struct connection *conn,
       memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
       lu_status->kernel_off += size;
+    errno = 0;
       return NULL;
   }

I forgot to update lu_binary(). I will respin the patch once I get some feedback.

With setting errno to 0 before returning NULL in lu_binary() you can add

Juergen

#3 lore raw

Hi Juergen,

Quoted 47 lines · Juergen Gross
Quoted 37 lines · Julien Grall
Quoted 34 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment, do_control_lu() will set errno to 0 before calling lu_arch() and then check errno. The expectation is nothing in lu_arch() will change the value unless there is an error.

However, per errno(3), a function that succeeds is allowed to change errno. In fact, syslog() will overwrite errno if the logs are rotated at the time it is called.

To prevent any further issue, errno is now always set before returning NULL.

Additionally, errno is only checked when returning NULL so the client can see the error message if there is any.

---
  tools/xenstore/xenstored_control.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
tools/xenstore/xenstored_control.c +1 -0
index 6b68b79faac7..6fcb42095b59 100644
@@ -324,6 +324,7 @@ static const char *lu_binary_alloc(const void 
 *ctx, struct connection *conn,
       lu_status->kernel_size = size;
       lu_status->kernel_off = 0;
+    errno = 0;
       return NULL;
   }
@@ -339,6 +340,7 @@ static const char *lu_binary_save(const void 
Quoted 8 lines

tools/xenstore/xenstored_control.c static const char *lu_binary_save(const void *ctx, struct connection *conn, · line 345

Quoted 6 lines
tools/xenstore/xenstored_control.c +1 -0
 *ctx, struct connection *conn,
       memcpy(lu_status->kernel + lu_status->kernel_off, data, size);
       lu_status->kernel_off += size;
+    errno = 0;
       return NULL;
   }

I forgot to update lu_binary(). I will respin the patch once I get some feedback.

Quoted 3 lines

With setting errno to 0 before returning NULL in lu_binary() you can add

Thanks! I will commit it.

Cheers,

Signature · 1 line
Julien Grall