Skip to content

Commit a232e8f

Browse files
chadrakoshipilev
authored andcommitted
8325621: Improve jspawnhelper version checks
Reviewed-by: erikj, shade, rriggs, ihse
1 parent c879627 commit a232e8f

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

make/modules/java.base/Launcher.gmk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ ifeq ($(call isTargetOsType, unix), true)
7878
NAME := jspawnhelper, \
7979
SRC := $(TOPDIR)/src/$(MODULE)/unix/native/jspawnhelper, \
8080
OPTIMIZATION := LOW, \
81-
CFLAGS := $(CFLAGS_JDKEXE) -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \
81+
CFLAGS := $(CFLAGS_JDKEXE) $(VERSION_CFLAGS) \
82+
-I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \
8283
EXTRA_OBJECT_FILES := $(SUPPORT_OUTPUTDIR)/native/$(MODULE)/libjava/childproc$(OBJ_SUFFIX), \
8384
LDFLAGS := $(LDFLAGS_JDKEXE), \
8485
OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/modules_libs/$(MODULE), \

make/modules/java.base/lib/CoreLibraries.gmk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \
5959
CFLAGS := $(CFLAGS_JDKLIB) \
6060
$(LIBJAVA_CFLAGS), \
6161
jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \
62+
ProcessImpl_md.c_CFLAGS := $(VERSION_CFLAGS), \
6263
DISABLED_WARNINGS_gcc_ProcessImpl_md.c := unused-result, \
6364
LDFLAGS := $(LDFLAGS_JDKLIB) \
6465
$(call SET_SHARED_LIBRARY_ORIGIN), \

src/java.base/unix/native/jspawnhelper/jspawnhelper.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <signal.h>
3030
#include <stdio.h>
3131
#include <stdlib.h>
32+
#include <string.h>
3233
#include <unistd.h>
3334
#include <sys/types.h>
3435
#include <sys/stat.h>
@@ -50,6 +51,10 @@ extern int errno;
5051
#define ERR_PIPE 2
5152
#define ERR_ARGS 3
5253

54+
#ifndef VERSION_STRING
55+
#error VERSION_STRING must be defined
56+
#endif
57+
5358
void error (int fd, int err) {
5459
if (write (fd, &err, sizeof(err)) != sizeof(err)) {
5560
/* Not sure what to do here. I have no one to speak to. */
@@ -59,6 +64,7 @@ void error (int fd, int err) {
5964
}
6065

6166
void shutItDown() {
67+
fprintf(stdout, "jspawnhelper version %s\n", VERSION_STRING);
6268
fprintf(stdout, "This command is not for general use and should ");
6369
fprintf(stdout, "only be run as the result of a call to\n");
6470
fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java ");
@@ -141,19 +147,29 @@ int main(int argc, char *argv[]) {
141147
int r, fdinr, fdinw, fdout;
142148
sigset_t unblock_signals;
143149

144-
if (argc != 2) {
145-
shutItDown();
146-
}
147-
148150
#ifdef DEBUG
149151
jtregSimulateCrash(0, 4);
150152
#endif
151-
r = sscanf (argv[1], "%d:%d:%d", &fdinr, &fdinw, &fdout);
153+
154+
if (argc != 3) {
155+
fprintf(stdout, "Incorrect number of arguments: %d\n", argc);
156+
shutItDown();
157+
}
158+
159+
if (strcmp(argv[1], VERSION_STRING) != 0) {
160+
fprintf(stdout, "Incorrect Java version: %s\n", argv[1]);
161+
shutItDown();
162+
}
163+
164+
r = sscanf (argv[2], "%d:%d:%d", &fdinr, &fdinw, &fdout);
152165
if (r == 3 && fcntl(fdinr, F_GETFD) != -1 && fcntl(fdinw, F_GETFD) != -1) {
153166
fstat(fdinr, &buf);
154-
if (!S_ISFIFO(buf.st_mode))
167+
if (!S_ISFIFO(buf.st_mode)) {
168+
fprintf(stdout, "Incorrect input pipe\n");
155169
shutItDown();
170+
}
156171
} else {
172+
fprintf(stdout, "Incorrect FD array data: %s\n", argv[2]);
157173
shutItDown();
158174
}
159175

src/java.base/unix/native/libjava/ProcessImpl_md.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ Java_java_lang_ProcessImpl_init(JNIEnv *env, jclass clazz)
300300
#define WTERMSIG(status) ((status)&0x7F)
301301
#endif
302302

303+
#ifndef VERSION_STRING
304+
#error VERSION_STRING must be defined
305+
#endif
306+
303307
static const char *
304308
getBytes(JNIEnv *env, jbyteArray arr)
305309
{
@@ -488,7 +492,7 @@ spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath)
488492
pid_t resultPid;
489493
int i, offset, rval, bufsize, magic;
490494
char *buf, buf1[(3 * 11) + 3]; // "%d:%d:%d\0"
491-
char *hlpargs[3];
495+
char *hlpargs[4];
492496
SpawnInfo sp;
493497

494498
/* 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)
497501
snprintf(buf1, sizeof(buf1), "%d:%d:%d", c->childenv[0], c->childenv[1], c->fail[1]);
498502
/* NULL-terminated argv array.
499503
* argv[0] contains path to jspawnhelper, to follow conventions.
500-
* argv[1] contains the fd string as argument to jspawnhelper
504+
* argv[1] contains the version string as argument to jspawnhelper
505+
* argv[2] contains the fd string as argument to jspawnhelper
501506
*/
502507
hlpargs[0] = (char*)helperpath;
503-
hlpargs[1] = buf1;
504-
hlpargs[2] = NULL;
508+
hlpargs[1] = VERSION_STRING;
509+
hlpargs[2] = buf1;
510+
hlpargs[3] = NULL;
505511

506512
/* Following items are sent down the pipe to the helper
507513
* after it is spawned.

test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/*
2727
* @test
28-
* @bug 8325567
28+
* @bug 8325567 8325621
2929
* @requires (os.family == "linux") | (os.family == "aix") | (os.family == "mac")
3030
* @library /test/lib
3131
* @run driver JspawnhelperWarnings
@@ -47,11 +47,30 @@ private static void tryWithNArgs(int nArgs) throws Exception {
4747
OutputAnalyzer oa = new OutputAnalyzer(p);
4848
oa.shouldHaveExitValue(1);
4949
oa.shouldContain("This command is not for general use");
50+
if (nArgs != 2) {
51+
oa.shouldContain("Incorrect number of arguments");
52+
} else {
53+
oa.shouldContain("Incorrect Java version");
54+
}
55+
}
56+
57+
private static void testVersion() throws Exception {
58+
String[] args = new String[3];
59+
args[0] = Paths.get(System.getProperty("java.home"), "lib", "jspawnhelper").toString();
60+
args[1] = "wrongVersion";
61+
args[2] = "1:1:1";
62+
Process p = ProcessTools.startProcess("jspawnhelper", new ProcessBuilder(args));
63+
OutputAnalyzer oa = new OutputAnalyzer(p);
64+
oa.shouldHaveExitValue(1);
65+
oa.shouldContain("This command is not for general use");
66+
oa.shouldContain("Incorrect Java version: wrongVersion");
5067
}
5168

5269
public static void main(String[] args) throws Exception {
5370
for (int nArgs = 0; nArgs < 10; nArgs++) {
5471
tryWithNArgs(nArgs);
5572
}
73+
74+
testVersion();
5675
}
5776
}

0 commit comments

Comments
 (0)