Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1432503rwb; Thu, 1 Dec 2022 17:46:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf7TCuVjh41jRbXTz7YRhDIdieOAnzousblDc2tmouioXvGEu6Z4TH/XDxTGC1bW72hqWiNB X-Received: by 2002:a17:906:2988:b0:7c0:b56a:eadf with SMTP id x8-20020a170906298800b007c0b56aeadfmr3837365eje.271.1669945575357; Thu, 01 Dec 2022 17:46:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669945575; cv=none; d=google.com; s=arc-20160816; b=WxqA8ypjrBNhqstcybHR82FPtoMgmJzhWLrqnylCqMMRxMS32XORzGTIblwlOxPzQi 8sr3p2a75PmDo0cINQPUcp5+C+XqnMhUUAwuKfkyUNBGK6aLZJz/0hfFDDpT0lgFLKbm rlba1f2gsC+44WqhzbHVIVzl+Os6NusAwHKYxhrqSr4edNy5nK6NWwlrfTZSA9CmzIwJ 9g2IyyMODPqEIcVHvuuflNVTypDkNRvv0lWCyh01zjsAzcOm8hNOKGj0s97pLVCxt4sf 7w5wA103+WIqROXK3RWOYPulAMDPtGx42SPrvybUD/HOXvffuCK/znjPFnDFslwHGGy9 2HxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=CFb3U5p+g9lHeAJEKkWmB54GJi34sM+dMSVNG4ofgcI=; b=OGX7Fos2KP/9nR2lWqP9bcoleLoqsgzlHkTOYZv0iDamG1WPdZaAmCFUqJAUjzEifr MyM6Lvv9tscwRHA5oHpv0BcQXkqYnJa1pNc7fv2B2QErvG7ljX3suCswJiNrPrZ87+GP nWhdzhFTHIq8eI9nN2MggjmuctdFV8RuPyc8vR5i5pePkU7TdWLtVaweL7AOq3AMxw8U MCRvY8oiQzRPiWGrPGDRpcmltv1xAHCxrmXpx6h5a5HE0IpKeYSob6wl6O9H39+Ca5Nu nVnyGZLW85XTQFV/FR0Sdh7LPwqkQBm75b3mud++MRUKueskihNqBpn47f4SM4NoqZsD jPsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mdafWcQT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u14-20020aa7d88e000000b0046b4fc516b7si4835054edq.128.2022.12.01.17.45.55; Thu, 01 Dec 2022 17:46:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mdafWcQT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231358AbiLBBfh (ORCPT + 81 others); Thu, 1 Dec 2022 20:35:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbiLBBff (ORCPT ); Thu, 1 Dec 2022 20:35:35 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5296AD20A1 for ; Thu, 1 Dec 2022 17:35:34 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id 130so3543814pfu.8 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=PdF8Xc7Wmqtk1A2tFf7GelHlQ9gmctgET7LBwo7zWsEDteoFNAFgXNjHBOVhEIy7QA FyHVmIewNFngG4TC4WZmKaPIk799VqR/WFWUFpbl26f/N0zUDbGVOPGeRuf57KGCOduf ccWMjF38XuLZU4UEYy9/OcdzjC9Ld9mdTMDbslmOC/3Z5PYGw1/ozdsZgcGqOik/APxv oZe1R6Rlx6fYZ67xpTgNKPRGppBXp4xUEo89gUVpL9KIYZnfKsV/YjBdSr5f+7CmEy95 8Fqh7vsQTgEhSUN2abOXRb5nBsPJ0PlHvteN2vMpLDF9+g0uLomtl3Nktc951ywirMcq fF1g== X-Gm-Message-State: ANoB5pm4QAfhqTNeiFBB9jwqazCBhzEuw+Dn8579Qv6rlg57Z+OxhoyJ SZ+ANTIB7ojevgf1gnEG/qGoUQ== 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 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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