Skip to contentSkip to conversation

give plugins a method to pass log lines to openvpn

RFC In review Heiko Hund 2 participants 5 messages Jul 30, 2012 – Jul 31, 2012

lore
Share & tools
lore

Heiko Hund

author
#0 lore raw

RFC 1/1 give plugins a method to pass log lines to openvpn

Some plugins want to add messages to the openvpn log file.

V3 plugins can indirectly write to the log by specifying one or more openvpn_plugin_log_list structs and returning them in the provided openvpn_plugin_args_{open,func}_return struct.

The openvpn plugin backend will log the provided information upon reception.

---
 include/openvpn-plugin.h |   39 ++++++++++++++++++++++++++++++++++++++-
 src/openvpn/plugin.c     |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)
include/openvpn-plugin.h +38 -1
index 1c80eec..08db7b2 100644
@@ -201,8 +201,39 @@ struct openvpn_plugin_string_list
  *    1      Initial plugin v3 structures providing the same API as
  *           the v2 plugin interface + X509 certificate information.
  *
+ *    2      Add log_list to openvpn_plugin_args_{open,func}_return
+ *           structs. The log_list struct is evaluated by openvpn and
+ *           enables plugins to add information to the openvpn log
+ *           output.
+ *
+ */
+#define OPENVPN_PLUGINv3_STRUCTVER 2
+
+
+typedef enum
+{
+  PLOG_ERR       = (1 << 0),  /* Error condition message */
+  PLOG_WARN      = (1 << 1),  /* General warning message */
+  PLOG_NOTE      = (1 << 2),  /* Informational message */
+  PLOG_DEBUG     = (1 << 3),  /* Debug message, displayed if verb >= 7 */
+
+  PLOG_NOMUTE    = (1 << 8),  /* Mute setting does not apply for message */
+  PLOG_NOIPREFIX = (1 << 9)   /* No instance prefix for this message */
+
+} openvpn_plugin_log_flags_t;
+
+/**
+ * Struct to transport log lines from the plugin to openvpn.
+ * The plugin should allocate all structure instances and msg strings
+ * with malloc, since OpenVPN will free them after logging took place.
  */
