Module Name:    src
Committed By:   riastradh
Date:           Sun Mar 16 15:35:00 UTC 2025

Modified Files:
        src/sys/arch/alpha/alpha: machdep.c
        src/sys/arch/amd64/amd64: machdep.c
        src/sys/arch/hppa/hppa: machdep.c
        src/sys/arch/i386/i386: machdep.c
        src/sys/arch/ia64/ia64: machdep.c
        src/sys/arch/m68k/m68k: m68k_machdep.c
        src/sys/arch/mips/mips: mips_machdep.c
        src/sys/arch/sh3/sh3: sh3_machdep.c
        src/sys/arch/vax/vax: trap.c
        src/tests/kernel: t_execregs.c

Log Message:
Clear trapframe on exec.

Do this for all architectures, even if the trapframe is fully
initialized -- makes it easier to audit and be confident it's
correct, and most likely (with the exception of sh3 which has an
intermediate call to ufetch_int in the middle) the compiler can
eliminate redundant stores in these routines.

PR kern/59084: exec/spawn leaks register content


To generate a diff of this commit:
cvs rdiff -u -r1.379 -r1.380 src/sys/arch/alpha/alpha/machdep.c
cvs rdiff -u -r1.371 -r1.372 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/hppa/hppa/machdep.c
cvs rdiff -u -r1.843 -r1.844 src/sys/arch/i386/i386/machdep.c
cvs rdiff -u -r1.45 -r1.46 src/sys/arch/ia64/ia64/machdep.c
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/m68k/m68k/m68k_machdep.c
cvs rdiff -u -r1.306 -r1.307 src/sys/arch/mips/mips/mips_machdep.c
cvs rdiff -u -r1.113 -r1.114 src/sys/arch/sh3/sh3/sh3_machdep.c
cvs rdiff -u -r1.138 -r1.139 src/sys/arch/vax/vax/trap.c
cvs rdiff -u -r1.3 -r1.4 src/tests/kernel/t_execregs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/alpha/alpha/machdep.c
diff -u src/sys/arch/alpha/alpha/machdep.c:1.379 src/sys/arch/alpha/alpha/machdep.c:1.380
--- src/sys/arch/alpha/alpha/machdep.c:1.379	Sun Mar 31 17:13:29 2024
+++ src/sys/arch/alpha/alpha/machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/* $NetBSD: machdep.c,v 1.379 2024/03/31 17:13:29 thorpej Exp $ */
+/* $NetBSD: machdep.c,v 1.380 2025/03/16 15:34:59 riastradh Exp $ */
 
 /*-
  * Copyright (c) 1998, 1999, 2000, 2019, 2020 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.379 2024/03/31 17:13:29 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.380 2025/03/16 15:34:59 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1684,11 +1684,11 @@ setregs(register struct lwp *l, struct e
 		panic("crash requested by boot flags");
 #endif
 
+	memset(tfp, 0, sizeof(*tfp));
+
 #ifdef DEBUG
 	for (i = 0; i < FRAME_SIZE; i++)
 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
-#else
-	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
 #endif
 	pcb = lwp_getpcb(l);
 	memset(&pcb->pcb_fp, 0, sizeof(pcb->pcb_fp));

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.371 src/sys/arch/amd64/amd64/machdep.c:1.372
--- src/sys/arch/amd64/amd64/machdep.c:1.371	Wed Jan 22 10:03:55 2025
+++ src/sys/arch/amd64/amd64/machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.371 2025/01/22 10:03:55 riastradh Exp $	*/
+/*	$NetBSD: machdep.c,v 1.372 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.371 2025/01/22 10:03:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.372 2025/03/16 15:34:59 riastradh Exp $");
 
 #include "opt_modular.h"
 #include "opt_user_ldt.h"
@@ -1388,6 +1388,8 @@ setregs(struct lwp *l, struct exec_packa
 	kpreempt_enable();
 
 	tf = l->l_md.md_regs;
+	memset(tf, 0, sizeof(*tf));
+
 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
 	tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
 	tf->tf_rdi = 0;

Index: src/sys/arch/hppa/hppa/machdep.c
diff -u src/sys/arch/hppa/hppa/machdep.c:1.21 src/sys/arch/hppa/hppa/machdep.c:1.22
--- src/sys/arch/hppa/hppa/machdep.c:1.21	Wed Apr 17 07:47:48 2024
+++ src/sys/arch/hppa/hppa/machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.21 2024/04/17 07:47:48 macallan Exp $	*/
+/*	$NetBSD: machdep.c,v 1.22 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.21 2024/04/17 07:47:48 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.22 2025/03/16 15:34:59 riastradh Exp $");
 
 #include "opt_cputype.h"
 #include "opt_ddb.h"
@@ -1889,6 +1889,22 @@ setregs(struct lwp *l, struct exec_packa
 	struct trapframe *tf = l->l_md.md_regs;
 	struct pcb *pcb = lwp_getpcb(l);
 
+	memset(tf, 0, sizeof(*tf));
+
+	/*
+	 * Initialize the External Interrupt Enable Mask, Processor
+	 * Status Word, and NetBSD's floating-point register area
+	 * pointer to the correct defaults for a user process.
+	 *
+	 * XXXMPSAFE If curcpu()->ci_eiem can vary from CPU to CPU, we
+	 * have bigger problems here -- if the lwp is migrated from one
+	 * CPU to another CPU between when the trapframe is saved and
+	 * when the trapframe is restored, it might be invalidated.
+	 */
+	tf->tf_eiem = curcpu()->ci_eiem;
+	tf->tf_ipsw = PSW_MBS | (hppa_cpu_ispa20_p() ? PSW_O : 0);
+	tf->tf_cr30 = (u_int)pcb->pcb_fpregs;
+
 	tf->tf_flags = TFF_SYS|TFF_LAST;
 	tf->tf_iioq_tail = 4 +
 	    (tf->tf_iioq_head = pack->ep_entry | HPPA_PC_PRIV_USER);
