49 Commits

Author SHA1 Message Date
Nicolas Lacasse d22dedf3d5 Check all 3 stdio FDs to determine if terminal is connected to a pty.
Previously we were only looking at stdin, which could be a pty but other stdio
fds might be redirected. In that case, we can incorrectly end up using the
stdin fd as *the* console fd, and sending all stdout/stderr to that FD,
ignoring the redirect.

Note that the behavior was actually flaky because the mechanism for choosing
which stdio fd to treat as *the* pty fd is non-deterministic (due to the map
iteration in fdimport/fdimport.go:Import) and so sometimes we would choose
the correct one.

This CL also cleans up `argsFromProcess` and `argsFromCLI`, which were setting
their `FilePayload` unnecessarily, since it is always set in `Execute`.

Fixes #11350
Fixes #11349

PiperOrigin-RevId: 716733446
2025-01-17 11:15:59 -08:00
Nayana Bidari bd0cbf8071 Fix typo in exec.go.
PiperOrigin-RevId: 705917223
2024-12-13 10:04:24 -08:00
Ayush Ranjan 0009d4e568 runsc: exec: Use container spec as default when --process is not provided.
This is consistent with runc. This fixes several bugs with runsc exec:
- When --process flag is specified, the process spec should be validated. The
  process spec should not inherit values from the OCI spec except capabilities.
  Earlier, we were setting WorkingDirectory and Envv from the spec if these
  were not set in the process file.
- When --process flag is not specified, we should use the Process defined in
  the container spec as the base and append the following flags onto that
  process spec. Earlier if these flags were specified, we were not using the
  container spec values and just setting to these passed flags, hence making it
  look like runsc is "clearing" these fields when their flags are passed.
    - additional-gids
    - cap
    - env
- When --process flag is not specified, we should use the following values
  defined in the container spec's Process. Those values should be selectively
  overridden when the corresponding flag is set. Earlier, we were always using
  the flag values, even when the flag was not set. One implication was that we
  were always running with UID=GID=0 when --process and --user are not set.
    - user
    - cwd
- When --cap is set, it should not append to the Inheritable capabilities
  defined in the spec. And it should only be appended to Ambient if Inheritable
  in the original spec is non-empty.

Fixes #11108

PiperOrigin-RevId: 694642077
2024-11-08 14:32:01 -08:00
Koichi Shiraishi 0cf77c02f8 all: remove use io/ioutil deprecated package & fix some deprecated thing
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
2024-10-10 20:36:24 +09:00
Ayush Ranjan 7981df85f3 Make all custom flag.Value implementations idempotent.
Set(String()) should be an idempotent operation. This is a useful property
which allows us to generate args while re-execing the same process. Setting
`--flag-name=val.String()` should work.

PiperOrigin-RevId: 552598313
2023-07-31 14:53:21 -07:00
B. Blechschmidt 761bda09a5 Add support for execution via host file descriptor
This commit adds support for program execution via a host file
descriptor. To make use of this feature, the host file descriptor must
be provided to the --exec-fd argument. For example,

    exec 3</usr/bin/echo
    runsc exec --exec-fd=3 mycontainer hello world

will run the host's echo binary inside gVisor. In this case, "hello" is
supplied to echo as argv[0]. As a result, the output of the above
command is "world".

This feature is useful for bootstrapping unknown guest environments and
allows static executables to perform setup actions inside the container
while they need not be part of the guest file system.
2023-04-02 01:27:19 +02:00
B. Blechschmidt a1f2c94e20 Add lost changes to --pass-fd merge
GitHub pull request #8634 concluded that commit d1f3b45 should be
merged. However, the changes since commit feb40ac got lost in merge
36793c0. This commit is the lost diff.
2023-03-29 17:17:38 +02:00
gVisor bot 36793c077c Merge pull request #8634 from blechschmidt:passfd
PiperOrigin-RevId: 516362077
2023-03-13 17:02:22 -07:00
B. Blechschmidt d1f3b45b38 Add --pass-fd flag to runsc run and exec
This commit implements file descriptor passing from the host to the
guest. It implements a --pass-fd flag that can be specified multiple
times with FD numbers from the host that will be inserted into the file
descriptor table of the guest.
2023-03-10 00:30:44 +01:00
B. Blechschmidt d5e1ed3cd9 Add terminal support to do, exec and run commands
When a terminal is used, signals are now forwared to the foreground
process inside the sandbox. As a result, control commands like Ctrl+C
can be handled by the terminal in the guest.
2023-02-27 01:21:16 +01:00
Kevin Krakauer d8aa09e04c 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
Fabricio Voznika 368a4fe8b3 Refactor subcommands error handling into a separate package
This is going to be used by the trace subcommand which lives in
another package.

Updates #4805

PiperOrigin-RevId: 447006075
2022-05-06 09:40:39 -07:00
Fabricio Voznika f51e0486d4 Fix stdios ownership
Set stdio ownership based on the container's user to ensure the
user can open/read/write to/from stdios.

