Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp2204969rwb; Fri, 5 Aug 2022 15:41:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR7mzC9GTPl8DShEP5lAJ8UAsSJVzSnN5SN0/OzQXsm00xnyks31x3BMJcj+RSDGOlpg+CYs X-Received: by 2002:a05:6402:540c:b0:434:d965:f8a with SMTP id ev12-20020a056402540c00b00434d9650f8amr8437167edb.30.1659739288241; Fri, 05 Aug 2022 15:41:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659739288; cv=none; d=google.com; s=arc-20160816; b=HjID38pJf2HL30aK5Gh6oQIKEDsGFiVHelfeX12K5O/QFUU8+Ulp8oZM9TiB84T+wL dJK2WRYV1Hj3HsyFsn6tTeDYTP9qXIZbrwfE69gTZCd3/DHPWMEbDelPtmeSaDP9nMWb 5ssBCsFyuXc4cyMrg3e2uXYQGajl2DCBRIBpWzA94sZmoUTV3ZOq0MEFEiSaHHVsBzWq xwAPBErbdedLOMrxt1c1G1yoKTwO6tVZ937Y8ISRsnQ7O48nZDxw2JtoYw61TAgiIBPU ouhZkBqiEscP7lairCS4zBFvD8Iyw5fa/UFDvzqefrf+QXdPRg46oZ0CMSs6Oi/+gR7B gyew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=18f+8YL6aOvEHaZ2GuJdUMnF89FwCoTjUmHERMQZXDE=; b=iEVgbCDG82vnkM76KO4IyfUKbgq8wo11TWqPCx4WmJhSqFR74mXhioQjaPPuvyTmja UnAJp97rJmR/47O1dzcBiyQNBj1D3k7F70WeIu2nCjvZsAZ8P4WB3lMrbbDjfsJGeib9 og5vSIIZgaxxiCMdNkDVrzEwuLLHb41p73hteSA+/dPI9YwLGk/kMbzHL5uh4DXuUrTr oq+QmKYgnXOZ0BY4l2g3fLRDFdxwxybIvvqIn+oljOBi02KY50XrTO2GzNHgsW6NVfJv 5U9/FSqCi6ikjStCAPoUKf9H/KoobnpB/wOYojmfZTioJcSQAJWKJ0+Ll0PAeZief7qI +asw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FsK4U64s; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q2-20020a1709064cc200b007307f9e6b64si4205494ejt.547.2022.08.05.15.41.01; Fri, 05 Aug 2022 15:41:28 -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=@google.com header.s=20210112 header.b=FsK4U64s; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241849AbiHEWWJ (ORCPT + 99 others); Fri, 5 Aug 2022 18:22:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241779AbiHEWV4 (ORCPT ); Fri, 5 Aug 2022 18:21:56 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C3A11ADAF for ; Fri, 5 Aug 2022 15:21:55 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id y17-20020a63e251000000b0041d322b88b6so173779pgj.2 for ; Fri, 05 Aug 2022 15:21:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=18f+8YL6aOvEHaZ2GuJdUMnF89FwCoTjUmHERMQZXDE=; b=FsK4U64sZVq+y1SyuqOQStvSBS7X8QECCn6LIsVMr2xn0nkzLI8+c5D4j5EMd+AUhk j3t/30i3HkCUWA8GRrEV/n5la5QXWMAMmnEib1Gi4341JdPl8J5TfnHLWNa+y5fVP+eV oJ9bBE123va/TwKlbkn1J19gX+WOPS5lk3FwZlPtz0EcE3ZPCeaSKNYq9YOdEzFcDRFj lzgI8luSNRGb7P0bwY+aM7gsZ7WkQblht0scSjo8RmMJapESKs0qw+DFJAb+seJ/d+fa mzUs1GKxmI0hZowF1OygbHCwUgPDBmTWHAd9Z5XtwUGQJ2j7xvAyT0Qa4igHULcSRtrM vWkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=18f+8YL6aOvEHaZ2GuJdUMnF89FwCoTjUmHERMQZXDE=; b=LDFgZmATr2+n85CsSaAXdxzclozTdaDUQjUFwtFFzJM8+BiuxwiPIplaYTR1wr3S2v pFPeS9XV4aeCF2t0uxsE7fejclcLLijbGZO0eYa/NIepS8Pt01ARwWMecx5FMqeLdZNl 8+MsTijDXyKvMDtbpJhl7elHW/PyR/8wp0wgvqaZFgYScArr9X0BE5ZWJBG4Gd7ePmyt sYUyLLjJ5PHo6PSstdwbF7yoq1HcF8O8hIzyHZpBVA63sbhLN35qYzFsds4G3mqVB0mX EyeL21uXiC2iP1hoo8LnG4lJXo8gHrEHmt6cUKW4wlhvqu87eesWc15y/SWKPXKiV5G7 +ndQ== X-Gm-Message-State: ACgBeo34+mEviIUStsYMqD42ua1mPNiiGgbGLfBv0zeGa6PFu63XsBAV BNRgK1GddHzeTZZhlyhGeTexatSzS14= X-Received: from jeffxud.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:e37]) (user=jeffxu job=sendgmr) by 2002:a05:6a00:22d0:b0:52e:49f3:3f52 with SMTP id f16-20020a056a0022d000b0052e49f33f52mr8783529pfj.54.1659738115213; Fri, 05 Aug 2022 15:21:55 -0700 (PDT) Date: Fri, 5 Aug 2022 22:21:25 +0000 In-Reply-To: <20220805222126.142525-1-jeffxu@google.com> Message-Id: <20220805222126.142525-5-jeffxu@google.com> Mime-Version: 1.0 References: <20220805222126.142525-1-jeffxu@google.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog Subject: [PATCH v2 4/5] selftests/memfd: add tests for MFD_NOEXEC From: To: skhan@linuxfoundation.org Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jeffxu@google.com, jorgelo@chromium.org, keescook@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, mnissler@chromium.org, jannh@google.com, Jeff Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 From: Daniel Verkamp Tests that ensure MFD_NOEXEC memfds have the appropriate mode bits and cannot be chmod-ed into being executable. Co-developed-by: Jeff Xu Signed-off-by: Jeff Xu Signed-off-by: Daniel Verkamp --- tools/testing/selftests/memfd/memfd_test.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 1d7e7b36bbdd..4906f778564e 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -36,6 +36,10 @@ #define MAX_PATH 256 #endif +#ifndef MFD_NOEXEC +#define MFD_NOEXEC 0x0008U +#endif + /* * Default is not to test hugetlbfs */ @@ -1006,6 +1010,35 @@ static void test_seal_exec(void) close(fd); } +/* + * Test memfd_create with MFD_NOEXEC flag + * Test that MFD_NOEXEC applies F_SEAL_EXEC and prevents change of exec bits + */ +static void test_noexec(void) +{ + int fd; + + printf("%s NOEXEC\n", memfd_str); + + /* Create with NOEXEC and ALLOW_SEALING */ + fd = mfd_assert_new("kern_memfd_noexec", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + mfd_fail_chmod(fd, 0777); + close(fd); + + /* Create with NOEXEC but without ALLOW_SEALING */ + fd = mfd_assert_new("kern_memfd_noexec", + mfd_def_size, + MFD_CLOEXEC | MFD_NOEXEC); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC | F_SEAL_SEAL); + mfd_fail_chmod(fd, 0777); + close(fd); +} + /* * Test sharing via dup() * Test that seals are shared between dupped FDs and they're all equal. @@ -1179,6 +1212,7 @@ int main(int argc, char **argv) test_create(); test_basic(); + test_noexec(); test_seal_write(); test_seal_future_write(); -- 2.37.1.559.g78731f0fdb-goog