mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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)
This commit is contained in:
parent
8b77a5b7ae
commit
b51c366bfc
1 changed files with 10 additions and 0 deletions
|
@ -3,10 +3,13 @@ package symlink
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxLoopCounter = 100
|
||||||
|
|
||||||
// FollowSymlink will follow an existing link and scope it to the root
|
// FollowSymlink will follow an existing link and scope it to the root
|
||||||
// path provided.
|
// path provided.
|
||||||
func FollowSymlinkInScope(link, root string) (string, error) {
|
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.Join(prev, p)
|
||||||
prev = filepath.Clean(prev)
|
prev = filepath.Clean(prev)
|
||||||
|
|
||||||
|
loopCounter := 0
|
||||||
for {
|
for {
|
||||||
|
loopCounter++
|
||||||
|
|
||||||
|
if loopCounter >= maxLoopCounter {
|
||||||
|
return "", fmt.Errorf("loopCounter reached MAX: %v", loopCounter)
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(prev, root) {
|
if !strings.HasPrefix(prev, root) {
|
||||||
// Don't resolve symlinks outside of root. For example,
|
// Don't resolve symlinks outside of root. For example,
|
||||||
// we don't have to check /home in the below.
|
// we don't have to check /home in the below.
|
||||||
|
|
Loading…
Add table
Reference in a new issue