Skip to content

Instantly share code, notes, and snippets.

@gingerhot
Forked from BorePlusPlus/Session
Created September 4, 2016 09:30
Show Gist options
  • Save gingerhot/f1768e459e8b6caeaade2db8bdd35f4d to your computer and use it in GitHub Desktop.
Save gingerhot/f1768e459e8b6caeaade2db8bdd35f4d to your computer and use it in GitHub Desktop.
Setuid/Getuid in golang
$ go build setuid.go
$ sudo su
[sudo] password for bore:
# chown root:root setuid
# chmod u+s setuid
$ ./setuid
Real UID: 1000
Effective UID: 0
Real UID: 1000
Effective UID: 1000
$
// But if I use ps:
$ ps -eo euser,ruser,suser,comm | grep setuid
root bore root setuid
// After seven seconds it's still the same, even if golang reports changed effective UID
$ ps -eo euser,ruser,suser,comm | grep setuid
root bore root setuid
// C implementation behaves as expected
$ ps -eo euser,ruser,suser,comm | grep setuid
root bore root setuid
$ ps -eo euser,ruser,suser,comm | grep setuid
bore bore bore setuid
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void printdelay()
{
printf("Current UID: %ld\n", (long) getuid());
printf("Effective UID: %ld\n", (long) geteuid());
fflush(stdout);
sleep(7);
}
int main(int argc, char *argv[])
{
printdelay();
if (setuid(getuid()) == -1) {
printf("Error setting UID");
exit(1);
}
printdelay();
return 0;
}
package main
import (
"fmt"
"syscall"
"time"
"log"
"os"
)
func main() {
printdelay()
err := syscall.Setuid(syscall.Getuid())
if err != nil {
log.Fatal(err)
os.Exit(1)
}
printdelay()
}
func printdelay() {
fmt.Printf("Real UID: %d\n", syscall.Getuid())
fmt.Printf("Effective UID: %d\n", syscall.Geteuid())
time.Sleep(7 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment