| 1 | package webread |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/netip" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "runtime" |
| 15 | "sync" |
| 16 | "syscall" |
| 17 | "testing" |
| 18 | |
| 19 | "amadan.net/oficina/equipo/internal/websource" |
| 20 | ) |
| 21 | |
| 22 | // noChildReport is what the filtered child saw. |
| 23 | type noChildReport struct { |
| 24 | // Exec and Early are starting /bin/true from the main thread and from |
| 25 | // a thread that existed before the filter: the error, and whether it |
| 26 | // was EPERM. |
| 27 | Exec, Early string |
| 28 | ExecEPERM, EarlyEPERM bool |
| 29 | // Clone is a bare clone (a new process, as fork makes) from the main |
| 30 | // thread. |
| 31 | Clone string |
| 32 | CloneEPERM bool |
| 33 | // Clone3 is the same with clone3, which answers ENOSYS. |
| 34 | Clone3 string |
| 35 | Clone3NoSys bool |
| 36 | // Threads is how many goroutines, each on its own OS thread, ran after |
| 37 | // the filter; Fetched what the fetch phase read. |
| 38 | Threads int |
| 39 | Fetched string |
| 40 | } |
| 41 | |
| 42 | // noChildMain runs in a child of the test binary: a thread made before |
| 43 | // the filter waits; then it limits itself as the fetcher does, tries to |
| 44 | // start /bin/true (from the main thread and that earlier thread) and a |
| 45 | // bare clone, makes |
| 46 | // new threads, and runs a fetch against the parent's server. |
| 47 | func noChildMain(args []string) int { |
| 48 | early, done := make(chan struct{}), make(chan error) |
| 49 | go func() { |
| 50 | runtime.LockOSThread() |
| 51 | <-early |
| 52 | done <- exec.Command("/bin/true").Run() |
| 53 | }() |
| 54 | if err := limitSelf(); err != nil { |
| 55 | fmt.Fprintln(os.Stderr, err) |
| 56 | return 1 |
| 57 | } |
| 58 | var r noChildReport |
| 59 | err := exec.Command("/bin/true").Run() |
| 60 | r.Exec, r.ExecEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) |
| 61 | err = tryClone() |
| 62 | r.Clone, r.CloneEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) |
| 63 | err = tryClone3() |
| 64 | r.Clone3, r.Clone3NoSys = fmt.Sprint(err), errors.Is(err, syscall.ENOSYS) |
| 65 | close(early) |
| 66 | err = <-done |
| 67 | r.Early, r.EarlyEPERM = fmt.Sprint(err), errors.Is(err, syscall.EPERM) |
| 68 | var wg sync.WaitGroup |
| 69 | var mu sync.Mutex |
| 70 | for range 16 { |
| 71 | wg.Add(1) |
| 72 | go func() { |
| 73 | defer wg.Done() |
| 74 | runtime.LockOSThread() |
| 75 | mu.Lock() |
| 76 | r.Threads++ |
| 77 | mu.Unlock() |
| 78 | }() |
| 79 | } |
| 80 | wg.Wait() |
| 81 | d := defaults() |
| 82 | d.lookup = func(context.Context, string) ([]netip.Addr, error) { |
| 83 | return []netip.Addr{netip.MustParseAddr(public1)}, nil |
| 84 | } |
| 85 | d.dial = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| 86 | return (&net.Dialer{}).DialContext(ctx, network, args[0]) |
| 87 | } |
| 88 | u, _ := websource.Parse("http://shop.example.com/") |
| 89 | p := Plan{Sources: []Source{{ID: "s1", URL: u, Origin: OriginPaul}}, Inventory: testInventory} |
| 90 | dir, err := os.MkdirTemp(args[1], "out-") |
| 91 | if err == nil { |
| 92 | _, err = fetch(context.Background(), p, dir, d) |
| 93 | } |
| 94 | if err == nil { |
| 95 | var b Bundle |
| 96 | if b, err = ReadBundle(dir); err == nil && len(b.Files) == 1 { |
| 97 | r.Fetched = string(b.Files[0].Data) |
| 98 | } |
| 99 | } |
| 100 | if err != nil { |
| 101 | r.Fetched = "error: " + err.Error() |
| 102 | } |
| 103 | json.NewEncoder(os.Stdout).Encode(r) |
| 104 | return 0 |
| 105 | } |
| 106 | |
| 107 | // TestFetcherStartsNoChildren (Astra security finding 4): once the |
| 108 | // fetcher has limited itself, no thread of it can start a program |
| 109 | // (EPERM), while threads and the fetch itself still work. |
| 110 | func TestFetcherStartsNoChildren(t *testing.T) { |
| 111 | if runtime.GOOS != "linux" { |
| 112 | t.Skip("the fetcher runs only on Linux") |
| 113 | } |
| 114 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("hello")) })) |
| 115 | defer srv.Close() |
| 116 | self, err := os.Executable() |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | out, err := exec.Command(self, "webread-nochild", srv.Listener.Addr().String(), t.TempDir()).Output() |
| 121 | if err != nil { |
| 122 | t.Fatalf("child: %v %s", err, out) |
| 123 | } |
| 124 | var r noChildReport |
| 125 | if err := json.Unmarshal(out, &r); err != nil { |
| 126 | t.Fatalf("%v: %s", err, out) |
| 127 | } |
| 128 | if !r.ExecEPERM || !r.EarlyEPERM || !r.CloneEPERM || !r.Clone3NoSys { |
| 129 | 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) |
| 130 | } |
| 131 | if r.Threads != 16 || r.Fetched != "hello" { |
| 132 | t.Fatalf("after the filter: %+v", r) |
| 133 | } |
| 134 | } |
| 135 | |