Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1813496rwd; Thu, 18 May 2023 18:28:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ51C8d6IvMGDeQXrKUK0KiWFyxZ8S6SlEOVTfrnyreQzFxO7nZDrLqJ/XtXmqmyWTKCMxF0 X-Received: by 2002:a17:90a:8185:b0:246:634d:a89c with SMTP id e5-20020a17090a818500b00246634da89cmr355556pjn.41.1684459726420; Thu, 18 May 2023 18:28:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684459726; cv=none; d=google.com; s=arc-20160816; b=ly7/FKvIdmtUGCbngNfGVrWATGmmYd66R8Eggx/zuAsNUsuV95lM1ktC3gYBpl2nsa qaRcV1JsUNn6uWeI1+b1tkGQd3p4Zzkag5ed6mcrYUemJhngmKICLOmEozkzYFnF8JJh mzuxVEa1aA9u2CJ9/p9cpRXwnp1hgiBIOxiodDzvm2SDEeIg1nWYf5hM0vTwbQtN5sGc 20Wuq9Fz/hSVlSb2d5dn3j+osU4YUuVcAu2Zafcf58Zt55ZEBMbsJuZDJV0ew1ZDmHjx x5t5KrtU9jowl/iWyQ2SZXwFWDKcv+01qV7e3wMU668Xug4EpOHY1CFWtRDMIUivVEl3 hXjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=UqVmcIUSI4Xw+jP09wSJXm4OiN2hCwHu9FnNFUiMdYo=; b=hAo/vU3Y2851or7GsNLt51YO2AX3pf5ZfKZToQWOgPZ5u3iRogFrrN64aYKkPas95t B9KFMAlRP0iXBADEqinxLYmvNkPbSUzcUvnzS2zovWmAqUWbCOMiDn5BvRU3lnrFTgwL EZkvnrUjMrEYm47E6rGgrnO6/3ZLxGO2tVx+dRkmw3B41uSZkqF4rDoRTgds2Dyh3ALN jtrQBEdgkDkfMglwD4erL1PJRENnFGo6vXgSd7MvkEJAwPr9qRM/ZNHT/hthPjqiErlx 0sOELt+PIHUaOiuOTGBnCela5L9mvuS0XGLAy7IpYeDAGSdvHTsKKTjs6X+2Rv2tW0nz DhmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=wEdgpcJ7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v5-20020a63b945000000b0052ca3209fc9si2708801pgo.654.2023.05.18.18.28.34; Thu, 18 May 2023 18:28:46 -0700 (PDT) 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=@google.com header.s=20221208 header.b=wEdgpcJ7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbjESBWZ (ORCPT + 99 others); Thu, 18 May 2023 21:22:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbjESBWY (ORCPT ); Thu, 18 May 2023 21:22:24 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0178B1713 for ; Thu, 18 May 2023 18:22:01 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3f5dbd8f677so15905e9.1 for ; Thu, 18 May 2023 18:22:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684459320; x=1687051320; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UqVmcIUSI4Xw+jP09wSJXm4OiN2hCwHu9FnNFUiMdYo=; b=wEdgpcJ7Dj3UtTNIU2HlM1dn84V1NZeOPc9e5NTTZ9Qn8Ec7oCYJok2TaVM4qFxyRM FbcmfZ9z2gaRNvuKFS2K8atqsFjmPb2UE2b0ugJLBPHiSjwFZgv5e6KPjpBEIq8m8afm XJHV0AN3KeoKo4bZoTm14Q1q8fo6IEKrrGU8fpXh6lu+6jeIuuAX/nECVHVPC9b3mlK3 ym1FsB2/o21Em8ybiFGt4mpC7RspE4ILcTv8bq64xXEA4gccNf8v5xfOp1bGvPOWJl0v iSuFttY/MFvWUYEG0bu9PDAsRNQXBKlE1hQDE8ckKFe8niEpj9AyfVYkSUuvzNuYMhZw Fbwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684459320; x=1687051320; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UqVmcIUSI4Xw+jP09wSJXm4OiN2hCwHu9FnNFUiMdYo=; b=ergmVIWhAJ3/kqpLgd66qPOigunIqlYZg+KT4LL39j/prFGvEnHJQbydi9s6jq7cq9 rhVYtb2+lMieyYVLhMymOgElyzSgZg+FM7mNf8ZNe+VcOzF06BZyJY6BzkS7j4rIK/0p Z7DpH9TbSxtO9Ofy6g5G4kOw+SVag+gBdl2MLDV3H5OkrEnG3Qlz6oRMI3XF5cGys/xv PGgMlhqQY/Va1XW0L+6/oMa1Rz1Hj5fJwredJ4UqUQhvvS10d2GdgtR+JeAHczXX1cSu W2I1TjXpTHB+8ujrSo39U4DzJ1adoavtGoBjvGXz09BLM8g11GVJNd2Pw2nWaUm0T3+Y OBrg== X-Gm-Message-State: AC+VfDzEiOSUr/XCnLSjXvKLTE83xQ84NMxLjVskRnUFBl5ROXM6Oc5I hAOySw6kcz8wnNRADbFItTiKvSh4/q1YOWMCymXwfw== X-Received: by 2002:a05:600c:4994:b0:3f1:758c:dd23 with SMTP id h20-20020a05600c499400b003f1758cdd23mr89382wmp.7.1684459320145; Thu, 18 May 2023 18:22:00 -0700 (PDT) MIME-Version: 1.0 References: <20230519011915.846407-1-jeffxu@chromium.org> In-Reply-To: <20230519011915.846407-1-jeffxu@chromium.org> From: Jeff Xu Date: Thu, 18 May 2023 18:21:24 -0700 Message-ID: Subject: Re: [PATCH v1 0/6] Memory Mapping (VMA) protection using PKU - set 1 To: jeffxu@chromium.org Cc: dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, keescook@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 This is updating code comments from v0. There are on-going discussions related to threat-model and io_uring which we can use the V0 thread. On Thu, May 18, 2023 at 6:19=E2=80=AFPM wrote: > > From: Jeff Xu > > This is the first set of Memory mapping (VMA) protection patches using PK= U. > > * * * > > Background: > > As discussed previously in the kernel mailing list [1], V8 CFI [2] uses > PKU to protect memory, and Stephen R=C3=B6ttger proposes to extend the PK= U to > memory mapping [3]. > > We're using PKU for in-process isolation to enforce control-flow integrit= y > for a JIT compiler. In our threat model, an attacker exploits a > vulnerability and has arbitrary read/write access to the whole process > space concurrently to other threads being executed. This attacker can > manipulate some arguments to syscalls from some threads. > > Under such a powerful attack, we want to create a =E2=80=9Csafe/isolated= =E2=80=9D > thread environment. We assign dedicated PKUs to this thread, > and use those PKUs to protect the threads=E2=80=99 runtime environment. > The thread has exclusive access to its run-time memory. This > includes modifying the protection of the memory mapping, or > munmap the memory mapping after use. And the other threads > won=E2=80=99t be able to access the memory or modify the memory mapping > (VMA) belonging to the thread. > > * * * > > Proposed changes: > > This patch introduces a new flag, PKEY_ENFORCE_API, to the pkey_alloc() > function. When a PKEY is created with this flag, it is enforced that any > thread that wants to make changes to the memory mapping (such as mprotect= ) > of the memory must have write access to the PKEY. PKEYs created without > this flag will continue to work as they do now, for backwards > compatibility. > > Only PKEY created from user space can have the new flag set, the PKEY > allocated by the kernel internally will not have it. In other words, > ARCH_DEFAULT_PKEY(0) and execute_only_pkey won=E2=80=99t have this flag s= et, > and continue work as today. > > This flag is checked only at syscall entry, such as mprotect/munmap in > this set of patches. It will not apply to other call paths. In other > words, if the kernel want to change attributes of VMA for some reasons, > the kernel is free to do that and not affected by this new flag. > > This set of patch covers mprotect/munmap, I plan to work on other > syscalls after this. > > * * * > > Testing: > > I have tested this patch on a Linux kernel 5.15, 6,1, and 6.4-rc1, > new selftest is added in: pkey_enforce_api.c > > * * * > > Discussion: > > We believe that this patch provides a valuable security feature. > It allows us to create =E2=80=9Csafe/isolated=E2=80=9D thread environment= s that are > protected from attackers with arbitrary read/write access to > the process space. > > We believe that the interface change and the patch don't > introduce backwards compatibility risk. > > We would like to disucss this patch in Linux kernel community > for feedback and support. > > * * * > > Reference: > > [1]https://lore.kernel.org/all/202208221331.71C50A6F@keescook/ > [2]https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQ= MyXgeaRHo/edit?usp=3Dsharing > [3]https://docs.google.com/document/d/1qqVoVfRiF2nRylL3yjZyCQvzQaej1HRPh3= f5wj1AS9I/edit > > * * * > Current status: > > There are on-going discussion related to threat model, io_uring, we will = continue discuss using v0 thread. > > * * * > PATCH history: > > v1: update code related review comments: > mprotect.c: > remove syscall from do_mprotect_pkey() > remove pr_warn_ratelimited > > munmap.c: > change syscall to enum caller_origin > remove pr_warn_ratelimited > > v0: > https://lore.kernel.org/linux-mm/20230515130553.2311248-1-jeffxu@chromium= .org/ > > Best Regards, > -Jeff Xu > > > Jeff Xu (6): > PKEY: Introduce PKEY_ENFORCE_API flag > PKEY: Add arch_check_pkey_enforce_api() > PKEY: Apply PKEY_ENFORCE_API to mprotect > PKEY:selftest pkey_enforce_api for mprotect > PKEY: Apply PKEY_ENFORCE_API to munmap > PKEY:selftest pkey_enforce_api for munmap > > arch/powerpc/include/asm/pkeys.h | 19 +- > arch/x86/include/asm/mmu.h | 7 + > arch/x86/include/asm/pkeys.h | 92 +- > arch/x86/mm/pkeys.c | 2 +- > include/linux/mm.h | 8 +- > include/linux/pkeys.h | 18 +- > include/uapi/linux/mman.h | 5 + > mm/mmap.c | 31 +- > mm/mprotect.c | 17 +- > mm/mremap.c | 6 +- > tools/testing/selftests/mm/Makefile | 1 + > tools/testing/selftests/mm/pkey_enforce_api.c | 1312 +++++++++++++++++ > 12 files changed, 1499 insertions(+), 19 deletions(-) > create mode 100644 tools/testing/selftests/mm/pkey_enforce_api.c > > > base-commit: ba0ad6ed89fd5dada3b7b65ef2b08e95d449d4ab > -- > 2.40.1.606.ga4b1b128d6-goog >