From patchwork Fri Aug 30 18:02:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13785418 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 34795CA0EDC for ; Fri, 30 Aug 2024 18:03:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C47CF6B00ED; Fri, 30 Aug 2024 14:02:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF7BF6B0189; Fri, 30 Aug 2024 14:02:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FBA96B01F9; Fri, 30 Aug 2024 14:02:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 71F6E6B00ED for ; Fri, 30 Aug 2024 14:02:50 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1C1461C39B4 for ; Fri, 30 Aug 2024 18:02:50 +0000 (UTC) X-FDA: 82509682500.17.405B4CB Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf15.hostedemail.com (Postfix) with ESMTP id 16B09A0028 for ; Fri, 30 Aug 2024 18:02:47 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=YEaGWCfs; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.174 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725040901; a=rsa-sha256; cv=none; b=us9R3bAK7Il/3abioXR77HGnyicpvmymA5MBWjD0bxsrE/Z+0kAE7H7UZJs0d/wJJ9VvLc +AjKQz807ZLkQwXaYN87+od8Z/Oc5bLcuhAChBw/sM+WCJHIocM/eQzXpPfhSYUUZreBg0 Gqz6laU+OwGPSUyUfQdlSan9/Dk7DC0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=YEaGWCfs; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.174 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725040901; 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=UsW4WSHclFQuxQsQy9Vi261+UBXUohqxuc8CpQQRsjc=; b=2iJ4cj2WzFHmaYzWMNW+hkhbk1PAkZBxQwvfBtX87eruUg2gVuq5l6c97SQ+EEhGdqk2mH F8H3fPfuj4B38dNycXKhS7U7BxyYTzNC6N1V0YETKIFaum2EJCcTwqn/W7DiZuBYkHMcB4 b6O5K1CCJj3UxITN2c7H9iRgHZV1048= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7c3d415f85eso268319a12.0 for ; Fri, 30 Aug 2024 11:02:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725040967; x=1725645767; 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=UsW4WSHclFQuxQsQy9Vi261+UBXUohqxuc8CpQQRsjc=; b=YEaGWCfsuBKqV8MDcE/FFR2DnWqoTPXVifAywpltvL7+olcnxFHSx7Nx9anJfCtuhh 8wUTk6YZ1oQ4y+vVXdyi4gFU7LVbVivYrHvesItdIlIphTa4TIwFg+2HbeB6dWlncPiK hoJcEXgMbp5/GEw0ocEwNdSmO5oxHPLjXXYHM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725040967; x=1725645767; 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=UsW4WSHclFQuxQsQy9Vi261+UBXUohqxuc8CpQQRsjc=; b=RTSSKAkpAyEFKnS+8znrASrv4Q0VxtbZ7GCMvkDwb9uzWi9UAemiA/w+9byqL4iMj9 tuxzrExTvRdqOZnah0lXeIDsBEXFM0CMiMyjE+ADEhh4SVv6pp6Sk4KvIggR4zbqGRKD IlGQiENrAm6EvYUSwXrdKvlMe+Qow4YqVw9rDXl/36m4nw8Ys9qOC1J3zj1DWg4Vm3Wb Nl6P2mISgZOeanYp7ZPop5HSyfAuspZbWCB4jO86Y68LuLVI7x8Qmd+jkwr+A/yMlM0j KrVhmqJot0tmo0Apc8Oq2u179sHmrzfVUh6dwZMzsp+XZ956op5tK7vZCvQ5DqJJ0NMg H7Hg== X-Forwarded-Encrypted: i=1; AJvYcCXZ16ECP3lroP2p82JSnhjtM2PGx1iSW0xkcDVIERWbo0ZD7e1tWh+P65F63CplFBUQnzzbPgOMxA==@kvack.org X-Gm-Message-State: AOJu0YwcVvhElXfEHtsnlBLCAn7GTTDLW/ZpUXDP/+t9xo8ICkSk1tfc CUAIllbB2JZZY7v7gRD07v1G+LWzDdxqYTsEiDoofSQ3frukcxLz2gq7z0XXXQ== X-Google-Smtp-Source: AGHT+IFVcv9BKi1oAf72gfGg95j/WA/x6/ipRZgMh/F89Lt2abZpbCyTm4IVK3HWruQmyswmFRdJyw== X-Received: by 2002:a05:6a21:3385:b0:1c4:af14:d9a4 with SMTP id adf61e73a8af0-1ccee3bc42dmr1727971637.3.1725040966307; Fri, 30 Aug 2024 11:02:46 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-715e56d7278sm3153762b3a.171.2024.08.30.11.02.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Aug 2024 11:02:45 -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 v3 5/5] selftests/mseal: add more tests for mremap Date: Fri, 30 Aug 2024 18:02:37 +0000 Message-ID: <20240830180237.1220027-6-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830180237.1220027-1-jeffxu@chromium.org> References: <20240830180237.1220027-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 16B09A0028 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: mqpu3pej3ego9yesswpchheacjsmxwsh X-HE-Tag: 1725040967-389902 X-HE-Meta: U2FsdGVkX1+gTtUmLsb5ssY4d1qRAZ8iWXNYBOqXEZZ0AyEOX5m1XH1/Z8LwS1FsxANRz9iBsVuNiSBPrD/W3D7z8T0zFJu9RarPPdBeWfoR3jySdiLPdaSJBf3QyP59M+xevo46B0MMMAe7zzlGltmJhx5mkDJjmWGrFjoWHlajQ33aIFGr9UWPyrj28n0ICTm0SpMe0bhOi3YZ9wjBfWZ6PaufLYkSHFB/Cn6LyuaHCYnp/Mjrl4zr4i55zb6GP8l8vOhnih7XGINBYFW11qKImKVmKvi6ffNXMuMmZ3A2UuxIl6BNWIV1wfFlZOHcEE5TiTbfwNPfmCQFSGPBxHfs1SdOp4CK5lSqEqymXDu0IHGnZ65ewgyvj3cSkLbR1sV0P+ZofPH9DlxEk3/ZidbEH/o186uRmjdMZtjXXhD8hQujYjNMEQPy5hbzbLDvNXh8PNTp1kTbCZdG/FYRW+RBokAr3qPsUMgaVEkS9RqHwAOCkjytLNsymXawPYHfKlxgycmNeKzwEZ8p/9I7xdj6cRL7FZCYVIoLyzSiyjZ4oD4Oh8OmVQtywU/NCxjqhmZU2c7rnvFFmNRaTj4FqQBGuvQWNKoxPsPmwHZcial/OXvTCxLC8pJzmb3yaLf7Pdbfx9su9LoohmbEJ9wqSH0X0kIXfdW8MIOUd88os5tamd1PZtDDboHN0tdzHo4p+QJUmlWTUPrlODIG1B3MLy9uKIIP73Hm3v1FvJdGXD81YiIv3VgQTuePT/TZtTv8yFIvx/Mg7bIJ1uHb97v746McDaLfJCWgrJeq6EzBBt4T+ix09aU/Auuwiiku3UodX3dba5nyglMYisA0xgp/l39RzH95lEUH+cVHusBS2q4Nuft2cIpg9607OjtHskVNNFK6Tl8r8INm3rw7gGBkA7joX6LefbwhsWL9q7aBVodNzTcqRdPK0xNkwmUIPr5h6L6o4kU/pUxudFR5a0/ 3ivZGqfE NzdEZWh02v5u1cvXi7lJG5IuczGwK+rSWu+a5DAsLcxqO66GORZyn0Q18+DnjiGkp81YW3VH/q9o7RSOs0BCMrSfRR2bJXc65vF6e+aNzkl+tFlAJdqG2zG2T5rfORQ1sEDsF3KWIqfSgG59yPxOAIXg3GOK7sWUomuL/uXK2bIXc/rA49WkTDnotYdmKul7GxbgQ65NQvr96MbLAnI1ySG/n9RvQzpsadkVVxI7QiYNznZGIsAGdb5Ulw/MPoPNblr7QtD7ntcPOTabaPBkwf0NuBdHhTXrqRLX/sKwEdinENt7nj5uOwUbunxDvOfLhlkzvhsbzKVWlb/s= 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 3516389034a7..fee655bbbf0a 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2339,6 +2339,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(); @@ -2360,7 +2551,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(); @@ -2481,5 +2672,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(); }