Skip to content

Commit

Permalink
Merge pull request #23167 from mheon/fix_rhel_37948
Browse files Browse the repository at this point in the history
Ignore result of EvalSymlinks on ENOENT
  • Loading branch information
openshift-merge-bot[bot] authored Jul 11, 2024
2 parents fe65b58 + 830e550 commit 04bd415
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
16 changes: 6 additions & 10 deletions libpod/boltdb_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
package libpod

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -237,20 +235,18 @@ func readOnlyValidateConfig(bucket *bolt.Bucket, toCheck dbConfigValidation) (bo
// which is symlinked to /var/home.
if toCheck.isPath {
if dbValue != "" {
// Ignore ENOENT on both, on a fresh system some paths
// may not exist this early in Libpod init.
dbVal, err := filepath.EvalSymlinks(dbValue)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
checkedVal, err := evalSymlinksIfExists(dbValue)
if err != nil {
return false, fmt.Errorf("evaluating symlinks on DB %s path %q: %w", toCheck.name, dbValue, err)
}
dbValue = dbVal
dbValue = checkedVal
}
if ourValue != "" {
ourVal, err := filepath.EvalSymlinks(ourValue)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
checkedVal, err := evalSymlinksIfExists(ourValue)
if err != nil {
return false, fmt.Errorf("evaluating symlinks on configured %s path %q: %w", toCheck.name, ourValue, err)
}
ourValue = ourVal
ourValue = checkedVal
}
}

Expand Down
20 changes: 11 additions & 9 deletions libpod/sqlite_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"database/sql"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
goruntime "runtime"
Expand Down Expand Up @@ -379,21 +378,24 @@ func (s *SQLiteState) ValidateDBConfig(runtime *Runtime) (defErr error) {

checkField := func(fieldName, dbVal, ourVal string, isPath bool) error {
if isPath {
// Evaluate symlinks. Ignore ENOENT. No guarantee all
// directories exist this early in Libpod init.
// Tolerate symlinks when possible - most relevant for OStree systems
// and rootless containers, where we want to put containers in /home,
// which is symlinked to /var/home.
// Ignore ENOENT as reasonable, as some paths may not exist in early Libpod
// init.
if dbVal != "" {
dbValClean, err := filepath.EvalSymlinks(dbVal)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
checkedVal, err := evalSymlinksIfExists(dbVal)
if err != nil {
return fmt.Errorf("cannot evaluate symlinks on DB %s path %q: %w", fieldName, dbVal, err)
}
dbVal = dbValClean
dbVal = checkedVal
}
if ourVal != "" {
ourValClean, err := filepath.EvalSymlinks(ourVal)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
checkedVal, err := evalSymlinksIfExists(ourVal)
if err != nil {
return fmt.Errorf("cannot evaluate symlinks on our %s path %q: %w", fieldName, ourVal, err)
}
ourVal = ourValClean
ourVal = checkedVal
}
}

Expand Down
20 changes: 20 additions & 0 deletions libpod/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -288,3 +289,22 @@ func writeStringToPath(path, contents, mountLabel string, uid, gid int) error {

return nil
}

// If the given path exists, evaluate any symlinks in it. If it does not, clean
// the path and return it. Used to try and verify path equality in a somewhat
// sane fashion.
func evalSymlinksIfExists(toCheck string) (string, error) {
checkedVal, err := filepath.EvalSymlinks(toCheck)
if err != nil {
// If the error is not ENOENT, something more serious has gone
// wrong, return it.
if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
// This is an ENOENT. On ENOENT, EvalSymlinks returns "".
// We don't want that. Return a cleaned version of the original
// path.
return filepath.Clean(toCheck), nil
}
return checkedVal, nil
}

0 comments on commit 04bd415

Please sign in to comment.