2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 18:08:23 +00:00
sudo/plugins/sudoers/linux_audit.c

115 lines
3.1 KiB
C
Raw Normal View History

2010-06-16 11:17:02 -04:00
/*
2019-04-29 07:21:51 -06:00
* SPDX-License-Identifier: ISC
*
2017-12-03 17:53:40 -07:00
* Copyright (c) 2010-2015 Todd C. Miller <Todd.Miller@sudo.ws>
2010-06-16 11:17:02 -04:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2018-10-26 08:39:09 -06:00
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
2010-06-16 11:17:02 -04:00
#include <config.h>
#ifdef HAVE_LINUX_AUDIT
2010-06-16 11:17:02 -04:00
#include <stdio.h>
#include <stdlib.h>
2010-06-16 11:17:02 -04:00
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <libaudit.h>
#include "sudoers.h"
#include "linux_audit.h"
2010-06-16 11:17:02 -04:00
#define AUDIT_NOT_CONFIGURED -2
2010-06-16 11:17:02 -04:00
/*
* Open audit connection if possible.
* Returns audit fd on success and -1 on failure.
*/
static int
linux_audit_open(void)
2010-06-16 11:17:02 -04:00
{
static int au_fd = -1;
debug_decl(linux_audit_open, SUDOERS_DEBUG_AUDIT);
2010-06-16 11:17:02 -04:00
if (au_fd != -1)
debug_return_int(au_fd);
2010-06-16 11:17:02 -04:00
au_fd = audit_open();
if (au_fd == -1) {
/* Kernel may not have audit support. */
if (errno == EINVAL || errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT)
au_fd = AUDIT_NOT_CONFIGURED;
else
sudo_warn(U_("unable to open audit system"));
2010-06-16 11:17:02 -04:00
} else {
(void)fcntl(au_fd, F_SETFD, FD_CLOEXEC);
}
debug_return_int(au_fd);
2010-06-16 11:17:02 -04:00
}
int
linux_audit_command(char *const argv[], int result)
2010-06-16 11:17:02 -04:00
{
int au_fd, rc = -1;
2020-08-07 14:22:56 -06:00
char *cp, *command = NULL;
char * const *av;
2010-06-16 11:17:02 -04:00
size_t size, n;
debug_decl(linux_audit_command, SUDOERS_DEBUG_AUDIT);
2010-06-16 11:17:02 -04:00
/* Don't return an error if auditing is not configured. */
if ((au_fd = linux_audit_open()) < 0)
debug_return_int(au_fd == AUDIT_NOT_CONFIGURED ? 0 : -1);
2010-06-16 11:17:02 -04:00
/* Convert argv to a flat string. */
for (size = 0, av = argv; *av != NULL; av++)
size += strlen(*av) + 1;
2020-08-07 14:22:56 -06:00
if (size != 0)
command = malloc(size);
if (command == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
2018-10-19 13:35:20 -06:00
for (av = argv, cp = command; *av != NULL; av++) {
2010-06-16 11:17:02 -04:00
n = strlcpy(cp, *av, size - (cp - command));
if (n >= size - (cp - command)) {
sudo_warnx(U_("internal error, %s overflow"), __func__);
goto done;
}
2010-06-16 11:17:02 -04:00
cp += n;
*cp++ = ' ';
}
*--cp = '\0';
/* Log command, ignoring ECONNREFUSED on error. */
if (audit_log_user_command(au_fd, AUDIT_USER_CMD, command, NULL, result) <= 0) {
if (errno != ECONNREFUSED) {
sudo_warn(U_("unable to send audit message"));
goto done;
}
}
rc = 0;
2010-06-16 11:17:02 -04:00
done:
free(command);
2010-06-16 11:17:02 -04:00
debug_return_int(rc);
2010-06-16 11:17:02 -04:00
}
#endif /* HAVE_LINUX_AUDIT */