History

landlock: Add landlock_deny_access_fs and landlock_deny_access_net

Add per-type tracepoints emitted from landlock_log_denial() when an access is denied: landlock_deny_access_fs for filesystem denials and landlock_deny_access_net for network denials. They use the "deny_" prefix (rather than "check_") to mark that they fire only on a denial, and they complement the check_rule events by making the denial-by-absence case explicit (when no rule matches, no check_rule event fires). Unlike the audit records, these events fire regardless of the audit configuration and the domain's log flags: the user's "disable logging" intent applies to audit records, not to kernel tracing. The logged field records whether the domain's log policy would submit the denial to audit; it is the decision computed once by landlock_log_denial() and passed to both the audit and the tracing emitter, so a stateless ftrace filter can select the audit-visible denials with logged==1. TP_PROTO passes the denying hierarchy node, not the task's current domain, so domain_id reports the specific node that blocked the access, matching audit record semantics. (check_rule instead passes the current domain, which it needs to size its per-layer array.) same_exec is also passed explicitly because it is computed from the credential bitmask and is not derivable from the hierarchy pointer alone. The denial field is named blockers to match the audit record field. The filesystem path comes from the request's audit data. Its type selects which union member holds the object, exactly as dump_common_audit_data() selects it (a path, a file's path, an ioctl op's path, or a bare dentry); reading the wrong member would dereference garbage, so every reachable type has an explicit case and an unexpected one is flagged with WARN_ONCE() instead of misread. Path-backed types resolve via d_absolute_path() (as landlock_add_rule_fs does) and the bare-dentry case via dentry_path_raw(). The inode number is read defensively. A filesystem denial can carry a negative dentry (no backing inode), for example a denied creation, so the event mirrors the guard in dump_common_audit_data() and reports inode 0 rather than dereferencing a NULL inode. The sibling fs tracepoints do not need the guard: a dentry that matches a rule during an access check, or one opened to add a rule, always has a backing inode. Landlock tracepoints are reachable by unprivileged sandboxees, so a denial on a negative dentry with the event enabled must not fault the kernel.

Mickaël Salaün committed 3 months ago

Commit 01ce2601 parent 3f1f1064 files · +265 −1Browse files

Showing the changes to include (1 of 4 files). Show all

Unified · Split

