From patchwork Fri Jul 29 06:15:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 12931956 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 2B715C00144 for ; Fri, 29 Jul 2022 06:15:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C9DBC94000B; Fri, 29 Jul 2022 02:15:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4D486B0074; Fri, 29 Jul 2022 02:15:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3C0A94000B; Fri, 29 Jul 2022 02:15:08 -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 A6EC56B0072 for ; Fri, 29 Jul 2022 02:15:08 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7E71A8111F for ; Fri, 29 Jul 2022 06:15:08 +0000 (UTC) X-FDA: 79739124696.05.69027A5 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 16BE8400A3 for ; Fri, 29 Jul 2022 06:15:07 +0000 (UTC) Received: by mail-il1-f201.google.com with SMTP id n6-20020a056e02100600b002dde21bcbe5so2463367ilj.18 for ; Thu, 28 Jul 2022 23:15:07 -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=hjVlZubyermfSkZizgaaxGB+KlUF18YguF7xhPiT/Ik=; b=mpb2InHEhH2Sm2XbOc+6nFidWtRjX48upV3ID2C0mZq33mBbL2LkFJDrwU0RgRfcfp CGD/5pjCizX/0U2QYQDrmNV6cxJPPVVrXc6JoqnuC3S6TlIgPIOgiv4JMFfw5iX1yQwZ uVQOwuh108d/qhSCddg4TYbrkdw8qB1afCSPXDA3StlvbI+X88SxRkRzNULWeei6GS+l LqAP3YzqRqGidDfaT7SlBaH/HCFCczYSPbdiaDKp23k/W0m+RwmMD1ZTNx6y51UlCnJG CFEn7BKNrVf2EHB0NrXGZ8i7oz8R8rUar3ilz/J444yv+8rBH8lb34dAI+B8yLNbkzI5 CP7Q== 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=hjVlZubyermfSkZizgaaxGB+KlUF18YguF7xhPiT/Ik=; b=BT0jzNPcAJC2ydQefNqJIPhBcRmBI5G2oSr1NLfdkr9occH9g5QUUdeh0xZ+QNK6AM AcqXZ7BhTlK9DpTSbeaHgiLgzIhPVJCJkLhWhMOVgDmLIAFftOauiQUyg2GVZI8ITO9x CU4cfM7cMji2NPj49ySTCL0aLFn/pJXzkN+k0VSJWk1jgxQlJrvOwjiKE1vrDozPCIN+ CdoN68OANF+1eb2y1Mai2VgSZhDbX87qoU/DZbATSjgPlsThDqLUXBNI/knjN75hyD0+ xQIG2Apqx1QORnT3jnl5OlM5rMEUIBRake+OdN+KLuT9AH9hOSu7WcfAUhIXeR1URcBm +QIg== X-Gm-Message-State: AJIora8pHP996vKu7xSp80HFTare/n6fTBEcUJLHspCC2JfeixkGNI19 Np28rs3A8bJoOohSLGVIdQVGiT3Mrr0= X-Google-Smtp-Source: AGRyM1sNSfg4OzUat+/XKg7JS3pq2fMNP0DXAZQDKp78R0vaRsinGs45PmzZCFSPn5h/qfz1QMFtWKG16Bw= X-Received: from jeffxuk510a.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:65b]) (user=jeffxu job=sendgmr) by 2002:a05:6602:2e8d:b0:67c:c24c:fec4 with SMTP id m13-20020a0566022e8d00b0067cc24cfec4mr683187iow.134.1659075307329; Thu, 28 Jul 2022 23:15:07 -0700 (PDT) Date: Fri, 29 Jul 2022 06:15:03 +0000 In-Reply-To: <6f71a4f3-8f8e-926b-883c-1df630cfc1a0@linuxfoundation.org> Message-Id: <20220729061504.744140-1-jeffxu@google.com> Mime-Version: 1.0 References: <6f71a4f3-8f8e-926b-883c-1df630cfc1a0@linuxfoundation.org> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 3/4] selftests/memfd: add tests for F_SEAL_EXEC From: Jeff Xu 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 ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=mpb2InHE; spf=pass (imf07.hostedemail.com: domain of 363rjYgYKCIwzuvvDAw44w1u.s421y3AD-220Bqs0.47w@flex--jeffxu.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=363rjYgYKCIwzuvvDAw44w1u.s421y3AD-220Bqs0.47w@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659075308; a=rsa-sha256; cv=none; b=mV9UEzbVVzVccVJGG/4fC1ItVKk8DsDpjd7O+q49wIe0bI1tozBXxlGPMaeo0ET1gEngZ2 wo24XiaqDYSVox+uAvetIOpxOktfb250jwdZTutaTS/gJFEE69F9ZopTcCGT36OGRl6XCT Oke7MT/pLpDZy78iKm6LSyAuKTRytwY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659075308; 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=hjVlZubyermfSkZizgaaxGB+KlUF18YguF7xhPiT/Ik=; b=WqVUmJPtHx4n6FbzDip2JLfpWyS3989TXpu9/seFKFX7s/t1jJmgc/fwhB4QuwmTTO2Yy+ sHXvBsJqWXSsl+eMU58ZLSVGwp7+iKKweDb00pADIKkOHiru3DPHzdHXBOthh3Zc4mN4uE vL/t/3EREj/yku36Sn81NWOG+Tfmcp8= X-Stat-Signature: t5fot9yfjte8ktut55y4hw4ocyyebg8b X-Rspamd-Queue-Id: 16BE8400A3 X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=mpb2InHE; spf=pass (imf07.hostedemail.com: domain of 363rjYgYKCIwzuvvDAw44w1u.s421y3AD-220Bqs0.47w@flex--jeffxu.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=363rjYgYKCIwzuvvDAw44w1u.s421y3AD-220Bqs0.47w@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-HE-Tag: 1659075307-893818 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: From: Daniel Verkamp Basic tests to ensure that user/group/other execute bits cannot be changed after applying F_SEAL_EXEC to a memfd. Co-developed-by: Jeff Xu Signed-off-by: Jeff Xu Signed-off-by: Daniel Verkamp --- tools/testing/selftests/memfd/memfd_test.c | 129 ++++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 94df2692e6e4..1d7e7b36bbdd 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -28,12 +28,44 @@ #define MFD_DEF_SIZE 8192 #define STACK_SIZE 65536 +#ifndef F_SEAL_EXEC +#define F_SEAL_EXEC 0x0020 +#endif + +#ifndef MAX_PATH +#define MAX_PATH 256 +#endif + /* * Default is not to test hugetlbfs */ static size_t mfd_def_size = MFD_DEF_SIZE; static const char *memfd_str = MEMFD_STR; +static ssize_t fd2name(int fd, char *buf, size_t bufsize) +{ + char buf1[MAX_PATH]; + int size; + ssize_t nbytes; + + size = snprintf(buf1, MAX_PATH, "/proc/self/fd/%d", fd); + if (size < 0) { + printf("snprintf(%d) failed on %m\n", fd); + abort(); + } + + /* + * reserver one byte for string termination. + */ + nbytes = readlink(buf1, buf, bufsize-1); + if (nbytes == -1) { + printf("readlink(%s) failed %m\n", buf1); + abort(); + } + buf[nbytes] = '\0'; + return nbytes; +} + static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) { int r, fd; @@ -98,11 +130,14 @@ static unsigned int mfd_assert_get_seals(int fd) static void mfd_assert_has_seals(int fd, unsigned int seals) { + char buf[MAX_PATH]; + int nbytes; unsigned int s; + fd2name(fd, buf, MAX_PATH); s = mfd_assert_get_seals(fd); if (s != seals) { - printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd); + printf("%u != %u = GET_SEALS(%s)\n", seals, s, buf); abort(); } } @@ -594,6 +629,64 @@ static void mfd_fail_grow_write(int fd) } } +static void mfd_assert_mode(int fd, int mode) +{ + struct stat st; + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fstat(fd, &st) < 0) { + printf("fstat(%s) failed: %m\n", buf); + abort(); + } + + if ((st.st_mode & 07777) != mode) { + printf("fstat(%s) wrong file mode 0%04o, but expected 0%04o\n", + buf, (int)st.st_mode & 07777, mode); + abort(); + } +} + +static void mfd_assert_chmod(int fd, int mode) +{ + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fchmod(fd, mode) < 0) { + printf("fchmod(%s, 0%04o) failed: %m\n", buf, mode); + abort(); + } + + mfd_assert_mode(fd, mode); +} + +static void mfd_fail_chmod(int fd, int mode) +{ + struct stat st; + char buf[MAX_PATH]; + int nbytes; + + fd2name(fd, buf, MAX_PATH); + + if (fstat(fd, &st) < 0) { + printf("fstat(%s) failed: %m\n", buf); + abort(); + } + + if (fchmod(fd, mode) == 0) { + printf("fchmod(%s, 0%04o) didn't fail as expected\n", + buf, mode); + abort(); + } + + /* verify that file mode bits did not change */ + mfd_assert_mode(fd, st.st_mode & 07777); +} + static int idle_thread_fn(void *arg) { sigset_t set; @@ -880,6 +973,39 @@ static void test_seal_resize(void) close(fd); } +/* + * Test SEAL_EXEC + * Test that chmod() cannot change x bits after sealing + */ +static void test_seal_exec(void) +{ + int fd; + + printf("%s SEAL-EXEC\n", memfd_str); + + fd = mfd_assert_new("kern_memfd_seal_exec", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + + mfd_assert_mode(fd, 0777); + + mfd_assert_chmod(fd, 0644); + + mfd_assert_has_seals(fd, 0); + mfd_assert_add_seals(fd, F_SEAL_EXEC); + mfd_assert_has_seals(fd, F_SEAL_EXEC); + + mfd_assert_chmod(fd, 0600); + mfd_fail_chmod(fd, 0777); + mfd_fail_chmod(fd, 0670); + mfd_fail_chmod(fd, 0605); + mfd_fail_chmod(fd, 0700); + mfd_fail_chmod(fd, 0100); + mfd_assert_chmod(fd, 0666); + + close(fd); +} + /* * Test sharing via dup() * Test that seals are shared between dupped FDs and they're all equal. @@ -1059,6 +1185,7 @@ int main(int argc, char **argv) test_seal_shrink(); test_seal_grow(); test_seal_resize(); + test_seal_exec(); test_share_dup("SHARE-DUP", ""); test_share_mmap("SHARE-MMAP", ""); From patchwork Fri Jul 29 06:15:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 12931957 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 4A998C19F2B for ; Fri, 29 Jul 2022 06:15:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFD918E0001; Fri, 29 Jul 2022 02:15:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAC966B0072; Fri, 29 Jul 2022 02:15:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9C488E0001; Fri, 29 Jul 2022 02:15:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BC3F66B0071 for ; Fri, 29 Jul 2022 02:15:31 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 98E46C15F0 for ; Fri, 29 Jul 2022 06:15:31 +0000 (UTC) X-FDA: 79739125662.28.41FA597 Received: from mail-il1-f202.google.com (mail-il1-f202.google.com [209.85.166.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 486521600B6 for ; Fri, 29 Jul 2022 06:15:31 +0000 (UTC) Received: by mail-il1-f202.google.com with SMTP id k1-20020a056e021a8100b002dd46be73fbso2450766ilv.6 for ; Thu, 28 Jul 2022 23:15:31 -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=j24QTnRW2WQx+RjstTVyEuiliRIDIVKlQJ1Kiz8ekAE=; b=TEY2CXEZm6Qhc3rYKvDwoWR/qoEJmmQ7Dv9aTFM4za8jX3EZDWIALRH/Jk0rO6tPEL 04GuD37Jd3MrehHYH11XBPPlg4Ar0TKo05/09sN/luz3mvH4iOyypcvdVRxfZxH1jiLq z6KNyVuyRvOrF4sbUH0kFFCIH44cHkjk9Ca/vEhgLt5eCqosFqxT3Uv0lbcxlA1OinoY eCy6RGBjPmwCDEiP+1BZ7XztqysMqUukHNaowrmkwQ9t25Av6nS4HGYTibxIvcKbm58T b84If3L1S4cDWzjgsUdPA3aHu/qxczyKskkktR/6a1+RDH5qSMsEybpvE8wQhxHPmUoX NgVA== 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=j24QTnRW2WQx+RjstTVyEuiliRIDIVKlQJ1Kiz8ekAE=; b=TJFJBddh7fvvnIQ8nxlX4syzE4fC40to3JQ0UcKRA6yI6dwAwCPpqwkIyfKihrJiBg oPp8jMeN6zZBPyXZlXhL/J4FzVL68PTh8kVH8Chwns6jlAk2+CH0LqukzW5jgw8cPXge piw5+kYH2J219oN18mvLMLNCnq0FJ6CfuNmfrotYTSufV+AVkXkSDFdawn9MeFvTGsmO 6MjTLYGsVKz6nSMDWuDQS+2y22F3iYkaCrsx4bjPoj46k3lO3aBqppRiWwMTuPdv9v9G fBNq2jrPLa/c7YqeTqO+TqMeHm68alNbq9xLTL+/50K37sVxLmJr7Mp+DCY8JkUoPaki Dbsg== X-Gm-Message-State: AJIora/vqy7xL+bb4noXi9kZ26KOn5+OEen6Y88kNmpE5V6FwLmuVTGL qf+TtCkf43rm/sCwa2RLQDTRar9N+6I= X-Google-Smtp-Source: AGRyM1uskQwYHUNqkg5mvGxPHUBp/Ocjhd1K7H8Mnvd07a1z9dH1JiWMOc91PrXNG4arVs1JBvaBPDb8FW4= X-Received: from jeffxuk510a.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:65b]) (user=jeffxu job=sendgmr) by 2002:a05:6638:13c2:b0:341:5b3f:399f with SMTP id i2-20020a05663813c200b003415b3f399fmr873301jaj.91.1659075330697; Thu, 28 Jul 2022 23:15:30 -0700 (PDT) Date: Fri, 29 Jul 2022 06:15:04 +0000 In-Reply-To: <20220729061504.744140-1-jeffxu@google.com> Message-Id: <20220729061504.744140-2-jeffxu@google.com> Mime-Version: 1.0 References: <6f71a4f3-8f8e-926b-883c-1df630cfc1a0@linuxfoundation.org> <20220729061504.744140-1-jeffxu@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 4/4] selftests/memfd: add tests for MFD_NOEXEC From: Jeff Xu 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659075331; a=rsa-sha256; cv=none; b=I8xfHGD1D6tIyfXUzolkloAdQL5c1ciPNa5+62rXDKtlMf3IKdqvw6k7uGDuSCnTbYKJdd oDZu4QzGH3BYr5s7KH2mg7KbTBhtFaaNkrbQUi3RZOFMV2vLyMDgJV/BEoC6cMkCjy7rmB RedHwyN7iGwGLtenPU4kfxc4jRnN6yc= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TEY2CXEZ; spf=pass (imf08.hostedemail.com: domain of 3AnvjYgYKCKMMHIIaXJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jeffxu.bounces.google.com designates 209.85.166.202 as permitted sender) smtp.mailfrom=3AnvjYgYKCKMMHIIaXJRRJOH.FRPOLQXa-PPNYDFN.RUJ@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=1659075331; 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=j24QTnRW2WQx+RjstTVyEuiliRIDIVKlQJ1Kiz8ekAE=; b=eTPN5h8/LHxHU5NZJATnMu+hLjkxrJWK/TQTfA44LDd0wFHxkqgk3KgblSSQp/7FkBbFqs 3u2IhUWHH+5GFgwL0tljaymtHiWJuEXw8Bn8m0pTlTMOC55Ee0KdXygka7hgsOD3oTtYhG ueGeWOiB3H6IXYx4sqSMdvOL3zPOpjw= Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TEY2CXEZ; spf=pass (imf08.hostedemail.com: domain of 3AnvjYgYKCKMMHIIaXJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jeffxu.bounces.google.com designates 209.85.166.202 as permitted sender) smtp.mailfrom=3AnvjYgYKCKMMHIIaXJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 486521600B6 X-Stat-Signature: 898tdm9y9uiwzsg6978cjf6whutnwo1p X-HE-Tag: 1659075331-315511 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: 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();