From patchwork Thu Dec 15 00:12:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 2979 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp43539wrn; Wed, 14 Dec 2022 16:15:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf4rDSwCYsbD6YSZfv/5AnwmJWDlPAL/Kc3ClKSM3K+KzbRUrA5vvg7CAYqAx2IkhhH1w/nt X-Received: by 2002:a17:906:d922:b0:7c1:31c:e884 with SMTP id rn2-20020a170906d92200b007c1031ce884mr21368003ejb.17.1671063339378; Wed, 14 Dec 2022 16:15:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671063339; cv=none; d=google.com; s=arc-20160816; b=yPcB8Pv12dg0mlvnop1jf8oX7vQG9Nm+bS/NU4FrSxFLVd/mBLGPfezxP7v8Ik3TYz 09TCFbZKR9hS3vPnJjW5qRP6ScW1WMSkthQlE8/ukmyZxqotgA4jV/fmgKxDGkhTDbeh WVGa4CRZGIhuPlM33VzaxXa/FMsGMNXqvofEmqKSyX478XWzInL30Ir2/+i0XJ1FLFqE RNNA5UR6/pZel+O8rDah/utgTOk6SI9LH6CRayHeu66fZy+v6TRg27Pp72EkIksECsRY di/71hnQ+EoFX4JNxAKXsEbLmKxmsTB4KiN6yVndph9gDfJ8ZbAmf0OLL2eGv9oTqBOw 6B3A== 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=5JdRkrY4AHyx5V8nEAerxJVsYW5SOsjwL/9h06jnfpo=; b=KchVkOtUN3sff8av/04zI8ik5eZwtmu5K0DAokzjuHgntpYuPD8tKmQLGkWRSECsDs H/fI6gCDe0O5MzJh43u3DPS8Ao+JhapxlNCsJzB95stzp7zBGkCbkmT2y4N5dmNx4DqN P3tGMLKveNxLBVmzUA5uVPa2AI9b18Ij1WapCFnkEe8ZDqui+GPypPg2JpyTttdUwH6r 6Eb8rucj4MNDpV/bqP4Ia1WNSekFhW+riYAMGnAQNIiKNwNx6EYUV2vAYSLOEaTCXqqb i/rycAoSuyKwUvQVvhxoxDL9LydyWUv1gPqJC/UnoYqdV34WednqrxQyEAWKadshAxJA eqHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CgO9jXmX; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr30-20020a1709073f9e00b0078dcddc1b8csi12940072ejc.788.2022.12.14.16.15.14; Wed, 14 Dec 2022 16:15:39 -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=CgO9jXmX; 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 S230176AbiLOAOV (ORCPT + 99 others); Wed, 14 Dec 2022 19:14:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbiLOANl (ORCPT ); Wed, 14 Dec 2022 19:13:41 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60F9C532D8 for ; Wed, 14 Dec 2022 16:12:14 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id 3-20020a17090a098300b00219041dcbe9so990446pjo.3 for ; Wed, 14 Dec 2022 16:12:14 -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=5JdRkrY4AHyx5V8nEAerxJVsYW5SOsjwL/9h06jnfpo=; b=CgO9jXmXzNorxxCMtt58UeRi5g6/jidHsD3ynHjxaWxA8VdHsZJSxDrf+YAy5/+4rQ F0DOwu5R5L6IOCPs5dHRKeQXGZk6Kh4bxTWV9oyybTWUcY19oJZv1nYIn8SB8/kMT96W +UtNwSduBVXSD/fBZm3EIsNZoK1klg+cu/Qn0= 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=5JdRkrY4AHyx5V8nEAerxJVsYW5SOsjwL/9h06jnfpo=; b=uMYKl6ndm6VC1TgeoT7gzcd9Q9rTEzAI6nf/IK+7Jr240/x7U4HlQNzMQyOxSoZhPb n3RJxOqcv42jbKH0FQO1whmWCH+iooqTTkJhBaGEI+WeqSMy75BmwyZtKySZNdeZtdsI o+acftmP0ZUyL71MPR06kacMdJj5b7Cy39GnVosZfskDBvFgIyKgu3OtXjH4KtBSEo5m inVTFg42wU5pEWzRqHMvGyx9Cfo7/WkYJubXzM73TzeK63kegCKVMa+DK1GPdiQWlP9n z4VuPCJsCOpPNsxEVpUIyQjYq7r2yEAKMgHZEi2sH8TPhjUV4UEeCXU9zQkQkqgQD9Ym XKhA== X-Gm-Message-State: ANoB5pmECmvE9IwDIyofXFImJRWUTBjdwV+GZW+XQz2TeAXZsExQOGUo 205fo5RgttkNJqzwS2CvdMAjvA== X-Received: by 2002:a17:90a:ee8a:b0:219:b034:9415 with SMTP id i10-20020a17090aee8a00b00219b0349415mr27482016pjz.12.1671063133657; Wed, 14 Dec 2022 16:12:13 -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 3-20020a17090a08c300b0021937b2118bsm1845738pjn.54.2022.12.14.16.12.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 16:12:13 -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, jannh@google.com, linux-hardening@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v8 0/5] mm/memfd: introduce MFD_NOEXEC_SEAL and MFD_EXEC Date: Thu, 15 Dec 2022 00:12:00 +0000 Message-Id: <20221215001205.51969-1-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog MIME-Version: 1.0 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=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752236911915125478?= X-GMAIL-MSGID: =?utf-8?q?1752236911915125478?= 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 the 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. Change history: v8: - Update ref bug in cover letter. - Add Reviewed-by field. - Remove security hook (security_memfd_create) patch, which will have its own patch set in future. v7: - patch 2/6: remove #ifdef and MAX_PATH (memfd_test.c). - patch 3/6: check capability (CAP_SYS_ADMIN) from userns instead of global ns (pid_sysctl.h). Add a tab (pid_namespace.h). - patch 5/6: remove #ifdef (memfd_test.c) - patch 6/6: remove unneeded security_move_mount(security.c). v6:https://lore.kernel.org/lkml/20221206150233.1963717-1-jeffxu@google.com/ - Address comment and move "#ifdef CONFIG_" from .c file to pid_sysctl.h v5:https://lore.kernel.org/lkml/20221206152358.1966099-1-jeffxu@google.com/ - Pass vm.memfd_noexec from current ns to child ns. - Fix build issue detected by kernel test robot. - Add missing security.c v3:https://lore.kernel.org/lkml/20221202013404.163143-1-jeffxu@google.com/ - Address API design comments in v2. - Let memfd_create() to set X bit at creation time. - A new pid namespace sysctl: vm.memfd_noexec to control behavior of X bit. - A new security hook in memfd_create(). v2:https://lore.kernel.org/lkml/20220805222126.142525-1-jeffxu@google.com/ - address comments in V1. - add sysctl (vm.mfd_noexec) to set the default file permissions of memfd_create to be non-executable. v1:https://lwn.net/Articles/890096/ [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/ Daniel Verkamp (2): mm/memfd: add F_SEAL_EXEC selftests/memfd: add tests for F_SEAL_EXEC Jeff Xu (3): mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd selftests/memfd: add tests for MFD_NOEXEC_SEAL MFD_EXEC include/linux/pid_namespace.h | 19 ++ include/uapi/linux/fcntl.h | 1 + include/uapi/linux/memfd.h | 4 + kernel/pid_namespace.c | 5 + kernel/pid_sysctl.h | 59 ++++ mm/memfd.c | 56 +++- mm/shmem.c | 6 + tools/testing/selftests/memfd/fuse_test.c | 1 + tools/testing/selftests/memfd/memfd_test.c | 341 ++++++++++++++++++++- 9 files changed, 489 insertions(+), 3 deletions(-) create mode 100644 kernel/pid_sysctl.h base-commit: eb7081409f94a9a8608593d0fb63a1aa3d6f95d8