From 987ced003ec2971797e8969e0cf0a37a751ebc84 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 26 Feb 2025 21:21:57 +0100 Subject: syscallcompat: rename thread_credentials files to include "linux" As with the other files, include "linux" because the code only builds on linux renamed: internal/syscallcompat/thread_credentials.go -> internal/syscallcompat/thread_credentials_linux.go renamed: internal/syscallcompat/thread_credentials_368_arm.go -> internal/syscallcompat/thread_credentials_linux_368_arm.go renamed: internal/syscallcompat/thread_credentials_other.go -> internal/syscallcompat/thread_credentials_linux_other.go --- internal/syscallcompat/thread_credentials.go | 60 ---------------------- .../syscallcompat/thread_credentials_368_arm.go | 35 ------------- internal/syscallcompat/thread_credentials_linux.go | 60 ++++++++++++++++++++++ .../thread_credentials_linux_368_arm.go | 35 +++++++++++++ .../thread_credentials_linux_other.go | 35 +++++++++++++ internal/syscallcompat/thread_credentials_other.go | 35 ------------- 6 files changed, 130 insertions(+), 130 deletions(-) delete mode 100644 internal/syscallcompat/thread_credentials.go delete mode 100644 internal/syscallcompat/thread_credentials_368_arm.go create mode 100644 internal/syscallcompat/thread_credentials_linux.go create mode 100644 internal/syscallcompat/thread_credentials_linux_368_arm.go create mode 100644 internal/syscallcompat/thread_credentials_linux_other.go delete mode 100644 internal/syscallcompat/thread_credentials_other.go (limited to 'internal') diff --git a/internal/syscallcompat/thread_credentials.go b/internal/syscallcompat/thread_credentials.go deleted file mode 100644 index 8cfb703..0000000 --- a/internal/syscallcompat/thread_credentials.go +++ /dev/null @@ -1,60 +0,0 @@ -//go:build linux - -// golang.org/x/sys/unix commit -// https://github.com/golang/sys/commit/d0df966e6959f00dc1c74363e537872647352d51 -// changed unix.Setreuid/unix.Setregid functions to affect the whole thread, which is -// what gocryptfs does NOT want (https://github.com/rfjakob/gocryptfs/issues/893). -// The functions Setreuid/Setegid are copy-pasted from one commit before -// (9e1f76180b77a12eb07c82eb8e1ea8a7f8d202e7). -// -// Looking at the diff at https://github.com/golang/sys/commit/d0df966e6959f00dc1c74363e537872647352d51 -// we see that only two architectures, 386 and arm, use SYS_SETREUID32/SYS_SETREGID32 -// (see "man 2 setreuid" for why). -// All the others architectures use SYS_SETREUID/SYS_SETREGID. -// -// As of golang.org/x/sys/unix v0.30.0, Setgroups/setgroups is still per-thread, but -// it is likely that this will change, too. Setgroups/setgroups are copy-pasted from -// v0.30.0. The SYS_SETGROUPS32/SYS_SETGROUPS split is the same as for Setreuid. -// -// Note: _Gid_t is always uint32 on linux, so we can directly use uint32 for setgroups. -package syscallcompat - -import ( - "log" -) - -func Setgroups(gids []int) (err error) { - if len(gids) == 0 { - return setgroups(0, nil) - } - - a := make([]uint32, len(gids)) - for i, v := range gids { - a[i] = uint32(v) - } - return setgroups(len(a), &a[0]) -} - -// SetgroupsPanic calls Setgroups and panics on error -func SetgroupsPanic(gids []int) { - err := Setgroups(gids) - if err != nil { - log.Panic(err) - } -} - -// SetregidPanic calls Setregid and panics on error -func SetregidPanic(rgid int, egid int) { - err := Setregid(rgid, egid) - if err != nil { - log.Panic(err) - } -} - -// SetreuidPanic calls Setreuid and panics on error -func SetreuidPanic(ruid int, euid int) { - err := Setreuid(ruid, euid) - if err != nil { - log.Panic(err) - } -} diff --git a/internal/syscallcompat/thread_credentials_368_arm.go b/internal/syscallcompat/thread_credentials_368_arm.go deleted file mode 100644 index 1244160..0000000 --- a/internal/syscallcompat/thread_credentials_368_arm.go +++ /dev/null @@ -1,35 +0,0 @@ -//go:build (linux && 386) || (linux && arm) - -package syscallcompat - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -// See thread_credentials.go for docs - -func Setreuid(ruid int, euid int) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETREUID32, uintptr(ruid), uintptr(euid), 0) - if e1 != 0 { - err = e1 - } - return -} - -func Setregid(rgid int, egid int) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETREGID32, uintptr(rgid), uintptr(egid), 0) - if e1 != 0 { - err = e1 - } - return -} - -func setgroups(n int, list *uint32) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETGROUPS32, uintptr(n), uintptr(unsafe.Pointer(list)), 0) - if e1 != 0 { - err = e1 - } - return -} diff --git a/internal/syscallcompat/thread_credentials_linux.go b/internal/syscallcompat/thread_credentials_linux.go new file mode 100644 index 0000000..8cfb703 --- /dev/null +++ b/internal/syscallcompat/thread_credentials_linux.go @@ -0,0 +1,60 @@ +//go:build linux + +// golang.org/x/sys/unix commit +// https://github.com/golang/sys/commit/d0df966e6959f00dc1c74363e537872647352d51 +// changed unix.Setreuid/unix.Setregid functions to affect the whole thread, which is +// what gocryptfs does NOT want (https://github.com/rfjakob/gocryptfs/issues/893). +// The functions Setreuid/Setegid are copy-pasted from one commit before +// (9e1f76180b77a12eb07c82eb8e1ea8a7f8d202e7). +// +// Looking at the diff at https://github.com/golang/sys/commit/d0df966e6959f00dc1c74363e537872647352d51 +// we see that only two architectures, 386 and arm, use SYS_SETREUID32/SYS_SETREGID32 +// (see "man 2 setreuid" for why). +// All the others architectures use SYS_SETREUID/SYS_SETREGID. +// +// As of golang.org/x/sys/unix v0.30.0, Setgroups/setgroups is still per-thread, but +// it is likely that this will change, too. Setgroups/setgroups are copy-pasted from +// v0.30.0. The SYS_SETGROUPS32/SYS_SETGROUPS split is the same as for Setreuid. +// +// Note: _Gid_t is always uint32 on linux, so we can directly use uint32 for setgroups. +package syscallcompat + +import ( + "log" +) + +func Setgroups(gids []int) (err error) { + if len(gids) == 0 { + return setgroups(0, nil) + } + + a := make([]uint32, len(gids)) + for i, v := range gids { + a[i] = uint32(v) + } + return setgroups(len(a), &a[0]) +} + +// SetgroupsPanic calls Setgroups and panics on error +func SetgroupsPanic(gids []int) { + err := Setgroups(gids) + if err != nil { + log.Panic(err) + } +} + +// SetregidPanic calls Setregid and panics on error +func SetregidPanic(rgid int, egid int) { + err := Setregid(rgid, egid) + if err != nil { + log.Panic(err) + } +} + +// SetreuidPanic calls Setreuid and panics on error +func SetreuidPanic(ruid int, euid int) { + err := Setreuid(ruid, euid) + if err != nil { + log.Panic(err) + } +} diff --git a/internal/syscallcompat/thread_credentials_linux_368_arm.go b/internal/syscallcompat/thread_credentials_linux_368_arm.go new file mode 100644 index 0000000..e244a15 --- /dev/null +++ b/internal/syscallcompat/thread_credentials_linux_368_arm.go @@ -0,0 +1,35 @@ +//go:build (linux && 386) || (linux && arm) + +package syscallcompat + +import ( + "unsafe" + + "golang.org/x/sys/unix" +) + +// See thread_credentials_linux.go for docs + +func Setreuid(ruid int, euid int) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETREUID32, uintptr(ruid), uintptr(euid), 0) + if e1 != 0 { + err = e1 + } + return +} + +func Setregid(rgid int, egid int) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETREGID32, uintptr(rgid), uintptr(egid), 0) + if e1 != 0 { + err = e1 + } + return +} + +func setgroups(n int, list *uint32) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETGROUPS32, uintptr(n), uintptr(unsafe.Pointer(list)), 0) + if e1 != 0 { + err = e1 + } + return +} diff --git a/internal/syscallcompat/thread_credentials_linux_other.go b/internal/syscallcompat/thread_credentials_linux_other.go new file mode 100644 index 0000000..ba385b6 --- /dev/null +++ b/internal/syscallcompat/thread_credentials_linux_other.go @@ -0,0 +1,35 @@ +//go:build !((linux && 386) || (linux && arm)) + +package syscallcompat + +import ( + "unsafe" + + "golang.org/x/sys/unix" +) + +// See thread_credentials_linux.go for docs + +func Setreuid(ruid int, euid int) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETREUID, uintptr(ruid), uintptr(euid), 0) + if e1 != 0 { + err = e1 + } + return +} + +func Setregid(rgid int, egid int) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETREGID, uintptr(rgid), uintptr(egid), 0) + if e1 != 0 { + err = e1 + } + return +} + +func setgroups(n int, list *uint32) (err error) { + _, _, e1 := unix.RawSyscall(unix.SYS_SETGROUPS, uintptr(n), uintptr(unsafe.Pointer(list)), 0) + if e1 != 0 { + err = e1 + } + return +} diff --git a/internal/syscallcompat/thread_credentials_other.go b/internal/syscallcompat/thread_credentials_other.go deleted file mode 100644 index e3c78d0..0000000 --- a/internal/syscallcompat/thread_credentials_other.go +++ /dev/null @@ -1,35 +0,0 @@ -//go:build !((linux && 386) || (linux && arm)) - -package syscallcompat - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -// See thread_credentials.go for docs - -func Setreuid(ruid int, euid int) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETREUID, uintptr(ruid), uintptr(euid), 0) - if e1 != 0 { - err = e1 - } - return -} - -func Setregid(rgid int, egid int) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETREGID, uintptr(rgid), uintptr(egid), 0) - if e1 != 0 { - err = e1 - } - return -} - -func setgroups(n int, list *uint32) (err error) { - _, _, e1 := unix.RawSyscall(unix.SYS_SETGROUPS, uintptr(n), uintptr(unsafe.Pointer(list)), 0) - if e1 != 0 { - err = e1 - } - return -} -- cgit v1.2.3