From nobody Thu Dec 18 23:47:13 2025 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DA4B1BA888 for ; Thu, 29 Aug 2024 21:44:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967848; cv=none; b=HNQADKffykvrHccWwo+TxbxhmnqNG4IX6+h2qS00PxL6ykOda1zMu3SM49DgInaaRsR29zLkFFOJJVuk8k7ChhOQt6FbCAMALGvDQ6R+iTH0rcm2pQ7vi98tp/v3Q2ERKiIrFTGbjCgh8oJOXMWwdvPSpjDz7xJ2t/WZ/Ez6VyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967848; c=relaxed/simple; bh=apMHPNI1yPMaoYfRUKKhhBb7LeuUcGyz20qs9PeM88Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a7rNpwDOvxD939bTepTRcLQQQMgdorcicWmurU1tGRsuQ1liKtMDsG+/13RTmWJPk3hfBlUdZ5fA7eZ4mpsb/cUS5Ak5pY0tNemBAF0gSJz0O6t0NxH4Ipmqmv/SsuShahThOVNpeTAeXW6x1ddule4DYqW8U+chrLeDcnWZ6TE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=P7Sfqjpz; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="P7Sfqjpz" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7cd894e2adfso132337a12.2 for ; Thu, 29 Aug 2024 14:44:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967845; x=1725572645; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EV36Q5Y79tUSIn32vCscn3phXv9vAn9RdQBx5GFOQFk=; b=P7SfqjpzayJeGZQwjX8m9s55w08FyW0NWH5+QZc54QS2U8ATByHntp18V68hOV5bmQ P2BRFAMjBIMaDG0vvbdk2iPjlP1OSqdB6YfsiS4e2eyKasrpVfHp3tLpJFlArmNGgUxP CBCkwnuvDe2gn3Jbmotv/fbTked8rPLZNYJ6E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967845; x=1725572645; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EV36Q5Y79tUSIn32vCscn3phXv9vAn9RdQBx5GFOQFk=; b=cpG3EDqSyymMDbshCNUdDgpo8XCov4HqXiQGuM4+suE9nUl4+67yxBYqHYaJBKV2o4 qC9Q/fMzBJLEGG0Y/o93Uk1TU6co9ZobCYITdSRLWBEq2s2hTc6iYsiAGcJl/m3eco1x tZbklxUyJv7pYscqQb2PTlppcacvYg9WjIbqT02HdKrkALWfFAcOPvy2Ug19Zl/LSFoT 4pjhm40lhK+ZOl5xftYabgMi2nip3Ke2MxHiEjLmnYdirRZ9n0HawUJaXWf+cr9KxXUL nKk7i0xd6PW+CxxESiN2R1OERwKVcludcfD/YWRDD6fdU4vp1cEjBcE4IZb09vKwo+yP +khQ== X-Forwarded-Encrypted: i=1; AJvYcCUH2ODXDgbdGwvjX0cb/zGv10C5V7RSdWWLvaIOiw2QkmKtsI5uSAkUQ6WRp9we2mqgws2OUC0GGN+AdIo=@vger.kernel.org X-Gm-Message-State: AOJu0YxOnXC2eH/BJs6vH5VOAo17dfof/6I0EV4IZsYT/J55DeLFof1P W695DcgMpHQB0ISheYeFje5JZeH2OIt7P58aGsRQsc6fxabQHXtOzTMF3CGFiQ== X-Google-Smtp-Source: AGHT+IGXS/7yU03nYXMN9eJKu2hQiu0AShJffAnSYmN8VBCAEYihBobOV5g45a7+u97gPxZ2hllOuA== X-Received: by 2002:a05:6a20:a125:b0:1be:c3fc:1ccf with SMTP id adf61e73a8af0-1ccee3aa41emr204386637.2.1724967844597; Thu, 29 Aug 2024 14:44:04 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-2d8445e80absm4717791a91.13.2024.08.29.14.44.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:04 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, pedro.falcato@gmail.com, willy@infradead.org, lorenzo.stoakes@oracle.com, broonie@kernel.org, vbabka@suse.cz, Liam.Howlett@oracle.com, rientjes@google.com, keescook@chromium.org, Jeff Xu Subject: [PATCH v2 1/4] selftests/mm: mseal_test, add vma size check Date: Thu, 29 Aug 2024 21:43:49 +0000 Message-ID: <20240829214352.963001-2-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829214352.963001-1-jeffxu@chromium.org> References: <20240829214352.963001-1-jeffxu@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jeff Xu Add check for vma size, prot bits and error return. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 398 ++++++++++++++++++++---- 1 file changed, 332 insertions(+), 66 deletions(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selfte= sts/mm/mseal_test.c index e7991e5fdcf3..adc646cf576c 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -170,18 +170,31 @@ static void set_pkey(int pkey, unsigned long pkey_val= ue) static void setup_single_address(int size, void **ptrOut) { void *ptr; + unsigned long page_size =3D getpagesize(); =20 - ptr =3D mmap(NULL, size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - *ptrOut =3D ptr; + *ptrOut =3D (void *)-1; + ptr =3D mmap(NULL, size + 2 * page_size, PROT_READ, MAP_ANONYMOUS | MAP_P= RIVATE, -1, 0); + if (ptr !=3D (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 =3D ptr + page_size; + } } =20 static void setup_single_address_rw(int size, void **ptrOut) { void *ptr; unsigned long mapflags =3D MAP_ANONYMOUS | MAP_PRIVATE; + unsigned long page_size =3D getpagesize(); =20 - ptr =3D mmap(NULL, size, PROT_READ | PROT_WRITE, mapflags, -1, 0); - *ptrOut =3D ptr; + *ptrOut =3D (void *)-1; + ptr =3D mmap(NULL, size + 2 * page_size, PROT_READ | PROT_WRITE, mapflags= , -1, 0); + if (ptr !=3D (void *) -1) { + sys_mprotect(ptr, page_size, PROT_NONE); + sys_mprotect(ptr + size + page_size, page_size, PROT_NONE); + *ptrOut =3D ptr + page_size; + } } =20 static int clean_single_address(void *ptr, int size) @@ -226,6 +239,21 @@ bool pkey_supported(void) return false; } =20 +bool get_vma_size_supported(void) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 4 * page_size; + int prot; + + setup_single_address(size, &ptr); + size =3D get_vma_size(ptr, &prot); + if (size =3D=3D 4 * page_size && prot =3D=3D 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 =3D 4 * page_size; int ret; =20 - setup_single_address(8 * page_size, &ptr); + setup_single_address(9 * page_size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); - ret =3D clean_single_address(ptr + 4 * page_size, 4 * page_size); + + ret =3D clean_single_address(ptr, page_size); FAIL_TEST_IF_FALSE(!ret); =20 + ret =3D clean_single_address(ptr + 5 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ptr =3D ptr + page_size; + /* invalid flag */ ret =3D 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 =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -533,9 +568,14 @@ static void test_seal_mprotect(bool seal) } =20 ret =3D sys_mprotect(ptr, size, PROT_READ | PROT_WRITE); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -547,6 +587,7 @@ static void test_seal_start_mprotect(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -558,9 +599,14 @@ static void test_seal_start_mprotect(bool seal) =20 /* the first page is sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 /* pages after the first page is not sealed. */ @@ -577,6 +623,7 @@ static void test_seal_end_mprotect(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -593,9 +640,14 @@ static void test_seal_end_mprotect(bool seal) /* last 3 page are sealed */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -607,6 +659,7 @@ static void test_seal_mprotect_unalign_len(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -618,9 +671,14 @@ static void test_seal_mprotect_unalign_len(bool seal) =20 /* 2 pages are sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 ret =3D sys_mprotect(ptr + page_size * 2, page_size, @@ -636,6 +694,7 @@ static void test_seal_mprotect_unalign_len_variant_2(bo= ol seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -646,9 +705,14 @@ static void test_seal_mprotect_unalign_len_variant_2(b= ool seal) =20 /* 3 pages are sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 ret =3D 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 =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -678,16 +743,26 @@ static void test_seal_mprotect_two_vma(bool seal) } =20 ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + } else FAIL_TEST_IF_FALSE(!ret); =20 ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr + page_size * 2, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -699,6 +774,7 @@ static void test_seal_mprotect_two_vma_with_split(bool = seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -719,17 +795,27 @@ static void test_seal_mprotect_two_vma_with_split(boo= l seal) =20 /* the second page is sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + } else FAIL_TEST_IF_FALSE(!ret); =20 /* the third page is sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 /* the fouth page is not sealed. */ @@ -746,6 +832,7 @@ static void test_seal_mprotect_partial_mprotect(bool se= al) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -758,9 +845,14 @@ static void test_seal_mprotect_partial_mprotect(bool s= eal) =20 /* mprotect first 2 page will fail, since the first page are sealed. */ ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -783,15 +875,15 @@ static void test_seal_mprotect_partial_mprotect_tail(= bool seal) } =20 ret =3D 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 =3D=3D EPERM); =20 - if (seal) { - FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); - } + } else + FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); } @@ -846,6 +938,7 @@ static void test_seal_mprotect_split(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -862,16 +955,34 @@ static void test_seal_mprotect_split(bool seal) =20 /* mprotect is sealed. */ ret =3D sys_mprotect(ptr, 2 * page_size, PROT_READ); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 =20 ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -883,6 +994,7 @@ static void test_seal_mprotect_merge(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -899,9 +1011,18 @@ static void test_seal_mprotect_merge(bool seal) =20 /* 2 pages are sealed. */ ret =3D sys_mprotect(ptr, 2 * page_size, PROT_READ); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 /* last 2 pages are not sealed. */ @@ -917,6 +1038,7 @@ static void test_seal_munmap(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -928,9 +1050,14 @@ static void test_seal_munmap(bool seal) =20 /* 4 pages are sealed. */ ret =3D sys_munmap(ptr, size); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -948,6 +1075,7 @@ static void test_seal_munmap_two_vma(bool seal) unsigned long page_size =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -962,15 +1090,33 @@ static void test_seal_munmap_two_vma(bool seal) } =20 ret =3D sys_munmap(ptr, page_size * 2); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + + size =3D get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 ret =3D sys_munmap(ptr + page_size, page_size * 2); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x6); + + size =3D get_vma_size(ptr + 2 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -1018,33 +1164,75 @@ static void test_seal_munmap_partial_across_vmas(bo= ol seal) { void *ptr; unsigned long page_size =3D getpagesize(); - unsigned long size =3D 2 * page_size; + unsigned long size =3D 12 * page_size; int ret; int prot; =20 - /* - * 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) v= ma. - */ - setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); =20 if (seal) { - ret =3D sys_mseal(ptr + page_size, page_size); + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); FAIL_TEST_IF_FALSE(!ret); } =20 - ret =3D sys_munmap(ptr, size); - if (seal) + ret =3D sys_munmap(ptr, 12 * page_size); + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 + + ret =3D 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 =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); - } + + size =3D get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else + FAIL_TEST_IF_FALSE(!ret); + + ret =3D sys_munmap(ptr + 6 * page_size, 6 * page_size); + if (seal) { + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 8 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else + FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); } @@ -1074,9 +1262,11 @@ static void test_munmap_start_freed(bool seal) ret =3D sys_munmap(ptr, size); if (seal) { FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); =20 size =3D get_vma_size(ptr + page_size, &prot); - FAIL_TEST_IF_FALSE(size =3D=3D page_size * 3); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 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 =3D getpagesize(); unsigned long size =3D 4 * page_size; int ret; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1111,9 +1302,14 @@ static void test_munmap_end_freed(bool seal) =20 /* unmap all pages. */ ret =3D sys_munmap(ptr, size); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(!ret); =20 REPORT_TEST_PASS(); @@ -1144,12 +1340,15 @@ static void test_munmap_middle_freed(bool seal) ret =3D sys_munmap(ptr, size); if (seal) { FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); =20 size =3D get_vma_size(ptr, &prot); FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); =20 size =3D get_vma_size(ptr + page_size * 3, &prot); FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else { FAIL_TEST_IF_FALSE(!ret); =20 @@ -1170,6 +1369,7 @@ static void test_seal_mremap_shrink(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1184,6 +1384,10 @@ static void test_seal_mremap_shrink(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D (void *) MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else { FAIL_TEST_IF_FALSE(ret2 !=3D (void *) MAP_FAILED); =20 @@ -1199,6 +1403,7 @@ static void test_seal_mremap_expand(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1216,6 +1421,10 @@ static void test_seal_mremap_expand(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else { FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); =20 @@ -1231,6 +1440,7 @@ static void test_seal_mremap_move(bool seal) unsigned long size =3D page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1249,10 +1459,12 @@ static void test_seal_mremap_move(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); - } else { - FAIL_TEST_IF_FALSE(ret2 !=3D MAP_FAILED); =20 - } + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 !=3D MAP_FAILED); =20 REPORT_TEST_PASS(); } @@ -1264,6 +1476,7 @@ static void test_seal_mmap_overwrite_prot(bool seal) unsigned long size =3D page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1279,6 +1492,10 @@ static void test_seal_mmap_overwrite_prot(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); =20 @@ -1292,6 +1509,7 @@ static void test_seal_mmap_expand(bool seal) unsigned long size =3D 12 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1310,6 +1528,10 @@ static void test_seal_mmap_expand(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 8 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); =20 @@ -1323,6 +1545,7 @@ static void test_seal_mmap_shrink(bool seal) unsigned long size =3D 12 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1338,6 +1561,10 @@ static void test_seal_mmap_shrink(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 12 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); =20 @@ -1352,6 +1579,7 @@ static void test_seal_mremap_shrink_fixed(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1369,6 +1597,10 @@ static void test_seal_mremap_shrink_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D newAddr); =20 @@ -1383,6 +1615,7 @@ static void test_seal_mremap_expand_fixed(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(page_size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1400,6 +1633,10 @@ static void test_seal_mremap_expand_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(newAddr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D newAddr); =20 @@ -1414,6 +1651,7 @@ static void test_seal_mremap_move_fixed(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1430,6 +1668,10 @@ static void test_seal_mremap_move_fixed(bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(newAddr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else FAIL_TEST_IF_FALSE(ret2 =3D=3D newAddr); =20 @@ -1443,6 +1685,7 @@ static void test_seal_mremap_move_fixed_zero(bool sea= l) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1460,9 +1703,12 @@ static void test_seal_mremap_move_fixed_zero(bool se= al) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); - } else { + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else FAIL_TEST_IF_FALSE(ret2 =3D=3D 0); - } =20 REPORT_TEST_PASS(); } @@ -1474,6 +1720,7 @@ static void test_seal_mremap_move_dontunmap(bool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1488,6 +1735,10 @@ static void test_seal_mremap_move_dontunmap(bool sea= l) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else { /* kernel will allocate a new address */ FAIL_TEST_IF_FALSE(ret2 !=3D MAP_FAILED); @@ -1503,6 +1754,7 @@ static void test_seal_mremap_move_dontunmap_anyaddr(b= ool seal) unsigned long size =3D 4 * page_size; int ret; void *ret2; + int prot; =20 setup_single_address(size, &ptr); FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); @@ -1529,6 +1781,10 @@ static void test_seal_mremap_move_dontunmap_anyaddr(= bool seal) if (seal) { FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); } else { /* remap success and return ptr2 */ FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr2); @@ -1690,9 +1946,10 @@ static void test_seal_discard_ro_anon_on_pkey(bool s= eal) /* sealing will take effect if PKRU deny write. */ set_pkey(pkey, PKEY_DISABLE_WRITE); ret =3D sys_madvise(ptr, size, MADV_DONTNEED); - if (seal) + if (seal) { FAIL_TEST_IF_FALSE(ret < 0); - else + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + } else FAIL_TEST_IF_FALSE(!ret); =20 /* base seal still apply. */ @@ -1876,6 +2133,15 @@ int main(int argc, char **argv) if (!pkey_supported()) ksft_print_msg("PKEY not supported\n"); =20 + /* + * 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); =20 test_seal_addseal(); --=20 2.46.0.469.g59c65b2a67-goog From nobody Thu Dec 18 23:47:13 2025 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5741B1BAEDE for ; Thu, 29 Aug 2024 21:44:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967848; cv=none; b=BotveIQkn4klpbg0jqgiY1f0hlsX/qvUvxvpcNElJG58Ch30etC+A9QCcfRcpjxBKhvYnfMfeyIQ5e7jduAUAJpj28Fjw8Cz/JZpfWDNUVHq4p3MWLvhudfQJRUCELjGlXATq7cxqSwjrqhM1Nw5ixMy722tBJ9x1rtqJ7Sl9GM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967848; c=relaxed/simple; bh=/IMNJ95SpejvcUBxNqKXzFolTjM9jYB1KqtTg3owDqw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aE2mv1la46983xZKLESArr/DlnO67inGMnQ3HUV4gI5vHsHtHLyGCsii5k2rJYn9Vjm+Ym5NnmWOzLZTLnwgvCL5SyI+Ro3wtpcFXm5YA5DTeBCaV7o2G8YqaMAOFHzewmltqJxSr5WfSwTgtM2NdS2yg8mY6/eB9HTcTAUN6EI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=RUk41ce4; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="RUk41ce4" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7c2e1441eddso9881a12.0 for ; Thu, 29 Aug 2024 14:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967846; x=1725572646; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=RUk41ce4GTZcfQNQ5jQ6IdTUD1C9NU5KJHte9pmHqksLBkLUgpHn81h1tHOmnscD4Y 2BJxb9rJVdrVtRjAIhz1744DgzJ65bR19CiYITJpB8WlvshCSCaWb8Dy1P844XxVCALz vz6NnMoRUvKXO0tlXFrGzrsUwyKV9ik/IPRTo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967846; x=1725572646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=GOk1JY2LXoAQ/EIwdWAuUqa0NBqFGNnS00jInj9PafuWx66K+DzrRBAUsFWY4e5qCd asXGi/aZx1vFhpen9S35Pf409vin+wklcHSseQh4kt9PQlIa+W3D6DPx8iJDXXRZrmi5 ryO40yn1Py8bqQfIGe9tEFZyv10WhxS+jnQtRTxLu5OIkAF2xcVi1R1l8/G7AQK52U68 5qgnGuWnWpLkovI8ozbIdstW2uJgMfiyHC6SDmpt5KZmQcYBaFj3+DB4JsIAbH0RzLrg Op8xJ+pOpWhDN0chMS2bATnBum5ttPOLKvCLnWdnSFbYZairBXhAdie+ZIEGfZrwur2p M/Pg== X-Forwarded-Encrypted: i=1; AJvYcCX9oTuK5X7FqtnCH5YSqXjQRyWh9JNdS146XIm4888oxHP3FQMZJU4S+09yqjGXnaVfqjQDtVUpW3tZ8ts=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5XFNl1eUB4Z9J1aQ6LDaSA0juyNKcfRVnPI5JoK1qm4CvSF0q 6ze5kP0DbuG0KUQn8pd8plCn9v1jvkGnv8g70yK/C8yYnIwcLZg1Mq6QoEuwtQ== X-Google-Smtp-Source: AGHT+IGw6LhTiqcHA4wOD2hbInscY7fZlLpfvydbHUPkI3AUpW/gqVA9D8kpvdBYJgGdQzekGp3weg== X-Received: by 2002:a17:903:2350:b0:1fa:a9f5:64b1 with SMTP id d9443c01a7336-2052761fcd8mr1316265ad.3.1724967846304; Thu, 29 Aug 2024 14:44:06 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-205152b26b2sm15882985ad.6.2024.08.29.14.44.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:05 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, pedro.falcato@gmail.com, willy@infradead.org, lorenzo.stoakes@oracle.com, broonie@kernel.org, vbabka@suse.cz, Liam.Howlett@oracle.com, rientjes@google.com, keescook@chromium.org, Jeff Xu Subject: [PATCH v2 2/4] selftests/mm: mseal_test add sealed madvise type Date: Thu, 29 Aug 2024 21:43:50 +0000 Message-ID: <20240829214352.963001-3-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829214352.963001-1-jeffxu@chromium.org> References: <20240829214352.963001-1-jeffxu@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jeff Xu Add a testcase to cover all sealed madvise type. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 108 +++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selfte= sts/mm/mseal_test.c index adc646cf576c..ae06c354220d 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2121,6 +2121,107 @@ static void test_seal_madvise_nodiscard(bool seal) REPORT_TEST_PASS(); } =20 +static void test_seal_discard_madvise_advice(void) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 4 * page_size; + int ret; + int sealed_advice[] =3D {MADV_FREE, MADV_DONTNEED, + MADV_DONTNEED_LOCKED, MADV_REMOVE, + MADV_DONTFORK, MADV_WIPEONFORK}; + int size_sealed_advice =3D sizeof(sealed_advice) / sizeof(int); + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + ret =3D seal_single_address(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + + for (int i =3D 0; i < size_sealed_advice; i++) { + ret =3D sys_madvise(ptr, size, sealed_advice[i]); + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + } + + REPORT_TEST_PASS(); +} + +static void test_munmap_free_multiple_ranges(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 8 * page_size; + int ret; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + /* unmap one page from beginning. */ + ret =3D sys_munmap(ptr, page_size); + FAIL_TEST_IF_FALSE(!ret); + + /* unmap one page from middle. */ + ret =3D sys_munmap(ptr + 4 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + + /* seal the last page */ + if (seal) { + ret =3D sys_mseal(ptr + 7 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + } + + /* munmap all 8 pages from beginning */ + ret =3D sys_munmap(ptr, 8 * page_size); + if (seal) { + FAIL_TEST_IF_FALSE(ret); + + /* verify mapping are not changed */ + size =3D get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 3 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 1 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + } else { + FAIL_TEST_IF_FALSE(!ret); + + for (int i =3D 0; i < 8; i++) { + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 0); + } + } + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal =3D seal_support(); @@ -2142,7 +2243,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); =20 - ksft_set_plan(88); + ksft_set_plan(91); =20 test_seal_addseal(); test_seal_unmapped_start(); @@ -2251,5 +2352,10 @@ int main(int argc, char **argv) test_seal_discard_ro_anon_on_pkey(false); test_seal_discard_ro_anon_on_pkey(true); =20 + test_seal_discard_madvise_advice(); + + test_munmap_free_multiple_ranges(false); + test_munmap_free_multiple_ranges(true); + ksft_finished(); } --=20 2.46.0.469.g59c65b2a67-goog From nobody Thu Dec 18 23:47:13 2025 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5772D1BB6A0 for ; Thu, 29 Aug 2024 21:44:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967851; cv=none; b=Jdj9Gyfs4oJmzPUi132Mquw/CHszhUXcE0MnkwiW7YlNKSzkIovO/gBmPsopU9B9Vz6N5J8Uqs6XleHt23+TO/aRf+HuK7kLeIRv47DfR0I69jpa+TfaGVvfaLSa30oHE3OarmAEppbl8FRPk4BnyLC5VpzctDUF8ylU3TTcTwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967851; c=relaxed/simple; bh=j5DIz/FwohbXIbAOYNtL4lsH2nx4Cd5E1K3MEN4JHG4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hHPk/Nc9Z9F9kvbuue6mrvmQRL3CQrtXZjfqMCcIS4aTQQbMg7PFuCJZxmsDCt1KAhLEoGW2tkbVFy8Zc+xfV1CrYqQ2lhYCviJswDncWG8Gw2wVGHHbd4Yh/zc4w/csWYb0UdzphdLsKzHPVGDgLhgHowiBGxeaQaafz+eN6RY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Vdihf0KT; arc=none smtp.client-ip=209.85.167.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Vdihf0KT" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3dd4297af75so70944b6e.0 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=vger.kernel.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=Vdihf0KTL5G8F2ERjwy0ZOXENyZVsW4MVBZXPRWFwiZytZamcKr84ShU4RPD+C/TPe 6sec6OMT3RTy4zCmCi6tl0iJutZv6NA+jlfk9p/wh2mFRk4IR+P/oQce95LqUaFgBjLW fEDsqIdii1lyOuUeqeUFmOml6vZdLL18ctdqk= 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=b1bfuMeuFa+hUlTC1o2+mAZtTzGC6RRkt2xN41VrpnTwEdh9xwoTu6Kiwa8OPsMFeg 6vDVSVHwsjmHfIid1FugXRpmV7odhVyqhsIh7hVEz89hgL6mtpYHPcHyvQhkRbH5nhuK QhkeIDzykzCPPcW6nqcSd8KUHXG5MmYzZIr1QH6VFCd69lAOOc8K1luykt8EOpNAXaSA NmoUMK8f+xEJWGGC66BwQvteVAsTt2iPgA545qRoCG519WS+aAfFF+3rdm2xomSrmORO ygxXW8sCMP2QTFJ2prSRTpR1q/8LGyHi6YGy5ukmysagUNtr+bzy607WDWqbLUKryDS3 hb4Q== X-Forwarded-Encrypted: i=1; AJvYcCXzAZMCnbFI7+zjdffGV9igaJuAxrBQlBv1I0St4MLpqM2sQh2BSFixgg4nnWBN1vvyQ+9BuK5i9utGqfU=@vger.kernel.org X-Gm-Message-State: AOJu0YwAAsd8wzzjXkF4q3UAn9rqBuTzqWh7XDemjnztkmw8ZxPnrFqm 1BiDSnM6kAwAWZmCVKCRM4aPqGXDtVJt7qkxw/oD7v2OjzyqB4D43uOhZR/wUw== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/selfte= sts/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 s= eal) REPORT_TEST_PASS(); } =20 +static void test_seal_mmap_expand_seal_middle(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + /* ummap last 4 pages. */ + ret =3D sys_munmap(ptr + 8 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 8 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to expand. */ + ret2 =3D mmap(ptr, 12 * page_size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); + + REPORT_TEST_PASS(); +} + +static void test_seal_mmap_shrink_seal_middle(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to shrink. */ + ret2 =3D mmap(ptr, 7 * page_size, PROT_READ, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 =3D=3D MAP_FAILED); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + } else + FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); + + REPORT_TEST_PASS(); +} + +static void test_seal_mmap_reuse_addr(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + if (seal) { + ret =3D sys_mseal(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* use mmap to change protection. */ + ret2 =3D mmap(ptr, size, PROT_NONE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + + FAIL_TEST_IF_FALSE(!(ret2 =3D=3D MAP_FAILED)); + FAIL_TEST_IF_FALSE(ret2 !=3D ptr); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0x4); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal =3D 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"); =20 - ksft_set_plan(91); + ksft_set_plan(97); =20 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); =20 + 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(); } --=20 2.46.0.469.g59c65b2a67-goog From nobody Thu Dec 18 23:47:13 2025 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DEDB81BB6BF for ; Thu, 29 Aug 2024 21:44:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967853; cv=none; b=aG7I3zgnmi1i9tpu3ztxHXGeVCHnQ1t5ln+TJhnUUdl/YggOO85gbGF9yJWc8KONNzEZVyI1lsbIj7LaEItoXT1JEosk21F7PWpG1yXhgLIu5ZHR17neiqvOFS2Br7xytg4N792GA0yP4O8yPJQhZMElxudtl9CSv2jvlg0GXME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724967853; c=relaxed/simple; bh=ACgdkIlvnRhZ7PHc5BWlTN2Eo+ik0nzcD6MIX1rROxc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TibAg2wNoDrA6XfWWXJDzxA6yvtlxf4fKaGpP91gXp6iFj+MNoeFM89w9tj5ZWkJm6iJGyQuJPDZY9pnAmR6hEgRH6U8SiK7YPMjEcoCbIDUEQkl5rhnEyB3CfG2sqOuJ0SAL+DvUc7RsL4WtLseAVGNIYzqEeqNMz7y3amk6ug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=i9NMRfNF; arc=none smtp.client-ip=209.85.167.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="i9NMRfNF" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3df075ef43cso91998b6e.3 for ; Thu, 29 Aug 2024 14:44:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967851; x=1725572651; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hAPF+mFWkE9aMPYn9qrkTRHhUapPOCqVhxefhrunDUc=; b=i9NMRfNFMGr3JCOXXE3q6gYMg8F8eTQZNKu1z/77/140ymVMZ4KjNAtJ9h+MhXDNLs pqj/Ts5TC3ije71bkF2NVNOykOfrdmnjyzxTCjWVQG2+kTuI0exNSGw5U1wM3lLtjH2a HIdvlo8cSQnYTW6Z4tcsLWXkPXdt53BJK5bmY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967851; x=1725572651; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hAPF+mFWkE9aMPYn9qrkTRHhUapPOCqVhxefhrunDUc=; b=GFzmlyBI/kiUd/Yh0RfJSKIwSCKP4y2cadLYrzGPz1q64636U1IVF/beZ7guVLZIg5 jjh1AaVdMpE88UlHu3VIifkyU4N8SX+k4/BCosfOkekR62sYZRTL3HgmCVXu6oHUjN/p Lqs116MvYO+Mhnu+LZZV8shNUetv5JEMo3Nc6k+yb381mKVWxNkQ9FwWXpEaQHM/ZkXP mEgjnst4ZM4i2+IAfmCsWF24lBZFQ6jbuJiTz4okzFq8l+llu8idUVMNHfuYPZd4JxFZ CUp49OKVOymbEYyjDzLufV0fP2ETT2qlWrTSo7n9oh7UTuRSQmwwSZl4JtHvObzSBeVc 4u1g== X-Forwarded-Encrypted: i=1; AJvYcCVbGaDG2P7Ti7NRW8Bu0SFekMQI/SH8/IYbORAFpnKepqMvyBTkwAFoZ9MI99MYS77vSNYPWQNEXgJh9c4=@vger.kernel.org X-Gm-Message-State: AOJu0YzhmqunmoTZtQ5opO7OXPeNhnBq4LbT+oD8Q7BpI+wZqK3BQSoA JB3NbAF6pI50uwpdpsz5TpILAlwEQXbQ/0fJ1Tyw3sqEAfivzRNZjArT3yNYqg== X-Google-Smtp-Source: AGHT+IHtVngpZOZlZonUTLqOqg/y/b0gvT/st7+gVnwMaqdrCOVHCIfjFfjqHOSo+TMO91gZzOvYCQ== X-Received: by 2002:a05:6871:5226:b0:260:ccfd:2e52 with SMTP id 586e51a60fabf-27790377431mr2564127fac.7.1724967850809; Thu, 29 Aug 2024 14:44:10 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-715e56d7593sm1585108b3a.168.2024.08.29.14.44.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:10 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, pedro.falcato@gmail.com, willy@infradead.org, lorenzo.stoakes@oracle.com, broonie@kernel.org, vbabka@suse.cz, Liam.Howlett@oracle.com, rientjes@google.com, keescook@chromium.org, Jeff Xu Subject: [PATCH v2 4/4] selftests/mm: mseal_test add more tests for mremap Date: Thu, 29 Aug 2024 21:43:52 +0000 Message-ID: <20240829214352.963001-5-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829214352.963001-1-jeffxu@chromium.org> References: <20240829214352.963001-1-jeffxu@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/selfte= sts/mm/mseal_test.c index d83538039e76..494035129d65 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2338,6 +2338,197 @@ static void test_seal_mmap_reuse_addr(bool seal) REPORT_TEST_PASS(); } =20 +static void test_seal_mremap_shrink_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + ret =3D sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret2 =3D sys_mremap(ptr, 12 * page_size, 6 * page_size, 0, 0); + if (seal) { + FAIL_TEST_IF_FALSE(ret2 =3D=3D (void *) MAP_FAILED); + FAIL_TEST_IF_FALSE(errno =3D=3D EPERM); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + } else { + FAIL_TEST_IF_FALSE(ret2 =3D=3D ptr); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 2 * page_size); + } + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_expand_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + ret =3D sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + /* ummap last 4 pages. */ + ret =3D sys_munmap(ptr + 8 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret2 =3D sys_mremap(ptr, 8 * page_size, 12 * page_size, 0, 0); + FAIL_TEST_IF_FALSE(ret2 =3D=3D (void *) MAP_FAILED); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_move_expand_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + void *ptr2; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + setup_single_address(size, &ptr2); + FAIL_TEST_IF_FALSE(ptr2 !=3D (void *)-1); + + ret =3D sys_munmap(ptr2, 12 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ret =3D sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + /* ummap last 4 pages. */ + ret =3D sys_munmap(ptr + 8 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* move and expand cross VMA boundary will fail */ + ret2 =3D sys_mremap(ptr, 8 * page_size, 10 * page_size, MREMAP_FIXED | MR= EMAP_MAYMOVE, ptr2); + FAIL_TEST_IF_FALSE(ret2 =3D=3D (void *) MAP_FAILED); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + + REPORT_TEST_PASS(); +} + +static void test_seal_mremap_move_shrink_multiple_vmas(bool seal) +{ + void *ptr; + unsigned long page_size =3D getpagesize(); + unsigned long size =3D 12 * page_size; + int ret; + void *ret2; + int prot; + void *ptr2; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr !=3D (void *)-1); + + setup_single_address(size, &ptr2); + FAIL_TEST_IF_FALSE(ptr2 !=3D (void *)-1); + + ret =3D sys_munmap(ptr2, 12 * page_size); + FAIL_TEST_IF_FALSE(!ret); + + ret =3D sys_mprotect(ptr + 4 * page_size, 4 * page_size, PROT_NONE); + FAIL_TEST_IF_FALSE(!ret); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0); + + if (seal) { + ret =3D sys_mseal(ptr + 4 * page_size, 4 * page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* move and shrink cross VMA boundary is NOK */ + ret2 =3D sys_mremap(ptr, 12 * page_size, 8 * page_size, MREMAP_FIXED | MR= EMAP_MAYMOVE, ptr2); + FAIL_TEST_IF_FALSE(ret2 =3D=3D (void *) MAP_FAILED); + + size =3D get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 4); + + size =3D get_vma_size(ptr + 4 * page_size, &prot); + FAIL_TEST_IF_FALSE(size =3D=3D 4 * page_size); + FAIL_TEST_IF_FALSE(prot =3D=3D 0); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal =3D seal_support(); @@ -2359,7 +2550,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); =20 - ksft_set_plan(97); + ksft_set_plan(105); =20 test_seal_addseal(); test_seal_unmapped_start(); @@ -2480,5 +2671,14 @@ int main(int argc, char **argv) test_seal_mmap_reuse_addr(false); test_seal_mmap_reuse_addr(true); =20 + 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(); } --=20 2.46.0.469.g59c65b2a67-goog