Skip to content

util/log: fix redactability of logging tags#72992

Merged
craig[bot] merged 3 commits intocockroachdb:masterfrom
knz:20211119-avoid-err-panic
Dec 21, 2021
Merged

util/log: fix redactability of logging tags#72992
craig[bot] merged 3 commits intocockroachdb:masterfrom
knz:20211119-avoid-err-panic

Conversation

@knz
Copy link
Copy Markdown
Contributor

@knz knz commented Nov 19, 2021

Fixes #72905.

Some time in the v21.2 cycle, the log entry preparation logic was
refactored and a mistake was introduced: the logging tags were not any
more subject to the redaction logic. The result was that redaction
markers were missing in log tag values, and if a value had contained
unbalanced redaction markers in a value string (say, as part of a SQL
table key), it would have caused log file corruption and possibly a
confidential data leak.

This patch fixes that, by preparing the logging tags in the same way
as the main message for each entry.

Release note (cli change): A bug affecting the redactability of
logging tags in output log entries has been fixed. This bug had
been introduced in the v21.2 release.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Nov 22, 2021

the two bugs:

  • redactable values in tags do not get redacted when redact: true is set
  • redaction markers are not removed when redactable: false is set

probably a 3rd bug as well: redaction markers inside sensitive values should be escaped. Need to check whether the test catches it.

@knz knz force-pushed the 20211119-avoid-err-panic branch from 273f5ae to e5ca0f3 Compare December 20, 2021 13:18
@knz knz changed the title util/log: new TestFormatRedaction to demonstrate two bugs util/log: fix redactability of logging tags Dec 20, 2021
@knz knz requested review from a team December 20, 2021 13:19
@knz knz force-pushed the 20211119-avoid-err-panic branch from e5ca0f3 to 6051cd5 Compare December 20, 2021 13:22
Copy link
Copy Markdown
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

Although this is my first time really digging into our logEntry/redaction code, I found the formattableTags API to be readable and straightforward - nice work.

It seems like a test or two is causing issues with the iterator, although I'm unsure if it's just the test or if we truly need to account for the case where bytes.IndexByte returns -1 🤔

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier and @knz)


pkg/util/log/formattable_tags.go, line 79 at r3 (raw file):

	}
	// Advance the cursor to the beginning of the first next value.
	nv := bytes.IndexByte(i.tags, 0)

should we code defensively against a case where bytes.IndexByte() returns -1?

Code quote:

nv := bytes.IndexByte(i.tags, 0)

pkg/util/log/formattable_tags.go, line 136 at r3 (raw file):

		if len(val) > 0 {
			if len(key) != 1 {
				w.SafeRune('=')

I'm curious, what does a key with a length of 1 represent/imply?

Code quote:

			if len(key) != 1 {
				w.SafeRune('=')
			}

@knz knz force-pushed the 20211119-avoid-err-panic branch from 6051cd5 to 608a9a9 Compare December 20, 2021 20:39
Copy link
Copy Markdown
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

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

It seems like a test or two is causing issues with the iterator,

it was just a linter error. Fixed.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier)


pkg/util/log/formattable_tags.go, line 79 at r3 (raw file):

Previously, abarganier (Alex Barganier) wrote…

should we code defensively against a case where bytes.IndexByte() returns -1?

No need - it's an invariant of the formattableTags that there's always a value for every key, possibly empty.


pkg/util/log/formattable_tags.go, line 136 at r3 (raw file):

Previously, abarganier (Alex Barganier) wrote…

I'm curious, what does a key with a length of 1 represent/imply?

We skip the = sign for 1-letter keys: we write "n1" in logs, not "n=1".

knz added 2 commits December 20, 2021 22:28
If `ApplyConfig` encountered certain kinds of errors,
specifically malformed arguments generated by erroneous unit tests,
it would fail with a go panic instead of a regular error.

This is because `return nil, err` would assign `nil` to the cleanup
callback just before it would be called in a defer.

This patch fixes it.

Release note: None
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Dec 20, 2021

oh the test in TestStoreRangeMergeWithData reveals there may be nul bytes in keys or values. Looking into it now.

Some time in the v21.2 cycle, the log entry preparation logic was
refactored and a mistake was introduced: the logging tags were not any
more subject to the redaction logic. The result was that redaction
markers were missing in log tag values, and if a value had contained
unbalanced redaction markers in a value string (say, as part of a SQL
table key), it would have caused log file corruption and possibly a
confidential data leak.

This patch fixes that, by preparing the logging tags in the same way
as the main message for each entry.

Additionally, this patch addresses a long-standing shortcoming:
it was previously possible to see raw ascii nul bytes in the tag
output. This is not possible any more.

Release note (cli change): A bug affecting the redactability of
logging tags in output log entries has been fixed. This bug had
been introduced in the v21.2 release.
@knz knz force-pushed the 20211119-avoid-err-panic branch from 608a9a9 to e57ace1 Compare December 20, 2021 21:47
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Dec 20, 2021

oh the test in TestStoreRangeMergeWithData reveals there may be nul bytes in keys or values. Looking into it now.

Fixed.

Copy link
Copy Markdown
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

:lgtm:, thank you for fixing!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @abarganier and @knz)


pkg/util/log/formattable_tags.go, line 79 at r3 (raw file):

Previously, knz (kena) wrote…

No need - it's an invariant of the formattableTags that there's always a value for every key, possibly empty.

Ack, with the addition of escapeNulBytes it seems like we're good here 👍


pkg/util/log/formattable_tags.go, line 136 at r3 (raw file):

Previously, knz (kena) wrote…

We skip the = sign for 1-letter keys: we write "n1" in logs, not "n=1".

Ack.

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Dec 21, 2021

TFYR!

bors r=abarganier

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Dec 21, 2021

blathers backport 21.2

@craig craig bot merged commit 779f08b into cockroachdb:master Dec 21, 2021
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Dec 21, 2021

Build succeeded:

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.

log: the 21.2 refactor removed auto-redaction for log tags

3 participants