Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3449080rdg; Tue, 17 Oct 2023 15:36:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG/XLvgnaBJDw/Xv+cQD19fEg3XWx+RJFPBPp31bbS4StHpa8YVaPu8gaLp79xs5nqii/nL X-Received: by 2002:a05:6359:5e02:b0:142:dfc2:a441 with SMTP id pw2-20020a0563595e0200b00142dfc2a441mr3662351rwb.8.1697582163928; Tue, 17 Oct 2023 15:36:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697582163; cv=none; d=google.com; s=arc-20160816; b=B+IXm69s1sqzkHhYSxVI84E951Dc8UT1Klb/auFN143UqQWkYw+dYpKtlbjLyAZIi2 gVK0teMYMSWIsHjSCoh8mLhecg83dwoyZyOTgyVyM9LHIke9DOWcQkGgwy4SXngpafMP SDAN/8qf4Ld0oMRcdHX2Nhb1Jp3hmEsGc+7Ru8CyUZMhJJeB1j10kd5wdBbIwYL1JzoU 5wiR2X9Qx2D4naXs/z1v5GrSLtPgzAOa17kvBlVzH714ahHL2boqflvDoMWmG7NpP97R lcv1qK0n0brfmR0VP1THJA8HLpsWjktv4bXhk/9bDdrrh1bH9axCy1M0Vg+F/c9tq2p4 TRNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=wdeKf2/h6eRXVjrIhtW4p5eKxKczJ25w9PIDmoQxibo=; fh=966dXuEandmoaYnTY8GHRyfEWFigD9UAjPBnrELZGaM=; b=MiKqKCtrJJ4G0MboV3wyY0SrgeuA7wxyMq5yUkrj6xCOq3IjHZ8/CPEUCrwyMUn1se cvz3dXrw0SZc3YhL3R089PEGPKoleOX/C1Aenc1O+9xHrZsSHLLJYtq+0i7f/gttlFMt JQGg1hoQmn49RqbM00rQXxam4ZmqXBr/eb71Jnm5YxpHvdcZ2ZX04sZq5TigIqLEiSW3 QAYnyp1tG8ZJvBublKCQvUW+qyUGbexYOK9ogywQpPRinl/UgUclcxggcT1E4rGPzmcc Q0iuSa1kjuRhvue/MSYGTt5WR5paQHjuPtEj70eC9I7aGvk/n62C2U+cLgUDdUaWSx86 9hoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kz9JnNxI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id cn6-20020a056a020a8600b005891a066de8si795995pgb.416.2023.10.17.15.36.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 15:36:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kz9JnNxI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id CF6C480F7F12; Tue, 17 Oct 2023 15:36:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232068AbjJQWfq (ORCPT + 99 others); Tue, 17 Oct 2023 18:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbjJQWfp (ORCPT ); Tue, 17 Oct 2023 18:35:45 -0400 Received: from mail-ua1-x92a.google.com (mail-ua1-x92a.google.com [IPv6:2607:f8b0:4864:20::92a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 231C1D3; Tue, 17 Oct 2023 15:35:43 -0700 (PDT) Received: by mail-ua1-x92a.google.com with SMTP id a1e0cc1a2514c-7b652e785dbso1670703241.0; Tue, 17 Oct 2023 15:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697582142; x=1698186942; darn=vger.kernel.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=wdeKf2/h6eRXVjrIhtW4p5eKxKczJ25w9PIDmoQxibo=; b=kz9JnNxIUjf6OO+HO6ozFGxWY9E3XWxXqtZBmDfSzNx4d2FAWGHTk0r8Kh+sMBdVGN RR8dH13ai0vJx+omnUDA34J94KuTaWGgk7v50Hv4V/Dntj8JhkpctybYPrm5yRd7hUbo BX3GBr+CIOwpy2ZExkt+XzgE8Zp0CKqgZa5MuTANqZvvEYo2sm2waZ8kFh0pwAQgl6fp knqmhchJa0ZehJRzuKH3I7buMxUY/Sk0Y8BtIOQv8zRTlRFvqyD/hEmAy2Xl5G7mVymS fkVw/9y0R0TZ33LE6oAedBlbOH6JHJ25aYnq0CSbFjhVIuaUMkPLzfWjzGH/uHPxyZ65 ytKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697582142; x=1698186942; 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=wdeKf2/h6eRXVjrIhtW4p5eKxKczJ25w9PIDmoQxibo=; b=ef14aSyDOaG/WdbkyvrOi2P0Sh50+f8oFeixGt2X7B6zfV9+ex3w6ypSrq6yZ+kGSl Jz+8YmAZGpXLhRx/bQoNsfqv1g9MwMQndBIOxdnmltiYdr2nhf+Li6QdM0akFmL9N1Sz xw7lRk12enTysdSFvJgpCqDnSIy3/xXvW+e+BiXfW3UFnreyp/H48Co/sipstfEThMTM ScogehiUrX5S3CldeUOqOGsqL5foU/hNFTS6v35AU9Mnxm9pB4KJr2+5a5tAnnCYOFGf uhAYR5VlUYFmDz4VNeTb2N6HsFaPIvsE9TzC/w6TobHzjM22oLiv7q1ppuWXU0ZNUtGZ X2/Q== X-Gm-Message-State: AOJu0YwHWt/x23qSjeB4ZQfqolSZvjL2umELKC0rW7kuUpKg6H48KNpJ qsI+UvmeooRgzcT/IbYF/KLZxyUzgod+lSjAp/w= X-Received: by 2002:a67:cc15:0:b0:452:60c5:20b with SMTP id q21-20020a67cc15000000b0045260c5020bmr3720336vsl.15.1697582142134; Tue, 17 Oct 2023 15:35:42 -0700 (PDT) MIME-Version: 1.0 References: <20231016143828.647848-1-jeffxu@chromium.org> In-Reply-To: From: Pedro Falcato Date: Tue, 17 Oct 2023 23:35:30 +0100 Message-ID: Subject: Re: [RFC PATCH v1 0/8] Introduce mseal() syscall To: Jeff Xu Cc: Matthew Wilcox , 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, torvalds@linux-foundation.org, lstoakes@gmail.com, 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-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 17 Oct 2023 15:36:01 -0700 (PDT) On Tue, Oct 17, 2023 at 10:34=E2=80=AFPM Jeff Xu wrote: > > On Tue, Oct 17, 2023 at 8:30=E2=80=AFAM Pedro Falcato wrote: > > > > On Mon, Oct 16, 2023 at 4:18=E2=80=AFPM Matthew Wilcox wrote: > > > > > > On Mon, Oct 16, 2023 at 02:38:19PM +0000, jeffxu@chromium.org wrote: > > > > Modern CPUs support memory permissions such as RW and NX bits. Linu= x has > > > > supported NX since the release of kernel version 2.6.8 in August 20= 04 [1]. > > > > > > This seems like a confusing way to introduce the subject. Here, you'= re > > > talking about page permissions, whereas (as far as I can tell), mseal= () is > > > about making _virtual_ addresses immutable, for some value of immutab= le. > > > > > > > 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 syscall. For e= xample, > > > > such an attacker primitive can break control-flow integrity guarant= ees > > > > since read-only memory that is supposed to be trusted can become wr= itable > > > > or .text pages can get remapped. Memory sealing can automatically b= e > > > > applied by the runtime loader to seal .text and .rodata pages and > > > > applications can additionally seal security critical data at runtim= e. > > > > A similar feature already exists in the XNU kernel with the > > > > VM_FLAGS_PERMANENT [3] flag and on OpenBSD with the mimmutable sysc= all [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 cas= e. > > > > > > This [2] seems very generic and wide-ranging, not helpful. [5] was m= ore > > > useful to understand what you're trying to do. > > > > > > > The new mseal() is an architecture independent syscall, and with > > > > following signature: > > > > > > > > mseal(void addr, size_t len, unsigned int types, unsigned int flags= ) > > > > > > > > addr/len: memory range. Must be continuous/allocated memory, or el= se > > > > mseal() will fail and no VMA is updated. For details on acceptable > > > > arguments, please refer to comments in mseal.c. Those are also full= y > > > > covered by the selftest. > > > > > > Mmm. So when you say "continuous/allocated" what you really mean is > > > "Must have contiguous VMAs" rather than "All pages in this range must > > > be populated", yes? > > > > > > > types: bit mask to specify which syscall to seal, currently they ar= e: > > > > MM_SEAL_MSEAL 0x1 > > > > MM_SEAL_MPROTECT 0x2 > > > > MM_SEAL_MUNMAP 0x4 > > > > MM_SEAL_MMAP 0x8 > > > > MM_SEAL_MREMAP 0x10 > > > > > > I don't understand why we want this level of granularity. The OpenBS= D > > > and XNU examples just say "This must be immutable*". For values of > > > immutable that allow downgrading access (eg RW to RO or RX to RO), > > > but not upgrading access (RW->RX, RO->*, RX->RW). > > > > > > > Each bit represents sealing for one specific syscall type, e.g. > > > > MM_SEAL_MPROTECT will deny mprotect syscall. The consideration of b= itmask > > > > is that the API is extendable, i.e. when needed, the sealing can be > > > > extended to madvise, mlock, etc. Backward compatibility is also eas= y. > > > > > > Honestly, it feels too flexible. Why not just two flags to mprotect(= ) > > > -- PROT_IMMUTABLE and PROT_DOWNGRADABLE. I can see a use for that -- > > > maybe for some things we want to be able to downgrade and for other > > > things, we don't. > > > > I think it's worth pointing out that this suggestion (with PROT_*) > > could easily integrate with mmap() and as such allow for one-shot > > mmap() + mseal(). > > If we consider the common case as 'addr =3D mmap(...); mseal(addr);', i= t > > definitely sounds like a performance win as we halve the number of > > syscalls for a sealed mapping. And if we trivially look at e.g OpenBSD > > ld.so code, mmap() + mimmutable() and mprotect() + mimmutable() seem > > like common patterns. > > > Yes. mmap() can support sealing as well, and memory is allocated as > immutable from begining. > This is orthogonal to mseal() though. I don't see how this can be orthogonal to mseal(). In the case we opt for adding PROT_ bits, we should more or less only need to adapt calc_vm_prot_bits(), and the rest should work without issues. vma merging won't merge vmas with different prots. The current interfaces (mmap and mprotect) would work just fine. In this case, mseal() or mimmutable() would only be needed if you need to set immutability over a range of VMAs with different permissions. Note: modifications should look kinda like this: https://godbolt.org/z/Tbjj= d14Pe The only annoying wrench in my plans here is that we have effectively run out of vm_flags bits in 32-bit architectures, so this approach as I described is not compatible with 32-bit. > In case of ld.so, iiuc, memory can be first allocated as W, then later > changed to RO, for example, during symbol resolution. > The important point is that the application can decide what type of > sealing it wants, and when to apply it. There needs to be an api(), > that can be mseal() or mprotect2() or mimmutable(), the naming is not > important to me. > > mprotect() in linux have the following signature: > int mprotect(void addr[.len], size_t len, int prot); > the prot bitmasks are all taken here. > I have not checked the prot field in mmap(), there might be bits left, > even not, we could have mmap2(), so that is not an issue. I don't see what you mean. We have plenty of prot bits left (32-bits, and we seem to have around 8 different bits used). And even if we didn't, prot is the same in mprotect and mmap and mmap2 :) The only issue seems to be that 32-bit ran out of vm_flags, but that can probably be worked around if need be. --=20 Pedro