Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_topo.h"
3 :
4 : #include "../waker/fd_waker.h"
5 : #include "../metrics/fd_metrics.h"
6 : #include "../events/fd_event_report.h"
7 : #include "../../util/tile/fd_tile_private.h"
8 :
9 : #include <unistd.h>
10 : #include <errno.h>
11 : #include <fcntl.h>
12 : #include <limits.h>
13 : #include <pthread.h>
14 : #include <linux/futex.h>
15 : #include <sys/resource.h>
16 : #include <sys/prctl.h>
17 : #include <sys/stat.h>
18 : #include <sys/mman.h>
19 : #include <net/if.h>
20 :
21 : static void
22 : initialize_logging( char const * tile_name,
23 : ulong tile_kind_id,
24 0 : ulong tid ) {
25 0 : fd_log_cpu_set( NULL );
26 0 : fd_log_private_tid_set( tid );
27 0 : char thread_name[ 20 ];
28 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile_name, tile_kind_id ) );
29 0 : fd_log_thread_set( thread_name );
30 0 : fd_log_private_stack_discover( FD_TILE_PRIVATE_STACK_SZ,
31 0 : &fd_tile_private_stack0, &fd_tile_private_stack1 );
32 0 : FD_LOG_INFO(( "booting tile %s pid:%lu tid:%lu", thread_name, fd_log_group_id(), tid ));
33 :
34 : /* FD_LOG_* calls fd_log_wallclock_cstr, which calls localtime_r. In
35 : glibc, this ends up calling a function called tzset_internal. The
36 : first time tzset_internal is called by a process, it may (and
37 : almost always does) call __tzfile_read, which invokes the openat
38 : syscall, and possibly several others on the time zone file
39 : (typically /etc/localtime) or on a file in the time zone directory.
40 : This kind of behavior is tricky to sandbox, so the easiest thing to
41 : do is initialize it prior to the sandbox and hope whatever libc is
42 : used behaves like glibc. This only matters when both the logfile
43 : and stderr filters are strict enough so that the immediately prior
44 : FD_LOG_INFO call is a no-op, since otherwise that call would have
45 : taken care of it. */
46 0 : char wallclock[FD_LOG_WALLCLOCK_CSTR_BUF_SZ];
47 0 : fd_log_wallclock_cstr( 0L, wallclock );
48 0 : }
49 :
50 : void
51 : fd_topo_run_tile( fd_topo_t * topo,
52 : fd_topo_tile_t * tile,
53 : int sandbox,
54 : int keep_controlling_terminal,
55 : int core_dump_level,
56 : uint uid,
57 : uint gid,
58 : int allow_fd,
59 0 : fd_topo_run_tile_t * tile_run ) {
60 0 : char thread_name[ 20 ];
61 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile->name, tile->kind_id ) );
62 0 : if( FD_UNLIKELY( prctl( PR_SET_NAME, thread_name, 0, 0, 0 ) ) ) FD_LOG_ERR(( "prctl(PR_SET_NAME) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
63 :
64 0 : ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
65 0 : ulong tid = fd_sandbox_gettid(); /* Need to read /proc again.. we got a new TID from clone */
66 :
67 0 : initialize_logging( tile->name, tile->kind_id, tid );
68 :
69 : /* preload shared memory before sandboxing, so it is already mapped */
70 0 : fd_topo_join_tile_workspaces( topo, tile, core_dump_level );
71 :
72 0 : if( FD_UNLIKELY( tile_run->privileged_init ) )
73 0 : tile_run->privileged_init( topo, tile );
74 :
75 0 : ulong allow_fds_offset = 0UL;
76 0 : int allow_fds[ FD_SANDBOX_ALLOWED_FD_CNT_MAX ];
77 0 : for( ulong i=0UL; i<FD_SANDBOX_ALLOWED_FD_CNT_MAX; i++ ) allow_fds[ i ] = -1;
78 0 : if( FD_LIKELY( -1!=allow_fd ) ) {
79 0 : allow_fds_offset = 1UL;
80 0 : allow_fds[ 0 ] = allow_fd;
81 0 : }
82 0 : ulong allow_fds_cnt = 0UL;
83 0 : if( FD_LIKELY( tile_run->populate_allowed_fds ) ) {
84 0 : allow_fds_cnt = tile_run->populate_allowed_fds( topo,
85 0 : tile,
86 0 : FD_SANDBOX_ALLOWED_FD_CNT_MAX-allow_fds_offset,
87 0 : allow_fds+allow_fds_offset );
88 0 : }
89 :
90 :
91 0 : struct sock_filter seccomp_filter[ 256UL ];
92 0 : ulong seccomp_filter_cnt = 0UL;
93 0 : if( FD_LIKELY( tile_run->populate_allowed_seccomp ) ) {
94 0 : seccomp_filter_cnt = tile_run->populate_allowed_seccomp( topo,
95 0 : tile,
96 0 : sizeof(seccomp_filter)/sizeof(seccomp_filter[ 0 ]),
97 0 : seccomp_filter );
98 0 : }
99 :
100 0 : ulong rlimit_file_cnt = tile_run->rlimit_file_cnt;
101 0 : if( tile_run->rlimit_file_cnt_fn ) {
102 0 : rlimit_file_cnt = tile_run->rlimit_file_cnt_fn( topo, tile );
103 0 : }
104 :
105 0 : if( FD_LIKELY( sandbox ) ) {
106 0 : int dumpable = core_dump_level == FD_TOPO_CORE_DUMP_LEVEL_DISABLED ? 0 : 1;
107 0 : fd_sandbox_enter( uid,
108 0 : gid,
109 0 : tile_run->keep_host_networking,
110 0 : tile_run->allow_connect,
111 0 : tile_run->allow_renameat,
112 0 : keep_controlling_terminal,
113 0 : dumpable,
114 0 : rlimit_file_cnt,
115 0 : tile_run->rlimit_address_space,
116 0 : tile_run->rlimit_data,
117 0 : tile_run->rlimit_nproc,
118 0 : allow_fds_cnt+allow_fds_offset,
119 0 : allow_fds,
120 0 : seccomp_filter_cnt,
121 0 : seccomp_filter );
122 0 : } else {
123 0 : fd_sandbox_switch_uid_gid( uid, gid );
124 0 : }
125 :
126 : /* Now we are sandboxed, join all the tango IPC objects in the workspaces */
127 0 : fd_topo_fill_tile( topo, tile );
128 :
129 0 : FD_TEST( tile->metrics );
130 0 : fd_metrics_register( tile->metrics );
131 0 : fd_event_register( topo, tile );
132 :
133 0 : FD_MGAUGE_SET( TILE, PID, pid );
134 0 : FD_MGAUGE_SET( TILE, TID, tid );
135 :
136 0 : if( FD_UNLIKELY( tile_run->unprivileged_init ) )
137 0 : tile_run->unprivileged_init( topo, tile );
138 :
139 0 : tile_run->run( topo, tile );
140 0 : if( FD_UNLIKELY( !tile->allow_shutdown ) ) FD_LOG_ERR(( "tile %s:%lu run loop returned", tile->name, tile->kind_id ));
141 :
142 0 : FD_MGAUGE_SET( TILE, STATUS, 2UL );
143 0 : }
144 :
145 : typedef struct {
146 : fd_topo_t * topo;
147 : fd_topo_tile_t * tile;
148 : fd_topo_run_tile_t tile_run;
149 : uint uid;
150 : uint gid;
151 : volatile int copied;
152 : void * stack_lo;
153 : void * stack_hi;
154 : } fd_topo_run_thread_args_t;
155 :
156 : static void *
157 0 : run_tile_thread_main( void * _args ) {
158 0 : fd_topo_run_thread_args_t args = *(fd_topo_run_thread_args_t *)_args;
159 0 : FD_COMPILER_MFENCE();
160 0 : ((fd_topo_run_thread_args_t *)_args)->copied = 1;
161 0 : FD_COMPILER_MFENCE();
162 :
163 : /* Prevent fork() from smashing the stack */
164 0 : if( FD_UNLIKELY( madvise( args.stack_lo, (ulong)args.stack_hi - (ulong)args.stack_lo, MADV_DONTFORK ) ) ) {
165 0 : FD_LOG_ERR(( "madvise(stack,MADV_DONTFORK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
166 0 : }
167 :
168 0 : fd_topo_run_tile( args.topo, args.tile, 0, 1, 1, args.uid, args.gid, -1, &args.tile_run );
169 0 : FD_TEST( args.tile->allow_shutdown );
170 0 : return NULL;
171 0 : }
172 :
173 : /* fd_topo_tile_stack_join_anon is a variant of fd_topo_tile_stack_join
174 : that acquires private anonymous memory instead of shared pages.
175 :
176 : This is required for fork() to work, as the parent and child process
177 : would otherwise share a stack and corrupt each other. While fork()
178 : is banned in tile user code, some dynamic analysis tools (like MSan)
179 : unfortunately rely on it. */
180 :
181 : FD_FN_UNUSED static void *
182 0 : fd_topo_tile_stack_join_anon( void ) {
183 0 :
184 0 : ulong sz = 2*FD_TILE_PRIVATE_STACK_SZ;
185 0 : int prot = PROT_READ|PROT_WRITE;
186 0 : int flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK;
187 0 :
188 0 : uchar * stack = MAP_FAILED;
189 0 : #if !FD_HAS_ASAN && !FD_HAS_MSAN
190 0 : stack = mmap( NULL, sz, prot, flags|MAP_HUGETLB, -1, 0 );
191 0 : #endif
192 0 :
193 0 : if( stack==MAP_FAILED ) {
194 0 : stack = mmap( NULL, sz, prot, flags, -1, 0 );
195 0 : if( FD_UNLIKELY( stack==MAP_FAILED ) ) {
196 0 : FD_LOG_ERR(( "mmap() for stack failed (%i-%s)", errno, fd_io_strerror( errno ) ));
197 0 : }
198 0 : }
199 0 :
200 0 : /* Create the guard regions in the extra space */
201 0 : void * guard_lo = (void *)( stack - FD_SHMEM_NORMAL_PAGE_SZ );
202 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
203 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
204 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_lo, errno, fd_io_strerror( errno ) ));
205 0 :
206 0 : void * guard_hi = (void *)( stack + FD_TILE_PRIVATE_STACK_SZ );
207 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
208 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
209 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_hi, errno, fd_io_strerror( errno ) ));
210 0 :
211 0 : return stack;
212 0 : }
213 :
214 : void *
215 : fd_topo_tile_stack_join( char const * app_name,
216 : char const * tile_name,
217 0 : ulong tile_kind_id ) {
218 : #if FD_HAS_MSAN
219 : return fd_topo_tile_stack_join_anon();
220 : #endif
221 :
222 0 : char name[ PATH_MAX ];
223 0 : FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", app_name, tile_name, tile_kind_id ) );
224 :
225 0 : int dump = strcmp( tile_name, "sign" ) ? 1 : 0; /* avoid core dumps of sign tile stacks */
226 0 : uchar * stack = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, dump, NULL, NULL, NULL );
227 0 : if( FD_UNLIKELY( !stack ) ) FD_LOG_ERR(( "fd_shmem_join failed" ));
228 :
229 : /* Make space for guard lo and guard hi */
230 0 : if( FD_UNLIKELY( fd_shmem_release( stack, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
231 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
232 0 : stack += FD_SHMEM_HUGE_PAGE_SZ;
233 0 : if( FD_UNLIKELY( fd_shmem_release( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
234 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
235 :
236 : /* Create the guard regions in the extra space */
237 0 : void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
238 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
239 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
240 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
241 :
242 0 : void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
243 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
244 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
245 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
246 :
247 0 : return stack;
248 0 : }
249 :
250 : static inline void
251 : run_tile_thread( fd_topo_t * topo,
252 : fd_topo_tile_t * tile,
253 : fd_topo_run_tile_t tile_run,
254 : uint uid,
255 : uint gid,
256 : fd_cpuset_t const * float_cpu_set,
257 : fd_cpuset_t const * floating_cpu_set,
258 : int floating_priority,
259 0 : fd_topo_run_thread_args_t * args ) {
260 : /* tpool will assign a thread later */
261 0 : if( FD_UNLIKELY( tile_run.for_tpool ) ) return;
262 0 : void * stack = fd_topo_tile_stack_join( topo->app_name, tile->name, tile->kind_id );
263 :
264 0 : pthread_attr_t attr[ 1 ];
265 0 : int err = pthread_attr_init( attr );
266 0 : if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "pthread_attr_init() failed (%i-%s)", err, fd_io_strerror( err ) ));
267 0 : err = pthread_attr_setstack( attr, stack, FD_TILE_PRIVATE_STACK_SZ );
268 0 : if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "pthread_attr_setstack() failed (%i-%s)", err, fd_io_strerror( err ) ));
269 :
270 0 : FD_CPUSET_DECL( cpu_set );
271 0 : if( FD_LIKELY( tile->cpu_idx<65535UL ) ) {
272 : /* set the thread affinity before we clone the new process to ensure
273 : kernel first touch happens on the desired thread. */
274 0 : if( FD_UNLIKELY( tile->floats ) ) {
275 0 : ulong numa_idx = fd_shmem_numa_idx( tile->cpu_idx );
276 0 : for( ulong cpu=0UL; cpu<FD_TILE_MAX; cpu++ )
277 0 : if( fd_cpuset_test( float_cpu_set, cpu ) && fd_shmem_numa_idx( cpu )==numa_idx ) fd_cpuset_insert( cpu_set, cpu );
278 0 : }
279 0 : if( FD_UNLIKELY( !fd_cpuset_cnt( cpu_set ) ) ) fd_cpuset_insert( cpu_set, tile->cpu_idx );
280 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
281 0 : } else {
282 0 : fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
283 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
284 0 : }
285 :
286 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
287 0 : if( FD_LIKELY( errno==EINVAL ) ) {
288 0 : FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
289 0 : "you have specified for this tile in [layout.affinity] of your configuration file contains a "
290 0 : "CPU (%lu) which does not exist on this machine.",
291 0 : tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
292 0 : } else {
293 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
294 0 : }
295 0 : }
296 :
297 0 : *args = (fd_topo_run_thread_args_t) {
298 0 : .topo = topo,
299 0 : .tile = tile,
300 0 : .tile_run = tile_run,
301 0 : .uid = uid,
302 0 : .gid = gid,
303 0 : .copied = 0,
304 0 : .stack_lo = stack,
305 0 : .stack_hi = (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ
306 0 : };
307 :
308 0 : pthread_t pthread;
309 0 : err = pthread_create( &pthread, attr, run_tile_thread_main, args );
310 0 : if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "pthread_create() failed (%i-%s)", err, fd_io_strerror( err ) ));
311 0 : }
312 :
313 : static void
314 0 : join_isolation_cgroup( char const * app_name ) {
315 0 : char path[ PATH_MAX ];
316 0 : FD_TEST( fd_cstr_printf_check( path, sizeof(path), NULL, "/sys/fs/cgroup/%s/cgroup.procs", app_name ) );
317 :
318 0 : int fd = open( path, O_WRONLY );
319 0 : if( FD_UNLIKELY( fd<0 ) ) {
320 0 : if( FD_LIKELY( errno==ENOENT ) ) return; /* cpuset stage not configured */
321 0 : FD_LOG_ERR(( "open(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
322 0 : }
323 :
324 0 : char pid[ 32 ];
325 0 : ulong pid_len;
326 0 : FD_TEST( fd_cstr_printf_check( pid, sizeof(pid), &pid_len, "%ld", (long)getpid() ) );
327 0 : if( FD_UNLIKELY( write( fd, pid, pid_len )!=(long)pid_len ) )
328 0 : FD_LOG_ERR(( "write(%s,\"%s\") failed (%i-%s)", path, pid, errno, fd_io_strerror( errno ) ));
329 0 : if( FD_UNLIKELY( close( fd ) ) ) FD_LOG_ERR(( "close(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
330 0 : }
331 :
332 : void
333 : fd_topo_run_single_process( fd_topo_t * topo,
334 : int agave,
335 : uint uid,
336 : uint gid,
337 0 : fd_topo_run_tile_t (* tile_run )( fd_topo_tile_t const * tile ) ) {
338 0 : join_isolation_cgroup( topo->app_name );
339 :
340 0 : if( FD_LIKELY( agave!=1 ) ) {
341 0 : ulong waker_client_cnt = 0UL;
342 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
343 0 : ulong idx = topo->tiles[ i ].waker_client_idx;
344 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) waker_client_cnt = fd_ulong_max( waker_client_cnt, idx+1UL );
345 0 : }
346 0 : fd_waker_install( waker_client_cnt );
347 0 : }
348 :
349 : /* Save the current affinity, it will be restored after creating any child tiles */
350 0 : FD_CPUSET_DECL( floating_cpu_set );
351 0 : if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
352 0 : FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
353 :
354 : /* The CPUs of the floating tiles (efficient mode): floaters share
355 : these among themselves and never a pinned tile's CPU */
356 0 : FD_CPUSET_DECL( float_cpu_set );
357 0 : int any_floats = 0;
358 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
359 0 : if( FD_LIKELY( !topo->tiles[ i ].floats ) ) continue;
360 0 : fd_cpuset_insert( float_cpu_set, topo->tiles[ i ].cpu_idx );
361 0 : any_floats = 1;
362 0 : }
363 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ )
364 0 : if( FD_LIKELY( !topo->tiles[ i ].floats && topo->tiles[ i ].cpu_idx!=ULONG_MAX ) ) fd_cpuset_remove( float_cpu_set, topo->tiles[ i ].cpu_idx );
365 :
366 0 : errno = 0;
367 0 : int save_priority = getpriority( PRIO_PROCESS, 0 );
368 0 : if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
369 :
370 0 : fd_topo_run_thread_args_t args[ FD_TOPO_MAX_TILES ];
371 :
372 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
373 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
374 0 : if( !agave && tile->is_agave ) continue;
375 0 : if( agave==1 && !tile->is_agave ) continue;
376 :
377 0 : fd_topo_run_tile_t run_tile = tile_run( tile );
378 0 : int floating_priority = ( any_floats && !strcmp( tile->name, "waker" ) ) ? -19 : save_priority; /* the waker delivers floaters' fd readiness: never behind them */
379 0 : run_tile_thread( topo, tile, run_tile, uid, gid, float_cpu_set, floating_cpu_set, floating_priority, &args[ i ] );
380 0 : }
381 :
382 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
383 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
384 0 : if( !agave && tile->is_agave ) continue;
385 0 : if( agave==1 && !tile->is_agave ) continue;
386 :
387 0 : while( !FD_VOLATILE( args[ i ].copied ) ) FD_SPIN_PAUSE();
388 0 : }
389 :
390 0 : fd_sandbox_switch_uid_gid( uid, gid );
391 :
392 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
393 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
394 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
395 0 : }
|