From patchwork Thu May 4 17:09:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 90158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp484526vqo; Thu, 4 May 2023 10:18:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5txsreu/SaBz6ZESZWImZ8Afqqgi7cJ+bVqBMLF5n+oR3SdFjp7gWKk+xgYOvTchfzXMJ1 X-Received: by 2002:a05:6a00:1a11:b0:63b:89ba:fc9c with SMTP id g17-20020a056a001a1100b0063b89bafc9cmr3602098pfv.27.1683220679897; Thu, 04 May 2023 10:17:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683220679; cv=none; d=google.com; s=arc-20160816; b=oejHE8T25/2Qdn/tCnQVCGTCwYIS/A3aLWhFVrqmfgGB6Shk1oI3dk/DPoKDwPazde ou0y9iOXpKRDWa2+EuJ6x4FYbVP9K6QrUPNtB2dVL+7pKV7+q+77W+mHtpmhKUEu86p5 K3PuMOmpgjb9HgFxON+8AnKSqU2py0qZgx8oCXC41PyUK3jsWLYU9RfDr0Oco/9R4jxh 8kPCs3G4ZGMOlkThnafxcU8O1sAEQkPpEWwcGNpfwOodnyz7cAw0XYDrpKk+Lsk3VYrs reKYWyw/+xRoJZmg1hjOPS7iZwSa2+3ijQELK9OonM+wTYgxL2dxEK582UZiz0zRduEl Xcrg== 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=1SRzuJqpvnzSAOXKSWghhiqVYW6J1y/TsKOBytQBtN4=; b=VMj/Z3oLWi1/hH7R5ab9IVzzIGIzlF8xzAuX4jEEzEb+Mp+2bhVv1vM/H93Cw2bZb8 uCnDFRR96cPq/e1GqXgAWISTcaMJw4Ko/7YrQ1Kce9YJTe6w1NeXcio2lQNls9yrBatw RNFMH7iYZ2gVo3Z+AxZanxWqNmRsRVcUJvARBhDFLrjyafqOr/5ZdMVoCFrz6y/4vR/Z +4gFWojvqGQQlG8eVZM5dSGX8CVV2R26kI8cRcs4GCSTFESEMMS/BIDLVGlohBKEGbkL kdPD6dgXMBUC7jpF90iI+eTx8o1i9XgeNksoxNSe3tSGclvJyK/veOutxwRfsZDJ0mV1 A1zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=b0n2C4i8; 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 v2-20020aa799c2000000b00640ee8ddf24si23691159pfi.87.2023.05.04.10.17.42; Thu, 04 May 2023 10:17:59 -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=b0n2C4i8; 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 S229897AbjEDRKZ (ORCPT + 99 others); Thu, 4 May 2023 13:10:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229813AbjEDRKS (ORCPT ); Thu, 4 May 2023 13:10:18 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 921A944A0 for ; Thu, 4 May 2023 10:10:16 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-3064099f9b6so534494f8f.1 for ; Thu, 04 May 2023 10:10:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1683220214; x=1685812214; 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=1SRzuJqpvnzSAOXKSWghhiqVYW6J1y/TsKOBytQBtN4=; b=b0n2C4i8lymk8dRpWOfTuJnCia7q/qvlmX+zU/kRwHQNNJ4h/PvDPJfE3TW4FuEQyq GxNI2PXk7U7odx4x/SMJ8jZxUvjY9mXIu6yBOFpsQlRvi71FueOZC0jH/GF+2fU/HMk9 5hofoAb5Ek/MaQvYilxsgGZjtqaxikqck0/r8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683220214; x=1685812214; 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=1SRzuJqpvnzSAOXKSWghhiqVYW6J1y/TsKOBytQBtN4=; b=DZ/ol24vVKTFTJftGl6yuvlcUlYNwHIZudMIAGGQZnTbjfeonoJsbTlRRQJ3foLkSK 47WGx941O0VlpCYUDzXC/YbNdZnQS4RzZ/xcOWXOiCob5IkYzmoAaNRvQrtC3gyGYcB2 rT7X8HJ/67BcUDt2ckubHuPAqn3ippwdJqJYoXcbX5eOUROzQ8gv44eQk1jrsnriN23k T04BdL3Kzi/cvEb1Tct1x6/iMePinzvx2CPonaGEPic1sqqjwJbVb085FUgOBYC2RnKX +FZIyst+EsuDHdbA/1jsAb6e+7Z734c4d03ibeSsStygDUwr7v4Y3B5UGvV5yQ8FHo1x oK9A== X-Gm-Message-State: AC+VfDz5RM3ig+ULp7Iq0X4n1MnSzK2ckKpZKSiWd+2kNxA0fBrHnVeC Ky6eaxgZozlomq1KIVhkXmtXAIInh9kizdBnIZ0= X-Received: by 2002:a05:6000:108f:b0:307:4d2c:8353 with SMTP id y15-20020a056000108f00b003074d2c8353mr1400687wrw.53.1683220214530; Thu, 04 May 2023 10:10:14 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:9d:6:c740:f74d:132c:ca99]) by smtp.gmail.com with ESMTPSA id q3-20020a1cf303000000b003f3157988f8sm5447895wmq.26.2023.05.04.10.10.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 10:10:14 -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, nd@arm.com, broonie@kernel.org, szabolcs.nagy@arm.com, Florent Revest Subject: [PATCH 4/4] kselftest: vm: Add tests for no-inherit memory-deny-write-execute Date: Thu, 4 May 2023 19:09:42 +0200 Message-ID: <20230504170942.822147-5-revest@chromium.org> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog In-Reply-To: <20230504170942.822147-1-revest@chromium.org> References: <20230504170942.822147-1-revest@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.3 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?1764984807709424170?= X-GMAIL-MSGID: =?utf-8?q?1764984807709424170?= 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);