package webread import ( "context" "encoding/json" "errors" "fmt" "net" "net/http" "net/http/httptest" "net/netip" "os" "os/exec" "runtime" "sync" "syscall" "testing" "amadan.net/oficina/equipo/internal/websource" ) // noChildReport is what the filtered child saw. type noChildReport struct { // Exec and Early are starting /bin/true from the main thread and from // a thread that existed before the filter: the error, and whether it // was EPERM. Exec, Early string ExecEPERM, EarlyEPERM bool // Clone is a bare clone (a new process, as fork makes) from the main // thread. Clone string CloneEPERM bool // Clone3 is the same with clone3, which answers ENOSYS. Clone3 string Clone3NoSys bool // Threads is how many goroutines, each on its own OS thread, ran after // the filter; Fetched what the fetch phase read. Threads int Fetched string } // noChildMain runs in a child of the test binary: a thread made before // the filter waits; then it limits itself as the fetcher does, tries to // start /bin/true (from the main thread and that earlier thread) and a // bare clone, makes // new threads, and runs a fetch against the parent's server. func noChildMain(args []string) int { early, done := make(chan struct{}), make(chan error) go func() { runtime.LockOSThread() <-early done <- exec.Command("/bin/true").Run() }() if err := limitSelf(); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } var r noChildReport err := exec.Command("/bin/true").Run() r.Exec, r.ExecEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) err = tryClone() r.Clone, r.CloneEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) err = tryClone3() r.Clone3, r.Clone3NoSys = fmt.Sprint(err), errors.Is(err, syscall.ENOSYS) close(early) err = <-done r.Early, r.EarlyEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) var wg sync.WaitGroup var mu sync.Mutex for range 16 { wg.Add(1) go func() { defer wg.Done() runtime.LockOSThread() mu.Lock() r.Threads++ mu.Unlock() }() } wg.Wait() d := defaults() d.lookup = func(context.Context, string) ([]netip.Addr, error) { return []netip.Addr{netip.MustParseAddr(public1)}, nil } d.dial = func(ctx context.Context, network, _ string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, network, args[0]) } u, _ := websource.Parse("http://shop.example.com/") p := Plan{Sources: []Source{{ID: "s1", URL: u, Origin: OriginPaul}}, Inventory: testInventory} dir, err := os.MkdirTemp(args[1], "out-") if err == nil { _, err = fetch(context.Background(), p, dir, d) } if err == nil { var b Bundle if b, err = ReadBundle(dir); err == nil && len(b.Files) == 1 { r.Fetched = string(b.Files[0].Data) } } if err != nil { r.Fetched = "error: " + err.Error() } json.NewEncoder(os.Stdout).Encode(r) return 0 } // TestFetcherStartsNoChildren (Astra security finding 4): once the // fetcher has limited itself, no thread of it can start a program // (EPERM), while threads and the fetch itself still work. func TestFetcherStartsNoChildren(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("the fetcher runs only on Linux") } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("hello")) })) defer srv.Close() self, err := os.Executable() if err != nil { t.Fatal(err) } out, err := exec.Command(self, "webread-nochild", srv.Listener.Addr().String(), t.TempDir()).Output() if err != nil { t.Fatalf("child: %v %s", err, out) } var r noChildReport if err := json.Unmarshal(out, &r); err != nil { t.Fatalf("%v: %s", err, out) } if !r.ExecEPERM || !r.EarlyEPERM || !r.CloneEPERM || !r.Clone3NoSys { t.Fatalf("started a program or process: main thread %q, earlier thread %q, clone %q, clone3 %q", r.Exec, r.Early, r.Clone, r.Clone3) } if r.Threads != 16 || r.Fetched != "hello" { t.Fatalf("after the filter: %+v", r) } }