From patchwork Tue Jun 22 05:11:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43941 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D8582385501B for ; Tue, 22 Jun 2021 05:13:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D8582385501B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624338811; bh=8giCMhpTYlQEF+awMGJyN3TQfAWc8T8rTZ56/qbGKMw=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ppkvg1IT21F/2V/7p3bNZagXoheOxfbIZtTw0dkhv2NiEBR0X71a2pbLXFA3U/r8l 8O4Pp5Cx611bvqNtN0P/pSOGu0f7lVW/HdMxiaPsZOx7I893ikg2KLwTXmGCu8QCYz c7NW6+Ak10VI9NpD0CrOJ2vW/upQuuk5Q7E4PeAA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 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 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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) for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt) { -#define BUF_LEN elem_len + sizeof (gconv_conf_dirname) - const char *elem = __gconv_path_elem[cnt].name; size_t elem_len = __gconv_path_elem[cnt].len; - char *buf; /* No slash needs to be inserted between elem and gconv_conf_filename; elem already ends in a slash. */ - buf = alloca (BUF_LEN); + char *buf = malloc (elem_len + sizeof (gconv_conf_dirname)); + if (buf == NULL) + continue; + char *cp = __mempcpy (__mempcpy (buf, elem, elem_len), gconv_conf_filename, sizeof (gconv_conf_filename)); @@ -596,15 +596,16 @@ __gconv_read_conf (void) if (len > strlen (suffix) && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0) { - /* LEN <= PATH_MAX so this alloca is not unbounded. */ - char *conf = alloca (BUF_LEN + len + 1); - cp = 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 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 = strlen (dir); bool found = false; @@ -726,7 +725,10 @@ handle_dir (const char *dir) } /* First, look for a gconv-modules file. */ - char buf[BUF_LEN]; + char *buf = malloc (prefix_len + dirlen + sizeof "gconv-modules.d"); + if (buf == NULL) + goto out; + cp = buf; if (dir[0] == '/') cp = 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) == 0) { - /* LEN <= PATH_MAX so this alloca is not unbounded. */ - char *conf = alloca (BUF_LEN + len + 1); - cp = stpcpy (conf, buf); - sprintf (cp, "/%s", ent->d_name); + char *conf; + if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0) + continue; found |= handle_file (dir, conf); + free (conf); } } closedir (confdir); } + free (buf); + +out: if (!found) { error (0, errno, "failed to open gconv configuration files in `%s'",