8000 Avoid possibly accessing off the end of memory in examine_attribute(). · postwait/postgres@3cfb602 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cfb602

Browse files
committed
Avoid possibly accessing off the end of memory in examine_attribute().
Since the last couple of columns of pg_type are often NULL, sizeof(FormData_pg_type) can be an overestimate of the actual size of the tuple data part. Therefore memcpy'ing that much out of the catalog cache, as analyze.c was doing, poses a small risk of copying past the end of memory and incurring SIGSEGV. No such crash has been identified in the field, but we've certainly seen the equivalent happen in other code paths, so patch this one all the way back. Per valgrind testing by Noah Misch, though this is not his proposed patch. I chose to use SearchSysCacheCopy1 rather than inventing special-purpose infrastructure for copying only the minimal part of a pg_type tuple.
1 parent f85efee commit 3cfb602

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/backend/commands/analyze.c

Lines changed: 5 additions & 7 deletions
< 8000 colgroup>
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,12 @@ examine_attribute(Relation onerel, int attnum)
702702
stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats));
703703
stats->attr = (Form_pg_attribute) palloc(ATTRIBUTE_TUPLE_SIZE);
704704
memcpy(stats->attr, attr, ATTRIBUTE_TUPLE_SIZE);
705-
typtuple = SearchSysCache(TYPEOID,
706-
ObjectIdGetDatum(attr->atttypid),
707-
0, 0, 0);
705+
typtuple = SearchSysCacheCopy(TYPEOID,
706+
ObjectIdGetDatum(attr->atttypid),
707+
0, 0, 0);
708708
if (!HeapTupleIsValid(typtuple))
709709
elog(ERROR, "cache lookup failed for type %u", attr->atttypid);
710-
stats->attrtype = (Form_pg_type) palloc(sizeof(FormData_pg_type));
711-
memcpy(stats->attrtype, GETSTRUCT(typtuple), sizeof(FormData_pg_type));
712-
ReleaseSysCache(typtuple);
710+
stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
713711
stats->anl_context = anl_context;
714712
stats->tupattnum = attnum;
715713

@@ -725,7 +723,7 @@ examine_attribute(Relation onerel, int attnum)
725723

726724
if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
727725
{
728-
pfree(stats->attrtype);
726+
heap_freetuple(typtuple);
729727
pfree(stats->attr);
730728
pfree(stats);
731729
return NULL;

0 commit comments

Comments
 (0)
0