From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from antelope.elm.relay.mailchannels.net (antelope.elm.relay.mailchannels.net [23.83.212.4]) by sourceware.org (Postfix) with ESMTPS id 2741E393D009 for ; Tue, 22 Jun 2021 05:12:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2741E393D009 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 039C3401AF7; Tue, 22 Jun 2021 05:12:26 +0000 (UTC) Received: from pdx1-sub0-mail-a46.g.dreamhost.com (100-96-18-93.trex.outbound.svc.cluster.local [100.96.18.93]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 1BFB740216E; Tue, 22 Jun 2021 05:12:25 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a46.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.18.93 (trex/6.3.3); Tue, 22 Jun 2021 05:12:25 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Celery-Trade: 7462e89e773ab9c3_1624338745827_796092678 X-MC-Loop-Signature: 1624338745827:1331750803 X-MC-Ingress-Time: 1624338745826 Received: from pdx1-sub0-mail-a46.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a46.g.dreamhost.com (Postfix) with ESMTP id D45C0880B4; Mon, 21 Jun 2021 22:12:24 -0700 (PDT) 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-a46.g.dreamhost.com (Postfix) with ESMTPSA id EBA3E7F50D; Mon, 21 Jun 2021 22:12:22 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a46 From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Subject: [PATCH v2 1/6] iconv: Remove alloca use in gconv-modules configuration parsing Date: Tue, 22 Jun 2021 10:41:57 +0530 Message-Id: <20210622051202.4155709-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210622051202.4155709-1-siddhesh@sourceware.org> References: <20210622051202.4155709-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3494.6 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_H2, 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: Tue, 22 Jun 2021 05:12:28 -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. Reviewed-by: DJ Delorie --- 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