From patchwork Fri Aug 30 18:02:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13785417 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 7D29BCA0EFD for ; Fri, 30 Aug 2024 18:02:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F06636B01F8; Fri, 30 Aug 2024 14:02:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E90A16B01F9; Fri, 30 Aug 2024 14:02:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBB536B01FA; Fri, 30 Aug 2024 14:02:49 -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 A23BF6B01F8 for ; Fri, 30 Aug 2024 14:02:49 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 391D74026E for ; Fri, 30 Aug 2024 18:02:49 +0000 (UTC) X-FDA: 82509682458.02.37180DC Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf19.hostedemail.com (Postfix) with ESMTP id 4374E1A0014 for ; Fri, 30 Aug 2024 18:02:47 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=noHfrZUT; spf=pass (imf19.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.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=1725040922; 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=DG4FqLRVN7QRhtpNFNyTot11ECNVu7XseshInxYcE2s=; b=hzwjJRLBQmj9Cw4JZCnmZIWZOioZx70ekD3OpTpATUizSiUX9XlrenHdBb0tpTS3bLHJEa 8g1IgEuknTs2p/5NTJHaO/eU4FEEdVfqy1BJqDLsxI8ptpAEzreFTnISA00rG+/q8O54Dg UhVlC5r8x9h94vwKe97u6ClTSXf81Kc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=noHfrZUT; spf=pass (imf19.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.173 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=1725040922; a=rsa-sha256; cv=none; b=Mualrs8sZvtERBNLSUb65KKnn1pzcM4gpHuHAvz7RYuiVIXFug39dnYPcWNBAiDHXu01eI 0HqipdrFviJHFJUcHA7YKDk9ZbxC4zBujuw6DLkjYYK/Pp79kZz4XrULmdhpvZ7sQNq8uN N8DCycZQetd28fmEknPGjgJivpoLpsw= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-71423533ebbso91922b3a.2 for ; Fri, 30 Aug 2024 11:02:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725040966; x=1725645766; 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=DG4FqLRVN7QRhtpNFNyTot11ECNVu7XseshInxYcE2s=; b=noHfrZUTQlntstUeEq0bbvUeNFu+VVRv5cMag6FhpWSeYtcKeqWf/lzF/sk74Nbulq eQAOX58r1ocBPxDrWU6XZHvylDlRShRP+AtKgSNRLKsonOrxc0pBmDBzKBP6FokUKsJf dKIfEue5c4ovyJvmlHaF0d9hrQTChroxFIhXg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725040966; x=1725645766; 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=DG4FqLRVN7QRhtpNFNyTot11ECNVu7XseshInxYcE2s=; b=mUsoNxSocfnrU7nuGBaxXyGkizBbyH5M0km09xf1M6dqg8uXn7PopwJ7lZ1L15yI34 d25jG1lmNcDmwP0ouovnf3MPyuzdq6DyAWYZ3z2GpyUHb6au/6fhlqAGXsD8KCRVuGO7 4RVUfbQrtNjUu1FusqI9DCFITDcAZRkEQDl6WzIW1qoWFHE2Yqd3uB++hh2cp0H3HqG+ 77RUktGzp64+OaEeH2nixVAq86MkgyHFuyl7BPPSGSJalrbqaUQN+OwmleiOQrBIWGun XwUEGqRILVbIBKMezbQTaKAzo7Ojv42rgwBjpXaJfXCFkqYMXE5dJy5ZAxlTij5DRRBj Dq+A== X-Forwarded-Encrypted: i=1; AJvYcCXUftAZYZDSL+di4/hGDDR4bzQThOCY9RuyRbgj3wfK7eVjeUDlaZC5VpmVUdGhxkZkyHWtSF3KYg==@kvack.org X-Gm-Message-State: AOJu0YwVBOnWKRnlo+IgMe1GBWHu+mIrr1+c29SI74uhaov09jqgV6Ft m9OxtMIVLsGvbhKM9M7hti60Q4NH3fqVYS8BbOtkqN4KfmZyputATUsp+LDsAg== X-Google-Smtp-Source: AGHT+IFFGS+07CU8IBOM66L6oaRfXsetQe4pywdwlw95pQS95a2NVA/2F4lCrT4hvMrc8/Ywtkgvzg== X-Received: by 2002:a05:6a21:6d8d:b0:1c3:c1d0:226 with SMTP id adf61e73a8af0-1ccee48ec07mr1706297637.7.1725040965460; Fri, 30 Aug 2024 11:02:45 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-715e55769c6sm3051663b3a.33.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 4/5] selftests/mseal: add more tests for mmap Date: Fri, 30 Aug 2024 18:02:36 +0000 Message-ID: <20240830180237.1220027-5-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-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 4374E1A0014 X-Stat-Signature: 4z1noabk7hw648ybiggmazg6nixo83hx X-HE-Tag: 1725040967-503208 X-HE-Meta: U2FsdGVkX18iznADP2pRRBPkPKYZsggQctWjy12oZjihy+AoMjdcwY7rHgfu95XnTE545zyUHm0o3otS2X4PBA9D4Yw6xUzV1B53yHiZr7/G9v1Hx/RMQx2+Bynf2mcpnY0FAdXhpegxJZSnPE7q1rQrHM42PZOpo/CoOLxccbIa4zxV+HXSnajEa/rS1sugdYUHmVLhl64Bdj9tM5+5GvayXIB9sZq35u+OSoyQAQ1CgTQWOAg7dqgo98coFjnzqskGjdxXkWvFCio92OPi6TMTjXXIJNryMi9MDSk0Qa4GmRJGZT8Tpi2SykBLO4zjR2nVAUobOtDvSuky7wbvg1autD4y0fMxFjohWKj2kjTc8NR0A3i9FWWei3E2tR7a6n8UAth5aqD3sLeru3P2n0xe4rcni9HtYa526j1i6XKokmRqAosH40GvvHGy+lLNxMfYAw4ZAohOf7DT7Y2eo2ZfpIet1Lu3pyNBznITOi9hjukqJJY7sitUJ13jGtTjLh/eqjqJJahE92MLQ7Dkh9BeyjCt6SnkmjMVaie8hh5wZYM+3Olb7FRvBEcJfT22Lcy3haihMsVHzS0aMSXaaDGu5OWUcTe9207+j1G9BEC9YTxQe7PjJqIFO8gOousi4Gy38M+zGrNGyOLZime4oitaOjmivItcupVo35v7iTQNGYiqopUyB2idKCoSnhZPDBx0qI64zacn9gIdxJZWv45bTmvrAVw4eYQdX8CNmkhZpy6RebyDDoikNtMT5tSIxGv+3LiYU7PHWjwShF3n92j6Vcp+Hz12m8CZT2PEsZiLRTPipMEjW8GSkQGoXLhsqu/VgSFSVMmfler9NckqPpn2ibGOawdgQ2PU/vHpRHghzESnLo/Jm/aEGDX4CSd3nFazXx5ycWUPFh2E6z1TPFyiLbB9OYJSpxAUeFi5N1FxWbWcRqlPVnT9FOXTS1kSl3VRWTHDoIhCAgyNTTX 1ymWTys7 SAkc1Gsyak/uoQXGzrM2jc+9Q005cvpI3Q6W8aMMOzMQ2ykotUXeQoN49N5sHQtTaiTxbln5Y10Ib0HdbFeFQk+F3ucZGSmn+NU2sTfQ0uxVQwSzr8H+kUk1biKA2f/e3sJ21D7Z6vENS7bf3J0uqFX3+DJiI85/HLZJpM7Pxl/Ms5rEn5U/E9kqZUGTKwEdKBuBJzrptUZSoTQ6KRqaxemRfN9ZnNrZPTBZDO1SQJmS6VMV2o/2X2BuTX4oWZNHA+Ugm17aVyQgSfXlah4XgniRt7c46tWv3WC28zGC6FSErXjMLIJBFdEhH2G0dwyhPo+se0v+9LoJMCi8= 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 sealed vmas (MAP_FIXED) Reuse the same address in !MAP_FIXED case. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 126 +++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index e855c8ccefc3..3516389034a7 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2222,6 +2222,123 @@ 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 and overwrite (MAP_FIXED) */ + 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 and overwrite (MAP_FIXED) */ + 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); + + /* MAP_FIXED is not used, expect new addr */ + 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 +2360,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 +2474,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(); }