Description: Fix some possible problems in code
Author: Paul Wouters <paul@nohats.ca>
Origin: upstream git
Last-Update: 2012-06-29

Index: openswan-debian/include/alg_info.h
===================================================================
--- openswan-debian.orig/include/alg_info.h	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/include/alg_info.h	2012-06-29 19:16:40.509617514 +0200
@@ -140,8 +140,7 @@
 int alg_info_snprint(char *buf, int buflen
 		     , struct alg_info *alg_info, bool permitike);
 
-int alg_info_snprint_esp(char *buf, int buflen, struct alg_info_esp *alg_info);
-int alg_info_snprint_ike(char *buf, int buflen, struct alg_info_ike *alg_info);
+void alg_info_snprint_ike(char *buf, size_t buflen, struct alg_info_ike *alg_info);
 #define ALG_INFO_ESP_FOREACH(ai, ai_esp, i) \
 	for (i=(ai)->alg_info_cnt,ai_esp=(ai)->esp; i--; ai_esp++) 
 #define ALG_INFO_IKE_FOREACH(ai, ai_ike, i) \
Index: openswan-debian/include/ipsecconf/confread.h
===================================================================
--- openswan-debian.orig/include/ipsecconf/confread.h	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/include/ipsecconf/confread.h	2012-06-29 19:16:40.509617514 +0200
@@ -149,17 +149,6 @@
 					    , bool setuponly);
 extern struct starter_conn *alloc_add_conn(struct starter_config *cfg
 					   , char *name, err_t *perr);
-extern int init_load_conn(struct starter_config *cfg
-			  , struct config_parsed *cfgp
-			  , struct section_list *sconn
-			  , bool alsoprocessing
-			  , bool defaultconn
-			  , bool resolvip
-			  , err_t *perr);
-extern bool translate_conn (struct starter_conn *conn
-			    , struct section_list *sl
-			    , enum keyword_set   assigned_value
-			    , err_t *error);
 
 void confread_free(struct starter_config *cfg);
 
Index: openswan-debian/include/ipsecconf/keywords.h
===================================================================
--- openswan-debian.orig/include/ipsecconf/keywords.h	2012-06-29 19:16:39.229617823 +0200
+++ openswan-debian/include/ipsecconf/keywords.h	2012-06-29 19:16:40.509617514 +0200
@@ -266,8 +266,6 @@
 };
 
 TAILQ_HEAD(starter_comments_list, starter_comments);
