From patchwork Thu Aug 29 21:43:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13783934 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 4AD7BCA0EC0 for ; Thu, 29 Aug 2024 21:44:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D21676B0082; Thu, 29 Aug 2024 17:44:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCEEB6B0088; Thu, 29 Aug 2024 17:44:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFA1D6B0089; Thu, 29 Aug 2024 17:44:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8C6596B0082 for ; Thu, 29 Aug 2024 17:44:08 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3AE271412CD for ; Thu, 29 Aug 2024 21:44:08 +0000 (UTC) X-FDA: 82506611376.10.450D99E Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf18.hostedemail.com (Postfix) with ESMTP id 345141C0010 for ; Thu, 29 Aug 2024 21:44:05 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=WJ2D1S5+; spf=pass (imf18.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.177 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=1724967801; a=rsa-sha256; cv=none; b=u8XNlwiO2tf41ClHUaTQ+rUqhnrJ0US5Q32WkyESuebDtMqHthWKzGIIFW4fJ2zUvZ+tDT AXYrcg7yhJxZeZnksY5RxOO8dE5DrKFLxjiuj6L9IFuH0lM7Q1SKnqNLIuJwt1iCCk7S9q RzBGgp6AkPcT+DjFKXH/I7t3y5uIYcM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=WJ2D1S5+; spf=pass (imf18.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.177 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=1724967801; 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=EV36Q5Y79tUSIn32vCscn3phXv9vAn9RdQBx5GFOQFk=; b=IZ2V+3OC/BaHjhBQzCSIAUy/0VaUdZe85Sbo+JMo2x8B7wevC7xCQP2wNsu6BovO3ryKtm 6ZrzfJrMNbI4rMVCGos+r698yUeHhSyMievV0ppBYxGavpuPl+6t1lrh6Hlr38Ok4yszHg VibM8OsLCx4SYqJBttIkbt3zbFTnBiw= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7cd80d28743so131485a12.3 for ; Thu, 29 Aug 2024 14:44:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967845; x=1725572645; 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=EV36Q5Y79tUSIn32vCscn3phXv9vAn9RdQBx5GFOQFk=; b=WJ2D1S5+UOo2172Yg7BCiEXK4zgQvdJFvzlzCPPWAhkzUVeeV+JOoUMNjMOLy0MdEp qiRuobXe5Vm5ErNnRh64munV8QMVzn4nFPOBt+4/JwFXSS3pTITaQ9XRIxVA6C5arR/x 5rhVJZy4Q/UzXHCOToa5Do5FQCqrDpxF9DQPQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967845; x=1725572645; 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=EV36Q5Y79tUSIn32vCscn3phXv9vAn9RdQBx5GFOQFk=; b=GkKTSLwoUbxtVRpMElKAEdCGLgSwNJK0fytuigKtXTRpEibHLeT7YrgJ8Bdg9KSenH 6NjaQb4TPGr4QLFHfrrnHO2+bbDAjI/ZkuHM2oCyJre8NSXLS2HsV7cusROy9x8zqbH3 liO4AQqzJTjeaDrNngCWCfZ9qvHU5wbz7XpzGJqAriz/76GajDVgwE5EHtndrwP3WIP+ LubJ/HzFYPAUD71fiHjNpKQWH6/m3TLMR4o9lYd4UaK3TFqpse0DYhygbGtoZ1Xr4C/X NjWd2Q8X6jtZKVcmlL5L32EDjvKFqkbND+QALqLq5711DeRTTTq1iZqxnhVjKVqgQCrt dmhA== X-Forwarded-Encrypted: i=1; AJvYcCV3OASEWggNrU+Vej1HsO7hIQx+EbSowYiLSUe1VbKElIVUpV5xyoZMlmFJl3yCvl7xceUNyqzRzQ==@kvack.org X-Gm-Message-State: AOJu0YwGLHsYqD8AYaVTCRacnrCZD5DyCxtadk/IJ01ne6OXxEyl2HPY OJf+mdMx6DYV9jwHFr02Y85rGoqRyyg20yqBQCuMrbmxCBjsK5quLC9Nszwfkw== X-Google-Smtp-Source: AGHT+IGXS/7yU03nYXMN9eJKu2hQiu0AShJffAnSYmN8VBCAEYihBobOV5g45a7+u97gPxZ2hllOuA== X-Received: by 2002:a05:6a20:a125:b0:1be:c3fc:1ccf with SMTP id adf61e73a8af0-1ccee3aa41emr204386637.2.1724967844597; Thu, 29 Aug 2024 14:44:04 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2d8445e80absm4717791a91.13.2024.08.29.14.44.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:04 -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 1/4] selftests/mm: mseal_test, add vma size check Date: Thu, 29 Aug 2024 21:43:49 +0000 Message-ID: <20240829214352.963001-2-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: s8bjgrdpfdi89nbnep9796o5w74ss7tn X-Rspamd-Queue-Id: 345141C0010 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1724967845-744978 X-HE-Meta: U2FsdGVkX18whskIYYuoicYmg90i2B9XiA4CffEinXz04eVjIfBlv4AVCWon0fcDl0k539tgampxFo8jUDFBvMyujhYbFtrSG2yVVBFzj93lONo6k8RXrCuA1/EtO2wbjmmJKyA6s0cmBStWwZ/4mhQkEEw9pppclW3MH/9knIf0GDdhZ8W2w+x1MyheWDbxLK029xWqZEhbwf6nhyzDAUBHb8JgW7QG59HmBqE+krKH3myQeKhtXvG6Vht5zo/2S7x9qohrUuaQ+FIepe1Axztga7apfO0c63hAlQit8DGpzjUh3c40A8pDQ5tqo5CCJg5NMIWd84SphUPGrjGySOFqgyhzextGnD2GhDZpFNVgv95p1a/3ual/m70JmbZTPmKzHFAP6ps8bgYpLWkOrkcJwjO7zGGXJKKF/8KHlTxQXtwn9JLJYfcBTLqG7/hQPrftXLVee1OxA+QRT4TCvr3rdoY+9fi1ACNNLcg/rk1ZCmkVTEqqxwcGI9KwkP/EKreQfTshzy+5XcIe0rTF+gBRDEzWCIeVIqbHn34OZpqVbLMyURVM0bVfvQVGonuYugRzRx1RIeYJhKg8tT5qY4ppR7Ct3j6N58JqPC4G4MeERXH4bib5Sw2gm8/92m3HErLIK+vBhPqxC+jyn9Yz4erMv+87wJ0PzQ+e5BTWvoPAwLhZKkdh3RrSEsuuPOz5Nbf+LoWAQ0YQIVGjQ/xmLpnD+stVYmIkg1HUeckWVrgSbVe6HPEYjGedrQtHlEiHtIAodgHmOXimVDj/r/qzHhJlHZUSFcChpsCXCOQBmZk9SrM/zDXnJAW9ZMs3BLiDW5DggGhepsv/urrlhlZl4ywXRLFz9MwsYQV01bk+55KHielWlPfxJLNnKmkipSKCCyuYxfpEpuLzX7K4OB4dcfqtDck3VIDW9+53sWOmSNbjXSO+slKSHwICwfWwBM6ihOdS7OVWpaHChutQmT9 CBK5GlRI CqTC6bJw6cCG+YLublCjuWuL2cNj/FblklWZbEBAsxVQGMgJIu3oEUMyBGLt4O8ugnDFgeGO70lKtWMMVySM4a6NT/KrJGFvwOfIvNXi5/EdJmGJsxBudXLdInT/cM4QgaZ0C1YUwl1mUxQgeUrE9ZSA30CIdnuQU9c7UiZg0H6Eab4qqQFMc4aVRdNtFpYxg6Q5/dLIKti3Zx57PBzr5UVrLTkVnfqw951aXweXiIvcpKhN6HhX7cwLgSao/Uzq5jV4ipZhAX+Q/OKmAYBMwN9a+EkmwAjDy6Nn/ZYT/zRjYkn9wPUSEAHaa5uMq6FNP0NPWmE8IFkMQz8HmcWfZ385UdfdXyOpOVi4SNooh85xSXSv+cLGYFZlHSSYFXN444l71 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 check for vma size, prot bits and error return. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 398 ++++++++++++++++++++---- 1 file changed, 332 insertions(+), 66 deletions(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index e7991e5fdcf3..adc646cf576c 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -170,18 +170,31 @@ static void set_pkey(int pkey, unsigned long pkey_value) static void setup_single_address(int size, void **ptrOut) { void *ptr; + unsigned long page_size = getpagesize(); - ptr = mmap(NULL, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - *ptrOut = ptr; + *ptrOut = (void *)-1; + ptr = mmap(NULL, size + 2 * page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (ptr != (void *) -1) { + /* add 2 page at the beginning and end to avoid auto-merge of mapping */ + sys_mprotect(ptr, page_size, PROT_NONE); + sys_mprotect(ptr + size + page_size, page_size, PROT_NONE); + *ptrOut = ptr + page_size; + } } static void setup_single_address_rw(int size, void **ptrOut) { void *ptr; unsigned long mapflags = MAP_ANONYMOUS | MAP_PRIVATE; + unsigned long page_size = getpagesize(); - ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, mapflags, -1, 0); - *ptrOut = ptr; + *ptrOut = (void *)-1; + ptr = mmap(NULL, size + 2 * page_size, PROT_READ | PROT_WRITE, mapflags, -1, 0); + if (ptr != (void *) -1) { + sys_mprotect(ptr, page_size, PROT_NONE); + sys_mprotect(ptr + size + page_size, page_size, PROT_NONE); + *ptrOut = ptr + page_size; + } } static int clean_single_address(void *ptr, int size) @@ -226,6 +239,21 @@ bool pkey_supported(void) return false; } +bool get_vma_size_supported(void) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int prot; + + setup_single_address(size, &ptr); + size = get_vma_size(ptr, &prot); + if (size == 4 * page_size && prot == 0x4) + return true; + + return false; +} + static void test_seal_addseal(void) { int ret; @@ -419,11 +447,17 @@ static void test_seal_invalid_input(void) unsigned long size = 4 * page_size; int ret; - setup_single_address(8 * page_size, &ptr); + setup_single_address(9 * page_size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); - ret = clean_single_address(ptr + 4 * page_size, 4 * page_size); + + ret = clean_single_address(ptr, page_size); FAIL_TEST_IF_FALSE(!ret); + ret = clean_single_address(ptr + 5 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ptr = ptr + page_size; + /* invalid flag */ ret = syscall(__NR_mseal, ptr, size, 0x20); FAIL_TEST_IF_FALSE(ret < 0); @@ -523,6 +557,7 @@ static void test_seal_mprotect(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -533,9 +568,14 @@ static void test_seal_mprotect(bool seal) } ret = sys_mprotect(ptr, size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -547,6 +587,7 @@ static void test_seal_start_mprotect(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -558,9 +599,14 @@ static void test_seal_start_mprotect(bool seal) /* the first page is sealed. */ ret = sys_mprotect(ptr, page_size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); /* pages after the first page is not sealed. */ @@ -577,6 +623,7 @@ static void test_seal_end_mprotect(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -593,9 +640,14 @@ static void test_seal_end_mprotect(bool seal) /* last 3 page are sealed */ ret = sys_mprotect(ptr + page_size, page_size * 3, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -607,6 +659,7 @@ static void test_seal_mprotect_unalign_len(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -618,9 +671,14 @@ static void test_seal_mprotect_unalign_len(bool seal) /* 2 pages are sealed. */ ret = sys_mprotect(ptr, page_size * 2, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); ret = sys_mprotect(ptr + page_size * 2, page_size, @@ -636,6 +694,7 @@ static void test_seal_mprotect_unalign_len_variant_2(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -646,9 +705,14 @@ static void test_seal_mprotect_unalign_len_variant_2(bool seal) /* 3 pages are sealed. */ ret = sys_mprotect(ptr, page_size * 3, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); ret = sys_mprotect(ptr + page_size * 3, page_size, @@ -664,6 +728,7 @@ static void test_seal_mprotect_two_vma(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -678,16 +743,26 @@ static void test_seal_mprotect_two_vma(bool seal) } ret = sys_mprotect(ptr, page_size * 2, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + } else FAIL_TEST_IF_FALSE(!ret); ret = sys_mprotect(ptr + page_size * 2, page_size * 2, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr + page_size * 2, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -699,6 +774,7 @@ static void test_seal_mprotect_two_vma_with_split(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -719,17 +795,27 @@ static void test_seal_mprotect_two_vma_with_split(bool seal) /* the second page is sealed. */ ret = sys_mprotect(ptr + page_size, page_size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + } else FAIL_TEST_IF_FALSE(!ret); /* the third page is sealed. */ ret = sys_mprotect(ptr + 2 * page_size, page_size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); /* the fouth page is not sealed. */ @@ -746,6 +832,7 @@ static void test_seal_mprotect_partial_mprotect(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -758,9 +845,14 @@ static void test_seal_mprotect_partial_mprotect(bool seal) /* mprotect first 2 page will fail, since the first page are sealed. */ ret = sys_mprotect(ptr, 2 * page_size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -783,15 +875,15 @@ static void test_seal_mprotect_partial_mprotect_tail(bool seal) } ret = sys_mprotect(ptr, size, PROT_EXEC); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else - FAIL_TEST_IF_FALSE(!ret); + FAIL_TEST_IF_FALSE(errno == EPERM); - if (seal) { - FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); FAIL_TEST_IF_FALSE(prot == 0x4); - } + } else + FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); } @@ -846,6 +938,7 @@ static void test_seal_mprotect_split(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -862,16 +955,34 @@ static void test_seal_mprotect_split(bool seal) /* mprotect is sealed. */ ret = sys_mprotect(ptr, 2 * page_size, PROT_READ); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); ret = sys_mprotect(ptr + 2 * page_size, 2 * page_size, PROT_READ); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -883,6 +994,7 @@ static void test_seal_mprotect_merge(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -899,9 +1011,18 @@ static void test_seal_mprotect_merge(bool seal) /* 2 pages are sealed. */ ret = sys_mprotect(ptr, 2 * page_size, PROT_READ); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); /* last 2 pages are not sealed. */ @@ -917,6 +1038,7 @@ static void test_seal_munmap(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -928,9 +1050,14 @@ static void test_seal_munmap(bool seal) /* 4 pages are sealed. */ ret = sys_munmap(ptr, size); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -948,6 +1075,7 @@ static void test_seal_munmap_two_vma(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -962,15 +1090,33 @@ static void test_seal_munmap_two_vma(bool seal) } ret = sys_munmap(ptr, page_size * 2); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + + size = get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); ret = sys_munmap(ptr + page_size, page_size * 2); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x6); + + size = get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -1018,33 +1164,75 @@ static void test_seal_munmap_partial_across_vmas(bool seal) { void *ptr; unsigned long page_size = getpagesize(); - unsigned long size = 2 * page_size; + unsigned long size = 12 * 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, page_size); + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); FAIL_TEST_IF_FALSE(!ret); } - ret = sys_munmap(ptr, size); - if (seal) + ret = sys_munmap(ptr, 12 * page_size); + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, 6 * page_size); if (seal) { - FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); FAIL_TEST_IF_FALSE(prot == 0x4); - } + + size = get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr + 6 * page_size, 6 * page_size); + if (seal) { + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else + FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); } @@ -1074,9 +1262,11 @@ static void test_munmap_start_freed(bool seal) ret = sys_munmap(ptr, size); if (seal) { FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); size = get_vma_size(ptr + page_size, &prot); - FAIL_TEST_IF_FALSE(size == page_size * 3); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { /* note: this will be OK, even the first page is */ /* already unmapped. */ @@ -1095,6 +1285,7 @@ static void test_munmap_end_freed(bool seal) unsigned long page_size = getpagesize(); unsigned long size = 4 * page_size; int ret; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1111,9 +1302,14 @@ static void test_munmap_end_freed(bool seal) /* unmap all pages. */ ret = sys_munmap(ptr, size); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(!ret); REPORT_TEST_PASS(); @@ -1144,12 +1340,15 @@ static void test_munmap_middle_freed(bool seal) ret = sys_munmap(ptr, size); if (seal) { FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); size = get_vma_size(ptr, &prot); FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); size = get_vma_size(ptr + page_size * 3, &prot); FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { FAIL_TEST_IF_FALSE(!ret); @@ -1170,6 +1369,7 @@ static void test_seal_mremap_shrink(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1184,6 +1384,10 @@ static void test_seal_mremap_shrink(bool seal) 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); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { FAIL_TEST_IF_FALSE(ret2 != (void *) MAP_FAILED); @@ -1199,6 +1403,7 @@ static void test_seal_mremap_expand(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1216,6 +1421,10 @@ static void test_seal_mremap_expand(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { FAIL_TEST_IF_FALSE(ret2 == ptr); @@ -1231,6 +1440,7 @@ static void test_seal_mremap_move(bool seal) unsigned long size = page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1249,10 +1459,12 @@ static void test_seal_mremap_move(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); - } else { - FAIL_TEST_IF_FALSE(ret2 != MAP_FAILED); - } + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 != MAP_FAILED); REPORT_TEST_PASS(); } @@ -1264,6 +1476,7 @@ static void test_seal_mmap_overwrite_prot(bool seal) unsigned long size = page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1279,6 +1492,10 @@ static void test_seal_mmap_overwrite_prot(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == ptr); @@ -1292,6 +1509,7 @@ static void test_seal_mmap_expand(bool seal) unsigned long size = 12 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1310,6 +1528,10 @@ static void test_seal_mmap_expand(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 8 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == ptr); @@ -1323,6 +1545,7 @@ static void test_seal_mmap_shrink(bool seal) unsigned long size = 12 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1338,6 +1561,10 @@ static void test_seal_mmap_shrink(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 12 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == ptr); @@ -1352,6 +1579,7 @@ static void test_seal_mremap_shrink_fixed(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1369,6 +1597,10 @@ static void test_seal_mremap_shrink_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == newAddr); @@ -1383,6 +1615,7 @@ static void test_seal_mremap_expand_fixed(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(page_size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1400,6 +1633,10 @@ static void test_seal_mremap_expand_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(newAddr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == newAddr); @@ -1414,6 +1651,7 @@ static void test_seal_mremap_move_fixed(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1430,6 +1668,10 @@ static void test_seal_mremap_move_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(newAddr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else FAIL_TEST_IF_FALSE(ret2 == newAddr); @@ -1443,6 +1685,7 @@ static void test_seal_mremap_move_fixed_zero(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1460,9 +1703,12 @@ static void test_seal_mremap_move_fixed_zero(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); - } else { + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else FAIL_TEST_IF_FALSE(ret2 == 0); - } REPORT_TEST_PASS(); } @@ -1474,6 +1720,7 @@ static void test_seal_mremap_move_dontunmap(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1488,6 +1735,10 @@ static void test_seal_mremap_move_dontunmap(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { /* kernel will allocate a new address */ FAIL_TEST_IF_FALSE(ret2 != MAP_FAILED); @@ -1503,6 +1754,7 @@ static void test_seal_mremap_move_dontunmap_anyaddr(bool seal) unsigned long size = 4 * page_size; int ret; void *ret2; + int prot; setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr != (void *)-1); @@ -1529,6 +1781,10 @@ static void test_seal_mremap_move_dontunmap_anyaddr(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); } else { /* remap success and return ptr2 */ FAIL_TEST_IF_FALSE(ret2 == ptr2); @@ -1690,9 +1946,10 @@ static void test_seal_discard_ro_anon_on_pkey(bool seal) /* sealing will take effect if PKRU deny write. */ set_pkey(pkey, PKEY_DISABLE_WRITE); ret = sys_madvise(ptr, size, MADV_DONTNEED); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno == EPERM); + } else FAIL_TEST_IF_FALSE(!ret); /* base seal still apply. */ @@ -1876,6 +2133,15 @@ int main(int argc, char **argv) if (!pkey_supported()) ksft_print_msg("PKEY not supported\n"); + /* + * Possible reasons: + * - unable to read /proc/pid/maps (unlikely) + * - parsing error when reading /proc/pid/maps,e.g. len is not expected. + * Is this "TOPDOWN" mapping or format change in /proc/pid/maps ? + */ + if (!get_vma_size_supported()) + ksft_exit_skip("get_vma_size not supported\n"); + ksft_set_plan(88); test_seal_addseal(); From patchwork Thu Aug 29 21:43:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13783935 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 39460CA0EC4 for ; Thu, 29 Aug 2024 21:44:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BAA96B0088; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641B16B0089; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46AD76B008A; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 259D56B0088 for ; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AA3D61C3329 for ; Thu, 29 Aug 2024 21:44:09 +0000 (UTC) X-FDA: 82506611418.29.C615450 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf26.hostedemail.com (Postfix) with ESMTP id BA9A7140007 for ; Thu, 29 Aug 2024 21:44:07 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lQiW5eRP; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 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=1724967758; 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=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=7MHZW3hD89HefGL6soLlFj6dlai7DFkdJ921RxhFOOZVsGwITMZx8q0ki9hiDVOsMHU7gT NaTrxun9/vXIUe6GFezk6NUnnk/uLzm7VNVoyVBLwnRpOdgL3rQfloB5Vb9U54K5X9HLDu A6hLDEoicyrkUGF2h4n+XqafzziWyGs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724967758; a=rsa-sha256; cv=none; b=ze2KRcKp+fdTQoXKSw1QCFxHfcEI/r8xDh4ZBpRJ/s5QaNYh1T8gnY3HEMYWBYCAn9lsU9 pCa8MAq5VKDJ0W+gjDNCe4gzQw9EV9HMQrZ09iq9KBgw3iybk54VvAsFy5ChWw3afCIkDT IQW+3JAf88Ude0dO3CVmyWDfmLIYqz8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lQiW5eRP; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71420e66ecfso96279b3a.0 for ; Thu, 29 Aug 2024 14:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967846; x=1725572646; 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=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=lQiW5eRPU3Ovp/c531xW0lBLcTPv9DHCA+Z1uSEErkjH10bRJnLKW9t2JBkYRu4lQL aanyYatoo124YWwb/8o4orFehjAdQHn32hzAiAiCOgSDUI/EPWD5n/tTLy6ytbvNSL24 9wL+/W8IDfBYjUFB3NcrSNAvMMZkl1YyU3zOg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967846; x=1725572646; 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=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=Oj9qSuDWFzrR9i5DetDGemUg+Z19w11hqe+LI07KXM1wp4dzFMZUYmDrJvgEUwOnOT 8bILVKMzsndeEF4ywdyJ9Nu6yoeJE4/wL4B9yPko8QlGLbwe0P0m6whIljtbi7+FWhH/ yL8DX045mngTJhSR3O/H2sZmpBmrShPX4HfgaoWtGj7EoXW4Q7ap/DkiABFaaupN8Ws7 q9dZApLSmxVuICN+vYhKLCymljHcNY66UqFl7211ps2bERgf0+HoSHdT9INukdG7ostk pCZpa66Pj0/2Br787J3hdDInhpQmKnn7gLmI5YodsF6QyYdzG17h+J+7RqZ7kbYcaBdU gNmQ== X-Forwarded-Encrypted: i=1; AJvYcCUqoez9gI9JW9fE4edUC72NCR+h9iAQmLFK/YgQ8CR/jn0UHgUzSVR0+31NbnbJFHtkwc0QnqTbzg==@kvack.org X-Gm-Message-State: AOJu0Ywq3iQyUnNfcllUh6pNUaLqCtPDbwwObWrKt3WZ0NtIJFEvKJWJ xcn9a2nZRstMMJOI+8OMLjQv7HUxAIl2njwSsAgXIgRjSBbD+QoxEQ2VqlmdFA== X-Google-Smtp-Source: AGHT+IGw6LhTiqcHA4wOD2hbInscY7fZlLpfvydbHUPkI3AUpW/gqVA9D8kpvdBYJgGdQzekGp3weg== X-Received: by 2002:a17:903:2350:b0:1fa:a9f5:64b1 with SMTP id d9443c01a7336-2052761fcd8mr1316265ad.3.1724967846304; Thu, 29 Aug 2024 14:44:06 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-205152b26b2sm15882985ad.6.2024.08.29.14.44.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:05 -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 2/4] selftests/mm: mseal_test add sealed madvise type Date: Thu, 29 Aug 2024 21:43:50 +0000 Message-ID: <20240829214352.963001-3-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: jb487b3fzepys58998egg6tz86k8hpfb X-Rspamd-Queue-Id: BA9A7140007 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724967847-853846 X-HE-Meta: U2FsdGVkX1/oRgmXIVr+zFFFIvj3sgl9QSlo7CmUm/977ZKDuUDa1DgbqQAOVj7gu3UqsUjyeUsMHpaUEEmTIPs7uuO/QjnkEqPLm3Z/omkjqH6R+zbE3/gMWurYvg1bQXcMlswBCvq7/NpEjA6MFyp8iHfCnZzILME8hHNIB4mtt2ZWYzBggVIIXy/tTsHEGoH4/hHzDOqjMSUZKnVj0vTYgwohb/3tD7tMw0wTZ2nGZO77I5c5ywYxdbPnXkt0qV4QRO71tLZJ9q1Cci91niYvz3UpsieCFBvkHXg2iAr3EVnz0s7ncUVWroc8yXWbwmAGVjbkimR5lgRJFqn1X6ARf2mkBinttj0FSyc6qteyvBIEdUU5Yj5NBlT9q5wX+hs7YeZUdSxTDbX14c5TGAOcnNxoh3JLtI9P1LV3eMP4Rv5aupA+cFvtK8ZQL10xLx6NlwA0A5p1N0r/AeG19HdcUKS1oHn8cUOwQB7BKo/IbNYJe0JU9lQFhQFKrkGYyyrDL1FTrCT62+JIb8aJ5hX+K3incscTcdxdDSPEsMfzS4xb/rwdZZPkCw3TbJJwBykLcoSAQIrwP9HqsB4LZAkbEK9XorH7yN0sj64ELrxaNwPml4dq7F81Hon99/mvTdnQ6RhWzEaqlLAXncnwtWWzwN4rx5zaMpxppgI2p1PrI9qV6727Sdd1phTQh8liniI8RO1nBiwZRtZmttpungw8EcHW0IYGC4Wn9Js7qqDn6PsEyYn/Y2lXAPiWxxw5GLXBd6VCP/Jcn9Q3OqHgSMNyGedl2aAvnbchbFqsCvwQ6ujvH0ADFiT98HJW4/VwT4oqnFA98rcvq/zHwOsI6hZRVIT8PpjIwT4DWksjcmiWkl0GbZ8SPLd4OF5BB8czBipiJyx6fSIwvYkDxUS7NhUKRSfUq5w8CqDTPEVWAPTpUaLArekOOQtiOPTNeV0WSLgFqUgFlTc/I9am29a eoBFsUeG GD24PaggVHxxpKkte430mZP0uaItOiv3nGKmSTJ5hjHcv+2den+PJictdAki+eAwpvF4FRrTVBeRS4wIsmA5M1kAQ3J/i0u3LUJeLwsm6xBoNiDgOxvwA/wT/YCTCH+r2BBsDY8yE0IWq2OUoDYC5JVp5/vijBLp/C9jnY5yegHK4lIDc8Oq3714/pdi7oU+qCCyO5ewvvhEcjV7sFnKyLEBKtqWVSJgPggDfVMhE//d9baqNY6C0azg4hFsxh1KyKFB2j1zxuNsgSJl49rIyzOl3SXDzgjzpqk6U+eAFVtG3REPECksnau3+pN2Djw/hNIz1qUKDnYqehRiBV1yiH6aL9cKxX5T9pCQJ36hAcJey7wW++Wr5fNQ84A== 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 a testcase to cover all sealed madvise type. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 108 +++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index adc646cf576c..ae06c354220d 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2121,6 +2121,107 @@ static void test_seal_madvise_nodiscard(bool seal) REPORT_TEST_PASS(); } +static void test_seal_discard_madvise_advice(void) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + int sealed_advice[] = {MADV_FREE, MADV_DONTNEED, + MADV_DONTNEED_LOCKED, MADV_REMOVE, + MADV_DONTFORK, MADV_WIPEONFORK}; + int size_sealed_advice = sizeof(sealed_advice) / sizeof(int); + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + ret = seal_single_address(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + + for (int i = 0; i < size_sealed_advice; i++) { + ret = sys_madvise(ptr, size, sealed_advice[i]); + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); + } + + REPORT_TEST_PASS(); +} + +static void test_munmap_free_multiple_ranges(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 8 * page_size; + int ret; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + /* unmap one page from beginning. */ + ret = sys_munmap(ptr, page_size); + FAIL_TEST_IF_FALSE(!ret); + + /* unmap one page from middle. */ + ret = sys_munmap(ptr + 4 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + + /* seal the last page */ + if (seal) { + ret = sys_mseal(ptr + 7 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + } + + /* munmap all 8 pages from beginning */ + ret = sys_munmap(ptr, 8 * page_size); + if (seal) { + FAIL_TEST_IF_FALSE(ret); + + /* verify mapping are not changed */ + size = get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + } else { + FAIL_TEST_IF_FALSE(!ret); + + for (int i = 0; i < 8; i++) { + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 0); + } + } + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -2142,7 +2243,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); - ksft_set_plan(88); + ksft_set_plan(91); test_seal_addseal(); test_seal_unmapped_start(); @@ -2251,5 +2352,10 @@ int main(int argc, char **argv) test_seal_discard_ro_anon_on_pkey(false); test_seal_discard_ro_anon_on_pkey(true); + test_seal_discard_madvise_advice(); + + test_munmap_free_multiple_ranges(false); + test_munmap_free_multiple_ranges(true); + ksft_finished(); } From patchwork Thu Aug 29 21:43:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13783936 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 2EDC2CA0EC4 for ; Thu, 29 Aug 2024 21:44:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B74586B0089; Thu, 29 Aug 2024 17:44:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B218A6B008A; Thu, 29 Aug 2024 17:44:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99B736B008C; Thu, 29 Aug 2024 17:44:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7B21F6B0089 for ; Thu, 29 Aug 2024 17:44:11 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 426E0160690 for ; Thu, 29 Aug 2024 21:44:11 +0000 (UTC) X-FDA: 82506611502.02.2E49A46 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) by imf22.hostedemail.com (Postfix) with ESMTP id 6838CC0004 for ; Thu, 29 Aug 2024 21:44:09 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ON9Ui45H; spf=pass (imf22.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.167.173 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=1724967760; 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=pOFHefv5origwTURYhXBdEyQH2l+L4eXRhYcbCi88yk=; b=An8Aupb8lfkX27PYzk9+zXo3HmGUtqpWfxAqWWAHDVH09FKJRw4DlKs2fBgr/QUi43qFai i6RISvFmdCNVOx3GnKWHUhqTHMvmUxp8yogUAo2g0tg/FROrcFs1ij8IO3T0UvNNJsgvWy TF8oQPk2p3hhSyy/SleC3xdd84lnjgA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724967760; a=rsa-sha256; cv=none; b=nSAXoTMN4q9+kRFvVAKsG6lmB5m2PJWyVutKFtMdLMwjXk8tIWqSuyRS1q6YNf6qgSp+vb mEdcMdYYJv361Tzesar6lrwQ8nlR6GA2tovDSetRHHTeIyqG1HIU1ZsK2nVaF95ZNf1IdJ n1ire9C1HJWRJQwbBZVoaOHFHiGeWKg= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ON9Ui45H; spf=pass (imf22.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.167.173 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3db3763e924so84600b6e.2 for ; Thu, 29 Aug 2024 14:44:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967848; x=1725572648; 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=pOFHefv5origwTURYhXBdEyQH2l+L4eXRhYcbCi88yk=; b=ON9Ui45HFtCrgLAmtikMCLWobtmG5s2KZyVfJ3D3EuOhVxsipM9OPtoyW9aAsnbFQP 4YPTbjzZVMOqWLNDBB+dP6ld/+WCOdPIKI7u0CAP3nx7Ki6WNK77JWwuu34GtVd9x8X3 NOvQgU3S471SAlOwUe8isu9uz3Z3aao6X4f8Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967848; x=1725572648; 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=pOFHefv5origwTURYhXBdEyQH2l+L4eXRhYcbCi88yk=; b=QF6J9u2B2uOyowYkWLrR8eQnodbk1zAdEoNhy0A4tmIY5cAc/QXk7mVKvohQucnKfY gtsDXMrl1hs/gv2PkHspEVx1CAHFnKOPt/Vzy42SOLaYUNsqV0X/gkIvhJ16xzPilI+k bdiHgdOdsst2E+1VbHOk7rQjq7mQ97bA/0gRN4horydSVhqCCn0CO86Q2n2i6+E++HVB yn9TSMtaJD3r+JoGgdLiuBHsfW8hszPckUEJukyEbanybf6Cthxz2z2MNM5aE8qXiu+X JMbPST9zABxT/s1csdrakSOaVI4NhKkCyW6xREXBqcsSA1eSNQbbaL72WC0tiKmhSPaK waeQ== X-Forwarded-Encrypted: i=1; AJvYcCUBfsX3+8qt/vzkD0pFXu/BKlXc9tXItlBgKj9ICaaoX/a9I1He8+bvnvkh12Etk81CU1fRQchGXg==@kvack.org X-Gm-Message-State: AOJu0YyatVvGWwhcCUoKdpYQlchQZMPHliLUoF5Gj0E5a95vXpnqAlYn S0JG/lPkUKday9lvs1q8G3GA3KryTy5qii8jpjCDLL8vN8VYqNQ4EOqPHfvKmw== X-Google-Smtp-Source: AGHT+IFyc3ebYhYoxmlTy374CXmH+6Huc2oIlQtKye5oZuLAVTm63hVpSqGBNKx/nJcUYkxMgJL0wg== X-Received: by 2002:a05:6870:8a26:b0:26f:de62:7d8a with SMTP id 586e51a60fabf-277903bdf4cmr2281204fac.10.1724967848269; Thu, 29 Aug 2024 14:44:08 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-7d22e771c48sm1709417a12.23.2024.08.29.14.44.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:07 -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 3/4] selftests/mm: mseal_test add more tests for mmap Date: Thu, 29 Aug 2024 21:43:51 +0000 Message-ID: <20240829214352.963001-4-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6838CC0004 X-Stat-Signature: 6rzmsdigf6dhpgio3yx1u9mwk6fyht9n X-HE-Tag: 1724967849-960370 X-HE-Meta: U2FsdGVkX18YTjit3gFMueg8d3fzTobeXtdfpdTiV9AgEYkO+gIo4+sd3ArHFqrGJFnny8r8zprJI9jwrvtrTTij2+ZaCT+rtmVxwQDo1F/AU51miXkfqVyryY6zagExNnMUZwL+3jpgygpZjPGFyvDZFVscYB7B68DNtVg88CrAbfd4+uIx+OE0wu7VV/STpGMNZAOJ5mhtWahd0AkXro0dBU2ABdZbb/j0lxsBYh8zdBg/T8Piz1YWsxbosMLI+HE0nShsTFkbaaMMX8RSPaupopKJHEWU1dLDTuBhfiO6Zkc6telrFqWFk48VOtmNGNphJeoRGsPSIaNSvfvYA10n2FjGI8LWTkiAOxNaOHU+IEstPlWr7q3RQSRr0Eo3H6PlB4kUdzhIE4PBEypShcenlIGNAyK9CCO8HFgUrJ1LNoInLHBAAiUQ7MM03hn3rgccyE8cwfzXECDDGSTMHJIW/6MiAnAyiPXG5rgsiDDqJoa2JoGmyLiH5bVFEsk/VQW9xTl0ioG+3np1OEPXLzxteMLRDJjcyKd6pB04JcOgga+pEJr1uEWB6EGnYxOrDk8klb4dQmJcRwfD0ooIKALVjoObhjowUVPvIRDFPUINTXvSVv0x2vhGtqQj8DoIoNq5NINoYfag+ILOBXqyiD3kCO8qtavNoQw9QMPoFrpHZ2MbagYc1U7HHBzp6YLl/7qjBJpHd0JNlNnnFrCjX3gCKCyn3hYiZWwMZRwaJPv8duIpQi7M0y1t/aQcZ1Bz4Hnd/SzIYnrK/SBRAjRWaGX5MDC+3UyaN8CqpdHm3pDCIDq+Fm/3AC3kAzUI2LgEl+oJSXm13HJNvKexag9IJRDv0ZUt2bH7/Oz+b7kLprhHmkzSxxWt81fRN77+3j9ctENsmhJ4aU2mtsRyKvjgxv7FI6m7YBRtNJdbXaIzs9Fm/8QLq4+8yM80WD4CE9Y8MEWIkOlRAtuZ6FsmPsS 5vjmRIJ6 Csklxv6e+XpE8XXRmr9wxF/V9LqEcgz3D8veWAicabgdd9tlEhWsjZjG5zbg/jHwMcNZLGGV+CKw0Q5s9m/BmhkG0j3GSBKlyHzJ8ekUPa22i9TffsXRuEAftp4CGTZek952OZl4z8xAcM3O9+oitLftB1Ht2KFpm4wTLHRF0Q0KwdXJQZOn/jmaGngPTetfzDoty8DsEgHUGjBIJnY89UVZZ207jlQAkPmFtEVYL56ieH3l1g2fNqA6YNRcbbxslEnsj4JtvCMrcHm09gZKmx+O1cyL+4hH0PT5pPoPcJ1vfC9nbFM1fTV3/B95Y7DdE95hhxX3npmqCy0SbOPZUDBVr1urcJlJ9mE185U8i1k6d+Zitpp35+4vvJm9T3gvsKuE7 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 mmap for Expand/shrink across vmas. Reuse the same address in !MAP_FIXED case. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 125 +++++++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index ae06c354220d..d83538039e76 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2222,6 +2222,122 @@ static void test_munmap_free_multiple_ranges(bool seal) REPORT_TEST_PASS(); } +static void test_seal_mmap_expand_seal_middle(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); + /* 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 == 8 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to expand. */ + ret2 = mmap(ptr, 12 * page_size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 == ptr); + + REPORT_TEST_PASS(); +} + +static void test_seal_mmap_shrink_seal_middle(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); + + if (seal) { + ret = sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to shrink. */ + ret2 = mmap(ptr, 7 * page_size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 == MAP_FAILED); + FAIL_TEST_IF_FALSE(errno == EPERM); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + size = get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 4 * page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 == ptr); + + REPORT_TEST_PASS(); +} + +static void test_seal_mmap_reuse_addr(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = sys_mseal(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to change protection. */ + ret2 = mmap(ptr, size, PROT_NONE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + + FAIL_TEST_IF_FALSE(!(ret2 == MAP_FAILED)); + FAIL_TEST_IF_FALSE(ret2 != ptr); + + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == page_size); + FAIL_TEST_IF_FALSE(prot == 0x4); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -2243,7 +2359,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); - ksft_set_plan(91); + ksft_set_plan(97); test_seal_addseal(); test_seal_unmapped_start(); @@ -2357,5 +2473,12 @@ int main(int argc, char **argv) test_munmap_free_multiple_ranges(false); test_munmap_free_multiple_ranges(true); + test_seal_mmap_expand_seal_middle(false); + test_seal_mmap_expand_seal_middle(true); + test_seal_mmap_shrink_seal_middle(false); + test_seal_mmap_shrink_seal_middle(true); + test_seal_mmap_reuse_addr(false); + test_seal_mmap_reuse_addr(true); + ksft_finished(); } 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(); }