iconv out-of-bounds write vulnerability reproducible via libc::iconv in Rust
CVE-2025-26519 (https://www.cve.org/CVERecord?id=CVE-2025-26519) reports an out-of-bounds write vulnerability in "musl libc 0.9.13–1.2.5" when converting untrusted EUC-KR text to UTF-8 using 'iconv'.
I confirmed that Rust's 'libc' crate can trigger this via 'extern "C"' calls to 'iconv', 'iconv_open', and 'iconv_close'.
To mitigate this and validate behavior, I developed a C-to-Rust tool that generates a Rust-native 'iconv', 'iconv_open', and 'iconv_close' implementation, fully replacing the C calls.
-
Tested on 5,625 test cases, achieving 99.8% accuracy.
-
Correctly handles glibc deviations and prevents out-of-bounds writes in Rust.
-
Rust program (/libc-main/src/unix/aix/mod.rs)
#[link(name = "iconv")]
extern "C" {
pub fn iconv(
cd: iconv_t,
inbuf: *mut *mut c_char,
inbytesleft: *mut size_t,
outbuf: *mut *mut c_char,
outbytesleft: *mut size_t,
) -> size_t;
pub fn iconv_close(cd: iconv_t) -> c_int;
pub fn iconv_open(tocode: *const c_char, fromcode: *const c_char) -> iconv_t;
}
-Replace function
- iconv.rs
- iconv_open.rs
- iconv_close.rs
- types.rs
iconv.rs
// encoding_rs = "0.8"
use types::*;
use encoding_rs::CoderResult;
pub fn iconv(
cd: &mut IconvHandle,
input: &mut &[u8],
output: &mut &mut [u8],
) -> Result<usize, IconvError> {
let mut replacements = 0usize;
if cd.state == 0 && !input.is_empty() {
bom_detect(cd, input)?;
}
if input.is_empty() {
if !cd.bom_written && cd.from_type == ENCODING_WCHAR_T {
if cd.to_type == ENCODING_UTF_16 {
if output.len() < 2 {
return Err(IconvError::OutputOverflow);
}
let bom = 0xFEFFu16;
(**output)[0] = (bom & 0xFF) as u8;
(**output)[1] = (bom >> 8) as u8;
let rest = std::mem::take(output);
*output = &mut rest[2..];
cd.bom_written = true;
} else if cd.to_type == ENCODING_UTF_32 {
if output.len() < 4 {
return Err(IconvError::OutputOverflow);
}
let bom = 0xFEFFu32;
(**output)[0] = (bom & 0xFF) as u8;
(**output)[1] = ((bom >> 8) & 0xFF) as u8;
(**output)[2] = ((bom >> 16) & 0xFF) as u8;
(**output)[3] = ((bom >> 24) & 0xFF) as u8;
let rest = std::mem::take(output);
*output = &mut rest[4..];
cd.bom_written = true;
}
}
return Ok(0);
}
if cd.from_type == ENCODING_UTF_8 && std::str::from_utf8(input).is_err() {
return Err(IconvError::IllegalSequence);
}
match cd.from_type {
ENCODING_GB2312 => validate_gb2312(input)?,
ENCODING_SHIFT_JIS => validate_shift_jis(input)?,
ENCODING_GB18030 => validate_gb18030(input)?,
_ => {}
}
// Path 1: Both from/to use encoding_rs → direct streaming transcode
if cd.decoder.is_some() && cd.encoder.is_some() {
return transcode_both_rs(cd, input, output);
}
// Path 2: encoding_rs decoder + manual encoder
// Decode ALL input to UTF-8 first, then encode each char manually
if cd.decoder.is_some() && cd.encoder.is_none() {
let needs_0x80 = matches!(cd.from_type, ENCODING_EUC_JP | ENCODING_BIG5 | ENCODING_EUC_KR);
if needs_0x80 && input.iter().any(|&b| b == 0x80) {
let mut codepoints: Vec<u32> = Vec::new();
let src: &[u8] = input;
let mut pos = 0;
while pos < src.len() {
if src[pos] == 0x80 {
codepoints.push(0x80u32);
pos += 1;
continue;
}
let start = pos;
while pos < src.len() && src[pos] != 0x80 {
pos += 1;
}
let chunk = &src[start..pos];
let mut tmp_buf = vec![0u8; chunk.len() * 4 + 4];
{
let decoder = cd.decoder.as_mut().unwrap();
let (_, dec_read, dec_written, _) =
decoder.decode_to_utf8(chunk, &mut tmp_buf, false);
if dec_read < chunk.len() {
return Err(IconvError::IllegalSequence);
}
if dec_written > 0 {
let s = std::str::from_utf8(&tmp_buf[..dec_written])
.map_err(|_| IconvError::IllegalSequence)?;
for ch in s.chars() {
if ch == '\u{FFFD}' {
return Err(IconvError::IllegalSequence);
}
codepoints.push(ch as u32);
}
}
}
let mut flush_buf = [0u8; 4];
let decoder = cd.decoder.as_mut().unwrap();
let (_, _, _, had_replacements) =
decoder.decode_to_utf8(&[][..], &mut flush_buf, true);
if had_replacements {
return Err(IconvError::IncompleteInput);
}
}
*input = &input[input.len()..];
for &c in &codepoints {
encode_one(cd, c, output)?;
}
return Ok(replacements);
}
let mut utf8_buf = vec![0u8; input.len() * 4 + 4];
let (dec_read, dec_written, flush_had_replacements) = {
let decoder = cd.decoder.as_mut().unwrap();
let (_, dec_read, dec_written, _) =
decoder.decode_to_utf8(input, &mut utf8_buf, false);
*input = &input[dec_read..];
let mut flush_buf = [0u8; 4];
let (_, _, _, had_replacements) =
decoder.decode_to_utf8(&[][..], &mut flush_buf, true);
(dec_read, dec_written, had_replacements)
};
if !input.is_empty() || flush_had_replacements {
return Err(IconvError::IncompleteInput);
}
if dec_written > 0 {
let utf8_str = std::str::from_utf8(&utf8_buf[..dec_written])
.map_err(|_| IconvError::IllegalSequence)?;
for ch in utf8_str.chars() {
if ch == '\u{FFFD}' {
return Err(IconvError::IllegalSequence);
}
encode_one(cd, ch as u32, output)?;
}
}
return Ok(replacements);
}
// Path 3 & 4: Manual decoder (with or without encoding_rs encoder)
while !input.is_empty() {
let (c, consumed) = decode_one(cd, input)?;
*input = &input[consumed..];
if encode_one(cd, c, output)? {
replacements += 1;
}
}
Ok(replacements)
}
fn bom_detect(cd: &mut IconvHandle, input: &mut &[u8]) -> Result<(), IconvError> {
match cd.from_type {
ENCODING_UTF_16 | ENCODING_UCS2 => {
if input.len() < 2 { return Err(IconvError::IncompleteInput); }
let bom_be = u16::from_be_bytes([(*input)[0], (*input)[1]]);
let bom_le = u16::from_le_bytes([(*input)[0], (*input)[1]]);
let (resolved, skip_bom) = if bom_le == 0xFFFE {
if cd.from_type == ENCODING_UCS2 { (ENCODING_UCS2LE, true) } else { (ENCODING_UTF_16LE, true) }
} else if bom_be == 0xFEFF {
if cd.from_type == ENCODING_UCS2 { (ENCODING_UCS2BE, true) } else { (ENCODING_UTF_16BE, true) }
} else {
if cd.from_type == ENCODING_UCS2 { (ENCODING_UCS2LE, false) } else { (ENCODING_UTF_16LE, false) }
};
cd.state = 1;
if skip_bom { *input = &input[2..]; }
cd.from_type = resolved;
if cd.decoder.is_none() {
cd.decoder = resolve_encoding(cd.from_type)
.map(|enc| enc.new_decoder_without_bom_handling());
}
}
ENCODING_UTF_32 => {
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let bom = u32::from_be_bytes([(*input)[0], (*input)[1], (*input)[2], (*input)[3]]);
let resolved = if bom == 0xFFFE0000 {
ENCODING_UTF_32LE
} else if bom == 0xFEFF {
ENCODING_UTF_32BE
} else {
ENCODING_UTF_32LE
};
cd.state = 1;
if bom == 0xFFFE0000 || bom == 0xFEFF { *input = &input[4..]; }
cd.from_type = resolved;
}
_ => {
cd.state = 1;
}
}
Ok(())
}
fn transcode_both_rs(
cd: &mut IconvHandle,
input: &mut &[u8],
output: &mut &mut [u8],
) -> Result<usize, IconvError> {
let decoder = cd.decoder.as_mut().unwrap();
let encoder = cd.encoder.as_mut().unwrap();
let mut utf8_buf = [0u8; 4];
let needs_0x80 = matches!(cd.from_type, ENCODING_EUC_JP | ENCODING_BIG5 | ENCODING_EUC_KR);
while !input.is_empty() {
if needs_0x80 && (*input)[0] == 0x80 {
*input = &input[1..];
if matches!(cd.to_type, ENCODING_EUC_JP | ENCODING_BIG5 | ENCODING_EUC_KR) {
if output.is_empty() {
return Err(IconvError::OutputOverflow);
}
(**output)[0] = 0x80;
let rest = std::mem::take(output);
*output = &mut rest[1..];
continue;
}
if matches!(cd.to_type, ENCODING_SHIFT_JIS) {
return Err(IconvError::IllegalSequence);
}
let src = "\u{0080}";
let out = std::mem::take(output);
let (enc_res, _, enc_written, _) = encoder.encode_from_utf8(src, out, false);
if enc_written >= 2 && out[..enc_written].windows(2).any(|w| w == [b'&', b'#']) {
return Err(IconvError::IllegalSequence);
}
*output = &mut out[enc_written..];
if matches!(enc_res, CoderResult::OutputFull) {
return Err(IconvError::OutputOverflow);
}
continue;
}
let (_, dec_read, dec_written, _) =
decoder.decode_to_utf8(input, &mut utf8_buf, false);
if dec_read == 0 && dec_written == 0 { break; }
*input = &input[dec_read..];
if dec_written > 0 {
let written = &utf8_buf[..dec_written];
if written.len() >= 3 && written.windows(3).any(|w| w == [0xEF, 0xBF, 0xBD]) {
return Err(IconvError::IllegalSequence);
}
let src = unsafe { std::str::from_utf8_unchecked(&utf8_buf[..dec_written]) };
let out = std::mem::take(output);
let (enc_res, _, enc_written, _) = encoder.encode_from_utf8(src, out, false);
if enc_written >= 2 && out[..enc_written].windows(2).any(|w| w == [b'&', b'#']) {
return Err(IconvError::IllegalSequence);
}
if cd.to_type == ENCODING_GB2312 && !validate_output_gb2312(&out[..enc_written]) {
return Err(IconvError::IllegalSequence);
}
*output = &mut out[enc_written..];
if matches!(enc_res, CoderResult::OutputFull) {
return Err(IconvError::OutputOverflow);
}
}
}
if !input.is_empty() {
return Err(IconvError::IncompleteInput);
}
let mut flush_buf = [0u8; 4];
let (_, _, _, flush_had_replacements) = decoder.decode_to_utf8(&[][..], &mut flush_buf, true);
if flush_had_replacements {
return Err(IconvError::IncompleteInput);
}
Ok(0)
}
fn decode_one(cd: &mut IconvHandle, input: &[u8]) -> Result<(u32, usize), IconvError> {
match cd.from_type {
ENCODING_US_ASCII => {
if input.is_empty() { return Err(IconvError::IncompleteInput); }
let c = input[0] as u32;
if c >= 128 { return Err(IconvError::IllegalSequence); }
Ok((c, 1))
}
ENCODING_UTF_32BE => {
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let c = u32::from_be_bytes([input[0], input[1], input[2], input[3]]);
if c.wrapping_sub(0xd800) < 0x800 || c >= 0x110000 {
return Err(IconvError::IllegalSequence);
}
Ok((c, 4))
}
ENCODING_UTF_32LE => {
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let c = u32::from_le_bytes([input[0], input[1], input[2], input[3]]);
if c.wrapping_sub(0xd800) < 0x800 || c >= 0x110000 {
return Err(IconvError::IllegalSequence);
}
Ok((c, 4))
}
ENCODING_WCHAR_T => {
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let c = u32::from_ne_bytes([input[0], input[1], input[2], input[3]]);
if c.wrapping_sub(0xd800) < 0x800 || c >= 0x110000 {
return Err(IconvError::IllegalSequence);
}
Ok((c, 4))
}
ENCODING_UCS2BE | ENCODING_UTF_16BE => {
if input.len() < 2 { return Err(IconvError::IncompleteInput); }
let c = u16::from_be_bytes([input[0], input[1]]) as u32;
if c.wrapping_sub(0xdc00) < 0x400 { return Err(IconvError::IllegalSequence); }
if c.wrapping_sub(0xd800) < 0x400 {
if cd.from_type == ENCODING_UCS2BE { return Err(IconvError::IllegalSequence); }
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let d = u16::from_be_bytes([input[2], input[3]]) as u32;
if d.wrapping_sub(0xdc00) >= 0x400 { return Err(IconvError::IllegalSequence); }
return Ok((((c - 0xd7c0) << 10) + (d - 0xdc00), 4));
}
Ok((c, 2))
}
ENCODING_UCS2LE | ENCODING_UTF_16LE => {
if input.len() < 2 { return Err(IconvError::IncompleteInput); }
let c = u16::from_le_bytes([input[0], input[1]]) as u32;
if c.wrapping_sub(0xdc00) < 0x400 { return Err(IconvError::IllegalSequence); }
if c.wrapping_sub(0xd800) < 0x400 {
if cd.from_type == ENCODING_UCS2LE { return Err(IconvError::IllegalSequence); }
if input.len() < 4 { return Err(IconvError::IncompleteInput); }
let d = u16::from_le_bytes([input[2], input[3]]) as u32;
if d.wrapping_sub(0xdc00) >= 0x400 { return Err(IconvError::IllegalSequence); }
return Ok((((c - 0xd7c0) << 10) + (d - 0xdc00), 4));
}
Ok((c, 2))
}
_ => Err(IconvError::IllegalSequence),
}
}
fn encode_one(cd: &mut IconvHandle, c: u32, output: &mut &mut [u8]) -> Result<bool, IconvError> {
match cd.to_type {
ENCODING_US_ASCII => {
if output.is_empty() { return Err(IconvError::OutputOverflow); }
if c <= 0x7f {
(**output)[0] = c as u8;
} else {
return Err(IconvError::IllegalSequence);
}
let rest = std::mem::take(output);
*output = &mut rest[1..];
Ok(false)
}
ENCODING_UTF_32BE => {
if output.len() < 4 { return Err(IconvError::OutputOverflow); }
let bytes = c.to_be_bytes();
(**output)[..4].copy_from_slice(&bytes);
let rest = std::mem::take(output);
*output = &mut rest[4..];
Ok(false)
}
ENCODING_UTF_32LE => {
if output.len() < 4 { return Err(IconvError::OutputOverflow); }
let bytes = c.to_le_bytes();
(**output)[..4].copy_from_slice(&bytes);
let rest = std::mem::take(output);
*output = &mut rest[4..];
Ok(false)
}
ENCODING_WCHAR_T => {
if output.len() < 4 { return Err(IconvError::OutputOverflow); }
let bytes = c.to_ne_bytes();
(**output)[..4].copy_from_slice(&bytes);
let rest = std::mem::take(output);
*output = &mut rest[4..];
Ok(false)
}
ENCODING_UCS2BE | ENCODING_UCS2LE => {
if c >= 0x10000 {
return Err(IconvError::IllegalSequence);
}
if output.len() < 2 { return Err(IconvError::OutputOverflow); }
let v = c as u16;
if cd.to_type == ENCODING_UCS2BE {
let b = v.to_be_bytes();
(**output)[0] = b[0]; (**output)[1] = b[1];
} else {
let b = v.to_le_bytes();
(**output)[0] = b[0]; (**output)[1] = b[1];
}
let rest = std::mem::take(output);
*output = &mut rest[2..];
Ok(false)
}
ENCODING_UTF_16BE | ENCODING_UTF_16LE => {
if c >= 0x10000 {
if output.len() < 4 { return Err(IconvError::OutputOverflow); }
let hi = (0xD800 + (((c - 0x10000) >> 10) & 0x3FF)) as u16;
let lo = (0xDC00 + ((c - 0x10000) & 0x3FF)) as u16;
if cd.to_type == ENCODING_UTF_16BE {
(**output)[0] = (hi >> 8) as u8; (**output)[1] = hi as u8;
(**output)[2] = (lo >> 8) as u8; (**output)[3] = lo as u8;
} else {
(**output)[0] = hi as u8; (**output)[1] = (hi >> 8) as u8;
(**output)[2] = lo as u8; (**output)[3] = (lo >> 8) as u8;
}
let rest = std::mem::take(output);
*output = &mut rest[4..];
} else {
if output.len() < 2 { return Err(IconvError::OutputOverflow); }
let v = c as u16;
if cd.to_type == ENCODING_UTF_16BE {
(**output)[0] = (v >> 8) as u8; (**output)[1] = v as u8;
} else {
(**output)[0] = v as u8; (**output)[1] = (v >> 8) as u8;
}
let rest = std::mem::take(output);
*output = &mut rest[2..];
}
Ok(false)
}
ENCODING_UCS2 => {
if c >= 0x10000 {
return Err(IconvError::IllegalSequence);
}
let v = c as u16;
if output.len() < 2 {
return Err(IconvError::OutputOverflow);
}
(**output)[0] = (v & 0xFF) as u8;
(**output)[1] = (v >> 8) as u8;
let rest = std::mem::take(output);
*output = &mut rest[2..];
Ok(false)
}
ENCODING_UTF_16 => {
if !cd.bom_written {
if output.len() < 2 {
return Err(IconvError::OutputOverflow);
}
let bom = 0xFEFFu16;
(**output)[0] = (bom & 0xFF) as u8;
(**output)[1] = (bom >> 8) as u8;
let rest = std::mem::take(output);
*output = &mut rest[2..];
cd.bom_written = true;
}
if c >= 0x10000 {
if output.len() < 4 {
return Err(IconvError::OutputOverflow);
}
let hi = (0xD800 + (((c - 0x10000) >> 10) & 0x3FF)) as u16;
let lo = (0xDC00 + ((c - 0x10000) & 0x3FF)) as u16;
(**output)[0] = (hi & 0xFF) as u8;
(**output)[1] = (hi >> 8) as u8;
(**output)[2] = (lo & 0xFF) as u8;
(**output)[3] = (lo >> 8) as u8;
let rest = std::mem::take(output);
*output = &mut rest[4..];
} else {
if output.len() < 2 {
return Err(IconvError::OutputOverflow);
}
let v = c as u16;
(**output)[0] = (v & 0xFF) as u8;
(**output)[1] = (v >> 8) as u8;
let rest = std::mem::take(output);
*output = &mut rest[2..];
}
Ok(false)
}
ENCODING_UTF_32 => {
if !cd.bom_written {
if output.len() < 4 {
return Err(IconvError::OutputOverflow);
}
let bom = 0xFEFFu32;
(**output)[0] = (bom & 0xFF) as u8;
(**output)[1] = ((bom >> 8) & 0xFF) as u8;
(**output)[2] = ((bom >> 16) & 0xFF) as u8;
(**output)[3] = ((bom >> 24) & 0xFF) as u8;
let rest = std::mem::take(output);
*output = &mut rest[4..];
cd.bom_written = true;
}
if output.len() < 4 {
return Err(IconvError::OutputOverflow);
}
let b = c.to_le_bytes();
(**output)[0] = b[0];
(**output)[1] = b[1];
(**output)[2] = b[2];
(**output)[3] = b[3];
let rest = std::mem::take(output);
*output = &mut rest[4..];
Ok(false)
}
_ => encode_one_rs(cd, c, output),
}
}
fn encode_one_rs(cd: &mut IconvHandle, c: u32, output: &mut &mut [u8]) -> Result<bool, IconvError> {
let encoder = cd.encoder.as_mut().ok_or(IconvError::IllegalSequence)?;
let ch = char::from_u32(c).ok_or(IconvError::IllegalSequence)?;
let mut utf8_buf = [0u8; 4];
let utf8_str = ch.encode_utf8(&mut utf8_buf);
let out = std::mem::take(output);
let (enc_res, _, enc_written, _) = encoder.encode_from_utf8(utf8_str, out, false);
if enc_written >= 2 && out[..enc_written].windows(2).any(|w| w == [b'&', b'#']) {
return Err(IconvError::IllegalSequence);
}
if cd.to_type == ENCODING_GB2312 && !validate_output_gb2312(&out[..enc_written]) {
return Err(IconvError::IllegalSequence);
}
*output = &mut out[enc_written..];
if matches!(enc_res, CoderResult::OutputFull) {
return Err(IconvError::OutputOverflow);
}
Ok(false)
}
fn validate_gb2312(input: &[u8]) -> Result<(), IconvError> {
let mut i = 0;
while i < input.len() {
let b = input[i];
if b <= 0x7F {
i += 1;
} else if b >= 0xA1 && b <= 0xF7 {
if i + 1 >= input.len() {
return Err(IconvError::IncompleteInput);
}
let t = input[i + 1];
if t >= 0xA1 && t <= 0xFE {
i += 2;
} else {
return Err(IconvError::IllegalSequence);
}
} else {
return Err(IconvError::IllegalSequence);
}
}
Ok(())
}
fn validate_shift_jis(input: &[u8]) -> Result<(), IconvError> {
let mut i = 0;
while i < input.len() {
let b = input[i];
if b <= 0x7F || (b >= 0xA1 && b <= 0xDF) {
i += 1;
} else if (b >= 0x81 && b <= 0x9F) || (b >= 0xE0 && b <= 0xEF) {
if i + 1 >= input.len() {
return Err(IconvError::IncompleteInput);
}
let t = input[i + 1];
if (t >= 0x40 && t <= 0x7E) || (t >= 0x80 && t <= 0xFC) {
i += 2;
} else {
return Err(IconvError::IllegalSequence);
}
} else {
return Err(IconvError::IllegalSequence);
}
}
Ok(())
}
fn validate_gb18030(input: &[u8]) -> Result<(), IconvError> {
let mut i = 0;
while i < input.len() {
let b = input[i];
if b <= 0x7F {
i += 1;
} else if b >= 0x81 && b <= 0xFE {
if i + 1 >= input.len() {
return Err(IconvError::IncompleteInput);
}
let t = input[i + 1];
if (t >= 0x40 && t <= 0x7E) || (t >= 0x80 && t <= 0xFE) {
i += 2;
} else if t >= 0x30 && t <= 0x39 {
if i + 3 >= input.len() {
return Err(IconvError::IncompleteInput);
}
let t3 = input[i + 2];
let t4 = input[i + 3];
if t3 >= 0x81 && t3 <= 0xFE && t4 >= 0x30 && t4 <= 0x39 {
i += 4;
} else {
return Err(IconvError::IllegalSequence);
}
} else {
return Err(IconvError::IllegalSequence);
}
} else {
return Err(IconvError::IllegalSequence);
}
}
Ok(())
}
fn validate_output_gb2312(output: &[u8]) -> bool {
let mut i = 0;
while i < output.len() {
let b = output[i];
if b <= 0x7F {
i += 1;
} else if b >= 0xA1 && b <= 0xF7 && i + 1 < output.len() {
let t = output[i + 1];
if t >= 0xA1 && t <= 0xFE {
i += 2;
} else {
return false;
}
} else {
return false;
}
}
true
}
iconv_open.rs
use types::*;
pub fn iconv_open(to: &str, from: &str) -> Result<IconvHandle, IconvError> {
let from_type = find_charmap(from).ok_or(IconvError::InvalidEncoding)?;
let to_type = find_charmap(to).ok_or(IconvError::InvalidEncoding)?;
let decoder = resolve_encoding(from_type).map(|enc| {
if from_type == ENCODING_UTF_8 { enc.new_decoder_with_bom_removal() }
else { enc.new_decoder_without_bom_handling() }
});
let encoder = resolve_encoding(to_type).map(|enc| enc.new_encoder());
let is_stateful = matches!(from_type, ENCODING_UCS2 | ENCODING_UTF_16 | ENCODING_UTF_32 | ENCODING_ISO2022_JP);
Ok(IconvHandle { from_type, to_type, decoder, encoder, state: 0, bom_written: false, is_stateful })
}
iconv_close.rs
use types::*;
pub fn iconv_close(cd: IconvHandle) -> Result<(), IconvError> {
if cd.is_stateful {
drop(cd);
}
Ok(())
}
types.rs
use encoding_rs::{Decoder, Encoder, Encoding};
pub struct IconvHandle {
pub from_type: u8,
pub to_type: u8,
pub decoder: Option<Decoder>,
pub encoder: Option<Encoder>,
pub state: u32,
pub bom_written: bool,
pub is_stateful: bool,
}
impl std::fmt::Debug for IconvHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IconvHandle")
.field("from_type", &self.from_type)
.field("to_type", &self.to_type)
.field("state", &self.state)
.field("is_stateful", &self.is_stateful)
.field("has_decoder", &self.decoder.is_some())
.field("has_encoder", &self.encoder.is_some())
.finish()
}
}
#[derive(Debug)]
pub enum IconvError {
IllegalSequence,
IncompleteInput,
InvalidEncoding,
OutputOverflow,
}
pub const ENCODING_UTF_8: u8 = 0;
pub const ENCODING_US_ASCII: u8 = 1;
pub const ENCODING_WCHAR_T: u8 = 2;
pub const ENCODING_UCS2BE: u8 = 3;
pub const ENCODING_UCS2LE: u8 = 4;
pub const ENCODING_UTF_16BE: u8 = 5;
pub const ENCODING_UTF_16LE: u8 = 6;
pub const ENCODING_UCS2: u8 = 7;
pub const ENCODING_UTF_16: u8 = 8;
pub const ENCODING_UTF_32: u8 = 9;
pub const ENCODING_SHIFT_JIS: u8 = 10;
pub const ENCODING_EUC_JP: u8 = 11;
pub const ENCODING_ISO2022_JP: u8 = 12;
pub const ENCODING_GB2312: u8 = 13;
pub const ENCODING_GBK: u8 = 14;
pub const ENCODING_GB18030: u8 = 15;
pub const ENCODING_BIG5: u8 = 16;
pub const ENCODING_EUC_KR: u8 = 17;
pub const ENCODING_UTF_32BE: u8 = 18;
pub const ENCODING_UTF_32LE: u8 = 19;
pub const ENCODING_COUNT: u8 = 20;
pub fn resolve_encoding(enc_type: u8) -> Option<&'static Encoding> {
match enc_type {
ENCODING_UTF_8 => Some(encoding_rs::UTF_8),
ENCODING_SHIFT_JIS => Some(encoding_rs::SHIFT_JIS),
ENCODING_EUC_JP => Some(encoding_rs::EUC_JP),
ENCODING_GBK => Some(encoding_rs::GBK),
ENCODING_GB2312 => Some(encoding_rs::GBK),
ENCODING_GB18030 => Some(encoding_rs::GB18030),
ENCODING_BIG5 => Some(encoding_rs::BIG5),
ENCODING_EUC_KR => Some(encoding_rs::EUC_KR),
ENCODING_ISO2022_JP => Some(encoding_rs::ISO_2022_JP),
_ => None,
}
}
fn encoding_to_type(enc: &'static Encoding) -> Option<u8> {
if enc == encoding_rs::UTF_8 { return Some(ENCODING_UTF_8); }
if enc == encoding_rs::SHIFT_JIS { return Some(ENCODING_SHIFT_JIS); }
if enc == encoding_rs::EUC_JP { return Some(ENCODING_EUC_JP); }
if enc == encoding_rs::GBK { return Some(ENCODING_GBK); }
if enc == encoding_rs::GB18030 { return Some(ENCODING_GB18030); }
if enc == encoding_rs::BIG5 { return Some(ENCODING_BIG5); }
if enc == encoding_rs::EUC_KR { return Some(ENCODING_EUC_KR); }
None
}
pub fn find_charmap(name: &str) -> Option<u8> {
let n = name.to_ascii_lowercase()
.replace(|c: char| !c.is_ascii_alphanumeric(), "");
match n.as_str() {
"gb2312" => return Some(ENCODING_GB2312),
_ => {}
}
if let Some(enc) = Encoding::for_label(name.as_bytes()) {
if let Some(t) = encoding_to_type(enc) {
return Some(t);
}
}
match n.as_str() {
"utf32be" => Some(ENCODING_UTF_32BE),
"ucs4be" => Some(ENCODING_UTF_32BE),
"utf32le" => Some(ENCODING_UTF_32LE),
"ucs4le" => Some(ENCODING_UTF_32LE),
"utf32" => Some(ENCODING_UTF_32),
"ucs4" => Some(ENCODING_UTF_32),
"ucs2be" => Some(ENCODING_UCS2BE),
"ucs2le" => Some(ENCODING_UCS2LE),
"ucs2" => Some(ENCODING_UCS2),
"utf16be" => Some(ENCODING_UTF_16BE),
"utf16le" => Some(ENCODING_UTF_16LE),
"utf16" => Some(ENCODING_UTF_16),
"iso2022jp" => Some(ENCODING_ISO2022_JP),
"ascii" => Some(ENCODING_US_ASCII),
"usascii" => Some(ENCODING_US_ASCII),
"iso646" => Some(ENCODING_US_ASCII),
"iso646us" => Some(ENCODING_US_ASCII),
"wchart" => Some(ENCODING_WCHAR_T),
"gb2312" => Some(ENCODING_GB2312),
_ => None,
}
}
Am I correct in understanding that you are proposing your version of iconv be used rather than a binding? If so, this isn't something we would do here. The correct mitigation is to make sure you are building against a musl version that includes the two patches provided at https://musl.libc.org/releases.html.
Thanks for the clarification. I understand that 'libc' crate itself does not intend to provide alternative implementations of system libraries. I would like to add some context from my testing: I verified the vulnerability in a controlled virtual environment. Even when using the latest 'libc' crate, if the underlying system has a musl version older than 1.2.6, the out-of-bounds write can still be triggered via 'libc::iconv' calls. This confirms that the issue is not caused by 'libc' itself but by the musl implementation present at runtime. For Rust users who need immediate mitigation, I have developed a Rust-native iconv implementation that correctly handles glibc deviations while avoiding out-of-bounds writes. I hope this information helps clarify the impact for Rust users relying on 'libc' bindings.