Eval symlinks on XDG_RUNTIME_DIR

Partial Fix for https://github.com/containers/podman/issues/14606

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2022-09-23 13:49:44 -04:00
parent f21847917e
commit 71f0c9f33a
5 changed files with 37 additions and 2 deletions

View File

@ -105,6 +105,10 @@ func setXdgDirs() error {
if _, found := os.LookupEnv("DBUS_SESSION_BUS_ADDRESS"); !found { if _, found := os.LookupEnv("DBUS_SESSION_BUS_ADDRESS"); !found {
sessionAddr := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "bus") sessionAddr := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "bus")
if _, err := os.Stat(sessionAddr); err == nil { if _, err := os.Stat(sessionAddr); err == nil {
sessionAddr, err = filepath.EvalSymlinks(sessionAddr)
if err != nil {
return err
}
os.Setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path="+sessionAddr) os.Setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path="+sessionAddr)
} }
} }

View File

@ -157,7 +157,13 @@ func (r *Runtime) reset(ctx context.Context) error {
} }
} }
xdgRuntimeDir := filepath.Clean(os.Getenv("XDG_RUNTIME_DIR")) xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR")
if xdgRuntimeDir != "" {
xdgRuntimeDir, err = filepath.EvalSymlinks(xdgRuntimeDir)
if err != nil {
return err
}
}
_, prevError := r.store.Shutdown(true) _, prevError := r.store.Shutdown(true)
graphRoot := filepath.Clean(r.store.GraphRoot()) graphRoot := filepath.Clean(r.store.GraphRoot())
if graphRoot == xdgRuntimeDir { if graphRoot == xdgRuntimeDir {

View File

@ -129,7 +129,11 @@ func dbusAuthRootlessConnection(createBus func(opts ...godbus.ConnOption) (*godb
func newRootlessConnection() (*dbus.Conn, error) { func newRootlessConnection() (*dbus.Conn, error) {
return dbus.NewConnection(func() (*godbus.Conn, error) { return dbus.NewConnection(func() (*godbus.Conn, error) {
return dbusAuthRootlessConnection(func(opts ...godbus.ConnOption) (*godbus.Conn, error) { return dbusAuthRootlessConnection(func(opts ...godbus.ConnOption) (*godbus.Conn, error) {
path := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "systemd/private") path := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "systemd", "private")
path, err := filepath.EvalSymlinks(path)
if err != nil {
return nil, err
}
return godbus.Dial(fmt.Sprintf("unix:path=%s", path)) return godbus.Dial(fmt.Sprintf("unix:path=%s", path))
}) })
}) })

View File

@ -27,6 +27,12 @@ func GetRuntimeDir() (string, error) {
rootlessRuntimeDirOnce.Do(func() { rootlessRuntimeDirOnce.Do(func() {
runtimeDir := os.Getenv("XDG_RUNTIME_DIR") runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir != "" {
rootlessRuntimeDir, rootlessRuntimeDirError = filepath.EvalSymlinks(runtimeDir)
return
}
uid := fmt.Sprintf("%d", rootless.GetRootlessUID()) uid := fmt.Sprintf("%d", rootless.GetRootlessUID())
if runtimeDir == "" { if runtimeDir == "" {
tmpDir := filepath.Join("/run", "user", uid) tmpDir := filepath.Join("/run", "user", uid)

View File

@ -776,4 +776,19 @@ EOF
is "$output" ".*options ${dns_opt}" "--dns-option was added" is "$output" ".*options ${dns_opt}" "--dns-option was added"
} }
@test "podman rootless netns works when XDG_RUNTIME_DIR includes symlinks" {
# regression test for https://github.com/containers/podman/issues/14606
is_rootless || skip "only meaningful for rootless"
# Create a tmpdir symlink pointing to /run, and use it briefly
ln -s /run $PODMAN_TMPDIR/run
local tmp_run=$PODMAN_TMPDIR/run/user/$(id -u)
test -d $tmp_run || skip "/run/user/MYUID unavailable"
# This 'run' would previously fail with:
# IPAM error: failed to open database ....
XDG_RUNTIME_DIR=$tmp_run run_podman run --network bridge --rm $IMAGE ip a
assert "$output" =~ "eth0"
}
# vim: filetype=sh # vim: filetype=sh