22
33bool KittyInjector::init (pid_t pid, EKittyMemOP eMemOp)
44{
5- bool isLocal64bit = !KittyMemoryEx::getMapsContain (getpid (), " /lib64/" ).empty ();
6- bool isRemote64bit = !KittyMemoryEx::getMapsContain (pid, " /lib64/" ).empty ();
7- if (isLocal64bit != isRemote64bit)
8- {
9- KITTY_LOGE (" KittyInjector: Injector is %sbit but target app is %sbit." ,
10- isLocal64bit ? " 64" : " 32" , isRemote64bit ? " 64" : " 32" );
11- return false ;
12- }
13-
145 if (_kMgr.get ())
156 _kMgr.reset ();
167
@@ -22,6 +13,15 @@ bool KittyInjector::init(pid_t pid, EKittyMemOP eMemOp)
2213 return false ;
2314 }
2415
16+ bool isLocal64bit = !KittyMemoryEx::getMapsContain (getpid (), " /lib64/" ).empty ();
17+ bool isRemote64bit = !KittyMemoryEx::getMapsContain (pid, " /lib64/" ).empty ();
18+ if (isLocal64bit != isRemote64bit)
19+ {
20+ KITTY_LOGE (" KittyInjector: Injector is %sbit but target app is %sbit." ,
21+ isLocal64bit ? " 64" : " 32" , isRemote64bit ? " 64" : " 32" );
22+ return false ;
23+ }
24+
2525 if (!_remote_syscall.init (_kMgr.get ()))
2626 {
2727 KITTY_LOGE (" KittyInjector: Failed to initialize remote syscall." );
@@ -57,18 +57,24 @@ bool KittyInjector::init(pid_t pid, EKittyMemOP eMemOp)
5757 return true ;
5858}
5959
60- uintptr_t KittyInjector::injectLibrary (std::string libPath, int flags)
60+ uintptr_t KittyInjector::injectLibrary (std::string libPath, int flags, bool use_memfd_dl )
6161{
6262 if (!_kMgr.get () || !_kMgr->isMemValid ())
6363 {
6464 KITTY_LOGE (" injectLibrary: Not initialized." );
6565 return 0 ;
6666 }
6767
68+ if (!_kMgr->trace .isAttached ())
69+ {
70+ KITTY_LOGE (" injectLibrary: Not attached." );
71+ return 0 ;
72+ }
73+
6874 errno = 0 ;
69- bool useMemfd = _remote_dlopen_ext && !(syscall (syscall_memfd_create_n) < 0 && errno == ENOSYS);
75+ bool canUseMemfd = use_memfd_dl && _remote_dlopen_ext && !(syscall (syscall_memfd_create_n) < 0 && errno == ENOSYS);
7076
71- if (!_remote_dlopen && !useMemfd )
77+ if (!_remote_dlopen && !canUseMemfd )
7278 {
7379 KITTY_LOGE (" injectLibrary: remote dlopen not found." );
7480 return 0 ;
@@ -126,17 +132,11 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
126132 }
127133 }
128134
129- if (!_kMgr->trace .Attach ())
130- {
131- KITTY_LOGE (" injectLibrary: Failed to attach." );
132- return 0 ;
133- }
134-
135135 uintptr_t remoteLibPath = _remote_syscall.rmmap_str (libPath);
136136 if (!remoteLibPath)
137137 {
138138 KITTY_LOGE (" injectLibrary: mmaping lib name failed, errno = %s." ,
139- _remote_syscall.getRemoteError ().c_str ());
139+ _remote_syscall.getRemoteError ().c_str ());
140140 return 0 ;
141141 }
142142
@@ -145,7 +145,7 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
145145 // native dlopen
146146 if (libHdr.e_machine == kNativeEM )
147147 {
148- if (useMemfd )
148+ if (canUseMemfd )
149149 {
150150 do
151151 {
@@ -159,7 +159,7 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
159159
160160 auto libBuf = libFile.toBuffer ();
161161
162- int rmemfd = _remote_syscall.rmemfd_create (rmemfd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING, libBuf. size () );
162+ int rmemfd = _remote_syscall.rmemfd_create (rmemfd_name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
163163 if (rmemfd <= 0 )
164164 {
165165 KITTY_LOGE (" injectLibrary: memfd_create failed, errno = %s." ,
@@ -176,16 +176,7 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
176176 break ;
177177 }
178178
179- // mmap remote memfd in our process
180- void *rshmem = mmap (nullptr , libBuf.size (), PROT_READ | PROT_WRITE, MAP_SHARED, rmemfdFile.FD (), 0 );
181- if (!rshmem)
182- {
183- KITTY_LOGE (" injectLibrary: Failed to map shared memfd file, errno = %s." , strerror (errno));
184- break ;
185- }
186- // copy lib to remote memfd
187- memcpy (rshmem, libBuf.data (), libBuf.size ());
188- munmap (rshmem, libBuf.size ());
179+ libFile.writeToFd (rmemfdFile.FD ());
189180
190181 // restrict further modifications to remote memfd
191182 _remote_syscall.rmemfd_seal (rmemfd, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
@@ -205,11 +196,11 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
205196
206197 if (!remoteContainsMap (memfd_rand))
207198 {
208- if (useMemfd )
199+ if (canUseMemfd )
209200 KITTY_LOGW (" android_dlopen_ext failed, using legacy dlopen..." );
210201
211- _kMgr->trace .callFunction (_remote_dlopen, 2 , remoteLibPath, flags);
212- kINJ_WAIT ;
202+ _kMgr->trace .callFunction (_remote_dlopen, 2 , remoteLibPath, flags);
203+ kINJ_WAIT ;
213204 }
214205 }
215206 // bridge dlopen
@@ -305,7 +296,5 @@ uintptr_t KittyInjector::injectLibrary(std::string libPath, int flags)
305296 // cleanup
306297 _remote_syscall.clearAllocatedMaps ();
307298
308- _kMgr->trace .Detach ();
309-
310299 return libBase;
311300}
0 commit comments