2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2021-03-04 04:12:09 +00:00
|
|
|
"path/filepath"
|
2020-07-28 20:17:38 +00:00
|
|
|
"runtime"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-08-04 04:51:02 +00:00
|
|
|
"sync/atomic"
|
2020-07-28 20:17:38 +00:00
|
|
|
"syscall"
|
2020-03-14 03:53:58 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-11 23:45:12 +00:00
|
|
|
"go4.org/mem"
|
2020-03-14 03:53:58 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Reading the sockfiles on Linux is very fast, so we can do it often.
|
2020-03-14 03:53:58 +00:00
|
|
|
const pollInterval = 1 * time.Second
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
var sockfiles = []string{"/proc/net/tcp", "/proc/net/tcp6", "/proc/net/udp", "/proc/net/udp6"}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2022-08-04 04:51:02 +00:00
|
|
|
var sawProcNetPermissionErr atomic.Bool
|
2020-07-28 20:17:38 +00:00
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
const (
|
|
|
|
v6Localhost = "00000000000000000000000001000000:"
|
|
|
|
v6Any = "00000000000000000000000000000000:0000"
|
|
|
|
v4Localhost = "0100007F:"
|
|
|
|
v4Any = "00000000:0000"
|
|
|
|
)
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func listPorts() (List, error) {
|
2022-08-04 04:51:02 +00:00
|
|
|
if sawProcNetPermissionErr.Load() {
|
2020-07-28 20:17:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-09-13 04:16:03 +00:00
|
|
|
var ret []Port
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2021-09-13 04:16:03 +00:00
|
|
|
var br *bufio.Reader
|
2021-03-04 04:12:09 +00:00
|
|
|
for _, fname := range sockfiles {
|
2020-07-28 20:17:38 +00:00
|
|
|
// Android 10+ doesn't allow access to this anymore.
|
|
|
|
// https://developer.android.com/about/versions/10/privacy/changes#proc-net-filesystem
|
|
|
|
// Ignore it rather than have the system log about our violation.
|
|
|
|
if runtime.GOOS == "android" && syscall.Access(fname, unix.R_OK) != nil {
|
2022-08-04 04:51:02 +00:00
|
|
|
sawProcNetPermissionErr.Store(true)
|
2020-07-28 20:17:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
f, err := os.Open(fname)
|
2020-07-28 20:17:38 +00:00
|
|
|
if os.IsPermission(err) {
|
2022-08-04 04:51:02 +00:00
|
|
|
sawProcNetPermissionErr.Store(true)
|
2020-07-28 20:17:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %s", fname, err)
|
|
|
|
}
|
2021-09-13 04:16:03 +00:00
|
|
|
if br == nil {
|
|
|
|
br = bufio.NewReader(f)
|
|
|
|
} else {
|
|
|
|
br.Reset(f)
|
|
|
|
}
|
|
|
|
ports, err := parsePorts(br, filepath.Base(fname))
|
|
|
|
f.Close()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2021-03-04 04:12:09 +00:00
|
|
|
return nil, fmt.Errorf("parsing %q: %w", fname, err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-09-13 04:16:03 +00:00
|
|
|
ret = append(ret, ports...)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-09-13 04:16:03 +00:00
|
|
|
return ret, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 02:49:37 +00:00
|
|
|
// fileBase is one of "tcp", "tcp6", "udp", "udp6".
|
|
|
|
func parsePorts(r *bufio.Reader, fileBase string) ([]Port, error) {
|
|
|
|
proto := strings.TrimSuffix(fileBase, "6")
|
2021-03-04 04:12:09 +00:00
|
|
|
var ret []Port
|
|
|
|
|
|
|
|
// skip header row
|
2021-09-13 15:48:38 +00:00
|
|
|
_, err := r.ReadSlice('\n')
|
2021-03-04 04:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:45:12 +00:00
|
|
|
fields := make([]mem.RO, 0, 20) // 17 current fields + some future slop
|
|
|
|
|
2021-09-13 02:49:37 +00:00
|
|
|
wantRemote := mem.S(v4Any)
|
|
|
|
if strings.HasSuffix(fileBase, "6") {
|
|
|
|
wantRemote = mem.S(v6Any)
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:48:38 +00:00
|
|
|
// remoteIndex is the index within a line to the remote address field.
|
|
|
|
// -1 means not yet found.
|
|
|
|
remoteIndex := -1
|
|
|
|
|
|
|
|
// Add an upper bound on how many rows we'll attempt to read just
|
|
|
|
// to make sure this doesn't consume too much of their CPU.
|
|
|
|
// TODO(bradfitz,crawshaw): adaptively adjust polling interval as function
|
|
|
|
// of open sockets.
|
|
|
|
const maxRows = 1e6
|
|
|
|
rows := 0
|
|
|
|
|
|
|
|
// Scratch buffer for making inode strings.
|
|
|
|
inoBuf := make([]byte, 0, 50)
|
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
for err == nil {
|
2021-09-11 23:45:12 +00:00
|
|
|
line, err := r.ReadSlice('\n')
|
2021-03-04 04:12:09 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-13 15:48:38 +00:00
|
|
|
rows++
|
|
|
|
if rows >= maxRows {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// On the first row of output, find the index of the 3rd field (index 2),
|
|
|
|
// the remote address. All the rows are aligned, at least until 4 billion open
|
|
|
|
// TCP connections, per the Linux get_tcp4_sock's "%4d: " on an int i.
|
|
|
|
if remoteIndex == -1 {
|
|
|
|
remoteIndex = fieldIndex(line, 2)
|
|
|
|
if remoteIndex == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 04:12:09 +00:00
|
|
|
|
2021-09-13 15:48:38 +00:00
|
|
|
if len(line) < remoteIndex || !mem.HasPrefix(mem.B(line).SliceFrom(remoteIndex), wantRemote) {
|
2021-09-13 02:49:37 +00:00
|
|
|
// Fast path for not being a listener port.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
// sl local rem ... inode
|
2021-09-11 23:45:12 +00:00
|
|
|
fields = mem.AppendFields(fields[:0], mem.B(line))
|
|
|
|
local := fields[1]
|
|
|
|
rem := fields[2]
|
|
|
|
inode := fields[9]
|
2021-03-04 04:12:09 +00:00
|
|
|
|
2021-09-13 02:49:37 +00:00
|
|
|
if !rem.Equal(wantRemote) {
|
|
|
|
// not a "listener" port
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
// If a port is bound to localhost, ignore it.
|
|
|
|
// TODO: localhost is bigger than 1 IP, we need to ignore
|
|
|
|
// more things.
|
2021-09-11 23:45:12 +00:00
|
|
|
if mem.HasPrefix(local, mem.S(v4Localhost)) || mem.HasPrefix(local, mem.S(v6Localhost)) {
|
2021-03-04 04:12:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use strings.Split here, because it causes
|
|
|
|
// allocations significant enough to show up in profiles.
|
2021-09-11 23:45:12 +00:00
|
|
|
i := mem.IndexByte(local, ':')
|
2021-03-04 04:12:09 +00:00
|
|
|
if i == -1 {
|
2021-09-11 23:45:12 +00:00
|
|
|
return nil, fmt.Errorf("%q unexpectedly didn't have a colon", local.StringCopy())
|
2021-03-04 04:12:09 +00:00
|
|
|
}
|
2021-09-11 23:45:12 +00:00
|
|
|
portv, err := mem.ParseUint(local.SliceFrom(i+1), 16, 16)
|
2021-03-04 04:12:09 +00:00
|
|
|
if err != nil {
|
2021-09-11 23:45:12 +00:00
|
|
|
return nil, fmt.Errorf("%#v: %s", local.SliceFrom(9).StringCopy(), err)
|
2021-03-04 04:12:09 +00:00
|
|
|
}
|
2021-09-11 23:45:12 +00:00
|
|
|
inoBuf = append(inoBuf[:0], "socket:["...)
|
|
|
|
inoBuf = mem.Append(inoBuf, inode)
|
|
|
|
inoBuf = append(inoBuf, ']')
|
2021-03-04 04:12:09 +00:00
|
|
|
ret = append(ret, Port{
|
|
|
|
Proto: proto,
|
|
|
|
Port: uint16(portv),
|
2021-09-11 23:45:12 +00:00
|
|
|
inode: string(inoBuf),
|
2021-03-04 04:12:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func addProcesses(pl []Port) ([]Port, error) {
|
2020-03-14 03:53:58 +00:00
|
|
|
pm := map[string]*Port{} // by Port.inode
|
|
|
|
for i := range pl {
|
|
|
|
pm[pl[i].inode] = &pl[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
err := foreachPID(func(pid string) error {
|
2020-07-28 20:17:38 +00:00
|
|
|
fdPath := fmt.Sprintf("/proc/%s/fd", pid)
|
|
|
|
|
|
|
|
// Android logs a bunch of audit violations in logcat
|
|
|
|
// if we try to open things we don't have access
|
|
|
|
// to. So on Android only, ask if we have permission
|
|
|
|
// rather than just trying it to determine whether we
|
|
|
|
// have permission.
|
|
|
|
if runtime.GOOS == "android" && syscall.Access(fdPath, unix.R_OK) != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fdDir, err := os.Open(fdPath)
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
// Can't open fd list for this pid. Maybe
|
|
|
|
// don't have access. Ignore it.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer fdDir.Close()
|
|
|
|
|
|
|
|
targetBuf := make([]byte, 64) // plenty big for "socket:[165614651]"
|
|
|
|
for {
|
|
|
|
fds, err := fdDir.Readdirnames(100)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-19 05:48:55 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// This can happen if the directory we're
|
|
|
|
// reading disappears during the run. No big
|
|
|
|
// deal.
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
2020-05-04 14:44:41 +00:00
|
|
|
return fmt.Errorf("addProcesses.readDir: %w", err)
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
|
|
n, err := unix.Readlink(fmt.Sprintf("/proc/%s/fd/%s", pid, fd), targetBuf)
|
|
|
|
if err != nil {
|
|
|
|
// Not a symlink or no permission.
|
|
|
|
// Skip it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pe := pm[string(targetBuf[:n])] // m[string([]byte)] avoids alloc
|
|
|
|
if pe != nil {
|
2021-03-04 03:00:41 +00:00
|
|
|
bs, err := ioutil.ReadFile(fmt.Sprintf("/proc/%s/cmdline", pid))
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
// Usually shouldn't happen. One possibility is
|
|
|
|
// the process has gone away, so let's skip it.
|
|
|
|
continue
|
|
|
|
}
|
2021-03-04 03:00:41 +00:00
|
|
|
|
|
|
|
argv := strings.Split(strings.TrimSuffix(string(bs), "\x00"), "\x00")
|
|
|
|
pe.Process = argvSubject(argv...)
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
return pl, nil
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
func foreachPID(fn func(pidStr string) error) error {
|
2020-02-05 22:16:58 +00:00
|
|
|
pdir, err := os.Open("/proc")
|
|
|
|
if err != nil {
|
2020-03-14 03:53:58 +00:00
|
|
|
return err
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
defer pdir.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
pids, err := pdir.Readdirnames(100)
|
|
|
|
if err == io.EOF {
|
2020-03-14 03:53:58 +00:00
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-05-19 05:48:55 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// This can happen if the directory we're
|
|
|
|
// reading disappears during the run. No big
|
|
|
|
// deal.
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-05-04 14:44:41 +00:00
|
|
|
return fmt.Errorf("foreachPID.readdir: %w", err)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pid := range pids {
|
|
|
|
_, err := strconv.ParseInt(pid, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
// not a pid, ignore it.
|
|
|
|
// /proc has lots of non-pid stuff in it.
|
|
|
|
continue
|
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
if err := fn(pid); err != nil {
|
|
|
|
return err
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 02:49:37 +00:00
|
|
|
|
|
|
|
// fieldIndex returns the offset in line where the Nth field (0-based) begins, or -1
|
|
|
|
// if there aren't that many fields. Fields are separated by 1 or more spaces.
|
|
|
|
func fieldIndex(line []byte, n int) int {
|
|
|
|
skip := 0
|
|
|
|
for i := 0; i <= n; i++ {
|
|
|
|
// Skip spaces.
|
|
|
|
for skip < len(line) && line[skip] == ' ' {
|
|
|
|
skip++
|
|
|
|
}
|
|
|
|
if skip == len(line) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if i == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Skip non-space.
|
|
|
|
for skip < len(line) && line[skip] != ' ' {
|
|
|
|
skip++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return skip
|
|
|
|
}
|