|
| 1 | +//! Security tests for symlink handling in overlay mode. |
| 2 | +//! |
| 3 | +//! THREAT[TM-ESC-002]: Validates that symlinks cannot be used to escape |
| 4 | +//! mount boundaries, especially after rename/move operations. |
| 5 | +
|
| 6 | +use bashkit::{Bash, FileSystem, InMemoryFs, MountableFs, OverlayFs}; |
| 7 | +use std::path::Path; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +/// Renaming a symlink in overlay mode must preserve it as a symlink |
| 11 | +/// (not silently fail or convert to a regular file). |
| 12 | +#[tokio::test] |
| 13 | +async fn overlay_rename_preserves_symlink() { |
| 14 | + let lower = Arc::new(InMemoryFs::new()) as Arc<dyn FileSystem>; |
| 15 | + let overlay = Arc::new(OverlayFs::new(lower)); |
| 16 | + |
| 17 | + // Create a file and a symlink to it |
| 18 | + overlay |
| 19 | + .write_file(Path::new("/target.txt"), b"hello") |
| 20 | + .await |
| 21 | + .unwrap(); |
| 22 | + overlay |
| 23 | + .symlink(Path::new("/target.txt"), Path::new("/link1")) |
| 24 | + .await |
| 25 | + .unwrap(); |
| 26 | + |
| 27 | + // Rename the symlink |
| 28 | + overlay |
| 29 | + .rename(Path::new("/link1"), Path::new("/link2")) |
| 30 | + .await |
| 31 | + .unwrap(); |
| 32 | + |
| 33 | + // The renamed entry should still be a symlink |
| 34 | + let target = overlay.read_link(Path::new("/link2")).await.unwrap(); |
| 35 | + assert_eq!(target, Path::new("/target.txt")); |
| 36 | + |
| 37 | + // Original should be gone |
| 38 | + assert!(!overlay.exists(Path::new("/link1")).await.unwrap()); |
| 39 | +} |
| 40 | + |
| 41 | +/// Renaming a symlink from the lower layer into the upper layer must |
| 42 | +/// preserve it as a symlink in the upper layer. |
| 43 | +#[tokio::test] |
| 44 | +async fn overlay_rename_symlink_from_lower_layer() { |
| 45 | + let lower = Arc::new(InMemoryFs::new()); |
| 46 | + lower |
| 47 | + .write_file(Path::new("/data.txt"), b"secret") |
| 48 | + .await |
| 49 | + .unwrap(); |
| 50 | + lower |
| 51 | + .symlink(Path::new("/data.txt"), Path::new("/link_lower")) |
| 52 | + .await |
| 53 | + .unwrap(); |
| 54 | + |
| 55 | + let overlay = Arc::new(OverlayFs::new(lower.clone() as Arc<dyn FileSystem>)); |
| 56 | + |
| 57 | + // Rename symlink from lower to a new name (goes to upper) |
| 58 | + overlay |
| 59 | + .rename(Path::new("/link_lower"), Path::new("/link_upper")) |
| 60 | + .await |
| 61 | + .unwrap(); |
| 62 | + |
| 63 | + // Should be a symlink in the overlay |
| 64 | + let target = overlay.read_link(Path::new("/link_upper")).await.unwrap(); |
| 65 | + assert_eq!(target, Path::new("/data.txt")); |
| 66 | +} |
| 67 | + |
| 68 | +/// Copying a symlink in overlay mode should copy it as a symlink. |
| 69 | +#[tokio::test] |
| 70 | +async fn overlay_copy_preserves_symlink() { |
| 71 | + let lower = Arc::new(InMemoryFs::new()) as Arc<dyn FileSystem>; |
| 72 | + let overlay = Arc::new(OverlayFs::new(lower)); |
| 73 | + |
| 74 | + overlay |
| 75 | + .write_file(Path::new("/target.txt"), b"data") |
| 76 | + .await |
| 77 | + .unwrap(); |
| 78 | + overlay |
| 79 | + .symlink(Path::new("/target.txt"), Path::new("/link")) |
| 80 | + .await |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + overlay |
| 84 | + .copy(Path::new("/link"), Path::new("/link_copy")) |
| 85 | + .await |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + // Copy should be a symlink with same target |
| 89 | + let target = overlay.read_link(Path::new("/link_copy")).await.unwrap(); |
| 90 | + assert_eq!(target, Path::new("/target.txt")); |
| 91 | + |
| 92 | + // Original still exists |
| 93 | + let orig_target = overlay.read_link(Path::new("/link")).await.unwrap(); |
| 94 | + assert_eq!(orig_target, Path::new("/target.txt")); |
| 95 | +} |
| 96 | + |
| 97 | +/// A symlink pointing outside a mount should not allow reading the external |
| 98 | +/// file through cat after being moved within the mount. |
| 99 | +#[tokio::test] |
| 100 | +async fn symlink_rename_cannot_escape_mount_via_read() { |
| 101 | + // Lower layer has /etc/passwd (simulating files outside the sandbox scope) |
| 102 | + let lower = Arc::new(InMemoryFs::new()); |
| 103 | + lower.mkdir(Path::new("/etc"), false).await.unwrap(); |
| 104 | + lower |
| 105 | + .write_file(Path::new("/etc/passwd"), b"root:x:0:0") |
| 106 | + .await |
| 107 | + .unwrap(); |
| 108 | + lower.mkdir(Path::new("/sandbox"), false).await.unwrap(); |
| 109 | + |
| 110 | + let overlay = Arc::new(OverlayFs::new(lower as Arc<dyn FileSystem>)); |
| 111 | + |
| 112 | + // Create a symlink inside the overlay pointing to /etc/passwd |
| 113 | + overlay |
| 114 | + .symlink(Path::new("/etc/passwd"), Path::new("/sandbox/evil")) |
| 115 | + .await |
| 116 | + .unwrap(); |
| 117 | + |
| 118 | + // Rename symlink within the sandbox |
| 119 | + overlay |
| 120 | + .rename(Path::new("/sandbox/evil"), Path::new("/sandbox/moved")) |
| 121 | + .await |
| 122 | + .unwrap(); |
| 123 | + |
| 124 | + // The renamed symlink should exist and point to /etc/passwd |
| 125 | + let target = overlay |
| 126 | + .read_link(Path::new("/sandbox/moved")) |
| 127 | + .await |
| 128 | + .unwrap(); |
| 129 | + assert_eq!(target, Path::new("/etc/passwd")); |
| 130 | + |
| 131 | + // But reading through read_file must NOT follow the symlink and return content |
| 132 | + // (symlinks are intentionally not followed — TM-ESC-002) |
| 133 | + let result = overlay.read_file(Path::new("/sandbox/moved")).await; |
| 134 | + assert!(result.is_err(), "read_file on symlink must not follow it"); |
| 135 | +} |
| 136 | + |
| 137 | +/// Cross-mount rename of a symlink in MountableFs must preserve the symlink. |
| 138 | +#[tokio::test] |
| 139 | +async fn mountable_cross_mount_rename_preserves_symlink() { |
| 140 | + let root = Arc::new(InMemoryFs::new()); |
| 141 | + let mount_a = Arc::new(InMemoryFs::new()); |
| 142 | + let mount_b = Arc::new(InMemoryFs::new()); |
| 143 | + |
| 144 | + // Create a symlink in mount_a |
| 145 | + mount_a |
| 146 | + .symlink(Path::new("/target.txt"), Path::new("/link")) |
| 147 | + .await |
| 148 | + .unwrap(); |
| 149 | + |
| 150 | + let mountable = MountableFs::new(root as Arc<dyn FileSystem>); |
| 151 | + mountable |
| 152 | + .mount("/mnt/a", mount_a as Arc<dyn FileSystem>) |
| 153 | + .unwrap(); |
| 154 | + mountable |
| 155 | + .mount("/mnt/b", mount_b as Arc<dyn FileSystem>) |
| 156 | + .unwrap(); |
| 157 | + |
| 158 | + // Cross-mount rename: /mnt/a/link -> /mnt/b/link |
| 159 | + mountable |
| 160 | + .rename(Path::new("/mnt/a/link"), Path::new("/mnt/b/link")) |
| 161 | + .await |
| 162 | + .unwrap(); |
| 163 | + |
| 164 | + // Should be a symlink in mount_b |
| 165 | + let target = mountable.read_link(Path::new("/mnt/b/link")).await.unwrap(); |
| 166 | + assert_eq!(target, Path::new("/target.txt")); |
| 167 | + |
| 168 | + // Source should be gone |
| 169 | + assert!(!mountable.exists(Path::new("/mnt/a/link")).await.unwrap()); |
| 170 | +} |
| 171 | + |
| 172 | +/// Cross-mount copy of a symlink in MountableFs must preserve the symlink. |
| 173 | +#[tokio::test] |
| 174 | +async fn mountable_cross_mount_copy_preserves_symlink() { |
| 175 | + let root = Arc::new(InMemoryFs::new()); |
| 176 | + let mount_a = Arc::new(InMemoryFs::new()); |
| 177 | + let mount_b = Arc::new(InMemoryFs::new()); |
| 178 | + |
| 179 | + mount_a |
| 180 | + .symlink(Path::new("/target.txt"), Path::new("/link")) |
| 181 | + .await |
| 182 | + .unwrap(); |
| 183 | + |
| 184 | + let mountable = MountableFs::new(root as Arc<dyn FileSystem>); |
| 185 | + mountable |
| 186 | + .mount("/mnt/a", mount_a as Arc<dyn FileSystem>) |
| 187 | + .unwrap(); |
| 188 | + mountable |
| 189 | + .mount("/mnt/b", mount_b as Arc<dyn FileSystem>) |
| 190 | + .unwrap(); |
| 191 | + |
| 192 | + // Cross-mount copy |
| 193 | + mountable |
| 194 | + .copy(Path::new("/mnt/a/link"), Path::new("/mnt/b/link")) |
| 195 | + .await |
| 196 | + .unwrap(); |
| 197 | + |
| 198 | + // Both should be symlinks |
| 199 | + let target_a = mountable.read_link(Path::new("/mnt/a/link")).await.unwrap(); |
| 200 | + let target_b = mountable.read_link(Path::new("/mnt/b/link")).await.unwrap(); |
| 201 | + assert_eq!(target_a, Path::new("/target.txt")); |
| 202 | + assert_eq!(target_b, Path::new("/target.txt")); |
| 203 | +} |
| 204 | + |
| 205 | +/// mv of a symlink in a bash session should work and preserve the symlink. |
| 206 | +#[tokio::test] |
| 207 | +async fn bash_mv_symlink_in_overlay() { |
| 208 | + let lower = Arc::new(InMemoryFs::new()) as Arc<dyn FileSystem>; |
| 209 | + let overlay = Arc::new(OverlayFs::new(lower)); |
| 210 | + |
| 211 | + overlay |
| 212 | + .write_file(Path::new("/tmp/target.txt"), b"content") |
| 213 | + .await |
| 214 | + .unwrap(); |
| 215 | + overlay.mkdir(Path::new("/tmp"), true).await.unwrap_or(()); // may already exist |
| 216 | + |
| 217 | + let mut bash = Bash::builder().fs(overlay.clone()).build(); |
| 218 | + |
| 219 | + bash.exec("ln -s /tmp/target.txt /tmp/mylink") |
| 220 | + .await |
| 221 | + .unwrap(); |
| 222 | + let result = bash.exec("mv /tmp/mylink /tmp/renamed_link").await.unwrap(); |
| 223 | + assert_eq!(result.exit_code, 0, "mv should succeed: {}", result.stderr); |
| 224 | + |
| 225 | + // readlink should show the original target |
| 226 | + let result = bash.exec("readlink /tmp/renamed_link").await.unwrap(); |
| 227 | + assert_eq!(result.stdout.trim(), "/tmp/target.txt"); |
| 228 | +} |
0 commit comments