Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix host.Info() panic if /etc/debian_version is empty #822

Merged
merged 1 commit into from
Jan 18, 2020

Conversation

tgross
Copy link
Contributor

@tgross tgross commented Jan 17, 2020

The ReadLines helper function doesn't guarantee that the length of lines is non-zero or that the lines have contents. Most callers include a check for length but this was missing for version fingerprinting on Debian if /etc/debian_version was empty, leading to a panic.

This contributed to a panic for a user of Nomad in hashicorp/nomad#6954. A minimal reproduction using a Debian Buster container is below.

To fix, I've reused the same length check done everywhere else in the host_linux.go file, and extended the zero-check for the Alpine version fingerprinting to match as well.


Reproduction:

main.go:

package main

import (
	"fmt"
	"log"

	"github.com/shirou/gopsutil/host"
)

func main() {
	info, err := host.Info()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(info)
}

Before fix:

$ sudo  mv /etc/debian_version /etc/debian_version.bak
$ sudo touch /etc/debian_version
$ go run main.go  | jq .
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/shirou/gopsutil/host.PlatformInformationWithContext(0x565600, 0xc00009e030, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0000b0000)
        /home/me/go/src/github.com/shirou/gopsutil/host/host_linux.go:284 +0x1b4e
github.com/shirou/gopsutil/host.PlatformInformation(0xc00009e040, 0xc, 0x0, 0x0, 0x30, 0x30, 0x5321c0, 0x612160)
        /home/me/go/src/github.com/shirou/gopsutil/host/host_linux.go:240 +0x3d
github.com/shirou/gopsutil/host.InfoWithContext(0x565600, 0xc00009e030, 0x438b87, 0x614ea0, 0xc00003e750)
        /home/me/go/src/github.com/shirou/gopsutil/host/host_linux.go:48 +0x8c
github.com/shirou/gopsutil/host.Info(...)
        /home/me/go/src/github.com/shirou/gopsutil/host/host_linux.go:35
main.main()
        /home/me/go/src/github.com/hashicorp/issues/6954_gopsutil/main.go:11 +0x3d
exit status 2

After fix:

$ sudo  mv /etc/debian_version /etc/debian_version.bak
$ sudo touch /etc/debian_version
$ go run main.go  | jq .
{
  "hostname": "cf4038b71167",
  "uptime": 143893,
  "bootTime": 1579144017,
  "procs": 4,
  "os": "linux",
  "platform": "debian",
  "platformFamily": "debian",
  "platformVersion": "",
  "kernelVersion": "4.9.184-linuxkit",
  "kernelArch": "x86_64",
  "virtualizationSystem": "docker",
  "virtualizationRole": "guest",
  "hostid": "0f454763-0000-0000-84ca-5540bf5161f8"
}

The ReadLines helper function doesn't guarantee that the length of
lines is non-zero or that the lines have contents. Most callers
include a check for length but this was missing for version
fingerprinting on Debian if `/etc/debian_version` was empty, leading
to a panic.
@shirou
Copy link
Owner

shirou commented Jan 18, 2020

So, is there an case about debian_version file exists but empty? hmm. thank you for the info!

@shirou shirou merged commit 3964e5b into shirou:master Jan 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants