Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp7503938rwl; Fri, 30 Dec 2022 09:23:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXuxuCFxFKKhVacFI5OU+BzhsZnTTVjcEtsMWoCF5xmbRlzf8YPjVcQa0Bu2UmvA3+w4a1fD X-Received: by 2002:a17:906:260e:b0:7c1:9046:878a with SMTP id h14-20020a170906260e00b007c19046878amr27993614ejc.38.1672421007042; Fri, 30 Dec 2022 09:23:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672421007; cv=none; d=google.com; s=arc-20160816; b=wpAH5KkDQQ1j2mUqHH2IjtcD6b7J0FNqt7LfAPC6v/kzSBKKtGaPg929963jE9SWAU 8d5Kc9Szw8ES057jlaw9iM/uqWFmKlPA947gr5RWDICMGR7qHdBBSEUaN+Bv//h+ieTd q5K1NTfdEG1+KJIkTDeykklKETa4Y5OWWYMUMcJ3+yUhUAvtAVsmcXPQpbKG38tVHX5k CYHtpHgRWWESfn6hf6vjoJgR6MErLsrrkVrH5IUtsmOEK2FGumM8CY45SNL4O5tgNYmV 0+ht1uAlz/Qm9sJXOpNVJgy/L9E7AYixQdjLjYcXdvNP9YRSQhrtRnM/mWddXM52knz8 83xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WjxzHUW+ATNpAsgtDaf/I/iJFdkXQhViNn4fOLIII1s=; b=lneANnUf5llq3EpZPxyRySWB0yp6Zw9JCQvXV+nIg2fgc+42XooY4MWmgx/x3OGQ50 4Pdy8cBglIK9Ap2erUCoYL8pJO36sc0qdtHeufb+iBiqyzz/Mb/DGWAQowjbjzEUBTX+ a0XbR/r3PgfWVwulp6wkDuWVt6t7YMNcM3nGORGzzJ76ce400spmuEiB3KhZLDIIsl6E kB0m/jOsqykbysZemIpcFMGCe/EjLiVKb9zjqsmBmTJr9gcnV3imZGa/o6uqXXwisGYf 6teWl7g/vIaMY4X+Jxwy77U9aEr0XqYZV9fI/MXkzSKPeQH+OxyfKUwwZXs1/QXkhs09 X3/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=lcUDeWf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i8-20020a1709064fc800b007adb388df38si18949473ejw.706.2022.12.30.09.23.12; Fri, 30 Dec 2022 09:23:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=lcUDeWf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235164AbiL3RHr (ORCPT + 63 others); Fri, 30 Dec 2022 12:07:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbiL3RHo (ORCPT ); Fri, 30 Dec 2022 12:07:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0587B60DB for ; Fri, 30 Dec 2022 09:07:43 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9424A61B13 for ; Fri, 30 Dec 2022 17:07:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA8AEC43392 for ; Fri, 30 Dec 2022 17:07:41 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="lcUDeWf4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1672420057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WjxzHUW+ATNpAsgtDaf/I/iJFdkXQhViNn4fOLIII1s=; b=lcUDeWf4lKIHAVVyjoGjO8ijI+NNWVxDI27VVjPfGAWwA+LQVx1vyNOwDB8IEG7068c3W4 pGlYYCgvyZ7/3H3uOuceF2NqW1fXnyblaeuM989wZQoLbpb9PEq6RG2KpHw6S1TGvQinm9 /6hrWRTQO5i5EZCxX3Nm7e03tVlMgPU= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 10e78d47 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 30 Dec 2022 17:07:37 +0000 (UTC) Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-466c5fb1c39so291828737b3.10 for ; Fri, 30 Dec 2022 09:07:36 -0800 (PST) X-Gm-Message-State: AFqh2kpqkwR5oNAy6VwhRuncgmfJc32CLWcJxoHUlgRL0y8L2Sfxzw2d Mbv/1Si9UBX99JvWf2H3RxLNsHgTfSL+gF7CLZs= X-Received: by 2002:a05:690c:a83:b0:36f:de9a:3229 with SMTP id ci3-20020a05690c0a8300b0036fde9a3229mr3490284ywb.143.1672420055863; Fri, 30 Dec 2022 09:07:35 -0800 (PST) MIME-Version: 1.0 References: <20221228143831.396245-1-Jason@zx2c4.com> <6cab26b5-06ae-468d-ac79-ecdecb86ef07@linaro.org> <9188EEE9-2759-4389-B39E-0FEBBA3FA57D@zytor.com> In-Reply-To: From: "Jason A. Donenfeld" Date: Fri, 30 Dec 2022 18:07:24 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH qemu] x86: don't let decompressed kernel image clobber setup_data To: Borislav Petkov Cc: "H. Peter Anvin" , pbonzini@redhat.com, ebiggers@kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, qemu-devel@nongnu.org, ardb@kernel.org, kraxel@redhat.com, philmd@linaro.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Dec 30, 2022 at 6:01 PM Borislav Petkov wrote: > > On Fri, Dec 30, 2022 at 04:54:27PM +0100, Jason A. Donenfeld wrote: > > > Right, with CONFIG_X86_VERBOSE_BOOTUP=y in a guest here, it says: > > > > > > early console in extract_kernel > > > input_data: 0x000000000be073a8 > > > input_len: 0x00000000008cfc43 > > > output: 0x0000000001000000 > > > output_len: 0x000000000b600a98 > > > kernel_total_size: 0x000000000ac26000 > > > needed_size: 0x000000000b800000 > > > trampoline_32bit: 0x000000000009d000 > > > > > > so that's a ~9M kernel which gets decompressed at 0x1000000 and the > > > output len is, what, ~180M which looks like plenty to me... > > > > I think you might have misunderstood the thread. > > Maybe... > > I've been trying to make sense of all the decompression dancing we're doing and > the diagrams you've drawn and there's a comment over extract_kernel() which > basically says that the compressed image is at the back of the memory buffer > > input_data: 0x000000000be073a8 > > and we decompress to a smaller address > > output: 0x0000000001000000 > > And, it *looks* like setup_data is at an address somewhere >= 0x1000000 so when > we start decompressing, we overwrite it. > > I guess extract_kernel() could also dump the setup_data addresses so that we can > verify that... > > > First, to reproduce the bug that this patch fixes, you need a kernel with a > > compressed size of around 16 megs, not 9. > > Not only that - you need setup_data to be placed somewhere just so that it gets > overwritten during decompression. Also, see below. > > > Secondly, that crash is well understood and doesn't need to be reproduced; > > Is it? > > Where do you get the 0x100000 as the starting address of the kernel image? > > Because input_data above says that the input address is somewhere higher... Look closer at the boot process. The compressed image is initially at 0x100000, but it gets relocated to a safer area at the end of startup_64: /* * Copy the compressed kernel to the end of our buffer * where decompression in place becomes safe. */ pushq %rsi leaq (_bss-8)(%rip), %rsi leaq rva(_bss-8)(%rbx), %rdi movl $(_bss - startup_32), %ecx shrl $3, %ecx std rep movsq cld popq %rsi /* * The GDT may get overwritten either during the copy we just did or * during extract_kernel below. To avoid any issues, repoint the GDTR * to the new copy of the GDT. */ leaq rva(gdt64)(%rbx), %rax leaq rva(gdt)(%rbx), %rdx movq %rdx, 2(%rax) lgdt (%rax) /* * Jump to the relocated address. */ leaq rva(.Lrelocated)(%rbx), %rax jmp *%rax And that address winds up being calculated with a combination of the image size and the init_size param, so it's safe. Decompression won't overwrite the compressed kernel. HOWEVER, qemu currently appends setup_data to the end of the compressed kernel image, and this part isn't moved, and setup_data links aren't walked/relocated. So that means the original address remains, of 0x100000. Jason