Commit Graph
74 Commits
Author SHA1 Message Date
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
Shambhavi SrivastavaandgVisor bot f75fcdfa83 Implementing clone3
Updates #8585

PiperOrigin-RevId: 554554034
2023-08-07 12:19:32 -07:00
Jing ChenandgVisor bot 906efa4976 Implement setns CLONE_NEWUTS namespace type.
PiperOrigin-RevId: 554306089
2023-08-06 15:33:25 -07:00
Andrei VaginandgVisor bot 2d37d59eeb kernel: don't use atomic pointers for task.netns
task.netns is always changed from a task goroutine under task.mu.

It means that we can access it without any locks from a task goroutine
we don't need to increment a reference counter in such cases.

In all other cases, we need to take task.mu.

PiperOrigin-RevId: 552913323
2023-08-01 14:04:53 -07:00
Andrei VaginandgVisor bot 4af420d420 Implement setns for mount namespaces
PiperOrigin-RevId: 552859231
2023-08-01 11:12:29 -07:00
Andrei VaginandgVisor bot 4d39b66860 Implement mount namespaces
This change implements only the basic functions of mount namespaces.
All features that depends on user namespaces will be implemented separately.

PiperOrigin-RevId: 552673896
2023-07-31 21:12:21 -07:00
Jing ChenandgVisor bot a595c26bfe Implement setns CLONE_NEWIPC namespace type.
PiperOrigin-RevId: 552619565
2023-07-31 16:12:45 -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
Jamie LiuandgVisor bot fe546357e4 Pass context to kernel.TaskImage.release().
PiperOrigin-RevId: 543541608
2023-06-26 14:28:58 -07:00
Andrei VaginandgVisor bot 7170fa06fb kernel: unshare a network namespace without taking Task.mu
t.netns is an atomic pointer so it should not be a problem for readers.
As for writers, only task can change its network namespace.

Reported-by: [email protected]
Reported-by: [email protected]
Reported-by: [email protected]
PiperOrigin-RevId: 542384765
2023-06-21 15:51:28 -07:00
Nicolas LacasseandgVisor bot 98122bc1bf Clarify comment about copying Task.image in Task.Clone().
PiperOrigin-RevId: 510829046
2023-02-19 10:43:46 -08:00
Nicolas LacasseandgVisor bot b0a62cb67f Hold t.mu while accessing t.image.
We copy t.image with t.mu held, to avoid holding the lock
while calling t.image.Fork.

PiperOrigin-RevId: 510698814
2023-02-18 13:12:02 -08:00
Andrei VaginandgVisor bot febf833940 Allow to return an error from PullFullState.
PiperOrigin-RevId: 504415294
2023-01-24 17:15:17 -08:00
Lucas ManningandgVisor bot 13b3dac594 Fix circular lock between filesystemRWMutex and taskSetRWMutex.
PiperOrigin-RevId: 500747195
2023-01-09 10:24:35 -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
Ayush RanjanandgVisor bot 7cac4e2732 Start cleaning up VFS1.
PiperOrigin-RevId: 486586072
2022-11-07 00:39:54 -08:00
Fabricio VoznikaandgVisor bot 59f1b78fd5 Rename seccheck.checkers to seccheck.sinks
Makes the naming consistent with the public configuration. There is no
behavior change.

Updates #4805

PiperOrigin-RevId: 462448505
2022-07-21 12:47:06 -07:00
Andrei VaginandgVisor bot 89c8e320fc Don't leak network namespaces
PiperOrigin-RevId: 454707336
2022-06-13 15:05:21 -07:00
Andrei VaginandgVisor bot bffa7607f4 Automated rollback of changelist 445452190
PiperOrigin-RevId: 454236327
2022-06-10 14:01:33 -07:00
Ayush RanjanandgVisor bot 2026e1f078 Reformat codebase.
PiperOrigin-RevId: 449358041
2022-05-17 17:48:35 -07:00
Fabricio VoznikaandgVisor bot c7b4708168 Fire clone point for thread creation
Thread creation tracking is required by Falco.

Updates #4805

