Commit Graph
476 Commits
Author SHA1 Message Date
Jing ChenandgVisor bot e3f7443b9b Support MS_STRICTATIME flag for the syscall mount.
PiperOrigin-RevId: 560207305
2023-08-25 15:17:23 -07:00
Shambhavi SrivastavaandgVisor bot 884e65cf41 Automated rollback of changelist 557871250
PiperOrigin-RevId: 560158129
2023-08-25 11:58:29 -07:00
Andrei VaginandgVisor bot f556917137 vfs: MountNamespace.Root() has to return a top mount of /
A few mounts can be mounted on top of `/`.

PiperOrigin-RevId: 558264274
2023-08-18 15:35:47 -07:00
Nicolas LacasseandgVisor bot 5dc1f9fa34 Automated rollback of changelist 554554034
PiperOrigin-RevId: 557871250
2023-08-17 10:50:10 -07:00
Andrei VaginandgVisor bot 2b9d893bab Mount has to follow symlinks when it resolves target
For example, docker specifies a directory descriptor via /proc/self/fd:
mount(/sys/fs/cgroup/...", "/proc/self/fd/6", MS_BIND, 0x502f, <null>)

PiperOrigin-RevId: 557312955
2023-08-15 18:26:45 -07:00
Lucas ManningandgVisor bot 14e0833da8 Refactor and simplify propagation logic.
This refactor changes the "shared list" from being a list pointer that's
shared among all the peers, to a circularly linked list that links all the
mounts together. This is generally safer (no shared pointer) and easier
to understand and work with. It's also closer to how linux implements this
feature.

The propagationType is also removed in favor of just using the propagation
flags from linux. This makes more sense as mounts can have two types at once
(shared+slave).

PiperOrigin-RevId: 556906544
2023-08-14 14:10:56 -07:00
Shambhavi SrivastavaandgVisor bot f75fcdfa83 Implementing clone3
Updates #8585

PiperOrigin-RevId: 554554034
2023-08-07 12:19:32 -07:00
Andrei VaginandgVisor bot c5493106c6 Implement the setns syscall
This change introduces the nsfs file system. Each new namespace allocates
a new nsfs inode.

Here are reasons why we need these inodes:
* each namespace has to have an unique id.
* proc/pid/ns/ contains one entry for each namespace. Bind mounting one of
  the files in this directory to somewhere else in the filesystem keeps the
  corresponding namespace alive even if all processes currently in
  the namespace terminate.
* setns() allows the calling process to join an existing namespace specified
  by a file descriptor.

PiperOrigin-RevId: 550694515
2023-07-24 15:45:08 -07:00
Nicolas LacasseandgVisor bot 6733665148 Implement PR_{S,G}ET_CHILD_SUBREAPER.
Closes #2323

PiperOrigin-RevId: 548205854
2023-07-14 13:19:25 -07:00
turekt 355bb3b1f2 POLLRDHUP delivery fix
Fixes https://github.com/google/gvisor/issues/6015 | Added EventRdHUp to waiter events | Added POLLRDHUP delivery on half-closed connection detection | Added POLLRDHUP delivery on local read end shutdown
2023-07-09 15:53:24 +02:00
Etienne PerotandgVisor bot ba61bf6e93 gVisor metric library: Change interface for passing in field values.
This introduces a `metric.FieldValue` struct type that wraps a string.
All metric interfaces that deal with field values have been updated to use
pointers to this type instead of strings.

The intent of this change is to make it more obvious that field values must
be passed using references. Prior to this change, this was done using string
pointer comparisons. Now this must be done by using a pointer to the same
`metric.FieldValue` struct.

The struct type still externally exposes its string so that it can be referred
to in value function callbacks by "custom" metrics. (Though there are no
current uses of callback metrics with fields.)

PiperOrigin-RevId: 527030738
2023-04-25 11:49:02 -07:00
Etienne PerotandgVisor bot 0de4e69d3b gVisor metric library: Optimize operations for large-cardinality metrics.
This optimizes both increments and lookup operations.

It does so using the following:

- For metrics of potential cardinality larger than 48, it will switch to using
  a map rather than linear search for mapping field value combinations to the
  index that combination corresponds to in the flattened list of values.
  For metrics of cardinality 48 or smaller, linear search is still used as it
  is still faster (and also still faster than binary search), as determined by
  benchmarks.
- All field values are checked for pointer uniqueness (i.e. the pointer to the
  start of each field value string must be unique). This is then used for
  faster matching: instead of comparing whole strings (and needing to hash
  them, in the case of doing map-based lookups), it compares pointer values.
  In the context of this metric library, because all field values must be
  pre-declared ahead of time, keeping references to these pre-declared strings
  should always be possible. This is enforced: it will `panic` if the metric
  user does not do this. In practice, this is easy to do by using `const`
  strings for all metric values. This change does just that for existing
  metrics with string fields.

From benchmarks, this speeds up the time to take a snapshot of existing
metrics by -8.35%. With the unimplemented syscall counter metric, this
optimization reduces the slowdown of adding this metric from +4,250% to
a still-large but much more manageable +290%. A further optimization
(cl/524419591) will reduce this overhead further before re-introducing the
unimplemented syscall counter metric.

PiperOrigin-RevId: 526134756
2023-04-21 14:21:18 -07:00
Etienne PerotandgVisor bot c6d19db4e4 Update unimpl.EmitUnimplementedEvent interface to add the syscall number.
This catches up the interface to the `EmitUnimplementedEvent` method signature
on `kernel.Kernel`.

Also add build-time test to verify that `kernel.Kernel` implements this
interface, in order to catch such breakages at build time in the future.

PiperOrigin-RevId: 519000411
2023-03-23 17:01:37 -07:00
Etienne PerotandgVisor bot 6ff46ccaa2 runsc: Add metric counting calls of unimplemented system calls.
This is useful to determine which syscalls users want but are unimplemented.

PiperOrigin-RevId: 518962607
2023-03-23 14:24:56 -07:00
Etienne PerotandgVisor bot da6948a692 gVisor: Refactor SyscallFn to take in the syscall number as argument.
This will be used to plumb the syscall number through to a counter metric that
exports the number of times an unimplemented syscall has been called.

Plenty of syscall implementations call `EmitUnimplementedEvent` for flags and
settings that are not implemented. With `sysno` available, they will be able
to plumb that bit of information through.

PiperOrigin-RevId: 518635831
2023-03-22 12:06:26 -07:00
Etienne PerotandgVisor bot ab715c16d0 seccheck: Initialize seccheck.Points only when needed.
This moves the initialization of `seccheck.Points` out of package-level
`init` and instead moves it to an explicit `seccheck.Initialize()` function.

On AMD64, this saves about 614KiB of heap memory that would otherwise always
be live.

PiperOrigin-RevId: 514813332
2023-03-07 12:53:27 -08:00
Adin ScannellandgVisor bot 2911d8c3b8 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Lucas ManningandgVisor bot 46263053c6 Implement OFD POSIX locks.
This change also switches lock EOF to be MaxInt instead of MaxUint,
since the lock length can be a negative value.

Fixes #5264

PiperOrigin-RevId: 513571374
2023-03-02 10:14:34 -08:00
Konstantin BogomolovandgVisor bot 0416d0a2d3 Add TESTONLY sentry panic trigger through afs_syscall.
This flag is added for tests that need to trigger a panic in the sentry
kernel. Only done for x86_64 which does have a dedicated syscall number for
afs_syscall; ARM does not.

PiperOrigin-RevId: 512731631
2023-02-27 14:28:30 -08:00
Nicolas LacasseandgVisor bot bd9a28a368 Ignore AT_NO_AUTOMOUNT flag in statx() calls.
gVisor does not support automount, so this flag is a no-op.

Confirmed that this fixes recent versions of 'ls':

$ docker run -ti --runtime=fig rockylinux:8.7 /bin/bash
[root@c6c749af0a7a /]# ls /
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

PiperOrigin-RevId: 511376385
2023-02-21 20:10:44 -08:00
Nicolas LacasseandgVisor bot 20e925e730 Use linuxerr.Equals to check for equality with linuxerr.ErrWouldBlock.
Now that hostinet is enabled, we sometimes get EAGAIN which is not equal to
ErrWouldBlock, so we must use linuxerr.Equals to compare them.

PiperOrigin-RevId: 508501889
2023-02-09 16:13:53 -08:00
Nicolas LacasseandgVisor bot 256cb0cd62 Fix (most of) tcp_socket_test on hostinet.
A few fixes in here:

* Linux does not transition the state of a non-blocking socket to SS_CONNECTED
  when the connect happens asynchronously. Instead, it leaves the state as
  SS_CONNECTING (seems like a Linux bug, but OK). This can introduce weird
  behavior for subsequent connect() calls. gVisor now forces the state to
  update to SS_CONNECTED by calling connect() twice.

* Socket shutdown events are slightly different between gVisor and linux. We
  already assert different behavior between the two environments, and now we
  also have to check for gVisor+hostinet instead of just gvisor.

* Handle send/recv timeouts, which fixes blocking for read()/write() syscalls.

* Handle cases like MSG_PEEK where the senderAddress is not returned.

* Pass through some more socketopts, and allow them in the syscall filters.

Something is still wrong with shutdown() and poll() calls, so those tests are
temporarily disabled. I'm looking at those next.

This also fixes a number of other syscall test suites, so those are enabled as
well.

PiperOrigin-RevId: 508397317
2023-02-09 09:39:26 -08:00
Andrei VaginandgVisor bot febf833940 Allow to return an error from PullFullState.
PiperOrigin-RevId: 504415294
2023-01-24 17:15:17 -08:00
Fabricio VoznikaandgVisor bot 4d3d2741d3 Rename pselect to pselect6
The actual syscall name is pselect6. This name is used in Docker's
default seccomp profile.

PiperOrigin-RevId: 503297316
2023-01-19 16:38:48 -08:00
Rahat MahmoodandgVisor bot b47ecb8223 Disable io_uring syscalls by default.
The current io_uring support is very limited and experimental. Disable
it by default, and add a flag to enable it for testing.

PiperOrigin-RevId: 500760451
2023-01-09 11:11:57 -08:00
Ayush RanjanandgVisor bot e6493374a4 Cleanup remaining comments for #1624.
Fixes #1624

PiperOrigin-RevId: 492292937
2022-12-01 14:01:37 -08:00
Ayush RanjanandgVisor bot 64e8ac9ea1 Delete fsbridge.
Updates #1624

PiperOrigin-RevId: 492286535
2022-12-01 13:36:10 -08:00
Zach KoopmansandgVisor bot c7dc8294b3 Disable process_vm_(read|write)v.
Syzkaller has found several issues with the two syscalls and a rework is
required. Disable tests and the syscall until issues can be fixed.

PiperOrigin-RevId: 491716795
2022-11-29 13:14:29 -08:00
Zach KoopmansandgVisor bot b2bdf961a6 Don't use other process's scratch buffer.
If we use the remote process's scratch buffer in process_vm_readv|writev
calls, we will get data races. Use the local task's scratch buffer always.

PiperOrigin-RevId: 491468904
2022-11-28 16:14:02 -08:00
Ayush RanjanandgVisor bot 1003ae8e93 Delete VFS1 completely.
- Delete pkg/sentry/fs/*.
- Move pkg/sentry/fs/fsutil out of VFS1 directory and remove VFS1 components.
- Remove remaining unused references to VFS1 from remaining codebase.
- Rename/refactor code to avoid even referencing VFS2, unless necessary.
- Rewrite VFS1-only tests to VFS2.

Updates #1624

PiperOrigin-RevId: 490064269
2022-11-21 13:57:52 -08:00
Zach KoopmansandgVisor bot d40a81f27d Fix circular lock in process_vm_(read|write)v
PiperOrigin-RevId: 489582978
2022-11-18 16:11:21 -08:00
Zach KoopmansandgVisor bot abe3813b7f Re-enable process_vm_(read|write)v
PiperOrigin-RevId: 489298284
2022-11-17 13:50:43 -08:00
Shambhavi SrivastavaandgVisor bot 874909badf Adding more trace point integration tests for the following syscalls:
- Chdir
- Fchdir
- Setgid
- Setuid
- Setsid
- Setresuid
- Setresgid

Updates #4805

PiperOrigin-RevId: 489076929
2022-11-16 17:06:35 -08:00
Ayush RanjanandgVisor bot ee523cfbc7 Delete VFS1 filesystem implementations.
Updates #1624

PiperOrigin-RevId: 488986080
2022-11-16 11:05:10 -08:00
Fabricio VoznikaandgVisor bot 22de242ebf Add read/write syscalls to trace points
Closes #8092

PiperOrigin-RevId: 488719448
2022-11-15 11:54:23 -08:00
Ayush RanjanandgVisor bot d90d0743d3 Delete VFS1 syscall handlers.
Directly use VFS2 syscall handlers. No need to override VFS2 handlers.
Updates #1624

PiperOrigin-RevId: 488448348
2022-11-14 13:11:22 -08:00
Sergey MadaminovandgVisor bot 7be97e8499 io_uring_enter: adding support for READV operation.
Handling NOP operations helped us to introduce facilities to process submission
requests. Now we can proceed to adding more meaningful operations. The logical
choice for the first one is the READV operation as it is a next operation after
NOP and it is also used for the cat-like application example based of IO_URING.

PiperOrigin-RevId: 487615618
2022-11-10 12:54:50 -08:00
Kevin KrakauerandgVisor bot 3a76efe831 convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Sergey MadaminovandgVisor bot 66309c0d80 io_uring_enter with NOP implementation for the IO_URING.
With io_uring_setup() and mmap() we can proceed with implementing a simplest
operation, namely, NOP. Once we make sure that we can send this command and
receive a response from the gVisor, we can move forward to read operation.

PiperOrigin-RevId: 485106019
2022-10-31 11:39:18 -07:00
Lucas ManningandgVisor bot d5f623ef88 Allow external packages to clone and set the propagation type of mounts.
PiperOrigin-RevId: 483821346
2022-10-25 17:41:56 -07:00
Lucas ManningandgVisor bot b7a7b9c054 Allow for creation of shared mounts.
This is the first in a series of changes that will enabled shared mount
subtrees. This will emulate the Linux kernel implementation described here:
https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt

Mounts can now a be part of a mount groups. These groups are
identified by ids assigned by the VFS. Group IDs can be reused after all
mounts that are a member of that group are destroyed.

Newly created mounts inherit the propagation type of their parent mount. A
mount created on a shared mount will also be a shared mount, though it will
be part of a different group. A shared mount 'A' bound to a shared mount 'B'
will replicate that bind to all of 'B's peers. These new mounts will all be
part of a new shared peer group.

Unmounting a mount 'A' that is a direct child of a shared mount 'B' mounted
at dentry 'b' will propagate that event to all its peers. So peers B1, B2,
B3, etc will all unmount the mounts (A1, A2...) located at 'b'. However, if
any of peers of A have children, they are skipped. If the original mount A
has children, the mount is failed entirely.

The initial root mount has propagation type MS_PRIVATE.

This change only implements a basic version of mount groups. Notably it does
not implement MS_SLAVE, MS_UNBINDABLE, or MS_REC.

PiperOrigin-RevId: 483792757
2022-10-25 15:35:10 -07:00
Lucas ManningandgVisor bot 4fd1477fe0 Add support for bind mounting.
This change adds limited support for the MS_BIND flag. MS_REC is not yet
implemented.

PiperOrigin-RevId: 478030021
2022-09-30 10:52:34 -07:00
Sergey MadaminovandgVisor bot 485b68eb69 mmap() implementation for the IO_URING.
Once the user receives a file descriptor from `io_uring_setup()`, it will be
used for the subsequent `mmap()` calls. Thus, we need to add support for it in
our iouringfs.

PiperOrigin-RevId: 477318038
2022-09-27 17:44:25 -07:00
Sergey MadaminovandgVisor bot 50a8b30992 Pseudo-filesystem iouringfs for IO_URING.
Similar to Linux, we should add pseudo-filesystem iouringfs in gVisor to back
IO_URING's file descriptors. We start with stubbing `io_uring_setup(2)` so it
provides a file descriptor backed by anonfs.

PiperOrigin-RevId: 473384042
2022-09-09 18:09:29 -07:00
Zach KoopmansandgVisor bot 1e969283b9 Disable process_vm_read/writev and add a test.
ProcessVM readv/writev needs some work due to lock ordering issues
with locking MM between tasks. So disable the call until it
can be fixed.

Also add a test from one of the identified bugs from syzcaller.

PiperOrigin-RevId: 467085160
2022-08-11 17:11:17 -07:00
Zach KoopmansandgVisor bot 7fff218532 Support process_vm_read for same user only.
PiperOrigin-RevId: 466123035
2022-08-08 12:56:50 -07:00
Ayush RanjanandgVisor bot de25857eea Add inotify support to kernfs and anonfs.
This is consistent with Linux. Some applications recursively add
files to inotify FD. They fail if they run into kernfs files
because inotify_add_watch(2) returns EPERM in that case.

PiperOrigin-RevId: 462642204
2022-07-22 09:45:24 -07:00
Ayush RanjanandgVisor bot 2a928cea80 Handle IN_DONT_FOLLOW correctly in VFS2 inotify.
Added a syscall test for IN_DONT_FOLLOW as well.

PiperOrigin-RevId: 462224328
2022-07-20 14:03:14 -07:00
Etienne PerotandgVisor bot 15e3dd3c3d gVisor: Turn arch.Context interface references to struct pointers.
Since `arch.Context64` is the only concrete implementation of
`arch.Context`, this does not functionally change anything.
While `arch.Context64` *can* mean multiple things in the codebase, since
the `arch` module uses conditional compilation to create different builds
for AMD64 vs ARM64, the `Context64` type is still singular within each
possible version this can compile to.

This avoids the overhead involved in calling interface
functions in Go:
https://github.com/teh-cmc/go-internals/blob/master/chapter2_interfaces/README.md#dynamic-dispatch

This overhead is particularly painful for functions like `SyscallSaveOrig`
which is a no-op on AMD64.

On KVM, this reduces syscall latency by 4~5%:

```
name       old wall_ns/op  new wall_ns/op  delta
Getpid           602 ± 4%        579 ± 3%  -3.94%  (p=0.000 n=66+195)
GetpidOpt        604 ± 3%        573 ± 3%  -5.10%  (p=0.000 n=71+194)

name       old cpu_ns/op   new cpu_ns/op   delta
Getpid           604 ± 5%        581 ± 0%  -3.81%  (p=0.000 n=75+130)
GetpidOpt        604 ± 2%        574 ± 3%  -4.95%  (p=0.000 n=71+196)
```

PiperOrigin-RevId: 461742893
2022-07-18 16:32:28 -07:00
Ayush RanjanandgVisor bot 5772fca52c Implement MPOL_PREFERRED as MPOL_LOCAL when nodeset is empty.
After 7858d7bca7fb ("mm/mempolicy: don't handle MPOL_LOCAL like a
fake MPOL_PREFERRED policy"), MPOL_PREFERRED is implemented as
MPOL_LOCAL when nodeset is empty.

Earlier, MPOL_LOCAL was implemented as MPOL_PREFERRED and kernel
checked that nodeset was empty.

Fixed the syscall test to avoid this conversion process. We don't
know which machine our native tests will run on, so it is hard to
test for either of these conversions of MPOL modes. In this state
the syscall test passes on newer and older kernels.

PiperOrigin-RevId: 460560999
2022-07-12 15:06:40 -07:00