Skip to content

Commit 7b93210

Browse files
DeusDataSS-42
andcommitted
fix(cli): overflow-safe ZIP entry bounds check in self-update extraction
The truncation check `header_end + (int)comp_size > data_len` was bypassable for comp_size >= 2^31: the int cast turns the size negative, the sum drops below data_len, and the entry is accepted -- authorizing inflate to read up to ~4GB past the downloaded archive buffer during self-update ZIP extraction. Replace it with `header_end > data_len || comp_size > (uint32_t)(data_len - header_end)`, which is overflow-safe and strictly tighter. Regression guard cli_extract_binary_from_zip_rejects_truncated_deflate_size_over_int_max builds a 52-byte archive whose entry claims comp_size=0xFFFF0000 with a self-terminating DEFLATE stream: the old check admits it and extraction returns non-NULL (verified red pre-fix); the new check rejects it. Secondary cppcheck cleanups from the same review: - cli.c: extract zip_read_u16le/zip_read_u32le little-endian helpers (behavior-identical) and widen zip_extract_entry sizes to size_t with an explicit UINT_MAX guard before the zlib uInt narrowing. - cypher.c: drop unreachable IS NULL / IS NOT NULL null guards in eval_condition (resolve_condition_value result is checked earlier). Distilled from PR DeusData#784. Co-authored-by: SS-42 <noreply@incogni.to> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 09148ab commit 7b93210

3 files changed

Lines changed: 69 additions & 26 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ enum {
8080
#endif
8181
#include <errno.h> // EEXIST
8282
#include <fcntl.h> // open, O_WRONLY, O_CREAT, O_TRUNC
83+
#include <limits.h> // UINT_MAX
8384
#include <stdint.h> // uintptr_t
8485
#include <stdio.h>
8586
#include <stdlib.h>
@@ -2485,12 +2486,22 @@ enum {
24852486
ZIP_STORED = 0,
24862487
ZIP_DEFLATE = 8
24872488
};
2488-
static const uint32_t ZIP_MAX_UNCOMP = 500U * 1024U * 1024U;
2489+
static const size_t ZIP_MAX_UNCOMP = 500U * 1024U * 1024U;
2490+
2491+
static uint16_t zip_read_u16le(const unsigned char *p) {
2492+
return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << BYTE_SHIFT));
2493+
}
2494+
2495+
static uint32_t zip_read_u32le(const unsigned char *p) {
2496+
return ((uint32_t)p[0]) | ((uint32_t)p[1] << BYTE_SHIFT) |
2497+
((uint32_t)p[2] << (BYTE_SHIFT * CLI_PAIR_LEN)) |
2498+
((uint32_t)p[3] << (BYTE_SHIFT * CLI_JSON_INDENT));
2499+
}
24892500

