Commit Graph

158 Commits

Author SHA1 Message Date
Jamie Liu 3b0b1f104d Miscellaneous VFS2 fixes.
PiperOrigin-RevId: 316627764
2020-06-16 00:15:20 -07:00
Jamie Liu 77c206e371 Add //pkg/sentry/fsimpl/overlay.
Major differences from existing overlay filesystems:

- Linux allows lower layers in an overlay to require revalidation, but not the
  upper layer. VFS1 allows the upper layer in an overlay to require
  revalidation, but not the lower layer. VFS2 does not allow any layers to
  require revalidation. (Now that vfs.MkdirOptions.ForSyntheticMountpoint
  exists, no uses of overlay in VFS1 are believed to require upper layer
  revalidation; in particular, the requirement that the upper layer support the
  creation of "trusted." extended attributes for whiteouts effectively required
  the upper filesystem to be tmpfs in most cases.)

- Like VFS1, but unlike Linux, VFS2 overlay does not attempt to make mutations
  of the upper layer atomic using a working directory and features like
  RENAME_WHITEOUT. (This may change in the future, since not having a working
  directory makes error recovery for some operations, e.g. rmdir, particularly
  painful.)

- Like Linux, but unlike VFS1, VFS2 represents whiteouts using character
  devices with rdev == 0; the equivalent of the whiteout attribute on
  directories is xattr trusted.overlay.opaque = "y"; and there is no equivalent
  to the whiteout attribute on non-directories since non-directories are never
  merged with lower layers.

- Device and inode numbers work as follows:

    - In Linux, modulo the xino feature and a special case for when all layers
      are the same filesystem:

        - Directories use the overlay filesystem's device number and an
          ephemeral inode number assigned by the overlay.

        - Non-directories that have been copied up use the device and inode
          number assigned by the upper filesystem.

        - Non-directories that have not been copied up use a per-(overlay,
          layer)-pair device number and the inode number assigned by the lower
          filesystem.

    - In VFS1, device and inode numbers always come from the lower layer unless
      "whited out"; this has the adverse effect of requiring interaction with
      the lower filesystem even for non-directory files that exist on the upper
      layer.

    - In VFS2, device and inode numbers are assigned as in Linux, except that
      xino and the samefs special case are not supported.

- Like Linux, but unlike VFS1, VFS2 does not attempt to maintain memory mapping
  coherence across copy-up. (This may have to change in the future, as users
  may be dependent on this property.)

- Like Linux, but unlike VFS1, VFS2 uses the overlayfs mounter's credentials
  when interacting with the overlay's layers, rather than the caller's.

- Like Linux, but unlike VFS1, VFS2 permits multiple lower layers in an
  overlay.

- Like Linux, but unlike VFS1, VFS2's overlay filesystem is
  application-mountable.

Updates #1199

PiperOrigin-RevId: 316019067
2020-06-11 18:34:53 -07:00
Fabricio Voznika d58d57606a Don't copy structs with sync.Mutex during initialization
During inititalization inode struct was copied around, but
it isn't great pratice to copy it around since it contains
ref count and sync.Mutex.

Updates #1480

PiperOrigin-RevId: 315983788
2020-06-11 14:56:19 -07:00
Fabricio Voznika 67565078bb Implement flock(2) in VFS2
LockFD is the generic implementation that can be embedded in
FileDescriptionImpl implementations. Unique lock ID is
maintained in vfs.FileDescription and is created on demand.

Updates #1480

PiperOrigin-RevId: 315604825
2020-06-09 18:46:42 -07:00
Jamie Liu dc029b4b96 Implement VFS2 tmpfs mount options.
As in VFS1, the mode, uid, and gid options are supported.

Updates #1197

PiperOrigin-RevId: 315340510
2020-06-08 13:28:18 -07:00
Rahat Mahmood 21b6bc7280 Implement mount(2) and umount2(2) for VFS2.
This is mostly syscall plumbing, VFS2 already implements the internals of
mounts. In addition to the syscall defintions, the following mount-related
mechanisms are updated:

- Implement MS_NOATIME for VFS2, but only for tmpfs and goferfs. The other VFS2
  filesystems don't implement node-level timestamps yet.

