Skip to content

Commit

Permalink
Prevent escaping from /proc via symlinks
Browse files Browse the repository at this point in the history
This maybe could be more robustly solved via chroot
  • Loading branch information
hfinucane committed Jul 12, 2015
1 parent f210d62 commit 1fee38c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,18 @@ func vetPath(path string) (string, error) {
if strings.Contains(path, "..") {
return "", errors.New("directory traversal attempt detected")
}
return filepath.Join("/proc", path), nil

finalPath := filepath.Join("/proc", path)
cleanedFinalPath, err := filepath.EvalSymlinks(finalPath)
if err != nil {
return "", err
}

if cleanedFinalPath == "/proc" || strings.HasPrefix(cleanedFinalPath, "/proc/") {
return cleanedFinalPath, nil
}

return "", errors.New(fmt.Sprintf("Symlink traversal attempt detected from ", cleanedFinalPath))
}

func readProcPath(path string) (rval *ProcResult) {
Expand Down
13 changes: 13 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"os"
"testing"
)

Expand Down Expand Up @@ -56,3 +58,14 @@ func TestNoTraversal(t *testing.T) {
t.Errorf("Expected no contents, got %v", proc_result)
}
}

func TestNoSymlinkTraversal(t *testing.T) {
proc_result := readProcPath(fmt.Sprintf("/self/%d/tasks/cwd/main_test.go", os.Getpid()))

if proc_result.Err == "" {
t.Errorf("Expected an error, got %v", proc_result)
}
if proc_result.Contents != nil {
t.Errorf("Expected no contents, got %v", proc_result)
}
}

0 comments on commit 1fee38c

Please sign in to comment.