containerd/internal/cri/server
Wei Fu ee0ed75d64 internal/cri: simplify netns setup with pinned userns
Motivation:

For pod-level user namespaces, it's impossible to force the container runtime
to join an existing network namespace after creating a new user namespace.

According to the capabilities section in [user_namespaces(7)][1], a network
namespace created by containerd is owned by the root user namespace. When the
container runtime (like runc or crun) creates a new user namespace, it becomes
a child of the root user namespace. Processes within this child user namespace
are not permitted to access resources owned by the parent user namespace.

If the network namespace is not owned by the new user namespace, the container
runtime will fail to mount /sys due to the [sysfs: Restrict mounting sysfs][2]
patch.

Referencing the [cap_capable][3] function in Linux, a process can access a
resource if:

* The resource is owned by the process's user namespace, and the process has
the required capability.

* The resource is owned by a child of the process's user namespace, and the
owner's user namespace was created by the process's UID.

In the context of pod-level user namespaces, the CRI plugin delegates the
creation of the network namespace to the container runtime when running the
pause container. After the pause container is initialized, the CRI plugin pins
the pause container's network namespace into `/run/netns` and then executes
the `CNI_ADD` command over it.

However, if the pause container is terminated during the pinning process, the
CRI plugin might encounter a PID cycle, leading to the `CNI_ADD` command
operating on an incorrect network namespace.

Moreover, rolling back the `RunPodSandbox` API is complex due to the delegation
of network namespace creation. As highlighted in issue #10363, the CRI plugin
can lose IP information after a containerd restart, making it challenging to
maintain robustness in the RunPodSandbox API.

Solution:

Allow containerd to create a new user namespace and then create the network
namespace within that user namespace. This way, the CRI plugin can force the
container runtime to join both the user namespace and the network namespace.
Since the network namespace is owned by the newly created user namespace,
the container runtime will have the necessary permissions to mount `/sys` on
the container's root filesystem. As a result, delegation of network namespace
creation is no longer needed.

NOTE:

* The CRI plugin does not need to pin the newly created user namespace as it
does with the network namespace, because the kernel allows retrieving a user
namespace reference via [ioctl_ns(2)][4]. As a result, the podsandbox
implementation can obtain the user namespace using the `netnsPath` parameter.

[1]: <https://man7.org/linux/man-pages/man7/user_namespaces.7.html>
[2]: <7dc5dbc879>
[3]: <2c85ebc57b/security/commoncap.c (L65)>
[4]: <https://man7.org/linux/man-pages/man2/ioctl_ns.2.html>

