8325621: Improve jspawnhelper version checks

Reviewed-by: erikj, shade, rriggs, ihse
This commit is contained in:
Chad Rakoczy 2024-03-14 13:26:03 +00:00 committed by Aleksey Shipilev
parent c879627dbd
commit a232e8fb4e
5 changed files with 55 additions and 12 deletions

View file

@ -29,6 +29,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -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();
}

View file

@ -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.