Skip to content

Commit

Permalink
Use syscall package instead of cgo
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Jun 11, 2021
1 parent edbabee commit 71d15e3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 40 deletions.
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

0 comments on commit 71d15e3

Please sign in to comment.