From patchwork Tue Jun 11 03:49:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13692857 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 AFD96C27C5F for ; Tue, 11 Jun 2024 03:49:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40ED06B0095; Mon, 10 Jun 2024 23:49:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 346FF6B0099; Mon, 10 Jun 2024 23:49:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 171DE6B0098; Mon, 10 Jun 2024 23:49:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E64346B008C for ; Mon, 10 Jun 2024 23:49:12 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 89498120AB0 for ; Tue, 11 Jun 2024 03:49:12 +0000 (UTC) X-FDA: 82217227344.25.8AB59BA Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) by imf26.hostedemail.com (Postfix) with ESMTP id A7888140007 for ; Tue, 11 Jun 2024 03:49:10 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="E/IuySPM"; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.161.47 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=1718077750; 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=NovBVUB3IuRICfhBAy0nOxMqOymWO9ozrSvccHzKh4E=; b=Tf0v5SETN/1R7dRloo47KTOrvaWGHlRQLY3XgxjIV/itNDEc/bYY86rb92yRi55S44e+NC FBhNegItqAWbeyT5ny2/GnJuhksXB2IM4YgtzKMndtM1HFhCtSeKr+j1DvxkHmO+zrs04v EyUxW4jmDGBwpeTphHyOkCn/O6ihMIc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="E/IuySPM"; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.161.47 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=1718077750; a=rsa-sha256; cv=none; b=Klvp8+dQgGo1ZPuygYFilZ89g7hbUTyQs3lG4woRFN9sfU0xlxgwc3aue3jpBZb5rrA/yE 9AyXAfzNooJZwXuYolaqbFwB1bUM1QLIq65DfNzk6leI0PhNuW5+KiYcAedoJlKlYf6ux5 xgDXBQsq/9Ca+t08B0Eu72Qcxe+DMFs= Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5b970a97e8eso2883344eaf.1 for ; Mon, 10 Jun 2024 20:49:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718077749; x=1718682549; 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=NovBVUB3IuRICfhBAy0nOxMqOymWO9ozrSvccHzKh4E=; b=E/IuySPM5C+C8tM5eg23+YkLk0PEWW2kvtdqh9jr0th4tZ3UQT45H50iPz4LUYLfDR iNRL2rq3dd+2V7zWkluIuJycKM8MQOhqR+GMqBlE7g2U6w/WPl7K7U0q5IJGZ8qGodVx FQKSOw/t4ostH8kG0kzPndhlXnYm3rX/IBpgw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718077749; x=1718682549; 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=NovBVUB3IuRICfhBAy0nOxMqOymWO9ozrSvccHzKh4E=; b=GsRhQbnbMbV9ewTifnK89iXMEppzGpvR7NWzM0OhxwArWa+epfp/nwjoJYur//RCC7 WkfQN+fb6g5r6dzneA6lZi4Bx9+3j3ow8fkMq8qqB3HPIcrw1VYFN2DChdDyvkYh1n8P Hm0OmYQHyGw11XhkGWxAQwQNdktq3h5sE0twUumMEKSXwB5kY/vm7btjkd73YA9rIfqA yngRStTug0YAQS/euf7giYc5sOWhPsECWmT9TReEHjILuU1V1SBsq6s0bgvbzr+cYiE9 CgK7X57OyqtFYfw0xTUC6mKm9hnPyPyT0IEJT/IO7nfu0FMiBmGAt9Heon/5wAx/gmJA K0/g== X-Forwarded-Encrypted: i=1; AJvYcCXqMU4GjEouaJwPIc3DoE2Q6lVEl4944mCRYkyFO0OP7PGM8OYns2qh4c27na2nKN0Tn6H701vBBjkNgEoMRrrTPgA= X-Gm-Message-State: AOJu0Yxns1dgeJlZzGGN6M+pt8llGsgKM4wCoeUkqECBlOYuUW8rplTh UC9mel61nP8m1SQaZpnZQNKYG4x/sJ8MwaGFFyf/VE4cDYVU9LHHLCDk6/cSUg== X-Google-Smtp-Source: AGHT+IGagdt9lLhDYs8ZQLE5oZzdPqsj7gJG4Akw+QK/PSWbea/brv9OKcIEQqIgzG1KsFZZteI9uA== X-Received: by 2002:a05:6359:6b83:b0:19f:34c5:db76 with SMTP id e5c5f4694b2df-19f34c5dcb0mr814063555d.23.1718077749288; Mon, 10 Jun 2024 20:49:09 -0700 (PDT) Received: from localhost (213.126.145.34.bc.googleusercontent.com. [34.145.126.213]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-6e5fb4df3a5sm4226581a12.50.2024.06.10.20.49.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jun 2024 20:49:08 -0700 (PDT) From: jeffxu@chromium.org To: rdunlap@infradead.org Cc: akpm@linux-foundation.org, cyphar@cyphar.com, david@readahead.eu, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jeffxu@chromium.org, jeffxu@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 Subject: [PATCH v2 1/1] mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC Date: Tue, 11 Jun 2024 03:49:01 +0000 Message-ID: <20240611034903.3456796-2-jeffxu@chromium.org> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog In-Reply-To: <20240611034903.3456796-1-jeffxu@chromium.org> References: <20240611034903.3456796-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A7888140007 X-Stat-Signature: f94h1n16ngrtq5thzsqtzh67cock4w34 X-HE-Tag: 1718077750-422053 X-HE-Meta: U2FsdGVkX19QpotCzFtfFiGa5NdKkI42iqUvY9IsfUgAbtO7gPk5welnDjwoOxRS6d2dC0H4ucn5fQGw+eSs1oa/jRQ/Egjr13QoGsojiUvmH0Uakgg4N0c/SNF0OnpOW4IL+5hHzCJDSIXbPpx/novnE/+PXnNFTSnf73ZGcHFcFvTj8NU1yiv1ZkdOPPmpUHBd/XaWa3Uoz8qfGL+FcGoKfQLfhOBALzFDduSFAVZI0S5U6EzVYqKJxZ0Yjy93F8m2piO1JWJVwdP4Bj6aR1+zgHZS8wo1bL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+7R7X+EOoFJpq6/AkDOJydBMlqBkf88pAJbQlqMdh+df130S3ZRC4Rr5OiGBIcDPqEYXv+qifWS8V4I/qUuB6sRNXP6ZYw2j5egtgfcBM9JEq88QzjT161b68a0GXrOlef7mJ3kpky//Bhl7TO8cvYYydBMhdGasHbHe0HpTnNberyWkHPgnJRNWhVQgv2bsA6QcGPMartbFm5x+xzi4EpJ3GFfB+hzDb680DkemfaYDwvGYdBpNexXcOpGTOnoQpF7A4xDLzaENfYTHoFFQF4IFHHHjRAXOOSBx2z 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 Add documentation for memfd_create flags: MFD_NOEXEC_SEAL and MFD_EXEC Cc: stable@vger.kernel.org Signed-off-by: Jeff Xu --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/mfd_noexec.rst | 86 ++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 Documentation/userspace-api/mfd_noexec.rst diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst index 5926115ec0ed..8a251d71fa6e 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -32,6 +32,7 @@ Security-related interfaces seccomp_filter landlock lsm + mfd_noexec spec_ctrl tee diff --git a/Documentation/userspace-api/mfd_noexec.rst b/Documentation/userspace-api/mfd_noexec.rst new file mode 100644 index 000000000000..ec6e3560fbff --- /dev/null +++ b/Documentation/userspace-api/mfd_noexec.rst @@ -0,0 +1,86 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +Introduction of non executable mfd +================================== +:Author: + Daniel Verkamp + Jeff Xu + +:Contributor: + Aleksa Sarai + +Since Linux introduced the memfd feature, memfds have always had their +execute bit set, and the memfd_create() syscall doesn't allow setting +it differently. + +However, in a secure-by-default system, such as ChromeOS, (where all +executables should come from the rootfs, which is protected by verified +boot), this executable nature of memfd opens a door for NoExec bypass +and enables “confused deputy attack”. E.g, in VRP bug [1]: cros_vm +process created a memfd to share the content with an external process, +however the memfd is overwritten and used for executing arbitrary code +and root escalation. [2] lists more VRP of this kind. + +On the other hand, executable memfd has its legit use: runc uses memfd’s +seal and executable feature to copy the contents of the binary then +execute them. For such a system, we need a solution to differentiate runc's +use of executable memfds and an attacker's [3]. + +To address those above: + - Let memfd_create() set X bit at creation time. + - Let memfd be sealed for modifying X bit when NX is set. + - Add a new pid namespace sysctl: vm.memfd_noexec to help applications to + migrating and enforcing non-executable MFD. + +User API +======== +``int memfd_create(const char *name, unsigned int flags)`` + +``MFD_NOEXEC_SEAL`` + When MFD_NOEXEC_SEAL bit is set in the ``flags``, memfd is created + with NX. F_SEAL_EXEC is set and the memfd can't be modified to + add X later. MFD_ALLOW_SEALING is also implied. + This is the most common case for the application to use memfd. + +``MFD_EXEC`` + When MFD_EXEC bit is set in the ``flags``, memfd is created with X. + +Note: + ``MFD_NOEXEC_SEAL`` implies ``MFD_ALLOW_SEALING``. In case that + an app doesn't want sealing, it can add F_SEAL_SEAL after creation. + + +Sysctl: +======== +``pid namespaced sysctl vm.memfd_noexec`` + +The new pid namespaced sysctl vm.memfd_noexec has 3 values: + + - 0: MEMFD_NOEXEC_SCOPE_EXEC + memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like + MFD_EXEC was set. + + - 1: MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL + memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like + MFD_NOEXEC_SEAL was set. + + - 2: MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED + memfd_create() without MFD_NOEXEC_SEAL will be rejected. + +The sysctl allows finer control of memfd_create for old software that +doesn't set the executable bit; for example, a container with +vm.memfd_noexec=1 means the old software will create non-executable memfd +by default while new software can create executable memfd by setting +MFD_EXEC. + +The value of vm.memfd_noexec is passed to child namespace at creation +time. In addition, the setting is hierarchical, i.e. during memfd_create, +we will search from current ns to root ns and use the most restrictive +setting. + +[1] https://crbug.com/1305267 + +[2] https://bugs.chromium.org/p/chromium/issues/list?q=type%3Dbug-security%20memfd%20escalation&can=1 + +[3] https://lwn.net/Articles/781013/