From patchwork Sun Jun 25 23:17:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 71646 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 BA7F338582A1 for ; Sun, 25 Jun 2023 23:18:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA7F338582A1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735107; bh=yzdaXeFvSjrJgCPFh/GrqdtHSmYnDwOtLKYNVwbUp6w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=LaPSsclxQSJRPmjH6YvSZjHsHi5UTuORufHcaB9zGhPuaYuJonV7p3EB7k0+GduVA E2SbbCMU8DMmJbVZQjXt0hbFoCz8u/Eu5bHFHzPahaA/7aIvkfILxxrl9gVSDueN5O 8uXlpAW8BGB28HjKgJaUsUeZoUhkkQdm7baWtfu8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by sourceware.org (Postfix) with ESMTPS id CBA933858D20 for ; Sun, 25 Jun 2023 23:18:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBA933858D20 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b699a2fe86so13200111fa.3 for ; Sun, 25 Jun 2023 16:18:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735081; x=1690327081; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yzdaXeFvSjrJgCPFh/GrqdtHSmYnDwOtLKYNVwbUp6w=; b=EErNkk23RZthgNTrT+i6BBXpBKmoNge5SsCYgwIcZeKgqUNm3huBRNmf2nz+Y3gJys mJ2zNPNAFukHKnSSieLOtgtJY/DDeNaXEx6slVo644l/QH6T9//DrtOYZPaayXUmyq7K u1VeZj5qJQkI0v3aqC8AuMjEqcIp62FPmDWK+qtUSIhN+seuClLED5Jmcu9VcGglu/VP D4JTkpk6w/53x0V4gIWBQjDuhRi3xCTqjeJ0W8AvuxxVnsRPg6F9MOLAGm8cIuZNYMsc xyRxuCEUwKhCwGRmh+1h0YAHljZjWScdNPIldsNK4ERLjajnrNdE4U4zNZ88wlXaCiaV BYdg== X-Gm-Message-State: AC+VfDyMnlQ9ssx6r7K3mX5y3zXmMyH5x/aWUxWRZKIP94LAuQMUAugG jqY1RHMgMVggPkjEPNXnF5R4zUtfpI8= X-Google-Smtp-Source: ACHHUZ7EBrNdTpLot4o3j4PeNwOKIgC9fDHhus0jerlYwItDGkArX55IVpnvMiNUUkjYV88DvWfhyQ== X-Received: by 2002:a19:6755:0:b0:4f8:58f4:b96e with SMTP id e21-20020a196755000000b004f858f4b96emr16291229lfj.37.1687735080881; Sun, 25 Jun 2023 16:18:00 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:00 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 1/5] htl: Let Mach place thread stacks Date: Mon, 26 Jun 2023 02:17:47 +0300 Message-ID: <20230625231751.404120-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Instead of trying to allocate a thread stack at a specific address, looping over the address space, just set the ANYWHERE flag in vm_allocate (). The previous behavior: - defeats ASLR (for Mach versions that support ASLR), - is particularly slow if the lower 4 GB of the address space are mapped inaccessible, as we're planning to do on 64-bit Hurd, - is just silly. Signed-off-by: Sergey Bugaev --- sysdeps/mach/htl/pt-stack-alloc.c | 35 ++++++------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/sysdeps/mach/htl/pt-stack-alloc.c b/sysdeps/mach/htl/pt-stack-alloc.c index 429ac2d9..97e6b445 100644 --- a/sysdeps/mach/htl/pt-stack-alloc.c +++ b/sysdeps/mach/htl/pt-stack-alloc.c @@ -19,14 +19,9 @@ #include #include -#include #include -/* The next address to use for stack allocation. */ -static vm_address_t next_stack_base = VM_MIN_ADDRESS; - - /* Allocate a new stack of size STACKSIZE. If successful, store the address of the newly allocated stack in *STACKADDR and return 0. Otherwise return an error code (EINVAL for an invalid stack size, @@ -35,30 +30,12 @@ static vm_address_t next_stack_base = VM_MIN_ADDRESS; int __pthread_stack_alloc (void **stackaddr, size_t stacksize) { - vm_offset_t base; - int i = 0; - -get_stack: - i++; - for (base = next_stack_base; - base < VM_MAX_ADDRESS - && __vm_allocate (__mach_task_self (), &base, - stacksize, FALSE) != KERN_SUCCESS; base += stacksize) - ; - - if (base >= VM_MAX_ADDRESS) - { - if (i == 1) - { - next_stack_base = VM_MIN_ADDRESS; - goto get_stack; - } - else - return EAGAIN; - } + error_t err; - next_stack_base = base + stacksize; + err = __vm_allocate (__mach_task_self (), (vm_offset_t *) stackaddr, + stacksize, TRUE); - (*stackaddr) = (void *) base; - return 0; + if (err == KERN_NO_SPACE) + err = EAGAIN; + return err; } From patchwork Sun Jun 25 23:17:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 71645 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 1168B385703A for ; Sun, 25 Jun 2023 23:18:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1168B385703A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735108; bh=PhTZWH/Z1zQBT9/VMufWC+SklY+iUOjvbdzbh09F2Sk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ZgsG1RT9UD8Dr6rE2R/ZL2zL1qw/IzPeNo0DByAddYYcbHrXEdWoqfUWSYtyKwl0e NvgnKdSSYR3QdTPj3tDDpeqNAA8fn2t3veC9ucw6wCrv/2dr8BV4pHpqpH2Cq9+Afw rMS6L2QSHj+R8TFpSEQX3sg9VFm89Qp5Ejqbv14A= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id D296F3858D33 for ; Sun, 25 Jun 2023 23:18:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D296F3858D33 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2b69a48368fso12135401fa.0 for ; Sun, 25 Jun 2023 16:18:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735082; x=1690327082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PhTZWH/Z1zQBT9/VMufWC+SklY+iUOjvbdzbh09F2Sk=; b=BmPd+VDS+jCga+UtOuL1KqVIfkYpHcL0CmTyez41UgS1yGVyYiYRmwgQsdBrOBUwxI faeBUO/UzXGpS23f7F89CLUGuIkZhBqJv6ciRdTSH0hAaFdU4LQA1/X38UXo60UNqN1l l/Jq01ofqXcyC7ScFETlCC9lcCyNR8PWb20X4Z72CBWGXRTwiqLkMLD7Lc8nUGVk+hwG DpTIp6Z0dgLxveHNxLuI35+EVnoWSUijg0GsWeAYeqhqqI6fXe9MKybPNbqiWyE9KY3e 7HRUvOa9xWAJLFrwmPeLWOF49OjTiFVGbO2SO8ZiY/sI+KdjZK6Eu/wWGFaPewg5DZKR qdKw== X-Gm-Message-State: AC+VfDznFiRdnn/ioJ43ghfrChb9q9KeaHj8KClD0WrBrpNdtXj1crF4 TaDFX3JPq0WVzEjKHL/O1YnDehzC5yA= X-Google-Smtp-Source: ACHHUZ7Z5DsorQ41DEXczQtm5M572k3e1ng66m4Nq0eTVk9nFMkjJvzlBJB5+I0MsV1jmQqtr2iVnw== X-Received: by 2002:a05:6512:1584:b0:4f8:710c:e2c7 with SMTP id bp4-20020a056512158400b004f8710ce2c7mr9032004lfb.33.1687735081789; Sun, 25 Jun 2023 16:18:01 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:01 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 2/5] hurd: Map brk non-executable Date: Mon, 26 Jun 2023 02:17:48 +0300 Message-ID: <20230625231751.404120-2-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230625231751.404120-1-bugaevc@gmail.com> References: <20230625231751.404120-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The rest of the heap (backed by individual pages) is already mapped RW. Mapping these pages RWX presents a security hazard. Also, in another branch memory gets allocated using vm_allocate, which sets memory protection to VM_PROT_DEFAULT (which is RW). The mismatch between protections prevents Mach from coalescing the VM map entries. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/brk.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sysdeps/mach/hurd/brk.c b/sysdeps/mach/hurd/brk.c index f1349495..3a335194 100644 --- a/sysdeps/mach/hurd/brk.c +++ b/sysdeps/mach/hurd/brk.c @@ -106,7 +106,7 @@ _hurd_set_brk (vm_address_t addr) /* First finish allocation. */ err = __vm_protect (__mach_task_self (), pagebrk, alloc_start - pagebrk, 0, - VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); + VM_PROT_READ|VM_PROT_WRITE); if (! err) _hurd_brk = alloc_start; @@ -120,7 +120,7 @@ _hurd_set_brk (vm_address_t addr) else /* Make the memory accessible. */ err = __vm_protect (__mach_task_self (), pagebrk, pagend - pagebrk, - 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); + 0, VM_PROT_READ|VM_PROT_WRITE); if (err) return __hurd_fail (err); From patchwork Sun Jun 25 23:17:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 71649 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 6A1493858401 for ; Sun, 25 Jun 2023 23:19:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A1493858401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735150; bh=GLUIFJainxaJuSLOPaLTazcTDpF6gsL3kqEWAFrsW6E=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=xgGUh9bxd9Op9A38z9wWAGtn2HjqkBo3cb15PSMWzPMDiB3cIvOemsz3A8JY+HQXo cyHmU0CIU8Iy/XjASWzwenq1gmMvwXf5jordiTGmwjEesGhgvOAGpTaezuyXQN/QnU GRBh4URr3m51QMMaQS0RYD66YxHId3G+aAPWxnOQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 5802A3858D35 for ; Sun, 25 Jun 2023 23:18:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5802A3858D35 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4fb73ba3b5dso589424e87.1 for ; Sun, 25 Jun 2023 16:18:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735082; x=1690327082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GLUIFJainxaJuSLOPaLTazcTDpF6gsL3kqEWAFrsW6E=; b=FzI7cbvRvh9sU0Nf8KvZ/oPgbRtC4gQJkFLcbswCk94yeEVA1rmv8/+Kg5oi0aypHY T0pObyNaSu0SuknFFoVoqXtr3E7EbrlfK2adV/Qy08uLQkh0rTqRN6Vu7RS8f8d15Hmq O/egf4AKzC1xCevks+qXexgksJnZInU5bqBebDio+8/0hOi2tlmMIKQFef1MGJ+FnB8Z 7mKZIKqqyxdluBND2fwhT0gBWD/blOCdV6KFVqHh67rykN+2DACT/4IsD1+ujz40ON7O b7qGKoJ/7Qu2Q7XvrMH0xGBBp4GSAF+ItU7r5v/UUakYtVgmYjW2MXa3GIvhElpA5Ckq TZWQ== X-Gm-Message-State: AC+VfDzvQ1XUAWEuoTlH4e6ajEAdk0QZZI9ujEFl8hA3FcwME0QxFhlZ rtxxYnG8APVzt9NMNC0IHjuZ+wptzrE= X-Google-Smtp-Source: ACHHUZ6zjK81ZgBe2YU8VQK++5l1qZTxYLtco40dKq+FdWUxUfnJpq/CdvCRiZ7uNoOHrke3TqTxeg== X-Received: by 2002:a19:ca4e:0:b0:4f8:8be4:8a82 with SMTP id h14-20020a19ca4e000000b004f88be48a82mr9353650lfj.22.1687735082587; Sun, 25 Jun 2023 16:18:02 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:02 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 3/5] hurd: Fix calling vm_deallocate (NULL) Date: Mon, 26 Jun 2023 02:17:49 +0300 Message-ID: <20230625231751.404120-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230625231751.404120-1-bugaevc@gmail.com> References: <20230625231751.404120-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Only call vm_deallocate when we do have the old buffer, and check for unexpected errors. Spotted while debugging a msgids/readdir issue on x86_64-gnu. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/readdir64.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sysdeps/mach/hurd/readdir64.c b/sysdeps/mach/hurd/readdir64.c index 2c01ca22..2d946793 100644 --- a/sysdeps/mach/hurd/readdir64.c +++ b/sysdeps/mach/hurd/readdir64.c @@ -64,9 +64,13 @@ __readdir64 (DIR *dirp) /* The data was passed out of line, so our old buffer is no longer useful. Deallocate the old buffer and reset our information for the new buffer. */ - __vm_deallocate (__mach_task_self (), - (vm_address_t) dirp->__data, - dirp->__allocation); + if (dirp->__data != NULL) + { + err = __vm_deallocate (__mach_task_self (), + (vm_address_t) dirp->__data, + dirp->__allocation); + assert_perror (err); + } dirp->__data = data; dirp->__allocation = round_page (dirp->__size); } From patchwork Sun Jun 25 23:17:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 71647 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 879B33857727 for ; Sun, 25 Jun 2023 23:18:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 879B33857727 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735115; bh=NIOyXxLgUu6KWSFKDLh3q6MQA96VHsKXKsCRDdInXCI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ofM6NA2xqeQ4U/9Gjqn/uXt4UBNGCRpNiaIT9SdMTUeszNq8UR24gBNxzSkuTI61y l1hR33isznmil3V2QpmQgPfncNOhao6TjI5iDtKX/TVyTNC1rr8z+QOL+L6YgpZ2o/ EfYQhhX7U50nEy9vi3fZ4tfhX0QMFrP0gTGZZPZA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by sourceware.org (Postfix) with ESMTPS id 3CBBC3858D37 for ; Sun, 25 Jun 2023 23:18:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3CBBC3858D37 Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-4f766777605so3268413e87.1 for ; Sun, 25 Jun 2023 16:18:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735083; x=1690327083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NIOyXxLgUu6KWSFKDLh3q6MQA96VHsKXKsCRDdInXCI=; b=SB5dbEGdv9Ho6vcd81XzWsrgql2LVMdnYu8RpuAUO7ijSZm6L9yK0B1OfTAghNW168 xJ9wqQVn8pVfym7X4ME+Tm4cLNnNqZ4uj9qOVoEvPn8Hf3d/koQAAIDwM0HZSsnXvLIw LhpkHmstvU8ICPpuDx9MZIWVrMr9pUBYAb4xFdRujVrziwR4d4jWW/AnhdnZxib+pJal fzuIO1cajqzoyB3TkhTeo5aRpCuqQBBn/Umj2VjIVY8yV6brK37G7dIA8cRXVnODeE2M iUv3ZN6nmGMTCx39h7uaxGD2Du/lSWzFh6NWqB3Ufc0hvheJsAL8uYHF/AQpj+6jddIn 3Vwg== X-Gm-Message-State: AC+VfDyeNAzeBk1jBgj4lRhdNlzz3YuahGiKNsxzBj1S/Gygr+pR7eY3 9n9+QbjiIj1Nl7oXx/tv2Il11EQUIDM= X-Google-Smtp-Source: ACHHUZ7KFPRXw2LjnM5vso5GKAIj/SvtAYARoMyBq7tUBkIPjsZ0Dju/Np0FBifcSGx2NTHSC6uAPA== X-Received: by 2002:a19:3813:0:b0:4f8:7697:5207 with SMTP id f19-20020a193813000000b004f876975207mr10545491lfa.23.1687735083357; Sun, 25 Jun 2023 16:18:03 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:02 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 4/5] hurd: Fix mapping at address 0 with MAP_FIXED Date: Mon, 26 Jun 2023 02:17:50 +0300 Message-ID: <20230625231751.404120-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230625231751.404120-1-bugaevc@gmail.com> References: <20230625231751.404120-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Zero address passed to mmap () typically means the caller doesn't have any specific preferred address. Not so if MAP_FIXED is passed: in this case 0 means literal 0. Fix this case to pass anywhere = 0 into vm_map. Also add some documentation. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/mmap.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c index 5aa70083..33672cf6 100644 --- a/sysdeps/mach/hurd/mmap.c +++ b/sysdeps/mach/hurd/mmap.c @@ -38,7 +38,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) vm_prot_t vmprot, max_vmprot; memory_object_t memobj; vm_address_t mapaddr, mask; - boolean_t copy; + boolean_t copy, anywhere; mapaddr = (vm_address_t) addr; @@ -55,6 +55,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) vmprot |= VM_PROT_EXECUTE; copy = ! (flags & MAP_SHARED); + anywhere = ! (flags & MAP_FIXED); #ifdef __LP64__ if ((addr == NULL) && (prot & PROT_EXEC) @@ -141,9 +142,12 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) if (copy) max_vmprot = VM_PROT_ALL; + /* When ANYWHERE is true but the caller has provided a preferred address, + try mapping at that address with anywhere = 0 first. If this fails, + we'll retry with anywhere = 1 below. */ err = __vm_map (__mach_task_self (), &mapaddr, (vm_size_t) len, mask, - mapaddr == 0, + anywhere && (mapaddr == 0), memobj, (vm_offset_t) offset, copy, vmprot, max_vmprot, copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE); @@ -165,7 +169,10 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) } else { + /* This mmap call is allowed to allocate anywhere, */ if (mapaddr != 0 && (err == KERN_NO_SPACE || err == KERN_INVALID_ADDRESS)) + /* ...but above, we tried allocating at the specific address, + and failed to. Now try again, with anywhere = 1 this time. */ err = __vm_map (__mach_task_self (), &mapaddr, (vm_size_t) len, mask, 1, memobj, (vm_offset_t) offset, From patchwork Sun Jun 25 23:17:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 71648 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 C5421385662B for ; Sun, 25 Jun 2023 23:18:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5421385662B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735120; bh=ihugqOu6QpcBxCGXfh6PRaq5n1Q1I0Bl0P/lHPY0K48=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=a0EufvZJhzczI9MZKGjULhA9M+H4W5kjKv0MVE69QIc75cD0eW8G3UFeFoLYiSfU9 hcbJ4zlWk1McsZCkpy58IgxgV/UkiZO6BqLneja/sY2abEoxp/fmNNZi6/ScmfreYH 8hARSkZNuA902F0oDE6AU6R4x9WzWm6UcBoO5SVE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 3833C3858D39 for ; Sun, 25 Jun 2023 23:18:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3833C3858D39 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4f973035d60so3034921e87.3 for ; Sun, 25 Jun 2023 16:18:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735084; x=1690327084; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ihugqOu6QpcBxCGXfh6PRaq5n1Q1I0Bl0P/lHPY0K48=; b=L8TJVBEK4fx97oIoOJazV0n6uf1W2elkIr8jamIvVQsGtpC8O7YrS5lWa+l+3vnOmY Mvmy57akGmUWOuwpZfb+/ChBLhmyvOxo8P5FOEv4dfWgb7kvtFuh8EYyEwNOJ+uKMahC uvY7JyfUhJL2W8hsDJP7ZvbYDLSNHnKCxhNe5Yqdo/UfxveRjJs/+CLh110lzUNo6Pe7 YKM/v/91hNEyGY007CUF3nmkxh49/7m0VHdY2f4O/Wfas25ygEqRFpKQH9S2bp5B/pNJ wfecPInLfYBcObecclOqZb/kfcHYH+bKsk9m6AAYWhIe5C3+t4xQo7IIBYfn4Qi0bZ7T v7DA== X-Gm-Message-State: AC+VfDwNIAB6Xh5OZhzBE+TF8Kf6/WurIIrn+8OGsHcmUNhXIvzVp5F4 TEjl2uoao2XyloYBHbl8+gRVRNfbAYc= X-Google-Smtp-Source: ACHHUZ5RYI/2rTAIFqg4vPA/vz1XQhecR1s7hNa1IRMgAGt11YzX+HRxqv7SUQX3KBMAa2Cqnc6Wrg== X-Received: by 2002:a05:6512:b94:b0:4f9:72a5:2b76 with SMTP id b20-20020a0565120b9400b004f972a52b76mr4127841lfv.65.1687735084303; Sun, 25 Jun 2023 16:18:04 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:03 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 5/5] hurd: Implement MAP_EXCL Date: Mon, 26 Jun 2023 02:17:51 +0300 Message-ID: <20230625231751.404120-5-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230625231751.404120-1-bugaevc@gmail.com> References: <20230625231751.404120-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" MAP_FIXED is defined to silently replace any existing mappings at the address range being mapped over. This, however, is a dangerous, and only rarely desired behavior. Various Unix systems provide replacements or additions to MAP_FIXED: * SerenityOS and Linux provide MAP_FIXED_NOREPLACE. If the address space already contains a mapping in the requested range, Linux returns EEXIST. SerenityOS returns ENOMEM, however that is a bug, as the MAP_FIXED_NOREPLACE implementation is intended to be compatible with Linux. * FreeBSD provides the MAP_EXCL flag that has to be used in combination with MAP_FIXED. It returns EINVAL if the requested range already contains existing mappings. This is directly analogous to the O_EXCL flag in the open () call. * DragonFly BSD, NetBSD, and OpenBSD provide MAP_TRYFIXED, but with different semantics. DragonFly BSD returns ENOMEM if the requested range already contains existing mappings. NetBSD does not return an error, but instead creates the mapping at a different address if the requested range contains mappings. OpenBSD behaves the same, but also notes that this is the default behavior even without MAP_TRYFIXED (which is the case on the Hurd too). Since the Hurd leans closer to the BSD side, add MAP_EXCL as the primary API to request the behavior of not replacing existing mappings. Declare MAP_FIXED_NOREPLACE and MAP_TRYFIXED as aliases of (MAP_FIXED|MAP_EXCL), so any existing software that checks for either of those macros will pick them up automatically. For compatibility with Linux, return EEXIST if a mapping already exists. Finally, a fun bit of horrifying trivia: until very recently, there has been a function named try_mmap_fixed () in the Wine project. On Darwin, it used mach_vm_map () to map the (anonymous) pages without replacing any existing mappings. On Solaris and NetBSD, it instead paused the other threads in the process using vfork (), then used mincore () to probe the pages in the desired address range for being already mapped -- an error return from mincore () indicates that the page is not mapped at all. Finally, if no conflicting mappings were found, still in the vforked child process it performed the mapping with MAP_FIXED, and then returned from the child back into the parent, resuming the other threads. This relied on: * being able to do calls other than execve and _exit from the vforked child, which is undefined behavior according to POSIX; * the parent and the child sharing the address space, and changes made in the child being visible in the parent; * vfork suspending all the threads of the calling process, not just the calling thread. All of this is undefined according to POSIX, but was apparently true on Solaris, which is the system this function was originally implemented for. But on NetBSD, where this shim was later ported to, the last bullet point does not hold: vfork only suspends the calling thread; while the other threads continue to run. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/bits/mman_ext.h | 7 ++++++- sysdeps/mach/hurd/mmap.c | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/sysdeps/mach/hurd/bits/mman_ext.h b/sysdeps/mach/hurd/bits/mman_ext.h index bbb94743..9658cdd6 100644 --- a/sysdeps/mach/hurd/bits/mman_ext.h +++ b/sysdeps/mach/hurd/bits/mman_ext.h @@ -22,5 +22,10 @@ #ifdef __USE_GNU # define SHM_ANON ((const char *) 1) -# define MAP_32BIT 0x1000 + +# define MAP_32BIT 0x1000 /* Map in the lower 2 GB. */ +# define MAP_EXCL 0x4000 /* With MAP_FIXED, don't replace existing mappings. */ + +# define MAP_TRYFIXED (MAP_FIXED | MAP_EXCL) /* BSD name. */ +# define MAP_FIXED_NOREPLACE (MAP_FIXED | MAP_EXCL) /* Linux name. */ #endif /* __USE_GNU */ diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c index 33672cf6..20264a77 100644 --- a/sysdeps/mach/hurd/mmap.c +++ b/sysdeps/mach/hurd/mmap.c @@ -46,6 +46,9 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) if ((mapaddr & (__vm_page_size - 1)) || (offset & (__vm_page_size - 1))) return (void *) (long int) __hurd_fail (EINVAL); + if ((flags & MAP_EXCL) && ! (flags & MAP_FIXED)) + return (void *) (long int) __hurd_fail (EINVAL); + vmprot = VM_PROT_NONE; if (prot & PROT_READ) vmprot |= VM_PROT_READ; @@ -156,15 +159,20 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) { if (err == KERN_NO_SPACE) { - /* XXX this is not atomic as it is in unix! */ - /* The region is already allocated; deallocate it first. */ - err = __vm_deallocate (__mach_task_self (), mapaddr, len); - if (! err) - err = __vm_map (__mach_task_self (), - &mapaddr, (vm_size_t) len, mask, - 0, memobj, (vm_offset_t) offset, - copy, vmprot, max_vmprot, - copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE); + if (flags & MAP_EXCL) + err = EEXIST; + else + { + /* The region is already allocated; deallocate it first. */ + /* XXX this is not atomic as it is in unix! */ + err = __vm_deallocate (__mach_task_self (), mapaddr, len); + if (! err) + err = __vm_map (__mach_task_self (), + &mapaddr, (vm_size_t) len, mask, + 0, memobj, (vm_offset_t) offset, + copy, vmprot, max_vmprot, + copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE); + } } } else