From b28668e18d0370d05dce9f8e6263ee423df246ee Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 1 Apr 2020 22:40:59 -0700 Subject: [PATCH] Prevent possible race condition --- native/jni/init/twostage.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index e7e99461c..8fecbef4f 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -148,21 +148,20 @@ void SARFirstStageInit::traced_exec_init() { // Swap out init with bind mount xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); - int init = xopen("/dev/magisk", O_CREAT | O_WRONLY, 0750); + int init = xopen("/dev/magiskinit", O_CREAT | O_WRONLY, 0750); write(init, self.buf, self.sz); close(init); - xmount("/dev/magisk", "/init", nullptr, MS_BIND, nullptr); + xmount("/dev/magiskinit", "/init", nullptr, MS_BIND, nullptr); xumount2("/dev", MNT_DETACH); - xptrace(PTRACE_DETACH, pid); - - // Start daemon for 2nd stage preparation + // Establish socket for 2nd stage ack struct sockaddr_un sun{}; - auto len = setup_sockaddr(&sun); int sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); - xbind(sockfd, (struct sockaddr*) &sun, len); + xbind(sockfd, (struct sockaddr*) &sun, setup_sockaddr(&sun)); xlisten(sockfd, 1); + xptrace(PTRACE_DETACH, pid); + // Wait for second stage ack int client = xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC);