From patchwork Fri Jun 30 03:17:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13297531 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 46358EB64D9 for ; Fri, 30 Jun 2023 03:17:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A94888D0003; Thu, 29 Jun 2023 23:17:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A44458D0001; Thu, 29 Jun 2023 23:17:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90DF08D0003; Thu, 29 Jun 2023 23:17:32 -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 82F5C8D0001 for ; Thu, 29 Jun 2023 23:17:32 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 477BF120D2C for ; Fri, 30 Jun 2023 03:17:32 +0000 (UTC) X-FDA: 80957953944.06.1BE4087 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) by imf21.hostedemail.com (Postfix) with ESMTP id 7DBCA1C0010 for ; Fri, 30 Jun 2023 03:17:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Hhds97GU; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.53 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688095050; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=53ZMdGhNmZ+BOpMUbgSFLyEDTkK0ZHteXsorexZvGcc=; b=vrf5cUjqBK4UgTEtff8Nx8SfyVTm8D4AM+tgSIGhWapWGkBV0esbq7+C9Jpo8DjlBwOXoJ dvuvdcEv/aYz1Vx7LYy/1ORNrForR+w5p3LRxjEdW0t1p0OX3i1TQ1rA+MAFrdpgqC2PC7 IH0JcGB3VRWJKWrRE9YJJlI9A4p8HiY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Hhds97GU; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf21.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.53 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688095050; a=rsa-sha256; cv=none; b=uKn6NnnTGDrVEY+MU8/RICOZiqLWmITTXDII3noN9SLqZigphnwe8DoJlob3WV0tPkrSNB IEqRqTOXSBWX4YgC//zs/eYJOJz/OHF+8X/FIMFcdkY9KHEedsr/m/94aiA7FxVDI1wNZw NPDmOn0tps89nn5yQUbDMOoWTyGjjqc= Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-6b708b97418so1147345a34.3 for ; Thu, 29 Jun 2023 20:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688095049; x=1690687049; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=53ZMdGhNmZ+BOpMUbgSFLyEDTkK0ZHteXsorexZvGcc=; b=Hhds97GUYBeppbOwT50AZutzB1nVVfNLRInAfnNP58IkESH6yufdJYjjXoko1dy9G+ 7Ur8NWUwfJrbcpQSpTt286069+coFcb6LHT+Hpu/39T4cIbHcx3ohwZzEN7MljiB4mGX nGNyBGTlMBhsV2p04QbZotAIplV3r6a4qLE4w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688095049; x=1690687049; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=53ZMdGhNmZ+BOpMUbgSFLyEDTkK0ZHteXsorexZvGcc=; b=LP1KuO7LzMX0LoeUkPlXnwPXVwN6Fdm6dS+mcugamWYC1Vo4gxOz1mTc4XO4TB+5n6 IziShM4Yf5MWz80faU3BHCjnGpH/RSD2+WDt9FZkRj58/+VRcGKGjLYFtn3MnYfWQzdY 5KQeCFdrY1P1PeQAahBeiNqJN8O/qvA3EIJnKjlfv7GtRCNmpNaqUVZrCB+JILZl72n2 zXVHVodH44gEoIb6JiYFWIx3q+l80Fy0LzRLrkXNpSswXt7BH/yLk12b1lxW4h3U+Jj/ NUFcGvqgEn82bR3wkIjsS99LJsVyaXG4fsbHeKk6Fx1R74wWrtMzaUs1yrqxxLiYRwEH Yl7w== X-Gm-Message-State: AC+VfDyX5TSLOOINXX7JyJUmcVPYTSJkWL3axeTMpQ5P9l20h0UcygfL VqP9fZ9GpsNhElQPrwEoo1nwBw== X-Google-Smtp-Source: ACHHUZ7w0MIcW5kZcKcCzQ5+ZTjH6ysBlmgQ4PxTyUtw8h8Kk3p3zrfxkxjd+Odq2hqr4KUnUW6Icw== X-Received: by 2002:a05:6808:1921:b0:3a3:7e8f:2f2a with SMTP id bf33-20020a056808192100b003a37e8f2f2amr1773186oib.11.1688095049673; Thu, 29 Jun 2023 20:17:29 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id x24-20020aa784d8000000b0065438394fa4sm1622577pfn.90.2023.06.29.20.17.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Jun 2023 20:17:29 -0700 (PDT) From: jeffxu@chromium.org To: skhan@linuxfoundation.org, keescook@chromium.org Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, asmadeus@codewreck.org, hughd@google.com, jeffxu@google.com, jorgelo@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, jannh@google.com, linux-hardening@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v1 1/2] mm/memfd: sysctl: fix MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED Date: Fri, 30 Jun 2023 03:17:19 +0000 Message-ID: <20230630031721.623955-2-jeffxu@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230630031721.623955-1-jeffxu@google.com> References: <20230630031721.623955-1-jeffxu@google.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7DBCA1C0010 X-Stat-Signature: 6bea7n434rw3yuai1t8xn96czjfcf9wg X-Rspam-User: X-HE-Tag: 1688095050-797420 X-HE-Meta: U2FsdGVkX18x+FIjCBdF8RU/k1cyNcoRHhgl5iN9q/+kpXE7PdJO1fqePEwY9r7ak7XEGzgTED6r06REuGsBXAK4bmZZFGcC3D+NtjFmi1lYLfHJCI11Uk61Wo7br4aqDnOMUCdC3jmDIMs0J8PMUxb0RDlf4kYUiL/rApdGaKRaZ+MjJalvkM5w35jZFYl0VqNbRnkq8cw4m4iEEIHwgPoLnziKvfJjLcsVF60VBGegL1N5mBSiO6APF/Fyk1PClVMrva2PvPhoiBGnI10tNj4vsZYbyCI2cbplNK9Kpv3PZkEtsEZRqq6cJ8wW0A44EJJzYEFp6PvoKAzvj1Lvh2Sff0eb7dnAFkZTGilubNx3/a/zGK9Jg7KMtnUOQf3siTLAxyyULkcbunThzrN2PRdlYXgHdhE/yG5HnqJYNCj/RPN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TyJTM8ueAeT2R6oSTbxDuBe6oA72VIB4CX/ 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 When vm.memfd_noexec is 2 (MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED), memfd_create(.., MFD_EXEC) should fail. This complies with how MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED is defined - "memfd_create() without MFD_NOEXEC_SEAL will be rejected" Link:https://lore.kernel.org/linux-mm/CABi2SkXUX_QqTQ10Yx9bBUGpN1wByOi_=gZU6WEy5a8MaQY3Jw@mail.gmail.com/T/ Fixes: 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") Reported-by: Dominique Martinet Signed-off-by: Jeff Xu --- mm/memfd.c | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index e763e76f1106..812750298e8a 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -268,6 +268,19 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg) #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC) +static int sysctl_memfd_noexec(void) +{ + int sysctl = MEMFD_NOEXEC_SCOPE_EXEC; +#ifdef CONFIG_SYSCTL + struct pid_namespace *ns; + + ns = task_active_pid_ns(current); + if (ns) + sysctl = ns->memfd_noexec_scope; +#endif + return sysctl; +} + SYSCALL_DEFINE2(memfd_create, const char __user *, uname, unsigned int, flags) @@ -278,6 +291,7 @@ SYSCALL_DEFINE2(memfd_create, int fd, error; char *name; long len; + int sysctl = sysctl_memfd_noexec(); if (!(flags & MFD_HUGETLB)) { if (flags & ~(unsigned int)MFD_ALL_FLAGS) @@ -294,35 +308,23 @@ SYSCALL_DEFINE2(memfd_create, return -EINVAL; if (!(flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { -#ifdef CONFIG_SYSCTL - int sysctl = MEMFD_NOEXEC_SCOPE_EXEC; - struct pid_namespace *ns; - - ns = task_active_pid_ns(current); - if (ns) - sysctl = ns->memfd_noexec_scope; - - switch (sysctl) { - case MEMFD_NOEXEC_SCOPE_EXEC: - flags |= MFD_EXEC; - break; - case MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL: + if (sysctl == MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL) flags |= MFD_NOEXEC_SEAL; - break; - default: - pr_warn_once( - "memfd_create(): MFD_NOEXEC_SEAL is enforced, pid=%d '%s'\n", - task_pid_nr(current), get_task_comm(comm, current)); - return -EINVAL; - } -#else - flags |= MFD_EXEC; -#endif + else + flags |= MFD_EXEC; + pr_warn_once( "memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL, pid=%d '%s'\n", task_pid_nr(current), get_task_comm(comm, current)); } + if (flags & MFD_EXEC && sysctl >= MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED) { + pr_warn_once( + "memfd_create(): MFD_NOEXEC_SEAL is enforced, pid=%d '%s'\n", + task_pid_nr(current), get_task_comm(comm, current)); + return -EACCES; + } + /* length includes terminating zero */ len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); if (len <= 0)