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