Skip to content

metadata: avoid change metadata ref #566

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

Merged
merged 5 commits into from
Mar 28, 2022
Merged

Conversation

BusyJay
Copy link
Member

@BusyJay BusyJay commented Mar 26, 2022

After metadata is received, every entry should be owned by the grpc c
core, application should clone the byte slice if necessary.

In the past, we get around the problem by increasing the reference
count of the slice. However, it's unsafe to do so as the slice is not
guaranteed to be accessible in the first place.

This PR fixes the problem by introducing an unowned metadata type. It
works just like Metadata, but accessing its content requires manual
check for the lifetime of associated call.

I tried to add test case to cover the unsafe access, however it's too racy
and nearly impossible to introduce a case without touching the C core.

BusyJay added 2 commits March 2, 2022 18:51
After metadata is received, every entry should be owned by the grpc c
core, application should clone the byte slice if necessary.

In the past, we get around the problem by increasing the reference
count of the slice. However, it's unsafe to do so as the slice is not
guaranteed to be accessible in the first place.

This PR fixes the problem by introducing an unowned metadata type. It
works just like Metadata, but accessing its content requires manual
check for the lifetime of associated call.

Signed-off-by: Jay Lee <[email protected]>
@BusyJay BusyJay requested a review from hunterlxt March 26, 2022 15:05
Signed-off-by: Jay Lee <[email protected]>
@BusyJay
Copy link
Member Author

BusyJay commented Mar 28, 2022

Long time tests show that tikv/tikv#12202 and tikv/tikv#12198 are resolved by this patch.

@hunterlxt
Copy link
Member

hunterlxt commented Mar 28, 2022

However, it's unsafe to do so as the slice is not
guaranteed to be accessible in the first place.

can you explain that?

unsafe { mem::transmute(Metadata::with_capacity(0)) }
}
#[inline]
pub unsafe fn assume_valid(&self) -> &Metadata {
Copy link
Member

Choose a reason for hiding this comment

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

maybe convert_to_owned better

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not a convertion. It's more like a as_ref/deref. The returned value is not owned, it just reuses the struct for accessing APIs. That's why a reference is returned.

impl Drop for UnownedMetadata {
#[inline]
fn drop(&mut self) {
unsafe { grpcio_sys::grpcwrap_metadata_array_destroy_metadata_only(&mut self.0) }
Copy link
Member

Choose a reason for hiding this comment

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

why free here because I don't see any pointer init

Copy link
Member Author

Choose a reason for hiding this comment

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

@BusyJay
Copy link
Member Author

BusyJay commented Mar 28, 2022

can you explain that?

The order of handling batch result and destroying a call is uncertain. So it's possible that when the batch result is returned from the queue_next, the corresponding grpc call is already destroyed. In that case, referencing internal slices is UAF, and destroy the metadata later will dereference it, which is a double free.

Signed-off-by: Jay Lee <[email protected]>
@@ -147,7 +147,7 @@ impl MetadataBuilder {
///
/// Metadata value can be ascii string or bytes. They are distinguish by the
/// key suffix, key of bytes value should have suffix '-bin'.
#[repr(C)]
#[repr(transparent)]
Copy link
Member

Choose a reason for hiding this comment

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

seem that all struct with single field using repr(C) can be replaced with repr(transparent).

Signed-off-by: Jay Lee <[email protected]>
Copy link
Member

@hunterlxt hunterlxt left a comment

Choose a reason for hiding this comment

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

LGTM

@BusyJay BusyJay merged commit 7767d95 into tikv:master Mar 28, 2022
@BusyJay BusyJay deleted the fix-illegal-access branch March 28, 2022 07:34
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