Skip to contentSkip to conversation

Clean-up of the systemd integration

PATCH NAK [email protected]... 2 participants 10 messages Sep 5, 2014 – Sep 9, 2014

lore
Share & tools
lore
#0 lore raw

cover Clean-up of the systemd integration

From: David Sommerseth <[email protected]...>

There were a few issues with the systemd implementation, where the openvpn binary would have zombie processes attached to itself when systemd-ask-password was used. In addition to not always properly closing the communcation pipes used.

This patch set also avoids OpenVPN failing if systemd-ask-password is not found, but rather falls back to the normal console method.

David Sommerseth (4):
Don't let openvpn_popen() keep zombies around Don't try to use systemd-ask-password if it is not available Clean up the pipe closing in openvpn_popen() White-space clean-up of openvpn_popen()

 src/openvpn/console.c | 11 ++++----
 src/openvpn/misc.c    | 75 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 47 insertions(+), 39 deletions(-)

--
kind regards,

David Sommerseth

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/4 Don't let openvpn_popen() keep zombies around

From: David Sommerseth <[email protected]...>

Commit 9449e6a9eba30c9ed054f57d630a88c9f087080f introduced the openvpn_popen() function to support retrieving passwords via systemd.

It was discovered that the child processes openvpn fork()ed would be lingering around until openvpn stopped. This was due to the lack of a wait() call.

This patch also cleans up a few minor white-space issues in the same code segment.

[v2 proper initialisation of status variable]

---
 src/openvpn/misc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
src/openvpn/misc.c +5 -2
index 63b4c1c..19bbef7 100644
@@ -376,8 +376,11 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
 			}
 		      else /* parent side */
 			{
-                            ret=pipe_stdout[0];
-			    close (pipe_stdout[1]);
+                          int status = 0;
+
+                          waitpid(pid, &status, 0);
+                          ret = pipe_stdout[0];
+                          close (pipe_stdout[1]);
 			}
 	      }
 	      else {
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 2/4 Don't try to use systemd-ask-password if it is not available

In reply to [email protected]...

From: David Sommerseth <[email protected]...>

If the SYSTEMD_ASK_PASSWORD_PATH executable cannot be found, we don't consider systemd as running.

Also cleaned up some white-space issues here while at it.

---
 src/openvpn/console.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
src/openvpn/console.c +6 -5
index 059b6f0..e35b6bd 100644
@@ -143,15 +143,16 @@ close_tty (FILE *fp)
 static bool
 check_systemd_running ()
 {
-  struct stat a, b;
+  struct stat a, b, c;
 
   /* We simply test whether the systemd cgroup hierarchy is
-   * mounted */
+   * mounted, as well as the systemd-ask-password executable
+   * being available */
 
   return (lstat("/sys/fs/cgroup", &a) == 0)
-	  && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
-	  && (a.st_dev != b.st_dev);
-
+    && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
+    && (stat(SYSTEMD_ASK_PASSWORD_PATH, &c) == 0)
+    && (a.st_dev != b.st_dev);
 }
 
 static bool
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH 3/4 Clean up the pipe closing in openvpn_popen()

In reply to [email protected]...

From: David Sommerseth <[email protected]...>

Do the proper closing of the pipe ends which is not being used, also in error situations. Moved the closing of the parent side before the waitpid(), to be consistent with the child side (as early as possible).

Also improved the error messages with more details.

---
 src/openvpn/misc.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
src/openvpn/misc.c +11 -8
index 19bbef7..daf4900 100644
@@ -365,27 +365,30 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
 		      pid = fork ();
 		      if (pid == (pid_t)0) /* child side */
 			{
-			  close (pipe_stdout[0]);
+			  close (pipe_stdout[0]);  /* Close read end */
 			  dup2 (pipe_stdout[1],1);
 			  execve (cmd, argv, envp);
+			  close (pipe_stdout[1]);
 			  exit (127);
 			}
-		      else if (pid < (pid_t)0) /* fork failed */
-			{
-			  msg (M_ERR, "openvpn_popen: unable to fork");
-			}
-		      else /* parent side */
+		      else if (pid > (pid_t)0) /* parent side */
 			{
                           int status = 0;
 
+                          close (pipe_stdout[1]); /* Close write end */
                           waitpid(pid, &status, 0);
                           ret = pipe_stdout[0];
+			}
+		      else /* fork failed */
+			{
+                          close (pipe_stdout[0]);
                           close (pipe_stdout[1]);
+			  msg (M_ERR, "openvpn_popen: unable to fork %s", cmd);
 			}
 	      }
 	      else {
-		      msg (M_WARN, "openvpn_popen: unable to create stdout pipe");
-		      ret = -1;
+                msg (M_WARN, "openvpn_popen: unable to create stdout pipe for %s", cmd);
+                ret = -1;
 	      }
 	}
       else if (!warn_shown && (script_security < SSEC_SCRIPTS))
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 4/4 White-space clean-up of openvpn_popen()

