| 1 | #!/bin/bash |
| 2 | |
| 3 | # Check if we should keep the binary |
| 4 | KEEP_BINARY=false |
| 5 | USE_SYSTEM_BINARY=false |
| 6 | NO_BUILD=false |
| 7 | # Whether we should try to do network calls during testing. |
| 8 | INTERNET_ACCESS=true |
| 9 | |
| 10 | while [ "$#" -gt 0 ]; do |
| 11 | case "$1" in |
| 12 | "--keep-binary") |
| 13 | KEEP_BINARY=true |
| 14 | shift |
| 15 | ;; |
| 16 | "--use-system") |
| 17 | USE_SYSTEM_BINARY=true |
| 18 | shift |
| 19 | ;; |
| 20 | "--no-build") |
| 21 | NO_BUILD=true |
| 22 | shift |
| 23 | ;; |
| 24 | "--offline") |
| 25 | INTERNET_ACCESS=false |
| 26 | shift |
| 27 | ;; |
| 28 | *) |
| 29 | echo "Unknown parameter: $1" |
| 30 | exit 1 |
| 31 | ;; |
| 32 | esac |
| 33 | done |
| 34 | |
| 35 | # Don't exit on error, we'll handle errors in the run_test function |
| 36 | set +e |
| 37 | |
| 38 | # Colors for output |
| 39 | RED='\033[0;31m' |
| 40 | GREEN='\033[0;32m' |
| 41 | YELLOW='\033[1;33m' |
| 42 | NC='\033[0m' # No Color |
| 43 | |
| 44 | # Function to print colored output |
| 45 | print_status() { |
| 46 | echo -e "${YELLOW}[TEST]${NC} $1" |
| 47 | } |
| 48 | |
| 49 | print_success() { |
| 50 | echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 51 | } |
| 52 | |
| 53 | print_error() { |
| 54 | echo -e "${RED}[ERROR]${NC} $1" |
| 55 | } |
| 56 | |
| 57 | # Build the binary if not using system binary |
| 58 | if [ "$USE_SYSTEM_BINARY" = false ]; then |
| 59 | if [ "$NO_BUILD" = false ]; then |
| 60 | print_status "Building landrun binary..." |
| 61 | go build -o landrun cmd/landrun/main.go |
| 62 | if [ $? -ne 0 ]; then |
| 63 | print_error "Failed to build landrun binary" |
| 64 | exit 1 |
| 65 | fi |
| 66 | print_success "Binary built successfully" |
| 67 | else |
| 68 | print_success "Using already built landrun binary" |
| 69 | fi |
| 70 | fi |
| 71 | |
| 72 | # Create test directories |
| 73 | TEST_DIR="test_env" |
| 74 | RO_DIR="$TEST_DIR/ro" |
| 75 | RO_DIR_NESTED_RO="$RO_DIR/ro_nested_ro_1" |
| 76 | RO_DIR_NESTED_RW="$RO_DIR/ro_nested_rw_1" |
| 77 | RO_DIR_NESTED_EXEC="$RO_DIR/ro_nested_exec" |
| 78 | |
| 79 | RW_DIR="$TEST_DIR/rw" |
| 80 | RW_DIR_NESTED_RO="$RW_DIR/rw_nested_ro_1" |
| 81 | RW_DIR_NESTED_RW="$RW_DIR/rw_nested_rw_1" |
| 82 | RW_DIR_NESTED_EXEC="$RW_DIR/rw_nested_exec" |
| 83 | |
| 84 | EXEC_DIR="$TEST_DIR/exec" |
| 85 | NESTED_DIR="$TEST_DIR/nested/path/deep" |
| 86 | |
| 87 | print_status "Setting up test environment..." |
| 88 | rm -rf "$TEST_DIR" |
| 89 | mkdir -p "$RO_DIR" "$RW_DIR" "$EXEC_DIR" "$NESTED_DIR" "$RO_DIR_NESTED_RO" "$RO_DIR_NESTED_RW" "$RO_DIR_NESTED_EXEC" "$RW_DIR_NESTED_RO" "$RW_DIR_NESTED_RW" "$RW_DIR_NESTED_EXEC" |
| 90 | |
| 91 | # Create test files |
| 92 | echo "readonly content" > "$RO_DIR/test.txt" |
| 93 | echo "readwrite content" > "$RW_DIR/test.txt" |
| 94 | echo "nested content" > "$NESTED_DIR/test.txt" |
| 95 | echo "#!/bin/bash" > "$EXEC_DIR/test.sh" |
| 96 | echo "echo 'executable content'" >> "$EXEC_DIR/test.sh" |
| 97 | chmod +x "$EXEC_DIR/test.sh" |
| 98 | cp $EXEC_DIR/test.sh $EXEC_DIR/test2.sh |
| 99 | |
| 100 | cp "$RO_DIR/test.txt" "$RO_DIR_NESTED_RO/test.txt" |
| 101 | cp "$RO_DIR/test.txt" "$RW_DIR_NESTED_RO/test.txt" |
| 102 | |
| 103 | cp "$RW_DIR/test.txt" "$RO_DIR_NESTED_RW/test.txt" |
| 104 | cp "$RW_DIR/test.txt" "$RW_DIR_NESTED_RW/test.txt" |
| 105 | |
| 106 | cp "$EXEC_DIR/test.sh" "$RO_DIR_NESTED_EXEC/test.sh" |
| 107 | cp "$EXEC_DIR/test.sh" "$RW_DIR_NESTED_EXEC/test.sh" |
| 108 | cp "$EXEC_DIR/test.sh" "$RO_DIR_NESTED_RO/test.sh" |
| 109 | cp "$EXEC_DIR/test.sh" "$RW_DIR_NESTED_RO/test.sh" |
| 110 | cp "$EXEC_DIR/test.sh" "$RO_DIR_NESTED_RW/test.sh" |
| 111 | cp "$EXEC_DIR/test.sh" "$RW_DIR_NESTED_RW/test.sh" |
| 112 | |
| 113 | # Create a script in RW dir to test execution in RW dirs |
| 114 | echo "#!/bin/bash" > "$RW_DIR/rw_script.sh" |
| 115 | echo "echo 'this script is in a read-write directory'" >> "$RW_DIR/rw_script.sh" |
| 116 | chmod +x "$RW_DIR/rw_script.sh" |
| 117 | |
| 118 | # Function to run a test case |
| 119 | run_test() { |
| 120 | local name="$1" |
| 121 | local cmd="$2" |
| 122 | local expected_exit="$3" |
| 123 | |
| 124 | # Replace ./landrun with landrun if using system binary |
| 125 | if [ "$USE_SYSTEM_BINARY" = true ]; then |
| 126 | cmd="${cmd//.\/landrun/landrun}" |
| 127 | fi |
| 128 | |
| 129 | # The default target is Landlock ABI v9. Inject --best-effort so the suite |
| 130 | # gracefully degrades and runs on kernels below v9. This only affects which |
| 131 | # ABI level is targeted; the allow/deny semantics being tested are unchanged. |
| 132 | cmd="${cmd/landrun /landrun --best-effort }" |
| 133 | |
| 134 | print_status "Running test: $name" |
| 135 | eval "$cmd" |
| 136 | local exit_code=$? |
| 137 | |
| 138 | if [ $exit_code -eq $expected_exit ]; then |
| 139 | print_success "Test passed: $name" |
| 140 | return 0 |
| 141 | else |
| 142 | print_error "Test failed: $name (expected exit $expected_exit, got $exit_code)" |
| 143 | exit 1 |
| 144 | fi |
| 145 | } |
| 146 | |
| 147 | # Test cases |
| 148 | print_status "Starting test cases..." |
| 149 | |
| 150 | # Basic access tests |
| 151 | run_test "Read-only access to file" \ |
| 152 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR -- cat $RO_DIR/test.txt" \ |
| 153 | 0 |
| 154 | |
| 155 | run_test "Read-only access to nested file" \ |
| 156 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR -- cat $RO_DIR_NESTED_RO/test.txt" \ |
| 157 | 0 |
| 158 | |
| 159 | run_test "Write access to nested directory writable nested in read-only directory" \ |
| 160 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --rw $RO_DIR_NESTED_RW -- touch $RO_DIR_NESTED_RW/created_file" \ |
| 161 | 0 |
| 162 | |
| 163 | run_test "Write access to nested file writable nested in read-only directory" \ |
| 164 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --rw $RO_DIR_NESTED_RW/created_file -- touch $RO_DIR_NESTED_RW/created_file" \ |
| 165 | 0 |
| 166 | |
| 167 | run_test "Read-write access to file" \ |
| 168 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --rw $RW_DIR touch $RW_DIR/new.txt" \ |
| 169 | 0 |
| 170 | |
| 171 | run_test "No write access to read-only directory" \ |
| 172 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --rw $RW_DIR touch $RO_DIR/new.txt" \ |
| 173 | 1 |
| 174 | |
| 175 | # Executable permission tests |
| 176 | run_test "Execute access with rox flag" \ |
| 177 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rox $EXEC_DIR -- $EXEC_DIR/test.sh" \ |
| 178 | 0 |
| 179 | |
| 180 | run_test "Execute access with rox flag on file" \ |
| 181 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rox $EXEC_DIR/test.sh -- $EXEC_DIR/test.sh" \ |
| 182 | 0 |
| 183 | |
| 184 | run_test "Execute access with rox flag on a file that is executable in same directory that one is allowed" \ |
| 185 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rox $EXEC_DIR/test.sh -- $EXEC_DIR/test2.sh" \ |
| 186 | 1 |
| 187 | |
| 188 | run_test "Execute a file with --add-exec flag" \ |
| 189 | "./landrun --log-level debug --add-exec --rox /usr --ro /lib --ro /lib64 --rox $EXEC_DIR/test.sh -- $EXEC_DIR/test2.sh" \ |
| 190 | 0 |
| 191 | |
| 192 | run_test "Execute a file with --add-exec and --ldd flag" \ |
| 193 | "./landrun --log-level debug --add-exec --ldd -- $(which true)" \ |
| 194 | 0 |
| 195 | |
| 196 | |
| 197 | run_test "No execute access with just ro flag" \ |
| 198 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $EXEC_DIR -- $EXEC_DIR/test.sh" \ |
| 199 | 1 |
| 200 | |
| 201 | run_test "Execute access in read-write directory" \ |
| 202 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rwx $RW_DIR -- $RW_DIR/rw_script.sh" \ |
| 203 | 0 |
| 204 | |
| 205 | run_test "No execute access in read-write directory without rwx" \ |
| 206 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rw $RW_DIR -- $RW_DIR/rw_script.sh" \ |
| 207 | 1 |
| 208 | |
| 209 | # Directory traversal tests |
| 210 | run_test "Directory traversal with root access" \ |
| 211 | "./landrun --log-level debug --rox / -- ls /usr" \ |
| 212 | 0 |
| 213 | |
| 214 | run_test "Deep directory traversal" \ |
| 215 | "./landrun --log-level debug --rox / -- ls $NESTED_DIR" \ |
| 216 | 0 |
| 217 | |
| 218 | # Multiple paths and complex specifications |
| 219 | run_test "Multiple read paths" \ |
| 220 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --ro $NESTED_DIR -- cat $NESTED_DIR/test.txt" \ |
| 221 | 0 |
| 222 | |
| 223 | run_test "Comma-separated paths" \ |
| 224 | "./landrun --log-level debug --rox /usr --ro /lib,/lib64,$RO_DIR -- cat $RO_DIR/test.txt" \ |
| 225 | 0 |
| 226 | |
| 227 | # System command tests |
| 228 | run_test "Simple system command" \ |
| 229 | "./landrun --log-level debug --rox /usr --ro /etc -- whoami" \ |
| 230 | 0 |
| 231 | |
| 232 | run_test "System command with arguments" \ |
| 233 | "./landrun --log-level debug --rox / -- ls -la /usr/bin" \ |
| 234 | 0 |
| 235 | |
| 236 | # Edge cases |
| 237 | run_test "Non-existent read-only path" \ |
| 238 | "./landrun --log-level debug --ro /usr --ro /lib --ro /lib64 --ro /nonexistent/path -- ls" \ |
| 239 | 1 |
| 240 | |
| 241 | run_test "No configuration" \ |
| 242 | "./landrun --log-level debug -- ls /" \ |
| 243 | 1 |
| 244 | |
| 245 | # Process creation and redirection tests |
| 246 | run_test "Process creation with pipe" \ |
| 247 | "./landrun --log-level debug --rox / --env PATH -- bash -c 'ls /usr | grep bin'" \ |
| 248 | 0 |
| 249 | |
| 250 | run_test "File redirection" \ |
| 251 | "./landrun --log-level debug --rox / --rw $RW_DIR --env PATH -- bash -c 'ls /usr > $RW_DIR/output.txt && cat $RW_DIR/output.txt'" \ |
| 252 | 0 |
| 253 | |
| 254 | # Network restrictions tests (if kernel supports it) |
| 255 | $INTERNET_ACCESS && run_test "TCP connection without permission" \ |
| 256 | "./landrun --log-level debug --rox /usr --ro / -- curl -s --connect-timeout 2 https://example.com" \ |
| 257 | 7 |
| 258 | |
| 259 | $INTERNET_ACCESS && run_test "TCP connection with permission" \ |
| 260 | "./landrun --log-level debug --rox /usr --ro / --connect-tcp 443 -- curl -s --connect-timeout 2 https://example.com" \ |
| 261 | 0 |
| 262 | |
| 263 | # Environment isolation tests |
| 264 | export TEST_ENV_VAR="test_value_123" |
| 265 | run_test "Environment isolation" \ |
| 266 | "./landrun --log-level debug --rox /usr --ro / -- bash -c 'echo \$TEST_ENV_VAR'" \ |
| 267 | 0 |
| 268 | |
| 269 | run_test "Environment isolation (no variables should be passed)" \ |
| 270 | "./landrun --log-level debug --rox /usr --ro / -- bash -c '[[ -z \$TEST_ENV_VAR ]] && echo \"No env var\" || echo \$TEST_ENV_VAR'" \ |
| 271 | 0 |
| 272 | |
| 273 | run_test "Passing specific environment variable" \ |
| 274 | "./landrun --log-level debug --rox /usr --ro / --env TEST_ENV_VAR --env PATH -- bash -c 'echo \$TEST_ENV_VAR | grep \"test_value_123\"'" \ |
| 275 | 0 |
| 276 | |
| 277 | run_test "Passing custom environment variable" \ |
| 278 | "./landrun --log-level debug --rox /usr --ro / --env CUSTOM_VAR=custom_value --env PATH -- bash -c 'echo \$CUSTOM_VAR | grep \"custom_value\"'" \ |
| 279 | 0 |
| 280 | |
| 281 | # Combining different permission types |
| 282 | run_test "Mixed permissions" \ |
| 283 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --rox $EXEC_DIR --rwx $RW_DIR --env PATH -- bash -c '$EXEC_DIR/test.sh > $RW_DIR/output.txt && cat $RW_DIR/output.txt'" \ |
| 284 | 0 |
| 285 | |
| 286 | # Specific regression tests for bugs we fixed |
| 287 | run_test "Root path traversal regression test" \ |
| 288 | "./landrun --log-level debug --rox /usr -- $(which ls) /usr" \ |
| 289 | 0 |
| 290 | |
| 291 | run_test "Execute from read-only paths regression test" \ |
| 292 | "./landrun --log-level debug --rox /usr --ro /usr/bin -- $(which id)" \ |
| 293 | 0 |
| 294 | |
| 295 | run_test "Unrestricted filesystem access" \ |
| 296 | "./landrun --log-level debug --unrestricted-filesystem ls /usr" \ |
| 297 | 0 |
| 298 | |
| 299 | $INTERNET_ACCESS && run_test "Unrestricted network access" \ |
| 300 | "./landrun --log-level debug --unrestricted-network --rox / -- curl -s --connect-timeout 2 http://kernel.org" \ |
| 301 | 0 |
| 302 | |
| 303 | run_test "Restricted filesystem access" \ |
| 304 | "./landrun --log-level debug ls /usr" \ |
| 305 | 1 |
| 306 | |
| 307 | $INTERNET_ACCESS && run_test "Restricted network access" \ |
| 308 | "./landrun --log-level debug --rox / -- curl -s --connect-timeout 2 http://kernel.org" \ |
| 309 | 7 |
| 310 | |
| 311 | # New feature tests (Landlock V6-V9 / go-landlock v0.9.0) |
| 312 | run_test "Ignore missing path with --ignore-missing" \ |
| 313 | "./landrun --log-level debug --ignore-missing --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --ro /nonexistent/path -- cat $RO_DIR/test.txt" \ |
| 314 | 0 |
| 315 | |
| 316 | run_test "Missing path without --ignore-missing still fails" \ |
| 317 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro /nonexistent/path -- cat $RO_DIR/test.txt" \ |
| 318 | 1 |
| 319 | |
| 320 | run_test "Unrestricted IPC scoping smoke test" \ |
| 321 | "./landrun --log-level debug --unrestricted-scoped --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR -- cat $RO_DIR/test.txt" \ |
| 322 | 0 |
| 323 | |
| 324 | run_test "UNIX socket path allowed with --unix" \ |
| 325 | "./landrun --log-level debug --rox /usr --ro /lib --ro /lib64 --ro $RO_DIR --unix $RO_DIR/test.txt -- cat $RO_DIR/test.txt" \ |
| 326 | 0 |
| 327 | |
| 328 | |
| 329 | # Cleanup |
| 330 | print_status "Cleaning up..." |
| 331 | rm -rf "$TEST_DIR" |
| 332 | if [ "$KEEP_BINARY" = false ] && [ "$USE_SYSTEM_BINARY" = false ]; then |
| 333 | rm -f landrun |
| 334 | fi |
| 335 | |
| 336 | print_success "All tests completed!" |