-
Notifications
You must be signed in to change notification settings - Fork 1
fix: preserve file owner/group on atomic rewrite #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,15 +93,19 @@ func (f *File) Read() (string, error) { | |
| // step after its creation fails (including the rename); on success the remove | ||
| // is a no-op because the file has already been renamed away. | ||
| func (f *File) Write(content string) error { | ||
| mode, err := f.Mode() | ||
| info, err := f.Info() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| mode := info.Mode() | ||
|
|
||
| tempName := filepath.Join(f.Dir(), RandomString(20)) | ||
| if err := os.WriteFile(tempName, []byte(content), mode); err != nil { | ||
| return fmt.Errorf("create tempfile in %v: %w", f.Dir(), err) | ||
| } | ||
| if err := chownTempFromInfo(tempName, info); err != nil { | ||
| return fmt.Errorf("preserve ownership on temp file %v: %w", tempName, err) | ||
|
Comment on lines
+106
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
| // Make sure the temp file is removed if the rename below fails. On | ||
| // success, the rename has already moved the file to f.Path so this is | ||
| // a no-op (we deliberately ignore the not-exist error). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| //go:build !windows | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func chownTempFromInfo(tempPath string, info os.FileInfo) error { | ||
| sys, ok := info.Sys().(*syscall.Stat_t) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return os.Chown(tempPath, int(sys.Uid), int(sys.Gid)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build windows | ||
|
|
||
| package main | ||
|
|
||
| import "os" | ||
|
|
||
| func chownTempFromInfo(tempPath string, info os.FileInfo) error { | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For setuid/setgid files, Unix kernels clear those special permission bits on
chown, so the temp file is created withinfo.Mode()and then loses those bits here before it is renamed over the original. Rewriting a matching setuid/setgid executable therefore silently strips its mode bits; apply ownership before restoring the original mode, or chmod again after the chown succeeds.Useful? React with 👍 / 👎.