Skip to contentSkip to conversation

Improve signal handling using POSIX sigaction

PATCH In review [email protected]... 3 participants 13 messages Jan 28, 2023 – Aug 11, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Improve signal handling using POSIX sigaction

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (deferring) of signals during update not possible.

Improve this:

- Use sigaction to properly mask signals when modifying.

Notes:

Updating signal_reset() is handled in a follow up patch

SIG_SOURCE_CONNECTION_FAILED is retained in a hackish way. This value has the same meaning as SIG_SOURCE_SOFT everywhere except where the signal is printed. Looks cosmetic --- could be eliminated?

In pre_init_signal_catch() we ignore some unix signals, but the same signals from management are not ignored though both are treated as "HARD" signals. For example, during auth-user-pass query, "kill -SIGUSR1 <pid>" will be ignored, but "signal SIGUSR1" from management interface will cause M_FATAL and exit. This is the current behaviour, but could be improved?

This patch was originally submitted as 5/5 of the signals series. Now this is 1/2 of a new series with signal_reset changes moved to 2/2

---
 src/openvpn/errlevel.h |   1 +
 src/openvpn/sig.c      | 264 +++++++++++++++++++++++++++++++----------
 src/openvpn/socket.c   |   1 -
 3 files changed, 202 insertions(+), 64 deletions(-)
src/openvpn/errlevel.h +1 -0
index c69ea91d..dedc0790 100644
@@ -115,6 +115,7 @@
 #define D_CLIENT_NAT         LOGLEV(6, 69, M_DEBUG)  /* show client NAT debug info */
 #define D_XKEY               LOGLEV(6, 69, M_DEBUG)  /* show xkey-provider debug info */
 #define D_DCO_DEBUG          LOGLEV(6, 69, M_DEBUG)  /* show DCO related lowlevel debug messages */
+#define D_SIGNAL_DEBUG       LOGLEV(6, 69, M_DEBUG)  /* show signal related debug messages */
 
 #define D_SHOW_KEYS          LOGLEV(7, 70, M_DEBUG)  /* show data channel encryption keys */
 #define D_SHOW_KEY_SOURCE    LOGLEV(7, 70, M_DEBUG)  /* show data channel key source entropy */
src/openvpn/sig.c +201 -63

349 lines not on this page · raw message

src/openvpn/socket.c +0 -1
index a883ac4a..baafe1e6 100644
@@ -1588,7 +1588,6 @@ socket_connect(socket_descriptor_t *sd,
         openvpn_close_socket(*sd);
         *sd = SOCKET_UNDEFINED;
         register_signal(sig_info, SIGUSR1, "connection-failed");
-        sig_info->source = SIG_SOURCE_CONNECTION_FAILED;
     }
     else
     {
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 signal_reset(): combine check and reset operations

From: Selva Nair <[email protected]...>

- "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal state between check and reset operations.

- Avoid resetting the signal except when absolutely necessary (resetting has the potential of losing signals)

- In 'pre_init_signal_catch()', when certain low priority signals are set to SIG_IGN, clear any pending signals of the same type. Also, reset signal at the end of the SIGUSR1 and
SIGHUP loops where their values are checked instead of later. This avoids the need for 'signal_reset()' after SIGHUP or in 'init_instance()' which could cause a signal like SIGTERM to be lost.

---
 src/openvpn/init.c    |  3 ---
 src/openvpn/multi.c   |  5 ++---
 src/openvpn/openvpn.c |  5 ++---
 src/openvpn/sig.c     | 40 +++++++++++++++++++++++++++++++++-------
 src/openvpn/sig.h     |  7 ++++++-
 src/openvpn/socket.c  |  5 ++---
 src/openvpn/win32.c   |  2 +-
 7 files changed, 46 insertions(+), 21 deletions(-)
src/openvpn/init.c +0 -3
index b500d354..76a7be7b 100644
@@ -4299,9 +4299,6 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f
         do_inherit_env(c, env);
     }
 
