Skip to contentSkip to conversation

tools/xenconsoled: fix detection of sscanf() failure in xs_gather()

PATCH In review Bala Andegue François Lionnel 2 participants 3 messages Nov 27 – Dec 3

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenconsoled: fix detection of sscanf() failure in xs_gather()

xs_gather() only treats a return value of 0 from sscanf() as a parse failure. However, sscanf() returns EOF if the input ends before the first conversion, e.g. when the xenstore node holds an empty string. In that case the result is left untouched, but xs_gather() still reports success.

console_create_ring() then goes on with ring_ref or remote_port uninitialised, while a non-numeric value such as "abc" is correctly rejected with EINVAL.

All callers pass a single conversion, so treat any return value other than 1 as an error.

Assisted-by Claude:claude-opus-5-5

---

Notes:
Tested in a nested setup (Xen built from staging, busybox dom0 and a PV guest under QEMU/KVM). With a temporary debug log in console_create_ring(), writing "" to the guest's console/port node from dom0 gave err=0 before this patch, and EINVAL after it, the same as for "abc".

 tools/console/daemon/io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/console/daemon/io.c +1 -1
index b6c46d11dea7..728dd01fba31 100644
@@ -654,7 +654,7 @@ static int xs_gather(struct xs_handle *xs, const char *dir, ...)
 			break;
 		}
 		if (fmt) {
-			if (sscanf(p, fmt, result) == 0)
+			if (sscanf(p, fmt, result) != 1)
 				ret = EINVAL;
 			free(p);
 		} else

base-commit: abc47aca98fa87e9fdebd0e9f7fbbbaa482056fb

Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

tools/console/daemon/io.c static int xs_gather(struct xs_handle *xs, const char *dir, ...) · line 660

Quoted 7 lines · Bala Andegue François Lionnel

[...]

tools/console/daemon/io.c +1 -1
 		if (fmt) {
-			if (sscanf(p, fmt, result) == 0)
+			if (sscanf(p, fmt, result) != 1)
 				ret = EINVAL;
 			free(p);
 		} else

An empty value should not happen in normal use, since libxl always writes a number to these nodes and the guest can only read them. But the fix is still correct: xs_gather() passes one result pointer to each sscanf() call, so a format can only have one conversion and 1 is the only success value.

Stephen