2024-10-27 23:45:52 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-16 21:19:45 +09:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2024-10-27 23:45:52 +09:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2024-11-16 21:19:45 +09:00
|
|
|
"slices"
|
2024-10-27 23:45:52 +09:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-11-04 12:32:14 +09:00
|
|
|
compPoison = "INVALIDINVALIDINVALIDINVALIDINVALID"
|
2024-10-27 23:45:52 +09:00
|
|
|
fsuConfFile = "/etc/fsurc"
|
|
|
|
envShim = "FORTIFY_SHIM"
|
|
|
|
envAID = "FORTIFY_APP_ID"
|
2024-11-16 21:19:45 +09:00
|
|
|
envGroups = "FORTIFY_GROUPS"
|
2024-11-09 11:50:56 +09:00
|
|
|
|
|
|
|
PR_SET_NO_NEW_PRIVS = 0x26
|
2024-10-27 23:45:52 +09:00
|
|
|
)
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
var (
|
|
|
|
Fmain = compPoison
|
|
|
|
Fshim = compPoison
|
|
|
|
)
|
2024-11-04 12:32:14 +09:00
|
|
|
|
2024-10-27 23:45:52 +09:00
|
|
|
func main() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetPrefix("fsu: ")
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
|
|
|
|
if os.Geteuid() != 0 {
|
|
|
|
log.Fatal("this program must be owned by uid 0 and have the setuid bit set")
|
|
|
|
}
|
|
|
|
|
|
|
|
puid := os.Getuid()
|
|
|
|
if puid == 0 {
|
|
|
|
log.Fatal("this program must not be started by root")
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
var fmain, fshim string
|
2024-11-04 12:32:14 +09:00
|
|
|
if p, ok := checkPath(Fmain); !ok {
|
2024-10-27 23:45:52 +09:00
|
|
|
log.Fatal("invalid fortify path, this copy of fsu is not compiled correctly")
|
2024-11-02 03:03:44 +09:00
|
|
|
} else {
|
|
|
|
fmain = p
|
2024-10-27 23:45:52 +09:00
|
|
|
}
|
2024-11-16 21:19:45 +09:00
|
|
|
if p, ok := checkPath(Fshim); !ok {
|
|
|
|
log.Fatal("invalid fshim path, this copy of fsu is not compiled correctly")
|
|
|
|
} else {
|
|
|
|
fshim = p
|
|
|
|
}
|
2024-10-27 23:45:52 +09:00
|
|
|
|
2024-10-28 18:52:23 +09:00
|
|
|
pexe := path.Join("/proc", strconv.Itoa(os.Getppid()), "exe")
|
|
|
|
if p, err := os.Readlink(pexe); err != nil {
|
|
|
|
log.Fatalf("cannot read parent executable path: %v", err)
|
|
|
|
} else if strings.HasSuffix(p, " (deleted)") {
|
|
|
|
log.Fatal("fortify executable has been deleted")
|
2024-11-02 03:03:44 +09:00
|
|
|
} else if p != fmain {
|
2024-10-28 18:52:23 +09:00
|
|
|
log.Fatal("this program must be started by fortify")
|
|
|
|
}
|
|
|
|
|
2024-10-27 23:45:52 +09:00
|
|
|
// uid = 1000000 +
|
|
|
|
// fid * 10000 +
|
|
|
|
// aid
|
|
|
|
uid := 1000000
|
|
|
|
|
|
|
|
// authenticate before accepting user input
|
|
|
|
if fid, ok := parseConfig(fsuConfFile, puid); !ok {
|
|
|
|
log.Fatalf("uid %d is not in the fsurc file", puid)
|
|
|
|
} else {
|
|
|
|
uid += fid * 10000
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
// allowed aid range 0 to 9999
|
|
|
|
if as, ok := os.LookupEnv(envAID); !ok {
|
|
|
|
log.Fatal("FORTIFY_APP_ID not set")
|
|
|
|
} else if aid, err := parseUint32Fast(as); err != nil || aid < 0 || aid > 9999 {
|
|
|
|
log.Fatal("invalid aid")
|
|
|
|
} else {
|
|
|
|
uid += aid
|
|
|
|
}
|
|
|
|
|
2024-10-27 23:45:52 +09:00
|
|
|
// pass through setup path to shim
|
|
|
|
var shimSetupPath string
|
|
|
|
if s, ok := os.LookupEnv(envShim); !ok {
|
2024-11-16 21:19:45 +09:00
|
|
|
// fortify requests target uid
|
|
|
|
// print resolved uid and exit
|
|
|
|
fmt.Print(uid)
|
|
|
|
os.Exit(0)
|
2024-10-27 23:45:52 +09:00
|
|
|
} else if !path.IsAbs(s) {
|
|
|
|
log.Fatal("FORTIFY_SHIM is not absolute")
|
|
|
|
} else {
|
|
|
|
shimSetupPath = s
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
// supplementary groups
|
|
|
|
var suppGroups, suppCurrent []int
|
|
|
|
|
|
|
|
if gs, ok := os.LookupEnv(envGroups); ok {
|
|
|
|
if cur, err := os.Getgroups(); err != nil {
|
|
|
|
log.Fatalf("cannot get groups: %v", err)
|
|
|
|
} else {
|
|
|
|
suppCurrent = cur
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse space-separated list of group ids
|
|
|
|
gss := bytes.Split([]byte(gs), []byte{' '})
|
|
|
|
suppGroups = make([]int, len(gss)+1)
|
|
|
|
for i, s := range gss {
|
|
|
|
if gid, err := strconv.Atoi(string(s)); err != nil {
|
|
|
|
log.Fatalf("cannot parse %q: %v", string(s), err)
|
|
|
|
} else if gid > 0 && gid != uid && gid != os.Getgid() && slices.Contains(suppCurrent, gid) {
|
|
|
|
suppGroups[i] = gid
|
|
|
|
} else {
|
|
|
|
log.Fatalf("invalid gid %d", gid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
suppGroups[len(suppGroups)-1] = uid
|
2024-10-27 23:45:52 +09:00
|
|
|
} else {
|
2024-11-16 21:19:45 +09:00
|
|
|
suppGroups = []int{uid}
|
2024-10-27 23:45:52 +09:00
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
// careful! users in the allowlist is effectively allowed to drop groups via fsu
|
|
|
|
|
2024-10-27 23:45:52 +09:00
|
|
|
if err := syscall.Setresgid(uid, uid, uid); err != nil {
|
|
|
|
log.Fatalf("cannot set gid: %v", err)
|
|
|
|
}
|
2024-11-16 21:19:45 +09:00
|
|
|
if err := syscall.Setgroups(suppGroups); err != nil {
|
|
|
|
log.Fatalf("cannot set supplementary groups: %v", err)
|
|
|
|
}
|
2024-10-27 23:45:52 +09:00
|
|
|
if err := syscall.Setresuid(uid, uid, uid); err != nil {
|
|
|
|
log.Fatalf("cannot set uid: %v", err)
|
|
|
|
}
|
2024-11-09 11:50:56 +09:00
|
|
|
if _, _, errno := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_NO_NEW_PRIVS, 1, 0); errno != 0 {
|
|
|
|
log.Fatalf("cannot set no_new_privs flag: %s", errno.Error())
|
|
|
|
}
|
2024-11-16 21:19:45 +09:00
|
|
|
if err := syscall.Exec(fshim, []string{"fshim"}, []string{envShim + "=" + shimSetupPath}); err != nil {
|
2024-10-27 23:45:52 +09:00
|
|
|
log.Fatalf("cannot start shim: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:32:14 +09:00
|
|
|
func checkPath(p string) (string, bool) {
|
|
|
|
return p, p != compPoison && p != "" && path.IsAbs(p)
|
|
|
|
}
|