treewide: reject impossible user-supplied fd
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m36s
Test / Hakurei (push) Successful in 4m33s
Test / Sandbox (race detector) (push) Successful in 4m30s
Test / Hpkg (push) Successful in 4m53s
Test / Hakurei (race detector) (push) Successful in 6m12s
Test / Flake checks (push) Successful in 1m31s

These are all trusted user input, however this check reduces the likelihood of hard to debug errors.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-11-10 20:31:26 +09:00
parent bb92e3ada9
commit d7e0104ae4
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 24 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"syscall" "syscall"
"hakurei.app/hst" "hakurei.app/hst"
"hakurei.app/internal/outcome"
"hakurei.app/internal/store" "hakurei.app/internal/store"
"hakurei.app/message" "hakurei.app/message"
) )
@ -53,14 +54,23 @@ func tryFd(msg message.Msg, name string) io.ReadCloser {
} }
return nil return nil
} else { } else {
if v < 3 { // reject standard streams
return nil
}
msg.Verbosef("trying config stream from %d", v) msg.Verbosef("trying config stream from %d", v)
fd := uintptr(v) fd := uintptr(v)
if _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_GETFD, 0); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_GETFD, 0); errno != 0 {
if errors.Is(errno, syscall.EBADF) { if errors.Is(errno, syscall.EBADF) { // reject bad fd
return nil return nil
} }
log.Fatalf("cannot get fd %d: %v", fd, errno) log.Fatalf("cannot get fd %d: %v", fd, errno)
} }
if outcome.IsPollDescriptor(fd) { // reject runtime internals
log.Fatalf("invalid config stream %d", fd)
}
return os.NewFile(fd, strconv.Itoa(v)) return os.NewFile(fd, strconv.Itoa(v))
} }
} }

View File

@ -4,13 +4,26 @@ import (
"context" "context"
"log" "log"
"time" "time"
_ "unsafe" // for go:linkname
"hakurei.app/hst" "hakurei.app/hst"
"hakurei.app/message" "hakurei.app/message"
) )
// IsPollDescriptor reports whether fd is the descriptor being used by the poller.
//
//go:linkname IsPollDescriptor internal/poll.IsPollDescriptor
func IsPollDescriptor(fd uintptr) bool
// Main runs an app according to [hst.Config] and terminates. Main does not return. // Main runs an app according to [hst.Config] and terminates. Main does not return.
func Main(ctx context.Context, msg message.Msg, config *hst.Config, fd int) { func Main(ctx context.Context, msg message.Msg, config *hst.Config, fd int) {
// avoids runtime internals or standard streams
if fd >= 0 {
if IsPollDescriptor(uintptr(fd)) || fd < 3 {
log.Fatalf("invalid identifier fd %d", fd)
}
}
var id hst.ID var id hst.ID
if err := hst.NewInstanceID(&id); err != nil { if err := hst.NewInstanceID(&id); err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())