From: Michael Anthony Knyszek Date: Fri, 7 Feb 2025 23:22:50 +0000 (+0000) Subject: [release-branch.go1.24] os: hide SetFinalizer from users of Root X-Git-Tag: go1.24.0~2 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6d399e9da6090af289aba1f9c4bcc8488387ff9a;p=gostls13.git [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 Reviewed-by: Damien Neil LUCI-TryBot-Result: Go LUCI (cherry picked from commit a704d39b29dfc21599f644909c0f98bbfa745cb4) Reviewed-on: https://go-review.googlesource.com/c/go/+/648135 TryBot-Bypass: Michael Knyszek Reviewed-by: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov --- diff --git a/src/os/root.go b/src/os/root.go index 04741c0281..f91c0f75f3 100644 --- a/src/os/root.go +++ b/src/os/root.go @@ -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 ( diff --git a/src/os/root_noopenat.go b/src/os/root_noopenat.go index 8d5ead32b9..8be55a029f 100644 --- a/src/os/root_noopenat.go +++ b/src/os/root_noopenat.go @@ -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 { diff --git a/src/os/root_unix.go b/src/os/root_unix.go index 4b52b81de7..02d3b4bdad 100644 --- a/src/os/root_unix.go +++ b/src/os/root_unix.go @@ -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 } diff --git a/src/os/root_windows.go b/src/os/root_windows.go index dcc311cf86..32dfa070b7 100644 --- a/src/os/root_windows.go +++ b/src/os/root_windows.go @@ -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 }