From 7276c9747dac9e9d3a2e8548dd735fab194386c4 Mon Sep 17 00:00:00 2001 From: JGZYES Date: Mon, 31 Aug 2026 22:37:04 +0800 Subject: [PATCH] fix Linux: realpath buffer < PATH_MAX caused FORTIFY buffer-overflow abort (pmm -v / install); use realpath(NULL); disable FORTIFY in builds --- .github/workflows/release.yml | 2 +- src/pmm.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 36b515a..12fb9b1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,7 +38,7 @@ jobs: mkdir -p out VER=${{ github.ref_type == 'tag' && github.ref_name || 'dev' }} VER=${VER#v} - ${{ matrix.cc }} -O2 -Wall -static -std=c11 -DPMM_VERSION=\"$VER\" -o out/pmm${{ matrix.ext }} \ + ${{ matrix.cc }} -O2 -Wall -static -std=c11 -D_FORTIFY_SOURCE=0 -U_FORTIFY_SOURCE -DPMM_VERSION=\"$VER\" -o out/pmm${{ matrix.ext }} \ src/main.c src/json.c src/ini.c src/pmm.c src/http.c src/repo.c src/install.c \ src/sha256.c src/sha1.c src/mirrors.c src/pdm.c diff --git a/src/pmm.c b/src/pmm.c index b216fe8..4c23b24 100644 --- a/src/pmm.c +++ b/src/pmm.c @@ -22,15 +22,22 @@ #define PMM_PCLOSE_READ(p) pclose(p) #endif -static char g_self_path[1400] = ""; +static char g_self_path[4096] = ""; /* >= PATH_MAX: realpath FORTIFY requires it */ void pmm_set_self_path(const char *argv0) { if (!argv0 || !*argv0) return; #ifdef _WIN32 if (_fullpath(g_self_path, argv0, sizeof(g_self_path)) == NULL) snprintf(g_self_path, sizeof(g_self_path), "%s", argv0); #else - if (realpath(argv0, g_self_path) == NULL) + /* realpath(_, NULL) allocates internally — no fixed-buffer overflow, and + * no FORTIFY __realpath_chk abort when the caller buffer is < PATH_MAX. */ + char *rp = realpath(argv0, NULL); + if (rp) { + snprintf(g_self_path, sizeof(g_self_path), "%s", rp); + free(rp); + } else { snprintf(g_self_path, sizeof(g_self_path), "%s", argv0); + } #endif } const char *pmm_self_path(void) {