Skip to contentSkip to conversation

util/readline: Fix out-of-bounds access in readline_insert_char().

PATCH In review [email protected] 2 participants 5 messages Apr 2 – Apr 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/readline: Fix out-of-bounds access in readline_insert_char().

From: Nguyen Dinh Phi <[email protected]>

Currently, the readline_insert_char() function is guarded by the cursor position (cmd_buf_index) rather than the actual buffer fill level(cmd_buf_size). The current check is:

	if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE)

This logic is flawed because if the command buffer is full and a user moves the cursor backward (e.g. by sending left arrow key), cmd_buf_index can be decreased without descreasing of buffer size.
This allow subsequent insertions to increase cmd_buf_size past its maximum limit of rs->cmd_buf.

Because in the ReadLineState struct, cmd_buf[READLINE_CMD_BUF_SIZE + 1] is immediately followed by the cmd_buf_index integer, once the buffer size is sufficiently inflated, the memmove() operation inside readline_insert_char() can write past the end of cmd_buf[] and overwrite cmd_buf_index itself.

The subsequent line:

	rs->cmd_buf[rs->cmd_buf_index] = ch;

then writes the input character to an address determined by the now-corrupted index.

By providing a specifically crafted input sequence via HMP, this flaw can be used to redirect the write operation to overwrite any field within the ReadLineState structure, which can lead to unpredictable behavior or application crashes.

Fix this by adding the guard to check for buffer fullness and ensuring the index remains valid.

---
 util/readline.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/readline.c +2 -1
index 0f19674f52..6b1de1094a 100644
@@ -84,7 +84,8 @@ static void readline_update(ReadLineState *rs)
 
 static void readline_insert_char(ReadLineState *rs, int ch)
 {
-    if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
+    if (rs->cmd_buf_index <= rs->cmd_buf_size &&
+            rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) {
         memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
                 rs->cmd_buf + rs->cmd_buf_index,
                 rs->cmd_buf_size - rs->cmd_buf_index);
Signature · 1 line
2.43.0
#1 lore raw

Hi

Quoted 43 lines · On Thu, Apr 2, 2026 at 3:01 PM <[email protected]> wrote:

From: Nguyen Dinh Phi <[email protected]>

Currently, the readline_insert_char() function is guarded by the cursor position (cmd_buf_index) rather than the actual buffer fill level(cmd_buf_size). The current check is:

        if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE)

This logic is flawed because if the command buffer is full and a user moves the cursor backward (e.g. by sending left arrow key), cmd_buf_index can be decreased without descreasing of buffer size.
This allow subsequent insertions to increase cmd_buf_size past its maximum limit of rs->cmd_buf.

Because in the ReadLineState struct, cmd_buf[READLINE_CMD_BUF_SIZE + 1] is immediately followed by the cmd_buf_index integer, once the buffer size is sufficiently inflated, the memmove() operation inside readline_insert_char() can write past the end of cmd_buf[] and overwrite cmd_buf_index itself.

The subsequent line:

        rs->cmd_buf[rs->cmd_buf_index] = ch;

then writes the input character to an address determined by the now-corrupted index.

By providing a specifically crafted input sequence via HMP, this flaw can be used to redirect the write operation to overwrite any field within the ReadLineState structure, which can lead to unpredictable behavior or application crashes.

Fix this by adding the guard to check for buffer fullness and ensuring the index remains valid.

---
 util/readline.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/readline.c +0 -0
index 0f19674f52..6b1de1094a 100644

util/readline.c static void readline_update(ReadLineState *rs) · line 88

Quoted 6 lines
util/readline.c +2 -1
@@ -84,7 +84,8 @@ static void readline_update(ReadLineState *rs)
 
 static void readline_insert_char(ReadLineState *rs, int ch)
 {
-    if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
+    if (rs->cmd_buf_index <= rs->cmd_buf_size &&
+            rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) {

Why change rs->cmd_buf_index < READLINE_CMD_BUF_SIZE to rs->cmd_buf_index <= READLINE_CMD_BUF_SIZE ?

otherwise, good catch

Quoted 7 lines
util/readline.c +0 -1
         memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
                 rs->cmd_buf + rs->cmd_buf_index,
                 rs->cmd_buf_size - rs->cmd_buf_index);
--

2.43.0

Signature · 1 line
Marc-André Lureau
author
#2 lore raw
Quoted 2 lines · Marc-André Lureau

Why change rs->cmd_buf_index < READLINE_CMD_BUF_SIZE to rs->cmd_buf_index <= READLINE_CMD_BUF_SIZE ?

Hi,
my patch is actually change from

    if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE)

to

    if (rs->cmd_buf_index <= rs->cmd_buf_size &&
            rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) {

BR,
Phi

#3 lore raw

Hi

Quoted 4 lines · Phi Nguyen
Quoted 2 lines · Marc-André Lureau

Why change rs->cmd_buf_index < READLINE_CMD_BUF_SIZE to rs->cmd_buf_index <= READLINE_CMD_BUF_SIZE ?

Quoted 8 lines

Hi,
my patch is actually change from

     if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE)

to

     if (rs->cmd_buf_index <= rs->cmd_buf_size &&
             rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) {

Right, my bad. Wouldn't it be a programming error if "cmd_buf_index > rs->cmd_buf_size" ?

Maybe make this an assert() ?

Signature · 1 line
Marc-André Lureau
#4 lore raw
Quoted 16 lines · Marc-André Lureau

Hi

Quoted 4 lines · Phi Nguyen
Quoted 2 lines · Marc-André Lureau

Why change rs->cmd_buf_index < READLINE_CMD_BUF_SIZE to rs->cmd_buf_index <= READLINE_CMD_BUF_SIZE ?

Quoted 8 lines

Hi,
my patch is actually change from

      if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE)

to

      if (rs->cmd_buf_index <= rs->cmd_buf_size &&
              rs->cmd_buf_size < READLINE_CMD_BUF_SIZE) {
Quoted 5 lines

Right, my bad. Wouldn't it be a programming error if "cmd_buf_index > rs->cmd_buf_size" ?

Maybe make this an assert() ?

Hi,

That's right, I will send a new version to add the assert()

BR,
Phi