Commit Graph

117 Commits

Author SHA1 Message Date
Nalin Dahyabhai 76c18c8970 chroot: use $PATH when finding commands
Use the $PATH from the runtime config, if it includes one, so that when
the command to run isn't an absolute path and the command isn't being
processed by the shell, exec.Command()'s internal call to
exec.LookPath() will find it.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-09-10 12:55:59 -04:00
Jan Kaluza a0a9ac6380 Switch common, storage and image to monorepo.
Signed-off-by: Jan Kaluza <jkaluza@redhat.com>
2025-08-29 15:04:28 +02:00
Nalin Dahyabhai 4ea64c3871 Add a dummy "runtime" that just dumps its config file
Add a dummy "runtime" that just dumps its runtime config, either the
entirety of it, or a section of it corresponding to each command line
argument.  Tests can use it to ensure that we set the right thing in the
configuration without also depending on the runtime to do as its asked,
which isn't always something we have control over.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-29 14:17:37 -04:00
Nalin Dahyabhai 0da7ea1cc4 chroot: use symbolic names when complaining about mount() errors
Translate flags passed to mount() and read back using statfs() from hex
to named constants, to make troubleshooting a bit easier.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-04-15 17:53:43 -04:00
Kir Kolyshkin 0835cb4760 Use slices.Clone
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:09 -07:00
Kir Kolyshkin 84a3905f61 Use slices.Contains
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:01 -07:00
Kir Kolyshkin 4249d94d76 chroot: fix unused warnings
When running golangci-lint run --tests=false, it complains:

