Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d4d6de

Browse files
committed
pageinspect: Add more sanity checks to prevent out-of-bound reads
A couple of code paths use the special area on the page passed by the function caller, expecting to find some data in it. However, feeding an incorrect page can lead to out-of-bound reads when trying to access the page special area (like a heap page that has no special area, leading PageGetSpecialPointer() to grab a pointer outside the allocated page). The functions used for hash and btree indexes have some protection already against that, while some other functions using a relation OID as argument would make sure that the access method involved is correct, but functions taking in input a raw page without knowing the relation the page is attached to would run into problems. This commit improves the set of checks used in the code paths of BRIN, btree (including one check if a leaf page is found with a non-zero level), GIN and GiST to verify that the page given in input has a special area size that fits with each access method, which is done though PageGetSpecialSize(), becore calling PageGetSpecialPointer(). The scope of the checks done is limited to work with pages that one would pass after getting a block with get_raw_page(), as it is possible to craft byteas that could bypass existing code paths. Having too many checks would also impact the usability of pageinspect, as the existing code is very useful to look at the content details in a corrupted page, so the focus is really to avoid out-of-bound reads as this is never a good thing even with functions whose execution is limited to superusers. The safest approach could be to rework the functions so as these fetch a block using a relation OID and a block number, but there are also cases where using a raw page is useful. Tests are added to cover all the code paths that needed such checks, and an error message for hash indexes is reworded to fit better with what this commit adds. Reported-By: Alexander Lakhin Author: Julien Rouhaud, Michael Paquier Discussion: https://postgr.es/m/16527-ef7606186f0610a1@postgresql.org Discussion: https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ru Backpatch-through: 10
1 parent 4fbbea2 commit 3d4d6de

File tree

12 files changed

+145
-23
lines changed

12 files changed

+145
-23
lines changed

contrib/pageinspect/brinfuncs.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ brin_page_type(PG_FUNCTION_ARGS)
5858

5959
page = get_page_from_raw(raw_page);
6060

61+
/* verify the special space has the expected size */
62+
if (PageGetSpecialSize(page) != MAXALIGN(sizeof(BrinSpecialSpace)))
63+
ereport(ERROR,
64+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
65+
errmsg("input page is not a valid %s page", "BRIN"),
66+
errdetail("Expected special size %d, got %d.",
67+
(int) MAXALIGN(sizeof(BrinSpecialSpace)),
68+
(int) PageGetSpecialSize(page))));
69+
6170
switch (BrinPageType(page))
6271
{
6372
case BRIN_PAGETYPE_META:
@@ -86,6 +95,15 @@ verify_brin_page(bytea *raw_page, uint16 type, const char *strtype)
8695
{
8796
Page page = get_page_from_raw(raw_page);
8897

98+
/* verify the special space has the expected size */
99+
if (PageGetSpecialSize(page) != MAXALIGN(sizeof(BrinSpecialSpace)))
100+
ereport(ERROR,
101+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
102+
errmsg("input page is not a valid %s page", "BRIN"),
103+
errdetail("Expected special size %d, got %d.",
104+
(int) MAXALIGN(sizeof(BrinSpecialSpace)),
105+
(int) PageGetSpecialSize(page))));
106+
89107
/* verify the special space says this page is what we want */
90108
if (BrinPageType(page) != type)
91109
ereport(ERROR,

contrib/pageinspect/btreefuncs.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,27 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
547547

548548
uargs->offset = FirstOffsetNumber;
549549

550+
/* verify the special space has the expected size */
551+
if (PageGetSpecialSize(uargs->page) != MAXALIGN(sizeof(BTPageOpaqueData)))
552+
ereport(ERROR,
553+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
554+
errmsg("input page is not a valid %s page", "btree"),
555+
errdetail("Expected special size %d, got %d.",
556+
(int) MAXALIGN(sizeof(BTPageOpaqueData)),
557+
(int) PageGetSpecialSize(uargs->page))));
558+
550559
opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
551560

552561
if (P_ISMETA(opaque))
553562
ereport(ERROR,
554563
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
555564
errmsg("block is a meta page")));
556565

566+
if (P_ISLEAF(opaque) && opaque->btpo.level != 0)
567+
ereport(ERROR,
568+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
569+
errmsg("block is not a valid btree leaf page")));
570+
557571
if (P_ISDELETED(opaque))
558572
elog(NOTICE, "page is deleted");
559573

