Resolve only the fixed nfq2 package alias into trusted binaries parents, hold the no-follow engine descriptor and verify inode/hash against the running worker before nft activation. Keep Lua/import guards and completed-proof publication unchanged. Co-authored-by: GPT-6 <gpt-6@openai.com>
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// Only the packaged nfq2 alias is followed. All target parents and the final
|
|
// binary remain no-symlink, root-owned and not group/other writable. As with
|
|
// the runtime contract, a malicious root racing checks is outside the boundary.
|
|
type qualificationBinary struct {
|
|
root, path string
|
|
file *os.File
|
|
info, alias os.FileInfo
|
|
hash string
|
|
}
|
|
|
|
func qualificationTrusted(info os.FileInfo) bool {
|
|
st, ok := info.Sys().(*syscall.Stat_t)
|
|
return ok && st.Uid == 0 && info.Mode().Perm()&0022 == 0
|
|
}
|
|
|
|
func qualificationParents(path, boundary string) error {
|
|
for dir := filepath.Dir(path); ; dir = filepath.Dir(dir) {
|
|
info, e := os.Lstat(dir)
|
|
if e != nil || !info.IsDir() || !qualificationTrusted(info) {
|
|
return errResourcePlan
|
|
}
|
|
if dir == boundary {
|
|
return nil
|
|
}
|
|
if dir == "/" {
|
|
return errResourcePlan
|
|
}
|
|
}
|
|
}
|
|
|
|
func qualificationEnginePath(root string) (string, os.FileInfo, error) {
|
|
alias := filepath.Join(root, "nfq2/nfqws2")
|
|
boundary := root // Private fixture seam; production also checks /opt and /.
|
|
if root == "/opt/zapret2" {
|
|
boundary = "/"
|
|
}
|
|
if qualificationParents(alias, boundary) != nil {
|
|
return "", nil, errResourcePlan
|
|
}
|
|
info, e := os.Lstat(alias)
|
|
if e != nil || info.Mode()&os.ModeSymlink == 0 {
|
|
return "", nil, errResourcePlan
|
|
}
|
|
st, ok := info.Sys().(*syscall.Stat_t)
|
|
if !ok || st.Uid != 0 {
|
|
return "", nil, errResourcePlan
|
|
}
|
|
link, e := os.Readlink(alias)
|
|
if e != nil {
|
|
return "", nil, errResourcePlan
|
|
}
|
|
path := link
|
|
if !filepath.IsAbs(path) {
|
|
path = filepath.Join(filepath.Dir(alias), path)
|
|
}
|
|
path = filepath.Clean(path)
|
|
if !strings.HasPrefix(path, filepath.Join(root, "binaries")+"/") || filepath.Base(path) != "nfqws2" || qualificationParents(path, boundary) != nil {
|
|
return "", nil, errResourcePlan
|
|
}
|
|
return path, info, nil
|
|
}
|
|
|
|
func qualificationBinaryHash(f *os.File) (os.FileInfo, string, error) {
|
|
info, e := f.Stat()
|
|
if e != nil || !info.Mode().IsRegular() || !qualificationTrusted(info) || info.Mode().Perm()&0111 == 0 || info.Size() > 32<<20 {
|
|
return nil, "", errResourcePlan
|
|
}
|
|
b, e := io.ReadAll(io.NewSectionReader(f, 0, 32<<20+1))
|
|
if e != nil || int64(len(b)) != info.Size() {
|
|
return nil, "", errResourcePlan
|
|
}
|
|
return info, planDigest(b), nil
|
|
}
|
|
|
|
func openQualificationEngine(root string) (*qualificationBinary, error) {
|
|
path, alias, e := qualificationEnginePath(root)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
fd, e := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
|
|
if e != nil {
|
|
return nil, errResourcePlan
|
|
}
|
|
f := os.NewFile(uintptr(fd), path)
|
|
info, hash, e := qualificationBinaryHash(f)
|
|
b := &qualificationBinary{root: root, path: path, alias: alias, file: f, info: info, hash: hash}
|
|
if e != nil || b.validate() != nil {
|
|
f.Close()
|
|
return nil, errResourcePlan
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func (b *qualificationBinary) validate() error {
|
|
path, alias, e := qualificationEnginePath(b.root)
|
|
if e != nil || path != b.path || !os.SameFile(alias, b.alias) {
|
|
return errResourcePlan
|
|
}
|
|
current, e := os.Lstat(path)
|
|
if e != nil || !current.Mode().IsRegular() || !os.SameFile(current, b.info) || !qualificationTrusted(current) {
|
|
return errResourcePlan
|
|
}
|
|
info, hash, e := qualificationBinaryHash(b.file)
|
|
if e != nil || !os.SameFile(info, b.info) || hash != b.hash {
|
|
return errResourcePlan
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *qualificationBinary) running(procExe string) error {
|
|
// /proc/PID/exe is a kernel-owned link, not a catalogue path. Compare the
|
|
// opened running inode and bytes, never the alias spelling from readlink.
|
|
f, e := os.Open(procExe)
|
|
if e != nil {
|
|
return errResourcePlan
|
|
}
|
|
defer f.Close()
|
|
info, hash, e := qualificationBinaryHash(f)
|
|
if e != nil || !os.SameFile(info, b.info) || hash != b.hash {
|
|
return errResourcePlan
|
|
}
|
|
return nil
|
|
}
|