From patchwork Fri Apr 1 22:08:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Verkamp X-Patchwork-Id: 12798823 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B3E4C433EF for ; Fri, 1 Apr 2022 22:09:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8B126B0071; Fri, 1 Apr 2022 18:09:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B39D46B0072; Fri, 1 Apr 2022 18:09:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A01D88D0001; Fri, 1 Apr 2022 18:09:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id 90AC06B0071 for ; Fri, 1 Apr 2022 18:09:07 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4C9368155E9C for ; Fri, 1 Apr 2022 22:08:57 +0000 (UTC) X-FDA: 79309701114.24.F0CF431 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf23.hostedemail.com (Postfix) with ESMTP id DFCBA140005 for ; Fri, 1 Apr 2022 22:08:56 +0000 (UTC) Received: by mail-pl1-f177.google.com with SMTP id x2so3560424plm.7 for ; Fri, 01 Apr 2022 15:08:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lOkjGfGfl98SeySdZvzcrWOJEVOKytJoGvUFiOZ1aAI=; b=k3QYjMQbIkpZJu+RzninncrAbEnag3EmGlo4bQTg2Fc2+RK1oPVHKalNH09F6XaLOx k24r7rfB5xVIZvuu0s2NKWZ3DovCWTvt6Y0Y9zbNT6yHqQFH7IA3fZLbuFyHZSQeurjy G+A9aaph7FDTPINrdzYeKwqcjp/tXVW3oDQfc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lOkjGfGfl98SeySdZvzcrWOJEVOKytJoGvUFiOZ1aAI=; b=OnWKSpZtYXhH9f8jFuHpfZytuDRlgFE8/Sg5YJaOU33C/evTFRkT4/zyu9Bi/pVzDu Ygpf3D9L2r/VBckrKUkdTafHWtnI1WQMndghJObchKuzte7BEJ/q/i4rdgq86IJrgcUE 5Zz/aOQ29RMaNdI1U65gJSsbuBXcLjcMbcOAVKyVwoNoDuIDusVnWRwKDkRe7S7CwBl5 C3wfrvTbTLGwrwPSC+4y7jmQao8flanJCl2aXuD0ABZprFZucLZuiA2Nk38BHnoS7Ado Hh74AeEkk86Of+9R4/5sduSlE2rUaImqjNH+SB5NFh0R4FSODsqdnKcema7/xof3vJSB DTWQ== X-Gm-Message-State: AOAM532w1rq3IqEGW8qOr30oe8C0qOJ0mQ75r4sqjamyBHYMAWTOQsN9 vAIpcYKgiQt+kGYCmXpiL8H+a6QzRjk0Cg== X-Google-Smtp-Source: ABdhPJw/qfYgMATifUmtsmB46MXU37QSE6npLKOGpPlqsWgKaI76Pgjk/RohjePaQficI+4rGcFbvQ== X-Received: by 2002:a17:90b:30ca:b0:1c9:a577:5e8c with SMTP id hi10-20020a17090b30ca00b001c9a5775e8cmr14185468pjb.227.1648850935864; Fri, 01 Apr 2022 15:08:55 -0700 (PDT) Received: from localhost ([2620:15c:202:201:72c9:527e:d936:c24b]) by smtp.gmail.com with UTF8SMTPSA id b19-20020a17090ae39300b001ca070d9dafsm8369294pjz.19.2022.04.01.15.08.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Apr 2022 15:08:55 -0700 (PDT) From: Daniel Verkamp To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrew Morton , Hugh Dickins , Mattias Nissler , Dmitry Torokhov , Kees Cook , Daniel Verkamp Subject: [PATCH 4/4] selftests/memfd: add tests for MFD_NOEXEC Date: Fri, 1 Apr 2022 15:08:34 -0700 Message-Id: <20220401220834.307660-5-dverkamp@chromium.org> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog In-Reply-To: <20220401220834.307660-1-dverkamp@chromium.org> References: <20220401220834.307660-1-dverkamp@chromium.org> MIME-Version: 1.0 X-Stat-Signature: yzaumt5cpy4gakxt84fus75hw8w6dhak X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFCBA140005 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=k3QYjMQb; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf23.hostedemail.com: domain of dverkamp@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=dverkamp@chromium.org X-Rspam-User: X-HE-Tag: 1648850936-510844 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Tests that ensure MFD_NOEXEC memfds have the appropriate mode bits and cannot be chmod-ed into being executable. 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 fdb0e46e9df9..a79567161cdf 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -32,6 +32,10 @@ #define F_SEAL_EXEC 0x0020 #endif +#ifndef MFD_NOEXEC +#define MFD_NOEXEC 0x0008U +#endif + /* * Default is not to test hugetlbfs */ @@ -959,6 +963,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. @@ -1132,6 +1165,7 @@ int main(int argc, char **argv) test_create(); test_basic(); + test_noexec(); test_seal_write(); test_seal_future_write();