24902501
/* Decompress a single zip entry (stored or deflated). Returns malloc'd buffer
24912502
* or NULL on failure. *out_len receives the decompressed size. */
24922503
static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t method,
2493-
uint32_t comp_size, uint32_t uncomp_size, int *out_len) {
2504+
size_t comp_size, size_t uncomp_size, int *out_len) {
24942505
if (method == ZIP_STORED) {
24952506
if (comp_size > ZIP_MAX_UNCOMP) {
24962507
return NULL;
@@ -2507,15 +2518,18 @@ static unsigned char *zip_extract_entry(const unsigned char *file_data, uint16_t
25072518
if (uncomp_size > ZIP_MAX_UNCOMP) {
25082519
return NULL;
25092520
}
2521+
if (comp_size > UINT_MAX || uncomp_size > UINT_MAX) {
2522+
return NULL;
2523+
}
25102524
unsigned char *out = malloc(uncomp_size);
25112525
if (!out) {
25122526
return NULL;
25132527
}
25142528
z_stream strm = {0};
25152529
strm.next_in = (unsigned char *)file_data;
2516-
strm.avail_in = comp_size;
2530+
strm.avail_in = (uInt)comp_size;
25172531
strm.next_out = out;
2518-
strm.avail_out = uncomp_size;
2532+
strm.avail_out = (uInt)uncomp_size;
25192533
if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
25202534
free(out);
25212535
return NULL;
@@ -2545,28 +2559,14 @@ unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_l
25452559
break;
25462560
}
25472561

2548-
uint16_t method = (uint16_t)(data[pos + ZIP_OFF_METHOD] |
2549-
(data[pos + ZIP_OFF_METHOD + CLI_SKIP_ONE] << BYTE_SHIFT));
2550-
uint32_t comp_size =
2551-
(uint32_t)(data[pos + ZIP_OFF_COMP] |
2552-
(data[pos + ZIP_OFF_COMP + CLI_SKIP_ONE] << BYTE_SHIFT) |
2553-
(data[pos + ZIP_OFF_COMP + CLI_PAIR_LEN] << (BYTE_SHIFT * CLI_PAIR_LEN)) |
2554-
(data[pos + ZIP_OFF_COMP + CLI_JSON_INDENT]
2555-
<< (BYTE_SHIFT * CLI_JSON_INDENT)));
2556-
uint32_t uncomp_size =
2557-
(uint32_t)(data[pos + ZIP_OFF_UNCOMP] |
2558-
(data[pos + ZIP_OFF_UNCOMP + CLI_SKIP_ONE] << BYTE_SHIFT) |
2559-
(data[pos + ZIP_OFF_UNCOMP + CLI_PAIR_LEN] << (BYTE_SHIFT * CLI_PAIR_LEN)) |
2560-
(data[pos + ZIP_OFF_UNCOMP + CLI_JSON_INDENT]
2561-
<< (BYTE_SHIFT * CLI_JSON_INDENT)));
2562-
uint16_t name_len = (uint16_t)(data[pos + ZIP_OFF_NAMELEN] |
2563-
(data[pos + ZIP_OFF_NAMELEN + CLI_SKIP_ONE] << BYTE_SHIFT));
2564-
uint16_t extra_len =
2565-
(uint16_t)(data[pos + ZIP_OFF_EXTRALEN] |
2566-
(data[pos + ZIP_OFF_EXTRALEN + CLI_SKIP_ONE] << BYTE_SHIFT));
2562+
uint16_t method = zip_read_u16le(data + pos + ZIP_OFF_METHOD);
2563+
uint32_t comp_size = zip_read_u32le(data + pos + ZIP_OFF_COMP);
2564+
uint32_t uncomp_size = zip_read_u32le(data + pos + ZIP_OFF_UNCOMP);
2565+
uint16_t name_len = zip_read_u16le(data + pos + ZIP_OFF_NAMELEN);
2566+
uint16_t extra_len = zip_read_u16le(data + pos + ZIP_OFF_EXTRALEN);
25672567

25682568
int header_end = pos + ZIP_HDR_SZ + name_len + extra_len;
2569-
if (header_end + (int)comp_size > data_len) {
2569+
if (header_end > data_len || comp_size > (uint32_t)(data_len - header_end)) {
25702570
break;
25712571
}
25722572

‎src/cypher/cypher.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,11 +2441,11 @@ static bool eval_condition(const cbm_condition_t *c, binding_t *b) {
24412441

24422442
/* IS NULL / IS NOT NULL */
24432443
if (strcmp(c->op, "IS NULL") == 0) {
2444-
result = (!actual || actual[0] == '\0');
2444+
result = (actual[0] == '\0');
24452445
return c->negated ? !result : result;
24462446
}
24472447
if (strcmp(c->op, "IS NOT NULL") == 0) {
2448-
result = (actual && actual[0] != '\0');
2448+
result = (actual[0] != '\0');
24492449
return c->negated ? !result : result;
24502450
}
24512451

‎tests/test_cli.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "test_helpers.h"
1515
#include <cli/cli.h>
1616
#include <foundation/yaml.h>
17+
#include <stdint.h>
1718
#include <string.h>
1819
#include <stdlib.h>
1920
#include <stdio.h>
@@ -1361,6 +1362,47 @@ TEST(cli_extract_binary_from_zip_invalid) {
13611362
PASS();
13621363
}
13631364

1365+
TEST(cli_extract_binary_from_zip_rejects_truncated_deflate_size_over_int_max) {
1366+
const char *filename = "codebase-memory-mcp";
1367+
const unsigned char deflated[] = {0xAB, 0x00, 0x00}; /* raw DEFLATE for "x" */
1368+
size_t name_len = strlen(filename);
1369+
size_t zip_len = 30 + name_len + sizeof(deflated);
1370+
unsigned char *zip = calloc(1, zip_len);
1371+
ASSERT_NOT_NULL(zip);
1372+
1373+
uint32_t comp_size = 0xFFFF0000U;
1374+
uint32_t uncomp_size = 1U;
1375+
zip[0] = 0x50;
1376+
zip[1] = 0x4B;
1377+
zip[2] = 0x03;
1378+
zip[3] = 0x04;
1379+
zip[8] = 8;
1380+
zip[9] = 0;
1381+
zip[18] = (unsigned char)(comp_size & 0xFF);
1382+
zip[19] = (unsigned char)((comp_size >> 8) & 0xFF);
1383+
zip[20] = (unsigned char)((comp_size >> 16) & 0xFF);
1384+
zip[21] = (unsigned char)((comp_size >> 24) & 0xFF);
1385+
zip[22] = (unsigned char)(uncomp_size & 0xFF);
1386+
zip[23] = (unsigned char)((uncomp_size >> 8) & 0xFF);
1387+
zip[24] = (unsigned char)((uncomp_size >> 16) & 0xFF);
1388+
zip[25] = (unsigned char)((uncomp_size >> 24) & 0xFF);
1389+
zip[26] = (unsigned char)(name_len & 0xFF);
1390+
zip[27] = (unsigned char)((name_len >> 8) & 0xFF);
1391+
memcpy(zip + 30, filename, name_len);
1392+
memcpy(zip + 30 + name_len, deflated, sizeof(deflated));
1393+
1394+
int out_len = 0;
1395+
unsigned char *extracted = cbm_extract_binary_from_zip(zip, (int)zip_len, &out_len);
1396+
if (extracted) {
1397+
free(extracted);
1398+
free(zip);
1399+
FAIL("accepted a truncated deflated zip entry with a wrapped compressed size");
1400+
}
1401+
ASSERT_EQ(out_len, 0);
1402+
free(zip);
1403+
PASS();
1404+
}
1405+
13641406
/* ═══════════════════════════════════════════════════════════════════
13651407
* Skill dry-run tests
13661408
* ═══════════════════════════════════════════════════════════════════ */
@@ -2981,6 +3023,7 @@ SUITE(cli) {
29813023
RUN_TEST(cli_extract_binary_from_zip_not_found);
29823024
RUN_TEST(cli_extract_binary_from_zip_path_traversal);
29833025
RUN_TEST(cli_extract_binary_from_zip_invalid);
3026+
RUN_TEST(cli_extract_binary_from_zip_rejects_truncated_deflate_size_over_int_max);
29843027

29853028
/* Dry-run lifecycle (2 tests) */
29863029
RUN_TEST(cli_install_dry_run);

0 commit comments

Comments
 (0)