In reply to [email protected]...

From: David Sommerseth <[email protected]...>

The whole coding style was a mishmash of tab/spaces and different alignments.

Reformatted to 2 space indents (no tabs).

---
 src/openvpn/misc.c | 81 +++++++++++++++++++++++++++---------------------------
 1 file changed, 41 insertions(+), 40 deletions(-)
src/openvpn/misc.c +41 -40
index daf4900..d7cbb8c 100644
@@ -354,48 +354,49 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
     {
 #if defined(ENABLE_FEATURE_EXECVE)
       if (script_security >= SSEC_BUILT_IN)
-	{
-	      const char *cmd = a->argv[0];
-	      char *const *argv = a->argv;
-	      char *const *envp = (char *const *)make_env_array (es, true, &gc);
-	      pid_t pid;
-	      int pipe_stdout[2];
-
-              if (pipe (pipe_stdout) == 0) {
-		      pid = fork ();
-		      if (pid == (pid_t)0) /* child side */
-			{
-			  close (pipe_stdout[0]);  /* Close read end */
-			  dup2 (pipe_stdout[1],1);
-			  execve (cmd, argv, envp);
-			  close (pipe_stdout[1]);
-			  exit (127);
-			}
-		      else if (pid > (pid_t)0) /* parent side */
-			{
-                          int status = 0;
-
-                          close (pipe_stdout[1]); /* Close write end */
-                          waitpid(pid, &status, 0);
-                          ret = pipe_stdout[0];
-			}
-		      else /* fork failed */
-			{
-                          close (pipe_stdout[0]);
-                          close (pipe_stdout[1]);
-			  msg (M_ERR, "openvpn_popen: unable to fork %s", cmd);
-			}
-	      }
-	      else {
-                msg (M_WARN, "openvpn_popen: unable to create stdout pipe for %s", cmd);
-                ret = -1;
-	      }
-	}
+        {
+          const char *cmd = a->argv[0];
+          char *const *argv = a->argv;
+          char *const *envp = (char *const *)make_env_array (es, true, &gc);
+          pid_t pid;
+          int pipe_stdout[2];
+
+          if (pipe (pipe_stdout) == 0) {
+            pid = fork ();
+            if (pid == (pid_t)0) /* child side */
+              {
+                close (pipe_stdout[0]);  /* Close read end */
+                dup2 (pipe_stdout[1],1);
+                execve (cmd, argv, envp);
+                close (pipe_stdout[1]);
+                exit (127);
+              }
+            else if (pid > (pid_t)0) /* parent side */
+              {
+                int status = 0;
+
+                close (pipe_stdout[1]); /* Close write end */
+                waitpid(pid, &status, 0);
+                ret = pipe_stdout[0];
+              }
+            else /* fork failed */
+              {
+                close (pipe_stdout[0]);
+                close (pipe_stdout[1]);
+                msg (M_ERR, "openvpn_popen: unable to fork %s", cmd);
+              }
+          }
+          else
+            {
+              msg (M_WARN, "openvpn_popen: unable to create stdout pipe for %s", cmd);
+              ret = -1;
+            }
+        }
       else if (!warn_shown && (script_security < SSEC_SCRIPTS))
-	{
-	  msg (M_WARN, SCRIPT_SECURITY_WARNING);
+        {
+          msg (M_WARN, SCRIPT_SECURITY_WARNING);
           warn_shown = true;
-	}
+        }
 #else
       msg (M_WARN, "openvpn_popen: execve function not available");
 #endif
Signature · 1 line
1.8.3.1
#9 lore raw

Re: [Openvpn-devel] [PATCH 4/4] White-space clean-up of openvpn_popen()

Hi,

Quoted 1 line · On Fri, Sep 05, 2014 at 05:25:31PM +0200, [email protected]... wrote:

Reformatted to 2 space indents (no tabs).

NAK, as our current coding style is "2 space indents, tab=8" - even if not consistently used everywhere, *if* we do reformatting at this stage, do it towards the current style.

(I agree that we might want to reformat prior to releasing 2.4, and should discuss this in Munich)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#2 lore raw

Re: [Openvpn-devel] [PATCH 1/4] Don't let openvpn_popen() keep zombies around

In reply to [email protected]...

Hi,

Quoted 5 lines · On Fri, Sep 05, 2014 at 05:25:28PM +0200, [email protected]... wrote:

From: David Sommerseth <[email protected]...>

Commit 9449e6a9eba30c9ed054f57d630a88c9f087080f introduced the openvpn_popen() function to support retrieving passwords via systemd.

Quoted 8 lines

It was discovered that the child processes openvpn fork()ed would be lingering around until openvpn stopped. This was due to the lack of a wait() call.

This patch also cleans up a few minor white-space issues in the same code segment.

[v2 proper initialisation of status variable]

ACK.

(As a side note: not that the initialization of "status" makes any sense here :-), as even if waitpid() wouldn't set it due to some freak incident, it will never be used afterwards. But the general introduction of wait() in one of its forms is needed here, yes)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw

Re: [Openvpn-devel] [PATCH 2/4] Don't try to use systemd-ask-password if it is not available

In reply to [email protected]...

Hi,

src/openvpn/console.c close_tty (FILE *fp) · line 155

Quoted 7 lines · On Fri, Sep 05, 2014 at 05:25:29PM +0200, [email protected]... wrote:
   return (lstat("/sys/fs/cgroup", &a) == 0)
src/openvpn/console.c +3 -3
-	  && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
-	  && (a.st_dev != b.st_dev);
-
+    && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
+    && (stat(SYSTEMD_ASK_PASSWORD_PATH, &c) == 0)
+    && (a.st_dev != b.st_dev);

ACK on the code, NAK on the indent change. The old code had tabs there,
the new one has spaces - and not enough of them anyway.

For clarity, you might want to keep the "a.st_dev != b.st_dev" comparison befor the stat() on SYSTEMD_ASK_PASSWORD_PATH, as the comparison belongs to the /sys/fs/... stuff, not to the third stat().

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#7 lore raw

Re: [Openvpn-devel] [PATCH 3/4] Clean up the pipe closing in openvpn_popen()

In reply to [email protected]...

Hi,

src/openvpn/misc.c openvpn_popen (const struct argv *a, const struct env_set *es) · line 372

Quoted 10 lines · On Fri, Sep 05, 2014 at 05:25:30PM +0200, [email protected]... wrote:
src/openvpn/misc.c +2 -1
@@ -365,27 +365,30 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
 		      pid = fork ();
 		      if (pid == (pid_t)0) /* child side */
 			{
-			  close (pipe_stdout[0]);
+			  close (pipe_stdout[0]);  /* Close read end */
 			  dup2 (pipe_stdout[1],1);
 			  execve (cmd, argv, envp);
+			  close (pipe_stdout[1]);
 			  exit (127);

NAK, as that addition does not have any benefit. Even if the execve() fails, the next thing is an exit(), which will close all file descriptors anyway. It's just extra code that impairs reading.

The rest of the change would get an feature- and code-ACK, though :)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#3 lore raw

Re: [Openvpn-devel] [PATCH applied] Don't let openvpn_popen() keep zombies around

In reply to [email protected]...

From: David Sommerseth <[email protected]...>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Your patch has been applied to the master branch.

commit d886d468849051af525bb8ff1b9080f6c934e3ab

Author: David Sommerseth
Date:   Fri Sep 5 14:26:32 2014 +0200
     Don't let openvpn_popen() keep zombies around

     Signed-off-by: David Sommerseth <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: [email protected]...
     URL: http://article.gmane.org/gmane.network.openvpn.devel/9021

- --
kind regards,

David Sommerseth

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iEYEARECAAYFAlQPNjMACgkQDC186MBRfrqEIgCdFYeGJIQZIq6IpZQhhFHx5mxx LG8An3/LUlCTTIBq+UIkdyqOc1cRXOgW
=K4tA
-----END PGP SIGNATURE-----