From patchwork Wed Aug 7 17:33:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13756538 Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) (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 444C613CFB8; Wed, 7 Aug 2024 17:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723052080; cv=none; b=tq66WrrNcodcFkmZ2foZiLpoyFoXGYgNTf/XAKAQ9eh+P8/3Rd1792vMtsRaJvVnAA+3NkcWHA4QOk3XnMaveQLxuNCXjJ+zvQa+gRxtpPj1oOwEUOiRb4BeVneqOuW2lsL6w8cFfMMnGBgxu3gaykpY9x0cvlcxinJpmsR8Cpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723052080; c=relaxed/simple; bh=eRMrYiHu2aV19U37soYJjthM4l3uu3pGDQoBe1QBm7I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RhL69z8adKq8LRKrr9C0yeMtWvHJJC+teZlUgn23mVKKrp9K+dvHU613RLH67xswmb3mnrCx2qBQy3gO3SlHNq/8JBegL4uXOXFnmGzTLolHJmXt+QAs2Oa+Ir9n3y4BWrcpORy4PN3TYuxogRzDwI0kcXnaJ3osJQSF+B3WwyA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gjgbfLFz; arc=none smtp.client-ip=209.85.208.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gjgbfLFz" Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2f025b94e07so852071fa.0; Wed, 07 Aug 2024 10:34:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723052077; x=1723656877; 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=ug2t+gDCuZhsVMdks6JpJwrHC29Kn6imWNcM3efiL3k=; b=gjgbfLFztRb2kwi4cqJHuL4R70TnCB1dJz39XMdk5F/OQRi2dLq1BXVzlKCUOicW7F BkgjPG8NVER3t6oUNW6b+9UUp+FA9KA6s1JWRviPdubmXvvjGKPdJNhtpDuJkHVM7WvD +Cc2MEejVnm2HpERN9uEmk2Q+VqPIIwT7G0i85nwTLPg0U2NT2HZlCZ0lth3gXSEe7cB tuwmnXUq9eA+dkRqsypiKDiXc7W6meXTs2LnYM/JeXGjIrTAfbZG1OxUBdSIusRtgb4N pJESScCo9rHi5uE51s6kXDH0g3HIU5y9WOlJYXH2n5TOV+WkVRWJz8soYTSIH/iIv/Yz ACmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723052077; x=1723656877; 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=ug2t+gDCuZhsVMdks6JpJwrHC29Kn6imWNcM3efiL3k=; b=vbkcar+0bx9PZPtql86t34WLUko2Wt2iO8SSS7R7Tx9J4sLEVRY1amav4izEEbaNDd 3kej+BtS02Ha2w1ogpemosXoGfuAxwvSiUnhhcgJgawJqR5GTzWFy2PHkd07Cc7VaA0M jxir/HTQkKJ3B8Lz8vsQ/EsB1O+C9FoTfAk8f1c2ZmaVE9O3ZMf3FN85V2UyxSgY1Ozq quhqnK2dYlBRmT3oJMv09Pzd1DoNYiMykA6eQdqIJiqsGJnWVzyGbMOmhttAnAOkE/yG P2Av/7HmXPA1P5JD9VUkLKvr35hSOucuDQ/AvN0wB5qwQcVDbIhZ8qjz82pu3paBU/mZ r4iA== X-Forwarded-Encrypted: i=1; AJvYcCU/BTFXTuNLjL3zI5Sb1zMBq+TlZHzi3quOMzI9aKYGdsWbwDBaVsra7Z88M8owsUM1zAL4/sLfAh94Xi76Y0I=@vger.kernel.org, AJvYcCXJZPjIvFLUzXY7WetHq3KETA1n64FtkRuQ+eokLRNRu1O/GDNr1sZJJpZSJAJPErKIw66LQeim@vger.kernel.org X-Gm-Message-State: AOJu0YyUHe2pyvkY1oyt0UBxoPwkHabVRM5lQuFw6f39CnYSW4iN6JwL m/wX7uelip05CoUZRZW+TUXw/rIlCvBm4rQVQJXzvs+PZ3J5heZ7 X-Google-Smtp-Source: AGHT+IE1nmNuUFZkZfTHaPbqr0JMJqJr7POoTHVNrzs+I1BCOpZInKZ1u0Rx9Zb3aRndvzq9j9VUhg== X-Received: by 2002:a2e:914b:0:b0:2ef:2b56:36a9 with SMTP id 38308e7fff4ca-2f15aa991f9mr122894281fa.16.1723052076747; Wed, 07 Aug 2024 10:34:36 -0700 (PDT) Received: from PC-PEDRO-ARCH.lan ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42905801f5bsm40335185e9.29.2024.08.07.10.34.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:34:36 -0700 (PDT) From: Pedro Falcato To: Andrew Morton , Shuah Khan , "Liam R. Howlett" , Jeff Xu , Kees Cook , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Pedro Falcato , stable@vger.kernel.org Subject: [PATCH 1/2] mseal: Fix is_madv_discard() Date: Wed, 7 Aug 2024 18:33:35 +0100 Message-ID: <20240807173336.2523757-2-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240807173336.2523757-1-pedro.falcato@gmail.com> References: <20240807173336.2523757-1-pedro.falcato@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 is_madv_discard did its check wrong. MADV_ flags are not bitwise, they're normal sequential numbers. So, for instance: behavior & (/* ... */ | MADV_REMOVE) tagged both MADV_REMOVE and MADV_RANDOM (bit 0 set) as discard operations. This is obviously incorrect, so use a switch statement instead. Cc: stable@vger.kernel.org Fixes: 8be7258aad44 ("mseal: add mseal syscall") Signed-off-by: Pedro Falcato --- mm/mseal.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm/mseal.c b/mm/mseal.c index bf783bba8ed..15bba28acc0 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -40,9 +40,17 @@ static bool can_modify_vma(struct vm_area_struct *vma) static bool is_madv_discard(int behavior) { - return behavior & - (MADV_FREE | MADV_DONTNEED | MADV_DONTNEED_LOCKED | - MADV_REMOVE | MADV_DONTFORK | MADV_WIPEONFORK); + switch (behavior) { + case MADV_FREE: + case MADV_DONTNEED: + case MADV_DONTNEED_LOCKED: + case MADV_REMOVE: + case MADV_DONTFORK: + case MADV_WIPEONFORK: + return true; + } + + return false; } static bool is_ro_anon(struct vm_area_struct *vma) From patchwork Wed Aug 7 17:33:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13756539 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 423D313D53F; Wed, 7 Aug 2024 17:34:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723052081; cv=none; b=i+AQLDto8+9ivkoPm231At5SMuwWR4AeWgIxDKOJop8nOxRoIr4IslCF75nyGe3xd8LKzCrIBcyvf840ItI4cYD9irwGgH29x3gNuwDVGLEz7RhCQ0iOJ5LaXeMdoaI/oRzThZdfNsx8bu09p/AKOF0NMPlR43bFCmP3E+oTj5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723052081; c=relaxed/simple; bh=PrxRm/D+gH6hP8WNQ6u//nRouzN6ryRD5SwlsPZ1JAM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mPWnFRGyB8jCSQXYW6W56IqIqQbsHgDp7n6fTW+37FG2yGIh4CG046rrXQ56bgvEHFy+hMDgcI5BG4Ooim6A7VBBtNrzGBKkQmIrKxJAAoH8SNITzvxBJQtzEqefMSMmfCIEBT5WcBsW3mV1VY3MjfyEwZtlYV+QcClNpeKDJdw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=T940ysed; arc=none smtp.client-ip=209.85.208.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T940ysed" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2f035ae1083so510961fa.3; Wed, 07 Aug 2024 10:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723052078; x=1723656878; 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=fkbLPlxft252tCcvtQQOAioY0nhUSIetGhYQZRnuOjM=; b=T940ysedopRuEfFbaTROs+Haub9qKO0ElkICqA3wvrmKZYeRMrmt24OMRATCtZB3V1 vxBbAqgSGusaGBixFy8DdrLeglQpa4lJwCIX/RHTRwB5xcLdVMNHGRLaEGKVtdJocngh yR8w5x1PCy2HQV8A/xSLq1oQqk55Bmp716vzatWCT6shAnBkqNAA+MuXQK5vY9fX3wsW tAXvUftn+L2raRcEGAaJ3Xq5hvn8OLEHMU9k+AqYRljPc3a1SP/tH06LhSnfVLf/MHVE 4QgGDKFOzp+V5IP5Dpa/WeZ6HOSmiiyb5A/IjGlWXj9IcuueZlEfJ+STsc0ei48o3FbE jKKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723052078; x=1723656878; 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=fkbLPlxft252tCcvtQQOAioY0nhUSIetGhYQZRnuOjM=; b=l0GMnW1k0FhgD7vTmWbvVSSUuJeXLdObybJDix+g7nwHoxTtHAOab2gG7Iij/TIPP9 GYY1ls2BsQhHb6DUdo1JXYnp6LG4i6gcp+qk5JMjWuytoHag1prmnRBM3j16lfSSGuXf r0U61PCqru+WR2SiHQAL0G6qTjeBb8qfQMzPonpxn1hjYq1vWULJ4suN39IQG+8LhUSe vGwQCCRn/SsrLTFjquhj9j0IxEuftjiGEZHKtqgJV0oV+qrWzgtY8alc6KLUyutwyUsO z5Q+ZDq21/mSsTTTzk/FeAi3muJE8lXI/9fVO3hOwYUDWNhUxut+Rjc/VDTroqyGQBp/ usHA== X-Forwarded-Encrypted: i=1; AJvYcCU+emgO82Sd1XAZl+HPxl4ySFa56vInSwcCDFzdYYZh+in6QRWrab4KEGudiFiezP6IN67aKcFtATkGmjCuv7D2azdU9+JMsSDyQOMuaWJ3 X-Gm-Message-State: AOJu0YwmWtLDCPSmJFPhNDr1aZ6vQl8MUvXRBfLqgnQ4KlRf4N1XBIys A9hx6rZeKgla3KAEEdr69rIlCqzHvragiDaw6SICA+t0asWw8ZlA X-Google-Smtp-Source: AGHT+IHIIJqMOzpEvAkMj1C5BnPy4mssOYul/wmg3Y4s/jeLUk3lFc/ZevqzIKcixd25dALAJ8jRxg== X-Received: by 2002:a05:651c:a0a:b0:2f1:5c89:c875 with SMTP id 38308e7fff4ca-2f15c89ca6amr155201391fa.26.1723052077831; Wed, 07 Aug 2024 10:34:37 -0700 (PDT) Received: from PC-PEDRO-ARCH.lan ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42905801f5bsm40335185e9.29.2024.08.07.10.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 10:34:37 -0700 (PDT) From: Pedro Falcato To: Andrew Morton , Shuah Khan , "Liam R. Howlett" , Jeff Xu , Kees Cook , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Pedro Falcato Subject: [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise Date: Wed, 7 Aug 2024 18:33:36 +0100 Message-ID: <20240807173336.2523757-3-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240807173336.2523757-1-pedro.falcato@gmail.com> References: <20240807173336.2523757-1-pedro.falcato@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add an mseal test for madvise() operations that aren't considered "discard" (e.g purely advisory ops such as MADV_RANDOM). Signed-off-by: Pedro Falcato --- tools/testing/selftests/mm/mseal_test.c | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index a818f010de4..2dcda7440eb 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -1731,6 +1731,38 @@ static void test_seal_discard_ro_anon(bool seal) REPORT_TEST_PASS(); } +static void test_seal_madvise_nodiscard(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = seal_single_address(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + } + + /* + * Test a random madvise flag like MADV_RANDOM that does not touch page + * contents (and thus should work for msealed VMAs). RANDOM also happens to + * share bits with other discard-ish flags like REMOVE. + */ + ret = sys_madvise(ptr, size, MADV_RANDOM); + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -1822,6 +1854,8 @@ int main(int argc, char **argv) test_seal_mremap_move_fixed_zero(true); test_seal_mremap_move_dontunmap_anyaddr(false); test_seal_mremap_move_dontunmap_anyaddr(true); + test_seal_madvise_nodiscard(false); + test_seal_madvise_nodiscard(true); test_seal_discard_ro_anon(false); test_seal_discard_ro_anon(true); test_seal_discard_ro_anon_on_rw(false);