Prevent possible race condition

This commit is contained in:
topjohnwu 2020-04-01 22:40:59 -07:00
parent 5f1174de27
commit b28668e18d

View File

@ -148,21 +148,20 @@ void SARFirstStageInit::traced_exec_init() {
// Swap out init with bind mount // Swap out init with bind mount
xmount("tmpfs", "/dev", "tmpfs", 0, "mode=755"); 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); write(init, self.buf, self.sz);
close(init); close(init);
xmount("/dev/magisk", "/init", nullptr, MS_BIND, nullptr); xmount("/dev/magiskinit", "/init", nullptr, MS_BIND, nullptr);
xumount2("/dev", MNT_DETACH); xumount2("/dev", MNT_DETACH);
xptrace(PTRACE_DETACH, pid); // Establish socket for 2nd stage ack
// Start daemon for 2nd stage preparation
struct sockaddr_un sun{}; struct sockaddr_un sun{};
auto len = setup_sockaddr(&sun);
int sockfd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); 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); xlisten(sockfd, 1);
xptrace(PTRACE_DETACH, pid);
// Wait for second stage ack // Wait for second stage ack
int client = xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC); int client = xaccept4(sockfd, nullptr, nullptr, SOCK_CLOEXEC);