github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/runsc/boot/procfs/dump.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package procfs holds utilities for getting procfs information for sandboxed
    16  // processes.
    17  package procfs
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/metacubex/gvisor/pkg/abi/linux"
    25  	"github.com/metacubex/gvisor/pkg/context"
    26  	"github.com/metacubex/gvisor/pkg/hostarch"
    27  	"github.com/metacubex/gvisor/pkg/log"
    28  	"github.com/metacubex/gvisor/pkg/sentry/fsimpl/proc"
    29  	"github.com/metacubex/gvisor/pkg/sentry/kernel"
    30  	"github.com/metacubex/gvisor/pkg/sentry/limits"
    31  	"github.com/metacubex/gvisor/pkg/sentry/mm"
    32  	"github.com/metacubex/gvisor/pkg/sentry/vfs"
    33  )
    34  
    35  // FDInfo contains information about an application file descriptor.
    36  type FDInfo struct {
    37  	// Number is the FD number.
    38  	Number int32 `json:"number"`
    39  	// Path is the path of the file that FD represents.
    40  	Path string `json:"path,omitempty"`
    41  	// Mode is the file mode.
    42  	Mode uint16 `json:"mode"`
    43  }
    44  
    45  // UIDGID contains information for /proc/[pid]/status/{uid,gid}.
    46  type UIDGID struct {
    47  	Real      uint32 `json:"real"`
    48  	Effective uint32 `json:"effective"`
    49  	Saved     uint32 `json:"saved"`
    50  }
    51  
    52  // Status contains information for /proc/[pid]/status.
    53  type Status struct {
    54  	Comm   string `json:"comm,omitempty"`
    55  	PID    int32  `json:"pid"`
    56  	PPID   int32  `json:"ppid"`
    57  	UID    UIDGID `json:"uid,omitempty"`
    58  	GID    UIDGID `json:"gid,omitempty"`
    59  	VMSize uint64 `json:"vm_size,omitempty"`
    60  	VMRSS  uint64 `json:"vm_rss,omitempty"`
    61  }
    62  
    63  // Stat contains information for /proc/[pid]/stat.
    64  type Stat struct {
    65  	PGID int32 `json:"pgid"`
    66  	SID  int32 `json:"sid"`
    67  }
    68  
    69  // Mapping contains information for /proc/[pid]/maps.
    70  type Mapping struct {
    71  	Address     hostarch.AddrRange  `json:"address,omitempty"`
    72  	Permissions hostarch.AccessType `json:"permissions"`
    73  	Private     string              `json:"private,omitempty"`
    74  	Offset      uint64              `json:"offset"`
    75  	DevMajor    uint32              `json:"deviceMajor,omitempty"`
    76  	DevMinor    uint32              `json:"deviceMinor,omitempty"`
    77  	Inode       uint64              `json:"inode,omitempty"`
    78  	Pathname    string              `json:"pathname,omitempty"`
    79  }
    80  
    81  // ProcessProcfsDump contains the procfs dump for one process. For more details
    82  // on fields that directly correspond to /proc fields, see proc(5).
    83  type ProcessProcfsDump struct {
    84  	// Exe is the symlink target of /proc/[pid]/exe.
    85  	Exe string `json:"exe,omitempty"`
    86  	// Args is /proc/[pid]/cmdline split into an array.
    87  	Args []string `json:"args,omitempty"`
    88  	// Env is /proc/[pid]/environ split into an array.
    89  	Env []string `json:"env,omitempty"`
    90  	// CWD is the symlink target of /proc/[pid]/cwd.
    91  	CWD string `json:"cwd,omitempty"`
    92  	// FDs contains the directory entries of /proc/[pid]/fd and also contains the
    93  	// symlink target for each FD.
    94  	FDs []FDInfo `json:"fdlist,omitempty"`
    95  	// StartTime is the process start time in nanoseconds since Unix epoch.
    96  	StartTime int64 `json:"clone_ts,omitempty"`
    97  	// Root is /proc/[pid]/root.
    98  	Root string `json:"root,omitempty"`
    99  	// Limits constains resource limits for this process. Currently only
   100  	// RLIMIT_NOFILE is supported.
   101  	Limits map[string]limits.Limit `json:"limits,omitempty"`
   102  	// Cgroup is /proc/[pid]/cgroup split into an array.
   103  	Cgroup []kernel.TaskCgroupEntry `json:"cgroup,omitempty"`
   104  	// Status is /proc/[pid]/status.
   105  	Status Status `json:"status,omitempty"`
   106  	// Stat is /proc/[pid]/stat.
   107  	Stat Stat `json:"stat,omitempty"`
   108  	// Maps is /proc/[pid]/maps.
   109  	Maps []Mapping `json:"maps,omitempty"`
   110  }
   111  
   112  // getMM returns t's MemoryManager. On success, the MemoryManager's users count
   113  // is incremented, and must be decremented by the caller when it is no longer
   114  // in use.
   115  func getMM(t *kernel.Task) *mm.MemoryManager {
   116  	var mm *mm.MemoryManager
   117  	t.WithMuLocked(func(*kernel.Task) {
   118  		mm = t.MemoryManager()
   119  	})
   120  	if mm == nil || !mm.IncUsers() {
   121  		return nil
   122  	}
   123  	return mm
   124  }
   125  
   126  func getExecutablePath(ctx context.Context, pid kernel.ThreadID, mm *mm.MemoryManager) string {
   127  	exec := mm.Executable()
   128  	if exec == nil {
   129  		log.Warningf("No executable found for PID %s", pid)
   130  		return ""
   131  	}
   132  	defer exec.DecRef(ctx)
   133  
   134  	return exec.MappedName(ctx)
   135  }
   136  
   137  func getMetadataArray(ctx context.Context, pid kernel.ThreadID, mm *mm.MemoryManager, metaType proc.MetadataType) []string {
   138  	buf := bytes.Buffer{}
   139  	if err := proc.GetMetadata(ctx, mm, &buf, metaType); err != nil {
   140  		log.Warningf("failed to get %v metadata for PID %s: %v", metaType, pid, err)
   141  		return nil
   142  	}
   143  	// As per proc(5), /proc/[pid]/cmdline may have "a further null byte after
   144  	// the last string". Similarly, for /proc/[pid]/environ "there may be a null
   145  	// byte at the end". So trim off the last null byte if it exists.
   146  	return strings.Split(strings.TrimSuffix(buf.String(), "\000"), "\000")
   147  }
   148  
   149  func getCWD(ctx context.Context, t *kernel.Task, pid kernel.ThreadID) string {
   150  	cwdDentry := t.FSContext().WorkingDirectory()
   151  	if !cwdDentry.Ok() {
   152  		log.Warningf("No CWD dentry found for PID %s", pid)
   153  		return ""
   154  	}
   155  
   156  	root := vfs.RootFromContext(ctx)
   157  	if !root.Ok() {
   158  		log.Warningf("no root could be found from context for PID %s", pid)
   159  		return ""
   160  	}
   161  	defer root.DecRef(ctx)
   162  
   163  	vfsObj := cwdDentry.Mount().Filesystem().VirtualFilesystem()
   164  	name, err := vfsObj.PathnameWithDeleted(ctx, root, cwdDentry)
   165  	if err != nil {
   166  		log.Warningf("PathnameWithDeleted failed to find CWD: %v", err)
   167  	}
   168  	return name
   169  }
   170  
   171  func getFDs(ctx context.Context, t *kernel.Task, pid kernel.ThreadID) []FDInfo {
   172  	type fdInfo struct {
   173  		fd *vfs.FileDescription
   174  		no int32
   175  	}
   176  	var fds []fdInfo
   177  	defer func() {
   178  		for _, fd := range fds {
   179  			fd.fd.DecRef(ctx)
   180  		}
   181  	}()
   182  
   183  	t.WithMuLocked(func(t *kernel.Task) {
   184  		if fdTable := t.FDTable(); fdTable != nil {
   185  			fdNos := fdTable.GetFDs(ctx)
   186  			fds = make([]fdInfo, 0, len(fdNos))
   187  			for _, fd := range fdNos {
   188  				file, _ := fdTable.Get(fd)
   189  				if file != nil {
   190  					fds = append(fds, fdInfo{fd: file, no: fd})
   191  				}
   192  			}
   193  		}
   194  	})
   195  
   196  	root := vfs.RootFromContext(ctx)
   197  	defer root.DecRef(ctx)
   198  
   199  	res := make([]FDInfo, 0, len(fds))
   200  	for _, fd := range fds {
   201  		path, err := t.Kernel().VFS().PathnameWithDeleted(ctx, root, fd.fd.VirtualDentry())
   202  		if err != nil {
   203  			log.Warningf("PathnameWithDeleted failed to find path for fd %d in PID %s: %v", fd.no, pid, err)
   204  			path = ""
   205  		}
   206  		mode := uint16(0)
   207  		if statx, err := fd.fd.Stat(ctx, vfs.StatOptions{Mask: linux.STATX_MODE}); err != nil {
   208  			log.Warningf("Stat(STATX_MODE) failed for fd %d in PID %s: %v", fd.no, pid, err)
   209  		} else {
   210  			mode = statx.Mode
   211  		}
   212  		res = append(res, FDInfo{Number: fd.no, Path: path, Mode: mode})
   213  	}
   214  	return res
   215  }
   216  
   217  func getRoot(t *kernel.Task, pid kernel.ThreadID) string {
   218  	realRoot := t.MountNamespace().Root(t)
   219  	defer realRoot.DecRef(t)
   220  	root := t.FSContext().RootDirectory()
   221  	defer root.DecRef(t)
   222  	path, err := t.Kernel().VFS().PathnameWithDeleted(t, realRoot, root)
   223  	if err != nil {
   224  		log.Warningf("PathnameWithDeleted failed to find root path for PID %s: %v", pid, err)
   225  		return ""
   226  	}
   227  	return path
   228  }
   229  
   230  func getFDLimit(ctx context.Context, pid kernel.ThreadID) (limits.Limit, error) {
   231  	if limitSet := limits.FromContext(ctx); limitSet != nil {
   232  		return limitSet.Get(limits.NumberOfFiles), nil
   233  	}
   234  	return limits.Limit{}, fmt.Errorf("could not find limit set for pid %s", pid)
   235  }
   236  
   237  func getStatus(t *kernel.Task, mm *mm.MemoryManager, pid kernel.ThreadID, pidns *kernel.PIDNamespace) Status {
   238  	creds := t.Credentials()
   239  	uns := creds.UserNamespace
   240  	ppid := kernel.ThreadID(0)
   241  	if parent := t.Parent(); parent != nil {
   242  		ppid = pidns.IDOfThreadGroup(parent.ThreadGroup())
   243  	}
   244  	return Status{
   245  		Comm: t.Name(),
   246  		PID:  int32(pid),
   247  		PPID: int32(ppid),
   248  		UID: UIDGID{
   249  			Real:      uint32(creds.RealKUID.In(uns).OrOverflow()),
   250  			Effective: uint32(creds.EffectiveKUID.In(uns).OrOverflow()),
   251  			Saved:     uint32(creds.SavedKUID.In(uns).OrOverflow()),
   252  		},
   253  		GID: UIDGID{
   254  			Real:      uint32(creds.RealKGID.In(uns).OrOverflow()),
   255  			Effective: uint32(creds.EffectiveKGID.In(uns).OrOverflow()),
   256  			Saved:     uint32(creds.SavedKGID.In(uns).OrOverflow()),
   257  		},
   258  		VMSize: mm.VirtualMemorySize() >> 10,
   259  		VMRSS:  mm.ResidentSetSize() >> 10,
   260  	}
   261  }
   262  
   263  func getStat(t *kernel.Task, pid kernel.ThreadID, pidns *kernel.PIDNamespace) Stat {
   264  	return Stat{
   265  		PGID: int32(pidns.IDOfProcessGroup(t.ThreadGroup().ProcessGroup())),
   266  		SID:  int32(pidns.IDOfSession(t.ThreadGroup().Session())),
   267  	}
   268  }
   269  
   270  func getMappings(ctx context.Context, mm *mm.MemoryManager) []Mapping {
   271  	var maps []Mapping
   272  	mm.ReadMapsDataInto(ctx, func(start, end hostarch.Addr, permissions hostarch.AccessType, private string, offset uint64, devMajor, devMinor uint32, inode uint64, path string) {
   273  		maps = append(maps, Mapping{
   274  			Address: hostarch.AddrRange{
   275  				Start: start,
   276  				End:   end,
   277  			},
   278  			Permissions: permissions,
   279  			Private:     private,
   280  			Offset:      offset,
   281  			DevMajor:    devMajor,
   282  			DevMinor:    devMinor,
   283  			Inode:       inode,
   284  			Pathname:    path,
   285  		})
   286  	})
   287  
   288  	return maps
   289  }
   290  
   291  // Dump returns a procfs dump for process pid. t must be a task in process pid.
   292  func Dump(t *kernel.Task, pid kernel.ThreadID, pidns *kernel.PIDNamespace) (ProcessProcfsDump, error) {
   293  	ctx := t.AsyncContext()
   294  
   295  	mm := getMM(t)
   296  	if mm == nil {
   297  		return ProcessProcfsDump{}, fmt.Errorf("no MM found for PID %s", pid)
   298  	}
   299  	defer mm.DecUsers(ctx)
   300  
   301  	fdLimit, err := getFDLimit(ctx, pid)
   302  	if err != nil {
   303  		return ProcessProcfsDump{}, err
   304  	}
   305  
   306  	return ProcessProcfsDump{
   307  		Exe:       getExecutablePath(ctx, pid, mm),
   308  		Args:      getMetadataArray(ctx, pid, mm, proc.Cmdline),
   309  		Env:       getMetadataArray(ctx, pid, mm, proc.Environ),
   310  		CWD:       getCWD(ctx, t, pid),
   311  		FDs:       getFDs(ctx, t, pid),
   312  		StartTime: t.StartTime().Nanoseconds(),
   313  		Root:      getRoot(t, pid),
   314  		Limits: map[string]limits.Limit{
   315  			"RLIMIT_NOFILE": fdLimit,
   316  		},
   317  		// We don't need to worry about fake cgroup controllers as that is not
   318  		// supported in runsc.
   319  		Cgroup: t.GetCgroupEntries(),
   320  		Status: getStatus(t, mm, pid, pidns),
   321  		Stat:   getStat(t, pid, pidns),
   322  		Maps:   getMappings(ctx, mm),
   323  	}, nil
   324  }