1. stdios in the host are changed to have the owner be the same
uid/gid of the process running the sandbox. This ensures that the
sandbox has full control over it.
2. stdios owner owner inside the sandbox is changed to match the
container's user to give access inside the container and make it
behave the same as runc.

Fixes #6180

PiperOrigin-RevId: 384347009
2021-07-12 16:55:40 -07:00
Ayush Ranjan e668288faf [op] Replace syscall package usage with golang.org/x/sys/unix in runsc/.
The syscall package has been deprecated in favor of golang.org/x/sys.

Note that syscall is still used in some places because the following don't seem
to have an equivalent in unix package:
- syscall.SysProcIDMap
- syscall.Credential

Updates #214

PiperOrigin-RevId: 361381490
2021-03-06 22:07:07 -08:00
Fabricio Voznika 7e462a1c7f OCI spec may contain duplicate environment variables
Closes #5226

PiperOrigin-RevId: 351259576
2021-01-11 16:25:50 -08:00
Fabricio Voznika 8ea19b5818 Add sandbox ID to state file name
This allows to find all containers inside a sandbox more efficiently.
This operation is required every time a container starts and stops,
and previously required loading *all* container state files to check
whether the container belonged to the sandbox.

Apert from being inneficient, it has caused problems when state files
are stale or corrupt, causing inavalability to create any container.

Also adjust commands `list` and `debug` to skip over files that fail
to load.

Resolves #5052

PiperOrigin-RevId: 348050637
2020-12-17 10:52:44 -08:00
Fabricio Voznika e2d9a68eef Add support for TTY in multi-container
Fixes #2714

PiperOrigin-RevId: 342950412
2020-11-17 14:51:24 -08:00
Fabricio Voznika c47f8afe23 Fix failure setting OOM score adjustment
When OOM score adjustment needs to be set, all the containers need to be
loaded to find all containers that belong to the sandbox. However, each
load signals the container to ensure it is still alive. OOM score
adjustment is set during creation and deletion of every container, generating
a flood of signals to all containers. The fix removes the signal check
when it's not needed.

There is also a race fetching OOM score adjustment value from the parent when
the sandbox exits at the same  time (the time it took to signal containers above
made this window quite large). The fix is to store the original value
in the sandbox state file and use it when the value needs to be restored.

Also add more logging and made the existing ones more consistent to help with
debugging.

PiperOrigin-RevId: 340940799
2020-11-05 15:36:20 -08:00
Ayush Ranjan 2eaf54dd59 Refactor tty codebase to use master-replica terminology.
Updates #2972

PiperOrigin-RevId: 329584905
2020-09-01 14:43:41 -07:00
Fabricio Voznika be76c7ce6e Move boot.Config to its own package
Updates #3494

PiperOrigin-RevId: 327548511
2020-08-19 18:37:42 -07:00
Adin Scannell 3e8b38d08b Add flag package to limit visibility.
PiperOrigin-RevId: 294297004
2020-02-10 13:57:01 -08:00
Fabricio Voznika 0b02c3d5e5 Prevent CAP_NET_RAW from appearing in exec
'docker exec' was getting CAP_NET_RAW even when --net-raw=false
because it was not filtered out from when copying container's
capabilities.

PiperOrigin-RevId: 272260451
2019-10-01 11:49:49 -07:00
Nicolas Lacasse f2ea8e6b24 Always set HOME env var with runsc exec.
We already do this for `runsc run`, but need to do the same for `runsc exec`.

PiperOrigin-RevId: 270793459
2019-09-23 17:06:02 -07:00
Nicolas Lacasse 67e2f227aa Always set SysProcAttr.Ctty to an FD in the child's FD table.
Go was going to change the behavior of SysProcAttr.Ctty such that it must be an
FD in the *parent* FD table:
https://go-review.googlesource.com/c/go/+/178919/

However, after some debate, it was decided that this change was too
backwards-incompatible, and so it was reverted.
https://github.com/golang/go/issues/29458

The behavior going forward is unchanged: the Ctty FD must be an FD in the
*child* FD table.

PiperOrigin-RevId: 255228476
2019-06-26 11:27:31 -07:00
Nicolas Lacasse a8f148b8e4 Use different Ctty FDs based on the go version.
An upcoming change in Go 1.13 [1] changes the semantics of the SysProcAttr.Ctty
field. Prior to the change, the FD must be an FD in the child process's FD
table (aka "post-shuffle"). After the change, the FD must be an FD in the
current process's FD table (aka "pre-shuffle").

To be compatible with both versions this CL introduces a new boolean
"CttyFdIsPostShuffle" which indicates whether a pre- or post-shuffle FD should
be provided. We use build tags to chose the correct one.

1: https://go-review.googlesource.com/c/go/+/178919/
PiperOrigin-RevId: 255015303
2019-06-25 11:47:27 -07:00