Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/contributors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ users:
OdysseasKalaitsidis:
name: Odysseas Kalaitsidis
email: odysseaskalaitsides@gmail.com
tagadearpit:
name: Arpit Tagade
email: 143588157+tagadearpit@users.noreply.github.com
11 changes: 8 additions & 3 deletions cmd/urunc/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
)
Expand Down Expand Up @@ -62,11 +63,15 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
return ErrEmptyContainerID
}
rootDir := cmd.String("root")
containerDir := filepath.Join(rootDir, containerID)
e := os.RemoveAll(containerDir)
containerDir, e := securejoin.SecureJoin(rootDir, containerID)
if e != nil {
return fmt.Errorf("failed to resolve container directory: %w", e)
}
e = os.RemoveAll(containerDir)
if e != nil {
logrus.Errorf("remove %s: %v", containerDir, e)
}

if cmd.Bool("force") {
return nil
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/unikontainers/path_security_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package unikontainers

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
)

func TestGetRejectsContainerDirSymlink(t *testing.T) {
rootDir := t.TempDir()
outsideDir := t.TempDir()
bundleDir := t.TempDir()

spec := &specs.Spec{
Version: "1.0.2",
Linux: &specs.Linux{},
}
specData, err := json.Marshal(spec)
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(bundleDir, configFilename), specData, 0o600))

state := &specs.State{
Version: "1.0.2",
ID: "victim",
Bundle: bundleDir,
Annotations: map[string]string{
annotType: "linux",
annotHypervisor: "qemu",
},
}
stateData, err := json.Marshal(state)
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(outsideDir, stateFilename), stateData, 0o600))

require.NoError(t, os.Symlink(outsideDir, filepath.Join(rootDir, state.ID)))

_, err = Get(state.ID, rootDir)
require.ErrorIs(t, err, os.ErrNotExist)
}
11 changes: 9 additions & 2 deletions pkg/unikontainers/unikontainers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sync"
"syscall"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/urunc-dev/urunc/pkg/network"
"github.com/urunc-dev/urunc/pkg/unikontainers/hypervisors"
"github.com/urunc-dev/urunc/pkg/unikontainers/types"
Expand Down Expand Up @@ -93,7 +94,10 @@ func New(bundlePath string, containerID string, rootDir string, cfg *UruncConfig
confMap := config.Map()

maps.Copy(confMap, cfg.Map())
containerDir := filepath.Join(rootDir, containerID)
containerDir, err := securejoin.SecureJoin(rootDir, containerID)
if err != nil {
return nil, fmt.Errorf("failed to resolve container directory: %w", err)
}
state := &specs.State{
Version: spec.Version,
ID: containerID,
Expand All @@ -114,7 +118,10 @@ func New(bundlePath string, containerID string, rootDir string, cfg *UruncConfig
// Get retrieves unikernel data from disk to create a Unikontainer object
func Get(containerID string, rootDir string) (*Unikontainer, error) {
u := &Unikontainer{}
containerDir := filepath.Join(rootDir, containerID)
containerDir, err := securejoin.SecureJoin(rootDir, containerID)
if err != nil {
return nil, fmt.Errorf("failed to resolve container directory: %w", err)
}
stateFilePath := filepath.Join(containerDir, stateFilename)
state, err := loadUnikontainerState(stateFilePath)
if err != nil {
Expand Down