Line data Source code
1 : #define _GNU_SOURCE
2 : #include "../tiles.h"
3 :
4 : #include <linux/futex.h>
5 : #include "generated/fd_sign_tile_seccomp.h"
6 :
7 : #include "../keyguard/fd_keyguard.h"
8 : #include "../keyguard/fd_keyguard_bls.h"
9 : #include "../keyguard/fd_keyload.h"
10 : #include "../keyguard/fd_keyswitch.h"
11 : #include "../../discof/admin/fd_adminctl.h"
12 : #include "../../ballet/base58/fd_base58.h"
13 : #include "../metrics/fd_metrics.h"
14 : #include "../../ballet/bls/fd_bls.h"
15 :
16 : #include "../../util/hist/fd_histf.h"
17 :
18 : #include <errno.h>
19 : #include <sys/mman.h>
20 :
21 0 : #define MAX_IN (32UL)
22 :
23 0 : #define FD_KEYGUARD_BLS_KEY_MAX (FD_KEYGUARD_AUTH_VOTERS_MAX+1UL) /* identity plus authorized voters */
24 :
25 : FD_STATIC_ASSERT( sizeof(fd_keyguard_bls_key_t)*FD_KEYGUARD_BLS_KEY_MAX<=4096UL, bls_keys_fit_protected_page );
26 :
27 : /* fd_sign_in_ctx_t is a context object for each in (producer) mcache
28 : connected to the sign tile. */
29 :
30 : struct fd_sign_out_ctx {
31 : fd_wksp_t * out_mem;
32 : ulong out_chunk0;
33 : ulong out_wmark;
34 : ulong out_chunk;
35 : };
36 : typedef struct fd_sign_out_ctx fd_sign_out_ctx_t;
37 :
38 : struct fd_sign_in_ctx {
39 : int role;
40 : fd_wksp_t * mem;
41 : ulong chunk0;
42 : ulong wmark;
43 : ulong mtu;
44 : };
45 : typedef struct fd_sign_in_ctx fd_sign_in_ctx_t;
46 :
47 : typedef struct {
48 : uchar _data[ FD_KEYGUARD_SIGN_REQ_MTU ];
49 :
50 : /* Pre-staged with the public key base58 encoded, followed by "-" in the first bytes */
51 : ulong public_key_base58_sz;
52 : uchar concat[ FD_BASE58_ENCODED_32_SZ+1UL+9UL ];
53 :
54 : fd_sign_in_ctx_t in[ MAX_IN ];
55 : fd_sign_out_ctx_t out[ MAX_IN ];
56 :
57 : fd_sha512_t sha512 [ 1 ];
58 :
59 : fd_keyswitch_t * keyswitch;
60 :
61 : fd_keyswitch_t * av_keyswitch; /* authorized voters */
62 :
63 : uchar * public_key;
64 : uchar * private_key;
65 :
66 : fd_keyguard_bls_key_t * bls_keys;
67 :
68 : uchar tip_payment_program [32];
69 : uchar tip_distribution_program[32];
70 :
71 : ulong authorized_voters_cnt;
72 : uchar authorized_voter_pubkeys[ FD_KEYGUARD_AUTH_VOTERS_MAX ][ 32UL ];
73 : uchar authorized_voter_private_keys[ FD_KEYGUARD_AUTH_VOTERS_MAX ][ 32UL ];
74 :
75 : fd_histf_t sign_duration[1];
76 : } fd_sign_ctx_t;
77 :
78 : FD_FN_CONST static inline ulong
79 0 : scratch_align( void ) {
80 0 : return alignof( fd_sign_ctx_t );
81 0 : }
82 :
83 : FD_FN_PURE static inline ulong
84 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
85 0 : (void)tile;
86 0 : ulong l = FD_LAYOUT_INIT;
87 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
88 0 : return FD_LAYOUT_FINI( l, scratch_align() );
89 0 : }
90 :
91 : static void FD_FN_SENSITIVE
92 0 : derive_fields( fd_sign_ctx_t * ctx ) {
93 0 : uchar check_public_key[ 32 ];
94 0 : fd_ed25519_public_from_private( check_public_key, ctx->private_key, ctx->sha512 );
95 0 : if( FD_UNLIKELY( memcmp( check_public_key, ctx->public_key, 32UL ) ) )
96 0 : FD_LOG_EMERG(( "The public key in the identity key file does not match the public key derived from the private key. "
97 0 : "Firedancer will not use the key pair to sign as it might leak the private key." ));
98 :
99 0 : fd_base58_encode_32( ctx->public_key, &ctx->public_key_base58_sz, (char *)ctx->concat );
100 0 : ctx->concat[ ctx->public_key_base58_sz ] = '-';
101 :
102 0 : fd_keyguard_bls_key_derive( &ctx->bls_keys[ 0 ], ctx->public_key, ctx->private_key, ctx->sha512 );
103 0 : }
104 :
105 : static void FD_FN_SENSITIVE
106 0 : during_housekeeping_sensitive( fd_sign_ctx_t * ctx ) {
107 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
108 0 : memcpy( ctx->private_key, ctx->keyswitch->bytes, 32UL );
109 0 : fd_memzero_explicit( ctx->keyswitch->bytes, 32UL );
110 0 : FD_COMPILER_MFENCE();
111 0 : memcpy( ctx->public_key, ctx->keyswitch->bytes+32UL, 32UL );
112 :
113 0 : derive_fields( ctx );
114 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
115 0 : }
116 :
117 : /* firedancer only */
118 :
119 0 : if( FD_UNLIKELY( ctx->av_keyswitch && fd_keyswitch_state_query( ctx->av_keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
120 0 : ulong param = fd_keyswitch_param_query( ctx->av_keyswitch );
121 0 : if( FD_LIKELY( param==FD_KEYSWITCH_PARAM_AV_ADD ) ) {
122 0 : if( FD_UNLIKELY( ctx->authorized_voters_cnt==FD_KEYGUARD_AUTH_VOTERS_MAX ) ) {
123 0 : FD_LOG_WARNING(( "keyswitch failed: maximum number of authorized voters reached" ));
124 0 : fd_memzero_explicit( ctx->av_keyswitch->bytes, 64UL );
125 0 : ctx->av_keyswitch->result = FD_ADD_AUTHORIZED_VOTER_RESULT_MAX_AUTH_VOTERS;
126 0 : fd_keyswitch_state( ctx->av_keyswitch, FD_KEYSWITCH_STATE_FAILED );
127 0 : return;
128 0 : }
129 0 : for( ulong i=0UL; i<ctx->authorized_voters_cnt; i++ ) {
130 0 : if( FD_UNLIKELY( !memcmp( ctx->authorized_voter_pubkeys[ i ], ctx->av_keyswitch->bytes+32UL, 32UL ) ) ) {
131 0 : FD_BASE58_ENCODE_32_BYTES( ctx->authorized_voter_pubkeys[ i ], pubkey_b58 );
132 0 : FD_LOG_WARNING(( "keyswitch failed: authorized voter key duplicate (%s)", pubkey_b58 ));
133 0 : fd_memzero_explicit( ctx->av_keyswitch->bytes, 64UL );
134 0 : ctx->av_keyswitch->result = FD_ADD_AUTHORIZED_VOTER_RESULT_DUPLICATE_AUTH_VOTER;
135 0 : fd_keyswitch_state( ctx->av_keyswitch, FD_KEYSWITCH_STATE_FAILED );
136 0 : return;
137 0 : }
138 0 : }
139 :
140 0 : memcpy( ctx->authorized_voter_private_keys[ ctx->authorized_voters_cnt ], ctx->av_keyswitch->bytes, 32UL );
141 0 : fd_memzero_explicit( ctx->av_keyswitch->bytes, 32UL );
142 0 : FD_COMPILER_MFENCE();
143 0 : memcpy( ctx->authorized_voter_pubkeys[ ctx->authorized_voters_cnt ], ctx->av_keyswitch->bytes + 32UL, 32UL );
144 0 : fd_keyguard_bls_key_derive( &ctx->bls_keys[ ctx->authorized_voters_cnt+1UL ],
145 0 : ctx->authorized_voter_pubkeys[ ctx->authorized_voters_cnt ],
146 0 : ctx->authorized_voter_private_keys[ ctx->authorized_voters_cnt ],
147 0 : ctx->sha512 );
148 0 : ctx->authorized_voters_cnt++;
149 0 : } else if( FD_LIKELY( param==FD_KEYSWITCH_PARAM_AV_CLEAR ) ) {
150 0 : fd_memzero_explicit( ctx->authorized_voter_private_keys, sizeof( ctx->authorized_voter_private_keys ) );
151 0 : fd_memzero_explicit( ctx->authorized_voter_pubkeys, sizeof( ctx->authorized_voter_pubkeys ) );
152 0 : fd_memzero_explicit( ctx->bls_keys+1, sizeof(fd_keyguard_bls_key_t)*(FD_KEYGUARD_BLS_KEY_MAX-1UL) );
153 0 : ctx->authorized_voters_cnt = 0UL;
154 0 : } else {
155 0 : FD_LOG_CRIT(( "keyswitch: unexpected authorized voter operation %lu", param ));
156 0 : }
157 0 : fd_keyswitch_state( ctx->av_keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
158 0 : }
159 0 : }
160 :
161 : static inline void
162 0 : during_housekeeping( fd_sign_ctx_t * ctx ) {
163 0 : during_housekeeping_sensitive( ctx );
164 0 : }
165 :
166 : static inline void
167 0 : metrics_write( fd_sign_ctx_t * ctx ) {
168 0 : FD_MHIST_COPY( SIGN, SIGN_DURATION_NANOS, ctx->sign_duration );
169 0 : }
170 :
171 : /* during_frag is called between pairs for sequence number checks, as
172 : we are reading incoming frags. We don't actually need to copy the
173 : fragment here, see fd_dedup.c for why we do this.*/
174 :
175 : static void FD_FN_SENSITIVE
176 : during_frag_sensitive( void * _ctx,
177 : ulong in_idx,
178 : ulong seq,
179 : ulong sig,
180 : ulong chunk,
181 0 : ulong sz ) {
182 0 : (void)seq;
183 0 : (void)sig;
184 :
185 0 : fd_sign_ctx_t * ctx = (fd_sign_ctx_t *)_ctx;
186 0 : FD_TEST( in_idx<MAX_IN );
187 :
188 0 : int role = ctx->in[ in_idx ].role;
189 0 : ulong mtu = ctx->in[ in_idx ].mtu;
190 :
191 0 : if( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>mtu ) {
192 0 : FD_LOG_EMERG(( "oversz or out of bounds signing request (role=%d chunk=%lu sz=%lu mtu=%lu, chunk0=%lu, wmark=%lu)", role, chunk, sz, mtu, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
193 0 : }
194 :
195 0 : void * src = fd_chunk_to_laddr( ctx->in[ in_idx ].mem, chunk );
196 0 : fd_memcpy( ctx->_data, src, sz );
197 0 : }
198 :
199 :
200 : static void
201 : during_frag( void * _ctx,
202 : ulong in_idx,
203 : ulong seq,
204 : ulong sig,
205 : ulong chunk,
206 : ulong sz,
207 0 : ulong ctl FD_PARAM_UNUSED ) {
208 0 : during_frag_sensitive( _ctx, in_idx, seq, sig, chunk, sz );
209 0 : }
210 :
211 : static void FD_FN_SENSITIVE
212 : after_frag_sensitive( void * _ctx,
213 : ulong in_idx,
214 : ulong seq,
215 : ulong sig,
216 : ulong sz,
217 : ulong tsorig,
218 : ulong tspub,
219 0 : fd_stem_context_t * stem ) {
220 0 : (void)seq;
221 0 : (void)tspub;
222 :
223 0 : fd_sign_ctx_t * ctx = (fd_sign_ctx_t *)_ctx;
224 :
225 : /* The lower 32 bits are used to specify the sign type.
226 :
227 : If the frag is coming from the repair tile, then the upper 32 bits
228 : contain the repair tile nonce to identify the request.
229 :
230 : If the frag is coming from the send tile, then the upper 32 bits
231 : contain the index of the authorized voter that needs to sign the
232 : vote transaction. The least significant bit of the upper 32 is
233 : used to indicate if a second signature is needed. The next 4 least
234 : significant bits are used to encode the index of the authorized
235 : voter that a signature is needed from.
236 :
237 : BLS requests from the votor tile encode the upper 32 bits the same
238 : way, except that a set least significant bit selects the authorized
239 : voter's key in place of the identity's instead of adding a second
240 : signature. */
241 0 : int sign_type = (int)(uint)(sig);
242 0 : int needs_second_sign = ctx->in[ in_idx ].role==FD_KEYGUARD_ROLE_TXSEND && ((sig>>32) & 1UL);
243 :
244 0 : FD_TEST( in_idx<MAX_IN );
245 :
246 0 : int role = ctx->in[ in_idx ].role;
247 :
248 0 : fd_keyguard_authority_t authority = {0};
249 0 : memcpy( authority.identity_pubkey, ctx->public_key, 32UL );
250 0 : memcpy( authority.tip_payment_program, ctx->tip_payment_program, 32UL );
251 0 : memcpy( authority.tip_distribution_program, ctx->tip_distribution_program, 32UL );
252 :
253 0 : if( FD_UNLIKELY( !fd_keyguard_payload_authorize( &authority, ctx->_data, sz, role, sign_type ) ) ) {
254 0 : FD_LOG_EMERG(( "fd_keyguard_payload_authorize failed (role=%d sign_type=%d)", role, sign_type ));
255 0 : }
256 :
257 0 : long sign_duration = -fd_tickcount();
258 :
259 0 : uchar * dst = fd_chunk_to_laddr( ctx->out[ in_idx ].out_mem, ctx->out[ in_idx ].out_chunk );
260 0 : ulong out_sz = 64UL;
261 :
262 0 : switch( sign_type ) {
263 0 : case FD_KEYGUARD_SIGN_TYPE_ED25519: {
264 0 : fd_ed25519_sign( dst, ctx->_data, sz, ctx->public_key, ctx->private_key, ctx->sha512 );
265 0 : if( needs_second_sign ) {
266 0 : ulong authority_idx = (sig >> 33) & 0xFUL;
267 0 : if( FD_UNLIKELY( authority_idx>=ctx->authorized_voters_cnt ) ) {
268 0 : FD_LOG_CRIT(( "invalid sign request from in_idx=%lu: authority_idx=%lu out of range (authorized_voters_cnt=%lu)", in_idx, authority_idx, ctx->authorized_voters_cnt ));
269 0 : }
270 0 : fd_ed25519_sign( dst+64UL, ctx->_data, sz, ctx->authorized_voter_pubkeys[ authority_idx ], ctx->authorized_voter_private_keys[ authority_idx ], ctx->sha512 );
271 0 : out_sz = 128UL;
272 0 : }
273 0 : break;
274 0 : }
275 0 : case FD_KEYGUARD_SIGN_TYPE_SHA256_ED25519: {
276 0 : uchar hash[ 32 ];
277 0 : fd_sha256_hash( ctx->_data, sz, hash );
278 0 : fd_ed25519_sign( dst, hash, 32UL, ctx->public_key, ctx->private_key, ctx->sha512 );
279 0 : break;
280 0 : }
281 0 : case FD_KEYGUARD_SIGN_TYPE_PUBKEY_CONCAT_ED25519: {
282 0 : memcpy( ctx->concat+ctx->public_key_base58_sz+1UL, ctx->_data, 9UL );
283 0 : fd_ed25519_sign( dst, ctx->concat, ctx->public_key_base58_sz+1UL+9UL, ctx->public_key, ctx->private_key, ctx->sha512 );
284 0 : break;
285 0 : }
286 0 : case FD_KEYGUARD_SIGN_TYPE_BLS: {
287 0 : ulong key_idx = 0UL;
288 0 : if( (sig>>32) & 1UL ) {
289 0 : ulong authority_idx = (sig >> 33) & 0xFUL;
290 0 : if( FD_UNLIKELY( authority_idx>=ctx->authorized_voters_cnt ) ) {
291 0 : FD_LOG_CRIT(( "invalid sign request from in_idx=%lu: authority_idx=%lu out of range (authorized_voters_cnt=%lu)", in_idx, authority_idx, ctx->authorized_voters_cnt ));
292 0 : }
293 0 : key_idx = authority_idx+1UL;
294 0 : }
295 :
296 0 : fd_bls_sig_t bls_sig[1];
297 0 : fd_bls_sec_sign( &ctx->bls_keys[ key_idx ].secret_key, ctx->_data, sz, bls_sig );
298 0 : fd_bls_sig_ser( bls_sig, dst );
299 0 : out_sz = FD_BLS_SIG_SZ;
300 0 : break;
301 0 : }
302 0 : case FD_KEYGUARD_SIGN_TYPE_BLS_PUBKEY: {
303 0 : if( FD_UNLIKELY( sig!=FD_KEYGUARD_SIGN_TYPE_BLS_PUBKEY ) ) {
304 0 : FD_LOG_EMERG(( "invalid BLS public key request (sig=%lu)", sig ));
305 0 : }
306 0 : ulong authority_idx = FD_LOAD( ulong, ctx->_data );
307 0 : if( FD_UNLIKELY( authority_idx!=ULONG_MAX && authority_idx>=ctx->authorized_voters_cnt ) ) {
308 0 : FD_LOG_CRIT(( "invalid BLS public key request from in_idx=%lu: authority_idx=%lu out of range (authorized_voters_cnt=%lu)", in_idx, authority_idx, ctx->authorized_voters_cnt ));
309 0 : }
310 0 : ulong key_idx = authority_idx==ULONG_MAX ? 0UL : authority_idx+1UL;
311 0 : memcpy( dst, ctx->bls_keys[ key_idx ].public_key, FD_BLS_PUB_COMPRESSED_SZ );
312 0 : out_sz = FD_BLS_PUB_COMPRESSED_SZ;
313 0 : break;
314 0 : }
315 0 : default:
316 0 : FD_LOG_EMERG(( "invalid sign type: %d", sign_type ));
317 0 : }
318 :
319 0 : if( FD_LIKELY( sign_type!=FD_KEYGUARD_SIGN_TYPE_BLS_PUBKEY ) ) {
320 0 : sign_duration += fd_tickcount();
321 0 : fd_histf_sample( ctx->sign_duration, (ulong)sign_duration );
322 0 : }
323 :
324 0 : fd_stem_publish( stem, in_idx, sig, ctx->out[ in_idx ].out_chunk, out_sz, 0UL, tsorig, 0UL );
325 0 : ctx->out[ in_idx ].out_chunk = fd_dcache_compact_next( ctx->out[ in_idx ].out_chunk, out_sz, ctx->out[ in_idx ].out_chunk0, ctx->out[ in_idx ].out_wmark );
326 0 : }
327 :
328 : static void
329 : after_frag( void * _ctx,
330 : ulong in_idx,
331 : ulong seq,
332 : ulong sig,
333 : ulong sz,
334 : ulong tsorig,
335 : ulong tspub,
336 0 : fd_stem_context_t * stem ) {
337 0 : after_frag_sensitive( _ctx, in_idx, seq, sig, sz, tsorig, tspub, stem );
338 0 : }
339 :
340 : static void FD_FN_SENSITIVE
341 : privileged_init_sensitive( fd_topo_t const * topo,
342 0 : fd_topo_tile_t const * tile ) {
343 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
344 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
345 0 : fd_sign_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
346 :
347 0 : uchar * identity_key = fd_keyload_mprotect_wr( fd_keyload_load( tile->sign.identity_key_path, /* pubkey only: */ 0 ), /* public_key_only: */ 0 );
348 0 : ctx->private_key = identity_key;
349 0 : ctx->public_key = identity_key + 32UL;
350 :
351 0 : ctx->bls_keys = fd_keyload_alloc_protected_pages( 1UL, 2UL );
352 :
353 0 : FD_TEST( tile->sign.authorized_voter_paths_cnt<FD_KEYGUARD_BLS_KEY_MAX );
354 0 : ctx->authorized_voters_cnt = tile->sign.authorized_voter_paths_cnt;
355 0 : for( ulong i=0UL; i<tile->sign.authorized_voter_paths_cnt; i++ ) {
356 0 : uchar const * authorized_voter_key = fd_keyload_load( tile->sign.authorized_voter_paths[ i ], /* pubkey only: */ 0 );
357 0 : memcpy( ctx->authorized_voter_private_keys[ i ], authorized_voter_key, 32UL );
358 0 : memcpy( ctx->authorized_voter_pubkeys[ i ], authorized_voter_key + 32UL, 32UL );
359 0 : }
360 :
361 : /* The stack can be taken over and reorganized by under AddressSanitizer,
362 : which causes this code to fail. */
363 : #if FD_HAS_ASAN
364 : FD_LOG_WARNING(( "!!! SECURITY WARNING !!! YOU ARE RUNNING THE SIGNING TILE "
365 : "WITH ADDRESS SANITIZER ENABLED. THIS CAN LEAK SENSITIVE "
366 : "DATA INCLUDING YOUR PRIVATE KEYS INTO CORE DUMPS IF THIS "
367 : "PROCESS ABORTS. IT IS HIGHLY ADVISED TO NOT TO RUN IN THIS "
368 : "MODE IN PRODUCTION!" ));
369 : #else
370 : /* Prevent the stack from showing up in core dumps just in case the
371 : private key somehow ends up in there. */
372 0 : FD_TEST( fd_tile_stack0() );
373 0 : FD_TEST( fd_tile_stack_sz() );
374 0 : if( FD_UNLIKELY( madvise( (void*)fd_tile_stack0(), fd_tile_stack_sz(), MADV_DONTDUMP ) ) )
375 0 : FD_LOG_ERR(( "madvise failed (%i-%s)", errno, fd_io_strerror( errno ) ));
376 0 : #endif
377 0 : }
378 :
379 : static void
380 : privileged_init( fd_topo_t const * topo,
381 0 : fd_topo_tile_t const * tile ) {
382 0 : privileged_init_sensitive( topo, tile );
383 0 : }
384 :
385 : static void FD_FN_SENSITIVE
386 : unprivileged_init_sensitive( fd_topo_t const * topo,
387 0 : fd_topo_tile_t const * tile ) {
388 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
389 :
390 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
391 0 : fd_sign_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
392 0 : FD_TEST( fd_sha512_join( fd_sha512_new( ctx->sha512 ) ) );
393 :
394 0 : FD_TEST( tile->in_cnt<=MAX_IN );
395 0 : FD_TEST( tile->in_cnt==tile->out_cnt );
396 :
397 0 : fd_histf_join( fd_metrics_histf_new_ns( ctx->sign_duration, FD_MHIST_EDGES_NS( SIGN, SIGN_DURATION_NANOS ) ) );
398 :
399 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
400 0 : derive_fields( ctx );
401 0 : for( ulong i=0UL; i<ctx->authorized_voters_cnt; i++ ) {
402 0 : fd_keyguard_bls_key_derive( &ctx->bls_keys[ i+1UL ],
403 0 : ctx->authorized_voter_pubkeys[ i ],
404 0 : ctx->authorized_voter_private_keys[ i ],
405 0 : ctx->sha512 );
406 0 : }
407 :
408 0 : if( FD_LIKELY( tile->av_keyswitch_obj_id!=ULONG_MAX ) ) {
409 0 : ctx->av_keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->av_keyswitch_obj_id ) );
410 0 : FD_TEST( ctx->av_keyswitch );
411 0 : } else {
412 0 : ctx->av_keyswitch = NULL;
413 0 : }
414 :
415 0 : memcpy( ctx->tip_payment_program, tile->sign.bundle.tip_payment_program_addr, 32UL );
416 0 : memcpy( ctx->tip_distribution_program, tile->sign.bundle.tip_distribution_program_addr, 32UL );
417 :
418 0 : for( ulong i=0UL; i<MAX_IN; i++ ) ctx->in[ i ].role = -1;
419 :
420 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
421 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ i ] ];
422 0 : fd_topo_link_t const * out_link = &topo->links[ tile->out_link_id[ i ] ];
423 :
424 0 : if( in_link->mtu > FD_KEYGUARD_SIGN_REQ_MTU ) FD_LOG_CRIT(( "oversz link[%lu].mtu=%lu", i, in_link->mtu ));
425 0 : ctx->in[ i ].mem = fd_wksp_containing( in_link->dcache );
426 0 : ctx->in[ i ].mtu = in_link->mtu;
427 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, in_link->dcache );
428 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark( ctx->in[ i ].mem, in_link->dcache, in_link->mtu );
429 :
430 0 : ctx->out[ i ].out_mem = fd_wksp_containing( out_link->dcache );
431 0 : ctx->out[ i ].out_chunk0 = fd_dcache_compact_chunk0( ctx->out[ i ].out_mem, out_link->dcache );
432 0 : ctx->out[ i ].out_wmark = fd_dcache_compact_wmark( ctx->out[ i ].out_mem, out_link->dcache, out_link->mtu );
433 0 : ctx->out[ i ].out_chunk = ctx->out[ i ].out_chunk0;
434 :
435 0 : if( !strcmp( in_link->name, "shred_sign" ) ) {
436 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_LEADER;
437 0 : FD_TEST( !strcmp( out_link->name, "sign_shred" ) );
438 0 : FD_TEST( in_link->mtu==32UL );
439 0 : FD_TEST( out_link->mtu==64UL );
440 0 : } else if ( !strcmp( in_link->name, "gossip_sign" ) ) {
441 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_GOSSIP;
442 0 : FD_TEST( !strcmp( out_link->name, "sign_gossip" ) );
443 0 : FD_TEST( in_link->mtu==2048UL );
444 0 : FD_TEST( out_link->mtu==64UL );
445 0 : } else if ( !strcmp( in_link->name, "repair_sign" ) ) {
446 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_REPAIR;
447 0 : FD_TEST( !strcmp( out_link->name, "sign_repair" ) );
448 0 : FD_TEST( in_link->mtu==124UL ); // FD_REPAIR_MAX_PREIMAGE_SZ
449 0 : FD_TEST( out_link->mtu==64UL );
450 0 : } else if( !strcmp(in_link->name, "bundle_sign" ) ) {
451 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_BUNDLE;
452 0 : FD_TEST( !strcmp( out_link->name, "sign_bundle" ) );
453 0 : FD_TEST( in_link->mtu==9UL );
454 0 : FD_TEST( out_link->mtu==64UL );
455 0 : } else if( !strcmp(in_link->name, "event_sign" ) ) {
456 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_EVENT;
457 0 : FD_TEST( !strcmp( out_link->name, "sign_event" ) );
458 0 : FD_TEST( in_link->mtu==317UL );
459 0 : FD_TEST( out_link->mtu==64UL );
460 0 : } else if( !strcmp(in_link->name, "pack_sign" ) ) {
461 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_BUNDLE_CRANK;
462 0 : FD_TEST( !strcmp( out_link->name, "sign_pack" ) );
463 0 : FD_TEST( in_link->mtu==1232UL );
464 0 : FD_TEST( out_link->mtu==64UL );
465 0 : } else if( !strcmp(in_link->name, "rserve_sign" ) ) {
466 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_RSERVE;
467 0 : FD_TEST( !strcmp( out_link->name, "sign_rserve" ) );
468 0 : FD_TEST( in_link->mtu==32UL );
469 0 : FD_TEST( out_link->mtu==64UL );
470 0 : } else if( !strcmp(in_link->name, "votor_sign" ) ) {
471 0 : ctx->in[ i ].role = FD_KEYGUARD_ROLE_VOTOR;
472 0 : FD_TEST( !strcmp( out_link->name, "sign_votor" ) );
473 0 : FD_TEST( in_link->mtu==FD_KEYGUARD_SIGN_REQ_MTU );
474 0 : FD_TEST( out_link->mtu==FD_BLS_SIG_SZ );
475 0 : } else {
476 0 : FD_LOG_CRIT(( "unexpected link %s", in_link->name ));
477 0 : }
478 0 : }
479 :
480 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
481 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
482 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
483 0 : }
484 :
485 : static void
486 : unprivileged_init( fd_topo_t const * topo,
487 0 : fd_topo_tile_t const * tile ) {
488 0 : unprivileged_init_sensitive( topo, tile );
489 0 : }
490 :
491 : static ulong
492 : populate_allowed_seccomp( fd_topo_t const * topo,
493 : fd_topo_tile_t const * tile,
494 : ulong out_cnt,
495 0 : struct sock_filter * out ) {
496 0 : (void)topo;
497 0 : (void)tile;
498 :
499 0 : populate_sock_filter_policy_fd_sign_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
500 0 : return sock_filter_policy_fd_sign_tile_instr_cnt;
501 0 : }
502 :
503 : static ulong
504 : populate_allowed_fds( fd_topo_t const * topo,
505 : fd_topo_tile_t const * tile,
506 : ulong out_fds_cnt,
507 0 : int * out_fds ) {
508 0 : (void)topo;
509 0 : (void)tile;
510 :
511 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
512 :
513 0 : ulong out_cnt = 0;
514 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
515 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
516 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
517 0 : return out_cnt;
518 0 : }
519 :
520 0 : #define STEM_BURST (1UL)
521 :
522 : /* See explanation in fd_pack */
523 0 : #define STEM_LAZY (128L*3000L)
524 :
525 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_sign_ctx_t
526 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_sign_ctx_t)
527 :
528 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
529 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
530 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
531 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
532 :
533 : #include "../../disco/stem/fd_stem.c"
534 :
535 : fd_topo_run_tile_t fd_tile_sign = {
536 : .name = "sign",
537 : .populate_allowed_seccomp = populate_allowed_seccomp,
538 : .populate_allowed_fds = populate_allowed_fds,
539 : .scratch_align = scratch_align,
540 : .scratch_footprint = scratch_footprint,
541 : .privileged_init = privileged_init,
542 : .unprivileged_init = unprivileged_init,
543 : .run = stem_run,
544 : };
|