Commit Graph
58 Commits
Author SHA1 Message Date
Wei Fu a8fc3a0172 pkg/oci: resolve rootfs symlinks for user lookup
Some rootfs may use symlinks for /etc/passwd and /etc/group. For example,
NixOS uses absolute symlinks to files in /nix/store. os.Root is not used
because it rejects absolute symlinks.

The previous code only handled a symlink on the final path. Add a helper to
resolve symlinks in every path component with fs.ReadLinkFS. Absolute symlinks
are resolved from the rootfs, and the final path is still opened with fs.FS.

When tracing a path through symbolic links, the maximum number of links that
can be followed is [40][1].

[1]: <https://man7.org/linux/man-pages/man7/path_resolution.7.html>

Signed-off-by: Wei Fu <fuweid89@gmail.com>
2026-08-19 12:10:46 -04:00
Sebastiaan van Stijn 7a7aebfcbf pkg/oci: update TestOpenUserFileCapsReads to use newlined data
This test verifies the maximum file-size constraints that were added in
[containerd7b05ec4]. However, github.com/moby/sys@v0.4.1 adds similar
constraints, including a constraint on line-length (1M): [moby/sys@2c56c3d]
that may hit before the file-size limit is reached if the data does not
contain newlines.

This patch updates the test to use data that includes newlines to make
sure it's testing the file-size constraints, not line-limit constraints.

[containerd7b05ec4]: https://github.com/containerd/containerd/commit/7b05ec421d0a07b33964c74145b6bf5dff58f476
[moby/sys@2c56c3d]: https://github.com/moby/sys/commit/2c56c3d5d089bfa66d01b6c0bb69428e9d5d18c5

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-06-26 09:57:28 +02:00
Derek McGowan 53f4271fd9 Merge pull request #13661 from akerouanton/default-unix-spec
oci: use path.Join to fill CgroupsPath
2026-06-25 14:18:19 +00:00
Albin Kerouanton 51e3a8f4ab oci: use path.Join to fill CgroupsPath
`populateDefaultUnixSpec` uses `filepath.Join` to generate the default
`CgroupsPath`. On Windows, this produces invalid paths as path elems are
joined with backslash. Switch to `path.Join` instead.

