From patchwork Fri Aug 5 22:21:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 12937869 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 3EC72C25B0C for ; Fri, 5 Aug 2022 22:21:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC3948E0006; Fri, 5 Aug 2022 18:21:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C732F8E0003; Fri, 5 Aug 2022 18:21:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B15698E0006; Fri, 5 Aug 2022 18:21:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A170C8E0003 for ; Fri, 5 Aug 2022 18:21:58 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8303AABA8E for ; Fri, 5 Aug 2022 22:21:58 +0000 (UTC) X-FDA: 79766962716.08.0A89E6A Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf12.hostedemail.com (Postfix) with ESMTP id BD55C4013C for ; Fri, 5 Aug 2022 22:21:57 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-3230031a80fso31644387b3.5 for ; Fri, 05 Aug 2022 15:21:57 -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=p3xudRLR9dNuWUK9U/ylm6O3b8FVaNLRrb9pQqFzR0w=; b=tEC+w7ZCMSi41gOV32xpW1cs0vFZd0pjxoK1AeJyyH01noSX4dLvRhywvBcccI9kdU JKFh83XKg1Si8IMewvVtmvDTWjRKjw3vuzZtx6w3xA2KeuXUxoU/6vRz//098kabUzjE eFwl26ps6MG+nYqCcCI1foMbFHepHOTyrxI0z85iwM8dIVVGbzs366K/ASaVu+V0zPT/ uJ/lsElcVWzYS8YYewDpoOAwFTi+w1gRSdsGaOWMYHEI+AEzCyag0hQVzUs3cU93g+T8 p0Ut/ZVPUs8LdBcNe3+7UfinveQxYxEtWF7YFesMt+DabZUIKwIYMEaR3aoNOvIbKV0C 8E5Q== 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=p3xudRLR9dNuWUK9U/ylm6O3b8FVaNLRrb9pQqFzR0w=; b=dDUNXCSY3sw41nLC6QvnEAvAXkgAMAfrLch8zonpu0KNrD3exn4RpBxxgNMmjVmUZS 7S2Tkrm1SrtYOIS3TE16/dSLRx40DOjSe4JrSHCUwCImbtOGkc0I1zUGfoFCOH0TNHun 1wmFv3xBrdCUq73zJnow4k+zI7Sgt4L17f0Zn1KjJDuk0xhw1vB3e42lxV2TRKdW4tLh a53VYcxe3q3n35ltAzdSpSveyxKt2UFeSasWEfxMOYCCj18OVUDM8ZgY8YvMmwsTZYV+ guCrXG+SFZMltLklnnk1goEvP1+aW0wUOWYjti5ZIHlhlSS0zhj/FwaYXd/dvHgmd6Kf MM1g== X-Gm-Message-State: ACgBeo37VqxOXTZc17J4koD8ozwgsRaPo5yxxO9Bq9xGcx4OaFOMCuw+ 3ypm86vBPPD0f7aVZSAlcSFiLELGhD8= X-Google-Smtp-Source: AA6agR7Pdt67CAVIWF/fYp+P3xXpceR93JSbt9+i7cJEfvpa+CoelR+6dqwVhJxpBe3viA/07hQjowUu1E0= X-Received: from jeffxud.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:e37]) (user=jeffxu job=sendgmr) by 2002:a25:33c5:0:b0:67a:6950:c188 with SMTP id z188-20020a2533c5000000b0067a6950c188mr7334020ybz.175.1659738117501; Fri, 05 Aug 2022 15:21:57 -0700 (PDT) Date: Fri, 5 Aug 2022 22:21:26 +0000 In-Reply-To: <20220805222126.142525-1-jeffxu@google.com> Message-Id: <20220805222126.142525-6-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 5/5] sysctl: add support 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=1659738118; a=rsa-sha256; cv=none; b=JtLqy+6pz39TL4MhiAzlslxTG5CAtZWk+LE3eMl1Mf6xztAOVunPM9xch6JeTvoUz26rJJ JrTnAU2g7js2PEw/iwwMBy1ldDoxewOFRG//wK9+WqhH2FawxS8u/wS/8jlJn2vPNFlpGR vsZs5VJ3+mvI5RRLVIF1pwUGSQt+TAQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=tEC+w7ZC; spf=pass (imf12.hostedemail.com: domain of 3BZjtYgYKCAotopp74qyyqvo.mywvsx47-wwu5kmu.y1q@flex--jeffxu.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BZjtYgYKCAotopp74qyyqvo.mywvsx47-wwu5kmu.y1q@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=1659738118; 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=p3xudRLR9dNuWUK9U/ylm6O3b8FVaNLRrb9pQqFzR0w=; b=p+jGJeIIdVqpqXDzKOZ83wTmP0FRAc8UyquHSs3wWe8f7XhqyHyFa21RUQrwPiudpg0U3W Yos5YlGifHHhC/8AquBS6OpGsfK29lOaJPd58XzUYf0oPRuDcErTz/HEX1wP2Qa7vbkUDl wCyJCguB8h4QijqWgyL6dmjTZe767qQ= X-Stat-Signature: u3rud8npp47x86phe7w8pnsefffn5u1x X-Rspamd-Queue-Id: BD55C4013C Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=tEC+w7ZC; spf=pass (imf12.hostedemail.com: domain of 3BZjtYgYKCAotopp74qyyqvo.mywvsx47-wwu5kmu.y1q@flex--jeffxu.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3BZjtYgYKCAotopp74qyyqvo.mywvsx47-wwu5kmu.y1q@flex--jeffxu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1659738117-118109 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: Jeff Xu Add vm.mfd_noexec. When the value is 1 (enabled), memfd_create syscall will created non-executable memfd. The default value is 0 (disabled), admin can change the setting from 0 => 1, however 1 => 0 is not allowed, unless reboot. Signed-off-by: Jeff Xu Reported-by: kernel test robot Reported-by: kernel test robot --- include/linux/mm.h | 4 ++++ kernel/sysctl.c | 9 +++++++++ mm/memfd.c | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7898e29bcfb5..1c66cf4aca11 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -203,6 +203,10 @@ extern int sysctl_overcommit_memory; extern int sysctl_overcommit_ratio; extern unsigned long sysctl_overcommit_kbytes; +extern int sysctl_mfd_noexec_scope; +extern int mfd_noexec_dointvec_minmax(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos); + int overcommit_ratio_handler(struct ctl_table *, int, void *, size_t *, loff_t *); int overcommit_kbytes_handler(struct ctl_table *, int, void *, size_t *, diff --git a/kernel/sysctl.c b/kernel/sysctl.c index b233714a1c78..54510da007ff 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -2362,6 +2362,15 @@ static struct ctl_table vm_table[] = { .mode = 0644, .proc_handler = mmap_min_addr_handler, }, + { + .procname = "mfd_noexec", + .data = &sysctl_mfd_noexec_scope, + .maxlen = sizeof(sysctl_mfd_noexec_scope), + .mode = 0644, + .proc_handler = mfd_noexec_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, #endif #ifdef CONFIG_NUMA { diff --git a/mm/memfd.c b/mm/memfd.c index b841514eb0fd..c6ccb8481ed2 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -20,6 +20,11 @@ #include #include +#define MFD_NOEXEC_SCOPE_DISABLED 0 +#define MFD_NOEXEC_SCOPE_ENABLED 1 + +int sysctl_mfd_noexec_scope __read_mostly = MFD_NOEXEC_SCOPE_DISABLED; + /* * We need a tag: a new tag would expand every xa_node by 8 bytes, * so reuse a tag which we firmly believe is never set or cleared on tmpfs @@ -275,6 +280,10 @@ SYSCALL_DEFINE2(memfd_create, char *name; long len; + if (sysctl_mfd_noexec_scope == MFD_NOEXEC_SCOPE_ENABLED) { + flags |= MFD_NOEXEC; + } + if (!(flags & MFD_HUGETLB)) { if (flags & ~(unsigned int)MFD_ALL_FLAGS) return -EINVAL; @@ -351,3 +360,21 @@ SYSCALL_DEFINE2(memfd_create, kfree(name); return error; } + +#ifdef CONFIG_SYSCTL +int mfd_noexec_dointvec_minmax(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + struct ctl_table table_copy; + + if (write && !capable(CAP_SYS_ADMIN)) + return -EPERM; + + /* Lock the max value if it ever gets set. */ + table_copy = *table; + if (*(int *)table_copy.data == *(int *)table_copy.extra2) + table_copy.extra1 = table_copy.extra2; + + return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos); +} +#endif