contrib/pageinspect/expected/brin.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ SELECT * FROM brin_page_items(get_raw_page('test1_a_idx', 2), 'test1_a_idx')
5252
CREATE INDEX test1_a_btree ON test1 (a);
5353
SELECT brin_page_items(get_raw_page('test1_a_btree', 0), 'test1_a_btree');
5454
ERROR: "test1_a_btree" is not a BRIN index
55+
-- Mask DETAIL messages as these are not portable across architectures.
56+
\set VERBOSITY terse
57+
-- Invalid special area size
58+
SELECT brin_page_type(get_raw_page('test1', 0));
59+
ERROR: input page is not a valid BRIN page
60+
SELECT * FROM brin_metapage_info(get_raw_page('test1', 0));
61+
ERROR: input page is not a valid BRIN page
62+
SELECT * FROM brin_revmap_data(get_raw_page('test1', 0));
63+
ERROR: input page is not a valid BRIN page
64+
\set VERBOSITY default
5565
DROP TABLE test1;

contrib/pageinspect/expected/btree.out

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE test1 (a int8, b text);
2-
INSERT INTO test1 VALUES (72057594037927937, 'text');
1+
CREATE TABLE test1 (a int8, b int4range);
2+
INSERT INTO test1 VALUES (72057594037927937, '[0,1)');
33
CREATE INDEX test1_a_idx ON test1 USING btree (a);
44
\x
55
SELECT * FROM bt_metap('test1_a_idx');
@@ -72,11 +72,25 @@ SELECT bt_page_stats('test1_a_hash', 0);
7272
ERROR: "test1_a_hash" is not a btree index
7373
SELECT bt_page_items('test1_a_hash', 0);
7474
ERROR: "test1_a_hash" is not a btree index
75-
-- Failure with incorrect page size
75+
SELECT bt_page_items(get_raw_page('test1_a_hash', 0));
76+
ERROR: block is a meta page
77+
CREATE INDEX test1_b_gist ON test1 USING gist(b);
78+
-- Special area of GiST is the same as btree, this complains about inconsistent
79+
-- leaf data on the page.
80+
SELECT bt_page_items(get_raw_page('test1_b_gist', 0));
81+
ERROR: block is not a valid btree leaf page
82+
-- Several failure modes.
7683
-- Suppress the DETAIL message, to allow the tests to work across various
77-
-- page sizes.
84+
-- page sizes and architectures.
7885
\set VERBOSITY terse
86+
-- invalid page size
7987
SELECT bt_page_items('aaa'::bytea);
8088
ERROR: invalid page size
89+
-- invalid special area size
90+
CREATE INDEX test1_a_brin ON test1 USING brin(a);
91+
SELECT bt_page_items(get_raw_page('test1', 0));
92+
ERROR: input page is not a valid btree page
93+
SELECT bt_page_items(get_raw_page('test1_a_brin', 0));
94+
ERROR: input page is not a valid btree page
8195
\set VERBOSITY default
8296
DROP TABLE test1;