PiperOrigin-RevId: 447003670
2022-05-06 09:28:44 -07:00
Fabricio VoznikaandgVisor bot 0b5ad0d573 Remove TID from task start
This was used to map kernel.Task to goroutines before
kernel.Task.GoroutineID was available. It's not needed anymore.

PiperOrigin-RevId: 445452190
2022-04-29 10:32:08 -07:00
Zach KoopmansandgVisor bot 94557222ed Don't send security events for clone/exit tasks that are threads.
Two security messages points are PointCloneProcess and PointExitNotifyParent
(see pkg/sentry/seccheck/seccheck.go).
Both of these should only trigger when we have a process starting or exiting
respectively. Becuase of this, only send a start message if "clone()" is
called without CLONE_THREAD set, and only send an exit message if the thread
group (read: process) is exiting.

PiperOrigin-RevId: 445334328
2022-04-28 23:01:19 -07:00
Fabricio VoznikaandgVisor bot 81eaf62655 Refactor code to use seccheck.SendToCheckers
Updates #4805

PiperOrigin-RevId: 445017536
2022-04-27 18:04:14 -07:00
Kevin KrakauerandgVisor bot 3e7fcef1df switch fsimpl/ from sync/atomic to atomicbitops for 32 bit values
PiperOrigin-RevId: 443535714
2022-04-21 18:32:04 -07:00
Fabricio VoznikaandgVisor bot 93023f5e60 Use proto structs for seccheck points
Given that in most cases points are serialized to another process,
point data is now created diretly into protos.

As part of this change, infrastructure to track optional and context
fields was created to facilitate addition of lots of Points which is
needed for upcomming of changes.

Updates #4805

Currently the SST code is converting seccheck protos into SST protos
in the sentry before sending it to the API. After this change, SST
checker will be changed to send seccheck protos to the API and the
API then converts these into SST on the way to pubsub.

PiperOrigin-RevId: 442688320
2022-04-18 19:03:15 -07:00
Andrei Vagin 66f52d13e9 Fix circular locking of kernel.taskMutex and kernfs.filesystemRWMutex
mntns.DecRef can be called without holding task.Mu.

panic: WARNING: circular locking detected: kernel.taskMutex -> kernfs.filesystemRWMutex:
goroutine 68 [running]:
gvisor.dev/gvisor/pkg/log.Stacks(0x40)
        pkg/log/log.go:313 +0x9e
gvisor.dev/gvisor/pkg/sync/locking.checkLock(0xc0000b2780, 0xc0000b3b80, {0x0, 0x0, 0x739539})
        pkg/sync/locking/lockdep.go:53 +0x14a
gvisor.dev/gvisor/pkg/sync/locking.AddGLock(0xc0000b2780, 0x0)
        pkg/sync/locking/lockdep.go:102 +0x27d
gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs.(*filesystemRWMutex).Lock(0xc0001e0230)
        bazel-out/k8-fastbuild-ST-1a50d7a562c6/bin/pkg/sentry/fsimpl/kernfs/filesystem_mutex.go:16 +0x34
gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs.(*Filesystem).Release(0xc0001e01e0, {0x158ef50, 0xc00046ca80})
        pkg/sentry/fsimpl/kernfs/filesystem.go:266 +0x67
gvisor.dev/gvisor/pkg/sentry/fsimpl/proc.(*filesystem).Release(0xc0001e01e0, {0x158ef50, 0xc00046ca80})
        pkg/sentry/fsimpl/proc/filesystem.go:104 +0x6b
gvisor.dev/gvisor/pkg/sentry/vfs.(*Filesystem).DecRef.func1()
        pkg/sentry/vfs/filesystem.go:81 +0xbd
gvisor.dev/gvisor/pkg/sentry/vfs.(*FilesystemRefs).DecRef(0xc00046ca80, 0xc0005bbd00)
        bazel-out/k8-fastbuild-ST-1a50d7a562c6/bin/pkg/sentry/vfs/filesystem_refs.go:131 +0x74
gvisor.dev/gvisor/pkg/sentry/vfs.(*Filesystem).DecRef(0xc000512500, {0x158ef50, 0xc00046ca80})
        pkg/sentry/vfs/filesystem.go:77 +0x5b
gvisor.dev/gvisor/pkg/sentry/vfs.(*Mount).destroy(0xc000312d80, {0x158ef50, 0xc00046ca80})
        pkg/sentry/vfs/mount.go:544 +0x159
gvisor.dev/gvisor/pkg/sentry/vfs.(*Mount).DecRef(0xc000312d80, {0x158ef50, 0xc00046ca80})
        pkg/sentry/vfs/mount.go:528 +0x99
gvisor.dev/gvisor/pkg/sentry/vfs.(*MountNamespace).DecRef.func1()
        pkg/sentry/vfs/mount.go:583 +0x157
gvisor.dev/gvisor/pkg/sentry/vfs.(*MountNamespaceRefs).DecRef(0x0, 0xc0005bbed0)
        bazel-out/k8-fastbuild-ST-1a50d7a562c6/bin/pkg/sentry/vfs/mount_namespace_refs.go:131 +0x74
gvisor.dev/gvisor/pkg/sentry/vfs.(*MountNamespace).DecRef(0xc00046d238, {0x158ef50, 0xc00046ca80})
        pkg/sentry/vfs/mount.go:571 +0x87
gvisor.dev/gvisor/pkg/sentry/kernel.(*runExitMain).execute(0xc0004932a8, 0xc00046ca80)
        pkg/sentry/kernel/task_exit.go:256 +0x27f
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).run(0xc00046ca80, 0x1)
        pkg/sentry/kernel/task_run.go:95 +0x1ec
created by gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).Start
        pkg/sentry/kernel/task_start.go:345 +0x106

known lock chain: kernfs.filesystemRWMutex -> kernel.taskMutex

====== kernfs.filesystemRWMutex -> kernel.taskMutex =====
goroutine 68 [running]:
gvisor.dev/gvisor/pkg/log.Stacks(0x80)
        pkg/log/log.go:313 +0x9e
gvisor.dev/gvisor/pkg/sync/locking.AddGLock(0xc0000b3b80, 0x0)
        pkg/sync/locking/lockdep.go:105 +0x2b5
gvisor.dev/gvisor/pkg/sentry/kernel.(*taskMutex).Lock(0xc00046d238)
        bazel-out/k8-fastbuild-ST-1a50d7a562c6/bin/pkg/sentry/kernel/task_mutex.go:16 +0x34
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).WithMuLocked(0xc00046ca80, 0xc0005bad78)
        pkg/sentry/kernel/task.go:818 +0x3e
gvisor.dev/gvisor/pkg/sentry/fsimpl/proc.getMM(0x6245f69b)
        pkg/sentry/fsimpl/proc/task_files.go:48 +0x4d
gvisor.dev/gvisor/pkg/sentry/fsimpl/proc.(*taskOwnedInode).getOwner(0x1000000001ff, 0x0)
        pkg/sentry/fsimpl/proc/task.go:207 +0xb2
gvisor.dev/gvisor/pkg/sentry/fsimpl/proc.(*taskOwnedInode).Stat(0xc00049d2a8, {0x158ef50, 0xc00046ca80}, 0x0, {0x4, 0x0})
        pkg/sentry/fsimpl/proc/task.go:174 +0x117
gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs.(*Filesystem).StatAt(0xc0001e01e0, {0x158ef50, 0xc00046ca80}, 0xc00058c758, {0xb2a80, 0xc0})
        pkg/sentry/fsimpl/kernfs/filesystem.go:837 +0x262
gvisor.dev/gvisor/pkg/sentry/vfs.(*VirtualFilesystem).StatAt(0xc0005bb6f0, {0x158ef50, 0xc00046ca80}, 0xc0005bb6f0, 0xd9f54f, 0xc0005bb6c0)
        pkg/sentry/vfs/vfs.go:598 +0xe3
gvisor.dev/gvisor/pkg/sentry/syscalls/linux/vfs2.fstatat(0xc00046ca80, 0x7, 0x0, 0xc0005bbc14, 0x100)
        pkg/sentry/syscalls/linux/vfs2/stat.go:104 +0x5d1
