-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libcontainer/system: move userns utilities to separate package
Moving these utilities to a separate package, so that consumers of this package don't have to pull in the whole "system" package. Looking at uses of these utilities (outside of runc itself); `RunningInUserNS()` is used by [various external consumers][1], so adding a "Deprecated" alias for this. [1]: https://grep.app/search?current=2&q=.RunningInUserNS Signed-off-by: Sebastiaan van Stijn <[email protected]>
- Loading branch information
0 parents
commit ab29593
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package userns | ||
|
||
// RunningInUserNS detects whether we are currently running in a user namespace. | ||
// Originally copied from github.com/lxc/lxd/shared/util.go | ||
var RunningInUserNS = runningInUserNS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build gofuzz | ||
|
||
package userns | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/opencontainers/runc/libcontainer/user" | ||
) | ||
|
||
func FuzzUIDMap(data []byte) int { | ||
uidmap, _ := user.ParseIDMap(strings.NewReader(string(data))) | ||
_ = uidMapInUserNS(uidmap) | ||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package userns | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/opencontainers/runc/libcontainer/user" | ||
) | ||
|
||
var ( | ||
inUserNS bool | ||
nsOnce sync.Once | ||
) | ||
|
||
// runningInUserNS detects whether we are currently running in a user namespace. | ||
// Originally copied from github.com/lxc/lxd/shared/util.go | ||
func runningInUserNS() bool { | ||
nsOnce.Do(func() { | ||
uidmap, err := user.CurrentProcessUIDMap() | ||
if err != nil { | ||
// This kernel-provided file only exists if user namespaces are supported | ||
return | ||
} | ||
inUserNS = uidMapInUserNS(uidmap) | ||
}) | ||
return inUserNS | ||
} | ||
|
||
func uidMapInUserNS(uidmap []user.IDMap) bool { | ||
/* | ||
* We assume we are in the initial user namespace if we have a full | ||
* range - 4294967295 uids starting at uid 0. | ||
*/ | ||
if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build linux | ||
|
||
package userns | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/opencontainers/runc/libcontainer/user" | ||
) | ||
|
||
func TestUIDMapInUserNS(t *testing.T) { | ||
cases := []struct { | ||
s string | ||
expected bool | ||
}{ | ||
{ | ||
s: " 0 0 4294967295\n", | ||
expected: false, | ||
}, | ||
{ | ||
s: " 0 0 1\n", | ||
expected: true, | ||
}, | ||
{ | ||
s: " 0 1001 1\n 1 231072 65536\n", | ||
expected: true, | ||
}, | ||
{ | ||
// file exist but empty (the initial state when userns is created. see man 7 user_namespaces) | ||
s: "", | ||
expected: true, | ||
}, | ||
} | ||
for _, c := range cases { | ||
uidmap, err := user.ParseIDMap(strings.NewReader(c.s)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
actual := uidMapInUserNS(uidmap) | ||
if c.expected != actual { | ||
t.Fatalf("expected %v, got %v for %q", c.expected, actual, c.s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// +build !linux | ||
|
||
package userns | ||
|
||
import "github.com/opencontainers/runc/libcontainer/user" | ||
|
||
// runningInUserNS is a stub for non-Linux systems | ||
// Always returns false | ||
func runningInUserNS() bool { | ||
return false | ||
} | ||
|
||
// uidMapInUserNS is a stub for non-Linux systems | ||
// Always returns false | ||
func uidMapInUserNS(uidmap []user.IDMap) bool { | ||
return false | ||
} |