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