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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
// Copyright 2018 Google LLC
//
// 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
import (
"fmt"
"io"
"sync"
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/fsutil"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/proc/device"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/ramfs"
"gvisor.googlesource.com/gvisor/pkg/sentry/inet"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/waiter"
)
type tcpMemDir int
const (
tcpRMem tcpMemDir = iota
tcpWMem
)
// tcpMemInode is used to read/write the size of netstack tcp buffers.
//
// TODO: If we have multiple proc mounts, concurrent writes can
// leave netstack and the proc files in an inconsistent state. Since we set the
// buffer size from these proc files on restore, we may also race and end up in
// an inconsistent state on restore.
//
// +stateify savable
type tcpMemInode struct {
fsutil.SimpleFileInode
dir tcpMemDir
s inet.Stack `state:"wait"`
// size stores the tcp buffer size during save, and sets the buffer
// size in netstack in restore. We must save/restore this here, since
// netstack itself is stateless.
size inet.TCPBufferSize
// mu protects against concurrent reads/writes to files based on this
// inode.
mu sync.Mutex `state:"nosave"`
}
var _ fs.InodeOperations = (*tcpMemInode)(nil)
func newTCPMemInode(ctx context.Context, msrc *fs.MountSource, s inet.Stack, dir tcpMemDir) *fs.Inode {
tm := &tcpMemInode{
SimpleFileInode: *fsutil.NewSimpleFileInode(ctx, fs.RootOwner, fs.FilePermsFromMode(0444), linux.PROC_SUPER_MAGIC),
s: s,
dir: dir,
}
sattr := fs.StableAttr{
DeviceID: device.ProcDevice.DeviceID(),
InodeID: device.ProcDevice.NextIno(),
BlockSize: usermem.PageSize,
Type: fs.SpecialFile,
}
return fs.NewInode(tm, msrc, sattr)
}
// GetFile implements fs.InodeOperations.GetFile.
func (m *tcpMemInode) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
flags.Pread = true
return fs.NewFile(ctx, dirent, flags, &tcpMemFile{tcpMemInode: m}), nil
}
// +stateify savable
type tcpMemFile struct {
waiter.AlwaysReady `state:"nosave"`
fsutil.FileGenericSeek `state:"nosave"`
fsutil.FileNoIoctl `state:"nosave"`
fsutil.FileNoMMap `state:"nosave"`
fsutil.FileNoopRelease `state:"nosave"`
fsutil.FileNoopFlush `state:"nosave"`
fsutil.FileNoopFsync `state:"nosave"`
fsutil.FileNotDirReaddir `state:"nosave"`
fsutil.FileUseInodeUnstableAttr `state:"nosave"`
tcpMemInode *tcpMemInode
}
var _ fs.FileOperations = (*tcpMemFile)(nil)
// Read implements fs.FileOperations.Read.
func (f *tcpMemFile) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, offset int64) (int64, error) {
if offset != 0 {
return 0, io.EOF
}
f.tcpMemInode.mu.Lock()
defer f.tcpMemInode.mu.Unlock()
size, err := readSize(f.tcpMemInode.dir, f.tcpMemInode.s)
if err != nil {
return 0, err
}
s := fmt.Sprintf("%d\t%d\t%d\n", size.Min, size.Default, size.Max)
n, err := dst.CopyOut(ctx, []byte(s))
return int64(n), err
}
// Write implements fs.FileOperations.Write.
func (f *tcpMemFile) Write(ctx context.Context, _ *fs.File, src usermem.IOSequence, offset int64) (int64, error) {
if src.NumBytes() == 0 {
return 0, nil
}
f.tcpMemInode.mu.Lock()
defer f.tcpMemInode.mu.Unlock()
src = src.TakeFirst(usermem.PageSize - 1)
size, err := readSize(f.tcpMemInode.dir, f.tcpMemInode.s)
if err != nil {
return 0, err
}
buf := []int32{int32(size.Min), int32(size.Default), int32(size.Max)}
n, cperr := usermem.CopyInt32StringsInVec(ctx, src.IO, src.Addrs, buf, src.Opts)
newSize := inet.TCPBufferSize{
Min: int(buf[0]),
Default: int(buf[1]),
Max: int(buf[2]),
}
if err := writeSize(f.tcpMemInode.dir, f.tcpMemInode.s, newSize); err != nil {
return n, err
}
return n, cperr
}
func readSize(dirType tcpMemDir, s inet.Stack) (inet.TCPBufferSize, error) {
switch dirType {
case tcpRMem:
return s.TCPReceiveBufferSize()
case tcpWMem:
return s.TCPSendBufferSize()
default:
panic(fmt.Sprintf("unknown tcpMemFile type: %v", dirType))
}
}
func writeSize(dirType tcpMemDir, s inet.Stack, size inet.TCPBufferSize) error {
switch dirType {
case tcpRMem:
return s.SetTCPReceiveBufferSize(size)
case tcpWMem:
return s.SetTCPSendBufferSize(size)
default:
panic(fmt.Sprintf("unknown tcpMemFile type: %v", dirType))
}
}
// +stateify savable
type tcpSack struct {
stack inet.Stack `state:"wait"`
enabled *bool
fsutil.SimpleFileInode
}
func newTCPSackInode(ctx context.Context, msrc *fs.MountSource, s inet.Stack) *fs.Inode {
ts := &tcpSack{
SimpleFileInode: *fsutil.NewSimpleFileInode(ctx, fs.RootOwner, fs.FilePermsFromMode(0444), linux.PROC_SUPER_MAGIC),
stack: s,
}
sattr := fs.StableAttr{
DeviceID: device.ProcDevice.DeviceID(),
InodeID: device.ProcDevice.NextIno(),
BlockSize: usermem.PageSize,
Type: fs.SpecialFile,
}
return fs.NewInode(ts, msrc, sattr)
}
// GetFile implements fs.InodeOperations.GetFile.
func (s *tcpSack) GetFile(ctx context.Context, dirent *fs.Dirent, flags fs.FileFlags) (*fs.File, error) {
flags.Pread = true
flags.Pwrite = true
return fs.NewFile(ctx, dirent, flags, &tcpSackFile{
tcpSack: s,
stack: s.stack,
}), nil
}
// +stateify savable
type tcpSackFile struct {
waiter.AlwaysReady `state:"nosave"`
fsutil.FileGenericSeek `state:"nosave"`
fsutil.FileNoIoctl `state:"nosave"`
fsutil.FileNoMMap `state:"nosave"`
fsutil.FileNoopRelease `state:"nosave"`
fsutil.FileNoopFlush `state:"nosave"`
fsutil.FileNoopFsync `state:"nosave"`
fsutil.FileNotDirReaddir `state:"nosave"`
fsutil.FileUseInodeUnstableAttr `state:"nosave"`
tcpSack *tcpSack
stack inet.Stack `state:"wait"`
}
// Read implements fs.FileOperations.Read.
func (f *tcpSackFile) Read(ctx context.Context, _ *fs.File, dst usermem.IOSequence, offset int64) (int64, error) {
if offset != 0 {
return 0, io.EOF
}
if f.tcpSack.enabled == nil {
sack, err := f.stack.TCPSACKEnabled()
if err != nil {
return 0, err
}
f.tcpSack.enabled = &sack
}
val := "0\n"
if *f.tcpSack.enabled {
// Technically, this is not quite compatible with Linux. Linux
// stores these as an integer, so if you write "2" into
// tcp_sack, you should get 2 back. Tough luck.
val = "1\n"
}
n, err := dst.CopyOut(ctx, []byte(val))
return int64(n), err
}
// Write implements fs.FileOperations.Write.
func (f *tcpSackFile) Write(ctx context.Context, _ *fs.File, src usermem.IOSequence, offset int64) (int64, error) {
if src.NumBytes() == 0 {
return 0, nil
}
src = src.TakeFirst(usermem.PageSize - 1)
var v int32
n, err := usermem.CopyInt32StringInVec(ctx, src.IO, src.Addrs, &v, src.Opts)
if err != nil {
return n, err
}
if f.tcpSack.enabled == nil {
f.tcpSack.enabled = new(bool)
}
*f.tcpSack.enabled = v != 0
return n, f.tcpSack.stack.SetTCPSACKEnabled(*f.tcpSack.enabled)
}
func (p *proc) newSysNetCore(ctx context.Context, msrc *fs.MountSource, s inet.Stack) *fs.Inode {
// The following files are simple stubs until they are implemented in
// netstack, most of these files are configuration related. We use the
// value closest to the actual netstack behavior or any empty file,
// all of these files will have mode 0444 (read-only for all users).
contents := map[string]*fs.Inode{
"default_qdisc": newStaticProcInode(ctx, msrc, []byte("pfifo_fast")),
"message_burst": newStaticProcInode(ctx, msrc, []byte("10")),
"message_cost": newStaticProcInode(ctx, msrc, []byte("5")),
"optmem_max": newStaticProcInode(ctx, msrc, []byte("0")),
"rmem_default": newStaticProcInode(ctx, msrc, []byte("212992")),
"rmem_max": newStaticProcInode(ctx, msrc, []byte("212992")),
"somaxconn": newStaticProcInode(ctx, msrc, []byte("128")),
"wmem_default": newStaticProcInode(ctx, msrc, []byte("212992")),
"wmem_max": newStaticProcInode(ctx, msrc, []byte("212992")),
}
d := ramfs.NewDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(0555))
return newProcInode(d, msrc, fs.SpecialDirectory, nil)
}
func (p *proc) newSysNetIPv4Dir(ctx context.Context, msrc *fs.MountSource, s inet.Stack) *fs.Inode {
contents := map[string]*fs.Inode{
// Add tcp_sack.
"tcp_sack": newTCPSackInode(ctx, msrc, s),
// The following files are simple stubs until they are
// implemented in netstack, most of these files are
// configuration related. We use the value closest to the
// actual netstack behavior or any empty file, all of these
// files will have mode 0444 (read-only for all users).
"ip_local_port_range": newStaticProcInode(ctx, msrc, []byte("16000 65535")),
"ip_local_reserved_ports": newStaticProcInode(ctx, msrc, []byte("")),
"ipfrag_time": newStaticProcInode(ctx, msrc, []byte("30")),
"ip_nonlocal_bind": newStaticProcInode(ctx, msrc, []byte("0")),
"ip_no_pmtu_disc": newStaticProcInode(ctx, msrc, []byte("1")),
// tcp_allowed_congestion_control tell the user what they are
// able to do as an unprivledged process so we leave it empty.
"tcp_allowed_congestion_control": newStaticProcInode(ctx, msrc, []byte("")),
"tcp_available_congestion_control": newStaticProcInode(ctx, msrc, []byte("reno")),
"tcp_congestion_control": newStaticProcInode(ctx, msrc, []byte("reno")),
// Many of the following stub files are features netstack
// doesn't support. The unsupported features return "0" to
// indicate they are disabled.
"tcp_base_mss": newStaticProcInode(ctx, msrc, []byte("1280")),
"tcp_dsack": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_early_retrans": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_fack": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_fastopen": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_fastopen_key": newStaticProcInode(ctx, msrc, []byte("")),
"tcp_invalid_ratelimit": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_keepalive_intvl": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_keepalive_probes": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_keepalive_time": newStaticProcInode(ctx, msrc, []byte("7200")),
"tcp_mtu_probing": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_no_metrics_save": newStaticProcInode(ctx, msrc, []byte("1")),
"tcp_probe_interval": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_probe_threshold": newStaticProcInode(ctx, msrc, []byte("0")),
"tcp_retries1": newStaticProcInode(ctx, msrc, []byte("3")),
"tcp_retries2": newStaticProcInode(ctx, msrc, []byte("15")),
"tcp_rfc1337": newStaticProcInode(ctx, msrc, []byte("1")),
"tcp_slow_start_after_idle": newStaticProcInode(ctx, msrc, []byte("1")),
"tcp_synack_retries": newStaticProcInode(ctx, msrc, []byte("5")),
"tcp_syn_retries": newStaticProcInode(ctx, msrc, []byte("3")),
"tcp_timestamps": newStaticProcInode(ctx, msrc, []byte("1")),
}
// Add tcp_rmem.
if _, err := s.TCPReceiveBufferSize(); err == nil {
contents["tcp_rmem"] = newTCPMemInode(ctx, msrc, s, tcpRMem)
}
// Add tcp_wmem.
if _, err := s.TCPSendBufferSize(); err == nil {
contents["tcp_wmem"] = newTCPMemInode(ctx, msrc, s, tcpWMem)
}
d := ramfs.NewDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(0555))
return newProcInode(d, msrc, fs.SpecialDirectory, nil)
}
func (p *proc) newSysNetDir(ctx context.Context, msrc *fs.MountSource) *fs.Inode {
var contents map[string]*fs.Inode
if s := p.k.NetworkStack(); s != nil {
contents = map[string]*fs.Inode{
"ipv4": p.newSysNetIPv4Dir(ctx, msrc, s),
"core": p.newSysNetCore(ctx, msrc, s),
}
}
d := ramfs.NewDir(ctx, contents, fs.RootOwner, fs.FilePermsFromMode(0555))
return newProcInode(d, msrc, fs.SpecialDirectory, nil)
}
|