shashank11yadav opened a new issue, #1013:
URL: https://github.com/apache/arrow-nanoarrow/issues/1013
### Describe the bug
`ArrowIpcDecoderVerifyFooter()` and `ArrowIpcDecoderDecodeFooter()` compute
`decoder->header_size_bytes + sizeof(int32_t) + strlen("ARROW1")` and store the
result in an `int32_t`. `ArrowIpcDecoderPeekFooter()` only rejects negative
footer sizes, so a footer size such as `0x7FFFFFFE` is accepted. The sum then
narrows to a negative value, the `data.size_bytes <
footer_and_size_and_magic_size` check passes, and `footer_data` points about 2
GiB past the buffer. Both functions then read through that pointer.
`ArrowIpcDecoderVerifyFooter()` is the call that is meant to ensure "that
decoding the data will not access memory outside of the buffer specified by
data".
### To reproduce
```c
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "nanoarrow/nanoarrow.h"
#include "nanoarrow/nanoarrow_ipc.h"
static _Alignas(8) uint8_t buf[16];
// which: 0 = VerifyFooter, 1 = PeekFooter + DecodeFooter
static int run(int which) {
struct ArrowBufferView data;
data.data.as_uint8 = buf;
data.size_bytes = sizeof(buf);
struct ArrowIpcDecoder decoder;
struct ArrowError error;
error.message[0] = '\0';
if (ArrowIpcDecoderInit(&decoder) != NANOARROW_OK) return 2;
int rc;
if (which == 0) {
rc = ArrowIpcDecoderVerifyFooter(&decoder, data, &error);
} else {
rc = ArrowIpcDecoderPeekFooter(&decoder, data, &error);
if (rc == NANOARROW_OK) rc = ArrowIpcDecoderDecodeFooter(&decoder, data,
&error);
}
printf("ACTUAL: %s returned %d (%s)\n", which ? "DecodeFooter" :
"VerifyFooter", rc,
error.message);
fflush(stdout);
ArrowIpcDecoderReset(&decoder);
return rc == ESPIPE ? 0 : 2;
}
int main(void) {
// 16-byte tail: footer size 0x7FFFFFFE, "ARROW1". size+10 = 0x80000008
narrows to a
// negative int32, so footer_data = buf + 16 + 0x7FFFFFF8 (8-aligned, ~2
GiB past buf)
int32_t footer_size = 0x7FFFFFFE;
memcpy(buf + 6, &footer_size, 4);
memcpy(buf + 10, "ARROW1", 6);
int bad = 0;
for (int which = 0; which < 2; which++) {
printf("EXPECTED: %s fails cleanly (ESPIPE=%d, footer larger than the
16-byte buffer)\n",
which ? "DecodeFooter" : "VerifyFooter", ESPIPE);
fflush(stdout);
pid_t pid = fork();
if (pid == 0) _exit(run(which));
int st = 0;
waitpid(pid, &st, 0);
const char* name = which ? "DecodeFooter" : "VerifyFooter";
if (WIFEXITED(st) && WEXITSTATUS(st) == 0) {
printf("ACTUAL: %s returned ESPIPE (clean)\n", name);
} else if (WIFEXITED(st) && WEXITSTATUS(st) == 2) {
printf("ACTUAL: %s returned a code other than ESPIPE (see line
above)\n", name);
bad = 1;
} else {
printf("ACTUAL: %s crashed (child %s %d, see AddressSanitizer report
above)\n", name,
WIFSIGNALED(st) ? "killed by signal" : "exit",
WIFSIGNALED(st) ? WTERMSIG(st) : WEXITSTATUS(st));
bad = 1;
}
fflush(stdout);
}
return bad;
}
```
Output on `main` (ee22361), built with `-std=c11 -g
-fsanitize=address,undefined` (AddressSanitizer reports trimmed):
```
EXPECTED: VerifyFooter fails cleanly (ESPIPE=29, footer larger than the
16-byte buffer)
AddressSanitizer:DEADLYSIGNAL
==8==The signal is caused by a READ memory access.
ACTUAL: VerifyFooter crashed (child exit 1, see AddressSanitizer report
above)
EXPECTED: DecodeFooter fails cleanly (ESPIPE=29, footer larger than the
16-byte buffer)
AddressSanitizer:DEADLYSIGNAL
==9==The signal is caused by a READ memory access.
ACTUAL: DecodeFooter crashed (child exit 1, see AddressSanitizer report
above)
```
Both are SEGVs on an address about 2 GiB past `buf`: the first in flatcc's
`read_uoffset` (verifier.c:108), called from `ArrowIpcDecoderVerifyFooter` at
decoder.c:1768; the second in `org_apache_arrow_flatbuf_Footer_as_root`
(flatcc_generated.h:5463), called from `ArrowIpcDecoderDecodeFooter` at
decoder.c:1967.
### Expected behavior
Both calls should return ESPIPE ("Expected >= ... bytes of data but only 16
bytes are in the buffer"), as they do for any smaller footer size that doesn't
fit. The `nanoarrow_ipc.h` doc for `ArrowIpcDecoderVerifyFooter()` says it
ensures "that decoding the data will not access memory outside of the buffer
specified by data". The same int32 narrowing was fixed for message headers in
#984.
### Versions
- nanoarrow 0.9.0 (same code at decoder.c:1589 and :1770) and `main` @
ee22361 both crash
- gcc 15.2.0, Linux aarch64; CMake Debug build with `-DNANOARROW_IPC=ON`
### Additional context
Root cause: `src/nanoarrow/ipc/decoder.c:1753-1754` (VerifyFooter) and
`:1963-1966` (DecodeFooter), where `int32_t footer_and_size_and_magic_size =
decoder->header_size_bytes + sizeof(int32_t) +
(int)strlen(NANOARROW_IPC_MAGIC);` narrows a size_t sum that can exceed
INT32_MAX. Doing the sum and comparison in `int64_t`, as #984 did for the
message prefix, should fix both. These functions are marked as public only for
integration testing.
I have a fix with a regression test ready and will open a pull request for
it right after filing this issue; it will link back here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]