Skip to content

Commit 6864912

Browse files
committed
Isolate the testing import in test code
testing_mocks_linux.go imports the testing package, and that import leaks into any binary built with securejoin. This causes issues in projects which check for that, such as Kubernetes (see kubernetes/kubernetes#127832 and its tests). This adds a level of indirection for hooks used to force testing behaviour in procfs_linux.go, so that the mocks can be defined only in _test.go files. This ensures that the testing package is only pulled in for tests. Signed-off-by: Stephen Kitt <skitt@redhat.com>
1 parent 4348fee commit 6864912

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

procfs_linux.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func clonePrivateProcMount() (_ *os.File, Err error) {
134134
// we can be sure there are no over-mounts and so if the root is valid then
135135
// we're golden. Otherwise, we have to deal with over-mounts.
136136
procfsHandle, err := openTree(nil, "/proc", unix.OPEN_TREE_CLONE)
137-
if err != nil || testingForcePrivateProcRootOpenTreeAtRecursive(procfsHandle) {
137+
if err != nil || hookForcePrivateProcRootOpenTreeAtRecursive(procfsHandle) {
138138
procfsHandle, err = openTree(nil, "/proc", unix.OPEN_TREE_CLONE|unix.AT_RECURSIVE)
139139
}
140140
if err != nil {
@@ -152,13 +152,13 @@ func clonePrivateProcMount() (_ *os.File, Err error) {
152152
}
153153

154154
func privateProcRoot() (*os.File, error) {
155-
if !hasNewMountApi() || testingForceGetProcRootUnsafe() {
155+
if !hasNewMountApi() || hookForceGetProcRootUnsafe() {
156156
return nil, fmt.Errorf("new mount api: %w", unix.ENOTSUP)
157157
}
158158
// Try to create a new procfs mount from scratch if we can. This ensures we
159159
// can get a procfs mount even if /proc is fake (for whatever reason).
160160
procRoot, err := newPrivateProcMount()
161-
if err != nil || testingForcePrivateProcRootOpenTree(procRoot) {
161+
if err != nil || hookForcePrivateProcRootOpenTree(procRoot) {
162162
// Try to clone /proc then...
163163
procRoot, err = clonePrivateProcMount()
164164
}
@@ -227,10 +227,10 @@ func procThreadSelf(procRoot *os.File, subpath string) (_ *os.File, _ procThread
227227

228228
// Figure out what prefix we want to use.
229229
threadSelf := "thread-self/"
230-
if !hasProcThreadSelf() || testingForceProcSelfTask() {
230+
if !hasProcThreadSelf() || hookForceProcSelfTask() {
231231
/// Pre-3.17 kernels don't have /proc/thread-self, so do it manually.
232232
threadSelf = "self/task/" + strconv.Itoa(unix.Gettid()) + "/"
233-
if _, err := fstatatFile(procRoot, threadSelf, unix.AT_SYMLINK_NOFOLLOW); err != nil || testingForceProcSelf() {
233+
if _, err := fstatatFile(procRoot, threadSelf, unix.AT_SYMLINK_NOFOLLOW); err != nil || hookForceProcSelf() {
234234
// In this case, we running in a pid namespace that doesn't match
235235
// the /proc mount we have. This can happen inside runc.
236236
//
@@ -424,3 +424,18 @@ func checkProcSelfFdPath(path string, file *os.File) error {
424424
}
425425
return nil
426426
}
427+
428+
// Test hooks
429+
var hookForcePrivateProcRootOpenTree = func(_ *os.File) bool {
430+
return false
431+
}
432+
433+
var hookForcePrivateProcRootOpenTreeAtRecursive = hookForcePrivateProcRootOpenTree
434+
435+
var hookForceGetProcRootUnsafe = func() bool {
436+
return false
437+
}
438+
439+
var hookForceProcSelfTask = hookForceGetProcRootUnsafe
440+
441+
var hookForceProcSelf = hookForceGetProcRootUnsafe

testing_mocks_linux.go testing_mocks_linux_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ func testingForceProcSelf() bool {
6666
return testing.Testing() && testingForceProcThreadSelf != nil &&
6767
*testingForceProcThreadSelf >= forceProcSelf
6868
}
69+
70+
func init() {
71+
hookForceGetProcRootUnsafe = testingForceGetProcRootUnsafe
72+
hookForcePrivateProcRootOpenTree = testingForcePrivateProcRootOpenTree
73+
hookForcePrivateProcRootOpenTreeAtRecursive = testingForcePrivateProcRootOpenTreeAtRecursive
74+
hookForceProcSelf = testingForceProcSelf
75+
hookForceProcSelfTask = testingForceProcSelfTask
76+
}

0 commit comments

Comments
 (0)