-#define OPENVPN_PLUGINv3_STRUCTVER 1
+struct openvpn_plugin_log_list
+{
+  struct openvpn_plugin_log_list *next;
+  const openvpn_plugin_log_flags_t flags;
+  const char *msg;
+};
+
 
 /**
  * Arguments used to transport variables to the plug-in.
@@ -250,12 +281,15 @@ struct openvpn_plugin_args_open_in
  *
  * return_list : used to return data back to OpenVPN.
  *
+ * log_list : used to return log lines back to OpenVPN.
+ *
  */
 struct openvpn_plugin_args_open_return
 {
   int  type_mask;
   openvpn_plugin_handle_t *handle;
   struct openvpn_plugin_string_list **return_list;
+  struct openvpn_plugin_log_list *log_list;
 };
 
 /**
@@ -313,10 +347,13 @@ struct openvpn_plugin_args_func_in
  * return_list : used to return data back to OpenVPN for further processing/usage by
  *               the OpenVPN executable.
  *
+ * log_list : used to return log lines back to OpenVPN.
+ *
  */
 struct openvpn_plugin_args_func_return
 {
   struct openvpn_plugin_string_list **return_list;
+  struct openvpn_plugin_log_list *log_list;
 };
 
 /*
src/openvpn/plugin.c +37 -0
index 7ce2f5e..f4833df 100644
@@ -286,6 +286,41 @@ plugin_init_item (struct plugin *p, const struct plugin_option *o)
   gc_free (&gc);
 }
 
+
+static void
+plugin_do_log_list (struct openvpn_plugin_log_list *log_list)
+{
+  while (log_list)
+    {
+      struct openvpn_plugin_log_list *this = log_list;
+      if (log_list->msg)
+        {
+          unsigned int flags;
+
+          if (log_list->flags & PLOG_ERR)
+            flags = M_INFO | M_NONFATAL;
+          else if (log_list->flags & PLOG_WARN)
+            flags = M_INFO | M_WARN;
+          else if (log_list->flags & PLOG_NOTE)
+            flags = M_INFO;
+          else if (log_list->flags & PLOG_DEBUG)
+            flags = D_PLUGIN_DEBUG | M_DEBUG;
+
+          if (log_list->flags & PLOG_NOMUTE)
+            flags |= M_NOMUTE;
+          if (log_list->flags & PLOG_NOIPREFIX)
+            flags |= M_NOIPREFIX;
+
+          msg (flags, log_list->msg);
+          free (log_list->msg);
+        }
+
+      log_list = log_list->next;
+      free (this);
+    }
+}
+
+
 static void
 plugin_open_item (struct plugin *p,
 		  const struct plugin_option *o,
@@ -317,6 +352,7 @@ plugin_open_item (struct plugin *p,
 
         CLEAR(retargs);
         if ((*p->open3)(OPENVPN_PLUGINv3_STRUCTVER, &args, &retargs) == OPENVPN_PLUGIN_FUNC_SUCCESS) {
+          plugin_do_log_list(retargs.log_list);
           p->plugin_type_mask = retargs.type_mask;
           p->plugin_handle = retargs.handle;
           retlist = retargs.return_list;
@@ -399,6 +435,7 @@ plugin_call_item (const struct plugin *p,
 
         CLEAR(retargs);
         status = (*p->func3)(OPENVPN_PLUGINv3_STRUCTVER, &args, &retargs);
+        plugin_do_log_list(retargs.log_list);
         retlist = retargs.return_list;
       } else if (p->func2)
 	status = (*p->func2)(p->plugin_handle, type, (const char **)a.argv, envp, per_client_context, retlist);
Signature · 1 line
1.7.10.4
#1 lore raw

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

Quoted 6 lines · Heiko Hund

Some plugins want to add messages to the openvpn log file.

V3 plugins can indirectly write to the log by specifying one or more openvpn_plugin_log_list structs and returning them in the provided openvpn_plugin_args_{open,func}_return struct.

Quoted 8 lines

The openvpn plugin backend will log the provided information upon reception.

include/openvpn-plugin.h | 39
++++++++++++++++++++++++++++++++++++++- src/openvpn/plugin.c |
37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-)

Just a few quick thoughts. As the implementer of the v3 API, I'm happy to see such attempts. And as the developer of eurephia (which is an authentication plug-in for OpenVPN), I see the need for such a feature.

But I'm not fully convinced yet this is the proper way. In many ways, it is elegant, but in some parts I feel it falls short too. But it is a good starting point for a discussion.

The advantage of the log struct is that the plug-in can have far better control on how to format the log data. But it raises the complexity as each plug-in with this proposal needs to write their own logging mechanism which does the pointer-chain stuff. And adding new structs have the pitfall that it's easy to not be ABI (not API) compliant across versions, something the v3 plug-in API strives for. I would expect the vast majority of all plug-ins don't require this kind of flexibility.

My immediate thought/question is: Why can we not export msg() to plug-ins? Or at least provide something more like a functional oriented API to work with?

What if we extend the openvpn_plugin_args_open_in struct to include a function pointer to a log function? This log function can provide somewhat the same feature set as msg(). But it should also prefix these log lines as coming from a plug-in (preferably with the plug-in name included)... plug-ins wanting to log, can then save that function pointer and call this directly.

It's not so flexible as this current proposal can be, but it should be easier to provide a more predictable ABI for the future, And it would cover the use case for most of the plug-ins.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlAWvvcACgkQDC186MBRfrpQ2ACeMoAv/ugw+V0SMvxUSLm8iXw/ AWoAnj8O8c9OV0BmpC37MpFuSr0qGsgh
=2RqT
-----END PGP SIGNATURE-----

author
#2 lore raw

Hi David.

Thanks for the feedback.

Quoted 3 lines · On Monday 30 July 2012 19:06:02 David Sommerseth wrote:

My immediate thought/question is: Why can we not export msg() to plug-ins? Or at least provide something more like a functional oriented API to work with?

Actually that's my favored way as well. I just thought I'd stick to the, admittedly rather strange, way plugins interact with openvpn for the start. I'll hack a patch that provides a plugin_log() function, with the same semantics as the struct based approach.

Quoted 3 lines

But it should also prefix
these log lines as coming from a plug-in (preferably with the plug-in name included)...

I don't think enforcing something like this makes much sense, as evil plugins, trying to forge log lines to appear originating from the core process, will just call x_msg() directly then.

Heiko

Signature · 2 lines
Heiko Hund | Sr. Software Engineer | Tel +49-721-25516-237 | Fax -200
SOPHOS NSG | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany
#3 lore raw

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

Quoted 6 lines · Heiko Hund
Quoted 2 lines

But it should also prefix these log lines as coming from a plug-in (preferably with the plug-in name included)...

I don't think enforcing something like this makes much sense, as evil plugins, trying to forge log lines to appear originating from the core process, will just call x_msg() directly then.

Agreed, the intention wasn't to nail evil plug-ins, it was more a convenience - as the plug-in itself doesn't need to "identify" itself.
The plug-in cares for what it wants to add to the log, and the rest
is automatic and will also be "tagged" consistently among all plug-ins.

- From another point of view, an evil plug-in would have to know/figure out the address to x_msg() ... so that would require a bit more work, at least to work against different distro/compiler/linker combinations, as it's not given the address will be the same. However, it's not necessarily hard to figure it out, but requires more work. And an evil plug-in would anyway need to first be installed somehow too. So I consider this attack vector less attractive.

And, AFAIK, you can't use dlsym() without a handle to a shared lib opened with dlopen(). And I don't think you can't export the log function in the openvpn-plugin.h file as an external function, as the plug-in would fail to link (I would expect the linker wouldn't find the required symbol) ... which then brings us back to the point where you need to send a pointer to the log function to the plug-in ... That's basically how I came to this conclusion.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlAXzBAACgkQDC186MBRfrrDDgCfdGlu8GK4oD4aELiskzgqmoXm yoIAni0vANC55rDK3AtV9BqIlaTtx0gj
=ezeg
-----END PGP SIGNATURE-----

author
#4 lore raw
Quoted 7 lines · On Tuesday 31 July 2012 14:14:10 David Sommerseth wrote:

- From another point of view, an evil plug-in would have to know/figure out the address to x_msg() ... so that would require a bit more work, at least to work against different distro/compiler/linker combinations, as it's not given the address will be the same. However, it's not necessarily hard to figure it out, but requires more work. And an evil plug-in would anyway need to first be installed somehow too. So I consider this attack vector less attractive.

All the symbols are exported to plugins, so it's just a matter of adding the function prototype and flag values, if you do care at all.

Quoted 7 lines

And, AFAIK, you can't use dlsym() without a handle to a shared lib opened with dlopen(). And I don't think you can't export the log function in the openvpn-plugin.h file as an external function, as the plug-in would fail to link (I would expect the linker wouldn't find the required symbol) ... which then brings us back to the point where you need to send a pointer to the log function to the plug-in ... That's basically how I came to this conclusion.

The dynamic linker resolves undefined symbols in the plugin automagically, so there's no need to get the address by hand. Haven't tested on Windows, but the symbols are there as well.

We could limit the exported functions by hand, but then plugins should not be considered evil. We kind of had the discussion during the interactive service privilege escalation discussion a while ago. If you let someone into your home, you better make sure you trust him anyway. =)

Heiko

Signature · 2 lines
Heiko Hund | Sr. Software Engineer | Tel +49-721-25516-237 | Fax -200
SOPHOS NSG | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany