apache / apache/arrow

[C++][Gandiva] Out-of-bounds read when formatting invalid date/timestamp error messages

Open
#50,440 0 comments 0 reactions 1 assignee Claimed by @Arawoof06 View on GitHub
Component: C++ Component: Gandiva
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

### Describe the bug

`set_error_for_date()` in `cpp/src/gandiva/precompiled/time.cc` builds the error message for an invalid date/timestamp with:

```c
int size = length + static_cast(strlen(msg)) + 1;
char* error = reinterpret_cast(malloc(size));
snprintf(error, size, "%s%s", msg, input);
```

`input` is a Gandiva string given as pointer + `length`; it is not NUL-terminated (string values are stored contiguously in the Arrow values buffer). The `%s` conversion scans `input` for a NUL terminator, so it reads past the `length` bytes and, for the last value in a buffer, past the buffer end.

It is reached from `castDATE_utf8` / `castTIMESTAMP_utf8` (i.e. `CAST(str AS DATE/TIMESTAMP)`) whenever the input is not a valid date/timestamp, which is fully attacker-controlled.

Every other error formatter in the precompiled sources uses the bounded `%.*s` form with the explicit length. Using it here fixes the over-read.

### Component(s)

C++, Gandiva

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.