Skip to content
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

Use syscall package instead of cgo #630

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ docker-compilation-image-linux:

$(BUILD_DIR)/linux/lifecycle/lifecycle: export GOOS:=linux
$(BUILD_DIR)/linux/lifecycle/lifecycle: OUT_DIR:=$(BUILD_DIR)/$(GOOS)/lifecycle
$(BUILD_DIR)/linux/lifecycle/lifecycle: GOENV:=GOARCH=$(GOARCH) CGO_ENABLED=1
$(BUILD_DIR)/linux/lifecycle/lifecycle: docker-compilation-image-linux
$(BUILD_DIR)/linux/lifecycle/lifecycle: $(GOFILES)
$(BUILD_DIR)/linux/lifecycle/lifecycle:
Expand Down
41 changes: 2 additions & 39 deletions priv/user_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import (
"syscall"
)

/*
#cgo LDFLAGS: --static
#define _GNU_SOURCE
#include <unistd.h>
#include <errno.h>

static int
csetresuid(uid_t ruid, uid_t euid, uid_t suid) {
int ec = setresuid(ruid, euid, suid);
return (ec < 0) ? errno : 0;
}

static int
csetresgid(gid_t rgid, gid_t egid, gid_t sgid) {
int ec = setresgid(rgid, egid, sgid);
return (ec < 0) ? errno : 0;
}

*/
import "C"

// EnsureOwner recursively chowns a dir if it isn't writable
func EnsureOwner(uid, gid int, paths ...string) error {
for _, p := range paths {
Expand Down Expand Up @@ -105,32 +84,16 @@ func RunAs(uid, gid int) error {
return nil
}

if err := setresgid(gid, gid, gid); err != nil {
if err := syscall.Setresgid(gid, gid, gid); err != nil {
return err
}
if err := setresuid(uid, uid, uid); err != nil {
if err := syscall.Setresuid(uid, uid, uid); err != nil {
return err
}

return nil
}

func setresgid(rgid, egid, sgid int) error {
eno := C.csetresgid(C.gid_t(rgid), C.gid_t(egid), C.gid_t(sgid))
if eno != 0 {
return syscall.Errno(eno)
}
return nil
}

func setresuid(ruid, euid, suid int) error {
eno := C.csetresuid(C.uid_t(ruid), C.uid_t(euid), C.uid_t(suid))
if eno != 0 {
return syscall.Errno(eno)
}
return nil
}

func SetEnvironmentForUser(uid int) error {
user, err := user.LookupId(strconv.Itoa(uid))
if err != nil {
Expand Down