| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/audit.h> |
| 4 |
#include <linux/netlink.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
|
| 7 |
#include "sd-dlopen.h" |
| 8 |
|
| 9 |
#include "errno-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "iovec-util.h" |
| 12 |
#include "libaudit-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "socket-util.h" |
| 15 |
|
| 16 |
#if HAVE_AUDIT |
| 17 |
static DLSYM_PROTOTYPE(audit_close) = NULL; |
| 18 |
DLSYM_PROTOTYPE(audit_log_acct_message) = NULL; |
| 19 |
DLSYM_PROTOTYPE(audit_log_user_avc_message) = NULL; |
| 20 |
DLSYM_PROTOTYPE(audit_log_user_comm_message) = NULL; |
| 21 |
static DLSYM_PROTOTYPE(audit_open) = NULL; |
| 22 |
#endif |
| 23 |
|
| 24 |
int dlopen_libaudit(int log_level) { |
| 25 |
#if HAVE_AUDIT |
| 26 |
static void *libaudit_dl = NULL; |
| 27 |
|
| 28 |
SD_ELF_NOTE_DLOPEN( |
| 29 |
"audit", |
| 30 |
"Support for Audit logging", |
| 31 |
SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED, |
| 32 |
"libaudit.so.1"); |
| 33 |
|
| 34 |
return dlopen_many_sym_or_warn( |
| 35 |
&libaudit_dl, |
| 36 |
"libaudit.so.1", |
| 37 |
log_level, |
| 38 |
DLSYM_ARG(audit_close), |
| 39 |
DLSYM_ARG(audit_log_acct_message), |
| 40 |
DLSYM_ARG(audit_log_user_avc_message), |
| 41 |
DLSYM_ARG(audit_log_user_comm_message), |
| 42 |
DLSYM_ARG(audit_open)); |
| 43 |
#else |
| 44 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 45 |
"libaudit support is not compiled in."); |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
#if HAVE_AUDIT |
| 50 |
static int try_audit_request(int fd) { |
| 51 |
struct iovec iov; |
| 52 |
struct msghdr mh; |
| 53 |
ssize_t n; |
| 54 |
|
| 55 |
assert(fd >= 0); |
| 56 |
|
| 57 |
struct { |
| 58 |
struct nlmsghdr hdr; |
| 59 |
struct nlmsgerr err; |
| 60 |
} _packed_ msg = { |
| 61 |
.hdr.nlmsg_len = NLMSG_LENGTH(0), |
| 62 |
.hdr.nlmsg_type = AUDIT_GET_FEATURE, |
| 63 |
.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, |
| 64 |
}; |
| 65 |
iov = IOVEC_MAKE(&msg, msg.hdr.nlmsg_len); |
| 66 |
mh = (struct msghdr) { |
| 67 |
.msg_iov = &iov, |
| 68 |
.msg_iovlen = 1, |
| 69 |
}; |
| 70 |
|
| 71 |
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0) |
| 72 |
return -errno; |
| 73 |
|
| 74 |
iov.iov_len = sizeof(msg); |
| 75 |
|
| 76 |
n = recvmsg_safe(fd, &mh, 0); |
| 77 |
if (n < 0) |
| 78 |
return n; |
| 79 |
if (n != NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
| 80 |
return -EIO; |
| 81 |
|
| 82 |
if (msg.hdr.nlmsg_type != NLMSG_ERROR) |
| 83 |
return -EINVAL; |
| 84 |
|
| 85 |
return msg.err.error; |
| 86 |
} |
| 87 |
#endif |
| 88 |
|
| 89 |
bool use_audit(void) { |
| 90 |
#if HAVE_AUDIT |
| 91 |
static int cached_use = -1; |
| 92 |
int r; |
| 93 |
|
| 94 |
if (cached_use >= 0) |
| 95 |
return cached_use; |
| 96 |
|
| 97 |
if (dlopen_libaudit(LOG_DEBUG) < 0) |
| 98 |
return (cached_use = false); |
| 99 |
|
| 100 |
_cleanup_close_ int fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT); |
| 101 |
if (fd < 0) { |
| 102 |
cached_use = !ERRNO_IS_PRIVILEGE(errno) && !ERRNO_IS_NOT_SUPPORTED(errno); |
| 103 |
if (cached_use) |
| 104 |
log_debug_errno(errno, "Unexpected error while creating audit socket, proceeding with its use: %m"); |
| 105 |
else |
| 106 |
log_debug_errno(errno, "Won't talk to audit, because feature or privilege absent: %m"); |
| 107 |
} else { |
| 108 |
/* If we try and use the audit fd but get -ECONNREFUSED, it is because we are not in the |
| 109 |
* initial user namespace, and the kernel does not have support for audit outside of the |
| 110 |
* initial user namespace (see |
| 111 |
* https://elixir.bootlin.com/linux/latest/C/ident/audit_netlink_ok). |
| 112 |
* |
| 113 |
* If we receive any other error, do not disable audit because we are not sure that the error |
| 114 |
* indicates that audit will not work in general. */ |
| 115 |
r = try_audit_request(fd); |
| 116 |
if (r < 0) { |
| 117 |
cached_use = r != -ECONNREFUSED; |
| 118 |
log_debug_errno(r, cached_use ? |
| 119 |
"Failed to make request on audit fd, ignoring: %m" : |
| 120 |
"Won't talk to audit: %m"); |
| 121 |
} else |
| 122 |
cached_use = true; |
| 123 |
} |
| 124 |
|
| 125 |
return cached_use; |
| 126 |
#else |
| 127 |
return false; |
| 128 |
#endif |
| 129 |
} |
| 130 |
|
| 131 |
int close_audit_fd(int fd) { |
| 132 |
#if HAVE_AUDIT |
| 133 |
if (fd >= 0) |
| 134 |
sym_audit_close(fd); |
| 135 |
#else |
| 136 |
assert(fd < 0); |
| 137 |
#endif |
| 138 |
return -EBADF; |
| 139 |
} |
| 140 |
|
| 141 |
int open_audit_fd_or_warn(void) { |
| 142 |
#if HAVE_AUDIT |
| 143 |
int r; |
| 144 |
|
| 145 |
r = dlopen_libaudit(LOG_DEBUG); |
| 146 |
if (r < 0) |
| 147 |
return r; |
| 148 |
|
| 149 |
/* If the kernel lacks netlink or audit support, don't worry about it. */ |
| 150 |
int fd = sym_audit_open(); |
| 151 |
if (fd < 0) |
| 152 |
return log_full_errno(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING, |
| 153 |
errno, "Failed to connect to audit log, ignoring: %m"); |
| 154 |
|
| 155 |
return fd; |
| 156 |
#else |
| 157 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libaudit support not compiled in"); |
| 158 |
#endif |
| 159 |
} |