From patchwork Fri Sep 11 16:58:59 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 143491 X-Patchwork-Delegate: Wilco.Dijkstra@arm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A53524849D1F for ; Fri, 11 Sep 2026 17:00:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A53524849D1F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=kRtSbKQ4 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk2-x10.google.com (mail-qk2-x10.google.com [IPv6:2607:f8b0:4864:34::10]) by sourceware.org (Postfix) with ESMTPS id DFF374898CA8 for ; Fri, 11 Sep 2026 16:59:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFF374898CA8 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 DFF374898CA8 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:34::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789145997; cv=none; b=Cci0EA+0Eq76btxqxF697mMNZD6qI6u99iT9CSnH+F2tkEX2puZ5xay6fsrBvFfwqrjYfu+zvGgOgABarn7pjun85q/vnWIsKuH8+FVXqa6njL8itDBUhTU4USA7vpAdswUI4nMSH67JRerEneTjcfZSSqWlccGXtL5ua4rPccg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789145997; c=relaxed/simple; bh=hM7orIB2qBckG1G1/GPSjEuiVuaGFuSVfa0bmkXCAv0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=iR/IM/5aOicRMa5iyt3vcQ9cCQv00sLJqQY3cDKqhMTgC7XvQkb3vWUQhyWmUwdnPK60obcNzJPC1T3Lc7ADF7Lxw15Ru3/yacu18ROeDp5WC4vKDShfN8UhY2YFT7brzg1k4Uik6q0LnXfiNdVRD8TDQcE8EeHG8XKUSlcnczw= ARC-Authentication-Results: i=1; sourceware.org Received: by mail-qk2-x10.google.com with SMTP id af79cd13be357-939109fafd6so115189085a.3 for ; Fri, 11 Sep 2026 09:59:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1789145997; x=1789750797; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9M7cxk/loZS1Wmb0V0wf43i0jZ1DQuuzAhIIBmI3py4=; b=kRtSbKQ4PcvPAQ1J8oniviA/EXhZCpC7BYNmh5YsMKc4Dnh+bth4TUuJwxriLj0SRn QGy8rwKWTt61Oc9V5gBjqttMnfOc03fiZhEq2O+/A6XLks9o3q4wsivMeLq1s8G/lDfe 11gjHKGAGERmFVNcyPSNucesI2iAnJmx/svsyy0KpOp+04pFenpD785Qf4GXc082kSHi 39ZHfjzasLCmtCasDugGPdZi7jEILDvNhcKJpoEwlPK47dgB+G8Ko0tA52vVSBUx/njt 0FefBiiVedqrisKUeKZEUuvIUqqeL6PKJNoI/mgMBv+oC88VMQ9sTuqhVzXcq/qm8S89 nB0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789145997; x=1789750797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=9M7cxk/loZS1Wmb0V0wf43i0jZ1DQuuzAhI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-Received: by 2002:a05:620a:19a9:b0:937:80a9:94b4 with SMTP id af79cd13be357-939ea048e6dmr685530685a.2.1789145997079; Fri, 11 Sep 2026 09:59:57 -0700 (PDT) Received: from mandiga ([2804:1b3:a7c3:566e:1ba8:8d15:47b2:9eb]) by smtp.gmail.com with ESMTPSA id af79cd13be357-939f1dd527asm159040985a.16.2026.09.11.09.59.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 09:59:56 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Shamil Abdulaev Subject: [PATCH 1/3] posix: Fix wordexp buffer overflow for $* without positional parameters [BZ #34608] Date: Fri, 11 Sep 2026 13:58:59 -0300 Message-ID: <20260911165932.1737113-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260911165932.1737113-1-adhemerval.zanella@linaro.org> References: <20260911165932.1737113-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 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_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@sourceware.org When expanding an unquoted '$*' or '$@', parse_param sizes the buffer correclty only when there is at least one positional parameter. A process invoked without positional parameters makes the loop compute a length of zero, and then the terminating null byte access invalid memory. Checked on x86_64-linux-gnu and i686-linux-gnu. Reported-by: Shamil Abdulaev --- posix/Makefile | 7 +++++ posix/tst-wordexp-noargs.c | 64 ++++++++++++++++++++++++++++++++++++++ posix/wordexp.c | 3 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 posix/tst-wordexp-noargs.c diff --git a/posix/Makefile b/posix/Makefile index ae26443ac9b..bbbb5603122 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -332,11 +332,18 @@ tests := \ tst-wait4 \ tst-waitid \ tst-wordexp-append \ + tst-wordexp-noargs \ tst-wordexp-nocmd \ tst-wordexp-reuse \ tstgetopt \ # tests +ifeq ($(build-shared),yes) +tests-mcheck += \ + tst-wordexp-noargs \ + # tests-mcheck +endif + # Test for the glob symbol version that was replaced in glibc 2.27. ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes) tests += \ diff --git a/posix/tst-wordexp-noargs.c b/posix/tst-wordexp-noargs.c new file mode 100644 index 00000000000..589028b6767 --- /dev/null +++ b/posix/tst-wordexp-noargs.c @@ -0,0 +1,64 @@ +/* Test wordexp expansion of $* or $@ without positional parameters (BZ 34608). + Copyright (C) 2026 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 + . */ + +/* The testsuite runs this without positional parameters, so there is + nothing for $* and $@ below to expand to. */ + +#include + +#include + +static void +check_no_field (const char *words) +{ + wordexp_t we = { 0 }; + + TEST_COMPARE (wordexp (words, &we, 0), 0); + TEST_COMPARE (we.we_wordc, 0); + + wordfree (&we); +} + +static void +check_one_field (const char *words, const char *expected) +{ + wordexp_t we = { 0 }; + + TEST_COMPARE (wordexp (words, &we, 0), 0); + TEST_COMPARE (we.we_wordc, 1); + TEST_COMPARE_STRING (we.we_wordv[0], expected); + + wordfree (&we); +} + +static int +do_test (void) +{ + /* Both spellings take the same code path when unquoted, and field + splitting leaves nothing behind. */ + check_no_field ("$*"); + check_no_field ("$@"); + + /* ${#*} and ${#@} report the number of positional parameters. */ + check_one_field ("${#*}", "0"); + check_one_field ("${#@}", "0"); + + return 0; +} + +#include diff --git a/posix/wordexp.c b/posix/wordexp.c index 9a49e102eea..0ad005c0f38 100644 --- a/posix/wordexp.c +++ b/posix/wordexp.c @@ -1449,7 +1449,8 @@ envsubst: /* Build up value parameter by parameter (copy them) */ for (p = 1; __libc_argv[p]; ++p) plist_len += strlen (__libc_argv[p]) + 1; /* for space */ - value = malloc (plist_len); + /* Always compute the NULL byte. */ + value = malloc (plist_len + 1); if (value == NULL) goto no_space; end = value;