tools/libxenstat: don't crash when /sys/class/net can't be opened
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/3 tools/libxenstat: don't crash when /sys/class/net can't be opened
getBridge() doesn't check the return value of opendir(), so readdir() and closedir() are passed a NULL pointer when /sys/class/net can't be opened, e.g. when sysfs isn't mounted. As getBridge() is called on every network collection, the libxenstat user (xentop for instance) crashes instead of simply not finding a bridge.
Return early in that case. The caller's buffer is zero-initialised, so no bridge is reported, which xenstat_collect_networks() handles since commit 0c53660c1cf3 ("tools/libxenstat: don't lose all VIFs when dom0 has no Linux bridge").
--- tools/libs/stat/xenstat_linux.c | 3 +++ 1 file changed, 3 insertions(+)
tools/libs/stat/xenstat_linux.c +3 -0
@@ -75,6 +75,9 @@ static void getBridge(const char *excludeName, char *result, size_t resultLen)char tmp[512 ] = {0 }; d =opendir ("/sys/class/net" ); +if (d ==NULL ) +return ; +while ((de =readdir (d)) !=NULL ) {if ((strlen (de->d_name) >0 ) && (de->d_name[0 ] !='.' ) && (strstr (de->d_name, excludeName) ==NULL )) {
Signature · 10 lines
2.53.0 -- Olivier Lambert | Vates CEO XCP-ng & Xen Orchestra - Vates solutions Book a meeting with me: https://calrs.vates.tech/u/olivier-lambert web: https://vates.tech
Quoted 23 lines
getBridge()doesn't check the return value ofopendir(), soreaddir()andclosedir()are passed a NULL pointer when/sys/class/netcan't be opened, e.g. when sysfs isn't mounted. AsgetBridge()is called on every network collection, the libxenstat user (xentop for instance) crashes instead of simply not finding a bridge.Return early in that case. The caller's buffer is zero-initialised, so no bridge is reported, which
xenstat_collect_networks()handles since commit0c53660c1cf3("tools/libxenstat: don't lose all VIFs when dom0 has no Linux bridge").Fixes 1fd93d4f8cb8 ("xenstat: Linux dom0 statistics for case we use network bonding")Assisted-by Claude-Code:claude-opus-5-5Signed-off-by Olivier Lambert <[email protected]>--- tools/libs/stat/xenstat_linux.c | 3 +++ 1 file changed, 3 insertions(+)tools/libs/stat/xenstat_linux.c +0
-0
tools/libs/stat/xenstat_linux.c static void getBridge(const char *excludeName, char *result, size_t resultLen) · line 80
Quoted 6 lines
tools/libs/stat/xenstat_linux.c +3
-0@@ -75,6 +75,9 @@ static void getBridge(const char *excludeName, char *result, size_t resultLen)char tmp[512 ] = {0 }; d =opendir ("/sys/class/net" ); +if (d ==NULL ) +return ; +
We might want a message telling it failed as it's already done in the file for SYSFS_VBD_PATH.
I'd propose:
if (d == NULL) {
perror("Error opening /sys/class/net");
return;
}Signature · 1 line
Baptiste Le Duc <[email protected]>
Baptiste Le Duc
Reviewed-byQuoted 8 lines · Baptiste Le Duc
We might want a message telling it failed as it's already done in the file for
SYSFS_VBD_PATH.I'd propose:
if (d == NULL) { perror("Error opening /sys/class/net"); return; }
Please disregard my previous reply, it was a draft sent by mistake...
Returning silently seems to be fine as missing /sys/class/net isn't fatal here. Sorry for the noise.
So:
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH 2/3 tools/libxenstat: don't skip the /proc/net/dev check after a failure
In reply to a message not in the archivexenstat_collect_networks() opens /proc/net/dev once and validates its header before using it. If reading the header fails, or the header isn't the expected one, an error is returned but the stream is left open in priv->procnetdev. The next call then finds the stream already open, skips the validation and parses the file anyway.
Close the stream and clear the pointer on both error paths, so that the validation is done again on the next call.
--- tools/libs/stat/xenstat_linux.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/libs/stat/xenstat_linux.c +4 -0
@@ -296,12 +296,16 @@ int xenstat_collect_networks(xenstat_node * node)if (fread (header,sizeof (PROCNETDEV_HEADER ) -1 ,1 , priv->procnetdev) !=1 ) {perror ("Error reading /proc/net/dev header" ); +fclose (priv->procnetdev); + priv->procnetdev =NULL ;return 0 ; } header[sizeof (PROCNETDEV_HEADER ) -1 ] ='\0' ;if (strcmp (header,PROCNETDEV_HEADER ) !=0 ) {fprintf (stderr,"Unexpected /proc/net/dev format\n" ); +fclose (priv->procnetdev); + priv->procnetdev =NULL ;return 0 ; } }
Signature · 10 lines
2.53.0 -- Olivier Lambert | Vates CEO XCP-ng & Xen Orchestra - Vates solutions Book a meeting with me: https://calrs.vates.tech/u/olivier-lambert web: https://vates.tech
Re: [PATCH 2/3] tools/libxenstat: don't skip the /proc/net/dev check after a failure
Quoted 10 lines · Olivier Lambert
xenstat_collect_networks()opens/proc/net/devonce and validates its header before using it. If reading the header fails, or the header isn't the expected one, an error is returned but the stream is left open inpriv->procnetdev. The next call then finds the stream already open, skips the validation and parses the file anyway.Close the stream and clear the pointer on both error paths, so that the validation is done again on the next call.
[...]
Signature · 1 line
Baptiste Le Duc <[email protected]>
PATCH 3/3 tools/libxenstat: fix output initialisation in parseNetDevLine()
In reply to a message not in the archiveparseNetDevLine() zeroes all the outputs it is passed before parsing the line, but *rxPackets is zeroed twice while *rxMcast isn't zeroed at all, so it would be left untouched for a line that doesn't parse.
Also, iface is cleared with memset(iface, 0, sizeof(*iface)), which only covers the first byte. The function isn't told the buffer size, and an empty string is all that's needed, so write the terminator directly.
No functional change intended: the only caller passes NULL for rxMcast.
--- tools/libs/stat/xenstat_linux.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libs/stat/xenstat_linux.c +3 -3
@@ -123,7 +123,7 @@ static int parseNetDevLine(char *line, char *iface, unsigned long long *rxBytes,/* Initialize all variables called has passed as non-NULL to zeros */ if (iface !=NULL ) -memset (iface,0 ,sizeof (*iface)); + iface[0 ] ='\0' ;if (rxBytes !=NULL ) *rxBytes =0 ;if (rxPackets !=NULL )
@@ -136,10 +136,10 @@ static int parseNetDevLine(char *line, char *iface, unsigned long long *rxBytes, *rxFifo =0 ;if (rxFrames !=NULL ) *rxFrames =0 ; -if (rxPackets !=NULL ) - *rxPackets =0 ;if (rxComp !=NULL ) *rxComp =0 ; +if (rxMcast !=NULL ) + *rxMcast =0 ;if (txBytes !=NULL ) *txBytes =0 ;if (txPackets !=NULL )
Signature · 10 lines
2.53.0 -- Olivier Lambert | Vates CEO XCP-ng & Xen Orchestra - Vates solutions Book a meeting with me: https://calrs.vates.tech/u/olivier-lambert web: https://vates.tech
Re: [PATCH 3/3] tools/libxenstat: fix output initialisation in parseNetDevLine()
Quoted 8 lines
parseNetDevLine()zeroes all the outputs it is passed before parsing the line, but *rxPackets is zeroed twice while *rxMcast isn't zeroed at all, so it would be left untouched for a line that doesn't parse.Also, iface is cleared with
memset(iface, 0, sizeof(*iface)), which only covers the first byte. The function isn't told the buffer size, and an empty string is all that's needed, so write the terminator directly.
Quoted 5 lines
No functional change intended: the only caller passes NULL for rxMcast.
Assisted-by Claude-Code:claude-opus-5-5Signed-off-by Olivier Lambert <[email protected]>---
The subject says "fix", but the body says no functional change. Either
it's a bug fix and wants a Fixes: tag, or it isn't, and then I'd use
Amends: 1fd93d4f8cb8 ("xenstat: Linux dom0 statistics for case we use
network bonding") and drop "fix" from the subject. Based on the changes, I think the latter.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH 3/3] tools/libxenstat: fix output initialisation in parseNetDevLine()
Quoted 4 lines · Baptiste Le Duc
The subject says "fix", but the body says no functional change. Either
it's a bug fix and wants a Fixes: tag, or it isn't, and then I'd use
Amends:1fd93d4f8cb8("xenstat: Linux dom0 statistics for case we use
network bonding") and drop "fix" from the subject. Based on the changes, I think the latter.
Agreed, it isn't a bug fix: the only caller passes NULL for rxMcast, and the other two changes don't alter behaviour. All three come from 1fd93d4f8cb8, so Amends: fits.
In v2 the subject will be "tools/libxenstat: clean up output initialisation in parseNetDevLine()", with the tag added. I'll wait a bit for other comments before sending it.
Thanks for the review,
Olivier
--
Olivier Lambert | Vates CEO
XCP-ng & Xen Orchestra - Vates solutions
Book a meeting with me: https://calrs.vates.tech/u/olivier-lambert
web: https://vates.tech