From patchwork Wed Apr 17 17:40:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 789313 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp151733wrm; Wed, 17 Apr 2024 10:41:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVxj7zE2BGY8FjU6pNUcdYjb/BdQbfT5yfaUkz8agpPaXELjIVDkmOQe3VjSzPlXAk8MacHSGM+AcJHjreHoWlf X-Google-Smtp-Source: AGHT+IHnizgnAnEzntk8RBPnNaCt0Ai1eGyw92nMqob8A8l3OiiORPl8ZlTdp2cklNN5qFB3Negk X-Received: by 2002:a05:622a:30d:b0:437:6715:7732 with SMTP id q13-20020a05622a030d00b0043767157732mr208383qtw.14.1713375682248; Wed, 17 Apr 2024 10:41:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713375682; cv=pass; d=google.com; s=arc-20160816; b=RCy0nnEiHmG4zc92M1jGNaM/WFn4Q6nErHCGSHrl9N4mD/zTWTjas5mFB+U1qvmdkO VgTEmcbymgFsPctcL882YdThq7fyY8OFqYQrrOgqI+n71w3YfzURGjldpyyzI3ljDXD3 TrQe5kptF0aLm1D/69CcU2sxANRpWjJjG4GqoHpxw7m1pXiI3awElZRzs8dnbCZpNj7d 8WDtK/c27F8ZEwAoF5d4dtMv0qcPYWbWitVKQ7AL/wJowNlrjibTA0h/Xb4iHeMh0KDS c8p1ohNHyvTHZGgY2mB1z8ZzijuWYoKZU8Av9fPjdffits2nQFzcd1TaNhmxpHhE/hZK C3yA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=ln09BBq7MJi/t41PoekcXZ7dP/gYMwhUf9TYNQghPi8=; fh=rSyptbExDmbNVHWAVVULdfYz2yRMCyVIpD7I5VfvNUw=; b=KJQa1JQAiO/ijFwZ0G0MpnTGWv2iJZhpdv0D3rODY/SngZJbPFHkVepye20Ucwp2ZC x71nb/H1cZLvMm3jHRvM7eebtkmRRP1bHcpAs9G8BIZ1EMYzz8zs2+d8VlMswXNVwFc5 7fB3EEIpjDVjDN2NlnKzqBzD4MymU9Wmd1tAhjuJoYFSMNUSQy9UgYiABsxB9tSkj/Oo gTTg9+Z01SLV5GvFp7RgZa7XW25KQD6Xff1qjUe7zyiZCShSdprR1TafbBhs+LID8jk2 1+ovn1I98N8IIXLpX72jbM/anM3Ah4G654PMbrrMX9JebnFplCYmc/15vHfS+zL9fAny lPuw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ADu4HGiW; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z1-20020ae9f441000000b0078d78c9d23bsi15844750qkl.762.2024.04.17.10.41.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 10:41:22 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ADu4HGiW; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D76AC3858C78 for ; Wed, 17 Apr 2024 17:41:21 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id BD6383858D20 for ; Wed, 17 Apr 2024 17:41:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD6383858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BD6383858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713375671; cv=none; b=jRTWqN/kXtyFZ1WmQVp7hjTOn22EoT599AQcKJdQIHGnaHZSbb03jpR1OFFYWes3qkZxyFQ2uZC7r/r7XcqiXCzC9iQBf6Z78NKyitZkS8E56DjAx9pDZ8RKHWoU4spXEfjg9i02gIvuoBPmP94721hZLUnWgwyDM8TqE4M4B0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713375671; c=relaxed/simple; bh=DoL2tIZapV8eD9KXc3ytfY0TVBQeDvRTMpxnzWyQju8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=p+09MPnwSWJKOq+MhboFf9rbeuufFeHJAteKDRehahSJql5ySBBXhnjReA/fnESxK375Q/lmU/2XjWSBeHgxzRrGNFvg6bsnPEmGl3i9Wa575kf4TZQEnp3EeADmlS2w4T7QZji+mvml2bVX45m2ffgBg+Jb45sp5uSPAPYQlQs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6ed3cafd766so53411b3a.0 for ; Wed, 17 Apr 2024 10:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713375665; x=1713980465; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ln09BBq7MJi/t41PoekcXZ7dP/gYMwhUf9TYNQghPi8=; b=ADu4HGiWutJGKYFnY3Kyh6ysfrNvfoT4jGDGC111uAQzQeU0UlWWDwpq3QvB5XXNhn Y45B9a8HlBuse0W+uMi7Yh0wprngVB6CAa2OFZ4U96k2a7vdHqp0rYUdtgQatoo7QmXz zz3yJCq4ACwR1R9/rcJQJ5ijL3zCZC4oJMQWzxbvAWeo99a6FeNqyjT61M3XmueRiuL+ z1BuW2jWDq4+pQo7DBt1FLdYDDJGrGNny28SCzjOkggQFWYADxI+hyRIfF1/a8krVw5u /AAyeJFDvZW8bNPRbGeN+VgDaTblEwFPb+bWE0iFuBVr6allU5PtKDgwPwhf7+/hPns4 5leQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713375665; x=1713980465; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ln09BBq7MJi/t41PoekcXZ7dP/gYMwhUf9TYNQghPi8=; b=TVFFtkBAReu/mc6ps7YOltduPwRSC6GGF92WJG2BrUYNQBnKXBNwXeXO1KY9pkaOkl yHyaNOR3H8tAx5/aml0GxldG5k5z16WvC7Ui2d5GhufCOq1K1avfxRA9K3FqcfKvCv6O hz1v9YKKELw+nCFp19VXJXcTmqmfDw4HtPiobT7gZ2cziDv9340S9pR2K5cJntUM7QGP bKmz3SHP09EwSt4VdgI6yk2j8SlCSCVb5Mz7SoT96NBBYojAvCx93XjBNZrxlItfujgc ugoy/Q0XfVZZ4ZvgLRgRzsZjD7mAzis3VfY+a/yg+bYVx8L6cjrqroj2aQYnyrZs4+EI TBWQ== X-Gm-Message-State: AOJu0Yy//MOS5DP1l9ZyXyomDbuf7F6dvg0FzWmqnMFCK1n/nZFj5t0z OehSeTcZSZiX2WComcbooLnj88EO+79q1LNk2XGTBw6qzqXXn6amIRBwkKzOJcT5qKdNmdgioXo V X-Received: by 2002:a05:6a20:7284:b0:1aa:5a89:18d7 with SMTP id o4-20020a056a20728400b001aa5a8918d7mr470948pzk.19.1713375665347; Wed, 17 Apr 2024 10:41:05 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:d8e5:2400:1efa:e2e2:4d55]) by smtp.gmail.com with ESMTPSA id lc16-20020a056a004f5000b006ead1509847sm10953562pfb.216.2024.04.17.10.41.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 10:41:04 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Charles Fol , Carlos O'Donell Subject: [COMMITTED] iconv: ISO-2022-CN-EXT: fix out-of-bound writes when writing escape sequence (CVE-2024-2961) Date: Wed, 17 Apr 2024 14:40:56 -0300 Message-Id: <20240417174056.1578133-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org From: Charles Fol ISO-2022-CN-EXT uses escape sequences to indicate character set changes (as specified by RFC 1922). While the SOdesignation has the expected bounds checks, neither SS2designation nor SS3designation have its; allowing a write overflow of 1, 2, or 3 bytes with fixed values: '$+I', '$+J', '$+K', '$+L', '$+M', or '$*H'. Checked on aarch64-linux-gnu. Co-authored-by: Adhemerval Zanella Reviewed-by: Carlos O'Donell Tested-by: Carlos O'Donell --- iconvdata/Makefile | 5 +- iconvdata/iso-2022-cn-ext.c | 12 +++ iconvdata/tst-iconv-iso-2022-cn-ext.c | 128 ++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 iconvdata/tst-iconv-iso-2022-cn-ext.c diff --git a/iconvdata/Makefile b/iconvdata/Makefile index ea019ce5c0..7196a8744b 100644 --- a/iconvdata/Makefile +++ b/iconvdata/Makefile @@ -75,7 +75,8 @@ ifeq (yes,$(build-shared)) tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \ tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \ bug-iconv10 bug-iconv11 bug-iconv12 tst-iconv-big5-hkscs-to-2ucs4 \ - bug-iconv13 bug-iconv14 bug-iconv15 + bug-iconv13 bug-iconv14 bug-iconv15 \ + tst-iconv-iso-2022-cn-ext ifeq ($(have-thread-library),yes) tests += bug-iconv3 endif @@ -330,6 +331,8 @@ $(objpfx)bug-iconv14.out: $(addprefix $(objpfx), $(gconv-modules)) \ $(addprefix $(objpfx),$(modules.so)) $(objpfx)bug-iconv15.out: $(addprefix $(objpfx), $(gconv-modules)) \ $(addprefix $(objpfx),$(modules.so)) +$(objpfx)tst-iconv-iso-2022-cn-ext.out: $(addprefix $(objpfx), $(gconv-modules)) \ + $(addprefix $(objpfx),$(modules.so)) $(objpfx)iconv-test.out: run-iconv-test.sh \ $(addprefix $(objpfx), $(gconv-modules)) \ diff --git a/iconvdata/iso-2022-cn-ext.c b/iconvdata/iso-2022-cn-ext.c index b34c8a36f4..cce29b1969 100644 --- a/iconvdata/iso-2022-cn-ext.c +++ b/iconvdata/iso-2022-cn-ext.c @@ -574,6 +574,12 @@ DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); { \ const char *escseq; \ \ + if (outptr + 4 > outend) \ + { \ + result = __GCONV_FULL_OUTPUT; \ + break; \ + } \ + \ assert (used == CNS11643_2_set); /* XXX */ \ escseq = "*H"; \ *outptr++ = ESC; \ @@ -587,6 +593,12 @@ DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); { \ const char *escseq; \ \ + if (outptr + 4 > outend) \ + { \ + result = __GCONV_FULL_OUTPUT; \ + break; \ + } \ + \ assert ((used >> 5) >= 3 && (used >> 5) <= 7); \ escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2; \ *outptr++ = ESC; \ diff --git a/iconvdata/tst-iconv-iso-2022-cn-ext.c b/iconvdata/tst-iconv-iso-2022-cn-ext.c new file mode 100644 index 0000000000..96a8765fd5 --- /dev/null +++ b/iconvdata/tst-iconv-iso-2022-cn-ext.c @@ -0,0 +1,128 @@ +/* Verify ISO-2022-CN-EXT does not write out of the bounds. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include +#include +#include + +#include +#include +#include + +/* The test sets up a two memory page buffer with the second page marked + PROT_NONE to trigger a fault if the conversion writes beyond the exact + expected amount. Then we carry out various conversions and precisely + place the start of the output buffer in order to trigger a SIGSEGV if the + process writes anywhere between 1 and page sized bytes more (only one + PROT_NONE page is setup as a canary) than expected. These tests exercise + all three of the cases in ISO-2022-CN-EXT where the converter must switch + character sets and may run out of buffer space while doing the + operation. */ + +static int +do_test (void) +{ + iconv_t cd = iconv_open ("ISO-2022-CN-EXT", "UTF-8"); + TEST_VERIFY_EXIT (cd != (iconv_t) -1); + + char *ntf; + size_t ntfsize; + char *outbufbase; + { + int pgz = getpagesize (); + TEST_VERIFY_EXIT (pgz > 0); + ntfsize = 2 * pgz; + + ntf = xmmap (NULL, ntfsize, PROT_READ | PROT_WRITE, MAP_PRIVATE + | MAP_ANONYMOUS, -1); + xmprotect (ntf + pgz, pgz, PROT_NONE); + + outbufbase = ntf + pgz; + } + + /* Check if SOdesignation escape sequence does not trigger an OOB write. */ + { + char inbuf[] = "\xe4\xba\xa4\xe6\x8d\xa2"; + + for (int i = 0; i < 9; i++) + { + char *inp = inbuf; + size_t inleft = sizeof (inbuf) - 1; + + char *outp = outbufbase - i; + size_t outleft = i; + + TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft) + == (size_t) -1); + TEST_COMPARE (errno, E2BIG); + + TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0); + } + } + + /* Same as before for SS2designation. */ + { + char inbuf[] = "㴽 \xe3\xb4\xbd"; + + for (int i = 0; i < 14; i++) + { + char *inp = inbuf; + size_t inleft = sizeof (inbuf) - 1; + + char *outp = outbufbase - i; + size_t outleft = i; + + TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft) + == (size_t) -1); + TEST_COMPARE (errno, E2BIG); + + TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0); + } + } + + /* Same as before for SS3designation. */ + { + char inbuf[] = "劄 \xe5\x8a\x84"; + + for (int i = 0; i < 14; i++) + { + char *inp = inbuf; + size_t inleft = sizeof (inbuf) - 1; + + char *outp = outbufbase - i; + size_t outleft = i; + + TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft) + == (size_t) -1); + TEST_COMPARE (errno, E2BIG); + + TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0); + } + } + + TEST_VERIFY_EXIT (iconv_close (cd) != -1); + + xmunmap (ntf, ntfsize); + + return 0; +} + +#include