LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 413 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 20 0.0 %

          Line data    Source code
       1             : #include "fd_bundle_tile_private.h"
       2             : #include "../metrics/fd_metrics.h"
       3             : #include "../topo/fd_topo.h"
       4             : #include "../keyguard/fd_keyload.h"
       5             : #include "../plugin/fd_plugin.h"
       6             : #include "../../waltz/http/fd_url.h"
       7             : 
       8             : #include <errno.h>
       9             : #include <dirent.h> /* opendir */
      10             : #include <stdio.h> /* snprintf */
      11             : #include <fcntl.h> /* F_SETFL */
      12             : #include <unistd.h> /* close */
      13             : #include <sys/mman.h> /* PROT_READ (seccomp) */
      14             : #include <sys/uio.h> /* writev */
      15             : #include <netinet/in.h> /* AF_INET */
      16             : #include <netinet/tcp.h> /* TCP_FASTOPEN_CONNECT (seccomp) */
      17             : #include "../../waltz/resolv/fd_netdb.h"
      18             : 
      19             : #include "generated/fd_bundle_tile_seccomp.h"
      20             : 
      21             : /* Provided by fdctl/firedancer version.c */
      22             : extern char const fdctl_version_string[];
      23             : 
      24             : FD_FN_CONST static ulong
      25           0 : scratch_align( void ) {
      26           0 :   return alignof(fd_bundle_tile_t);
      27           0 : }
      28             : 
      29             : FD_FN_CONST static ulong
      30           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      31           0 :   (void)tile;
      32           0 :   ulong l = FD_LAYOUT_INIT;
      33           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
      34           0 :   l = FD_LAYOUT_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
      35           0 :   l = FD_LAYOUT_APPEND( l, fd_alloc_align(),          fd_alloc_footprint()                            );
      36           0 :   return FD_LAYOUT_FINI( l, 32 );
      37           0 : }
      38             : 
      39             : FD_FN_CONST static inline ulong
      40           0 : loose_footprint( fd_topo_tile_t const * tile ) {
      41           0 :   (void)tile;
      42             :   /* Leftover space for OpenSSL allocations */
      43           0 :   return 1UL<<26; /* 64 MiB */
      44           0 : }
      45             : 
      46             : static inline void
      47           0 : metrics_write( fd_bundle_tile_t * ctx ) {
      48           0 :   FD_MCNT_SET( BUNDLE, TRANSACTION_RECEIVED,   ctx->metrics.txn_received_cnt          );
      49           0 :   FD_MCNT_SET( BUNDLE, BUNDLE_RECEIVED,        ctx->metrics.bundle_received_cnt       );
      50           0 :   FD_MCNT_SET( BUNDLE, PACKET_RECEIVED,        ctx->metrics.packet_received_cnt       );
      51           0 :   FD_MCNT_SET( BUNDLE, SHREDSTREAM_HEARTBEATS, ctx->metrics.shredstream_heartbeat_cnt );
      52           0 :   FD_MCNT_SET( BUNDLE, KEEPALIVES,             ctx->metrics.ping_ack_cnt              );
      53           0 :   FD_MCNT_SET( BUNDLE, ERRORS_PROTOBUF,        ctx->metrics.decode_fail_cnt           );
      54           0 :   FD_MCNT_SET( BUNDLE, ERRORS_TRANSPORT,       ctx->metrics.transport_fail_cnt        );
      55           0 :   FD_MCNT_SET( BUNDLE, ERRORS_NO_FEE_INFO,     ctx->metrics.missing_builder_info_fail_cnt );
      56             : 
      57           0 :   FD_MGAUGE_SET( BUNDLE, RTT_SAMPLE,   (ulong)ctx->rtt->latest_rtt   );
      58           0 :   FD_MGAUGE_SET( BUNDLE, RTT_SMOOTHED, (ulong)ctx->rtt->smoothed_rtt );
      59           0 :   FD_MGAUGE_SET( BUNDLE, RTT_VAR,      (ulong)ctx->rtt->var_rtt      );
      60             : 
      61           0 :   fd_wksp_t * wksp = fd_wksp_containing( ctx );
      62           0 :   fd_wksp_usage_t usage[1];
      63           0 :   ulong const free_tag = 0UL;
      64           0 :   if( FD_UNLIKELY( !fd_wksp_usage( wksp, &free_tag, 1UL, usage ) ) ) {
      65           0 :     FD_LOG_ERR(( "fd_wksp_usage failed" )); /* unreachable */
      66           0 :   }
      67           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_SIZE,       usage->total_sz );
      68           0 :   FD_MGAUGE_SET( BUNDLE, HEAP_FREE_BYTES, usage->used_sz  );
      69             : 
      70           0 :   int bundle_status = fd_bundle_client_status( ctx );
      71           0 :   FD_MGAUGE_SET( BUNDLE, CONNECTED, bundle_status==FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE_STATUS_CONNECTED );
      72           0 :   ctx->bundle_status_recent = (uchar)bundle_status;
      73           0 : }
      74             : 
      75             : void
      76           0 : fd_bundle_tile_housekeeping( fd_bundle_tile_t * ctx ) {
      77           0 :   long log_interval_ns = (long)30e9;
      78           0 :   int  status          = fd_bundle_client_status( ctx );
      79           0 :   long log_next_ns     = ctx->last_bundle_status_log_nanos + log_interval_ns;
      80           0 :   long now_ns          = fd_log_wallclock();
      81           0 :   if( FD_UNLIKELY( status!=FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE_STATUS_CONNECTED && now_ns>log_next_ns ) ) {
      82           0 :     FD_LOG_WARNING(( "No bundle server connection in the last %ld seconds", log_interval_ns/(long)1e9 ) );
      83           0 :     ctx->last_bundle_status_log_nanos = now_ns;
      84           0 :   }
      85             : 
      86           0 :   if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
      87           0 :     fd_memcpy( ctx->auther.pubkey, ctx->keyswitch->bytes, 32UL );
      88           0 :     fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
      89           0 :     ctx->defer_reset = 1;
      90           0 :   }
      91           0 : }
      92             : 
      93             : static void
      94             : fd_bundle_tile_publish_block_engine_update(
      95             :     fd_bundle_tile_t *  ctx,
      96             :     fd_stem_context_t * stem
      97           0 : ) {
      98           0 :   fd_plugin_msg_block_engine_update_t * update =
      99           0 :       fd_chunk_to_laddr( ctx->plugin_out.mem, ctx->plugin_out.chunk );
     100           0 :   memset( update, 0, sizeof(fd_plugin_msg_block_engine_update_t) );
     101             : 
     102           0 :   strncpy( update->name, "jito", sizeof(update->name) );
     103             : 
     104             :   /* Deliberately silently truncates */
     105           0 :   snprintf( update->url, sizeof(update->url), "%s://%.*s:%u",
     106           0 :             ctx->is_ssl ? "https" : "http",
     107           0 :             (int)ctx->server_fqdn_len,
     108           0 :             ctx->server_fqdn,
     109           0 :             ctx->server_tcp_port );
     110             : 
     111             :   /* Format IPv4 string */
     112           0 :   snprintf( update->ip_cstr, sizeof(update->ip_cstr),
     113           0 :             FD_IP4_ADDR_FMT,
     114           0 :             FD_IP4_ADDR_FMT_ARGS( ctx->server_ip4_addr ) );
     115             : 
     116           0 :   update->status = (uchar)ctx->bundle_status_recent;
     117             : 
     118           0 :   ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
     119           0 :   fd_stem_publish(
     120           0 :       stem,
     121           0 :       ctx->plugin_out.idx,
     122           0 :       FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE,
     123           0 :       ctx->plugin_out.chunk,
     124           0 :       sizeof(fd_plugin_msg_block_engine_update_t),
     125           0 :       0UL, /* ctl */
     126           0 :       0UL, /* seq */
     127           0 :       tspub
     128           0 :   );
     129           0 :   ctx->plugin_out.chunk = fd_dcache_compact_next( ctx->plugin_out.chunk, sizeof(fd_plugin_msg_block_engine_update_t), ctx->plugin_out.chunk0, ctx->plugin_out.wmark );
     130           0 : }
     131             : 
     132             : static void
     133             : after_credit( fd_bundle_tile_t *  ctx,
     134             :               fd_stem_context_t * stem,
     135             :               int *               opt_poll_in,
     136           0 :               int *               charge_busy ) {
     137           0 :   (void)opt_poll_in;
     138           0 :   if( FD_UNLIKELY( !ctx->stem ) ) ctx->stem = stem;
     139           0 :   fd_bundle_client_step( ctx, charge_busy );
     140             : 
     141           0 :   if( ctx->plugin_out.mem ) {
     142           0 :     if( FD_UNLIKELY( ctx->bundle_status_recent != ctx->bundle_status_plugin ) ) {
     143           0 :       fd_bundle_tile_publish_block_engine_update( ctx, stem );
     144           0 :       ctx->bundle_status_plugin = (uchar)ctx->bundle_status_recent;
     145           0 :       *charge_busy = 1;
     146           0 :     }
     147           0 :   }
     148           0 : }
     149             : 
     150             : static void
     151             : parse_url( fd_url_t *   url_,
     152             :            char const * url_str,
     153             :            ulong        url_str_len,
     154             :            ushort *     tcp_port,
     155           0 :            _Bool *      is_ssl ) {
     156             : 
     157             :   /* Parse URL */
     158             : 
     159           0 :   int url_err[1];
     160           0 :   fd_url_t * url = fd_url_parse_cstr( url_, url_str, url_str_len, url_err );
     161           0 :   if( FD_UNLIKELY( !url ) ) {
     162           0 :     switch( *url_err ) {
     163           0 :     scheme_err:
     164           0 :     case FD_URL_ERR_SCHEME:
     165           0 :       FD_LOG_ERR(( "Invalid [tiles.bundle.url] `%.*s`: must start with `http://` or `https://`", (int)url_str_len, url_str ));
     166           0 :       break;
     167           0 :     case FD_URL_ERR_HOST_OVERSZ:
     168           0 :       FD_LOG_ERR(( "Invalid [tiles.bundle.url] `%.*s`: domain name is too long", (int)url_str_len, url_str ));
     169           0 :       break;
     170           0 :     default:
     171           0 :       FD_LOG_ERR(( "Invalid [tiles.bundle.url] `%.*s`", (int)url_str_len, url_str ));
     172           0 :       break;
     173           0 :     }
     174           0 :   }
     175             : 
     176             :   /* FIXME the URL scheme path technically shouldn't contain slashes */
     177           0 :   if( url->scheme_len==8UL && fd_memeq( url->scheme, "https://", 8UL ) ) {
     178           0 :     *is_ssl = 1;
     179           0 :   } else if( url->scheme_len==7UL && fd_memeq( url->scheme, "http://", 7UL ) ) {
     180           0 :     *is_ssl = 0;
     181           0 :   } else {
     182           0 :     goto scheme_err;
     183           0 :   }
     184             : 
     185             :   /* Parse port number */
     186             : 
     187           0 :   *tcp_port = 443;
     188           0 :   if( url->port_len ) {
     189           0 :     if( FD_UNLIKELY( url->port_len > 5 ) ) {
     190           0 :     invalid_port:
     191           0 :       FD_LOG_ERR(( "Invalid [tiles.bundle.url] `%.*s`: invalid port number", (int)url_str_len, url_str ));
     192           0 :     }
     193             : 
     194           0 :     char port_cstr[6];
     195           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( port_cstr ), url->port, url->port_len ) );
     196           0 :     ulong port_no = fd_cstr_to_ulong( port_cstr );
     197           0 :     if( FD_UNLIKELY( !port_no || port_no>USHORT_MAX ) ) goto invalid_port;
     198             : 
     199           0 :     *tcp_port = (ushort)port_no;
     200           0 :   }
     201             : 
     202             :   /* Resolve domain */
     203             : 
     204           0 :   if( FD_UNLIKELY( url->host_len > 255 ) ) {
     205           0 :     FD_LOG_CRIT(( "Invalid url->host_len" )); /* unreachable */
     206           0 :   }
     207           0 :   char host_cstr[ 256 ];
     208           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( host_cstr ), url->host, url->host_len ) );
     209           0 : }
     210             : 
     211             : static void
     212             : fd_bundle_tile_parse_endpoint( fd_bundle_tile_t *     ctx,
     213           0 :                                fd_topo_tile_t const * tile ) {
     214           0 :   fd_url_t url[1];
     215           0 :   _Bool is_ssl = 0;
     216           0 :   parse_url(
     217           0 :       url,
     218           0 :       tile->bundle.url, tile->bundle.url_len,
     219           0 :       &ctx->server_tcp_port,
     220           0 :       &is_ssl
     221           0 :   );
     222           0 :   if( FD_UNLIKELY( url->host_len > 255 ) ) {
     223           0 :     FD_LOG_CRIT(( "Invalid url->host_len" )); /* unreachable */
     224           0 :   }
     225           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_fqdn ), url->host, url->host_len ) );
     226           0 :   ctx->server_fqdn_len = url->host_len;
     227             : 
     228           0 :   if( FD_UNLIKELY( tile->bundle.sni_len ) ) {
     229           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), tile->bundle.sni, tile->bundle.sni_len ) );
     230           0 :     ctx->server_sni_len = tile->bundle.sni_len;
     231           0 :   } else {
     232           0 :     fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( ctx->server_sni ), url->host, url->host_len ) );
     233           0 :     ctx->server_sni_len = url->host_len;
     234           0 :   }
     235             : 
     236           0 :   ctx->is_ssl = !!is_ssl;
     237             : #if !FD_HAS_OPENSSL
     238             :   if( FD_UNLIKELY( is_ssl ) ) {
     239             :     FD_LOG_ERR(( "This build does not include OpenSSL. To install OpenSSL, re-run ./deps.sh and do a clean re build." ));
     240             :   }
     241             : #endif
     242           0 : }
     243             : 
     244             : #if FD_HAS_OPENSSL
     245             : 
     246             : /* OpenSSL allows us to specify custom memory allocation functions,
     247             :    which we want to point to an fd_alloc_t, but it does not let us use a
     248             :    context object.  Instead we stash it in this thread local, which is
     249             :    OK because the parent workspace exists for the duration of the SSL
     250             :    context, and the process only has one thread.
     251             : 
     252             :    Currently fd_alloc doesn't support realloc, so it's implemented on
     253             :    top of malloc and free, and then also it doesn't support getting the
     254             :    size of an allocation from the pointer, which we need for realloc, so
     255             :    we pad each alloc by 8 bytes and stuff the size into the first 8
     256             :    bytes. */
     257             : static FD_TL fd_alloc_t * fd_quic_ssl_mem_function_ctx = NULL;
     258             : 
     259             : static void *
     260             : crypto_malloc( ulong        num,
     261             :                char const * file,
     262           0 :                int          line ) {
     263           0 :   (void)file; (void)line;
     264           0 :   void * result = fd_alloc_malloc( fd_quic_ssl_mem_function_ctx, 16UL, num + 8UL );
     265           0 :   if( FD_UNLIKELY( !result ) ) {
     266           0 :     FD_MCNT_INC( BUNDLE, ERRORS_SSL_ALLOC, 1UL );
     267           0 :     return NULL;
     268           0 :   }
     269           0 :   *(ulong *)result = num;
     270           0 :   return (uchar *)result + 8UL;
     271           0 : }
     272             : 
     273             : static void
     274             : crypto_free( void *       addr,
     275             :              char const * file,
     276           0 :              int          line ) {
     277           0 :   (void)file;
     278           0 :   (void)line;
     279             : 
     280           0 :   if( FD_UNLIKELY( !addr ) ) return;
     281           0 :   fd_alloc_free( fd_quic_ssl_mem_function_ctx, (uchar *)addr - 8UL );
     282           0 : }
     283             : 
     284             : static void *
     285             : crypto_realloc( void *       addr,
     286             :                 ulong        num,
     287             :                 char const * file,
     288           0 :                 int          line ) {
     289           0 :   (void)file;
     290           0 :   (void)line;
     291             : 
     292           0 :   if( FD_UNLIKELY( !addr ) ) return crypto_malloc( num, file, line );
     293           0 :   if( FD_UNLIKELY( !num ) ) {
     294           0 :     crypto_free( addr, file, line );
     295           0 :     return NULL;
     296           0 :   }
     297             : 
     298           0 :   void * new = fd_alloc_malloc( fd_quic_ssl_mem_function_ctx, 16UL, num + 8UL );
     299           0 :   if( FD_UNLIKELY( !new ) ) return NULL;
     300             : 
     301           0 :   ulong old_num = *(ulong *)( (uchar *)addr - 8UL );
     302           0 :   fd_memcpy( (uchar*)new + 8, (uchar*)addr, fd_ulong_min( old_num, num ) );
     303           0 :   fd_alloc_free( fd_quic_ssl_mem_function_ctx, (uchar *)addr - 8UL );
     304           0 :   *(ulong *)new = num;
     305           0 :   return (uchar*)new + 8UL;
     306           0 : }
     307             : 
     308             : static void
     309             : fd_ossl_keylog_callback( SSL const *  ssl,
     310           0 :                          char const * line ) {
     311           0 :   SSL_CTX * ssl_ctx = SSL_get_SSL_CTX( ssl );
     312           0 :   fd_bundle_tile_t * ctx = SSL_CTX_get_ex_data( ssl_ctx, 0 );
     313           0 :   ulong line_len = strlen( line );
     314           0 :   struct iovec iovs[2] = {
     315           0 :     { .iov_base=(void *)line, .iov_len=line_len },
     316           0 :     { .iov_base=(void *)"\n", .iov_len=1UL }
     317           0 :   };
     318           0 :   if( FD_UNLIKELY( writev( ctx->keylog_fd, iovs, 2 )!=(long)line_len+1 ) ) {
     319           0 :     FD_LOG_WARNING(( "write(keylog) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     320           0 :   }
     321           0 : }
     322             : 
     323             : static void
     324           0 : fd_bundle_tile_load_certs( SSL_CTX * ssl_ctx ) {
     325           0 :   X509_STORE * ca_certs = X509_STORE_new();
     326           0 :   if( FD_UNLIKELY( !ca_certs ) ) {
     327           0 :     FD_LOG_ERR(( "X509_STORE_new failed" ));
     328           0 :   }
     329             : 
     330           0 :   static char const default_dir[] = "/etc/ssl/certs/";
     331           0 :   DIR * dir = opendir( default_dir );
     332           0 :   if( FD_UNLIKELY( !dir ) ) {
     333           0 :     FD_LOG_ERR(( "opendir(%s) failed (%i-%s)", default_dir, errno, fd_io_strerror( errno ) ));
     334           0 :   }
     335             : 
     336           0 :   struct dirent * entry;
     337           0 :   while( (entry = readdir( dir )) ) {
     338           0 :     if( !strcmp( entry->d_name, "." ) || !strcmp( entry->d_name, ".." ) ) continue;
     339             : 
     340           0 :     char cert_path[ PATH_MAX ];
     341           0 :     char * p = fd_cstr_init( cert_path );
     342           0 :     p = fd_cstr_append_text( p, default_dir, sizeof(default_dir)-1 );
     343           0 :     p = fd_cstr_append_cstr_safe( p, entry->d_name, (ulong)(cert_path+sizeof(cert_path)-1) - (ulong)p );
     344           0 :     fd_cstr_fini( p );
     345             : 
     346           0 :     if( !X509_STORE_load_locations( ca_certs, cert_path, NULL ) ) {
     347             :       /* Not all files in /etc/ssl/certs are valid certs, so ignore errors */
     348           0 :       continue;
     349           0 :     }
     350           0 :   }
     351             : 
     352           0 :   STACK_OF(X509) * cert_list = X509_STORE_get1_all_certs( ca_certs );
     353           0 :   FD_LOG_INFO(( "Loaded %d CA certs from %s into OpenSSL", sk_X509_num( cert_list ), default_dir ));
     354           0 :   if( fd_log_level_logfile()==0 ) {
     355           0 :     for( int i=0; i<sk_X509_num( cert_list ); i++ ) {
     356           0 :       X509 * cert = sk_X509_value( cert_list, i );
     357           0 :       FD_LOG_DEBUG(( "Loaded CA cert \"%s\"", X509_NAME_oneline( X509_get_subject_name( cert ), NULL, 0 ) ));
     358           0 :     }
     359           0 :   }
     360           0 :   sk_X509_pop_free( cert_list, X509_free );
     361             : 
     362           0 :   SSL_CTX_set_cert_store( ssl_ctx, ca_certs );
     363             : 
     364           0 :   if( FD_UNLIKELY( 0!=closedir( dir ) ) ) {
     365           0 :     FD_LOG_ERR(( "closedir(%s) failed (%i-%s)", default_dir, errno, fd_io_strerror( errno ) ));
     366           0 :   }
     367           0 : }
     368             : 
     369             : static void
     370             : fd_bundle_tile_init_openssl( fd_bundle_tile_t * ctx,
     371             :                              void *             alloc_mem,
     372           0 :                              int                tls_cert_verify ) {
     373           0 :   fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( alloc_mem, 1UL ), 1UL );
     374           0 :   if( FD_UNLIKELY( !alloc ) ) {
     375           0 :     FD_LOG_ERR(( "fd_alloc_new failed" ));
     376           0 :   }
     377           0 :   ctx->ssl_alloc               = alloc;
     378           0 :   fd_quic_ssl_mem_function_ctx = alloc;
     379             : 
     380           0 :   if( FD_UNLIKELY( !CRYPTO_set_mem_functions( crypto_malloc, crypto_realloc, crypto_free ) ) ) {
     381           0 :     FD_LOG_ERR(( "CRYPTO_set_mem_functions failed" ));
     382           0 :   }
     383             : 
     384           0 :   OPENSSL_init_ssl(
     385           0 :       OPENSSL_INIT_LOAD_SSL_STRINGS |
     386           0 :       OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
     387           0 :       OPENSSL_INIT_NO_LOAD_CONFIG,
     388           0 :       NULL
     389           0 :   );
     390             : 
     391           0 :   SSL_CTX * ssl_ctx = SSL_CTX_new( TLS_client_method() );
     392           0 :   if( FD_UNLIKELY( !ssl_ctx ) ) {
     393           0 :     FD_LOG_ERR(( "SSL_CTX_new failed" ));
     394           0 :   }
     395             : 
     396           0 :   if( FD_UNLIKELY( !SSL_CTX_set_ex_data( ssl_ctx, 0, ctx ) ) ) {
     397           0 :     FD_LOG_ERR(( "SSL_CTX_set_ex_data failed" ));
     398           0 :   }
     399             : 
     400           0 :   if( FD_UNLIKELY( !SSL_CTX_set_mode( ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_AUTO_RETRY ) ) ) {
     401           0 :     FD_LOG_ERR(( "SSL_CTX_set_mode failed" ));
     402           0 :   }
     403             : 
     404           0 :   if( FD_UNLIKELY( !SSL_CTX_set_min_proto_version( ssl_ctx, TLS1_3_VERSION ) ) ) {
     405           0 :     FD_LOG_ERR(( "SSL_CTX_set_min_proto_version(ssl_ctx,TLS1_3_VERSION) failed" ));
     406           0 :   }
     407             : 
     408           0 :   if( FD_UNLIKELY( 0!=SSL_CTX_set_alpn_protos( ssl_ctx, (const unsigned char *)"\x02h2", 3 ) ) ) {
     409           0 :     FD_LOG_ERR(( "SSL_CTX_set_alpn_protos failed" ));
     410           0 :   }
     411             : 
     412           0 :   if( tls_cert_verify ) {
     413           0 :     fd_bundle_tile_load_certs( ssl_ctx );
     414           0 :     SSL_CTX_set_verify( ssl_ctx, SSL_VERIFY_PEER, NULL );
     415           0 :   }
     416             : 
     417           0 :   if( FD_LIKELY( ctx->keylog_fd >= 0 ) ) {
     418           0 :     SSL_CTX_set_keylog_callback( ssl_ctx, fd_ossl_keylog_callback );
     419           0 :   }
     420             : 
     421           0 :   ctx->ssl_ctx = ssl_ctx;
     422           0 : }
     423             : 
     424             : #endif /* FD_HAS_OPENSSL */
     425             : 
     426             : static void
     427             : privileged_init( fd_topo_t *      topo,
     428           0 :                  fd_topo_tile_t * tile ) {
     429           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     430             : 
     431           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     432           0 :   fd_bundle_tile_t * ctx         = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_bundle_tile_t), sizeof(fd_bundle_tile_t)                        );
     433           0 :   void *             grpc_mem    = FD_SCRATCH_ALLOC_APPEND( l, fd_grpc_client_align(),    fd_grpc_client_footprint( tile->bundle.buf_sz ) );
     434           0 :   void *             alloc_mem   = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(),          fd_alloc_footprint()                            );
     435           0 :   ulong              scratch_end = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     436           0 :   (void)alloc_mem; /* potentially unused */
     437             : 
     438           0 :   if( FD_UNLIKELY( (ulong)ctx != (ulong)scratch ) ) {
     439           0 :     FD_LOG_CRIT(( "Invalid bundle tile scratch alignment" )); /* unreachable */
     440           0 :   }
     441           0 :   if( FD_UNLIKELY( scratch_end - (ulong)scratch > scratch_footprint( tile ) ) ) {
     442           0 :     FD_LOG_CRIT(( "Bundle tile scratch overflow" )); /* unreachable */
     443           0 :   }
     444             : 
     445           0 :   memset( ctx, 0, sizeof(fd_bundle_tile_t) );
     446           0 :   ctx->grpc_client_mem = grpc_mem;
     447           0 :   ctx->grpc_buf_max    = tile->bundle.buf_sz;
     448           0 :   ctx->tcp_sock        = -1;
     449             : 
     450           0 :   fd_bundle_auther_init( &ctx->auther );
     451           0 :   uchar const * public_key = fd_keyload_load( tile->bundle.identity_key_path, 1 /* public key only */ );
     452           0 :   fd_memcpy( ctx->auther.pubkey, public_key, 32UL );
     453             : 
     454           0 :   ctx->keylog_fd = -1;
     455             : 
     456           0 : # if FD_HAS_OPENSSL
     457             : 
     458           0 :   if( FD_UNLIKELY( tile->bundle.key_log_path[0] ) ) {
     459           0 :     ctx->keylog_fd = open( tile->bundle.key_log_path, O_WRONLY|O_APPEND|O_CREAT, 0644 );
     460           0 :     if( FD_UNLIKELY( ctx->keylog_fd < 0 ) ) {
     461           0 :       FD_LOG_ERR(( "open(%s) failed (%i-%s)", tile->bundle.key_log_path, errno, fd_io_strerror( errno ) ));
     462           0 :     }
     463           0 :   }
     464             : 
     465             :   /* OpenSSL goes and tries to read files and allocate memory and
     466             :      other dumb things on a thread local basis, so we need a special
     467             :      initializer to do it before seccomp happens in the process. */
     468           0 :   fd_bundle_tile_init_openssl( ctx, alloc_mem, tile->bundle.tls_cert_verify );
     469             : 
     470           0 : # endif /* FD_HAS_OPENSSL */
     471             : 
     472             :   /* Init resolver */
     473           0 :   if( FD_UNLIKELY( !fd_netdb_open_fds( ctx->netdb_fds ) ) ) {
     474           0 :     FD_LOG_ERR(( "fd_netdb_open_fds failed" ));
     475           0 :   }
     476             : 
     477             :   /* Random seed for header hashmap */
     478           0 :   if( FD_UNLIKELY( !fd_rng_secure( &ctx->map_seed, sizeof(ulong) ) ) ) {
     479           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     480           0 :   }
     481             : 
     482             :   /* Random seed for timing RNG */
     483           0 :   uint rng_seed;
     484           0 :   if( FD_UNLIKELY( !fd_rng_secure( &rng_seed, sizeof(uint) ) ) ) {
     485           0 :     FD_LOG_CRIT(( "fd_rng_secure failed" ));
     486           0 :   }
     487           0 :   if( FD_UNLIKELY( !fd_rng_join( fd_rng_new( &ctx->rng, rng_seed, 0UL ) ) ) ) {
     488           0 :     FD_LOG_CRIT(( "fd_rng_join failed" )); /* unreachable */
     489           0 :   }
     490           0 : }
     491             : 
     492             : static fd_bundle_out_ctx_t
     493             : bundle_out_link( fd_topo_t const *      topo,
     494             :                  fd_topo_link_t const * link,
     495           0 :                  ulong                  out_link_idx ) {
     496           0 :   fd_bundle_out_ctx_t out = {0};
     497           0 :   out.idx    = out_link_idx;
     498           0 :   out.mem    = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
     499           0 :   out.chunk0 = fd_dcache_compact_chunk0( out.mem, link->dcache );
     500           0 :   out.wmark  = fd_dcache_compact_wmark ( out.mem, link->dcache, link->mtu );
     501           0 :   out.chunk  = out.chunk0;
     502           0 :   return out;
     503           0 : }
     504             : 
     505             : static void
     506             : unprivileged_init( fd_topo_t *      topo,
     507           0 :                    fd_topo_tile_t * tile ) {
     508           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     509           0 :   if( FD_UNLIKELY( tile->kind_id!=0 ) ) {
     510           0 :     FD_LOG_ERR(( "There can only be one bundle tile" ));
     511           0 :   }
     512             : 
     513           0 :   ulong sign_in_idx = fd_topo_find_tile_in_link( topo, tile, "sign_bundle", tile->kind_id );
     514           0 :   if( FD_UNLIKELY( sign_in_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing sign_bundle link" ));
     515           0 :   fd_topo_link_t const * sign_in  = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
     516             : 
     517           0 :   ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_sign", tile->kind_id );
     518           0 :   if( FD_UNLIKELY( sign_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_sign link" ));
     519           0 :   fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
     520             : 
     521           0 :   if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new(
     522           0 :       ctx->keyguard_client,
     523           0 :       sign_out->mcache,
     524           0 :       sign_out->dcache,
     525           0 :       sign_in->mcache,
     526           0 :       sign_in->dcache
     527           0 :   ) ) ) ) {
     528           0 :     FD_LOG_ERR(( "fd_keyguard_client_join failed" )); /* unreachable */
     529           0 :   }
     530             : 
     531           0 :   ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->keyswitch_obj_id ) );
     532           0 :   FD_TEST( ctx->keyswitch );
     533             : 
     534           0 :   ulong verify_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_verif", tile->kind_id );
     535           0 :   if( FD_UNLIKELY( verify_out_idx==ULONG_MAX ) ) FD_LOG_ERR(( "Missing bundle_verif link" ));
     536           0 :   ctx->verify_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ verify_out_idx ] ], verify_out_idx );
     537             : 
     538           0 :   ulong plugin_out_idx = fd_topo_find_tile_out_link( topo, tile, "bundle_plugi", tile->kind_id );
     539           0 :   if( plugin_out_idx!=ULONG_MAX ) {
     540           0 :     ctx->plugin_out = bundle_out_link( topo, &topo->links[ tile->out_link_id[ plugin_out_idx ] ], plugin_out_idx );
     541           0 :   } else {
     542           0 :     ctx->plugin_out = (fd_bundle_out_ctx_t){ .idx=ULONG_MAX };
     543           0 :   }
     544             : 
     545             :   /* Set socket receive buffer size */
     546           0 :   ulong so_rcvbuf = tile->bundle.buf_sz;
     547           0 :   if( FD_UNLIKELY( so_rcvbuf < 2048UL  ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too small" ));
     548           0 :   if( FD_UNLIKELY( so_rcvbuf > INT_MAX ) ) FD_LOG_ERR(( "Invalid [development.bundle.buffer_size_kib]: too large" ));
     549           0 :   ctx->so_rcvbuf = (int)so_rcvbuf;
     550             : 
     551             :   /* Set idle ping timer */
     552           0 :   ctx->ping_threshold_ticks = fd_ulong_pow2_up( (ulong)
     553           0 :       ( (double)tile->bundle.keepalive_interval_nanos * fd_tempo_tick_per_ns( NULL ) ) );
     554           0 :   ctx->ping_randomize = fd_rng_ulong( ctx->rng );
     555             : 
     556           0 :   ctx->bundle_status_plugin = 127;
     557           0 :   ctx->bundle_status_recent = FD_PLUGIN_MSG_BLOCK_ENGINE_UPDATE_STATUS_DISCONNECTED;
     558           0 :   ctx->last_bundle_status_log_nanos = fd_log_wallclock();
     559             : 
     560           0 :   fd_bundle_tile_parse_endpoint( ctx, tile );
     561             : 
     562           0 :   ctx->grpc_client = fd_grpc_client_new( ctx->grpc_client_mem, &fd_bundle_client_grpc_callbacks, ctx->grpc_metrics, ctx, ctx->grpc_buf_max, ctx->map_seed );
     563           0 :   if( FD_UNLIKELY( !ctx->grpc_client ) ) {
     564           0 :     FD_LOG_CRIT(( "fd_grpc_client_new failed" )); /* unreachable */
     565           0 :   }
     566           0 :   fd_grpc_client_set_version( ctx->grpc_client, fdctl_version_string, strlen( fdctl_version_string ) );
     567           0 :   fd_grpc_client_set_authority( ctx->grpc_client, ctx->server_sni, ctx->server_sni_len, ctx->server_tcp_port );
     568           0 : }
     569             : 
     570             : static ulong
     571             : populate_allowed_seccomp( fd_topo_t const *      topo,
     572             :                           fd_topo_tile_t const * tile,
     573             :                           ulong                  out_cnt,
     574           0 :                           struct sock_filter *   out ) {
     575           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     576           0 :   populate_sock_filter_policy_fd_bundle_tile(
     577           0 :       out_cnt, out,
     578           0 :       (uint)fd_log_private_logfile_fd(),
     579           0 :       (uint)ctx->keylog_fd,
     580           0 :       (uint)ctx->netdb_fds->etc_hosts,
     581           0 :       (uint)ctx->netdb_fds->etc_resolv_conf
     582           0 :   );
     583           0 :   return sock_filter_policy_fd_bundle_tile_instr_cnt;
     584           0 : }
     585             : 
     586             : static ulong
     587             : populate_allowed_fds( fd_topo_t const *      topo,
     588             :                       fd_topo_tile_t const * tile,
     589             :                       ulong                  out_fds_cnt,
     590           0 :                       int *                  out_fds ) {
     591           0 :   fd_bundle_tile_t * ctx = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     592             : 
     593           0 :   if( FD_UNLIKELY( out_fds_cnt<5UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     594             : 
     595           0 :   ulong out_cnt = 0UL;
     596           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     597           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     598           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     599           0 :   if( FD_LIKELY( ctx->netdb_fds->etc_hosts >= 0 ) )
     600           0 :     out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_hosts;
     601           0 :   out_fds[ out_cnt++ ] = ctx->netdb_fds->etc_resolv_conf;
     602           0 :   if( FD_UNLIKELY( ctx->keylog_fd>=0 ) )
     603           0 :     out_fds[ out_cnt++ ] = ctx->keylog_fd;
     604           0 :   return out_cnt;
     605           0 : }
     606             : 
     607           0 : #define STEM_BURST (5UL)
     608             : 
     609           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_bundle_tile_t
     610           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bundle_tile_t)
     611             : 
     612           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING fd_bundle_tile_housekeeping
     613           0 : #define STEM_CALLBACK_METRICS_WRITE       metrics_write
     614           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     615             : 
     616             : #include "../stem/fd_stem.c"
     617             : 
     618             : fd_topo_run_tile_t fd_tile_bundle = {
     619             :   .name                     = "bundle",
     620             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     621             :   .populate_allowed_fds     = populate_allowed_fds,
     622             :   .scratch_align            = scratch_align,
     623             :   .scratch_footprint        = scratch_footprint,
     624             :   .loose_footprint          = loose_footprint,
     625             :   .privileged_init          = privileged_init,
     626             :   .unprivileged_init        = unprivileged_init,
     627             :   .run                      = stem_run,
     628             :   .rlimit_file_cnt          = 64,
     629             :   .keep_host_networking     = 1,
     630             :   .allow_connect            = 1
     631             : };

Generated by: LCOV version 1.14