OpenVPN Pf plugin/small status patch
Hi,
I have written a simple plugin for packet filtering that looks up fw rules in the order
Commonname.pf
IP_Port.pf
IP.pf
default.pf
If one of this files is found the file is used as PF configuration. Maybe this plugin is useful for someone else.
Add I would like to ask to include the patch for multi.c.
There are commands in the management interface which require the cid. The only way at the moment to get the cid of connected clients is to have always a management connection established. The patch adds the CID to the status output.
For my scenario is beneficial to have some standard pf rules in places and change the pf rules on a need basis for a certain, which I can only do If I have the CID.
Arne
enablepf/winbuild +19 -0
@@ -0,0 +1,19 @@ +# +# Build an OpenVPN plugin module on Windows/MinGW. +# The argument should be the base name of the C source file +# (without the .c). +# + +# This directory is where we will look for openvpn-plugin.h +INCLUDE="-I../.." + +CC_FLAGS="-O2 -Wall" + +gcc -DBUILD_DLL $CC_FLAGS $INCLUDE -c $1.c +gcc --disable-stdcall-fixup -mdll -DBUILD_DLL -o junk.tmp -Wl,--base-file,base.tmp $1.o +rm junk.tmp +dlltool --dllname $1.dll --base-file base.tmp --output-exp temp.exp --input-def $1.def +rm base.tmp +gcc --enable-stdcall-fixup -mdll -DBUILD_DLL -o $1.dll $1.o -Wl,temp.exp +rm temp.exp +
enablepf/build +14 -0
@@ -0,0 +1,14 @@ +#!/bin/sh + +# +# Build an OpenVPN plugin module on *nix. The argument should +# be the base name of the C source file (without the .c). +# + +# This directory is where we will look for openvpn-plugin.h +INCLUDE="-I../.." + +CC_FLAGS="-O2 -Wall -g" + +gcc $CC_FLAGS -fPIC -c $INCLUDE $1.c && \ +gcc $CC_FLAGS -fPIC -shared -Wl,-soname,$1.so -o $1.so $1.o -lc
enablepf/enablepf.def +6 -0
@@ -0,0 +1,6 @@ +LIBRARY OpenVPN_PLUGIN_ENABLEPF +DESCRIPTION "Enable OpenVPN Firewall policys." +EXPORTS + openvpn_plugin_open_v1 @1 + openvpn_plugin_func_v1 @2 + openvpn_plugin_close_v1 @3
enablepf/enablepf.c +199 -0
199 lines not on this page · raw message
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 5 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up fw rules in the order
Quoted 7 lines
Commonname.pf
IP_Port.pf
IP.pf
default.pfIf one of this files is found the file is used as PF configuration. Maybe this plugin is useful for someone else.
Hi!
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
How does this packet filtering further work? I do not completely understand how you imagine this should work. I see that it tries to open a number of files with different filename criteria , and if it finds a file it copies it somewhere.
But how does that itself really provide packet filtering?
The code itself is not bad, I'd like to have it cleaned up a little bit before inclusion. Things I'm picking on are consequent spacing, removing unneeded blank lines, improved comments (explaining *what* is happening).
In addition the copypffile() function can be written a bit more clever, avoiding using stack space for an array of file names. As you already have a for() loop, it would be better, IMHO, to build up the filename string in this loop, using a simple select() for the filename template.
You also have a potential buffer overflow bug with your snprintf(). If common name (the most likely one to attack) is longer than 256 bytes, the filename will consist of those 256 bytes without a NULL terminator, as snprintf() expect the NULL terminator to be included in the size/length provided.
I'd recommend something like:
char str[258]; // 256 bytes + 2 bytes extra
memset(&str, 0, 258); // Make sure it's NULL snprintf(str, 256, "%.253s.pf%c", cn, '\0');
or:
char *str = calloc(1, 258); // 256 + 2 extra snprintf(str, 256, "%.253s.pf%c", cn, '\0'); free(str);
This way str will never exceed 256 bytes, including the extension, and it will be NULL terminated for sure. In addition you are safer against buffer overflows. If using calloc(), the memory can be expected to be zeroed as well (one of the advantages of calloc() compared to malloc()).
Using the str pointer above in a for loop, you can just call snprintf() several times to change the contents, and free it after the for() loop.
I would also recommend to be stricter to the allowed string length when processing IP addresses, port numbers and device name. F.ex. the filename template for port number could be: "%.5s.pf", as a port number cannot be bigger than 65535, which is 5 digits.
And the last important thing, you have no error checking if copypffile() fails or not. OpenVPN will believe that this function call will always work. Granted, you if no files exist to be copied that might not be a failure at all. But it could be a failure if it could not write to the destination file when it does the copy.
You also have two functions which might not be needed as well, openvpn_plugin_client_contstructor_v1() and openvpn_plugin_client_destructor_v1(). They don't seem to do anything useful and OpenVPN don't require them, AFAIK.
The printf() messages could also specify the plug-in name instead of just generic function names ... especially important for users who use several plug-ins in parallel.
Quoted 6 lines
Add I would like to ask to include the patch for
multi.c.There are commands in the management interface which require the cid. The only way at the moment to get the cid of connected clients is to have always a management connection established. The patch adds the CID to the status output.
This patch is sent for review by more developers. It will need an official ACK before inclusion, so I hope that will come soon.
Thank you once again for your contributions!
kind regards,
David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAkuKbggACgkQDC186MBRfrrG6ACfWIuNyCPpufx1DwRmv2RGCqug 8TEAnjRy8EPYVfrlCXw6xdSNg0UPaJl2
=oaxR
-----END PGP SIGNATURE-----
Quoted 18 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 5 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up fw rules in the order
Quoted 7 lines
Commonname.pf
IP_Port.pf
IP.pf
default.pfIf one of this files is found the file is used as PF configuration. Maybe this plugin is useful for someone else.
Hi!
Quoted 8 lines
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
How does this packet filtering further work? I do not completely understand how you imagine this should work. I see that it tries to open a number of files with different filename criteria , and if it finds a file it copies it somewhere.
The packet filtering itself is already part of openvpn. It only works in tap mode iirc. See http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/pf.c. A description of the packet filter format is given in http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/management/management-notes.txt
COMMAND -- client-pf (OpenVPN 2.1 or higher)
This plugin/patch only add the possibility to use the packet filter functionality without use of the management interface.
Quoted 13 lines · David Sommerseth
Quoted 3 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up
fw
rulesQuoted 7 lines
in the order
Commonname.pf
IP_Port.pf
IP.pf
default.pf
Quoted 8 lines
Quoted 1 line
If one of this files is found the file is used as PF configuration.
Maybe
Quoted 1 line
this plugin is useful for someone else.
Hi!
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
I think that a vocabulary change could help avoid confusion, depending on exactly what the proposed vocabulary is here. "pf" is the offical name for the OpenBSD packet filter. IIRC it is also ported to FreeBSD and perhaps elsewhere. Using the name "pf" for this plugin could be confusing.
Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 6 lines · Karl O. Pinc
Quoted 6 lines
Quoted 6 lines
If one of this files is found the file is used as PF configuration. Maybe
Quoted 1 line
Quoted 1 line
this plugin is useful for someone else.
Hi!
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
I think that a vocabulary change could help avoid confusion, depending on exactly what the proposed vocabulary is here. "pf" is the offical name for the OpenBSD packet filter. IIRC it is also ported to FreeBSD and perhaps elsewhere. Using the name "pf" for this plugin could be confusing.
Even though I do agree with you, Karl, that the vocabulary can be confusing, I'm not sure it is up to us to change that. OpenVPN has implemented a packet filtering feature which placed in pf.[ch] and seems to use the abbreviation PF several places in the code.
We may dislike it or not, but we probably can't change that now. That this plug-in uses pf in OpenVPN context is in my eyes correct, as it corresponds to the OpenVPN implementation. Having that said, it could be better described in some comments that this plug-in is using OpenVPN's packet filter implementation.
Anyhow, this topic do deserve a little discussion on the #openvpn-discussion meeting on [email protected]...:00 UTC with James.
kind regards,
David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAkuLisAACgkQDC186MBRfrouHwCfaLLg9QCtlEEN8CYOJdtQVm+8 qeUAoJUvSKh1XM61khqHFZ55V9kvFMFg
=jtqZ
-----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 35 lines · Arne Schwabe
Quoted 20 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 6 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up fw rules
in the orderQuoted 8 lines
Commonname.pf
IP_Port.pf
IP.pf
default.pfIf one of this files is found the file is used as PF configuration. Maybe
this plugin is useful for someone else.Hi!
Quoted 8 lines
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
How does this packet filtering further work? I do not completely understand how you imagine this should work. I see that it tries to open a number of files with different filename criteria , and if it finds a file it copies it somewhere.
The packet filtering itself is already part of openvpn. It only works in tap mode iirc. See
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/pf.c. A description of the packet filter format is given in
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/management/management-notes.txt
Quoted 4 lines
COMMAND -- client-pf (OpenVPN 2.1 or higher)
This plugin/patch only add the possibility to use the packet filter functionality without use of the management interface.
Thank you for your pointers. I've been reading the docs and looking at the code. I'm a bit more informed now. But there are still some magic here which I don't understand. How does copying a file enable the packet filtering itself? Granted the man page and management-notes.txt might be a bit too vague here.
man page:
- ------------------------------------------------------------------------ --management-client-pf
Management interface clients must specify a packet filter file for each connecting client. See management-notes.txt in OpenVPN distribution for detailed notes.
- ------------------------------------------------------------------------
management-notes.txt:
- ------------------------------------------------------------------------ COMMAND -- client-pf (OpenVPN 2.1 or higher)
- ---------------------------------------------
Push a packet filter file to a specific client.
The OpenVPN server should have been started with the
- --management-client-pf directive so that it will require that VPN tunnel packets sent or received by client instances must conform to that client's packet filter configuration.
- ------------------------------------------------------------------------
In the docs, "packet filter file" is mentioned, but the docs does a poor job describing all the parts of the feature - which in fact might be /my/ main problem. It is not described the purpose of this file, except what kind of contents you might find in it and how to understand that. Further, I'm not sure if this should be run on the server or client side, or if it can be used on both sides. Is this something the server can push to the clients? It's many loose threads here, which confuses me a little bit.
Arne, you patch seems to play inside the defined playground you have available, so I'm not criticising your plug-in here now. But I need to be able to understand the magic happening here to give your plug-in a fair review.
Having that said, the whole packet filtering implementation in OpenVPN, having very good intentions indeed, seems to be rather "hackerish". Just to save the rules in a temporary file (which it looks like it does, according to pf.c:497) seems odd and so un-logic. But that's not your responsibility, Arne :)
But if you can please try to enlighten me further, I would appreciate that. After all, you have a plug-in which solves an issue for you - and I don't want to block your plug-in for inclusion as long as it is considered useful.
Kind regards,
David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAkuLlBgACgkQDC186MBRfrpaogCeK014qZ+vpgLJalvtTVvxJvda JJgAn3Q8oFwRbivFT/+sk9095NXOm02D
=o861
-----END PGP SIGNATURE-----
Quoted 72 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 35 lines · Arne Schwabe
Quoted 20 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 6 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up fw rules
in the orderQuoted 8 lines
Commonname.pf
IP_Port.pf
IP.pf
default.pfIf one of this files is found the file is used as PF configuration. Maybe
this plugin is useful for someone else.Hi!
Quoted 8 lines
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
How does this packet filtering further work? I do not completely understand how you imagine this should work. I see that it tries to open a number of files with different filename criteria , and if it finds a file it copies it somewhere.
The packet filtering itself is already part of openvpn. It only works in tap mode iirc. See
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/pf.c. A description of the packet filter format is given in
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/management/management-notes.txtQuoted 4 lines
COMMAND -- client-pf (OpenVPN 2.1 or higher)
This plugin/patch only add the possibility to use the packet filter functionality without use of the management interface.
Thank you for your pointers. I've been reading the docs and looking at the code. I'm a bit more informed now. But there are still some magic here which I don't understand. How does copying a file enable the packet filtering itself? Granted the man page and management-notes.txt might be a bit too vague here.
man page:
- ------------------------------------------------------------------------ --management-client-pf
Management interface clients must specify a packet filter file for each connecting client. See management-notes.txt in OpenVPN distribution for detailed notes.
- ------------------------------------------------------------------------management-notes.txt:
- ------------------------------------------------------------------------ COMMAND -- client-pf (OpenVPN 2.1 or higher)
- ---------------------------------------------Push a packet filter file to a specific client.
The OpenVPN server should have been started with the
- --management-client-pf directive so that it will require that VPN tunnel packets sent or received by client instances must conform to that client's packet filter configuration.
- ------------------------------------------------------------------------
Quoted 8 lines
In the docs, "packet filter file" is mentioned, but the docs does a poor job describing all the parts of the feature - which in fact might be /my/ main problem. It is not described the purpose of this file, except what kind of contents you might find in it and how to understand that. Further, I'm not sure if this should be run on the server or client side, or if it can be used on both sides. Is this something the server can push to the clients? It's many loose threads here, which confuses me a little bit.
As far as I recall correctly the packet filtering code runs *only* on the server if the server is in a) multi client mode and b) tap mode. You basically can restrict the addresses the clients can reach on a client basis. I needed some basic clients are allowd to access internal IP a but not b mechanism and the pf code of openvpn was good enough for me. But for the simple I did not want to keep another daemon around which waits for connecting client and then sends the pf rules so I wrote the plugin. That way I could have a default.pf
Quoted 5 lines
Arne, you patch seems to play inside the defined playground you have available, so I'm not criticising your plug-in here now. But I need to be able to understand the magic happening here to give your plug-in a fair review.
Quite understandable.
Quoted 10 lines
Having that said, the whole packet filtering implementation in OpenVPN, having very good intentions indeed, seems to be rather "hackerish". Just to save the rules in a temporary file (which it looks like it does, according to pf.c:497) seems odd and so un-logic. But that's not your responsibility, Arne :)
But if you can please try to enlighten me further, I would appreciate that. After all, you have a plug-in which solves an issue for you - and I don't want to block your plug-in for inclusion as long as it is considered useful.
Well the plugin is given a pointer to the temporary file name. If you copy a ruleset to that temporary file the openvpn pf filter code picks it up. I also think that this api is not the best around but at the Moment it is the one a plugin could use. When I wrote the patch, it was the least intrusive method to get the pf code working.
Arne
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 112 lines · Arne Schwabe
Quoted 77 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 36 lines · Arne Schwabe
Quoted 20 lines · David Sommerseth
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Quoted 6 lines · Arne Schwabe
Hi,
I have written a simple plugin for packet filtering that looks up fw rules
in the orderQuoted 8 lines
Commonname.pf
IP_Port.pf
IP.pf
default.pfIf one of this files is found the file is used as PF configuration. Maybe
this plugin is useful for someone else.Hi!
Quoted 8 lines
Thank you for your patches. I've been looking at both patches, and I have some questions in regards to this plug-in.
How does this packet filtering further work? I do not completely understand how you imagine this should work. I see that it tries to open a number of files with different filename criteria , and if it finds a file it copies it somewhere.
The packet filtering itself is already part of openvpn. It only works in tap mode iirc. See
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/pf.c. A description of the packet filter format is given in
http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn/management/management-notes.txtQuoted 4 lines
COMMAND -- client-pf (OpenVPN 2.1 or higher)
This plugin/patch only add the possibility to use the packet filter functionality without use of the management interface.
Thank you for your pointers. I've been reading the docs and looking at the code. I'm a bit more informed now. But there are still some magic here which I don't understand. How does copying a file enable the packet filtering itself? Granted the man page and management-notes.txt might be a bit too vague here.
man page:
(quoted diff) +0
-4- ------------------------------------------------------------------------ --management-client-pf Management interface clients must specify a packet filter file for each connecting client. See management-notes.txt in OpenVPN distribution for detailed notes. - ------------------------------------------------------------------------management-notes.txt:
-
------------------------------------------------------------------------
COMMAND -- client-pf (OpenVPN 2.1 or higher)
- ---------------------------------------------Push a packet filter file to a specific client.
The OpenVPN server should have been started with the
- --management-client-pf directive so that it will require that VPN tunnel packets sent or received by client instances must conform to that client's packet filter configuration. -
------------------------------------------------------------------------Quoted 8 lines
In the docs, "packet filter file" is mentioned, but the docs does a poor job describing all the parts of the feature - which in fact might be /my/ main problem. It is not described the purpose of this file, except what kind of contents you might find in it and how to understand that. Further, I'm not sure if this should be run on the server or client side, or if it can be used on both sides. Is this something the server can push to the clients? It's many loose threads here, which confuses me a little bit.
As far as I recall correctly the packet filtering code runs *only* on the server if the server is in a) multi client mode and b) tap mode. You basically can restrict the addresses the clients can reach on a client basis. I needed some basic clients are allowd to access internal IP a but not b mechanism and the pf code of openvpn was good enough for me. But for the simple I did not want to keep another daemon around which waits for connecting client and then sends the pf rules so I wrote the plugin. That way I could have a default.pf
Quoted 5 lines
Arne, you patch seems to play inside the defined playground you have available, so I'm not criticising your plug-in here now. But I need to be able to understand the magic happening here to give your plug-in a fair review.
Quite understandable.
Quoted 10 lines
Having that said, the whole packet filtering implementation in OpenVPN, having very good intentions indeed, seems to be rather "hackerish". Just to save the rules in a temporary file (which it looks like it does, according to pf.c:497) seems odd and so un-logic. But that's not your responsibility, Arne :)
But if you can please try to enlighten me further, I would appreciate that. After all, you have a plug-in which solves an issue for you - and I don't want to block your plug-in for inclusion as long as it is considered useful.
Quoted 5 lines
Well the plugin is given a pointer to the temporary file name. If you copy a ruleset to that temporary file the openvpn pf filter code picks it up. I also think that this api is not the best around but at the Moment it is the one a plugin could use. When I wrote the patch, it was the least intrusive method to get the pf code working.
Thanks a lot for your time and explanations! The dots begin to connect for me, and I begin to see how it works now. I see a big potential of improving the internal packet filtering, so this is an invitation to anyone who got time for that :)
Anyway, back to your plug-in ... if you could shape up those issues I found in regards to the usage of arrays and improve the buffer overflow issues I'm willing to include your plug-in. Also, please go through and make sure the coding style is coherent and remove not needed functions as well. In general we're trying to avoid cleaning up patches after they are included.
For more information about the development model we're trying to use, have a look here:
<http://www.secure-computing.net/wiki/index.php/OpenVPN/Developer_documentation>
kind regards,
David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAkuLqG8ACgkQDC186MBRfrpTDACdHGKwmeu6fdhWmXj8Xff8Sqzt yAUAoIkD27Qaq9+OOXWyZUqERAQvWEGr
=MViS
-----END PGP SIGNATURE-----
Quoted 2 lines · David Sommerseth
Even though I do agree with you, Karl, that the vocabulary can be confusing, I'm not sure it is up to us to change that.
Just figured I'd mention it.
Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein