From patchwork Fri May 19 01:19:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13247585 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 09B5EC77B73 for ; Fri, 19 May 2023 01:19:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D22EF280005; Thu, 18 May 2023 21:19:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CAC65280001; Thu, 18 May 2023 21:19:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB114280005; Thu, 18 May 2023 21:19:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 93688280001 for ; Thu, 18 May 2023 21:19:34 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5E199A09B8 for ; Fri, 19 May 2023 01:19:34 +0000 (UTC) X-FDA: 80805247068.12.2F7E299 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf14.hostedemail.com (Postfix) with ESMTP id 8AF5110000E for ; Fri, 19 May 2023 01:19:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KyUoiw4E; spf=pass (imf14.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684459172; a=rsa-sha256; cv=none; b=kz51ftLBXxg7StQyEmDTC+ZERAzUKSIiZLqBtHDLwneesyKFAYozCZyFReA99G/s1a0O11 3532QcanFH7OzsFLqNaje1IJ5X+Lnq+ImtjOUiHbnFfS4R2m37xUtCZAf92K7ypyN0BboZ faxG8OA6h4FnhBGS4XJXbdJ+A8Ezym4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KyUoiw4E; spf=pass (imf14.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.215.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684459172; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UP7PrspRcAj60L7H0nNNXk2sIZvcVn8Hzlm0pHNc5DI=; b=ZTO0B50jtO5Fo0w4mPLvGiMHhhThUofkxwXqe/O6jVGOK9Bx12iyMnmpm9iKCACriq+ZKC 54lWqoNw/QxgtHmT/kSsMi5klmawNRFE7rk7k5Elzphd5a79cWysb1GvvphrHujeoefGgL TByMIf0D+E0dRq4cHeKwoD0oTRTBF7I= Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-53404873a19so1850428a12.3 for ; Thu, 18 May 2023 18:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684459171; x=1687051171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UP7PrspRcAj60L7H0nNNXk2sIZvcVn8Hzlm0pHNc5DI=; b=KyUoiw4ElMe/LNfLauu3mRsWtl6tLzsjFFWTg2362wmgmg2Ddqu/eccJT5ajd/WVCK u1l0ug+0cIrf8bFw6zRHAri7y+MBhpSJTKovSJLlNyH0CjoNHHPa7W+0dgGBS04gOyOU DCYEDZN/Ib9Kcmnqo4b0yFdJ8ouzcRriQUlWU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684459171; x=1687051171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UP7PrspRcAj60L7H0nNNXk2sIZvcVn8Hzlm0pHNc5DI=; b=OGfqMyOO7ECbsdQKvzDr/Y6QU6HCwSmKMC/Wl16x8LY+GIsnNXHKdE6q9eNEqUEi17 AeZ9OwtKXS8AFqLM6VPCVSI0lMZ9Pd99dnn18SeE6UjDaqsej34JC42ZiHlUBBmNlSBm T09MnSJjjp4LEBklqEbdR0GIE+GM2+aNSAHvu5l/EyJH4st4s8gI2xr6ltBzF8zT/vJX aWeSX4IZXthVGyIQtB4Tt1/5hkPwnbq6pgyKkBY16U0060X7O12ylugLOAa2I61NRI7i XPy9/G4/SkyNGmAfrKhmjResC0/LA/X1qOlJLSilpgHNESUhpWyOk6bMVBiwrluOOfZU OcQQ== X-Gm-Message-State: AC+VfDxOIO1vFi/lRssDYdQ6UNhMTxTsBydztzEjbOYMLM5IRGcbn3Rm V2I3Sz27MsbFzNDqm6PFxXSk6A== X-Google-Smtp-Source: ACHHUZ68oo3FWI/wrCU6PluVlfGvpW5h8fWIl8HSfv8KKt7tiRrAfmCm24UxHEFUgu6q95GiIgMiZw== X-Received: by 2002:a17:902:dac7:b0:1ac:7e95:74be with SMTP id q7-20020a170902dac700b001ac7e9574bemr1183574plx.28.1684459171540; Thu, 18 May 2023 18:19:31 -0700 (PDT) Received: from localhost (183.43.230.35.bc.googleusercontent.com. [35.230.43.183]) by smtp.gmail.com with UTF8SMTPSA id ji17-20020a170903325100b001960706141fsm2102839plb.149.2023.05.18.18.19.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 18 May 2023 18:19:31 -0700 (PDT) From: jeffxu@chromium.org To: dave.hansen@intel.com, luto@kernel.org, jorgelo@chromium.org, keescook@chromium.org, groeck@chromium.org, jannh@google.com, sroettger@google.com Cc: akpm@linux-foundation.org, jeffxu@google.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: [PATCH v1 5/6] PKEY: Apply PKEY_ENFORCE_API to munmap Date: Fri, 19 May 2023 01:19:13 +0000 Message-ID: <20230519011915.846407-6-jeffxu@chromium.org> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230519011915.846407-1-jeffxu@chromium.org> References: <20230519011915.846407-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8AF5110000E X-Stat-Signature: aoma6hfcs1r7e3m34ueja7nbpw8fawck X-Rspam-User: X-HE-Tag: 1684459172-431552 X-HE-Meta: U2FsdGVkX1/Z48zfp6tQpKqKUHpgkNur19TPCOn7HLUj3ht6a3Ervqy3K0TdWDZwwYhPhWv86zTwQqr47XRLhqKjPUVE9/zuWPn7GpcnKTyTYnYB2v6tyHjWwHR8duUAG7SNnbPD2Ktp3nONMbTWcxoP7jImsRTIt3wrtpTmQi5HcTKgA8Gy/bb9J6idw2oCJ7KmRqIJ4mq7UY7WYQA2zFOTPQUAKGbB34O7bk4VJ2jfrs58KCBPc+YHABgwWxhhzu/MD0Vo/c5+B3Mzc6ura6ZLy1HwA1Ey50PxUpXPOJ8DfmLVM4oMfzqxkEmUvz26kzkBt5A+qXQGcGOFaTQZ1rP6eeHVox1z78q+x0j34tOzMsPHf8pTI2JA0WzZNyI6GkHMPUeYmNH7N5ANbHNkaUVmG81xupKQDhrrKigXc3r7aiHon48oQjukDHRzLo5Fc8cM4+GeO9nl8cNRqgzxvYI0w6EFiMWuE3yJdSq7jVpi/kwx/IkkUDtrmCpemMemcz31av+iYWtCqg/Dte9X7GMidh+poY7m5M3j5PwVR9rWmQR6C+Ich0XUlmezjfcnc0bknBwcXlbkc/fqjOW21QxMZu8h0erbXNPS565wvA6Hyse6sOiIK4rEl14GZeyT0EeXyp1+4AUkGerET4ZVTzkrFqHkS+tdToviO1GTbnL88W+qb1Lo6sjkEmXHg0NIgbYkiXbDFv/G6yIeVx1QC8RqYKwjvfVnB8WVzRC+6+VC97h7bCaMNfr3pOlcu9AtGezj5/W7dRK76PUs+mdH/s645fepFJPJpvJy40pS8PGnTctm0A9dx2dBbfs148mvhLKIStvbWa3hkLj9psQNjrzRkHIxhUBHy1/VpOQ+b0kED+dqXWb/pOqilkGcSb4+Do4g/iMPOLdXg40NaVUWnf91DPzafBGsN1vTPeEyU1SH8FeH08PXZinjcvOfPo+kV1lXns/tazkaWvnhiz3 hcQRYXJG gAyXfDHTNOQ3OXPH5PoCyn5VsBV2jttN36ZMZTpBl7u3kN+Cc/CqMJWYpM2sMLdTwD7jBL1HY/KCqnUFqLDBkKQ3CIz7g8I3p7D/ipOT5QofkvLyl9QjDoy/I4DZpmwgc3VPOu6XXn80K8pvXDLoIYN85xXBMHzNCRgHSJsSMO2UkqGwasVSPgR5Wd9j6npkij0faFLSHF4Bd3Jw5W9FrsGgmN/ihx/a82a16PY2woOA4hbE2luC0X+G+6EEz4lDacyx3SBsSCQI/+wxGQLWBMXzbO3J9hsOMauxSpjY/FRpgcbWIxVACzqZ1NdMxNqDVHLx0efMUctzJKpIsWdz+mGjGkSGHlH7sBeO3ZV58EvqHzWe6SyNbh/SJkg== 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: From: Jeff Xu This patch enables PKEY_ENFORCE_API for the munmap syscall. Signed-off-by: Jeff Xu --- include/linux/mm.h | 8 +++++++- mm/mmap.c | 31 +++++++++++++++++++++++-------- mm/mremap.c | 6 ++++-- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 27ce77080c79..795cdeee2ea7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -429,6 +429,12 @@ extern unsigned int kobjsize(const void *objp); #endif #define VM_FLAGS_CLEAR (ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR) +/* The current call stack is originated from user space or kernel */ +enum caller_origin { + ON_BEHALF_OF_KERNEL = 0, + ON_BEHALF_OF_USERSPACE, +}; + /* * mapping from the currently active vm_flags protection bits (the * low four bits) to a page protection mask.. @@ -3136,7 +3142,7 @@ extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long pgoff, unsigned long *populate, struct list_head *uf); extern 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, 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..3de95a6a4397 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2498,6 +2498,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, * @uf: The userfaultfd list_head * @downgrade: set to true if the user wants to attempt to write_downgrade the * mmap_lock + * @called: originated from user space or kernel * * 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, struct 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, enum caller_origin called) { unsigned long end; struct vm_area_struct *vma; @@ -2519,6 +2520,15 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, if (end == start) return -EINVAL; + /* + * When called by syscall from userspace, check if the calling + * thread has the PKEY permission to modify the memory mapping. + */ + if (called == ON_BEHALF_OF_USERSPACE && + arch_check_pkey_enforce_api(mm, start, end) < 0) { + return -EACCES; + } + /* arch_unmap() might do unmaps itself. */ arch_unmap(mm, start, end); @@ -2541,7 +2551,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, ON_BEHALF_OF_KERNEL); } unsigned long mmap_region(struct file *file, unsigned long addr, @@ -2575,7 +2585,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, } /* Unmap any existing mapping in the area */ - if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) + if (do_vmi_munmap(&vmi, mm, addr, len, uf, false, + ON_BEHALF_OF_KERNEL)) return -ENOMEM; /* @@ -2792,7 +2803,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return error; } -static int __vm_munmap(unsigned long start, size_t len, bool downgrade) +/* + * @called: originated from user space or kernel + */ +static int __vm_munmap(unsigned long start, size_t len, bool downgrade, + enum caller_origin called) { int ret; struct mm_struct *mm = current->mm; @@ -2802,7 +2817,7 @@ static int __vm_munmap(unsigned long start, size_t len, bool downgrade) if (mmap_write_lock_killable(mm)) return -EINTR; - ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); + ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade, called); /* * Returning 1 indicates mmap_lock is downgraded. * But 1 is not legal return value of vm_munmap() and munmap(), reset @@ -2820,14 +2835,14 @@ static int __vm_munmap(unsigned long start, size_t len, bool downgrade) int vm_munmap(unsigned long start, size_t len) { - return __vm_munmap(start, len, false); + return __vm_munmap(start, len, false, ON_BEHALF_OF_KERNEL); } EXPORT_SYMBOL(vm_munmap); SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) { addr = untagged_addr(addr); - return __vm_munmap(addr, len, true); + return __vm_munmap(addr, len, true, ON_BEHALF_OF_USERSPACE); } @@ -3055,7 +3070,7 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) if (ret) goto limits_failed; - ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); + ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0, ON_BEHALF_OF_KERNEL); if (ret) goto munmap_failed; diff --git a/mm/mremap.c b/mm/mremap.c index b11ce6c92099..a3f576effbb1 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -703,7 +703,8 @@ static unsigned long move_vma(struct vm_area_struct *vma, } vma_iter_init(&vmi, mm, old_addr); - if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false) < 0) { + if (do_vmi_munmap(&vmi, mm, old_addr, old_len, uf_unmap, false, + ON_BEHALF_OF_KERNEL) < 0) { /* OOM: unable to split vma, just get accounts right */ if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) vm_acct_memory(old_len >> PAGE_SHIFT); @@ -993,7 +994,8 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, VMA_ITERATOR(vmi, mm, addr + new_len); retval = do_vmi_munmap(&vmi, mm, addr + new_len, - old_len - new_len, &uf_unmap, true); + old_len - new_len, &uf_unmap, true, + ON_BEHALF_OF_KERNEL); /* Returning 1 indicates mmap_lock is downgraded to read. */ if (retval == 1) { downgraded = true;