| Lists: | pgsql-hackers |
|---|
| From: | Alexander Kuznetsov <kuznetsovam(at)altlinux(dot)org> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Detect buffer underflow in get_th() |
| Date: | 2024-07-24 09:43:19 |
| Message-ID: | e22df993-cdb4-4d0a-b629-42211ebed582@altlinux.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
Hello everyone,
In src/backend/utils/adt/formatting.c:1516, there is a get_th() function utilized to return ST/ND/RD/TH suffixes for simple numbers.
Upon reviewing its behavior, it appears capable of receiving non-numeric inputs (this is verified by a check at formatting.c:1527).
Given that the function can accept non-numeric inputs,
it is plausible that it could also receive an empty input,
although a brief examination of its calls did not reveal any such instances.
Nevertheless, if the function were to receive an empty input of zero length,
a buffer underflow would occur when attempting to compute *(num + (len - 1)), as (len - 1) would result in a negative shift.
To mitigate this issue, I propose a patch incorporating the zero_length_character_string error code, as detailed in the attachment.
--
Best regards,
Alexander Kuznetsov
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Detect-buffer-underflow-in-get_th.patch | text/x-patch | 1.1 KB |
| From: | Peter Eisentraut <peter(at)eisentraut(dot)org> |
|---|---|
| To: | kuznetsovam(at)altlinux(dot)org, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Re: Detect buffer underflow in get_th() |
| Date: | 2024-07-24 15:39:00 |
| Message-ID: | f884009d-4fe1-4e8d-920c-f0f6a5cc9e11@eisentraut.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
On 24.07.24 11:43, Alexander Kuznetsov wrote:
> Hello everyone,
>
> In src/backend/utils/adt/formatting.c:1516, there is a get_th() function
> utilized to return ST/ND/RD/TH suffixes for simple numbers.
> Upon reviewing its behavior, it appears capable of receiving non-numeric
> inputs (this is verified by a check at formatting.c:1527).
>
> Given that the function can accept non-numeric inputs,
> it is plausible that it could also receive an empty input,
> although a brief examination of its calls did not reveal any such
> instances.
>
> Nevertheless, if the function were to receive an empty input of zero
> length,
> a buffer underflow would occur when attempting to compute *(num + (len -
> 1)), as (len - 1) would result in a negative shift.
> To mitigate this issue, I propose a patch incorporating the
> zero_length_character_string error code, as detailed in the attachment.
If it can't happen in practice, maybe an assertion would be enough?
| From: | Alexander Kuznetsov <kuznetsovam(at)altlinux(dot)org> |
|---|---|
| To: | Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Re: Detect buffer underflow in get_th() |
| Date: | 2024-07-24 15:53:53 |
| Message-ID: | f2f28c76-eb34-4e0c-a5fa-f1da52918eb0@altlinux.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
24.07.2024 18:39, Peter Eisentraut wrote:
> If it can't happen in practice, maybe an assertion would be enough?
>
In practice, the function should not receive non-numeric strings either; however, since there is an exception in place for such cases, I thought it would be good to add a check for zero-length input in a similar manner.
But of course it's open for discussion and team decision whether this should be addressed as an assertion or handled differently.
--
Best regards,
Alexander Kuznetsov
| From: | Alexander Kuznetsov <kuznetsovam(at)altlinux(dot)org> |
|---|---|
| To: | Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Re: Detect buffer underflow in get_th() |
| Date: | 2024-09-24 14:52:32 |
| Message-ID: | 03df4973-3595-4078-a075-636bbb121f61@altlinux.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
Hello,
is there anything else we can help with or discuss in order to apply this fix?
24.07.2024 18:53, Alexander Kuznetsov пишет:
>
> 24.07.2024 18:39, Peter Eisentraut wrote:
>> If it can't happen in practice, maybe an assertion would be enough?
>>
>
> In practice, the function should not receive non-numeric strings either; however, since there is an exception in place for such cases, I thought it would be good to add a check for zero-length input in a similar manner.
>
> But of course it's open for discussion and team decision whether this should be addressed as an assertion or handled differently.
>
--
Best regards,
Alexander Kuznetsov
| From: | Alexander Kuznetsov <kuznetsovam(at)altlinux(dot)org> |
|---|---|
| To: | Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Re: Detect buffer underflow in get_th() |
| Date: | 2024-12-13 08:57:18 |
| Message-ID: | 0211f6ab-efc0-43c9-9765-bf40b592c1fa@altlinux.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
Hello, ping?
24.09.2024 17:52, Alexander Kuznetsov wrote:
> Hello,
>
> is there anything else we can help with or discuss in order to apply this fix?
>
> 24.07.2024 18:53, Alexander Kuznetsov пишет:
>>
>> 24.07.2024 18:39, Peter Eisentraut wrote:
>>> If it can't happen in practice, maybe an assertion would be enough?
>>>
>>
>> In practice, the function should not receive non-numeric strings either; however, since there is an exception in place for such cases, I thought it would be good to add a check for zero-length input in a similar manner.
>>
>> But of course it's open for discussion and team decision whether this should be addressed as an assertion or handled differently.
>>
>
--
Best regards,
Alexander Kuznetsov
| From: | Peter Eisentraut <peter(at)eisentraut(dot)org> |
|---|---|
| To: | kuznetsovam(at)altlinux(dot)org, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | nickel(at)altlinux(dot)org, egori(at)altlinux(dot)org |
| Subject: | Re: Detect buffer underflow in get_th() |
| Date: | 2025-08-18 09:16:28 |
| Message-ID: | f9b9fdc7-4131-4722-ae6b-7ab4fc1f6ead@eisentraut.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Lists: | pgsql-hackers |
On 13.12.24 09:57, Alexander Kuznetsov wrote:
> Hello, ping?
>
> 24.09.2024 17:52, Alexander Kuznetsov wrote:
>> Hello,
>>
>> is there anything else we can help with or discuss in order to apply
>> this fix?
>>
>> 24.07.2024 18:53, Alexander Kuznetsov пишет:
>>>
>>> 24.07.2024 18:39, Peter Eisentraut wrote:
>>>> If it can't happen in practice, maybe an assertion would be enough?
>>>>
>>>
>>> In practice, the function should not receive non-numeric strings
>>> either; however, since there is an exception in place for such cases,
>>> I thought it would be good to add a check for zero-length input in a
>>> similar manner.
>>>
>>> But of course it's open for discussion and team decision whether this
>>> should be addressed as an assertion or handled differently.
After some further (internal) consultations, I have committed your fix
as an assertion. Thanks.