Add overflow-safe allocation wrapper pmem_calloc_safe() (#369)

* Add overflow-safe allocation wrappers to pmem module

* Replace malloc(nmemb*size) with pmem_calloc_safe in high-risk sites

---------

Co-authored-by: Levi Neely <lkn@darkstar.example.net>
This commit is contained in:
Levi Neely 2026-03-08 14:55:21 +01:00 committed by GitHub
parent 89ef8663b0
commit d0c213f181
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 78 additions and 15 deletions

View File

@ -39,6 +39,7 @@
#include "psynclib.h" #include "psynclib.h"
#include "ptimer.h" #include "ptimer.h"
#include "pdbg.h" #include "pdbg.h"
#include "pmem.h"
/* /*
commented definitions are unused, but kept because they may be commented definitions are unused, but kept because they may be
@ -313,7 +314,7 @@ static binresult *do_parse_result(unsigned char **restrict indata,
arr = NULL; arr = NULL;
cnt = 0; cnt = 0;
alloc = 128; alloc = 128;
arr = (binresult **)malloc(sizeof(binresult *) * alloc); arr = (binresult **)pmem_calloc_safe(alloc, sizeof(binresult *));
if (!arr) if (!arr)
return NULL; return NULL;
while (**indata != RPARAM_END) { while (**indata != RPARAM_END) {
@ -346,7 +347,7 @@ static binresult *do_parse_result(unsigned char **restrict indata,
arr = NULL; arr = NULL;
cnt = 0; cnt = 0;
alloc = 32; alloc = 32;
arr = (struct _hashpair *)malloc(sizeof(struct _hashpair) * alloc); arr = (struct _hashpair *)pmem_calloc_safe(alloc, sizeof(struct _hashpair));
if (!arr) if (!arr)
return NULL; return NULL;
while (**indata != RPARAM_END) { while (**indata != RPARAM_END) {
@ -398,10 +399,10 @@ static binresult *parse_result(unsigned char *data, size_t datalen) {
retlen = calc_ret_len(&datac, &datalenc, &strcnt); retlen = calc_ret_len(&datac, &datalenc, &strcnt);
if (retlen == -1) if (retlen == -1)
return NULL; return NULL;
datac = malloc(sizeof(unsigned char) * retlen); datac = pmem_calloc_safe(retlen, sizeof(unsigned char));
if (!datac) if (!datac)
return NULL; return NULL;
strings = malloc(sizeof(binresult *) * strcnt); strings = pmem_calloc_safe(strcnt, sizeof(binresult *));
if (!strings) { if (!strings) {
free(datac); free(datac);
return NULL; return NULL;

View File

@ -32,6 +32,7 @@
#include "pnetlibs.h" #include "pnetlibs.h"
#include "psys.h" #include "psys.h"
#include "psql.h" #include "psql.h"
#include "pmem.h"
#include <stdio.h> #include <stdio.h>
@ -103,12 +104,18 @@ int do_psync_account_stopshare(psync_shareid_t usershareids[], int nusershareid,
if (unlikely(numparam == 1)) if (unlikely(numparam == 1))
return -3; return -3;
t = (binparam *)malloc(numparam * sizeof(binparam)); t = (binparam *)pmem_calloc_safe(numparam, sizeof(binparam));
if (!t)
return -1;
init_param_str(t, "auth", psync_my_auth); init_param_str(t, "auth", psync_my_auth);
if (nusershareid) { if (nusershareid) {
ids1 = (char *)malloc(nusershareid * FOLDERID_ENTRY_SIZE); ids1 = (char *)pmem_calloc_safe(nusershareid, FOLDERID_ENTRY_SIZE);
if (!ids1) {
free(t);
return -1;
}
idsp = ids1; idsp = ids1;
for (i = 0; i < nusershareid; ++i) { for (i = 0; i < nusershareid; ++i) {
k = sprintf(idsp, "%lld", (long long)usershareids[i]); k = sprintf(idsp, "%lld", (long long)usershareids[i]);
@ -124,7 +131,13 @@ int do_psync_account_stopshare(psync_shareid_t usershareids[], int nusershareid,
} }
if (nteamshareid) { if (nteamshareid) {
ids2 = (char *)malloc(nteamshareid * FOLDERID_ENTRY_SIZE); ids2 = (char *)pmem_calloc_safe(nteamshareid, FOLDERID_ENTRY_SIZE);
if (!ids2) {
if (nusershareid)
free(ids1);
free(t);
return -1;
}
idsp = ids2; idsp = ids2;
for (i = 0; i < nteamshareid; ++i) { for (i = 0; i < nteamshareid; ++i) {
k = sprintf(idsp, "%lld", (long long)teamshareids[i]); k = sprintf(idsp, "%lld", (long long)teamshareids[i]);
@ -209,14 +222,25 @@ int do_psync_account_modifyshare(psync_shareid_t usrshrids[], uint32_t uperms[],
if (unlikely(numparam == 1)) if (unlikely(numparam == 1))
return -3; return -3;
t = (binparam *)malloc(numparam * sizeof(binparam)); t = (binparam *)pmem_calloc_safe(numparam, sizeof(binparam));
if (!t)
return -1;
init_param_str(t, "auth", psync_my_auth); init_param_str(t, "auth", psync_my_auth);
if (nushid) { if (nushid) {
ids1 = (char *)malloc(nushid * FOLDERID_ENTRY_SIZE); ids1 = (char *)pmem_calloc_safe(nushid, FOLDERID_ENTRY_SIZE);
if (!ids1) {
free(t);
return -1;
}
idsp = ids1; idsp = ids1;
perms1 = (char *)malloc(nushid * FOLDERID_ENTRY_SIZE); perms1 = (char *)pmem_calloc_safe(nushid, FOLDERID_ENTRY_SIZE);
if (!perms1) {
free(ids1);
free(t);
return -1;
}
permsp = perms1; permsp = perms1;
for (i = 0; i < nushid; ++i) { for (i = 0; i < nushid; ++i) {
k = sprintf(idsp, "%lld", (long long)usrshrids[i]); k = sprintf(idsp, "%lld", (long long)usrshrids[i]);
@ -241,9 +265,26 @@ int do_psync_account_modifyshare(psync_shareid_t usrshrids[], uint32_t uperms[],
} }
if (ntmshid) { if (ntmshid) {
ids2 = (char *)malloc(ntmshid * FOLDERID_ENTRY_SIZE); ids2 = (char *)pmem_calloc_safe(ntmshid, FOLDERID_ENTRY_SIZE);
if (!ids2) {
if (nushid) {
free(perms1);
free(ids1);
}
free(t);
return -1;
}
idsp = ids2; idsp = ids2;
perms2 = (char *)malloc(ntmshid * FOLDERID_ENTRY_SIZE); perms2 = (char *)pmem_calloc_safe(ntmshid, FOLDERID_ENTRY_SIZE);
if (!perms2) {
free(ids2);
if (nushid) {
free(perms1);
free(ids1);
}
free(t);
return -1;
}
permsp = perms2; permsp = perms2;
for (i = 0; i < ntmshid; ++i) { for (i = 0; i < ntmshid; ++i) {

View File

@ -1,4 +1,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <stdlib.h>
#include <stdint.h>
#include "pcompiler.h" #include "pcompiler.h"
#include "psql.h" #include "psql.h"
@ -60,3 +62,10 @@ int pmem_munlock(void *ptr, size_t size) {
void pmem_reset(void *ptr, size_t size) { void pmem_reset(void *ptr, size_t size) {
madvise(ptr, size, MADV_DONTNEED); madvise(ptr, size, MADV_DONTNEED);
} }
void *pmem_calloc_safe(size_t nmemb, size_t size) {
if (nmemb != 0 && size > SIZE_MAX / nmemb) {
return NULL;
}
return calloc(nmemb, size);
}

View File

@ -2,6 +2,7 @@
#define __PMEM_H #define __PMEM_H
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
void *pmem_mmap(size_t size); void *pmem_mmap(size_t size);
void *pmem_mmap_safe(size_t size); void *pmem_mmap_safe(size_t size);
@ -9,5 +10,6 @@ int pmem_munmap(void *ptr, size_t size);
int pmem_mlock(void *ptr, size_t size); int pmem_mlock(void *ptr, size_t size);
int pmem_munlock(void *ptr, size_t size); int pmem_munlock(void *ptr, size_t size);
void pmem_reset(void *ptr, size_t size); void pmem_reset(void *ptr, size_t size);
void *pmem_calloc_safe(size_t nmemb, size_t size);
#endif #endif

View File

@ -50,6 +50,7 @@
#include "psettings.h" #include "psettings.h"
#include "psql.h" #include "psql.h"
#include "ptools.h" #include "ptools.h"
#include "pmem.h"
#define PTOOLS_MAX_PARAMS 30 #define PTOOLS_MAX_PARAMS 30
@ -135,7 +136,10 @@ int ptools_create_backend_event(const char *binapi, const char *category,
paramsLocal[5] = (binparam)PAPI_NUM(EPARAM_TIME, etime); paramsLocal[5] = (binparam)PAPI_NUM(EPARAM_TIME, etime);
if (pCnt > 0) { if (pCnt > 0) {
keyParams = (char *)malloc(258 * pCnt); keyParams = (char *)pmem_calloc_safe(pCnt, 258);
if (!keyParams) {
return -1;
}
keyParams[0] = 0; keyParams[0] = 0;
for (i = 0; i < pCnt; i++) { for (i = 0; i < pCnt; i++) {
@ -371,7 +375,10 @@ int ptools_backend_call(const char *binapi, const char *wsPath,
if (strlen(payloadName) > 0) { if (strlen(payloadName) > 0) {
payload = (binresult *)papi_find_result2(res, payloadName, PARAM_HASH); payload = (binresult *)papi_find_result2(res, payloadName, PARAM_HASH);
*resData = (binresult *)malloc(payload->length * sizeof(binresult)); *resData = (binresult *)pmem_calloc_safe(payload->length, sizeof(binresult));
if (!*resData) {
return -1;
}
memcpy(*resData, payload, (payload->length * sizeof(binresult))); memcpy(*resData, payload, (payload->length * sizeof(binresult)));
} }
} }
@ -450,7 +457,10 @@ void ptools_send_psyncs_event(const char *binapi, const char *auth) {
int intRes; int intRes;
int syncCnt = 0; int syncCnt = 0;
errMsg = (char *)malloc(1024 * sizeof(char)); errMsg = (char *)pmem_calloc_safe(1024, sizeof(char));
if (!errMsg) {
return;
}
errMsg[0] = 0; errMsg[0] = 0;
time(&rawtime); time(&rawtime);