contrib/pageinspect/expected/gin.out

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ FROM gin_leafpage_items(get_raw_page('test1_y_idx',
3535
-[ RECORD 1 ]
3636
?column? | t
3737

38-
-- Failure with incorrect page size
38+
-- Failure with various modes.
3939
-- Suppress the DETAIL message, to allow the tests to work across various
40-
-- page sizes.
40+
-- page sizes and architectures.
4141
\set VERBOSITY terse
42+
-- invalid page size
4243
SELECT gin_leafpage_items('aaa'::bytea);
4344
ERROR: invalid page size
4445
SELECT gin_metapage_info('bbb'::bytea);
4546
ERROR: invalid page size
4647
SELECT gin_page_opaque_info('ccc'::bytea);
4748
ERROR: invalid page size
49+
-- invalid special area size
50+
SELECT * FROM gin_metapage_info(get_raw_page('test1', 0));
51+
ERROR: input page is not a valid GIN metapage
52+
SELECT * FROM gin_page_opaque_info(get_raw_page('test1', 0));
53+
ERROR: input page is not a valid GIN data leaf page
54+
SELECT * FROM gin_leafpage_items(get_raw_page('test1', 0));
55+
ERROR: input page is not a valid GIN data leaf page
4856
\set VERBOSITY default

contrib/pageinspect/expected/hash.out

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ ERROR: page is not a hash bucket or overflow page
163163
CREATE INDEX test_hash_a_btree ON test_hash USING btree (a);
164164
SELECT hash_bitmap_info('test_hash_a_btree', 0);
165165
ERROR: "test_hash_a_btree" is not a hash index
166-
-- Failure with incorrect page size
166+
-- Failure with various modes.
167167
-- Suppress the DETAIL message, to allow the tests to work across various
168-
-- page sizes.
168+
-- page sizes and architectures.
169169
\set VERBOSITY terse
170+
-- invalid page size
170171
SELECT hash_metapage_info('aaa'::bytea);
171172
ERROR: invalid page size
172173
SELECT hash_page_items('bbb'::bytea);
@@ -175,5 +176,14 @@ SELECT hash_page_stats('ccc'::bytea);
175176
ERROR: invalid page size
176177
SELECT hash_page_type('ddd'::bytea);
177178
ERROR: invalid page size
179+
-- invalid special area size
180+
SELECT hash_metapage_info(get_raw_page('test_hash', 0));
181+
ERROR: input page is not a valid hash page
182+
SELECT hash_page_items(get_raw_page('test_hash', 0));
183+
ERROR: input page is not a valid hash page
184+
SELECT hash_page_stats(get_raw_page('test_hash', 0));
185+
ERROR: input page is not a valid hash page
186+
SELECT hash_page_type(get_raw_page('test_hash', 0));
187+
ERROR: input page is not a valid hash page
178188
\set VERBOSITY default
179189
DROP TABLE test_hash;

contrib/pageinspect/ginfuncs.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ gin_metapage_info(PG_FUNCTION_ARGS)
4949

5050
page = get_page_from_raw(raw_page);
5151

52+
if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
53+
ereport(ERROR,
54+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55+
errmsg("input page is not a valid GIN metapage"),
56+
errdetail("Expected special size %d, got %d.",
57+
(int) MAXALIGN(sizeof(GinPageOpaqueData)),
58+
(int) PageGetSpecialSize(page))));
59+
5260
opaq = (GinPageOpaque) PageGetSpecialPointer(page);
5361
if (opaq->flags != GIN_META)
5462
ereport(ERROR,
@@ -107,6 +115,14 @@ gin_page_opaque_info(PG_FUNCTION_ARGS)
107115

108116
page = get_page_from_raw(raw_page);
109117

118+
if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
119+
ereport(ERROR,
120+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
121+
errmsg("input page is not a valid GIN data leaf page"),
122+
errdetail("Expected special size %d, got %d.",
123+
(int) MAXALIGN(sizeof(GinPageOpaqueData)),
124+
(int) PageGetSpecialSize(page))));
125+
110126
opaq = (GinPageOpaque) PageGetSpecialPointer(page);
111127

112128
/* Build a tuple descriptor for our result type */
@@ -188,9 +204,9 @@ gin_leafpage_items(PG_FUNCTION_ARGS)
188204
ereport(ERROR,
189205
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
190206
errmsg("input page is not a valid GIN data leaf page"),
191-
errdetail("Special size %d, expected %d",
192-
(int) PageGetSpecialSize(page),
193-
(int) MAXALIGN(sizeof(GinPageOpaqueData)))));
207+
errdetail("Expected special size %d, got %d.",
208+
(int) MAXALIGN(sizeof(GinPageOpaqueData)),
209+
(int) PageGetSpecialSize(page))));
194210

195211
opaq = (GinPageOpaque) PageGetSpecialPointer(page);
196212
if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))

contrib/pageinspect/hashfuncs.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ verify_hash_page(bytea *raw_page, int flags)
6666

6767
if (PageGetSpecialSize(page) != MAXALIGN(sizeof(HashPageOpaqueData)))
6868
ereport(ERROR,
69-
(errcode(ERRCODE_INDEX_CORRUPTED),
70-
errmsg("index table contains corrupted page")));
69+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
70+
errmsg("input page is not a valid %s page", "hash"),
71+
errdetail("Expected special size %d, got %d.",
72+
(int) MAXALIGN(sizeof(HashPageOpaqueData)),
73+
(int) PageGetSpecialSize(page))));
7174

7275
pageopaque = (HashPageOpaque) PageGetSpecialPointer(page);
7376
if (pageopaque->hasho_page_id != HASHO_PAGE_ID)
7477
ereport(ERROR,
7578
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76-
errmsg("page is not a hash page"),
79+
errmsg("input page is not a valid %s page", "hash"),
7780
errdetail("Expected %08x, got %08x.",
7881
HASHO_PAGE_ID, pageopaque->hasho_page_id)));
7982

@@ -134,7 +137,7 @@ verify_hash_page(bytea *raw_page, int flags)
134137
ereport(ERROR,
135138
(errcode(ERRCODE_INDEX_CORRUPTED),
136139
errmsg("invalid version for metadata"),
137-
errdetail("Expected %d, got %d",
140+
errdetail("Expected %d, got %d.",
138141
HASH_VERSION, metap->hashm_version)));
139142
}
140143

contrib/pageinspect/sql/brin.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ SELECT * FROM brin_page_items(get_raw_page('test1_a_idx', 2), 'test1_a_idx')
1919
CREATE INDEX test1_a_btree ON test1 (a);
2020
SELECT brin_page_items(get_raw_page('test1_a_btree', 0), 'test1_a_btree');
2121

