2020-09-14 22:21:05 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"log"
|
2020-09-15 22:14:12 -04:00
|
|
|
"path/filepath"
|
2020-09-14 22:21:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Restrict access to the files specified in config in an OS-dependent way.
|
|
|
|
// The OpenBSD implementation uses pledge(2) and unveil(2) to restrict the
|
2020-09-16 23:24:41 -04:00
|
|
|
// operations available to the molly brown executable. Please note that (S)CGI
|
2020-09-16 23:49:03 -04:00
|
|
|
// processes that molly brown spawns or communicates with are unrestricted
|
2020-09-16 23:24:41 -04:00
|
|
|
// and should pledge their own restrictions and unveil their own files.
|
2020-09-14 22:21:05 -04:00
|
|
|
func enableSecurityRestrictions(config Config, errorLog *log.Logger) {
|
2020-09-15 22:14:12 -04:00
|
|
|
|
2020-09-16 23:24:41 -04:00
|
|
|
// Unveil the configured document base as readable.
|
|
|
|
log.Println("Unveiling \"" + config.DocBase + "\" as readable.")
|
2020-09-15 22:14:12 -04:00
|
|
|
err := unix.Unveil(config.DocBase, "r")
|
2020-09-14 22:21:05 -04:00
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Could not unveil DocBase: " + err.Error())
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-16 23:24:41 -04:00
|
|
|
|
|
|
|
// Unveil cgi path globs as executable.
|
2020-09-14 22:21:05 -04:00
|
|
|
for _, cgiPath := range config.CGIPaths {
|
2020-09-15 22:14:12 -04:00
|
|
|
cgiGlobbedPaths, err := filepath.Glob(cgiPath)
|
|
|
|
for _, cgiGlobbedPath := range cgiGlobbedPaths {
|
|
|
|
log.Println("Unveiling \"" + cgiGlobbedPath + "\" as executable.")
|
|
|
|
err = unix.Unveil(cgiGlobbedPath, "rx")
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Could not unveil CGIPaths: " + err.Error())
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-14 22:21:05 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 23:24:41 -04:00
|
|
|
|
|
|
|
// Unveil scgi socket paths as readable and writeable.
|
|
|
|
for _, scgiSocket := range config.SCGIPaths {
|
|
|
|
log.Println("Unveiling \"" + scgiSocket + "\" as read/write.")
|
|
|
|
err = unix.Unveil(scgiSocket, "rw")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize the unveil list.
|
|
|
|
// Any files not whitelisted above won't be accessible to molly brown.
|
2020-09-14 22:21:05 -04:00
|
|
|
err = unix.UnveilBlock()
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Could not block unveil: " + err.Error())
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-15 22:14:12 -04:00
|
|
|
|
|
|
|
// Pledge to only use stdio, inet, and rpath syscalls.
|
|
|
|
promises := "stdio inet rpath"
|
|
|
|
if len(config.CGIPaths) > 0 {
|
2020-09-16 23:24:41 -04:00
|
|
|
// If CGI paths have been specified, also allow exec syscalls.
|
2020-09-15 22:14:12 -04:00
|
|
|
promises += " exec proc"
|
|
|
|
}
|
2020-09-16 23:24:41 -04:00
|
|
|
if len(config.SCGIPaths) > 0 {
|
|
|
|
// If SCGI paths have been specified, also allow unix sockets.
|
|
|
|
promises += " unix"
|
|
|
|
}
|
2020-09-15 22:14:12 -04:00
|
|
|
err = unix.PledgePromises(promises)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Println("Could not pledge: " + err.Error())
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-14 22:21:05 -04:00
|
|
|
}
|