From patchwork Fri Aug 5 22:21:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 12937868 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 901A9C00140 for ; Fri, 5 Aug 2022 22:21:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B15F8E0005; Fri, 5 Aug 2022 18:21:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 262298E0003; Fri, 5 Aug 2022 18:21:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 101FD8E0005; Fri, 5 Aug 2022 18:21:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 009638E0003 for ; Fri, 5 Aug 2022 18:21:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D7D1E1A1913 for ; Fri, 5 Aug 2022 22:21:56 +0000 (UTC) X-FDA: 79766962632.24.0988553 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf08.hostedemail.com (Postfix) with ESMTP id BDD28160143 for ; Fri, 5 Aug 2022 22:21:54 +0000 (UTC) Received: by mail-pl1-f202.google.com with SMTP id f13-20020a170902ce8d00b0016eebfe70fcso2270458plg.7 for ; Fri, 05 Aug 2022 15:21:56 -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=3bUyl5ZfpwXuIiwiOHfthc8xa9NTsUDi3aTinpa20daWJcb178p7ToNo5YeP0p2HIq 9Q54GHj4FVKsLgjAbNK56mK/BCG5m7emMO++nG0Z2oXHIzCvgoFKYifWaawgxQa+oZXA 1DXmboReVFqbsHLDijsDcm7BjZhZKVOcLCSjHfJL6koDScklAPpqjmmLIEx3Md0dsNMW h9Xj0s/OuOXev58rz9voBVfI7z6IRNeApSkmXcWAv3J9aTQyUfmTD18EXvXQ43nw9300 QfVwug+xWJD5QNDj+9t9XJjCNAUgYUT7w9k2KWCzf5hM+nZXoBqKrS/5kajp6gEk/DH0 vQhg== X-Gm-Message-State: ACgBeo0/zJ+47eG/bLVMICu/aRXTjDLNbV6O0nVtxnuNyjBTr4oUOwV/ 0/nuWqVq8ZGplMm4YRj6PcYObf3whn4= X-Google-Smtp-Source: AA6agR5SYNgjRPD70HCAO4QbLAX75hHQo60LfWD/m1Gc2TjfjizdokqQLPw4iq40ChDMtnBNielltDU4BD0= 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659738116; a=rsa-sha256; cv=none; b=oz5nn16t0oduYh5iNahjaKzNstVZNftzvmF0JwFDhIrRNwRZBAeHEurRb/8guRvjJaYazK IcgjkP0yM+H4o/CsmP4pgHcavNvMatIFa5M0xJUrp/ZsHnoaR2B0sOMFTA16Ae1KDH6XWv aZHjkS11if5DoWWXyyRAkuml9vBCkl8= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FsK4U64s; spf=pass (imf08.hostedemail.com: domain of 3A5jtYgYKCAgrmnn52owwotm.kwutqv25-uus3iks.wzo@flex--jeffxu.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3A5jtYgYKCAgrmnn52owwotm.kwutqv25-uus3iks.wzo@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659738116; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=18f+8YL6aOvEHaZ2GuJdUMnF89FwCoTjUmHERMQZXDE=; b=VbuzzfezCEb3rLiiLAwiWR/e2MW0sdP7UVNkabk5sNFYYc1k4IasO8CybieSb+KW/bRnNA mymnFqGt3eQd5ITT/gA0uLV6Jc0DYEyVrQfC1NnofISIV9iXQ8ED8Ii9caY4BmEK71yQXY mU8cDw1NKjcRpybqkwWhAR8Wm/YBWWQ= X-Stat-Signature: pkmw4sgq3n3c75d914ex3m45xhe9p9oy X-Rspamd-Queue-Id: BDD28160143 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FsK4U64s; spf=pass (imf08.hostedemail.com: domain of 3A5jtYgYKCAgrmnn52owwotm.kwutqv25-uus3iks.wzo@flex--jeffxu.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3A5jtYgYKCAgrmnn52owwotm.kwutqv25-uus3iks.wzo@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1659738114-449982 X-Bogosity: Ham, tests=bogofilter, spamicity=0.007551, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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();