From patchwork Thu Oct 19 17:24:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 78181 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 BEB503857700 for ; Thu, 19 Oct 2023 17:27:03 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0E3AC3858C53 for ; Thu, 19 Oct 2023 17:26:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E3AC3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0E3AC3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697736410; cv=none; b=E0mvfZB6UvdXRMU9+P22nH0/cnZhE1bAIsjlAp/LAlMNlyBksfena1b8Wh7/j1lT81ofc0KFc12P4RFOpU9OiybqdzMNkQylzdC5WYg1XlsKYEaptD1uqwsca+U4rhFBS7ei7CXlZ1eoUX0KU3+gInbaABpes90MNlLf5c5XlcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697736410; c=relaxed/simple; bh=PV2Ne5RgaDJliuj01MhyoBfwmDBDemPhmLrxU4FJeB4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lzeHhJhaLJPBElIquN3DJ4/NNbEAcqhpy9iFo16IGCDZemKxZ4HOPk15a3BWP4dpDX9w/kHAYJgJ38zDo+VBegJK6KdKDMjdkAOa4LWam5ktY+xlrrK5qNI9PhG77vtPJOhfhsu0Q+wViEG1ObhHKR2Kd7Zw+peS6H2QwkNurOk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697736408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2gnCLp1mAPYTNJr1vQqO0BmnT5v5Fbhc+cwI1n7Y/2Q=; b=QobUtbUdmCFAazVEgK2T0FRgq29/ChKzkdFlg4qmkCxfE7uXLqJRdJKCMEcZ8H1pUcCR8w PgX6HthJTXIOJnAOfT/Z45itP4knd+rsss6hZp9WeEqJPKn7w5zIqmQ6sUsM1K0m7dXVf8 dpHBFJH9kRJxdq47SHzFwQ6PsvInXNY= Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-142-_lOuEG5-MWmGLC3nZelMrw-1; Thu, 19 Oct 2023 13:26:47 -0400 X-MC-Unique: _lOuEG5-MWmGLC3nZelMrw-1 Received: by mail-vk1-f200.google.com with SMTP id 71dfb90a1353d-49e213c7d7aso2996279e0c.0 for ; Thu, 19 Oct 2023 10:26:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697736406; x=1698341206; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2gnCLp1mAPYTNJr1vQqO0BmnT5v5Fbhc+cwI1n7Y/2Q=; b=jCK1JTkkQsAIgtEI45yQLX/wTmEuPCzCewrG1TiYKHsoXlFoZMsDUGvwJqUVcRbunE VgfQRzm448IEWMr4purU9mUUJeUOev+mVt+QfakX2I0NSKvOzYd9A0rsoNOm4LXjzxpb 8aQk+oTN8dDIb9lQF6c2ltl1IQ9Ta2+rpFzLvEwcdPwzBhUbsENGgPQYNdgjV8qU45+X OUGSsjiaJqYWsBqf5xhIocpDgGleUY/qQ8XfimNrIVec/rBhgoeFjnwMzCRONU3TlfYE AiNkF+SsjoIWugrl5q3s/LVIaaIg91ZX01dMyqHpPCqudgrXjsp1Ih04PeUChIVyuG0M 857A== X-Gm-Message-State: AOJu0Yy3/VB/Izylnfhyq2J+ohdLt2X4LlBXibJYjNECjZt5yrdFOfnr s8vCOUP6ZjEGFU2Xd3RldgNJU8cMrpDfrTFPJAy8SkUbEYnrGbEXRJ9W2IYVFhNtdUlsI+/gi// YVB5J1GaY2M+bLmvdcGmfob8G5BnC68HoDjmvKmFBz+JY4pjp0UCn7/ULHKQWHuM2SKb4zMZtOG 7IBPJV X-Received: by 2002:a05:6122:3c54:b0:49a:b737:4dfa with SMTP id fv20-20020a0561223c5400b0049ab7374dfamr2984326vkb.4.1697736405754; Thu, 19 Oct 2023 10:26:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHL9s417tup706PzDJ2wPfzeG9AdAuD/BYIb8k77yNucovE20qh71xCML457pU+oO8RBN2GYg== X-Received: by 2002:a05:6122:3c54:b0:49a:b737:4dfa with SMTP id fv20-20020a0561223c5400b0049ab7374dfamr2984305vkb.4.1697736405405; Thu, 19 Oct 2023 10:26:45 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id fd3-20020a05622a4d0300b003f6ac526568sm877360qtb.39.2023.10.19.10.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 10:26:45 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3] elf: dl-load: Get rid of alloca usage. Date: Thu, 19 Oct 2023 13:24:48 -0400 Message-ID: <20231019172642.1954729-1-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 server2.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 Replace alloca usage with a scratch_buffer. Change the semantics of is_trusted_path_normalize to return 1, 0, or -1 on error. Change _dl_dst_substitute to return NULL on error and update callers to handle the NULL return value. --- Changes to v2: * Remove 3 other alloca removals as separate patches. * Use "Memory allocation failure" message rather than a new one. * Simplify multiple returns into one. * Only call is_trusted_path_normalize if check_for_trusted is set. * In expand_dynamic_string_token handle a NULL return from _dl_dst_substitute and free malloc'ed memory. * Print an error message in fillin_rpath when expand_dynamic_string_token returns NULL. elf/dl-deps.c | 5 +++-- elf/dl-load.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/elf/dl-deps.c b/elf/dl-deps.c index 0549b4a4ff..16077384b6 100644 --- a/elf/dl-deps.c +++ b/elf/dl-deps.c @@ -100,8 +100,9 @@ DST not allowed in SUID/SGID programs")); \ __dst_cnt)); \ \ __result = _dl_dst_substitute (l, __str, __newp); \ - \ - if (*__result == '\0') \ + if (__result == NULL) \ + _dl_signal_error (0, __str, NULL, N_("Memory allocation failure")); \ + else if (*__result == '\0') \ { \ /* The replacement for the DST is not known. We can't \ processed. */ \ diff --git a/elf/dl-load.c b/elf/dl-load.c index 2923b1141d..1133421fd8 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -124,14 +125,21 @@ static const size_t system_dirs_len[] = }; #define nsystem_dirs_len array_length (system_dirs_len) -static bool +static int is_trusted_path_normalize (const char *path, size_t len) { if (len == 0) - return false; + return 0; + + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); - char *npath = (char *) alloca (len + 2); + if (!scratch_buffer_set_array_size (&sbuf, 1, len + 2)) + return -1; + + char *npath = sbuf.data; char *wnp = npath; + while (*path != '\0') { if (path[0] == '/') @@ -167,17 +175,22 @@ is_trusted_path_normalize (const char *path, size_t len) const char *trun = system_dirs; + int r = 0; for (size_t idx = 0; idx < nsystem_dirs_len; ++idx) { if (wnp - npath >= system_dirs_len[idx] && memcmp (trun, npath, system_dirs_len[idx]) == 0) /* Found it. */ - return true; + { + r = 1; + break; + } trun += system_dirs_len[idx] + 1; } - return false; + scratch_buffer_free (&sbuf); + return r; } /* Given a substring starting at INPUT, just after the DST '$' start @@ -270,7 +283,7 @@ _dl_dst_count (const char *input) least equal to the value returned by DL_DST_REQUIRED. Note that it is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to have colons, but we treat those as literal colons here, not as path - list delimiters. */ + list delimiters. Returns NULL on failure. */ char * _dl_dst_substitute (struct link_map *l, const char *input, char *result) { @@ -362,11 +375,16 @@ _dl_dst_substitute (struct link_map *l, const char *input, char *result) trusted to have designed this correctly. Only $ORIGIN is tested in this way because it may be manipulated in some ways with hard links. */ - if (__glibc_unlikely (check_for_trusted) - && !is_trusted_path_normalize (result, wp - result)) + if (__glibc_unlikely (check_for_trusted)) { - *result = '\0'; - return result; + int r = is_trusted_path_normalize (result, wp - result); + if (r == -1) + return NULL; + else if (r == 0) + { + *result = '\0'; + return result; + } } *wp = '\0'; @@ -406,7 +424,11 @@ expand_dynamic_string_token (struct link_map *l, const char *input) if (result == NULL) return NULL; - return _dl_dst_substitute (l, input, result); + char *r = _dl_dst_substitute (l, input, result); + if (r == NULL) + free (result); + + return r; } @@ -485,7 +507,11 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, /* expand_dynamic_string_token can return NULL in case of empty path or memory allocation failure. */ if (cp == NULL) - continue; + { + _dl_signal_error (errno, NULL, NULL, + N_("empty path or memory allocation failure")); + continue; + } /* Compute the length after dynamic string token expansion and ignore empty paths. */