From 29534ad96a84966fc77bef75ef9dcca0ad75685f Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 19 Aug 2019 08:36:30 -0600 Subject: [PATCH] Fix handling of real-time signals. --- lib/util/sig2str.c | 17 +++++++++++++++-- lib/util/str2sig.c | 35 ++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/util/sig2str.c b/lib/util/sig2str.c index 23237fa53..6049b1b2c 100644 --- a/lib/util/sig2str.c +++ b/lib/util/sig2str.c @@ -62,9 +62,22 @@ int sudo_sig2str(int signo, char *signame) { #if defined(SIGRTMIN) && defined(SIGRTMAX) - /* Realtime signal support as per Solaris. */ + /* Realtime signal support. */ if (signo >= SIGRTMIN && signo <= SIGRTMAX) { - (void)snprintf(signame, SIG2STR_MAX, "RTMIN+%d", (signo - SIGRTMIN)); + const long rtmax = sysconf(_SC_RTSIG_MAX); + if (rtmax > 0) { + if (signo == SIGRTMIN) { + strlcpy(signame, "RTMIN", SIG2STR_MAX); + } else if (signo == SIGRTMAX) { + strlcpy(signame, "RTMAX", SIG2STR_MAX); + } else if (signo <= SIGRTMIN + (rtmax / 2) - 1) { + (void)snprintf(signame, SIG2STR_MAX, "RTMIN+%d", + (signo - SIGRTMIN)); + } else { + (void)snprintf(signame, SIG2STR_MAX, "RTMAX-%d", + (SIGRTMAX - signo)); + } + } return 0; } #endif diff --git a/lib/util/str2sig.c b/lib/util/str2sig.c index 236348b6a..68b12c6d2 100644 --- a/lib/util/str2sig.c +++ b/lib/util/str2sig.c @@ -36,8 +36,9 @@ #ifdef HAVE_STRINGS_H # include #endif /* HAVE_STRINGS_H */ -#include #include +#include +#include #include "sudo_compat.h" @@ -117,9 +118,14 @@ sudo_str2sig(const char *signame, int *result) return 0; } if (signame[5] == '+') { - if (signame[6] == '1' || signame[6] == '2' || signame[6] == '3') { - *result = SIGRTMIN + (signame[6] - '0'); - return 0; + if (isdigit((unsigned char)signame[6])) { + const long rtmax = sysconf(_SC_RTSIG_MAX); + const int off = signame[6] - '0'; + + if (rtmax > 0 && off < rtmax / 2) { + *result = SIGRTMIN + off; + return 0; + } } } } @@ -131,9 +137,14 @@ sudo_str2sig(const char *signame, int *result) return 0; } if (signame[5] == '-') { - if (signame[6] == '1' || signame[6] == '2' || signame[6] == '3') { - *result = SIGRTMAX - (signame[6] - '0'); - return 0; + if (isdigit((unsigned char)signame[6])) { + const long rtmax = sysconf(_SC_RTSIG_MAX); + const int off = signame[6] - '0'; + + if (rtmax > 0 && off < rtmax / 2) { + *result = SIGRTMAX - off; + return 0; + } } } } @@ -141,10 +152,12 @@ sudo_str2sig(const char *signame, int *result) break; } - for (signo = 0; signo < NSIG; signo++) { - if (strcmp(signame, sudo_sys_signame[signo]) == 0) { - *result = signo; - return 0; + for (signo = 1; signo < NSIG; signo++) { + if (sudo_sys_signame[signo] != NULL) { + if (strcmp(signame, sudo_sys_signame[signo]) == 0) { + *result = signo; + return 0; + } } } -- 2.40.0