Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1318402rwd; Tue, 16 May 2023 15:31:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5QCcd9eE0+djnXRpWYdZLyAKETfde5zlVfp8Me/hNU3Bs9NWTCNve2/DqoBOnf3/Qc8ABq X-Received: by 2002:a17:902:ee42:b0:1ae:48b2:3c2b with SMTP id 2-20020a170902ee4200b001ae48b23c2bmr1790557plo.30.1684276283642; Tue, 16 May 2023 15:31:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684276283; cv=none; d=google.com; s=arc-20160816; b=tLGkP3ebBQ2O04/VAqi/4ssSjtTVNMsoonFN2BEnBazhqM1LTsnfXSFAyGHf8Z8CbX hxemCo+vkVvEtWUwcc7JDPbeb2T46x4yt4UCrxr54pypj6Ffa2ZuwDjDoga45CYLq3Iy vhYt1kLvtoXSHUrSK8TiPFiWrsCDUbozzBFUP8FP0kfBaWiFBl2I0vagBdY0rD9te6uw 4rjcxTYEtoLsBrfou3BkyNNvqbO8iKGM/vxItTsvIWldeMm7pqlL2V4pwmF+wtIohNks adEwDqZy6QuQEB2WuyWb25v4p2OX9HgMl7KIVItEOKHVrH6W8FAtlAcxfftaEG/qkSq5 Nkkw== 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=fsruOfx1bcVhMOcogHRbjQUtZfueJR8BYRLENPgR3EM=; b=ur0P/ZXHodgPAdFXjU+748KGCY0OLBqm7aubhRGpHEl4eCWMC6zqdvPVNmo9CKbSgx WzSsP6Bs/DFhpL+XaO+vX1Gu8sBkNZKtY5FE7frpL+nYliO9uRZ4VxzGLOQ9N6TbDzde PA95mUmkk73UlmbBKXXxYLUHAYswTlQKEz/tZdX5sPg/cGwgyPMH2DnZlIEQnMAB+Efy B8Ke6jPuZHv9eSAIDVxpEAbakMN5O29aN168NH9vIyFqMt+XFAJ6eLt9mu/yOzWF1RQu y5ADhEaOmIJzOmaUR9waSz7/qg3oVFrU1FGrZ6RjbWp2+15KMMSyxAxqxjvvJtZb1GXE mnMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=nPMklpwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020a170902bb9100b001a8d80c7bb1si17944940pls.128.2023.05.16.15.31.10; Tue, 16 May 2023 15:31:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=nPMklpwu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbjEPWZa (ORCPT + 99 others); Tue, 16 May 2023 18:25:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbjEPWZ2 (ORCPT ); Tue, 16 May 2023 18:25:28 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82ACE59F4 for ; Tue, 16 May 2023 15:25:27 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-510a5a5cf79so230a12.1 for ; Tue, 16 May 2023 15:25:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684275926; x=1686867926; 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=fsruOfx1bcVhMOcogHRbjQUtZfueJR8BYRLENPgR3EM=; b=nPMklpwuztnmJsZD5kYHbZk0A4Or9Gp42GrUE6S8mGkzO+p8Q1RGHNOcI9WUQKqniU gn0ajatAen6X7gzIymHoX91rfmlRWZySK/RySMNQAK5kOjMZlgPjP9hI6f4PqPpzcv1A LBGyQlE8hMCsRx6k7yS+9vCI2yhm6TyFxXN+0wWDEVAg6jByIHe8wrmjpYAM7Iz8TFFW EbMp6JKLcQV2GjBthPVWbAumM5Nq/xcQPdoYShR8dnGJP8ipVo9oyDAVf/X3Fw/CGfc6 jP7xbBtYQAsh40WgMFQ0B7WQ0XW57W6jOlkhRKNG/uCkGGZ6UI5mWA0n5ZXSyAGT3RNj Eb7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684275926; x=1686867926; 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=fsruOfx1bcVhMOcogHRbjQUtZfueJR8BYRLENPgR3EM=; b=G365xpAc7NcPXDJ21jL1DJ8wPd22es4dBh2OSbaw6C6lSn922kWX/dnaIBgXsJ4AAD Y921cmnZtUS1H0zvgF3cw+OJVB3QznB2b8jurQG+drXM3i9ADuGrWmwdLQO1xNFB9OYc 0lmHP+GrY6YIMsQ5ftOfkb12bpGVNniT25byTbpgDHK1jzxKYT3JbF2858mNo3bK+Qi3 /8c4HwP8HY3nEFZSh0fsIFPXyvaH0OdfMR4I0C+MT6HOCDqfWgeyfbn5VI/TnufhahKh CwD9kh+HconBdMm1mmg/F6B+NlMv4muO+gxm2iZWyV2B261jSUb7sgIyUVZslsTI7FUy +z+g== X-Gm-Message-State: AC+VfDxJh/o8JbZx1zeHzvVfGXNjxMKLdYdayONuWK/NRR3M4LYexrZ7 IUlNTFSWaE4NIXXPfSP07L+anL0BslW8YeeDYwpufA== X-Received: by 2002:a50:9515:0:b0:4bc:dee8:94ca with SMTP id u21-20020a509515000000b004bcdee894camr7459eda.7.1684275925862; Tue, 16 May 2023 15:25:25 -0700 (PDT) MIME-Version: 1.0 References: <20230515130553.2311248-1-jeffxu@chromium.org> <20230515130553.2311248-6-jeffxu@chromium.org> <202305161302.16BF756DEA@keescook> In-Reply-To: <202305161302.16BF756DEA@keescook> From: Jeff Xu Date: Tue, 16 May 2023 15:24:49 -0700 Message-ID: Subject: Re: [PATCH 5/6] KEY: Apply PKEY_ENFORCE_API to munmap To: Kees Cook Cc: jeffxu@chromium.org, dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 16, 2023 at 1:13=E2=80=AFPM Kees Cook w= rote: > > On Mon, May 15, 2023 at 01:05:51PM +0000, jeffxu@chromium.org wrote: > > From: Jeff Xu > > > > This patch enables PKEY_ENFORCE_API for the munmap > > syscall. > > > > Signed-off-by: Jeff Xu > > --- > > include/linux/mm.h | 2 +- > > mm/mmap.c | 34 ++++++++++++++++++++++++++-------- > > mm/mremap.c | 6 ++++-- > > 3 files changed, 31 insertions(+), 11 deletions(-) > > > > diff --git a/include/linux/mm.h b/include/linux/mm.h > > index 27ce77080c79..48076e845d53 100644 > > --- a/include/linux/mm.h > > +++ b/include/linux/mm.h > > @@ -3136,7 +3136,7 @@ extern unsigned long do_mmap(struct file *file, u= nsigned long addr, > > unsigned long pgoff, unsigned long *populate, struct list_head *u= f); > > extern int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *m= m, > > unsigned long start, size_t len, struct list_hea= d *uf, > > - bool downgrade); > > + bool downgrade, bool syscall); > > For type checking and readability, I suggest using an enum instead of > "bool". Perhaps something like: > > enum caller_origin { > ON_BEHALF_OF_KERNEL =3D 0, > ON_BEHALF_OF_USERSPACE, > }; > Sure, it makes sense. > extern int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, > unsigned long start, size_t len, struct list_hea= d *uf, > - bool downgrade); > + bool downgrade, enum caller_origin called); > > > extern int do_munmap(struct mm_struct *, unsigned long, size_t, > > struct list_head *uf); > > extern int do_madvise(struct mm_struct *mm, unsigned long start, size_= t len_in, int behavior); > > diff --git a/mm/mmap.c b/mm/mmap.c > > index 13678edaa22c..29329aa794a6 100644 > > --- a/mm/mmap.c > > +++ b/mm/mmap.c > > @@ -2498,6 +2498,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, str= uct vm_area_struct *vma, > > * @uf: The userfaultfd list_head > > * @downgrade: set to true if the user wants to attempt to write_downg= rade the > > * mmap_lock > > + * @syscall: set to true if this is called from syscall entry > > * > > * This function takes a @mas that is either pointing to the previous = VMA or set > > * to MA_START and sets it up to remove the mapping(s). The @len will= be > > @@ -2507,7 +2508,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, str= uct vm_area_struct *vma, > > */ > > int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, > > unsigned long start, size_t len, struct list_head *uf, > > - bool downgrade) > > + bool downgrade, bool syscall) > > { > > unsigned long end; > > struct vm_area_struct *vma; > > @@ -2519,6 +2520,19 @@ int do_vmi_munmap(struct vma_iterator *vmi, stru= ct mm_struct *mm, > > if (end =3D=3D start) > > return -EINVAL; > > > > + /* > > + * When called by syscall from userspace, check if the calling > > + * thread has the PKEY permission to modify the memory mapping. > > + */ > > + if (syscall && arch_check_pkey_enforce_api(mm, start, end) < 0) { > > if (called =3D=3D ON_BEHALF_OF_USERSPACE && > arch_check_pkey_enforce_api(mm, start, end) < 0) { > > > + char comm[TASK_COMM_LEN]; > > + > > + pr_warn_ratelimited( > > + "munmap was denied on PKEY_ENFORCE_API memory, pi= d=3D%d '%s'\n", > > + task_pid_nr(current), get_task_comm(comm, current= )); > > + return -EACCES; > > + } > > + > > /* arch_unmap() might do unmaps itself. */ > > arch_unmap(mm, start, end); > > > > @@ -2541,7 +2555,7 @@ int do_munmap(struct mm_struct *mm, unsigned long= start, size_t len, > > { > > VMA_ITERATOR(vmi, mm, start); > > > > - return do_vmi_munmap(&vmi, mm, start, len, uf, false); > > + return do_vmi_munmap(&vmi, mm, start, len, uf, false, false); > > + return do_vmi_munmap(&vmi, mm, start, len, uf, false, ON_BEHALF_O= F_KERNEL); > > > [...] > > SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) > > { > > addr =3D untagged_addr(addr); > > - return __vm_munmap(addr, len, true); > > + return __vm_munmap(addr, len, true, true); > > + return __vm_munmap(addr, len, true, ON_BEHALF_OF_USERSPACE); > > etc. > > -- > Kees Cook Thanks! -Jeff Xu