gvisor.dev/gvisor/pkg/sentry/syscalls/linux/vfs2.Newfstatat(0x65, {{0x7}, {0x56043e7212c0}, {0x56043e721230}, {0x100}, {0x100}, {0x1}})
        pkg/sentry/syscalls/linux/vfs2/stat.go:50 +0x5f
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).executeSyscall(0xc00046ca80, 0x106, {{0x7}, {0x56043e7212c0}, {0x56043e721230}, {0x100}, {0x100}, {0x1}})
        pkg/sentry/kernel/task_syscall.go:103 +0x2d7
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).doSyscallInvoke(0xc00046ca80, 0xf6a220, {{0x7}, {0x56043e7212c0}, {0x56043e721230}, {0x100}, {0x100}, {0x1}})
        pkg/sentry/kernel/task_syscall.go:238 +0x57
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).doSyscallEnter(0xc0000c9c70, 0x15a3038, {{0x7}, {0x56043e7212c0}, {0x56043e721230}, {0x100}, {0x100}, {0x1}})
        pkg/sentry/kernel/task_syscall.go:198 +0x85
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).doSyscall(0xc00046ca80)
        pkg/sentry/kernel/task_syscall.go:173 +0x38e
gvisor.dev/gvisor/pkg/sentry/kernel.(*runApp).execute(0xc0004932a8, 0xc00046ca80)
        pkg/sentry/kernel/task_run.go:254 +0x10fb
gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).run(0xc00046ca80, 0x1)
        pkg/sentry/kernel/task_run.go:95 +0x1ec
created by gvisor.dev/gvisor/pkg/sentry/kernel.(*Task).Start
        pkg/sentry/kernel/task_start.go:345 +0x106
2022-03-31 15:42:34 -07:00
Konstantin BogomolovandgVisor bot 03113543e2 Implement close_range.
Fixes #5500

PiperOrigin-RevId: 431454836
2022-02-28 09:37:03 -08:00
Andrei Vagin 3ba699a85f Implement RLIMIT_NPROC
This is simplified version without supporting hierarchical
user-namespace counters.

Signed-off-by: Andrei Vagin <[email protected]>
2021-12-23 13:52:05 -08:00
gVisor bot f97f41330f Merge pull request #6345 from sudo-sturbia:mq/syscalls
PiperOrigin-RevId: 404901660
2021-10-21 16:05:53 -07:00
Zyad A. Ali a81833c478 Initialize POSIX queues' registry after creating a new IPCNamespace.
Updates #136
2021-09-28 20:43:52 +02:00
Andrei Vagin a29216bfb6 kernel: allow to access Task.netns without taking Task.mu
This allows to avoind unnecessary lock-ordering dependencies on task.mu.
2021-09-23 18:28:08 -07:00
Jamie LiuandgVisor bot 933aa25263 Add //pkg/sentry/seccheck.
This defines common infrastructure for dynamically-configured security checks,
including an example usage in the clone(2) path.

PiperOrigin-RevId: 394797270
2021-09-03 19:12:54 -07:00
Jamie LiuandgVisor bot b627edef15 Replace kernel package types for clone and unshare with linux package types.
PiperOrigin-RevId: 386312456
2021-07-22 13:41:46 -07:00
Zach KoopmansandgVisor bot 895c2eaa09 [syserror] Update syserror to linuxerr for EACCES, EBADF, and EPERM.
Update all instances of the above errors to the faster linuxerr implementation.
With the temporary linuxerr.Equals(), no logical changes are made.

PiperOrigin-RevId: 382306655
2021-06-30 08:18:59 -07:00
Zach KoopmansandgVisor bot cd84634cc0 [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL
Remove three syserror entries duplicated in linuxerr. Because of the
linuxerr.Equals method, this is a mere change of return values from
syserror to linuxerr definitions.

Done with only these three errnos as CLs removing all grow to a significantly
large size.

PiperOrigin-RevId: 382173835
2021-06-29 15:08:46 -07:00
Zach KoopmansandgVisor bot d8f34027b3 [syserror] Split usermem package
Split usermem package to help remove syserror dependency in go_marshal.
New hostarch package contains code not dependent on syserror.

PiperOrigin-RevId: 365651233
2021-03-29 13:30:21 -07:00
Jamie LiuandgVisor bot 0dede4687f Rename kernel.TaskContext to kernel.TaskImage.
This reduces confusion with context.Context (which is also relevant to
kernel.Tasks) and is consistent with existing function kernel.LoadTaskImage().

PiperOrigin-RevId: 342167298
2020-11-12 17:39:19 -08:00
Jamie LiuandgVisor bot 4f9be2d6d4 Separate kernel.Task.AsCopyContext() into CopyContext() and OwnCopyContext().
kernel.copyContext{t} cannot be used outside of t's task goroutine, for three
reasons:

- t.CopyScratchBuffer() is task-goroutine-local.

- Calling t.MemoryManager() without running on t's task goroutine or locking
  t.mu violates t.MemoryManager()'s preconditions.

- kernel.copyContext passes t as context.Context to MM IO methods, which is
  illegal outside of t's task goroutine (cf. kernel.Task.Value()).

Fix this by splitting AsCopyContext() into CopyContext() (which takes an
explicit context.Context and is usable outside of the task goroutine) and
OwnCopyContext() (which uses t as context.Context, but is only usable by t's
task goroutine).

PiperOrigin-RevId: 339933809
2020-10-30 13:54:47 -07:00
Dean DengandgVisor bot b2ee1accb2 [vfs2] Fix fork reference leaks.
PiperOrigin-RevId: 337919424
2020-10-19 13:20:13 -07:00
Dean DengandgVisor bot 61a20003c0 Fix shm reference leak.
All shm segments in an IPC namespace should be released once that namespace is
destroyed. Add reference counting to IPCNamespace so that once the last task
with a reference on it exits, we can trigger a destructor that will clean up
all shm segments that have not been explicitly freed by the application.

PiperOrigin-RevId: 337032977
2020-10-14 00:13:21 -07:00
Rahat MahmoodandgVisor bot c25d44649f Enable automated marshalling for the syscall package.
PiperOrigin-RevId: 331940975
2020-09-15 23:38:57 -07:00
Andrei VaginandgVisor bot 1e39390724 Add callbacks to support lazy loading/restoring thread states
PiperOrigin-RevId: 324748508
2020-08-03 22:08:25 -07:00
Nayana BidariandgVisor bot 4a8d0d3f20 Plumbing context.Context to DecRef() and Release().
context is passed to DecRef() and Release() which is
needed for SO_LINGER implementation.

PiperOrigin-RevId: 324672584
2020-08-03 13:36:05 -07:00
Jamie LiuandgVisor bot 090e61f4b2 Fix oom_score_adj.
- Make oomScoreAdj a ThreadGroup field (Linux: signal_struct::oom_score_adj).

- Avoid deadlock caused by Task.OOMScoreAdj()/SetOOMScoreAdj() locking Task.mu
  and TaskSet.mu in the wrong order (via Task.ExitState()).

PiperOrigin-RevId: 300814698
2020-03-13 13:19:13 -07:00
Ian LewisandgVisor bot 60f65f3a04 Stub oom_score_adj and oom_score.
Adds an oom_score_adj and oom_score proc file stub. oom_score_adj accepts
writes of values -1000 to 1000 and persists the value with the task. New tasks
inherit the parent's oom_score_adj.

oom_score is a read-only stub that always returns the value '0'.

Issue #202

PiperOrigin-RevId: 299245355
2020-03-05 18:23:01 -08:00
gVisor botandCopybara-Service 27ef5c3926 Initial network namespace support.
TCP/IP will work with netstack networking. hostinet doesn't work, and sockets
will have the same behavior as it is now.

Before the userspace is able to create device, the default loopback device can
be used to test.

/proc/net and /sys/net will still be connected to the root network stack; this
is the same behavior now.

Issue #1833

PiperOrigin-RevId: 296309389
2020-02-20 15:20:40 -08:00
gVisor bot 649b11f624 Plumb VFS2 inside the Sentry
- Added fsbridge package with interface that can be used to open
  and read from VFS1 and VFS2 files.
- Converted ELF loader to use fsbridge
- Added VFS2 types to FSContext
- Added vfs.MountNamespace to ThreadGroup

Updates #1623

PiperOrigin-RevId: 295183950
2020-02-14 11:12:47 -08:00