Skip to content
Snippets Groups Projects
Commit 6d399e9d authored by Michael Anthony Knyszek's avatar Michael Anthony Knyszek Committed by Michael Knyszek
Browse files

[release-branch.go1.24] os: hide SetFinalizer from users of Root

Currently Root embeds a root and calls SetFinalizer on &r.root. This
sets the finalizer on the outer root, which is visible to users of
os.Root, and thus they can mutate the finalizer attached to it.

This change modifies Root to not embed its inner root, but rather to
refer to it by pointer. This allows us to set the finalizer on this
independent inner object, preventing users of os.Root from changing the
finalizer. This follows the same pattern as os.File's finalizer.

Fixes #71617.

Change-Id: Ibd199bab1b3c877d5e12ef380fd4647b4e10221f
Reviewed-on: https://go-review.googlesource.com/c/go/+/647876


Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: default avatarDamien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit a704d39b)
Reviewed-on: https://go-review.googlesource.com/c/go/+/648135


TryBot-Bypass: Michael Knyszek <mknyszek@google.com>
Reviewed-by: default avatarDmitri Shuralyov <dmitshur@google.com>
Reviewed-by: default avatarDmitri Shuralyov <dmitshur@golang.org>
parent b7b4c605
No related branches found
No related tags found
No related merge requests found
......@@ -60,7 +60,7 @@ func OpenInRoot(dir, name string) (*File, error) {
// - When GOOS=plan9 or GOOS=js, Root does not track directories across renames.
// On these platforms, a Root references a directory name, not a file descriptor.
type Root struct {
root root
root *root
}
const (
......
......@@ -49,7 +49,7 @@ func newRoot(name string) (*Root, error) {
if !fi.IsDir() {
return nil, errors.New("not a directory")
}
return &Root{root{name: name}}, nil
return &Root{&root{name: name}}, nil
}
func (r *root) Close() error {
......
......@@ -48,11 +48,11 @@ func newRoot(fd int, name string) (*Root, error) {
syscall.CloseOnExec(fd)
}
r := &Root{root{
r := &Root{&root{
fd: fd,
name: name,
}}
runtime.SetFinalizer(&r.root, (*root).Close)
runtime.SetFinalizer(r.root, (*root).Close)
return r, nil
}
......
......@@ -105,11 +105,11 @@ func newRoot(fd syscall.Handle, name string) (*Root, error) {
return nil, &PathError{Op: "open", Path: name, Err: errors.New("not a directory")}
}
r := &Root{root{
r := &Root{&root{
fd: fd,
name: name,
}}
runtime.SetFinalizer(&r.root, (*root).Close)
runtime.SetFinalizer(r.root, (*root).Close)
return r, nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment