8297451: ProcessHandleImpl should assert privilege when modifying reaper thread

Reviewed-by: chegar, alanb
This commit is contained in:
Ryan Ernst 2022-11-26 20:04:59 +00:00 committed by Chris Hegarty
parent 99d3840d36
commit 50f9043c69
2 changed files with 36 additions and 7 deletions

View file

@ -100,7 +100,7 @@ final class ProcessHandleImpl implements ProcessHandle {
ThreadFactory threadFactory = grimReaper -> {
Thread t = InnocuousThread.newSystemThread("process reaper", grimReaper,
stackSize, Thread.MAX_PRIORITY);
t.setDaemon(true);
privilegedThreadSetDaemon(t, true);
return t;
};
@ -115,6 +115,22 @@ final class ProcessHandleImpl implements ProcessHandle {
}
}
@SuppressWarnings("removal")
private static void privilegedThreadSetName(Thread thread, String name) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
thread.setName(name);
return null;
});
}
@SuppressWarnings("removal")
private static void privilegedThreadSetDaemon(Thread thread, boolean on) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
thread.setDaemon(on);
return null;
});
}
/**
* Returns a CompletableFuture that completes with process exit status when
* the process completes.
@ -140,8 +156,9 @@ final class ProcessHandleImpl implements ProcessHandle {
processReaperExecutor.execute(new Runnable() {
// Use inner class to avoid lambda stack overhead
public void run() {
String threadName = Thread.currentThread().getName();
Thread.currentThread().setName("process reaper (pid " + pid + ")");
Thread t = Thread.currentThread();
String threadName = t.getName();
privilegedThreadSetName(t, "process reaper (pid " + pid + ")");
try {
int exitValue = waitForProcessExit0(pid, shouldReap);
if (exitValue == NOT_A_CHILD) {
@ -172,7 +189,7 @@ final class ProcessHandleImpl implements ProcessHandle {
completions.remove(pid, newCompletion);
} finally {
// Restore thread name
Thread.currentThread().setName(threadName);
privilegedThreadSetName(t, threadName);
}
}
});