Skip to content

Commit

Permalink
backport of commit 20b347e (hashicorp#19315)
Browse files Browse the repository at this point in the history
Co-authored-by: miagilepner <mia.epner@hashicorp.com>
  • Loading branch information
1 parent 62eeda8 commit 6a73f37
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog/19311.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
server/config: Use file.Stat when checking file permissions when VAULT_ENABLE_FILE_PERMISSIONS_CHECK is enabled
```
18 changes: 14 additions & 4 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -465,9 +464,14 @@ func LoadConfig(path string) (*Config, error) {
return nil, errors.New("Error parsing the environment variable VAULT_ENABLE_FILE_PERMISSIONS_CHECK")
}
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()

if enableFilePermissionsCheck {
err = osutil.OwnerPermissionsMatch(path, 0, 0)
err = osutil.OwnerPermissionsMatchFile(f, 0, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -496,8 +500,14 @@ func CheckConfig(c *Config, e error) (*Config, error) {

// LoadConfigFile loads the configuration from the given file.
func LoadConfigFile(path string) (*Config, error) {
// Open the file
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
// Read the file
d, err := ioutil.ReadFile(path)
d, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand All @@ -518,7 +528,7 @@ func LoadConfigFile(path string) (*Config, error) {

if enableFilePermissionsCheck {
// check permissions of the config file
err = osutil.OwnerPermissionsMatch(path, 0, 0)
err = osutil.OwnerPermissionsMatchFile(f, 0, 0)
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions helper/osutil/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ func OwnerPermissionsMatch(path string, uid int, permissions int) error {

return nil
}

// OwnerPermissionsMatchFile checks if vault user is the owner and permissions are secure for the input file
func OwnerPermissionsMatchFile(file *os.File, uid int, permissions int) error {
info, err := file.Stat()
if err != nil {
return fmt.Errorf("file stat error on path %q: %w", file.Name(), err)
}
err = checkPathInfo(info, file.Name(), uid, permissions)
if err != nil {
return err
}

return nil
}
96 changes: 96 additions & 0 deletions helper/osutil/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -82,3 +83,98 @@ func TestCheckPathInfo(t *testing.T) {
}
}
}

// TestOwnerPermissionsMatchFile creates a file and verifies that the current user of the process is the owner of the
// file
func TestOwnerPermissionsMatchFile(t *testing.T) {
currentUser, err := user.Current()
if err != nil {
t.Fatal("failed to get current user", err)
}
uid, err := strconv.ParseInt(currentUser.Uid, 0, 64)
if err != nil {
t.Fatal("failed to convert uid", err)
}
dir := t.TempDir()
path := filepath.Join(dir, "foo")
f, err := os.Create(path)
if err != nil {
t.Fatal("failed to create test file", err)
}
defer f.Close()

info, err := os.Stat(path)
if err != nil {
t.Fatal("failed to stat test file", err)
}

if err := OwnerPermissionsMatchFile(f, int(uid), int(info.Mode())); err != nil {
t.Fatalf("expected no error but got %v", err)
}
}

// TestOwnerPermissionsMatchFile_OtherUser creates a file using the user that started the current process and verifies
// that a different user is not the owner of the file
func TestOwnerPermissionsMatchFile_OtherUser(t *testing.T) {
currentUser, err := user.Current()
if err != nil {
t.Fatal("failed to get current user", err)
}
uid, err := strconv.ParseInt(currentUser.Uid, 0, 64)
if err != nil {
t.Fatal("failed to convert uid", err)
}
dir := t.TempDir()
path := filepath.Join(dir, "foo")
f, err := os.Create(path)
if err != nil {
t.Fatal("failed to create test file", err)
}
defer f.Close()

info, err := os.Stat(path)
if err != nil {
t.Fatal("failed to stat test file", err)
}

if err := OwnerPermissionsMatchFile(f, int(uid)+1, int(info.Mode())); err == nil {
t.Fatalf("expected error but none")
}
}

// TestOwnerPermissionsMatchFile_Symlink creates a file and a symlink to that file. The test verifies that the current
// user of the process is the owner of the file
func TestOwnerPermissionsMatchFile_Symlink(t *testing.T) {
currentUser, err := user.Current()
if err != nil {
t.Fatal("failed to get current user", err)
}
uid, err := strconv.ParseInt(currentUser.Uid, 0, 64)
if err != nil {
t.Fatal("failed to convert uid", err)
}
dir := t.TempDir()
path := filepath.Join(dir, "foo")
f, err := os.Create(path)
if err != nil {
t.Fatal("failed to create test file", err)
}
defer f.Close()

symlink := filepath.Join(dir, "symlink")
err = os.Symlink(path, symlink)
if err != nil {
t.Fatal("failed to symlink file", err)
}
symlinkedFile, err := os.Open(symlink)
if err != nil {
t.Fatal("failed to open file", err)
}
info, err := os.Stat(symlink)
if err != nil {
t.Fatal("failed to stat test file", err)
}
if err := OwnerPermissionsMatchFile(symlinkedFile, int(uid), int(info.Mode())); err != nil {
t.Fatalf("expected no error but got %v", err)
}
}

0 comments on commit 6a73f37

Please sign in to comment.