12 Commits
Author SHA1 Message Date
Shailend ChandandgVisor bot c687bd9c65 Fix zero-length receives on unix sockets
A zero-length recvmsg() on a unix socket returned 0 immediately,
without touching the message queue. On Linux, such a receive still
interacts with the queue. It blocks or returns EAGAIN when the queue
is empty. On packet (datagram and seqpacket) sockets, it dequeues the
next message: its control messages are delivered, MSG_TRUNC is set in
msg_flags when a non-empty payload is discarded, and the message is
consumed, unless MSG_PEEK is set. On stream sockets, no payload is
consumed, but the control messages at the current read position are
delivered.

Route zero-length receives through the same path as MSG_TRUNC reads
with a zero-byte destination, which already implements these
semantics.

PiperOrigin-RevId: 958610858
2026-08-03 15:33:03 -07:00
Ryan El Kochta ad1f2ef91a SCM_CREDENTIALS: use PID from ucred rather than caller's PID
Previously, the caller-provided PID was used for the CAP_SYS_ADMIN check, but wasn't actually passed along.
2026-06-30 10:18:20 -07:00
gVisor bot 3818aa1109 Test for passing zero-length SCM_RIGHTS fd arrays
PiperOrigin-RevId: 583488887
2023-11-17 14:37:09 -08:00
gVisor bot 3175ff4ce8 Inherit SO_PASSCRED from listening socket on server socket
PiperOrigin-RevId: 508137166
2023-02-08 11:35:35 -08:00
Ghanan GowripalanandgVisor bot 129cc47c63 Move socket_test_util to //test/util
...and rename the library to socket_util.

PiperOrigin-RevId: 386348306
2021-07-22 16:37:15 -07:00
Dean DengandgVisor bot b25bb452f7 Fix typo boundry -> boundary.
PiperOrigin-RevId: 352905565
2021-01-20 17:01:00 -08:00
Dean DengandgVisor bot bf509e78d6 Fix SCM Rights S/R reference leak.
Control messages collected when peeking into a socket were being leaked.

PiperOrigin-RevId: 339114961
2020-10-26 14:15:55 -07:00
Dean DengandgVisor bot f6aa673ece Rewrite reference leak checker without finalizers.
Our current reference leak checker uses finalizers to verify whether an object
has reached zero references before it is garbage collected. There are multiple
problems with this mechanism, so a rewrite is in order.

With finalizers, there is no way to guarantee that a finalizer will run before
the program exits. When an unreachable object with a finalizer is garbage
collected, its finalizer will be added to a queue and run asynchronously. The
best we can do is run garbage collection upon sandbox exit to make sure that
all finalizers are enqueued.

Furthermore, if there is a chain of finalized objects, e.g. A points to B
points to C, garbage collection needs to run multiple times before all of the
finalizers are enqueued. The first GC run will register the finalizer for A but
not free it. It takes another GC run to free A, at which point B's finalizer
can be registered. As a result, we need to run GC as many times as the length
of the longest such chain to have a somewhat reliable leak checker.

Finally, a cyclical chain of structs pointing to one another will never be
garbage collected if a finalizer is set. This is a well-known issue with Go
finalizers (https://github.com/golang/go/issues/7358). Using leak checking on
filesystem objects that produce cycles will not work and even result in memory
leaks.

The new leak checker stores reference counted objects in a global map when
leak check is enabled and removes them once they are destroyed. At sandbox
exit, any remaining objects in the map are considered as leaked. This provides
a deterministic way of detecting leaks without relying on the complexities of
finalizers and garbage collection.

This approach has several benefits over the former, including:
- Always detects leaks of objects that should be destroyed very close to
  sandbox exit. The old checker very rarely detected these leaks, because it
  relied on garbage collection to be run in a short window of time.
- Panics if we forgot to enable leak check on a ref-counted object (we will try
  to remove it from the map when it is destroyed, but it will never have been
  added).
- Can store extra logging information in the map values without adding to the
  size of the ref count struct itself. With the size of just an int64, the ref
  count object remains compact, meaning frequent operations like IncRef/DecRef
  are more cache-efficient.
- Can aggregate leak results in a single report after the sandbox exits.
  Instead of having warnings littered in the log, which were
  non-deterministically triggered by garbage collection, we can print all
  warning messages at once. Note that this could also be a limitation--the
  sandbox must exit properly for leaks to be detected.

Some basic benchmarking indicates that this change does not significantly
affect performance when leak checking is enabled, which is understandable
since registering/unregistering is only done once for each filesystem object.

Updates #1486.

PiperOrigin-RevId: 338685972
2020-10-23 09:17:02 -07:00
Andrei VaginandgVisor bot 6fbe2c3e57 strace: don't create a slice with a negative value
PiperOrigin-RevId: 283613824
2019-12-03 13:49:38 -08:00
HaiboandgVisor bot eaab6788a0 test/syscall: Remove duplicated gtest/gtest.h.
Signed-off-by: Haibo Xu <[email protected]>
Change-Id: I05a7ec69b98b88931ba4a8adb3e8a7b822006001
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/gvisor/pull/1023 from xiaobo55x:syscall_test d44a8b1f827ed4081997af96cd58ba7449e0a9e1
PiperOrigin-RevId: 276740442
2019-10-25 12:40:36 -07:00
Michael PrattandgVisor bot 5b41ba5d0e Fix various spelling issues in the documentation
Addresses obvious typos, in the documentation only.

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/gvisor/pull/443 from Pixep:fix/documentation-spelling 4d0688164eafaf0b3010e5f4824b35d1e7176d65
PiperOrigin-RevId: 255477779
2019-06-27 14:25:50 -07:00
Michael PrattandShentubot 57772db2e7 Shutdown host sockets on internal shutdown
This is required to make the shutdown visible to peers outside the
sandbox.

The readClosed / writeClosed fields were dropped, as they were
preventing a shutdown socket from reading the remainder of queued bytes.
The host syscalls will return the appropriate errors for shutdown.

The control message tests have been split out of socket_unix.cc to make
the (few) remaining tests accessible to testing inherited host UDS,
which don't support sending control messages.

Updates #273

PiperOrigin-RevId: 251763060
2019-06-05 18:40:37 -07:00