From patchwork Wed May 17 15:03:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 95336 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1212782vqo; Wed, 17 May 2023 08:14:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7JxKVE9znvQbiMR1gaVCEFB+QbJGsln9018GJBkwk8SePnw+sRjb7dAR8Jp5f8gb+SfYzX X-Received: by 2002:a05:6a00:1306:b0:63b:8423:9e31 with SMTP id j6-20020a056a00130600b0063b84239e31mr20835pfu.11.1684336495228; Wed, 17 May 2023 08:14:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684336495; cv=none; d=google.com; s=arc-20160816; b=Ork/9oxDpa0qG59zmKwB0CFVbgwjuTDj6LiWbCjCA3gId41RmoHyjC1qoJoeeP9O6b 13p0cUZVQvnL0xUTH7rjbjBTiMVK1ZPzYn5acjFtUe19fv2zyjFYVZN4bLh7WUtJd4Ab MpcGKFiYjrORQv3yUIIjl6hUH4O9CWwInkfYD/7+DV8wXPiUD/vtLTYu6Y/GtSzDSESO nWzSlvDjZyl+20PJYFw88JhCgLlo0ety3YPUsPYgswtqGN7uSKxazPXSGDn9lzWy1R9m S9TVPxg/13F4pVeElPtzu8Bml5DpGocR5HuclIDE66p5t9+CWOpYs+3ZSG0IHHd6c1n4 Mukw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=J7ff2T9SWyCTsi5ruehr0chbFAgRla4S02ez1WlmA+w=; b=UjEGZDjGBQYwO9luDFwaGexK1QLUqwOYeajLwuWTBBFkC4BKx0qFebVbehMa2JAjhW zaKSnWzkBsbH8RYIZ5NtPd2hH5C6sECoJNupLuiTfGFL6I7DpM8u48CZbYWLe5t3u6cN 0WcQAUBU1/KUC7g0bxnMuXlt1W8XVOGldXDgDwZWveYhvV84yA+aZMhKMQsIiKiD3eZT UiP95VPvSx82G28z1L65+SAVN0J6f6JMjZCXGIObeMI0zjDmPBwMQJvBjADLLtkqnKIv lm7XuddPdKlbYFiXDndJKILzyT7zXlA40rUkmyUFhW9Svx4f4mfLuDPv/Me6WLAYfmwf Lonw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kGPLMlra; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k16-20020a628410000000b0063d2130eadasi22776460pfd.256.2023.05.17.08.14.42; Wed, 17 May 2023 08:14:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kGPLMlra; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231320AbjEQPFO (ORCPT + 99 others); Wed, 17 May 2023 11:05:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231907AbjEQPEg (ORCPT ); Wed, 17 May 2023 11:04:36 -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 124329ECC for ; Wed, 17 May 2023 08:04:18 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3093eb8cd1fso598225f8f.1 for ; Wed, 17 May 2023 08:04:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684335827; x=1686927827; 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=J7ff2T9SWyCTsi5ruehr0chbFAgRla4S02ez1WlmA+w=; b=kGPLMlrayHP1gkVYlmuR+aicFY/HlsmqbrMGUNf9veQEKPQqXMfCmsxUskzCN5t4z9 edI4hy/vkfueZyS+EvxWMN3BNzLs82IMkIZoa4eWel4ucQjUbPcYjaApd5sC90zM3Bad WpnVorrZEg2iClzBo2gfsPxFa8/oL9nrPRdLQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335827; x=1686927827; 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=J7ff2T9SWyCTsi5ruehr0chbFAgRla4S02ez1WlmA+w=; b=aURl/y3Q4+Jmxwb23dGld6N/ukX6kx5nbHUV737w8S8yfCI0/pxW3lMF3HjBW9fqHh W/aiYuQEHO5pEUGD+EXOgD5Ttx3hYj1Pp3LLTqD+Je4wwZH7N3773I7j29FPxs/BPcrt fxMZsYIwuZ2ZYzf+vbKyAza8OQwlL3eXPY3tXa/jI+0K6dFEVu3gRNfbsr07xzqxyce9 4wgFHyQRvJ5X5Ui6KGHeYX8SUWmPOOrGOuw7xVXRgABDnkYvKczO35SFaXWW8hovJuV0 E257uHL60iffTDW2Qak4zlSVBWF9YTUpGR3kxKl3HX82Yid7jhiQGUw+xlQ1uFXwoffF CrFw== X-Gm-Message-State: AC+VfDxFBUVw0Swpd69OC0q/PdUREvOaAJsLo11/sprJHbqtjHvtqcC4 2lmvfD5iY0ffD3GAHrPIeMLhT/n7r6wkCcnZqr8= X-Received: by 2002:adf:f109:0:b0:306:342a:6a01 with SMTP id r9-20020adff109000000b00306342a6a01mr880226wro.47.1684335826853; Wed, 17 May 2023 08:03:46 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:e223:a0c2:d2c:c371]) by smtp.gmail.com with ESMTPSA id e17-20020adffd11000000b003047ea78b42sm3038211wrr.43.2023.05.17.08.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:03:45 -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 v2 1/5] kselftest: vm: Fix tabs/spaces inconsistency in the mdwe test Date: Wed, 17 May 2023 17:03:17 +0200 Message-ID: <20230517150321.2890206-2-revest@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230517150321.2890206-1-revest@chromium.org> References: <20230517150321.2890206-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766154824779468754?= X-GMAIL-MSGID: =?utf-8?q?1766154824779468754?= Signed-off-by: Florent Revest Reviewed-by: David Hildenbrand --- 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/selftests/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) FIXTURE_VARIANT_ADD(mdwe, stock) { - .enabled = false, + .enabled = false, .forked = false, }; FIXTURE_VARIANT_ADD(mdwe, enabled) { - .enabled = true, + .enabled = true, .forked = false, }; FIXTURE_VARIANT_ADD(mdwe, forked) { - .enabled = true, + .enabled = true, .forked = true, }; From patchwork Wed May 17 15:03:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 95339 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1212890vqo; Wed, 17 May 2023 08:15:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6aoNJmU57NXfB3sF5tHrAQ76vsqb24ZMrQhOfuWwkDJEn580x6xiXnQ25f3flhawrN0gUQ X-Received: by 2002:a05:6a00:1253:b0:644:c365:50d5 with SMTP id u19-20020a056a00125300b00644c36550d5mr53748pfi.6.1684336503391; Wed, 17 May 2023 08:15:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684336503; cv=none; d=google.com; s=arc-20160816; b=W6PRk3NCA2+UzbpvGawqMQRyY3XxFe/j7bAQqKQxPYLkJrKdnEoCWkUhYi+cX1C62m wKKQknWSIZE4sGlq/xEt3FRyc/finBqcapDlhs/SvwMmhD1yC/h5xzOa35LfZYmgUiUS hNzzN4daSj2CLGPDos4NxPdoVwy1aL9sHUtiEHM5lowSS3gklY/bneaqihMsnJPYBehJ 9mR3KMT7yiOYc6aDAj+98byijj4BhFI3FX+HomZkjHXCSJPKTrHFSztOUKIsIP/jNuOA EQtIgIitxbZqekWpl6xTdYSSiUBFpDaa7XhPwZB+m8z/OFkj7WOAjOPZsTnrUUwncImq Aa6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Cfh3GE7IWMNsnxpLZpSEij25ZLJGjbedQ1hG+0yiDYk=; b=Qbk9rW4dbUZ1PahOPqQY2gmXpwOOZpT6c/2MSTKE5ztyZE3T/OiU7blF/sl8ZwWa5x EIGAKytEbYEYdalkvPvXedwSbs+BmIN/u/IPervyGXbyiLpQSANPQM58zQ72hh6tCA6G 6eV648c6VWfTGUN734Rf6luwoxkCTTzCmxYLl/iCI9z62YUMTfSzX0YbowetGr4NhkI6 OXWbEpaBShKhGri9Z281dBj+Icl+vZdh1pJTvYzrg6hfn23XC7PD9lqX644pvz/aaWQZ Z3f2yfHAQAlRWiD1CjnykfbNNBLtxlFn1K92oEoSXvYhxQj2B9vLQJoofZYq3sgvH1+o 0sJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TBK4OSB6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u129-20020a627987000000b0064580409a12si22328379pfc.320.2023.05.17.08.14.44; Wed, 17 May 2023 08:15:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TBK4OSB6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231853AbjEQPFU (ORCPT + 99 others); Wed, 17 May 2023 11:05:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230492AbjEQPEj (ORCPT ); Wed, 17 May 2023 11:04:39 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F482A5E6 for ; Wed, 17 May 2023 08:04:20 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-30935d343f7so842653f8f.1 for ; Wed, 17 May 2023 08:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684335829; x=1686927829; 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=Cfh3GE7IWMNsnxpLZpSEij25ZLJGjbedQ1hG+0yiDYk=; b=TBK4OSB65hONFNWvZypUGnx7S8WLlWfYWTq7/K+E9K+S8Su0GdBk6u6iGCqrsSrKG7 GqW6tBLQWziSzCi6z0EemnxyB+m/nKMEsmMfHGQ51NphG3FFy8IWI/ZDoiWu+0+j9qHS J3G4o2dX78bFaRnun6CZnrH+uMpsoOvIf6WsM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335829; x=1686927829; 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=Cfh3GE7IWMNsnxpLZpSEij25ZLJGjbedQ1hG+0yiDYk=; b=ib6wl0f2FvtGY1nj9JvbDSDiTYJU0nvwqwptWY7YmsEE/fpuvNVDGdP9GFKYkR1Cap oyzlZ/OTkG55tGC55NRxwJEAH9pCDLgqVm41KaWLUfwE3eKMOAgVhAxcoMY410nkxZ+T oQWjnkJKxoWUEblzHz6e9gI4+9L3gSRDAlWL8Y+4cykrAaPpQo1TUajoqNkG5nI/HDYd S2mVjN6acHOiqQqQydLIDPX2PirVix7RGy4KL3l1vMSufFF50tHyO9AQv7rBPU4n+c5b pSJ6JfiFA7/8LB0VXZS+K2HnZb6N/ng/D7P14jMMa//GR0r//Aah62gnLQSrNiZhd0wa 00XQ== X-Gm-Message-State: AC+VfDxqKAKF4InlAL3CbQatS7K3BLzfJaevbUj/7WSECpoYvohuoefu jBgmEj+GKToUSGDEjwtJnoYLP3zmHhOtyRqyd4w= X-Received: by 2002:adf:fa03:0:b0:2f9:4fe9:74bb with SMTP id m3-20020adffa03000000b002f94fe974bbmr791746wrr.40.1684335828943; Wed, 17 May 2023 08:03:48 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:e223:a0c2:d2c:c371]) by smtp.gmail.com with ESMTPSA id e17-20020adffd11000000b003047ea78b42sm3038211wrr.43.2023.05.17.08.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:03:48 -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 v2 2/5] kselftest: vm: Fix mdwe's mmap_FIXED test case Date: Wed, 17 May 2023 17:03:18 +0200 Message-ID: <20230517150321.2890206-3-revest@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230517150321.2890206-1-revest@chromium.org> References: <20230517150321.2890206-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766154833309024461?= X-GMAIL-MSGID: =?utf-8?q?1766154833309024461?= 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 Fixes: 4cf1fe34fd18 ("kselftest: vm: add tests for memory-deny-write-execute") Reviewed-by: David Hildenbrand --- 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/selftests/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 = mmap(NULL, self->size, PROT_READ, self->flags, 0, 0); ASSERT_NE(self->p, MAP_FAILED); - p = mmap(self->p + self->size, self->size, PROT_READ | PROT_EXEC, + /* MAP_FIXED unmaps the existing page before mapping which is allowed */ + p = 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); } TEST_F(mdwe, arm64_BTI) From patchwork Wed May 17 15:03:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 95337 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1212800vqo; Wed, 17 May 2023 08:14:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ65nnTJ+OiVy+cOoNT5HoZe0eDuiqKMrNEp3DQerRvRpAfHjuOVyTRcveY9NEIXplUTXFOq X-Received: by 2002:a05:6a00:1808:b0:64a:6cad:d840 with SMTP id y8-20020a056a00180800b0064a6cadd840mr1566125pfa.25.1684336497045; Wed, 17 May 2023 08:14:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684336497; cv=none; d=google.com; s=arc-20160816; b=R1qohyuvvWUQl9AD2cxEm66AWKLZx9jeSv0ob/GAsFjl9/aqUmCGrnzoMQobx8w0um P+wTfygpZa1Pf+f1c4zSsLReiQ+yguRlK+39uOQkzK+tWW1Rys8LhMyJJARH0Sr4It83 1M/BCYGKyjON4vZNpgtSLBbFVUrHTUfvv+LvWIEV5/0dukWhFsS8hx7GtuYCufP8VCtM P19UekiuJ3cp/+FjyQ/sqj3hx/rU3vgX5OiA9jshdLqxN2srKgSD5JmLrE565akOn+gP lkI3FcPdMx9pZAt/H2NgRAzdv1RsuzNckIvV/NsrGp7L3IBfDlqwLNZxXoVLaJapgeAQ 9MqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oipxQEZj91stE4t+lxud7GTslgXbD/udBSL61+uH1L8=; b=COBxNnlJFLqra/V2A18dMNgkJ+cVrXP2KtfEXfseQBX6bffLKyiqq01ZvqA0weKwXJ H49EBN3wrSD3ZrNqhIAIZvDLMqJFzCSgI2Di0ZNQZjlUeyeb8FpyMPKjSgAJoB9sjHfy elSI/GgLzXUBqJvjS1Bj0YJajE/gng+LWfxP6cNZcsX+oylkJaRNywrdEiSp8lZutiiA zaOKl+l9CimZ3NjKNFVoC1+j6WT44Mw3YNq8qqBJpv4UUbNiCl8lIY2ZrLgdzG1b3JVn n178gRfSpejgjkzxs7+gARqjSV7rZM1KMD3aKZ6gQcgz+FxYVPDN+7zu+wlQw4TJSygY Lrpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nYRCTEvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y187-20020a638ac4000000b00524f3526a91si18065348pgd.16.2023.05.17.08.14.43; Wed, 17 May 2023 08:14:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nYRCTEvO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231675AbjEQPFS (ORCPT + 99 others); Wed, 17 May 2023 11:05:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231152AbjEQPEj (ORCPT ); Wed, 17 May 2023 11:04:39 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC9887EE7 for ; Wed, 17 May 2023 08:04:20 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-30644c18072so607480f8f.2 for ; Wed, 17 May 2023 08:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684335831; x=1686927831; 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=oipxQEZj91stE4t+lxud7GTslgXbD/udBSL61+uH1L8=; b=nYRCTEvOWhb0FzekCMnJ6+34uErlW9tdW8D7eUgNnDXRCvoUVAdioTe4bDYStyLbtZ +wOkz4pBqp/FKbfovlnRGHcbww4zHsdW1pobdsCTi7FymG/8pUeZAv4ix9bN6fTKR5K0 yeoouszxWZdws1CxpHmjxNcnk9WQ5iIHC5Qms= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335831; x=1686927831; 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=oipxQEZj91stE4t+lxud7GTslgXbD/udBSL61+uH1L8=; b=Gx9noDoJncCDjnI2jPkT310rwAwaX0iUYQdKvP+lHzIZnptNn+lmq3G5TGdML5znoK kLkUFsGFnkCRGBB4ps6yIHXbW7573AjqVEvvv+pZqD3KVouWQ2Wp9AH8Rz1stTwurEjO SmPXagSA1YxDFXf7xBVxvbrBfBEKoPE2mkWuW+Okp8VxaEgF45x94d3q4RkACBMyYpHY L8ml1DzKEC45N85mjQJCQeaPP9dv0Qgj1njqh77Llv2PwDMwLCD17CzvQ/qVUvEnXBpn wk7/WHTXG96SIqlAu97x2Xp4Sjpyn6xRHZSn9EXAi2ft0nv6rPVgTJyJDNT0KGznHyb4 3pIg== X-Gm-Message-State: AC+VfDwyqt5I4Ni9s6X4GBSbBfMrAtywlz20xmfBKfhChLj7XT0izKZR DZzYxsPdKyzUI5eZ54mIy9VMePHe8nVHcD3tIH0= X-Received: by 2002:a5d:4cc9:0:b0:2fb:7099:6070 with SMTP id c9-20020a5d4cc9000000b002fb70996070mr866319wrt.47.1684335831152; Wed, 17 May 2023 08:03:51 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:e223:a0c2:d2c:c371]) by smtp.gmail.com with ESMTPSA id e17-20020adffd11000000b003047ea78b42sm3038211wrr.43.2023.05.17.08.03.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:03:50 -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 v2 3/5] mm: Make PR_MDWE_REFUSE_EXEC_GAIN an unsigned long Date: Wed, 17 May 2023 17:03:19 +0200 Message-ID: <20230517150321.2890206-4-revest@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230517150321.2890206-1-revest@chromium.org> References: <20230517150321.2890206-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766154826321129656?= X-GMAIL-MSGID: =?utf-8?q?1766154826321129656?= Alexey pointed out that defining a prctl flag as an int is a footgun because, under some circumstances, when used as a flag to prctl, it can be casted to long with garbage upper bits which would result in unexpected behaviors. This patch changes the constant to a UL to eliminate these possibilities. Signed-off-by: Florent Revest Suggested-by: Alexey Izbyshev Reviewed-by: David Hildenbrand Acked-by: Catalin Marinas --- 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 { /* Memory deny write / execute */ #define PR_SET_MDWE 65 -# define PR_MDWE_REFUSE_EXEC_GAIN 1 +# define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) #define PR_GET_MDWE 66 diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/prctl.h index 759b3f53e53f..6e6563e97fef 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -283,7 +283,7 @@ struct prctl_mm_map { /* Memory deny write / execute */ #define PR_SET_MDWE 65 -# define PR_MDWE_REFUSE_EXEC_GAIN 1 +# define PR_MDWE_REFUSE_EXEC_GAIN (1UL << 0) #define PR_GET_MDWE 66 From patchwork Wed May 17 15:03:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 95346 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:3046:b0:115:7a1d:dabb with SMTP id p6csp1101477rwl; Wed, 17 May 2023 08:28:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6f/ZOaxtyaQ24hrvNgVOncHwidy6VCLOoTtL97/JwP2n0k6ZUWYwQtVUKsQlYhq6az/ZtL X-Received: by 2002:a05:6a20:3d27:b0:100:99a:7f71 with SMTP id y39-20020a056a203d2700b00100099a7f71mr48630361pzi.2.1684337319755; Wed, 17 May 2023 08:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684337319; cv=none; d=google.com; s=arc-20160816; b=1FT6LZmSEGaJqPjmAs5gL3x4RkEqxZVXt6rSE7Fa/V5tZFWUJkW4vF3xeiioXWnPhc BW8EcDJaBf+UZ+Lp4AqJs3rNXDT0DHfI8LordjAJQEjoTfLB+h7Db3QaxkbJg8GbS7sX SaCEE3iDHMv53+i1lKLK7oTf88S2xc/sC2hxsR4vFDMJNLNX6/NVD4miN63OQZJegUxp ssL41OJ8PAUzHLeUjjlNowLrunKmSsV4aISh0EOhz9jSGpy1kcqu7MKjgNvErQfXSFVB qBkzgGplm0OFQ7zohJOPm4q9Dvkmw3qGj1VhnVItcv/UjToNWlSOvZhiZ3KvDZGpkUgk aHIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PcjJGxge+F640GphIVPPWC0i7d+7dT4zRE3gQfpttMo=; b=NBZdkrTi4Gm3ZAfa5zXbb02qZXwsVM16dgIx/mSBLKGrQCXjGLEMvdZ8MoIekvs4ND wmmVBBDXjB67NfzEKeHqnbenxvFGR/RllW1KgTDLHNndmIDwozDDzJYUqjY9rFXMJZh8 Y+j0f7NHFkl7UNfQvrwsVMvT45zyq7tWiUwTfTk8PIYquXvpR2a63QUKnse1I3Q+G2pS +XBljsXExUFRaGHqX6TB1QnCOf3RxR+CxjbaV7MYGKruaGM/m0Q07JeRtRsOF2XeT9J9 le6ua3W2Zc4SUGDAfSz7+EOMGj1/6NEl35hPQqrwDVjYlQvYWUUq4FpwUzHTEzbQTpc/ eRJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Rm1/o51a"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l192-20020a6391c9000000b0053071414c3asi17076805pge.562.2023.05.17.08.28.26; Wed, 17 May 2023 08:28:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Rm1/o51a"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232081AbjEQPGz (ORCPT + 99 others); Wed, 17 May 2023 11:06:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231577AbjEQPEp (ORCPT ); Wed, 17 May 2023 11:04:45 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 060A0AD0A for ; Wed, 17 May 2023 08:04:24 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-307c040797bso607158f8f.3 for ; Wed, 17 May 2023 08:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684335833; x=1686927833; 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=PcjJGxge+F640GphIVPPWC0i7d+7dT4zRE3gQfpttMo=; b=Rm1/o51a2lpXg5BFBk6NXPTxOQL/+q5wPYqudYkYD0LO0vbb3G0X6cU/WUbHK9OmTT xyGPbYoI6XKipdHkAru8gZICWPEsUbwO6Jkk3dOBXa87Qq8BY6bIYCXC4rzbLx1Q5gZs 9jEBolPQhL6s6/L97SOAkqIjsQdHbRbx6RyLY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335833; x=1686927833; 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=PcjJGxge+F640GphIVPPWC0i7d+7dT4zRE3gQfpttMo=; b=V8Yr3z4cUWrhcAuuoqf+g/27c2BoijkcUw2TetXgoMLCjreUXNK1+gqMg7EqJVPMNP JS9lsmTgHvvtOhHQwTppWhXFZxuWbsq+gzib1aiKpDBKAJmnsuuggnwGK0pRslzrZK0p sv+k9CyUe3LemZ6SxZUJ3Mz2MAfC2EpFyTfSaffoGJgmsSmPx+a4TDeYPWvwXIwDm8a3 uy+XUnH68gB1QodG0fkplM8K1eyrL5mHPmPQOWmSE/I38GsaULsk4/T+UlWAdEebGZdM tY7y6dMDpSeaC2oVioQINT0vcRbafudjnio4c61B+N7C5ab21LbUdEHWbye61QmFhNHl iScA== X-Gm-Message-State: AC+VfDwzgr5gvYEMKQOBXTKEcNmYzBTqfctDtN+QH6Gez1PlA+pNI+jK ifWZusrp7VXU99JYjkUj837id5jxihJvBZXZAuA= X-Received: by 2002:a5d:4a50:0:b0:2f6:1a30:605c with SMTP id v16-20020a5d4a50000000b002f61a30605cmr999306wrs.3.1684335832915; Wed, 17 May 2023 08:03:52 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:e223:a0c2:d2c:c371]) by smtp.gmail.com with ESMTPSA id e17-20020adffd11000000b003047ea78b42sm3038211wrr.43.2023.05.17.08.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:03:52 -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 v2 4/5] mm: Add a NO_INHERIT flag to the PR_SET_MDWE prctl Date: Wed, 17 May 2023 17:03:20 +0200 Message-ID: <20230517150321.2890206-5-revest@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230517150321.2890206-1-revest@chromium.org> References: <20230517150321.2890206-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766155689580698861?= X-GMAIL-MSGID: =?utf-8?q?1766155689580698861?= 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 --- include/linux/sched/coredump.h | 10 ++++++++++ include/uapi/linux/prctl.h | 1 + kernel/fork.c | 2 +- kernel/sys.c | 24 +++++++++++++++++++++--- tools/include/uapi/linux/prctl.h | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 0ee96ea7a0e9..11f5e3dacb4e 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) #define MMF_VM_MERGE_ANY 29 +#define MMF_HAS_MDWE_NO_INHERIT 30 + +#define MMF_INIT_FLAGS(flags) ({ \ + unsigned long new_flags = flags; \ + if (new_flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) \ + new_flags &= ~((1UL << MMF_HAS_MDWE) | \ + (1UL << MMF_HAS_MDWE_NO_INHERIT)); \ + new_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) #define PR_GET_MDWE 66 diff --git a/kernel/fork.c b/kernel/fork.c index d17995934eb4..62d52ad99937 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); if (current->mm) { - mm->flags = current->mm->flags & MMF_INIT_MASK; + mm->flags = MMF_INIT_FLAGS(current->mm->flags); mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; } else { mm->flags = default_dump_filter; diff --git a/kernel/sys.c b/kernel/sys.c index 339fee3eff6a..320eae3b12ab 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2368,9 +2368,25 @@ static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3, if (arg3 || arg4 || arg5) return -EINVAL; - 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; + + /* Can't gain NO_INHERIT from !NO_INHERIT */ + if (bits & PR_MDWE_NO_INHERIT && + test_bit(MMF_HAS_MDWE, ¤t->mm->flags) && + !test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags)) + return -EPERM; + + if (bits & PR_MDWE_NO_INHERIT) + set_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags); + else if (test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags) + && !(bits & PR_MDWE_REFUSE_EXEC_GAIN)) + return -EPERM; /* Cannot unset the flag */ + 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)) @@ -2385,8 +2401,10 @@ 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 (test_bit(MMF_HAS_MDWE, ¤t->mm->flags) ? + PR_MDWE_REFUSE_EXEC_GAIN : 0) | + (test_bit(MMF_HAS_MDWE_NO_INHERIT, ¤t->mm->flags) ? + PR_MDWE_NO_INHERIT : 0); } 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/prctl.h index 6e6563e97fef..f7448d99520c 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) #define PR_GET_MDWE 66 From patchwork Wed May 17 15:03:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 95338 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1212885vqo; Wed, 17 May 2023 08:15:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ56VwjcuL8DPRvwfzc3nUk4YMgc+J3JLyUwkXL//nCh9bOq8FS2kxoZnGwmu+3jEiSm88o9 X-Received: by 2002:a17:902:e810:b0:1a1:f5dd:2dce with SMTP id u16-20020a170902e81000b001a1f5dd2dcemr49547701plg.6.1684336503269; Wed, 17 May 2023 08:15:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684336503; cv=none; d=google.com; s=arc-20160816; b=K7hnosoLye6lBp4JSiTi6sNg9YLltMCV0RRjwFdZQtISmuPw1/xLaDJ/zXsxGJePxr ODhgG43YtxJsoYxUPK9COldymvaTWmRW9IlEXa6RoA6OXMZSGy6trNiJW/v8ybGizZfL KaI6CA5A4swcdfne7sAIepj69pAY1mzmOloEabbpxLSj8cANqVGXT7V+L2LOfEZZ9rnR 0DfjgNX2Uy3cbdPejA8bu9xwmXCh+qMckC7ACAHThGqIOI+mlxTGq7bs0HHrKJw6i0+e 4mozL7LO8kSZkgCyfJZCNbR9ngMRIkHcryyt8T9TStX9HEB8rpEBzxy53pcOrvhGnV1s rU4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XZna8pJSuLGWyf+gz4k0SP+q2QXja3naQzgW+6vcrp4=; b=g+rsODWfbI45NA8uMKoX0/MFEeUfI0pIHVHts9NHlMBTJ3kjk7QjoFSeSNyf+ZEKj9 D5rogEPisGs7jdrxKAdEyEO6ksWmPSoZ6HnXAwMrrfVx6Cv0OfwV+Q7kgHdyP32Eb/s5 C9p7PLKmClawb3RLoP2PSJ3RUiuVCFRFIRS0houS6jDWy6y25mGuAks8WRb/fYVZVSUC UCxWjxJCyl2rv+BIVHcG4hESEa0PQQCCqaslLm3nNB2pbW7inirOdQwXKEzTdLIQDcxI mJy0iAA8+Ea/csCmJnIpAXoGH/mAfUWB+ZBCaneasWtklBjm/wGfNm7C5ziH9OQ5dhpC mgQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VOiuLe3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jj9-20020a170903048900b001a6d08dc834si19847803plb.22.2023.05.17.08.14.49; Wed, 17 May 2023 08:15:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VOiuLe3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232123AbjEQPFX (ORCPT + 99 others); Wed, 17 May 2023 11:05:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232023AbjEQPEp (ORCPT ); Wed, 17 May 2023 11:04:45 -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 325B8AD3B for ; Wed, 17 May 2023 08:04:25 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3078c092056so629235f8f.1 for ; Wed, 17 May 2023 08:04:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684335835; x=1686927835; 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=XZna8pJSuLGWyf+gz4k0SP+q2QXja3naQzgW+6vcrp4=; b=VOiuLe3bG4vHmfbabe07F3nlpYx08FfV/3zQLCyMZi11R5cM1HKH5T65JN6ugqyHwi G99lvNgNYrIk8eHNaql93JvIcv3h0iKEaBhgawogRCgZMmftz/RTh2Bf/JXFZ7nUlRzk EK6wmUgGQj7vyyj5mggmdbjXb8QJLmSkQc7OE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335835; x=1686927835; 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=XZna8pJSuLGWyf+gz4k0SP+q2QXja3naQzgW+6vcrp4=; b=kytnbw/P3JT+3g/5MAfwgNdMbHu/z+8Tldwb48wrJcigwtwIS5lovgBb630slN/Dnv oEpWOG/YpHMRWamWB5wbQhLO8JuGH/68BJ2FYtvVdCdIkY18oBZF4URhS2L+tV+rZJ1E +VOrT4EkJvpXF2eV+z5xdUPXDi6rY/iI/lx/LSJFIkLd+DzB/E3wOZ8G6x7xofjqYgNn UPNXiCiER5sVuV5UgJ/8/OqEyKCD24Wh6ysKUilqJQ7hMSC9Nrpei540ok/a3vcUUDBZ 2m53Kw7R6HIPntSmiEOH7p7vTt5M1nlAi3oa9mwMCCrYTJSqb7I0x3ACwuNqhKLYJYRX VFgA== X-Gm-Message-State: AC+VfDymaDs8MZC+7aJjkWr9ENmeuvOZFtLvAZX2k0Pxk3Qsa0KHAiQ6 ZaBzxQCXodTNS/wO1Ui4xMw+mAzlP6cr5i0jEhc= X-Received: by 2002:adf:ee89:0:b0:306:2979:8ab0 with SMTP id b9-20020adfee89000000b0030629798ab0mr1017312wro.24.1684335834709; Wed, 17 May 2023 08:03:54 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:e223:a0c2:d2c:c371]) by smtp.gmail.com with ESMTPSA id e17-20020adffd11000000b003047ea78b42sm3038211wrr.43.2023.05.17.08.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:03:54 -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 v2 5/5] kselftest: vm: Add tests for no-inherit memory-deny-write-execute Date: Wed, 17 May 2023 17:03:21 +0200 Message-ID: <20230517150321.2890206-6-revest@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230517150321.2890206-1-revest@chromium.org> References: <20230517150321.2890206-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766154832786403869?= X-GMAIL-MSGID: =?utf-8?q?1766154832786403869?= Add some tests to cover the new PR_MDWE_NO_INHERIT flag of the PR_SET_MDWE prctl. Signed-off-by: Florent Revest --- tools/testing/selftests/mm/mdwe_test.c | 95 ++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/mdwe_test.c b/tools/testing/selftests/mm/mdwe_test.c index 91aa9c3099e7..9f08ed1b99ae 100644 --- a/tools/testing/selftests/mm/mdwe_test.c +++ b/tools/testing/selftests/mm/mdwe_test.c @@ -22,6 +22,8 @@ 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,66 @@ TEST(prctl_flags) EXPECT_LT(prctl(PR_GET_MDWE, 0L, 0L, 0L, 7L), 0); } +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 = PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags = PR_MDWE_REFUSE_EXEC_GAIN, + .should_work = true, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_disable_mdwe) +{ + .first_flags = PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags = 0, + .should_work = false, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_disable_mdwe_no_inherit) +{ + .first_flags = PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .second_flags = 0, + .should_work = false, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, can_lower_privileges) +{ + .first_flags = PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .second_flags = PR_MDWE_REFUSE_EXEC_GAIN, + .should_work = true, +}; + +FIXTURE_VARIANT_ADD(consecutive_prctl_flags, cant_gain_privileges) +{ + .first_flags = PR_MDWE_REFUSE_EXEC_GAIN, + .second_flags = PR_MDWE_REFUSE_EXEC_GAIN | PR_MDWE_NO_INHERIT, + .should_work = false, +}; + +TEST_F(consecutive_prctl_flags, two_prctls) +{ + int ret; + + EXPECT_EQ(prctl(PR_SET_MDWE, variant->first_flags, 0L, 0L, 0L), 0); + + ret = prctl(PR_SET_MDWE, variant->second_flags, 0L, 0L, 0L); + if (variant->should_work) { + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } +} + FIXTURE(mdwe) { void *p; @@ -45,28 +107,45 @@ FIXTURE_VARIANT(mdwe) { bool enabled; bool forked; + bool inherit; }; FIXTURE_VARIANT_ADD(mdwe, stock) { .enabled = false, .forked = false, + .inherit = false, }; FIXTURE_VARIANT_ADD(mdwe, enabled) { .enabled = true, .forked = false, + .inherit = true, }; -FIXTURE_VARIANT_ADD(mdwe, forked) +FIXTURE_VARIANT_ADD(mdwe, inherited) { .enabled = true, .forked = true, + .inherit = true, }; +FIXTURE_VARIANT_ADD(mdwe, not_inherited) +{ + .enabled = true, + .forked = true, + .inherit = false, +}; + +static bool executable_map_should_fail(const FIXTURE_VARIANT(mdwe) *variant) +{ + return variant->enabled && (!variant->forked || variant->inherit); +} + FIXTURE_SETUP(mdwe) { + unsigned long mdwe_flags; int ret, status; self->p = NULL; @@ -76,13 +155,17 @@ FIXTURE_SETUP(mdwe) if (!variant->enabled) return; - ret = prctl(PR_SET_MDWE, PR_MDWE_REFUSE_EXEC_GAIN, 0L, 0L, 0L); + mdwe_flags = PR_MDWE_REFUSE_EXEC_GAIN; + if (!variant->inherit) + mdwe_flags |= PR_MDWE_NO_INHERIT; + + ret = prctl(PR_SET_MDWE, mdwe_flags, 0L, 0L, 0L); ASSERT_EQ(ret, 0) { TH_LOG("PR_SET_MDWE failed or unsupported"); } ret = prctl(PR_GET_MDWE, 0L, 0L, 0L, 0L); - ASSERT_EQ(ret, 1); + ASSERT_EQ(ret, mdwe_flags); if (variant->forked) { self->pid = fork(); @@ -113,7 +196,7 @@ TEST_F(mdwe, mmap_READ_EXEC) TEST_F(mdwe, mmap_WRITE_EXEC) { self->p = 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 +222,7 @@ TEST_F(mdwe, mprotect_add_EXEC) ASSERT_NE(self->p, MAP_FAILED); ret = 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 +237,7 @@ TEST_F(mdwe, mprotect_WRITE_EXEC) ASSERT_NE(self->p, MAP_FAILED); ret = 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);