mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Predict integer overflow to avoid buffer overruns.
Several functions, mostly type input functions, calculated an allocation size such that the calculation wrapped to a small positive value when arguments implied a sufficiently-large requirement. Writes past the end of the inadvertent small allocation followed shortly thereafter. Coverity identified the path_in() vulnerability; code inspection led to the rest. In passing, add check_stack_depth() to prevent stack overflow in related functions. Back-patch to 8.4 (all supported versions). The non-comment hstore changes touch code that did not exist in 8.4, so that part stops at 9.0. Noah Misch and Heikki Linnakangas, reviewed by Tom Lane. Security: CVE-2014-0064
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
#include "catalog/pg_type.h"
|
||||
#include "funcapi.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/memutils.h"
|
||||
|
||||
#include "hstore.h"
|
||||
|
||||
@ -90,6 +91,19 @@ hstoreArrayToPairs(ArrayType *a, int *npairs)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* A text array uses at least eight bytes per element, so any overflow in
|
||||
* "key_count * sizeof(Pairs)" is small enough for palloc() to catch.
|
||||
* However, credible improvements to the array format could invalidate
|
||||
* that assumption. Therefore, use an explicit check rather than relying
|
||||
* on palloc() to complain.
|
||||
*/
|
||||
if (key_count > MaxAllocSize / sizeof(Pairs))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||
errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
|
||||
key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
|
||||
|
||||
key_pairs = palloc(sizeof(Pairs) * key_count);
|
||||
|
||||
for (i = 0, j = 0; i < key_count; i++)
|
||||
@ -648,6 +662,7 @@ hstore_slice_to_hstore(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(out);
|
||||
}
|
||||
|
||||
/* hstoreArrayToPairs() checked overflow */
|
||||
out_pairs = palloc(sizeof(Pairs) * nkeys);
|
||||
bufsiz = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user