1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright © 2025 Microsoft Corporation 4 * Copyright © 2026 Cloudflare, Inc. 5 */ 6 7 #undef TRACE_SYSTEM 8 #define TRACE_SYSTEM landlock 9 10 #if !defined(_TRACE_LANDLOCK_H) || defined(TRACE_HEADER_MULTI_READ) 11 #define _TRACE_LANDLOCK_H 12 13 #include <linux/landlock.h> 14 #include <linux/string.h> 15 #include <linux/string_helpers.h> 16 #include <linux/tracepoint.h> 17 #include <linux/trace_seq.h> 18 #include <net/af_unix.h> 19 20 struct dentry; 21 struct landlock_domain; 22 struct landlock_hierarchy; 23 struct landlock_rule; 24 struct landlock_ruleset; 25 struct path; 26 struct sock; 27 struct task_struct; 28 29 #ifdef CREATE_TRACE_POINTS 30 31 /* 32 * Escapes @len bytes of an untrusted string into the trace sequence @p so it 33 * cannot inject field separators or control characters into the ftrace text 34 * output, and can be unambiguously recovered. Called from the TP_printk() of 35 * the tracepoints that expose paths and process names. @len is passed by the 36 * caller (rather than derived with strlen()) so a name that is not 37 * NUL-terminated or carries embedded NUL bytes (an abstract socket name) is 38 * escaped in full instead of being truncated at the first NUL. 39 * 40 * Return: a pointer into @p's buffer, or NULL if @src is NULL or the buffer is 41 * exhausted (normal when the trace buffer is full). 42 */ 43 static inline const char * 44 __trace_print_untrusted_str(struct trace_seq *p, const char *src, size_t len) 45 { 46 int escaped_size; 47 char *buf; 48 size_t buf_size = seq_buf_get_buf(&p->seq, &buf); 49 const char *ret = trace_seq_buffer_ptr(p); 50 51 /* Buffer exhaustion is normal when the trace buffer is full. */ 52 if (!src || buf_size == 0) 53 return NULL; 54 55 escaped_size = 56 string_escape_mem(src, len, buf, buf_size, 57 ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NAP | 58 ESCAPE_APPEND | ESCAPE_OCTAL, 59 " ='\"\\"); 60 if (unlikely(escaped_size >= buf_size)) { 61 /* We need some room for the final '\0'. */ 62 seq_buf_set_overflow(&p->seq); 63 p->full = 1; 64 return NULL; 65 } 66 seq_buf_commit(&p->seq, escaped_size); 67 trace_seq_putc(p, 0); 68 return ret; 69 } 70 71 /* 72 * Fills the dense per-domain-layer array layers (one access mask per layer, 73 * indexed by level - 1) from rule's sparse layer stack, keeping only the 74 * requested rights (access_request). Layers with no matching rule entry get 75 * a zero mask. Shared by the check_rule_fs and check_rule_net events. 76 * 77 * rule->layers is sorted by ascending level, with levels in the domain's 78 * [1, num_layers] range (see landlock_merge_ruleset()), so every entry maps 79 * to a slot. A leftover entry would be a malformed rule; the zero-filled 80 * slots keep the output and the array bounds safe regardless. 81 */ 82 static inline void 83 __trace_landlock_fill_layers(access_mask_t *const layers, 84 const size_t num_layers, 85 const struct landlock_rule *const rule, 86 const access_mask_t access_request) 87 { 88 size_t i = 0; 89 90 for (size_t level = 1; level <= num_layers; level++) { 91 access_mask_t grants = 0; 92 93 if (i < rule->num_layers && level == rule->layers[i].level) { 94 grants = rule->layers[i].access & access_request; 95 i++; 96 } 97 layers[level - 1] = grants; 98 } 99 100 /* A leftover entry means an out-of-range or unsorted rule level. */ 101 WARN_ON_ONCE(i < rule->num_layers); 102 } 103 104 /* 105 * Renders the dense per-domain-layer access array as symbolic flag names for 106 * the grants field: layers wrapped in "{}", flags within a layer joined by 107 * "|", layers separated by ",", an empty layer rendered as nothing. 108 * Open-codes the flag walk because trace_print_flags_seq() NUL-terminates per 109 * call and so cannot be chained into a single field. The shared names table 110 * covers every access right, so masked bits are always named. Returns the 111 * trace_seq position like __print_flags(). 112 */ 113 static inline const char *__trace_landlock_print_layers( 114 struct trace_seq *p, const access_mask_t *const layers, 115 const size_t num_layers, const struct trace_print_flags *const names, 116 const size_t names_size) 117 { 118 const char *const ret = trace_seq_buffer_ptr(p); 119 120 trace_seq_putc(p, '{'); 121 for (size_t i = 0; i < num_layers; i++) { 122 access_mask_t mask = layers[i]; 123 bool first = true; 124 125 if (i) 126 trace_seq_putc(p, ','); 127 for (size_t j = 0; mask && j < names_size; j++) { 128 if ((mask & names[j].mask) != names[j].mask) 129 continue; 130 if (!first) 131 trace_seq_putc(p, '|'); 132 trace_seq_puts(p, names[j].name); 133 mask &= ~names[j].mask; 134 first = false; 135 } 136 } 137 trace_seq_putc(p, '}'); 138 trace_seq_putc(p, 0); 139 return ret; 140 } 141 142 #endif /* CREATE_TRACE_POINTS */ 143 144 /* clang-format off */ 145 146 /* Maps a shared _LANDLOCK_*_NAMES entry to a __print_flags() pair. */ 147 #define _LANDLOCK_NAME_ENTRY(mask, name) { mask, name } 148 149 /** 150 * DOC: Landlock trace events 151 * 152 * These guarantees and constraints hold for every Landlock tracepoint. 153 * A new tracepoint must uphold them, and an eBPF consumer can rely on 154 * them. 155 * 156 * Decision context 157 * ~~~~~~~~~~~~~~~~ 158 * 159 * A denial event, together with the lifecycle events, exposes the full 160 * set of inputs the verdict consumed, so a consumer that tracked domain 161 * creation (landlock_create_ruleset, landlock_create_domain) can verify 162 * or reproduce the Landlock decision rather than merely observe it 163 * happened. In who/what/why terms: who is the denying domain (the domain 164 * field, always the subject that enforced the policy, never the current 165 * task), what is the operation and its object, and why is every other 166 * input the verdict weighed. 167 * 168 * Lifecycle consistency 169 * ~~~~~~~~~~~~~~~~~~~~~~ 170 * 171 * Lifecycle events are balanced: a creation event always has a matching 172 * deallocation event and vice versa, so an eBPF program can model object 173 * lifetimes from the trace stream without reconciliation logic. A creation 174 * event fires while the object is still private to the calling thread 175 * (landlock_create_ruleset fires before the ruleset's file descriptor is 176 * installed, so it cannot race a concurrent :manpage:`close(2)`); if fd 177 * installation later fails and the ruleset is freed, free_ruleset still 178 * fires, keeping the pair balanced. The domain pair (create_domain and 179 * free_domain) is balanced the same way: create_domain fires when the 180 * domain is created (under the ruleset lock, before thread-sync), and 181 * free_domain fires when it is freed. A rare thread-sync failure aborts 182 * the just-created domain, which then emits both events (its creation, then 183 * an immediate free). Denial events fire only for denials that actually 184 * happen. 185 * 186 * Pointer access 187 * ~~~~~~~~~~~~~~ 188 * 189 * All pointer arguments in TP_PROTO are guaranteed non-NULL by the 190 * caller, but pointers reached through them may still be NULL (e.g., 191 * hierarchy->parent at a root domain) and must be checked. eBPF programs 192 * read these pointers via BTF for richer introspection than the 193 * TP_STRUCT__entry fields, which serve TP_printk display only. 194 * 195 * Mutable object pointers are passed while the caller holds the object's 196 * lock, so TP_fast_assign and a BTF reader see the exact object the event 197 * reports, a snapshot no concurrent writer can change: add_rule holds the 198 * modified ruleset's lock, and create_domain holds the ruleset lock across 199 * the emission (before the thread-sync wait) so the inspected ruleset is 200 * the one merged into the domain. Objects immutable at the emission site 201 * (a domain after creation, a hierarchy at its last reference) need no 202 * lock. A few values that no held lock protects are a best-effort 203 * lockless snapshot instead: a task's comm, and the deny_access_net struct 204 * sock (whose network hook holds no socket lock), matching how the sched 205 * and signal trace events sample comm. 206 * 207 * Field encoding 208 * ~~~~~~~~~~~~~~ 209 * 210 * Fields that mirror the Landlock UAPI use the same C types and endianness 211 * (e.g. network ports are __u64 in host endianness, like 212 * landlock_net_port_attr.port). Per-event details, such as where a value 213 * is byte-swapped, live in the field's own kdoc. 214 * 215 * Rule-check fields 216 * ~~~~~~~~~~~~~~~~~ 217 * 218 * The check_rule events fire during an access check, once per matching 219 * rule, before the final allow-or-deny verdict. They share domain (the 220 * enforcing domain being evaluated), access_request (the access mask being 221 * checked), and rule (the matching rule, with per-layer access masks). 222 * 223 * Denial fields 224 * ~~~~~~~~~~~~~ 225 * 226 * Every denial event shares three fields. domain is the ID of the 227 * innermost domain that blocked the access. same_exec tells whether the 228 * current task is the same executable that entered that domain. logged is 229 * the domain's audit-logging decision for this denial (its log_status is 230 * enabled and the per-execution flag selected by same_exec is set); a 231 * stateless ftrace filter can select the denials the domain submits to 232 * audit with logged==1, without reconstructing it from the per-execution 233 * log flags. Denial events order their fields as domain, same_exec, 234 * logged, then blockers (deny_access events only), then the type-specific 235 * object fields, then any variable-length field. 236 * 237 * Relational referents 238 * ~~~~~~~~~~~~~~~~~~~~~ 239 * 240 * A scope or ptrace verdict compares two domains, so the other party's 241 * domain is part of the decision context. It is exposed as a scalar 242 * domain ID (0 when that party is unsandboxed): target_domain (signal), 243 * peer_domain (abstract unix socket), tracee_domain (ptrace). With both 244 * IDs in the stream, a consumer that tracked domain creation can relate 245 * the two parties without kernel-internal state. The ID is a scalar 246 * snapshot, not a live domain pointer that could dangle: an optional 247 * relational referent is a scalar (0 sentinel), not a nullable pointer. 248 */ 249 250 /* 251 * Prints a per-layer access mask array (the dynamic array @array) as symbolic 252 * flag names using the shared @flag_names list (a _LANDLOCK_*_NAMES macro). 253 * Stays outside CREATE_TRACE_POINTS: TP_printk is expanded in the print-output 254 * pass where that macro is undefined. 255 */ 256 #define __print_landlock_layers(array, flag_names...) \ 257 ({ \ 258 static const struct trace_print_flags __layer_names[] = { \ 259 flag_names \ 260 }; \ 261 __trace_landlock_print_layers( \ 262 p, __get_dynamic_array(array), \ 263 __get_dynamic_array_len(array) / \ 264 sizeof(access_mask_t), \ 265 __layer_names, ARRAY_SIZE(__layer_names)); \ 266 }) 267 268 /** 269 * landlock_create_ruleset - New ruleset created 270 * 271 * @ruleset: Newly created ruleset (never NULL); not yet shared via an fd, 272 * so no lock is needed. 273 * 274 * Emitted by sys_landlock_create_ruleset() while the new ruleset is still 275 * private to the calling thread, before its file descriptor is installed, 276 * so it cannot race a concurrent :manpage:`close(2)`. Balanced by a 277 * matching landlock_free_ruleset event. 278 */ 279 TRACE_EVENT(landlock_create_ruleset, 280 281 TP_PROTO(const struct landlock_ruleset *ruleset), 282 283 TP_ARGS(ruleset), 284 285 TP_STRUCT__entry( 286 __field( __u64, ruleset_id ) 287 __field( __u32, ruleset_version ) 288 __field( access_mask_t, handled_fs ) 289 __field( access_mask_t, handled_net ) 290 __field( access_mask_t, scoped ) 291 ), 292 293 TP_fast_assign( 294 __entry->ruleset_id = ruleset->id; 295 __entry->ruleset_version = ruleset->version; 296 __entry->handled_fs = ruleset->handled_masks.fs; 297 __entry->handled_net = ruleset->handled_masks.net; 298 __entry->scoped = ruleset->handled_masks.scope; 299 ), 300 301 TP_printk("ruleset=%llx.%u handled_fs=%s handled_net=%s scoped=%s", 302 __entry->ruleset_id, __entry->ruleset_version, 303 __print_flags(__entry->handled_fs, "|", _LANDLOCK_ACCESS_FS_NAMES), 304 __print_flags(__entry->handled_net, "|", _LANDLOCK_ACCESS_NET_NAMES), 305 __print_flags(__entry->scoped, "|", _LANDLOCK_SCOPE_NAMES)) 306 ); 307 308 /** 309 * landlock_free_ruleset - Ruleset freed 310 * 311 * @ruleset: Ruleset being freed (never NULL); at its last reference, so no 312 * lock is needed. 313 * 314 * Emitted when a ruleset's last reference is dropped (typically when 315 * the creating process closes the ruleset file descriptor). Fires even 316 * when file-descriptor installation failed after creation, keeping the 317 * create/free pair balanced. 318 */ 319 TRACE_EVENT(landlock_free_ruleset, 320 321 TP_PROTO(const struct landlock_ruleset *ruleset), 322 323 TP_ARGS(ruleset), 324 325 TP_STRUCT__entry( 326 __field( __u64, ruleset_id ) 327 __field( __u32, ruleset_version ) 328 ), 329 330 TP_fast_assign( 331 __entry->ruleset_id = ruleset->id; 332 __entry->ruleset_version = ruleset->version; 333 ), 334 335 TP_printk("ruleset=%llx.%u", 336 __entry->ruleset_id, __entry->ruleset_version) 337 ); 338 339 /** 340 * landlock_add_rule_fs - Filesystem rule added to a ruleset 341 * 342 * @ruleset: Source ruleset (never NULL). 343 * @access_rights: Effective access mask stored in the rule, not the raw 344 * sys_landlock_add_rule() argument (unhandled rights 345 * added). 346 * @path: Filesystem path for the rule (never NULL). 347 * @pathname: Resolved absolute path string (never NULL; error placeholder 348 * on resolution failure). 349 * 350 * Emitted by sys_landlock_add_rule() under the modified ruleset's lock, so 351 * the reported ruleset is a stable snapshot that no concurrent writer can 352 * change. 353 */ 354 TRACE_EVENT(landlock_add_rule_fs, 355 356 TP_PROTO(const struct landlock_ruleset *ruleset, 357 access_mask_t access_rights, const struct path *path, 358 const char *pathname), 359 360 TP_ARGS(ruleset, access_rights, path, pathname), 361 362 TP_STRUCT__entry( 363 __field( __u64, ruleset_id ) 364 __field( __u32, ruleset_version ) 365 __field( access_mask_t, access_rights ) 366 __field( dev_t, dev ) 367 __field( ino_t, ino ) 368 __string( pathname, pathname ) 369 ), 370 371 TP_fast_assign( 372 lockdep_assert_held(&ruleset->lock); 373 __entry->ruleset_id = ruleset->id; 374 __entry->ruleset_version = ruleset->version; 375 __entry->access_rights = access_rights; 376 __entry->dev = path->dentry->d_sb->s_dev; 377 /* 378 * The inode number may not be the user-visible one, 379 * but it will be the same used by audit. 380 */ 381 __entry->ino = d_backing_inode(path->dentry)->i_ino; 382 __assign_str(pathname); 383 ), 384 385 TP_printk("ruleset=%llx.%u access_rights=%s dev=%u:%u ino=%lu path=%s", 386 __entry->ruleset_id, __entry->ruleset_version, 387 __print_flags(__entry->access_rights, "|", _LANDLOCK_ACCESS_FS_NAMES), 388 MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino, 389 __trace_print_untrusted_str(p, __get_str(pathname), 390 __get_dynamic_array_len(pathname) - 1)) 391 ); 392 393 /** 394 * landlock_add_rule_net - Network port rule added to a ruleset 395 * 396 * @ruleset: Source ruleset (never NULL). 397 * @access_rights: Effective access mask stored in the rule, not the raw 398 * sys_landlock_add_rule() argument (unhandled rights 399 * added). 400 * @port: Network port, the landlock_net_port_attr.port UAPI value 401 * forwarded directly. 402 * 403 * Emitted by sys_landlock_add_rule() under the modified ruleset's lock, so 404 * the reported ruleset is a stable snapshot that no concurrent writer can 405 * change. 406 */ 407 TRACE_EVENT(landlock_add_rule_net, 408 409 TP_PROTO(const struct landlock_ruleset *ruleset, 410 access_mask_t access_rights, __u64 port), 411 412 TP_ARGS(ruleset, access_rights, port), 413 414 TP_STRUCT__entry( 415 __field( __u64, ruleset_id ) 416 __field( __u32, ruleset_version ) 417 __field( access_mask_t, access_rights ) 418 __field( __u64, port ) 419 ), 420 421 TP_fast_assign( 422 lockdep_assert_held(&ruleset->lock); 423 __entry->ruleset_id = ruleset->id; 424 __entry->ruleset_version = ruleset->version; 425 __entry->access_rights = access_rights; 426 __entry->port = port; 427 ), 428 429 TP_printk("ruleset=%llx.%u access_rights=%s port=%llu", 430 __entry->ruleset_id, __entry->ruleset_version, 431 __print_flags(__entry->access_rights, "|", _LANDLOCK_ACCESS_NET_NAMES), 432 __entry->port) 433 ); 434 435 /** 436 * landlock_create_domain - New domain created 437 * 438 * @domain: Newly created domain (never NULL, immutable after creation). 439 * @domain->hierarchy->id is its unique ID, shared with the 440 * landlock_enforce_domain and landlock_free_domain events; 441 * @domain->hierarchy->details holds the requesting process. 442 * @ruleset: Source ruleset frozen into the domain (never NULL). The 443 * ruleset lock is held across the emission, so a BPF program 444 * reading it via BTF sees the exact merged ruleset; 445 * @ruleset->id / @ruleset->version identify it. 446 * 447 * Emitted by sys_landlock_restrict_self() once, in the requesting 448 * thread's context, right after the merge and before thread-sync. The 449 * flags-only path (ruleset_fd == -1) creates no domain and does not 450 * emit this event. Paired with the per-thread landlock_enforce_domain 451 * (join on @domain->hierarchy->id) and balanced by a matching 452 * landlock_free_domain event. 453 */ 454 TRACE_EVENT(landlock_create_domain, 455 456 TP_PROTO(const struct landlock_domain *domain, 457 const struct landlock_ruleset *ruleset), 458 459 TP_ARGS(domain, ruleset), 460 461 TP_STRUCT__entry( 462 __field( __u64, domain_id ) 463 __field( __u64, parent_id ) 464 __field( __u64, ruleset_id ) 465 __field( __u32, ruleset_version ) 466 ), 467 468 TP_fast_assign( 469 lockdep_assert_held(&ruleset->lock); 470 __entry->domain_id = domain->hierarchy->id; 471 __entry->parent_id = domain->hierarchy->parent ? 472 domain->hierarchy->parent->id : 0; 473 __entry->ruleset_id = ruleset->id; 474 __entry->ruleset_version = ruleset->version; 475 ), 476 477 TP_printk("domain=%llx parent=%llx ruleset=%llx.%u", 478 __entry->domain_id, __entry->parent_id, 479 __entry->ruleset_id, __entry->ruleset_version) 480 ); 481 482 /** 483 * landlock_enforce_domain - Domain enforced on a thread 484 * 485 * @domain: Domain now enforced on the current thread (never NULL, 486 * immutable; read locklessly). Correlate to 487 * landlock_create_domain via @domain->hierarchy->id for the 488 * source ruleset and requesting thread, or read 489 * @domain->hierarchy->details for the requesting process. 490 * @complete: Set on the single event that concludes the operation, after 491 * all its other enforcements; filter on it for one event per 492 * operation. 493 * @process_wide: The enforcement covers every eligible (non-exiting) 494 * thread of the process: set when the caller used 495 * %LANDLOCK_RESTRICT_SELF_TSYNC or the process is 496 * single-threaded. A lone thread whose group still 497 * holds a zombie leader is not counted single-threaded, 498 * so process_wide == 0 never proves the opposite. 499 * @no_new_privs: The enforcing thread's no_new_privs state at 500 * enforcement time: 1 if set (by a prior 501 * :manpage:`prctl(2)` %PR_SET_NO_NEW_PRIVS or by 502 * %LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS), 0 if the domain 503 * was enforced with %CAP_SYS_ADMIN instead. 504 * 505 * Emitted for each thread sys_landlock_restrict_self() enforces the 506 * domain on, in that thread's own context, right after its 507 * commit_creds(), so it fires only once the thread is irreversibly 508 * enforcing the domain (aborted operations emit none). Not 509 * balanced; every enforcement falls between the domain's 510 * landlock_create_domain and landlock_free_domain events. 511 * 512 * @complete == 1 && @process_wide == 1 means the whole process is 513 * sandboxed by @domain, durably (Landlock domains are monotonic and 514 * inherited on :manpage:`clone(2)`). 515 */ 516 TRACE_EVENT(landlock_enforce_domain, 517 518 TP_PROTO(const struct landlock_domain *domain, bool complete, 519 bool process_wide, bool no_new_privs), 520 521 TP_ARGS(domain, complete, process_wide, no_new_privs), 522 523 TP_STRUCT__entry( 524 __field( __u64, domain_id ) 525 __field( bool, complete ) 526 __field( bool, process_wide ) 527 __field( bool, no_new_privs ) 528 ), 529 530 TP_fast_assign( 531 __entry->domain_id = domain->hierarchy->id; 532 __entry->complete = complete; 533 __entry->process_wide = process_wide; 534 __entry->no_new_privs = no_new_privs; 535 ), 536 537 TP_printk("domain=%llx complete=%d process_wide=%d no_new_privs=%d", 538 __entry->domain_id, __entry->complete, __entry->process_wide, 539 __entry->no_new_privs) 540 ); 541 542 /** 543 * landlock_free_domain - Domain freed 544 * 545 * @hierarchy: Hierarchy node being freed (never NULL). 546 * 547 * Emitted when the hierarchy node's last reference is dropped: its 548 * refcount reaches zero after all child domains have released their 549 * parent reference. A committed domain is 550 * freed from a kworker via landlock_put_domain_deferred() (the credential 551 * free path runs in RCU context, where sleeping is forbidden), so the 552 * current task is not the sandboxed task that triggered the free. Balanced 553 * by a matching landlock_create_domain event. 554 */ 555 TRACE_EVENT(landlock_free_domain, 556 557 TP_PROTO(const struct landlock_hierarchy *hierarchy), 558 559 TP_ARGS(hierarchy), 560 561 TP_STRUCT__entry( 562 __field( __u64, domain_id ) 563 __field( __u64, denials ) 564 ), 565 566 TP_fast_assign( 567 __entry->domain_id = hierarchy->id; 568 __entry->denials = atomic64_read(&hierarchy->num_denials); 569 ), 570 571 TP_printk("domain=%llx denials=%llu", 572 __entry->domain_id, __entry->denials) 573 ); 574 575 /** 576 * landlock_check_rule_fs - Filesystem rule evaluated during access check 577 * 578 * @domain: Enforcing domain (never NULL). 579 * @rule: Matching rule with per-layer access masks (never NULL). 580 * @access_request: Access mask evaluated against the rule (the domain's 581 * handled mask during rename/link double-checks). 582 * @dentry: Filesystem dentry being checked (never NULL). 583 * 584 * Emitted for each rule that matches during a filesystem access check. 585 * The grants array shows the requested rights the rule grants at each 586 * domain layer. See Documentation/trace/events-landlock.rst for how to 587 * interpret it. 588 */ 589 TRACE_EVENT(landlock_check_rule_fs, 590 591 TP_PROTO(const struct landlock_domain *domain, 592 const struct landlock_rule *rule, 593 access_mask_t access_request, const struct dentry *dentry), 594 595 TP_ARGS(domain, rule, access_request, dentry), 596 597 TP_STRUCT__entry( 598 __field( __u64, domain_id ) 599 __field( access_mask_t, access_request ) 600 __field( dev_t, dev ) 601 __field( ino_t, ino ) 602 __dynamic_array(access_mask_t, grants, 603 domain->num_layers) 604 ), 605 606 TP_fast_assign( 607 __entry->domain_id = domain->hierarchy->id; 608 __entry->access_request = access_request; 609 __entry->dev = dentry->d_sb->s_dev; 610 __entry->ino = d_backing_inode(dentry)->i_ino; 611 612 __trace_landlock_fill_layers(__get_dynamic_array(grants), 613 __get_dynamic_array_len(grants) / 614 sizeof(access_mask_t), 615 rule, access_request); 616 ), 617 618 TP_printk("domain=%llx access_request=%s dev=%u:%u ino=%lu grants=%s", 619 __entry->domain_id, 620 __print_flags(__entry->access_request, "|", _LANDLOCK_ACCESS_FS_NAMES), 621 MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino, 622 __print_landlock_layers(grants, _LANDLOCK_ACCESS_FS_NAMES)) 623 ); 624 625 /** 626 * landlock_check_rule_net - Network port rule evaluated during access check 627 * 628 * @domain: Enforcing domain (never NULL). 629 * @rule: Matching rule with per-layer access masks (never NULL). 630 * @access_request: Access mask being requested. 631 * @port: Network port being checked (host endianness). 632 * 633 * Emitted for each rule that matches during a network access check. The 634 * grants array shows the requested rights the rule grants at each domain 635 * layer. See Documentation/trace/events-landlock.rst for how to 636 * interpret it. 637 */ 638 TRACE_EVENT(landlock_check_rule_net, 639 640 TP_PROTO(const struct landlock_domain *domain, 641 const struct landlock_rule *rule, 642 access_mask_t access_request, __u64 port), 643 644 TP_ARGS(domain, rule, access_request, port), 645 646 TP_STRUCT__entry( 647 __field( __u64, domain_id ) 648 __field( access_mask_t, access_request ) 649 __field( __u64, port ) 650 __dynamic_array(access_mask_t, grants, 651 domain->num_layers) 652 ), 653 654 TP_fast_assign( 655 __entry->domain_id = domain->hierarchy->id; 656 __entry->access_request = access_request; 657 __entry->port = port; 658 659 __trace_landlock_fill_layers(__get_dynamic_array(grants), 660 __get_dynamic_array_len(grants) / 661 sizeof(access_mask_t), 662 rule, access_request); 663 ), 664 665 TP_printk("domain=%llx access_request=%s port=%llu grants=%s", 666 __entry->domain_id, 667 __print_flags(__entry->access_request, "|", _LANDLOCK_ACCESS_NET_NAMES), 668 __entry->port, 669 __print_landlock_layers(grants, _LANDLOCK_ACCESS_NET_NAMES)) 670 ); 671 672 /** 673 * landlock_deny_access_fs - Filesystem access denied 674 * 675 * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the 676 * domain field. 677 * @same_exec: Whether the current task entered the denying domain itself. 678 * @logged: The domain's audit-logging decision for this denial. 679 * @blockers: Access mask that was blocked (zero for a mount-topology 680 * change, whose only blocker is the operation itself). 681 * @path: Filesystem path that was denied (never NULL). 682 * @pathname: Resolved path string (never NULL; an error placeholder on 683 * resolution failure). 684 * 685 * Emitted when a Landlock domain denies a filesystem access. 686 */ 687 TRACE_EVENT(landlock_deny_access_fs, 688 689 TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec, 690 bool logged, access_mask_t blockers, const struct path *path, 691 const char *pathname), 692 693 TP_ARGS(hierarchy, same_exec, logged, blockers, path, pathname), 694 695 TP_STRUCT__entry( 696 __field( __u64, domain_id ) 697 __field( bool, same_exec ) 698 __field( bool, logged ) 699 __field( access_mask_t, blockers ) 700 __field( dev_t, dev ) 701 __field( ino_t, ino ) 702 __string( pathname, pathname ) 703 ), 704 705 TP_fast_assign( 706 const struct inode *inode = d_backing_inode(path->dentry); 707 708 __entry->domain_id = hierarchy->id; 709 __entry->same_exec = same_exec; 710 __entry->logged = logged; 711 __entry->blockers = blockers; 712 __entry->dev = path->dentry->d_sb->s_dev; 713 /* 714 * A negative dentry has no backing inode, so mirror the 715 * guard in dump_common_audit_data() and report inode 0. 716 */ 717 __entry->ino = inode ? inode->i_ino : 0; 718 __assign_str(pathname); 719 ), 720 721 TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s dev=%u:%u ino=%lu path=%s", 722 __entry->domain_id, __entry->same_exec, __entry->logged, 723 __print_flags(__entry->blockers, "|", _LANDLOCK_ACCESS_FS_NAMES), 724 MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino, 725 __trace_print_untrusted_str(p, __get_str(pathname), 726 __get_dynamic_array_len(pathname) - 1)) 727 ); 728 729 /** 730 * landlock_deny_access_net - Network access denied 731 * 732 * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the 733 * domain field. 734 * @same_exec: Whether the current task entered the denying domain itself. 735 * @logged: The domain's audit-logging decision for this denial. 736 * @blockers: Access mask that was blocked. 737 * @sk: Socket object (never NULL), read without a socket lock, so its 738 * fields are a best-effort snapshot. The denied endpoint is not 739 * available: the hook runs before :manpage:`bind(2)` / 740 * :manpage:`connect(2)` sets the socket addresses. 741 * @sport: Source port in host endianness, set for bind denials (zero for 742 * an autobind/ephemeral port); zero for connect and send denials. 743 * @dport: Destination port in host endianness, set for connect and send 744 * denials; zero for bind denials, and also zero for a UDP send to 745 * an AF_UNSPEC address on an IPv6 socket (indistinguishable from a 746 * real destination port 0). The bind-vs-connect direction is 747 * given by @blockers, not by which port is set. 748 * 749 * Emitted when a Landlock domain denies a network operation. 750 * 751 * The port fields are converted from the socket's network byte order to 752 * host endianness before emitting. 753 */ 754 TRACE_EVENT(landlock_deny_access_net, 755 756 TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec, 757 bool logged, access_mask_t blockers, const struct sock *sk, 758 __u64 sport, __u64 dport), 759 760 TP_ARGS(hierarchy, same_exec, logged, blockers, sk, sport, dport), 761 762 TP_STRUCT__entry( 763 __field( __u64, domain_id ) 764 __field( bool, same_exec ) 765 __field( bool, logged ) 766 __field( access_mask_t, blockers ) 767 __field( __u64, sport ) 768 __field( __u64, dport ) 769 ), 770 771 TP_fast_assign( 772 __entry->domain_id = hierarchy->id; 773 __entry->same_exec = same_exec; 774 __entry->logged = logged; 775 __entry->blockers = blockers; 776 __entry->sport = sport; 777 __entry->dport = dport; 778 ), 779 780 TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s sport=%llu dport=%llu", 781 __entry->domain_id, __entry->same_exec, __entry->logged, 782 __print_flags(__entry->blockers, "|", _LANDLOCK_ACCESS_NET_NAMES), 783 __entry->sport, __entry->dport) 784 ); 785 786 /** 787 * landlock_deny_ptrace - Ptrace access denied by a Landlock domain 788 * 789 * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the 790 * domain field. 791 * @same_exec: Whether the current task entered the denying domain itself. 792 * @logged: The domain's audit-logging decision for this denial. 793 * @tracee_domain_id: The tracee's Landlock domain ID, or 0 if the tracee 794 * is unsandboxed. 795 * @tracee: The target task ptrace acted on (never NULL). tracee_pid is 796 * the init-namespace TGID (like audit's opid). 797 * 798 * Emitted when a Landlock domain denies a ptrace operation. 799 */ 800 TRACE_EVENT(landlock_deny_ptrace, 801 802 TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec, 803 bool logged, u64 tracee_domain_id, 804 const struct task_struct *tracee), 805 806 TP_ARGS(hierarchy, same_exec, logged, tracee_domain_id, tracee), 807 808 TP_STRUCT__entry( 809 __field( __u64, domain_id ) 810 __field( bool, same_exec ) 811 __field( bool, logged ) 812 __field( __u64, tracee_domain_id) 813 __field( pid_t, tracee_pid ) 814 __string( tracee_comm, tracee->comm ) 815 ), 816 817 TP_fast_assign( 818 __entry->domain_id = hierarchy->id; 819 __entry->same_exec = same_exec; 820 __entry->logged = logged; 821 __entry->tracee_domain_id = tracee_domain_id; 822 __entry->tracee_pid = task_tgid_nr((struct task_struct *)tracee); 823 __assign_str(tracee_comm); 824 ), 825 826 TP_printk("domain=%llx same_exec=%d logged=%d tracee_domain=%llx tracee_pid=%d tracee_comm=%s", 827 __entry->domain_id, __entry->same_exec, __entry->logged, 828 __entry->tracee_domain_id, __entry->tracee_pid, 829 __trace_print_untrusted_str(p, __get_str(tracee_comm), 830 __get_dynamic_array_len(tracee_comm) - 1)) 831 ); 832 833 /** 834 * landlock_deny_scope_signal - Signal delivery denied by 835 * LANDLOCK_SCOPE_SIGNAL 836 * 837 * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the 838 * domain field. 839 * @same_exec: Whether the current task entered the denying domain itself. 840 * @logged: The domain's audit-logging decision for this denial. 841 * @target_domain_id: The target's Landlock domain ID, or 0 if the target 842 * is unsandboxed. 843 * @target: The task the signal was aimed at (never NULL). target_pid is 844 * the init-namespace TGID (like audit's opid). 845 * 846 * Emitted when a Landlock domain denies signal delivery to a scoped-out 847 * target. 848 */ 849 TRACE_EVENT(landlock_deny_scope_signal, 850 851 TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec, 852 bool logged, u64 target_domain_id, 853 const struct task_struct *target), 854 855 TP_ARGS(hierarchy, same_exec, logged, target_domain_id, target), 856 857 TP_STRUCT__entry( 858 __field( __u64, domain_id ) 859 __field( bool, same_exec ) 860 __field( bool, logged ) 861 __field( __u64, target_domain_id) 862 __field( pid_t, target_pid ) 863 __string( target_comm, target->comm ) 864 ), 865 866 TP_fast_assign( 867 __entry->domain_id = hierarchy->id; 868 __entry->same_exec = same_exec; 869 __entry->logged = logged; 870 __entry->target_domain_id = target_domain_id; 871 __entry->target_pid = task_tgid_nr((struct task_struct *)target); 872 __assign_str(target_comm); 873 ), 874 875 TP_printk("domain=%llx same_exec=%d logged=%d target_domain=%llx target_pid=%d target_comm=%s", 876 __entry->domain_id, __entry->same_exec, __entry->logged, 877 __entry->target_domain_id, __entry->target_pid, 878 __trace_print_untrusted_str(p, __get_str(target_comm), 879 __get_dynamic_array_len(target_comm) - 1)) 880 ); 881 882 /** 883 * landlock_deny_scope_abstract_unix_socket - Abstract unix socket access 884 * denied by LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET 885 * 886 * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the 887 * domain field. 888 * @same_exec: Whether the current task entered the denying domain itself. 889 * @logged: The domain's audit-logging decision for this denial. 890 * @peer_domain_id: The peer's Landlock domain ID, or 0 if the peer is 891 * unsandboxed. 892 * @peer: Peer socket (never NULL). peer_pid is best-effort: it is 0 for 893 * a datagram peer (no SO_PEERCRED), so sun_path is the reliable 894 * peer identifier. 895 * 896 * Emitted when a Landlock domain denies access to a scoped-out abstract 897 * unix socket. 898 */ 899 TRACE_EVENT(landlock_deny_scope_abstract_unix_socket, 900 901 TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec, 902 bool logged, u64 peer_domain_id, const struct sock *peer), 903 904 TP_ARGS(hierarchy, same_exec, logged, peer_domain_id, peer), 905 906 TP_STRUCT__entry( 907 __field( __u64, domain_id ) 908 __field( bool, same_exec ) 909 __field( bool, logged ) 910 __field( __u64, peer_domain_id ) 911 __field( pid_t, peer_pid ) 912 /* 913 * Abstract socket names are untrusted binary data from 914 * user space. Use __string_len because abstract names 915 * are not NUL-terminated; their length is determined by 916 * addr->len. unix_sk(peer)->addr is stable here because 917 * the caller (hook_unix_stream_connect or 918 * hook_unix_may_send) holds unix_state_lock(peer). 919 */ 920 __string_len( sun_path, 921 unix_sk(peer)->addr ? 922 unix_sk(peer)->addr->name->sun_path + 1 : 923 "", 924 unix_sk(peer)->addr ? 925 unix_sk(peer)->addr->len - 926 offsetof(struct sockaddr_un, 927 sun_path) - 1 : 928 0) 929 ), 930 931 TP_fast_assign( 932 struct pid *peer_pid; 933 934 lockdep_assert_held(&unix_sk(peer)->lock); 935 __entry->domain_id = hierarchy->id; 936 __entry->same_exec = same_exec; 937 __entry->logged = logged; 938 __entry->peer_domain_id = peer_domain_id; 939 /* 940 * Best-effort (0 for a datagram peer). sk_peer_pid is 941 * canonically guarded by sk->sk_peer_lock, but the target 942 * peer's peercred is set once and not updated concurrently in 943 * these hooks, so this READ_ONCE() is safe; sun_path is the 944 * reliable identifier. 945 */ 946 peer_pid = READ_ONCE(peer->sk_peer_pid); 947 __entry->peer_pid = peer_pid ? pid_nr(peer_pid) : 0; 948 __assign_str(sun_path); 949 ), 950 951 TP_printk("domain=%llx same_exec=%d logged=%d peer_domain=%llx peer_pid=%d sun_path=%s", 952 __entry->domain_id, __entry->same_exec, __entry->logged, 953 __entry->peer_domain_id, __entry->peer_pid, 954 __trace_print_untrusted_str(p, __get_str(sun_path), 955 __get_dynamic_array_len(sun_path) - 1)) 956 ); 957 958 #undef _LANDLOCK_NAME_ENTRY 959 960 #endif /* _TRACE_LANDLOCK_H */ 961 962 /* This part must be outside protection */ 963 #include <trace/define_trace.h> 964 965 /* clang-format on */ 966