summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/proc/proc.go
blob: 459eb7e62c9e863f93d0eb18ff7dacd2ad22df4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package proc implements a partial in-memory file system for profs.
package proc

import (
	"fmt"
	"sort"
	"strconv"

	"gvisor.googlesource.com/gvisor/pkg/sentry/context"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs/proc/device"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs/proc/seqfile"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs/ramfs"
	"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
)

// proc is a root proc node.
type proc struct {
	ramfs.Dir

	// k is the Kernel containing this proc node.
	k *kernel.Kernel

	// pidns is the PID namespace of the task that mounted the proc filesystem
	// that this node represents.
	pidns *kernel.PIDNamespace
}

// New returns the root node of a partial simple procfs.
func New(ctx context.Context, msrc *fs.MountSource) (*fs.Inode, error) {
	k := kernel.KernelFromContext(ctx)
	if k == nil {
		return nil, fmt.Errorf("procfs requires a kernel")
	}
	pidns := kernel.PIDNamespaceFromContext(ctx)
	if pidns == nil {
		return nil, fmt.Errorf("procfs requires a PID namespace")
	}

	p := &proc{k: k, pidns: pidns}
	p.InitDir(ctx, map[string]*fs.Inode{
		// Note that these are just the static members. There are
		// dynamic members populated in Readdir and Lookup below.
		"filesystems": seqfile.NewSeqFileInode(ctx, &filesystemsData{}, msrc),
		"loadavg":     seqfile.NewSeqFileInode(ctx, &loadavgData{}, msrc),
		"meminfo":     seqfile.NewSeqFileInode(ctx, &meminfoData{k}, msrc),
		"mounts":      newMountsSymlink(ctx, msrc),
		"stat":        seqfile.NewSeqFileInode(ctx, &statData{k}, msrc),
		"version":     seqfile.NewSeqFileInode(ctx, &versionData{k}, msrc),
	}, fs.RootOwner, fs.FilePermsFromMode(0555))

	p.AddChild(ctx, "cpuinfo", p.newCPUInfo(ctx, msrc))
	p.AddChild(ctx, "uptime", p.newUptime(ctx, msrc))

	return newFile(p, msrc, fs.SpecialDirectory, nil), nil
}

// self is a magical link.
type self struct {
	ramfs.Symlink

	pidns *kernel.PIDNamespace
}

// newSelf returns a new "self" node.
func (p *proc) newSelf(ctx context.Context, msrc *fs.MountSource) *fs.Inode {
	s := &self{pidns: p.pidns}
	s.InitSymlink(ctx, fs.RootOwner, "")
	return newFile(s, msrc, fs.Symlink, nil)
}

// Readlink implements fs.InodeOperations.Readlink.
func (s *self) Readlink(ctx context.Context, inode *fs.Inode) (string, error) {
	if t := kernel.TaskFromContext(ctx); t != nil {
		tgid := s.pidns.IDOfThreadGroup(t.ThreadGroup())
		if tgid == 0 {
			return "", ramfs.ErrNotFound
		}
		return strconv.FormatUint(uint64(tgid), 10), nil
	}

	// Who is reading this link?
	return "", ramfs.ErrInvalidOp
}

// Lookup loads an Inode at name into a Dirent.
func (p *proc) Lookup(ctx context.Context, dir *fs.Inode, name string) (*fs.Dirent, error) {
	// Is it one of the static ones?
	dirent, walkErr := p.Dir.Lookup(ctx, dir, name)
	if walkErr == nil {
		return dirent, nil
	}

	// Is it a dynamic element?
	nfs := map[string]func() *fs.Inode{
		"net":  func() *fs.Inode { return p.newNetDir(ctx, dir.MountSource) },
		"self": func() *fs.Inode { return p.newSelf(ctx, dir.MountSource) },
		"sys":  func() *fs.Inode { return p.newSysDir(ctx, dir.MountSource) },
	}
	if nf, ok := nfs[name]; ok {
		return fs.NewDirent(nf(), name), nil
	}

	// Try to lookup a corresponding task.
	tid, err := strconv.ParseUint(name, 10, 64)
	if err != nil {
		// Ignore the parse error and return the original.
		return nil, walkErr
	}

	// Grab the other task.
	otherTask := p.pidns.TaskWithID(kernel.ThreadID(tid))
	if otherTask == nil {
		// Per above.
		return nil, walkErr
	}

	// Wrap it in a taskDir.
	td := newTaskDir(otherTask, dir.MountSource, p.pidns, true)
	return fs.NewDirent(td, name), nil
}

// Readdir synthesizes proc contents.
func (p *proc) DeprecatedReaddir(ctx context.Context, dirCtx *fs.DirCtx, offset int) (int, error) {
	// Serialize normal contents.
	_, err := p.Dir.DeprecatedReaddir(ctx, dirCtx, offset)
	if err != nil {
		return offset, err
	}

	m := make(map[string]fs.DentAttr)
	var names []string

	// Add special files.
	m["sys"] = fs.GenericDentAttr(fs.SpecialFile, device.ProcDevice)
	names = append(names, "sys")

	// Collect tasks.
	// Per linux we only include it in directory listings if it's the leader.
	// But for whatever crazy reason, you can still walk to the given node.
	for _, tg := range p.pidns.ThreadGroups() {
		if leader := tg.Leader(); leader != nil {
			name := strconv.FormatUint(uint64(tg.ID()), 10)
			m[name] = fs.GenericDentAttr(fs.SpecialDirectory, device.ProcDevice)
			names = append(names, name)
		}
	}

	if offset >= len(m) {
		return offset, nil
	}
	sort.Strings(names)
	names = names[offset:]
	for _, name := range names {
		if err := dirCtx.DirEmit(name, m[name]); err != nil {
			return offset, err
		}
		offset++
	}
	return offset, err
}

// newMountsSymlink returns a symlink to "self/mounts"
func newMountsSymlink(ctx context.Context, msrc *fs.MountSource) *fs.Inode {
	s := &ramfs.Symlink{}
	s.InitSymlink(ctx, fs.RootOwner, "self/mounts")
	return newFile(s, msrc, fs.Symlink, nil)
}