-
Notifications
You must be signed in to change notification settings - Fork 27
Interface for test infrastructure to inspect streams #1202
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
akoshelev
merged 4 commits into
private-attribution:main
from
akoshelev:1195-maliciously-friendly-test-infra
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| use std::borrow::Cow; | ||
|
|
||
| use crate::{ | ||
| helpers::{HelperIdentity, Role, RoleAssignment}, | ||
| protocol::Gate, | ||
| sharding::ShardIndex, | ||
| sync::Arc, | ||
| }; | ||
|
|
||
| pub type DynStreamInterceptor = Arc<dyn StreamInterceptor<Context = InspectContext>>; | ||
|
|
||
| /// The interface for stream interceptors. | ||
| /// | ||
| /// It is used in test infrastructure to inspect | ||
| /// incoming streams and perform actions based on | ||
| /// their contents. | ||
| /// | ||
| /// The `peek` method takes a context object and a mutable reference | ||
| /// to the data buffer. It is responsible for inspecting the data | ||
| /// and performing any necessary actions based on the context. | ||
| pub trait StreamInterceptor: Send + Sync { | ||
| /// The context type for the stream peeker. | ||
| /// See [`InspectContext`] and [`MaliciousHelperContext`] for | ||
| /// details. | ||
| type Context; | ||
|
|
||
| /// Inspects the stream data and performs any necessary actions. | ||
| /// The `data` buffer may be modified in-place. | ||
| /// | ||
| /// ## Implementation considerations | ||
| /// This method is free to mutate the `data` buffer | ||
| /// however it wants, but it needs to account for the following: | ||
| /// | ||
| /// ### Prime field streams | ||
| /// Corrupting streams that send data as sequences of serialized | ||
| /// [`PrimeField`] may cause `GreaterThanPrimeError` errors at | ||
| /// the serialization layer, instead of maybe intended malicious | ||
| /// validation failures. | ||
| /// | ||
| /// ### Boolean fields | ||
| /// Flipping bits in fixed-size bit strings is indistinguishable | ||
| /// from additive attacks without additional measures implemented | ||
| /// at the transport layer, like checksumming, share consistency | ||
| /// checks, etc. | ||
| fn peek(&self, ctx: &Self::Context, data: &mut Vec<u8>); | ||
| } | ||
|
|
||
| impl<F: Fn(&InspectContext, &mut Vec<u8>) + Send + Sync + 'static> StreamInterceptor for F { | ||
| type Context = InspectContext; | ||
|
|
||
| fn peek(&self, ctx: &Self::Context, data: &mut Vec<u8>) { | ||
| (self)(ctx, data); | ||
| } | ||
| } | ||
|
|
||
| /// The general context provided to stream inspectors. | ||
| #[derive(Debug)] | ||
| pub struct InspectContext { | ||
| /// The shard index of this instance. | ||
| /// This is `None` for non-sharded helpers. | ||
| pub shard_index: Option<ShardIndex>, | ||
| /// The MPC identity of this instance. | ||
| /// The combination (`shard_index`, `identity`) | ||
| /// uniquely identifies a single shard within | ||
| /// a multi-sharded MPC system. | ||
| pub identity: HelperIdentity, | ||
| /// Helper that will receive this stream. | ||
| pub dest: Cow<'static, str>, | ||
| /// Circuit gate this stream is tied to. | ||
| pub gate: Gate, | ||
| } | ||
|
|
||
| /// The no-op stream peeker, which does nothing. | ||
| /// This is used as a default value for stream | ||
| /// peekers that don't do anything. | ||
| #[inline] | ||
| #[must_use] | ||
| pub fn passthrough() -> Arc<dyn StreamInterceptor<Context = InspectContext>> { | ||
| Arc::new(|_ctx: &InspectContext, _data: &mut Vec<u8>| {}) | ||
| } | ||
|
|
||
| /// This narrows the implementation of stream seeker | ||
| /// to a specific helper role. Only streams sent from | ||
| /// that helper will be inspected by the provided closure. | ||
| /// Other helper's streams will be left untouched. | ||
| /// | ||
| /// It does not support sharded environments and will panic | ||
| /// if used in a sharded test infrastructure. | ||
| #[derive(Debug)] | ||
| pub struct MaliciousHelper<F> { | ||
| identity: HelperIdentity, | ||
| role_assignment: RoleAssignment, | ||
| inner: F, | ||
| } | ||
|
|
||
| impl<F: Fn(&MaliciousHelperContext, &mut Vec<u8>) + Send + Sync> MaliciousHelper<F> { | ||
| pub fn new(role: Role, role_assignment: &RoleAssignment, peeker: F) -> Arc<Self> { | ||
| Arc::new(Self { | ||
| identity: role_assignment.identity(role), | ||
| role_assignment: role_assignment.clone(), | ||
| inner: peeker, | ||
| }) | ||
| } | ||
|
|
||
| fn context(&self, ctx: &InspectContext) -> MaliciousHelperContext { | ||
| let dest = HelperIdentity::try_from(ctx.dest.as_ref()) | ||
| .unwrap_or_else(|e| panic!("Can't resolve helper identity for {}: {e}", ctx.dest)); | ||
| let dest = self.role_assignment.role(dest); | ||
|
|
||
| MaliciousHelperContext { | ||
| shard_index: ctx.shard_index, | ||
| dest, | ||
| gate: ctx.gate.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Special contexts for stream inspectors | ||
| /// created with [`MaliciousHelper`]. | ||
| /// It provides convenient access to the | ||
| /// destination role and assumes a single MPC | ||
| /// helper intercepting streams. | ||
| #[derive(Debug)] | ||
| pub struct MaliciousHelperContext { | ||
| /// The shard index of this instance. | ||
| /// This is `None` for non-sharded helpers. | ||
| pub shard_index: Option<ShardIndex>, | ||
| /// Helper that will receive this stream. | ||
| pub dest: Role, | ||
| /// Circuit gate this stream is tied to. | ||
| pub gate: Gate, | ||
| } | ||
|
|
||
| impl<F: Fn(&MaliciousHelperContext, &mut Vec<u8>) + Send + Sync> StreamInterceptor | ||
| for MaliciousHelper<F> | ||
| { | ||
| type Context = InspectContext; | ||
|
|
||
| fn peek(&self, ctx: &Self::Context, data: &mut Vec<u8>) { | ||
| if ctx.identity == self.identity { | ||
| (self.inner)(&self.context(ctx), data); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
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.
Is it important to have this associated type, vs. using
InspectContextdirectly? I also wonder if there is some existing type likeAddrin the transport layer that could be reused here.It also seems like using
Contextin these names is potentially confusing. MaybeInspectStateorStreamMetadata?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.
We could use
Addrhere, the tradeoff is people will be writingadd.gate.unwrap()and figure out a way to get rid of genericIonAddr. I feel that we will eventually want to inspect cross-shard traffic in addition to cross-helper, so generics may not work well here.I agree it is messier on the stream inspector implementation side, but makes the usage of it a bit simpler