Debian Codegraph @

src/journal/journald-client.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cgroup-util.h"
5 #include "errno-util.h"
6 #include "journald-client.h"
7 #include "journald-context.h"
8 #include "log.h"
9 #include "nulstr-util.h"
10 #include "pcre2-util.h"
11 #include "set.h"
12 #include "strv.h"
13
14 /* This consumes both `allow_list` and `deny_list` arguments. Hence, those arguments are not owned by the
15 * caller anymore and should not be freed. */
16 static void client_set_filtering_patterns(ClientContext *c, Set *allow_list, Set *deny_list) {
17 assert(c);
18
19 set_free_and_replace(c->log_filter_allowed_patterns, allow_list);
20 set_free_and_replace(c->log_filter_denied_patterns, deny_list);
21 }
22
23 static int client_parse_log_filter_nulstr(const char *nulstr, size_t len, Set **ret) {
24 _cleanup_set_free_ Set *s = NULL;
25 _cleanup_strv_free_ char **patterns_strv = NULL;
26 int r;
27
28 assert(nulstr);
29 assert(ret);
30
31 patterns_strv = strv_parse_nulstr(nulstr, len);
32 if (!patterns_strv)
33 return log_oom_debug();
34
35 STRV_FOREACH(pattern, patterns_strv) {
36 _cleanup_(pcre2_code_freep) pcre2_code *compiled_pattern = NULL;
37
38 r = pattern_compile_and_log(*pattern, 0, &compiled_pattern);
39 if (r < 0)
40 return r;
41
42 r = set_ensure_consume(&s, &pcre2_code_hash_ops_free, TAKE_PTR(compiled_pattern));
43 if (r < 0)
44 return log_debug_errno(r, "Failed to insert regex into set: %m");
45 }
46
47 *ret = TAKE_PTR(s);
48
49 return 0;
50 }
51
52 int client_context_read_log_filter_patterns(ClientContext *c, const char *cgroup) {
53 int r;
54
55 assert(c);
56
57 _cleanup_free_ char *unit_cgroup = NULL;
58 r = cg_path_get_unit_path(cgroup, &unit_cgroup);
59 if (r < 0)
60 return log_debug_errno(r, "Failed to get the unit's cgroup path for %s: %m", cgroup);
61
62 _cleanup_free_ char *xattr = NULL;
63 size_t xattr_size = 0;
64 r = cg_get_xattr(unit_cgroup, "user.journald_log_filter_patterns", &xattr, &xattr_size);
65 if (ERRNO_IS_NEG_XATTR_ABSENT(r)) {
66 client_set_filtering_patterns(c, /* allow_list= */ NULL, /* deny_list= */ NULL);
67 return 0;
68 }
69 if (r < 0)
70 return log_debug_errno(r, "Failed to get user.journald_log_filter_patterns xattr for %s: %m", unit_cgroup);
71
72 const char *xattr_end = xattr + xattr_size;
73
74 /* We expect '0xff' to be present in the attribute, even if the lists are empty. We expect the
75 * following:
76 * - Allow list, but no deny list: 0xXX, ...., 0xff
77 * - No allow list, but deny list: 0xff, 0xXX, ....
78 * - Allow list, and deny list: 0xXX, ...., 0xff, 0xXX, ....
79 * This is due to the fact allowed and denied patterns list are two nulstr joined together with '0xff'.
80 * None of the allowed or denied nulstr have a nul-termination character.
81 *
82 * We do not expect both the allow list and deny list to be empty, as this condition is tested
83 * before writing to xattr. */
84 const char *deny_list_xattr = memchr(xattr, (char)0xff, xattr_size);
85 if (!deny_list_xattr)
86 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
87 "Missing delimiter in cgroup user.journald_log_filter_patterns attribute.");
88
89 _cleanup_set_free_ Set *allow_list = NULL;
90 r = client_parse_log_filter_nulstr(xattr, deny_list_xattr - xattr, &allow_list);
91 if (r < 0)
92 return r;
93
94 /* Use 'deny_list_xattr + 1' to skip '0xff'. */
95 ++deny_list_xattr;
96 _cleanup_set_free_ Set *deny_list = NULL;
97 r = client_parse_log_filter_nulstr(deny_list_xattr, xattr_end - deny_list_xattr, &deny_list);
98 if (r < 0)
99 return r;
100
101 client_set_filtering_patterns(c, TAKE_PTR(allow_list), TAKE_PTR(deny_list));
102
103 return 0;
104 }
105
106 int client_context_check_keep_log(ClientContext *c, const char *message, size_t len) {
107 pcre2_code *regex;
108
109 if (!c || !message)
110 return true;
111
112 SET_FOREACH(regex, c->log_filter_denied_patterns)
113 if (pattern_matches_and_log(regex, message, len, NULL) > 0)
114 return false;
115
116 SET_FOREACH(regex, c->log_filter_allowed_patterns)
117 if (pattern_matches_and_log(regex, message, len, NULL) > 0)
118 return true;
119
120 return set_isempty(c->log_filter_allowed_patterns);
121 }