Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ca2aa2

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 thefunction caller, expecting to find some data in it. However, feedingan incorrect page can lead to out-of-bound reads when trying to accessthe page special area (like a heap page that has no special area,leading PageGetSpecialPointer() to grab a pointer outside the allocatedpage).The functions used for hash and btree indexes have some protectionalready against that, while some other functions using a relation OIDas argument would make sure that the access method involved is correct,but functions taking in input a raw page without knowing the relationthe 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-zerolevel), GIN and GiST to verify that the page given in input has aspecial area size that fits with each access method, which is donethough PageGetSpecialSize(), becore calling PageGetSpecialPointer().The scope of the checks done is limited to work with pages that onewould pass after getting a block with get_raw_page(), as it is possibleto craft byteas that could bypass existing code paths. Having too manychecks would also impact the usability of pageinspect, as the existingcode 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 agood thing even with functions whose execution is limited tosuperusers.The safest approach could be to rework the functions so as these fetch ablock using a relation OID and a block number, but there are also caseswhere using a raw page is useful.Tests are added to cover all the code paths that needed such checks, andan error message for hash indexes is reworded to fit better with whatthis commit adds.Reported-By: Alexander LakhinAuthor: Julien Rouhaud, Michael PaquierDiscussion:https://postgr.es/m/16527-ef7606186f0610a1@postgresql.orgDiscussion:https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ruBackpatch-through: 10
1 parentcb8586d commit5ca2aa2

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
@@ -60,6 +60,15 @@ brin_page_type(PG_FUNCTION_ARGS)
6060

6161
page=get_page_from_raw(raw_page);
6262

63+
/* verify the special space has the expected size */
64+
if (PageGetSpecialSize(page)!=MAXALIGN(sizeof(BrinSpecialSpace)))
65+
ereport(ERROR,
66+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
67+
errmsg("input page is not a valid %s page","BRIN"),
68+
errdetail("Expected special size %d, got %d.",
69+
(int)MAXALIGN(sizeof(BrinSpecialSpace)),
70+
(int)PageGetSpecialSize(page))));
71+
6372
switch (BrinPageType(page))
6473
{
6574
caseBRIN_PAGETYPE_META:
@@ -88,6 +97,15 @@ verify_brin_page(bytea *raw_page, uint16 type, const char *strtype)
8897
{
8998
Pagepage=get_page_from_raw(raw_page);
9099

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

‎contrib/pageinspect/btreefuncs.c

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

451451
uargs->offset=FirstOffsetNumber;
452452

453+
/* verify the special space has the expected size */
454+
if (PageGetSpecialSize(uargs->page)!=MAXALIGN(sizeof(BTPageOpaqueData)))
455+
ereport(ERROR,
456+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
457+
errmsg("input page is not a valid %s page","btree"),
458+
errdetail("Expected special size %d, got %d.",
459+
(int)MAXALIGN(sizeof(BTPageOpaqueData)),
460+
(int)PageGetSpecialSize(uargs->page))));
461+
453462
opaque= (BTPageOpaque)PageGetSpecialPointer(uargs->page);
454463

455464
if (P_ISMETA(opaque))
456465
ereport(ERROR,
457466
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
458467
errmsg("block is a meta page")));
459468

469+
if (P_ISLEAF(opaque)&&opaque->btpo.level!=0)
470+
ereport(ERROR,
471+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
472+
errmsg("block is not a valid btree leaf page")));
473+
460474
if (P_ISDELETED(opaque))
461475
elog(NOTICE,"page is deleted");
462476

‎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, btext);
2-
INSERT INTO test1 VALUES (72057594037927937, 'text');
1+
CREATE TABLE test1 (a int8, bint4range);
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');
@@ -65,11 +65,25 @@ SELECT bt_page_stats('test1_a_hash', 0);
6565
ERROR: "test1_a_hash" is not a btree index
6666
SELECT bt_page_items('test1_a_hash', 0);
6767
ERROR: "test1_a_hash" is not a btree index
68-
-- Failure with incorrect page size
68+
SELECT bt_page_items(get_raw_page('test1_a_hash', 0));
69+
ERROR: block is a meta page
70+
CREATE INDEX test1_b_gist ON test1 USING gist(b);
71+
-- Special area of GiST is the same as btree, this complains about inconsistent
72+
-- leaf data on the page.
73+
SELECT bt_page_items(get_raw_page('test1_b_gist', 0));
74+
ERROR: block is not a valid btree leaf page
75+
-- Several failure modes.
6976
-- Suppress the DETAIL message, to allow the tests to work across various
70-
-- page sizes.
77+
-- page sizes and architectures.
7178
\set VERBOSITY terse
79+
-- invalid page size
7280
SELECT bt_page_items('aaa'::bytea);
7381
ERROR: invalid page size
82+
-- invalid special area size
83+
CREATE INDEX test1_a_brin ON test1 USING brin(a);
84+
SELECT bt_page_items(get_raw_page('test1', 0));
85+
ERROR: input page is not a valid btree page
86+
SELECT bt_page_items(get_raw_page('test1_a_brin', 0));
87+
ERROR: input page is not a valid btree page
7488
\set VERBOSITY default
7589
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 withincorrect page size
38+
-- Failure withvarious 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 withincorrect page size
166+
-- Failure withvarious 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
@@ -50,6 +50,14 @@ gin_metapage_info(PG_FUNCTION_ARGS)
5050

5151
page=get_page_from_raw(raw_page);
5252

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

109117
page=get_page_from_raw(raw_page);
110118

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

113129
/* 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("Specialsize %d,expected %d",
192-
(int)PageGetSpecialSize(page),
193-
(int)MAXALIGN(sizeof(GinPageOpaqueData)))));
207+
errdetail("Expected specialsize %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 ahashpage"),
79+
errmsg("inputpage is not avalid %spage","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
CREATEINDEXtest1_a_btreeON 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
DROPTABLE 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-
CREATETABLEtest1 (a int8, btext);
2-
INSERT INTO test1VALUES (72057594037927937,'text');
1+
CREATETABLEtest1 (a int8, bint4range);
2+
INSERT INTO test1VALUES (72057594037927937,'[0,1)');
33
CREATEINDEXtest1_a_idxON 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+
CREATEINDEXtest1_b_gistON 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+
CREATEINDEXtest1_a_brinON 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
DROPTABLE 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 withincorrect page size
21+
-- Failure withvarious 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
CREATEINDEXtest_hash_a_btreeON test_hash USING btree (a);
8181
SELECT hash_bitmap_info('test_hash_a_btree',0);
8282

83-
-- Failure withincorrect page size
83+
-- Failure withvarious 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
DROPTABLE test_hash;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp