Module Name:    src
Committed By:   thorpej
Date:           Wed Oct 27 04:45:42 UTC 2021

Modified Files:
        src/sys/arch/aarch64/include: signal.h
        src/sys/arch/amd64/include: signal.h
        src/sys/kern: kern_sig.c sys_sig.c

Log Message:
- In sendsig() and sigaction1(), don't hard-code signal trampoline
  versions.  Instead, use the version constants from <sys/signal.h>
  and automatically (and correctly) handle cases where multiple versions
  of a particular trampoline flavor exist.  Conditionalize support
  for sigcontext trampolines on __HAVE_STRUCT_SIGCONTEXT.
- aarch64 and amd64 don't use sigcontext natively, but do need to
  support it for 32-bit compatibility; define __HAVE_STRUCT_SIGCONTEXT
  conditionally on _KERNEL.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/aarch64/include/signal.h
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/amd64/include/signal.h
cvs rdiff -u -r1.399 -r1.400 src/sys/kern/kern_sig.c
cvs rdiff -u -r1.52 -r1.53 src/sys/kern/sys_sig.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/aarch64/include/signal.h
diff -u src/sys/arch/aarch64/include/signal.h:1.2 src/sys/arch/aarch64/include/signal.h:1.3
--- src/sys/arch/aarch64/include/signal.h:1.2	Sun Apr  1 04:35:03 2018
+++ src/sys/arch/aarch64/include/signal.h	Wed Oct 27 04:45:42 2021
@@ -1,3 +1,13 @@
-/* $NetBSD: signal.h,v 1.2 2018/04/01 04:35:03 ryo Exp $ */
+/* $NetBSD: signal.h,v 1.3 2021/10/27 04:45:42 thorpej Exp $ */
+
+#ifndef _AARCH64_SIGNAL_H_
+#define	_AARCH64_SIGNAL_H_
 
 #include <arm/signal.h>
+
+#ifdef _KERNEL
+/* This is needed to support COMPAT_NETBSD32. */ 
+#define	__HAVE_STRUCT_SIGCONTEXT
+#endif /* _KERNEL */
+
+#endif /* ! _AARCH64_SIGNAL_H_ */

Index: src/sys/arch/amd64/include/signal.h
diff -u src/sys/arch/amd64/include/signal.h:1.12 src/sys/arch/amd64/include/signal.h:1.13
--- src/sys/arch/amd64/include/signal.h:1.12	Wed Jan  2 19:40:23 2013
+++ src/sys/arch/amd64/include/signal.h	Wed Oct 27 04:45:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: signal.h,v 1.12 2013/01/02 19:40:23 dsl Exp $	*/
+/*	$NetBSD: signal.h,v 1.13 2021/10/27 04:45:42 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
@@ -38,6 +38,11 @@
 
 #include <sys/featuretest.h>
 
+#ifdef _KERNEL
+/* This is needed to support COMPAT_NETBSD32. */
+#define	__HAVE_STRUCT_SIGCONTEXT
+#endif /* _KERNEL */
+
 typedef int sig_atomic_t;
 
 #if defined(_NETBSD_SOURCE)

Index: src/sys/kern/kern_sig.c
diff -u src/sys/kern/kern_sig.c:1.399 src/sys/kern/kern_sig.c:1.400
--- src/sys/kern/kern_sig.c:1.399	Sun Sep 26 17:34:19 2021
+++ src/sys/kern/kern_sig.c	Wed Oct 27 04:45:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.399 2021/09/26 17:34:19 thorpej Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.400 2021/10/27 04:45:42 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.399 2021/09/26 17:34:19 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.400 2021/10/27 04:45:42 thorpej Exp $");
 
 #include "opt_execfmt.h"
 #include "opt_ptrace.h"
@@ -2169,14 +2169,17 @@ sendsig(const struct ksiginfo *ksi, cons
 	sa = curproc->p_sigacts;
 
 	switch (sa->sa_sigdesc[sig].sd_vers)  {
-	case 0:
-	case 1:
+	case __SIGTRAMP_SIGCODE_VERSION:
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+	case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
+	     __SIGTRAMP_SIGCONTEXT_VERSION_MAX:
 		/* Compat for 1.6 and earlier. */
 		MODULE_HOOK_CALL_VOID(sendsig_sigcontext_16_hook, (ksi, mask),
 		    break);
 		return;
-	case 2:
-	case 3:
+#endif /* __HAVE_STRUCT_SIGCONTEXT */
+	case __SIGTRAMP_SIGINFO_VERSION_MIN ...
+	     __SIGTRAMP_SIGINFO_VERSION_MAX:
 		sendsig_siginfo(ksi, mask);
 		return;
 	default:

Index: src/sys/kern/sys_sig.c
diff -u src/sys/kern/sys_sig.c:1.52 src/sys/kern/sys_sig.c:1.53
--- src/sys/kern/sys_sig.c:1.52	Thu Sep 23 06:58:47 2021
+++ src/sys/kern/sys_sig.c	Wed Oct 27 04:45:42 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_sig.c,v 1.52 2021/09/23 06:58:47 ryo Exp $	*/
+/*	$NetBSD: sys_sig.c,v 1.53 2021/10/27 04:45:42 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.52 2021/09/23 06:58:47 ryo Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.53 2021/10/27 04:45:42 thorpej Exp $");
 
 #include "opt_dtrace.h"
 
@@ -394,30 +394,32 @@ sigaction1(struct lwp *l, int signum, co
 	ksiginfo_queue_init(&kq);
 
 	/*
-	 * Trampoline ABI version 0 is reserved for the legacy kernel
-	 * provided on-stack trampoline.  Conversely, if we are using a
-	 * non-0 ABI version, we must have a trampoline.  Only validate the
-	 * vers if a new sigaction was supplied and there was an actual
-	 * handler specified (not SIG_IGN or SIG_DFL), which don't require
-	 * a trampoline. Emulations use legacy kernel trampolines with
-	 * version 0, alternatively check for that too.
+	 * Trampoline ABI version __SIGTRAMP_SIGCODE_VERSION (0) is reserved
+	 * for the legacy kernel provided on-stack trampoline.  Conversely,
+	 * if we are using a non-0 ABI version, we must have a trampoline.
+	 * Only validate the vers if a new sigaction was supplied and there
+	 * was an actual handler specified (not SIG_IGN or SIG_DFL), which
+	 * don't require a trampoline. Emulations use legacy kernel
+	 * trampolines with version 0, alternatively check for that too.
 	 *
-	 * If version < 2, we try to autoload the compat module.  Note
-	 * that we interlock with the unload check in compat_modcmd()
-	 * using kernconfig_lock.  If the autoload fails, we don't try it
-	 * again for this process.
+	 * If version < __SIGTRAMP_SIGINFO_VERSION_MIN (usually 2), we try
+	 * to autoload the compat module.  Note that we interlock with the
+	 * unload check in compat_modcmd() using kernconfig_lock.  If the
+	 * autoload fails, we don't try it again for this process.
 	 */
 	if (nsa != NULL && nsa->sa_handler != SIG_IGN
 	    && nsa->sa_handler != SIG_DFL) {
-		if (__predict_false(vers < 2)) {
-			if (p->p_flag & PK_32) {
-				v0v1valid = true;
-			} else if (vers == 0 &&
+		if (__predict_false(vers < __SIGTRAMP_SIGINFO_VERSION_MIN)) {
+			if (vers == __SIGTRAMP_SIGCODE_VERSION &&
 			    p->p_sigctx.ps_sigcode != NULL) {
 				/*
 				 * if sigcode is used for this emulation,
 				 * version 0 is allowed.
 				 */
+			}
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+			else if (p->p_flag & PK_32) {
+				v0v1valid = true;
 			} else if ((p->p_lflag & PL_SIGCOMPAT) == 0) {
 				kernconfig_lock();
 				(void)module_autoload("compat_16",
@@ -440,30 +442,35 @@ sigaction1(struct lwp *l, int signum, co
 				mutex_exit(&proc_lock);
 				kernconfig_unlock();
 			}
+#endif /* __HAVE_STRUCT_SIGCONTEXT */
 		}
 
 		switch (vers) {
-		case 0:
-			/* sigcontext, kernel supplied trampoline. */
+		case __SIGTRAMP_SIGCODE_VERSION:
+			/* kernel supplied trampoline. */
 			if (tramp != NULL ||
 			    (p->p_sigctx.ps_sigcode == NULL && !v0v1valid)) {
 				return EINVAL;
 			}
 			break;
-		case 1:
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+		case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
+		     __SIGTRAMP_SIGCONTEXT_VERSION_MAX:
 			/* sigcontext, user supplied trampoline. */
 			if (tramp == NULL || !v0v1valid) {
 				return EINVAL;
 			}
 			break;
-		case 2:
-		case 3:
+#endif /* __HAVE_STRUCT_SIGCONTEXT */
+		case __SIGTRAMP_SIGINFO_VERSION_MIN ...
+		     __SIGTRAMP_SIGINFO_VERSION_MAX:
 			/* siginfo, user supplied trampoline. */
 			if (tramp == NULL) {
 				return EINVAL;
 			}
 			break;
 		default:
+			/* Invalid trampoline version. */
 			return EINVAL;
 		}
 	}

Reply via email to