-    /* signals caught here will abort */
-    signal_reset(c->sig);
-
     if (c->mode == CM_P2P)
     {
         init_management_callback_p2p(c);
src/openvpn/multi.c +2 -3
index f2559016..c52c8f14 100644
@@ -3868,7 +3868,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
                        &m->deferred_shutdown_signal.wakeup,
                        compute_wakeup_sigma(&m->deferred_shutdown_signal.wakeup));
 
-    signal_reset(m->top.sig);
+    signal_reset(m->top.sig, 0);
 }
 
 /*
@@ -3878,12 +3878,11 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
 bool
 multi_process_signal(struct multi_context *m)
 {
-    if (m->top.sig->signal_received == SIGUSR2)
+    if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2)
     {
         struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
         multi_print_status(m, so, m->status_file_version);
         status_close(so);
-        signal_reset(m->top.sig);
         return false;
     }
     else if (proto_is_dgram(m->top.options.ce.proto)
src/openvpn/openvpn.c +2 -3
index cba58276..ad0aa8a2 100644
@@ -194,7 +194,6 @@ openvpn_main(int argc, char *argv[])
             context_clear_all_except_first_time(&c);
 
             /* static signal info object */
-            CLEAR(siginfo_static);
             c.sig = &siginfo_static;
 
             /* initialize garbage collector scoped to context object */
@@ -333,14 +332,14 @@ openvpn_main(int argc, char *argv[])
                 /* pass restart status to management subsystem */
                 signal_restart_status(c.sig);
             }
-            while (c.sig->signal_received == SIGUSR1);
+            while (signal_reset(c.sig, SIGUSR1) == SIGUSR1);
 
             env_set_destroy(c.es);
             uninit_options(&c.options);
             gc_reset(&c.gc);
             uninit_early(&c);
         }
-        while (c.sig->signal_received == SIGHUP);
+        while (signal_reset(c.sig, SIGHUP) == SIGHUP);
     }
 
     context_gc_free(&c);
src/openvpn/sig.c +33 -7
index 559ca35d..4eead996 100644
@@ -259,15 +259,37 @@ register_signal(struct signal_info *si, int signum, const char *signal_text)
     }
 }
 
-void
-signal_reset(struct signal_info *si)
+/**
+ * Clear the signal if its current value equals signum. If
+ * signum is zero the signal is cleared independent of its current
+ * value. Returns the current value of the signal.
+ */
+int
+signal_reset(struct signal_info *si, int signum)
 {
+    int sig_saved = 0;
     if (si)
     {
-        si->signal_received = 0;
-        si->signal_text = NULL;
-        si->source = SIG_SOURCE_SOFT;
+        if (si == &siginfo_static) /* attempting to alter the global signal */
+        {
+            block_async_signals();
+        }
+
+        sig_saved = si->signal_received;
+        if (!signum || sig_saved == signum)
+        {
+            si->signal_received = 0;
+            si->signal_text = NULL;
+            si->source = SIG_SOURCE_SOFT;
+            msg(D_SIGNAL_DEBUG, "signal_reset: signal %s is cleared", signal_name(signum, true));
+        }
+
+        if (si == &siginfo_static)
+        {
+            unblock_async_signals();
+        }
     }
+    return sig_saved;
 }
 
 void
@@ -397,6 +419,10 @@ pre_init_signal_catch(void)
     sigaction(SIGUSR2, &sa, NULL);
     sigaction(SIGPIPE, &sa, NULL);
 #endif /* _WIN32 */
+    /* clear any pending signals of the ignored type */
+    signal_reset(&siginfo_static, SIGUSR1);
+    signal_reset(&siginfo_static, SIGUSR2);
+    signal_reset(&siginfo_static, SIGHUP);
 }
 
 void
@@ -534,7 +560,7 @@ process_explicit_exit_notification_init(struct context *c)
      * will be ignored during the exit notification period.
      */
     halt_low_priority_signals(); /* Set hard SIGUSR1/SIGHUP/SIGUSR2 to be ignored */
-    signal_reset(c->sig);
+    signal_reset(c->sig, 0);
 
     c->c2.explicit_exit_notification_time_wait = now;
 
