Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp954252rwb; Mon, 26 Sep 2022 07:54:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5zWu2i8n0AQNM0IbzIjMlBemcXCkx1Wvomubj/cPD8vRwwMinDt45CmZLLh5Qxfe02Hru7 X-Received: by 2002:a17:902:a411:b0:178:9e4c:2ef8 with SMTP id p17-20020a170902a41100b001789e4c2ef8mr22399895plq.95.1664204082900; Mon, 26 Sep 2022 07:54:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664204082; cv=none; d=google.com; s=arc-20160816; b=Mbe+BpdK3O5UDx5axuTiQf632ygGfIngEpJ7umcGOpqVY7YkaCJGmPN45E4wRJ5LpV f7LjK9DmJTR4eegIU3QKuBpdFr3z4WAIcRitob7Gl6XuJ3/KifoXhuBxuRz+bi5yPPRm 9MLjbcJNxvQAVyflKweunU0A7WKKG/zLYLlK0sA1iqW3+/wXvWzbsFuVnZVBBp+yuRKV wJWPPhXiyllRtZieq93bpgjaD4ElW74ox2V0tUIDjdJtN0uN7t86waf7Go40qrBNL8Nh qsW9TrNZZd+OfsT9WI0JOeORUjlwu3ALjxbVkJSHA082K+zV2RVpFFonJGr5XmS1V1xF VJjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=eR2+vCt5rbu8oFVMMRzFPsYJE450ddjyrWAqQ3X2XMU=; b=me1BveUKh3hB0I+2yxJatHnpoV8maZyTMxW0XZ624Rl8/nDjJX7Xg/oi+PP9Yp3trN Gj1+O8xgoTPJhTLvHdaYZPgiOtPtf/MZ71wExpOVq65sE3zkOapyYTQTrrvtasr4AbZS 6cgZD8bG876X/SweDvTB6AQsIonrx1NpTkJFNxNn0Go2xorngaAzjQZrwcxUwIg9NoEl lB5BBEzQqI3CFa5aVAt+gFRP33FyPYHPG9qcY+1gl/wk/1bxzK0z5pBV/ZuDrGe3k4iP QGsRZ1lfBV587yvGLBXpyP2EJ9+QKTKtQ1tQJGQNcfaio5hqfBLDMbGbZfXtVqkd5FD5 c05A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=MwWiARNJ; 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 r8-20020a632b08000000b00434e31807f6si18200066pgr.676.2022.09.26.07.54.01; Mon, 26 Sep 2022 07:54:42 -0700 (PDT) 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=MwWiARNJ; 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 S234931AbiIZOmh (ORCPT + 99 others); Mon, 26 Sep 2022 10:42:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235164AbiIZOlz (ORCPT ); Mon, 26 Sep 2022 10:41:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3451642AF5 for ; Mon, 26 Sep 2022 06:04:29 -0700 (PDT) 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 B1A3160CF4 for ; Mon, 26 Sep 2022 13:04:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB93CC43470 for ; Mon, 26 Sep 2022 13:04:27 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="MwWiARNJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1664197464; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eR2+vCt5rbu8oFVMMRzFPsYJE450ddjyrWAqQ3X2XMU=; b=MwWiARNJk+ubCmBPxambBqKQrEfaD7haj27+m07FBb1ztWPVbW31oxH3PT02U15qKHuzEX SWwE68cQvH8TPin4gdfCK7Zq8kEBY3hOkL8qB4MpB/LvCnYznXt/wQJZZ09xvN+uXlqd0Z yQdU/b2Rn6prBrz+SNWmDdmGyYCG5ic= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 148b2e38 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Mon, 26 Sep 2022 13:04:24 +0000 (UTC) Received: by mail-vs1-f41.google.com with SMTP id j7so6408190vsr.13 for ; Mon, 26 Sep 2022 06:04:24 -0700 (PDT) X-Gm-Message-State: ACrzQf26v/1u7lzxHxY6uKKQZH7JtiAkcoo0EFn36d24JgI+q1ydDs5K QU+d7wXpULuudWu7CWtHAkXo8UqJBzyXqW6UPlI= X-Received: by 2002:a05:6102:2908:b0:398:ac40:d352 with SMTP id cz8-20020a056102290800b00398ac40d352mr6808337vsb.55.1664197463461; Mon, 26 Sep 2022 06:04:23 -0700 (PDT) MIME-Version: 1.0 References: <20220921185208.3549140-1-Jason@zx2c4.com> <20220921185208.3549140-2-Jason@zx2c4.com> In-Reply-To: From: "Jason A. Donenfeld" Date: Mon, 26 Sep 2022 15:04:12 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] m68k: virt: generate new RNG seed on reboot To: Laurent Vivier Cc: Geert Uytterhoeven , linux-m68k@lists.linux-m68k.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 Mon, Sep 26, 2022 at 3:02 PM Laurent Vivier wrote: > > Le 26/09/2022 =C3=A0 14:56, Jason A. Donenfeld a =C3=A9crit : > > Hi Laurent, > > > > On Mon, Sep 26, 2022 at 2:52 PM Laurent Vivier wrot= e: > >> > >> Hi Jason, > >> > >> Le 26/09/2022 =C3=A0 14:02, Jason A. Donenfeld a =C3=A9crit : > >>> Hi Laurent, > >>> > >>> On Fri, Sep 23, 2022 at 3:10 PM Laurent Vivier wr= ote: > >>>> > >>>> Le 23/09/2022 =C3=A0 14:50, Geert Uytterhoeven a =C3=A9crit : > >>>>> Hi Jason, > >>>>> > >>>>> On Fri, Sep 23, 2022 at 2:26 PM Jason A. Donenfeld wrote: > >>>>>> On Fri, Sep 23, 2022 at 2:23 PM Geert Uytterhoeven wrote: > >>>>>>>>>> + if (rng_seed_record && rng_seed_record->size > sizeof(= *rng_seed_record) + 2) { > >>>>>>>>>> + u16 len =3D rng_seed_record->size - sizeof(*rn= g_seed_record) - 2; > >>>>>>>>>> + get_random_bytes((u8 *)rng_seed_record->data += 2, len); > >>>>>>>>>> + *(u16 *)rng_seed_record->data =3D len; > >>>>>>> > >>>>>>> Storing the length should use the proper cpu_to_be16 accessor. > >>>>>> > >>>>>> Okay, I'll do that for v2. > >>>>>> > >>>>>> (Simply out of curiosity, why? Isn't m68k always big endian and th= is > >>>>>> is arch/ code?) > >>>>> > >>>>> Yes it is. But virt_parse_bootinfo() below already uses the right > >>>>> accessor. > >>>>> > >>>>> BTW, I guess people thought the same about PowerPC? > >>>>> Although I agree the probability of someone creating a little-endia= n > >>>>> m68k clone in an FPGA or SkyWater project and trying to run Linux o= n > >>>>> it quite low ;-) > >>>>> > >>>>>>>> The way I tested this is by having my initramfs just call > >>>>>>>> `reboot(RB_AUTOBOOT);`, and having add_bootloader_randomness() p= rint > >>>>>>>> its contents to the console. I checked that it was both present = and > >>>>>>>> different every time. > >>>>>>> > >>>>>>> Are you sure the new kernel did receive the same randomness as pr= epared > >>>>>>> by get_random_bytes()? I would expect it to just reboot into qemu= , > >>>>>>> reload the kernel from disk, and recreate a new bootinfo from scr= atch, > >>>>>>> including generating a new random seed. > >>>>>> > >>>>>> Yes I'm sure. Without this patch, the new kernel sees the zeroed s= tate. > >>>>> > >>>>> That's interesting. So QEMU preserves the old bootinfo, which is > >>>>> AFAIK not guaranteed to be still available (that's why I added > >>>>> save_bootinfo()). Perhaps that works because only memory starting > >>>>> from a rounded-up value of _end will be used, and you're just lucky= ? > >>>>> I'm wondering what else it preserves. It sure has to reload the > >>>>> kernel image, as at least the data section will no longer contain t= he > >>>>> initialization values after a reboot... > >>>>> > >>>>> Laurent? > >>>>> > >>>> > >>>> In QEMU the loader makes a copy of the kernel and the initrd and thi= s copy is restored on a reset. > >>>> > >>>> I don't think there is a mechanism in QEMU to save the BOOTINFO sect= ion, so I think it works by > >>>> luck. I will check. > >>>> > >>>> Thanks, > >>>> Laurent > >>> > >>> Are you sure about that? Or at least, could you point me to where you > >>> think this happens? I'm not as familiar as you with this code base, > >>> but I really am not seeing it. So far as I can tell, on reset, the pc > >>> and stack are reset to their initial places, after TCG resets the cpu > >>> registers to a known state. But the kernel is not reloaded. The same > >>> thing that was in memory before is used again. > >> > >> Yes, this is not clear in QEMU but I think this happens in rom_reset()= : > >> > >> hw/core/loader.c > >> > >> 1180 if (rom->mr) { > >> 1181 void *host =3D memory_region_get_ram_ptr(rom->mr); > >> 1182 memcpy(host, rom->data, rom->datasize); > >> 1183 memset(host + rom->datasize, 0, rom->romsize - rom->d= atasize); > >> 1184 } else { > >> 1185 address_space_write_rom(rom->as, rom->addr, MEMTXATTR= S_UNSPECIFIED, > >> 1186 rom->data, rom->datasize); > >> 1187 address_space_set(rom->as, rom->addr + rom->datasize,= 0, > >> 1188 rom->romsize - rom->datasize, > >> 1189 MEMTXATTRS_UNSPECIFIED); > >> 1190 } > >> > >> kernel and initrd are loaded with load_elf() and load_image_targphys()= only once at startup by the > >> machine init function (virt_init()). > >> > >> rom_add_elf_program() adds the kernel to the ROM list > >> (in include/hw/elf_ops.h, glue(load_elf, SZ) that generates load_elf32= () when SZ is 32...) > >> > >> rom_add_file() adds the initrd to the ROM list too. > >> > >> And ROMs are restored on reset from these copies by rom_reset(). > >> > >> rom_reset() is registered as a reset handler with qemu_register_reset(= ) by > >> rom_check_and_register_reset() at the end of the machine creation by q= dev_machine_creation_done(). > >> > >> So I think bootinfo are not restored because there is no such function= calls. Perhaps they are saved > >> and restaured if they are stored in address space of one of the previo= us registered ROM. > > > > Ahh interesting, thanks for the explanation. > > > > So from my debugging, bootinfo is *not* restored, and the previous one > > appears to be used. Fortunately it's intact and everything works well > > on a reboot. > > > > With that in mind, we now we have to decide whether to: > > A) Go with my linux patch to write the rng seed before rebooting (3/3 > > in v4 of that series). > > B) Not go with the linux patch, but instead make sure bootinfo is > > restored to its previous value, and then also register a qemu reboot > > notifier to refresh the seed in it, like what x86 does. > > > > I prefer B :) > It's cleaner and under QEMU control. Okay. I'm happy to follow your preference. Just one last question, though: is this what happens on baremetal bootloaders too? Or does no such thing really exist so it doesn't matter? Jason