1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Landlock - Tracepoint helpers 4 * 5 * Copyright © 2025 Microsoft Corporation 6 * Copyright © 2026 Cloudflare, Inc. 7 */ 8 9 #include <kunit/test.h> 10 #include <linux/cleanup.h> 11 #include <linux/dcache.h> 12 #include <linux/err.h> 13 #include <linux/fs.h> 14 #include <linux/lsm_audit.h> 15 #include <linux/socket.h> 16 #include <net/sock.h> 17 18 #include "access.h" 19 #include "domain.h" 20 #include "fs.h" 21 #include "log.h" 22 #include "ruleset.h" 23 #include "trace.h" 24 25 /* 26 * Generates the tracepoint definitions in this translation unit. The trace 27 * event header dereferences the traced objects in TP_fast_assign, so the full 28 * struct definitions (e.g. ruleset.h, domain.h) must be included before it. 29 */ 30 #define CREATE_TRACE_POINTS 31 #include <trace/events/landlock.h> 32 33 /** 34 * landlock_trace_free_domain - Emit a tracepoint on domain deallocation 35 * 36 * @hierarchy: The domain's hierarchy being deallocated. 37 * 38 * Fires only for a hierarchy whose creation event was emitted, i.e. one that 39 * left LANDLOCK_LOG_UNCOMMITTED in landlock_restrict_self(). This keeps the 40 * create/free pair balanced: a hierarchy that never became observable is freed 41 * silently, while a domain that landlock_restrict_self() created and a 42 * thread-sync failure then aborted still fires free_domain, because its 43 * creation event already fired. 44 * 45 * Called from landlock_log_free_domain(). 46 */ 47 void landlock_trace_free_domain(const struct landlock_hierarchy *const hierarchy) 48 { 49 /* 50 * The log_status read is a correctness guard (keep the create/free pair 51 * balanced), not a cost guard, so this cold path needs no 52 * trace_..._enabled() check: the tracepoint is a static-branch no-op 53 * when disabled. The denial path guards trace_..._enabled() instead 54 * because it does expensive __getname()/path work before emitting. 55 */ 56 if (READ_ONCE(hierarchy->log_status) != LANDLOCK_LOG_UNCOMMITTED) 57 trace_landlock_free_domain(hierarchy); 58 } 59 60 /** 61 * landlock_trace_denial - Emit a tracepoint for a denied access request 62 * 63 * @request: Detail of the user space request. 64 * @youngest_denied: The youngest hierarchy node that denied the access. 65 * @missing: The final missing access or permission subset, when applicable. 66 * @same_exec: Whether the policy subject is the same executable that called 67 * landlock_restrict_self() for the denying domain, as computed 68 * by landlock_log_denial(). 69 * @logged: Whether the domain's policy selects this denial for logging, as 70 * computed by landlock_log_denial(). 71 * 72 * Emits the tracepoint matching @request->type when its event is enabled. 73 * Unlike audit, fires regardless of @logged; the value is recorded in the event 74 * so consumers can filter on it. 75 * 76 * Called from landlock_log_denial(). 77 */ 78 void landlock_trace_denial( 79 const struct landlock_request *const request, 80 const struct landlock_hierarchy *const youngest_denied, 81 const access_mask_t missing, const bool same_exec, const bool logged) 82 { 83 switch (request->type) { 84 case LANDLOCK_REQUEST_NAMESPACE_CREATE: 85 case LANDLOCK_REQUEST_NAMESPACE_ENTER: 86 if (trace_landlock_deny_permission_namespace_enabled()) { 87 const struct landlock_blockers blockers = { 88 .access = missing, 89 .type = request->type, 90 }; 91 92 trace_landlock_deny_permission_namespace( 93 youngest_denied, same_exec, logged, &blockers, 94 request->audit.u.ns.ns_type, 95 request->audit.u.ns.ns_id); 96 } 97 break; 98 case LANDLOCK_REQUEST_CAPABILITY: 99 if (trace_landlock_deny_permission_capability_enabled()) { 100 const struct landlock_blockers blockers = { 101 .access = missing, 102 .type = request->type, 103 }; 104 105 trace_landlock_deny_permission_capability( 106 youngest_denied, same_exec, logged, &blockers, 107 request->audit.u.cap); 108 } 109 break; 110 case LANDLOCK_REQUEST_FS_ACCESS: 111 case LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY: 112 if (trace_landlock_deny_access_fs_enabled()) { 113 const struct landlock_blockers blockers = { 114 .access = missing, 115 .type = request->type, 116 }; 117 char *buf __free(__putname) = __getname(); 118 struct path dentry_path; 119 const char *pathname; 120 const struct path *path = NULL; 121 122 /* 123 * Selects the path from the audit data type, as 124 * dump_common_audit_data() does. A FS_ACCESS denial 125 * carries a file (hook_file_truncate) or an ioctl op 126 * (hook_file_ioctl) rather than a path; 127 * FS_CHANGE_TOPOLOGY carries a path or a bare dentry. 128 * Reading the wrong union member would dereference 129 * garbage, so every reachable type is handled here. 130 */ 131 switch (request->audit.type) { 132 case LSM_AUDIT_DATA_FILE: 133 path = &request->audit.u.file->f_path; 134 break; 135 case LSM_AUDIT_DATA_IOCTL_OP: 136 path = &request->audit.u.op->path; 137 break; 138 case LSM_AUDIT_DATA_DENTRY: 139 /* 140 * Build a path on the stack with the real 141 * dentry so TP_fast_assign can extract dev and 142 * ino; the mnt field is unused there. 143 */ 144 dentry_path = (struct path){ 145 .dentry = request->audit.u.dentry, 146 }; 147 path = &dentry_path; 148 break; 149 case LSM_AUDIT_DATA_PATH: 150 path = &request->audit.u.path; 151 break; 152 default: 153 WARN_ONCE(1, 154 "Unhandled Landlock FS audit type %d", 155 request->audit.type); 156 break; 157 } 158 159 if (!path) 160 break; 161 162 if (!buf) { 163 pathname = "<no_mem>"; 164 } else if (request->audit.type == 165 LSM_AUDIT_DATA_DENTRY) { 166 /* No vfsmount: render the dentry path alone. */ 167 pathname = dentry_path_raw( 168 request->audit.u.dentry, buf, PATH_MAX); 169 if (IS_ERR(pathname)) 170 pathname = 171 PTR_ERR(pathname) == 172 -ENAMETOOLONG ? 173 "<too_long>" : 174 "<unreachable>"; 175 } else { 176 pathname = resolve_path_for_trace(path, buf); 177 } 178 179 trace_landlock_deny_access_fs(youngest_denied, 180 same_exec, logged, 181 &blockers, path, 182 pathname); 183 } 184 break; 185 case LANDLOCK_REQUEST_NET_ACCESS: 186 if (trace_landlock_deny_access_net_enabled()) { 187 const struct landlock_net_trace *const trace_net = 188 request->trace_net; 189 const struct landlock_blockers blockers = { 190 .access = missing, 191 .type = request->type, 192 }; 193 struct sockaddr_storage address = {}; 194 195 if (WARN_ON_ONCE(!trace_net || !trace_net->address)) 196 return; 197 198 if (WARN_ON_ONCE( 199 trace_net->addrlen < 200 (int)offsetofend(struct sockaddr, 201 sa_family) || 202 trace_net->addrlen > (int)sizeof(address))) 203 return; 204 205 memcpy(&address, trace_net->address, 206 trace_net->addrlen); 207 trace_landlock_deny_access_net( 208 youngest_denied, same_exec, logged, &blockers, 209 request->audit.u.net->sk, 210 trace_net->socket_family, &address, 211 trace_net->addrlen); 212 } 213 break; 214 case LANDLOCK_REQUEST_PTRACE: 215 if (trace_landlock_deny_ptrace_enabled()) { 216 const struct landlock_ptrace_trace *const trace_ptrace = 217 request->trace_ptrace; 218 219 if (WARN_ON_ONCE(!trace_ptrace || 220 !trace_ptrace->tracer)) 221 return; 222 223 trace_landlock_deny_ptrace( 224 youngest_denied, same_exec, logged, 225 trace_ptrace->tracee_domain_id, 226 request->audit.u.tsk, trace_ptrace->tracer); 227 } 228 break; 229 case LANDLOCK_REQUEST_SCOPE_SIGNAL: 230 if (trace_landlock_deny_scope_signal_enabled()) { 231 const struct landlock_signal_trace *const trace_signal = 232 request->trace_signal; 233 234 if (WARN_ON_ONCE(!trace_signal)) 235 return; 236 237 trace_landlock_deny_scope_signal( 238 youngest_denied, same_exec, logged, 239 trace_signal->target_domain_id, 240 request->audit.u.tsk, trace_signal->signal); 241 } 242 break; 243 case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET: 244 if (trace_landlock_deny_scope_abstract_unix_socket_enabled()) 245 trace_landlock_deny_scope_abstract_unix_socket( 246 youngest_denied, same_exec, logged, 247 request->other_domain_id, 248 request->audit.u.net->sk); 249 break; 250 default: 251 WARN_ONCE(1, "Unhandled Landlock request type %d", 252 request->type); 253 break; 254 } 255 } 256 257 #ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST 258 259 static void test_trace_seq_init(struct trace_seq *const seq, const size_t size) 260 { 261 memset(seq, 0, sizeof(*seq)); 262 seq_buf_init(&seq->seq, seq->buffer, size); 263 } 264 265 static void test_untrusted_str_data(struct kunit *const test) 266 { 267 const char binary[] = { 'a', '\0', '<' }; 268 static const char ellipsis[] = "\xe2\x80\xa6"; 269 struct trace_seq *const seq = 270 kunit_kzalloc(test, sizeof(*seq), GFP_KERNEL); 271 const char *output; 272 273 KUNIT_ASSERT_NOT_NULL(test, seq); 274 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 275 output = __trace_print_untrusted_str(seq, "<too_long>", 10); 276 KUNIT_ASSERT_NOT_NULL(test, output); 277 KUNIT_EXPECT_STREQ(test, output, "<too_long>"); 278 279 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 280 output = __trace_print_untrusted_str(seq, binary, sizeof(binary)); 281 KUNIT_ASSERT_NOT_NULL(test, output); 282 KUNIT_EXPECT_STREQ(test, output, "a\\000<"); 283 284 /* Input ellipsis bytes are escaped and cannot mimic the raw marker. */ 285 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 286 output = __trace_print_untrusted_str(seq, ellipsis, 287 sizeof(ellipsis) - 1); 288 KUNIT_ASSERT_NOT_NULL(test, output); 289 KUNIT_EXPECT_STREQ(test, output, "\\342\\200\\246"); 290 } 291 292 static void test_untrusted_str_boundaries(struct kunit *const test) 293 { 294 static const char escaped_space[] = "\\040"; 295 const size_t output_size = TRACE_UNTRUSTED_STR_OUTPUT_SIZE; 296 const size_t marker_len = sizeof(TRACE_TRUNCATION_MARKER) - 1; 297 const size_t escape_len = sizeof(escaped_space) - 1; 298 const size_t exact_prefix_len = 299 output_size - marker_len - 1 - escape_len; 300 const size_t short_prefix_len = exact_prefix_len + 1; 301 struct trace_seq *const seq = 302 kunit_kzalloc(test, sizeof(*seq), GFP_KERNEL); 303 char *const input = kunit_kmalloc(test, output_size + 1, GFP_KERNEL); 304 char *const expected = kunit_kmalloc(test, output_size, GFP_KERNEL); 305 const char *output; 306 307 KUNIT_ASSERT_NOT_NULL(test, seq); 308 KUNIT_ASSERT_NOT_NULL(test, input); 309 KUNIT_ASSERT_NOT_NULL(test, expected); 310 311 /* The escaped string and its trailing NUL exactly fit the limit. */ 312 memset(input, 'a', output_size - 1); 313 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 314 output = __trace_print_untrusted_str(seq, input, output_size - 1); 315 KUNIT_ASSERT_NOT_NULL(test, output); 316 KUNIT_EXPECT_EQ(test, seq->seq.len, output_size); 317 KUNIT_EXPECT_EQ(test, memcmp(output, input, output_size - 1), 0); 318 319 /* Stop before a four-byte escape when only three bytes remain. */ 320 memset(input, 'a', short_prefix_len); 321 input[short_prefix_len] = ' '; 322 memset(input + short_prefix_len + 1, 'b', 5); 323 memset(expected, 'a', short_prefix_len); 324 memcpy(expected + short_prefix_len, TRACE_TRUNCATION_MARKER, 325 marker_len + 1); 326 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 327 output = __trace_print_untrusted_str(seq, input, short_prefix_len + 6); 328 KUNIT_ASSERT_NOT_NULL(test, output); 329 KUNIT_EXPECT_STREQ(test, output, expected); 330 331 /* Include a four-byte escape that exactly fills the prefix capacity. */ 332 memset(input, 'a', exact_prefix_len); 333 input[exact_prefix_len] = ' '; 334 memset(input + exact_prefix_len + 1, 'b', marker_len + 1); 335 memset(expected, 'a', exact_prefix_len); 336 memcpy(expected + exact_prefix_len, escaped_space, escape_len); 337 memcpy(expected + exact_prefix_len + escape_len, 338 TRACE_TRUNCATION_MARKER, marker_len + 1); 339 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 340 output = __trace_print_untrusted_str(seq, input, 341 exact_prefix_len + marker_len + 2); 342 KUNIT_ASSERT_NOT_NULL(test, output); 343 KUNIT_EXPECT_STREQ(test, output, expected); 344 345 /* Literal backslashes remain escaped in complete output. */ 346 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 347 output = __trace_print_untrusted_str(seq, "/\\000", 5); 348 KUNIT_ASSERT_NOT_NULL(test, output); 349 KUNIT_EXPECT_STREQ(test, output, "/\\\\000"); 350 } 351 352 static void test_untrusted_str_cursor(struct kunit *const test) 353 { 354 const size_t padding_len = 355 TRACE_SEQ_BUFFER_SIZE - TRACE_UNTRUSTED_STR_OUTPUT_SIZE + 1; 356 struct trace_seq *const seq = 357 kunit_kzalloc(test, sizeof(*seq), GFP_KERNEL); 358 char *const padding = kunit_kzalloc(test, padding_len, GFP_KERNEL); 359 const char *output; 360 361 KUNIT_ASSERT_NOT_NULL(test, seq); 362 KUNIT_ASSERT_NOT_NULL(test, padding); 363 364 /* Accept available space exactly equal to the fixed reservation. */ 365 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 366 trace_seq_putmem(seq, padding, padding_len - 1); 367 output = __trace_print_untrusted_str(seq, "/a", 2); 368 KUNIT_ASSERT_NOT_NULL(test, output); 369 KUNIT_EXPECT_STREQ(test, output, "/a"); 370 KUNIT_EXPECT_EQ(test, seq->seq.len, padding_len - 1 + sizeof("/a")); 371 372 /* Reject one byte less without changing the scratch cursor. */ 373 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 374 trace_seq_putmem(seq, padding, padding_len); 375 output = __trace_print_untrusted_str(seq, "/a", 2); 376 KUNIT_EXPECT_NULL(test, output); 377 KUNIT_EXPECT_EQ(test, seq->seq.len, padding_len); 378 } 379 380 static void test_untrusted_str_composition(struct kunit *const test) 381 { 382 static const struct trace_print_flags flags[] = { 383 { .mask = 1, .name = "read" }, 384 }; 385 const size_t output_size = TRACE_UNTRUSTED_STR_OUTPUT_SIZE; 386 const size_t prefix_len = output_size - sizeof(TRACE_TRUNCATION_MARKER); 387 struct trace_seq *const seq = 388 kunit_kzalloc(test, sizeof(*seq), GFP_KERNEL); 389 char *const expected = kunit_kmalloc(test, output_size, GFP_KERNEL); 390 char *const path = kunit_kmalloc(test, output_size, GFP_KERNEL); 391 const char *flags_output, *path_output; 392 393 KUNIT_ASSERT_NOT_NULL(test, seq); 394 KUNIT_ASSERT_NOT_NULL(test, expected); 395 KUNIT_ASSERT_NOT_NULL(test, path); 396 memset(path, 'a', output_size); 397 memset(expected, 'a', prefix_len); 398 memcpy(expected + prefix_len, TRACE_TRUNCATION_MARKER, 399 sizeof(TRACE_TRUNCATION_MARKER)); 400 401 /* Exercise both legal TP_printk() sibling evaluation orders. */ 402 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 403 path_output = __trace_print_untrusted_str(seq, path, output_size); 404 flags_output = 405 trace_print_flags_seq(seq, "|", 1, flags, ARRAY_SIZE(flags)); 406 KUNIT_ASSERT_NOT_NULL(test, path_output); 407 KUNIT_EXPECT_STREQ(test, path_output, expected); 408 KUNIT_EXPECT_STREQ(test, flags_output, "read"); 409 410 test_trace_seq_init(seq, TRACE_SEQ_BUFFER_SIZE); 411 flags_output = 412 trace_print_flags_seq(seq, "|", 1, flags, ARRAY_SIZE(flags)); 413 path_output = __trace_print_untrusted_str(seq, path, output_size); 414 KUNIT_ASSERT_NOT_NULL(test, path_output); 415 KUNIT_EXPECT_STREQ(test, path_output, expected); 416 KUNIT_EXPECT_STREQ(test, flags_output, "read"); 417 } 418 419 static struct kunit_case test_cases[] = { 420 /* clang-format off */ 421 KUNIT_CASE(test_untrusted_str_data), 422 KUNIT_CASE(test_untrusted_str_boundaries), 423 KUNIT_CASE(test_untrusted_str_cursor), 424 KUNIT_CASE(test_untrusted_str_composition), 425 {} 426 /* clang-format on */ 427 }; 428 429 static struct kunit_suite test_suite = { 430 .name = "landlock_trace", 431 .test_cases = test_cases, 432 }; 433 434 kunit_test_suite(test_suite); 435 436 #endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ 437