CVE-2015-10024 (published 2023-01-07T13:15Z)

A vulnerability classified as critical was found in hoffie larasync. This vulnerability affects unknown code of the file repository/content/file_storage.go. The manipulation leads to path traversal. The name of the patch is 776bad422f4bd4930d09491711246bbeb1be9ba5. It is recommended to apply a patch to fix this issue. The identifier of this vulnerability is VDB-217612.

CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/hoffie/larasync
commit_details: repo_url: https://github.com/hoffie/larasync
author: Christian Hoffmann
author_date: 2015-01-20 23:21:00+01:00
msg: repository/content: fix potential (authenticated) path traversal (#195)
repository: repo_url: https://github.com/hoffie/larasync
repo_name: hoffie/larasync
description: larasync is an end-to-end encrypted, simple and fast self-hosted file synchronization solution.
repo_language: Go
owner: hoffie
file_changes: filename: file_storage.go
change_type: ModificationType.MODIFY
diff: @@ -1,9 +1,12 @@
 package content
 
 import (
+	"errors"
 	"io"
 	"os"
 	"path"
+	"path/filepath"
+	"strings"
 
 	"github.com/hoffie/larasync/helpers/atomic"
 )
@@ -14,6 +17,10 @@ const (
 	defaultDirPerms  = 0700
 )
 
+// ErrInvalidPath is returned if storage at a path not rooted at the FileStorage's
+// root path is attempted.
+var ErrInvalidPath = errors.New("invalid path")
+
 // FileStorage is the basic implementation of the Storage
 // implementation which stores the data into the file system.
 type FileStorage struct {
@@ -39,23 +46,40 @@ func (f *FileStorage) CreateDir() error {
 }
 
 // storagePathFor returns the storage path for the data entry.
-func (f *FileStorage) storagePathFor(contentID string) string {
-	return path.Join(f.path, contentID)
+func (f *FileStorage) storagePathFor(contentID string) (string, error) {
+	p := path.Join(f.path, contentID)
+	p = filepath.Clean(p)
+	root := f.path
+	/*if len(root) > 1 && root[len(root)-1] != filepath.Separator {
+		root += filepath.Separator
+	}*/
+	if !strings.HasPrefix(p, root) {
+		return "", ErrInvalidPath
+	}
+	return p, nil
 }
 
 // Get returns the file handle for the given contentID.
 // If there is no data stored for the Id it should return a
 // os.ErrNotExists error.
 func (f *FileStorage) Get(contentID string) (io.ReadCloser, error) {
-	if f.Exists(contentID) {
-		return os.Open(f.storagePathFor(contentID))
+	if !f.Exists(contentID) {
+		return nil, os.ErrNotExist
+	}
+	// FIXME TOCTU race
+	p, err := f.storagePathFor(contentID)
+	if err != nil {
+		return nil, err
 	}
-	return nil, os.ErrNotExist
+	return os.Open(p)
 }
 
 // Set sets the data of the given contentID in the blob storage.
 func (f *FileStorage) Set(contentID string, reader io.Reader) error {
-	blobStoragePath := f.storagePathFor(contentID)
+	blobStoragePath, err := f.storagePathFor(contentID)
+	if err != nil {
+		return err
+	}
 
 	writer, err := atomic.NewStandardWriter(blobStoragePath, defaultFilePerms)
 	if err != nil {
@@ -79,7 +103,12 @@ func (f *FileStorage) Set(contentID string, reader io.Reader) error {
 
 // Exists checks if the given entry is stored in the database.
 func (f *FileStorage) Exists(contentID string) bool {
-	_, err := os.Stat(f.storagePathFor(contentID))
+	p, err := f.storagePathFor(contentID)
+	if err != nil {
+		// FIXME maybe return error instead?
+		return false
+	}
+	_, err = os.Stat(p)
 	if err != nil {
 		return !os.IsNotExist(err)
 	}
@@ -88,5 +117,9 @@ func (f *FileStorage) Exists(contentID string) bool {
 
 // Delete removes the data with the given contentID from the store.
 func (f *FileStorage) Delete(contentID string) error {
-	return os.Remove(f.storagePathFor(contentID))
+	p, err := f.storagePathFor(contentID)
+	if err != nil {
+		return err
+	}
+	return os.Remove(p)
 }

programming_language: Go
filename: file_storage_test.go
change_type: ModificationType.MODIFY
diff: @@ -20,6 +20,8 @@ type FileStorageTests struct {
 
 var _ = Suite(&FileStorageTests{})
 
+var dangerousNames = []string{"..", "../xyz"}
+
 func (t *FileStorageTests) SetUpTest(c *C) {
 	t.dir = c.MkDir()
 	t.storage = NewFileStorage(t.dir)
@@ -43,6 +45,14 @@ func (t *FileStorageTests) setData() error {
 	return t.storage.Set(t.blobID(), t.testReader())
 }
 
+func (t *FileStorageTests) TestSetDangerousName(c *C) {
+	r := t.testReader()
+	for _, id := range dangerousNames {
+		err := t.storage.Set(id, r)
+		c.Assert(err, NotNil)
+	}
+}
+
 func (t *FileStorageTests) TestSet(c *C) {
 	err := t.setData()
 	c.Assert(err, IsNil)
@@ -72,6 +82,13 @@ func (t *FileStorageTests) TestGet(c *C) {
 	c.Assert(err, IsNil)
 }
 
+func (t *FileStorageTests) TestGetDangerousName(c *C) {
+	for _, id := range dangerousNames {
+		_, err := t.storage.Get(id)
+		c.Assert(err, NotNil)
+	}
+}
+
 func (t *FileStorageTests) TestGetData(c *C) {
 	t.setData()
 	file, _ := t.storage.Get(t.blobID())

programming_language: Go