> chroot/seccomp.go:15:7: const `seccompAvailable` is unused (unused)
> const seccompAvailable = true
>       ^
> chroot/seccomp.go:182:6: func `setupSeccomp` is unused (unused)
> func setupSeccomp(spec *specs.Spec, seccompProfilePath string) error {
>      ^

Fix this.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Kir Kolyshkin 7b15646547 Apply De Morgan's law to fix QF1001 warnings
This fixes the following warnings:

> chroot/run_test.go:319:39: QF1001: could apply De Morgan's law (staticcheck)
> 				if limit == unix.RLIM_INFINITY && !(rlim == nil || (rlim.Soft == unix.RLIM_INFINITY && rlim.Hard == unix.RLIM_INFINITY)) {
> 				                                  ^
> copier/copier.go:1012:92: QF1001: could apply De Morgan's law (staticcheck)
> 			if target, err := os.Readlink(filepath.Join(workingPath, components[0])); err == nil && !(len(components) == 1 && !evaluateFinalComponent) {
> 			                                                                                        ^
> run_linux.go:459:118: QF1001: could apply De Morgan's law (staticcheck)
> 	if !slices.Contains(volumes, resolvconf.DefaultResolvConf) && options.ConfigureNetwork != define.NetworkDisabled && !(len(b.CommonBuildOpts.DNSServers) == 1 && strings.ToLower(b.CommonBuildOpts.DNSServers[0]) == "none") {
> 	                                                                                                                    ^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Nalin Dahyabhai c1fcbb2d96 createPlatformContainer: drop MS_REMOUNT|MS_BIND
When attempting to change the mount propagation of the old root
directory tree before unmounting it, it's enough that we pass the
requested propagation flags.

In particular, MS_REC is the only flag that is supposed to be allowed to
be specified along with a mount propagation flag, but in practice it was
only triggering an error some of the time, and CI wasn't one of those
times.

The added test mounts the root filesystem as an overlay and then runs
buildah as a rootless user on top of that, which is more comparable to a
root-on-composefs configuration, which manages to trigger the error.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-03-25 17:47:46 -04:00
Paul Holzinger a1715cc863
replace deprecated selinux/label calls
These functions were removed in github.com/opencontainers/selinux
v1.12.0.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-03-24 13:32:00 +01:00
Nalin Dahyabhai 699c84d40b chroot createPlatformContainer: use MS_REMOUNT
When setting mount propagation on the root mount before unmounting it,
use MS_REBIND, since we know it's already a bind mount, and we actually
want to affect the extant bind mount instead of creating another right
over it. Otherwise, we might as well have not bothered.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-02-19 13:29:20 -05:00
flouthoc aadfc5cf30
unit_test: use Parallel test where possible
Add `t.Parallel()` to unit tests whereever its possible without race.

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
2025-02-04 11:07:15 -08:00
Eng Zer Jun 59024c41ad
refactor: replace golang.org/x/exp with stdlib
These experimental packages are now available in the Go standard
library since Go 1.21:

	1. golang.org/x/exp/slices -> slices [1]
	2. golang.org/x/exp/maps -> maps [2]

[1]: https://go.dev/doc/go1.21#slices
[2]: https://go.dev/doc/go1.21#maps

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2025-01-24 00:27:55 +08:00
Nalin Dahyabhai 62709d718e Fix an error message in the chroot unit test
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-12-13 15:16:16 -05:00
Nalin Dahyabhai 4adca286fe chroot: on Linux, try to pivot_root before falling back to chroot
Unless --no-pivot or the equivalent API flag is set, try to pivot_root()
to enter the rootfs during Run().  Fall back to using chroot() as before
if that fails for any reason.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-12-12 14:33:12 -05:00
openshift-merge-bot[bot] 83b5dcf219
Merge pull request #5753 from nalind/newline-error
chroot: add newlines at the end of printed error messages
2024-10-23 13:55:54 +00:00
Kir Kolyshkin 95f2e1020f vendor: switch to moby/sys/capability
github.com/moby/sys/capability is a fork of the (no longer maintained)
github.com/syndtr/gocapability package.

For the list of changes since the fork took place, see
https://github.com/moby/sys/blob/main/capability/CHANGELOG.md

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-09-30 13:19:03 -07:00
Kir Kolyshkin 37a6bde534 Don't set ambient capabilities
Ambient capabilities can't be raised without inheritable ones, and since we
don't raise inheritable, we should not raise ambient either.

This went unnoticed because of a bug in syndtr/gocapability which is
only fixed in its fork (see the next commit).

Amends commit e7e55c988.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-09-30 13:19:03 -07:00
Nalin Dahyabhai 2ff91b8b08 chroot: add newlines at the end of printed error messages
If we print an error message to stderr, end it with a newline.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-09-24 17:11:16 -04:00
Nalin Dahyabhai 0ad0ffaad9 Fix some govet linter warnings
govet warned about some places where we were passing something other
than a literal string to a function that took format specifiers.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 16:09:24 -04:00
Nalin Dahyabhai 8ae99121c1 CI: enable the gofumpt linter
Turn on the gofumpt linter.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 13:17:44 -04:00
Kir Kolyshkin fd3aba9917 *: use gofmt -s, add gofmt linter
1. Use gofmt to format the code:

	git ls-files \*.go | grep -Ev '/?vendor/' | xargs gofmt -w -s

2. Add gofmt to golangci-lint (it's not enabled by default).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-08-09 17:05:58 -07:00
Kir Kolyshkin 24da18800e *: fix build tags
This change is generated by `go1.23rc2 fix ./...`.

Had to use go1.23rc2, since all released go versions have a bug
preventing it from working with `go 1.22.0` in go.mod (opened
https://github.com/golang/go/issues/68825,
https://github.com/golang/go/issues/68824 for awareness).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-08-09 17:05:30 -07:00
Nalin Dahyabhai fdf1c75cd3 linters: unused arguments shouldn't have names
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-07 10:10:35 -04:00
Nalin Dahyabhai ae0d9d002e linters: don't name variables "cap"
Rename some variables so that we don't use a keyword as a variable name.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-07 10:10:35 -04:00
Jonah Bull 939a58b967
fix secret mounts for env vars when using chroot isolation
Before #5083, when running with chroot isolation ro mounts like secrets
from env vars would explicitly have the unix.MS_NOEXEC, unix.MS_NOSUID
and unix.MS_NODEV flags set when they were remounted. Now when running
with chroot isolation ro mounts like secrets from env vars are not
getting those same flags set and so the remount operation fails.
Specifically it looks like we are missing the unix.MS_NOSUID and
unix.MS_NODEV flags.

This change adds special handling for read-only mounts when we need to do
a remount to try to get the desired flags to stick. If we've requested
a read-only mount (unix.ST_RDONLY is set in requestFlags), then we add any
possibleImportantFlags that are set in fs.Flags to remountFlags so the remount
operation doesn't fail because they are missing. I've also added a test to
bud.bats that covers this case.

Signed-off-by: Jonah Bull <jonah.bull@elastic.co>
2024-05-25 15:49:51 -05:00
Giuseppe Scrivano 9dcd1cc9a6
chroot: use fileutils.(Le|E)xists
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-04-12 09:58:13 +02:00
Nalin Dahyabhai 4f0b619dd2 Use golang.org/x/exp/slices.Contains
... instead of github.com/containers/common/pkg/util.StringInSlice,
per linters.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-02-07 11:50:41 -05:00
Nalin Dahyabhai 04847f57f6 Set CONTAINERS_CONF in the chroot-mount-flags integration test
... in an attempt to try to get UID 0 in a user namespace to stop trying
to read files from root's home directory, where the permissions error is
treated as a hard failure.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-02-07 11:48:29 -05:00
Philip Dubé a42bfd0191 Replace map[K]bool with map[K]struct{} where it makes sense
Signed-off-by: Philip Dubé <philip@peerdb.io>
2024-01-05 15:58:43 +00:00
Nalin Dahyabhai 2a3a956cbb chroot.setupChrootBindMounts: pay more attention to flags
Pay better attention to dev/nodev/exec/noexec/suid/nosuid/ro/rw flags on
bind, overlay, and tmpfs mounts when any of them are specified.  Stop
quietly adding "nodev" when it isn't asked for.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2023-10-20 09:46:58 -04:00
Michal Biesek 5643a7fa8c
chroot: `setSeccomp` add support for `ArchPARISC(64)` and `ArchRISCV64`
Signed-off-by: Michal Biesek <michalbiesek@gmail.com>
2023-08-17 09:11:16 +02:00
Giuseppe Scrivano e8d11201a9
chroot: lock thread before setPdeathsig
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2023-05-16 20:07:57 +02:00
Chris Evich 46eea31588
Replace io/ioutil calls with os calls
In golang 1.19, `io/ioutil` is fully deprecated preventing Buildah from
compiling.  Replace all calls with equivalent calls from the `os`
package.

Signed-off-by: Chris Evich <cevich@redhat.com>
2022-12-06 14:29:32 -05:00
Giuseppe Scrivano ffb00243f1
chroot: fix mounting of ro bind mounts
a bind mount cannot be made RDONLY in the same mount operation as it
is created.  For that we need a second operation.

Closes: https://github.com/containers/buildah/issues/4203

[NO NEW TESTS NEEDED] it fails in Buildah in a container

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-11-01 21:20:15 +01:00
Daniel J Walsh 8d5d763213
Fix stutters
Podman adds an Error: to every error message. So starting an error
message with "error" ends up being reported to the user as

Error: error ...

This patch removes the stutter.

Also ioutil.ReadFile errors report the Path, so wrapping the err message
with the path causes a stutter.

Signed-off-by: Daniel J Walsh dwalsh@redhat.com

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-19 07:11:44 -04:00
Doug Rabson 9c147ab8a9 chroot: Fix cross build break
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:48 +01:00
Doug Rabson 309f714b5f chroot: Move isDevNull to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:48 +01:00
Doug Rabson 363bf9c5bc chroot: Fix setRlimit build on FreeBSD
On FreeBSD, members of the rlimit structure are signed, not unsigned.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:48 +01:00
Doug Rabson dc49c3cb4d chroot: Move parseRLimits and setRlimits to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson b64d814325 chroot: Fix runUsingChrootExecMain on FreeBSD
This adds no-op stubs for various things and adds an optional override
for creating the container chroot - on FreeBSD we use a jail to allow
setting the container hostname.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson 4d963eb5e1 chroot: Move runUsingChrootExecMain to run_common.go
Again, this breaks the FreeBSD build and this will be addressed by
refactoring in the next commit.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson 0a61e4b280 chroot: Factor out Linux-specific unshare options from runUsingChroot
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson 3b42b51524 chroot: Move runUsingChroot to run_common.go
This intentionally breaks the FreeBSD so that I can move the code
unmodified which will help with future merge conflicts. A subsequent
commit will resolve this by factoring out Linux-specific code.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson 350c509ecf chroot: Move RunUsingChroot and runUsingChrootMain to run_common.go
This leaves runUsingChrootSubprocOptions in the platform-specific file
since syscall.SysProcIDMap isn't available on FreeBSD.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson 188dcc3608 chroot: Factor out /dev/ptmx pty implementation
The ptmx device is fairly common and this code could be used on
platforms other than Linux.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Doug Rabson f9814601dd chroot: Add FreeBSD support for run with chroot isolation
This copies a large amount of code from run_linux.go. Later diffs in
this stack will factor out duplicate code where possible.

Terminal handling is implemented using the posix_openpt API. We could
use the Linux implementation which uses the /dev/ptmx but that is not
present on standard FreeBSD installs - its supplied by an optional
kernel module. Conversely, posix_openpt could be used for both platforms
but has a downside of requiring cgo so its probably better to use this
only on FreeBSD.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-01 13:50:47 +01:00
Eng Zer Jun 0c4b19ba83
test: use `T.TempDir` to create temporary test directory
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-08-20 21:13:27 +08:00
Nalin Dahyabhai bb149ea686 Use errors.Is() instead of os.Is{Not,}Exist
If errors for which os.IsExist() or os.IsNotExist() would have returned
true have been wrapped using fmt.Errorf()'s "%w" verb, os.IsExist() and
os.IsNotExist(), not having been retrofitted to use errors.Is(), will
return false.

Use errors.Is() to check if an error is an os.ErrExist or os.ErrNotExist
error instead of calling os.IsExist() or os.IsNotExist().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-07-26 15:36:58 -04:00
Doug Rabson ac4b39a220 Rename chroot/run.go to chroot/run_linux.go
This is a precursor to adding isolation=chroot support for
FreeBSD.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-25 11:28:51 +01:00