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
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2010-06-16 11:17:02 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
2015-05-21 11:07:13 -06:00
|
|
|
#ifdef HAVE_LINUX_AUDIT
|
|
|
|
|
2010-06-16 11:17:02 -04:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2010-06-16 11:17:02 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <libaudit.h>
|
|
|
|
|
2014-10-23 09:26:13 -06:00
|
|
|
#include "sudoers.h"
|
2010-09-07 16:45:19 -04:00
|
|
|
#include "linux_audit.h"
|
2010-06-16 11:17:02 -04:00
|
|
|
|
2014-03-26 13:00:56 -06: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.
|
|
|
|
*/
|
2014-03-26 13:00:56 -06:00
|
|
|
static int
|
|
|
|
linux_audit_open(void)
|
2010-06-16 11:17:02 -04:00
|
|
|
{
|
|
|
|
static int au_fd = -1;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(linux_audit_open, SUDOERS_DEBUG_AUDIT);
|
2010-06-16 11:17:02 -04:00
|
|
|
|
|
|
|
if (au_fd != -1)
|
2011-10-22 14:40:21 -04:00
|
|
|
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. */
|
2014-10-13 08:33:25 -06:00
|
|
|
if (errno == EINVAL || errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT)
|
2014-08-15 15:56:25 -06:00
|
|
|
au_fd = AUDIT_NOT_CONFIGURED;
|
2014-10-13 08:33:25 -06:00
|
|
|
else
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warn("%s", U_("unable to open audit system"));
|
2021-01-06 10:16:00 -07:00
|
|
|
} else if (fcntl(au_fd, F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
sudo_warn("%s", U_("unable to open audit system"));
|
|
|
|
audit_close(au_fd);
|
|
|
|
au_fd = -1;
|
2010-06-16 11:17:02 -04:00
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(au_fd);
|
2010-06-16 11:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-06-03 20:22:25 -06:00
|
|
|
linux_audit_command(char *const argv[], int result)
|
2010-06-16 11:17:02 -04:00
|
|
|
{
|
2014-03-26 13:00:56 -06:00
|
|
|
int au_fd, rc = -1;
|
2020-08-07 14:22:56 -06:00
|
|
|
char *cp, *command = NULL;
|
2020-06-03 20:22:25 -06:00
|
|
|
char * const *av;
|
2010-06-16 11:17:02 -04:00
|
|
|
size_t size, n;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(linux_audit_command, SUDOERS_DEBUG_AUDIT);
|
2010-06-16 11:17:02 -04:00
|
|
|
|
2014-03-26 13:00:56 -06: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);
|
2015-06-17 06:49:59 -06:00
|
|
|
if (command == NULL) {
|
2015-06-19 14:51:17 -06:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2015-06-17 06:49:59 -06:00
|
|
|
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));
|
2012-08-10 12:18:38 -04:00
|
|
|
if (n >= size - (cp - command)) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
2014-03-26 13:00:56 -06:00
|
|
|
goto done;
|
2012-08-10 12:18:38 -04:00
|
|
|
}
|
2010-06-16 11:17:02 -04:00
|
|
|
cp += n;
|
|
|
|
*cp++ = ' ';
|
|
|
|
}
|
|
|
|
*--cp = '\0';
|
|
|
|
|
2010-09-21 07:56:16 -04:00
|
|
|
/* Log command, ignoring ECONNREFUSED on error. */
|
2014-03-26 13:00:56 -06:00
|
|
|
if (audit_log_user_command(au_fd, AUDIT_USER_CMD, command, NULL, result) <= 0) {
|
|
|
|
if (errno != ECONNREFUSED) {
|
2020-08-12 13:45:09 -06:00
|
|
|
sudo_warn("%s", U_("unable to send audit message"));
|
2014-03-26 13:00:56 -06:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
2010-06-16 11:17:02 -04:00
|
|
|
|
2014-03-26 13:00:56 -06:00
|
|
|
done:
|
2015-06-17 06:49:59 -06:00
|
|
|
free(command);
|
2010-06-16 11:17:02 -04:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(rc);
|
2010-06-16 11:17:02 -04:00
|
|
|
}
|
2015-05-21 11:07:13 -06:00
|
|
|
|
|
|
|
#endif /* HAVE_LINUX_AUDIT */
|