2222import org .elasticsearch .logging .Logger ;
2323
2424import java .io .IOException ;
25+ import java .lang .reflect .InvocationTargetException ;
2526import java .nio .file .Files ;
2627import java .nio .file .Path ;
28+ import java .nio .file .attribute .FileAttribute ;
2729import java .util .Map ;
2830import java .util .function .Function ;
2931
@@ -144,19 +146,33 @@ private static String findAgentJar() {
144146 * @throws IllegalStateException if the entitlements system can't prevent an unauthorized action of our choosing
145147 */
146148 private static void selfTest () {
147- ensureCannotStartProcess ();
148- ensureCanCreateTempFile ();
149+ ensureCannotStartProcess (false );
150+ ensureCannotStartProcess (true );
151+ ensureCanCreateTempFile (false );
152+ ensureCanCreateTempFile (true );
149153 }
150154
151- private static void ensureCannotStartProcess () {
155+ private static void ensureCannotStartProcess (boolean useReflection ) {
152156 try {
153157 // The command doesn't matter; it doesn't even need to exist
154- new ProcessBuilder ("" ).start ();
158+ ProcessBuilder builder = new ProcessBuilder ("" );
159+ if (useReflection ) {
160+ try {
161+ var start = ProcessBuilder .class .getMethod ("start" );
162+ start .invoke (builder );
163+ } catch (InvocationTargetException e ) {
164+ throw e .getCause ();
165+ }
166+ } else {
167+ builder .start ();
168+ }
155169 } catch (NotEntitledException e ) {
156170 logger .debug ("Success: Entitlement protection correctly prevented process creation" );
157171 return ;
158172 } catch (IOException e ) {
159173 throw new IllegalStateException ("Failed entitlement protection self-test" , e );
174+ } catch (Throwable e ) {
175+ throw new IllegalStateException ("Error during entitlement protection self-test" , e );
160176 }
161177 throw new IllegalStateException ("Entitlement protection self-test was incorrectly permitted" );
162178 }
@@ -165,9 +181,15 @@ private static void ensureCannotStartProcess() {
165181 * Originally {@code Security.selfTest}.
166182 */
167183 @ SuppressForbidden (reason = "accesses jvm default tempdir as a self-test" )
168- private static void ensureCanCreateTempFile () {
184+ private static void ensureCanCreateTempFile (boolean useReflection ) {
169185 try {
170- Path p = Files .createTempFile (null , null );
186+ Path p ;
187+ if (useReflection ) {
188+ p = (Path ) Files .class .getMethod ("createTempFile" , String .class , String .class , FileAttribute [].class )
189+ .invoke (null , null , null , new FileAttribute <?>[0 ]);
190+ } else {
191+ p = Files .createTempFile (null , null );
192+ }
171193 p .toFile ().deleteOnExit ();
172194
173195 // Make an effort to clean up the file immediately; also, deleteOnExit leaves the file if the JVM exits abnormally.
0 commit comments