@@ -585,7 +611,7 @@ process_sigusr2(const struct context *c)
     struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
     print_status(c, so);
     status_close(so);
-    signal_reset(c->sig);
+    signal_reset(c->sig, SIGUSR2);
 }
 
 static bool
src/openvpn/sig.h +6 -1
index 4858eb93..7d76389a 100644
@@ -81,7 +81,12 @@ void register_signal(struct signal_info *si, int sig, const char *text);
 
 void process_explicit_exit_notification_timer_wakeup(struct context *c);
 
-void signal_reset(struct signal_info *si);
+/**
+ * Clear the signal if its current value equals signum. If signum is
+ * zero the signal is cleared independent of its current value.
+ * @returns the current value of the signal.
+ */
+int signal_reset(struct signal_info *si, int signum);
 
 static inline void
 halt_non_edge_triggered_signals(void)
src/openvpn/socket.c +2 -3
index baafe1e6..a2af2498 100644
@@ -567,12 +567,11 @@ openvpn_getaddrinfo(unsigned int flags,
                 if (sig_info->signal_received) /* were we interrupted by a signal? */
                 {
                     /* why are we overwriting SIGUSR1 ? */
-                    if (sig_info->signal_received == SIGUSR1) /* ignore SIGUSR1 */
+                    if (signal_reset(sig_info, SIGUSR1) == SIGUSR1) /* ignore SIGUSR1 */
                     {
                         msg(level,
                             "RESOLVE: Ignored SIGUSR1 signal received during "
                             "DNS resolution attempt");
-                        signal_reset(sig_info);
                     }
                     else
                     {
@@ -2176,7 +2175,7 @@ link_socket_init_phase2(struct context *c)
     if (sig_info->signal_received)
     {
         sig_save = *sig_info;
-        signal_reset(sig_info);
+        sig_save.signal_received = signal_reset(sig_info, 0);
     }
 
     /* initialize buffers */
src/openvpn/win32.c +1 -1
index 44176936..60e6c9fd 100644
@@ -677,7 +677,7 @@ win32_signal_get(struct win32_signal *ws)
     }
     if (ret)
     {
-        throw_signal(ret); /* this will update signinfo_static.signal received */
+        throw_signal(ret); /* this will update siginfo_static.signal received */
     }
     return (siginfo_static.signal_received);
 }
Signature · 1 line
2.34.1
Acked-by on v1 1/2
#6 lore raw
In reply to [email protected]...
Quoted 11 lines · On Sat, Jan 28, 2023 at 04:59:00PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (deferring) of signals during update not possible.

Improve this:

- Use sigaction to properly mask signals when modifying.

Stared at code intensively. AFAICT this should not change the general behavior except to be more generally safe.

Only compile+t_client tested.

Regards,

Signature · 1 line
  Frank Lichtenheld
author
#7 lore raw

Hi Gert,

Now that 2.6 appears to have reached a fairly stable state, may I request you to look into this patch for 2.7 -- this one has an ACK (thanks to Frank), 2/2 may need a closer look but that one is small.

I dread the prospect of this developing serious merge conflicts and having to drill down into the details to resolve them. Right now it looks like no one has yet touched related chunks.

Thanks,

Selva

Quoted 24 lines · Frank Lichtenheld
Quoted 11 lines · On Sat, Jan 28, 2023 at 04:59:00PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (deferring) of signals during update not possible.

Improve this:

- Use sigaction to properly mask signals when modifying.

Stared at code intensively. AFAICT this should not change the general behavior except to be more generally safe.

Only compile+t_client tested.

Regards,
--
Frank Lichtenheld

#8 lore raw

Hi,

Quoted 7 lines · Selva Nair

Now that 2.6 appears to have reached a fairly stable state, may I request you to look into this patch for 2.7 -- this one has an ACK (thanks to Frank), 2/2 may need a closer look but that one is small.

I dread the prospect of this developing serious merge conflicts and having to drill down into the details to resolve them. Right now it looks like no one has yet touched related chunks.

Will not really have much available time next week, but I have heard the message - will look into this the week after. If not, please feel free to send me another reminder.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
author
#9 lore raw
Quoted 8 lines · Gert Doering

Hi,

Quoted 5 lines · Selva Nair

Now that 2.6 appears to have reached a fairly stable state, may I request you to look into this patch for 2.7 -- this one has an ACK (thanks to Frank), 2/2 may need a closer look but that one is small.

I dread the prospect of this developing serious merge conflicts and

Quoted 9 lines

having

Quoted 1 line

to drill down into the details to resolve them. Right now it looks like

no

Quoted 1 line

one has yet touched related chunks.

Will not really have much available time next week, but I have heard the message - will look into this the week after. If not, please feel free to send me another reminder.

Here it is :)

