Skip to content
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

Syslog parser drop invalid #3565

Merged
merged 25 commits into from Mar 19, 2021

Conversation

bazsi
Copy link
Collaborator

@bazsi bazsi commented Feb 6, 2021

This PR contains a combination of a long-needed msg-format related refactor (moving common code out of format modules into the msg-format layer) and the ability for syslog-parser() to drop incorrectly formatted messages (right now it is only RFC5424 that can indicate invalid messages, RFC3164 consumes everything).

The motivation behind this feature is that more and more rfc5424 style messages are actually incorrectly formatted, while still containing the initial "1 " version indicator right after the priority field. Instead of making the parser less strict, the drop-invalid() setting allows the easy identification of these messages, so that an alternative parser can be applied, like this:

@version: 3.30

log {
        source { tcp(port(2000) flags(no-parse)); };

        if {
                parser { syslog-parser(drop-invalid(yes) flags(syslog-protocol)); };
        } else {
                # drop RFC5424 indicator as it was not right
                rewrite { subst("^(<[0-9]+>)1 ", "$1"); };
                parser { syslog-parser(drop-invalid(no)); };
        };
        destination { file("log"); };
};

This was a long requested feature by the https://github.com/splunk/splunk-connect-for-syslog team @rfaircloth-splunk

@kira-syslogng
Copy link
Contributor

Build FAILURE

@lgtm-com
Copy link

lgtm-com bot commented Feb 6, 2021

This pull request introduces 2 alerts when merging 6f67a4cb2c1f65ab7d4dbe86c486f64670d59464 into 975d950 - view on LGTM.com

new alerts:

  • 2 for Implicit function declaration

@kira-syslogng
Copy link
Contributor

Build FAILURE

@bazsi bazsi changed the title WIP: Syslog parser drop invalid Syslog parser drop invalid Feb 15, 2021
@bazsi
Copy link
Collaborator Author

bazsi commented Feb 15, 2021

dropping "WIP", this should be good enough for review. The changes are not complex, however there's a series of them. I'd recommend going patch-by-patch.

The motiviation behind all the changes, the feature itself is trivial, once all the preparation is there.

@kira-syslogng
Copy link
Contributor

Build FAILURE

@mitzkia
Copy link
Collaborator

mitzkia commented Feb 18, 2021

@kira-syslogng test this please;

@kira-syslogng
Copy link
Contributor

Build FAILURE

@gaborznagy gaborznagy added this to the syslog-ng-3.31 milestone Feb 25, 2021
@gaborznagy gaborznagy self-requested a review February 25, 2021 09:21
@ryanfaircloth
Copy link
Contributor

looking forward to having this in the toolbox

Copy link
Collaborator

@gaborznagy gaborznagy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only have some questions/comments.

The new feature looks good to me!

lib/msg-format.c Show resolved Hide resolved
lib/msg-format.c Show resolved Hide resolved
lib/msg-format.c Outdated Show resolved Hide resolved
lib/msg-format.c Show resolved Hide resolved
lib/msg-format.c Show resolved Hide resolved
modules/syslogformat/syslog-parser.c Show resolved Hide resolved
@kira-syslogng
Copy link
Contributor

Build FAILURE

@bazsi bazsi force-pushed the syslog-parser-drop-invalid branch 2 times, most recently from e63a217 to bd9d626 Compare February 27, 2021 18:31
@kira-syslogng
Copy link
Contributor

Build FAILURE

@bazsi
Copy link
Collaborator Author

bazsi commented Feb 27, 2021

@kira-syslogng retest this please;

@kira-syslogng
Copy link
Contributor

Build FAILURE

1 similar comment
@kira-syslogng
Copy link
Contributor

Build FAILURE

@gaborznagy
Copy link
Collaborator

@kira-syslogng retest this please branch=syslog-parser-update-wrong-format-test;

@kira-syslogng
Copy link
Contributor

Build SUCCESS

@gaborznagy
Copy link
Collaborator

I haven't checked why does the test_secure_logging UT fail only with cmake.
I've updated a test that was affected by the injected error message format, so kira is green now.

@bazsi bazsi force-pushed the syslog-parser-drop-invalid branch from 1747ca3 to 8e72c8a Compare March 4, 2021 13:39
@kira-syslogng
Copy link
Contributor

