Skip to content

config.c: enforce stricter parsing of config files#427

Merged
cgzones merged 1 commit intologrotate:masterfrom
felixwilhelm:stricter-parsing
Dec 20, 2021
Merged

config.c: enforce stricter parsing of config files#427
cgzones merged 1 commit intologrotate:masterfrom
felixwilhelm:stricter-parsing

Conversation

@felixwilhelm
Copy link
Contributor

A number of recent exploits have abused logrotate's lax parsing of config files as an easy way to turn privileged file write bugs into code execution:

These exploits depend on bugs in other parts of the OS (apport, kernel, ..) to write partially controlled files to the logrotate config directory. Targeting logrotate is highly useful for attackers, because it will ignore invalid parts of config files and continue parsing until it finds a valid configuration.
This behaviour makes exploitation of tricky bugs such as the linked coredump vulnerabilities much easier.

This PR enforces stricter parsing of config files, by turning some of the already existing parser warnings into hard errors. It does not have any effect on users using valid configuration files, but might be a breaking change for people relying on the current handling of invalid files. As current versions already print an error message when encountering such files, no widely used configurations should be affected.

Overall, I think this would be a nice defense-in-depth change to improve the security of many downstream logrotate users.

@kdudka
Copy link
Member

kdudka commented Oct 21, 2021

I have not yet looked at the web pages you refer to but we always need to balance. What about compatibility across logrotate versions? If a logrotate config file gets updated to use an option that is not yet supported by the installed version of logrotate, should it prevent logs from being rotated?

@felixwilhelm
Copy link
Contributor Author

The PR contains two changes:

  1. Abort when encountering unexpected special chars at the beginning of a line (instead of skipping the line)
  2. Abort after parsing an unsupported keyword.

1 should not have any negative compatibility side effects, but you are right that 2 breaks forward compatibility when a new keyword is introduced.

I personally would prefer not rotating logfiles to potentially incorrect rotation when an unsupported option is used, but I guess that depends on a lot of external factors.

How about changing config.c#L1113 to a hard error instead? This would still allow unknown keywords as long as they are followed by a space or = sign so valid config files of future versions should not be an issue. But it would still make it harder to trick the parser into parsing a random file as a valid config.

WDYT?

@kdudka
Copy link
Member

kdudka commented Oct 27, 2021

Sorry for the delay! Yes, it makes sense to me. Unknown configuration directive should trigger a warning only whereas lexically incorrect garbage should trigger an error and prevent the remainder of the configuration file from being taken into account.

Abort parsing of config files that contain invalid lines.
This makes it harder to abuse logrotate for privilege escalation
attacks where an attacker can partially control a privileged file write.
@felixwilhelm
Copy link
Contributor Author

Thanks Kamil.
I've update the PR to only fail on lexically incorrectly configs. Can you take another look?
(The build failure looks like a travis issue, not sure how I can trigger a retry)

@felixwilhelm
Copy link
Contributor Author

@kdudka friendly ping.

@kdudka
Copy link
Member

kdudka commented Nov 1, 2021

Sorry for the delay! I was on vacation last week. The proposed changes look fair to me. I would appreciate an additional review by @cgzones though. He is the original author of the keyword ... not properly separated diagnostic message (and the most active contributor of logrotate these days).

@kdudka kdudka requested a review from cgzones November 1, 2021 14:39
Copy link
Member

@cgzones cgzones left a comment

Choose a reason for hiding this comment

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

Please add test cases for the following configurations:

&DIR&/test1.log {
    newkeyword
}
&DIR&/test2.log {
    rotate 1
}

and check that test1.log has not been rotated, but test2.log.

&DIR&/test1.log {
    g@rbag€[]+#*
}
&DIR&/test2.log {
    rotate 1
}

and check that both test1.log and test2.log have not been rotated.


$RLR test-config.102 --force

if [ $? -eq 0 ]; then
Copy link
Member

Choose a reason for hiding this comment

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

Please also check that test.log has not been rotated.


$RLR test-config.103 --force

if [ $? -eq 0 ]; then
Copy link
Member

Choose a reason for hiding this comment

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

Please also check that test.log has not been rotated.

@kdudka
Copy link
Member

kdudka commented Nov 3, 2021

@cgzones My proposal was actually not to treat unknown configuration directives as errors that would prevent log files from being rotated. logrotate configuration files are often updated independently of logrotate itself and their compatibility with older logrotate releases is important in my opinion.

@cgzones
Copy link
Member

cgzones commented Dec 17, 2021

@kdudka since you approved #431, can I merge this one here and afterwards apply the test suite changes?

@kdudka
Copy link
Member

kdudka commented Dec 20, 2021

Definitely. Thank you for taking care of it!

@cgzones cgzones merged commit 124e4ca into logrotate:master Dec 20, 2021
@cgzones
Copy link
Member

cgzones commented Dec 20, 2021

Applied, thanks for the contribution.

@felixwilhelm
Copy link
Contributor Author

Thanks for pushing this through! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants