Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1049876ybp; Thu, 17 Oct 2019 07:21:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqwvuzjxAa3YI+okxjgTfPEXuN6DqyaJ85EHcNsg5daH7+NptXrKLcm7U9u+VVcrK7IAOnee X-Received: by 2002:aa7:df0b:: with SMTP id c11mr4072489edy.101.1571322075866; Thu, 17 Oct 2019 07:21:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571322075; cv=none; d=google.com; s=arc-20160816; b=ZpjBkJLbjj154+Ku0LeJtdjnSiQgwp2boEzZbK4LdhEqOSuIENvojj/4X1a2/IL3uc S6cNvLD1bZnxpKAu0fIXgjLURnyWDYiNiFLmWa33VUmk0MyRuCaGdu37RHUcqbf1Wu4X JH6C2djB4a+GD7c/WuhtFsRj8ktkav0khwaOPpmsrEs+0E8KrSOYbjFyzdHGLhGz6MMz SxnsxFCOiQt5Mxl0YtFTVa6V2nZDtj3cE9mCYWaQAgN3imaRu6OXC/tc+U8BSZ9zq+yf RQMMkj4iy4oSfIu6h/itIU6HiFpFGvvvYsJHbxKqRa+ncYBJfzqI01ivHJbCbhVvnr1T St3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TL9C+nmqqmZGt9gKjyV4ss1nrjcPdHX50g6UvBW4jPA=; b=RiAsrzuOXvk7ibnu4Qnz92AB6/85lbLRsHZVopEfLj7S7USmHtaUVneST4Rz4BTYN2 sMEnT63KmHFq/BaTPC3HbcVELzd5uHhBAEjh1KdRGaLRv6rX9ToGucxALyARJ6gxgpvr DHiilMgUUBnSM8A95g6Y3CNx7tqDNbcvLLUm8ksRGCYGg0wyhq3Mazo/Akai0+xpBhTH YZWni9IOmw/Ug5ZPh3PsVkcm1DH/Qjt53TcfzuwCAaDTYkMGCVpuX/lWtdf3gl3gS4Pd dpOWTtEYQvQ2/tbpDYeLh/T+9d5hbk6Na45QXRPZAKejhqeeZKJhjJ74dIc7yxv8JdOU 60KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gcADDCF7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m17si1596303ejn.237.2019.10.17.07.20.52; Thu, 17 Oct 2019 07:21:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gcADDCF7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2395129AbfJPVy7 (ORCPT + 99 others); Wed, 16 Oct 2019 17:54:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:45066 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2395090AbfJPVyy (ORCPT ); Wed, 16 Oct 2019 17:54:54 -0400 Received: from localhost (unknown [192.55.54.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8464521925; Wed, 16 Oct 2019 21:54:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571262893; bh=Sa+6Pf0T69eMRUz9sXVo9A+EjgsvDu3rNexnocYZWFs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gcADDCF7Uu/LVdhsodOEcs9btM9IKp/3fXIfZgjDe3nKlHdThIQz8zHUZ6VWkCnJV vZAWby1E9PPJHPNN1knKH8iQUwjeBqPMUjZ1XSQESZJZ1hB6WYoR3rG6qW4cEvpMPn tEUjkmVMEEPHuWPKo1Q8AelPF3Ti2fsaLivzmhys= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Randy Dunlap , Matthew Wilcox , Kees Cook Subject: [PATCH 4.9 10/92] usercopy: Avoid HIGHMEM pfn warning Date: Wed, 16 Oct 2019 14:49:43 -0700 Message-Id: <20191016214806.748467068@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016214759.600329427@linuxfoundation.org> References: <20191016214759.600329427@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook commit 314eed30ede02fa925990f535652254b5bad6b65 upstream. When running on a system with >512MB RAM with a 32-bit kernel built with: CONFIG_DEBUG_VIRTUAL=y CONFIG_HIGHMEM=y CONFIG_HARDENED_USERCOPY=y all execve()s will fail due to argv copying into kmap()ed pages, and on usercopy checking the calls ultimately of virt_to_page() will be looking for "bad" kmap (highmem) pointers due to CONFIG_DEBUG_VIRTUAL=y: ------------[ cut here ]------------ kernel BUG at ../arch/x86/mm/physaddr.c:83! invalid opcode: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.3.0-rc8 #6 Hardware name: Dell Inc. Inspiron 1318/0C236D, BIOS A04 01/15/2009 EIP: __phys_addr+0xaf/0x100 ... Call Trace: __check_object_size+0xaf/0x3c0 ? __might_sleep+0x80/0xa0 copy_strings+0x1c2/0x370 copy_strings_kernel+0x2b/0x40 __do_execve_file+0x4ca/0x810 ? kmem_cache_alloc+0x1c7/0x370 do_execve+0x1b/0x20 ... The check is from arch/x86/mm/physaddr.c: VIRTUAL_BUG_ON((phys_addr >> PAGE_SHIFT) > max_low_pfn); Due to the kmap() in fs/exec.c: kaddr = kmap(kmapped_page); ... if (copy_from_user(kaddr+offset, str, bytes_to_copy)) ... Now we can fetch the correct page to avoid the pfn check. In both cases, hardened usercopy will need to walk the page-span checker (if enabled) to do sanity checking. Reported-by: Randy Dunlap Tested-by: Randy Dunlap Fixes: f5509cc18daa ("mm: Hardened usercopy") Cc: Matthew Wilcox Cc: stable@vger.kernel.org Signed-off-by: Kees Cook Reviewed-by: Matthew Wilcox (Oracle) Link: https://lore.kernel.org/r/201909171056.7F2FFD17@keescook Signed-off-by: Greg Kroah-Hartman --- mm/usercopy.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -15,6 +15,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#include #include #include @@ -217,7 +218,12 @@ static inline const char *check_heap_obj if (!virt_addr_valid(ptr)) return NULL; - page = virt_to_head_page(ptr); + /* + * When CONFIG_HIGHMEM=y, kmap_to_page() will give either the + * highmem page or fallback to virt_to_page(). The following + * is effectively a highmem-aware virt_to_head_page(). + */ + page = compound_head(kmap_to_page((void *)ptr)); /* Check slab allocator for flags and size. */ if (PageSlab(page))