summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/kernel
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2020-09-29 18:54:47 +0000
committergVisor bot <gvisor-bot@google.com>2020-09-29 18:54:47 +0000
commitfbaf56af454c281979112ea542784155f77ede96 (patch)
treebad5780ddb0582a60e4b5e234e0e793863e51761 /pkg/sentry/kernel
parent44f50b440fa6241105a90f17501417ed0b34f172 (diff)
parent7d64bc1fdf538869de5964475110a69580b481ad (diff)
Merge release-20200921.0-66-g7d64bc1fd (automated)
Diffstat (limited to 'pkg/sentry/kernel')
-rw-r--r--pkg/sentry/kernel/kernel.go83
-rw-r--r--pkg/sentry/kernel/kernel_state_autogen.go103
-rw-r--r--pkg/sentry/kernel/socket_list.go32
3 files changed, 140 insertions, 78 deletions
diff --git a/pkg/sentry/kernel/kernel.go b/pkg/sentry/kernel/kernel.go
index 08bb5bd12..d6c21adb7 100644
--- a/pkg/sentry/kernel/kernel.go
+++ b/pkg/sentry/kernel/kernel.go
@@ -220,13 +220,18 @@ type Kernel struct {
// danglingEndpoints is used to save / restore tcpip.DanglingEndpoints.
danglingEndpoints struct{} `state:".([]tcpip.Endpoint)"`
- // sockets is the list of all network sockets the system. Protected by
- // extMu.
+ // sockets is the list of all network sockets in the system.
+ // Protected by extMu.
+ // TODO(gvisor.dev/issue/1624): Only used by VFS1.
sockets socketList
- // nextSocketEntry is the next entry number to use in sockets. Protected
+ // socketsVFS2 records all network sockets in the system. Protected by
+ // extMu.
+ socketsVFS2 map[*vfs.FileDescription]*SocketRecord
+
+ // nextSocketRecord is the next entry number to use in sockets. Protected
// by extMu.
- nextSocketEntry uint64
+ nextSocketRecord uint64
// deviceRegistry is used to save/restore device.SimpleDevices.
deviceRegistry struct{} `state:".(*device.Registry)"`
@@ -414,6 +419,8 @@ func (k *Kernel) Init(args InitKernelArgs) error {
return fmt.Errorf("failed to create sockfs mount: %v", err)
}
k.socketMount = socketMount
+
+ k.socketsVFS2 = make(map[*vfs.FileDescription]*SocketRecord)
}
return nil
@@ -1512,20 +1519,27 @@ func (k *Kernel) SupervisorContext() context.Context {
}
}
-// SocketEntry represents a socket recorded in Kernel.sockets. It implements
+// SocketRecord represents a socket recorded in Kernel.socketsVFS2.
+//
+// +stateify savable
+type SocketRecord struct {
+ k *Kernel
+ Sock *refs.WeakRef // TODO(gvisor.dev/issue/1624): Only used by VFS1.
+ SockVFS2 *vfs.FileDescription // Only used by VFS2.
+ ID uint64 // Socket table entry number.
+}
+
+// SocketRecordVFS1 represents a socket recorded in Kernel.sockets. It implements
// refs.WeakRefUser for sockets stored in the socket table.
//
// +stateify savable
-type SocketEntry struct {
+type SocketRecordVFS1 struct {
socketEntry
- k *Kernel
- Sock *refs.WeakRef
- SockVFS2 *vfs.FileDescription
- ID uint64 // Socket table entry number.
+ SocketRecord
}
// WeakRefGone implements refs.WeakRefUser.WeakRefGone.
-func (s *SocketEntry) WeakRefGone(context.Context) {
+func (s *SocketRecordVFS1) WeakRefGone(context.Context) {
s.k.extMu.Lock()
s.k.sockets.Remove(s)
s.k.extMu.Unlock()
@@ -1536,9 +1550,14 @@ func (s *SocketEntry) WeakRefGone(context.Context) {
// Precondition: Caller must hold a reference to sock.
func (k *Kernel) RecordSocket(sock *fs.File) {
k.extMu.Lock()
- id := k.nextSocketEntry
- k.nextSocketEntry++
- s := &SocketEntry{k: k, ID: id}
+ id := k.nextSocketRecord
+ k.nextSocketRecord++
+ s := &SocketRecordVFS1{
+ SocketRecord: SocketRecord{
+ k: k,
+ ID: id,
+ },
+ }
s.Sock = refs.NewWeakRef(sock, s)
k.sockets.PushBack(s)
k.extMu.Unlock()
@@ -1550,29 +1569,45 @@ func (k *Kernel) RecordSocket(sock *fs.File) {
// Precondition: Caller must hold a reference to sock.
//
// Note that the socket table will not hold a reference on the
-// vfs.FileDescription, because we do not support weak refs on VFS2 files.
+// vfs.FileDescription.
func (k *Kernel) RecordSocketVFS2(sock *vfs.FileDescription) {
k.extMu.Lock()
- id := k.nextSocketEntry
- k.nextSocketEntry++
- s := &SocketEntry{
+ if _, ok := k.socketsVFS2[sock]; ok {
+ panic(fmt.Sprintf("Socket %p added twice", sock))
+ }
+ id := k.nextSocketRecord
+ k.nextSocketRecord++
+ s := &SocketRecord{
k: k,
ID: id,
SockVFS2: sock,
}
- k.sockets.PushBack(s)
+ k.socketsVFS2[sock] = s
+ k.extMu.Unlock()
+}
+
+// DeleteSocketVFS2 removes a VFS2 socket from the system-wide socket table.
+func (k *Kernel) DeleteSocketVFS2(sock *vfs.FileDescription) {
+ k.extMu.Lock()
+ delete(k.socketsVFS2, sock)
k.extMu.Unlock()
}
// ListSockets returns a snapshot of all sockets.
//
-// Callers of ListSockets() in VFS2 should use SocketEntry.SockVFS2.TryIncRef()
+// Callers of ListSockets() in VFS2 should use SocketRecord.SockVFS2.TryIncRef()
// to get a reference on a socket in the table.
-func (k *Kernel) ListSockets() []*SocketEntry {
+func (k *Kernel) ListSockets() []*SocketRecord {
k.extMu.Lock()
- var socks []*SocketEntry
- for s := k.sockets.Front(); s != nil; s = s.Next() {
- socks = append(socks, s)
+ var socks []*SocketRecord
+ if VFS2Enabled {
+ for _, s := range k.socketsVFS2 {
+ socks = append(socks, s)
+ }
+ } else {
+ for s := k.sockets.Front(); s != nil; s = s.Next() {
+ socks = append(socks, &s.SocketRecord)
+ }
}
k.extMu.Unlock()
return socks
diff --git a/pkg/sentry/kernel/kernel_state_autogen.go b/pkg/sentry/kernel/kernel_state_autogen.go
index d0ff135d7..f20800960 100644
--- a/pkg/sentry/kernel/kernel_state_autogen.go
+++ b/pkg/sentry/kernel/kernel_state_autogen.go
@@ -297,7 +297,8 @@ func (x *Kernel) StateFields() []string {
"netlinkPorts",
"danglingEndpoints",
"sockets",
- "nextSocketEntry",
+ "socketsVFS2",
+ "nextSocketRecord",
"deviceRegistry",
"DirentCacheLimiter",
"SpecialOpts",
@@ -317,7 +318,7 @@ func (x *Kernel) StateSave(m state.Sink) {
var danglingEndpoints []tcpip.Endpoint = x.saveDanglingEndpoints()
m.SaveValue(24, danglingEndpoints)
var deviceRegistry *device.Registry = x.saveDeviceRegistry()
- m.SaveValue(27, deviceRegistry)
+ m.SaveValue(28, deviceRegistry)
m.Save(0, &x.featureSet)
m.Save(1, &x.timekeeper)
m.Save(2, &x.tasks)
@@ -343,15 +344,16 @@ func (x *Kernel) StateSave(m state.Sink) {
m.Save(22, &x.nextInotifyCookie)
m.Save(23, &x.netlinkPorts)
m.Save(25, &x.sockets)
- m.Save(26, &x.nextSocketEntry)
- m.Save(28, &x.DirentCacheLimiter)
- m.Save(29, &x.SpecialOpts)
- m.Save(30, &x.vfs)
- m.Save(31, &x.hostMount)
- m.Save(32, &x.pipeMount)
- m.Save(33, &x.shmMount)
- m.Save(34, &x.socketMount)
- m.Save(35, &x.SleepForAddressSpaceActivation)
+ m.Save(26, &x.socketsVFS2)
+ m.Save(27, &x.nextSocketRecord)
+ m.Save(29, &x.DirentCacheLimiter)
+ m.Save(30, &x.SpecialOpts)
+ m.Save(31, &x.vfs)
+ m.Save(32, &x.hostMount)
+ m.Save(33, &x.pipeMount)
+ m.Save(34, &x.shmMount)
+ m.Save(35, &x.socketMount)
+ m.Save(36, &x.SleepForAddressSpaceActivation)
}
func (x *Kernel) afterLoad() {}
@@ -382,26 +384,26 @@ func (x *Kernel) StateLoad(m state.Source) {
m.Load(22, &x.nextInotifyCookie)
m.Load(23, &x.netlinkPorts)
m.Load(25, &x.sockets)
- m.Load(26, &x.nextSocketEntry)
- m.Load(28, &x.DirentCacheLimiter)
- m.Load(29, &x.SpecialOpts)
- m.Load(30, &x.vfs)
- m.Load(31, &x.hostMount)
- m.Load(32, &x.pipeMount)
- m.Load(33, &x.shmMount)
- m.Load(34, &x.socketMount)
- m.Load(35, &x.SleepForAddressSpaceActivation)
+ m.Load(26, &x.socketsVFS2)
+ m.Load(27, &x.nextSocketRecord)
+ m.Load(29, &x.DirentCacheLimiter)
+ m.Load(30, &x.SpecialOpts)
+ m.Load(31, &x.vfs)
+ m.Load(32, &x.hostMount)
+ m.Load(33, &x.pipeMount)
+ m.Load(34, &x.shmMount)
+ m.Load(35, &x.socketMount)
+ m.Load(36, &x.SleepForAddressSpaceActivation)
m.LoadValue(24, new([]tcpip.Endpoint), func(y interface{}) { x.loadDanglingEndpoints(y.([]tcpip.Endpoint)) })
- m.LoadValue(27, new(*device.Registry), func(y interface{}) { x.loadDeviceRegistry(y.(*device.Registry)) })
+ m.LoadValue(28, new(*device.Registry), func(y interface{}) { x.loadDeviceRegistry(y.(*device.Registry)) })
}
-func (x *SocketEntry) StateTypeName() string {
- return "pkg/sentry/kernel.SocketEntry"
+func (x *SocketRecord) StateTypeName() string {
+ return "pkg/sentry/kernel.SocketRecord"
}
-func (x *SocketEntry) StateFields() []string {
+func (x *SocketRecord) StateFields() []string {
return []string{
- "socketEntry",
"k",
"Sock",
"SockVFS2",
@@ -409,25 +411,49 @@ func (x *SocketEntry) StateFields() []string {
}
}
-func (x *SocketEntry) beforeSave() {}
+func (x *SocketRecord) beforeSave() {}
-func (x *SocketEntry) StateSave(m state.Sink) {
+func (x *SocketRecord) StateSave(m state.Sink) {
+ x.beforeSave()
+ m.Save(0, &x.k)
+ m.Save(1, &x.Sock)
+ m.Save(2, &x.SockVFS2)
+ m.Save(3, &x.ID)
+}
+
+func (x *SocketRecord) afterLoad() {}
+
+func (x *SocketRecord) StateLoad(m state.Source) {
+ m.Load(0, &x.k)
+ m.Load(1, &x.Sock)
+ m.Load(2, &x.SockVFS2)
+ m.Load(3, &x.ID)
+}
+
+func (x *SocketRecordVFS1) StateTypeName() string {
+ return "pkg/sentry/kernel.SocketRecordVFS1"
+}
+
+func (x *SocketRecordVFS1) StateFields() []string {
+ return []string{
+ "socketEntry",
+ "SocketRecord",
+ }
+}
+
+func (x *SocketRecordVFS1) beforeSave() {}
+
+func (x *SocketRecordVFS1) StateSave(m state.Sink) {
x.beforeSave()
m.Save(0, &x.socketEntry)
- m.Save(1, &x.k)
- m.Save(2, &x.Sock)
- m.Save(3, &x.SockVFS2)
- m.Save(4, &x.ID)
+ m.Save(1, &x.SocketRecord)
}
-func (x *SocketEntry) afterLoad() {}
+func (x *SocketRecordVFS1) afterLoad() {}
-func (x *SocketEntry) StateLoad(m state.Source) {
+func (x *SocketRecordVFS1) StateLoad(m state.Source) {
m.Load(0, &x.socketEntry)
- m.Load(1, &x.k)
- m.Load(2, &x.Sock)
- m.Load(3, &x.SockVFS2)
- m.Load(4, &x.ID)
+ m.Load(1, &x.SocketRecord)
}
func (x *pendingSignals) StateTypeName() string {
@@ -2264,7 +2290,8 @@ func init() {
state.Register((*FSContextRefs)(nil))
state.Register((*IPCNamespace)(nil))
state.Register((*Kernel)(nil))
- state.Register((*SocketEntry)(nil))
+ state.Register((*SocketRecord)(nil))
+ state.Register((*SocketRecordVFS1)(nil))
state.Register((*pendingSignals)(nil))
state.Register((*pendingSignalQueue)(nil))
state.Register((*pendingSignal)(nil))
diff --git a/pkg/sentry/kernel/socket_list.go b/pkg/sentry/kernel/socket_list.go
index d2d4307a1..246fba405 100644
--- a/pkg/sentry/kernel/socket_list.go
+++ b/pkg/sentry/kernel/socket_list.go
@@ -13,7 +13,7 @@ type socketElementMapper struct{}
// This default implementation should be inlined.
//
//go:nosplit
-func (socketElementMapper) linkerFor(elem *SocketEntry) *SocketEntry { return elem }
+func (socketElementMapper) linkerFor(elem *SocketRecordVFS1) *SocketRecordVFS1 { return elem }
// List is an intrusive list. Entries can be added to or removed from the list
// in O(1) time and with no additional memory allocations.
@@ -27,8 +27,8 @@ func (socketElementMapper) linkerFor(elem *SocketEntry) *SocketEntry { return el
//
// +stateify savable
type socketList struct {
- head *SocketEntry
- tail *SocketEntry
+ head *SocketRecordVFS1
+ tail *SocketRecordVFS1
}
// Reset resets list l to the empty state.
@@ -43,12 +43,12 @@ func (l *socketList) Empty() bool {
}
// Front returns the first element of list l or nil.
-func (l *socketList) Front() *SocketEntry {
+func (l *socketList) Front() *SocketRecordVFS1 {
return l.head
}
// Back returns the last element of list l or nil.
-func (l *socketList) Back() *SocketEntry {
+func (l *socketList) Back() *SocketRecordVFS1 {
return l.tail
}
@@ -63,7 +63,7 @@ func (l *socketList) Len() (count int) {
}
// PushFront inserts the element e at the front of list l.
-func (l *socketList) PushFront(e *SocketEntry) {
+func (l *socketList) PushFront(e *SocketRecordVFS1) {
linker := socketElementMapper{}.linkerFor(e)
linker.SetNext(l.head)
linker.SetPrev(nil)
@@ -77,7 +77,7 @@ func (l *socketList) PushFront(e *SocketEntry) {
}
// PushBack inserts the element e at the back of list l.
-func (l *socketList) PushBack(e *SocketEntry) {
+func (l *socketList) PushBack(e *SocketRecordVFS1) {
linker := socketElementMapper{}.linkerFor(e)
linker.SetNext(nil)
linker.SetPrev(l.tail)
@@ -106,7 +106,7 @@ func (l *socketList) PushBackList(m *socketList) {
}
// InsertAfter inserts e after b.
-func (l *socketList) InsertAfter(b, e *SocketEntry) {
+func (l *socketList) InsertAfter(b, e *SocketRecordVFS1) {
bLinker := socketElementMapper{}.linkerFor(b)
eLinker := socketElementMapper{}.linkerFor(e)
@@ -124,7 +124,7 @@ func (l *socketList) InsertAfter(b, e *SocketEntry) {
}
// InsertBefore inserts e before a.
-func (l *socketList) InsertBefore(a, e *SocketEntry) {
+func (l *socketList) InsertBefore(a, e *SocketRecordVFS1) {
aLinker := socketElementMapper{}.linkerFor(a)
eLinker := socketElementMapper{}.linkerFor(e)
@@ -141,7 +141,7 @@ func (l *socketList) InsertBefore(a, e *SocketEntry) {
}
// Remove removes e from l.
-func (l *socketList) Remove(e *SocketEntry) {
+func (l *socketList) Remove(e *SocketRecordVFS1) {
linker := socketElementMapper{}.linkerFor(e)
prev := linker.Prev()
next := linker.Next()
@@ -168,26 +168,26 @@ func (l *socketList) Remove(e *SocketEntry) {
//
// +stateify savable
type socketEntry struct {
- next *SocketEntry
- prev *SocketEntry
+ next *SocketRecordVFS1
+ prev *SocketRecordVFS1
}
// Next returns the entry that follows e in the list.
-func (e *socketEntry) Next() *SocketEntry {
+func (e *socketEntry) Next() *SocketRecordVFS1 {
return e.next
}
// Prev returns the entry that precedes e in the list.
-func (e *socketEntry) Prev() *SocketEntry {
+func (e *socketEntry) Prev() *SocketRecordVFS1 {
return e.prev
}
// SetNext assigns 'entry' as the entry that follows e in the list.
-func (e *socketEntry) SetNext(elem *SocketEntry) {
+func (e *socketEntry) SetNext(elem *SocketRecordVFS1) {
e.next = elem
}
// SetPrev assigns 'entry' as the entry that precedes e in the list.
-func (e *socketEntry) SetPrev(elem *SocketEntry) {
+func (e *socketEntry) SetPrev(elem *SocketRecordVFS1) {
e.prev = elem
}