From patchwork Fri Jun 7 20:35:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13690642 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 A3BF5C27C53 for ; Fri, 7 Jun 2024 20:36:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C9A76B009C; Fri, 7 Jun 2024 16:36:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 351646B009D; Fri, 7 Jun 2024 16:36:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 218C36B009E; Fri, 7 Jun 2024 16:36:00 -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 02AAA6B009C for ; Fri, 7 Jun 2024 16:35:59 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7896BA344F for ; Fri, 7 Jun 2024 20:35:59 +0000 (UTC) X-FDA: 82205249238.20.66D68E8 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf23.hostedemail.com (Postfix) with ESMTP id 8BFC8140006 for ; Fri, 7 Jun 2024 20:35:57 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=DvNa596O; spf=pass (imf23.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.171 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=1717792557; 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=/oXWJ5Wemf0IINqz67lVh3TgdBP0Qd9snz/STsYYB+I=; b=d3amHjSQms4yfMw6Q2xL++ZPgHyWKX09+p3u8U/9i9Om+eiNf/Rf8p6o0u5XuTKZEn5Fnj Zrq49NwXKpguomscOmePlgFV/wbfw+Bzd8ORuKEqACZAW4vMAdnDaQgdT6R9FaEjt1UqZG ehMzn0FbE1D02QIOSxnaklOqktHXRjY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=DvNa596O; spf=pass (imf23.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.171 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=1717792557; a=rsa-sha256; cv=none; b=e0wCjDQZII+F8XHl+w940Mzjex7QJyivEFOAeFzVJyEeQoedVZixIJmfnQMT4ttp9pkYY2 L9Bt2POeQbCmAd6OZe1bjSHA8tqLq1YyGs1/DQJTimxN1MY37taPvEIjqmFW27vS6kvUIO +GwUxmcfvliYTfIr5JNEiNzuhE8GV8E= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1f47f07acd3so24076225ad.0 for ; Fri, 07 Jun 2024 13:35:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1717792556; x=1718397356; 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=/oXWJ5Wemf0IINqz67lVh3TgdBP0Qd9snz/STsYYB+I=; b=DvNa596OFO0qPCXSBcakWVdc9e0HWbyrZ9ves/N1cpjNQQT6x7JMdyvge7IhHAMjMi +q2tVOkKNRtjOy3827Kl3Jo5/RO/a9P2bbbqhBVkOfM2QlkEhBj51JQcI2MhqIZaqvNY myc1mxyphY/Fx9T08beE+VS7R5lxH27HSi5IA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717792556; x=1718397356; 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=/oXWJ5Wemf0IINqz67lVh3TgdBP0Qd9snz/STsYYB+I=; b=CV1Z6jI6AaixC39G4WShGF1p9y/stuhVSNRYOCcpFTk0r63QqOhG34QOqKwVZFPpv9 PTib2riZTQMfa3pdbWMHzlfdeW70KRBISp7W3IQ0iYWl5KgTCh1IKkHHRbmvhadSIEzw tHxExnKTk4sRZFsTrDRu/x3Qt7ovtq24y2Yl3DPJS0RJZZSbcTwWSnGzDKcS41jVl/Qo FmS1cDUst+PTKegy9kfS/m8lwGzDwv3RmxpjhXr1Fvq7I/7VIJVLUqOZHwG4T6Xr+NDV /vQO+mlnH/lPRo9oRgLZEHZFCyssmdb9d8IYPdMM14yOzaxhx75/I88o66nMCNz5UmiM mJzw== X-Forwarded-Encrypted: i=1; AJvYcCWexPAxd7K8/5zBGXxzWsxWDOgG0u82Ikyvzre0i16H7ZJOdZLcyzVdT/mPW7cb9CtkwvatlkopsusarT5MlRj1tL0= X-Gm-Message-State: AOJu0YyjR7JN4kO4MCdjIuEWNF124MMea4hxcAnDCPV0aGPVTdqh/cqY gG3Po3bb+p0wFscsM3gv1mkd/fpu5tceAr7n5rfd6ric4GZGirm+zz39cREKNQ== X-Google-Smtp-Source: AGHT+IFnrQY/Ai8Uk6udhcAWbv2t7ZCDY3TRPvTN4XQwhL+Rwb7V0TBa2XUWwrA1IeeDyrYAYKBOgQ== X-Received: by 2002:a17:902:c94f:b0:1f6:8a19:4562 with SMTP id d9443c01a7336-1f6dfc426d6mr25813685ad.24.1717792555893; Fri, 07 Jun 2024 13:35:55 -0700 (PDT) Received: from localhost (213.126.145.34.bc.googleusercontent.com. [34.145.126.213]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-1f6bd7ccfd3sm38538225ad.132.2024.06.07.13.35.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 Jun 2024 13:35:55 -0700 (PDT) From: jeffxu@chromium.org To: jeffxu@chromium.org Cc: akpm@linux-foundation.org, cyphar@cyphar.com, david@readahead.eu, 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, pobrn@protonmail.com, skhan@linuxfoundation.org, stable@vger.kernel.org Subject: [PATCH v1 1/1] mm/memfd: add documentation for MFD_NOEXEC_SEAL MFD_EXEC Date: Fri, 7 Jun 2024 20:35:41 +0000 Message-ID: <20240607203543.2151433-2-jeffxu@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog In-Reply-To: <20240607203543.2151433-1-jeffxu@google.com> References: <20240607203543.2151433-1-jeffxu@google.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 8BFC8140006 X-Stat-Signature: 67pszk7ytiofp5ogr7np8s6y3tux8guh X-HE-Tag: 1717792557-714221 X-HE-Meta: U2FsdGVkX19EJUFUodDzinFv1cMvltoi/1/Dg0jrCMDlNJwPOyUTNe8X0mB7wb4jaL2dKsSOGLsoThapIt8FJzOre7+74jCfdRWgVDbAhSr77TwKzHs21cOYAnLw/K8H95lha9j+Lx27QXVIV3aoVZBiu2BbwoWtovowgxH4DtFUlare9Wdc4FJ3M8Y00DLWHUKxDFKVOoXi6IqzMe5BV/EZsjziMER881SSJwLpUXY0ONntbQw7b5nBNUQi/pIkCloWYki8GiZ6hNI2eWDCrkM7IZGApbYU/6vrp7dV5m9TGxZ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A5hXPGBsY+TpINM9jHthd/yRpLODCYVioPieCpRmF3RnCwGPx89CYlEi6cAMe7d9xh4GYwn5syHpdcd/9MXhqa1vdYCB7/MbmZzs1QSjdYiubMk0WhLeLrpJCdrt4sau0hLaW1r0hTYJfTiVT8N3Y1Dabk3nkFTQJNAnIbgLsRzD/PJMSl88zGZZV81mV3pmySyL24uIl2hdrSpFD+90+e8x4fmGBiJzylc/Z2rFD/CSzGgjlX0IgwlDPl/VLu7o3u8srE5tnrMcDrvKomwRaEtDwJWb7bMpAcbA== 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: FMD_NOEXEC_SEAL and MFD_EXEC 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..0d2c840f37e1 --- /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, memfd 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 in 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 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. + - 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 + 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/