Skip to content

Commit da53d0c

Browse files
AntonMoryakovbvanassche
authored andcommitted
fix prevent buffer overflow in ASN_OCTET_STR index allocation
In register_index(), when generating sequential string indices from 'prev_idx_ptr', the code used a flawed algorithm that could lead to buffer overflow by writing beyond the end of new_index->varbind->buf. The loop condition relied on buf[i] without proper bounds checking, and the fallback logic for growing the string wrote to buf[i+1] without validating available space. Rewrite the octet string increment logic to: - Properly check array bounds using i >= 0 - Use memmove to shift string left when growing - Validate buffer capacity before extending - Update val_len correctly Now returns NULL if buffer is full, preventing memor Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
1 parent 174b6cd commit da53d0c

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

agent/agent_index.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,33 @@ register_index(netsnmp_variable_list * varbind, int flags,
346346
break;
347347
case ASN_OCTET_STR:
348348
if (prev_idx_ptr) {
349+
size_t maxlen = sizeof(new_index->varbind->buf);
349350
i = new_index->varbind->val_len - 1;
350-
while (new_index->varbind->buf[i] == 'z') {
351+
352+
while (i >= 0 && new_index->varbind->buf[i] == 'z') {
351353
new_index->varbind->buf[i] = 'a';
352354
i--;
353-
if (i < 0) {
354-
i = new_index->varbind->val_len;
355-
new_index->varbind->buf[i] = 'a';
356-
new_index->varbind->buf[i + 1] = 0;
355+
}
356+
357+
if (i >= 0) {
358+
new_index->varbind->buf[i]++;
359+
} else {
360+
/* All 'z's — need to grow */
361+
if (new_index->varbind->val_len + 1 < maxlen) {
362+
memmove(new_index->varbind->buf + 1, new_index->varbind->buf,
363+
new_index->varbind->val_len);
364+
new_index->varbind->buf[0] = 'a';
365+
new_index->varbind->val_len++;
366+
} else {
367+
/* Buffer full — cannot grow */
368+
free(new_index);
369+
return NULL;
357370
}
358371
}
359-
new_index->varbind->buf[i]++;
360-
} else
372+
} else {
361373
strcpy((char *) new_index->varbind->buf, "aaaa");
362-
new_index->varbind->val_len =
363-
strlen((char *) new_index->varbind->buf);
374+
new_index->varbind->val_len = 4;
375+
}
364376
break;
365377
case ASN_OBJECT_ID:
366378
if (prev_idx_ptr) {

0 commit comments

Comments
 (0)