From patchwork Tue Jan 9 15:45:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13515034 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 2CA5DC4707C for ; Tue, 9 Jan 2024 15:46:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A7FEA6B0083; Tue, 9 Jan 2024 10:46:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 930DC6B008C; Tue, 9 Jan 2024 10:46:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 776396B0083; Tue, 9 Jan 2024 10:46:08 -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 56F1B6B0083 for ; Tue, 9 Jan 2024 10:46:08 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1A95B1A03E3 for ; Tue, 9 Jan 2024 15:46:08 +0000 (UTC) X-FDA: 81660198816.13.1526E25 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf12.hostedemail.com (Postfix) with ESMTP id 399774000D for ; Tue, 9 Jan 2024 15:46:06 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=oKXk5Uy0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf12.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.182 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=1704815166; 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:in-reply-to:references:references:dkim-signature; bh=Jycb6hYHwyiIhFY8GEZ+4HXYhrCSpdkd/0XeHo/twrU=; b=8YeM1At+zmE8GnMCfHE33yj6LJGrLEkKQUhFy0lq0nufoIYpl+WLdDJsxBHjJvEH4uGhCx gihNKnBQnt19Pcu0z8E+pQPlgHYpc599pNw2hiB4bHJgf20+QUjNrNL0abmWymXGSpQci5 cGFykNjqg11pNs4gaE+7N7RWSoNZ+8E= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=oKXk5Uy0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf12.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704815166; a=rsa-sha256; cv=none; b=A8/PnCO61LB/7eQhkQ8aBlPEMGoNlZEOy3t51YTnIiQR7N1kXBj1LI/ubQm5CD5CQmgyLF EOUddizzlNJOnCn82K+bb8vl2gwbyjlZ4kBUPBqLWEKQlhOcKZtyuUwuwsS5ZlbwM5V187 mPk3Fu4t2M76w4NlM7BFV/kcK7/vSWI= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d44200b976so15163455ad.2 for ; Tue, 09 Jan 2024 07:46:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1704815165; x=1705419965; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Jycb6hYHwyiIhFY8GEZ+4HXYhrCSpdkd/0XeHo/twrU=; b=oKXk5Uy01eC9XnXkfdYZLmOM/QxeTNe5f2V748aAjbPJigl3um8cGJa/pY8/6l272b rqFuyjZCqO+tai7fOpy0RLgNWRbpyNLSnKBDabMcoems4rx0ALGysvlZpO7SYIoJ2hs0 /G5WDktclvK7QuND4ZCE9MMf3IZGlH7HKPoZI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704815165; x=1705419965; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jycb6hYHwyiIhFY8GEZ+4HXYhrCSpdkd/0XeHo/twrU=; b=F1qxGEJbM6j2lpzfI4rZ5sgnxi1kZI87E0tIKvVVokzXXESRsgEgzklnzNC/eR9xX0 hFesq39PiKDqKzbp3V3MvJg7mxZvwXeOrrV3Zdy/XyvrACW9vFKhM55vRz1IycRtIPnV Nh3htIeeVW0jhJNlnfd09VNPoGErCwSGG9fGEPAsFkqU5W64ItTZq7phHVN5fsZqMzmx 3E0kJb1kdBgC1Al1qkwTkbXCrDnykdqzkbELW+i3Dr1BVv34cRYwTa/JhoAp0pmEyxtw +SUfxKYl1m7y3enZknFMB54rY51SqQIFnfHMAzS88pscIOsglL+0avElUrNPukY5g8BW UQMA== X-Gm-Message-State: AOJu0YwQsuy5ebNAl+6zNc13aHjbcjlC7LZRv3hMxwfpe+s13mw61FjH 4myvKjqyYQ7ouT7Fu3bPep/2onDneu4t X-Google-Smtp-Source: AGHT+IF2Hp+30LVgBqzpG5ABJ24nVs2S8m6pU7B+HXzD5Q7hgY+HfkPtpFPsAMpNMu3VF8po1GaWxQ== X-Received: by 2002:a17:903:98d:b0:1d5:36ea:f63e with SMTP id mb13-20020a170903098d00b001d536eaf63emr3163984plb.22.1704815164990; Tue, 09 Jan 2024 07:46:04 -0800 (PST) Received: from localhost (34.85.168.34.bc.googleusercontent.com. [34.168.85.34]) by smtp.gmail.com with UTF8SMTPSA id z12-20020a1709028f8c00b001cc2ebd2c2csm1932782plo.256.2024.01.09.07.46.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Jan 2024 07:46:04 -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 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, Jeff Xu Subject: [RFC PATCH v5 4/4] mseal:add documentation Date: Tue, 9 Jan 2024 15:45:42 +0000 Message-ID: <20240109154547.1839886-5-jeffxu@chromium.org> X-Mailer: git-send-email 2.43.0.195.gebba966016-goog In-Reply-To: <20240109154547.1839886-1-jeffxu@chromium.org> References: <20240109154547.1839886-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 399774000D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: yra3pdeiqsq5hm4p16b4a7edna189ooc X-HE-Tag: 1704815166-14682 X-HE-Meta: U2FsdGVkX18y/s4Rn7xpjejdJl5wb0f15gjwvVPUNwbkmeLOZIa+VFi5moOATQwKZm70KFLk3AjnkI/EHRc91kd0a9LHNJb+beqlrELYSsZaZFk6Id1pH7M7j6/WvMCBPlxQkPMQxdaDy45ja5K3nf2PfjmjIkqvbWcR/JX6fgnJTLkmX21l2HFF14eFD+8s84vk3AdvgpyqF1O4oYjBDx7nQ+0VkJ8kwJWc1OKw9ULPbJCzhdcJfOJD5Vx4RmAjtfRHcoHQzqyPwrZDUXWRX6hSE0na5fQgPznwC9KSx9UnrnEDtE56VBar1pOFRmLRITaJ0t8TdtoX9OJ1YN9ACzJYudRdZLtrr9W0wZpZn5u14JLq+JHDMXLz7YxIxzfKeMyyu/8i0M5kwHTaBxSFrLu840Kihq86fyxnNsFOUOkHZ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wRzszAXya/egAxvl+BloDoyAK5ka7haCLVANJYrKaWvT8uEy191xVAscxs0gg70khrjITqSOUCgQJKX+e2T6IG9Rxgbp6utNSvkFUb0iy5w+P1DPTwiwcWoGKgclbeETgbvo 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 Add documentation for mseal(). Signed-off-by: Jeff Xu --- Documentation/userspace-api/mseal.rst | 181 ++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 Documentation/userspace-api/mseal.rst diff --git a/Documentation/userspace-api/mseal.rst b/Documentation/userspace-api/mseal.rst new file mode 100644 index 000000000000..1700ce5af218 --- /dev/null +++ b/Documentation/userspace-api/mseal.rst @@ -0,0 +1,181 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===================== +Introduction of mseal +===================== + +:Author: Jeff Xu + +Modern CPUs support memory permissions such as RW and NX bits. The memory +permission feature improves security stance on memory corruption bugs, i.e. +the attacker can’t just write to arbitrary memory and point the code to it, +the memory has to be marked with X bit, or else an exception will happen. + +Memory sealing additionally protects the mapping itself against +modifications. This 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 flag [1] and on OpenBSD with the mimmutable syscall [2]. + +User API +======== +Two system calls are involved in virtual memory sealing, mseal() and mmap(). + +mseal() +----------- +The mseal() syscall has following signature: + +``int mseal(void addr, size_t len, unsigned long flags)`` + +**addr/len**: virtual memory address range. + +The address range set by ``addr``/``len`` must meet: + - The start address must be in an allocated VMA. + - The start address must be page aligned. + - The end address (``addr`` + ``len``) must be in an allocated VMA. + - no gap (unallocated memory) between start and end address. + +The ``len`` will be paged aligned implicitly by the kernel. + +**flags**: reserved for future use. + +**return values**: + +- ``0``: Success. + +- ``-EINVAL``: + - Invalid input ``flags``. + - The start address (``addr``) is not page aligned. + - Address range (``addr`` + ``len``) overflow. + +- ``-ENOMEM``: + - The start address (``addr``) is not allocated. + - The end address (``addr`` + ``len``) is not allocated. + - A gap (unallocated memory) between start and end address. + +- ``-EACCES``: + - ``MAP_SEALABLE`` is not set during mmap(). + +- ``-EPERM``: + - sealing is supported only on 64 bit CPUs, 32-bit is not supported. + +- For above error cases, users can expect the given memory range is + unmodified, i.e. no partial update. + +- There might be other internal errors/cases not listed here, e.g. + error during merging/splitting VMAs, or the process reaching the max + number of supported VMAs. In those cases, partial updates to the given + memory range could happen. However, those cases shall be rare. + +**Blocked operations after sealing**: + 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. + + Moving or expanding a different VMA into the current location, + via mremap(). + + Modifying a VMA via mmap(MAP_FIXED). + + 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. + + mprotect() and pkey_mprotect(). + + 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. + +**Note**: + +- mseal() only works on 64-bit CPUs, not 32-bit CPU. + +- users can call mseal() multiple times, mseal() on an already sealed memory + is a no-action (not error). + +- munseal() is not supported. + +mmap() +---------- +``void *mmap(void* addr, size_t length, int prot, int flags, int fd, +off_t offset);`` + +We add two changes in ``prot`` and ``flags`` of mmap() related to +memory sealing. + +**prot** + +The ``PROT_SEAL`` bit in ``prot`` field of mmap(). + +When present, it marks the memory is sealed since creation. + +This is useful as optimization because it avoids having to make two +system calls: one for mmap() and one for mseal(). + +It's worth noting that even though the sealing is set via the +``prot`` field in mmap(), it can't be set in the ``prot`` +field in later mprotect(). This is unlike the ``PROT_READ``, +``PROT_WRITE``, ``PROT_EXEC`` bits, e.g. if ``PROT_WRITE`` is not set in +mprotect(), it means that the region is not writable. + +Setting ``PROT_SEAL`` implies setting ``MAP_SEALABLE`` below. + +**flags** + +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; In other words, +mseal() will fail for such a map. + + +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(). + +Note: for a map created without ``MAP_SEALABLE`` or a map created +with ``MAP_SEALABLE`` but not sealed yet, mmap(MAP_FIXED) can +change the sealable or sealing bit. + +Use Case: +========= +- glibc: + The dynamic linker, during loading ELF executables, can apply sealing to + non-writable memory segments. + +- Chrome browser: protect some security sensitive data-structures. + +Additional notes: +================= +As Jann Horn pointed out in [3], there are still a few ways to write +to RO memory, which is, in a way, by design. Those cases are not covered +by mseal(). If applications want to block such cases, sandbox tools (such as +seccomp, LSM, etc) might be considered. + +Those cases are: + +- Write to read-only memory through /proc/self/mem interface. +- Write to read-only memory through ptrace (such as PTRACE_POKETEXT). +- userfaultfd. + +The idea that inspired this patch comes from Stephen Röttger’s work in V8 +CFI [4]. Chrome browser in ChromeOS will be the first user of this API. + +Reference: +========== +[1] https://github.com/apple-oss-distributions/xnu/blob/1031c584a5e37aff177559b9f69dbd3c8c3fd30a/osfmk/mach/vm_statistics.h#L274 + +[2] https://man.openbsd.org/mimmutable.2 + +[3] https://lore.kernel.org/lkml/CAG48ez3ShUYey+ZAFsU2i1RpQn0a5eOs2hzQ426FkcgnfUGLvA@mail.gmail.com + +[4] https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQMyXgeaRHo/edit#heading=h.bvaojj9fu6hc