Selva

#10 lore raw
In reply to [email protected]...

Sorry for taking so long on this - it hit post-release fatigue...

I did test this by running t_client test (which use signals to kill running openvpn) across all supported OSes, and server side tests on Linux, with and without DCO. Of course this is not really excercising the signal handling code, but at least it confirms that nothing is grossly broken.

Stared at the code & the manpages as well. Following my path through the twisty maze of passages (especially halt_low_priority_signals()) was not easy, but "it seems to make sense" (this blocks signals, but there is no new code to unblock - if I'm reading this right, this is because we are at "exit (single) instance" time, and unblocking signals will be done at init_instance_handle_signals() on the reconnection).

The actual "change to POSIX ways" part of this is fairly trivial and easy to understand :-) - though I do wonder why you're using an extra variable for block_mask -> sa.sa_mask, and not using sigfillset(&sa.sa_mask) - at least on BSD on Linux, both are sigset_t, so it should do the same thing in a slightly more direct way.

Your patch has been applied to the master branch.

commit 3abfc0fb5ea522fb5fc45fddbf35c3cc5c8e9ef1

Author: Selva Nair
Date:   Sat Jan 28 16:59:00 2023 -0500
     Improve signal handling using POSIX sigaction

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26087.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

author
#11 lore raw
Quoted 7 lines · Gert Doering

The actual "change to POSIX ways" part of this is fairly trivial and easy to understand :-) - though I do wonder why you're using an extra variable for block_mask -> sa.sa_mask, and not using sigfillset(&sa.sa_mask) - at least on BSD on Linux, both are sigset_t, so it should do the same thing in a slightly more direct way.

I can't recall any particular reason for having that intermediate variable. It does look redundant.

Now I wish someone could review 2/2 of this patch-set. That's where further potential
loss of signal during signal-reset is avoided.

Selva

#12 lore raw

Hi,

Quoted 2 lines · Selva Nair

Now I wish someone could review 2/2 of this patch-set. That's where further potential loss of signal during signal-reset is avoided.

Working on finding a vict^Wvolunteer :-)

(I've briefly looked over 2/2 and didn't find anything "obviously problematic", but this needs more brains and life is a bit chaotic over here right now - sufficient brains to test & merge, not enough to really understand all the fine details involved here)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#2 lore raw

Re: [Openvpn-devel] [PATCH 2/2] signal_reset(): combine check and reset operations

In reply to [email protected]...
Quoted 18 lines · On Sat, Jan 28, 2023 at 04:59:01PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal state between check and reset operations.

- Avoid resetting the signal except when absolutely necessary (resetting has the potential of losing signals)

- In 'pre_init_signal_catch()', when certain low priority signals are set to SIG_IGN, clear any pending signals of the same type. Also, reset signal at the end of the SIGUSR1 and
SIGHUP loops where their values are checked instead of later. This avoids the need for 'signal_reset()' after SIGHUP or in 'init_instance()' which could cause a signal like SIGTERM to be lost.

[...]

src/openvpn/openvpn.c openvpn_main(int argc, char *argv[]) · line 197

Quoted 10 lines
src/openvpn/openvpn.c +0 -1
index cba58276..ad0aa8a2 100644
@@ -194,7 +194,6 @@ openvpn_main(int argc, char *argv[])
             context_clear_all_except_first_time(&c);
 
             /* static signal info object */
-            CLEAR(siginfo_static);
             c.sig = &siginfo_static;

