Skip to content

aureport/ausearch: check for non-input stdin pipes #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions audisp/plugins/remote/audisp-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,6 @@ static int sync_error_handler (const char *why)
return 0;
}

static int is_pipe(int fd)
{
struct stat st;

if (fstat(fd, &st) == 0) {
if (S_ISFIFO(st.st_mode))
return 1;
}
return 0;
}

static void change_runlevel(const char *level)
{
char *argv[3];
Expand Down
42 changes: 42 additions & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <fcntl.h>
#include <stdlib.h> // strtol
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>

/*
* This function returns 1 if it is the last record in an event.
Expand Down Expand Up @@ -162,3 +164,43 @@ long time_string_to_seconds(const char *time_string,
return i;
}

int is_pipe(int fd)
{
struct stat st;

return (!fstat(fd, &st) && S_ISFIFO(st.st_mode));
}

/*
* Check if stdin is a pipe, and it is, check if it has data on it.
*
* Return:
* 0: no data to read
* 1: has data to read
* -errno: error from poll() or -EPIPE if errno wasn't set
*/
int check_stdin_data(void)
{
struct pollfd in = {
.fd = STDIN_FILENO,
.events = POLLIN,
};
int ret;

if (!is_pipe(in.fd))
return 0;

/* this is stdin, so a 0 timeout should be enough for this check */
ret = poll(&in, 1, 0);
if (ret < 0 || (in.revents & POLLERR)) {
ret = errno ? -errno : -EPIPE;
fprintf(stderr, "<error %d polling data from stdin>\n", ret);

return ret;
}

if (!ret || !(in.revents & POLLIN))
return 0;

return 1;
}
3 changes: 3 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ void wall_message(const char *fmt, ...)
;
#endif

int is_pipe(int fd);
int check_stdin_data(void);

AUDIT_HIDDEN_END
#endif

23 changes: 7 additions & 16 deletions src/aureport.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,10 @@ extern int force_logs;
extern time_t arg_eoe_timeout;


static int is_pipe(int fd)
{
struct stat st;

if (fstat(fd, &st) == 0) {
if (S_ISFIFO(st.st_mode))
return 1;
}
return 0;
}

int main(int argc, char *argv[])
{
struct rlimit limit;
int rc;
int rc, has_stdin;

/* Check params and build regexpr */
setlocale (LC_ALL, "");
Expand Down Expand Up @@ -111,6 +100,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "NOTE - using built-in logs: %s\n",
config.log_file);

has_stdin = check_stdin_data();
if (has_stdin < 0)
return 1;

/* Set timeout from the config file */
lol_set_eoe_timeout((time_t)config.end_of_event_timeout);

Expand Down Expand Up @@ -143,12 +136,10 @@ int main(int argc, char *argv[])
break;
}
}
} else if (force_logs)
} else if (force_logs || !has_stdin)
rc = process_logs();
else if (is_pipe(0))
rc = process_stdin();
else
rc = process_logs();
rc = process_stdin();
lol_clear(&lo);
if (rc)
return rc;
Expand Down
1 change: 1 addition & 0 deletions src/ausearch-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <time.h>
#include "ausearch-string.h"
#include "auparse-defs.h"
#include "common.h"

/*
* MAX_EVENT_DELTA_SECS is the maximum number of seconds it would take for
Expand Down
25 changes: 8 additions & 17 deletions src/ausearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,10 @@ extern void output_auparse_finish(void);
*/
extern time_t arg_eoe_timeout;

static int is_pipe(int fd)
{
struct stat st;
int pipe_mode=0;

if (fstat(fd, &st) == 0) {
if (S_ISFIFO(st.st_mode))
pipe_mode = 1;
}
return pipe_mode;
}

int main(int argc, char *argv[])
{
struct rlimit limit;
int rc;
int rc, has_stdin;
struct stat sb;

/* Check params and build regexpr */
Expand Down Expand Up @@ -124,6 +112,10 @@ int main(int argc, char *argv[])
}
}

has_stdin = check_stdin_data();
if (has_stdin < 0)
return 1;

/* Set timeout from the config file */
lol_set_eoe_timeout((time_t)config.end_of_event_timeout);

Expand Down Expand Up @@ -179,16 +171,15 @@ int main(int argc, char *argv[])
free_config(&config);
break;
}
} else if (force_logs)
} else if (force_logs || !has_stdin)
rc = process_logs();
else if (is_pipe(0)) {
else {
rc = process_stdin();
if (checkpt_filename)
fprintf(stderr,
"Warning - checkpointing stdin is not supported");
goto skip_checkpt; // Don't overwrite chkpt when reading a pipe
} else
rc = process_logs();
}

/* Generate a checkpoint if required */
if (checkpt_filename) {
Expand Down