11#include < zygisk.hpp>
2+ #include < cstdlib>
23#include < cstring>
34#include < cerrno>
5+ #include < limits>
46#include < logging.h>
57#include < cstdio>
68#include < sys/socket.h>
@@ -177,28 +179,78 @@ class ZygiskModule : public zygisk::ModuleBase {
177179
178180static int dex_mem_fd = -1 ;
179181static size_t dex_size = 0 ;
180- static uid_t manager_uid = -1 , settings_uid = -1 ;
181- static char manager_process[kProcessNameMax ], settings_process[kProcessNameMax ];
182182
183- static void ReadApplicationInfo (const char * package, uid_t & uid, char * process) {
183+ struct AppIdentity {
184+ char package[kProcessNameMax ]{};
185+ uid_t uid = static_cast <uid_t >(-1 );
186+ char process[kProcessNameMax ]{};
187+ };
188+
189+ static AppIdentity manager, settings;
190+
191+ static bool ReadLine (const uint8_t * bytes, size_t size, size_t & offset, char * value,
192+ size_t value_size) {
193+ if (offset >= size || value_size == 0 ) {
194+ return false ;
195+ }
196+
197+ size_t end = offset;
198+ while (end < size && bytes[end] != ' \r ' && bytes[end] != ' \n ' ) {
199+ ++end;
200+ }
201+
202+ size_t length = end - offset;
203+ if (length == 0 || length >= value_size) {
204+ return false ;
205+ }
206+ memcpy (value, bytes + offset, length);
207+ value[length] = ' \0 ' ;
208+
209+ while (end < size && (bytes[end] == ' \r ' || bytes[end] == ' \n ' )) {
210+ ++end;
211+ }
212+ offset = end;
213+ return true ;
214+ }
215+
216+ static bool ReadApplicationInfo (const char * name, AppIdentity& identity) {
184217 char buf[PATH_MAX ];
185- snprintf (buf, PATH_MAX , " /data/adb/modules/%s/%s" , ZYGISK_MODULE_ID , package );
218+ snprintf (buf, PATH_MAX , " /data/adb/modules/%s/%s" , ZYGISK_MODULE_ID , name );
186219 auto file = Buffer (buf);
187220 auto bytes = file.data ();
188221 auto size = file.size ();
189222 if (bytes == nullptr || size == 0 ) {
190223 LOGW (" ReadApplicationInfo: failed to read %s" , buf);
191- return ;
224+ return false ;
192225 }
193- for (int i = 0 ; i < size; ++i) {
194- if (bytes[i] == ' \n ' ) {
195- memset (process, 0 , kProcessNameMax );
196- size_t process_size = std::min<size_t >(size - i - 1 , kProcessNameMax - 1 );
197- memcpy (process, bytes + i + 1 , process_size);
198- bytes[i] = 0 ;
199- uid = atoi ((char *)bytes);
200- break ;
201- }
226+
227+ char uid_string[32 ]{};
228+ size_t offset = 0 ;
229+ if (!ReadLine (bytes, size, offset, identity.package , sizeof (identity.package )) ||
230+ !ReadLine (bytes, size, offset, uid_string, sizeof (uid_string)) ||
231+ !ReadLine (bytes, size, offset, identity.process , sizeof (identity.process ))) {
232+ LOGW (" ReadApplicationInfo: invalid data in %s" , buf);
233+ return false ;
234+ }
235+
236+ char * end = nullptr ;
237+ errno = 0 ;
238+ unsigned long uid = strtoul (uid_string, &end, 10 );
239+ if (errno != 0 || end == uid_string || *end != ' \0 ' ||
240+ uid > std::numeric_limits<uid_t >::max ()) {
241+ LOGW (" ReadApplicationInfo: invalid uid in %s" , buf);
242+ return false ;
243+ }
244+ identity.uid = static_cast <uid_t >(uid);
245+ return true ;
246+ }
247+
248+ static void RefreshApplicationInfo () {
249+ if (manager.uid == static_cast <uid_t >(-1 )) {
250+ ReadApplicationInfo (MANAGER_APPLICATION_INFO , manager);
251+ }
252+ if (settings.uid == static_cast <uid_t >(-1 )) {
253+ ReadApplicationInfo (SETTINGS_APPLICATION_INFO , settings);
202254 }
203255}
204256
@@ -236,11 +288,11 @@ static bool PrepareCompanion() {
236288
237289 LOGI (" Companion: dex fd is %d" , dex_mem_fd);
238290
239- ReadApplicationInfo (MANAGER_APPLICATION_ID , manager_uid, manager_process );
240- ReadApplicationInfo (SETTINGS_APPLICATION_ID , settings_uid, settings_process );
291+ ReadApplicationInfo (MANAGER_APPLICATION_INFO , manager );
292+ ReadApplicationInfo (SETTINGS_APPLICATION_INFO , settings );
241293
242- LOGI (" Companion: SystemUI %d %s" , manager_uid, manager_process );
243- LOGI (" Companion: Settings %d %s" , settings_uid, settings_process );
294+ LOGI (" Companion: SystemUI %s % d %s" , manager. package , manager. uid , manager. process );
295+ LOGI (" Companion: Settings %s % d %s" , settings. package , settings. uid , settings. process );
244296
245297 result = true ;
246298
@@ -259,6 +311,8 @@ static void CompanionEntry(int socket) {
259311 return ;
260312 }
261313
314+ RefreshApplicationInfo ();
315+
262316 char process_name[kProcessNameMax ]{0 };
263317 Identity whoami;
264318
@@ -270,10 +324,10 @@ static void CompanionEntry(int socket) {
270324 read_full (socket, process_name, kProcessNameMax );
271325
272326 LOGI (" SuiCompanion: Checking app: uid=%d, process=%s" , uid, process_name);
273- if (uid == manager_uid && strcmp (process_name, manager_process ) == 0 ) {
327+ if (uid == manager. uid && strcmp (process_name, manager. process ) == 0 ) {
274328 whoami = Identity::SYSTEM_UI ;
275329 LOGI (" SuiCompanion: Matched SYSTEM_UI!" );
276- } else if (uid == settings_uid && strcmp (process_name, settings_process ) == 0 ) {
330+ } else if (uid == settings. uid && strcmp (process_name, settings. process ) == 0 ) {
277331 whoami = Identity::SETTINGS ;
278332 LOGI (" SuiCompanion: Matched SETTINGS!" );
279333 } else {
0 commit comments