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 E745DC4707B for ; Thu, 11 Jan 2024 05:20:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29A866B0087; Thu, 11 Jan 2024 00:20:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 222CD6B008A; Thu, 11 Jan 2024 00:20:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C69D6B008C; Thu, 11 Jan 2024 00:20:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id ED28E6B0087 for ; Thu, 11 Jan 2024 00:20:01 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C37DB1C0E2B for ; Thu, 11 Jan 2024 05:20:01 +0000 (UTC) X-FDA: 81665878602.20.0A012F9 Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) by imf18.hostedemail.com (Postfix) with ESMTP id 0F3211C0003 for ; Thu, 11 Jan 2024 05:19:59 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uKzroXxy; spf=pass (imf18.hostedemail.com: domain of jeffxu@google.com designates 209.85.160.181 as permitted sender) smtp.mailfrom=jeffxu@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704950400; 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=UgeziysLb2J3LhlwEkz6N5kyQ2UlLlN8+orUb8FLk+4=; b=nBU5MIQMX+jCJEwQ/H3eBts99uRFCmQhgmrmgCgwb5DDjnZXb+DBOqtPHVIDQItbqqIf3L mPDHXwaHQzuw3erBcDCmJWbeloeKR7uiiz1pGSw/qVYZoKezu+X//yHCicqI1ukP9D8BqJ halQBYpVMO6hQQM8C67Cbzsv40IcHDk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704950400; a=rsa-sha256; cv=none; b=bMpQKV9fC9Jr6qjWW1y4Q8JK9hKwlxJ3JgQD/Tccol1QMI1Up0GrdFp61oox4qt19Q1Eqs usg6NzOz9ZoqHaEN2tCBxa0yRhbXFT9AmHUjcv040cJgJSUt9P7rwsIwF7c4wh4NhC1Zuu on6Izchn45ox332AtefdAVwUWU2Ua8E= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uKzroXxy; spf=pass (imf18.hostedemail.com: domain of jeffxu@google.com designates 209.85.160.181 as permitted sender) smtp.mailfrom=jeffxu@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f181.google.com with SMTP id d75a77b69052e-428405a0205so117601cf.1 for ; Wed, 10 Jan 2024 21:19:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704950399; x=1705555199; darn=kvack.org; 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=UgeziysLb2J3LhlwEkz6N5kyQ2UlLlN8+orUb8FLk+4=; b=uKzroXxycGTKx9EliQN7BhiW+EEXLGsO3jev4dJK5nXPtpB/IakwoWxNth7wfSxDb2 bcmC/MYcnR1quBsDf6nOvsk4a5EWh+jA1cg0CBSDuWSJ3WH76mF0gUcNPHJtVMy2wN8Y hTf79lv8WE1PWS4CsqIilzCNCEQaFRZ90ZVE43iYlEwhfUW0GgWQPlNUFg47bgAHYjCy 4zewyAkYc/V/y/LdA/kPRBwBSTFmoaPtzAFlw6ssdq/ka0x5JW2EdwJ1GfTKonCZde+I L7GbyvHriVWw08L2MLdSm3DxNNYmnSBqwi7FCMLvExPlWQ6J1ijoCUyKMV9eZZvFBqRk GElQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704950399; x=1705555199; 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=UgeziysLb2J3LhlwEkz6N5kyQ2UlLlN8+orUb8FLk+4=; b=tzd0DQOrhulHz9tAx4osyGKhrX26/ZEue01ad7nTkF3TGk+mRvRoBntCfuEstB1DP/ PMgmr39cRwk4+QHMJziY0fx2YEAFV687WpGm6Ce6CCCuuTyS7fCROgVyybQ/voWlFami ECBKgOskEOIp5Bc7exXuEj6PjuMbFQy58TpG61FYhlvNNXcgd6vZbcN9oLHfgGCDImJG tQsxhXbv8lMfSfbgcXtR7XSXvN9M0hprxwaxr+Sz/dWk/B6J7pIxUZJE68PYTeI8spz6 mlTJ9GTpJZurxd5A8JNBP84O9fApXQ8IrE5w9fBKNBsmiYGGMWenUnSxyhrEnAGJqxXb S5EQ== X-Gm-Message-State: AOJu0YwA1uJ3K3Odfn4RU/yQrCQf7z/h9iLQ6l9kI72OzrFykoIcuIY/ Cvp3nc54Dnoy6UpfrUdSV/C1MFKdty3fWa4VuBM0YnWm9e/bmtra+oJiPCXJ9Q== X-Google-Smtp-Source: AGHT+IEe8s9dpzI9ncLxh0Z+0oPzE8+94OJNpX+tnA36qrhzi0o9LTutmeb9aH99f60803+gXUvtn+zpbllNONpyBbM= X-Received: by 2002:a05:622a:609:b0:429:8fa1:e4c7 with SMTP id z9-20020a05622a060900b004298fa1e4c7mr86121qta.0.1704950398872; Wed, 10 Jan 2024 21:19:58 -0800 (PST) MIME-Version: 1.0 References: <20240109154547.1839886-1-jeffxu@chromium.org> <20240109154547.1839886-5-jeffxu@chromium.org> In-Reply-To: From: Jeff Xu Date: Wed, 10 Jan 2024 21:19:21 -0800 Message-ID: Subject: Re: [RFC PATCH v5 4/4] mseal:add documentation To: Randy Dunlap Cc: jeffxu@chromium.org, 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, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 0F3211C0003 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 3jokjwutnjujum5o7ss3ruskqhnxrmuz X-HE-Tag: 1704950399-190485 X-HE-Meta: U2FsdGVkX18OhPMiZMcaB+1Bo3LoNI1ZyYxfmLWiAHoAkRhyGSYDSOXK6eWBIPQYfxst2gNdw9+9F+Y8PK3bCFvshPSSZPKaa4vXvsbT5HubLMh1oD0gAPNmsP9eICkBUrke3Dd1IBzuQbgp5B4cIwSHeVORs1XBzffufNqQvocO5fW/yrOqBShwJJdja5/b4RzSezAIsCzZEUDZmopMFgB3eiBIE7Ahvifeta/DefdHcWsYSU2uJH3kW4A6bj0/ykg+k0OfXz1NfKXXWqyllOQB29Y2ArB2fRrqUtR/Zml23ecq3zpptqTCC4YbfUpdSSRptT4N0ymSPFiPeZNGBX9VlIM4OWKoQZS8Sr6nYwH/UwvaUKYW6jTzksrdqgrRDjtYW4RPuXBHjeh0LFeXM5C+hYuaC3C9OkFdWer0HCodAUTtDXcvqcevqcPnO3plJITFEcpyNtXCgIBeTbFXBUF56x1Y9n1E1BmLOH94xgbjuxEcT6xy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JzifSYS3xy1gVGLxwg5nCfMHY7inVrVgksdq+8Ak= 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: On Wed, Jan 10, 2024 at 7:16=E2=80=AFPM Randy Dunlap wrote: > > > > On 1/9/24 07:45, jeffxu@chromium.org wrote: > > 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/user= space-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 > > + > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > +Introduction of mseal > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > + > > +:Author: Jeff Xu > > + > > +Modern CPUs support memory permissions such as RW and NX bits. The mem= ory > > +permission feature improves security stance on memory corruption bugs,= i.e. > > +the attacker can=E2=80=99t just write to arbitrary memory and point th= e code to it, > > +the memory has to be marked with X bit, or else an exception will happ= en. > > + > > +Memory sealing additionally protects the mapping itself against > > +modifications. This is useful to mitigate memory corruption issues whe= re 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 writa= ble > > +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 > > +=3D=3D=3D=3D=3D=3D=3D=3D > > +Two system calls are involved in virtual memory sealing, mseal() and m= map(). > > + > > +mseal() > > +----------- > > +The mseal() syscall has following signature: > > has the 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 supporte= d. > > 64-bit > > > + > > +- 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 giv= en > > + memory range could happen. However, those cases shall be rare. > > s/shall/should/ > unless you are predicting the future. > > > + > > +**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 th= e > > + memory. Those behaviors can alter region contents by discarding pa= ges, > > + 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, > > sealing. In > > > +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 > > 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: > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D > > +- glibc: > > + The dynamic linker, during loading ELF executables, can apply sealin= g to > > + non-writable memory segments. > > + > > +- Chrome browser: protect some security sensitive data-structures. > > + > > +Additional notes: > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > +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 cover= ed > > +by mseal(). If applications want to block such cases, sandbox tools (s= uch 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=C3=B6ttger=E2= =80=99s work in V8 > > +CFI [4]. Chrome browser in ChromeOS will be the first user of this API= . > > + > > +Reference: > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > +[1] https://github.com/apple-oss-distributions/xnu/blob/1031c584a5e37a= ff177559b9f69dbd3c8c3fd30a/osfmk/mach/vm_statistics.h#L274 > > + > > +[2] https://man.openbsd.org/mimmutable.2 > > + > > +[3] https://lore.kernel.org/lkml/CAG48ez3ShUYey+ZAFsU2i1RpQn0a5eOs2hzQ= 426FkcgnfUGLvA@mail.gmail.com > > + > > +[4] https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmw= dvxQMyXgeaRHo/edit#heading=3Dh.bvaojj9fu6hc > Thanks. Will update in the next version. -Jeff > -- > #Randy