1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "journald-audit.h"
24 #include "audit-type.h"
26 typedef struct MapField
{
27 const char *audit_field
;
28 const char *journal_field
;
29 int (*map
)(const char *field
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
);
32 static int map_simple_field(const char *field
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
) {
33 _cleanup_free_
char *c
= NULL
;
34 size_t l
= 0, allocated
= 0;
44 c
= malloc(allocated
);
49 for (e
= *p
; *e
!= ' ' && *e
!= 0; e
++) {
50 if (!GREEDY_REALLOC(c
, allocated
, l
+2))
58 if (!GREEDY_REALLOC(*iov
, *n_iov_allocated
, *n_iov
+ 1))
61 (*iov
)[*n_iov
].iov_base
= c
;
62 (*iov
)[*n_iov
].iov_len
= l
;
71 static int map_string_field_internal(const char *field
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
, bool filter_printable
) {
72 _cleanup_free_
char *c
= NULL
;
81 /* The kernel formats string fields in one of two formats. */
84 /* Normal quoted syntax */
90 l
= strlen(field
) + (e
- s
);
95 *((char*) mempcpy(stpcpy(c
, field
), s
, e
- s
)) = 0;
99 } else if (unhexchar(**p
) >= 0) {
100 /* Hexadecimal escaping */
101 size_t allocated
= 0;
105 c
= malloc(allocated
);
110 for (e
= *p
; *e
!= ' ' && *e
!= 0; e
+= 2) {
122 x
= ((uint8_t) a
<< 4 | (uint8_t) b
);
124 if (filter_printable
&& x
< (uint8_t) ' ')
127 if (!GREEDY_REALLOC(c
, allocated
, l
+2))
137 if (!GREEDY_REALLOC(*iov
, *n_iov_allocated
, *n_iov
+ 1))
140 (*iov
)[*n_iov
].iov_base
= c
;
141 (*iov
)[*n_iov
].iov_len
= l
;
150 static int map_string_field(const char *field
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
) {
151 return map_string_field_internal(field
, p
, iov
, n_iov_allocated
, n_iov
, false);
154 static int map_string_field_printable(const char *field
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
) {
155 return map_string_field_internal(field
, p
, iov
, n_iov_allocated
, n_iov
, true);
158 static int map_generic_field(const char *prefix
, const char **p
, struct iovec
**iov
, size_t *n_iov_allocated
, unsigned *n_iov
) {
163 /* Implements fallback mappings for all fields we don't know */
165 for (e
= *p
; e
< *p
+ 16; e
++) {
167 if (*e
== 0 || *e
== ' ')
173 if (!((*e
>= 'a' && *e
<= 'z') ||
174 (*e
>= 'A' && *e
<= 'Z') ||
175 (*e
>= '0' && *e
<= '9') ||
176 *e
== '_' || *e
== '-'))
180 if (e
<= *p
|| e
>= *p
+ 16)
183 c
= alloca(strlen(prefix
) + (e
- *p
) + 2);
185 t
= stpcpy(c
, prefix
);
186 for (f
= *p
; f
< e
; f
++) {
189 if (*f
>= 'a' && *f
<= 'z')
190 x
= (*f
- 'a') + 'A'; /* uppercase */
192 x
= '_'; /* dashes → underscores */
202 r
= map_simple_field(c
, &e
, iov
, n_iov_allocated
, n_iov
);
210 /* Kernel fields are those occurring in the audit string before
211 * msg='. All of these fields are trusted, hence carry the "_" prefix.
212 * We try to translate the fields we know into our native names. The
213 * other's are generically mapped to _AUDIT_FIELD_XYZ= */
214 static const MapField map_fields_kernel
[] = {
216 /* First, we map certain well-known audit fields into native
217 * well-known fields */
218 { "pid=", "_PID=", map_simple_field
},
219 { "ppid=", "_PPID=", map_simple_field
},
220 { "uid=", "_UID=", map_simple_field
},
221 { "euid=", "_EUID=", map_simple_field
},
222 { "fsuid=", "_FSUID=", map_simple_field
},
223 { "gid=", "_GID=", map_simple_field
},
224 { "egid=", "_EGID=", map_simple_field
},
225 { "fsgid=", "_FSGID=", map_simple_field
},
226 { "tty=", "_TTY=", map_simple_field
},
227 { "ses=", "_AUDIT_SESSION=", map_simple_field
},
228 { "auid=", "_AUDIT_LOGINUID=", map_simple_field
},
229 { "subj=", "_SELINUX_CONTEXT=", map_simple_field
},
230 { "comm=", "_COMM=", map_string_field
},
231 { "exe=", "_EXE=", map_string_field
},
232 { "proctitle=", "_CMDLINE=", map_string_field_printable
},
234 /* Some fields don't map to native well-known fields. However,
235 * we know that they are string fields, hence let's undo
236 * string field escaping for them, though we stick to the
237 * generic field names. */
238 { "path=", "_AUDIT_FIELD_PATH=", map_string_field
},
239 { "dev=", "_AUDIT_FIELD_DEV=", map_string_field
},
240 { "name=", "_AUDIT_FIELD_NAME=", map_string_field
},
244 /* Userspace fields are those occurring in the audit string after
245 * msg='. All of these fields are untrusted, hence carry no "_"
246 * prefix. We map the fields we don't know to AUDIT_FIELD_XYZ= */
247 static const MapField map_fields_userspace
[] = {
248 { "cwd=", "AUDIT_FIELD_CWD=", map_string_field
},
249 { "cmd=", "AUDIT_FIELD_CMD=", map_string_field
},
250 { "acct=", "AUDIT_FIELD_ACCT=", map_string_field
},
251 { "exe=", "AUDIT_FIELD_EXE=", map_string_field
},
252 { "comm=", "AUDIT_FIELD_COMM=", map_string_field
},
256 static int map_all_fields(
258 const MapField map_fields
[],
262 size_t *n_iov_allocated
,
269 assert(n_iov_allocated
);
277 p
+= strspn(p
, WHITESPACE
);
283 v
= startswith(p
, "msg='");
288 /* Userspace message. It's enclosed in
289 simple quotation marks, is not
290 escaped, but the last field in the
291 line, hence let's remove the
292 quotation mark, and apply the
293 userspace mapping instead of the
296 e
= endswith(v
, "'");
298 return 0; /* don't continue splitting up if the final quotation mark is missing */
300 c
= strndupa(v
, e
- v
);
301 return map_all_fields(c
, map_fields_userspace
, "AUDIT_FIELD_", false, iov
, n_iov_allocated
, n_iov
);
305 /* Try to map the kernel fields to our own names */
306 for (m
= map_fields
; m
->audit_field
; m
++) {
307 v
= startswith(p
, m
->audit_field
);
311 r
= m
->map(m
->journal_field
, &v
, iov
, n_iov_allocated
, n_iov
);
313 return log_debug_errno(r
, "Failed to parse audit array: %m");
323 r
= map_generic_field(prefix
, &p
, iov
, n_iov_allocated
, n_iov
);
325 return log_debug_errno(r
, "Failed to parse audit array: %m");
328 /* Couldn't process as generic field, let's just skip over it */
329 p
+= strcspn(p
, WHITESPACE
);
334 static void process_audit_string(Server
*s
, int type
, const char *data
, size_t size
) {
335 _cleanup_free_
struct iovec
*iov
= NULL
;
336 size_t n_iov_allocated
= 0;
337 unsigned n_iov
= 0, k
;
338 uint64_t seconds
, msec
, id
;
339 const char *p
, *type_name
;
341 char id_field
[sizeof("_AUDIT_ID=") + DECIMAL_STR_MAX(uint64_t)],
342 type_field
[sizeof("_AUDIT_TYPE=") + DECIMAL_STR_MAX(int)],
343 source_time_field
[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t
)];
354 /* Note that the input buffer is NUL terminated, but let's
355 * check whether there is a spurious NUL byte */
356 if (memchr(data
, 0, size
))
359 p
= startswith(data
, "audit");
363 if (sscanf(p
, "(%" PRIu64
".%" PRIu64
":%" PRIu64
"):%n",
371 p
+= strspn(p
, WHITESPACE
);
376 n_iov_allocated
= N_IOVEC_META_FIELDS
+ 7;
377 iov
= new(struct iovec
, n_iov_allocated
);
383 IOVEC_SET_STRING(iov
[n_iov
++], "_TRANSPORT=audit");
385 sprintf(source_time_field
, "_SOURCE_REALTIME_TIMESTAMP=%" PRIu64
,
386 (usec_t
) seconds
* USEC_PER_SEC
+ (usec_t
) msec
* USEC_PER_MSEC
);
387 IOVEC_SET_STRING(iov
[n_iov
++], source_time_field
);
389 sprintf(type_field
, "_AUDIT_TYPE=%i", type
);
390 IOVEC_SET_STRING(iov
[n_iov
++], type_field
);
392 sprintf(id_field
, "_AUDIT_ID=%" PRIu64
, id
);
393 IOVEC_SET_STRING(iov
[n_iov
++], id_field
);
395 assert_cc(32 == LOG_AUTH
);
396 IOVEC_SET_STRING(iov
[n_iov
++], "SYSLOG_FACILITY=32");
397 IOVEC_SET_STRING(iov
[n_iov
++], "SYSLOG_IDENTIFIER=audit");
399 type_name
= audit_type_name_alloca(type
);
401 m
= strjoina("MESSAGE=", type_name
, " ", p
);
402 IOVEC_SET_STRING(iov
[n_iov
++], m
);
406 map_all_fields(p
, map_fields_kernel
, "_AUDIT_FIELD_", true, &iov
, &n_iov_allocated
, &n_iov
);
408 if (!GREEDY_REALLOC(iov
, n_iov_allocated
, n_iov
+ N_IOVEC_META_FIELDS
)) {
413 server_dispatch_message(s
, iov
, n_iov
, n_iov_allocated
, NULL
, NULL
, NULL
, 0, NULL
, LOG_NOTICE
, 0);
416 /* free() all entries that map_all_fields() added. All others
417 * are allocated on the stack or are constant. */
419 for (; z
< n_iov
; z
++)
420 free(iov
[z
].iov_base
);
423 void server_process_audit_message(
427 const struct ucred
*ucred
,
428 const union sockaddr_union
*sa
,
431 const struct nlmsghdr
*nl
= buffer
;
435 if (buffer_size
< ALIGN(sizeof(struct nlmsghdr
)))
440 /* Filter out fake data */
442 salen
!= sizeof(struct sockaddr_nl
) ||
443 sa
->nl
.nl_family
!= AF_NETLINK
||
444 sa
->nl
.nl_pid
!= 0) {
445 log_debug("Audit netlink message from invalid sender.");
449 if (!ucred
|| ucred
->pid
!= 0) {
450 log_debug("Audit netlink message with invalid credentials.");
454 if (!NLMSG_OK(nl
, buffer_size
)) {
455 log_error("Audit netlink message truncated.");
459 /* Ignore special Netlink messages */
460 if (IN_SET(nl
->nlmsg_type
, NLMSG_NOOP
, NLMSG_ERROR
))
463 /* Below AUDIT_FIRST_USER_MSG theer are only control messages, let's ignore those */
464 if (nl
->nlmsg_type
< AUDIT_FIRST_USER_MSG
)
467 process_audit_string(s
, nl
->nlmsg_type
, NLMSG_DATA(nl
), nl
->nlmsg_len
- ALIGN(sizeof(struct nlmsghdr
)));
470 static int enable_audit(int fd
, bool b
) {
473 struct nlmsghdr header
;
474 uint8_t header_space
[NLMSG_HDRLEN
];
476 struct audit_status body
;
477 } _packed_ request
= {
478 .header
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct audit_status
)),
479 .header
.nlmsg_type
= AUDIT_SET
,
480 .header
.nlmsg_flags
= NLM_F_REQUEST
,
481 .header
.nlmsg_seq
= 1,
482 .header
.nlmsg_pid
= 0,
483 .body
.mask
= AUDIT_STATUS_ENABLED
,
486 union sockaddr_union sa
= {
487 .nl
.nl_family
= AF_NETLINK
,
490 struct iovec iovec
= {
491 .iov_base
= &request
,
492 .iov_len
= NLMSG_LENGTH(sizeof(struct audit_status
)),
498 .msg_namelen
= sizeof(sa
.nl
),
503 n
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
506 if (n
!= NLMSG_LENGTH(sizeof(struct audit_status
)))
509 /* We don't wait for the result here, we can't do anything
515 int server_open_audit(Server
*s
) {
516 static const int one
= 1;
519 if (s
->audit_fd
< 0) {
520 static const union sockaddr_union sa
= {
521 .nl
.nl_family
= AF_NETLINK
,
523 .nl
.nl_groups
= AUDIT_NLGRP_READLOG
,
526 s
->audit_fd
= socket(AF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, NETLINK_AUDIT
);
527 if (s
->audit_fd
< 0) {
528 if (errno
== EAFNOSUPPORT
|| errno
== EPROTONOSUPPORT
)
529 log_debug("Audit not supported in the kernel.");
531 log_warning_errno(errno
, "Failed to create audit socket, ignoring: %m");
536 if (bind(s
->audit_fd
, &sa
.sa
, sizeof(sa
.nl
)) < 0) {
537 log_warning_errno(errno
,
538 "Failed to join audit multicast group. "
539 "The kernel is probably too old or multicast reading is not supported. "
541 s
->audit_fd
= safe_close(s
->audit_fd
);
545 fd_nonblock(s
->audit_fd
, 1);
547 r
= setsockopt(s
->audit_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
549 return log_error_errno(errno
, "Failed to set SO_PASSCRED on audit socket: %m");
551 r
= sd_event_add_io(s
->event
, &s
->audit_event_source
, s
->audit_fd
, EPOLLIN
, server_process_datagram
, s
);
553 return log_error_errno(r
, "Failed to add audit fd to event loop: %m");
555 /* We are listening now, try to enable audit */
556 r
= enable_audit(s
->audit_fd
, true);
558 log_warning_errno(r
, "Failed to issue audit enable call: %m");