|
| 1 | +import ContainerizationArchive |
| 2 | +import ContainerizationEXT4 |
| 3 | +import Foundation |
| 4 | +import SystemPackage |
| 5 | +import Testing |
| 6 | + |
| 7 | +struct EXT4WhiteoutTests { |
| 8 | + |
| 9 | + private func makeTempFileURL(prefix: String) throws -> URL { |
| 10 | + let base = FileManager.default.temporaryDirectory |
| 11 | + let url = base.appendingPathComponent("\(prefix)-\(UUID().uuidString)") |
| 12 | + return url |
| 13 | + } |
| 14 | + |
| 15 | + private func writeLayerWithOpaqueWhiteout(to url: URL) throws { |
| 16 | + let writer = try ArchiveWriter( |
| 17 | + format: .pax, |
| 18 | + filter: .gzip, |
| 19 | + file: url |
| 20 | + ) |
| 21 | + |
| 22 | + let ts = Date() |
| 23 | + |
| 24 | + let entry = WriteEntry() |
| 25 | + entry.modificationDate = ts |
| 26 | + entry.creationDate = ts |
| 27 | + entry.owner = 0 |
| 28 | + entry.group = 0 |
| 29 | + |
| 30 | + entry.fileType = .directory |
| 31 | + entry.permissions = 0o755 |
| 32 | + |
| 33 | + entry.path = "usr" |
| 34 | + try writer.writeEntry(entry: entry, data: nil) |
| 35 | + |
| 36 | + entry.path = "usr/local" |
| 37 | + try writer.writeEntry(entry: entry, data: nil) |
| 38 | + |
| 39 | + entry.path = "usr/local/bin" |
| 40 | + try writer.writeEntry(entry: entry, data: nil) |
| 41 | + |
| 42 | + entry.fileType = .regular |
| 43 | + entry.permissions = 0o644 |
| 44 | + |
| 45 | + let fooData = Data("hello\n".utf8) |
| 46 | + entry.path = "usr/local/bin/foo" |
| 47 | + entry.size = Int64(fooData.count) |
| 48 | + try writer.writeEntry(entry: entry, data: fooData) |
| 49 | + |
| 50 | + entry.fileType = .regular |
| 51 | + entry.permissions = 0o000 |
| 52 | + entry.size = 0 |
| 53 | + entry.path = "usr//.wh..wh..opq" |
| 54 | + try writer.writeEntry(entry: entry, data: nil) |
| 55 | + |
| 56 | + try writer.finishEncoding() |
| 57 | + } |
| 58 | + |
| 59 | + private func withFormatter<T>( |
| 60 | + prefix: String = "ext4-whiteout", |
| 61 | + blockSize: UInt32 = 4096, |
| 62 | + minDiskSize: UInt64 = 16.mib(), |
| 63 | + _ body: (EXT4.Formatter, FilePath) throws -> T |
| 64 | + ) throws -> T { |
| 65 | + let imageURL = try makeTempFileURL(prefix: prefix) |
| 66 | + let imagePath = FilePath(imageURL.path) |
| 67 | + |
| 68 | + defer { |
| 69 | + try? FileManager.default.removeItem(at: imageURL) |
| 70 | + } |
| 71 | + |
| 72 | + let formatter = try EXT4.Formatter( |
| 73 | + imagePath, |
| 74 | + blockSize: blockSize, |
| 75 | + minDiskSize: minDiskSize |
| 76 | + ) |
| 77 | + |
| 78 | + let result = try body(formatter, imagePath) |
| 79 | + return result |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + func unpack_with_opaque_whiteout_path_does_not_stack_overflow_and_cleans_directory() throws { |
| 84 | + let layerURL = try makeTempFileURL(prefix: "ext4-wh-layer") |
| 85 | + defer { |
| 86 | + try? FileManager.default.removeItem(at: layerURL) |
| 87 | + } |
| 88 | + |
| 89 | + try writeLayerWithOpaqueWhiteout(to: layerURL) |
| 90 | + |
| 91 | + try withFormatter { formatter, imagePath in |
| 92 | + try formatter.unpack( |
| 93 | + source: FilePath(layerURL.path).url, |
| 94 | + format: .pax, |
| 95 | + compression: .gzip, |
| 96 | + progress: nil |
| 97 | + ) |
| 98 | + |
| 99 | + try formatter.close() |
| 100 | + |
| 101 | + let reader = try EXT4.EXT4Reader(blockDevice: FilePath(imagePath.description)) |
| 102 | + |
| 103 | + #expect(try reader.exists(FilePath("/usr/local/bin")) == false) |
| 104 | + |
| 105 | + #expect(try reader.exists(FilePath("/usr/local/bin/foo")) == false) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + func directoryWhiteout_from_wh_opq_path_with_repeated_slashes_terminates() throws { |
| 111 | + try withFormatter { formatter, _ in |
| 112 | + try formatter.create( |
| 113 | + path: FilePath("/usr"), |
| 114 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 115 | + ) |
| 116 | + try formatter.create( |
| 117 | + path: FilePath("/usr/local"), |
| 118 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 119 | + ) |
| 120 | + try formatter.create( |
| 121 | + path: FilePath("/usr/local/bin"), |
| 122 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 123 | + ) |
| 124 | + try formatter.create( |
| 125 | + path: FilePath("/usr/local/bin/foo"), |
| 126 | + mode: EXT4.Inode.Mode(.S_IFREG, 0o644) |
| 127 | + ) |
| 128 | + try formatter.create( |
| 129 | + path: FilePath("/usr/local/bin/bar"), |
| 130 | + mode: EXT4.Inode.Mode(.S_IFREG, 0o644) |
| 131 | + ) |
| 132 | + |
| 133 | + let whiteoutEntry = FilePath("//usr//.wh..wh..opq") |
| 134 | + let directoryToWhiteout = whiteoutEntry.dir |
| 135 | + let normalized = directoryToWhiteout.lexicallyNormalized() |
| 136 | + #expect(normalized == FilePath("/usr")) |
| 137 | + try formatter.unlink(path: directoryToWhiteout, directoryWhiteout: true) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Test the exact recursion attack sequence: |
| 142 | + /// create /_d |
| 143 | + /// create symlink / -> /_ |
| 144 | + /// create /_ |
| 145 | + /// create symlink / -> /_ |
| 146 | + /// |
| 147 | + /// This creates a recursive symlink structure that can cause infinite recursion |
| 148 | + /// during directory traversal operations. |
| 149 | + @Test |
| 150 | + func recursion_attack_sequence_does_not_cause_infinite_recursion() throws { |
| 151 | + try withFormatter { formatter, _ in |
| 152 | + // Step 1: create /_d |
| 153 | + try formatter.create( |
| 154 | + path: FilePath("/_d"), |
| 155 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 156 | + ) |
| 157 | + |
| 158 | + // Step 2: create symlink / -> /_ |
| 159 | + try formatter.create( |
| 160 | + path: FilePath("/"), |
| 161 | + link: FilePath("/_"), |
| 162 | + mode: EXT4.Inode.Mode(.S_IFLNK, 0o777) |
| 163 | + ) |
| 164 | + |
| 165 | + try formatter.create( |
| 166 | + path: FilePath("/_"), |
| 167 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 168 | + ) |
| 169 | + |
| 170 | + try formatter.create( |
| 171 | + path: FilePath("/"), |
| 172 | + link: FilePath("/_"), |
| 173 | + mode: EXT4.Inode.Mode(.S_IFLNK, 0o777) |
| 174 | + ) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + func file_whiteouts_and_directory_whiteouts_interact_correctly() throws { |
| 180 | + try withFormatter { formatter, imagePath in |
| 181 | + // Lower‑layer content |
| 182 | + try formatter.create( |
| 183 | + path: FilePath("/opt"), |
| 184 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 185 | + ) |
| 186 | + try formatter.create( |
| 187 | + path: FilePath("/opt/app"), |
| 188 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 189 | + ) |
| 190 | + try formatter.create( |
| 191 | + path: FilePath("/opt/app/cache"), |
| 192 | + mode: EXT4.Inode.Mode(.S_IFDIR, 0o755) |
| 193 | + ) |
| 194 | + try formatter.create( |
| 195 | + path: FilePath("/opt/app/cache/file"), |
| 196 | + mode: EXT4.Inode.Mode(.S_IFREG, 0o644) |
| 197 | + ) |
| 198 | + try formatter.unlink(path: FilePath("/opt/app/cache/file")) |
| 199 | + try formatter.unlink( |
| 200 | + path: FilePath("/opt/app/cache"), |
| 201 | + directoryWhiteout: true |
| 202 | + ) |
| 203 | + try formatter.close() |
| 204 | + |
| 205 | + let reader = try EXT4.EXT4Reader(blockDevice: FilePath(imagePath.description)) |
| 206 | + #expect(try reader.exists(FilePath("/opt"))) |
| 207 | + #expect(try reader.exists(FilePath("/opt/app"))) |
| 208 | + #expect(try reader.exists(FilePath("/opt/app/cache"))) |
| 209 | + #expect(try reader.exists(FilePath("/opt/app/cache/file")) == false) |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments