| 1 | //go:build linux |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // M3, Linux: a symlink, a FIFO, a directory and an oversized --policy file |
| 15 | // are refused before anything is read; the FIFO's open does not wait for a |
| 16 | // writer. |
| 17 | func TestInstallChannelPolicyOpenRefusesWhatIsNotAPolicyFile(t *testing.T) { |
| 18 | dir := t.TempDir() |
| 19 | // M3: a symlink, a FIFO, a directory and an oversized file are refused |
| 20 | // before anything is read; the FIFO's open does not wait for a writer. |
| 21 | raw0 := signedPolicyBytes(t) |
| 22 | real := filepath.Join(dir, "real.json") |
| 23 | if err := os.WriteFile(real, raw0, 0o600); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | link := filepath.Join(dir, "link.json") |
| 27 | if err := os.Symlink(real, link); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | fifo := filepath.Join(dir, "fifo.json") |
| 31 | if err := syscall.Mkfifo(fifo, 0o600); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | big := filepath.Join(dir, "big.json") |
| 35 | if err := os.WriteFile(big, append(raw0, bytes.Repeat([]byte(" "), channelPolicyLimit)...), 0o600); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | for _, path := range []string{link, fifo, dir, big} { |
| 39 | if _, err := readChannelPolicy(path); err == nil || !strings.HasPrefix(err.Error(), "the policy "+path+" could not be read, so nothing was installed: ") { |
| 40 | t.Fatalf("%s: %v", path, err) |
| 41 | } |
| 42 | } |
| 43 | if got, err := readChannelPolicy(real); err != nil || !bytes.Equal(got, raw0) { |
| 44 | t.Fatalf("the real file: %v", err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // R1, Linux: --identity's file goes through --policy's hardened open, so a |
| 49 | // symlink, a FIFO, a directory and an oversized file are refused before |
| 50 | // anything is read. |
| 51 | func TestInstallChannelIdentityOpenRefusesWhatIsNotAnIdentityFile(t *testing.T) { |
| 52 | dir := t.TempDir() |
| 53 | policy := filepath.Join(dir, "policy.json") |
| 54 | if err := os.WriteFile(policy, signedPolicyBytes(t), 0o600); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | raw0 := []byte(`{"payload":"","signature":""}`) |
| 58 | real := filepath.Join(dir, "real.json") |
| 59 | if err := os.WriteFile(real, raw0, 0o600); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | link := filepath.Join(dir, "link.json") |
| 63 | if err := os.Symlink(real, link); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | fifo := filepath.Join(dir, "fifo.json") |
| 67 | if err := syscall.Mkfifo(fifo, 0o600); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | big := filepath.Join(dir, "big.json") |
| 71 | if err := os.WriteFile(big, append(raw0, bytes.Repeat([]byte(" "), channelPolicyLimit)...), 0o600); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | for _, path := range []string{link, fifo, dir, big} { |
| 75 | if _, _, err := readChannelInputs(policy, path); err == nil || !strings.HasPrefix(err.Error(), "the identity "+path+" could not be read, so nothing was installed: ") { |
| 76 | t.Fatalf("%s: %v", path, err) |
| 77 | } |
| 78 | } |
| 79 | if _, got, err := readChannelInputs(policy, real); err != nil || !bytes.Equal(got, raw0) { |
| 80 | t.Fatalf("the real file: %v", err) |
| 81 | } |
| 82 | } |
| 83 | |