22+
-- Mask DETAIL messages as these are not portable across architectures.
23+
\set VERBOSITY terse
24+
-- Invalid special area size
25+
SELECT brin_page_type(get_raw_page('test1', 0));
26+
SELECT * FROM brin_metapage_info(get_raw_page('test1', 0));
27+
SELECT * FROM brin_revmap_data(get_raw_page('test1', 0));
28+
\set VERBOSITY default
29+
2230
DROP TABLE test1;

contrib/pageinspect/sql/btree.sql

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE TABLE test1 (a int8, b text);
2-
INSERT INTO test1 VALUES (72057594037927937, 'text');
1+
CREATE TABLE test1 (a int8, b int4range);
2+
INSERT INTO test1 VALUES (72057594037927937, '[0,1)');
33
CREATE INDEX test1_a_idx ON test1 USING btree (a);
44

55
\x
@@ -23,12 +23,22 @@ CREATE INDEX test1_a_hash ON test1 USING hash(a);
2323
SELECT bt_metap('test1_a_hash');
2424
SELECT bt_page_stats('test1_a_hash', 0);
2525
SELECT bt_page_items('test1_a_hash', 0);
26+
SELECT bt_page_items(get_raw_page('test1_a_hash', 0));
27+
CREATE INDEX test1_b_gist ON test1 USING gist(b);
28+
-- Special area of GiST is the same as btree, this complains about inconsistent
29+
-- leaf data on the page.
30+
SELECT bt_page_items(get_raw_page('test1_b_gist', 0));
2631

27-
-- Failure with incorrect page size
32+
-- Several failure modes.
2833
-- Suppress the DETAIL message, to allow the tests to work across various
29-
-- page sizes.
34+
-- page sizes and architectures.
3035
\set VERBOSITY terse
36+
-- invalid page size
3137
SELECT bt_page_items('aaa'::bytea);
38+
-- invalid special area size
39+
CREATE INDEX test1_a_brin ON test1 USING brin(a);
40+
SELECT bt_page_items(get_raw_page('test1', 0));
41+
SELECT bt_page_items(get_raw_page('test1_a_brin', 0));
3242
\set VERBOSITY default
3343

3444
DROP TABLE test1;

contrib/pageinspect/sql/gin.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ FROM gin_leafpage_items(get_raw_page('test1_y_idx',
1818
(pg_relation_size('test1_y_idx') /
1919
current_setting('block_size')::bigint)::int - 1));
2020

21-
-- Failure with incorrect page size
21+
-- Failure with various modes.
2222
-- Suppress the DETAIL message, to allow the tests to work across various
23-
-- page sizes.
23+
-- page sizes and architectures.
2424
\set VERBOSITY terse
25+
-- invalid page size
2526
SELECT gin_leafpage_items('aaa'::bytea);
2627
SELECT gin_metapage_info('bbb'::bytea);
2728
SELECT gin_page_opaque_info('ccc'::bytea);
29+
-- invalid special area size
30+
SELECT * FROM gin_metapage_info(get_raw_page('test1', 0));
31+
SELECT * FROM gin_page_opaque_info(get_raw_page('test1', 0));
32+
SELECT * FROM gin_leafpage_items(get_raw_page('test1', 0));
2833
\set VERBOSITY default

contrib/pageinspect/sql/hash.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,20 @@ SELECT * FROM hash_page_items(get_raw_page('test_hash_a_idx', 5));
8080
CREATE INDEX test_hash_a_btree ON test_hash USING btree (a);
8181
SELECT hash_bitmap_info('test_hash_a_btree', 0);
8282

83-
-- Failure with incorrect page size
83+
-- Failure with various modes.
8484
-- Suppress the DETAIL message, to allow the tests to work across various
85-
-- page sizes.
85+
-- page sizes and architectures.
8686
\set VERBOSITY terse
87+
-- invalid page size
8788
SELECT hash_metapage_info('aaa'::bytea);
8889
SELECT hash_page_items('bbb'::bytea);
8990
SELECT hash_page_stats('ccc'::bytea);
9091
SELECT hash_page_type('ddd'::bytea);
92+
-- invalid special area size
93+
SELECT hash_metapage_info(get_raw_page('test_hash', 0));
94+
SELECT hash_page_items(get_raw_page('test_hash', 0));
95+
SELECT hash_page_stats(get_raw_page('test_hash', 0));
96+
SELECT hash_page_type(get_raw_page('test_hash', 0));
9197
\set VERBOSITY default
9298

9399
DROP TABLE test_hash;

0 commit comments

Comments
 (0)