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);