pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: cilizili@protonmail.com
Subject: BUG #16082: TOAST's pglz_decompress access to uninitialized data, if  the database is corrupted.
Date: Sat, 26 Oct 2019 07:46:25 +0000
Message-ID: <16082-96c2ec8be45150cb@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16082
Logged by:          cili
Email address:      cilizili@protonmail.com
PostgreSQL version: 12.0
Operating system:   Microsoft Windows [Version 10.0.18362.418]
Description:        

The function pglz_decompress in src/common/pglz_decompress.c may refer
invalid data in the corrupted database file.
I show you two bad cases along with corrupted database file, and how to
make.

The first byte of TOAST structure is a control byte. If the LSB of control
byte is set, the 2nd byte is the length and the 3rd byte is an offset of
repeating bytes in dest block.
There is two case that they are valid for invalid data. In the case 1, it
reads an uninitialized data in the dest. In the case 2, it reads
uninitialized or out-of-bound data in the dest. They are invalid.
I'll show you the setup and one normal case, and then show two bad bug
cases.

Detail of case 1:
The first byte of TOAST structure in the corrupted database file is set
LSB.
The line 741 in pg_lzcompress.c read a byte from dp[-off] with off = 0, and
write to *dp.
It refers an uninitialized byte in the dest.

Detail of case 2:
The first byte of TOAST structure in the corrupted database file is not set
LSB, and the third byte such as the control byte in the second tag is set
LSB.
Similar to the case 2, the line 741 in pg_lzcompress.c read a byte from
dp[-off] with off > 0, and write to *dp.
It refers out-of-bound bytes in the dest.

Expected:
The program prevents the invalid accesses, and reports an error.

Examples:
= is an informal comment.
% is a CLI command in OS Shell.
# is a SQL command in SQL Shell.
%%% is a reminder.
*** is a instruction to corrupt the database.

===============================================
= [setup]
=   create database file.
=   insert data into database.
=   find the database filename. Then we modify the database file.
===============================================
% initdb testdb
% psql -d testdb
# CREATE TABLE test(id INTEGER, body BYTEA, PRIMARY KEY (id));
# INSERT INTO test values(1, convert_to(repeat('A', 32768), 'UTF8'));
# SELECT relfilenode from pg_class where relname like 'test';

 relfilenode
-------------
        16401

===============================================
= [normal case]
===============================================
# SELECT body from test;
\x41414141...

# \q

%%% Please remember the result of normal case.
%%% We modify the dabase file, and show two bad cases.

===============================================
= [bad case 1]
=   corrupt the databae file
===============================================
*** Edit the database file '16401' with case 1. Restart the PostgreSQL.
% psql -d testdb
# SELECT body from test;
\x000000...  ...0041

# \q

===============================================
= [bad case 2]
=   corrupt the databae file
===============================================

*** Edit the database file '16401' with case 2. Restart the PostgreSQL.
% psql -d testdb
# SELECT body from test;
\x410000...  ...0000
# \q

================================================
= example of database file '16401'
================================================
original:
00001E80   00 80 00 00 FE 41 0F 01 FF 0F 01 FF 0F 01 FF 0F

case 1:
00001E80   00 80 00 00 FD 0F FF FF 41 0F 01 FF 0F 01 FF 0F

case 2:
00001E80   00 80 00 00 FE 41 0F FF FF 0F 01 FF 0F 01 FF 0F



view thread (4+ messages)  latest in thread

Message-ID: <16082-96c2ec8be45150cb@postgresql.org>
Permalink:  ../16082-96c2ec8be45150cb@postgresql.org/
Also on:    postgresql.org/message-id/16082-96c2ec8be45150cb@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, cilizili@protonmail.com
  Subject: Re: BUG #16082: TOAST's pglz_decompress access to uninitialized data, if  the database is corrupted.
  In-Reply-To: <16082-96c2ec8be45150cb@postgresql.org>
  References: <16082-96c2ec8be45150cb@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox