zcalusic / sysinfo

Sysinfo is a Go library providing Linux OS / kernel / hardware system information.
MIT License
518 stars 91 forks source link

GetSysInfo segfaults #29

Closed jm33-m0 closed 3 years ago

jm33-m0 commented 3 years ago

I got a segfault when calling si.GetSysInfo, as shown in the stack trace.

The code responsible for this segfault is:

https://github.com/zcalusic/sysinfo/blob/aff387a52b3af60423548f44c504eee6d2d647ec/network.go#L92

unexpected fault address 0x0                                                                                                                                            
fatal error: fault                                                                                                                                                      
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x561cda8bc692]                                                                                           

goroutine 1 [running]:                                                                                                                                                  
runtime.throw({0x561cda8e0b5d, 0xc0002b7838})                                                                                                                           
        runtime/panic.go:1198 +0x71 fp=0xc0002b7800 sp=0xc0002b77d0 pc=0x561cda52ceb1                                                                                   
runtime.sigpanic()                                                                                                                                                      
        runtime/signal_unix.go:742 +0x2f6 fp=0xc0002b7850 sp=0xc0002b7800 pc=0x561cda542656                                                                             
github.com/zcalusic/sysinfo.getSupported({0xc00002278f, 0x4})                                                                                                           
        github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/network.go:92 +0x172 fp=0xc0002b7908 sp=0xc0002b7850 pc=0x561cda8bc692                           
github.com/zcalusic/sysinfo.(*SysInfo).getNetworkInfo(0xc0002b7b38)                                                                                                     
        github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/network.go:117 +0x1da fp=0xc0002b7a60 sp=0xc0002b7908 pc=0x561cda8bc97a                          
github.com/zcalusic/sysinfo.(*SysInfo).GetSysInfo(0xc0002b7b38)                                                                                                         
        github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/sysinfo.go:44 +0xcf fp=0xc0002b7a78 sp=0xc0002b7a60 pc=0x561cda8bf02f                            
github.com/jm33-m0/emp3r0r/core/lib/agent.CollectSystemInfo()
jm33-m0 commented 3 years ago

I compiled this si on my laptop (go 1.17 on Linux 5.13) and it works fine, but on a VMware guest (Linux 5.10, with the same si) it fails:

┌──(k㉿kali)-[~/Desktop/si]
└─$ ./si                                                                                                                                                            1 ⨯
unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x4b5d92]

goroutine 1 [running]:
runtime.throw({0x4d7eac, 0xc000043a60})
        /usr/lib/go/src/runtime/panic.go:1198 +0x71 fp=0xc000043a28 sp=0xc0000439f8 pc=0x431391
runtime.sigpanic()
        /usr/lib/go/src/runtime/signal_unix.go:742 +0x2f6 fp=0xc000043a78 sp=0xc000043a28 pc=0x444e96
github.com/zcalusic/sysinfo.getSupported({0xc0000147bf, 0x4})
        /home/jm33/go/pkg/mod/github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/network.go:92 +0x172 fp=0xc000043b30 sp=0xc000043a78 pc=0x4b5d92
github.com/zcalusic/sysinfo.(*SysInfo).getNetworkInfo(0xc000043ce8)
        /home/jm33/go/pkg/mod/github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/network.go:117 +0x1da fp=0xc000043c88 sp=0xc000043b30 pc=0x4b607a
github.com/zcalusic/sysinfo.(*SysInfo).GetSysInfo(0xc000043ce8)
        /home/jm33/go/pkg/mod/github.com/zcalusic/sysinfo@v0.0.0-20210609180555-aff387a52b3a/sysinfo.go:44 +0xcf fp=0xc000043ca0 sp=0xc000043c88 pc=0x4b872f
main.main()
        /tmp/si/main.go:11 +0x5d fp=0xc000043f80 sp=0xc000043ca0 pc=0x4b8f1d
runtime.main()
        /usr/lib/go/src/runtime/proc.go:255 +0x227 fp=0xc000043fe0 sp=0xc000043f80 pc=0x433a67
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc000043fe8 sp=0xc000043fe0 pc=0x45d421

┌──(k㉿kali)-[~/Desktop/si]
└─$ cat main.go                                                                                                                                                     2 ⨯
package main

import (
        "fmt"

        "github.com/zcalusic/sysinfo"
)

func main() {
        var si sysinfo.SysInfo
        si.GetSysInfo()
        fmt.Printf("%s", si.Node.Hostname)
}

However, if I compile si on the VMware guest and then run it, there's no segfault

mudler commented 3 years ago

Happens here to me as well, but just with golang 1.17 and on certain hosts, it doesn't happen for example on a vbox VM. go 1.16.x is fine with it: indeed https://golang.org/doc/go1.17#language seems to indicate the reason why

mudler commented 3 years ago

I've assembled a PR here https://github.com/zcalusic/sysinfo/pull/31 which fixes (just tested locally, where I could reproduce) the issue

cc @zcalusic :bow: could you please have a look? Thank you!

zcalusic commented 3 years ago

Oh dear, what a nasty little bug. Now I see why they called it "unsafe". :yum:

First of all, thank you @jm33-m0 for reporting the bug, and especially thank you @mudler for taking the time to dissect and eventually fix it.

I've been exceptionally busy last few days, so it would take me a bit more time to attend to this issue, but I guess no excuses now when you actually did the whole work. I've tested it briefly on one of my machines where I observed a segfault, and it seems to work just fine.

Will be applied in a matter of minutes. Thanks again, I guess I owe you one. :+1:

mudler commented 3 years ago

Oh dear, what a nasty little bug. Now I see why they called it "unsafe". :yum:

First of all, thank you @jm33-m0 for reporting the bug, and especially thank you @mudler for taking the time to disect and eventually fix it.

I've been exceptionally busy last few days, so it would take me a bit more time to attend to this issue, but I guess no excuses now when you actually did the whole work. I've tested it briefly on one of my machine where I observed a segfault, and it seems to work just fine.

Will be applied in a matter of minutes. Thanks again, I guess I owe you one. :+1:

Thanks to you actually! That was quick! 😁

Cheers