From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05956C5B543 for ; Wed, 4 Jun 2025 15:07:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CDFE6B0171; Wed, 4 Jun 2025 11:07:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 77E566B0578; Wed, 4 Jun 2025 11:07:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 695196B05F6; Wed, 4 Jun 2025 11:07:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 472CB6B0171 for ; Wed, 4 Jun 2025 11:07:48 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E7749161819 for ; Wed, 4 Jun 2025 15:07:47 +0000 (UTC) X-FDA: 83518047774.28.BA8E877 Received: from m.syntacore.com (m.syntacore.com [178.249.69.228]) by imf17.hostedemail.com (Postfix) with ESMTP id 1399540011 for ; Wed, 4 Jun 2025 15:07:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=syntacore.com header.s=m header.b=qFFBj5JE; spf=pass (imf17.hostedemail.com: domain of svetlana.parfenova@syntacore.com designates 178.249.69.228 as permitted sender) smtp.mailfrom=svetlana.parfenova@syntacore.com; dmarc=pass (policy=none) header.from=syntacore.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749049665; h=from:from:sender: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: references:dkim-signature; bh=cFvYaW2WkbtOra3S0g7FSljIzV4LxrpuqkAbvhUZRVI=; b=XPBsGX02QmiNZB10ErITAoLLqmiUAKanTjcYJmRe3IGbJ2pxvy3Cjf3YI5UrhGae05A6Ku OTl9pX8ycHt1K+cS1xSWbChn7Ip3cHcL36jUsgteCkVSg1KzhEfgrl23wIUItQO5wIkoqc OOSDCqb4LtLneJ5KTHMqBx0cYjlBHwA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=syntacore.com header.s=m header.b=qFFBj5JE; spf=pass (imf17.hostedemail.com: domain of svetlana.parfenova@syntacore.com designates 178.249.69.228 as permitted sender) smtp.mailfrom=svetlana.parfenova@syntacore.com; dmarc=pass (policy=none) header.from=syntacore.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749049665; a=rsa-sha256; cv=none; b=zl4nNuKP60v4H4/TSdUDPJwndZPL8XmUu9g2yRykP8l+2Fv54ngElhSGLNG22whxO5QSyv RvOK2phweu5ioDGPjupOdidW1rCV5rJt/KE1suH3riwMD+BigDl5ntApHRqLXLxK6fc2Xq yk05OGYsA6K9jP53mPuD980uEOzzE8c= Received: from MRN-SC-KSMG-01.corp.syntacore.com (localhost [127.0.0.1]) by m.syntacore.com (Postfix) with ESMTP id 386171A0004; Wed, 4 Jun 2025 15:07:43 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 m.syntacore.com 386171A0004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=m; t=1749049663; bh=cFvYaW2WkbtOra3S0g7FSljIzV4LxrpuqkAbvhUZRVI=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=qFFBj5JEyV6NX3EeyzhabNsOKCgp9jJ9AFkzUYTr7lX9o+e4rwwXk5S81XGzUfEi1 LNQDEBsNDZs6a6o1xuli/VYrLGk4xDpdU11Z8sRU9LiqoRrk/sjtVcJDUm3rMlPP6F 6xFw521QT9wRVQVhk1boOFiMCUOHRs1gXCSxK+bMJyLcQ7sGz9hkWl42jKqdkBrIAv IVZpAhBMUcCFAVLRde+a/IgzLNAKMTuWC3JYRG9wJ9wDKHrROMXu95QlNFG6ZyDZ7l GpqCr/eSzO4xhWXg3rxXh6nlXf8urJZZKLfcFQceCqN1gNzeTvzzfyjeLHaKHVYOU5 gAdYVUb0Ohnmg== Received: from S-SC-EXCH-01.corp.syntacore.com (exchange.syntacore.com [10.76.202.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by m.syntacore.com (Postfix) with ESMTPS; Wed, 4 Jun 2025 15:07:41 +0000 (UTC) Received: from localhost (10.199.25.251) by S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 4 Jun 2025 18:06:55 +0300 From: Svetlana Parfenova To: Kees Cook , CC: Svetlana Parfenova , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , , Subject: [RFC] binfmt_elf: preserve original ELF e_flags in core dumps Date: Wed, 4 Jun 2025 22:05:02 +0700 Message-ID: <20250604150502.622178-1-svetlana.parfenova@syntacore.com> X-Mailer: git-send-email 2.39.5 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.199.25.251] X-ClientProxiedBy: S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) To S-SC-EXCH-01.corp.syntacore.com (10.76.202.20) X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.1.8310, bases: 2025/06/04 12:53:00 #27536686 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: NotDetected X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 5 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 1399540011 X-Stat-Signature: bww4c6ewxikdmw6egozcs1e1hku3bcu9 X-Rspam-User: X-HE-Tag: 1749049664-123763 X-HE-Meta: U2FsdGVkX1803QrTcoAVI242EvXzsoviqQZL/iKSDECfxhsVOBg8nId7v8IeaV+NRgZly2yVlfMZapz2TJwDFdpP1oyA7XHdb3SAeu/QhlSCHSpLjNmvtwmGKdCk8y2dgKUFCtN1X9mm/mPWfVYw3wmOq50RC/oZNULc/3ddgYk0HQxY3J5MT7Bsd6LPkS6mIf3Z08vRvEZa9gtCjgMEaYMO2/igv7C7jKei1CG4UAfV+v43qQEsGHxTU8QUc1M2q7zusGpZhfUSkkKNBrrn9qXryb0gUkvMYsUCtHrXlpME9QJUg/lESM1JtFbh6W/FghpmaatBB4Ea3qhmagNOVASjJnbQP6oQcOQOsxDunck5appDbpE30/ZasAekRToJ+RRyTPV5PjlVTlaxDptNXiVn0bCZKG6lj9GveKO+Qizn6c7M/O0omT1tEKDnHhpXGXAtva81/h1MJWl4HRt5ZUrPIHGsS6m4QwGTJ4IrkBpleDVRkO2OJpsvSfUGp68oAUrWeI5jXQ19nklSFUV9kLmnhEur1d6TE2D5MC6vmwUZVglATxnAqvAmPXRbdm/PC92Yep+O04Q4bhJ/w5SPmucAUNs1s4wDkIkNFzmgNcIVaxrEzKaJfkrcqGvZf1Rsk9k6wIUZuERMHL5GJvbXiHjYoFF9BTuS8iiIQUPaDEFnOfFvLGED+1ojc8xB2BJkfzfR6gKGtSQiJthZEPZwt2IaW8wY7c+8FfdZvmI/WSyYkSKZQl0HrqIkJyWdj23sEH78glx+jD3e6Roj19X0GS1vwUZs/sBM8ss8zz9dhDSUt6zKABjVpDeeBFU6GX1KSlBFlJSWqmiooPvM06hjGvhwRwsOw7CK1I9tSydY6oZo93R5rFg2LdQoOeFKAVXl71SM8+175In3TuksEv0AYppsm8k2mCBDzL9L2IGuKD+mQE+Cj4iuilHfiNJkkncBqGQPgrLnnDQyZOsF1O1 YSZuIF36 IR85DQqkGDXfljTJi2auhW9Sy9/7N0QGXf1NlikjPCDXm4w2vS808XVQhb558fSySCfBw+bMHiL8Ufbwjhbt7jmSAfIcktBWanGHKwwyDp+MaLfx60zijctf1PYCFPARmVHco9vR4ZtGSjwOreABgUbaDQCGkYqwnwUjZ18ctevAmn3oSbwB7xMJ2o6PJekMxsghn2xHruk4VoS9TgsbTrCWBiUprmEk3pMlLwBNZh6g2vjmxchxj2bkyefw9o7Xn4AgbQ6wTrVRIizLO0kPetp1P2QBCs/TIGRJnSD+iTPvvUM35+DAcskSHwEO0FNKPGmnm8/UJgFAQo75/ixnjFUs4b5V5Di29jsW//a7mMr2C/8+/GLe/Uv/OS1h8w1MK4QIsMIw5nQQqsVqzeXr04eyBNR8MYYkuvFb0PidvP5pcFdmrFcxlyGq3TlnwxzWrqVudTY/QiEKWOVySN8vGrdKoNVfqNuaoNI5i328kKJhZxH53Dinh1afecIP+hWWqgYIK X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Preserve the original ELF e_flags from the executable in the core dump header instead of relying on compile-time defaults (ELF_CORE_EFLAGS or value from the regset view). This ensures that ABI-specific flags in the dump file match the actual binary being executed. Save the e_flags field during ELF binary loading (in load_elf_binary()) into the mm_struct, and later retrieve it during core dump generation (in fill_note_info()). Use this saved value to populate the e_flags in the core dump ELF header. Add a new Kconfig option, CONFIG_CORE_DUMP_USE_PROCESS_EFLAGS, to guard this behavior. Although motivated by a RISC-V use case, the mechanism is generic and can be applied to all architectures. This change is needed to resolve a debugging issue encountered when analyzing core dumps with GDB for RISC-V systems. GDB inspects the e_flags field to determine whether optional register sets such as the floating-point unit are supported. Without correct flags, GDB may warn and ignore valid register data: warning: Unexpected size of section '.reg2/213' in core file. As a result, floating-point registers are not accessible in the debugger, even though they were dumped. Preserving the original e_flags enables GDB and other tools to properly interpret the dump contents. Signed-off-by: Svetlana Parfenova --- fs/Kconfig.binfmt | 9 +++++++++ fs/binfmt_elf.c | 26 ++++++++++++++++++++------ include/linux/mm_types.h | 5 +++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index bd2f530e5740..45bed2041542 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -184,4 +184,13 @@ config EXEC_KUNIT_TEST This builds the exec KUnit tests, which tests boundary conditions of various aspects of the exec internals. +config CORE_DUMP_USE_PROCESS_EFLAGS + bool "Preserve ELF e_flags from executable in core dumps" + depends on BINFMT_ELF && ELF_CORE && RISCV + default n + help + Save the ELF e_flags from the process executable at load time + and use it in the core dump header. This ensures the dump reflects + the original binary ABI. + endmenu diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 4c1ea6b52a53..baf749e431a1 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1297,6 +1297,11 @@ static int load_elf_binary(struct linux_binprm *bprm) mm->end_data = end_data; mm->start_stack = bprm->p; +#ifdef CONFIG_CORE_DUMP_USE_PROCESS_EFLAGS + /* stash e_flags for use in core dumps */ + mm->saved_e_flags = elf_ex->e_flags; +#endif + /** * DOC: "brk" handling * @@ -1870,6 +1875,8 @@ static int fill_note_info(struct elfhdr *elf, int phdrs, struct elf_thread_core_info *t; struct elf_prpsinfo *psinfo; struct core_thread *ct; + u16 machine; + u32 flags; psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL); if (!psinfo) @@ -1897,17 +1904,24 @@ static int fill_note_info(struct elfhdr *elf, int phdrs, return 0; } - /* - * Initialize the ELF file header. - */ - fill_elf_header(elf, phdrs, - view->e_machine, view->e_flags); + machine = view->e_machine; + flags = view->e_flags; #else view = NULL; info->thread_notes = 2; - fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS); + machine = ELF_ARCH; + flags = ELF_CORE_EFLAGS; #endif +#ifdef CONFIG_CORE_DUMP_USE_PROCESS_EFLAGS + flags = dump_task->mm->saved_e_flags; +#endif + + /* + * Initialize the ELF file header. + */ + fill_elf_header(elf, phdrs, machine, flags); + /* * Allocate a structure for each thread. */ diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 56d07edd01f9..5487d6ba6fcb 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1059,6 +1059,11 @@ struct mm_struct { unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */ +#ifdef CONFIG_CORE_DUMP_USE_PROCESS_EFLAGS + /* the ABI-related flags from the ELF header. Used for core dump */ + unsigned long saved_e_flags; +#endif + struct percpu_counter rss_stat[NR_MM_COUNTERS]; struct linux_binfmt *binfmt; -- 2.39.5