소스 검색

Defend against infinite loop when following symlinks

ideally it should never reach it, but there was already multiple issues with infinite loop
at following symlinks. this fixes hanging unit tests

Docker-DCO-1.1-Signed-off-by: Lajos Papp <lajos.papp@sequenceiq.com> (github: lalyos)
lalyos 11 년 전
부모
커밋
b51c366bfc
1개의 변경된 파일10개의 추가작업 그리고 0개의 파일을 삭제
  1. 10 0
      pkg/symlink/fs.go

+ 10 - 0
pkg/symlink/fs.go

@@ -3,10 +3,13 @@ package symlink
 import (
 	"fmt"
 	"os"
+	"path"
 	"path/filepath"
 	"strings"
 )
 
+const maxLoopCounter = 100
+
 // FollowSymlink will follow an existing link and scope it to the root
 // path provided.
 func FollowSymlinkInScope(link, root string) (string, error) {
@@ -30,7 +33,14 @@ func FollowSymlinkInScope(link, root string) (string, error) {
 		prev = filepath.Join(prev, p)
 		prev = filepath.Clean(prev)
 
+		loopCounter := 0
 		for {
+			loopCounter++
+
+			if loopCounter >= maxLoopCounter {
+				return "", fmt.Errorf("loopCounter reached MAX: %v", loopCounter)
+			}
+
 			if !strings.HasPrefix(prev, root) {
 				// Don't resolve symlinks outside of root. For example,
 				// we don't have to check /home in the below.