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

Add -uid and -uid flags to drop permissions #804

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions cmd/yggdrasil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ type yggArgs struct {
getaddr bool
getsnet bool
loglevel string
runuid int
rungid int
}

func getArgs() yggArgs {
Expand All @@ -207,7 +209,10 @@ func getArgs() yggArgs {
getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration")
getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration")
loglevel := flag.String("loglevel", "info", "loglevel to enable")
runuid := flag.Int("uid", -1, "drop privileges to this user id")
rungid := flag.Int("gid", -1, "drop privileges to this group id")
flag.Parse()

return yggArgs{
genconf: *genconf,
useconf: *useconf,
Expand All @@ -220,6 +225,8 @@ func getArgs() yggArgs {
getaddr: *getaddr,
getsnet: *getsnet,
loglevel: *loglevel,
runuid: *runuid,
rungid: *rungid,
}
}

Expand Down Expand Up @@ -364,6 +371,17 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
address := n.core.Address()
subnet := n.core.Subnet()
public := n.core.GetSelf().Key
// Lower permissions from root to something else, if the user wants to
if syscall.Getuid() == 0 {
if args.rungid > 0 {
fmt.Println("Dropping gid to ", args.rungid)
syscall.Setgid(args.rungid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know this makes Yggdrasil no longer portable, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see the circleci failure under the "other" OS (I assume that's windows?). I'm a bit surprised there's an OS that doesn't have UIDs, but just a little bit. Anyhow, I guess the fix for that would be to move the args struct and arg parser into a arg_windows.go and a arg_posix.go, and only have the gid/uid options in the args_posix side. Similarly define a drop_perm_unix.go that does the above, and a drop_perm_windows.go that's just a no-op. I can definitely do that, but before I do, is there interest in privilege dropping in yggdrasil? I'm happy to do the grunt work, but if it's something that's not seen as necessary anyhow, then I can just drop this.

Copy link
Member

@Arceliar Arceliar Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More importantly, syscall.Setgid (or setting UIDs) probably won't work in Go. Or to be exact, it will set the ID for whatever random thread the goroutine happened to be running on at the time, but the rest of the threads will be unaffected.

The language recently added https://golang.org/pkg/syscall/#AllThreadsSyscall which is meant to address issues like this, but it has some caveats associated with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the AllThreadsSyscall function is only available on linux, so this feature wouldn't be cross-platform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX setuid is required to set the uid for the entire process (ref and a stackoverflow). It definitely appears to be working under FreeBSD and Linux, but I haven't tried it under OSX. I'd expect it to work the same there, as OSX's POSIX layer appears to still be based on FreeBSD.

Or am I misunderstanding the problem? It looks like Linux is actually the weird one, because it does (in the kernel) implement per-thread uids, and sane programs depend on glibc or musl to provide the POSIX semantics that people tend to expect (more). If Go's "libc" really is just sending the raw syscalls to the kernel, then I can see how the AllThreadsSyscall would be needed to be run explicitly on LInux systems. Unfortunately I'm not actually a Go programmer, and I have no idea what AllThreadsSyscall's function signature means. So, I guess I'll drop this, unless somebody really wants to see it completed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it more, it appears that Setgid and Setuid were rewritten to use AllThreadsSyscall on linux (as long as cgo isn't being used), so this should be doing the right thing.

Some refactoring/cleanup is needed to avoid breaking things on platforms where syscall.Setuid/syscall.Setgid are undefined (if any... windows maybe?). That shouldn't be too difficult, I can probably take a look at in the next few days.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody else could figure out how to do the conditional compilation thing, that would be cool. I tried to split out main.go's argv into its own main_posix.go and main_other.go with // +build directives for per-OS compilation, and I just couldn't get the thing to compile. I don't know Go though, and its package system seems to be pretty unique, so I'm sure I was just doing something really basic wrong.

}
if args.runuid > 0 {
fmt.Println("Dropping uid to ", args.rungid)
syscall.Setuid(args.runuid)
}
}
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
logger.Infof("Your IPv6 address is %s", address.String())
logger.Infof("Your IPv6 subnet is %s", subnet.String())
Expand Down