From mboxrd@z Thu Jan 1 00:00:00 1970 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 97582C47074 for ; Thu, 4 Jan 2024 18:52:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 824C66B0136; Thu, 4 Jan 2024 13:51:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AE096B0137; Thu, 4 Jan 2024 13:51:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D9A76B0138; Thu, 4 Jan 2024 13:51:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4ADDC6B0136 for ; Thu, 4 Jan 2024 13:51:55 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0A70AA1BA3 for ; Thu, 4 Jan 2024 18:51:55 +0000 (UTC) X-FDA: 81642522990.15.27FFB47 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf13.hostedemail.com (Postfix) with ESMTP id 0850B20022 for ; Thu, 4 Jan 2024 18:51:52 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=R+q0TzKo; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf13.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.174 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=1704394313; 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=iD233mk7AkP+v7m2Kk19vTG+Z84jTUOLcB7cVPRt8O9WWwxciZI2+IzhxDg7nXwbiuzN6x KPRdUrn8GY6Ki4xkLoxXg27K4bygQnldscxVOQx+bHbA7A0uwV4ZtaU64edvoPTVMf/Fef bTxtW4QDsS4ADz0GyCLnQmeLyoIwuEk= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=R+q0TzKo; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf13.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.214.174 as permitted sender) smtp.mailfrom=jeffxu@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704394313; a=rsa-sha256; cv=none; b=a6qzZjjR3pAyZ5jkCizzr7jfsdf3zGPVc+4kZS3Pk8FUPa4JIT+8b0dZwSexSj/YSLJEh2 7AKevRxAOiZZLslZObwScyw+bKbuAqHdAk2zcnsiR4+4iVcspncaRlrPf+XYwN0yofLPq7 q3YbkMfu97WbfvrioNpdiub4dQZ9krg= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d3aa0321b5so5932775ad.2 for ; Thu, 04 Jan 2024 10:51:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1704394312; x=1704999112; 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=R+q0TzKorAY6EHjsBqOwx8Wg+33QSlOZuzZi3FznGjj1wtiF1GE/5X7SSV0glOfFHv Fb7H11CLUBAQ3Ir8a+563lJ66EdHB70Cz2mYg7hvEJMQf6qqQFPiv6/hsKH7jcDip3bd WmC3qG0m6hZSHLBlZcDs1JWbzLSMhCNEGuarE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704394312; x=1704999112; 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=k/GSCgHQtV3vPIugaIPTUPYPNn+5ddNSY8Q94TKBGoATpnqbIlj7oTqVehQNI5Vtv1 lCUwo5NCnw+/Sq9ej8Nmqbhttn+5iA36ZinaG48Xs2RrnhEjCJDRfd2aewNCaGNwCoUV FvXVKJq7FM9NTBKpt1aNPwgdm+B2+k23TptRBRDd+uxe21Bfw7OamNn/j+lNM4H8biwL O9rhHUzr8Ah1L1+k4y3+7C4297rvoFWl9MyPRwSXp5tLpgMeMdyQJy5cOnvQ3zO1YnWt 3r9TKj+UgpZL3MYaKw0MR3ExNnd2a/RRtVk70V7dVXBVPoy0PccYyy0RqPsEgZ/XyCAo JL6w== X-Gm-Message-State: AOJu0Yzcgl2Tjcht28X1/Laztvh2GvDMzCcBbdQ6L+5n/Gah7cRelrJZ uB0XBsBgFOp/1Zy2XKo/KuV0hrmne2xi X-Google-Smtp-Source: AGHT+IGiMfjpvphjF5Sdh5G8uUcbaXUgkSlaqH4Fciap9D7iafeQeLa6oLHP/SX6kjaHPtcOmgVn1g== X-Received: by 2002:a17:902:7c12:b0:1d4:3012:f4ca with SMTP id x18-20020a1709027c1200b001d43012f4camr942477pll.56.1704394311796; Thu, 04 Jan 2024 10:51:51 -0800 (PST) Received: from localhost (34.85.168.34.bc.googleusercontent.com. [34.168.85.34]) by smtp.gmail.com with UTF8SMTPSA id p16-20020a170903249000b001cffd42711csm8234968plw.199.2024.01.04.10.51.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Jan 2024 10:51:50 -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 v4 4/4] mseal:add documentation Date: Thu, 4 Jan 2024 18:51:37 +0000 Message-ID: <20240104185138.169307-5-jeffxu@chromium.org> X-Mailer: git-send-email 2.43.0.195.gebba966016-goog In-Reply-To: <20240104185138.169307-1-jeffxu@chromium.org> References: <20240104185138.169307-1-jeffxu@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 0850B20022 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: qf4dacpk6cy3eqt8aaszm6r5tbkqp4y5 X-HE-Tag: 1704394312-849786 X-HE-Meta: U2FsdGVkX1/+WXsrc3I56qmOG4iYuxIJR9R300/YGFJH3G65o35p9pIG2Ocm9uctHv7MP8DZPyl1GYknmF81VUFN192P9FQh+G5sfTUpCMDILgf9S24z3ucJd32p/4gvqVEhoQ8frGENPEBaMXc8BoxHtgrO6NgKv+a8BcKOPGYF0Ra7WjHzkiiqjYHj3Jwa1A5bRotVeONFofFfwg/2kgtVd2/Oc3qsoAd6LE/ujvrqV5tG9E0S6faVztveJqBDoI93XzsTdKEG+s+ZnN5+I7gDiOzJHKfT21lQ8mQPLgzyDlNkP6K8YwmKNBI4Rz8hdD8YmNHUmyOZiTRMUOr88qelVqBJvPYTDca5uqpZ/snA6Vgp3XsLKT/do3dzmCpiXenGWY+0GrK2szQhRSMEDyV1OLb0tF86EhfO3P8jJD18vMVwWNicMlJkbhCjq+pSwMKvrW6Pp32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zUuGF9AYQcU9YoB042qwA+8m8A4W+WCRTQ3JZYOTCJPh4L8KwiDhEePHKN3xZKqa/x4/G4E0Jb3tB83YHgJWe7uPGsfyhimCeE 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 -- 2.43.0.195.gebba966016-goog