Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755296AbdDDWl0 (ORCPT ); Tue, 4 Apr 2017 18:41:26 -0400 Received: from mail-ua0-f175.google.com ([209.85.217.175]:35207 "EHLO mail-ua0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754147AbdDDWlY (ORCPT ); Tue, 4 Apr 2017 18:41:24 -0400 MIME-Version: 1.0 In-Reply-To: References: <1491295954-124682-1-git-send-email-chao.p.peng@linux.intel.com> From: Andy Lutomirski Date: Tue, 4 Apr 2017 15:41:02 -0700 Message-ID: Subject: Re: [PATCH v2] x86/boot: Support uncompressed kernel To: Kees Cook Cc: Chao Peng , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "x86@kernel.org" , Michal Marek , Yinghai Lu , Baoquan He , Jiri Kosina , "H.J. Lu" , Paul Bolle , Masahiro Yamada , Borislav Petkov , Andrew Morton , Arnd Bergmann , Petr Mladek , "David S. Miller" , "Paul E. McKenney" , Andy Lutomirski , Thomas Garnier , Nicolas Pitre , Tejun Heo , Daniel Mack , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , LKML , linux-kbuild Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1456 Lines: 29 On Tue, Apr 4, 2017 at 12:14 PM, Kees Cook wrote: > On Tue, Apr 4, 2017 at 1:52 AM, Chao Peng wrote: >> - memmove(dest, output + phdr->p_offset, phdr->p_filesz); >> + memmove(dest, buf + phdr->p_offset, phdr->p_filesz); > > With the renaming from "buf" to "input" this becomes much more > comprehensible: the PT_LOAD segments from "input" are loaded into > "output". However, does this mean that the RAW load uses parse_elf to > move the kernel into place? Does this happen safely? If it's already > safe, shouldn't we not need "input" at all, and leave this as-is, like > how the decompressed kernel operate? This is a bit of a brain dump of what I learned when I played with this code a couple weeks ago: parse_elf() is incredibly fragile. It seems to work correct in two cases: 1. We're randomizing. As far as I can tell, it just works. 2. We get lucky and all the memmoves are backwards. The first segment overwrites the headers, the second segment may overwrite the first, etc. This also requires that the (missing, ahem) memsets to zero out the ends of segments don't overwrite anything. Of course, nothing accounts for them now and adding the missing memset breaks the boot. #2 is very fragile, needless to say. I'd love to see the code that sets up the decompressor figure out much wiggle room is actually needed and allocate accordingly.