- Implement the 'mode', 'uid' and 'gid' mount options for VFS2's tmpfs.

- Plumb mount namespace ownership, which is necessary for checking appropriate
  capabilities during mount(2).

Updates #1035

PiperOrigin-RevId: 315035352
2020-06-05 19:12:03 -07:00
Jamie Liu 49a9b78f74 Fix VFS2 gofer open(O_CREAT) reference leak.
gofer.filesystem.createAndOpenChildLocked() doesn't need to take a reference on
the new dentry since vfs.FileDescription.Init() will do so.

PiperOrigin-RevId: 314242127
2020-06-01 18:13:01 -07:00
Jamie Liu 3a987160aa Handle gofer blocking opens of host named pipes in VFS2.
Using tee instead of read to detect when a O_RDONLY|O_NONBLOCK pipe FD has a
writer circumvents the problem of what to do with the byte read from the pipe,
avoiding much of the complexity of the fdpipe package.

PiperOrigin-RevId: 314216146
2020-06-01 15:33:30 -07:00
Dean Deng ccf69bdd7e Implement IN_EXCL_UNLINK inotify option in vfs2.
Limited to tmpfs. Inotify support in other filesystem implementations to
follow.

Updates #1479

PiperOrigin-RevId: 313828648
2020-05-29 12:28:49 -07:00
Dean Deng 9ada8c972e Fix the smallest of typos.
PiperOrigin-RevId: 313817646
2020-05-29 11:35:09 -07:00
Dean Deng fe464f44b7 Port inotify to vfs2, with support in tmpfs.
Support in other filesystem impls is still needed. Unlike in Linux and vfs1, we
need to plumb inotify down to each filesystem implementation in order to keep
track of links/inode structures properly.

IN_EXCL_UNLINK still needs to be implemented, as well as a few inotify hooks
that are not present in either vfs1 or vfs2. Those will be addressed in
subsequent changes.

Updates #1479.

PiperOrigin-RevId: 313781995
2020-05-29 08:09:14 -07:00
Dean Deng 32021bce96 Correctly update link and ref counts in rmdir.
Inotify sends events when a watch target is reaches a link count of 0 (see
include/linux/fsnotify.h:fsnotify_inoderemove). Currently, we do not account
for both dir/ and dir/.. in unlink, causing
syscalls/linux/inotify.cc:WatchTargetDeletionGeneratesEvent to fail because
the expected inotify events are not generated.

Furthermore, we should DecRef() once the inode reaches zero links; otherwise,
we will leak a reference.

PiperOrigin-RevId: 313502091
2020-05-27 18:19:38 -07:00
Jamie Liu e028714a0d Support dfltuid and dfltgid mount options in the VFS2 gofer client.
PiperOrigin-RevId: 313332542
2020-05-26 22:44:31 -07:00
Jamie Liu af3121a523 Implement splice(2) and tee(2) for VFS2.
Updates #138

PiperOrigin-RevId: 313326354
2020-05-26 21:43:26 -07:00
Dean Deng 76369b6480 Move fsimpl/host file offset from inode to fileDescription.
PiperOrigin-RevId: 312559861
2020-05-20 14:53:51 -07:00
Dean Deng 05c89af6ed Implement mmap for host fs in vfs2.
In VFS1, both fs/host and fs/gofer used the same utils for host file mappings.
Refactor parts of fsimpl/gofer to create similar utils to share with
fsimpl/host (memory accounting code moved to fsutil, page rounding arithmetic
moved to usermem).

Updates #1476.

PiperOrigin-RevId: 312345090
2020-05-19 13:46:42 -07:00
Fabricio Voznika 20e6efd302 Remove IfChange/ThenChange lint from VFS2
As new functionality is added to VFS2, corresponding files in VFS1
don't need to be changed.

PiperOrigin-RevId: 312153799
2020-05-18 14:26:09 -07:00
Jamie Liu fb7e5f1676 Make utimes_test pass on VFS2.
PiperOrigin-RevId: 311657502
2020-05-14 20:09:55 -07:00
Nicolas Lacasse 47dfba7661 Port memfd_create to vfs2 and finish implementation of file seals.
Closes #2612.

