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
statedb: Fix termination of string and IP keys #29368
Merged
joamaki
merged 2 commits into
cilium:main
from
joamaki:pr/joamaki/statedb-fix-key-termination
Nov 28, 2023
Merged
statedb: Fix termination of string and IP keys #29368
joamaki
merged 2 commits into
cilium:main
from
joamaki:pr/joamaki/statedb-fix-key-termination
Nov 28, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maintainer-s-little-helper
bot
added
the
dont-merge/needs-release-note-label
The author needs to describe the release impact of these changes.
label
Nov 24, 2023
joamaki
added
kind/bug
This is a bug in the Cilium logic.
release-note/bug
This PR fixes an issue in a previous release of Cilium.
and removed
kind/bug
This is a bug in the Cilium logic.
labels
Nov 24, 2023
maintainer-s-little-helper
bot
removed
the
dont-merge/needs-release-note-label
The author needs to describe the release impact of these changes.
label
Nov 24, 2023
bimmlerd
approved these changes
Nov 27, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this fixes the issue 🚀
When I briefly looked into fixing this, all I could come up with was the expensive check during iteration - happy to see you found another way!
joamaki
force-pushed
the
pr/joamaki/statedb-fix-key-termination
branch
3 times, most recently
from
November 27, 2023 13:59
fc573ee
to
d088c33
Compare
… key) Signed-off-by: Jussi Maki <jussi@isovalent.com>
Due to missing zero byte termination in the keys returned by index.String and index.Stringer the Get() method returned wrong results, e.g. Get("foo") would've returned both "foo" and "foobar" for a unique index. Fix by adding the termination. Similar issue also existed for index.NetIP and index.NetIPAddr since they returned variable-sized keys (e.g. 4 bytes for IPv4 and 16 bytes for IPv6). Fix this by changing them to fixed-size keys. While at it, make the construction of keys a bit more explicit by making index.Key a newtype over []byte and fix usages. Add documentation about the need for termination for variable sized keys to index.Key. I did think about alternative ways to fix this. We can't just add "\x00" to keys since that may be a valid byte (e.g. in net.IP) and thus would not serve as termination. A more complex marker or adding e.g. length to beginning or end has the same issues. The only working alternative I can think of is to have a special iterator for Get() which does full key comparison, but that would be quite expensive for large keys, thus I opted for making the index functions carry the responsibility of properly either terminating the key or using fixed sizes. Fixes: cilium#29324 Fixes: 23b0492 ("statedb2: StateDB v2.0 with per-table locks and deletion tracking") Signed-off-by: Jussi Maki <jussi@isovalent.com>
joamaki
force-pushed
the
pr/joamaki/statedb-fix-key-termination
branch
from
November 27, 2023 15:06
d088c33
to
ca6924c
Compare
/test |
aspsk
approved these changes
Nov 28, 2023
dylandreimerink
approved these changes
Nov 28, 2023
maintainer-s-little-helper
bot
added
the
ready-to-merge
This PR has passed all tests and received consensus from code owners to merge.
label
Nov 28, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
ready-to-merge
This PR has passed all tests and received consensus from code owners to merge.
release-note/bug
This PR fixes an issue in a previous release of Cilium.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Due to missing zero byte termination in the keys returned by index.String and
index.Stringer the Get() method returned wrong results, e.g. Get("foo") would've returned both "foo" and "foobar" for a unique index. Fix by adding the termination.
Similar issue also existed for index.NetIP and index.NetIPAddr since they returned variable-sized keys (e.g. 4 bytes for IPv4 and 16 bytes for IPv6). Fix this by changing them to fixed-size keys.
While at it, make the construction of keys a bit more explicit by making index.Key a newtype over []byte and fix usages. Add documentation about the need for termination for variable sized keys to index.Key.
I did think about alternative ways to fix this. We can't just add "\x00" to keys since that may be a valid byte (e.g. in net.IP) and thus would not serve as termination. A more complex marker or adding e.g. length to beginning or end has the same issues (and breaks prefix searching). The only working alternative I can think of is to have a special iterator for Get() which does full key comparison, but that would be quite expensive for large keys, thus I opted for making the index functions carry the responsibility of properly either terminating the key or using fixed sizes.