From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 576BBC3A5A7 for ; Fri, 2 Dec 2022 01:35:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230456AbiLBBff (ORCPT ); Thu, 1 Dec 2022 20:35:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229893AbiLBBff (ORCPT ); Thu, 1 Dec 2022 20:35:35 -0500 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58CCDD20A4 for ; Thu, 1 Dec 2022 17:35:34 -0800 (PST) Received: by mail-pg1-x531.google.com with SMTP id 6so3180813pgm.6 for ; Thu, 01 Dec 2022 17:35:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CFb3U5p+g9lHeAJEKkWmB54GJi34sM+dMSVNG4ofgcI=; b=mdafWcQTAJYDEQ2dOGqG6ilVN2VKgB8RpEUHnDf8RiKhaOq0MKEbdp5cfX8NIvn97I 6/ahQ5hyeJLoiAtAMJ/lMf7umtTcDcoTi33aivbciA8wLlGp2BDEi42fdNinfTJ34jTz 9IclAngJoJMYwcRhCg0k4ykpIKZU8Yl5koyAo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CFb3U5p+g9lHeAJEKkWmB54GJi34sM+dMSVNG4ofgcI=; b=wXyD5yJ2nmD5TVUloWWQcORai/SR99dJIEINZ/J4FOO1cVeDgUe9YwhMX/kgttlOcv VZVnafLNo+IKB6nMxF0uBWwICLkZfdIzS/nw25SxwJPCJzga6BoB/+KXeV2S37txYu94 LLkj6cDnAm4wRge8hat0KSsAmffvDA3ja5VoTzj0nrhIkDTeVGslmRuPjboDfsRiq+IA FfMkYJ7OHOmtDupUkdQQYwGYdYWPpvI143qX6u038wHkXXzsKQAhRj6cZbEJkEGXuYhv hY/EhloV/vJLGYtdQQlnOS5kClfe7cz5mpDu1Rsi3UumNs5um+jjwP+1qHnV6hyadwRp 9wJA== X-Gm-Message-State: ANoB5pmvsY7pdN5oolJty1dCkuA7qiBk2UE2x49uIDNMwp9brPeiAbCO 2fAJwDDVDyM/I+jvNxl0fDgnXg== X-Google-Smtp-Source: AA0mqf7A4pjAtf52pyg17PB+DLjH1xkxcDzx5zywoVg/h0X18sxGNmaa3OBZrWn1jeYQiHgNE8kbhw== X-Received: by 2002:aa7:8686:0:b0:576:1b68:2b59 with SMTP id d6-20020aa78686000000b005761b682b59mr5884350pfo.72.1669944933695; Thu, 01 Dec 2022 17:35:33 -0800 (PST) Received: from jeffxud.c.googlers.com.com (30.202.168.34.bc.googleusercontent.com. [34.168.202.30]) by smtp.gmail.com with ESMTPSA id s13-20020a65690d000000b00477fb27eaddsm3074241pgq.63.2022.12.01.17.35.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 17:35:33 -0800 (PST) From: jeffxu@chromium.org To: skhan@linuxfoundation.org, keescook@chromium.org Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jeffxu@google.com, jorgelo@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, mnissler@chromium.org, jannh@google.com, linux-hardening@vger.kernel.org, Jeff Xu Subject: [PATCH v3] mm/memfd: MFD_NOEXEC_SEAL and MFD_EXEC Date: Fri, 2 Dec 2022 01:33:58 +0000 Message-Id: <20221202013404.163143-1-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org From: Jeff Xu 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, this set of patches add following: 1> Let memfd_create() set X bit at creation time. 2> Let memfd to be sealed for modifying X bit. 3> A new pid namespace sysctl: vm.memfd_noexec to control behavior of X bit.  For example, if a container has vm.memfd_noexec=2, then memfd_create() without MFD_NOEXEC_SEAL will be rejected. 4> A new security hook in memfd_create(). This make it possible to a new LSM, which rejects or allows executable memfd based on its security policy. This is V3 version of patch: see [4] [5] for previous versions.   [1] https://crbug.com/1305411 [2] https://bugs.chromium.org/p/chromium/issues/list?q=type%3Dbug-security%20memfd%20escalation&can=1 [3] https://lwn.net/Articles/781013/ [4] https://lwn.net/Articles/890096/ [5] https://lore.kernel.org/lkml/20220805222126.142525-1-jeffxu@google.com/ Daniel Verkamp (2): mm/memfd: add F_SEAL_EXEC selftests/memfd: add tests for F_SEAL_EXEC Jeff Xu (4): mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC mm/memfd: security hook for memfd_create mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 4 + include/linux/pid_namespace.h | 19 ++ include/linux/security.h | 6 + include/uapi/linux/fcntl.h | 1 + include/uapi/linux/memfd.h | 4 + kernel/pid_namespace.c | 47 ++++ mm/memfd.c | 54 +++- mm/shmem.c | 6 + tools/testing/selftests/memfd/fuse_test.c | 1 + tools/testing/selftests/memfd/memfd_test.c | 305 ++++++++++++++++++++- 11 files changed, 445 insertions(+), 3 deletions(-) base-commit: eb7081409f94a9a8608593d0fb63a1aa3d6f95d8 -- 2.39.0.rc0.267.gcb52ba06e7-goog