Line data Source code
1 :
2 : #include "../fd_txn_m.h"
3 : #include <linux/futex.h>
4 : #include "generated/fd_dedup_tile_seccomp.h"
5 :
6 : #include "../topo/fd_topo.h"
7 : #include "../metrics/fd_metrics.h"
8 :
9 : #include "../../discof/replay/fd_replay_tile.h"
10 :
11 : /* fd_dedup provides services to deduplicate multiple streams of input
12 : fragments and present them to a mix of reliable and unreliable
13 : consumers as though they were generated by a single multi-stream
14 : producer. */
15 :
16 0 : #define IN_KIND_VERIFY (1UL)
17 0 : #define IN_KIND_REPLAY (3UL)
18 :
19 : /* fd_dedup_in_ctx_t is a context object for each in (producer) mcache
20 : connected to the dedup tile. */
21 :
22 : typedef struct {
23 : fd_wksp_t * mem;
24 : ulong chunk0;
25 : ulong wmark;
26 : ulong mtu;
27 : } fd_dedup_in_ctx_t;
28 :
29 : /* fd_dedup_ctx_t is the context object provided to callbacks from
30 : fd_stem, and contains all state needed to progress the tile. */
31 :
32 : typedef struct {
33 : ulong tcache_depth; /* == fd_tcache_depth( tcache ), depth of this dedups's tcache (const) */
34 : ulong tcache_map_cnt; /* == fd_tcache_map_cnt( tcache ), number of slots to use for tcache map (const) */
35 : ulong * tcache_sync; /* == fd_tcache_oldest_laddr( tcache ), local join to the oldest key in the tcache */
36 : ulong * tcache_ring;
37 : ulong * tcache_map;
38 :
39 : ulong in_kind[ 64UL ];
40 : fd_dedup_in_ctx_t in[ 64UL ];
41 :
42 : /* Dedup tag hashed early in during_frag, valid iff tag_ready */
43 : int tag_ready;
44 : ulong tag;
45 :
46 : int bundle_failed;
47 : ulong bundle_id;
48 : ulong bundle_idx;
49 : uchar bundle_signatures[ 4UL ][ 64UL ];
50 :
51 : fd_wksp_t * out_mem;
52 : ulong out_chunk0;
53 : ulong out_wmark;
54 : ulong out_chunk;
55 :
56 : ulong hashmap_seed;
57 :
58 : struct {
59 : ulong dedup_tile_result[ FD_METRICS_ENUM_DEDUP_RESULT_CNT ];
60 : } metrics;
61 : } fd_dedup_ctx_t;
62 :
63 : FD_FN_CONST static inline ulong
64 0 : scratch_align( void ) {
65 0 : return alignof( fd_dedup_ctx_t );
66 0 : }
67 :
68 : FD_FN_PURE static inline ulong
69 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
70 0 : ulong l = FD_LAYOUT_INIT;
71 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_dedup_ctx_t ), sizeof( fd_dedup_ctx_t ) );
72 0 : l = FD_LAYOUT_APPEND( l, fd_tcache_align(), fd_tcache_footprint( tile->dedup.tcache_depth, 0UL ) );
73 0 : return FD_LAYOUT_FINI( l, scratch_align() );
74 0 : }
75 :
76 : static inline void
77 0 : metrics_write( fd_dedup_ctx_t * ctx ) {
78 0 : FD_MCNT_ENUM_COPY( DEDUP, TXN_PROCESSED, ctx->metrics.dedup_tile_result );
79 0 : }
80 :
81 : /* during_frag is called between pairs for sequence number checks, as
82 : we are reading incoming frags. We don't actually need to copy the
83 : fragment here, flow control prevents it getting overrun, and
84 : downstream consumers could reuse the same chunk and workspace to
85 : improve performance.
86 :
87 : The bounds checking and copying here are defensive measures,
88 :
89 : * In a functioning system, the bounds checking should never fail,
90 : but we want to prevent an attacker with code execution on a producer
91 : tile from trivially being able to jump to a consumer tile with
92 : out of bounds chunks.
93 :
94 : * For security reasons, we have chosen to isolate all workspaces from
95 : one another, so for example, if the QUIC tile is compromised with
96 : RCE, it cannot wait until the sigverify tile has verified a transaction,
97 : and then overwrite the transaction while it's being processed by the
98 : banking stage. */
99 :
100 : static inline void
101 : during_frag( fd_dedup_ctx_t * ctx,
102 : ulong in_idx,
103 : ulong seq FD_PARAM_UNUSED,
104 : ulong sig,
105 : ulong chunk,
106 : ulong sz,
107 0 : ulong ctl FD_PARAM_UNUSED ) {
108 :
109 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
110 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
111 :
112 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
113 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
114 :
115 0 : ctx->tag_ready = 0;
116 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) ) {
117 0 : if( FD_LIKELY( sig==REPLAY_SIG_TXN_EXECUTED ) ) {
118 0 : fd_replay_txn_executed_t * txn_executed = fd_type_pun( src );
119 0 : if( FD_UNLIKELY( !txn_executed->is_committable ) ) return;
120 0 : ulong ha_dedup_tag = fd_hash( ctx->hashmap_seed, fd_txn_get_signatures( TXN(txn_executed->txn), txn_executed->txn->payload ), FD_TXN_SIGNATURE_SZ );
121 0 : int _is_dup;
122 0 : FD_TCACHE_INSERT( _is_dup, *ctx->tcache_sync, ctx->tcache_ring, ctx->tcache_depth, ctx->tcache_map, ctx->tcache_map_cnt, ha_dedup_tag );
123 0 : (void)_is_dup;
124 0 : }
125 0 : } else {
126 : /* Hash the signature and warm its tcache map slot before copying,
127 : so the map miss overlaps the copy misses. */
128 0 : for( ulong off=0UL; off<sz; off+=64UL ) __builtin_prefetch( src+off, 0, 3 );
129 0 : fd_txn_m_t const * txnm = (fd_txn_m_t const *)src;
130 0 : if( FD_LIKELY( sz>=sizeof(fd_txn_m_t) && txnm->payload_sz<=FD_TPU_MTU && txnm->txn_t_sz ) ) {
131 0 : fd_txn_t const * txn = fd_txn_m_txn_t_const( txnm );
132 0 : if( FD_LIKELY( (ulong)((uchar const *)txn-src)+sizeof(fd_txn_t)<=sz &&
133 0 : (ulong)txn->signature_off+FD_TXN_SIGNATURE_SZ<=txnm->payload_sz ) ) {
134 0 : ctx->tag = fd_hash( ctx->hashmap_seed, fd_txn_m_payload_const( txnm )+txn->signature_off, FD_TXN_SIGNATURE_SZ );
135 0 : ctx->tag_ready = 1;
136 0 : __builtin_prefetch( ctx->tcache_map + fd_tcache_map_start( ctx->tag, ctx->tcache_map_cnt ), 1, 3 );
137 0 : }
138 0 : }
139 0 : fd_memcpy( dst, src, sz );
140 0 : }
141 0 : }
142 :
143 : /* After the transaction has been fully received, and we know we were
144 : not overrun while reading it, check if it's a duplicate of a prior
145 : transaction. */
146 :
147 : static inline void
148 : after_frag( fd_dedup_ctx_t * ctx,
149 : ulong in_idx,
150 : ulong seq,
151 : ulong sig,
152 : ulong sz,
153 : ulong tsorig,
154 : ulong _tspub,
155 0 : fd_stem_context_t * stem ) {
156 0 : (void)seq;
157 0 : (void)sig;
158 0 : (void)sz;
159 0 : (void)_tspub;
160 :
161 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) ) return;
162 :
163 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
164 0 : if( FD_UNLIKELY( txnm->payload_sz>FD_TPU_MTU ) ) {
165 0 : FD_LOG_ERR(( "dedup: txn payload size %hu exceeds max %lu", txnm->payload_sz, FD_TPU_MTU ));
166 0 : }
167 0 : fd_txn_t * txn = fd_txn_m_txn_t( txnm );
168 :
169 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id && (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
170 0 : ctx->bundle_failed = 0;
171 0 : ctx->bundle_id = txnm->block_engine.bundle_id;
172 0 : ctx->bundle_idx = 0UL;
173 0 : }
174 :
175 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id && ctx->bundle_failed ) ) {
176 0 : ctx->metrics.dedup_tile_result[ FD_METRICS_ENUM_DEDUP_RESULT_V_BUNDLE_PEER_FAILED_IDX ]++;
177 0 : return;
178 0 : }
179 :
180 0 : int is_dup = 0;
181 0 : if( FD_LIKELY( !txnm->block_engine.bundle_id ) ) {
182 : /* Compute fd_hash(signature) for dedup. */
183 0 : ulong ha_dedup_tag = ctx->tag_ready ? ctx->tag : fd_hash( ctx->hashmap_seed, fd_txn_m_payload( txnm )+txn->signature_off, 64UL );
184 :
185 0 : FD_TCACHE_INSERT( is_dup, *ctx->tcache_sync, ctx->tcache_ring, ctx->tcache_depth, ctx->tcache_map, ctx->tcache_map_cnt, ha_dedup_tag );
186 :
187 : /* The next insert evicts ring[oldest], warm the map slot it probes */
188 0 : ulong next_evict = ctx->tcache_ring[ *ctx->tcache_sync ];
189 0 : __builtin_prefetch( ctx->tcache_map + fd_tcache_map_start( next_evict, ctx->tcache_map_cnt ), 1, 3 );
190 0 : } else {
191 : /* Make sure bundles don't contain a duplicate transaction inside
192 : the bundle, which would not be valid. */
193 :
194 0 : if( FD_UNLIKELY( ctx->bundle_idx>4UL ) ) FD_LOG_ERR(( "bundle_idx %lu > 4", ctx->bundle_idx ));
195 :
196 0 : for( ulong i=0UL; i<ctx->bundle_idx; i++ ) {
197 0 : if( !memcmp( ctx->bundle_signatures[ i ], fd_txn_m_payload( txnm )+txn->signature_off, 64UL ) ) {
198 0 : is_dup = 1;
199 0 : break;
200 0 : }
201 0 : }
202 :
203 0 : if( FD_UNLIKELY( ctx->bundle_idx==4UL ) ) ctx->bundle_idx++;
204 0 : else fd_memcpy( ctx->bundle_signatures[ ctx->bundle_idx++ ], fd_txn_m_payload( txnm )+txn->signature_off, 64UL );
205 0 : }
206 :
207 0 : if( FD_LIKELY( is_dup ) ) {
208 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
209 :
210 0 : ctx->metrics.dedup_tile_result[ FD_METRICS_ENUM_DEDUP_RESULT_V_DUPLICATE_IDX ]++;
211 0 : } else {
212 0 : ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 0 );
213 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
214 : /* Mark bundle transactions with sig==1 so that downstream resolv
215 : tiles can route all bundle traffic to resolv:0. */
216 0 : ulong out_sig = !!txnm->block_engine.bundle_id;
217 0 : fd_stem_publish( stem, 0UL, out_sig, ctx->out_chunk, realized_sz, 0UL, tsorig, tspub );
218 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, realized_sz, ctx->out_chunk0, ctx->out_wmark );
219 :
220 0 : ctx->metrics.dedup_tile_result[ FD_METRICS_ENUM_DEDUP_RESULT_V_SUCCESS_IDX ]++;
221 0 : }
222 0 : }
223 :
224 : static void
225 : privileged_init( fd_topo_t const * topo,
226 0 : fd_topo_tile_t const * tile ) {
227 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
228 :
229 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
230 0 : fd_dedup_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_dedup_ctx_t ), sizeof( fd_dedup_ctx_t ) );
231 0 : FD_TEST( fd_rng_secure( &ctx->hashmap_seed, 8U ) );
232 0 : }
233 :
234 : static void
235 : unprivileged_init( fd_topo_t const * topo,
236 0 : fd_topo_tile_t const * tile ) {
237 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
238 :
239 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
240 0 : fd_dedup_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_dedup_ctx_t ), sizeof( fd_dedup_ctx_t ) );
241 0 : fd_tcache_t * tcache = fd_tcache_join( fd_tcache_new( FD_SCRATCH_ALLOC_APPEND( l, fd_tcache_align(), fd_tcache_footprint( tile->dedup.tcache_depth, 0) ), tile->dedup.tcache_depth, 0 ) );
242 0 : if( FD_UNLIKELY( !tcache ) ) FD_LOG_ERR(( "fd_tcache_new failed" ));
243 :
244 0 : ctx->tag_ready = 0;
245 0 : ctx->bundle_failed = 0;
246 0 : ctx->bundle_id = 0UL;
247 0 : ctx->bundle_idx = 0UL;
248 :
249 0 : memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
250 :
251 0 : ctx->tcache_depth = fd_tcache_depth ( tcache );
252 0 : ctx->tcache_map_cnt = fd_tcache_map_cnt ( tcache );
253 0 : ctx->tcache_sync = fd_tcache_oldest_laddr( tcache );
254 0 : ctx->tcache_ring = fd_tcache_ring_laddr ( tcache );
255 0 : ctx->tcache_map = fd_tcache_map_laddr ( tcache );
256 :
257 0 : FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
258 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
259 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
260 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
261 :
262 0 : ctx->in[i].mem = link_wksp->wksp;
263 0 : ctx->in[i].mtu = link->mtu;
264 0 : ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
265 0 : ctx->in[i].wmark = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
266 :
267 0 : if( !strcmp( link->name, "verify_dedup" ) ) {
268 0 : ctx->in_kind[ i ] = IN_KIND_VERIFY;
269 0 : } else if( !strcmp( link->name, "replay_out" ) ) {
270 0 : ctx->in_kind[ i ] = IN_KIND_REPLAY;
271 0 : } else {
272 0 : FD_LOG_ERR(( "unexpected link name %s", link->name ));
273 0 : }
274 0 : }
275 :
276 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
277 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
278 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
279 0 : ctx->out_chunk = ctx->out_chunk0;
280 :
281 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
282 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
283 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
284 0 : }
285 :
286 : static ulong
287 : populate_allowed_seccomp( fd_topo_t const * topo,
288 : fd_topo_tile_t const * tile,
289 : ulong out_cnt,
290 0 : struct sock_filter * out ) {
291 0 : (void)topo;
292 0 : (void)tile;
293 :
294 0 : populate_sock_filter_policy_fd_dedup_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
295 0 : return sock_filter_policy_fd_dedup_tile_instr_cnt;
296 0 : }
297 :
298 : static ulong
299 : populate_allowed_fds( fd_topo_t const * topo,
300 : fd_topo_tile_t const * tile,
301 : ulong out_fds_cnt,
302 0 : int * out_fds ) {
303 0 : (void)topo;
304 0 : (void)tile;
305 :
306 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
307 :
308 0 : ulong out_cnt = 0UL;
309 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
310 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
311 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
312 0 : return out_cnt;
313 0 : }
314 :
315 0 : #define STEM_BURST (1UL)
316 :
317 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_dedup_ctx_t
318 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_dedup_ctx_t)
319 :
320 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
321 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
322 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
323 :
324 : #include "../stem/fd_stem.c"
325 :
326 : fd_topo_run_tile_t fd_tile_dedup = {
327 : .name = "dedup",
328 : .populate_allowed_seccomp = populate_allowed_seccomp,
329 : .populate_allowed_fds = populate_allowed_fds,
330 : .scratch_align = scratch_align,
331 : .scratch_footprint = scratch_footprint,
332 : .privileged_init = privileged_init,
333 : .unprivileged_init = unprivileged_init,
334 : .run = stem_run,
335 : };
|