From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id 6DCA03858D20 for ; Wed, 17 Apr 2024 17:45:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DCA03858D20 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 6DCA03858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713375935; cv=none; b=hoDNO9QqFo17xRpvkIi4Ax6K+RartSYUyAcidKOLfxRQRReOhhZE5TnA6qEHvqp4sfn1EwEB6ufanQFCrUpyKgukTyJ0eMGkurvA9gHnIaqotXte1MZP+AvlDu85HDuI/dwYNg816g6vF0OIiqsrJRIJryBbZBPqlSUvGACEiBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713375935; c=relaxed/simple; bh=oPavbwjv1fAKJUZKT5AQDPMwhVBt1HfHv2wcz1v1yW8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Lexalrnn/6SKW1AI1jeqCaDbYNzM5rREKTTw+KUYjWwgOr0JFhOGTx6xm45Z5nU3PjaiT6V/C/xtNSggT61SdrjpEcRtx/deSdrIfsZLK5UTmU1U3WBfW3DnzLekp8HzGt0QS7rg+Er4j26ePqaNONJpW4FVccKXtYW8GkcSQyM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1e3ff14f249so226345ad.1 for ; Wed, 17 Apr 2024 10:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713375931; x=1713980731; 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=NiI8jN4q6nNBBm1U7cCxiEeNq4HqwOcDVxMJmSu1V4w=; b=VlqKVFBiKe8p/NUbWGV35RK4uFtXixxf3w2Go8f+269fwHs3vuPkZvhvjNmrHdy1KH CJ0VJwAL5w7HbUTQwfFohk7h/+JQYZsm+nKVgm63wlwW9juhZzb0aXyCIxmotNE6IwUT KcQnp7lO4tTqLYexFF9QEjZRZCtJXtXRTL2jSMaQpudTKTneZN+gwnCifX3Gct4n84JR /HwOGxffw1xrCBunVEWChYllvXB2segbHMnmYzlV99rCe7nskWwVyZ8JUQoQqiNksN6/ ppfmEXTtVINvE35KWaPglf/RtX7cJW5igvplYjn2UrqZzz8CzqZrak9DYSj2pzziEGRG iNQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713375931; x=1713980731; 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=NiI8jN4q6nNBBm1U7cCxiEeNq4HqwOcDVxMJmSu1V4w=; b=fSW3ZQWmDAy8CK45c5EiTvN9cUXgpCtyE7Ebbf+Jh/UbQRdrsWf8J5LHE4wGy9lC0O 0zOMPncwI8bOLAVZjuJhh9/4BWHEXJtz9/tQsWIelkbBtTv8AI2airU/cutI7lujGTiq 5/+D5GSOvtaxQAenVxa07s0hLfOpIFHRDWBg06+WVcygXQ/ljkJbsFlE5S8TS/k7r3JU aKXK4x15OJuKPIY+f8J5a46lU0cvwJKKVu0Lo7va9Iw7hOcIVfdjvDtoXcxbdmTZLKrq db3rOv5K9Li18Mo7GjUFj5ea40og9Daw3SO25+gg86CgmL8sXO/asvy3kjzDrdTp2fGQ DmGQ== X-Gm-Message-State: AOJu0Yy58RzUltGlBC7wuC6QBGo+ti+wEqbKmGDu3O57Rc+/T8pPgAFN i7ajlMvTan8jxhe7rKPIORZC+LODLfJeRAE8DWOEK9BZ4NcKRy6ePf0HVT8awJGJptFg4efPnmi P X-Google-Smtp-Source: AGHT+IHvmQpcLf6GtBEU47YLFZw41jSXxOnzmbbekAzdumw0y/DAxKj9FmF8sFzHRfI3KcqExWshXQ== X-Received: by 2002:a17:902:a703:b0:1e2:bb09:6270 with SMTP id w3-20020a170902a70300b001e2bb096270mr112307plq.28.1713375930858; Wed, 17 Apr 2024 10:45:30 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:d8e5:2400:1efa:e2e2:4d55]) by smtp.gmail.com with ESMTPSA id w10-20020a170902e88a00b001e3e0a8b32asm11811612plg.45.2024.04.17.10.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 10:45:30 -0700 (PDT) From: Adhemerval Zanella To: libc-stable@sourceware.org Cc: Charles Fol , Carlos O'Donell Subject: [COMMITTED 2.38] iconv: ISO-2022-CN-EXT: fix out-of-bound writes when writing escape sequence (CVE-2024-2961) Date: Wed, 17 Apr 2024 14:45:25 -0300 Message-Id: <20240417174525.1579224-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 List-Id: 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 (cherry picked from commit f9dc609e06b1136bb0408be9605ce7973a767ada) --- 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 dd5cafab21..075098dce8 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 36727f0865..9bb02238a3 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 -- 2.34.1