PiperOrigin-RevId: 311548074
2020-05-14 09:35:54 -07:00
Nicolas Lacasse db655f020e Resolve remaining TODOs for tmpfs.
Closes #1197

PiperOrigin-RevId: 311438223
2020-05-13 17:36:37 -07:00
Jamie Liu d846077628 Enable overlayfs_stale_read by default for runsc.
Linux 4.18 and later make reads and writes coherent between pre-copy-up and
post-copy-up FDs representing the same file on an overlay filesystem. However,
memory mappings remain incoherent:

- Documentation/filesystems/overlayfs.rst, "Non-standard behavior": "If a file
  residing on a lower layer is opened for read-only and then memory mapped with
  MAP_SHARED, then subsequent changes to the file are not reflected in the
  memory mapping."

- fs/overlay/file.c:ovl_mmap() passes through to the underlying FD without any
  management of coherence in the overlay.

- Experimentally on Linux 5.2:

```
$ cat mmap_cat_page.c
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

int main(int argc, char **argv) {
  if (argc < 2) {
    errx(1, "syntax: %s [FILE]", argv[0]);
  }
  const int fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    err(1, "open(%s)", argv[1]);
  }
  const size_t page_size = sysconf(_SC_PAGE_SIZE);
  void* page = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  if (page == MAP_FAILED) {
    err(1, "mmap");
  }
  for (;;) {
    write(1, page, strnlen(page, page_size));
    if (getc(stdin) == EOF) {
      break;
    }
  }
  return 0;
}

$ gcc -O2 -o mmap_cat_page mmap_cat_page.c
$ mkdir lowerdir upperdir workdir overlaydir
$ echo old > lowerdir/file
$ sudo mount -t overlay -o "lowerdir=lowerdir,upperdir=upperdir,workdir=workdir" none overlaydir
$ ./mmap_cat_page overlaydir/file
old
^Z
[1]+  Stopped                 ./mmap_cat_page overlaydir/file
$ echo new > overlaydir/file
$ cat overlaydir/file
new
$ fg
./mmap_cat_page overlaydir/file

old
```

Therefore, while the VFS1 gofer client's behavior of reopening read FDs is only
necessary pre-4.18, replacing existing memory mappings (in both sentry and
application address spaces) with mappings of the new FD is required regardless
of kernel version, and this latter behavior is common to both VFS1 and VFS2.
Re-document accordingly, and change the runsc flag to enabled by default.

New test:
- Before this CL: https://source.cloud.google.com/results/invocations/5b222d2c-e918-4bae-afc4-407f5bac509b
- After this CL: https://source.cloud.google.com/results/invocations/f28c747e-d89c-4d8c-a461-602b33e71aab

PiperOrigin-RevId: 311361267
2020-05-13 10:53:37 -07:00
Jamie Liu 94251aedb4 Internal change.
PiperOrigin-RevId: 311046755
2020-05-11 20:03:25 -07:00
Jamie Liu 15de8cc9e0 Add fsimpl/gofer.InternalFilesystemOptions.OpenSocketsByConnecting.
PiperOrigin-RevId: 311014995
2020-05-11 16:14:36 -07:00
Jamie Liu 21b71395a6 Pass flags to fsimpl/host.inode.open().
This has two effects: It makes flags passed to open("/proc/[pid]/fd/[hostfd]")
effective, and it prevents imported pipes/sockets/character devices from being
opened with O_NONBLOCK unconditionally (because the underlying host FD was set
to non-blocking in ImportFD()).

PiperOrigin-RevId: 310596062
2020-05-08 11:35:41 -07:00
Jamie Liu 9115f26851 Allocate device numbers for VFS2 filesystems.
Updates #1197, #1198, #1672

PiperOrigin-RevId: 310432006
2020-05-07 14:01:53 -07:00
Nicolas Lacasse d0b1d0233d Move pkg/sentry/vfs/{eventfd,timerfd} to new packages in pkg/sentry/fsimpl.
They don't depend on anything in VFS2, so they should be their own packages.

