From patchwork Thu Aug 29 21:43:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13783937 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 400A8CA0EC0 for ; Thu, 29 Aug 2024 21:44:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6693E6B008C; Thu, 29 Aug 2024 17:44:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 618EC6B0092; Thu, 29 Aug 2024 17:44:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B9D76B0093; Thu, 29 Aug 2024 17:44:14 -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 249306B008C for ; Thu, 29 Aug 2024 17:44:14 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B9462C0C45 for ; Thu, 29 Aug 2024 21:44:13 +0000 (UTC) X-FDA: 82506611586.14.EBE180A Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by imf21.hostedemail.com (Postfix) with ESMTP id E3F5B1C000E for ; Thu, 29 Aug 2024 21:44:11 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Q21PzM2c; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.167.171 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724967806; a=rsa-sha256; cv=none; b=s/e66g8xGXbsTOY1SMyHq65iSOcjXOQkTaRQQxqDwNfhsE36guzRSTRfAMisM2Cl80anW5 Bdc+jzzeBkWPVtaIAPySRzcsycLuO4PIAH8BDqWFrsTHJAAYq/D/YnicixmeanUJMKzg+9 Nk4ykc/a3by/ygfKzP6xSaFnY2m/T/8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Q21PzM2c; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.167.171 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724967806; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hAPF+mFWkE9aMPYn9qrkTRHhUapPOCqVhxefhrunDUc=; b=hGKVmp/ZbasmnJnfC9gcbttQ6y8A7R/UIYPuPV1tRGGN7DMxnHZ211KDlGL0eNClK1swEj cEf2q62cbjfmzg+ExLVbN9Hs6Pv6IPTH6HwHW/5HZSm7JicZhTSXBy5qspdWykDkFTS7xd aHvly4OXipmuCrz6yZ9OlG6cxwdbPpc= Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3df036d7132so110073b6e.0 for ; Thu, 29 Aug 2024 14:44:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967851; x=1725572651; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hAPF+mFWkE9aMPYn9qrkTRHhUapPOCqVhxefhrunDUc=; b=Q21PzM2cD8qU0mhmIg9BbKEWlNaaxxDKJGbe8l0+d8TExSXNzxl9RjCRV/z5aAQoTt I6T17GfLzuckky/1i0bAEPrpIgcgRQU3bRMivAGsFvGwsFRiry+qVYJTHaxMfwJ+6Oy4 u0km6B3nwhS7BTzkDgAictrslLf7k1bsIkxxw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967851; x=1725572651; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hAPF+mFWkE9aMPYn9qrkTRHhUapPOCqVhxefhrunDUc=; b=KL0znMZTgXlteZ45JW8EK090dm4PAH3DnkdcDF5cSx940s8QbMUSj+8rL+c0VyiS9z qy8KSYeYuymQFgUrEXhiXVHUDaCwfWbsb8UNroNyvP1AW6Qr7e52CzOdSgoQ+k5xpwW7 YhyBYUNV+SDohwHbzXzHeph1iRl27a8pGpZbnyGOJDMtlzDIcewQxDleCGFb6KOSSu4v iSf/KICK/StbrSGKdG4ET0N/RH8CijPGHOszPOeEbMU3lq/56800a2/xaRnwwqAOAoJj +XW0nHqg/hgytPIF8+sbyjOPTLPjBfZ0/GJzSsxPG7WjbFgHdxClfyei5rgjL5RadDkA VEkg== X-Forwarded-Encrypted: i=1; AJvYcCUGPRltKiULW1L8RAN7D0e0791AZxvGwjHpJ7Zx9IOOyD6t49E5smnkgLAuOpEXJ2HVauYWZ7HZrQ==@kvack.org X-Gm-Message-State: AOJu0YymrXuSQC3x/P8wZYuqxtyG2jJ0INiS6l6Qvp1h85UY0Cm0BozI ymcYfhMde1HYvOn0n/cSGnOR4H1cIFdaO3wIKbhz3wUROrePwQHQheEOVVf+Aw== X-Google-Smtp-Source: AGHT+IHtVngpZOZlZonUTLqOqg/y/b0gvT/st7+gVnwMaqdrCOVHCIfjFfjqHOSo+TMO91gZzOvYCQ== X-Received: by 2002:a05:6871:5226:b0:260:ccfd:2e52 with SMTP id 586e51a60fabf-27790377431mr2564127fac.7.1724967850809; Thu, 29 Aug 2024 14:44:10 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-715e56d7593sm1585108b3a.168.2024.08.29.14.44.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:10 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, pedro.falcato@gmail.com, willy@infradead.org, lorenzo.stoakes@oracle.com, broonie@kernel.org, vbabka@suse.cz, Liam.Howlett@oracle.com, rientjes@google.com, keescook@chromium.org, Jeff Xu Subject: [PATCH v2 4/4] selftests/mm: mseal_test add more tests for mremap Date: Thu, 29 Aug 2024 21:43:52 +0000 Message-ID: <20240829214352.963001-5-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829214352.963001-1-jeffxu@chromium.org> References: <20240829214352.963001-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Stat-Signature: qhkqonaii6reyzb93o53gxaika8ihdqj X-Rspamd-Queue-Id: E3F5B1C000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1724967851-169561 X-HE-Meta: U2FsdGVkX1+LuNJfbOIdv8BqSCjdtFhC8bdhC+H9CLUSi74JYg/DOJrc2cmoozEU7lc/xPwX7ILuDCU0y+4bac7K13lunZe3TWGQfSgPas3JyVjBkdA49a8YKRFAfr0iVCRccQUWHBI2cisvctBtHkVIQNN59+fWbnG7pAF5yHWVlfUW2KbmAfWkDmGBVy7g8SUnLwO2a0B6JOFEhPvTD/TqXDyf3dLrgn5p1QaUrOFjmbXA+7VDZamSQEx/iFMmycD17JVOGmfoFCAXszvNdv0CLvUSWs3PePK5o1r+AuO/odQSohgA1FRDPHwhz8iLOHKeE3FaaOW82LkfmeegaUCQ/iIPRE64Haoo6z+sbu/ltJB8sbpktSzNASCp1txikuaXYieDYfBX7drh1AEUkAAqNmbd4t7CNyVfq898Bp3vLV1HpAwMbzbDwrRFeg8Du4Scud7/KuDdKWP5wbhECM6m0VIRY+o6S8zlw+t5CLwjCvSE5UqVJpTS8D0QMyyfSGLbRGTh8pTUrc5jjC1Y5pj6zAir9fWsOFMDilIonvtwAcYha3vTYQrFiAJduU851zegcgm/qNpq3z7mEmrkwMfqZnfcyXnq89qizoswywBe2Gfi4MY5NfiHIrPbMGy/gzRGaaMHrgxEPcTPBU3sJYPLrR+8aYQzveScNAFzdX301VLT7DytKX6lWbkIUkHZQ8pREMr4l/7N6Z3nblyVClSF/lKOiRj6cYd9i+Osuk6XDLgEk77eu4C5/RrCBABmx/aWqqdShJP+Q+eA8P8oSRsZkOrsU8ONDMRPUefDZ7WsHt9Q1B+XWLFr20/HU/Ie24L6ysZk+6hvAHI0n2dA57IiZzEVlaQrSQSggRDO47d4xTEBpYAUGgoNNGIGl/6aOFRPCUpMkfUzfTPpi28BqZI/14HNAr2XJyD+j6QqLkMShsqBuvqRWADE3u6XE/d+yy7SReSfYuRLrZ9kXUu tpSBNr2f cuK8BBik75wYTZGirArn6eDsuxnnP9fi2wuspXy3mh0GCNXHi+Mt0d0ofDkai2Rrgx48i7M0B5Jtv9LFmFbeJEHAE+/SNA+BcxjMnmNUTNv5E4QZeG1CODscof1PCL4kdpA4ylyH2sWJ6XPgVCSmBdpOHIFgLM1h6JqUD5sO+GayyBm4GhqQdJIW/nQkRjf3AzJeKo5ZconpILvzyvxkwyJYvdBx4R3TYLf75wjMZnDQjWadPpZMNztfKexcfaYlTU8JD0HsirOsI+WYPPmiXo6VsSnp2BdUl+3eGr1iDaCRM07vT/TPap1wapannGmOK9Bmc0UT6dMhxse9wChvi6JJ6toQs6bxnbCEolclYszs59Lv0DDXbQ1rMH+CXue2wRsCj X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Jeff Xu Add sealing test to cover mremap for Expand/shrink/move across vmas. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 202 +++++++++++++++++++++++- 1 file changed, 201 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index d83538039e76..494035129d65 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2338,6 +2338,197 @@ static void test_seal_mmap_reuse_addr(bool seal) REPORT_TEST_PASS(); } +static void test_seal_mremap_shrink_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + ret = sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret2 = sys_mremap(ptr, 12 * page_size, 6 * page_size, 0, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 == (void *) MAP_FAILED); + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + } else { + FAIL_TEST_IF_FALSE(ret2 == ptr); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + } + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_expand_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + ret = sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + /* ummap last 4 pages. */ + ret = sys_munmap(ptr + 8 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret2 = sys_mremap(ptr, 8 * page_size, 12 * page_size, 0, 0); + FAIL_TEST_IF_FALSE(ret2 == (void *) MAP_FAILED); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_move_expand_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 12 * page_size; + int ret; + void *ret2; + int prot; + void *ptr2; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + setup_single_address(size, &ptr2); + FAIL_TEST_IF_FALSE(ptr2 != (void *)-1); + + ret = sys_munmap(ptr2, 12 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + /* ummap last 4 pages. */ + ret = sys_munmap(ptr + 8 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* move and expand cross VMA boundary will fail */ + ret2 = sys_mremap(ptr, 8 * page_size, 10 * page_size, MREMAP_FIXED | MREMAP_MAYMOVE, ptr2); + FAIL_TEST_IF_FALSE(ret2 == (void *) MAP_FAILED); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_move_shrink_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 12 * page_size; + int ret; + void *ret2; + int prot; + void *ptr2; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + setup_single_address(size, &ptr2); + FAIL_TEST_IF_FALSE(ptr2 != (void *)-1); + + ret = sys_munmap(ptr2, 12 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* move and shrink cross VMA boundary is NOK */ + ret2 = sys_mremap(ptr, 12 * page_size, 8 * page_size, MREMAP_FIXED | MREMAP_MAYMOVE, ptr2); + FAIL_TEST_IF_FALSE(ret2 == (void *) MAP_FAILED); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -2359,7 +2550,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); - ksft_set_plan(97); + ksft_set_plan(105); test_seal_addseal(); test_seal_unmapped_start(); @@ -2480,5 +2671,14 @@ int main(int argc, char **argv) test_seal_mmap_reuse_addr(false); test_seal_mmap_reuse_addr(true); + test_seal_mremap_shrink_multiple_vmas(false); + test_seal_mremap_shrink_multiple_vmas(true); + test_seal_mremap_expand_multiple_vmas(false); + test_seal_mremap_expand_multiple_vmas(true); + test_seal_mremap_move_expand_multiple_vmas(false); + test_seal_mremap_move_expand_multiple_vmas(true); + test_seal_mremap_move_shrink_multiple_vmas(false); + test_seal_mremap_move_shrink_multiple_vmas(true); + ksft_finished(); }