From patchwork Mon May 15 13:05:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13241397 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 C0E5DC77B75 for ; Mon, 15 May 2023 13:06:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A005900004; Mon, 15 May 2023 09:06:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14FCE900002; Mon, 15 May 2023 09:06:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0178A900004; Mon, 15 May 2023 09:06:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E5B14900002 for ; Mon, 15 May 2023 09:06:40 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A26CBA12FE for ; Mon, 15 May 2023 13:06:40 +0000 (UTC) X-FDA: 80792513760.26.5C0D3B3 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf14.hostedemail.com (Postfix) with ESMTP id 22023100233 for ; Mon, 15 May 2023 13:06:01 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=NaHgRv2b; spf=pass (imf14.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.182 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=1684155962; 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: references:dkim-signature; bh=x8d5WHH7LCFe26MinsbsZnyby5kr64LG/XJK9V0WNKg=; b=rur/O5WwR5OAfj6b2G3fzJZba0liXYzfW3xjGN+kOl90UoDFHU5V4offDlgac6trK5e0yX zkVu+cV+7ddkmZulTDwVmAZRTdRyIeICscZwUZTRv2ILkS3WQgucFNqjQrtR/+TZ+nhOrI 0MWr83MB+H3YTdzFM+ie5DOgdqwXSJs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684155962; a=rsa-sha256; cv=none; b=Ck4O5aJBOYI1pss3cAD55rfTJSqUEzlA90+C2QZC16xghic/nQ6TN98EcSDkZa4caE4HM9 eXUrXuii8WMCWZzbJ+p/I/D8jtxvGOcuWQ/QY0d8/qMGFZnUTd25cDjHNtav/WagradhIM W1V+st1EOljs7fHfsJSeypvUOF+9t8g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=NaHgRv2b; spf=pass (imf14.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.182 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-53033a0b473so6199456a12.0 for ; Mon, 15 May 2023 06:06:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684155960; x=1686747960; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=x8d5WHH7LCFe26MinsbsZnyby5kr64LG/XJK9V0WNKg=; b=NaHgRv2bUNBZrF0o+5C3FTpheRmJqZQFaOSP8QzMXPq7WqJETIhWEOKc2lqj4Nm5T5 a32pCCSQx3QIgMU9tHmNWc24LCETh4+iOmKh8vrKudEVT2S+1tbvRWw9LG+/FQnfDIYd tKTg4DVijbBMGL0PW9Mno6mGxd/JJ4F4NkV9M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684155960; x=1686747960; 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=x8d5WHH7LCFe26MinsbsZnyby5kr64LG/XJK9V0WNKg=; b=Ld2nMmXaPFMzMopT/TK6dkdEvjdI2wx3b+69kEgd30S4L83VBU5jb5+itnhWosM+gu qlrsXK/R4OcSy+HiKeaa52lMMGvcRk7z/akqmCCpQMc0Cx3btbPPgxIFzUsy0z/D0sdU 9Ug9kYkELv+wbJ/U3md9RCSCY3FSiMa507pP7gFLuy9nOcB1HG1pE/5rjCBHEiJlK3eP mnwmkZZCi2VUOUoo8+lAc2zvMgeHl68jARssQFlaOyBa4Uo/tMjJRhvt3SwPhEDVzhpY dWSsBKRh7502cwpjXDT3YSHVEpNPavU1wAFQoJOfVOHVy3Lqgkhg12W6SmLnG+WvxRf2 SZdQ== X-Gm-Message-State: AC+VfDzWHGvRdUPDj/gNNdFtgsppzlfoc3xf9OtIam2C1c6Vye7yhTDe cE4/kt6gUpsn+OLKiQiHrx48Gw== X-Google-Smtp-Source: ACHHUZ5TQiYvEA3rPzvQv/lZBIMaILwMcV3Xu9P4Ckghl/s+uNjurYMNj1KtT/hqR4tacuNKKlahcA== X-Received: by 2002:a17:902:dace:b0:1ac:6e1f:d1bd with SMTP id q14-20020a170902dace00b001ac6e1fd1bdmr37375631plx.19.1684155960357; Mon, 15 May 2023 06:06:00 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id ji1-20020a170903324100b001a9b7584824sm13461277plb.159.2023.05.15.06.05.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 15 May 2023 06:05:59 -0700 (PDT) From: jeffxu@chromium.org To: dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, keescook@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com Cc: akpm@linux-foundation.org, jeffxu@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: [PATCH 0/6] Memory Mapping (VMA) protection using PKU - set 1 Date: Mon, 15 May 2023 13:05:46 +0000 Message-ID: <20230515130553.2311248-1-jeffxu@chromium.org> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog MIME-Version: 1.0 X-Rspamd-Queue-Id: 22023100233 X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: atysicdf5hdgybr8b89qayur4y9u6j55 X-HE-Tag: 1684155961-902804 X-HE-Meta: U2FsdGVkX19o/DEVVPCA6ybBVp9M81zFZ+K26LubT7v4pdZseKv1QrfY5KZa/Ajo210G4didksldmtXBKeZjW90b/A6VO7BDLW7nApDyCFZt3EREo/Tm2/d7g8aZOGgPjAQOfnvThXjRtILiwGhtbAJqpCpw/E0e13uXz9KXIV52wL6hHIN/kc/aYx7n9pWUOOXpPcqDW6/aNY1QIlt69ihH+vKL7t78N3BPQ+iT6tIpm1ZKLT3GluX/B+5HdWplv41hsphjCggnUug7Zav2WLOSp/8pimNGeotlBHKY1tWAtgiwqZZi0Lb59dm/pNiWmCa2GfMhY2n1aTN0ZYyBQpF5X321x86116k6/UdiabgJZlqc5qO+phE1DyLjm7zOkt2LbAR5qxKr86btYAtXoALHClOi5uZXEVafC+h2m8LIDhcHckHVYR6npey7m/TjDZcqqIePt9HvkoR3SeBj/Zvc+H+Og9RU8seL8gh74XZWMBqvGemaDpnnJqShPgRVMDT3i4sPlfZbqNkgFird6lh0y/Y8G0Rko7k5/ShA5WBfr4W9xiXTiT/Ct04FtBO+UypYs+LpwGB7DpnhTHl5rQ+KixM5gxcQR1d0+j0TFWbGBbeQ4LrAhBMgLpAOR+CYM9KGOVOh5HZbvGjSgKCZisvVQIvysW9XtzYKHQd9nOYcvh9fZC8FOi89Nb2tkDU/+cI41EdJIc4/Ig+u+xcpj9Sh00JvlJdBGBCbacGAaCm1hYQjLMTCxARdcUxqAzCM590TYA9mnVDugVyQ6WxQfl3BB1Xq/VJuPL6f+P16DeO3dgLC9SnV6wrTFlqAeKMxZRvv9SjGaadWrlPOIhp9hE6cKv3ezFwaEs48aRCv7c/mC9yv75AVSCclGuR8XHwBjvPifyWPznv7L3FWMJMA8g0LYEs7J7Tfs/iHN3PFv8NrFsGcRFOJZS3AUYxzDV/f1ktPy/Om6qi6W842CBR wDtNNKv+ W4BDjWAC5dHeJuOBAhzGL4Ps/uK4n3GPD4Y/1KbYh5MJA0ljOV80YdsegZsoe/L8o/No8ySG0FkMPo/N6ybEbmLPNWn6DiMPAbk4wpV/48Mat5XuB45JdYr7R2xXO3tjOEbPQsz/CdyXTps9a/2+DGVVZARy4lg04maqUTavKz9BzZzEQSKq6gctzDKE4OkUUqiwvx+qgT4DMDlmR8IEQq5unA8hjD6CZFxUBb9XexYbflyfmXFplPrySYLy90w87yfT1zG1olG0b8bti6jMBSKq9CFMR2qn4zECDcPA/dH3RQw+fBL8TyRdEd0ZhTO0Gv/q0o/yUBY9MlLwsS5FUeQoSuuNOZ/9j3EJE4+7n3efq0wP3hfDLSExTrwgnZ5LhsOxGA7HfR1QIxqbM61IzXwTYH+rRQbcDCnYfx0JjcHnu5Fe8KfufkgUc39Ei7lTJlRwe 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: From: Jeff Xu This is the first set of Memory mapping (VMA) protection patches using PKU. * * * Background: As discussed previously in the kernel mailing list [1], V8 CFI [2] uses PKU to protect memory, and Stephen Röttger proposes to extend the PKU to memory mapping [3]. We're using PKU for in-process isolation to enforce control-flow integrity 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 “safe/isolated” thread environment. We assign dedicated PKUs to this thread, and use those PKUs to protect the threads’ 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’t 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’t have this flag set, 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 “safe/isolated” thread environments 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/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQMyXgeaRHo/edit?usp=sharing [3]https://docs.google.com/document/d/1qqVoVfRiF2nRylL3yjZyCQvzQaej1HRPh3f5wj1AS9I/edit 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 KEY: 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 | 2 +- include/linux/pkeys.h | 18 +- include/uapi/linux/mman.h | 5 + mm/mmap.c | 34 +- mm/mprotect.c | 31 +- mm/mremap.c | 6 +- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/pkey_enforce_api.c | 1312 +++++++++++++++++ 12 files changed, 1507 insertions(+), 22 deletions(-) create mode 100644 tools/testing/selftests/mm/pkey_enforce_api.c base-commit: ba0ad6ed89fd5dada3b7b65ef2b08e95d449d4ab