Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3948143rdg; Wed, 18 Oct 2023 10:15:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOr+EAOcKFUlGxLgny+EYGEf201+Ob/1hWpC/l3potiIAjX4TpxrTHY/PawYVUol6mtkoE X-Received: by 2002:a17:90a:e397:b0:27d:51c4:1679 with SMTP id b23-20020a17090ae39700b0027d51c41679mr5541126pjz.27.1697649302337; Wed, 18 Oct 2023 10:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697649302; cv=none; d=google.com; s=arc-20160816; b=tgnHkD4LnjF4T0QclZJrmhLLmJFw1ryO+OMc7METrEzmdiZnK79lwgLkX50huGuDma wG2AOYZh5lWT2nFuMsx2iF+cu/G8WuaWbltezcMMFZ7CbmjELyQx5a+rWSgId5u+lgz4 wQIaR+cr+9vZbdf6UMLHnc7M72McmEkhtQYJeYj9KIttwP0KZkc8wNaeWyWenvlbKCbx IYPNMpQ8tjfrOQl49BaoR7uCkX6YZrGjD1xVrIQ9VLNRxwik3KOH5WcQI7EELLGsBVV+ esJUJOXX1LOfXyUa5mePdZs521OEuol9LCtuP/tFCW7Nx4cOMnXQwmwvQk+qN2g7btmS JoUQ== 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=CxyGP1aBivHX+AXORIadgXhguJIarGguet1dgSguQtQ=; fh=A5tR/fPg4W5/UY+St1h4u8Kg+luuf+U9l8bopJi0YzI=; b=nNbYXafI1y/Jkeo1/f5jQCLZwpkuIQESeG+69qdqIUPIyEWec3WELwB12CCpjA/fnx n4GZOlg8S587+RvHxbSr5WSBNqTi+S6uR1aG3IYLD3bCBYCC7UFAE6TU4jghBqCWEHva 20/eahLbb6EAvQMI+rYvx6vj1DlBjk1lxdUk3IfTvMTd5zrCqxq3LlaTyNz3O2zGO9p9 L430QliDx2rskAUeQAipvJUrQwvmQQ6oiRG+EoxK0mjWSuxoHj6314l2iRAHOhiQBkEX Mos2zSnc0aoA4sSiVRWPGlCkX3yCVgktC8/wAcWBwT1HOf1PaxL2DZetUtvF+Ng8vCGf FNAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=hMh+AtPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id k69-20020a636f48000000b005859c221c6dsi2492944pgc.221.2023.10.18.10.15.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 10:15:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=hMh+AtPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 55DCA820D5DD; Wed, 18 Oct 2023 10:14:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229695AbjJRROm (ORCPT + 99 others); Wed, 18 Oct 2023 13:14:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbjJRROl (ORCPT ); Wed, 18 Oct 2023 13:14:41 -0400 Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2205113 for ; Wed, 18 Oct 2023 10:14:39 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id d75a77b69052e-41b19dda4c6so10771cf.1 for ; Wed, 18 Oct 2023 10:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697649279; x=1698254079; 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=CxyGP1aBivHX+AXORIadgXhguJIarGguet1dgSguQtQ=; b=hMh+AtPXbOLlCOMOk1bJIuiKgIrFaWev5dVIPjVvZbUc383ON12sf1zqHY+CywqTTR 5kod30imCKWk29UIxMLZqeV5v+fA6QJya83nZXmlZ5e96KeOZ94A9sei38WJaverrHb1 UD/Es20kPRerxiIvIKLjxm5v/AWkYbHdrsnxKkMMbFERGhXOePTuYPx8z6OOkhNOa3dv NLoq3brL+zxqPX5XNxGPr3VsGQmTht7JW1ls2WFLQ3WZcUkRGrtP4OHJe+v2REq4NxI+ y3wwgbfA49RlUedhKb3/4EvoqfveNyLHsfpEywyU4Rcxxnz2R9GuRCBWrumiNp+ZmNbc baPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697649279; x=1698254079; 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=CxyGP1aBivHX+AXORIadgXhguJIarGguet1dgSguQtQ=; b=fkT+uPKLuE6DJGFeioLVucvq8d2KX0OyXO2QX237q7j0gyibY0yPbLxylPM7ogrYA2 GydYMP3fOcnTJqBsbOFTBmzI67vBmQB8ZHfTuDBsrcY3vDgZ5OkMqP/3brEiBPjvC3hn 30JmsGaN7IJ2WgN7OmdCVoNLKOErhJ82JrCmkn+GUsy2RXON7xIE8Loq31ftq43vNuyA Sjx6VkXIey4GMuZoaINmVgzBmuL76VyE+o444npSCUErsMEybl0K6q3OzQ3ZR0CESTS+ w+dyu6/I3uMeNgmViUCdggS5z6smKjacpndMF3pyxeYE9j6NmxtOVMx/VG7v3WU4GZ/Y gV0Q== X-Gm-Message-State: AOJu0YyHZhHz+NRY/+f3B6PPtcS1kQB9ZE0MhIevWOvlY3+2rLl9i1+d OycXU5zaMRTdSwAnOADDWzUZt3HN9cCPW+IHagXdyg== X-Received: by 2002:ac8:4d4a:0:b0:41b:aedb:a82d with SMTP id x10-20020ac84d4a000000b0041baedba82dmr15384qtv.28.1697649278489; Wed, 18 Oct 2023 10:14:38 -0700 (PDT) MIME-Version: 1.0 References: <20231017090815.1067790-1-jeffxu@chromium.org> <20231017090815.1067790-6-jeffxu@chromium.org> In-Reply-To: From: Jeff Xu Date: Wed, 18 Oct 2023 10:14:02 -0700 Message-ID: Subject: Re: [RFC PATCH v2 5/8] mseal: Check seal flag for munmap(2) To: Linus Torvalds Cc: jeffxu@chromium.org, akpm@linux-foundation.org, keescook@chromium.org, jannh@google.com, sroettger@google.com, willy@infradead.org, gregkh@linuxfoundation.org, jorgelo@chromium.org, groeck@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, 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, 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=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 18 Oct 2023 10:14:59 -0700 (PDT) On Wed, Oct 18, 2023 at 8:08=E2=80=AFAM Jeff Xu wrote: > > On Tue, Oct 17, 2023 at 9:54=E2=80=AFAM Linus Torvalds > wrote: > > > > On Tue, 17 Oct 2023 at 02:08, wrote: > > > > > > Of all the call paths that call into do_vmi_munmap(), > > > this is the only place where checkSeals =3D MM_SEAL_MUNMAP. > > > The rest has checkSeals =3D 0. > > > > Why? > > > > None of this makes sense. > > > > So you say "we can't munmap in this *one* place, but all others ignore > > the sealing". > > > I apologize that previously, I described what this code does, and not rea= soning. > > In our threat model, as Stephen R=C3=B6ttger point out in [1], and I quot= e: > > V8 exploits typically follow a similar pattern: an initial bug leads > to memory corruption but often the initial corruption is limited and > the attacker has to find a way to arbitrarily read/write in the whole > address space. > > The memory correction is in the user space process, e.g. Chrome. > Attackers will try to modify permission of the memory, by calling > mprotect, or munmap then mmap to the same address but with different > permission, etc. > > Sealing blocks mprotect/munmap/mremap/mmap call from the user space > process, e.g. Chrome. > > At time of handling those 4 syscalls, we need to check the seal ( > can_modify_mm), this requires locking the VMA ( > mmap_write_lock_killable), and ideally, after validating the syscall > input. The reasonable place for can_modify_mm() is from utility > functions, such as do_mmap(), do_vmi_munmap(), etc. > > However, there is no guarantee that do_mmap() and do_vmi_munmap() are > only reachable from mprotect/munmap/mremap/mmap syscall entry point > (SYSCALL_DEFINE_XX). In theory, the kernel can call those in other > scenarios, and some of them can be perfectly legit. Those other > scenarios are not covered by our threat model at this time. Therefore, > we need a flag, passed from the SYSCALL_DEFINE_XX entry , down to > can_modify_mm(), to differentiate those other scenarios. > > Now, back to code, it did some optimization, i.e. doesn't pass the > flag from SYSCALL_DEFINE_XX in all cases. If SYSCALL_DEFINE_XX calls > do_a, and do_a has only one caller, I will set the flag in do_a, > instead of SYSCALL_DEFINE_XX. Doing this reduces the size of the > patchset, but it also makes the code less readable indeed. I could > remove this optimization in V3. I welcome suggestions to improve > readability on this. > > When handing the mmap/munmap/mremap/mmap, once the code passed > can_modify_mm(), it means the memory area is not sealed, if the code > continues to call the other utility functions, we don't need to check > the seal again. This is the case for mremap(), the seal of src address > and dest address (when applicable) are checked first, later when the > code calls do_vmi_munmap(), it no longer needs to check the seal > again. > > [1] https://v8.dev/blog/control-flow-integrity > > -Jeff There is also alternative approach: For all the places that call do_vmi_munmap(), find out which case should ignore the sealing flag legitimately, set an ignore_seal flag and pass it down into do_vmi_munmap(). For the rest case, use default behavior. All future API will automatically be covered for sealing, by using default. The risky side, if I missed a case that requires setting ignore_seal, there will be a bug. Also if a driver calls the utility functions to unmap a memory, the seal will be checked as well. (Driver is not in our threat model, but Chrome probably doesn't mind it.) Which of those two approaches are better ? I appreciate the direction on th= is. Thanks! -Jeff -Jeff