From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from eastern.birch.relay.mailchannels.net (eastern.birch.relay.mailchannels.net [23.83.209.55]) by sourceware.org (Postfix) with ESMTPS id F1A50399E04E for ; Thu, 10 Jun 2021 11:19:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1A50399E04E X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 84D0A341F9D; Thu, 10 Jun 2021 11:19:42 +0000 (UTC) Received: from pdx1-sub0-mail-a13.g.dreamhost.com (100-96-133-111.trex.outbound.svc.cluster.local [100.96.133.111]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id F0F99341FA1; Thu, 10 Jun 2021 11:19:41 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a13.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.133.111 (trex/6.3.1); Thu, 10 Jun 2021 11:19:42 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Left-Army: 16e54b286949f880_1623323982307_2367986257 X-MC-Loop-Signature: 1623323982307:97461113 X-MC-Ingress-Time: 1623323982307 Received: from pdx1-sub0-mail-a13.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a13.g.dreamhost.com (Postfix) with ESMTP id A7B4280468; Thu, 10 Jun 2021 11:19:41 +0000 (UTC) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a13.g.dreamhost.com (Postfix) with ESMTPSA id DEE10800C5; Thu, 10 Jun 2021 11:19:37 +0000 (UTC) X-DH-BACKEND: pdx1-sub0-mail-a13 From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, dj@redhat.com, schwab@linux-m68k.org Subject: [PATCH 1/6] iconv: Remove alloca use in gconv-modules configuration parsing Date: Thu, 10 Jun 2021 16:48:48 +0530 Message-Id: <20210610111853.2286873-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210610111853.2286873-1-siddhesh@sourceware.org> References: <20210610111853.2286873-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3494.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Jun 2021 11:19:45 -0000 The alloca sizes ought to be constrained to PATH_MAX, but replace them with dynamic allocation to be safe. A static PATH_MAX array would have worked too but Hurd does not have PATH_MAX and the code path is not hot enough to micro-optimise this allocation. Revisit if any of those realities change. --- iconv/gconv_conf.c | 17 +++++++++-------- iconv/iconvconfig.c | 17 +++++++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/iconv/gconv_conf.c b/iconv/gconv_conf.c index c8ad8099a4..3f2cef255b 100644 --- a/iconv/gconv_conf.c +++ b/iconv/gconv_conf.c @@ -559,15 +559,15 @@ __gconv_read_conf (void) =20 for (cnt =3D 0; __gconv_path_elem[cnt].name !=3D NULL; ++cnt) { -#define BUF_LEN elem_len + sizeof (gconv_conf_dirname) - const char *elem =3D __gconv_path_elem[cnt].name; size_t elem_len =3D __gconv_path_elem[cnt].len; - char *buf; =20 /* No slash needs to be inserted between elem and gconv_conf_filen= ame; elem already ends in a slash. */ - buf =3D alloca (BUF_LEN); + char *buf =3D malloc (elem_len + sizeof (gconv_conf_dirname)); + if (buf =3D=3D NULL) + continue; + char *cp =3D __mempcpy (__mempcpy (buf, elem, elem_len), gconv_conf_filename, sizeof (gconv_conf_filename)); =20 @@ -596,15 +596,16 @@ __gconv_read_conf (void) if (len > strlen (suffix) && strcmp (ent->d_name + len - strlen (suffix), suffix) =3D=3D 0) { - /* LEN <=3D PATH_MAX so this alloca is not unbounded. */ - char *conf =3D alloca (BUF_LEN + len + 1); - cp =3D stpcpy (conf, buf); - sprintf (cp, "/%s", ent->d_name); + char *conf; + if (__asprintf (&conf, "%s/%s", buf, ent->d_name) < 0) + continue; read_conf_file (conf, elem, elem_len, &modules, &nmodules); + free (conf); } } __closedir (confdir); } + free (buf); } #endif =20 diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c index b2a868919c..c9607fb645 100644 --- a/iconv/iconvconfig.c +++ b/iconv/iconvconfig.c @@ -712,7 +712,6 @@ handle_file (const char *dir, const char *infile) static int handle_dir (const char *dir) { -#define BUF_LEN prefix_len + dirlen + sizeof "gconv-modules.d" char *cp; size_t dirlen =3D strlen (dir); bool found =3D false; @@ -726,7 +725,10 @@ handle_dir (const char *dir) } =20 /* First, look for a gconv-modules file. */ - char buf[BUF_LEN]; + char *buf =3D malloc (prefix_len + dirlen + sizeof "gconv-modules.d"); + if (buf =3D=3D NULL) + goto out; + cp =3D buf; if (dir[0] =3D=3D '/') cp =3D mempcpy (cp, prefix, prefix_len); @@ -756,16 +758,19 @@ handle_dir (const char *dir) if (len > strlen (suffix) && strcmp (ent->d_name + len - strlen (suffix), suffix) =3D=3D 0) { - /* LEN <=3D PATH_MAX so this alloca is not unbounded. */ - char *conf =3D alloca (BUF_LEN + len + 1); - cp =3D stpcpy (conf, buf); - sprintf (cp, "/%s", ent->d_name); + char *conf; + if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0) + continue; found |=3D handle_file (dir, conf); + free (conf); } } closedir (confdir); } =20 + free (buf); + +out: if (!found) { error (0, errno, "failed to open gconv configuration files in `%s'= ", --=20 2.31.1