From e11ed1ef79f380f40e14af8d45720f80fc172162 Mon Sep 17 00:00:00 2001 From: Zach van Rijn Date: Tue, 15 Nov 2022 19:43:24 +0000 Subject: user/mbuffer: add patch to clamp 32-bit parameters. fixes #834. The number of physical and available pages will of course be correct on real 32-bit systems, but mbuffer does not account for the case of when a 64-bit system with more memory is running with a 32-bit personality. In this scenario, dynamic detection of system memory limits yields values that are not safe for use in a 32-bit environment. --- user/mbuffer/clamp-32-bit-parameters.patch | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 user/mbuffer/clamp-32-bit-parameters.patch (limited to 'user/mbuffer/clamp-32-bit-parameters.patch') diff --git a/user/mbuffer/clamp-32-bit-parameters.patch b/user/mbuffer/clamp-32-bit-parameters.patch new file mode 100644 index 000000000..3dc2d0eac --- /dev/null +++ b/user/mbuffer/clamp-32-bit-parameters.patch @@ -0,0 +1,39 @@ +In addition to clamping NumP and AvP, this patch reorders +shift operations to prevent possible overflow when given +values that are found in some environments where a 64-bit +system is running with a 32-bit personality. + +It should not be construed as fixing any bug in mbuffer. + +--- a/mbuffer.c ++++ b/mbuffer.c +@@ -984,7 +984,14 @@ + + /* get physical memory size */ + #if defined(_SC_PHYS_PAGES) +- NumP = sysconf(_SC_PHYS_PAGES); ++ if (sizeof(void *) == 4) ++ { ++ NumP = (unsigned)-1 / PgSz; /* 4GB worth of pages */ ++ } ++ else ++ { ++ NumP = sysconf(_SC_PHYS_PAGES); ++ } + if (NumP < 0) { + warningmsg("unable to determine number of total memory pages: %s\n",strerror(errno)); + NumP = 0; +@@ -1004,8 +1011,12 @@ + char *at = strstr(tmp,"MemAvailable:"); + if (at) { + AvP = strtol(at+13,0,0); +- AvP <<= 10; + AvP /= PgSz; ++ AvP <<= 10; ++ if (sizeof(void *) == 4 && AvP > NumP) ++ { ++ AvP = NumP; ++ } + debugmsg("available memory: %lu pages\n",AvP); + } + } -- cgit v1.2.3-70-g09d2