Skip to content

Commit 2926e43

Browse files
committed
fixup! add exec spawning implementation
Ensure that no new file descriptors are racily opened by signal handlers in the child process.
1 parent 486c24c commit 2926e43

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

core/jni/com_android_internal_os_Zygote.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,18 @@ static jint com_android_internal_os_Zygote_nativeForkExec(JNIEnv* env, jclass,
31433143
// first, but 32-bit zygote won't have them set if this is the first app launch zygote command.
31443144
SetSignalHandlers();
31453145

3146+
sigset64_t full_sig_set;
3147+
sigfillset64(&full_sig_set);
3148+
3149+
sigset64_t prev_sig_set;
3150+
3151+
// ensure that no new file descriptors are racily opened by signal handlers in the child process
3152+
if (sigprocmask64(SIG_BLOCK, &full_sig_set, &prev_sig_set) != 0) {
3153+
ALOGE("sigprocmask64 failed before fork: %s", strerror(errno));
3154+
close(cmd_fd);
3155+
return -1;
3156+
}
3157+
31463158
// fork() runs bionic fork hooks which are unnecessary for this use-case
31473159
pid_t pid = _Fork();
31483160

@@ -3151,6 +3163,10 @@ static jint com_android_internal_os_Zygote_nativeForkExec(JNIEnv* env, jclass,
31513163
if (pid == -1) {
31523164
ALOGE("fork failed: %s", strerror(errno));
31533165
}
3166+
if (sigprocmask64(SIG_SETMASK, &prev_sig_set, nullptr) != 0) {
3167+
ALOGE("sigprocmask64 failed in parent after fork: %s", strerror(errno));
3168+
_exit(1);
3169+
}
31543170
close(cmd_fd);
31553171
if (is_environment_cloned) {
31563172
free_environ(environment);
@@ -3169,6 +3185,11 @@ static jint com_android_internal_os_Zygote_nativeForkExec(JNIEnv* env, jclass,
31693185
_exit(1);
31703186
}
31713187

3188+
if (sigprocmask64(SIG_SETMASK, &prev_sig_set, nullptr) != 0) {
3189+
async_safe_format_log(ANDROID_LOG_ERROR, "sigprocmask64 failed in child after fork: %#m", nullptr);
3190+
_exit(1);
3191+
}
3192+
31723193
#if defined(__aarch64__)
31733194
const int FLAG_COMPAT_VA_39_BIT = 1 << 30;
31743195
execveat(-1, argv[0], (char **) argv, environment, enable_compat_va_39_bit ? FLAG_COMPAT_VA_39_BIT : 0);

0 commit comments

Comments
 (0)