@@ -1906,6 +1922,7 @@ setregs(struct lwp *l, struct exec_packa
 
 	/* reset any of the pending FPU exceptions */
 	hppa_fpu_flush(l);
+	memset(pcb->pcb_fpregs, 0, sizeof(*pcb->pcb_fpregs));
 	pcb->pcb_fpregs->fpr_regs[0] = ((uint64_t)HPPA_FPU_INIT) << 32;
 	pcb->pcb_fpregs->fpr_regs[1] = 0;
 	pcb->pcb_fpregs->fpr_regs[2] = 0;

Index: src/sys/arch/i386/i386/machdep.c
diff -u src/sys/arch/i386/i386/machdep.c:1.843 src/sys/arch/i386/i386/machdep.c:1.844
--- src/sys/arch/i386/i386/machdep.c:1.843	Tue Feb 18 10:16:03 2025
+++ src/sys/arch/i386/i386/machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.843 2025/02/18 10:16:03 imil Exp $	*/
+/*	$NetBSD: machdep.c,v 1.844 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009, 2017
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.843 2025/02/18 10:16:03 imil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.844 2025/03/16 15:34:59 riastradh Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_freebsd.h"
@@ -859,6 +859,8 @@ setregs(struct lwp *l, struct exec_packa
 	x86_dbregs_clear(l);
 
 	tf = l->l_md.md_regs;
+	memset(tf, 0, sizeof(*tf));
+
 	tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
 	tf->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
 	tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);

Index: src/sys/arch/ia64/ia64/machdep.c
diff -u src/sys/arch/ia64/ia64/machdep.c:1.45 src/sys/arch/ia64/ia64/machdep.c:1.46
--- src/sys/arch/ia64/ia64/machdep.c:1.45	Fri Oct  6 11:45:16 2023
+++ src/sys/arch/ia64/ia64/machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.45 2023/10/06 11:45:16 skrll Exp $	*/
+/*	$NetBSD: machdep.c,v 1.46 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2003,2004 Marcel Moolenaar
@@ -710,6 +710,8 @@ setregs(register struct lwp *l, struct e
 	vaddr_t uv = uvm_lwp_getuarea(l);
 
 	tf = l->l_md.md_tf;
+	memset(tf, 0, sizeof(*tf));
+
 	regstkp = uv + sizeof(struct pcb);
 
 	ksttop =

Index: src/sys/arch/m68k/m68k/m68k_machdep.c
diff -u src/sys/arch/m68k/m68k/m68k_machdep.c:1.11 src/sys/arch/m68k/m68k/m68k_machdep.c:1.12
--- src/sys/arch/m68k/m68k/m68k_machdep.c:1.11	Tue Sep 26 12:46:30 2023
+++ src/sys/arch/m68k/m68k/m68k_machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: m68k_machdep.c,v 1.11 2023/09/26 12:46:30 tsutsui Exp $	*/
+/*	$NetBSD: m68k_machdep.c,v 1.12 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: m68k_machdep.c,v 1.11 2023/09/26 12:46:30 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: m68k_machdep.c,v 1.12 2025/03/16 15:34:59 riastradh Exp $");
 
 #include "opt_compat_sunos.h"
 
@@ -93,6 +93,8 @@ setregs(struct lwp *l, struct exec_packa
 	struct trapframe *tf = (struct trapframe *)l->l_md.md_regs;
 	struct pcb *pcb = lwp_getpcb(l);
 
+	memset(tf, 0, sizeof(*tf));
+
 	tf->tf_sr = PSL_USERSET;
 	tf->tf_pc = pack->ep_entry & ~1;
 	tf->tf_regs[D0] = 0;

Index: src/sys/arch/mips/mips/mips_machdep.c
diff -u src/sys/arch/mips/mips/mips_machdep.c:1.306 src/sys/arch/mips/mips/mips_machdep.c:1.307
--- src/sys/arch/mips/mips/mips_machdep.c:1.306	Sat Jan  6 07:27:35 2024
+++ src/sys/arch/mips/mips/mips_machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: mips_machdep.c,v 1.306 2024/01/06 07:27:35 simonb Exp $	*/
+/*	$NetBSD: mips_machdep.c,v 1.307 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*
  * Copyright 2002 Wasabi Systems, Inc.
@@ -111,7 +111,7 @@
  */
 
 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
-__KERNEL_RCSID(0, "$NetBSD: mips_machdep.c,v 1.306 2024/01/06 07:27:35 simonb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mips_machdep.c,v 1.307 2025/03/16 15:34:59 riastradh Exp $");
 
 #define __INTR_PRIVATE
 #include "opt_cputype.h"
@@ -1697,7 +1697,7 @@ setregs(struct lwp *l, struct exec_packa
 	struct trapframe * const tf = l->l_md.md_utf;
 	struct proc * const p = l->l_proc;
 
-	memset(tf, 0, sizeof(struct trapframe));
+	memset(tf, 0, sizeof(*tf));
 	tf->tf_regs[_R_SP] = (intptr_t)stack;
 	tf->tf_regs[_R_PC] = (intptr_t)pack->ep_entry & ~3;
 	tf->tf_regs[_R_T9] = (intptr_t)pack->ep_entry & ~3; /* abicall requirement */

Index: src/sys/arch/sh3/sh3/sh3_machdep.c
diff -u src/sys/arch/sh3/sh3/sh3_machdep.c:1.113 src/sys/arch/sh3/sh3/sh3_machdep.c:1.114
--- src/sys/arch/sh3/sh3/sh3_machdep.c:1.113	Wed Dec 20 15:34:45 2023
+++ src/sys/arch/sh3/sh3/sh3_machdep.c	Sun Mar 16 15:34:59 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: sh3_machdep.c,v 1.113 2023/12/20 15:34:45 thorpej Exp $	*/
+/*	$NetBSD: sh3_machdep.c,v 1.114 2025/03/16 15:34:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2002 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sh3_machdep.c,v 1.113 2023/12/20 15:34:45 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sh3_machdep.c,v 1.114 2025/03/16 15:34:59 riastradh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -518,6 +518,7 @@ setregs(struct lwp *l, struct exec_packa
 	l->l_md.md_flags &= ~(MDL_USEDFPU | MDL_SSTEP);
 
 	tf = l->l_md.md_regs;
+	memset(tf, 0, sizeof(*tf));
 
 	tf->tf_ssr = PSL_USERSET;
 	tf->tf_spc = pack->ep_entry;

Index: src/sys/arch/vax/vax/trap.c
diff -u src/sys/arch/vax/vax/trap.c:1.138 src/sys/arch/vax/vax/trap.c:1.139
--- src/sys/arch/vax/vax/trap.c:1.138	Thu Oct  5 19:41:06 2023
+++ src/sys/arch/vax/vax/trap.c	Sun Mar 16 15:35:00 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.138 2023/10/05 19:41:06 ad Exp $     */
+/*	$NetBSD: trap.c,v 1.139 2025/03/16 15:35:00 riastradh Exp $     */
 
 /*
  * Copyright (c) 1994 Ludd, University of Lule}, Sweden.
@@ -28,7 +28,7 @@
  /* All bugs are subject to removal without further notice */
 		
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.138 2023/10/05 19:41:06 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.139 2025/03/16 15:35:00 riastradh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_multiprocessor.h"
@@ -371,6 +371,8 @@ setregs(struct lwp *l, struct exec_packa
 {
 	struct trapframe * const tf = l->l_md.md_utf;
 
+	memset(tf, 0, sizeof(*tf));
+
 	tf->tf_pc = pack->ep_entry + 2;
 	tf->tf_sp = stack;
 	tf->tf_r6 = stack;				/* for ELF */

Index: src/tests/kernel/t_execregs.c
diff -u src/tests/kernel/t_execregs.c:1.3 src/tests/kernel/t_execregs.c:1.4
--- src/tests/kernel/t_execregs.c:1.3	Fri Feb 28 16:08:42 2025
+++ src/tests/kernel/t_execregs.c	Sun Mar 16 15:35:00 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_execregs.c,v 1.3 2025/02/28 16:08:42 riastradh Exp $	*/
+/*	$NetBSD: t_execregs.c,v 1.4 2025/03/16 15:35:00 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2025 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_execregs.c,v 1.3 2025/02/28 16:08:42 riastradh Exp $");
+__RCSID("$NetBSD: t_execregs.c,v 1.4 2025/03/16 15:35:00 riastradh Exp $");
 
 #include <sys/wait.h>
 
@@ -80,13 +80,6 @@ checkregs(const register_t regs[static N
 	}
 #endif
 
-#if defined(__hppa__) || \
-    defined(__ia64__) || \
-    defined(__vax__) || \
-    defined(__x86_64__)
-	atf_tc_expect_fail("PR kern/59084: exec/spawn leaks register content");
-#endif
-
 	for (i = 0; i < NEXECREGS; i++) {
 		if (regs[i] != 0) {
 			for (i = 0; i < NEXECREGS; i++) {

Reply via email to