LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_sign.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 9 164 5.5 %
Date: 2024-11-13 11:58:15 Functions: 2 12 16.7 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : #include "../../../../disco/tiles.h"
       3             : 
       4             : #include "generated/sign_seccomp.h"
       5             : 
       6             : #include "../../../../disco/keyguard/fd_keyguard.h"
       7             : #include "../../../../disco/keyguard/fd_keyload.h"
       8             : #include "../../../../ballet/base58/fd_base58.h"
       9             : 
      10             : #include <errno.h>
      11             : #include <sys/mman.h>
      12             : 
      13           0 : #define MAX_IN (32UL)
      14             : 
      15             : /* fd_sign_in_ctx_t is a context object for each in (producer) mcache
      16             :    connected to the sign tile. */
      17             : 
      18             : typedef struct {
      19             :   ulong            seq;
      20             :   fd_frag_meta_t * mcache;
      21             :   uchar *          data;
      22             : } fd_sign_out_ctx_t;
      23             : 
      24             : typedef struct {
      25             :   uchar             _data[ FD_KEYGUARD_SIGN_REQ_MTU ];
      26             : 
      27             :   /* Pre-staged with the public key base58 encoded, followed by "-" in the first bytes */
      28             :   ulong public_key_base58_sz;
      29             :   uchar concat[ FD_BASE58_ENCODED_32_SZ+1UL+9UL ];
      30             : 
      31             :   int               in_role[ MAX_IN ];
      32             :   uchar *           in_data[ MAX_IN ];
      33             :   ushort            in_mtu [ MAX_IN ];
      34             : 
      35             :   fd_sign_out_ctx_t out[ MAX_IN ];
      36             : 
      37             :   fd_sha512_t       sha512 [ 1 ];
      38             : 
      39             :   uchar const *     public_key;
      40             :   uchar const *     private_key;
      41             : } fd_sign_ctx_t;
      42             : 
      43             : FD_FN_CONST static inline ulong
      44           3 : scratch_align( void ) {
      45           3 :   return alignof( fd_sign_ctx_t );
      46           3 : }
      47             : 
      48             : FD_FN_PURE static inline ulong
      49           3 : scratch_footprint( fd_topo_tile_t const * tile ) {
      50           3 :   (void)tile;
      51           3 :   ulong l = FD_LAYOUT_INIT;
      52           3 :   l = FD_LAYOUT_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
      53           3 :   return FD_LAYOUT_FINI( l, scratch_align() );
      54           3 : }
      55             : 
      56             : /* during_frag is called between pairs for sequence number checks, as
      57             :    we are reading incoming frags.  We don't actually need to copy the
      58             :    fragment here, see fd_dedup.c for why we do this.*/
      59             : 
      60             : static void FD_FN_SENSITIVE
      61             : during_frag_sensitive( void * _ctx,
      62             :                        ulong  in_idx,
      63             :                        ulong  seq,
      64             :                        ulong  sig,
      65             :                        ulong  chunk,
      66           0 :                        ulong  sz ) {
      67           0 :   (void)seq;
      68           0 :   (void)sig;
      69           0 :   (void)chunk;
      70           0 :   (void)sz;
      71             : 
      72           0 :   fd_sign_ctx_t * ctx = (fd_sign_ctx_t *)_ctx;
      73           0 :   FD_TEST( in_idx<MAX_IN );
      74             : 
      75           0 :   int  role = ctx->in_role[ in_idx ];
      76           0 :   uint mtu  = ctx->in_mtu [ in_idx ];
      77             : 
      78           0 :   if( sz>mtu ) {
      79           0 :     FD_LOG_EMERG(( "oversz signing request (role=%d sz=%lu mtu=%u)", role, sz, mtu ));
      80           0 :   }
      81           0 :   fd_memcpy( ctx->_data, ctx->in_data[ in_idx ], sz );
      82           0 : }
      83             : 
      84             : 
      85             : static void
      86             : during_frag( void * _ctx,
      87             :              ulong  in_idx,
      88             :              ulong  seq,
      89             :              ulong  sig,
      90             :              ulong  chunk,
      91           0 :              ulong  sz ) {
      92           0 :   during_frag_sensitive( _ctx, in_idx, seq, sig, chunk, sz );
      93           0 : }
      94             : 
      95             : static void FD_FN_SENSITIVE
      96             : after_frag_sensitive( void *              _ctx,
      97             :                       ulong               in_idx,
      98             :                       ulong               seq,
      99             :                       ulong               sig,
     100             :                       ulong               chunk,
     101             :                       ulong               sz,
     102             :                       ulong               tsorig,
     103           0 :                       fd_stem_context_t * stem ) {
     104           0 :   (void)seq;
     105           0 :   (void)chunk;
     106           0 :   (void)tsorig;
     107           0 :   (void)stem;
     108             : 
     109           0 :   fd_sign_ctx_t * ctx = (fd_sign_ctx_t *)_ctx;
     110             : 
     111           0 :   int sign_type = (int)(uint)sig;
     112             : 
     113           0 :   FD_TEST( in_idx<MAX_IN );
     114             : 
     115           0 :   int role = ctx->in_role[ in_idx ];
     116             : 
     117           0 :   fd_keyguard_authority_t authority = {0};
     118           0 :   memcpy( authority.identity_pubkey, ctx->public_key, 32 );
     119             : 
     120           0 :   if( FD_UNLIKELY( !fd_keyguard_payload_authorize( &authority, ctx->_data, sz, role, sign_type ) ) ) {
     121           0 :     FD_LOG_EMERG(( "fd_keyguard_payload_authorize failed (role=%d sign_type=%d)", role, sign_type ));
     122           0 :   }
     123             : 
     124           0 :   switch( sign_type ) {
     125           0 :   case FD_KEYGUARD_SIGN_TYPE_ED25519: {
     126           0 :     fd_ed25519_sign( ctx->out[ in_idx ].data, ctx->_data, sz, ctx->public_key, ctx->private_key, ctx->sha512 );
     127           0 :     break;
     128           0 :   }
     129           0 :   case FD_KEYGUARD_SIGN_TYPE_SHA256_ED25519: {
     130           0 :     uchar hash[ 32 ];
     131           0 :     fd_sha256_hash( ctx->_data, sz, hash );
     132           0 :     fd_ed25519_sign( ctx->out[ in_idx ].data, hash, 32UL, ctx->public_key, ctx->private_key, ctx->sha512 );
     133           0 :     break;
     134           0 :   }
     135           0 :   case FD_KEYGUARD_SIGN_TYPE_PUBKEY_CONCAT_ED25519: {
     136           0 :     memcpy( ctx->concat+ctx->public_key_base58_sz+1UL, ctx->_data, 9UL );
     137           0 :     fd_ed25519_sign( ctx->out[ in_idx ].data, ctx->concat, ctx->public_key_base58_sz+1UL+9UL, ctx->public_key, ctx->private_key, ctx->sha512 );
     138           0 :     break;
     139           0 :   }
     140           0 :   default:
     141           0 :     FD_LOG_EMERG(( "invalid sign type: %d", sign_type ));
     142           0 :   }
     143             : 
     144           0 :   fd_mcache_publish( ctx->out[ in_idx ].mcache, 128UL, ctx->out[ in_idx ].seq, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL );
     145           0 :   ctx->out[ in_idx ].seq = fd_seq_inc( ctx->out[ in_idx ].seq, 1UL );
     146           0 : }
     147             : 
     148             : static void
     149             : after_frag( void *              _ctx,
     150             :             ulong               in_idx,
     151             :             ulong               seq,
     152             :             ulong               sig,
     153             :             ulong               chunk,
     154             :             ulong               sz,
     155             :             ulong               tsorig,
     156           0 :             fd_stem_context_t * stem ) {
     157           0 :   after_frag_sensitive( _ctx, in_idx, seq, sig, chunk, sz, tsorig, stem );
     158           0 : }
     159             : 
     160             : static void FD_FN_SENSITIVE
     161             : privileged_init_sensitive( fd_topo_t *      topo,
     162           0 :                            fd_topo_tile_t * tile ) {
     163           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     164             : 
     165           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     166           0 :   fd_sign_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
     167             : 
     168           0 :   uchar const * identity_key = fd_keyload_load( tile->sign.identity_key_path, /* pubkey only: */ 0 );
     169           0 :   ctx->private_key = identity_key;
     170           0 :   ctx->public_key  = identity_key + 32UL;
     171             : 
     172             :     /* The stack can be taken over and reorganized by under AddressSanitizer,
     173             :      which causes this code to fail.  */
     174             : #if FD_HAS_ASAN
     175             :   FD_LOG_WARNING(( "!!! SECURITY WARNING !!! YOU ARE RUNNING THE SIGNING TILE "
     176             :                    "WITH ADDRESS SANITIZER ENABLED. THIS CAN LEAK SENSITIVE "
     177             :                    "DATA INCLUDING YOUR PRIVATE KEYS INTO CORE DUMPS IF THIS "
     178             :                    "PROCESS ABORTS. IT IS HIGHLY ADVISED TO NOT TO RUN IN THIS "
     179             :                    "MODE IN PRODUCTION!" ));
     180             : #else
     181             :   /* Prevent the stack from showing up in core dumps just in case the
     182             :      private key somehow ends up in there. */
     183           0 :   FD_TEST( fd_tile_stack0() );
     184           0 :   FD_TEST( fd_tile_stack_sz() );
     185           0 :   if( FD_UNLIKELY( madvise( (void*)fd_tile_stack0(), fd_tile_stack_sz(), MADV_DONTDUMP ) ) )
     186           0 :     FD_LOG_ERR(( "madvise failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     187           0 : #endif
     188           0 : }
     189             : 
     190             : static void
     191             : privileged_init( fd_topo_t *      topo,
     192           0 :                  fd_topo_tile_t * tile ) {
     193           0 :   privileged_init_sensitive( topo, tile );
     194           0 : }
     195             : 
     196             : static void FD_FN_SENSITIVE
     197             : unprivileged_init_sensitive( fd_topo_t *      topo,
     198           0 :                              fd_topo_tile_t * tile ) {
     199           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     200             : 
     201           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     202           0 :   fd_sign_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_sign_ctx_t ), sizeof( fd_sign_ctx_t ) );
     203           0 :   FD_TEST( fd_sha512_join( fd_sha512_new( ctx->sha512 ) ) );
     204             : 
     205           0 :   uchar check_public_key[ 32 ];
     206           0 :   fd_ed25519_public_from_private( check_public_key, ctx->private_key, ctx->sha512 );
     207           0 :   if( FD_UNLIKELY( memcmp( check_public_key, ctx->public_key, 32 ) ) )
     208           0 :     FD_LOG_EMERG(( "The public key in the identity key file does not match the public key derived from the private key. "
     209           0 :                    "Firedancer will not use the key pair to sign as it might leak the private key." ));
     210             : 
     211           0 :   FD_TEST( tile->in_cnt<=MAX_IN );
     212           0 :   FD_TEST( tile->in_cnt==tile->out_cnt );
     213             : 
     214           0 :   fd_base58_encode_32( ctx->public_key, &ctx->public_key_base58_sz, (char *)ctx->concat );
     215           0 :   ctx->concat[ ctx->public_key_base58_sz ] = '-';
     216             : 
     217           0 :   for( ulong i=0; i<MAX_IN; i++ ) ctx->in_role[ i ] = -1;
     218             : 
     219           0 :   for( ulong i=0; i<tile->in_cnt; i++ ) {
     220           0 :     fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ i ] ];
     221           0 :     fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ i ] ];
     222             : 
     223           0 :     if( in_link->mtu > FD_KEYGUARD_SIGN_REQ_MTU ) FD_LOG_CRIT(( "oversz link[%lu].mtu=%lu", i, in_link->mtu ));
     224           0 :     ctx->in_data[ i ] = in_link->dcache;
     225           0 :     ctx->in_mtu [ i ] = (ushort)in_link->mtu;
     226             : 
     227           0 :     ctx->out[ i ].mcache = out_link->mcache;
     228           0 :     ctx->out[ i ].data   = out_link->dcache;
     229           0 :     ctx->out[ i ].seq    = 0UL;
     230             : 
     231           0 :     if( !strcmp( in_link->name, "shred_sign" ) ) {
     232           0 :       ctx->in_role[ i ] = FD_KEYGUARD_ROLE_LEADER;
     233           0 :       FD_TEST( !strcmp( out_link->name, "sign_shred" ) );
     234           0 :       FD_TEST( in_link->mtu==32UL );
     235           0 :       FD_TEST( out_link->mtu==64UL );
     236           0 :     } else if ( !strcmp( in_link->name, "gossip_sign" ) ) {
     237           0 :       ctx->in_role[ i ] = FD_KEYGUARD_ROLE_GOSSIP;
     238           0 :       FD_TEST( !strcmp( out_link->name, "sign_gossip" ) );
     239           0 :       FD_TEST( in_link->mtu==2048UL );
     240           0 :       FD_TEST( out_link->mtu==64UL );
     241           0 :     } else if ( !strcmp( in_link->name, "repair_sign")) {
     242           0 :       ctx->in_role[ i ] = FD_KEYGUARD_ROLE_REPAIR;
     243           0 :       FD_TEST( !strcmp( out_link->name, "sign_repair" ) );
     244           0 :       FD_TEST( in_link->mtu==2048UL );
     245           0 :       FD_TEST( out_link->mtu==64UL );
     246           0 :     } else if ( !strcmp(in_link->name, "voter_sign" ) ) {
     247           0 :       ctx->in_role[ i ] = FD_KEYGUARD_ROLE_VOTER;
     248           0 :       FD_TEST( !strcmp( out_link->name, "sign_voter"  ) );
     249           0 :       FD_TEST( in_link->mtu==FD_TXN_MTU  );
     250           0 :       FD_TEST( out_link->mtu==64UL );
     251           0 :     } else if( !strcmp(in_link->name, "bundle_sign" ) ) {
     252           0 :       ctx->in_role[ i ] = FD_KEYGUARD_ROLE_BUNDLE;
     253           0 :       FD_TEST( !strcmp( out_link->name, "sign_bundle" ) );
     254           0 :       FD_TEST( in_link->mtu==9UL );
     255           0 :       FD_TEST( out_link->mtu==64UL );
     256           0 :     } else {
     257           0 :       FD_LOG_CRIT(( "unexpected link %s", in_link->name ));
     258           0 :     }
     259           0 :   }
     260             : 
     261           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     262           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     263           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     264           0 : }
     265             : 
     266             : static void
     267             : unprivileged_init( fd_topo_t *      topo,
     268           0 :                    fd_topo_tile_t * tile ) {
     269           0 :   unprivileged_init_sensitive( topo, tile );
     270           0 : }
     271             : 
     272             : static ulong
     273             : populate_allowed_seccomp( fd_topo_t const *      topo,
     274             :                           fd_topo_tile_t const * tile,
     275             :                           ulong                  out_cnt,
     276           0 :                           struct sock_filter *   out ) {
     277           0 :   (void)topo;
     278           0 :   (void)tile;
     279             : 
     280           0 :   populate_sock_filter_policy_sign( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     281           0 :   return sock_filter_policy_sign_instr_cnt;
     282           0 : }
     283             : 
     284             : static ulong
     285             : populate_allowed_fds( fd_topo_t const *      topo,
     286             :                       fd_topo_tile_t const * tile,
     287             :                       ulong                  out_fds_cnt,
     288           0 :                       int *                  out_fds ) {
     289           0 :   (void)topo;
     290           0 :   (void)tile;
     291             : 
     292           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     293             : 
     294           0 :   ulong out_cnt = 0;
     295           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     296           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     297           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     298           0 :   return out_cnt;
     299           0 : }
     300             : 
     301           0 : #define STEM_BURST (1UL)
     302             : 
     303             : /* See explanation in fd_pack */
     304           0 : #define STEM_LAZY  (128L*3000L)
     305             : 
     306           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_sign_ctx_t
     307           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_sign_ctx_t)
     308             : 
     309           0 : #define STEM_CALLBACK_DURING_FRAG during_frag
     310           0 : #define STEM_CALLBACK_AFTER_FRAG  after_frag
     311             : 
     312             : #include "../../../../disco/stem/fd_stem.c"
     313             : 
     314             : fd_topo_run_tile_t fd_tile_sign = {
     315             :   .name                     = "sign",
     316             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     317             :   .populate_allowed_fds     = populate_allowed_fds,
     318             :   .scratch_align            = scratch_align,
     319             :   .scratch_footprint        = scratch_footprint,
     320             :   .privileged_init          = privileged_init,
     321             :   .unprivileged_init        = unprivileged_init,
     322             :   .run                      = stem_run,
     323             : };

Generated by: LCOV version 1.14