fix Linux: realpath buffer < PATH_MAX caused FORTIFY buffer-overflow abort (pmm -v / install); use realpath(NULL); disable FORTIFY in builds

这个提交包含在:
JGZYES
2026-08-31 22:37:04 +08:00
父节点 aa2d2a487c
当前提交 7276c9747d
修改 2 个文件,包含 10 行新增3 行删除
+1 -1
查看文件
@@ -38,7 +38,7 @@ jobs:
mkdir -p out mkdir -p out
VER=${{ github.ref_type == 'tag' && github.ref_name || 'dev' }} VER=${{ github.ref_type == 'tag' && github.ref_name || 'dev' }}
VER=${VER#v} 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/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 src/sha256.c src/sha1.c src/mirrors.c src/pdm.c
+9 -2
查看文件
@@ -22,15 +22,22 @@
#define PMM_PCLOSE_READ(p) pclose(p) #define PMM_PCLOSE_READ(p) pclose(p)
#endif #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) { void pmm_set_self_path(const char *argv0) {
if (!argv0 || !*argv0) return; if (!argv0 || !*argv0) return;
#ifdef _WIN32 #ifdef _WIN32
if (_fullpath(g_self_path, argv0, sizeof(g_self_path)) == NULL) if (_fullpath(g_self_path, argv0, sizeof(g_self_path)) == NULL)
snprintf(g_self_path, sizeof(g_self_path), "%s", argv0); snprintf(g_self_path, sizeof(g_self_path), "%s", argv0);
#else #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); snprintf(g_self_path, sizeof(g_self_path), "%s", argv0);
}
#endif #endif
} }
const char *pmm_self_path(void) { const char *pmm_self_path(void) {