From nobody Mon Feb 9 10:50:26 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18BB4C001B0 for ; Tue, 4 Jul 2023 15:37:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231744AbjGDPhN (ORCPT ); Tue, 4 Jul 2023 11:37:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230255AbjGDPhK (ORCPT ); Tue, 4 Jul 2023 11:37:10 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 445171B2 for ; Tue, 4 Jul 2023 08:37:09 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3112f5ab0b1so6394889f8f.0 for ; Tue, 04 Jul 2023 08:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688485027; x=1691077027; 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=1v5lGibnAF/heTsoRNB1jAJr0Wc+6STg6A/2r7lAh8k=; b=Z1/poYmW45cjv7LNYkKB69NwMZFwnZvKh4vFG+6k08+f3spLw+mSSsETwE1tgZbMD1 HKU7aZ1QKyT4aOvcP8fHNQn8gTaqeG/F0VlqaCbQYBQXuAmW0OSwQa/XQQti57AVxQCe p79ZfZDYrSgg8pFr2OcW+0iCZO/Excu3e9VeE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688485027; x=1691077027; 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=1v5lGibnAF/heTsoRNB1jAJr0Wc+6STg6A/2r7lAh8k=; b=Yj33gVUmkyjpJ6gCzjFkgPppjKMkAde98R6UzisrzRZG/RUf4zAxZTRdY9OgYvP1qr 8TUumgAZq9FpA7mPDmBHAHlxQnK28KGuKEB5xkYYrr8KpFMqJAaDrLaPo3FvRSIrQ+cI zerBaCvDDClX8ih1Wuo0mwk5tWBJkmEPOnXunXlchP1WPQ034luvJAjTIoUsqq+G2V38 1UJay8pgEVAbLy5XfejIzvS8yYaPicyxAObG2RbVwT+KK7HS0uuVLTPQtp9f06B3K/Sl C2+jZ0P7oaNnChFcn535yEYlyEYKY8TEhF6DNB5hT5NoRnaFRDdGJVTA0ulrFgV52uqT 1rcg== X-Gm-Message-State: ABy/qLZExW4S+fMbTKoKl6Sk2WVHlGVhPLNsy2We/tUV6PANlesIcSgz VQSuOVGgQb2sTma8bZgDlcscuYz3kCNWVHA76cg= X-Google-Smtp-Source: APBJJlF4ddN4NnLSebE8v9sNmpNAPwAbxVyqwdoBjPzfy26Qv0Li1SwR3xWjrXbxRNN2kNlT3NSVuA== X-Received: by 2002:adf:cd0a:0:b0:314:10d8:b482 with SMTP id w10-20020adfcd0a000000b0031410d8b482mr10533375wrm.65.1688485027313; Tue, 04 Jul 2023 08:37:07 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:99b2:81bb:8407:5369]) by smtp.gmail.com with ESMTPSA id b2-20020adfde02000000b0030c4d8930b1sm28538709wrm.91.2023.07.04.08.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 08:37:06 -0700 (PDT) From: Florent Revest To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, broonie@kernel.org, szabolcs.nagy@arm.com, kpsingh@kernel.org, gthelen@google.com, toiwoton@gmail.com, Florent Revest Subject: [PATCH v3 1/5] kselftest: vm: Fix tabs/spaces inconsistency in the mdwe test Date: Tue, 4 Jul 2023 17:36:25 +0200 Message-ID: <20230704153630.1591122-2-revest@chromium.org> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230704153630.1591122-1-revest@chromium.org> References: <20230704153630.1591122-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Reviewed-by: David Hildenbrand Signed-off-by: Florent Revest Acked-by: Catalin Marinas Reviewed-by: Kees Cook --- tools/testing/selftests/mm/mdwe_test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftes= ts/mm/mdwe_test.c index bc91bef5d254..d0954c657feb 100644 --- a/tools/testing/selftests/mm/mdwe_test.c +++ b/tools/testing/selftests/mm/mdwe_test.c @@ -49,19 +49,19 @@ FIXTURE_VARIANT(mdwe) =20 FIXTURE_VARIANT_ADD(mdwe, stock) { - .enabled =3D false, + .enabled =3D false, .forked =3D false, }; =20 FIXTURE_VARIANT_ADD(mdwe, enabled) { - .enabled =3D true, + .enabled =3D true, .forked =3D false, }; =20 FIXTURE_VARIANT_ADD(mdwe, forked) { - .enabled =3D true, + .enabled =3D true, .forked =3D true, }; =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Mon Feb 9 10:50:26 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C700DEB64D9 for ; Tue, 4 Jul 2023 15:37:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjGDPhR (ORCPT ); Tue, 4 Jul 2023 11:37:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230300AbjGDPhM (ORCPT ); Tue, 4 Jul 2023 11:37:12 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B216F1B2 for ; Tue, 4 Jul 2023 08:37:10 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-314313f127fso3214290f8f.1 for ; Tue, 04 Jul 2023 08:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688485029; x=1691077029; 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=beswSwVGAkwwt+hvsxKunV4bjZ5oFhW4HtgFlCfoMD8=; b=im9v3tz+dDQKllRZMfAl2CXof/kWuglwbcaU6I+/SM3taNN9W0TM7sI67nnkl3lDSX 9evAJPoxo2Tv50rEz5lihIj5frPU3Hxo1Et5ixkKvTNtQhQyTA6xiirfJK5+I7SVGHC2 vJhjhWvhzJTSHRPd/yK9QTQbknde3VwTLbMWE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688485029; x=1691077029; 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=beswSwVGAkwwt+hvsxKunV4bjZ5oFhW4HtgFlCfoMD8=; b=MpG+8JekEMy0pHYzBEZv+2XuIVtA/n0sm91DryhRltOz/JaIUifUDLq0lSi3CccjLd 7EwsoCsAcKyxl/m36SiyVz9UWyKUaEnjcmc7pS0i6bKiXgI1BPA4hVDJl56s6OKP7Vp4 xNAbvah6h3QDzb4xckM115a7WAtDUgqTg34yCutXbHct0u4RlUNkAJUmNVInZRtB7nLi U8djlMy2vH16JaBjDcorYbth385YLowGNqv4xOeOyn9VVRpki9tb8ZBCCKfwXjktWmZG zTcStHtqZuhG2q9l2UrC8WA5+BgyqcTfTRtMA1POhe3fsUg1T4amwBwKuagNV2Xz/QER fP9w== X-Gm-Message-State: ABy/qLY0bet3RijTbkuXboK2VQkt2DWkRiMZywCx+d69CQHx1qc2GmNR IKERAT2CrOV3uBdJnSWAC7Ppt+jRI/WTJy8A3vs= X-Google-Smtp-Source: APBJJlGdWcugyCLxuhlb72LvzlOj5uz7Xrz2PmeD25M8SMc8R/B4axzPNqhXW2AizS/HJbijddnx2A== X-Received: by 2002:a5d:4950:0:b0:313:f7f1:e34c with SMTP id r16-20020a5d4950000000b00313f7f1e34cmr11728009wrs.60.1688485028942; Tue, 04 Jul 2023 08:37:08 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:99b2:81bb:8407:5369]) by smtp.gmail.com with ESMTPSA id b2-20020adfde02000000b0030c4d8930b1sm28538709wrm.91.2023.07.04.08.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 08:37:08 -0700 (PDT) From: Florent Revest To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, broonie@kernel.org, szabolcs.nagy@arm.com, kpsingh@kernel.org, gthelen@google.com, toiwoton@gmail.com, Florent Revest Subject: [PATCH v3 2/5] kselftest: vm: Fix mdwe's mmap_FIXED test case Date: Tue, 4 Jul 2023 17:36:26 +0200 Message-ID: <20230704153630.1591122-3-revest@chromium.org> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230704153630.1591122-1-revest@chromium.org> References: <20230704153630.1591122-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" I checked with the original author, the mmap_FIXED test case wasn't properly tested and fails. Currently, it maps two consecutive (non overlapping) pages and expects the second mapping to be denied by MDWE but these two pages have nothing to do with each other so MDWE is actually out of the picture here. What the test actually intended to do was to remap a virtual address using MAP_FIXED. However, this operation unmaps the existing mapping and creates a new one so the va is backed by a new page and MDWE is again out of the picture, all remappings should succeed. This patch keeps the test case to make it clear that this situation is expected to work. Signed-off-by: Florent Revest Reviewed-by: David Hildenbrand Fixes: 4cf1fe34fd18 ("kselftest: vm: add tests for memory-deny-write-execut= e") Reviewed-by: Catalin Marinas Reviewed-by: Kees Cook Reviewed-by: Ryan Roberts Tested-by: Ayush Jain Tested-by: Ryan Roberts --- tools/testing/selftests/mm/mdwe_test.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftes= ts/mm/mdwe_test.c index d0954c657feb..91aa9c3099e7 100644 --- a/tools/testing/selftests/mm/mdwe_test.c +++ b/tools/testing/selftests/mm/mdwe_test.c @@ -168,13 +168,10 @@ TEST_F(mdwe, mmap_FIXED) self->p =3D mmap(NULL, self->size, PROT_READ, self->flags, 0, 0); ASSERT_NE(self->p, MAP_FAILED); =20 - p =3D mmap(self->p + self->size, self->size, PROT_READ | PROT_EXEC, + /* MAP_FIXED unmaps the existing page before mapping which is allowed */ + p =3D mmap(self->p, self->size, PROT_READ | PROT_EXEC, self->flags | MAP_FIXED, 0, 0); - if (variant->enabled) { - EXPECT_EQ(p, MAP_FAILED); - } else { - EXPECT_EQ(p, self->p); - } + EXPECT_EQ(p, self->p); } =20 TEST_F(mdwe, arm64_BTI) --=20 2.41.0.255.g8b1d071c50-goog From nobody Mon Feb 9 10:50:26 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23377EB64DA for ; Tue, 4 Jul 2023 15:37:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231572AbjGDPhU (ORCPT ); Tue, 4 Jul 2023 11:37:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231732AbjGDPhN (ORCPT ); Tue, 4 Jul 2023 11:37:13 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FCE8E4F for ; Tue, 4 Jul 2023 08:37:12 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-313f61890fbso6220183f8f.3 for ; Tue, 04 Jul 2023 08:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688485030; x=1691077030; 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=4ZVHggtHO+yJauR9LskgHgrRJhbEnps/FtwfSudSsCo=; b=MFwOF7sqVXBxFanxQfr0POOuegrGEdOtkhF028G3OsKGL3xdsPPzrHjjHCl/yZUQsi 0u2xWsEBho6iRvSucxENpzwYqu7b0USkS0W+q1mqavp72R9LwsRuiUSOgH6bJqxDI2N6 Myg7WgOAMHNQAOKGq0YrI/FSXNdf8mMkegW+0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688485030; x=1691077030; 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=4ZVHggtHO+yJauR9LskgHgrRJhbEnps/FtwfSudSsCo=; b=Yq04T6bXx7osBDBTUMvMlyuoTX7fPi2Wi+Ma8mZxYng5+hwgFUrTS7PswYvqJcOcBJ 6/wn8gUL0W1NUu7cd3O3fUf2axQFUgFSx0rso9VSzC4HINpUzoTbs9+Bo19P69XFMieE B8qBGNC0+O2xfhy93Xh34U/y45j/jzZRHdU1sw9ZQtonxhjIZYRNUwje8Q0CBz/vmVsg 2R164CMP6DoWDC0sUtgBo1/53BKtYvg3iup18AFrEZ4pBmHYTKE6KdEqoD/h0kxTI86G BKkSV+YBZyLqoibMoQPW52iDVhUTVvdwrz5oib0KQrW+AHXEoz78mTaG2iXrT6YZ/Nex rE3g== X-Gm-Message-State: ABy/qLY4Qu2l+nV43do8+I1+2zr27sbfXs0xnYMtHlwT5q+I5ZvVsUtJ nzmtB6QqZvgDGWEwbmXCUY8ddtdvJbUYdWYoqzI= X-Google-Smtp-Source: APBJJlEAgV7A2O402mT7fPusAvg3tuUXWOjSGe1PiyyavUQPEZBAhTj4IWlt79euih0ZpRWlsQjz3Q== X-Received: by 2002:a5d:44c2:0:b0:314:1ad7:2ea8 with SMTP id z2-20020a5d44c2000000b003141ad72ea8mr11140519wrr.54.1688485030643; Tue, 04 Jul 2023 08:37:10 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:99b2:81bb:8407:5369]) by smtp.gmail.com with ESMTPSA id b2-20020adfde02000000b0030c4d8930b1sm28538709wrm.91.2023.07.04.08.37.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 08:37:10 -0700 (PDT) From: Florent Revest To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, broonie@kernel.org, szabolcs.nagy@arm.com, kpsingh@kernel.org, gthelen@google.com, toiwoton@gmail.com, Florent Revest , linux-stable@vger.kernel.org Subject: [PATCH v3 3/5] mm: Make PR_MDWE_REFUSE_EXEC_GAIN an unsigned long Date: Tue, 4 Jul 2023 17:36:27 +0200 Message-ID: <20230704153630.1591122-4-revest@chromium.org> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230704153630.1591122-1-revest@chromium.org> References: <20230704153630.1591122-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Defining a prctl flag as an int is a footgun because on a 64 bit machine and with a variadic implementation of prctl (like in musl and glibc), when used directly as a prctl argument, it can get casted to long with garbage upper bits which would result in unexpected behaviors. This patch changes the constant to an unsigned long to eliminate that possibilities. This does not break UAPI. Fixes: b507808ebce2 ("mm: implement memory-deny-write-execute as a prctl") Cc: linux-stable@vger.kernel.org Signed-off-by: Florent Revest Suggested-by: Alexey Izbyshev Reviewed-by: David Hildenbrand Acked-by: Catalin Marinas Reviewed-by: Kees Cook --- include/uapi/linux/prctl.h | 2 +- tools/include/uapi/linux/prctl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index f23d9a16507f..6e9af6cbc950 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -283,7 +283,7 @@ struct prctl_mm_map { =20 /* Memory deny write / execute */ #define PR_SET_MDWE 65 -# define PR_MDWE_REFUSE_EXEC_GAIN 1 +# define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) =20 #define PR_GET_MDWE 66 =20 diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/pr= ctl.h index f23d9a16507f..6e9af6cbc950 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -283,7 +283,7 @@ struct prctl_mm_map { =20 /* Memory deny write / execute */ #define PR_SET_MDWE 65 -# define PR_MDWE_REFUSE_EXEC_GAIN 1 +# define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) =20 #define PR_GET_MDWE 66 =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Mon Feb 9 10:50:26 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD637EB64DA for ; Tue, 4 Jul 2023 15:37:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231828AbjGDPhX (ORCPT ); Tue, 4 Jul 2023 11:37:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231750AbjGDPhP (ORCPT ); Tue, 4 Jul 2023 11:37:15 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6031BE42 for ; Tue, 4 Jul 2023 08:37:14 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-307d20548adso6353240f8f.0 for ; Tue, 04 Jul 2023 08:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688485032; x=1691077032; 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=R7tunI1lKMH4tXxhhZlNnHm4rteRjjg9hHxMHKz9rMs=; b=IH8/lYadcMH//iw9M9Op8dU0l1sj9aFWw0lBe6q6pMiopghB2cvLo/OcIkjbXfOgC6 Y7x5+a5kcNvS9pp+WWGqp5o36R5bI4rsQwvbYsUc1Qx6T4i713ReMLLt4ZaDVZsYaSd5 A3rBFK/cCnkuHUUrd8RED5SGzIm8RjYv50e0Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688485032; x=1691077032; 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=R7tunI1lKMH4tXxhhZlNnHm4rteRjjg9hHxMHKz9rMs=; b=YAJu9BjjfN8+TYd0Nt1Lp57Ikw7WDZGHqjRCYNtEScSb6Qjj8aZNy1t6b8kUtHG1TV rCz/4heUAb+GZmuur/RIr+limqkce2IJxrtUv6eBZ1gXT/kYSOXEUtdgDXsvNs2DCx6L Ml7uPZxHNcYZjNPXeBWh7qpAqSzeO7J83mxfO0u12ZLUnc6VGj6hn3aEZqL/XnzYpygF HHTEkTGE6gq4C+69vRUsYBOtZ+7vg120cTmwkkU/AtBFcI9LRikgW9fYHoG+ix4tM+0q NPVPnYJUWtbpfyFB27w+uX7KZvbFVpd3gKNF8f3xs4J1Czvw8K1dwcqdo/+l9DEDv0J3 cOnw== X-Gm-Message-State: ABy/qLZRFdQIjVjwyk4k2bYnmt8rncUUGbFjOsZr+8c4ZZzqXTkYF1+x 3EpqAeGx6lukjvHAJyhI81cnS8FMZuwNtr3krGc= X-Google-Smtp-Source: APBJJlEk398zYlZ/TviSclnfJx8pfvjJ6ut2plYZwXPVcNfINX4lPmJVH71We1oWGxYpyyxB1gS6eg== X-Received: by 2002:a5d:4f86:0:b0:30f:c42e:3299 with SMTP id d6-20020a5d4f86000000b0030fc42e3299mr10482799wru.60.1688485032503; Tue, 04 Jul 2023 08:37:12 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:99b2:81bb:8407:5369]) by smtp.gmail.com with ESMTPSA id b2-20020adfde02000000b0030c4d8930b1sm28538709wrm.91.2023.07.04.08.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 08:37:11 -0700 (PDT) From: Florent Revest To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, broonie@kernel.org, szabolcs.nagy@arm.com, kpsingh@kernel.org, gthelen@google.com, toiwoton@gmail.com, Florent Revest Subject: [PATCH v3 4/5] mm: Add a NO_INHERIT flag to the PR_SET_MDWE prctl Date: Tue, 4 Jul 2023 17:36:28 +0200 Message-ID: <20230704153630.1591122-5-revest@chromium.org> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230704153630.1591122-1-revest@chromium.org> References: <20230704153630.1591122-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This extends the current PR_SET_MDWE prctl arg with a bit to indicate that the process doesn't want MDWE protection to propagate to children. To implement this no-inherit mode, the tag in current->mm->flags must be absent from MMF_INIT_MASK. This means that the encoding for "MDWE but without inherit" is different in the prctl than in the mm flags. This leads to a bit of bit-mangling in the prctl implementation. Signed-off-by: Florent Revest Reviewed-by: Catalin Marinas Reviewed-by: Kees Cook --- include/linux/sched/coredump.h | 10 ++++++++++ include/uapi/linux/prctl.h | 1 + kernel/fork.c | 2 +- kernel/sys.c | 32 ++++++++++++++++++++++++++------ tools/include/uapi/linux/prctl.h | 1 + 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 0ee96ea7a0e9..1b37fa8fc723 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -91,4 +91,14 @@ static inline int get_dumpable(struct mm_struct *mm) MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) =20 #define MMF_VM_MERGE_ANY 29 +#define MMF_HAS_MDWE_NO_INHERIT 30 + +static inline unsigned long mmf_init_flags(unsigned long flags) +{ + if (flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) + flags &=3D ~((1UL << MMF_HAS_MDWE) | + (1UL << MMF_HAS_MDWE_NO_INHERIT)); + return flags & MMF_INIT_MASK; +} + #endif /* _LINUX_SCHED_COREDUMP_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 6e9af6cbc950..dacbe824e7c3 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -284,6 +284,7 @@ struct prctl_mm_map { /* Memory deny write / execute */ #define PR_SET_MDWE 65 # define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) +# define PR_MDWE_NO_INHERIT (1UL << 1) =20 #define PR_GET_MDWE 66 =20 diff --git a/kernel/fork.c b/kernel/fork.c index d17995934eb4..bc3c762d378f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1284,7 +1284,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm= , struct task_struct *p, hugetlb_count_init(mm); =20 if (current->mm) { - mm->flags =3D current->mm->flags & MMF_INIT_MASK; + mm->flags =3D mmf_init_flags(current->mm->flags); mm->def_flags =3D current->mm->def_flags & VM_INIT_DEF_MASK; } else { mm->flags =3D default_dump_filter; diff --git a/kernel/sys.c b/kernel/sys.c index 339fee3eff6a..1a2dc3da43ea 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2362,19 +2362,41 @@ static int prctl_set_vma(unsigned long opt, unsigne= d long start, } #endif /* CONFIG_ANON_VMA_NAME */ =20 +static inline unsigned long get_current_mdwe(void) +{ + unsigned long ret =3D 0; + + if (test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) + ret |=3D PR_MDWE_REFUSE_EXEC_GAIN; + if (test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags)) + ret |=3D PR_MDWE_NO_INHERIT; + + return ret; +} + static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3, unsigned long arg4, unsigned long arg5) { + unsigned long current_bits; + if (arg3 || arg4 || arg5) return -EINVAL; =20 - if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN)) + if (bits & ~(PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT)) + return -EINVAL; + + /* NO_INHERIT only makes sense with REFUSE_EXEC_GAIN */ + if (bits & PR_MDWE_NO_INHERIT && !(bits & PR_MDWE_REFUSE_EXEC_GAIN)) return -EINVAL; =20 + current_bits =3D get_current_mdwe(); + if (current_bits && current_bits !=3D bits) + return -EPERM; /* Cannot unset the flags */ + + if (bits & PR_MDWE_NO_INHERIT) + set_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags); if (bits & PR_MDWE_REFUSE_EXEC_GAIN) set_bit(MMF_HAS_MDWE, ¤t->mm->flags); - else if (test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) - return -EPERM; /* Cannot unset the flag */ =20 return 0; } @@ -2384,9 +2406,7 @@ static inline int prctl_get_mdwe(unsigned long arg2, = unsigned long arg3, { if (arg2 || arg3 || arg4 || arg5) return -EINVAL; - - return test_bit(MMF_HAS_MDWE, ¤t->mm->flags) ? - PR_MDWE_REFUSE_EXEC_GAIN : 0; + return (int)get_current_mdwe(); } =20 static int prctl_get_auxv(void __user *addr, unsigned long len) diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/pr= ctl.h index 6e9af6cbc950..dacbe824e7c3 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -284,6 +284,7 @@ struct prctl_mm_map { /* Memory deny write / execute */ #define PR_SET_MDWE 65 # define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) +# define PR_MDWE_NO_INHERIT (1UL << 1) =20 #define PR_GET_MDWE 66 =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Mon Feb 9 10:50:26 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07A7BEB64D9 for ; Tue, 4 Jul 2023 15:37:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231371AbjGDPh0 (ORCPT ); Tue, 4 Jul 2023 11:37:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231765AbjGDPhR (ORCPT ); Tue, 4 Jul 2023 11:37:17 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF47E54 for ; Tue, 4 Jul 2023 08:37:16 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-313fb7f0f80so6339744f8f.2 for ; Tue, 04 Jul 2023 08:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688485034; x=1691077034; 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=rLVIku7SViX3h+Lcl0E/1oJd3wDCllnW/4/cStAOVU8=; b=JOEPaz3vnVicEmuh0M2bGeQ1kl3GzOWIF49GpZl60KzO+GPWcT5dbD+wvseGGR0wpF IxOhrzyb0mInXy8d4183tOtH9SMwjHJhP23A8fyQrPc/56y0fXEdeGOra4JIsTvrhDEV wdGE+jVfHMmym/en2jJSDy3V5xHMkHz6rdQqU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688485034; x=1691077034; 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=rLVIku7SViX3h+Lcl0E/1oJd3wDCllnW/4/cStAOVU8=; b=XYT7+FwE5PS0sdDRVttelkLM3NC82f8D8IrxqfR3f7abV+v/dHsEILZqj37bzIo4RR bQCgNqoa4+57CqfOq+L8AivDrJwO2ZK751tYFzW5FlIwD1wPyA0jX66voeeipF0Vo6rn XvYGRAxIOSmthMhgLA7nZ2D/8HtdpG+zLuxlEUMBtZPjgRT+k/SUChegcFq2YaVl4zTi ScCCX/jYx5/qDgVQP1m8Ut59z9JE0gciQQXPXIq2Wk0aKzMPgVG8pnz682uSx1egiNAa XXRz42dfQK2E6lk6a+Ku/2W6ALmvFcQhoXO0QUoqJ4Mu3jfXk30wGoNW05y+6lN9oGCu PPMA== X-Gm-Message-State: ABy/qLZ8AvheEEdUdJqXXDlE+8/tLyEds0Q/seCA6Qe5qOaCneeTd+np KqJzZuerfHunuiPBnyPUXble9lc/bjKgEH/U3TI= X-Google-Smtp-Source: APBJJlGPcUUIL7rFhoXERgudOfZJobL4dZUD8SGmsqpar9eStx2K5Uf+A1VwfXk9CRgRtCCc2GDieQ== X-Received: by 2002:a5d:6392:0:b0:314:37a2:5900 with SMTP id p18-20020a5d6392000000b0031437a25900mr5264050wru.14.1688485034312; Tue, 04 Jul 2023 08:37:14 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:99b2:81bb:8407:5369]) by smtp.gmail.com with ESMTPSA id b2-20020adfde02000000b0030c4d8930b1sm28538709wrm.91.2023.07.04.08.37.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 08:37:13 -0700 (PDT) From: Florent Revest To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: akpm@linux-foundation.org, catalin.marinas@arm.com, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, broonie@kernel.org, szabolcs.nagy@arm.com, kpsingh@kernel.org, gthelen@google.com, toiwoton@gmail.com, Florent Revest Subject: [PATCH v3 5/5] kselftest: vm: Add tests for no-inherit memory-deny-write-execute Date: Tue, 4 Jul 2023 17:36:29 +0200 Message-ID: <20230704153630.1591122-6-revest@chromium.org> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230704153630.1591122-1-revest@chromium.org> References: <20230704153630.1591122-1-revest@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add some tests to cover the new PR_MDWE_NO_INHERIT flag of the PR_SET_MDWE prctl. Check that: - it can't be set without PR_SET_MDWE - MDWE flags can't be unset - when set, PR_SET_MDWE doesn't propagate to children Signed-off-by: Florent Revest Acked-by: Catalin Marinas --- tools/testing/selftests/mm/mdwe_test.c | 98 ++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftes= ts/mm/mdwe_test.c index 91aa9c3099e7..7bfc98bf9baa 100644 --- a/tools/testing/selftests/mm/mdwe_test.c +++ b/tools/testing/selftests/mm/mdwe_test.c @@ -22,6 +22,8 @@ =20 TEST(prctl_flags) { + EXPECT_LT(prctl(PR_SET_MDWE, PR_MDWE_NO_INHERIT, 0L, 0L, 7L), 0); + EXPECT_LT(prctl(PR_SET_MDWE, 7L, 0L, 0L, 0L), 0); EXPECT_LT(prctl(PR_SET_MDWE, 0L, 7L, 0L, 0L), 0); EXPECT_LT(prctl(PR_SET_MDWE, 0L, 0L, 7L, 0L), 0); @@ -33,6 +35,69 @@ TEST(prctl_flags) EXPECT_LT(prctl(PR_GET_MDWE, 0L, 0L, 0L, 7L), 0); } =20 +FIXTURE(consecutive_prctl_flags) {}; +FIXTURE_SETUP(consecutive_prctl_flags) {} +FIXTURE_TEARDOWN(consecutive_prctl_flags) {} + +FIXTURE_VARIANT(consecutive_prctl_flags) +{ + unsigned long first_flags; + unsigned long second_flags; + bool should_work; +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, same) +{ + .first_flags =3D PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags =3D PR_MDWE_REFUSE_EXEC_GAIN, + .should_work =3D true, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_disable_mdwe) +{ + .first_flags =3D PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags =3D 0, + .should_work =3D false, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_disable_mdwe_no_inherit) +{ + .first_flags =3D PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .second_flags =3D 0, + .should_work =3D false, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_disable_no_inherit) +{ + .first_flags =3D PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .second_flags =3D PR_MDWE_REFUSE_EXEC_GAIN, + .should_work =3D false, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_enable_no_inherit) +{ + .first_flags =3D PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags =3D PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .should_work =3D false, +}; + +TEST_F(consecutive_prctl_flags, two_prctls) +{ + int ret; + + EXPECT_EQ(prctl(PR_SET_MDWE, variant->first_flags, 0L, 0L, 0L), 0); + + ret =3D prctl(PR_SET_MDWE, variant->second_flags, 0L, 0L, 0L); + if (variant->should_work) { + EXPECT_EQ(ret, 0); + + ret =3D prctl(PR_GET_MDWE, 0L, 0L, 0L, 0L); + ASSERT_EQ(ret, variant->second_flags); + } else { + EXPECT_NE(ret, 0); + } +} + FIXTURE(mdwe) { void *p; @@ -45,28 +110,45 @@ FIXTURE_VARIANT(mdwe) { bool enabled; bool forked; + bool inherit; }; =20 FIXTURE_VARIANT_ADD(mdwe, stock) { .enabled =3D false, .forked =3D false, + .inherit =3D false, }; =20 FIXTURE_VARIANT_ADD(mdwe, enabled) { .enabled =3D true, .forked =3D false, + .inherit =3D true, }; =20 -FIXTURE_VARIANT_ADD(mdwe, forked) +FIXTURE_VARIANT_ADD(mdwe, inherited) { .enabled =3D true, .forked =3D true, + .inherit =3D true, }; =20 +FIXTURE_VARIANT_ADD(mdwe, not_inherited) +{ + .enabled =3D true, + .forked =3D true, + .inherit =3D false, +}; + +static bool executable_map_should_fail(const FIXTURE_VARIANT(mdwe) *varian= t) +{ + return variant->enabled && (!variant->forked || variant->inherit); +} + FIXTURE_SETUP(mdwe) { + unsigned long mdwe_flags; int ret, status; =20 self->p =3D NULL; @@ -76,13 +158,17 @@ FIXTURE_SETUP(mdwe) if (!variant->enabled) return; =20 - ret =3D prctl(PR_SET_MDWE, PR_MDWE_REFUSE_EXEC_GAIN, 0L, 0L, 0L); + mdwe_flags =3D PR_MDWE_REFUSE_EXEC_GAIN; + if (!variant->inherit) + mdwe_flags |=3D PR_MDWE_NO_INHERIT; + + ret =3D prctl(PR_SET_MDWE, mdwe_flags, 0L, 0L, 0L); ASSERT_EQ(ret, 0) { TH_LOG("PR_SET_MDWE failed or unsupported"); } =20 ret =3D prctl(PR_GET_MDWE, 0L, 0L, 0L, 0L); - ASSERT_EQ(ret, 1); + ASSERT_EQ(ret, mdwe_flags); =20 if (variant->forked) { self->pid =3D fork(); @@ -113,7 +199,7 @@ TEST_F(mdwe, mmap_READ_EXEC) TEST_F(mdwe, mmap_WRITE_EXEC) { self->p =3D mmap(NULL, self->size, PROT_WRITE | PROT_EXEC, self->flags, 0= , 0); - if (variant->enabled) { + if (executable_map_should_fail(variant)) { EXPECT_EQ(self->p, MAP_FAILED); } else { EXPECT_NE(self->p, MAP_FAILED); @@ -139,7 +225,7 @@ TEST_F(mdwe, mprotect_add_EXEC) ASSERT_NE(self->p, MAP_FAILED); =20 ret =3D mprotect(self->p, self->size, PROT_READ | PROT_EXEC); - if (variant->enabled) { + if (executable_map_should_fail(variant)) { EXPECT_LT(ret, 0); } else { EXPECT_EQ(ret, 0); @@ -154,7 +240,7 @@ TEST_F(mdwe, mprotect_WRITE_EXEC) ASSERT_NE(self->p, MAP_FAILED); =20 ret =3D mprotect(self->p, self->size, PROT_WRITE | PROT_EXEC); - if (variant->enabled) { + if (executable_map_should_fail(variant)) { EXPECT_LT(ret, 0); } else { EXPECT_EQ(ret, 0); --=20 2.41.0.255.g8b1d071c50-goog