From patchwork Wed Oct 18 13:08:43 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: 78089 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 85442385843A for ; Wed, 18 Oct 2023 13:09:12 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id CDE2B3858D33 for ; Wed, 18 Oct 2023 13:08:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDE2B3858D33 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 CDE2B3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697634539; cv=none; b=nU7gCI345JcihSsCD62nxU7EL/Pq0wnWfURPs2qK5RDnCstp9m50+ENv1HvHE2Sy2z8HDU/I3NN3BKFWyo22ZGCapk2X1VAtHwq4Xpgjs24gzZOdiX8P58IzOn4lnFyHV4DvQbw8Jih6n8dJ0L3GyBTv7AXpgOV8jCF0SwZymzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697634539; c=relaxed/simple; bh=9Jvtx14XIc0AdHVLer2Twbsw+DjtFg2e7hnmSZ1PRjE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JSCKItMO+LXzJQWc+idZb2bd7b4If49E0ZCwCeOfJyUJX3dDDK8adF8RLWDhNNkPkRbHhsg/txpoKCSRX5PMBnuMA063QPrmmCyvycFvjIQQAuqRVXcX80CzMOjZinDD4XSfhpL2wzphwWIA2e7TWrjv8Be1Ed7CyQAe3wawtbc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697634537; 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=F3VmBBMbMr4eXWg9kv9PKjNoNjCJGFA2cYw7dM/g+2c=; b=iOIc8SXIqcH+uavhEe1QObpBOHmjRFb1NdBneI5xRQbU/78DqKSpKxgDqlc81ReqO5I0qa 4Rd97Yiw/aMuKYQe7oEWdn71SE0jZ3cjkjj2pM06d6A6iAsD3t09qknJmDa0Lt9K5Dkq8+ 9OkK5JcZhFfo0OFXiw1SQ166/2BTJTI= Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-681-Hnhj-cfkMFWdszSsRu3BKQ-1; Wed, 18 Oct 2023 09:08:51 -0400 X-MC-Unique: Hnhj-cfkMFWdszSsRu3BKQ-1 Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-5a7cf717bacso105954737b3.1 for ; Wed, 18 Oct 2023 06:08:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697634530; x=1698239330; 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=2sedyapmA8jpoyofrrW/UgexCXokdn0ookd/y810/ZM=; b=BlBuJ1BVVWQMfNWjvntvl63z+94P2Nql9CgRyBJGhICa3YCDnHkOHGQBFXZui6/+/Q 4gLkMBWjKEFfVOBueGXYN+ewO/Qu7iN7C2YKsg4wlm4xX6trPyocg1PQ5r7j3NuuhLho 8DUrpfUZGRAZo9kRkXKqPJbnHMtYtUPnGiRdeX4EO2hzqXAQUWpOEqTXDXTE2b/1V94W RU4Iq0Ygmmq4/OmJE7Cif77JRLf4118lLklKslEIlNoIhL6L/9KbwnumsR+n39iqVDY0 QfUXnY8B5iTPy0O5ZwbvNM0YNdHkvLd85ORuAFSUrH961cWNsn1GWBrwTACeefjf+RSF /Whg== X-Gm-Message-State: AOJu0YzSefVRnKuCEsDDUF/wjkIfROloc2DkyR7xiXv9wG/9HroSMRAo ugflL1ef04ZwToRm1AM0S2pGZV5CbNSKTdGM2Q2FXziCbUphfXl8cN3p7xWfqX75snztuYxMDVj FcIusTJS7Bijcg7jufI8SkNfzIzj1jVUuLsA+GCxr93IYV3fP2gV7QzHa4pN0EAXcKutEBk7u49 DTHRWY X-Received: by 2002:a0d:f701:0:b0:5a7:b481:4dd2 with SMTP id h1-20020a0df701000000b005a7b4814dd2mr5088791ywf.47.1697634530051; Wed, 18 Oct 2023 06:08:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGX7yJzeYIfNi797xp9GFhzTBHumDE9f//YbGVzm+sPHr0YBRWlMITJ1WJOsOKk4xzonXvtA== X-Received: by 2002:a0d:f701:0:b0:5a7:b481:4dd2 with SMTP id h1-20020a0df701000000b005a7b4814dd2mr5088767ywf.47.1697634529675; Wed, 18 Oct 2023 06:08:49 -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 o80-20020a0dcc53000000b0059bcadded9dsm1480411ywd.116.2023.10.18.06.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 06:08:49 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] elf: dl-load: Get rid of alloca usage. Date: Wed, 18 Oct 2023 09:08:43 -0400 Message-ID: <20231018130847.1570121-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=-11.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, 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 scratch_buffers. 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 v1: * _dl_dst_substitute returns NULL on failure. * update callers of _dl_dst_substitute to handle NULL return value. * Add missing scratch_buffer_free calls. elf/dl-deps.c | 5 +-- elf/dl-load.c | 89 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/elf/dl-deps.c b/elf/dl-deps.c index 0549b4a4ff..c6f67c3e27 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 failed")); \ + 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..23b3b80c88 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; - char *npath = (char *) alloca (len + 2); + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); + + 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] == '/') @@ -171,13 +179,17 @@ is_trusted_path_normalize (const char *path, size_t len) { if (wnp - npath >= system_dirs_len[idx] && memcmp (trun, npath, system_dirs_len[idx]) == 0) - /* Found it. */ - return true; + { + scratch_buffer_free (&sbuf); + /* Found it. */ + return 1; + } trun += system_dirs_len[idx] + 1; } - return false; + scratch_buffer_free (&sbuf); + return 0; } /* Given a substring starting at INPUT, just after the DST '$' start @@ -270,7 +282,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) { @@ -283,6 +295,7 @@ _dl_dst_substitute (struct link_map *l, const char *input, char *result) char *wp = result; const char *start = input; bool check_for_trusted = false; + int itpn; do { @@ -362,8 +375,14 @@ _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)) + itpn = is_trusted_path_normalize (result, wp - result); + if (itpn == -1) + { + _dl_signal_error (ENOMEM, NULL, NULL, N_("Failed to allocate memory")); + return NULL; + } + + if (__glibc_unlikely (check_for_trusted) && itpn) { *result = '\0'; return result; @@ -951,6 +970,8 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* Initialize to keep the compiler happy. */ const char *errstring = NULL; int errval = 0; + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); /* Get file information. To match the kernel behavior, do not fill in this information for the executable in case of an explicit @@ -982,6 +1003,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, free ((void *) l->l_phdr); free (l); free (realname); + scratch_buffer_free (&sbuf); _dl_signal_error (errval, name, NULL, errstring); } @@ -998,6 +1020,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, free (realname); add_name_to_object (l, name); + scratch_buffer_free (&sbuf); return l; } } @@ -1029,6 +1052,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* Add the map for the mirrored object to the object list. */ _dl_add_to_namespace_list (l, nsid); + scratch_buffer_free (&sbuf); return l; } #endif @@ -1039,6 +1063,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, loaded. So return now. */ free (realname); __close_nocancel (fd); + scratch_buffer_free (&sbuf); return NULL; } @@ -1071,7 +1096,12 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, phdr = (void *) (fbp->buf + header->e_phoff); else { - phdr = alloca (maplength); + if (!scratch_buffer_set_array_size (&sbuf, 1, maplength)) + { + errstring = N_("cannot allocate memory"); + goto lose_errno; + } + phdr = sbuf.data; if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, header->e_phoff) != maplength) { @@ -1485,7 +1515,10 @@ cannot enable executable stack as shared object requires"); /* Skip auditing and debugger notification when called from 'sprof'. */ if (mode & __RTLD_SPROF) - return l; + { + scratch_buffer_free (&sbuf); + return l; + } /* Signal that we are going to add new objects. */ struct r_debug *r = _dl_debug_update (nsid); @@ -1515,6 +1548,7 @@ cannot enable executable stack as shared object requires"); _dl_audit_objopen (l, nsid); #endif + scratch_buffer_free (&sbuf); return l; } @@ -1598,6 +1632,8 @@ open_verify (const char *name, int fd, /* Initialize it to make the compiler happy. */ const char *errstring = NULL; int errval = 0; + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); #ifdef SHARED /* Give the auditing libraries a chance. */ @@ -1660,6 +1696,7 @@ open_verify (const char *name, int fd, name = strdupa (realname); free (realname); } + scratch_buffer_free (&sbuf); __close_nocancel (fd); _dl_signal_error (errval, name, NULL, errstring); } @@ -1696,6 +1733,7 @@ open_verify (const char *name, int fd, 32-bit and 64-bit binaries can be run this might happen. */ *found_other_class = true; + scratch_buffer_free (&sbuf); __close_nocancel (fd); __set_errno (ENOENT); return -1; @@ -1734,6 +1772,7 @@ open_verify (const char *name, int fd, } if (! __glibc_likely (elf_machine_matches_host (ehdr))) { + scratch_buffer_free (&sbuf); __close_nocancel (fd); __set_errno (ENOENT); return -1; @@ -1755,7 +1794,14 @@ open_verify (const char *name, int fd, phdr = (void *) (fbp->buf + ehdr->e_phoff); else { - phdr = alloca (maplength); + if (!scratch_buffer_set_array_size (&sbuf, 1, maplength)) + { + errval = errno; + errstring = N_("cannot allocate memory"); + goto lose; + } + phdr = sbuf.data; + if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength, ehdr->e_phoff) != maplength) { @@ -1769,6 +1815,7 @@ open_verify (const char *name, int fd, (phdr, ehdr->e_phnum, fbp->buf, fbp->len, loader, fd))) { + scratch_buffer_free (&sbuf); __close_nocancel (fd); __set_errno (ENOENT); return -1; @@ -1776,6 +1823,7 @@ open_verify (const char *name, int fd, } + scratch_buffer_free (&sbuf); return fd; } @@ -1796,13 +1844,18 @@ open_path (const char *name, size_t namelen, int mode, int fd = -1; const char *current_what = NULL; int any = 0; + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); if (__glibc_unlikely (dirs == NULL)) /* We're called before _dl_init_paths when loading the main executable given on the command line when rtld is run directly. */ return -1; - buf = alloca (max_dirnamelen + max_capstrlen + namelen); + if (!scratch_buffer_set_array_size (&sbuf, 1, + max_dirnamelen + max_capstrlen + namelen)) + return -1; + buf = sbuf.data; do { struct r_search_path_elem *this_dir = *dirs; @@ -1901,6 +1954,7 @@ open_path (const char *name, size_t namelen, int mode, if (*realname != NULL) { memcpy (*realname, buf, buflen); + scratch_buffer_free (&sbuf); return fd; } else @@ -1908,12 +1962,16 @@ open_path (const char *name, size_t namelen, int mode, /* No memory for the name, we certainly won't be able to load and link it. */ __close_nocancel (fd); + scratch_buffer_free (&sbuf); return -1; } } if (here_any && (err = errno) != ENOENT && err != EACCES) - /* The file exists and is readable, but something went wrong. */ - return -1; + { + /* The file exists and is readable, but something went wrong. */ + scratch_buffer_free (&sbuf); + return -1; + } /* Remember whether we found anything. */ any |= here_any; @@ -1934,6 +1992,7 @@ open_path (const char *name, size_t namelen, int mode, sps->dirs = (void *) -1; } + scratch_buffer_free (&sbuf); return -1; }