From patchwork Fri May 24 03:39:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13672653 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 4B7DCC25B74 for ; Fri, 24 May 2024 03:39:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DECCA6B009F; Thu, 23 May 2024 23:39:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D73FF6B00A0; Thu, 23 May 2024 23:39:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C14AD6B00A1; Thu, 23 May 2024 23:39:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A36696B009F for ; Thu, 23 May 2024 23:39:53 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 55997807FE for ; Fri, 24 May 2024 03:39:53 +0000 (UTC) X-FDA: 82151885466.09.F58AC16 Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) by imf26.hostedemail.com (Postfix) with ESMTP id 72578140002 for ; Fri, 24 May 2024 03:39:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=kNYM1PwR; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.160.52 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716521991; a=rsa-sha256; cv=none; b=LcRj8oS8ItjsLRZKmXrcHH/EPcok+y8RUT0MCphARZZ2jPQACjB3DnFjQGcUO93eA1eHcZ tHBCiZYhbh/g0bekr0MyEFfGxwzJliMFVkV3WwkSBrv9EHmzKGBz13zQBv7QIIqrqZ2jBe f6rimU/odBLTyM90hIGRXePRWayvRVE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=kNYM1PwR; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.160.52 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716521991; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=j6jr022mlOQVdTQNdYCM0jmCCOa9PK9T8m0GA7PmwSw=; b=4kjdkfR04LYEPejKP0dWGdcocAfXXYKWD3kAmTS06dM79TTQgrOYAKBcPs27uco1YaNgLV Cs/czh2BXk0AzxXbBe+yZvzXE6sx3w8NJiwnGNWZ0kfuhfT+SDlZzoIcxZ/gUGDNbTafON QPLiTuvRuQBr9yYc9gCXxyctvi7/i9I= Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-2451da9b4feso3225195fac.0 for ; Thu, 23 May 2024 20:39:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1716521990; x=1717126790; darn=kvack.org; 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=j6jr022mlOQVdTQNdYCM0jmCCOa9PK9T8m0GA7PmwSw=; b=kNYM1PwR5vtex++C0X+gtlO2Zxiw4MThVeE+9l6UDWoe31J05fpDxYQJaw7Gy1Xei2 VcgMdTl+QDCkp+yptmvFNsb187aj7vGUkO7mTzZTXr0U26RkLC1jBSvjSQfWw5lLvh5O g/gHGZwu6F3nHuP9SpWdbtludwqx36t8fG4EQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716521990; x=1717126790; 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=j6jr022mlOQVdTQNdYCM0jmCCOa9PK9T8m0GA7PmwSw=; b=UkDR2HKS3gQ0/X+wpliRGL6Khc3+xS+CvB+IKYdoyg0epqaWthIbA74JMVVMzwaJNI XxtUWqsTDoBSQQk1FWvCdxDt/lkRNh82Ac1OgNKkldAxWzt22ENHv94miDViwlIe40lC k5aGc/P0yNYOhZoSQzb3zIBDxhmQC7sx7TZApc4Bk6YtW+a1/l7xeh14yJUdnYb3KSk3 KbqNu1xwDZ++eHF1SYXqXuRbrPunN3UbdCl9el/83wmdjZGM9TmvXQt0AXTJB2Tm+dqm 9cmFRxsz1SykVVZ5gZOzVp10hV1XpkZWOnfPJvmzMSinZt+AQMprkNDb2OOsvpbzzagT 82ow== X-Forwarded-Encrypted: i=1; AJvYcCUo19AUQkKoI7WF1mfS6G8RERu5iC27ovq6/W75Yh2MH/2UQbw2tNLxgdGFxnwNYI+M0mo9cQ32IdlV/8/axqOUFCE= X-Gm-Message-State: AOJu0YwPzrNT0t+v1HhunNfV8+qk30HPH6+EdfrQ6YRU6nGyHlsWuZbe 3iyDPaPsh/X44p+8aw/sa8kZNaVyNJVH4P3E6Djil9IktgU6TmwxTLzVWHOWWw== X-Google-Smtp-Source: AGHT+IHZf+jr2XVCdIxoQjdDZnqMqm+D003gUslsBjdQqg0R++wGRM0qaMzD5yCdbM2LCfnRng/Tyg== X-Received: by 2002:a05:6870:1492:b0:24c:6198:5ff8 with SMTP id 586e51a60fabf-24ca105306amr1500902fac.4.1716521990321; Thu, 23 May 2024 20:39:50 -0700 (PDT) Received: from localhost (197.59.83.34.bc.googleusercontent.com. [34.83.59.197]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-6f8fcbeb674sm315258b3a.122.2024.05.23.20.39.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 May 2024 20:39:50 -0700 (PDT) From: jeffxu@chromium.org To: jeffxu@google.com Cc: jeffxu@chromium.org, akpm@linux-foundation.org, cyphar@cyphar.com, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jorgelo@chromium.org, keescook@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, pobrn@protonmail.com, skhan@linuxfoundation.org, stable@vger.kernel.org, David Rheinsberg Subject: [PATCH v2 1/2] memfd: fix MFD_NOEXEC_SEAL to be non-sealable by default Date: Fri, 24 May 2024 03:39:30 +0000 Message-ID: <20240524033933.135049-2-jeffxu@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240524033933.135049-1-jeffxu@google.com> References: <20240524033933.135049-1-jeffxu@google.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 72578140002 X-Stat-Signature: wyhzs99ik35zy86izepemhxrb9rgnzrn X-HE-Tag: 1716521991-210074 X-HE-Meta: U2FsdGVkX19IfIhlM1eKYTNWBz9XdT53fZqDHMKatZ6fLf7CHR3XbWmllRUwClTEdFSufRbpa23Y1RyCPJ544nUzHbplehWp9PoOlK2L7lI1P2cTkW1e1ovPmHEl+bDi0Dt2IclbhghjTQrQHYJxxe/Q8fAtaL1MUNBeWAGZMKU/VEiTrxaLPDZiZIIcVnEHzx0z/9ILwgTIa/v2sgSL/OXxjqt1a4+05VMoJZQELvAGYFwP0TgqJtUmklcz8YYtSbEkmXdUOl73L729Jm5iEzaSxi7VGtz5ZEDbaEUYDVxqi71VGOu3WTtiEWC4I5ieHe3oT9bTcPIqap+I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5GUsAHVo5tgeaUap0NFsYngWmoeNFMuYMNQmax7dq5Alj0QcBUq6/93s+Bj9Qx1wY/FjD8L8mJgyuHjeQjawKnHpVw4XleX62ven2Zcr+0ixpfjpofR4abdxqwvGz9d/I4oG5e/WU6D4gs4h6AVpUFoFGN5KAjBi/MC+2dS4nCK4y/gM/E6d8SU4EyIdb9pw1M6JPvfreGUjP2TdBqxdLTVauPYGQpI1mrcuJnjY2THSO8uFpbqMgc0Rbe805xG8GDDpr8= 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: List-Subscribe: List-Unsubscribe: From: Jeff Xu By default, memfd_create() creates a non-sealable MFD, unless the MFD_ALLOW_SEALING flag is set. When the MFD_NOEXEC_SEAL flag is initially introduced, the MFD created with that flag is sealable, even though MFD_ALLOW_SEALING is not set. This patch changes MFD_NOEXEC_SEAL to be non-sealable by default, unless MFD_ALLOW_SEALING is explicitly set. This is a non-backward compatible change. However, as MFD_NOEXEC_SEAL is new, we expect not many applications will rely on the nature of MFD_NOEXEC_SEAL being sealable. In most cases, the application already sets MFD_ALLOW_SEALING if they need a sealable MFD. Additionally, this enhances the useability of pid namespace sysctl vm.memfd_noexec. When vm.memfd_noexec equals 1 or 2, the kernel will add MFD_NOEXEC_SEAL if mfd_create does not specify MFD_EXEC or MFD_NOEXEC_SEAL, and the addition of MFD_NOEXEC_SEAL enables the MFD to be sealable. This means, any application that does not desire this behavior will be unable to utilize vm.memfd_noexec = 1 or 2 to migrate/enforce non-executable MFD. This adjustment ensures that applications can anticipate that the sealable characteristic will remain unmodified by vm.memfd_noexec. This patch was initially developed by Barnabás Pőcze, and Barnabás used Debian Code Search and GitHub to try to find potential breakages and could only find a single one. Dbus-broker's memfd_create() wrapper is aware of this implicit `MFD_ALLOW_SEALING` behavior, and tries to work around it [1]. This workaround will break. Luckily, this only affects the test suite, it does not affect the normal operations of dbus-broker. There is a PR with a fix[2]. In addition, David Rheinsberg also raised similar fix in [3] [1]: https://github.com/bus1/dbus-broker/blob/9eb0b7e5826fc76cad7b025bc46f267d4a8784cb/src/util/misc.c#L114 [2]: https://github.com/bus1/dbus-broker/pull/366 [3]: https://lore.kernel.org/lkml/20230714114753.170814-1-david@readahead.eu/ Cc: stable@vger.kernel.org Fixes: 105ff5339f498a ("mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC") Signed-off-by: Barnabás Pőcze Signed-off-by: Jeff Xu Reviewed-by: David Rheinsberg --- mm/memfd.c | 9 ++++---- tools/testing/selftests/memfd/memfd_test.c | 26 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 7d8d3ab3fa37..8b7f6afee21d 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -356,12 +356,11 @@ SYSCALL_DEFINE2(memfd_create, inode->i_mode &= ~0111; file_seals = memfd_file_seals_ptr(file); - if (file_seals) { - *file_seals &= ~F_SEAL_SEAL; + if (file_seals) *file_seals |= F_SEAL_EXEC; - } - } else if (flags & MFD_ALLOW_SEALING) { - /* MFD_EXEC and MFD_ALLOW_SEALING are set */ + } + + if (flags & MFD_ALLOW_SEALING) { file_seals = memfd_file_seals_ptr(file); if (file_seals) *file_seals &= ~F_SEAL_SEAL; diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 95af2d78fd31..8579a93d006b 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -1151,7 +1151,7 @@ static void test_noexec_seal(void) mfd_def_size, MFD_CLOEXEC | MFD_NOEXEC_SEAL); mfd_assert_mode(fd, 0666); - mfd_assert_has_seals(fd, F_SEAL_EXEC); + mfd_assert_has_seals(fd, F_SEAL_SEAL | F_SEAL_EXEC); mfd_fail_chmod(fd, 0777); close(fd); } @@ -1169,6 +1169,14 @@ static void test_sysctl_sysctl0(void) mfd_assert_has_seals(fd, 0); mfd_assert_chmod(fd, 0644); close(fd); + + fd = mfd_assert_new("kern_memfd_sysctl_0_dfl", + mfd_def_size, + MFD_CLOEXEC); + mfd_assert_mode(fd, 0777); + mfd_assert_has_seals(fd, F_SEAL_SEAL); + mfd_assert_chmod(fd, 0644); + close(fd); } static void test_sysctl_set_sysctl0(void) @@ -1206,6 +1214,14 @@ static void test_sysctl_sysctl1(void) mfd_assert_has_seals(fd, F_SEAL_EXEC); mfd_fail_chmod(fd, 0777); close(fd); + + fd = mfd_assert_new("kern_memfd_sysctl_1_noexec_nosealable", + mfd_def_size, + MFD_CLOEXEC | MFD_NOEXEC_SEAL); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC | F_SEAL_SEAL); + mfd_fail_chmod(fd, 0777); + close(fd); } static void test_sysctl_set_sysctl1(void) @@ -1238,6 +1254,14 @@ static void test_sysctl_sysctl2(void) mfd_assert_has_seals(fd, F_SEAL_EXEC); mfd_fail_chmod(fd, 0777); close(fd); + + fd = mfd_assert_new("kern_memfd_sysctl_2_noexec_notsealable", + mfd_def_size, + MFD_CLOEXEC | MFD_NOEXEC_SEAL); + mfd_assert_mode(fd, 0666); + mfd_assert_has_seals(fd, F_SEAL_EXEC | F_SEAL_SEAL); + mfd_fail_chmod(fd, 0777); + close(fd); } static void test_sysctl_set_sysctl2(void)