-extern void move_comment_list(struct starter_comments_list *to,
-			      struct starter_comments_list *from);
 
 struct section_list {
     TAILQ_ENTRY(section_list) link;
Index: openswan-debian/lib/libipsecconf/confread.c
===================================================================
--- openswan-debian.orig/lib/libipsecconf/confread.c	2012-06-29 19:16:39.229617823 +0200
+++ openswan-debian/lib/libipsecconf/confread.c	2012-06-29 19:16:40.509617514 +0200
@@ -575,6 +575,7 @@
  *        value is considered acceptable.
  * @return bool 0 if successfull
  */
+static
 bool translate_conn (struct starter_conn *conn
 		     , struct section_list *sl
 		     , enum keyword_set   assigned_value
@@ -794,7 +795,7 @@
     return err;
 }
 
-
+static
 void move_comment_list(struct starter_comments_list *to,
 		       struct starter_comments_list *from)
 {
@@ -1196,6 +1197,7 @@
     return conn;
 }
 
+static
 int init_load_conn(struct starter_config *cfg
 		   , struct config_parsed *cfgp
 		   , struct section_list *sconn
Index: openswan-debian/lib/libipsecconf/virtif.c
===================================================================
--- openswan-debian.orig/lib/libipsecconf/virtif.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libipsecconf/virtif.c	2012-06-29 19:16:40.509617514 +0200
@@ -252,6 +252,9 @@
 	for (i=0; i<N_IPSEC_IF; i++) {
 		_iface_down (sock, &(_ipsec_if[i]));
 	}
+	if(close(sock)){
+		starter_log(LOG_LEVEL_ERR,"starter_ifaces_clear socket close() failed: %s", strerror(errno));
+	}
 }
 
 int starter_ifaces_load (char **ifaces, unsigned int omtu, int nat_t)
Index: openswan-debian/lib/libopenswan/alg_info.c
===================================================================
--- openswan-debian.orig/lib/libopenswan/alg_info.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libopenswan/alg_info.c	2012-06-29 19:16:40.509617514 +0200
@@ -715,7 +715,6 @@
 		 * If we detect sha2_256 with key size 96, we know they really meant
 		 * to configure sha2_256_trunc 256 bit with a hash truncation of 96
 		 */
-#warning paul check and redo me
 #if 0
 		if( !strncmp( ctx.aalg_buf, sha2_256, 8) && (ctx.eklen==0) && (ctx.aklen==96)) {
 		   DBG(DBG_CRYPT,DBG_log(" converting sha2_256-96 to sha2_256_trunc-256"));
Index: openswan-debian/lib/libopenswan/id.c
===================================================================
--- openswan-debian.orig/lib/libopenswan/id.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libopenswan/id.c	2012-06-29 19:16:40.509617514 +0200
@@ -225,8 +225,9 @@
     case ID_IPV4_ADDR:
     case ID_IPV6_ADDR:
 	if(isanyaddr(&id->ip_addr)) {
+	    passert(dstlen > sizeof("%any"));
 	    dst[0]='\0';
-	    strncat(dst, "%any", dstlen);
+	    strncat(dst, "%any", dstlen -1);
 	    n = strlen(dst);
 	} else {
 	    n = (int)addrtot(&id->ip_addr, 0, dst, dstlen) - 1;
Index: openswan-debian/lib/libopenswan/oswlog.c
===================================================================
--- openswan-debian.orig/lib/libopenswan/oswlog.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libopenswan/oswlog.c	2012-06-29 19:16:40.509617514 +0200
@@ -82,11 +82,11 @@
     buf[0] = '\0';
     if (reproc)
 	fmt++;	/* ~ at start of format suppresses this prefix */
-    else if (progname != NULL)
+    else if (progname != NULL && (strlen(progname)+1+1) < buf_len)
     {
 	/* start with name of connection */
-	strncat(buf, progname, buf_len);
-	strncat(buf, " ", buf_len);
+	strncat(buf, progname, buf_len - 1);
+	strncat(buf, " ", buf_len - 1);
     }
 
     ps = strlen(buf);
Index: openswan-debian/lib/libopenswan/x509dn.c
===================================================================
--- openswan-debian.orig/lib/libopenswan/x509dn.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libopenswan/x509dn.c	2012-06-29 19:16:40.509617514 +0200
@@ -36,7 +36,6 @@
 #include "constants.h"
 #include "oswlog.h"
 #include "oswalloc.h"
-#include "oswlog.h"
 #include "oswtime.h"
 #include "mpzfuncs.h"
 #include "id.h"
@@ -1300,7 +1299,7 @@
 	   PRArenaPool *arena;
 	   SECStatus retVal = SECSuccess;
 	   SECItem nss_n, nss_e, dsig;
-	   SECItem signature, data;
+	   SECItem signature;
            mpz_t e;
            mpz_t n;
 	   mpz_t s;
@@ -1379,10 +1378,6 @@
 	    signature.data = sc.ptr;
 	    signature.len  = (unsigned int)sc.len;
 
-	    data.type = siBuffer;
-	    data.data = digest->ptr;
-	    data.len  = (unsigned int)digest->len;
-
 	    dsigc.len = (unsigned int)sc.len;
 	    dsigc.ptr = alloc_bytes(dsigc.len, "NSS decrypted signature");
             dsig.type = siBuffer;
Index: openswan-debian/linux/net/ipsec/addrtot.c
===================================================================
--- openswan-debian.orig/linux/net/ipsec/addrtot.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/linux/net/ipsec/addrtot.c	2012-06-29 19:16:40.509617514 +0200
@@ -81,9 +81,8 @@
 
 	n = addrbytesptr(src, &b);
 	if (n == 0) {
-	bad:
 	  dst[0]='\0';
-	  strncat(dst, "<invalid>", dstlen);
+	  strncat(dst, "<invalid>", dstlen -1); /* we hope possible truncation does not cause problems */
 	  return sizeof("<invalid>");
 	}
 
@@ -107,7 +106,9 @@
 		n = reverse6(b, n, buf, &p);
 		break;
 	default:		/* including (AF_INET, 'R') */
-	        goto bad;
+		dst[0]='\0';
+		strncat(dst, "<invalid>", dstlen - 1); /* we hope possible truncation does not cause problems */
+		return sizeof("<invalid>");
 	}
 
 	if (dstlen > 0) {
@@ -132,15 +133,15 @@
 	size_t n;
 	char buf[1+ADDRTOT_BUF+1];	/* :address: */
 	char *p;
+
 #	define	TF(t, f)	(((t)<<8) | (f))
 
 	switch (t) {
 	case AF_INET: n = IP4BYTES; break;
 	case AF_INET6: n = IP6BYTES; break;
 	default:
-	bad:
 	  dst[0]='\0';
-	  strncat(dst, "<invalid>", dstlen);
+	  strncat(dst, "<invalid>", dstlen - 1); /* we hope possible truncation does not cause problems */
 	  return sizeof("<invalid>");
 	}
 
@@ -164,7 +165,9 @@
 		n = reverse6(src, n, buf, &p);
 		break;
 	default:		/* including (AF_INET, 'R') */
-		goto bad;
+	  	dst[0]='\0';
+	  	strncat(dst, "<invalid>", dstlen - 1); /* we hope possible truncation does not cause problems */
+	  	return sizeof("<invalid>");
 	}
 
 	if (dstlen > 0) {
@@ -190,6 +193,7 @@
 		struct sockaddr_in sin;
 		struct sockaddr_in6 sin6;
 	} *sinp = (const union SINSIN6 *) src;
+
 	switch (sinp->sin.sin_family) {
 	case AF_INET:
 		return inet_addrtot(AF_INET,&sinp->sin.sin_addr,format,dst,dstlen);
@@ -197,7 +201,7 @@
 		return inet_addrtot(AF_INET6,&sinp->sin6.sin6_addr,format,dst,dstlen);
 	default:
 		dst[0]='\0';
-		strncat(dst, "<invalid>", dstlen);
+		strncat(dst, "<invalid>", dstlen - 1); /* we hope possible truncation does not cause problems */
 		return sizeof("<invalid>");
 	}
 }
Index: openswan-debian/programs/pluto/connections.c
===================================================================
--- openswan-debian.orig/programs/pluto/connections.c	2012-06-29 19:16:32.205617479 +0200
+++ openswan-debian/programs/pluto/connections.c	2012-06-29 19:16:40.509617514 +0200
@@ -533,8 +533,7 @@
     if (isanyaddr(&this->host_addr))
     {
 	if(this->host_type == KH_IPHOSTNAME) {
-	    host=host_space;
-	    strcpy(host_space, "%dns");
+	    host = strcpy(host_space, "%dns");
 	    dohost_name=TRUE;
 	} else {
 	    switch (policy & (POLICY_GROUP | POLICY_OPPO))
@@ -596,13 +595,16 @@
 
     if(dohost_name) {
     	if(this->host_addr_name) {
-	    strncat(host_space, "<", sizeof(host_space)-1);
-	    strncat(host_space, this->host_addr_name, sizeof(host_space)-1);
-	    strncat(host_space, ">", sizeof(host_space));
+		size_t icl = strlen(host_space);
+		int room = sizeof(host_space) - icl - 1;
+		int needed = snprintf(host_space + icl, room, "<%s>", this->host_addr_name);
+
+		if (needed > room) {
+		   loglog(RC_BADID, "format_end: buffer too small for dohost_name - should not happen\n");
+		}
 	}
     }
 
-
     host_port[0] = '\0';
     if (this->host_port_specific)
 	snprintf(host_port, sizeof(host_port), ":%u"
@@ -1138,6 +1140,7 @@
     } while (reqid != start);
 
     exit_log("unable to allocate reqid");
+    return 0; /* never reached, here to make compiler happy */
 }
 
 void
Index: openswan-debian/programs/pluto/crypt_dh.c
===================================================================
--- openswan-debian.orig/programs/pluto/crypt_dh.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/crypt_dh.c	2012-06-29 19:16:40.509617514 +0200
@@ -165,7 +165,7 @@
 
     dhshared_len = PK11_GetKeyLength(dhshared); 
     if( group->bytes > dhshared_len ) {
-	DBG(DBG_CRYPT, DBG_log("Dropped %d leading zeros", group->bytes-dhshared_len));
+	DBG(DBG_CRYPT, DBG_log("Dropped %lu leading zeros", group->bytes-dhshared_len));
 	chunk_t zeros;
 	PK11SymKey *newdhshared = NULL;
 	CK_KEY_DERIVATION_STRING_DATA string_params;
@@ -1334,7 +1334,7 @@
 
 	for(;;)
 	{
-	   PK11SymKey *tkey11,*tkey3;;
+	   PK11SymKey *tkey11,*tkey3;
 
 	   if(vpss.counter[0]== 0x01) {
 		PK11SymKey *tkey2 = pk11_derive_wrapper_osw(tkey1, CKM_XOR_BASE_AND_DATA
Index: openswan-debian/programs/pluto/crypt_ke.c
===================================================================
--- openswan-debian.orig/programs/pluto/crypt_ke.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/crypt_ke.c	2012-06-29 19:16:40.509617514 +0200
@@ -57,6 +57,7 @@
 # include <nss.h>
 # include <pk11pub.h>
 # include <keyhi.h>
+# include <nspr4/prerror.h>
 # include "oswconf.h"
 #endif
 
Index: openswan-debian/programs/pluto/crypto.c
===================================================================
--- openswan-debian.orig/programs/pluto/crypto.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/crypto.c	2012-06-29 19:16:40.513617529 +0200
@@ -354,13 +354,12 @@
 do_3des(u_int8_t *buf, size_t buf_len
 	, u_int8_t *key, size_t key_size, u_int8_t *iv, bool enc)
 {
-    des_key_schedule ks[3];
-
     passert(key != NULL);
 
 #ifdef HAVE_LIBNSS
 	do_3des_nss(buf, buf_len, key, key_size, iv, enc);
 #else
+    des_key_schedule ks[3];
 
     passert(key_size==(DES_CBC_BLOCK_SIZE * 3));
 
Index: openswan-debian/programs/pluto/fetch.c
===================================================================
--- openswan-debian.orig/programs/pluto/fetch.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/fetch.c	2012-06-29 19:16:40.513617529 +0200
@@ -710,7 +710,7 @@
     unlock_ocsp_fetch_list("fetch_ocsp");
 }
 
-static void*
+static void
 fetch_thread(void *arg UNUSED)
 {
     struct timespec wait_interval;
Index: openswan-debian/programs/pluto/ike_alg_status.c
===================================================================
--- openswan-debian.orig/programs/pluto/ike_alg_status.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/ike_alg_status.c	2012-06-29 19:16:40.513617529 +0200
@@ -104,7 +104,7 @@
 		    , instance
 		    , buf);
 
-		alg_info_snprint_ike(buf, sizeof(buf)-1, c->alg_info_ike);
+		alg_info_snprint_ike(buf, sizeof(buf), c->alg_info_ike);
 		whack_log(RC_COMMENT
 		    , "\"%s\"%s:   IKE algorithms found:  %s"
 		    , c->name
Index: openswan-debian/programs/pluto/ikev2_child.c
===================================================================
--- openswan-debian.orig/programs/pluto/ikev2_child.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/ikev2_child.c	2012-06-29 19:16:40.513617529 +0200
@@ -64,6 +64,7 @@
 #include "virtual.h"
 #include "hostpair.h"
 
+#if 0
 static void print_ikev2_ts(struct traffic_selector *ts){
         char lbx[ADDRTOT_BUF];
         char hbx[ADDRTOT_BUF];
@@ -79,6 +80,7 @@
         DBG_log("ip high: %s", hbx);
 	DBG_log("PAUL marker ------------------------");
 }
+#endif
 
 void ikev2_print_ts(struct traffic_selector *ts){
 	char lbx[ADDRTOT_BUF];
@@ -495,7 +497,6 @@
 {
     unsigned int tsi_ni, tsr_ni;
     int bestfit = -1;
-    int best_tsr, best_tsi; 
     struct end *ei, *er;
     
     if(role == INITIATOR) {
@@ -595,8 +596,6 @@
 		);
 
 		if(fitbits > bestfit) {
-		    best_tsi = tsi_ni;
-		    best_tsr = tsr_ni;
 		    bestfit = fitbits;
 		}
 	    }
Index: openswan-debian/programs/pluto/ikev2_parent.c
===================================================================
--- openswan-debian.orig/programs/pluto/ikev2_parent.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/ikev2_parent.c	2012-06-29 19:16:40.513617529 +0200
@@ -2100,7 +2100,6 @@
 			struct payload_digest *const tsi_pd = md->chain[ISAKMP_NEXT_v2TSi];
 			struct payload_digest *const tsr_pd = md->chain[ISAKMP_NEXT_v2TSr];
 			struct traffic_selector tsi[16], tsr[16];
-			int tsc=0;
 #if 0
 			bool instantiate = FALSE;
 			ip_subnet tsi_subnet, tsr_subnet;
@@ -2317,7 +2316,7 @@
 	n_hdr.isa_np = ISAKMP_NEXT_v2N;
 	n_hdr.isa_flags &= ~ISAKMP_FLAGS_I;
 	n_hdr.isa_flags  |=  ISAKMP_FLAGS_R;
-#warning check msgid code here
+
 	// PAUL: shouldn't we set n_hdr.isa_msgid = [htonl](p1st->st_msgid);
 	if (!out_struct(&n_hdr, &isakmp_hdr_desc, &reply, &rbody)) 
 	{
Index: openswan-debian/programs/pluto/kernel_netlink.c
===================================================================
--- openswan-debian.orig/programs/pluto/kernel_netlink.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/kernel_netlink.c	2012-06-29 19:16:40.513617529 +0200
@@ -1117,6 +1117,8 @@
 {
 	struct sadb_alg alg;
 
+	alg.sadb_alg_reserved = 0;
+
 	alg.sadb_alg_ivlen = 8;
 	alg.sadb_alg_minbits = 128;
 	alg.sadb_alg_maxbits = 256;
Index: openswan-debian/programs/pluto/ocsp.c
===================================================================
--- openswan-debian.orig/programs/pluto/ocsp.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/ocsp.c	2012-06-29 19:16:40.513617529 +0200
@@ -1652,7 +1652,8 @@
 	plog("ocsp response contains no nonce, replay attack possible");
     }
     /* check if the nonce is identical */
-    if (res.nonce.ptr != NULL && !same_chunk(res.nonce, location->nonce))
+    if (location->nonce.ptr != NULL && res.nonce.ptr != NULL
+	&& !same_chunk(res.nonce, location->nonce))
     {
 	plog("invalid nonce in ocsp response");
 	return;
Index: openswan-debian/programs/pluto/plutoalg.c
===================================================================
--- openswan-debian.orig/programs/pluto/plutoalg.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/plutoalg.c	2012-06-29 19:16:40.513617529 +0200
@@ -186,8 +186,8 @@
  * print which ESP algorithm has actually been selected, based upon which
  * ones are actually loaded.
  */
-int
-alg_info_snprint_esp(char *buf, int buflen, struct alg_info_esp *alg_info)
+static void
+alg_info_snprint_esp(char *buf, size_t buflen, struct alg_info_esp *alg_info)
 {
 	char *ptr=buf;
 	int ret;
@@ -196,9 +196,11 @@
 	int eklen, aklen;
 	const char *sep="";
 
-	ptr=buf;
+	passert(buflen >= sizeof("none"));
 
-	buf[0]=0; strncat(buf, "none", buflen);
+	ptr=buf;
+	buf[0]=0;
+	strncat(buf, "none", buflen - 1);
 
 	ALG_INFO_ESP_FOREACH(alg_info, esp_info, cnt) {
 	    if (kernel_alg_esp_enc_ok(esp_info->esp_ealg_id, 0, NULL)) {
@@ -224,21 +226,23 @@
 			 , esp_info->esp_ealg_id, eklen
 			 , enum_name(&auth_alg_names, esp_info->esp_aalg_id) + (esp_info->esp_aalg_id ? sizeof("AUTH_ALGORITHM_HMAC") : sizeof("AUTH_ALGORITHM"))
 			 , esp_info->esp_aalg_id, aklen);
+
+	    if ( ret < 0 || (size_t)ret >= buflen) {
+		DBG_log("alg_info_snprint_esp: buffer too short for snprintf");
+		break;
+	    }
 	    ptr+=ret;
 	    buflen-=ret;
-	    if (buflen<0) break;
-
 	    sep = ", ";
 	}
-	return ptr-buf;
 }
 
 /*
  * print which AH algorithm has actually been selected, based upon which
  * ones are actually loaded.
  */
-int
-alg_info_snprint_ah(char *buf, int buflen, struct alg_info_esp *alg_info)
+static void
+alg_info_snprint_ah(char *buf, size_t buflen, struct alg_info_esp *alg_info)
 {
 	char *ptr=buf;
 	int ret;
@@ -247,9 +251,11 @@
 	int aklen;
 	const char *sep="";
 
+	passert(buflen >= sizeof("none"));
 	ptr=buf;
 
-	buf[0]=0; strncat(buf, "none", buflen);
+	buf[0]=0;
+	strncat(buf, "none", buflen - 1);
 
 	ALG_INFO_ESP_FOREACH(alg_info, esp_info, cnt) {
 
@@ -266,23 +272,27 @@
 			 , sep
 			 , enum_name(&auth_alg_names, esp_info->esp_aalg_id)+sizeof("AUTH_ALGORITHM_HMAC")
 			 , esp_info->esp_aalg_id, aklen);
+
+	    if ( ret < 0 || (size_t)ret >= buflen) {
+		DBG_log("alg_info_snprint_ah: buffer too short for snprintf");
+		break;
+	    }
 	    ptr+=ret;
 	    buflen-=ret;
-	    if (buflen<0) break;
-
 	    sep = ", ";
 	}
-	return ptr-buf;
 }
 
-int
-alg_info_snprint_phase2(char *buf, int buflen, struct alg_info_esp *alg_info)
+void
+alg_info_snprint_phase2(char *buf, size_t buflen, struct alg_info_esp *alg_info)
 {
     switch(alg_info->alg_info_protoid) {
     case PROTO_IPSEC_ESP:
-	return alg_info_snprint_esp(buf, buflen, alg_info);
+	alg_info_snprint_esp(buf, buflen, alg_info);
+	return;
     case PROTO_IPSEC_AH:
-	return alg_info_snprint_ah(buf, buflen, alg_info);
+	alg_info_snprint_ah(buf, buflen, alg_info);
+	return;
     default:
 	bad_case(alg_info->alg_info_protoid);
     }
@@ -304,8 +314,8 @@
     return buf;
 }
 
-int
-alg_info_snprint_ike(char *buf, int buflen, struct alg_info_ike *alg_info)
+void
+alg_info_snprint_ike(char *buf, size_t buflen, struct alg_info_ike *alg_info)
 {
 	char *ptr=buf;
 	int ret;
@@ -316,6 +326,7 @@
 	struct encrypt_desc *enc_desc;
 	struct hash_desc *hash_desc;
 
+
 	ALG_INFO_IKE_FOREACH(alg_info, ike_info, cnt) {
 	    if (ike_alg_enc_present(ike_info->ike_ealg) 
 		&& (ike_alg_hash_present(ike_info->ike_halg)) 
@@ -340,14 +351,14 @@
 			     , ike_info->ike_halg, aklen
 			     , enum_name(&oakley_group_names, ike_info->ike_modp)+sizeof("OAKLEY_GROUP")
 			     , ike_info->ike_modp);
+		if ( ret < 0 || (size_t)ret >= buflen) {
+		   DBG_log("alg_info_snprint_ike: buffer too short for snprintf");
+		   break;
+		}
 		ptr+=ret;
 		buflen-=ret;
-		if (buflen<0) break;
-
-		sep = ", ";
 	    }
 	}
-	return ptr-buf;
 }
 
 /*	
Index: openswan-debian/programs/pluto/plutoalg.h
===================================================================
--- openswan-debian.orig/programs/pluto/plutoalg.h	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/plutoalg.h	2012-06-29 19:16:40.513617529 +0200
@@ -13,9 +13,6 @@
 extern struct alg_info_ike *
 alg_info_ike_create_from_str (const char *alg_str, const char **err_p);
 
-extern int alg_info_snprint_ah(char *buf, int buflen
-			       , struct alg_info_esp *alg_info);
-
-extern int alg_info_snprint_phase2(char *buf, int buflen
+extern void alg_info_snprint_phase2(char *buf, size_t buflen
 				   , struct alg_info_esp *alg_info);
 
Index: openswan-debian/programs/pluto/pluto_crypt.c
===================================================================
--- openswan-debian.orig/programs/pluto/pluto_crypt.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/pluto_crypt.c	2012-06-29 19:16:40.513617529 +0200
@@ -739,7 +739,7 @@
     r = &reqbuf[0];
 
     if(r->pcr_len > sizeof(reqbuf)) {
-	loglog(RC_LOG_SERIOUS, "helper(%d) pid=%d screwed up length: %lu > %lu, killing it"
+	loglog(RC_LOG_SERIOUS, "helper(%d) pid=%lu screwed up length: %lu > %lu, killing it"
 	       , w->pcw_helpernum
 	       , w->pcw_pid, (unsigned long)r->pcr_len
                , (unsigned long)sizeof(reqbuf));
@@ -994,7 +972,7 @@
 				  , int status)
 {
     if(w->pcw_pipe) {
-	loglog(RC_LOG_SERIOUS, "closing helper(%u) pid=%d fd=%d exit=%d"
+	loglog(RC_LOG_SERIOUS, "closing helper(%u) pid=%lu fd=%d exit=%d"
 	       , w->pcw_helpernum, w->pcw_pid, w->pcw_pipe, status);
 	close(w->pcw_pipe);
     }
Index: openswan-debian/programs/pluto/xauth.c
===================================================================
--- openswan-debian.orig/programs/pluto/xauth.c	2012-06-29 19:16:32.205617479 +0200
+++ openswan-debian/programs/pluto/xauth.c	2012-06-29 19:16:40.513617529 +0200
@@ -2307,8 +2307,12 @@
 		break;
 		
 	    case XAUTH_MESSAGE:
-		if(len > 80) len=80;
-		memcpy(msgbuf, dat, len);
+		if(len > 80) {
+		    len=80;
+		}
+		if(dat) {
+		   memcpy(msgbuf, dat, len);
+		}
 		msgbuf[len]='\0';
 		loglog(RC_LOG_SERIOUS, "XAUTH: Bad Message: %s", msgbuf);
 		break;
Index: openswan-debian/lib/libipsecconf/starterwhack.c
===================================================================
--- openswan-debian.orig/lib/libipsecconf/starterwhack.c	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/lib/libipsecconf/starterwhack.c	2012-06-29 19:16:40.513617529 +0200
@@ -74,8 +74,8 @@
 }
 
 int starter_whack_read_reply(int sock,
-			     char xauthname[128],
-			     char xauthpass[128],
+			     char xauthname[XAUTH_MAX_NAME_LENGTH],
+			     char xauthpass[XAUTH_MAX_PASS_LENGTH],
 			     int xauthnamelen,
 			     int xauthpasslen)
 {
@@ -144,7 +144,11 @@
 			case RC_ENTERSECRET:
 				if(xauthpasslen==0) {
 					xauthpasslen = whack_get_secret(xauthpass
-								  , sizeof(xauthpass));
+								  , XAUTH_MAX_PASS_LENGTH);
+				}
+				if (xauthpasslen > XAUTH_MAX_PASS_LENGTH) { /* for input >= 128, xauthpasslen would be 129 */
+					xauthpasslen = XAUTH_MAX_PASS_LENGTH;
+					starter_log(LOG_LEVEL_ERR, "xauth password cannot be >= %d chars", XAUTH_MAX_PASS_LENGTH);
 				}
 				ret=send_reply(sock, xauthpass, xauthpasslen);
 				if(ret!=0) return ret;
@@ -153,7 +157,11 @@
 			case RC_XAUTHPROMPT:
 				if(xauthnamelen==0) {
 					xauthnamelen = whack_get_value(xauthname
-								 , sizeof(xauthname));
+								 , XAUTH_MAX_NAME_LENGTH);
+				}
+				if (xauthnamelen > XAUTH_MAX_NAME_LENGTH) { /* for input >= 128, xauthnamelen would be 129 */
+					xauthnamelen = XAUTH_MAX_NAME_LENGTH;
+					starter_log(LOG_LEVEL_ERR, "xauth name cannot be >= %s chars", XAUTH_MAX_NAME_LENGTH);
 				}
 				ret=send_reply(sock, xauthname, xauthnamelen);
 				if(ret!=0) return ret;
@@ -232,8 +240,8 @@
 	 * read reply
 	 */
 	{
-		char xauthname[128];
-		char xauthpass[128];
+		char xauthname[XAUTH_MAX_NAME_LENGTH];
+		char xauthpass[XAUTH_MAX_PASS_LENGTH];
 			
 		ret = starter_whack_read_reply(sock, xauthname,xauthpass,0,0);
 		close(sock);
Index: openswan-debian/include/pluto_constants.h
===================================================================
--- openswan-debian.orig/include/pluto_constants.h	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/include/pluto_constants.h	2012-06-29 19:16:40.513617529 +0200
@@ -666,5 +666,8 @@
     PPK_XAUTH=5,
 };
 
+#define XAUTH_PROMPT_TRIES 3
+#define XAUTH_MAX_NAME_LENGTH 128
+#define XAUTH_MAX_PASS_LENGTH 128
 
 
Index: openswan-debian/programs/pluto/xauth.h
===================================================================
--- openswan-debian.orig/programs/pluto/xauth.h	2012-06-29 19:15:39.869617493 +0200
+++ openswan-debian/programs/pluto/xauth.h	2012-06-29 19:16:40.513617529 +0200
@@ -55,6 +55,3 @@
 extern oakley_auth_t xauth_calcbaseauth(oakley_auth_t baseauth);
 extern stf_status modecfg_send_request(struct state *st);
 
-/* How many times can remote users try to login ? */
-#define XAUTH_PROMPT_TRIES 3
-