Signed-off-by: Albin Kerouanton <albin.kerouanton@docker.com>
2026-06-24 10:20:54 +02:00
Chris Henzie 5a91c99584 Merge commit from fork 2026-06-15 21:25:17 -07:00
Sebastiaan van Stijn 633a5be1c9 pkg/oci: WithUser: remove redundant isErrRange utility
`strconv.NumError` implements `Unwrap` (see https://go.dev/cl/194563),
so there's no need to manually assert the type and unwrap.

Updates 9439355c2b

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-05-26 10:14:17 +02:00
LEI WANG 9439355c2b oci: return explicit error for out-of-range USER values
Detect strconv.ErrRange and validate uid/gid bounds to avoid falling back to username/group lookups.

Signed-off-by: LEI WANG <ssst0n3@gmail.com>
2026-05-19 11:36:54 -07:00
Chris Henzie 7b05ec421d Bound user-database file reads in openUserFile
openUserFile now stats the opened file, refuses anything that is not a
regular file, and wraps the returned fs.File so reads are capped at
maxUserFileBytes (10 MiB). All callers of openUserFile read either
etc/passwd or etc/group; both are regular files on real systems, well
under the cap.

The cap and the regular-file check together bound parser memory use
when reading user-database files of unexpected shape or size.

Adds tests for the cap and for the non-regular file rejection. The cap
test covers three boundary points: a small pad (trailing entry parsed),
a pad placing the entry's last byte exactly on the cap (still parsed),
and a pad past the cap (read returns an "exceeds" error).

Assisted-by: Antigravity
Signed-off-by: Chris Henzie <chrishenzie@gmail.com>
2026-05-16 01:04:01 +00:00
Kir Kolyshkin 899dee1f59 pkg: remove unused nolint annotations
Commit f9c80be1b removed varcheck linter, but some nolint:varcheck
annotations are still there, resulting in golangci-lint warning:

> WARN [runner/nolint_filter] Found unknown linters in //nolint directives: varcheck

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-05-12 11:02:51 -07:00
Derek McGowan c61c4e8dab pkg/oci: update fs error handling to use errors.Is
The `os.IsNotExist` documentation is clear that it should only be used to
check the error returned from calls within the os package, calls via
`fs.FS` should use `errors.Is`.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2026-04-06 22:05:47 -07:00
Rob Murray a5df2782d4 pkg/oci: remove darwin guards from user/group spec opts
Remove `runtime.GOOS == "darwin"` checks from WithUser,
WithAdditionalGIDs, and WithAppendAdditionalGroups. These guards
prevented supplementary group resolution when containerd runs on a
non-Linux host (e.g. macOS or Windows) that creates Linux containers.
The `s.Windows != nil` check for LCOW is retained.

Move user/group tests from spec_opts_linux_test.go to the new
cross-platform spec_opts_user_test.go so they run on all platforms.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2026-04-04 22:37:38 -07:00
Derek McGowan f384d2eb6c pkg/oci: update OCI with user to try mount for Darwin
Signed-off-by: Derek McGowan <derek@mcg.dev>
2026-04-04 22:37:37 -07:00
Derek McGowan c1eb9430af pkg/oci: update OCI spec generation to use fsview if available
Signed-off-by: Derek McGowan <derek@mcg.dev>
2026-04-04 22:37:37 -07:00
Maksym Pavlenko c586bec957 Merge pull request #13036 from cpuguy83/fix_data_digest
content: use descriptor digest algorithm instead of assuming sha256
2026-04-05 01:39:30 +00:00
Brian Goff 2a14c42545 pkg/oci: fix fake image digest computation in tests
newFakeImage used digest.NewDigestFromBytes which hex-encodes the raw
bytes rather than hashing them, producing a digest with an invalid
length. This was previously harmless since the embedded data path in
content.ReadBlob never matched, but now that the digest is validated
before use, the invalid digest causes an error.

Use digest.Canonical.FromBytes which correctly hashes the content.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2026-03-16 12:24:47 -07:00
Sebastiaan van Stijn 18c74abd56 *: modernize: slicescontains
go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest
    modernize -slicescontains -fix ./...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-03-15 18:24:14 +01:00
Sebastiaan van Stijn 5dd377a6ae pkg: modernize: mapsloop
go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest
    modernize -mapsloop -fix ./...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-03-15 15:00:12 +01:00
Sebastiaan van Stijn 3354221293 pkg: modernize: rangeint
go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest
    modernize -rangeint -fix ./...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-03-15 14:55:21 +01:00
Paulo Oliveira fc406dbc5c fix(oci): apply absolute symlink resolution to /etc/group
This is a follow-up to PR #12732.

As noted by @TheColorman, while the previous PR successfully resolved absolute symlinks pointing outside the mount root for /etc/passwd during user lookups, the same logic was missing for group lookups. This caused `openat etc/group: path escapes from parent` errors when /etc/group was also an absolute symlink (e.g., in NixOS environments).

This patch updates GIDFromFS, getSupplementalGroupsFromFS, and WithAppendAdditionalGroups to use the openUserFile helper, ensuring absolute symlinks are correctly re-anchored across all OCI user/group resolution paths. Includes unit test for validation.

Fixes #12683

Signed-off-by: Paulo Oliveira <paulo.hco47@gmail.com>
2026-02-21 22:43:21 -03:00
Paulo Oliveira 9bbb1309f0 test(oci): use fstest and mock fs for better symlink coverage
Signed-off-by: Paulo Oliveira <paulo.hco47@gmail.com>
2026-01-13 20:00:34 -03:00
Paulo Oliveira 85b5418ef5 fix(oci): handle absolute symlinks in rootfs user lookup
Go 1.24 introduced stricter checks for os.DirFS (via os.Root), which causes failures when /etc/passwd or /etc/group are absolute symlinks pointing outside the mount root (common in NixOS).

This patch introduces a helper that detects absolute symlinks and resolves them relative to the rootfs before opening, preventing the 'path escapes from parent' error.

Fixes #12683

Signed-off-by: Paulo Oliveira <paulo.hco47@gmail.com>
2026-01-08 08:44:30 -03:00
Youfu Zhang 01fd590a77 pkg/oci: add WithUmask for SpecOpts
opencontainers/runtime-spec#941 added umask field and released with v1.0.2.
This commit add the missing helper function for this field.

Signed-off-by: Youfu Zhang <zhangyoufu@gmail.com>
2025-12-24 09:23:07 +08:00
dependabot[bot] 13b1f43712 build(deps): bump github.com/containerd/cgroups/v3 from 3.1.0 to 3.1.1
Bumps [github.com/containerd/cgroups/v3](https://github.com/containerd/cgroups) from 3.1.0 to 3.1.1.
- [Release notes](https://github.com/containerd/cgroups/releases)
- [Commits](https://github.com/containerd/cgroups/compare/v3.1.0...v3.1.1)

---
updated-dependencies:
- dependency-name: github.com/containerd/cgroups/v3
  dependency-version: 3.1.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2025-11-19 17:48:52 +09:00
Samuel Karp ee1f94e4d1 ctr: allow rlimit-nofile override
Signed-off-by: Samuel Karp <samuelkarp@google.com>
2025-11-17 21:50:12 -08:00
Derek McGowan 069cbfe8f0 Use mount manager for temp mounts
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-09-29 17:08:37 -07:00
Enji Cooper f45716efed Clean up issues cited by usetesting package with golangci
This commit makes all of the recommended changes to use the `testing`
package helper functions instead of doing the equivalent longhand
versions of the same thing.

This change was needed in order to properly detect errors, as the code
would previously skip running `tenv` stating that it had been deprecated
in favor of `usetesting`.

Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
2025-09-07 14:07:40 -07:00
Derek McGowan cc2a52ca8f Update pkg/oci to use FS interface
Switch to use fs.FS interface over directly requiring path string.
Use os.OpenRoot over continuity RootPath.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-08-29 12:59:35 -07:00
Maksym Pavlenko c5372ac8b1 Merge pull request #11942 from thaJeztah/oci_test_WithParentCgroupDevices
pkg/oci: add basic test for WithParentCgroupDevices
2025-08-18 20:51:21 +00:00
Mike Brown 4bcd549f74 Merge pull request #11946 from thaJeztah/oci_privileged_no_var
pkg/oci: don't use var for WithPrivileged
2025-06-09 02:00:28 +00:00
Akihiro Suda cd0f2cc23a Merge pull request #11945 from thaJeztah/oci_no_vars_for_funcs
pkg/oci: don't use vars for WithAllKnownCapabilities, WithAllCurrentCapabilities
2025-06-08 06:19:37 +00:00
Maksym Pavlenko a6c250ed77 Merge pull request #11938 from thaJeztah/oci_linting
pkg/oci: fix minor linting issues
2025-06-06 22:21:33 +00:00
Phil Estes 0bf07cd5c6 Merge pull request #11939 from thaJeztah/oci_rm_go116
pkg/oci: remove compatibility code for go1.16 and older
2025-06-04 15:02:30 +00:00
Sebastiaan van Stijn 8de612020e pkg/oci: don't use var for WithPrivileged
This variable was introduced in 062c3a00ef,
which didn't describe it as intentional to be able to override the option.

Based on the above, I assume the use of a variable was purely convenience,
the there's no intent for packages to be able to override them, so this
patch changes these to be a regular function.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 16:35:13 +02:00
Sebastiaan van Stijn cf667aa7ee pkg/oci: add basic test for WithParentCgroupDevices
It was not used in the code, and had no coverage, so adding some
basic unit-tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 16:19:00 +02:00
Sebastiaan van Stijn d72c21450f pkg/oci: don't use vars for WithAllKnownCapabilities, WithAllCurrentCapabilities
These were introduced in c818a6b13d, refactored
in 808b223536 and bdd84abf05,
and moved in a2d1a8a865, but none provided
a motivation for using a variable / alias for these.

Based on the above, I assume the use of a variable was purely convenience,
the there's no intent for packages to be able to override them, so this
patch changes these to be a regular function.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 16:10:41 +02:00
Sebastiaan van Stijn ac3c3ad5da pkg/oci: cleanup some tests
- Use testify for asserting
- Fix various unhandled errors
- Use native t.TempDir() for temporary files

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 13:42:09 +02:00
Sebastiaan van Stijn 4de598d94b pkg/oci: remove compatibility code for go1.16 and older
These were added as part of b7f673790f,
to provide backward compatibility with go1.16, which we no longer
supports, so we can remove this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 12:52:40 +02:00
Sebastiaan van Stijn 17c632e785 pkg/oci: fix minor linting issues
- remove redundant aliases for imports
- rename variables that shadowed imports
- use errors.Is instead of straight error comparing

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-04 12:45:57 +02:00
Sebastiaan van Stijn b0052d94a6 pkg/oci: prevent panic for some platform-specific options
Some of these options are designed to be a no-op when used on a Spec
that doesn't match the platform for the option. However, if the given
plaform was not present, they would panic.

This patch:

- Adds an early-return for options that are only applied on a
  specific platform.
- Update the GoDoc for these functions to describe they're a
  no-op on other platforms.
- Adds some rudimentary unit-tests to verify their behavior.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-15 00:44:45 +02:00
Jin Dong c8effff1a8 Fix CI lint error
Signed-off-by: Jin Dong <djdongjin95@gmail.com>
2025-03-18 01:16:07 +00:00
Craig Ingram de1341c201 validate uid/gid 2025-03-11 14:44:38 +00:00
Derek McGowan bdc847f1eb Remove deprecated WithCDIDevices in oci spec opts
This function has been moved to prevent an unintended dependency on CDI.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-01-13 22:07:37 -08:00
Derek McGowan e20f7f4a24 Move CDI device spec out of the OCI package
The CDI device injection spec opt was mistakenly added to the OCI
package which brought in an unintended dependency on CDI and its
transitive dependencies.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-01-13 21:59:59 -08:00
Austin Vazquez bee64b2b93 Remove loop variable copies
Signed-off-by: Austin Vazquez <macedonv@amazon.com>
2024-12-23 23:14:49 -07:00
Sebastiaan van Stijn 9776047243 migrate to github.com/moby/sys/userns
Commit 8437c567d8 migrated the use of the
userns package to the github.com/moby/sys/user module.

After further discussion with maintainers, it was decided to move the
userns package to a separate module, as it has no direct relation with
"user" operations (other than having "user" in its name).

This patch migrates our code to use the new module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-08-08 12:48:54 +02:00
Kazuyoshi Kato 2ddd3db952 Merge pull request #9858 from w13915984028/fixdebug
Add file name to device type check failure message
2024-07-26 21:20:38 +00:00
Sebastiaan van Stijn 8437c567d8 pkg/userns: deprecate and migrate to github.com/moby/sys/user/userns
The userns package in libcontainer was integrated into the moby/sys/user
module at commit [3778ae603c706494fd1e2c2faf83b406e38d687d][1].

This patch deprecates the containerd fork of that package, and adds it as
an alias for the moby/sys/user/userns package.

[1]: https://github.com/opencontainers/runc/commit/3778ae603c706494fd1e2c2faf83b406e38d687d

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-26 09:47:50 +02:00
Danny Canter b41bb6df73 Avoid potential reallocs by pre-sizing some slices
There's a couple spots where we know exactly how large
the destination buffer should be, so pre-size these to
avoid any reallocs to a higher capacity.

Signed-off-by: Danny Canter <danny@dcantah.dev>
2024-07-19 13:05:49 -07:00
Derek McGowan 2ac2b9c909 Make api a Go sub-module
Allow the api to stay at the same v1 go package name and keep using a
1.x version number. This indicates the API is still at 1.x and allows
sharing proto types with containerd 1.6 and 1.7 releases.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-05-02 11:03:00 -07:00
Evan Lezar 1b62224181 Bump tags.cncf.io/container-device-interface to v0.7.1
This includes migrating from cdi.GetRegistry() to cdi.Configure() and
using top-level cdi Refresh and InjectDevices functions as applicable.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
2024-04-10 15:25:11 +02:00