From patchwork Sun Jun 12 21:32:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 12878845 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 BCDE5C433EF for ; Sun, 12 Jun 2022 21:32:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 575736B015B; Sun, 12 Jun 2022 17:32:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 54B546B015D; Sun, 12 Jun 2022 17:32:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EC1F6B015E; Sun, 12 Jun 2022 17:32:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2F3F46B015B for ; Sun, 12 Jun 2022 17:32:41 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0122733918 for ; Sun, 12 Jun 2022 21:32:40 +0000 (UTC) X-FDA: 79570883322.03.C176ADD Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf12.hostedemail.com (Postfix) with ESMTP id B73DF40086 for ; Sun, 12 Jun 2022 21:32:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=tvuIUvh4KMt/W3Ck0YL8jqhN/KST+B/kPdeyBtOnYMI=; b=jOcEWP6MokPpHUTnlAkh/E65mL 4W8V4vdlxVz2zQtkO9qZYuNZFn6zxe5LZMB+22Jesq35WG5x3Nw2E3mICgwIDFOWEmlaEzeLzTZif F1GoRcMUDGfoYNRj1nVAjB22aXZ/MwOO1EqMsv4gwAqSiELfP/UKhYIiEePON6kF+HKG7nHdQzv2v lQDev+2c7TK3GFkWkjZwQ1dgj9RG1XcUcrKpO8SY1zyRMfgip7xKLuuYv2IEupKC4R2Dp5F8os/YI opoHyaafYUTwT8RK0IuVDaP//qQIWKe5VGQMnGY6LVMulBYnbpkT5IkqyQj0zN0ywgWy3VDWwcTGT 66BdCNUQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0VC9-00GHpw-CG; Sun, 12 Jun 2022 21:32:29 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Uladzislau Rezki , Zorro Lang , linux-xfs@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/3] usercopy: Handle vm_map_ram() areas Date: Sun, 12 Jun 2022 22:32:25 +0100 Message-Id: <20220612213227.3881769-2-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220612213227.3881769-1-willy@infradead.org> References: <20220612213227.3881769-1-willy@infradead.org> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655069560; a=rsa-sha256; cv=none; b=Y4czRpO2KltR31C+QwefRlN9GNG6JAM30faH9jAqgkccqdDkWhGskT5idnob9KmpDPGIik uK6lSsH4Auo8O0bdNwyDsKWONosgBlBmk3KbDx0OXP5MTZNb1UyPH0VHWMT1VtFDdzP0gI H8CWQ9q3DD4k6QYIBjgiYlhrfKLwcGY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=jOcEWP6M; dmarc=none; spf=none (imf12.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655069560; 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=tvuIUvh4KMt/W3Ck0YL8jqhN/KST+B/kPdeyBtOnYMI=; b=JSVl1X0UZa/Odx4vPrad91OsVRBC1EVN8TJm2D9J/ATKoYLQBVvkwlftyUlUYUG3JvdlNC Mv7eVT8DZ82/Y0KvzbLEFFW07Qt72c/FBzzeNc0nnkVoDscUAz3+cGF488HL5DKWfFHuOY Yu5Wo9rA77CpTCpAvHkliI0GtfTOjFc= X-Stat-Signature: carysqra8ymj1f4j859jayucw9jjpeb5 X-Rspamd-Queue-Id: B73DF40086 X-Rspam-User: X-Rspamd-Server: rspam10 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=jOcEWP6M; dmarc=none; spf=none (imf12.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-HE-Tag: 1655069560-652461 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: vmalloc does not allocate a vm_struct for vm_map_ram() areas. That causes us to deny usercopies from those areas. This affects XFS which uses vm_map_ram() for its directories. Fix this by calling find_vmap_area() instead of find_vm_area(). Fixes: 0aef499f3172 ("mm/usercopy: Detect vmalloc overruns") Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/vmalloc.h | 1 + mm/usercopy.c | 8 +++++--- mm/vmalloc.c | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index b159c2789961..096d48aa3437 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -215,6 +215,7 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size, void free_vm_area(struct vm_struct *area); extern struct vm_struct *remove_vm_area(const void *addr); extern struct vm_struct *find_vm_area(const void *addr); +struct vmap_area *find_vmap_area(unsigned long addr); static inline bool is_vm_area_hugepages(const void *addr) { diff --git a/mm/usercopy.c b/mm/usercopy.c index baeacc735b83..fdd1bed3b90a 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -173,7 +173,7 @@ static inline void check_heap_object(const void *ptr, unsigned long n, } if (is_vmalloc_addr(ptr)) { - struct vm_struct *area = find_vm_area(ptr); + struct vmap_area *area = find_vmap_area((unsigned long)ptr); unsigned long offset; if (!area) { @@ -181,8 +181,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n, return; } - offset = ptr - area->addr; - if (offset + n > get_vm_area_size(area)) + /* XXX: We should also abort for free vmap_areas */ + + offset = (unsigned long)ptr - area->va_start; + if ((unsigned long)ptr + n > area->va_end) usercopy_abort("vmalloc", NULL, to_user, offset, n); return; } diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 07db42455dd4..effd1ff6a4b4 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1798,7 +1798,7 @@ static void free_unmap_vmap_area(struct vmap_area *va) free_vmap_area_noflush(va); } -static struct vmap_area *find_vmap_area(unsigned long addr) +struct vmap_area *find_vmap_area(unsigned long addr) { struct vmap_area *va; From patchwork Sun Jun 12 21:32:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 12878842 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 8087DC433EF for ; Sun, 12 Jun 2022 21:32:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8AE56B0154; Sun, 12 Jun 2022 17:32:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3B1A6B0157; Sun, 12 Jun 2022 17:32:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C01DD6B0158; Sun, 12 Jun 2022 17:32:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B02456B0154 for ; Sun, 12 Jun 2022 17:32:34 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 806FD349FD for ; Sun, 12 Jun 2022 21:32:34 +0000 (UTC) X-FDA: 79570883028.23.9C29D7F Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf31.hostedemail.com (Postfix) with ESMTP id 9664F2009C for ; Sun, 12 Jun 2022 21:32:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=KF4owYCbPISYV5a61DiGRvGs/L1LMsz9ReyZ7eXT5t8=; b=geEs3gAVKXk3eNcQdArpYi2DVd f/MN4q94FG7FHdgrWVmDI4vEtNGBpS1vD/DP2rHs7ji3PrgfGGHOWvn8A3AAmpRzatm8leS4fNMaR Fs6S3luOZoQnLu1wdgThCd5i4g41xF0vblXF68HK3PGR3NF5mHXwdGlK8f7j8EYhpBEMYEy9oRHEH qlEW7b8tnYQ0rBpT67IXfWPu99LTz+lOBZmsIyf5LrvwXzKFSHCbS4hUlucLJWK2Wrj8/2TQSpg3W i9qz6yiljtaOFgTf83pXjguAzUIl6miPCNqPkGAOUJitBDthtLjXjcCzOkxJg685Cs7EdPJSyQkRX IVlGdg9w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0VC9-00GHpy-EN; Sun, 12 Jun 2022 21:32:29 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Uladzislau Rezki , Zorro Lang , linux-xfs@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/3] usercopy: Cast pointer to an integer once Date: Sun, 12 Jun 2022 22:32:26 +0100 Message-Id: <20220612213227.3881769-3-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220612213227.3881769-1-willy@infradead.org> References: <20220612213227.3881769-1-willy@infradead.org> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655069554; 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=KF4owYCbPISYV5a61DiGRvGs/L1LMsz9ReyZ7eXT5t8=; b=sb5KitDAFjJpexETOaOo+ony2nrwWW9p6BGXHk+w2xyO8So7U3qlVo1G7ulUR7w0QII0kx YRG3nu6iyCZ0PqQ11UgH1MKddy752wX8q+aolmGFJG+GzGbgF6An8iFcChk7UdQA2fJBir JkSjvMLP4MFW/gYK8C1HlFtxP2hsH0w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655069554; a=rsa-sha256; cv=none; b=56LRTpNPFU+7f+Uw87ECaJ4xZ3ZdZCIoIVEh3axEwhinvNhfCdLbsfDOPFghP5qcyfc2Bg U9JAqK84uOB7mf0BCfc+OtvVXq5XgfnHE2DUEj+Jl9PSMr5/wPIcF4aCwIYjoFESilZATA 8zwwk4QF9pWH2tPu+l182FM+IWm64Gg= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=geEs3gAV; dmarc=none; spf=none (imf31.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=geEs3gAV; dmarc=none; spf=none (imf31.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam03 X-Stat-Signature: ksboe8yokws3rnq61138kznacbg7h4h7 X-Rspamd-Queue-Id: 9664F2009C X-HE-Tag: 1655069553-890885 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: Get rid of a lot of annoying casts by setting 'addr' once at the top of the function. Signed-off-by: Matthew Wilcox (Oracle) --- mm/usercopy.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/usercopy.c b/mm/usercopy.c index fdd1bed3b90a..31deee7dd2f5 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -161,19 +161,20 @@ static inline void check_bogus_address(const unsigned long ptr, unsigned long n, static inline void check_heap_object(const void *ptr, unsigned long n, bool to_user) { + uintptr_t addr = (uintptr_t)ptr; struct folio *folio; if (is_kmap_addr(ptr)) { - unsigned long page_end = (unsigned long)ptr | (PAGE_SIZE - 1); + unsigned long page_end = addr | (PAGE_SIZE - 1); - if ((unsigned long)ptr + n - 1 > page_end) + if (addr + n - 1 > page_end) usercopy_abort("kmap", NULL, to_user, offset_in_page(ptr), n); return; } if (is_vmalloc_addr(ptr)) { - struct vmap_area *area = find_vmap_area((unsigned long)ptr); + struct vmap_area *area = find_vmap_area(addr); unsigned long offset; if (!area) { @@ -183,8 +184,8 @@ static inline void check_heap_object(const void *ptr, unsigned long n, /* XXX: We should also abort for free vmap_areas */ - offset = (unsigned long)ptr - area->va_start; - if ((unsigned long)ptr + n > area->va_end) + offset = addr - area->va_start; + if (addr + n > area->va_end) usercopy_abort("vmalloc", NULL, to_user, offset, n); return; } From patchwork Sun Jun 12 21:32:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 12878843 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 E3C74C43334 for ; Sun, 12 Jun 2022 21:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 550A96B0157; Sun, 12 Jun 2022 17:32:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FCEA6B0159; Sun, 12 Jun 2022 17:32:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 377496B015A; Sun, 12 Jun 2022 17:32:36 -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 24ED36B0157 for ; Sun, 12 Jun 2022 17:32:36 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EF96E2095F for ; Sun, 12 Jun 2022 21:32:35 +0000 (UTC) X-FDA: 79570883070.13.AD49415 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf03.hostedemail.com (Postfix) with ESMTP id A576520087 for ; Sun, 12 Jun 2022 21:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=HTrppkgyoE0jfkD/I3hwHGO9wel7JMRK5JWmxra0aTw=; b=SEdm11PHMGs3xIT5eFgs1AaLQw wBdGhsxowR+3VG3Z0qlqYVd4ShqfwhcvCeXebJDQgFdY21nGO8jGU1DiQ9AnXML62aXC9T9iJ+xqE ZaQFMUEEbXRdl1IMHc60ASgvvamq7MuPHSP1mYdaxzNfDsL6pe3370ki95kajTAbs4E9G8M7XpSva qayDemMjdO4a79683xTqgJLYpn/2Q+rd2YonUuDU7DgtZ7ppIissSTdoJs4IUW+zX/+2vkE+ezHUC tJjdE39HiA2gssJMq9StqsbNVvNpaQY/pdSNrHJbus6q0krzIq7U/nYgXzmP4Q3fh+G+ICbIlUBSl lFYersDQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1o0VC9-00GHq0-GJ; Sun, 12 Jun 2022 21:32:29 +0000 From: "Matthew Wilcox (Oracle)" To: Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Uladzislau Rezki , Zorro Lang , linux-xfs@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 3/3] usercopy: Make usercopy resilient against ridiculously large copies Date: Sun, 12 Jun 2022 22:32:27 +0100 Message-Id: <20220612213227.3881769-4-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220612213227.3881769-1-willy@infradead.org> References: <20220612213227.3881769-1-willy@infradead.org> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655069554; a=rsa-sha256; cv=none; b=tA2kVQ4WaJI5VJKgvVEBV5S2t7M5oCAMCrcHsW1IK0SbUDmvHEGm0hBTLtTzGLNkBLCgE4 azypDKs8lrF3cPsuBCzDT9mk8iYGMs0Tuk5BUozVH7mMpLMEnNQCmTJmPArc5KimHeREvi me5Rc2NCMI054TXM8Xa7MrDpFalV1tE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SEdm11PH; dmarc=none; spf=none (imf03.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655069554; 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=HTrppkgyoE0jfkD/I3hwHGO9wel7JMRK5JWmxra0aTw=; b=2OktHJMiHk9Ev/jRbSTJX/R9TOD987fE2puC/k+0kuHTirwLdvl+JHMws+wO3rKyPZyZph g9hj6mANou/9+l4WxCr70lXn4c6nC4riml7NSD0MtuFNKtL88wS1MT8JJYhfLUIMMllC04 jt3uYP+47ozvgWttRNtTQO7y5uD85RQ= X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A576520087 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SEdm11PH; dmarc=none; spf=none (imf03.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Stat-Signature: 1gwsm4i1zfs6eiemefyingoyiofmedzt X-HE-Tag: 1655069554-443835 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: If 'n' is so large that it's negative, we might wrap around and mistakenly think that the copy is OK when it's not. Such a copy would probably crash, but just doing the arithmetic in a more simple way lets us detect and refuse this case. Signed-off-by: Matthew Wilcox (Oracle) --- mm/usercopy.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mm/usercopy.c b/mm/usercopy.c index 31deee7dd2f5..ff16083cf1c8 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -162,20 +162,18 @@ static inline void check_heap_object(const void *ptr, unsigned long n, bool to_user) { uintptr_t addr = (uintptr_t)ptr; + unsigned long offset; struct folio *folio; if (is_kmap_addr(ptr)) { - unsigned long page_end = addr | (PAGE_SIZE - 1); - - if (addr + n - 1 > page_end) - usercopy_abort("kmap", NULL, to_user, - offset_in_page(ptr), n); + offset = offset_in_page(ptr); + if (n > PAGE_SIZE - offset) + usercopy_abort("kmap", NULL, to_user, offset, n); return; } if (is_vmalloc_addr(ptr)) { struct vmap_area *area = find_vmap_area(addr); - unsigned long offset; if (!area) { usercopy_abort("vmalloc", "no area", to_user, 0, n); @@ -184,9 +182,10 @@ static inline void check_heap_object(const void *ptr, unsigned long n, /* XXX: We should also abort for free vmap_areas */ - offset = addr - area->va_start; - if (addr + n > area->va_end) + if (n > area->va_end - addr) { + offset = addr - area->va_start; usercopy_abort("vmalloc", NULL, to_user, offset, n); + } return; } @@ -199,8 +198,8 @@ static inline void check_heap_object(const void *ptr, unsigned long n, /* Check slab allocator for flags and size. */ __check_heap_object(ptr, n, folio_slab(folio), to_user); } else if (folio_test_large(folio)) { - unsigned long offset = ptr - folio_address(folio); - if (offset + n > folio_size(folio)) + offset = ptr - folio_address(folio); + if (n > folio_size(folio) - offset) usercopy_abort("page alloc", NULL, to_user, offset, n); } }