PiperOrigin-RevId: 310416807
2020-05-07 12:44:03 -07:00
Nicolas Lacasse 26c60d7d5d Port signalfd to vfs2.
PiperOrigin-RevId: 310404113
2020-05-07 11:41:50 -07:00
Nicolas Lacasse 591ff0e424 Add maximum memory limit.
PiperOrigin-RevId: 310179277
2020-05-06 10:30:18 -07:00
Dean Deng faf89dd31a Update vfs2 socket TODOs.
Three updates:
- Mark all vfs2 socket syscalls as supported.
- Use the same dev number and ino number generator for all types of sockets,
  unlike in VFS1.
- Do not use host fd for hostinet metadata.

Fixes #1476, #1478, #1484, 1485, #2017.

PiperOrigin-RevId: 309994579
2020-05-05 12:11:14 -07:00
Dean Deng a6dbf9596d Update comments for synthetic gofer files in vfs2.
PiperOrigin-RevId: 309966538
2020-05-05 10:01:28 -07:00
Fabricio Voznika b3bd41434c Return correct name for imported host files
Implement PrependPath() in host.filesystem to correctly format
name for host files.

Updates #1672

PiperOrigin-RevId: 309959135
2020-05-05 09:21:14 -07:00
Jamie Liu 35951c3671 Translate p9.NoUID/GID to OverflowUID/GID.
p9.NoUID/GID (== uint32(-1) == auth.NoID) is not a valid auth.KUID/KGID; in
particular, using it for file ownership causes capabilities to be ineffective
since file capabilities require that the file's KUID and KGID are mapped into
the capability holder's user namespace [1], and auth.NoID is not mapped into
any user namespace. Map p9.NoUID/GID to a different, valid KUID/KGID; in the
unlikely case that an application actually using the overflow KUID/KGID
attempts an operation that is consequently permitted by client permission
checks, the remote operation will still fail with EPERM.

Since this changes the VFS2 gofer client to no longer ignore the invalid IDs
entirely, this CL both permits and requires that we change synthetic mount point
creation to use root credentials.

[1] See fs.Inode.CheckCapability or vfs.GenericCheckPermissions.

PiperOrigin-RevId: 309856455
2020-05-05 09:19:52 -07:00
Fabricio Voznika 57dbd7f362 Remove kernfs.Filesystem cast from GenericDirectoryFD
This allows for kerfs.Filesystem to be overridden by
different implementations.

Updates #1672

PiperOrigin-RevId: 309809321
2020-05-04 13:40:37 -07:00
Fabricio Voznika cbc5bef2a6 Add TTY support on VFS2 to runsc
Updates #1623, #1487

PiperOrigin-RevId: 309777922
2020-05-04 10:59:20 -07:00
Dean Deng 82bae30cee Port netstack, hostinet, and netlink sockets to VFS2.
All three follow the same pattern:
1. Refactor VFS1 sockets into socketOpsCommon, so that most of the methods can
   be shared with VFS2.
2. Create a FileDescriptionImpl with the corresponding socket operations,
   rewriting the few that cannot be shared with VFS1.
3. Set up a VFS2 socket provider that creates a socket by setting up a dentry
   in the global Kernel.socketMount and connecting it with a new
   FileDescription.

This mostly completes the work for porting sockets to VFS2, and many syscall
tests can be enabled as a result.
There are several networking-related syscall tests that are still not passing:
1. net gofer tests
2. socketpair gofer tests
2. sendfile tests (splice is not implemented in VFS2 yet)

Updates #1478, #1484, #1485

PiperOrigin-RevId: 309457331
2020-05-01 12:54:41 -07:00
Jamie Liu 01beec3bb4 Add gofer.InternalFilesystemOptions.LeakConnection.
PiperOrigin-RevId: 309317605
2020-04-30 16:04:21 -07:00
Nicolas Lacasse bae30a0c69 Implement waiter.Waitable methods on VFS2 host inodes.
This fixes bash in Ubuntu.

Updates #1672.

PiperOrigin-RevId: 309298252
2020-04-30 14:19:45 -07:00
Dean Deng 442fde405d Fix proc net bugs in VFS2.
The /proc/net/udp header was missing, and /proc/sys/net was set up as
/proc/sys/net/net. Discovered while trying to run networking tests for VFS2.

PiperOrigin-RevId: 309243758
2020-04-30 09:48:00 -07:00
Dean Deng ce19497c1c Fix Unix socket permissions.
Enforce write permission checks in BoundEndpointAt, which corresponds to the
permission checks in Linux (net/unix/af_unix.c:unix_find_other).
Also, create bound socket files with the correct permissions in VFS2.

