| 1 | |
| 2 | |
| 3 | package reversesshfs |
| 4 | |
| 5 | import ( |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "unicode/utf8" |
| 12 | ) |
| 13 | |
| 14 | var sameNameSeeds = [][2]string{ |
| 15 | {".git", ".GIT"}, |
| 16 | {".git", ".g" + zwnj + "it"}, |
| 17 | {".git", bom + ".git"}, |
| 18 | {"café", "café"}, |
| 19 | {"glass", "glaß"}, |
| 20 | {"k", "K"}, |
| 21 | {"hooks", "HOOKS"}, |
| 22 | {".husky", ".Husky"}, |
| 23 | {"tools", "toòls"}, |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | func FuzzSameNameFS(f *testing.F) { |
| 31 | for _, s := range sameNameSeeds { |
| 32 | f.Add(s[0], s[1]) |
| 33 | } |
| 34 | dir := f.TempDir() |
| 35 | f.Fuzz(func(t *testing.T, name, other string) { |
| 36 | if !utf8.ValidString(name) || !utf8.ValidString(other) || !plainName(name) || !plainName(other) { |
| 37 | return |
| 38 | } |
| 39 | d, err := os.MkdirTemp(dir, "") |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | defer os.RemoveAll(d) |
| 44 | if err := os.Mkdir(filepath.Join(d, name), 0o755); err != nil { |
| 45 | return |
| 46 | } |
| 47 | a, err := os.Stat(filepath.Join(d, name)) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | b, err := os.Stat(filepath.Join(d, other)) |
| 52 | if err != nil || !os.SameFile(a, b) { |
| 53 | return |
| 54 | } |
| 55 | if !sameName(other, name) { |
| 56 | t.Errorf("%q opens %q on this file system, and sameName says it does not", other, name) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | func plainName(s string) bool { |
| 63 | return s != "" && s != "." && s != ".." && len(s) < 200 && !strings.ContainsAny(s, "/\\:\x00") && |
| 64 | !strings.HasSuffix(s, ".") && !strings.HasSuffix(s, " ") |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | func FuzzRootedOps(f *testing.F) { |
| 72 | for _, seed := range [][]byte{ |
| 73 | {0, 3, 1, 2, 5}, |
| 74 | {5, 9, 0, 4, 7, 2, 6, 0}, |
| 75 | {6, 2, 2, 3, 1, 0, 0, 9, 3}, |
| 76 | {7, 8, 10, 0, 12, 1, 1, 0}, |
| 77 | {3, 9, 2, 0, 0, 11, 10, 0, 0, 13}, |
| 78 | } { |
| 79 | f.Add(seed) |
| 80 | } |
| 81 | f.Fuzz(func(t *testing.T, ops []byte) { |
| 82 | if len(ops) > 64 { |
| 83 | return |
| 84 | } |
| 85 | c, root := setupRooted(t, false) |
| 86 | secretDir := filepath.Join(filepath.Dir(root), "secret-dir") |
| 87 | if err := os.MkdirAll(secretDir, 0o755); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | |
| 91 | secret := strings.Repeat("SECRET", 200)[:1000] |
| 92 | if err := os.WriteFile(filepath.Join(secretDir, "s"), []byte(secret), 0o644); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | comps := []string{"", ".", "..", ".git", ".GIT", ".g" + zwnj + "it", "hooks", "config", "src", "gitlink", "configlink", "outside", "secret-dir", "s", "x"} |
| 96 | bases := []string{"", root + "/", "/", filepath.Dir(root) + "/"} |
| 97 | next := func() byte { |
| 98 | if len(ops) == 0 { |
| 99 | return 0 |
| 100 | } |
| 101 | b := ops[0] |
| 102 | ops = ops[1:] |
| 103 | return b |
| 104 | } |
| 105 | pathOf := func() string { |
| 106 | b := next() |
| 107 | p := bases[int(b>>4)%len(bases)] |
| 108 | for i := 0; i <= int(b&3); i++ { |
| 109 | if i > 0 { |
| 110 | p += "/" |
| 111 | } |
| 112 | p += comps[int(next())%len(comps)] |
| 113 | } |
| 114 | if p == "" { |
| 115 | p = "." |
| 116 | } |
| 117 | return p |
| 118 | } |
| 119 | seen := func(b []byte) { |
| 120 | if strings.Contains(string(b), "SECRET") { |
| 121 | t.Fatalf("read a file outside the root: %q", b) |
| 122 | } |
| 123 | } |
| 124 | for len(ops) > 0 { |
| 125 | switch next() % 13 { |
| 126 | case 0: |
| 127 | if fh, err := c.OpenFile(pathOf(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC); err == nil { |
| 128 | fh.Write([]byte("PWN")) |
| 129 | fh.Close() |
| 130 | } |
| 131 | case 1: |
| 132 | if fh, err := c.OpenFile(pathOf(), os.O_WRONLY|os.O_APPEND); err == nil { |
| 133 | fh.Write([]byte("PWN")) |
| 134 | fh.Close() |
| 135 | } |
| 136 | case 2: |
| 137 | if fh, err := c.Open(pathOf()); err == nil { |
| 138 | b, _ := io.ReadAll(fh) |
| 139 | fh.Close() |
| 140 | seen(b) |
| 141 | } |
| 142 | case 3: |
| 143 | c.Mkdir(pathOf()) |
| 144 | case 4: |
| 145 | c.Remove(pathOf()) |
| 146 | case 5: |
| 147 | c.PosixRename(pathOf(), pathOf()) |
| 148 | case 6: |
| 149 | c.Rename(pathOf(), pathOf()) |
| 150 | case 7: |
| 151 | c.Symlink(pathOf(), pathOf()) |
| 152 | case 8: |
| 153 | c.Link(pathOf(), pathOf()) |
| 154 | case 9: |
| 155 | c.Chmod(pathOf(), 0o777) |
| 156 | case 10: |
| 157 | c.Truncate(pathOf(), 0) |
| 158 | case 11: |
| 159 | if ents, err := c.ReadDir(pathOf()); err == nil { |
| 160 | for _, e := range ents { |
| 161 | if e.Size() == int64(len(secret)) { |
| 162 | t.Fatalf("listed a folder outside the root") |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | case 12: |
| 167 | if l, err := c.ReadLink(pathOf()); err == nil { |
| 168 | seen([]byte(l)) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | assertUnchanged(t, root) |
| 173 | if b, err := os.ReadFile(filepath.Join(secretDir, "s")); err != nil || string(b) != secret { |
| 174 | t.Fatalf("secret-dir/s changed: %q, %v", b, err) |
| 175 | } |
| 176 | if ents, err := os.ReadDir(secretDir); err != nil || len(ents) != 1 { |
| 177 | t.Fatalf("secret-dir changed: %v, %v", ents, err) |
| 178 | } |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | func FuzzSameNameEqualFold(f *testing.F) { |
| 185 | for _, s := range sameNameSeeds { |
| 186 | f.Add(s[0], s[1]) |
| 187 | } |
| 188 | f.Fuzz(func(t *testing.T, a, b string) { |
| 189 | if strings.EqualFold(a, b) && !sameName(a, b) { |
| 190 | t.Errorf("EqualFold(%q, %q), and sameName says they differ", a, b) |
| 191 | } |
| 192 | if sameName(a, b) != sameName(b, a) { |
| 193 | t.Errorf("sameName(%q, %q) is not symmetric", a, b) |
| 194 | } |
| 195 | }) |
| 196 | } |