1616#include <sys/queue.h>
1717#include <unistd.h>
1818
19+ #ifdef __ANDROID__
20+ #include <android/dlext.h>
21+ #endif /* __ANDROID__ */
22+
1923#ifdef SUPPLEMENTAL__SYSCALL_RECORD
2024/*
2125 * SUPPLEMENTAL: syscall record without syscalls
@@ -681,6 +685,72 @@ static void setup_trampoline(void) {
681685 }
682686}
683687
688+ #ifdef __ANDROID__
689+ // REF: https://gist.github.com/khanhduytran0/faee2be9c8fd1282783b936156a03e1c
690+ static void * _libdl_handle = NULL ;
691+ static struct android_namespace_t * (* _create_namespace )(
692+ const char * , const char * , const char * , uint64_t , const char * ,
693+ struct android_namespace_t * ) = NULL ;
694+ static void * (* _dlopen_ext )(const char * , int ,
695+ const android_dlextinfo * ) = NULL ;
696+
697+ static void * get_libdl_handle (void ) {
698+ return _libdl_handle ? _libdl_handle : dlopen ("libdl.so" , RTLD_NOW );
699+ }
700+
701+ static void * create_namespace (const char * name , const char * ld_library_path ,
702+ const char * default_library_path , uint64_t type ,
703+ const char * permitted_when_isolated_path ,
704+ struct android_namespace_t * parent_namespace ) {
705+ if (!_create_namespace ) {
706+ void * handle = get_libdl_handle ();
707+ if (!handle ) {
708+ goto fallback ;
709+ }
710+
711+ _create_namespace = (struct android_namespace_t *
712+ (* )(const char * , const char * , const char * , uint64_t ,
713+ const char * , struct android_namespace_t * ))
714+ dlsym (handle , "android_create_namespace" );
715+ if (!_create_namespace ) {
716+ goto fallback ;
717+ }
718+ }
719+
720+ return _create_namespace (name , ld_library_path , default_library_path , type ,
721+ permitted_when_isolated_path , parent_namespace );
722+
723+ fallback :
724+ return NULL ;
725+ }
726+
727+ static void * dlopen_ext (const char * filename , int flags ,
728+ const android_dlextinfo * extinfo ) {
729+ if (!_dlopen_ext ) {
730+ void * handle = get_libdl_handle ();
731+ if (!handle ) {
732+ goto fallback ;
733+ }
734+
735+ _dlopen_ext =
736+ (void * (* )(const char * , int , const android_dlextinfo * ))dlsym (
737+ handle , "android_dlopen_ext" );
738+ if (!_dlopen_ext ) {
739+ goto fallback ;
740+ }
741+ }
742+
743+ if (extinfo == NULL ) {
744+ goto fallback ;
745+ }
746+
747+ return _dlopen_ext (filename , flags , extinfo );
748+
749+ fallback :
750+ return dlopen (filename , flags );
751+ }
752+ #endif /* __ANDROID__ */
753+
684754static void load_hook_lib (void ) {
685755 void * handle ;
686756 {
@@ -692,11 +762,22 @@ static void load_hook_lib(void) {
692762 return ;
693763 }
694764
695- #ifdef __GLIBC__
765+ #if defined( __GLIBC__ )
696766 handle = dlmopen (LM_ID_NEWLM , filename , RTLD_NOW | RTLD_LOCAL );
697- #else
767+ #elif defined(__ANDROID__ )
768+ struct android_namespace_t * ns = create_namespace (
769+ "hook-namespace" , NULL ,
770+ "/system/lib:/vendor/lib:/system/vendor/lib/hw/:/vendor/lib/hw" ,
771+ 0 /* ANDROID_NAMESPACE_TYPE_REGULAR */ , NULL , NULL );
772+
773+ android_dlextinfo extinfo = {
774+ .flags = ANDROID_DLEXT_USE_NAMESPACE ,
775+ .library_namespace = ns ,
776+ };
777+ handle = dlopen_ext (filename , RTLD_NOW | RTLD_LOCAL , & extinfo );
778+ #else /* !__GLIBC__ && !__ANDROID__ */
698779 handle = dlopen (filename , RTLD_NOW | RTLD_LOCAL );
699- #endif
780+ #endif /* __GLIBC__ || __ANDROID__ */
700781 if (!handle ) {
701782 fprintf (stderr , "dlopen/dlmopen failed: %s\n\n" , dlerror ());
702783 fprintf (
0 commit comments