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