From 4c06e33c2341cdce81cf1765a3b563d5d9a2042e Mon Sep 17 00:00:00 2001 From: David O'Rourke Date: Sat, 30 May 2020 18:10:33 +0100 Subject: [PATCH] filesystem_freebsd: Fix label values We must know the length of the various filesystem C strings before turning them from a byte array into a Go string, otherwise our Go strings could contain null bytes, corrupting the label values. Signed-off-by: David O'Rourke --- collector/filesystem_freebsd.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/collector/filesystem_freebsd.go b/collector/filesystem_freebsd.go index f37029e0..8bc83140 100644 --- a/collector/filesystem_freebsd.go +++ b/collector/filesystem_freebsd.go @@ -16,6 +16,8 @@ package collector import ( + "bytes" + "github.com/go-kit/kit/log/level" "golang.org/x/sys/unix" ) @@ -40,14 +42,19 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) { } stats := []filesystemStats{} for _, fs := range buf { - mountpoint := string(fs.Mntonname[:]) + // We need to work out the lengths of the actual strings here, + // otherwuse we will end up with null bytes in our label values. + mountpoint_len := bytes.Index(fs.Mntonname[:], []byte{0}) + mountpoint := string(fs.Mntonname[:mountpoint_len]) if c.ignoredMountPointsPattern.MatchString(mountpoint) { level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint) continue } - device := string(fs.Mntfromname[:]) - fstype := string(fs.Fstypename[:]) + device_len := bytes.Index(fs.Mntfromname[:], []byte{0}) + fstype_len := bytes.Index(fs.Fstypename[:], []byte{0}) + device := string(fs.Mntfromname[:device_len]) + fstype := string(fs.Fstypename[:fstype_len]) if c.ignoredFSTypesPattern.MatchString(fstype) { level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype) continue