Skip to content

Commit 86e6182

Browse files
committed
merge #32 into cyphar/filepath-securejoin:main
Stephen Kitt (1): Isolate the testing import in test code LGTMs: cyphar
2 parents 4348fee + 6864912 commit 86e6182

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)