Skip to content
Closed
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
14 changes: 11 additions & 3 deletions libnetwork/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func netavarkBackendFromConf(store storage.Store, conf *config.Config, syslog bo

// We cannot use the runroot for rootful since the network namespace is shared for all
// libpod instances they also have to share the same ipam db.
// For rootless we have our own network namespace per libpod instances,
// For rootless users we have our own network namespace per libpod instances,
// so this is not a problem there.
runDir := netavarkRunDir
if unshare.IsRootless() {
if isRootlessUser() {
runDir = filepath.Join(store.RunRoot(), "networks")
}

Expand Down Expand Up @@ -141,8 +141,16 @@ func defaultNetworkBackend(store storage.Store, conf *config.Config) (backend ty
// use "/etc/containers/networks" and for rootless "$graphroot/networks". We cannot
// use the graphroot for rootful since the network namespace is shared for all
// libpod instances.
func isRootlessUser() bool {
return rootlessModeForUser(unshare.IsRootless(), unshare.GetRootlessUID())
}

func rootlessModeForUser(isRootless bool, rootlessUID int) bool {
return isRootless && rootlessUID > 0
}

func getDefaultNetavarkConfigDir(store storage.Store) string {
if !unshare.IsRootless() {
if !isRootlessUser() {
return netavarkConfigDir
}
return filepath.Join(store.GraphRoot(), "networks")
Expand Down
3 changes: 1 addition & 2 deletions libnetwork/network/interface_cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/containers/common/pkg/machine"
"github.com/containers/storage"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/unshare"
)

const (
Expand Down Expand Up @@ -42,7 +41,7 @@ func getCniInterface(conf *config.Config) (types.ContainerNetwork, error) {
}

func getDefaultCNIConfigDir() (string, error) {
if !unshare.IsRootless() {
if !isRootlessUser() {
return cniConfigDir, nil
}

Expand Down
45 changes: 45 additions & 0 deletions libnetwork/network/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build linux || freebsd

package network

import "testing"

func TestRootlessModeForUser(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
isRootless bool
rootlessUID int
expect bool
}{
{
name: "rootless user",
isRootless: true,
rootlessUID: 1000,
expect: true,
},
{
name: "root in nested user namespace",
isRootless: true,
rootlessUID: 0,
expect: false,
},
{
name: "rootful",
isRootless: false,
rootlessUID: 0,
expect: false,
},
}

for _, tcl := range testCases {
tc := tcl
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := rootlessModeForUser(tc.isRootless, tc.rootlessUID); got != tc.expect {
t.Fatalf("expected %v, got %v", tc.expect, got)
}
})
}
}
Loading