This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-daemon.git


The following commit(s) were added to refs/heads/master by this push:
     new 7e0da54e Enforce a 255 resolved entries maximum for JVMs, retaining 
the (#320)
7e0da54e is described below

commit 7e0da54e683f1cf6ae4f4a7c717c630fa88051c1
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Nov 16 06:43:02 2026 -0500

    Enforce a 255 resolved entries maximum for JVMs, retaining the (#320)
    
    256-pointer allocation. A 256th entry now produces an error and rejects
    the configuration, with cleanup.
    
    See src/native/unix/tests/README.txt
---
 .github/workflows/codeql-analysis.yml           |   1 +
 .gitignore                                      |   4 +
 pom.xml                                         |   3 +
 src/assembly/native-src.xml                     |   3 +
 src/assembly/src.xml                            |   3 +
 src/changes/changes.xml                         |   1 +
 src/native/unix/Makefile.in                     |   5 ++
 src/native/unix/configure.ac                    |   2 +-
 src/native/unix/native/home.c                   |  29 +++++-
 src/native/unix/{ => tests}/Makefile.in         |  29 +++---
 src/native/unix/tests/README.txt                |  41 +++++++++
 src/native/unix/{Makefile.in => tests/check.sh} |  31 +++----
 src/native/unix/tests/test_home.c               | 112 ++++++++++++++++++++++++
 13 files changed, 225 insertions(+), 39 deletions(-)

diff --git a/.github/workflows/codeql-analysis.yml 
b/.github/workflows/codeql-analysis.yml
index 615ba81a..0e2be19f 100644
--- a/.github/workflows/codeql-analysis.yml
+++ b/.github/workflows/codeql-analysis.yml
@@ -83,6 +83,7 @@ jobs:
         export LDFLAGS=-m64
         ./configure
         make
+        make check
 
     - name: Perform CodeQL Analysis
       uses: 
github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
diff --git a/.gitignore b/.gitignore
index cade0748..0f97f41e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,3 +25,7 @@
 # NetBeans files
 nb-configuration.xml
 nbactions.xml
+
+/src/native/unix/tests/Makefile
+/src/native/unix/tests/test_home
+/src/native/unix/tests/test_home.dSYM/
diff --git a/pom.xml b/pom.xml
index 7667eff8..f2779058 100644
--- a/pom.xml
+++ b/pom.xml
@@ -195,6 +195,9 @@
               <inputExclude>src/native/unix/config.nice</inputExclude>
               <inputExclude>src/native/unix/config.status</inputExclude>
               <inputExclude>src/native/unix/config.log</inputExclude>
+              <inputExclude>src/native/unix/tests/Makefile</inputExclude>
+              <inputExclude>src/native/unix/tests/test_home</inputExclude>
+              
<inputExclude>src/native/unix/tests/test_home.dSYM/**</inputExclude>
               <!-- autoconf cache - not included in release -->
               <inputExclude>src/native/unix/autom4te.cache/**</inputExclude>
             </inputExcludes>
diff --git a/src/assembly/native-src.xml b/src/assembly/native-src.xml
index 9ef41fbc..097eac0f 100644
--- a/src/assembly/native-src.xml
+++ b/src/assembly/native-src.xml
@@ -50,6 +50,9 @@
                 <exclude>native/*.o</exclude>
                 <exclude>native/Makefile</exclude>
                 <exclude>native/libservice.a</exclude>
+                <exclude>tests/Makefile</exclude>
+                <exclude>tests/test_home</exclude>
+                <exclude>tests/test_home.dSYM/**</exclude>
                 <!-- Excluded here, because processed below -->
                 <exclude>**/*.sh</exclude> 
                 <!-- Bug https://issues.apache.org/jira/browse/MASSEMBLY-462 
-->
diff --git a/src/assembly/src.xml b/src/assembly/src.xml
index 8d6552c1..5a2aa667 100644
--- a/src/assembly/src.xml
+++ b/src/assembly/src.xml
@@ -51,6 +51,9 @@
                 <exclude>native/unix/native/*.o</exclude>
                 <exclude>native/unix/native/Makefile</exclude>
                 <exclude>native/unix/native/libservice.a</exclude>
+                <exclude>native/unix/tests/Makefile</exclude>
+                <exclude>native/unix/tests/test_home</exclude>
+                <exclude>native/unix/tests/test_home.dSYM/**</exclude>
                 <exclude>native/windows/apps/prunmgr/*_GUI_RELEASE/**</exclude>
                 <exclude>native/windows/apps/prunsrv/*_RELEASE/**</exclude>
                 <!-- Excluded here, because processed below -->
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b0b53caa..7e67cb32 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -43,6 +43,7 @@
     <release version="1.6.2" date="YYYY-MM-DD" description="Bug fix release">
       <!-- FIX -->
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix native build 
with C23 boolean keywords (#319).</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">jsvc. Prevent a 
heap buffer overflow when parsing jvm.cfg by rejecting configurations with more 
than 255 resolvable JVM definitions.</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Mohd Irshad, Gary Gregory">Add 
unit test for DaemonInitException.getMessageWithCause (#318).</action>
       <!-- UPDATE -->
diff --git a/src/native/unix/Makefile.in b/src/native/unix/Makefile.in
index eb0db0ee..0f3e04b0 100644
--- a/src/native/unix/Makefile.in
+++ b/src/native/unix/Makefile.in
@@ -22,7 +22,11 @@ all: native/all
 native/all:
        (cd native; $(MAKE)  all)
 
+check:
+       (cd tests; $(MAKE) check)
+
 clean:
+       (cd tests; $(MAKE) clean)
        (cd native; $(MAKE)  clean)
 
 distclean: clean
@@ -31,6 +35,7 @@ distclean: clean
        rm -f config.status
        rm -f config.nice
        rm -f native/Makefile
+       rm -f tests/Makefile
        rm -f Makefile
        rm -f Makedefs
        rm -rf autom4te.cache
diff --git a/src/native/unix/configure.ac b/src/native/unix/configure.ac
index 66d76da3..3f221a7a 100644
--- a/src/native/unix/configure.ac
+++ b/src/native/unix/configure.ac
@@ -135,7 +135,7 @@ dnl 
-------------------------------------------------------------------------
 dnl Random programs we need to compile locally
 dnl -------------------------------------------------------------------------
 AP_MSG_HEADER([Writing output files])
-AC_CONFIG_FILES(Makefile Makedefs native/Makefile)
+AC_CONFIG_FILES(Makefile Makedefs native/Makefile tests/Makefile)
 AC_OUTPUT
 
 dnl -------------------------------------------------------------------------
diff --git a/src/native/unix/native/home.c b/src/native/unix/native/home.c
index d1573c43..8daa6771 100644
--- a/src/native/unix/native/home.c
+++ b/src/native/unix/native/home.c
@@ -44,6 +44,9 @@ static bool checkfile(char *path)
     return (false);
 }
 
+/* Maximum number of configured JVMs, excluding the NULL terminator. */
+#define MAX_JVMS 255
+
 /* Parse a VM configuration file */
 static bool parse(home_data *data)
 {
@@ -56,7 +59,12 @@ static bool parse(home_data *data)
         return (false);
     }
 
-    data->jvms = (home_jvm **)malloc(256 * sizeof(home_jvm *));
+    data->jvms = (home_jvm **)malloc((MAX_JVMS + 1) * sizeof(home_jvm *));
+    if (data->jvms == NULL) {
+        fclose(cfgf);
+        return (false);
+    }
+    data->jvms[0] = NULL;
 
     while ((ret = fgets(buf, 1024, cfgf)) != NULL) {
         char *tmp = strchr(ret, '#');
@@ -98,11 +106,13 @@ static bool parse(home_data *data)
                 k = replace(temp, 1024, orig, "$JAVA_HOME", data->path);
                 if (k != 0) {
                     log_error("Can't replace home in VM library (%d)", k);
+                    fclose(cfgf);
                     return (false);
                 }
                 k = replace(repl, 1024, temp, "$VM_NAME", ret);
                 if (k != 0) {
                     log_error("Can't replace name in VM library (%d)", k);
+                    fclose(cfgf);
                     return (false);
                 }
 
@@ -118,6 +128,13 @@ static bool parse(home_data *data)
                 log_debug("Cannot locate library for VM %s (skipping)", ret);
             }
             else {
+                /* Reserve the final slot for the NULL terminator. */
+                if (data->jnum >= MAX_JVMS) {
+                    log_error("Too many JVM definitions in %s (maximum %d)", 
data->cfgf, MAX_JVMS);
+                    free(libf);
+                    fclose(cfgf);
+                    return (false);
+                }
                 data->jvms[data->jnum] = (home_jvm *)malloc(sizeof(home_jvm));
                 data->jvms[data->jnum]->name = strdup(ret);
                 data->jvms[data->jnum]->libr = libf;
@@ -199,6 +216,16 @@ static home_data *build(char *path)
     /* If we got here, we most definitely found a jvm.cfg file */
     if (parse(data) == false) {
         log_error("Cannot parse VM configuration file %s", data->cfgf);
+        for (x = 0; x < data->jnum; x++) {
+            free(data->jvms[x]->name);
+            free(data->jvms[x]->libr);
+            free(data->jvms[x]);
+        }
+        free(data->jvms);
+        free(data->cfgf);
+        free(data->path);
+        free(data);
+        return (NULL);
     }
 
     return (data);
diff --git a/src/native/unix/Makefile.in b/src/native/unix/tests/Makefile.in
similarity index 61%
copy from src/native/unix/Makefile.in
copy to src/native/unix/tests/Makefile.in
index eb0db0ee..36af6523 100644
--- a/src/native/unix/Makefile.in
+++ b/src/native/unix/tests/Makefile.in
@@ -15,25 +15,20 @@
 # limitations under the License.
 #
 
-SUBDIRS = native
+include ../Makedefs
 
-all: native/all
+# Compile production sources separately so test flags never affect native 
objects.
+SOURCES = test_home.c ../native/home.c ../native/replace.c 
../native/location.c ../native/debug.c
+HEADERS = $(wildcard ../native/*.h)
 
-native/all:
-       (cd native; $(MAKE)  all)
+.PHONY: check clean
 
-clean:
-       (cd native; $(MAKE)  clean)
+check: test_home
+       sh ./check.sh
 
-distclean: clean
-       rm -f config.cache
-       rm -f config.log
-       rm -f config.status
-       rm -f config.nice
-       rm -f native/Makefile
-       rm -f Makefile
-       rm -f Makedefs
-       rm -rf autom4te.cache
+test_home: $(SOURCES) $(HEADERS) ../Makedefs
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(EXTRA_CFLAGS) $(INCLUDES) -I../native 
$(SOURCES) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LIBS) -o $@
 
-realclean: distclean
-       rm -f configure
+clean:
+       rm -f test_home
+       rm -rf test_home.dSYM
diff --git a/src/native/unix/tests/README.txt b/src/native/unix/tests/README.txt
new file mode 100644
index 00000000..e51ca9f4
--- /dev/null
+++ b/src/native/unix/tests/README.txt
@@ -0,0 +1,41 @@
+/*
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       https://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+Native parser regression tests
+==============================
+
+From src/native/unix, configure and run the tests:
+
+    sh support/buildconf.sh
+    ./configure --with-java=$JAVA_HOME
+    make check
+
+The tests compile a separate executable from test_home.c and the production
+parser sources. The normal make target does not build tests. No test code is
+linked into jsvc or libservice.a. make clean removes the test executable;
+make distclean also removes the generated test Makefile.
+
+On compilers supporting AddressSanitizer and UndefinedBehaviorSanitizer:
+
+    make -C tests clean
+    make check EXTRA_CFLAGS="-g -O1 -fsanitize=address,undefined" 
EXTRA_LDFLAGS="-fsanitize=address,undefined"
+
+The shell runner creates temporary Java homes under /tmp and removes them on
+exit. Empty library files suffice because home() checks their existence without
+loading them. Fixtures use the platform's production library-location patterns.
+Tests cover empty input, ordinary input, the 255-entry boundary, rejection of
+256 and 257 entries, duplicate definitions, skipped missing libraries, entry
+order, library paths, and the terminating NULL pointer.
diff --git a/src/native/unix/Makefile.in b/src/native/unix/tests/check.sh
similarity index 69%
copy from src/native/unix/Makefile.in
copy to src/native/unix/tests/check.sh
index eb0db0ee..a56d8246 100644
--- a/src/native/unix/Makefile.in
+++ b/src/native/unix/tests/check.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
 #
 # Licensed to the Apache Software Foundation (ASF) under one or more
 # contributor license agreements.  See the NOTICE file distributed with
@@ -15,25 +16,15 @@
 # limitations under the License.
 #
 
-SUBDIRS = native
+set -eu
 
-all: native/all
+# Keep paths short enough for the production parser's fixed path buffers.
+test_dir=$(mktemp -d /tmp/jsvc-home-test.XXXXXXXX)
+trap 'rm -rf "$test_dir"' 0
+trap 'exit 1' 1 2 3 15
 
-native/all:
-       (cd native; $(MAKE)  all)
-
-clean:
-       (cd native; $(MAKE)  clean)
-
-distclean: clean
-       rm -f config.cache
-       rm -f config.log
-       rm -f config.status
-       rm -f config.nice
-       rm -f native/Makefile
-       rm -f Makefile
-       rm -f Makedefs
-       rm -rf autom4te.cache
-
-realclean: distclean
-       rm -f configure
+for duplicates in 0 1; do
+    for count in 0 1 254 255 256 257; do
+        ./test_home "$test_dir/home-$duplicates-$count" "$count" "$duplicates"
+    done
+done
diff --git a/src/native/unix/tests/test_home.c 
b/src/native/unix/tests/test_home.c
new file mode 100644
index 00000000..117176c8
--- /dev/null
+++ b/src/native/unix/tests/test_home.c
@@ -0,0 +1,112 @@
+/*
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       https://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+#include "jsvc.h"
+#include <errno.h>
+
+/* These checks must remain active even when NDEBUG is defined. */
+#define CHECK(condition) do { \
+    if (!(condition)) { \
+        fprintf(stderr, "%s:%d: %s failed\n", __FILE__, __LINE__, #condition); 
\
+        exit(1); \
+    } \
+} while (0)
+
+/* Create parent directories for a fixture file inside the temporary home. */
+static FILE *create_file(char *path)
+{
+    char *p;
+    FILE *file;
+
+    for (p = path + 1; *p != '\0'; p++) {
+        if (*p == '/') {
+            *p = '\0';
+            CHECK(mkdir(path, 0700) == 0 || errno == EEXIST);
+            *p = '/';
+        }
+    }
+    file = fopen(path, "w");
+    CHECK(file != NULL);
+    return file;
+}
+
+static void library_path(char *path, char *java_home, char *name)
+{
+    char temp[1024];
+
+    CHECK(replace(temp, sizeof(temp), location_jvm_configured[0], 
"$JAVA_HOME", java_home) == 0);
+    CHECK(replace(path, 1024, temp, "$VM_NAME", name) == 0);
+}
+
+int main(int argc, char *argv[])
+{
+    char cfg[1024];
+    char path[1024];
+    char name[32];
+    FILE *file;
+    home_data *data;
+    int count, duplicates, i;
+
+    CHECK(argc == 4);
+    count = atoi(argv[2]);
+    duplicates = atoi(argv[3]);
+    CHECK(count >= 0 && count <= 257);
+    CHECK(location_jvm_cfg[0] != NULL);
+    CHECK(location_jvm_configured[0] != NULL);
+    CHECK(replace(cfg, sizeof(cfg), location_jvm_cfg[0], "$JAVA_HOME", 
argv[1]) == 0);
+    file = create_file(cfg);
+    CHECK(fputs("# Test configuration\n\n", file) >= 0);
+    for (i = 0; i < count; i++) {
+        FILE *library;
+
+        sprintf(name, "vm%d", duplicates ? 0 : i);
+        library_path(path, argv[1], name);
+        library = create_file(path);
+        CHECK(fclose(library) == 0);
+        CHECK(fprintf(file, "-missing KNOWN\n-%s KNOWN\n", name) > 0);
+    }
+    /* Unresolvable definitions must not consume the last available slot. */
+    CHECK(fputs("-missing KNOWN\n# trailing comment\n\n", file) >= 0);
+    CHECK(fclose(file) == 0);
+
+    data = home(argv[1]);
+    if (count > 255) {
+        /* Reject the whole configuration, not a truncated VM table. */
+        CHECK(data == NULL);
+    }
+    else {
+        CHECK(data != NULL);
+        CHECK(data->jnum == count);
+        CHECK(data->jvms != NULL);
+        CHECK(data->jvms[count] == NULL);
+        for (i = 0; i < count; i++) {
+            sprintf(name, "vm%d", duplicates ? 0 : i);
+            library_path(path, argv[1], name);
+            CHECK(data->jvms[i] != NULL);
+            CHECK(strcmp(data->jvms[i]->name, name) == 0);
+            CHECK(strcmp(data->jvms[i]->libr, path) == 0);
+            free(data->jvms[i]->name);
+            free(data->jvms[i]->libr);
+            free(data->jvms[i]);
+        }
+        free(data->jvms);
+        free(data->cfgf);
+        free(data->path);
+        free(data);
+    }
+    printf("PASS: %d resolved entries, duplicates=%d\n", count, duplicates);
+    return 0;
+}

Reply via email to