diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-09-29 18:54:47 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-09-29 18:54:47 +0000 |
commit | fbaf56af454c281979112ea542784155f77ede96 (patch) | |
tree | bad5780ddb0582a60e4b5e234e0e793863e51761 /pkg/sentry/socket/unix | |
parent | 44f50b440fa6241105a90f17501417ed0b34f172 (diff) | |
parent | 7d64bc1fdf538869de5964475110a69580b481ad (diff) |
Merge release-20200921.0-66-g7d64bc1fd (automated)
Diffstat (limited to 'pkg/sentry/socket/unix')
-rw-r--r-- | pkg/sentry/socket/unix/socket_refs.go | 24 | ||||
-rw-r--r-- | pkg/sentry/socket/unix/socket_vfs2_refs.go | 118 | ||||
-rw-r--r-- | pkg/sentry/socket/unix/unix.go | 36 | ||||
-rw-r--r-- | pkg/sentry/socket/unix/unix_state_autogen.go | 77 | ||||
-rw-r--r-- | pkg/sentry/socket/unix/unix_vfs2.go | 20 |
5 files changed, 220 insertions, 55 deletions
diff --git a/pkg/sentry/socket/unix/socket_refs.go b/pkg/sentry/socket/unix/socket_refs.go index dababb85f..ea63dc659 100644 --- a/pkg/sentry/socket/unix/socket_refs.go +++ b/pkg/sentry/socket/unix/socket_refs.go @@ -11,7 +11,7 @@ import ( // ownerType is used to customize logging. Note that we use a pointer to T so // that we do not copy the entire object when passed as a format parameter. -var socketOpsCommonownerType *socketOpsCommon +var socketOperationsownerType *SocketOperations // Refs implements refs.RefCounter. It keeps a reference count using atomic // operations and calls the destructor when the count reaches zero. @@ -25,7 +25,7 @@ var socketOpsCommonownerType *socketOpsCommon // without growing the size of Refs. // // +stateify savable -type socketOpsCommonRefs struct { +type socketOperationsRefs struct { // refCount is composed of two fields: // // [32-bit speculative references]:[32-bit real references] @@ -36,7 +36,7 @@ type socketOpsCommonRefs struct { refCount int64 } -func (r *socketOpsCommonRefs) finalize() { +func (r *socketOperationsRefs) finalize() { var note string switch refs_vfs1.GetLeakMode() { case refs_vfs1.NoLeakChecking: @@ -45,20 +45,20 @@ func (r *socketOpsCommonRefs) finalize() { note = "(Leak checker uninitialized): " } if n := r.ReadRefs(); n != 0 { - log.Warningf("%sRefs %p owned by %T garbage collected with ref count of %d (want 0)", note, r, socketOpsCommonownerType, n) + log.Warningf("%sRefs %p owned by %T garbage collected with ref count of %d (want 0)", note, r, socketOperationsownerType, n) } } // EnableLeakCheck checks for reference leaks when Refs gets garbage collected. -func (r *socketOpsCommonRefs) EnableLeakCheck() { +func (r *socketOperationsRefs) EnableLeakCheck() { if refs_vfs1.GetLeakMode() != refs_vfs1.NoLeakChecking { - runtime.SetFinalizer(r, (*socketOpsCommonRefs).finalize) + runtime.SetFinalizer(r, (*socketOperationsRefs).finalize) } } // ReadRefs returns the current number of references. The returned count is // inherently racy and is unsafe to use without external synchronization. -func (r *socketOpsCommonRefs) ReadRefs() int64 { +func (r *socketOperationsRefs) ReadRefs() int64 { return atomic.LoadInt64(&r.refCount) + 1 } @@ -66,9 +66,9 @@ func (r *socketOpsCommonRefs) ReadRefs() int64 { // IncRef implements refs.RefCounter.IncRef. // //go:nosplit -func (r *socketOpsCommonRefs) IncRef() { +func (r *socketOperationsRefs) IncRef() { if v := atomic.AddInt64(&r.refCount, 1); v <= 0 { - panic(fmt.Sprintf("Incrementing non-positive ref count %p owned by %T", r, socketOpsCommonownerType)) + panic(fmt.Sprintf("Incrementing non-positive ref count %p owned by %T", r, socketOperationsownerType)) } } @@ -79,7 +79,7 @@ func (r *socketOpsCommonRefs) IncRef() { // other TryIncRef calls from genuine references held. // //go:nosplit -func (r *socketOpsCommonRefs) TryIncRef() bool { +func (r *socketOperationsRefs) TryIncRef() bool { const speculativeRef = 1 << 32 v := atomic.AddInt64(&r.refCount, speculativeRef) if int32(v) < 0 { @@ -104,10 +104,10 @@ func (r *socketOpsCommonRefs) TryIncRef() bool { // A: TryIncRef [transform speculative to real] // //go:nosplit -func (r *socketOpsCommonRefs) DecRef(destroy func()) { +func (r *socketOperationsRefs) DecRef(destroy func()) { switch v := atomic.AddInt64(&r.refCount, -1); { case v < -1: - panic(fmt.Sprintf("Decrementing non-positive ref count %p, owned by %T", r, socketOpsCommonownerType)) + panic(fmt.Sprintf("Decrementing non-positive ref count %p, owned by %T", r, socketOperationsownerType)) case v == -1: diff --git a/pkg/sentry/socket/unix/socket_vfs2_refs.go b/pkg/sentry/socket/unix/socket_vfs2_refs.go new file mode 100644 index 000000000..dc55f2947 --- /dev/null +++ b/pkg/sentry/socket/unix/socket_vfs2_refs.go @@ -0,0 +1,118 @@ +package unix + +import ( + "fmt" + "runtime" + "sync/atomic" + + "gvisor.dev/gvisor/pkg/log" + refs_vfs1 "gvisor.dev/gvisor/pkg/refs" +) + +// ownerType is used to customize logging. Note that we use a pointer to T so +// that we do not copy the entire object when passed as a format parameter. +var socketVFS2ownerType *SocketVFS2 + +// Refs implements refs.RefCounter. It keeps a reference count using atomic +// operations and calls the destructor when the count reaches zero. +// +// Note that the number of references is actually refCount + 1 so that a default +// zero-value Refs object contains one reference. +// +// TODO(gvisor.dev/issue/1486): Store stack traces when leak check is enabled in +// a map with 16-bit hashes, and store the hash in the top 16 bits of refCount. +// This will allow us to add stack trace information to the leak messages +// without growing the size of Refs. +// +// +stateify savable +type socketVFS2Refs struct { + // refCount is composed of two fields: + // + // [32-bit speculative references]:[32-bit real references] + // + // Speculative references are used for TryIncRef, to avoid a CompareAndSwap + // loop. See IncRef, DecRef and TryIncRef for details of how these fields are + // used. + refCount int64 +} + +func (r *socketVFS2Refs) finalize() { + var note string + switch refs_vfs1.GetLeakMode() { + case refs_vfs1.NoLeakChecking: + return + case refs_vfs1.UninitializedLeakChecking: + note = "(Leak checker uninitialized): " + } + if n := r.ReadRefs(); n != 0 { + log.Warningf("%sRefs %p owned by %T garbage collected with ref count of %d (want 0)", note, r, socketVFS2ownerType, n) + } +} + +// EnableLeakCheck checks for reference leaks when Refs gets garbage collected. +func (r *socketVFS2Refs) EnableLeakCheck() { + if refs_vfs1.GetLeakMode() != refs_vfs1.NoLeakChecking { + runtime.SetFinalizer(r, (*socketVFS2Refs).finalize) + } +} + +// ReadRefs returns the current number of references. The returned count is +// inherently racy and is unsafe to use without external synchronization. +func (r *socketVFS2Refs) ReadRefs() int64 { + + return atomic.LoadInt64(&r.refCount) + 1 +} + +// IncRef implements refs.RefCounter.IncRef. +// +//go:nosplit +func (r *socketVFS2Refs) IncRef() { + if v := atomic.AddInt64(&r.refCount, 1); v <= 0 { + panic(fmt.Sprintf("Incrementing non-positive ref count %p owned by %T", r, socketVFS2ownerType)) + } +} + +// TryIncRef implements refs.RefCounter.TryIncRef. +// +// To do this safely without a loop, a speculative reference is first acquired +// on the object. This allows multiple concurrent TryIncRef calls to distinguish +// other TryIncRef calls from genuine references held. +// +//go:nosplit +func (r *socketVFS2Refs) TryIncRef() bool { + const speculativeRef = 1 << 32 + v := atomic.AddInt64(&r.refCount, speculativeRef) + if int32(v) < 0 { + + atomic.AddInt64(&r.refCount, -speculativeRef) + return false + } + + atomic.AddInt64(&r.refCount, -speculativeRef+1) + return true +} + +// DecRef implements refs.RefCounter.DecRef. +// +// Note that speculative references are counted here. Since they were added +// prior to real references reaching zero, they will successfully convert to +// real references. In other words, we see speculative references only in the +// following case: +// +// A: TryIncRef [speculative increase => sees non-negative references] +// B: DecRef [real decrease] +// A: TryIncRef [transform speculative to real] +// +//go:nosplit +func (r *socketVFS2Refs) DecRef(destroy func()) { + switch v := atomic.AddInt64(&r.refCount, -1); { + case v < -1: + panic(fmt.Sprintf("Decrementing non-positive ref count %p, owned by %T", r, socketVFS2ownerType)) + + case v == -1: + + if destroy != nil { + destroy() + } + } +} diff --git a/pkg/sentry/socket/unix/unix.go b/pkg/sentry/socket/unix/unix.go index 917055cea..f80011ce4 100644 --- a/pkg/sentry/socket/unix/unix.go +++ b/pkg/sentry/socket/unix/unix.go @@ -55,6 +55,7 @@ type SocketOperations struct { fsutil.FileNoopFlush `state:"nosave"` fsutil.FileUseInodeUnstableAttr `state:"nosave"` + socketOperationsRefs socketOpsCommon } @@ -84,11 +85,27 @@ func NewWithDirent(ctx context.Context, d *fs.Dirent, ep transport.Endpoint, sty return fs.NewFile(ctx, d, flags, &s) } +// DecRef implements RefCounter.DecRef. +func (s *SocketOperations) DecRef(ctx context.Context) { + s.socketOperationsRefs.DecRef(func() { + s.ep.Close(ctx) + if s.abstractNamespace != nil { + s.abstractNamespace.Remove(s.abstractName, s) + } + }) +} + +// Release implemements fs.FileOperations.Release. +func (s *SocketOperations) Release(ctx context.Context) { + // Release only decrements a reference on s because s may be referenced in + // the abstract socket namespace. + s.DecRef(ctx) +} + // socketOpsCommon contains the socket operations common to VFS1 and VFS2. // // +stateify savable type socketOpsCommon struct { - socketOpsCommonRefs socket.SendReceiveTimeout ep transport.Endpoint @@ -101,23 +118,6 @@ type socketOpsCommon struct { abstractNamespace *kernel.AbstractSocketNamespace } -// DecRef implements RefCounter.DecRef. -func (s *socketOpsCommon) DecRef(ctx context.Context) { - s.socketOpsCommonRefs.DecRef(func() { - s.ep.Close(ctx) - if s.abstractNamespace != nil { - s.abstractNamespace.Remove(s.abstractName, s) - } - }) -} - -// Release implemements fs.FileOperations.Release. -func (s *socketOpsCommon) Release(ctx context.Context) { - // Release only decrements a reference on s because s may be referenced in - // the abstract socket namespace. - s.DecRef(ctx) -} - func (s *socketOpsCommon) isPacket() bool { switch s.stype { case linux.SOCK_DGRAM, linux.SOCK_SEQPACKET: diff --git a/pkg/sentry/socket/unix/unix_state_autogen.go b/pkg/sentry/socket/unix/unix_state_autogen.go index 89d78a9ad..51fd66b78 100644 --- a/pkg/sentry/socket/unix/unix_state_autogen.go +++ b/pkg/sentry/socket/unix/unix_state_autogen.go @@ -6,26 +6,49 @@ import ( "gvisor.dev/gvisor/pkg/state" ) -func (x *socketOpsCommonRefs) StateTypeName() string { - return "pkg/sentry/socket/unix.socketOpsCommonRefs" +func (x *socketOperationsRefs) StateTypeName() string { + return "pkg/sentry/socket/unix.socketOperationsRefs" } -func (x *socketOpsCommonRefs) StateFields() []string { +func (x *socketOperationsRefs) StateFields() []string { return []string{ "refCount", } } -func (x *socketOpsCommonRefs) beforeSave() {} +func (x *socketOperationsRefs) beforeSave() {} -func (x *socketOpsCommonRefs) StateSave(m state.Sink) { +func (x *socketOperationsRefs) StateSave(m state.Sink) { x.beforeSave() m.Save(0, &x.refCount) } -func (x *socketOpsCommonRefs) afterLoad() {} +func (x *socketOperationsRefs) afterLoad() {} -func (x *socketOpsCommonRefs) StateLoad(m state.Source) { +func (x *socketOperationsRefs) StateLoad(m state.Source) { + m.Load(0, &x.refCount) +} + +func (x *socketVFS2Refs) StateTypeName() string { + return "pkg/sentry/socket/unix.socketVFS2Refs" +} + +func (x *socketVFS2Refs) StateFields() []string { + return []string{ + "refCount", + } +} + +func (x *socketVFS2Refs) beforeSave() {} + +func (x *socketVFS2Refs) StateSave(m state.Sink) { + x.beforeSave() + m.Save(0, &x.refCount) +} + +func (x *socketVFS2Refs) afterLoad() {} + +func (x *socketVFS2Refs) StateLoad(m state.Source) { m.Load(0, &x.refCount) } @@ -35,6 +58,7 @@ func (x *SocketOperations) StateTypeName() string { func (x *SocketOperations) StateFields() []string { return []string{ + "socketOperationsRefs", "socketOpsCommon", } } @@ -43,13 +67,15 @@ func (x *SocketOperations) beforeSave() {} func (x *SocketOperations) StateSave(m state.Sink) { x.beforeSave() - m.Save(0, &x.socketOpsCommon) + m.Save(0, &x.socketOperationsRefs) + m.Save(1, &x.socketOpsCommon) } func (x *SocketOperations) afterLoad() {} func (x *SocketOperations) StateLoad(m state.Source) { - m.Load(0, &x.socketOpsCommon) + m.Load(0, &x.socketOperationsRefs) + m.Load(1, &x.socketOpsCommon) } func (x *socketOpsCommon) StateTypeName() string { @@ -58,7 +84,6 @@ func (x *socketOpsCommon) StateTypeName() string { func (x *socketOpsCommon) StateFields() []string { return []string{ - "socketOpsCommonRefs", "SendReceiveTimeout", "ep", "stype", @@ -71,23 +96,21 @@ func (x *socketOpsCommon) beforeSave() {} func (x *socketOpsCommon) StateSave(m state.Sink) { x.beforeSave() - m.Save(0, &x.socketOpsCommonRefs) - m.Save(1, &x.SendReceiveTimeout) - m.Save(2, &x.ep) - m.Save(3, &x.stype) - m.Save(4, &x.abstractName) - m.Save(5, &x.abstractNamespace) + m.Save(0, &x.SendReceiveTimeout) + m.Save(1, &x.ep) + m.Save(2, &x.stype) + m.Save(3, &x.abstractName) + m.Save(4, &x.abstractNamespace) } func (x *socketOpsCommon) afterLoad() {} func (x *socketOpsCommon) StateLoad(m state.Source) { - m.Load(0, &x.socketOpsCommonRefs) - m.Load(1, &x.SendReceiveTimeout) - m.Load(2, &x.ep) - m.Load(3, &x.stype) - m.Load(4, &x.abstractName) - m.Load(5, &x.abstractNamespace) + m.Load(0, &x.SendReceiveTimeout) + m.Load(1, &x.ep) + m.Load(2, &x.stype) + m.Load(3, &x.abstractName) + m.Load(4, &x.abstractNamespace) } func (x *SocketVFS2) StateTypeName() string { @@ -100,6 +123,7 @@ func (x *SocketVFS2) StateFields() []string { "FileDescriptionDefaultImpl", "DentryMetadataFileDescriptionImpl", "LockFD", + "socketVFS2Refs", "socketOpsCommon", } } @@ -112,7 +136,8 @@ func (x *SocketVFS2) StateSave(m state.Sink) { m.Save(1, &x.FileDescriptionDefaultImpl) m.Save(2, &x.DentryMetadataFileDescriptionImpl) m.Save(3, &x.LockFD) - m.Save(4, &x.socketOpsCommon) + m.Save(4, &x.socketVFS2Refs) + m.Save(5, &x.socketOpsCommon) } func (x *SocketVFS2) afterLoad() {} @@ -122,11 +147,13 @@ func (x *SocketVFS2) StateLoad(m state.Source) { m.Load(1, &x.FileDescriptionDefaultImpl) m.Load(2, &x.DentryMetadataFileDescriptionImpl) m.Load(3, &x.LockFD) - m.Load(4, &x.socketOpsCommon) + m.Load(4, &x.socketVFS2Refs) + m.Load(5, &x.socketOpsCommon) } func init() { - state.Register((*socketOpsCommonRefs)(nil)) + state.Register((*socketOperationsRefs)(nil)) + state.Register((*socketVFS2Refs)(nil)) state.Register((*SocketOperations)(nil)) state.Register((*socketOpsCommon)(nil)) state.Register((*SocketVFS2)(nil)) diff --git a/pkg/sentry/socket/unix/unix_vfs2.go b/pkg/sentry/socket/unix/unix_vfs2.go index 8b1abd922..3345124cc 100644 --- a/pkg/sentry/socket/unix/unix_vfs2.go +++ b/pkg/sentry/socket/unix/unix_vfs2.go @@ -45,6 +45,7 @@ type SocketVFS2 struct { vfs.DentryMetadataFileDescriptionImpl vfs.LockFD + socketVFS2Refs socketOpsCommon } @@ -91,6 +92,25 @@ func NewFileDescription(ep transport.Endpoint, stype linux.SockType, flags uint3 return vfsfd, nil } +// DecRef implements RefCounter.DecRef. +func (s *SocketVFS2) DecRef(ctx context.Context) { + s.socketVFS2Refs.DecRef(func() { + t := kernel.TaskFromContext(ctx) + t.Kernel().DeleteSocketVFS2(&s.vfsfd) + s.ep.Close(ctx) + if s.abstractNamespace != nil { + s.abstractNamespace.Remove(s.abstractName, s) + } + }) +} + +// Release implements vfs.FileDescriptionImpl.Release. +func (s *SocketVFS2) Release(ctx context.Context) { + // Release only decrements a reference on s because s may be referenced in + // the abstract socket namespace. + s.DecRef(ctx) +} + // GetSockOpt implements the linux syscall getsockopt(2) for sockets backed by // a transport.Endpoint. func (s *SocketVFS2) GetSockOpt(t *kernel.Task, level, name int, outPtr usermem.Addr, outLen int) (marshal.Marshallable, *syserr.Error) { |