4 ms·
A word of warning guys, I've had a look through the code and unless I've missed the obvious, there's nothing in there to change user ID; which means this would
by laumars 13y ago
A word of warning guys, I've had a look through the code and unless I've missed the obvious, there's nothing in there to change user ID; which means this would either need to be run as root, or would need to listen on a port > 1024.
In the case of the former, that's a huge step backwards in terms of security.
In the case of the latter, that would mean you'd need another reverse proxy hooked up - which would negate the need for this web server to begin with.
This can easily be fixed within Go though:
import (
"log"
"syscall"
)
const (
user_id int = 1000
group_id int = 1000
)
func secureDaemon() {
// set group id first as you need to be root to change group
err := syscall.Setgid(group_id)
if err != nil {
log.Fatalln(err)
}
err = syscall.Setuid(user_id)
if err != nil {
log.Fatalln(err)
}
}
You can also add chroot to your code if you want to be ultra paranoid:
import (
"os"
)
const (
chroot_dir string = "/opt/go-webserver"
)
func chrootDaemon() {
err := os.Chdir(chroot_dir)
if err != nil {
log.Fatalln(err)
}
err = syscall.Chroot(chroot_dir)
if err != nil {
log.Fatalln(err)
}
}
This will need to be done before you change your user ID (as you need root permissions to chroot) and you may need to compile the Go without CGO because some of the standard Go libraries will have SO dependencies (I found this to be the case with domain name lookups).
(the above code is adapted from my own Go web framework that I'm in the processes of building)
- 1331 13y agoA program run as a non-privileged user can access privileged ports using authbind: http://en.wikipedia.org/wiki/Authbind http://en.wikipedia.org/wiki/Authbind
- laumars 13y agoIndeed, but it would make more sense (in my opinion) to have the web server natively 'de-elevate' it's permissions than to expect the sysadmin to be competent enough to set up authbind manually. Particularly when setuid/setgid is so easy in Go and that every other webserver out there natively drops down to a designated webuser after the daemons been launched and ports binded.
- peterbotond 13y agoauthbind seems to defeat the purpose of privileged and non-privileged users to bind.
- tptacek 13y agoThere's really not much of a purpose to that anymore, anyways. It was a bigger deal when servers were multi-user and when services were diverse.
- pjmlp 13y agoSeems to be GNU/Linux only.
- coolsunglasses 13y agoCome in, the water's warm.
- oakaz 13y agothanks! would you mind sending a pull request to https://github.com/azer/boxcars/blob/master/boxcars/boxcars.go#L16 https://github.com/azer/boxcars/blob/master/boxcars/boxcars.... ?
- laumars 13y agoI'm not on Github, sorry. But that code should be pretty easy to port to your app :)
- ghotli 13y agoJust curious why you aren't on github. Surely there's a reason worth sharing?
- laumars 13y agoI tend not to sign up to many cloud based solutions as I prefer to host my own solutions (partly out of paranoia and partly because I have the server capacity to run my own solutions so it seems to make sense to use my servers where I can). With github, it's very rare when I have anything worth contributing to other peoples gits so I've never really missed it. But I'm sure I'll end up on there at some point in the future.
- SEJeff 13y agoWhy not send them a patch?
- laumars 13y agoI'm not on github, which is why I made my OP so verbose; all the working code is there, s/he just needs to integrate it into their own code :-)
- drivebyacct2 13y agoThere are system level ways of doing this that make far more sense that fucking around with unix syscalls that won't be portable.
- oakaz 13y agojust implemented -secure option for this purpose; https://github.com/azer/boxcars/blob/master/secure.go https://github.com/azer/boxcars/blob/master/secure.go usage example; http://github.com/azer/boxcars#security http://github.com/azer/boxcars#security thanks for the helpful comment!
- sarnowski 13y agoPlease see my comment to your parent. Additional hint: you should also care about dropping/controlling your user's groups, see https://github.com/sarnowski/mitigation/blob/master/mitigation.go https://github.com/sarnowski/mitigation/blob/master/mitigati...
- oakaz 13y agoyeah I saw it, thanks so much. I like this kind of single-purpose small libraries. The only thing is, I hesitate to depend on other people's repositories since they may get moved or change the API.
- sarnowski 13y agoWARNING: I implemented a tiny library for that in go a while ago. Go under Linux is not safe! Linux is not posix compliant and only sets the IDs for the current thread. Go almost always spawned more threads before initializing your code which are then still owned by root and are in the threadpool for your goroutines: https://github.com/sarnowski/mitigation/blob/master/mitigation.go https://github.com/sarnowski/mitigation/blob/master/mitigati... Comments mentions safe OS and golang bugticket.
- laumars 13y agoI'm guessing it might depend on the point in your Go code whrn you setuid as the tests that I did on my own project, setuid changed the user id as expacted then the processes were spawned. However you have given me reason to test my own code again and more thoroughly :-)
- chrissnell 13y agoIt's easy enough to run this as a non-root user and use your local software firewall port to forward port 80 inbound to whatever high port this server is listening on.
- laumars 13y agoIndeed, but that doesn't change the fact that it makes more sense having that functionality in the web server itself given its a standard and expected function in all web servers.