Fixes #2324.

PiperOrigin-RevId: 308949084
2020-04-28 20:13:01 -07:00
Dean Deng f3ca5ca82a Support pipes and sockets in VFS2 gofer fs.
Named pipes and sockets can be represented in two ways in gofer fs:
1. As a file on the remote filesystem. In this case, all file operations are
   passed through 9p.
2. As a synthetic file that is internal to the sandbox. In this case, the
   dentry stores an endpoint or VFSPipe for sockets and pipes respectively,
   which replaces interactions with the remote fs through the gofer.
In gofer.filesystem.MknodAt, we attempt to call mknod(2) through 9p,
and if it fails, fall back to the synthetic version.

Updates #1200.

PiperOrigin-RevId: 308828161
2020-04-28 08:34:00 -07:00
Dean Deng 1c2ecbb1a0 Import host sockets.
The FileDescription implementation for hostfs sockets uses the standard Unix
socket implementation (unix.SocketVFS2), but is also tied to a hostfs dentry.

Updates #1672, #1476

PiperOrigin-RevId: 308716426
2020-04-27 16:02:18 -07:00
Zach Koopmans 15a822a193 VFS2: Get HelloWorld image tests to pass with VFS2
This change includes:
- Modifications to loader_test.go to get TestCreateMountNamespace to
pass with VFS2.
- Changes necessary to get TestHelloWorld in image tests to pass with
VFS2. This means runsc can run the hello-world container with docker
on VSF2.

Note: Containers that use sockets will not run with these changes.
See "//test/image/...". Any tests here with sockets currently fail
(which is all of them but HelloWorld).
PiperOrigin-RevId: 308363072
2020-04-24 18:23:37 -07:00
Dean Deng f13f26d17d Port SCM Rights to VFS2.
Fixes #1477.

PiperOrigin-RevId: 308317511
2020-04-24 13:46:58 -07:00
Dean Deng 632b104aff Plumb context.Context into kernfs.Inode.Open().
PiperOrigin-RevId: 308304793
2020-04-24 12:37:49 -07:00
Fabricio Voznika 2cc0fd42f4 Fixes for procfs
- Return ENOENT for /proc/[pid]/task if task is zoombied or terminated
- Allow directory to be Seek() to the end
- Construct synthetic files for /proc/[pid]/ns/*
- Changed GenericDirectoryFD.Init to not register with FileDescription,
  otherwise other implementation cannot change behavior.

Updates #1195,1193

PiperOrigin-RevId: 308294649
2020-04-24 11:45:19 -07:00
Dean Deng 1b88c63b3e Move hostfs mount to Kernel struct.
This is needed to set up host fds passed through a Unix socket. Note that
the host package depends on kernel, so we cannot set up the hostfs mount
directly in Kernel.Init as we do for sockfs and pipefs.

Also, adjust sockfs to make its setup look more like hostfs's and pipefs's.

PiperOrigin-RevId: 308274053
2020-04-24 10:03:43 -07:00
Dean Deng 40a712c57c Refactor syscall.Fstat calls in hostfs.
Just call syscall.Fstat directly each time mode/file owner are needed. This
feels more natural than using i.getPermissions().

PiperOrigin-RevId: 308257405
2020-04-24 08:20:28 -07:00
Nicolas Lacasse 696feaf10c Port devpts to VFS2.
PiperOrigin-RevId: 308164359
2020-04-23 17:34:29 -07:00
Jamie Liu 5042ea7e2c Add vfs.MkdirOptions.ForSyntheticMountpoint.
PiperOrigin-RevId: 308143529
2020-04-23 15:37:10 -07:00
Adin Scannell 1481499fe2 Simplify Docker test infrastructure.
This change adds a layer of abstraction around the internal Docker APIs,
and eliminates all direct dependencies on Dockerfiles in the infrastructure.

A subsequent change will automated the generation of local images (with
efficient caching). Note that this change drops the use of bazel container
rules, as that experiment does not seem to be viable.

PiperOrigin-RevId: 308095430
2020-04-23 11:33:30 -07:00