From a232e8fb4e6e9e2e9a5285bf01c93b8d1d995f04 Mon Sep 17 00:00:00 2001 From: Chad Rakoczy Date: Thu, 14 Mar 2024 13:26:03 +0000 Subject: [PATCH] 8325621: Improve jspawnhelper version checks Reviewed-by: erikj, shade, rriggs, ihse --- make/modules/java.base/Launcher.gmk | 3 +- make/modules/java.base/lib/CoreLibraries.gmk | 1 + .../unix/native/jspawnhelper/jspawnhelper.c | 28 +++++++++++++++---- .../unix/native/libjava/ProcessImpl_md.c | 14 +++++++--- .../ProcessBuilder/JspawnhelperWarnings.java | 21 +++++++++++++- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/make/modules/java.base/Launcher.gmk b/make/modules/java.base/Launcher.gmk index a559322f9d3..f6a68affb61 100644 --- a/make/modules/java.base/Launcher.gmk +++ b/make/modules/java.base/Launcher.gmk @@ -78,7 +78,8 @@ ifeq ($(call isTargetOsType, unix), true) NAME := jspawnhelper, \ SRC := $(TOPDIR)/src/$(MODULE)/unix/native/jspawnhelper, \ OPTIMIZATION := LOW, \ - CFLAGS := $(CFLAGS_JDKEXE) -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \ + CFLAGS := $(CFLAGS_JDKEXE) $(VERSION_CFLAGS) \ + -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \ EXTRA_OBJECT_FILES := $(SUPPORT_OUTPUTDIR)/native/$(MODULE)/libjava/childproc$(OBJ_SUFFIX), \ LDFLAGS := $(LDFLAGS_JDKEXE), \ OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/modules_libs/$(MODULE), \ diff --git a/make/modules/java.base/lib/CoreLibraries.gmk b/make/modules/java.base/lib/CoreLibraries.gmk index 48d56895297..a3c62d1eb16 100644 --- a/make/modules/java.base/lib/CoreLibraries.gmk +++ b/make/modules/java.base/lib/CoreLibraries.gmk @@ -59,6 +59,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \ CFLAGS := $(CFLAGS_JDKLIB) \ $(LIBJAVA_CFLAGS), \ jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \ + ProcessImpl_md.c_CFLAGS := $(VERSION_CFLAGS), \ DISABLED_WARNINGS_gcc_ProcessImpl_md.c := unused-result, \ LDFLAGS := $(LDFLAGS_JDKLIB) \ $(call SET_SHARED_LIBRARY_ORIGIN), \ diff --git a/src/java.base/unix/native/jspawnhelper/jspawnhelper.c b/src/java.base/unix/native/jspawnhelper/jspawnhelper.c index 974329a2857..1b4236b2150 100644 --- a/src/java.base/unix/native/jspawnhelper/jspawnhelper.c +++ b/src/java.base/unix/native/jspawnhelper/jspawnhelper.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,10 @@ extern int errno; #define ERR_PIPE 2 #define ERR_ARGS 3 +#ifndef VERSION_STRING +#error VERSION_STRING must be defined +#endif + void error (int fd, int err) { if (write (fd, &err, sizeof(err)) != sizeof(err)) { /* Not sure what to do here. I have no one to speak to. */ @@ -59,6 +64,7 @@ void error (int fd, int err) { } void shutItDown() { + fprintf(stdout, "jspawnhelper version %s\n", VERSION_STRING); fprintf(stdout, "This command is not for general use and should "); fprintf(stdout, "only be run as the result of a call to\n"); fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java "); @@ -141,19 +147,29 @@ int main(int argc, char *argv[]) { int r, fdinr, fdinw, fdout; sigset_t unblock_signals; - if (argc != 2) { - shutItDown(); - } - #ifdef DEBUG jtregSimulateCrash(0, 4); #endif - r = sscanf (argv[1], "%d:%d:%d", &fdinr, &fdinw, &fdout); + + if (argc != 3) { + fprintf(stdout, "Incorrect number of arguments: %d\n", argc); + shutItDown(); + } + + if (strcmp(argv[1], VERSION_STRING) != 0) { + fprintf(stdout, "Incorrect Java version: %s\n", argv[1]); + shutItDown(); + } + + r = sscanf (argv[2], "%d:%d:%d", &fdinr, &fdinw, &fdout); if (r == 3 && fcntl(fdinr, F_GETFD) != -1 && fcntl(fdinw, F_GETFD) != -1) { fstat(fdinr, &buf); - if (!S_ISFIFO(buf.st_mode)) + if (!S_ISFIFO(buf.st_mode)) { + fprintf(stdout, "Incorrect input pipe\n"); shutItDown(); + } } else { + fprintf(stdout, "Incorrect FD array data: %s\n", argv[2]); shutItDown(); } diff --git a/src/java.base/unix/native/libjava/ProcessImpl_md.c b/src/java.base/unix/native/libjava/ProcessImpl_md.c index 9ed0ed30959..558882f61e1 100644 --- a/src/java.base/unix/native/libjava/ProcessImpl_md.c +++ b/src/java.base/unix/native/libjava/ProcessImpl_md.c @@ -300,6 +300,10 @@ Java_java_lang_ProcessImpl_init(JNIEnv *env, jclass clazz) #define WTERMSIG(status) ((status)&0x7F) #endif +#ifndef VERSION_STRING +#error VERSION_STRING must be defined +#endif + static const char * getBytes(JNIEnv *env, jbyteArray arr) { @@ -488,7 +492,7 @@ spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) pid_t resultPid; int i, offset, rval, bufsize, magic; char *buf, buf1[(3 * 11) + 3]; // "%d:%d:%d\0" - char *hlpargs[3]; + char *hlpargs[4]; SpawnInfo sp; /* need to tell helper which fd is for receiving the childstuff @@ -497,11 +501,13 @@ spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath) snprintf(buf1, sizeof(buf1), "%d:%d:%d", c->childenv[0], c->childenv[1], c->fail[1]); /* NULL-terminated argv array. * argv[0] contains path to jspawnhelper, to follow conventions. - * argv[1] contains the fd string as argument to jspawnhelper + * argv[1] contains the version string as argument to jspawnhelper + * argv[2] contains the fd string as argument to jspawnhelper */ hlpargs[0] = (char*)helperpath; - hlpargs[1] = buf1; - hlpargs[2] = NULL; + hlpargs[1] = VERSION_STRING; + hlpargs[2] = buf1; + hlpargs[3] = NULL; /* Following items are sent down the pipe to the helper * after it is spawned. diff --git a/test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java b/test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java index 0ff68186502..daffb4b8c84 100644 --- a/test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java +++ b/test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java @@ -25,7 +25,7 @@ /* * @test - * @bug 8325567 + * @bug 8325567 8325621 * @requires (os.family == "linux") | (os.family == "aix") | (os.family == "mac") * @library /test/lib * @run driver JspawnhelperWarnings @@ -47,11 +47,30 @@ public class JspawnhelperWarnings { OutputAnalyzer oa = new OutputAnalyzer(p); oa.shouldHaveExitValue(1); oa.shouldContain("This command is not for general use"); + if (nArgs != 2) { + oa.shouldContain("Incorrect number of arguments"); + } else { + oa.shouldContain("Incorrect Java version"); + } + } + + private static void testVersion() throws Exception { + String[] args = new String[3]; + args[0] = Paths.get(System.getProperty("java.home"), "lib", "jspawnhelper").toString(); + args[1] = "wrongVersion"; + args[2] = "1:1:1"; + Process p = ProcessTools.startProcess("jspawnhelper", new ProcessBuilder(args)); + OutputAnalyzer oa = new OutputAnalyzer(p); + oa.shouldHaveExitValue(1); + oa.shouldContain("This command is not for general use"); + oa.shouldContain("Incorrect Java version: wrongVersion"); } public static void main(String[] args) throws Exception { for (int nArgs = 0; nArgs < 10; nArgs++) { tryWithNArgs(nArgs); } + + testVersion(); } }