From patchwork Tue Sep 19 18: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: 76409 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 378DA3856956 for ; Tue, 19 Sep 2023 18:24:44 +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 5ADED3858D20 for ; Tue, 19 Sep 2023 18:24:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5ADED3858D20 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=1695147851; 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=8ghiMfpcknV2FIJlXnjWWBxurM9jSVp2s50aMijaNiA=; b=Ccyfz8TDKXEv7MAl6PCS7MMzwEjQ2KHDP1pdOYoiBcT1cXEwbNJo/WfO/PrisFG0Z+poMt u6lWARDJ4xLlv8DOmHZj3QJiSIHL3fM1qT+SPur/+wCXUY2VYgkvc8wQ1PiBTvdio9f2GL Ty1k/tZYkPwow0NRE734YiR1glps/KU= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-227-WK3aYC2_N5O6DNNXuX1-jg-1; Tue, 19 Sep 2023 14:24:10 -0400 X-MC-Unique: WK3aYC2_N5O6DNNXuX1-jg-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-773aeb22094so553073985a.1 for ; Tue, 19 Sep 2023 11:24:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695147850; x=1695752650; 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=8ghiMfpcknV2FIJlXnjWWBxurM9jSVp2s50aMijaNiA=; b=P6f4uAlxC+334Zz1IGzh/09coB8Q1INbggAf6/4XRnsbYThbzAgwaejxQZbn7vaaEY tuwMAGzWqAQenEBZQOH7oTbcAaX63voqFTv4gW27U8EbzwFgVtlUARaMm2x9jsySRbby H2tqBe0tJJJpVAXxJEjx/4xw+uECHVRHt+1Unp1BijxJFliq1Z7cDgPhRspiuPKk42Io G3FSyT8XxR/jdiaKiCaFZ5oeA7sYpDqJ9d5DmuEgaIqrh09MRApFrexRGz/zza7e6LEJ SklZIE4aXcWFQazdrGzXQRHILonNAAtEbXRakdgeEFkc51hlXyFzD/ZBrJXMiaWCiQlJ 9w2A== X-Gm-Message-State: AOJu0YwkprKiwl9NClp8D7/75mSOyzWiHA9UWWladqs4cwQ+aqzGIrB3 7/LFb5FmY0lpIpGGNzhWdHtWMTcpEQ2SKJUyOJPiPtuGwexo6QOMrGaExgino9r3wIA+DSAoG+/ QTQygvkCyoE+xmEMHJJn6AtS2dHFgoIX3qrcqQ99GMjpJ08Hlttohf3HK14pYmn8IACZCsxTZD3 zw5OmJ X-Received: by 2002:a05:620a:2887:b0:76d:ba72:689d with SMTP id j7-20020a05620a288700b0076dba72689dmr560365qkp.30.1695147849861; Tue, 19 Sep 2023 11:24:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHOg2ofrjNRkKMmMOs2yFPyp/8gUbd+GmR3CZjeSV+Qzbc73XgGZHKJWyMvqDbxrnyZRske0w== X-Received: by 2002:a05:620a:2887:b0:76d:ba72:689d with SMTP id j7-20020a05620a288700b0076dba72689dmr560345qkp.30.1695147849433; Tue, 19 Sep 2023 11:24:09 -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 op29-20020a05620a535d00b0076daaccb7f7sm4131181qkn.135.2023.09.19.11.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 11:24:08 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] elf: Remove alloca usage from chroot_canon. Date: Tue, 19 Sep 2023 14:24:01 -0400 Message-ID: <20230919182404.727450-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, KAM_SHORT, 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 with scratch_buffers to prevent potential stack overflow. --- elf/chroot_canon.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/elf/chroot_canon.c b/elf/chroot_canon.c index 63a1ae6dbb..a03a2998d2 100644 --- a/elf/chroot_canon.c +++ b/elf/chroot_canon.c @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, see . */ +#include #include #include #include @@ -49,6 +50,10 @@ chroot_canon (const char *chroot, const char *name) const char *rpath_limit; int num_links = 0; size_t chroot_len = strlen (chroot); + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); + struct scratch_buffer extra_sbuf; + scratch_buffer_init (&extra_sbuf); if (chroot_len < 1) { @@ -123,7 +128,9 @@ chroot_canon (const char *chroot, const char *name) if (S_ISLNK (st.st_mode)) { - char *buf = alloca (PATH_MAX); + if (!scratch_buffer_set_array_size (&sbuf, 1, PATH_MAX)) + goto error; + char *buf = sbuf.data; size_t len; if (++num_links > __eloop_threshold ()) @@ -142,7 +149,11 @@ chroot_canon (const char *chroot, const char *name) buf[n] = '\0'; if (!extra_buf) - extra_buf = alloca (PATH_MAX); + { + if (!scratch_buffer_set_array_size (&extra_sbuf, 1, PATH_MAX)) + goto error; + extra_buf = extra_sbuf.data; + } len = strlen (end); if (len >= PATH_MAX - n) @@ -168,10 +179,14 @@ chroot_canon (const char *chroot, const char *name) if (dest > rpath_root + 1 && dest[-1] == '/') --dest; *dest = '\0'; + scratch_buffer_free (&sbuf); + scratch_buffer_free (&extra_sbuf); return rpath; error: free (rpath); + scratch_buffer_free (&sbuf); + scratch_buffer_free (&extra_sbuf); return NULL; }