From patchwork Mon Oct 2 13:24:01 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: 76973 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 228C5385CC8E for ; Mon, 2 Oct 2023 13:24:24 +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 94A2F3858412 for ; Mon, 2 Oct 2023 13:24:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94A2F3858412 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696253050; 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=sHYPzpTUJqFn9S5bpJbFlTFdgxZ0w4wBfGISHgo2z5k=; b=OD5t2cZnSAiUh2PLhyrBuEbZX/HhMaWdS2hRL2A8mCm36pRGlYvGWxbzYI87P1Sd5V2eLY zUcZpijFVwXADQnwUCVTdwLyJ/e/d+zf8ybCcu20ZgJjxizvNUe9m1ztYqZsEeUXtbP19R Pfaf3YAk3WbE6cB4WIfutzsl49WEVNY= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-67-dhH9PqqUNSW_n9zk2yoSVQ-1; Mon, 02 Oct 2023 09:24:09 -0400 X-MC-Unique: dhH9PqqUNSW_n9zk2yoSVQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7740517a478so2855977285a.3 for ; Mon, 02 Oct 2023 06:24:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696253048; x=1696857848; 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=7m+Oq9D7xG6KpOYXimPi5H3LKpRny74lwjVJjm0XkrY=; b=DNvc8k4fzKms3jmf5efsMtJoGidPPVBJ/rWarOF2qBMV0iQABMgtGjiBZuDev2xxg4 d9InZOR5IFsq3SL4gyWDcsa43rKw1Eby+tLn1paI3nhsgyF9Qij+goGQrHwJg4q6MRwV 8jZ5gfn1lmy+dL0m3KNqZOhrZpqEkz0MtjRpGYFoJUvkDKQji/+3JZYlciT6oFwBVIj0 sMl+ZoJwUzoZKkOFgG1U1Yr2uptBDoQTBeqDASM4u5ZC0+HPTjBZnaVK9ZDIOO52Z6Zp SitL7YB9CpJMYAMAs6Ww8pzNZ69jQ/VyftcqzOJsLjpeYogEjtg2SjbuvFb/cNPQX50K Z5hg== X-Gm-Message-State: AOJu0YzQVx4vw9/6qoEs1MLxwPcruX0m59wJQQuzWUvOYAINr8pKwwtL XhAS3JV+GtYOfWd0OFqkPScHfw5mcd/DrvVySLjpAhjO9TUH+SfREy0hQRrMnS95J+hBvnT27HT S6JxGRmmYSCm+nWc3hVqaR0bMZL8BUNCh/oJ03tyG++Xuz/mvdR9fxUFY6TYFPW43VhQBgWxNY0 zTPvZb X-Received: by 2002:a05:620a:24c6:b0:775:9f94:1a7b with SMTP id m6-20020a05620a24c600b007759f941a7bmr7174637qkn.0.1696253048273; Mon, 02 Oct 2023 06:24:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG4CywEBej0YL5x0qPcH9365IyDYoIk74RO/IdJ/YBv5Q0U8u+oddx5oRZGvux5U825FJKzSw== X-Received: by 2002:a05:620a:24c6:b0:775:9f94:1a7b with SMTP id m6-20020a05620a24c600b007759f941a7bmr7174613qkn.0.1696253047877; Mon, 02 Oct 2023 06:24:07 -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 a14-20020a05620a124e00b0076cc4610d0asm8993664qkl.85.2023.10.02.06.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 06:24:07 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] elf: dl-load: Get rid of alloca usage. Date: Mon, 2 Oct 2023 09:24:01 -0400 Message-ID: <20231002132404.249890-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 sematics of is_trusted_path_normalize to return 1, 0, or -1 on error. --- elf/dl-load.c | 72 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 2923b1141d..c8e135b6e5 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); + + if (!scratch_buffer_set_array_size (&sbuf, 1, len + 2)) + return -1; - char *npath = (char *) alloca (len + 2); + char *npath = sbuf.data; char *wnp = npath; + while (*path != '\0') { if (path[0] == '/') @@ -172,12 +180,12 @@ 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; + return 1; trun += system_dirs_len[idx] + 1; } - return false; + return 0; } /* Given a substring starting at INPUT, just after the DST '$' start @@ -363,7 +371,7 @@ _dl_dst_substitute (struct link_map *l, const char *input, char *result) 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)) + && is_trusted_path_normalize (result, wp - result) != 1) { *result = '\0'; return result; @@ -951,6 +959,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 +992,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 +1009,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 +1041,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 +1052,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 +1085,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 +1504,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 +1537,7 @@ cannot enable executable stack as shared object requires"); _dl_audit_objopen (l, nsid); #endif + scratch_buffer_free (&sbuf); return l; } @@ -1598,6 +1621,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 +1685,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 +1722,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 +1761,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 +1783,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 +1804,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 +1812,7 @@ open_verify (const char *name, int fd, } + scratch_buffer_free (&sbuf); return fd; } @@ -1796,13 +1833,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 +1943,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 +1951,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 +1981,7 @@ open_path (const char *name, size_t namelen, int mode, sps->dirs = (void *) -1; } + scratch_buffer_free (&sbuf); return -1; }