Is that actually save? Doesn't that mean that siginfo_static might be used uninitialized?

Regards,

Signature · 1 line
  Frank Lichtenheld
author
#3 lore raw

Re: [Openvpn-devel] [PATCH 2/2] signal_reset(): combine check and reset operations

Quoted 28 lines · Frank Lichtenheld
Quoted 8 lines · On Sat, Jan 28, 2023 at 04:59:01PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal state between check and reset operations.

Quoted 8 lines

- Avoid resetting the signal except when absolutely necessary (resetting has the potential of losing signals)

- In 'pre_init_signal_catch()', when certain low priority signals are set to SIG_IGN, clear any pending signals of the same type. Also, reset signal at the end of the SIGUSR1 and SIGHUP loops where their values are checked instead of later. This avoids the need for 'signal_reset()' after SIGHUP or in

'init_instance()'

Quoted 2 lines

which could cause a signal like SIGTERM to be lost.

[...]

Quoted 7 lines
src/openvpn/openvpn.c +0 -0
index cba58276..ad0aa8a2 100644
@@ -194,7 +194,6 @@ openvpn_main(int argc, char *argv[])
             context_clear_all_except_first_time(&c);
 
Quoted 7 lines

src/openvpn/openvpn.c openvpn_main(int argc, char *argv[]) · line 197

Quoted 3 lines
src/openvpn/openvpn.c +0 -1
             /* static signal info object */
-            CLEAR(siginfo_static);
             c.sig = &siginfo_static;

Is that actually save? Doesn't that mean that siginfo_static might be used uninitialized?

siginfo_static is a global variable with no explicit initialization, so it is guaranteed to
get initialized to {0} at start up.

Selva

#4 lore raw

Re: [Openvpn-devel] [PATCH 2/2] signal_reset(): combine check and reset operations

Quoted 39 lines · Selva Nair
Quoted 28 lines · Frank Lichtenheld
Quoted 8 lines · On Sat, Jan 28, 2023 at 04:59:01PM -0500, [email protected]... wrote:

From: Selva Nair <[email protected]...>

- "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal state between check and reset operations.

Quoted 8 lines

- Avoid resetting the signal except when absolutely necessary (resetting has the potential of losing signals)

- In 'pre_init_signal_catch()', when certain low priority signals are set to SIG_IGN, clear any pending signals of the same type. Also, reset signal at the end of the SIGUSR1 and SIGHUP loops where their values are checked instead of later. This avoids the need for 'signal_reset()' after SIGHUP or in

'init_instance()'

Quoted 2 lines

which could cause a signal like SIGTERM to be lost.

[...]

Quoted 7 lines
src/openvpn/openvpn.c +0 -0
index cba58276..ad0aa8a2 100644
@@ -194,7 +194,6 @@ openvpn_main(int argc, char *argv[])
             context_clear_all_except_first_time(&c);
 
Quoted 7 lines

src/openvpn/openvpn.c openvpn_main(int argc, char *argv[]) · line 197

Quoted 3 lines
src/openvpn/openvpn.c +0 -1
             /* static signal info object */
-            CLEAR(siginfo_static);
             c.sig = &siginfo_static;

Is that actually save? Doesn't that mean that siginfo_static might be used uninitialized?

Quoted 3 lines

siginfo_static is a global variable with no explicit initialization, so it is guaranteed to
get initialized to {0} at start up.

Right.

Stared at code, all makes sense to me. Didn't do any testing specific to the changes.

Regards,

Signature · 1 line
  Frank Lichtenheld
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: signal_reset(): combine check and reset operations

In reply to [email protected]...

Has taken us long enough...

Tested in the server threadmill (Linux), and on buildbot/GHA (all the OSes). No explosions.

Also, stared a bit at the code, if Frank has overlooked anything, just for completeness - haven't found anything :-)

Your patch has been applied to the master branch.

commit a854a7f30c129049ae55b51c22208fad97e80250

Author: Selva Nair
Date:   Sat Jan 28 16:59:01 2023 -0500
     signal_reset(): combine check and reset operations

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26088.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering