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 BC87AC54E60 for ; Thu, 14 Mar 2024 16:13:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B2636B0071; Thu, 14 Mar 2024 12:13:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43A70800B4; Thu, 14 Mar 2024 12:13:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DCD26B007B; Thu, 14 Mar 2024 12:13:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 183BE800B4 for ; Thu, 14 Mar 2024 12:13:05 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D8FA3120AAA for ; Thu, 14 Mar 2024 16:13:04 +0000 (UTC) X-FDA: 81896138688.11.CADD412 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf24.hostedemail.com (Postfix) with ESMTP id D893F180038 for ; Thu, 14 Mar 2024 16:13:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=E7LWWV1C; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf24.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.178 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710432783; a=rsa-sha256; cv=none; b=EjhdYp+fuoqspnlDRX0mFNOvXCCw67uJLfY48Ql9vSfhSol1OIxHUELUjBTVA9oEzoNJqN L+L/Zn6UoSKGMEY8AAmJP7ERR46c+LyJTr16aehsd0kPmTfdp89P/c3NbutrnC4lSu/LcW 2RcVUQxBnNeYuEXcdfDByLHe8L4wSwM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=E7LWWV1C; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf24.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.178 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710432782; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NNT5KeqAV92Vo5ttm2M3Vsv7FvukLoVx50A9qFgKXzo=; b=1Qn/LqXiWPEb9yThgCiPZjuEhTcU79KLz5zFm4+ULGWT7+XKoTy1G92jlLbOVfFj9NXSKz U+MfowkT5clMkv6u3ZFkx1lWCbpRzzekK2npiTit5WNIFfPUhc0I0+ygDwAf9wD/mthgC4 /x4ZbArCyZOwqahXm7fgLkRdckR2VdE= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6e6aaa2f02cso995292b3a.2 for ; Thu, 14 Mar 2024 09:13:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1710432782; x=1711037582; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=NNT5KeqAV92Vo5ttm2M3Vsv7FvukLoVx50A9qFgKXzo=; b=E7LWWV1CKcJRduabwFUbPT5k6Zxc2fiNWZEHs1KCBbau1c640/9TUYJsjNNGvVXq50 wsjkshtoFg3X7HlKBNSC9KUtZXFvuw+TY1UHDTQ7a41NJcMFUtx0gyBHt9sSkptr+Cvc DTzF84zhaj+I2VEBOHALB+Pb0+MvW67bkJDz4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710432782; x=1711037582; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NNT5KeqAV92Vo5ttm2M3Vsv7FvukLoVx50A9qFgKXzo=; b=a31+FGroudxHn89ZYzX8Fx0HXLAIHLxmCOPRRSPZZMXhXW2RFzwc41lq8mJRSt7wgG /DdhuQ2pmdFM/ctYn/GKWI7t23rIjCFFLpIQ75Swdfy33T2Ly9/5fLydFdqOUKeTcaWQ KWztPN3dvVu03Xw3X2Cic5Eptl6XlqtmPoLQE05B2fb/dYhm/qDJXgSy7SxlFED23V2h oOTGzU6ndaKwOO6mnAvEcLRC184heytXYgjcRG76/Cuvj7Ir5HDencSBoE5EmaFyDUvv SOgudC5Uhmuw9ZVBjndjJWA/HFozG1Zy+ZtkqdIa/i7RS1x/gB3q8+7s6s4hDoZR/TL3 Ex3Q== X-Forwarded-Encrypted: i=1; AJvYcCWpCEykrjUU1jCY8PkM8uUB9VTXqjL4jpO7FJS+GypmQNoC5ih56k2PFsmt5+d7CqoVmicWnjSm/1WhcLuK5p6qJ8o= X-Gm-Message-State: AOJu0YwBa7BCP6Ry9Teio0iYRJdx/9aGXwwTZ246Y9hZZ8gvsURmqh0u 36+FpS7g8IiqHNRIAvinolEanfg2EdZrbX9Kd9MIXmFbKQ+TnwHGcoEIU+7sSA== X-Google-Smtp-Source: AGHT+IE/O6pHS1pZmi6EViuPOL/FfHus2QPDk6/PDxlis6XiF7Wo+Q7Gf92imFZIDKoMF5G/u+CmPw== X-Received: by 2002:a05:6a20:8415:b0:1a3:378d:dc6 with SMTP id c21-20020a056a20841500b001a3378d0dc6mr2804292pzd.38.1710432781569; Thu, 14 Mar 2024 09:13:01 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id b2-20020a056a00114200b006e6b7124b33sm1659371pfm.209.2024.03.14.09.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 09:13:01 -0700 (PDT) Date: Thu, 14 Mar 2024 09:13:00 -0700 From: Kees Cook To: Vignesh Balasubramanian Cc: linux-kernel@vger.kernel.org, linux-toolchains@vger.kernel.org, mpe@ellerman.id.au, npiggin@gmail.com, christophe.leroy@csgroup.eu, aneesh.kumar@kernel.org, naveen.n.rao@linux.ibm.com, ebiederm@xmission.com, x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, bpetkov@amd.com, jinisusan.george@amd.com, matz@suse.de, binutils@sourceware.org, jhb@freebsd.org, felix.willgerodt@intel.com Subject: Re: [PATCH 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files Message-ID: <202403140850.5659C0F4@keescook> References: <20240314112359.50713-1-vigbalas@amd.com> <20240314112359.50713-2-vigbalas@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240314112359.50713-2-vigbalas@amd.com> X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D893F180038 X-Stat-Signature: n5z7knz68wukuzqoo3h8igwwcsykfxss X-HE-Tag: 1710432782-192667 X-HE-Meta: U2FsdGVkX18fnLe/LIJ2FADiXyE77zqgc09Vm6cYlEHl7tEabvjpidBOtKOyZybdKTnomhYXyQgWd/HUzBqhae+avE0ulLkq78VuPTiIFNeupYbNapMwE2twI9Hn7BCf7WcoDYFNV6XcECq0TqnhWcRbdie5E5YO5qGdRurIDXL5U0LFAUUt4ItyXzu8B4XtRVbdz4Vx/nmUKdmRk4fghl8W3PcIN6BDHXw+XPCa6KxCRXQ23scoj60NnouVaGlsCwskVvgjrgcQnp39Y+bm57lQRATlkWOyrIstRsGIy8q06sHzHSR3QB1bxONvH24vU51CeZKnvfOVAS6Xp25f0mKrJGm+bhZGfzT0obNvbly9eXiT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ity: 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: On Thu, Mar 14, 2024 at 04:53:28PM +0530, Vignesh Balasubramanian wrote: > Add a new .note section containing type, size, offset and flags of > every xfeature that is present. > > This information will be used by the debuggers to understand the XSAVE > layout of the machine where the core file is dumped, and to read XSAVE > registers, especially during cross-platform debugging. I see binutils in CC. Can someone from gdb confirm that this solution can be used? > > Co-developed-by: Jini Susan George > Signed-off-by: Jini Susan George > Signed-off-by: Vignesh Balasubramanian > --- > arch/Kconfig | 9 +++ > arch/powerpc/Kconfig | 1 + > arch/powerpc/include/asm/elf.h | 2 - > arch/x86/Kconfig | 1 + > arch/x86/include/asm/elf.h | 7 +++ > arch/x86/kernel/fpu/xstate.c | 101 +++++++++++++++++++++++++++++++++ > include/linux/elf.h | 2 +- > include/uapi/linux/elf.h | 1 + > 8 files changed, 121 insertions(+), 3 deletions(-) > > diff --git a/arch/Kconfig b/arch/Kconfig > index fd18b7db2c77..3bd8a0b2bba1 100644 > --- a/arch/Kconfig > +++ b/arch/Kconfig > @@ -502,6 +502,15 @@ config MMU_LAZY_TLB_SHOOTDOWN > config ARCH_HAVE_NMI_SAFE_CMPXCHG > bool > > +config ARCH_HAVE_EXTRA_ELF_NOTES > + bool > + help > + An architecture should select this in order to enable adding an > + arch-specific ELF note section to core files. It must provide two > + functions: elf_coredump_extra_notes_size() and > + elf_coredump_extra_notes_write() which are invoked by the ELF core > + dumper. > + > config ARCH_HAS_NMI_SAFE_THIS_CPU_OPS > bool > > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig > index a91cb070ca4a..3b31bd7490e2 100644 > --- a/arch/powerpc/Kconfig > +++ b/arch/powerpc/Kconfig > @@ -156,6 +156,7 @@ config PPC > select ARCH_HAS_UACCESS_FLUSHCACHE > select ARCH_HAS_UBSAN > select ARCH_HAVE_NMI_SAFE_CMPXCHG > + select ARCH_HAVE_EXTRA_ELF_NOTES if SPU_BASE > select ARCH_KEEP_MEMBLOCK > select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE if PPC_RADIX_MMU > select ARCH_MIGHT_HAVE_PC_PARPORT > diff --git a/arch/powerpc/include/asm/elf.h b/arch/powerpc/include/asm/elf.h > index 79f1c480b5eb..bb4b94444d3e 100644 > --- a/arch/powerpc/include/asm/elf.h > +++ b/arch/powerpc/include/asm/elf.h > @@ -127,8 +127,6 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm, > /* Notes used in ET_CORE. Note name is "SPU//". */ > #define NT_SPU 1 > > -#define ARCH_HAVE_EXTRA_ELF_NOTES > - > #endif /* CONFIG_SPU_BASE */ > > #ifdef CONFIG_PPC64 > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index 78050d5d7fac..35e8d1201099 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -104,6 +104,7 @@ config X86 > select ARCH_HAS_DEBUG_WX > select ARCH_HAS_ZONE_DMA_SET if EXPERT > select ARCH_HAVE_NMI_SAFE_CMPXCHG > + select ARCH_HAVE_EXTRA_ELF_NOTES > select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE > select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI > select ARCH_MIGHT_HAVE_PC_PARPORT > diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h > index 1fb83d47711f..1b9f0b4bf6bc 100644 > --- a/arch/x86/include/asm/elf.h > +++ b/arch/x86/include/asm/elf.h > @@ -13,6 +13,13 @@ > #include > #include > > +struct xfeat_component { > + u32 xfeat_type; > + u32 xfeat_sz; > + u32 xfeat_off; > + u32 xfeat_flags; > +} __packed; While it is currently true, just for robustness, can you add a _Static_assert that sizeof(struct xfeat_component) % 4 == 0 ? Notes must be 4-byte aligned. > + > typedef unsigned long elf_greg_t; > > #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t)) > diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c > index 117e74c44e75..6e5ea483ec1d 100644 > --- a/arch/x86/kernel/fpu/xstate.c > +++ b/arch/x86/kernel/fpu/xstate.c > @@ -13,6 +13,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -1836,3 +1837,103 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, > return 0; > } > #endif /* CONFIG_PROC_PID_ARCH_STATUS */ > + > +/* > + * Dump type, size, offset and flag values for every xfeature that is present. > + */ > +static int dump_xsave_layout_desc(struct coredump_params *cprm) > +{ > + > + struct xfeat_component xc; > + int num_records = 0; > + int i; > + > + /* XFEATURE_FPU and XFEATURE_SSE, both are fixed legacy states. */ > + for (i = 0; i < FIRST_EXTENDED_XFEATURE; i++) { > + xc.xfeat_type = i; > + xc.xfeat_sz = xstate_sizes[i]; > + xc.xfeat_off = xstate_offsets[i]; > + xc.xfeat_flags = xstate_flags[i]; > + > + if (!dump_emit(cprm, &xc, sizeof(struct xfeat_component))) > + return 0; > + num_records++; > + } > + > + for_each_extended_xfeature(i, fpu_user_cfg.max_features) { > + xc.xfeat_type = i; > + xc.xfeat_sz = xstate_sizes[i]; > + xc.xfeat_off = xstate_offsets[i]; > + xc.xfeat_flags = xstate_flags[i]; > + > + if (!dump_emit(cprm, &xc, sizeof(struct xfeat_component))) > + return 0; > + num_records++; > + } > + > + return num_records; > +} > + > +static int get_xsave_desc_size(void) > +{ > + /* XFEATURE_FP and XFEATURE_SSE, both are fixed legacy states */ > + int xfeatures_count = 2; > + int i; > + > + for_each_extended_xfeature(i, fpu_user_cfg.max_features) > + xfeatures_count++; > + > + return xfeatures_count * (sizeof(struct xfeat_component)); > +} > + > +int elf_coredump_extra_notes_write(struct coredump_params *cprm) > +{ > + const char *owner_name = "LINUX"; If you use an array instead of a pointer, there's no need for strlen(), and you can make it a static outside of the function to refer to it later. static const char owner_name[] = "LINUX"; > + int num_records = 0; > + struct elf_note en; > + > + en.n_namesz = strlen(owner_name) + 1; en.n_namesz = sizeof(owner_name); > + en.n_descsz = get_xsave_desc_size(); > + en.n_type = NT_X86_XSAVE_LAYOUT; > + > + if (!dump_emit(cprm, &en, sizeof(en))) > + return 1; > + if (!dump_emit(cprm, owner_name, en.n_namesz)) > + return 1; > + if (!dump_align(cprm, 4)) > + return 1; > + > + num_records = dump_xsave_layout_desc(cprm); > + if (!num_records) { > + pr_warn("Error adding XSTATE layout ELF note. XSTATE buffer in the core file will be unparseable."); Can you make this pr_warn_ratelimited() (and below)? > + return 1; > + } > + > + /* Total size should be equal to the number of records */ > + if ((sizeof(struct xfeat_component) * num_records) != en.n_descsz) { > + pr_warn("Error adding XSTATE layout ELF note. The size of the .note section does not match with the total size of the records."); > + return 1; > + } > + > + if (!dump_align(cprm, 4)) > + return 1; I don't think this call is needed? > + > + return 0; > +} > + > +/* > + * Return the size of new note. > + */ > +int elf_coredump_extra_notes_size(void) > +{ > + const char *fullname = "LINUX"; Now this can be dropped. > + int size = 0; > + > + /* NOTE Header */ > + size += sizeof(struct elf_note); > + /* name + align */ > + size += roundup(strlen(fullname) + 1, 4); size += roundup(sizeof(owner_name), 4); > + size += get_xsave_desc_size(); > + > + return size; > +} > diff --git a/include/linux/elf.h b/include/linux/elf.h > index c9a46c4e183b..5c402788da19 100644 > --- a/include/linux/elf.h > +++ b/include/linux/elf.h > @@ -65,7 +65,7 @@ extern Elf64_Dyn _DYNAMIC []; > struct file; > struct coredump_params; > > -#ifndef ARCH_HAVE_EXTRA_ELF_NOTES > +#ifndef CONFIG_ARCH_HAVE_EXTRA_ELF_NOTES > static inline int elf_coredump_extra_notes_size(void) { return 0; } > static inline int elf_coredump_extra_notes_write(struct coredump_params *cprm) { return 0; } > #else > diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h > index 9417309b7230..3325488cb39b 100644 > --- a/include/uapi/linux/elf.h > +++ b/include/uapi/linux/elf.h > @@ -411,6 +411,7 @@ typedef struct elf64_shdr { > #define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ > /* Old binutils treats 0x203 as a CET state */ > #define NT_X86_SHSTK 0x204 /* x86 SHSTK state */ > +#define NT_X86_XSAVE_LAYOUT 0x205 /* XSAVE layout description */ > #define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ > #define NT_S390_TIMER 0x301 /* s390 timer register */ > #define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ > -- > 2.43.0 > Otherwise looks reasonable, though I see Dave has feedback to address too. :) Thanks for working on this! -Kees -- Kees Cook