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