Build FAILURE

@gaborznagy
Copy link
Collaborator

@kira-syslogng retest this please branch=syslog-parser-update-wrong-format-test;

@kira-syslogng
Copy link
Contributor

Build FAILURE

bazsi added 11 commits March 11, 2021 14:51
Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
This makes it easier for me the read the argument list, "LogMessage *msg"
becomes the first one right after the "self"-style argument, while the input
and the error indication come last.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
msg_format_parse() might be called outside of the normal parsing
(ie. syslog-parser), so move the message to log_msg_new() where it
indeed happens.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
This function would return if it was successful instead of simply handling
the error itself.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
msg_format_inject_parse_error() uses the string before that error position,
which can read from buf[-1] in case position is zero, potentially
leaking the byte in front of the data buffer, or if that address
is unmapped, can also cause a SIGSEGV.

In reality, that byte is part of the heap allocation header, so it shouldn't
be unmapped, so SIGSEGV is not very probable, at least on common platforms.

This patch also changes to using a dynamic buffer instead of a
statically sized one, avoiding the truncation of the message at 2048 bytes.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Previously we retained flags like "mark" or "internal" which means that
these values leak through a log_msg_clear(), but if we consider
log_msg_clear() to be a function that gives us an empty slate, we should
get rid of those flags too. Same as if we created a new LogMessage instance.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
The condition to use "kernel" as program name depended on
LogMessage->flags set prior to the message being parsed.

Since the location where these flags were set is pretty far from
the syslog message parser code, this dependency was not easy to
recognize and the previous set of refactoring steps even broke the
assumption.

I've decided to make the dependency clearer while retaining the workaround
where it is today:
  * LF_INTERNAL check is removed, the syslog parser is never invoked on
    internal() logs (historically it probably was...)
  * the check on LF_LOCAL was converted to a parse_options->flags check
    on LP_LOCAL. Parse_options clearly affects how the message is parsed and is
    used in a number of different locations within the same function.
  * the check on <pri> value is retained, as parsing the pri value is logically
    in the same location.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
…t test

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
Kokan
Kokan previously approved these changes Mar 11, 2021
MsgFormatOptions template_parse_options = {0};
msg_format_options_defaults(&template_parse_options);
syslog_format_handler(&template_parse_options, (const guchar *)raw_msg, strlen(raw_msg), msg);
LogMessage *msg = log_msg_new(raw_msg, strlen(raw_msg), &parse_options);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this UT not using syslogformat in the first place. But this was not introduced by this PR, so I am okay with the follow up.

@Kokan
Copy link
Collaborator

Kokan commented Mar 11, 2021

Also if possible please write a news entry under news/ directory. But this does not block the merge, if somebody merges this as is, they can write one and open a PR.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
@bazsi
Copy link
Collaborator Author

bazsi commented Mar 12, 2021

added news file + rebased

@kira-syslogng
Copy link
Contributor

Build FAILURE

@bazsi
Copy link
Collaborator Author

bazsi commented Mar 12, 2021

@kira-syslogng retest this please branch=syslog-parser-update-wrong-format-test;

@kira-syslogng
Copy link
Contributor

Build SUCCESS

@gaborznagy gaborznagy added the user-visible-feature User visible feature label Mar 19, 2021
@gaborznagy gaborznagy merged commit de171ed into syslog-ng:master Mar 19, 2021
@gaborznagy
Copy link
Collaborator

I've created an internal doc update ticket about the new option and the changed behaviour of msg-format flags (i.e. from now on if "no-parse" flag is used additional flags e.g. "no-multi-line" can be used as well.)

@gaborznagy
Copy link
Collaborator

@bazsi I wonder: did we intentionally not document the injected error message?
This is the only thing in the documentation:

If syslog-ng OSE cannot parse a message, it results in an error

I would mention the details of the error message (rewritten program and message fields, modified facility and severity values).

@bazsi
Copy link
Collaborator Author

bazsi commented Mar 19, 2021 via email

@gaborznagy
Copy link
Collaborator

Thanks, I've submitted a ticket to the doc team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user-visible-feature User visible feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants