Skip to contentSkip to conversation

nfs-utils: fix memory usage problems in gssd

PATCH In review Jeff Layton 1 participant 6 messages Mar 7, 2009 – Mar 10, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover nfs-utils: fix memory usage problems in gssd

This patchset fixes a compiler warning and some problems noticed while running gssd under valgrind. With this, valgrind now reports no warnings or significant memory leaks when run under a simple test that repeatedly mounts and unmounts an export with sec=krb5.

Jeff Layton (3):
nfs-utils: initialize fakeseed in prepare_krb5_rfc1964_buffer
nfs-utils: NULL-terminate buffer after read in read_service_info
nfs-utils: free buffer allocated by gssd_k5_err_msg

 utils/gssd/context_heimdal.c |   25 ++++++----
 utils/gssd/context_lucid.c   |    6 ++-
 utils/gssd/gssd_proc.c       |    1 +
 utils/gssd/krb5_util.c       |  105 +++++++++++++++++++++++++-----------------
 utils/gssd/krb5_util.h       |    3 +-
 5 files changed, 83 insertions(+), 57 deletions(-)
#1 lore raw

PATCH 1/3 nfs-utils: initialize fakeseed in prepare_krb5_rfc1964_buffer

This causes a compiler warning and also means that we're stuffing the buffer with uninitialized junk from the stack. Other places in this code initialize "fakeseed" to 0. Do the same here.

---
 utils/gssd/context_lucid.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
utils/gssd/context_lucid.c +4 -2
index af7de58..4a682ae 100644
@@ -70,9 +70,10 @@ static int
 prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
 	gss_buffer_desc *buf, int32_t *endtime)
 {
+#define FAKESEED_SIZE 16
 	char *p, *end;
 	static int constant_zero = 0;
-	unsigned char fakeseed[16];
+	unsigned char fakeseed[FAKESEED_SIZE];
 	uint32_t word_send_seq;
 	gss_krb5_lucid_key_t enc_key;
 	int i;
@@ -88,6 +89,7 @@ prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
 	 */
 	memset(&enc_key, 0, sizeof(enc_key));
 	memset(&fakeoid, 0, sizeof(fakeoid));
+	memset(fakeseed, 0, FAKESEED_SIZE);
 
 	if (!(buf->value = calloc(1, MAX_CTX_LEN)))
 		goto out_err;
@@ -98,7 +100,7 @@ prepare_krb5_rfc1964_buffer(gss_krb5_lucid_context_v1_t *lctx,
 
 	/* seed_init and seed not used by kernel anyway */
 	if (WRITE_BYTES(&p, end, constant_zero)) goto out_err;
-	if (write_bytes(&p, end, &fakeseed, 16)) goto out_err;
+	if (write_bytes(&p, end, &fakeseed, FAKESEED_SIZE)) goto out_err;
 
 	if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.sign_alg)) goto out_err;
 	if (WRITE_BYTES(&p, end, lctx->rfc1964_kd.seal_alg)) goto out_err;
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/3 nfs-utils: NULL-terminate buffer after read in read_service_info

In reply to Jeff Layton

Valgrind complains that we're passing an unintialized buffer to sscanf here. The main problem seems to be that we're not ensuring that the buffer is NULL terminated before we pass it off.

---
 utils/gssd/gssd_proc.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
utils/gssd/gssd_proc.c +1 -0
index d0d3f7f..0ebdfcd 100644
@@ -133,6 +133,7 @@ read_service_info(char *info_file_name, char **servicename, char **servername,
 	if ((nbytes = read(fd, buf, INFOBUFLEN)) == -1)
 		goto fail;
 	close(fd);
+	buf[nbytes] = '\0';
 
 	numfields = sscanf(buf,"RPC server: %127s\n"
 		   "service: %127s %15s version %15s\n"
Signature · 1 line
1.6.0.6
#3 lore raw

Re: [PATCH 2/3] nfs-utils: NULL-terminate buffer after read in read_service_info

Quoted 14 lines · On Sat, 7 Mar 2009 07:45:27 -0500 Jeff Layton <[email protected]> wrote:

Valgrind complains that we're passing an unintialized buffer to sscanf here. The main problem seems to be that we're not ensuring that the buffer is NULL terminated before we pass it off.

---
 utils/gssd/gssd_proc.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
utils/gssd/gssd_proc.c +0 -0
index d0d3f7f..0ebdfcd 100644

utils/gssd/gssd_proc.c read_service_info(char *info_file_name, char **servicename, char **servername, · line 139

Quoted 7 lines
utils/gssd/gssd_proc.c +1 -0
@@ -133,6 +133,7 @@ read_service_info(char *info_file_name, char **servicename, char **servername,
 	if ((nbytes = read(fd, buf, INFOBUFLEN)) == -1)
 		goto fail;
 	close(fd);
+	buf[nbytes] = '\0';
 
 	numfields = sscanf(buf,"RPC server: %127s\n"
 		   "service: %127s %15s version %15s\n"

This is not quite right....

We need to expand buf by 1 char in case nbytes == INFOBUFLEN. I'll respin and resend...

Cheers,

Signature · 1 line
Jeff Layton <[email protected]>
#4 lore raw

PATCH 3/3 nfs-utils: free buffer allocated by gssd_k5_err_msg

In reply to Jeff Layton

There's no way for the caller of gssd_k5_err_msg to know whether to free the string it returns. It can call krb5_get_error_message which returns a string that must be freed via krb5_free_error_string. The other ways that it can return a string require that the memory not be freed.

Deal with this by copying the string to a new buffer in all cases. Then we can properly free the string allocated by krb5_get_error_message.

---
 utils/gssd/context_heimdal.c |   25 ++++++----
 utils/gssd/krb5_util.c       |  105 +++++++++++++++++++++++++-----------------
 utils/gssd/krb5_util.h       |    3 +-
 3 files changed, 78 insertions(+), 55 deletions(-)
utils/gssd/context_heimdal.c +14 -11
index ddf064d..6f3b8fd 100644
@@ -69,19 +69,19 @@ int write_heimdal_enc_key(char **p, char *end, gss_ctx_id_t ctx)
 	krb5_context context;
 	krb5_error_code ret;
 	int i;
-	char *skd, *dkd;
+	char *skd, *dkd, *k5err = NULL;
 	int code = -1;
 
 	if ((ret = krb5_init_context(&context))) {
-		printerr(0, "ERROR: initializing krb5_context: %s\n",
-			gssd_k5_err_msg(NULL, ret));
+		k5err = gssd_k5_err_msg(NULL, ret);
+		printerr(0, "ERROR: initializing krb5_context: %s\n", k5err);
 		goto out_err;
 	}
 
 	if ((ret = krb5_auth_con_getlocalsubkey(context,
 						ctx->auth_context, &key))){
-		printerr(0, "ERROR: getting auth_context key: %s\n",
-			gssd_k5_err_msg(context, ret));
+		k5err = gssd_k5_err_msg(context, ret);
+		printerr(0, "ERROR: getting auth_context key: %s\n", k5err);
 		goto out_err_free_context;
 	}
 
@@ -97,9 +97,9 @@ int write_heimdal_enc_key(char **p, char *end, gss_ctx_id_t ctx)
 	enc_key.keyvalue.length = key->keyvalue.length;
 	if ((enc_key.keyvalue.data =
 				calloc(1, enc_key.keyvalue.length)) == NULL) {
-
+		k5err = gssd_k5_err_msg(context, ENOMEM);
 		printerr(0, "ERROR: allocating memory for enc key: %s\n",
-			gssd_k5_err_msg(context, ENOMEM));
+			 k5err);
 		goto out_err_free_key;
 	}
 	skd = (char *) key->keyvalue.data;
@@ -119,6 +119,7 @@ int write_heimdal_enc_key(char **p, char *end, gss_ctx_id_t ctx)
     out_err_free_context:
 	krb5_free_context(context);
     out_err:
+	free(k5err);
 	printerr(2, "write_heimdal_enc_key: %s\n", code ? "FAILED" : "SUCCESS");
 	return(code);
 }
@@ -128,18 +129,19 @@ int write_heimdal_seq_key(char **p, char *end, gss_ctx_id_t ctx)
 	krb5_keyblock *key;
 	krb5_context context;
 	krb5_error_code ret;
+	char *k5err = NULL;
 	int code = -1;
 
 	if ((ret = krb5_init_context(&context))) {
-		printerr(0, "ERROR: initializing krb5_context: %s\n",
-			gssd_k5_err_msg(NULL, ret));
+		k5err = gssd_k5_err_msg(NULL, ret);
+		printerr(0, "ERROR: initializing krb5_context: %s\n", k5err);
 		goto out_err;
 	}
 
 	if ((ret = krb5_auth_con_getlocalsubkey(context,
 						ctx->auth_context, &key))){
-		printerr(0, "ERROR: getting auth_context key: %s\n",
-			gssd_k5_err_msg(context, ret));
+		k5err = gssd_k5_err_msg(context, ret);
+		printerr(0, "ERROR: getting auth_context key: %s\n", k5err);
 		goto out_err_free_context;
 	}
 
@@ -162,6 +164,7 @@ int write_heimdal_seq_key(char **p, char *end, gss_ctx_id_t ctx)
     out_err_free_context:
 	krb5_free_context(context);
     out_err:
+	free(k5err);
 	printerr(2, "write_heimdal_seq_key: %s\n", code ? "FAILED" : "SUCCESS");
 	return(code);
 }
utils/gssd/krb5_util.c +63 -42

290 lines not on this page · raw message

utils/gssd/krb5_util.h +1 -2
index 4b2da6b..62f37d6 100644
@@ -25,8 +25,7 @@ void gssd_setup_krb5_machine_gss_ccache(char *servername);
 void gssd_destroy_krb5_machine_creds(void);
 int  gssd_refresh_krb5_machine_credential(char *hostname,
 					  struct gssd_k5_kt_princ *ple);
-const char *
-gssd_k5_err_msg(krb5_context context, krb5_error_code code);
+char *gssd_k5_err_msg(krb5_context context, krb5_error_code code);
 void gssd_k5_get_default_realm(char **def_realm);
 
 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 1/1 nfs-utils: NULL-terminate buffer after read in read_service_info (try #2)

In reply to Jeff Layton

Valgrind complains that we're passing an unintialized buffer to sscanf here. The main problem seems to be that we're not ensuring that the buffer is NULL terminated before we pass it off.

This is the second version of this patch, the first one did not increase the buffer allocation by 1 which could have led to clobbering the next byte on the stack if nbytes == INFOBUFLEN.

---
 utils/gssd/gssd_proc.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
utils/gssd/gssd_proc.c +2 -1
index 295c37d..fb97a13 100644
@@ -107,7 +107,7 @@ static int
 read_service_info(char *info_file_name, char **servicename, char **servername,
 		  int *prog, int *vers, char **protocol, int *port) {
 #define INFOBUFLEN 256
-	char		buf[INFOBUFLEN];
+	char		buf[INFOBUFLEN + 1];
 	static char	dummy[128];
 	int		nbytes;
 	static char	service[128];
@@ -132,6 +132,7 @@ read_service_info(char *info_file_name, char **servicename, char **servername,
 	if ((nbytes = read(fd, buf, INFOBUFLEN)) == -1)
 		goto fail;
 	close(fd);
+	buf[nbytes] = '\0';
 
 	numfields = sscanf(buf,"RPC server: %127s\n"
 		   "service: %127s %15s version %15s\n"
Signature · 1 line
1.6.0.6