@@ -29,7 +29,6 @@ extern "C" {
2929
3030#include "pyconfig.h"
3131#include "internal/pycore_ceval.h"
32- #include <stddef.h>
3332
3433#ifdef __linux__
3534# include <elf.h>
@@ -61,6 +60,7 @@ extern "C" {
6160#endif
6261
6362#if defined(__APPLE__ ) && defined(TARGET_OS_OSX ) && TARGET_OS_OSX
63+ # include "internal/pycore_bitutils.h"
6464# include <libproc.h>
6565# include <mach-o/loader.h>
6666# include <mach/error.h>
@@ -297,7 +297,155 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) {
297297
298298#if defined(__APPLE__ ) && defined(TARGET_OS_OSX ) && TARGET_OS_OSX
299299
300- #include "remote_debug_macho.h"
300+ static uint32_t
301+ macho_u32 (uint32_t value , int swap )
302+ {
303+ return swap ? _Py_bswap32 (value ) : value ;
304+ }
305+
306+ static uint64_t
307+ macho_u64 (uint64_t value , int swap )
308+ {
309+ return swap ? _Py_bswap64 (value ) : value ;
310+ }
311+
312+ static uintptr_t
313+ macho_invalid_image (void )
314+ {
315+ PyErr_SetString (PyExc_RuntimeError , "Invalid or truncated Mach-O image" );
316+ return 0 ;
317+ }
318+
319+ static size_t
320+ macho_header_size (uint32_t magic )
321+ {
322+ switch (magic ) {
323+ case MH_MAGIC :
324+ case MH_CIGAM :
325+ return sizeof (struct mach_header );
326+ case MH_MAGIC_64 :
327+ case MH_CIGAM_64 :
328+ return sizeof (struct mach_header_64 );
329+ default :
330+ return 0 ;
331+ }
332+ }
333+
334+ static uintptr_t
335+ macho_section_address (const void * image , size_t image_size ,
336+ uintptr_t base , const char * name )
337+ {
338+ if (strlen (name ) > 16 ) {
339+ return 0 ;
340+ }
341+ struct mach_header header ;
342+ if (image_size < sizeof (header )) {
343+ return macho_invalid_image ();
344+ }
345+ memcpy (& header , image , sizeof (header ));
346+ int wide = header .magic == MH_MAGIC_64 || header .magic == MH_CIGAM_64 ;
347+ int swap = header .magic == MH_CIGAM || header .magic == MH_CIGAM_64 ;
348+ size_t header_size = macho_header_size (header .magic );
349+ if (header_size == 0 || image_size < header_size ) {
350+ return macho_invalid_image ();
351+ }
352+ size_t commands_size = macho_u32 (header .sizeofcmds , swap );
353+ if (commands_size > image_size - header_size ) {
354+ return macho_invalid_image ();
355+ }
356+
357+ const unsigned char * cursor = (const unsigned char * )image + header_size ;
358+ size_t remaining = commands_size ;
359+ uint64_t header_address = 0 , section_address = 0 ;
360+ int have_header = 0 , have_section = 0 ;
361+ uint32_t commands = macho_u32 (header .ncmds , swap );
362+ for (uint32_t index = 0 ; index < commands ; index ++ ) {
363+ struct load_command command ;
364+ if (remaining < sizeof (command )) {
365+ return macho_invalid_image ();
366+ }
367+ memcpy (& command , cursor , sizeof (command ));
368+ uint32_t kind = macho_u32 (command .cmd , swap );
369+ size_t length = macho_u32 (command .cmdsize , swap );
370+ if (length < sizeof (command ) || length > remaining ) {
371+ return macho_invalid_image ();
372+ }
373+ if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT )) {
374+ uint64_t address , file_offset , file_size ;
375+ uint32_t sections ;
376+ size_t segment_size , section_size ;
377+ int data_segment ;
378+ if (wide ) {
379+ struct segment_command_64 segment ;
380+ segment_size = sizeof (segment );
381+ section_size = sizeof (struct section_64 );
382+ if (length < segment_size ) {
383+ return macho_invalid_image ();
384+ }
385+ memcpy (& segment , cursor , segment_size );
386+ address = macho_u64 (segment .vmaddr , swap );
387+ file_offset = macho_u64 (segment .fileoff , swap );
388+ file_size = macho_u64 (segment .filesize , swap );
389+ sections = macho_u32 (segment .nsects , swap );
390+ data_segment = strncmp (segment .segname , "__DATA" , 16 ) == 0 ;
391+ }
392+ else {
393+ struct segment_command segment ;
394+ segment_size = sizeof (segment );
395+ section_size = sizeof (struct section );
396+ if (length < segment_size ) {
397+ return macho_invalid_image ();
398+ }
399+ memcpy (& segment , cursor , segment_size );
400+ address = macho_u32 (segment .vmaddr , swap );
401+ file_offset = macho_u32 (segment .fileoff , swap );
402+ file_size = macho_u32 (segment .filesize , swap );
403+ sections = macho_u32 (segment .nsects , swap );
404+ data_segment = strncmp (segment .segname , "__DATA" , 16 ) == 0 ;
405+ }
406+ if (sections > (length - segment_size ) / section_size ) {
407+ return macho_invalid_image ();
408+ }
409+ /* __PAGEZERO has no file contents and does not define the slide. */
410+ if (file_offset == 0 && file_size != 0 ) {
411+ header_address = address ;
412+ have_header = 1 ;
413+ }
414+ if (data_segment ) {
415+ const unsigned char * record = cursor + segment_size ;
416+ for (uint32_t item = 0 ; item < sections ;
417+ item ++ , record += section_size )
418+ {
419+ if (strncmp ((const char * )record , name , 16 ) != 0 ) {
420+ continue ;
421+ }
422+ if (wide ) {
423+ struct section_64 section ;
424+ memcpy (& section , record , sizeof (section ));
425+ section_address = macho_u64 (section .addr , swap );
426+ }
427+ else {
428+ struct section section ;
429+ memcpy (& section , record , sizeof (section ));
430+ section_address = macho_u32 (section .addr , swap );
431+ }
432+ have_section = 1 ;
433+ }
434+ }
435+ }
436+ cursor += length ;
437+ remaining -= length ;
438+ }
439+ if (!have_section ) {
440+ return 0 ;
441+ }
442+ if (!have_header || section_address < header_address ||
443+ section_address - header_address > UINTPTR_MAX - base )
444+ {
445+ return macho_invalid_image ();
446+ }
447+ return base + (uintptr_t )(section_address - header_address );
448+ }
301449
302450static uintptr_t
303451search_section_in_memory (proc_handle_t * handle , uintptr_t base ,
@@ -313,7 +461,6 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base,
313461 }
314462 int swap = header .magic == MH_CIGAM || header .magic == MH_CIGAM_64 ;
315463 size_t commands_size = macho_u32 (header .sizeofcmds , swap );
316- /* A corrupt remote header must not trigger an unbounded allocation. */
317464 const size_t max_commands_size = 16 * 1024 * 1024 ;
318465 if (commands_size > max_commands_size ||
319466 base > UINTPTR_MAX - header_size - commands_size )
@@ -334,7 +481,6 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base,
334481 return result ;
335482}
336483
337-
338484static mach_port_t
339485pid_to_task (pid_t pid )
340486{
@@ -415,8 +561,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
415561 return -1 ;
416562 }
417563
418- /* dyld_all_image_infos starts with version, count, and an array pointer.
419- * Decode the target layout rather than the host's pointer-bearing struct. */
564+ /* dyld_all_image_infos uses the target's pointer width. */
420565 size_t timestamp_offset = list -> pointer_size == 8 ? 184 : 104 ;
421566 size_t size = (size_t )Py_MIN (info .all_image_info_size ,
422567 timestamp_offset + sizeof (list -> timestamp ));
@@ -442,7 +587,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
442587 list -> images = (uintptr_t )images ;
443588 list -> timestamp = 0 ;
444589 if (version >= 15 ) {
445- /* Version 15 added infoArrayChangeTimestamp at these ABI offsets . */
590+ /* Version 15 added infoArrayChangeTimestamp. */
446591 if (size < timestamp_offset + sizeof (list -> timestamp )) {
447592 PyErr_SetString (PyExc_RuntimeError , "Truncated dyld image information" );
448593 return -1 ;
0 commit comments