Signed-off-by: Wei Fu <fuweid89@gmail.com>
2024-09-11 07:21:43 +08:00
..
events Make api a Go sub-module 2024-05-02 11:03:00 -07:00
images Remove extra span created in the instrumented service layer 2024-08-05 02:34:27 +00:00
podsandbox internal/cri: simplify netns setup with pinned userns 2024-09-11 07:21:43 +08:00
blockio_linux.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
blockio_stub.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
cni_conf_syncer.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_attach.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_checkpoint_linux.go use format string when using printf like commands 2024-08-14 17:04:53 +05:30
container_checkpoint.go Wire through CRI ContainerCheckpoint RPC 2024-03-07 17:34:07 +00:00
container_create_linux_test.go CRI: remove disable_cgroup 2024-08-15 06:08:30 +09:00
container_create_linux.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_create_other_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_create_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_create_test.go remove uses of platforms.Platform alias 2024-05-28 14:56:30 +02:00
container_create_windows_test.go KEP-3857: Recursive Read-only (RRO) mounts 2024-02-19 00:42:50 +09:00
container_create_windows.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_create.go CRI: remove disable_cgroup 2024-08-15 06:08:30 +09:00
container_events.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_exec.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_execsync_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_execsync.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_list_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_list.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_log_reopen.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_remove_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_remove.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_start_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_start.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_stats_list_test.go Cumulative stats can't decrease 2024-08-28 23:23:30 +00:00
container_stats_list.go Cumulative stats can't decrease 2024-08-28 23:23:30 +00:00
container_stats.go cri: get pid count from container metrics 2024-07-01 15:34:47 +00:00
container_status_linux_test.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_status_linux.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_status_other.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_status_test.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_status_windows.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_status.go KEP-3619: Fine grained SupplementalGroups control 2024-06-12 09:10:40 +09:00
container_stop_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_stop.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
container_update_resources_linux_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_update_resources_linux.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_update_resources_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_update_resources_windows.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
container_update_resources.go cmd: don't alias context package, and use cliContext for cli.Context 2024-06-20 02:15:13 +02:00
events.go Merge pull request #10201 from abel-von/retry-remote-sandbox-wait 2024-05-29 00:00:29 +00:00
fuzz.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
helpers_linux.go pkg/seutil: move to internal/cri 2024-03-20 11:11:24 +01:00
helpers_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
helpers_test.go Make api a Go sub-module 2024-05-02 11:03:00 -07:00
helpers_windows_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
helpers_windows.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
helpers.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
list_metric_descriptors.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
list_pod_sandbox_metrics.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
metrics.go Wire through CRI ContainerCheckpoint RPC 2024-03-07 17:34:07 +00:00
nri_linux.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
nri_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
nri.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
rdt_stub.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
rdt.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
restart.go cri: restart created container with correct io type 2024-05-13 17:42:58 +08:00
runtime_config_linux_test.go pkg/systemd: move to internal/cri 2024-03-20 11:05:25 +01:00
runtime_config_linux.go Make api a Go sub-module 2024-05-02 11:03:00 -07:00
runtime_config_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
runtime_config.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_list_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_list.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_portforward_linux.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_portforward_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_portforward_windows.go Account for ipv4 vs ipv6 localhost 2024-04-19 11:30:49 -07:00
sandbox_portforward.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_remove.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
sandbox_run_linux.go internal/cri: simplify netns setup with pinned userns 2024-09-11 07:21:43 +08:00
sandbox_run_other.go internal/cri: simplify netns setup with pinned userns 2024-09-11 07:21:43 +08:00
sandbox_run_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_run_windows.go internal/cri: simplify netns setup with pinned userns 2024-09-11 07:21:43 +08:00
sandbox_run.go internal/cri: simplify netns setup with pinned userns 2024-09-11 07:21:43 +08:00
sandbox_service.go remove uses of platforms.Platform alias 2024-05-28 14:56:30 +02:00
sandbox_stats_linux.go cri: get pid count from container metrics 2024-07-01 15:34:47 +00:00
sandbox_stats_list.go cri: optimize ListPodSandboxStats with parallelism 2024-07-12 09:02:33 +00:00
sandbox_stats_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_stats_windows_test.go Move protobuf package under pkg 2024-05-02 10:52:03 -07:00
sandbox_stats_windows.go Make api a Go sub-module 2024-05-02 11:03:00 -07:00
sandbox_stats.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_status_test.go cri/server: Add userns tests in PodSandboxStatus 2024-02-07 17:06:01 +01:00
sandbox_status.go sandbox: add methods to sandboxService 2024-02-26 10:10:11 +08:00
sandbox_stop_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
sandbox_stop.go Add spans to CRI runtime service and related client methods 2024-08-05 02:34:20 +00:00
service_linux.go migrate to github.com/moby/sys/userns 2024-08-08 12:48:54 +02:00
service_other.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
service_test.go sandbox: add update api for controller 2024-06-14 02:31:51 +00:00
service_windows.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
service.go KEP-3619: implement features.supplemental_groups_policy in RuntimeStatus 2024-07-23 01:01:36 +09:00
status_test.go Make api a Go sub-module 2024-05-02 11:03:00 -07:00
status.go KEP-3619: implement features.supplemental_groups_policy in RuntimeStatus 2024-07-23 01:01:36 +09:00
streaming.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
test_config.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
update_runtime_config_test.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
update_runtime_config.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00
version.go Move CRI from pkg/ to internal/ 2024-02-02 10:12:08 -08:00