Skip to content

Commit 2e13d39

Browse files
committed
pkg/process: Only use idmap mounts if runc supports it
runc, as mandated by the runtime-spec, ignores unknown fields in the config.json. This is unfortunate for cases where we _must_ enable that feature or fail. For example, if we want to start a container with user namespaces and volumes, using the uidMappings/gidMappings field is needed so the UID/GIDs in the volume don't end up with garbage. However, if we don't fail when runc will ignore these fields (because they are unknown to runc), we will just start a container without using the mappings and the UID/GIDs the container will persist to volumes the hostUID/GID, that can change if the container is re-scheduled by Kubernetes. This will end up in volumes having "garbage" and unmapped UIDs that the container can no longer change. So, let's avoid this entirely by just checking that runc supports idmap mounts if the container we are about to create needs them. Please note that the "runc features" subcommand is only run when we are using idmap mounts. If idmap mounts are not used, the subcommand is not run and therefore this should not affect containers that don't use idmap mounts in any way. Signed-off-by: Rodrigo Campos <[email protected]>
1 parent fce1b95 commit 2e13d39

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

integration/pod_userns_linux_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package integration
1818

1919
import (
20+
"context"
21+
"errors"
2022
"fmt"
2123
"os"
2224
"os/user"
@@ -27,6 +29,7 @@ import (
2729
"time"
2830

2931
"github.com/containerd/containerd/integration/images"
32+
runc "github.com/containerd/go-runc"
3033
"github.com/stretchr/testify/assert"
3134
"github.com/stretchr/testify/require"
3235
exec "golang.org/x/sys/execabs"
@@ -234,6 +237,9 @@ func TestPodUserNS(t *testing.T) {
234237
if test.hostVolumes && !supportsIDMap(volumeHostPath) {
235238
t.Skipf("ID mappings are not supported host volume filesystem: %v", volumeHostPath)
236239
}
240+
if err := supportsRuncIDMap(); err != nil {
241+
t.Skipf("OCI runtime doesn't support idmap mounts: %v", err)
242+
}
237243

238244
testPodLogDir := t.TempDir()
239245
sandboxOpts := append(test.sandboxOpts, WithPodLogDirectory(testPodLogDir))
@@ -297,3 +303,22 @@ func TestPodUserNS(t *testing.T) {
297303
})
298304
}
299305
}
306+
307+
func supportsRuncIDMap() error {
308+
var r runc.Runc
309+
features, err := r.Features(context.Background())
310+
if err != nil {
311+
// If the features command is not implemented, then runc is too old.
312+
return fmt.Errorf("features command failed: %w", err)
313+
}
314+
315+
if features.Linux.MountExtensions == nil || features.Linux.MountExtensions.IDMap == nil {
316+
return errors.New("missing `mountExtensions.idmap` entry in `features` command")
317+
318+
}
319+
if enabled := features.Linux.MountExtensions.IDMap.Enabled; enabled == nil || !*enabled {
320+
return errors.New("idmap mounts not supported")
321+
}
322+
323+
return nil
324+
}

pkg/process/init.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package process
2121
import (
2222
"context"
2323
"encoding/json"
24+
"errors"
2425
"fmt"
2526
"io"
2627
"os"
@@ -140,6 +141,13 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
140141
if socket != nil {
141142
opts.ConsoleSocket = socket
142143
}
144+
145+
// runc ignores silently features it doesn't know about, so for things that this is
146+
// problematic let's check if this runc version supports them.
147+
if err := p.validateRuncFeatures(ctx, r.Bundle); err != nil {
148+
return fmt.Errorf("failed to detect OCI runtime features: %w", err)
149+
}
150+
143151
if err := p.runtime.Create(ctx, r.ID, r.Bundle, opts); err != nil {
144152
return p.runtimeError(err, "OCI runtime create failed")
145153
}
@@ -173,6 +181,56 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
173181
return nil
174182
}
175183

184+
func (p *Init) validateRuncFeatures(ctx context.Context, bundle string) error {
185+
// TODO: We should remove the logic from here and rebase on #8509.
186+
// This way we can avoid the call to readConfig() here and the call to p.runtime.Features()
187+
// in validateIDMapMounts().
188+
// But that PR is not yet merged nor it is clear if it will be refactored.
189+
// Do this contained hack for now.
190+
spec, err := readConfig(bundle)
191+
if err != nil {
192+
return fmt.Errorf("failed to read config: %w", err)
193+
}
194+
195+
if err := p.validateIDMapMounts(ctx, spec); err != nil {
196+
return fmt.Errorf("OCI runtime doesn't support idmap mounts: %w", err)
197+
}
198+
199+
return nil
200+
}
201+
202+
func (p *Init) validateIDMapMounts(ctx context.Context, spec *specs.Spec) error {
203+
var used bool
204+
for _, m := range spec.Mounts {
205+
if m.UIDMappings != nil || m.GIDMappings != nil {
206+
used = true
207+
break
208+
}
209+
}
210+
211+
if !used {
212+
return nil
213+
}
214+
215+
// From here onwards, we require idmap mounts. So if we fail to check, we return an error.
216+
features, err := p.runtime.Features(ctx)
217+
if err != nil {
218+
// If the features command is not implemented, then runc is too old.
219+
return fmt.Errorf("features command failed: %w", err)
220+
221+
}
222+
223+
if features.Linux.MountExtensions == nil || features.Linux.MountExtensions.IDMap == nil {
224+
return errors.New("missing `mountExtensions.idmap` entry in `features` command")
225+
}
226+
227+
if enabled := features.Linux.MountExtensions.IDMap.Enabled; enabled == nil || !*enabled {
228+
return errors.New("idmap mounts not supported")
229+
}
230+
231+
return nil
232+
}
233+
176234
func (p *Init) openStdin(path string) error {
177235
sc, err := fifo.OpenFifo(context.Background(), path, unix.O_WRONLY|unix.O_NONBLOCK, 0)
178236
if err != nil {

pkg/process/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package process
2121
import (
2222
"context"
2323
"encoding/json"
24+
"errors"
2425
"fmt"
2526
"io"
2627
"os"
@@ -31,6 +32,7 @@ import (
3132

3233
"github.com/containerd/containerd/errdefs"
3334
runc "github.com/containerd/go-runc"
35+
specs "github.com/opencontainers/runtime-spec/specs-go"
3436
"golang.org/x/sys/unix"
3537
)
3638

@@ -39,6 +41,8 @@ const (
3941
RuncRoot = "/run/containerd/runc"
4042
// InitPidFile name of the file that contains the init pid
4143
InitPidFile = "init.pid"
44+
// configFile is the name of the runc config file
45+
configFile = "config.json"
4246
)
4347

4448
// safePid is a thread safe wrapper for pid.
@@ -184,3 +188,23 @@ func stateName(v interface{}) string {
184188
}
185189
panic(fmt.Errorf("invalid state %v", v))
186190
}
191+
192+
func readConfig(path string) (spec *specs.Spec, err error) {
193+
cfg := filepath.Join(path, configFile)
194+
f, err := os.Open(cfg)
195+
if err != nil {
196+
if os.IsNotExist(err) {
197+
return nil, fmt.Errorf("JSON specification file %s not found", cfg)
198+
}
199+
return nil, err
200+
}
201+
defer f.Close()
202+
203+
if err = json.NewDecoder(f).Decode(&spec); err != nil {
204+
return nil, fmt.Errorf("failed to parse config: %w", err)
205+
}
206+
if spec == nil {
207+
return nil, errors.New("config cannot be null")
208+
}
209+
return spec, nil
210+
}

0 commit comments

Comments
 (0)