Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1877829rwl; Thu, 30 Mar 2023 02:48:11 -0700 (PDT) X-Google-Smtp-Source: AKy350bQk+fX0gFffdwEv6dXJG+9aV26qVDZkYHLHTFgI0xHCns1GYqW1JktvfbyHHyLEmqpYK6S X-Received: by 2002:a05:6402:2803:b0:502:e50:3358 with SMTP id h3-20020a056402280300b005020e503358mr1980385ede.3.1680169690836; Thu, 30 Mar 2023 02:48:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680169690; cv=none; d=google.com; s=arc-20160816; b=kEs4jgTEUqEsPZy7qk47XOFF+l60uCGt42kHt4RIpi5OuQmYomD7WgmpYzjYxqPiWc H0RRIm8bYKrvDNBCCnDAtfITyKkuKpf1m/Ju8arF7Rek34vDFYc4uve9tMrkoO7+4G3K i+nYBGUKXCmjX/Zf1AcYQNDjapHXN3uPgjaPPAeMi3ir9d0u6dnFyGBYhBKqmHsICO0Z zg0hTIcM5vCcKsx/3pczGZMoCn6FNxjBZBYuMdxd1kC1ipHOWcOmX/J6VDNqlDwxzepG QtF92fXOQP/YTfaZdckZdqVmWhwKjBW/0g/QugOJPhK7V9AcEze6Lr7GphpECsGe2+e2 THsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=P0ZT4tLV4pwIFoJoCyH/LTIscOWlu6sttcJkFj6RAJg=; b=YneT/00uVzedA0NcFyKlcZkp3QvRCGYTKTXg4UOMbPmqDOslHCCBzm+uZP50X7pqGL iYyCFVjcxHs8srhwWJ3Ue4CQERPR72SwuG+yUy4cMul9EeWGweObEtbVHHynhXSZN1+c 2lwgiCTOGeLLd0IH2zQN5BBtov8UFxat71+drtLjuNCsq1Uq1G2XMWuejS326mZv9i2r XVroSF6RKNRBXVFfos60GGUfSFx1EWEFp4pKMnWygXI4OL3atxFrnQigLPCi9Ln0ucMN KotYHQYYQBN46XJ2eH2WWejv48oiQItLP+Ge40nhD//bb/QzpDVWcsepZtAPC9uNGxUh gGWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MKhXbw6K; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k11-20020aa7d8cb000000b004ad05f8a5cbsi34734601eds.266.2023.03.30.02.47.45; Thu, 30 Mar 2023 02:48:10 -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=@chromium.org header.s=google header.b=MKhXbw6K; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230368AbjC3JqD (ORCPT + 99 others); Thu, 30 Mar 2023 05:46:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230370AbjC3Jpl (ORCPT ); Thu, 30 Mar 2023 05:45:41 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D788C8A4E for ; Thu, 30 Mar 2023 02:45:17 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id eh3so74019505edb.11 for ; Thu, 30 Mar 2023 02:45:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680169514; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=P0ZT4tLV4pwIFoJoCyH/LTIscOWlu6sttcJkFj6RAJg=; b=MKhXbw6KldDmbyijGdW2H7JuSKi026ZosGMSIpAUYa7SUhoSNajx2CAwoSK78pgnXv 9F1Q9tXqHnihud+Axk0aABNgz4iHDjH0mabEzb78y0ER0Y9LmD+SwmoeJePo8qY4ZTp+ nu/4byyaZZi3pZZV96rCwuaexyXd2rOqeDjNg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680169514; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=P0ZT4tLV4pwIFoJoCyH/LTIscOWlu6sttcJkFj6RAJg=; b=0uHYX2ljEFY520kiOybAbtrWiE5eLqG1xa2sGVIDGbCb9TT3uhTm6NAfvWKB8hE/rK h51ZtgllOtvCRa+foQDQ2DIPCb34BtOdP/Zwe3F62juFN1BDSBPfIobtAw3H6uHNEap8 Od3okKlykPPCCnY2y5uClhTOrnr1F+qDEOzdg4Et2UzyJm+UeLggBzpaGeF+hYvaZdsE opLOWknB+7ifJxjcsUgTXdeFu82pR8yMxaZKDtBmFq4WphHFCgANwgXANppLuURtXmsp mMrfYmH17Euugr0Ei902bi0MBa8vcJ0dpT4Ur3MlzVdrv2viZV9gdv9O9qkt+AqrGnXe 1jEA== X-Gm-Message-State: AAQBX9e0ydnYZ3EPzM9dMhzOZVHfU9S4I80M5PDvPZy9sebPQB/X6jJT xDxS2Z/ZyT8fMejXhB6vDby5Pg== X-Received: by 2002:a17:906:144a:b0:944:394:93f7 with SMTP id q10-20020a170906144a00b00944039493f7mr16259171ejc.61.1680169514692; Thu, 30 Mar 2023 02:45:14 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:1396:ff5d:6e2a:df6d]) by smtp.gmail.com with ESMTPSA id k9-20020a17090666c900b0092b606cb803sm17683616ejp.140.2023.03.30.02.45.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 02:45:14 -0700 (PDT) From: Ricardo Ribalda Date: Thu, 30 Mar 2023 11:44:47 +0200 Subject: [PATCH v5 1/2] kexec: Support purgatories with .text.hot sections MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230321-kexec_clang16-v5-1-5563bf7c4173@chromium.org> References: <20230321-kexec_clang16-v5-0-5563bf7c4173@chromium.org> In-Reply-To: <20230321-kexec_clang16-v5-0-5563bf7c4173@chromium.org> To: Eric Biederman Cc: Baoquan He , Philipp Rudo , kexec@lists.infradead.org, linux-kernel@vger.kernel.org, Ross Zwisler , Steven Rostedt , Simon Horman , Ricardo Ribalda , stable@vger.kernel.org X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2485; i=ribalda@chromium.org; h=from:subject:message-id; bh=l/5U4QEZ8HewezM0SKo7DhSrv7EPFo5/mMqWDXwDLoo=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkJVofL0ZHdw3ddeiqI0n8LoN5gHxlFKFxXyM1S urlJaHanIOJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZCVaHwAKCRDRN9E+zzrE iBg1D/91C6RHT9QE0cVG0e+v65DsVYnvrTBce1q5oPhLB+VmdQz1sThWgK+n/xYoYX9kPacvf/z 6IDhkaLhHIQUVpdlui544DRnz6ShOt/8I3rM8gWQ4rD8jSJ52Qgc6Jd4CZu9gphgilDDor3kf+U gELhcxQkcHt+WlPPSiUYJ1cQw2LJWGMJiK1Pdosh6xEMM/lIz3zvuRTZz1ww/UhJ6CRQ5R9wyi8 6N8DggUWF8LDooOKLv1tDJptKXtOiyTqCrjjFUQ8oqK3DEcSsy3lvHTg42bJFqLanTknUJrsCU0 f5OLuScX7v2fZKP6t/RzVbNJbNoMBc1BWVYsoqieeMxwHnEBeCzL/nhDjCgsawICPSJbkkbNH4A PIBr5S7JcxPJ+AAK1wDC7gSiv80UNRE093oL2GFWo6D4osNDiqWDAPGtEv25dLIqMpQ6YbbkCYa iM5Opq0IJXCKm+Zd8jJQO8/PwXnR5TgHsf4OeYYUAm45oEyBEr5F9XYgR9mvDLbGKMCY1wmRN8t 2RB1yHeR6VtZpkUdsFQfEfqD8w5jjh/qGLn77FIjPqh49Y64YZH0pdqscm1wpaHQYpJLFphTt/E PMV5yLjcLpgHIPop1pIp8gYl4cnmsm8T2+dAUcp1Jfj9CPUk6EenylzOoGoT+nk4Rq/fIP3CYL8 dgSeZj8HENys/Ww== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 Clang16 links the purgatory text in two sections: [ 1] .text PROGBITS 0000000000000000 00000040 00000000000011a1 0000000000000000 AX 0 0 16 [ 2] .rela.text RELA 0000000000000000 00003498 0000000000000648 0000000000000018 I 24 1 8 ... [17] .text.hot. PROGBITS 0000000000000000 00003220 000000000000020b 0000000000000000 AX 0 0 1 [18] .rela.text.hot. RELA 0000000000000000 00004428 0000000000000078 0000000000000018 I 24 17 8 And both of them have their range [sh_addr ... sh_addr+sh_size] on the area pointed by `e_entry`. This causes that image->start is calculated twice, once for .text and another time for .text.hot. The second calculation leaves image->start in a random location. Because of this, the system crashes immediately after: kexec_core: Starting new kernel Cc: stable@vger.kernel.org Fixes: 930457057abe ("kernel/kexec_file.c: split up __kexec_load_puragory") Reviewed-by: Ross Zwisler Signed-off-by: Ricardo Ribalda --- kernel/kexec_file.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index f1a0e4e3fb5c..c7a0e51a6d87 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -901,10 +901,22 @@ static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi, } offset = ALIGN(offset, align); + + /* + * Check if the segment contains the entry point, if so, + * calculate the value of image->start based on it. + * If the compiler has produced more than one .text section + * (Eg: .text.hot), they are generally after the main .text + * section, and they shall not be used to calculate + * image->start. So do not re-calculate image->start if it + * is not set to the initial value, and warn the user so they + * have a chance to fix their purgatory's linker script. + */ if (sechdrs[i].sh_flags & SHF_EXECINSTR && pi->ehdr->e_entry >= sechdrs[i].sh_addr && pi->ehdr->e_entry < (sechdrs[i].sh_addr - + sechdrs[i].sh_size)) { + + sechdrs[i].sh_size) && + !WARN_ON(kbuf->image->start != pi->ehdr->e_entry)) { kbuf->image->start -= sechdrs[i].sh_addr; kbuf->image->start += kbuf->mem + offset; } -- 2.40.0.348.gf938b09366-goog