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 C02BFCDB474 for ; Tue, 17 Oct 2023 23:01:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E33C8D0117; Tue, 17 Oct 2023 19:01:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 492CA8D0002; Tue, 17 Oct 2023 19:01:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 333128D0117; Tue, 17 Oct 2023 19:01:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 20E468D0002 for ; Tue, 17 Oct 2023 19:01:55 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E0EA5A0F07 for ; Tue, 17 Oct 2023 23:01:54 +0000 (UTC) X-FDA: 81356477748.21.D0BB0B9 Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) by imf28.hostedemail.com (Postfix) with ESMTP id 24583C0019 for ; Tue, 17 Oct 2023 23:01:51 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vf7Yfqtv; spf=pass (imf28.hostedemail.com: domain of jeffxu@google.com designates 209.85.160.171 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=1697583712; 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=ImnM3gTgvQUDZbbKNrv2s4PovNIFWxZGTx+tFBxEKCA=; b=mAZYIEqMGPIgLV98Zjuj0E256/2dB0KIAXd/U6IA61/X+xpmOmwE9Vo72PpWwIkb/QOzI6 h7tSzLZzXSOsX+hSeReodiO6VvrWaQXhYclTbaY49k6Qz48PVUB2WbLjwA1DZFjmPwcA76 3hpz73sdU+VlMggJc/fJF0bDchIW8kQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697583712; a=rsa-sha256; cv=none; b=p4jFIvfrKB4sgEEhP1U/2BsUIFOl5+s5U7XrWP6FuIV4OkA3HSFfxFWDNOJXPMfnwIbIdF EhJ3mz1YSc8jlj4JdtrmeQkYEN1NsM0rDUkx57rs9Fax4PXftyrF/wQHECIxj2WWcjBulH mcWYUFHAE/LAjEHe20Y47B6LwrXjgOQ= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vf7Yfqtv; spf=pass (imf28.hostedemail.com: domain of jeffxu@google.com designates 209.85.160.171 as permitted sender) smtp.mailfrom=jeffxu@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f171.google.com with SMTP id d75a77b69052e-41b813f0a29so65001cf.0 for ; Tue, 17 Oct 2023 16:01:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697583711; x=1698188511; 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=ImnM3gTgvQUDZbbKNrv2s4PovNIFWxZGTx+tFBxEKCA=; b=vf7YfqtvGyXM5kbYIQjTj8yWGtuvknprW6uw0zrUwHU6QeV09fjF9mkzwFlUHN1+SW YID6k4C5MpuDWJ97jc94AhFiT2kmF1LA/Z+HHsfEVdnA2qp+rPXALE2QBj412idpINqR KKLDaet/1hxEDIqCDu+VCyUTKDw9KIC146sYt7EHxeVEuTpUtY/MMHj5BJML9Gwplt9G S5ux2KV9T+sPkwDO7/N4C4l4M/HihFZbux3tuZYPUCtz/V9lLJp9eFskNLa/9YHUey94 BxReIcb+oJ3kEhA+inl54GsGjqtjjaEbM9t7T61JgCQGKtmBzpEtEQqfs1ZxDXZHTmH0 aw4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697583711; x=1698188511; 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=ImnM3gTgvQUDZbbKNrv2s4PovNIFWxZGTx+tFBxEKCA=; b=JfxI+86DAG0PPd51jIg18ixaGDaifND7IBLQzlBA+JDamWNTWb6QYfWPvDAVopi2PS qelT13gQkcPIdFwNyZm78UWSAgVHBv3QktTbOTjunSHBNOt5xtdMKnlVdli2kLiEOiyd 6trfe3ZrBFPCwigmqwceD6HGTukE2QiRgAKt/WGMZi1Zsv6FQdhAcHoQ4tvXImU931kF w7MXqm9PsmCO3u5o5sS82pCY3/3F8hRU8Au+bR+u7Gmm9pgkxeGT8S8CnsqK77UcMJoG jR7KXcic/qY8hJeemc/rab5P87pI3TQsxzzbsR2YCy08g0giBRwkbHTgfrTQ/Hs7cJ5x OuAg== X-Gm-Message-State: AOJu0YzC6nzrX4UT5rmy0PVNIqdmg+OcT7HZ35MQ6JPkqqEEuxY5yTPe ZE9kPVMuIdFCR/zVy0LRYibXFeSRI1inFmYwaOT5Dw== X-Google-Smtp-Source: AGHT+IFRiCphE8SGRZza9wFnaYv5dtjuDGiQxMHSXLNnXD68r7Tsnp6XlmaQHLEDn/NPKGLCG14R2gjFx0iFXzfE8zw= X-Received: by 2002:a05:622a:a047:b0:41b:ef8f:dcbc with SMTP id ju7-20020a05622aa04700b0041bef8fdcbcmr149648qtb.0.1697583710994; Tue, 17 Oct 2023 16:01:50 -0700 (PDT) MIME-Version: 1.0 References: <20231016143828.647848-1-jeffxu@chromium.org> <55960.1697566804@cvs.openbsd.org> In-Reply-To: <55960.1697566804@cvs.openbsd.org> From: Jeff Xu Date: Tue, 17 Oct 2023 16:01:13 -0700 Message-ID: Subject: Re: [RFC PATCH v1 0/8] Introduce mseal() syscall To: Theo de Raadt Cc: Linus Torvalds , jeffxu@chromium.org, akpm@linux-foundation.org, keescook@chromium.org, sroettger@google.com, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, jannh@google.com, surenb@google.com, alex.sierra@amd.com, apopple@nvidia.com, aneesh.kumar@linux.ibm.com, axelrasmussen@google.com, ben@decadent.org.uk, catalin.marinas@arm.com, david@redhat.com, dwmw@amazon.co.uk, ying.huang@intel.com, hughd@google.com, joey.gouly@arm.com, corbet@lwn.net, wangkefeng.wang@huawei.com, Liam.Howlett@oracle.com, lstoakes@gmail.com, willy@infradead.org, mawupeng1@huawei.com, linmiaohe@huawei.com, namit@vmware.com, peterx@redhat.com, peterz@infradead.org, ryan.roberts@arm.com, shr@devkernel.io, vbabka@suse.cz, xiujianfeng@huawei.com, yu.ma@intel.com, zhangpeng362@huawei.com, dave.hansen@intel.com, luto@kernel.org, linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: k9nyz1tbiq4ybcgxordfia7y9yu55yug X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 24583C0019 X-Rspam-User: X-HE-Tag: 1697583711-93797 X-HE-Meta: U2FsdGVkX1+1Lj+T0OwsMy38EJdLiVVBWDeJ0wfSl4y/KuXkFFaZC7HtNPeYpnwtkl7K6aGFIQRKw4dBmLOUUuCbIpvx/MAWgyv0iaXJH5nGH4AtwQsJMHjnSUV0XyshPoEZg60+405SJxYFdhqcno3QevJCfWM+CWGc9G/c9FycMNwknO0aj1TzLRFMh35QH4aBr1qZmZ0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1jKW8s1ifArxHDuA+erlxmwwM81tqzsn9WHhZlWa7NPz13pGyYM/680r7Bae5+bU/pwFPXeg== 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: On Tue, Oct 17, 2023 at 11:20=E2=80=AFAM Theo de Raadt wrote: > > Linus Torvalds wrote: > > > On Tue, 17 Oct 2023 at 02:08, Jeff Xu wrote: > > > > > > It is probably worth noting that I choose to check one and only > > > one sealing type per syscall. i.e. munmap(2) checks > > > MM_SEAL_MUNMAP only. > > > > Yeah, this is wrong. > > > > It's wrong exactly because other system calls will unmap things too. > > > > Using mmap() to over-map something will unmap the old one. > > > > Same goes for mremap() to move over an existing mapping. > > > > So the whole "do things by the name of the system call" is not workable= . > > > > All that matters is what the system calls *do*, not what their name is. > > I agree completely... > > mseal() is a clone of mimmutable(2), but with an extremely > over-complicated API based upon dubious arguments. > > I designed mimmutable(2) [1] in OpenBSD, it took about a year to get all > the components working correctly. There were many intermediate API > during development, but in the end the API is simply: > > int mimmutable(void *addr, size_t len); > > The kernel code for mimmutable() traverses the specified VA range. In > that range, it will find unmapped sub-regions (which are are ignored) > and mapped sub-regions. For these mapped regions, it does not care what > the permissions are, it just marks each sub-region as immutable. > > Later on, when any VM operation request upon a VA range attempts to > (1) change the permissions > (2) to re-map on top > (3) or dispose of the mapping, > that operation is refused with errno EPERM. We don't care where the > request comes from (ie. what system call). It is a behaviour of the > VM system, when asked to act upon a VA sub-range mapping. > > Very simple semantics. > > The only case where the immutable marker is ignored is during address spa= ce > teardown as a result of process termination. > May I ask, for BSD's implementation of immutable(), do you cover things such as mlock(), madvice() ? or just the protection bit (WRX) + remap() + unmap(). In other words: Is BSD's definition of immutable equivalent to MM_SEAL_MPROTECT|MM_SEAL_MUNMAP|MM_SEAL_MREMAP|MM_SEAL_MMAP, of this patch = set ? I hesitate to introduce the concept of immutable into linux because I don't= know all the scenarios present in linux where VMAs's metadata can be modified. As Jann's email pointed out, There could be quite a few things we still need to deal with, to completely block the possibility, e.g. malicious code attempting to write to a RO memory or change RW memory to RWX. If, as part of immutable, I also block madvice(), mlock(), which also updat= es VMA's metadata, so by definition, I could. What if the user wants the features in madvice() and at the same time, also wants their .text protected ? Also, if linux introduces a new syscall that depends on a new metadata of V= MA, say msecret(), (for discussion purpose), should immutable automatically support that ? Without those questions answered, I couldn't choose the route of immutable() yet. -Jeff > > In his submission of this API, Jeff Xu makes three claims I find dubious; > > > Also, Chrome wants to adopt this feature for their CFI work [2] and thi= s > > patchset has been designed to be compatible with the Chrome use case. > > I specifically designed mimmutable(2) with chrome in mind, and the > chrome binary running on OpenBSD is full of immutable mappings. All the > library regions automatically become immutable because ld.so can infer > it and do the mimmutable calls for the right subregions. > > So this chrome work has already been done by OpenBSD, and it is dead > simple. During early development I thought mimmutable(2) would be > called by applications or libraries, but I was dead wrong: 99.9% of > calls are from ld.so, and no applications need to call it, these are the > two exceptions: > > In OpenBSD, mimmutable() is used in libc malloc() to lock-down some data > structures at initialization time, so they canoot be attacked to create > an invariant for use in ROP return-to-libc style methods. > > In Chrome, there is a v8_flags variable rounded out to a full page, and > placed in .data. Chrome initialized this variable, and wants to mprotect > PROT_READ, but .data has been made immutable by ld.so. So we force this > page into a new ELF section called "openbsd.mutable" which also behaves R= W > like .data. Where chrome does the mprotect PROT_READ, it now also perfo= rms > mimmutable() on that page. > > > Having a seal type per syscall type helps to add the feature incrementa= lly. > > Yet, somehow OpenBSD didn't do it per syscall, and we managed to make our > entire base operating system and 10,000+ applications automatically recei= ve > the benefits. In one year's effort. The only application which cared ab= out > it was chrome, described in the previous paragraph. > > I think Jeff's idea here is super dangerous. What will actually happen > is people will add a few mseal() sub-operations and think the job is done= . > It isn't done. They need all the mseal() requests, or the mapping are > not safe. > > It is very counterproductive to provide developers a complex API that has > insecure suboperations. > > > Applications also know exactly what is sealed. > > Actually applicatins won't know because there is no tooling to inspect th= is -- > but I will argue further that applications don't need to know. Immutable > marking is a system decision, not a program decision. > > > I'll close by asking for a new look at the mimmutable(2) API we settled > on for OpenBSD. I think there is nothing wrong with it. I'm willing to > help guide glibc / ld.so / musl teams through the problems they may find > along the way, I know where the skeletons are buried. Two in > particular: -znow RELRO already today, and xonly-text in the future. > > > [1] https://man.openbsd.org/mimmutable.2 >