From patchwork Thu Jan 11 23:41:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13517854 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 0C38EC47077 for ; Thu, 11 Jan 2024 23:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D86B6B0092; Thu, 11 Jan 2024 18:42:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 789506B0095; Thu, 11 Jan 2024 18:42:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 629926B0096; Thu, 11 Jan 2024 18:42:31 -0500 (EST) 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 5383A6B0092 for ; Thu, 11 Jan 2024 18:42:31 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 21D8340257 for ; Thu, 11 Jan 2024 23:42:31 +0000 (UTC) X-FDA: 81668656902.30.F9C957B Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) by imf15.hostedemail.com (Postfix) with ESMTP id 647EFA002B for ; Thu, 11 Jan 2024 23:42:28 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=WMm2chf0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.51 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705016548; 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=nQ88mFK7sFkh1njhDonNomw1p3DChoUnv33VvGn+i0U=; b=R/+pPC3truioDfu5kZIrY2I4Kd5s7DktYDfjqcKrk9gt2fPwHaOtPhH/hZpXM1OibRSga/ 1zZwFeLyODEHcbAXebI0CQRWd5DPoXtQYkbwujW0oMGX9dg66Z3c20lpbfyjxmhpmCAaRD Gg86M5HA+u2qMOzpWnG4ViJvrrTGsKY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=WMm2chf0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.51 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705016548; a=rsa-sha256; cv=none; b=esosQMPQV0S6PDiRv1LOof0gXiHlRj3o+qapRtBU0TUmsDH6SFv50O7clYzLero2CqwNbi VeCsN13XrMIVhiOkZcq9tzRsUFrcp6uYUvO0noEpamArWdHLZZir9zN3iMHCaCfKWRKOB+ Ua3CGbqwHcd1zWEMzF16fDl9pZ/ty3s= Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6dde290d09eso2228684a34.2 for ; Thu, 11 Jan 2024 15:42:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1705016547; x=1705621347; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nQ88mFK7sFkh1njhDonNomw1p3DChoUnv33VvGn+i0U=; b=WMm2chf0uoPwQk9WRoCAAt10h5erYwl0BHKuIzhZfeLYitIk3kVK5h3LejYIXI5yfK D7uTDLeaS6CELWMiLr86Y2lA8MHSoKjhV7aouqMxeqvHj1wca5RXqZYSULkizKL+tePj IoNYvTw9CUTgAyERXGLJaDgSHwFljiuUZ6M0c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705016547; x=1705621347; 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=nQ88mFK7sFkh1njhDonNomw1p3DChoUnv33VvGn+i0U=; b=rxQdhJTev1h4XmFlJ7JDwZSPpgr4YnuAst9oLVT4lh3IZBQ13wgxf0093hswvyZ4KF 3EmUm7fWHnPxS7nCpPCnUdaMxdz293WNTu6uA5H3+zkZxU8oDOYki5YnnsEiAfOjcI7T 8dfIuwmJfgRvNEhe04Z7QXqutSuRyYE34z2jHAb3mH7uVlUVeUMnqlakSBhM/Ne4bdRQ ZXoLA6zLsOXZGUpyfMpQR88wInBozrjWyvW5bfQcGs27mpBTx3gIvsBtb+6PdDnNed25 McQR65xjzqcktYvDbr6cUsfW+W1t6nfMMlkDqvyIob3QUrvCnzhORLnJed/Ek4RYK+Fm W4Nw== X-Gm-Message-State: AOJu0YwsOrRLhJKE3vrJ85zPc/i1m0IGqpb15pT+DfMDMrS/eP28uY0r gxyMuwEcsndBjgkSrBEGB23L1JBv7R6f X-Google-Smtp-Source: AGHT+IFnuGh6CbgifPotsFGdhRSuRfjLegeHtIDMuAskzoqgzDFGpw7dCKrIqYFRDmH/OzHEpxULTA== X-Received: by 2002:a05:6359:504a:b0:172:d476:3f33 with SMTP id om10-20020a056359504a00b00172d4763f33mr1097417rwb.53.1705016547235; Thu, 11 Jan 2024 15:42:27 -0800 (PST) Received: from localhost (34.85.168.34.bc.googleusercontent.com. [34.168.85.34]) by smtp.gmail.com with UTF8SMTPSA id c23-20020aa78817000000b006d96dc803b3sm1847158pfo.12.2024.01.11.15.42.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 Jan 2024 15:42:26 -0800 (PST) From: jeffxu@chromium.org To: akpm@linux-foundation.org, keescook@chromium.org, jannh@google.com, sroettger@google.com, willy@infradead.org, gregkh@linuxfoundation.org, torvalds@linux-foundation.org, usama.anjum@collabora.com, rdunlap@infradead.org Cc: jeffxu@google.com, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, pedro.falcato@gmail.com, dave.hansen@intel.com, linux-hardening@vger.kernel.org, deraadt@openbsd.org Subject: [PATCH v6 0/4] Introduce mseal() Date: Thu, 11 Jan 2024 23:41:37 +0000 Message-ID: <20240111234142.2944934-1-jeffxu@chromium.org> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog MIME-Version: 1.0 X-Rspamd-Queue-Id: 647EFA002B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: o59km9jdtoxc4bhpboifnziyt91jcqaj X-HE-Tag: 1705016548-808605 X-HE-Meta: U2FsdGVkX1+ZBkmx0Zq1Y3GyUF12fryWFp9QOFgJ+Gm1+7be5YwFMomOjq8EjXue+W+C3PuGnkPl96iskFvjLZhvvb3VkspS9cvgWgZJg3aYn0XUJyA2DSCucD/tZ8DTC51kmesSaxEwO2R/BZAGQOHhHz3VpcdJ5Aw2WO95CPxM3A/AMaIDXN4AC/s76soTFARXx1/l6W9L+HKglN99hMVLwQr+g+6iVcCgb+/6CfG3Qe8ichoiXFrmhXYVThN8cG5DCpkuuzDQ9Se2IIwZ+Fl7tziU8a92mQOiGimNEEizf7a6fNqgalmBL6E8NRs08kSDdRZxrI5r7S/lkdB8hkCcGvpFtZlTteEtvlN2slq05IPb0hmCS+KMo+Szgu6Q1EK7Pds2xWOsyex279l6ZM2/2a/SAW2fSp5KnUfBT0A2l5DpirOZkXn+EmNq0Jm7ghkUo29CYT3zU0NIRTMjoMNbtq+ryFuZaISenkvvFIa6LOreBuIeqBrIAOwfNzr3roGExX1giH8AC1nbtjuNqF5MTg6TuELANyEwsqF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DtdpW7h8+pHieuYwJgOu2zMON16oq+u2Vne0Zb7F8MqkMsLPwHni5j345IGXqjJ86q7rE9AfZfAbE8+s/qIvvgjFTycvBtnwS6aZ2GxulzZ9JJSmNw5IYU0VfkD5nmptJ/WntssCn3QuFUDqy4pgy5+t3hYpZNvBhh7eGhYUIM= 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: List-Subscribe: List-Unsubscribe: From: Jeff Xu This patchset proposes a new mseal() syscall for the Linux kernel. In a nutshell, mseal() protects the VMAs of a given virtual memory range against modifications, such as changes to their permission bits. Modern CPUs support memory permissions, such as the read/write (RW) and no-execute (NX) bits. Linux has supported NX since the release of kernel version 2.6.8 in August 2004 [1]. The memory permission feature improves the security stance on memory corruption bugs, as an attacker cannot simply write to arbitrary memory and point the code to it. The memory must be marked with the X bit, or else an exception will occur. Internally, the kernel maintains the memory permissions in a data structure called VMA (vm_area_struct). mseal() additionally protects the VMA itself against modifications of the selected seal type. Memory sealing is useful to mitigate memory corruption issues where a corrupted pointer is passed to a memory management system. For example, such an attacker primitive can break control-flow integrity guarantees since read-only memory that is supposed to be trusted can become writable or .text pages can get remapped. Memory sealing can automatically be applied by the runtime loader to seal .text and .rodata pages and applications can additionally seal security critical data at runtime. A similar feature already exists in the XNU kernel with the VM_FLAGS_PERMANENT [3] flag and on OpenBSD with the mimmutable syscall [4]. Also, Chrome wants to adopt this feature for their CFI work [2] and this patchset has been designed to be compatible with the Chrome use case. Two system calls are involved in sealing the map: mmap() and mseal(). The new mseal() is an syscall on 64 bit CPU, and with following signature: int mseal(void addr, size_t len, unsigned long flags) addr/len: memory range. flags: reserved. mseal() blocks following operations for the given memory range. 1> Unmapping, moving to another location, and shrinking the size, via munmap() and mremap(), can leave an empty space, therefore can be replaced with a VMA with a new set of attributes. 2> Moving or expanding a different VMA into the current location, via mremap(). 3> Modifying a VMA via mmap(MAP_FIXED). 4> Size expansion, via mremap(), does not appear to pose any specific risks to sealed VMAs. It is included anyway because the use case is unclear. In any case, users can rely on merging to expand a sealed VMA. 5> mprotect() and pkey_mprotect(). 6> Some destructive madvice() behaviors (e.g. MADV_DONTNEED) for anonymous memory, when users don't have write permission to the memory. Those behaviors can alter region contents by discarding pages, effectively a memset(0) for anonymous memory. In addition: mmap() has two related changes. The PROT_SEAL bit in prot field of mmap(). When present, it marks the map sealed since creation. The MAP_SEALABLE bit in the flags field of mmap(). When present, it marks the map as sealable. A map created without MAP_SEALABLE will not support sealing, i.e. mseal() will fail. Applications that don't care about sealing will expect their behavior unchanged. For those that need sealing support, opt-in by adding MAP_SEALABLE in mmap(). The idea that inspired this patch comes from Stephen Röttger’s work in V8 CFI [5]. Chrome browser in ChromeOS will be the first user of this API. Indeed, the Chrome browser has very specific requirements for sealing, which are distinct from those of most applications. For example, in the case of libc, sealing is only applied to read-only (RO) or read-execute (RX) memory segments (such as .text and .RELRO) to prevent them from becoming writable, the lifetime of those mappings are tied to the lifetime of the process. Chrome wants to seal two large address space reservations that are managed by different allocators. The memory is mapped RW- and RWX respectively but write access to it is restricted using pkeys (or in the future ARM permission overlay extensions). The lifetime of those mappings are not tied to the lifetime of the process, therefore, while the memory is sealed, the allocators still need to free or discard the unused memory. For example, with madvise(DONTNEED). However, always allowing madvise(DONTNEED) on this range poses a security risk. For example if a jump instruction crosses a page boundary and the second page gets discarded, it will overwrite the target bytes with zeros and change the control flow. Checking write-permission before the discard operation allows us to control when the operation is valid. In this case, the madvise will only succeed if the executing thread has PKEY write permissions and PKRU changes are protected in software by control-flow integrity. Although the initial version of this patch series is targeting the Chrome browser as its first user, it became evident during upstream discussions that we would also want to ensure that the patch set eventually is a complete solution for memory sealing and compatible with other use cases. The specific scenario currently in mind is glibc's use case of loading and sealing ELF executables. To this end, Stephen is working on a change to glibc to add sealing support to the dynamic linker, which will seal all non-writable segments at startup. Once this work is completed, all applications will be able to automatically benefit from these new protections. Change history: =============== V6: - Drop RFC from subject, Given Linus's general approval. - Adjust syscall number for mseal (main Jan.11/2024) - Code style fix (Matthew Wilcox) - selftest: use ksft macros (Muhammad Usama Anjum) - Document fix. (Randy Dunlap) V5: - fix build issue in mseal-Wire-up-mseal-syscall (Suggested by Linus Torvalds, and Greg KH) - updates on selftest. https://lore.kernel.org/lkml/20240109154547.1839886-1-jeffxu@chromium.org/#r V4: (Suggested by Linus Torvalds) - new signature: mseal(start,len,flags) - 32 bit is not supported. vm_seal is removed, use vm_flags instead. - single bit in vm_flags for sealed state. - CONFIG_MSEAL kernel config is removed. - single bit of PROT_SEAL in the "Prot" field of mmap(). Other changes: - update selftest (Suggested by Muhammad Usama Anjum) - update documentation. https://lore.kernel.org/all/20240104185138.169307-1-jeffxu@chromium.org/ V3: - Abandon per-syscall approach, (Suggested by Linus Torvalds). - Organize sealing types around their functionality, such as MM_SEAL_BASE, MM_SEAL_PROT_PKEY. - Extend the scope of sealing from calls originated in userspace to both kernel and userspace. (Suggested by Linus Torvalds) - Add seal type support in mmap(). (Suggested by Pedro Falcato) - Add a new sealing type: MM_SEAL_DISCARD_RO_ANON to prevent destructive operations of madvise. (Suggested by Jann Horn and Stephen Röttger) - Make sealed VMAs mergeable. (Suggested by Jann Horn) - Add MAP_SEALABLE to mmap() - Add documentation - mseal.rst https://lore.kernel.org/linux-mm/20231212231706.2680890-2-jeffxu@chromium.org/ v2: Use _BITUL to define MM_SEAL_XX type. Use unsigned long for seal type in sys_mseal() and other functions. Remove internal VM_SEAL_XX type and convert_user_seal_type(). Remove MM_ACTION_XX type. Remove caller_origin(ON_BEHALF_OF_XX) and replace with sealing bitmask. Add more comments in code. Add a detailed commit message. https://lore.kernel.org/lkml/20231017090815.1067790-1-jeffxu@chromium.org/ v1: https://lore.kernel.org/lkml/20231016143828.647848-1-jeffxu@chromium.org/ ---------------------------------------------------------------- [1] https://kernelnewbies.org/Linux_2_6_8 [2] https://v8.dev/blog/control-flow-integrity [3] https://github.com/apple-oss-distributions/xnu/blob/1031c584a5e37aff177559b9f69dbd3c8c3fd30a/osfmk/mach/vm_statistics.h#L274 [4] https://man.openbsd.org/mimmutable.2 [5] https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQMyXgeaRHo/edit#heading=h.bvaojj9fu6hc [6] https://lore.kernel.org/lkml/CAG48ez3ShUYey+ZAFsU2i1RpQn0a5eOs2hzQ426FkcgnfUGLvA@mail.gmail.com/ [7] https://lore.kernel.org/lkml/20230515130553.2311248-1-jeffxu@chromium.org/ Jeff Xu (4): mseal: Wire up mseal syscall mseal: add mseal syscall selftest mm/mseal memory sealing mseal:add documentation Documentation/userspace-api/mseal.rst | 181 ++ arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + include/linux/mm.h | 60 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/mman-common.h | 8 + include/uapi/asm-generic/unistd.h | 5 +- kernel/sys_ni.c | 1 + mm/Makefile | 4 + mm/madvise.c | 12 + mm/mmap.c | 27 + mm/mprotect.c | 10 + mm/mremap.c | 31 + mm/mseal.c | 330 +++ tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/mseal_test.c | 1997 +++++++++++++++++++ 32 files changed, 2686 insertions(+), 2 deletions(-) create mode 100644 Documentation/userspace-api/mseal.rst create mode 100644 mm/mseal.c create mode 100644 tools/testing/selftests/mm/mseal_test.c