From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 514ACC433EF for ; Wed, 6 Oct 2021 03:02:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B3357611CC for ; Wed, 6 Oct 2021 03:02:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B3357611CC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 44F25900002; Tue, 5 Oct 2021 23:02:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 400D16B0071; Tue, 5 Oct 2021 23:02:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C743900002; Tue, 5 Oct 2021 23:02:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id 1C5256B006C for ; Tue, 5 Oct 2021 23:02:11 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DB05F1838DC1F for ; Wed, 6 Oct 2021 03:02:10 +0000 (UTC) X-FDA: 78664513620.26.5A9DBD4 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf23.hostedemail.com (Postfix) with ESMTP id 8B170900105E for ; Wed, 6 Oct 2021 03:02:10 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id y5so764923pll.3 for ; Tue, 05 Oct 2021 20:02:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=S062zvGWe96jQhsQCTIfKj1biRqnGqmVRArKyoc3nfQ=; b=OIzxlT5X5Oe51YZhvkte9UOaK/VbWp7vwT7C7BS0RskOuSPmywNHWrwaeVzELJARi4 vSzAb44nCjO2a24H5FeQKTwsJWkZzqC/b4VBGNvoFkxzqf2UQbfdVzlyUPTdy5RVnRNP AKki2cWjby+EEk1TVeXFeflxXCI5rKO8Yp8Q8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=S062zvGWe96jQhsQCTIfKj1biRqnGqmVRArKyoc3nfQ=; b=dOo2+1OBbrs15LJi7+0PBd3wMWeuuntnofjtuTFrFK/+E/+6aqQGnJVZdhK/q5w+H7 tKYXr9m1fF4+TZDV38VA0CxUX06y89qDaa4ZJbsS60pnPoYZnFsVejC2Eno+Q3U9m9H1 ZxfconBAAdLpn9AlpoI9jiIn+HmMR5fLUSjVCjpaX4GQ7mvV6t+Butfgo642KpNWaefJ x8ESO4uQwfEBsIYiKd82QrTZTPdLOpXhHKrXSmIzFXGXXlzOHw6i/mLUWHCIpBAu01mv IrurzI5f+MfJKoYs+ke4Al7lJbhJlP3xXOKb3G5bp2J+kcX0RbUUbTzT4grj1aXNjkfb aS9g== X-Gm-Message-State: AOAM532eVXLGBXkjTN1eDBzX0oUCnXHJ7wexm1guTWyi5/tBDz7k0sSl sQzQxkM6v4cR00DAE1N8BIKqMA== X-Google-Smtp-Source: ABdhPJwtKgSsdVZqGrsXA70C0f/aGH2+1aKKFyn+aQHeSpAhgjFReBIj6Q0tfpkT6Zvwl6I+q9IsyA== X-Received: by 2002:a17:90a:a88a:: with SMTP id h10mr7912028pjq.226.1633489329357; Tue, 05 Oct 2021 20:02:09 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id a67sm6277164pfa.128.2021.10.05.20.02.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Oct 2021 20:02:09 -0700 (PDT) Date: Tue, 5 Oct 2021 20:02:08 -0700 From: Kees Cook To: Matthew Wilcox Cc: linux-mm@kvack.org, Thomas Gleixner Subject: Re: [PATCH 2/3] mm/usercopy: Detect vmalloc overruns Message-ID: <202110051958.FC5C68F6@keescook> References: <20211004224224.4137992-1-willy@infradead.org> <20211004224224.4137992-3-willy@infradead.org> <202110051423.9F6F93752@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8B170900105E X-Stat-Signature: kcdjcpjxefc5chfys94ztyx7nhgzrfpq Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=OIzxlT5X; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf23.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.174 as permitted sender) smtp.mailfrom=keescook@chromium.org X-HE-Tag: 1633489330-703116 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: On Wed, Oct 06, 2021 at 02:26:41AM +0100, Matthew Wilcox wrote: > On Tue, Oct 05, 2021 at 02:25:23PM -0700, Kees Cook wrote: > > On Mon, Oct 04, 2021 at 11:42:22PM +0100, Matthew Wilcox (Oracle) wrote: > > > If you have a vmalloc() allocation, or an address from calling vmap(), > > > you cannot overrun the vm_area which describes it, regardless of the > > > size of the underlying allocation. This probably doesn't do much for > > > security because vmalloc comes with guard pages these days, but it > > > prevents usercopy aborts when copying to a vmap() of smaller pages. > > > > > > Signed-off-by: Matthew Wilcox (Oracle) > > > --- > > > mm/usercopy.c | 9 +++++++++ > > > 1 file changed, 9 insertions(+) > > > > > > diff --git a/mm/usercopy.c b/mm/usercopy.c > > > index ac95b22fbbce..7bfc4f9ed1e4 100644 > > > --- a/mm/usercopy.c > > > +++ b/mm/usercopy.c > > > @@ -17,6 +17,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include > > > #include > > > #include > > > @@ -236,6 +237,14 @@ static inline void check_heap_object(const void *ptr, unsigned long n, > > > return; > > > } > > > > > > + if (is_vmalloc_addr(ptr)) { > > > + struct vm_struct *vm = find_vm_area(ptr); > > > + > > > + if (ptr + n > vm->addr + vm->size) > > > + usercopy_abort("vmalloc", NULL, to_user, 0, n); > > > > This "0" is easy to make "ptr - vm->addr". With that fixed: > > > > Acked-by: Kees Cook > > Looking at this again, if we do ... > > char *p = vmalloc(2 * PAGE_SIZE); > copy_from_user(p + 2 * PAGE_SIZE, ...); > > then 'vm' can be NULL. I think. While we can't catch everything, a > NULL pointer dereference here seems a little unfriendly? So how about > this: Oh right, because ptr will be in a guard page (or otherwise unallocated) but within the vmalloc range? > > if (is_vmalloc_addr(ptr)) { > struct vm_struct *vm = find_vm_area(ptr); > unsigned long offset; > > if (!vm) { > usercopy_abort("vmalloc", NULL, to_user, 0, n); > return; > } > > offset = ptr - vm->addr; > if (offset + n > vm->size) > usercopy_abort("vmalloc", NULL, to_user, offset, n); > return; > } > > Do we want to distinguish the two cases somehow? I'd report the first's "details" as "unmapped" or something: usercopy_abort("vmalloc", "unmapped", to_user, 0, n); and the latter is fine as-is. -- Kees Cook