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: thiagotnunes@gmail.com
Subject: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template
Date: Tue, 14 Mar 2023 01:51:09 +0000
Message-ID: <17839-aada50db24d7b0da@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      17839
Logged by:          Thiago Nunes
Email address:      thiagotnunes@gmail.com
PostgreSQL version: 15.2
Operating system:   Linux
Description:        

Heap-buffer overflow on float8_to_char when format exceeds max double
digits. I noticed this when running tests with memory sanitiser (msan).

The following example triggers the failure (considering max double digits
`DBL_DIG` is 15):

```
float8_to_char(12345678901, "FM9999999999D999990")
```

Explanation below:

1. After parsing the format, `Num.pre` will be 10, `Num.post` will be 6
`Num.zero_end` will be 16
(https://github.com/postgres/postgres/blob/REL_15_2/src/backend/utils/adt/formatting.c#L1196-L1228)
2. The template size is greater than the `DBL_DIG`, `Num.post` will be moved
back here
(https://github.com/postgres/postgres/blob/REL_15_2/src/backend/utils/adt/formatting.c#L6688-L6689).
3. The shortened template with the max `DBL_DIG` will be "stringfied" out
here
(https://github.com/postgres/postgres/blob/REL_15_2/src/backend/utils/adt/formatting.c#L6712-L6717).
The result will be "##########.####" (10 significant digits + '.' + 4
decimal digits).
4. `Np->last_relevant` will be lesser than `Num->zero_end`, so it is updated
to an invalid position in the result above (pointer + 16) here
(https://github.com/postgres/postgres/blob/REL_15_2/src/backend/utils/adt/formatting.c#L5740-L5743).
5. When applying FILLMODE here
(https://github.com/postgres/postgres/blob/REL_15_2/src/backend/utils/adt/formatting.c#L5563),
it will try to get the character at Np->last_relevant, which is out of
bounds.



view thread (5+ messages)  latest in thread

Message-ID: <17839-aada50db24d7b0da@postgresql.org>
Permalink:  ../17839-aada50db24d7b0da@postgresql.org/
Also on:    postgresql.org/message-id/17839-aada50db24d7b0da@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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, thiagotnunes@gmail.com
  Subject: Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template
  In-Reply-To: <17839-aada50db24d7b0da@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