tools/xenconsoled: fix detection of sscanf() failure in xs_gather()
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.
---
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
@@ -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
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
-1if (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
Quoted 1 line · Stephen Cheng
Reviewed-by Stephen Cheng <[email protected]>
Thanks for the review, Stephen.
Agreed, this can't happen with nodes written by libxl, as the guest only has read access. The patch is only about not using a value that was never parsed.
Bala