From ce4b6e92971027eb9c819594199d4526aae2d9f0 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Wed, 6 Apr 2022 15:37:25 -0400 Subject: [PATCH 1/2] internal/exec/util/passwd: add more error-prefixing We're hitting an error in that area but the error message isn't very descriptive. Let's add more error prefixing. --- internal/exec/util/passwd.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/exec/util/passwd.go b/internal/exec/util/passwd.go index 267ed8614..596cf39a7 100644 --- a/internal/exec/util/passwd.go +++ b/internal/exec/util/passwd.go @@ -167,18 +167,18 @@ func translateV2_1PasswdUserGroupSliceToStringSlice(groups []types.Group) []stri // creating any directories in fp as needed. func writeAuthKeysFile(u *user.User, fp string, keys []byte) error { if err := as_user.MkdirAll(u, filepath.Dir(fp), 0700); err != nil { - return err + return fmt.Errorf("creating parent dirs for %q: %w", fp, err) } f, err := as_user.OpenFile(u, fp, unix.O_WRONLY|unix.O_CREAT|unix.O_TRUNC, 0600) if err != nil { - return err + return fmt.Errorf("opening file %q as user %s and group %s: %w", fp, u.Uid, u.Gid, err) } if _, err = f.Write(keys); err != nil { - return err + return fmt.Errorf("writing file %q: %w", fp, err) } if err := f.Close(); err != nil { - return err + return fmt.Errorf("closing file %q: %w", fp, err) } return nil } From 78538da3714e74b7e7351631475fa5bd22d92e70 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Wed, 20 Apr 2022 09:09:53 -0400 Subject: [PATCH 2/2] internal/exec/util/passwd: plug fd leak in error path Doesn't really matter since we'll likely bubble all the way to the exit anyway, but it's good practice. --- internal/exec/util/passwd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/exec/util/passwd.go b/internal/exec/util/passwd.go index 596cf39a7..e6050d049 100644 --- a/internal/exec/util/passwd.go +++ b/internal/exec/util/passwd.go @@ -175,6 +175,7 @@ func writeAuthKeysFile(u *user.User, fp string, keys []byte) error { return fmt.Errorf("opening file %q as user %s and group %s: %w", fp, u.Uid, u.Gid, err) } if _, err = f.Write(keys); err != nil { + f.Close() // ignore errors return fmt.Errorf("writing file %q: %w", fp, err) } if err := f.Close(); err != nil {