|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Landlock - Audit helpers |
| 4 | + * |
| 5 | + * Copyright © 2023-2025 Microsoft Corporation |
| 6 | + */ |
| 7 | + |
| 8 | +#include <kunit/test.h> |
| 9 | +#include <linux/audit.h> |
| 10 | +#include <linux/lsm_audit.h> |
| 11 | + |
| 12 | +#include "audit.h" |
| 13 | +#include "cred.h" |
| 14 | +#include "domain.h" |
| 15 | +#include "limits.h" |
| 16 | +#include "ruleset.h" |
| 17 | + |
| 18 | +static const char *get_blocker(const enum landlock_request_type type) |
| 19 | +{ |
| 20 | + switch (type) { |
| 21 | + case LANDLOCK_REQUEST_PTRACE: |
| 22 | + return "ptrace"; |
| 23 | + } |
| 24 | + |
| 25 | + WARN_ON_ONCE(1); |
| 26 | + return "unknown"; |
| 27 | +} |
| 28 | + |
| 29 | +static void log_blockers(struct audit_buffer *const ab, |
| 30 | + const enum landlock_request_type type) |
| 31 | +{ |
| 32 | + audit_log_format(ab, "%s", get_blocker(type)); |
| 33 | +} |
| 34 | + |
| 35 | +static struct landlock_hierarchy * |
| 36 | +get_hierarchy(const struct landlock_ruleset *const domain, const size_t layer) |
| 37 | +{ |
| 38 | + struct landlock_hierarchy *hierarchy = domain->hierarchy; |
| 39 | + ssize_t i; |
| 40 | + |
| 41 | + if (WARN_ON_ONCE(layer >= domain->num_layers)) |
| 42 | + return hierarchy; |
| 43 | + |
| 44 | + for (i = domain->num_layers - 1; i > layer; i--) { |
| 45 | + if (WARN_ON_ONCE(!hierarchy->parent)) |
| 46 | + break; |
| 47 | + |
| 48 | + hierarchy = hierarchy->parent; |
| 49 | + } |
| 50 | + |
| 51 | + return hierarchy; |
| 52 | +} |
| 53 | + |
| 54 | +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST |
| 55 | + |
| 56 | +static void test_get_hierarchy(struct kunit *const test) |
| 57 | +{ |
| 58 | + struct landlock_hierarchy dom0_hierarchy = { |
| 59 | + .id = 10, |
| 60 | + }; |
| 61 | + struct landlock_hierarchy dom1_hierarchy = { |
| 62 | + .parent = &dom0_hierarchy, |
| 63 | + .id = 20, |
| 64 | + }; |
| 65 | + struct landlock_hierarchy dom2_hierarchy = { |
| 66 | + .parent = &dom1_hierarchy, |
| 67 | + .id = 30, |
| 68 | + }; |
| 69 | + struct landlock_ruleset dom2 = { |
| 70 | + .hierarchy = &dom2_hierarchy, |
| 71 | + .num_layers = 3, |
| 72 | + }; |
| 73 | + |
| 74 | + KUNIT_EXPECT_EQ(test, 10, get_hierarchy(&dom2, 0)->id); |
| 75 | + KUNIT_EXPECT_EQ(test, 20, get_hierarchy(&dom2, 1)->id); |
| 76 | + KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, 2)->id); |
| 77 | + KUNIT_EXPECT_EQ(test, 30, get_hierarchy(&dom2, -1)->id); |
| 78 | +} |
| 79 | + |
| 80 | +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ |
| 81 | + |
| 82 | +static bool is_valid_request(const struct landlock_request *const request) |
| 83 | +{ |
| 84 | + if (WARN_ON_ONCE(request->layer_plus_one > LANDLOCK_MAX_NUM_LAYERS)) |
| 85 | + return false; |
| 86 | + |
| 87 | + if (WARN_ON_ONCE(!request->layer_plus_one)) |
| 88 | + return false; |
| 89 | + |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * landlock_log_denial - Create audit records related to a denial |
| 95 | + * |
| 96 | + * @subject: The Landlock subject's credential denying an action. |
| 97 | + * @request: Detail of the user space request. |
| 98 | + */ |
| 99 | +void landlock_log_denial(const struct landlock_cred_security *const subject, |
| 100 | + const struct landlock_request *const request) |
| 101 | +{ |
| 102 | + struct audit_buffer *ab; |
| 103 | + struct landlock_hierarchy *youngest_denied; |
| 104 | + size_t youngest_layer; |
| 105 | + |
| 106 | + if (WARN_ON_ONCE(!subject || !subject->domain || |
| 107 | + !subject->domain->hierarchy || !request)) |
| 108 | + return; |
| 109 | + |
| 110 | + if (!is_valid_request(request)) |
| 111 | + return; |
| 112 | + |
| 113 | + if (!audit_enabled) |
| 114 | + return; |
| 115 | + |
| 116 | + youngest_layer = request->layer_plus_one - 1; |
| 117 | + youngest_denied = get_hierarchy(subject->domain, youngest_layer); |
| 118 | + |
| 119 | + /* Ignores denials after an execution. */ |
| 120 | + if (!(subject->domain_exec & (1 << youngest_layer))) |
| 121 | + return; |
| 122 | + |
| 123 | + /* Uses consistent allocation flags wrt common_lsm_audit(). */ |
| 124 | + ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN, |
| 125 | + AUDIT_LANDLOCK_ACCESS); |
| 126 | + if (!ab) |
| 127 | + return; |
| 128 | + |
| 129 | + audit_log_format(ab, "domain=%llx blockers=", youngest_denied->id); |
| 130 | + log_blockers(ab, request->type); |
| 131 | + audit_log_lsm_data(ab, &request->audit); |
| 132 | + audit_log_end(ab); |
| 133 | +} |
| 134 | + |
| 135 | +#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST |
| 136 | + |
| 137 | +static struct kunit_case test_cases[] = { |
| 138 | + /* clang-format off */ |
| 139 | + KUNIT_CASE(test_get_hierarchy), |
| 140 | + {} |
| 141 | + /* clang-format on */ |
| 142 | +}; |
| 143 | + |
| 144 | +static struct kunit_suite test_suite = { |
| 145 | + .name = "landlock_audit", |
| 146 | + .test_cases = test_cases, |
| 147 | +}; |
| 148 | + |
| 149 | +kunit_test_suite(test_suite); |
| 150 | + |
| 151 | +#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */ |
0 commit comments