From patchwork Wed Jul 5 06:33:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13301699 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 4F42EEB64DD for ; Wed, 5 Jul 2023 06:33:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C57B6B0071; Wed, 5 Jul 2023 02:33:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 675CF6B0072; Wed, 5 Jul 2023 02:33:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53D5F8D0001; Wed, 5 Jul 2023 02:33:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 408FE6B0071 for ; Wed, 5 Jul 2023 02:33:23 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ED8221C83EF for ; Wed, 5 Jul 2023 06:33:22 +0000 (UTC) X-FDA: 80976591444.19.BE071A9 Received: from mail-vk1-f171.google.com (mail-vk1-f171.google.com [209.85.221.171]) by imf02.hostedemail.com (Postfix) with ESMTP id 25D3280014 for ; Wed, 5 Jul 2023 06:33:20 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bFlmbiX7; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf02.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.221.171 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688538801; a=rsa-sha256; cv=none; b=Xaa1O+hg9iKRi7xaeezxy+KtYWVjklb1s4c9jG5g5mrQyAo/+ow6Nd52ehK8QXeTw5fqK0 309NN0hJXXuvqWWpffILXpwaJHqE6de2YosQ/2m/6X1PGlIMCCkQW2hRoGvZdtdmP137hP upqSgat6l1nmONqlGa6/00l6Wf2MRN0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bFlmbiX7; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf02.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.221.171 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=1688538801; 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=W+MSmdpLaQYcYfYw7xJr5lmjiO0vLCrOZIxlLos01I4=; b=tl/waQtZNRsv5Pdt77AKxPwORgVvGzzwfOLmHpxHVMjf7DFoX6WCdDP8gJVSd6329dn4PQ v5CKH7jRaLXyqKDfsYA2EAhH2ioUTpIc8DNuk+n4yOFgh8veUAGcu1/+NhBzObNqNXnvCj HBWjRSQwbfOMFlTjIbTwayTe7cY6ucU= Received: by mail-vk1-f171.google.com with SMTP id 71dfb90a1353d-47e793ccdc7so150125e0c.1 for ; Tue, 04 Jul 2023 23:33:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688538800; x=1691130800; 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=W+MSmdpLaQYcYfYw7xJr5lmjiO0vLCrOZIxlLos01I4=; b=bFlmbiX7I0IMNBN+awjy44Gd2HHxolAdFzcioJ6vbbxbKEhkdrQChkaBrAG9TgBfKT x0Z2gtzkhzO55v/fQhqA0JPCBoHFOFGB4fsAyofg2LzDmXjriBm2FqrokpTc7btoXEHH bLA19vYllamBMyiqrr9cjjjuZUbPI0WKT4WEE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688538800; x=1691130800; 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=W+MSmdpLaQYcYfYw7xJr5lmjiO0vLCrOZIxlLos01I4=; b=YUTIN9Wc6+uGAL6Fof+dMrmMVYKauyPL+XlqtOyXvinoTafOAKq9ytfRN2AHjKrClF piuX/5FUxDqgGQFLi5cjrEYnGCZLpWFdv75YeC2F7/gUm0yUzj2e7HEVwTqge1ChkJWL Cap7auofYqZgP4DU9w52Aag0+vOEzpU9jlng5WvXLlCe362P19r3SeQBQI7Z0NfpHXCE By8EZmub5lryOVRS5HiHvMRnXoxxBjuumYFlP5gu8WyyAL8hPTgmyz0upZjo+QI6F7oX CbTOXneMV19IWeM1JCrP55XKPtRFGP7zF9aYF3wmiFmi77EqJhtbR4HL+3xcz/YkW4QK ikXg== X-Gm-Message-State: ABy/qLbEkB6zYXwwPhMv+vPFJvZ4EXchmexD6NZeRXg5JH1tobh9DaWP TwjtiJY4eJHvzqIIXEacRmOujQ== X-Google-Smtp-Source: APBJJlFcgKaSPJKkbCoS2vIlhS3WYafUAvQl5v36URdmF0hDQjm/PTL88mvcn7PB87UgQocXD3sayA== X-Received: by 2002:a05:6122:2af:b0:46e:6af4:3333 with SMTP id 15-20020a05612202af00b0046e6af43333mr6858407vkq.5.1688538800100; Tue, 04 Jul 2023 23:33:20 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id bj2-20020a17090b088200b002633fa95ac2sm633631pjb.13.2023.07.04.23.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Jul 2023 23:33:19 -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, kernel test robot Subject: [PATCH v2 1/2] mm/memfd: sysctl: fix MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED Date: Wed, 5 Jul 2023 06:33:14 +0000 Message-ID: <20230705063315.3680666-2-jeffxu@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog In-Reply-To: <20230705063315.3680666-1-jeffxu@google.com> References: <20230705063315.3680666-1-jeffxu@google.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 25D3280014 X-Stat-Signature: 9b89qyo6gcyto8hqfbn9uhm1wiuoifzn X-HE-Tag: 1688538800-342070 X-HE-Meta: U2FsdGVkX19E1QFWZlC98QIkMWEl2LNMo2Wknnqx9GFbqMV1g2ONf9y0mvheBXjtW43Hn6NCB+vPX3XnZx15ArEuZpeLb9crLBstnonrnyawtZ2m+s6vYkxnzFiVcVh7cLl3+0s7K4f26vo7LDVNovBHf9RaP4gsKUvtBFuxMX8QuKQUo9BuBb6+gPcNrdYvm5CZuSFiHWB0j/W838SJM8bVFSxzGBCxrS8Vn4k8cA7fxUqIDCDLtFj4gnj+jHJfnZXXqFUwvKNrrfoShYaDldybyLUwqTPe6VIwjIhU7rkCaOr1Yirgrqi1fySBjzOpH9z2AXC3F6jp8aeog0/mqrDl1FWs/BQSzYRSRZpYmz/E/AW5BnNVIHNDTbj48PHjjES+U14+XIgg418HkXKvVPQYUuEuPvnBlF4k+WSil501IpKSlyYA80cPEh0gy1VVD2lijCv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aDF++eHo9FQjhfy2l2NNm8YRfSZFlLR+q 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" Fixes: 105ff5339f49 ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") Reported-by: Dominique Martinet Closes: https://lore.kernel.org/linux-mm/CABi2SkXUX_QqTQ10Yx9bBUGpN1wByOi_=gZU6WEy5a8MaQY3Jw@mail.gmail.com/T/ Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202306301351.kkbSegQW-lkp@intel.com/ Signed-off-by: Jeff Xu --- mm/memfd.c | 57 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index e763e76f1106..0bdbd2335af7 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -268,6 +268,36 @@ 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 check_sysctl_memfd_noexec(unsigned int *flags) +{ +#ifdef CONFIG_SYSCTL + char comm[TASK_COMM_LEN]; + int sysctl = MEMFD_NOEXEC_SCOPE_EXEC; + struct pid_namespace *ns; + + ns = task_active_pid_ns(current); + if (ns) + sysctl = ns->memfd_noexec_scope; + + if (!(*flags & (MFD_EXEC | MFD_NOEXEC_SEAL))) { + if (sysctl == MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL) + *flags |= MFD_NOEXEC_SEAL; + else + *flags |= MFD_EXEC; + } + + 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; + } +#endif + + return 0; +} + SYSCALL_DEFINE2(memfd_create, const char __user *, uname, unsigned int, flags) @@ -294,35 +324,14 @@ 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: - 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 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 (check_sysctl_memfd_noexec(&flags) < 0) + return -EACCES; + /* length includes terminating zero */ len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); if (len <= 0)