From patchwork Wed Apr 22 12:35:30 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 133634 X-Patchwork-Delegate: carlos@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id B42004BBC081 for ; Wed, 22 Apr 2026 12:36:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B42004BBC081 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=Thl0gxJI X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ua1-x92f.google.com (mail-ua1-x92f.google.com [IPv6:2607:f8b0:4864:20::92f]) by sourceware.org (Postfix) with ESMTPS id 1103B4B9209D for ; Wed, 22 Apr 2026 12:35:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1103B4B9209D 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 1103B4B9209D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::92f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776861344; cv=none; b=ZAtqTLSGvMFB9CLsOR8STmqkRWjMylDSE1/CfrGUapFwFCu8kBUxtBPY3ZDktyQT6PmAbQ3rjW5OG8O5Jix7D8luR1AZ6mnxqZj/CL+t6l6pofaEqOpfOmXoeKcMU8ehaHICXL+FBDnKKC5rJBaTXDBHC1Pku1x2zhHYItkWvPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776861344; c=relaxed/simple; bh=4vrqnlEtaTTruBWFQ/qeYqI0ttyzdkUJnfmQymYPidA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZjsX/LJwtPDpzZxFQ9hw698205R/nIHJPvzItO8nvDhK0u2Hk/x8VuogjN6z12adg8/ICO4hAv2LKdX/LCVHa8zu5BOjuRjpuCqKC31563lCtzBs4kHiPGEEOEPLOtG4/bdcWAmqo9DLPhI0c+KHgOLE9a/o6/8aaXXgf8zIhVo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1103B4B9209D Received: by mail-ua1-x92f.google.com with SMTP id a1e0cc1a2514c-94de68feaf4so2624281241.0 for ; Wed, 22 Apr 2026 05:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1776861343; x=1777466143; 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=d/GhnB8EuCP8E61zLVRdcopa88nu4WhX0gnJfGnrwsk=; b=Thl0gxJISBKeDvKExD5mpuVkpMp/ES27ygcEIXBzIILVesjnVJCizrKjYt08k2KHMM QJpP8Q0IODUafJOLPzbgcue0lParVITSSvMtKiuGTJtwyBXrKC1I3vyeWyZJjuHsHOFy yhxsJwrM2t1I/9yUBhOWGTeox6nOsIbtPyC2z93jzwl1DnFpmQuI5EdLz78liBjBlFPg P08KmuROh/TpexV+WhFso66wvk/kbmU6LceY7D/c+7gWfi2WfyMReDmN/4yKTBfQsFir GkRzqBRmguUU4XR2GeDX+k6G23v9zVATZsP+KCZI+9NFX7euEhx1biLoyNqrZGsXZ8yR PK7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776861343; x=1777466143; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=d/GhnB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0:605:6615:d9dd with SMTP id ada2fe7eead31-616f473aaf1mr12248971137.3.1776861342885; Wed, 22 Apr 2026 05:35:42 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:d5d0:3e95:69d2:d83e:ba1f]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-61745c9e51bsm8419090137.4.2026.04.22.05.35.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2026 05:35:42 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Siddhesh Poyarekar , Carlos O'Donell Subject: [PATCH] posix: Fix stack overflow in wordexp tilde expansion (BZ 34091, CVE-2026-6791) Date: Wed, 22 Apr 2026 09:35:30 -0300 Message-ID: <20260422123538.889818-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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 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 The parse_tilde function previously used strndupa to allocate memory for the parsed username on the stack, and since the input is user-defined, this can lead to a stack overflow. This patch fixes the issue by replacing strndupa with scratch_buffer, by reusing the buffer used in the __getpwnam_r call. The new “tst-wordexp-tilde.c” test is a test-container to avoid using system-defined NSS modules. Checked on x86_64-linux-gnu and i686-linux-gnu. --- posix/Makefile | 1 + posix/tst-wordexp-tilde.c | 244 ++++++++++++++++++ posix/tst-wordexp-tilde.root/etc/group | 1 + .../tst-wordexp-tilde.root/etc/nsswitch.conf | 3 + posix/tst-wordexp-tilde.root/etc/passwd | 1 + posix/wordexp.c | 28 +- 6 files changed, 270 insertions(+), 8 deletions(-) create mode 100644 posix/tst-wordexp-tilde.c create mode 100644 posix/tst-wordexp-tilde.root/etc/group create mode 100644 posix/tst-wordexp-tilde.root/etc/nsswitch.conf create mode 100644 posix/tst-wordexp-tilde.root/etc/passwd diff --git a/posix/Makefile b/posix/Makefile index a5e5162c61..b2de242321 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -359,6 +359,7 @@ tests-internal := \ tests-container := \ bug-ga2 \ tst-vfork3 \ + tst-wordexp-tilde \ # tests-container tests-time64 := \ diff --git a/posix/tst-wordexp-tilde.c b/posix/tst-wordexp-tilde.c new file mode 100644 index 0000000000..35f200a0b7 --- /dev/null +++ b/posix/tst-wordexp-tilde.c @@ -0,0 +1,244 @@ +/* Test wordexp tilde expansion with large usernames (BZ #XXXXX). + 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 + . */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +typedef void (*func_callback_t)(void); + +static void +subprocess_small_stack (void *closure) +{ + struct rlimit rl; + TEST_COMPARE (getrlimit (RLIMIT_STACK, &rl), 0); + rl.rlim_cur = 512 * 1024; + TEST_COMPARE (setrlimit (RLIMIT_STACK, &rl), 0); + + func_callback_t func_test = closure; + func_test (); +} + +/* Build a string "~/tail" where is LEN bytes of the + character CH. The caller must free the result. */ +static char * +make_tilde_input (char ch, size_t len, const char *tail) +{ + /* ~ + len + / + tail + \0 */ + size_t taillen = tail != NULL ? strlen (tail) : 0; + size_t total = 1 + len + 1 + taillen + 1; + char *buf = xmalloc (total); + buf[0] = '~'; + memset (buf + 1, ch, len); + buf[1 + len] = '/'; + if (tail != NULL) + memcpy (buf + 1 + len + 1, tail, taillen); + buf[total - 1] = '\0'; + return buf; +} + +/* Test 1: A very long username must not crash. The username will not match + any real user, so wordexp returns ~/rest. */ +static void +test_long_username (void) +{ + printf ("info: test_long_username_no_crash\n"); + + static const char REST[] = "rest"; + + /* 1 MiB username — well beyond any reasonable stack frame. */ + const size_t long_len = 1024 * 1024; + char *input = make_tilde_input ('A', long_len, REST); + + wordexp_t we = { 0 }; + int ret = wordexp (input, &we, 0); + /* The (non-existent) username is invalid, so wordexp falls back to + literal output: ~AAA…/rest. */ + TEST_COMPARE (ret, 0); + TEST_COMPARE (we.we_wordc, 1); + + /* Verify prefix: '~' followed by long_len 'A's. */ + const char *result = we.we_wordv[0]; + TEST_COMPARE (result[0], '~'); + TEST_COMPARE (strlen (result), + 1 /* ~ */ + long_len + sizeof (REST)); + for (size_t j = 1; j <= long_len; j++) + if (result[j] != 'A') + { + printf (" mismatch at position %zu: expected 'A', got '%c'\n", + j, result[j]); + support_record_failure (); + break; + } + /* Verify the tail after the username. */ + TEST_COMPARE_STRING (result + 1 + long_len, "/rest"); + + wordfree (&we); + free (input); +} + +/* Test 2: A username that just exceeds the default scratch_buffer inline + size (1024 bytes) exercises the scratch_buffer_set_array_size growth path + without being excessively large. */ +static void +test_scratch_buffer_growth (void) +{ + printf ("info: test_scratch_buffer_growth\n"); + + const size_t len = 2048; + char *input = make_tilde_input ('x', len, NULL); + + wordexp_t we = { 0 }; + int ret = wordexp (input, &we, 0); + TEST_COMPARE (ret, 0); + TEST_COMPARE (we.we_wordc, 1); + + /* ~xxx…/ — the trailing slash makes a separate empty component, but + wordexp merges it into the single token ~xxx…/. */ + const char *result = we.we_wordv[0]; + TEST_COMPARE (result[0], '~'); + for (size_t j = 1; j <= len; j++) + if (result[j] != 'x') + { + printf (" mismatch at position %zu\n", j); + support_record_failure (); + break; + } + TEST_COMPARE (result[1 + len], '/'); + + wordfree (&we); + free (input); +} + +/* Test 3: ~root still resolves to the correct home directory through the + __getpwnam_r path. */ +static void +test_known_user (void) +{ + printf ("info: test_known_user\n"); + + /* Look up root's home directory for comparison. */ + struct passwd *pw = getpwnam ("root"); + if (pw == NULL || pw->pw_dir == NULL) + { + printf (" SKIP: cannot look up root\n"); + return; + } + + char *expected = xasprintf ("%s/file", pw->pw_dir); + + wordexp_t we = { 0 }; + TEST_COMPARE (wordexp ("~root/file", &we, 0), 0); + TEST_COMPARE (we.we_wordc, 1); + TEST_COMPARE_STRING (we.we_wordv[0], expected); + + wordfree (&we); + free (expected); +} + +/* Test 4: Bare tilde expands to $HOME. */ +static void +test_bare_tilde (void) +{ + printf ("info: test_bare_tilde\n"); + + const char *home = getenv ("HOME"); + if (home == NULL) + { + printf (" SKIP: HOME is not set\n"); + return; + } + + wordexp_t we = { 0 }; + TEST_COMPARE (wordexp ("~", &we, 0), 0); + TEST_COMPARE (we.we_wordc, 1); + TEST_COMPARE_STRING (we.we_wordv[0], home); + + wordfree (&we); +} + +/* Test 5: Short non-existent username falls back to literal ~username output, + exercising the invalid-login-name path. */ +static void +test_unknown_user (void) +{ + printf ("info: test_unknown_user\n"); + + /* Pick a username that is extremely unlikely to exist. */ + wordexp_t we = { 0 }; + TEST_COMPARE (wordexp ("~no_such_user_xyzzy42", &we, 0), 0); + TEST_COMPARE (we.we_wordc, 1); + TEST_COMPARE_STRING (we.we_wordv[0], "~no_such_user_xyzzy42"); + + wordfree (&we); +} + +/* Test 6: Tilde with username and WRDE_APPEND — exercises parse_tilde's + interaction with the WRDE_APPEND word list. */ +static void +test_tilde_with_append (void) +{ + printf ("info: test_tilde_with_append\n"); + + const char *home = getenv ("HOME"); + if (home == NULL) + { + printf (" SKIP: HOME is not set\n"); + return; + } + + wordexp_t we = { 0 }; + TEST_COMPARE (wordexp ("first", &we, 0), 0); + + TEST_COMPARE (wordexp ("~/path", &we, WRDE_APPEND), 0); + TEST_COMPARE (we.we_wordc, 2); + TEST_COMPARE_STRING (we.we_wordv[0], "first"); + + char *expected = xasprintf ("%s/path", home); + TEST_COMPARE_STRING (we.we_wordv[1], expected); + + wordfree (&we); + free (expected); +} + +static int +do_test (void) +{ + test_known_user (); + test_bare_tilde (); + test_unknown_user (); + test_tilde_with_append (); + + support_isolate_in_subprocess (subprocess_small_stack, + test_long_username); + + support_isolate_in_subprocess (subprocess_small_stack, + test_scratch_buffer_growth); + + return 0; +} + +#include diff --git a/posix/tst-wordexp-tilde.root/etc/group b/posix/tst-wordexp-tilde.root/etc/group new file mode 100644 index 0000000000..1dbf9013ee --- /dev/null +++ b/posix/tst-wordexp-tilde.root/etc/group @@ -0,0 +1 @@ +root:x:0: diff --git a/posix/tst-wordexp-tilde.root/etc/nsswitch.conf b/posix/tst-wordexp-tilde.root/etc/nsswitch.conf new file mode 100644 index 0000000000..098a8d5938 --- /dev/null +++ b/posix/tst-wordexp-tilde.root/etc/nsswitch.conf @@ -0,0 +1,3 @@ +passwd: files +group: files +shadow: files diff --git a/posix/tst-wordexp-tilde.root/etc/passwd b/posix/tst-wordexp-tilde.root/etc/passwd new file mode 100644 index 0000000000..eb85a552ad --- /dev/null +++ b/posix/tst-wordexp-tilde.root/etc/passwd @@ -0,0 +1 @@ +root:x:0:0:root:/root:/bin/sh diff --git a/posix/wordexp.c b/posix/wordexp.c index 4a8541add4..a6b9391906 100644 --- a/posix/wordexp.c +++ b/posix/wordexp.c @@ -335,17 +335,29 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, else { /* Look up user name in database to get home directory */ - char *user = strndupa (&words[1 + *offset], i - (1 + *offset)); - struct passwd pwd, *tpwd; - int result; + size_t userlen = i - (1 + *offset); + /* tmpbuf contains both the user and the __getpwnam_r working area. */ struct scratch_buffer tmpbuf; scratch_buffer_init (&tmpbuf); + if (!scratch_buffer_set_array_size (&tmpbuf, userlen + 1, 1)) + return WRDE_NOSPACE; + char *user = tmpbuf.data; + memcpy (user, &words[1 + *offset], userlen); + user[userlen] = '\0'; - while ((result = __getpwnam_r (user, &pwd, tmpbuf.data, tmpbuf.length, - &tpwd)) != 0 - && errno == ERANGE) - if (!scratch_buffer_grow (&tmpbuf)) - return WRDE_NOSPACE; + struct passwd pwd, *tpwd; + int result; + while ((result = __getpwnam_r (user, + &pwd, + tmpbuf.data + userlen + 1, + tmpbuf.length - userlen - 1, + &tpwd) + != 0)) + { + if (!scratch_buffer_grow_preserve (&tmpbuf)) + return WRDE_NOSPACE; + user = tmpbuf.data; + } if (result == 0 && tpwd != NULL && pwd.pw_dir) *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);