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+IEstP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-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(); }