From patchwork Fri Aug 30 18:02:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13785414 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 9EC1ACA0EDC for ; Fri, 30 Aug 2024 18:02:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 487FC6B01F4; Fri, 30 Aug 2024 14:02:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BE4D6B01F7; Fri, 30 Aug 2024 14:02:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 148926B01F5; Fri, 30 Aug 2024 14:02:47 -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 CD42B6B01F3 for ; Fri, 30 Aug 2024 14:02:46 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6C8EB401C4 for ; Fri, 30 Aug 2024 18:02:46 +0000 (UTC) X-FDA: 82509682332.20.6BBE566 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf13.hostedemail.com (Postfix) with ESMTP id 4D2DE20031 for ; Fri, 30 Aug 2024 18:02:44 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=c3HCbgtf; spf=pass (imf13.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.181 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=1725040873; 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=sAuPAhpR0ghb6Pn/oIDgZr3XAmYbFrFmC+BmGo7f9vo=; b=RDPCXB1L6L6z8qZZya/9ebJxJUzxXsrgz+nN+C0a5StY3BS9YYzOKS++7ueZFw+MG+d43q mX3MhY4PPVI+470uuuntp+DgOtnGMJqswAnw+/d7vPoMJllmgp42m2o2mJHXyi3IJc2b84 2RB6wnxB/tXjAhE64jv+u7vkTo015IM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725040873; a=rsa-sha256; cv=none; b=1T8IxFj6lYQONd1yJJf1HBfgkqmdEmcaCAfCZ9e2d7mHXWfreNzPJZG88MyF3Jo9B74hhc D9Z22ucX7HXVrfnK+Z9f7hWZbQfXefbC7TsToLwKWmBkmhTwsrRqPqecHn7f+/VTTl2G8F gREMlfj5WfeOuYcnmaYjqQ/jLGJVDa8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=c3HCbgtf; spf=pass (imf13.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.181 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71447da79e1so82588b3a.0 for ; Fri, 30 Aug 2024 11:02:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725040963; x=1725645763; 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=sAuPAhpR0ghb6Pn/oIDgZr3XAmYbFrFmC+BmGo7f9vo=; b=c3HCbgtf078UPPAmHdXi0zsaoaFufZewFaC6y2z07fmpG/ff3umqhBfn7HrGx1bNd6 uElqcVQ+6q5Lh9FHxXzG3wMLsMTbEBvL/Y+b4KByPGboLzknbyfMBPqfzUF2bi/T9AMT vo4axPIVSUDjPMxYaCkPk9lmRb5mxG+vMueqw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725040963; x=1725645763; 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=sAuPAhpR0ghb6Pn/oIDgZr3XAmYbFrFmC+BmGo7f9vo=; b=xJ0sMPpvVUaSHY/XWGsQrUnKaTic9l/dmzJHbxL/T/RMuBB8+hZ7PVBSeJNg3/rha0 qQaVzeClMC2o5iGB5cQOMoOoo9g9iAGVvjxOKUNizH//ZlwZrc+gFLkU6BrcT4uwLUyh H7g2k9DeNE6W4pdRFbnbbzEV/sM08GNapryDYqkI/tq4zwie2ESMkpPz0HqZ7imQMVAu kFWTojfy+cxjF80Oo43OpDTnNqN8OXor+sV/TwGZJubsN2GlIRvjm7RJYlnQNI0uZBUn rLUPOdmA8agN6kwusJ3wRKn/T2OnGnc33KcEYOkgTS1ShUV/LpcVswdvuDkiWB6xeDzu 6L1A== X-Forwarded-Encrypted: i=1; AJvYcCWA5gygc6zwDIjsOHfvsWrlFEp8jtsMLe5cIBCqPBrGWqroaUbAJxH79nkkPuFfZSU+PXG2MBjRQg==@kvack.org X-Gm-Message-State: AOJu0YxNZNJSPNJ/LeFrBnGFwyHE1c5rpcKhXyp+eHpRCZBMAyjoaO1F ZktXHtw5lgz4E1smg3oGFZeQ5UZ+ZT4YO2mvDxhO4DkFQP8nSsVYtEzv9xPq2g== X-Google-Smtp-Source: AGHT+IHXhd/ntf6xjL03WH4K2Xgnu6oCFd2q6NEHWxdWLQirlAtwyd0QSPMAd8gnR3mPfhxjRDuS5A== X-Received: by 2002:a05:6a00:14c2:b0:714:2198:26ae with SMTP id d2e1a72fcca58-7173078b21dmr1711491b3a.5.1725040962275; Fri, 30 Aug 2024 11:02:42 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-7d22e771c48sm3304251a12.23.2024.08.30.11.02.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Aug 2024 11:02:41 -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 1/5] selftests/mseal_test: Check vma_size, prot, error code. Date: Fri, 30 Aug 2024 18:02:33 +0000 Message-ID: <20240830180237.1220027-2-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-Stat-Signature: i16h4i8u6n9a7mt3oej8xt1osmu4yag7 X-Rspamd-Queue-Id: 4D2DE20031 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725040964-513616 X-HE-Meta: U2FsdGVkX1/pWPe004DdSrORhkm+u5CsTpV7pk17zwe3p6dkjZp7cTHS0ZZtq+sBhJuuIc1Coax4bK4NSIy8LsCXL5TkTGU/hKJSJYuu9nsU3cwWpYeG6KLPJIFPdxPvBS5yR+jd+iQ9B5ntX4g1G35AVY/iY42jdv97nxhwSTQcSjfmrfEvX0Ua9fIBOllmu+0PrZZp/EjzeG5tuyaKCpvbx7wDYIjagmyXg38dqqwonthgD8GhDAyLSKlyfXRWlgNjsyIrB1WMS8eaiVkQRxa7JIvDKLeJ+Otvw15ERqrnjZvtd4eXliA1SuymRBDFB4UYDpYmoAiK1BhaI5ry/oxvpmTOKO6b4YK/6BxMPSIDEK0M9wbKdr7XKnRzcyQY01JrycdaYOIv6maXETQN2GFciDdz43JgwHTkWrIRFi7Slfr1wmjdZJ9LmDa6qjg7XRZ5DBoeV+EDhr/UowhRDQwE6DL/CeCAl6TjxPTyvuo6o8TxVv9hjQBZBSuXmPtX7dk6q5CYs/Yt1ny+WbxVq0WEYrGoZc8drDnumbagZuILlbeEYOOwJnTiUXTZ3Xm6dQPt7jYw9IoHMEwA3Lm540XeTFn64dXmILDiJlSrglYYV0cvcOfVSMq6kcTangDKP5VPjQQmL/DW5V1bck5d3I+FhVzkEu8muxDcg/QGK+GDPC4eRtywcRyLnRVbsxch66rHlg3YLDOw/d7LlppiXESjeDBUmQumWpfaoWu56FCCl9qEjAER2Jxa1/68hVprMRhfiUz5bmL3ydBhQZtBfy1ij1iHGG7S/iGyZ3uc/KJSW+S7EsXHiYUIQ1vw2ZmWh9m04pSFJPLgfOtj0SsJePQxAQFCozNT7oZrh9BM4qpIWXPQw9dbIgcmFhFb+DLR7B+cjCRYLoqTdhYXqLiwfxyxtchBITf+zLhdZ67aYtKAGR6OZQjhEAZqkPcM3sx90eMbnKgc8Zm31xTvKTr sD6y2EL7 QJ1RYstQVpsxev3jnzeyIYAVFpL0oQRP+Qzt5pZtPMOhfV3NC67b4hv2QK3fc25RIY8EVX7wUHA4gu0+O/xUnUS5HPiexWbPuJaRJkraqkWM8zSoqKTsX1GP/ztcJRQww0ar0OLX0Bo6CAl6eZTWXMfYj28TZlSIguzzaCOoOCDk4O/n5nPWPdvgRCEYXZsj+PJdeeDS2ZojXuSmOZHO2whOBVz/B0wlgBvB5qciblTBY3rfIFWGfsogEyXdBKr66qspEkJC7VvelSDwikJDgNvb1OTCxZdo+MUIoC2e0zZm6PDjFv2MK+0HU8iJgNb5c99vvrIXiXxPnd8Bi5nXKk+FguXyvF2Hyq+CDDr3tkmeAUmKWdU8iY8qU/p591/zfpt21 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 more checks for vma size, prot bits and api errcode after attempt of modifing sealed mappings. 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..7198f2314f9b 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 *) MAP_FAILED; + 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 *) MAP_FAILED; + 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 Fri Aug 30 18:02:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13785416 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 6BFE1CA1009 for ; Fri, 30 Aug 2024 18:02:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 320E96B01F5; Fri, 30 Aug 2024 14:02:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 234B46B01F6; Fri, 30 Aug 2024 14:02:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 05F356B01F7; Fri, 30 Aug 2024 14:02:47 -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 C0D8C6B01F5 for ; Fri, 30 Aug 2024 14:02:47 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5FB0C120321 for ; Fri, 30 Aug 2024 18:02:47 +0000 (UTC) X-FDA: 82509682374.28.58FCBD5 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf05.hostedemail.com (Postfix) with ESMTP id 4AD9D10000D for ; Fri, 30 Aug 2024 18:02:45 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="gv/looj0"; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 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=1725040864; 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=O/lLriAP27R5nHPgsK9ypk/521J0I4x/lWwi5ADQ+zA=; b=FCvYxX4G4G8lwGJ0KRUZgFjORGHlDhuVZBABqLqBD59wpoVK0JEDdLOJprv+ib+TRHBemQ 3Je38BKRamQ24EDU9QlMoieAoLRWTT0l3jwLCoz+bBR4+7Re/FRCPws4iWvY5kfZrlLBqq U582QNvnH12QH9rhhahYUuGfqYmoZHs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725040864; a=rsa-sha256; cv=none; b=O3lLH5NqWuv//hxq2FEMzsT/rM66Sw6GQrhL1LPJ9Vir5PhHHLLYJN9qPtEuaw1MCi9fJW SAKFvesXqWIbVuTrNTKeel9z9htxzwzD6gFv/tc/DFnPVXCTeveIDPr+HKdZLcpZ9h09xA 1aiO3AktDrnx+W+hwuOoG0dYPxiKYjk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="gv/looj0"; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71420e66ecfso219156b3a.0 for ; Fri, 30 Aug 2024 11:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725040964; x=1725645764; 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=O/lLriAP27R5nHPgsK9ypk/521J0I4x/lWwi5ADQ+zA=; b=gv/looj0fKDVRO14i4xstrogh0gDy3WitwUxk/Lvvqw+hjqXpBp6Prw5ETrzZbw9j/ v9I799QImya/AWaLliyncRgnuQULkPZTjKXzddy8ifpTvAHQujEtcRReYPywHseO85sZ LnfhH860TOpxKuF91jPd40Wb2ftEoilDu2sYI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725040964; x=1725645764; 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=O/lLriAP27R5nHPgsK9ypk/521J0I4x/lWwi5ADQ+zA=; b=sZNuvxSQV4mi6Qepd3SzdsqTDM4H0bcxxUyRvb212kLmuhb2DfiR0h6NbhNGC79697 BRpvpiuhfZ23BMOOYjbTTim79pASc54jX6/N0nArUH6SuLuGRkuCnYbie4mmPRpV77f+ II5zrZxcQomvuALBqsfeBJJQpPuOyCPcAdlo54xqM5Ne1F1wM+UnVYnWQhj0WGDpwGcp uEN8LjrCbiX+gA1Wesy/eD9hx4NJakC8C6JatPL7dzLOJOfTXrT99PiO5FrGzUcQJd/F GTsR6tOYk4yXfKzo3a19KD7UcIZ8c+nZUy/eqVQGHwXdvWUL4yF0gZp0pnOjhQmtx350 mzYg== X-Forwarded-Encrypted: i=1; AJvYcCXIXFj1R8qXO9pjcIYDBCq/QvHkhF3J4riwaY/dNdJ8Is7UhmTofZbUOvrbmXRCBcApgMZ0oBkaBg==@kvack.org X-Gm-Message-State: AOJu0YyM90SXq0MvyQLt+ggvqB1+vv9ev0yBb5vtvMq67dKFWdU/xOPJ 0RbYqHmzSmvHFlQzCgSjPbMDLoym8AZQFb5UFSpHlk3+05flYy2BuKAgHSGwPQ== X-Google-Smtp-Source: AGHT+IF7ss5vHuxDy95Wgb4WUGPyZvgp4tHfy8UO1MWvFABXKWvo/BydPzQQ6FPcavBSzZ5iLhUHAQ== X-Received: by 2002:a05:6a00:3e0a:b0:710:5243:4161 with SMTP id d2e1a72fcca58-7173078b724mr1724291b3a.5.1725040963782; Fri, 30 Aug 2024 11:02:43 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-715e575d087sm3037827b3a.207.2024.08.30.11.02.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Aug 2024 11:02:42 -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 2/5] selftests/mseal: add sealed madvise type Date: Fri, 30 Aug 2024 18:02:34 +0000 Message-ID: <20240830180237.1220027-3-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: rspam07 X-Rspamd-Queue-Id: 4AD9D10000D X-Stat-Signature: hofh4omgm9xpw4c4xeruwoaed8kkun4n X-Rspam-User: X-HE-Tag: 1725040965-257858 X-HE-Meta: U2FsdGVkX1/3bUjG8Qp7cAPzIoI4WRB+/KJLDgJSzusBjYVpzlxLqZI4/vDv07DcFhFstb7Q99sFlrxERhDfQ4onnsdP3VIwwasPeSkwAsx4xLUp9hZ338n/nVDyf0Qdy8Sk0TErBSwVgNPNfcWedZ8XpSKYPKP7nmZtADvUB7fG0Iu4q27Y5kuRHiQryPL/OhVjbhd0SMOUo1MAxQcJbxyKNalQR+ySxgE2WIbJlmAdIa1IrwXz+zZg90KU62PFR1ILPTthiIFNp10eJFr2ZMk8dFWNFIuaEM1NxB3V9Q7j1TtfkWdsTk1KBoB5g1Yzhyjv8eRyd5DWPHvtr4juyetJfI3oBvxbddRS+PPS5wzurHDbM9YNxJNgb3mjuqZkgRsJDUNyAx1f3L4CIT4eOs0nTv/1FyI/qYERKa1cdhYq/W/UKOi28YIfbCHaNsjrPJ/HAfceno5lbzGroWKXApvAnjbp3+XeW1edwuyVaafhbOYBoG/t6NXrM6/fSZmWGCf7bSE1hoMRBDaHh1GMTUXB3LnQfjNDoA6u/hISKpPVJexNPWeA/dhy7gbUgv8eSj/ACy67NCGeC/Ru7rfv9fmsyFqx9cpOwTyVS5qRFogrdBJ8GInyW2ZIgFM1709ZYMb6DPZn7D3l1RhjaoCSJtWdV3PrZAdMabcWhPca18dq5imkmT2qUrutIpUG/SFtPgwVhoLRlL/akzfcAOBZiXQ4PPJqpu62rSlzJwkbbO4grFP5vGSdq48Edx6zVtmp3OUGPWxEl4/OxflLXpeggOxHAfO3ZixlQ7TvnU0FgCLFmrjOnLkX3xS2t/zfUljYED+AInH5KsAKNkKolnXBlH1xXcWTuIbBYcrsZmI7Vx00wjYBwi3VCgO99DRBMZlBE0MnXxpnpZOZ0WRb287wvCLn9nmgSh3hBZiBQDGUwAzJ+QfjoIp7K6peH93JFj0jzB/lNZlQAneSoDIpFBL jP74F0Nu Z87Y5pA2fJkgMKG7xspO6khq8iaTBOtkLeTYeYbJZBZcBlHaEYyQNv1RYUYr9Y7bPVh6sbziX5rhUm8Z5MdAldYkjLC20bnsGC/lwlZL9AOPD1lEMoKrOthkPOhFWn6rCzYdqbjLKWmAyLygKvk9m+D+WMkrpqTXBUKic+Skx4g3Ye/tuoyxti5PMsFAk/SU/579+orubkWvDhNsJI1pEbP8mMBmJ6igBMP8byy9Gg+Zm16zktmpCBScHVoxeoT69pThn+unwsARsJAK27LLbyYetOhDGQKwUoTt0j/KzT2iBTcxIHrDPJR5D6OAkncnsBmCwit8RXP58guJIFzX96KPPaQ== 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 | 30 ++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 7198f2314f9b..6d77dc9b5442 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2121,6 +2121,32 @@ 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(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -2142,7 +2168,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(89); test_seal_addseal(); test_seal_unmapped_start(); @@ -2251,5 +2277,7 @@ 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(); + ksft_finished(); } From patchwork Fri Aug 30 18:02:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13785415 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 B66BBCA0EFD for ; Fri, 30 Aug 2024 18:02:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 012A46B01F7; Fri, 30 Aug 2024 14:02:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EDD756B01F9; Fri, 30 Aug 2024 14:02:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D56736B01F8; Fri, 30 Aug 2024 14:02:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9F94B6B01F6 for ; Fri, 30 Aug 2024 14:02:48 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3A8541A02E4 for ; Fri, 30 Aug 2024 18:02:48 +0000 (UTC) X-FDA: 82509682416.18.DCCCC32 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf20.hostedemail.com (Postfix) with ESMTP id 4D9E41C0022 for ; Fri, 30 Aug 2024 18:02:46 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="NhJO/LsI"; spf=pass (imf20.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.44 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=1725040921; 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=iKGNPxNsYKipWJTu2FnzfGVLgyBodzOHMPjLCFGxD/E=; b=D8hv8uuvDleQ7ZwCuawIDFad7tIU0TXTgJ1mXGEYDRaqBqdlsPqTA5w9afxrT3TVtM0Ixf KX7HLsdj2TcDS+0XHdEe+SyghsRqn+rVmuOTI84/7BsdN/CH+v5MDR/bq0/2xg++hsSjzD xtvb7T9nH4/oxTA1aXsXXlrlAEwQPU4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="NhJO/LsI"; spf=pass (imf20.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.216.44 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=1725040921; a=rsa-sha256; cv=none; b=qi1wkUZ6AxejJWeoWro4ECncr4nk2otV9/f9O4/V4z4XGqvJ7B5S+GILubB0XPiuEIatU9 RLgN9ZJ3l5UNFJPrHsdV9unQt80T17bOqu5amQ3WnoWmLMSGbcvf3SNCJ3wNcQOIAz7sq6 mldKmBY14Z/cpK3FvARbXOWEQctVCqo= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2d88ec20283so18664a91.0 for ; Fri, 30 Aug 2024 11:02:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725040965; x=1725645765; 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=iKGNPxNsYKipWJTu2FnzfGVLgyBodzOHMPjLCFGxD/E=; b=NhJO/LsIw/A8j8jhLuS1Rcdr95ptds/mFiHLUa8HNFYIsCWC2Gsy3YWIsyEnoEyXY0 W06ka7sitsaaEhmweiPeOxc+UlM/9u4AiftPAfCaAoVr9p9FWqJvn6LRvaxnpT0ZtgHt sup1Rx51wcVFBVsctsvHS+9VYgh+2DwF4ranQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725040965; x=1725645765; 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=iKGNPxNsYKipWJTu2FnzfGVLgyBodzOHMPjLCFGxD/E=; b=eV+E//PXmGsZ1XBaPcDsi/6QlcBEtMFOjXzkI30OcOPmFjyVd7vGmX6trMi5qutR4I XSEh5KSFy8lBCjl1/vPN1dhFiI5JQMNCsbj0p46+su5R377vnZk98PhRkV4Kkz5eDCy1 Xk5D6W5zvTAkFDInSsOGmqr27NFdzF5si76eo5ZDfZPZbNv9MtGRqd0vnVewN3f8at3/ U2SFBArqMnd/GVP9GamxjJEkUNf2LPqteCi8bpHmC+H464k2bC1Bktgem+Br7xWrDGll E9EneZsgbBEEeDDy+ZpTtqPT1k7cpo03sHFh8WyevAlUTAgT9PHNLCVjwwCqHo1AYJNF icBQ== X-Forwarded-Encrypted: i=1; AJvYcCVdYZq0fJlQN8Gg99/MZJsnmdKyz8IoCK0UYdpozFmRVXQ3clRqk3PN6Eoe+VZrrjoHslLQV1NqWw==@kvack.org X-Gm-Message-State: AOJu0Yz5k09jqpkpAR0p3q3fQ/SzJSZsxETSuqwleiEkeVg1BKM3S9jo BsPPrCxaKC9Switeo/bQhh58EAL8Tb0Uq7fVHqgUdKxJqNUOxFjsom6Q1zEMmg== X-Google-Smtp-Source: AGHT+IEihnCD63EfKi0DOVXqIMhOWH4VJANKaavsJJCvNs5Q8uboJudceTAmj4oEGAUcSNs9tNCDsA== X-Received: by 2002:a17:90a:d904:b0:2d3:da94:171 with SMTP id 98e67ed59e1d1-2d86b8c3a42mr1724919a91.5.1725040964619; Fri, 30 Aug 2024 11:02:44 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2d8445d5bb0sm6770604a91.1.2024.08.30.11.02.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Aug 2024 11:02:44 -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 3/5] selftests/mseal: munmap across multiple vma ranges. Date: Fri, 30 Aug 2024 18:02:35 +0000 Message-ID: <20240830180237.1220027-4-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: 4D9E41C0022 X-Stat-Signature: h8yfrkatixbrm7mf4i15eqd3gzbeqqsz X-HE-Tag: 1725040966-777953 X-HE-Meta: U2FsdGVkX19IgoOLMij0BBN2MKfCeSOosD4rwakDGjAQy+xE0+dcyg9e0IYFK694ss7w/3aRaU9xK5B+vnm0hUNbaklVxyIcJmVlJy2R1HkYnB3L6N34bLi77+3bXrlIOebabj/O2rsFf0N2Q5Ca5cYzEcNiIPJr7p7cLoyooPr8UGHpvfHgaguyTKJ8rno+YOVtKJ0hKgIcLqNbbwk3lSlvC0qDBQWdD0FBP4IzL+1YqkT6jE8ZADATzlaCkOVkogJ28ChpjSE/hLJSKh6GH/nIUkHKya+S1SB9V9FX6lzu6FDXYeeZ4y2h/VEhmpRqMZihH9E08o36AIFBlFoFX4oBfq4Z/mkeYgqGpZ1usSZxMJfoGHI3RIDwBfzWLb1tg3wBK0qQTLky3SXdF43XJNNpS4uK8YpzdvzwPagtIgWxDVIgmXqq5uyMwmAwIcAwhZT16qvGLulvrZRYGM3QVOiWP/GalDY8/7kfAmjHo4BaUmMm9bvZjJx32L1tpj1FeeO7eUTjK5mNyZrM/4uwr/isI+Y5SdTuNsoKU8xoqJ2dAZzd5GOGvJdBao3YnfNMmZZZD9PRibOEBIp9ZWuxeTuKHTwT5u7L4uygi0HfXbNKNA1/Sdk3oY7AgBIzqSHwl1C9LUdWIwoB/8Hw9emR7tbeDduoZ3tfFSUwdMFqb7jOcH3heGhrPGtFZlMncIvjsdo1WKdTJHGp9a4xVC0fMcf29xAjN1lEKjFATicckSABJt+coHs9a0FjCfP5+KnYd9dGsxsNLMWYgGAb5dtWzm8HhnhmikZA5pGkeFK5wgriyjvP2TMS9j6Y/fyRcO0+B64DilrPeyxcLKh6pfxQ5tFxQZOcBRbCRDDq5NyZLQsQSAck8YdQ3vl79b7IYT89PS6WJbq0er2dLmeqZSG2lzC39fjPR5cKFqHHxkmvXtlwsx7CsJ31MXVzFYgW/bqBWkpmfYnHEnrW8t/hwdL 0rWWUhHY CpJhFwV+DEs1tjb9NN672m72RxzRl1N70qFALyCdsxSJ3/3oJLDRykjH8MAMRYPcFn1ILh9NtcgAFoPQCq7cD+NKRFq0sfhPB3ph3mE3xkoZ+WKiIWKJh4/nlZ+ZYZwWT1/FOUzp6mkqTeqiodO4BTgIIeGutpo0baV03cWjuniRzCLuzJ57v8OPe7dcKsO7YPFru8QMjQr1n7Kzu1gUti4tmacMLwAa/lB3OFBF6kmeVPt76x1CsQfnXdOIkAfATYLkF7l7QF8llJDMLoNYujhcXmaH7adORo0VLib/rCJzhVShzXJPwS9P0nLRX+EZ+yBcWFkEvcEFwcfOBlyaZXhVNHg== 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 test to munmap across multiple vma ranges. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 80 ++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 6d77dc9b5442..e855c8ccefc3 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2147,6 +2147,81 @@ static void test_seal_discard_madvise_advice(void) 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(); @@ -2168,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(89); + ksft_set_plan(91); test_seal_addseal(); test_seal_unmapped_start(); @@ -2279,5 +2354,8 @@ int main(int argc, char **argv) test_seal_discard_madvise_advice(); + test_munmap_free_multiple_ranges(false); + test_munmap_free_multiple_ranges(true); + ksft_finished(); } 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(); } 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(); }