Skip to contentSkip to conversation

auth-pam:Fix buffer overflow by user supplied data

PATCH In review Jens 3 participants 9 messages Apr 19, 2016 – Apr 21, 2016

lore
Share & tools
lore

Jens

author
#0 lore raw

PATCH 1/1 auth-pam:Fix buffer overflow by user supplied data

Hi,

please review the following patch.

The AUTH-PAM plugin contains the function searchandreplace. The buffer allocated there can be overflown if the parameter replace_with is to long (depending on the format string).

E.g.:

searchandreplace(to search :=“X”, searchfor := “X”, replacewith := “0123456789!”)

would overflow temp with the character ‘!’.

The call tree contains multiple calls with a user supplied value for replace_with.

Jens

#1 lore raw

Hi,

Quoted 7 lines · Jens

The AUTH-PAM plugin contains the function searchandreplace. The buffer allocated there can be overflown if the parameter replace_with is to long (depending on the format string).

E.g.:

searchandreplace(to search :=“X”, searchfor := “X”, replacewith := “0123456789!”)

would overflow temp with the character ‘!’.

Good find!

Quoted 4 lines
src/plugins/auth-pam/auth-pam.c +0 -0
@@ -123,13 +123,15 @@ static void pam_server (int fd, const char *service, int verb, const struct name
 static char *

src/plugins/auth-pam/auth-pam.c static void pam_server (int fd, const char *service, int verb, const struct name · line 132

Quoted 10 lines
src/plugins/auth-pam/auth-pam.c +5 -1
 searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
 {
+  if (!tosearch || !searchfor || !replacewith) return 0;
+  if (!strlen(tosearch) || !strlen(searchfor) || !strlen(replacewith)) return 0;
+
   const char *searching=tosearch;
   char *scratch;
-  char temp[strlen(tosearch)*10];
+
+  char temp[strlen(tosearch) * strlen(replacewith) + 1];

This is without a doubt an improvement, but it still leaves an opportunity open to achieve a buffer overflow through an integer overflow. Consider a tosearch with len 11, and a replacewith with len SIZE_MAX/10. While we're fixing this code, let's fix that too. For example:

    size_t tosearchlen = strlen(tosearch);
    size_t replacewithlen = strlen(replacewith);
    size_t templen = tosearchlen * replacewithlen;
    if ((tosearchlen != 0 && templen / tosearchlen != replacewithlen)
|| templen == SIZE_MAX) {
        return NULL;
    }
    char temp[templen+1];

-Steffan

author
#2 lore raw

Hi Steffan,

[…]

Quoted 4 lines

This is without a doubt an improvement, but it still leaves an opportunity open to achieve a buffer overflow through an integer overflow. Consider a tosearch with len 11, and a replacewith with len

[…]

Good point. Patch attached.

Cheers
Jens

#3 lore raw
Quoted 53 lines · Jens

Passing very long usernames/passwords for pam authentication could possibly lead to a stack based buffer overrun in the auth-pam plugin.

Adds a dependency to C99 (includes stdbool.h)

---
 src/plugins/auth-pam/auth-pam.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
src/plugins/auth-pam/auth-pam.c +23 -0
index 95692ab..159fd07 100644
@@ -39,6 +39,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -119,17 +120,37 @@ static void pam_server (int fd, const char *service, int verb, const struct name
  *  a pointer to the NEW string.  Does not modify the input strings.  Will not enter an
  *  infinite loop with clever 'searchfor' and 'replacewith' strings.
  *  Daniel Johnson - [email protected]... / [email protected]...
+ *
+ *  Retuns NULL when
+ *   - any parameter is NULL
+ *   - the worst-case result is to large ( >= SIZE_MAX)
  */
 static char *
 searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
 {
+  if (!tosearch || !searchfor || !replacewith) return NULL;
+
+  size_t tosearchlen = strlen(tosearch);
+  size_t replacewithlen = strlen(replacewith);
+  size_t templen = tosearchlen * replacewithlen;
+
+  if (tosearchlen == 0 || strlen(searchfor) == 0 || replacewithlen == 0) {
+    return NULL;
+  }
+
+  bool is_potential_integer_overflow =  (templen == SIZE_MAX) || (templen / tosearchlen != replacewithlen);
+
+  if (is_potential_integer_overflow) {
+       return NULL;
+  }
+
+  // state: all parameters are valid
+
   const char *searching=tosearch;
   char *scratch;

src/plugins/auth-pam/auth-pam.c static void pam_server (int fd, const char *service, int verb, const struct name · line 156

Quoted 10 lines
src/plugins/auth-pam/auth-pam.c +2 -4
-  char temp[strlen(tosearch)*10];
-  temp[0]=0;
 
-  if (!tosearch || !searchfor || !replacewith) return 0;
-  if (!strlen(tosearch) || !strlen(searchfor) || !strlen(replacewith)) return 0;
+  char temp[templen+1];
+  temp[0]=0;
 
   scratch = strstr(searching,searchfor);
   if (!scratch) return strdup(tosearch);

Thanks for following up.

ACK

-Steffan

#4 lore raw
In reply to Jens

Hi,

Quoted 7 lines · Jens
Quoted 3 lines

This is without a doubt an improvement, but it still leaves an opportunity open to achieve a buffer overflow through an integer overflow. Consider a tosearch with len 11, and a replacewith with len

[???]

Good point. Patch attached.

While this looks totally reasonable, it does not compile... both in master and release/2.3, both on linux and FreeBSD, I get

libtool: compile: cc -DHAVE_CONFIG_H -I. -I../../../../openvpn/src/plugins/auth-pam -I../../.. -I../../../include -I../../../../openvpn/include -g -O2 -MT auth-pam.lo -MD -MP -MF .deps/auth-pam.Tpo -c ../../../../openvpn/src/plugins/auth-pam/auth-pam.c -fPIC -DPIC -o .libs/auth-pam.o ../../../../openvpn/src/plugins/auth-pam/auth-pam.c:141:53: error: use of
undeclared identifier 'SIZE_MAX'
bool is_potential_integer_overflow = (templen == SIZE_MAX) || (temple...

... you need to #include <stdint.h> to get that symbol...

Steffan: for the sake of "avoid yet another full loop and a v3", could you just ACK this change as well?

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
Quoted 4 lines · Gert Doering

... you need to #include <stdint.h> to get that symbol...

Steffan: for the sake of "avoid yet another full loop and a v3", could you just ACK this change as well?

Yes, ACK.

-Steffan

#6 lore raw
In reply to Gert Doering

Hi Gert,

[…]

*sigh*. Thanks for finding out && providing the patch!

Learning: Try on multiple machines (It “compiles on my machine [Mac]” apparently is not enough)

My test vm (precise) fails with the same error.

Jens

Quoted 20 lines

While this looks totally reasonable, it does not compile... both in master and release/2.3, both on linux and FreeBSD, I get

libtool: compile: cc -DHAVE_CONFIG_H -I. -I../../../../openvpn/src/plugins/auth-pam -I../../.. -I../../../include -I../../../../openvpn/include -g -O2 -MT auth-pam.lo -MD -MP -MF .deps/auth-pam.Tpo -c ../../../../openvpn/src/plugins/auth-pam/auth-pam.c -fPIC -DPIC -o .libs/auth-pam.o ../../../../openvpn/src/plugins/auth-pam/auth-pam.c:141:53: error: use of
undeclared identifier 'SIZE_MAX'
bool is_potential_integer_overflow = (templen == SIZE_MAX) || (temple...

... you need to #include <stdint.h> to get that symbol...

Steffan: for the sake of "avoid yet another full loop and a v3", could you just ACK this change as well?

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

Hi,

Quoted 5 lines · Jens Neuhalfen

*sigh*. Thanks for finding out && providing the patch!

Learning: Try on multiple machines (It ???compiles on my machine [Mac]??? apparently is not enough)

My test vm (precise) fails with the same error.

Mac is special, Solaris is special, Windows is :-) - all the others are usually "close enough" as far as C standards and header files go.

If you delve into the ugly details of networking, it's "Linux" vs. "Windows" vs. "BSD and MacOS"...

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]...
#8 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix buffer overflow by user supplied data

In reply to Jens

Your patch has been applied to the master and release/2.3 branch.

As discussed downthread, I've added <stdint.h> to fix the MAX_SIZE compilation error on Linux and FreeBSD.

commit 7c0ecd1191e66fa242708f93baa4006ba0a73c7a (master) commit b15d511aa6ca75c643a46b703b5536016a77d395 (release/2.3)

Author: Jens Neuhalfen
Date:   Tue Apr 19 20:42:55 2016 +0200
     Fix buffer overflow by user supplied data

     Signed-off-by: Jens Neuhalfen <[email protected]...>
     Acked-by: Steffan Karger <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/11477
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering