From patchwork Fri Nov 24 17:01:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 80738 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 EE391385842F for ; Fri, 24 Nov 2023 17:02:09 +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 0400A3858C66 for ; Fri, 24 Nov 2023 17:01:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0400A3858C66 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 0400A3858C66 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=1700845316; cv=none; b=HCNXN1lWgakdXoEMSqKo5EYNwwY+9OuUcIIsH7gA5Dz1kAt+OQbjH+P3laKjUoSo/Ub+zKXnCciFVO2xwbbmCOJ/VzETa6IoI6Nc0WGsrkwWs4nDDHeVZWZbc5E0xUElpV8ub5EvSEHicwS55gJ5GpSjhMehv/DT39bVsreKm2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700845316; c=relaxed/simple; bh=9fvFUG8bO1AqgO5Cfqt0CksQ/2qfucQjnNJmeKZdKg0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QD//YrmES4KUwH7wQ4NRnMCu3+Xj5ZAO8z3P9tYCgOqDRHG2KzfkJf4P7Tf7vot2yL8U5VOp4kMKyz6IyCVQPz/OCp+loewRF91jx4VeDQUkC5h+xpbDEZX3RigbC7xBC3JbJYw8M5n3Lj/0I4L4PF4n3S/1F/aU2AIZl9+pajY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700845313; 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; bh=IccrnyBD09zBKEMvTp2RUDcspJQpeRTkil5nr8/Y0H0=; b=HFDB9pkmNUcNGVcrnAMqJIPejib9A+YEKHhTXmq/qYWqz6l/5hVP3N/Ww48Pebq2JJosha Rm30wRFHIIjkuZhwWZuMg4jo7CEbksT9o0TszSm5wz3iAFQC6QUaJg57o5WHNiACN4iMkL h/l9rrpsT0HuRzGCcixKJmY4WhcycmE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-jfXAu4YcO_qM6ezheMk7IQ-1; Fri, 24 Nov 2023 12:01:52 -0500 X-MC-Unique: jfXAu4YcO_qM6ezheMk7IQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3713C280CFAD for ; Fri, 24 Nov 2023 17:01:52 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4A9AD112130A; Fri, 24 Nov 2023 17:01:51 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Cc: Frederic Berat Subject: [PATCH] stdlib: Fix array bounds protection in insertion sort phase of qsort Date: Fri, 24 Nov 2023 18:01:49 +0100 Message-ID: <87edgfhypu.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 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, T_SCC_BODY_TEXT_LINE 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 The previous check did not do anything because tmp_ptr already points before run_ptr due to the way it is initialized. Fixes commit e4d8117b82065dc72e8df80097360e7c05a349b9 ("stdlib: Avoid another self-comparison in qsort"). Tested on i686-linux-gnu and x86_64-linux-gnu. Thanks, Florian Reviewed-by: Adhemerval Zanella --- stdlib/Makefile | 1 + stdlib/qsort.c | 2 +- stdlib/tst-qsort6.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) base-commit: 2e0c0ff95ca0e3122eb5b906ee26a31f284ce5ab diff --git a/stdlib/Makefile b/stdlib/Makefile index 6194d1cb22..0b154e57c5 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -216,6 +216,7 @@ tests := \ tst-qsort2 \ tst-qsort3 \ tst-qsort5 \ + tst-qsort6 \ tst-quick_exit \ tst-rand48 \ tst-rand48-2 \ diff --git a/stdlib/qsort.c b/stdlib/qsort.c index be01fb5598..62477010b6 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -238,7 +238,7 @@ insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, while ((run_ptr += size) <= end_ptr) { tmp_ptr = run_ptr - size; - while (run_ptr != tmp_ptr && cmp (run_ptr, tmp_ptr, arg) < 0) + while (tmp_ptr != base_ptr && cmp (run_ptr, tmp_ptr, arg) < 0) tmp_ptr -= size; tmp_ptr += size; diff --git a/stdlib/tst-qsort6.c b/stdlib/tst-qsort6.c new file mode 100644 index 0000000000..8ec0a6b633 --- /dev/null +++ b/stdlib/tst-qsort6.c @@ -0,0 +1,60 @@ +/* Test qsort with invalid comparison functions. + Copyright (C) 2023 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 + +/* Invalid comparison function that always returns -1. */ +static int +invalid_compare_1 (const void *a1, const void *b1) +{ + const int *a = a1; + const int *b = b1; + /* Check that the marker value matches, which means that we are + likely within the array. */ + TEST_COMPARE (*a, 842523635); + TEST_COMPARE (*b, 842523635); + TEST_VERIFY_EXIT (*a == 842523635); + TEST_VERIFY_EXIT (*b == 842523635); + return -1; +} + +/* Invalid comparison function that always returns 1. */ +static int +invalid_compare_2 (const void *a1, const void *b1) +{ + const int *a = a1; + const int *b = b1; + TEST_COMPARE (*a, 842523635); + TEST_COMPARE (*b, 842523635); + TEST_VERIFY_EXIT (*a == 842523635); + TEST_VERIFY_EXIT (*b == 842523635); + return 1; +} + +static int +do_test (void) +{ + int array[] = {842523635, 842523635, 842523635, 842523635, 842523635}; + qsort (array, array_length (array), sizeof (array[0]), invalid_compare_1); + qsort (array, array_length (array), sizeof (array[0]), invalid_compare_2); + return 0; +} + +#include