From patchwork Sat Aug 17 00:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A57B7C52D7D for ; Sat, 17 Aug 2024 00:18:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BAF66B03D6; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 375856B03D7; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E6CD6B03D8; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E3E2D6B03D6 for ; Fri, 16 Aug 2024 20:18:51 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A15F81A03FF for ; Sat, 17 Aug 2024 00:18:51 +0000 (UTC) X-FDA: 82459826862.07.2960254 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf13.hostedemail.com (Postfix) with ESMTP id C923520003 for ; Sat, 17 Aug 2024 00:18:49 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iIGSQDCd; spf=pass (imf13.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723853893; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=ejXn6j6eMLotvfqtJv4uDf5agaArO4szd4cy99xIoWlIcxwE0/Y+Ql55I3WD+aJSmps+hL wnXAoJ/ZqrBexg17GF0l1VQDMPCo60xlrteB0Akpedw/AjK9n82K/h2IkcZ9URIuznYUgg 6Z0jfh2Y4Kx6zR7NpIRxZWnZN22jbuA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iIGSQDCd; spf=pass (imf13.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853893; a=rsa-sha256; cv=none; b=jCvflqmDpEAhBBgoCfNuTYZzNsk26sfHR1Gr8dzek/nZKcHt7AMchA84H6DK1+ldwwY1VF FVGjizJ5rXEQNPwZYz4R9eNrGLH5kQEQU9P2Zu/jdB4kNPIetGulMhRN7CCkRa5iEwPUBz b3sO6e6Q1boRAIqrt6drbezI4htYVYo= Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4280bca3960so19732995e9.3 for ; Fri, 16 Aug 2024 17:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853928; x=1724458728; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=iIGSQDCdkXkSfjEuKjLnp2MqBpMtf+fJlLc78vvyLLsglVTk/fhBdWlOSsOpb0M/fj VY2WuUckflpyP4fSqWqYO0xU9zCzWvAVdczrbjxLgT75mz7lhQTIQHhYxTytRR0s6uyt gCDB/Ad4aH9YpTy9EbXMjOzLFV8kgbi7Z+Dg4iYSkq0mJTj7klDAw8pceL/ZGBqleAFU stPH4qd7OhmqKj4yLywyWgU3MLx6+DPuOFW1EDfxQ7zHQruDbYqCmH0qwL9BXdf1eM5U cDD4EycKiB2m6cyFEkFPdI/QB7gKpDgFdMMgXIhNtOem8+kuSl/PdQSThe94Ky3uhxTD iWmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853928; x=1724458728; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=nfVifn3Zy62A1tX6tGMfMx3ysvmswZB8iH538V1yM4PvghHl8qyGkw5liB0h5c9L5G PQN2c++vC6OnlpoyYnsNFtdbjZZIyAwawhOaNKtpGEbB3JK5rBfnV12hHH/TMiL40JP4 2mR2VzT8TA6CIUnqwhR251b3Ur2SEkGN5pwCbQWTE/NXrgl3zJjj6yCrFalZnxlfQGUA kIpfj/B3F5BJu06QRP3p3aMsMQO9SWBkwNM+W+KkVW3SSltxIkWJSVmJj4F9AqHxrXNw 2S2EB68dtaEt6pZxtH+s0znK+XUhpP0pjD3f/DpGh2+uk4QD5ocyqbEjO1AFy1GnZeLW TcRA== X-Gm-Message-State: AOJu0Yx6CEMryBYsIcvP+uOD6loNneQwYfuccr5KgNIioTfhq12QUdaj C89fVtB0D28oSrxQnGrDwA3PsLKt97HO/j7VZ1buKFKhmq5RvCXb X-Google-Smtp-Source: AGHT+IFIhkDMqUt+pj5IYnY7Cri9JLM7/ke3nV3b+/GsdZDIlIl1wzdvrsc5f4Owwa72QzEPcfRwGQ== X-Received: by 2002:a05:600c:45ca:b0:426:5ddf:fd22 with SMTP id 5b1f17b1804b1-429ed78572bmr30664255e9.6.1723853927337; Fri, 16 Aug 2024 17:18:47 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:46 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:34 +0100 Subject: [PATCH v3 7/7] selftests/mm: add more mseal traversal tests MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-7-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: C923520003 X-Stat-Signature: gfj1kpw5megascqofa35gxob6ncxou6y X-HE-Tag: 1723853929-909501 X-HE-Meta: U2FsdGVkX1/UC3na4Puflsk/joc1k49PAuANOUcuTk45G7o0WPNLFKQ5eci38p+UBys/NrCiKRdEGaPLKkqF04yDmM5sQq1v0Y04/bvmEtRS2/tE2rQ0ZHEk2Cgb7scIrjvu9eEN3gdG4dgVvZL9ipzJ6CwyKcUuNbjKUCsqchrv3TpIybxUtWTVYzqN9rvZhL580v1QP+fI01mz6l/9b05455BhU7+67QbMb3uv3UDDxmQ9v0tkOP6l0N2vcZjv6hBLE+IMFieAn0HTg7T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C1PzFesCDYEafpWB4PHx1/aLO4vePSAGrKXQ2zJBwBpEWQx5jBvIAubIWH8xmfxvoSqa1v2FWMsRs08UTQoVFRiyPUbj0cQDRWJX2iTotY/NAGaUbjt/rRJ58Y6o68sbnpX3E6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000131, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add more mseal traversal tests across VMAs, where we could possibly screw up sealing checks. These test more across-vma traversal for mprotect, munmap and madvise. Particularly, we test for the case where a regular VMA is followed by a sealed VMA. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes Signed-off-by: Pedro Falcato --- tools/testing/selftests/mm/mseal_test.c | 111 +++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 259bef4945e9..0d4d40fb0f88 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -766,6 +766,42 @@ static void test_seal_mprotect_partial_mprotect(bool seal) REPORT_TEST_PASS(); } +static void test_seal_mprotect_partial_mprotect_tail(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + int prot; + + /* + * Check if a partial mseal (that results in two vmas) works correctly. + * It might mprotect the first, but it'll never touch the second (msealed) vma. + */ + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = sys_mseal(ptr + page_size, size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_mprotect(ptr, size, PROT_EXEC); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + FAIL_TEST_IF_FALSE(prot == 0x4); + } + + REPORT_TEST_PASS(); +} + + static void test_seal_mprotect_two_vma_with_gap(bool seal) { void *ptr; @@ -983,6 +1019,41 @@ static void test_seal_munmap_vma_with_gap(bool seal) REPORT_TEST_PASS(); } +static void test_seal_munmap_partial_across_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + int prot; + + /* + * Check if a partial mseal (that results in two vmas) works correctly. + * It might unmap the first, but it'll never unmap the second (msealed) vma. + */ + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = sys_mseal(ptr + page_size, size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + FAIL_TEST_IF_FALSE(prot == 0x4); + } + + REPORT_TEST_PASS(); +} + static void test_munmap_start_freed(bool seal) { void *ptr; @@ -1735,6 +1806,37 @@ static void test_seal_discard_ro_anon(bool seal) REPORT_TEST_PASS(); } +static void test_seal_discard_across_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = seal_single_address(ptr + page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_madvise(ptr, size, MADV_DONTNEED); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + REPORT_TEST_PASS(); +} + + static void test_seal_madvise_nodiscard(bool seal) { void *ptr; @@ -1779,7 +1881,7 @@ int main(int argc, char **argv) if (!pkey_supported()) ksft_print_msg("PKEY not supported\n"); - ksft_set_plan(82); + ksft_set_plan(88); test_seal_addseal(); test_seal_unmapped_start(); @@ -1825,12 +1927,17 @@ int main(int argc, char **argv) test_seal_mprotect_split(false); test_seal_mprotect_split(true); + test_seal_mprotect_partial_mprotect_tail(false); + test_seal_mprotect_partial_mprotect_tail(true); + test_seal_munmap(false); test_seal_munmap(true); test_seal_munmap_two_vma(false); test_seal_munmap_two_vma(true); test_seal_munmap_vma_with_gap(false); test_seal_munmap_vma_with_gap(true); + test_seal_munmap_partial_across_vmas(false); + test_seal_munmap_partial_across_vmas(true); test_munmap_start_freed(false); test_munmap_start_freed(true); @@ -1862,6 +1969,8 @@ int main(int argc, char **argv) test_seal_madvise_nodiscard(true); test_seal_discard_ro_anon(false); test_seal_discard_ro_anon(true); + test_seal_discard_across_vmas(false); + test_seal_discard_across_vmas(true); test_seal_discard_ro_anon_on_rw(false); test_seal_discard_ro_anon_on_rw(true); test_seal_discard_ro_anon_on_shared(false);