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

fix: panic index out of range for invalid series keys [Port to 1.11] #24595

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

jdockerty
Copy link
Member

@jdockerty jdockerty commented Jan 23, 2024

This is a cherry-pick of 6af0be9 (introduced in #24565) for a port to 1.11.

Original pull request comment:

Panics can be avoided by checking the validity of the return from ReadSeriesKeyLen, at the moment the size check resulting from this function is entirely discarded. The contained call to binary.Uvarint contains an implicit error, depending on the returned value:

If an error occurred, the value is 0 and the number of bytes n is <= 0

In every occurrence of ReadSeriesKeyMeasurement, the ReadSeriesKeyLen function is called prior to it, determined by glancing over the output of rg "ReadSeriesKeyMeasurement" -B 5 across the full codebase.

This PR refactors the flow slightly to include ReadSeriesKeyLen returned value checks and the appropriate action to avoid panicking when trying to access memory out of bounds, it has a knock-on effect such that the ParseSeriesKey function can return nil at another point which indicates an invalid result and the appropriate action should be taken.

Closes #24593

  • I've read the contributing section of the project README.
  • Signed CLA (if not already signed).

* chore: add scaffolding for naive solution

* feat: test case scaffolding

* fix: implement check for series key before proceeding

* fix: add validation for ReadSeriesKeyMeasurement usage

* refactor: explicit use of series key len

* feat: add remaining check to index

* feat: add check to remaining files

As the Len function is used as part of the parseSeriesKey, this also needs to be accounted for on the nil return from this function as it is used in different contexts

* feat: expand test cases

* chore: go fmt

* chore: update test failure message

* chore: impl feedback on unnecessary sz checks

* feat: expand test cases

* fix: nil series key check

In both sections for index.go there is a pre-existing length check against the series key which should catch invalid values, perhaps this explains why it hasn't cropped up in the reported panics. For even more safety, we can also skip a nil key because we know that subsequent calls will cause a panic where this key is attempted to be used

* fix: remove nil tags check

A key with no tags is valid, so we should not check for BOTH nil key and tags as a key could be nil, which is invalid, yet still have tags and therefore cause the check to pass which we do not want

* feat: extend test cases from feedback

* fix: extend checks for CompareSeriesKeys

* feat: add nilKeyHandler for shared key checking logic

* fix: logical error in nilKeyHandler

Prior to this, the else was always defaulted to at the end of the conditional branch, which causes unexpected behaviour and a failure of a bunch of tests.

* fix: return tags keep nil data

In a recent change to this, we agreed on a simple name == nil check for the actual data. As a follow on to this, I just realised that we don't actually want to nil back the tags, even if they're not checked, because having no tags is a valid input so we can simply return whatever we were passed unchanged.

* fix: use len == 0 for extra safety

* feat: extra test for blank series key
@jdockerty jdockerty self-assigned this Jan 23, 2024
Copy link
Contributor

@davidby-influx davidby-influx left a comment

Choose a reason for hiding this comment

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

LGTM

@jdockerty jdockerty merged commit 8b9a9c6 into 1.11 Jan 23, 2024
13 checks passed
@jdockerty jdockerty deleted the fix/panic-invalid-series-key-1-11 branch January 23, 2024 17:08
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.

2 participants