proc/alloc.*: Use size_t, not unsigned int.

Otherwise this can truncate sizes on 64-bit platforms, and is one of the
reasons the integer overflows in file2strvec() are exploitable at all.
Also: catch potential integer overflow in xstrdup() (should never
happen, but better safe than sorry), and use memcpy() instead of
strcpy() (faster).

Warnings:

- in glibc, realloc(ptr, 0) is equivalent to free(ptr), but not here,
  because of the ++size;

- here, xstrdup() can return NULL (if str is NULL), which goes against
  the idea of the xalloc wrappers.

We were tempted to call exit() or xerrx() in those cases, but decided
against it, because it might break things in unexpected places; TODO?
This commit is contained in:
Qualys Security Advisory 1970-01-01 00:00:00 +00:00 committed by Craig Small
parent 98b79d1ef1
commit f1077b7a55
2 changed files with 16 additions and 12 deletions

View File

@ -37,14 +37,14 @@ static void xdefault_error(const char *restrict fmts, ...) {
message_fn xalloc_err_handler = xdefault_error; message_fn xalloc_err_handler = xdefault_error;
void *xcalloc(unsigned int size) { void *xcalloc(size_t size) {
void * p; void * p;
if (size == 0) if (size == 0)
++size; ++size;
p = calloc(1, size); p = calloc(1, size);
if (!p) { if (!p) {
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size); xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return p; return p;
@ -57,20 +57,20 @@ void *xmalloc(size_t size) {
++size; ++size;
p = malloc(size); p = malloc(size);
if (!p) { if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size); xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return(p); return(p);
} }
void *xrealloc(void *oldp, unsigned int size) { void *xrealloc(void *oldp, size_t size) {
void *p; void *p;
if (size == 0) if (size == 0)
++size; ++size;
p = realloc(oldp, size); p = realloc(oldp, size);
if (!p) { if (!p) {
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size); xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return(p); return(p);
@ -80,13 +80,17 @@ char *xstrdup(const char *str) {
char *p = NULL; char *p = NULL;
if (str) { if (str) {
unsigned int size = strlen(str) + 1; size_t size = strlen(str) + 1;
p = malloc(size); if (size < 1) {
if (!p) { xalloc_err_handler("%s refused to allocate %zu bytes of memory", __func__, size);
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(p, str); p = malloc(size);
if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
memcpy(p, str, size);
} }
return(p); return(p);
} }

View File

@ -10,9 +10,9 @@ typedef void (*message_fn)(const char *__restrict, ...) __attribute__((format(pr
/* change xalloc_err_handler to override the default fprintf(stderr... */ /* change xalloc_err_handler to override the default fprintf(stderr... */
extern message_fn xalloc_err_handler; extern message_fn xalloc_err_handler;
extern void *xcalloc(unsigned int size) MALLOC; extern void *xcalloc(size_t size) MALLOC;
extern void *xmalloc(size_t size) MALLOC; extern void *xmalloc(size_t size) MALLOC;
extern void *xrealloc(void *oldp, unsigned int size) MALLOC; extern void *xrealloc(void *oldp, size_t size) MALLOC;
extern char *xstrdup(const char *str) MALLOC; extern char *xstrdup(const char *str) MALLOC;
EXTERN_C_END EXTERN_C_END