include/trace/events/landlock.h+129−0
@@ -22,6 +22,7 @@ struct landlock_hierarchy; · 21 unmodified lines
2222 struct landlock_rule;
2323 struct landlock_ruleset;
2424 struct path;
25+struct sock;
2526
2627 #ifdef CREATE_TRACE_POINTS
2728
@@ -204,6 +205,20 @@ static inline const char *__trace_landlock_print_layers( · 176 unmodified lines
204205 * rule, before the final allow-or-deny verdict. They share domain (the
205206 * enforcing domain being evaluated), access_request (the access mask being
206207 * checked), and rule (the matching rule, with per-layer access masks).
208+ *
209+ * Denial fields
210+ * ~~~~~~~~~~~~~
211+ *
212+ * Every denial event shares three fields. domain is the ID of the
213+ * innermost domain that blocked the access. same_exec tells whether the
214+ * current task is the same executable that entered that domain. logged is
215+ * the domain's audit-logging decision for this denial (its log_status is
216+ * enabled and the per-execution flag selected by same_exec is set); a
217+ * stateless ftrace filter can select the denials the domain submits to
218+ * audit with logged==1, without reconstructing it from the per-execution
219+ * log flags. Denial events order their fields as domain, same_exec,
220+ * logged, then blockers (deny_access events only), then the type-specific
221+ * object fields, then any variable-length field.
207222 */
208223
209224 /*
@@ -628,6 +643,120 @@ TRACE_EVENT(landlock_check_rule_net, · 418 unmodified lines
628643 __print_landlock_layers(grants, _LANDLOCK_ACCESS_NET_NAMES))
629644 );
630645
646+/**
647+ * landlock_deny_access_fs - Filesystem access denied
648+ *
649+ * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the
650+ * domain field.
651+ * @same_exec: Whether the current task entered the denying domain itself.
652+ * @logged: The domain's audit-logging decision for this denial.
653+ * @blockers: Access mask that was blocked (zero for a mount-topology
654+ * change, whose only blocker is the operation itself).
655+ * @path: Filesystem path that was denied (never NULL).
656+ * @pathname: Resolved path string (never NULL; an error placeholder on
657+ * resolution failure).
658+ *
659+ * Emitted when a Landlock domain denies a filesystem access.
660+ */
661+TRACE_EVENT(landlock_deny_access_fs,
662+
663+ TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec,
664+ bool logged, access_mask_t blockers, const struct path *path,
665+ const char *pathname),
666+
667+ TP_ARGS(hierarchy, same_exec, logged, blockers, path, pathname),
668+
669+ TP_STRUCT__entry(
670+ __field( __u64, domain_id )
671+ __field( bool, same_exec )
672+ __field( bool, logged )
673+ __field( access_mask_t, blockers )
674+ __field( dev_t, dev )
675+ __field( ino_t, ino )
676+ __string( pathname, pathname )
677+ ),
678+
679+ TP_fast_assign(
680+ const struct inode *inode = d_backing_inode(path->dentry);
681+
682+ __entry->domain_id = hierarchy->id;
683+ __entry->same_exec = same_exec;
684+ __entry->logged = logged;
685+ __entry->blockers = blockers;
686+ __entry->dev = path->dentry->d_sb->s_dev;
687+ /*
688+ * A negative dentry has no backing inode, so mirror the
689+ * guard in dump_common_audit_data() and report inode 0.
690+ */
691+ __entry->ino = inode ? inode->i_ino : 0;
692+ __assign_str(pathname);
693+ ),
694+
695+ TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s dev=%u:%u ino=%lu path=%s",
696+ __entry->domain_id, __entry->same_exec, __entry->logged,
697+ __print_flags(__entry->blockers, "|", _LANDLOCK_ACCESS_FS_NAMES),
698+ MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino,
699+ __trace_print_untrusted_str(p, __get_str(pathname),
700+ __get_dynamic_array_len(pathname) - 1))
701+);
702+
703+/**
704+ * landlock_deny_access_net - Network access denied
705+ *
706+ * @hierarchy: Denying domain's hierarchy node (never NULL); its id is the
707+ * domain field.
708+ * @same_exec: Whether the current task entered the denying domain itself.
709+ * @logged: The domain's audit-logging decision for this denial.
710+ * @blockers: Access mask that was blocked.
711+ * @sk: Socket object (never NULL), read without a socket lock, so its
712+ * fields are a best-effort snapshot. The denied endpoint is not
713+ * available: the hook runs before :manpage:`bind(2)` /
714+ * :manpage:`connect(2)` sets the socket addresses.
715+ * @sport: Source port in host endianness, set for bind denials (zero for
716+ * an autobind/ephemeral port); zero for connect and send denials.
717+ * @dport: Destination port in host endianness, set for connect and send
718+ * denials; zero for bind denials, and also zero for a UDP send to
719+ * an AF_UNSPEC address on an IPv6 socket (indistinguishable from a
720+ * real destination port 0). The bind-vs-connect direction is
721+ * given by @blockers, not by which port is set.
722+ *
723+ * Emitted when a Landlock domain denies a network operation.
724+ *
725+ * The port fields are converted from the socket's network byte order to
726+ * host endianness before emitting.
727+ */
728+TRACE_EVENT(landlock_deny_access_net,
729+
730+ TP_PROTO(const struct landlock_hierarchy *hierarchy, bool same_exec,
731+ bool logged, access_mask_t blockers, const struct sock *sk,
732+ __u64 sport, __u64 dport),
733+
734+ TP_ARGS(hierarchy, same_exec, logged, blockers, sk, sport, dport),
735+
736+ TP_STRUCT__entry(
737+ __field( __u64, domain_id )
738+ __field( bool, same_exec )
739+ __field( bool, logged )
740+ __field( access_mask_t, blockers )
741+ __field( __u64, sport )
742+ __field( __u64, dport )
743+ ),
744+
745+ TP_fast_assign(
746+ __entry->domain_id = hierarchy->id;
747+ __entry->same_exec = same_exec;
748+ __entry->logged = logged;
749+ __entry->blockers = blockers;
750+ __entry->sport = sport;
751+ __entry->dport = dport;
752+ ),
753+
754+ TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s sport=%llu dport=%llu",
755+ __entry->domain_id, __entry->same_exec, __entry->logged,
756+ __print_flags(__entry->blockers, "|", _LANDLOCK_ACCESS_NET_NAMES),
757+ __entry->sport, __entry->dport)
758+);
759+
631760 #undef _LANDLOCK_NAME_ENTRY
632761
633762 #endif /* _TRACE_LANDLOCK_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t