Trouble running exec.Command() as a different unix user.

2,480 views
Skip to first unread message

AJ Bahnken

unread,
Mar 27, 2015, 12:51:56 PM3/27/15
to golan...@googlegroups.com
Hey Everyone,

I am having trouble with running exec.Command() as a different user. I have tried a few different users, so I do not think that is the problem. The error I am getting is "fork/exec /bin/date: operation not permitted". I tried googling around, but couldn't find any mention of this exact problem. I have tried a few different simple unix commands other than date as well. Also, not sure if it is relevant, but I am using Ubuntu 14.04 and go version go1.4 linux/amd64.

Any ideas?

Stackoverflow Answer I followed to get here: http://stackoverflow.com/questions/21705950/running-external-commands-through-os-exec-under-another-user

Code:

package main

import (
       
"fmt"
       
"os/exec"
       
"os/user"
       
"strconv"
       
"syscall"
)

func main
() {
        cmd
:= exec.Command("date")

        myuser
, _ := user.Current()

        uid
, _ := strconv.Atoi(myuser.Uid)
        gid
, _ := strconv.Atoi(myuser.Gid)

        cmd
.SysProcAttr = &syscall.SysProcAttr{}
        cmd
.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}

       
out, err := cmd.Output()
       
if err != nil {
                fmt
.Printf("Run Command got an Error: %s\n", err)
               
return
       
}
        fmt
.Println(out)
}


Pastebin link: http://pastebin.com/WetNsnvH

Final Output:
Run Command got an Error: fork/exec /bin/date: operation not permitted


Thanks so much,
AJ

James Bardin

unread,
Mar 27, 2015, 1:20:30 PM3/27/15
to golan...@googlegroups.com


On Friday, March 27, 2015 at 12:51:56 PM UTC-4, AJ Bahnken wrote:

Final Output:
Run Command got an Error: fork/exec /bin/date: operation not permitted



Are you running this as root? (or  do have the the appropriate capabilities set?)

AJ B

unread,
Mar 27, 2015, 3:38:49 PM3/27/15
to James Bardin, golan...@googlegroups.com
So I just ran it as root and it worked. Thanks for the help. But why wouldn't my default user be able to run a command as itself using a binary it obviously has permissions to run?

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/bcjk9ncP5ac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
AJ Bahnken / @ajvbahnken

James Bardin

unread,
Mar 27, 2015, 3:48:30 PM3/27/15
to AJ B, golan...@googlegroups.com

On Fri, Mar 27, 2015 at 3:38 PM, AJ B <a...@ajvb.me> wrote:
So I just ran it as root and it worked. Thanks for the help. But why wouldn't my default user be able to run a command as itself using a binary it obviously has permissions to run?

Because setuid and setgid are privileged calls, regardless of the values you pass to them. 

worc...@googlemail.com

unread,
May 7, 2019, 12:32:49 PM5/7/19
to golang-nuts
Hi AJ!

Note, this issue was solved in go 1.9 with the introduction of NoSetGroups property of syscall.SysProcAttr.


Pete
Reply all
Reply to author
Forward
0 new messages