From dd5f948614b6662fc40dc8de3f567078cfe6295e Mon Sep 17 00:00:00 2001 From: Tom Yu Date: Mon, 13 Jul 2015 18:05:35 -0400 Subject: [PATCH] Fix princflags memory management Fix some out of memory error cases (found by Coverity) that could cause multiple frees or freeing of invalid pointers. In krb5_flagnum_to_string(), don't assume that asprintf() stores a null pointer on failure (it does in BSD but not in glibc). In krb5_flags_to_strings(), free the correct pointer in the cleanup loop in on error. ticket: 8215 --- src/lib/kadm5/str_conv.c | 18 ++++++++++-------- src/tests/t_princflags.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/lib/kadm5/str_conv.c b/src/lib/kadm5/str_conv.c index fd9a1d178a..0441a17682 100644 --- a/src/lib/kadm5/str_conv.c +++ b/src/lib/kadm5/str_conv.c @@ -196,7 +196,8 @@ krb5_flagspec_to_mask(const char *spec, krb5_flags *toset, krb5_flags *toclear) } /* - * Copy the flag name of flagnum to outstr. + * Copy the flag name of flagnum to outstr. On error, outstr points to a null + * pointer. */ krb5_error_code krb5_flagnum_to_string(int flagnum, char **outstr) @@ -204,14 +205,15 @@ krb5_flagnum_to_string(int flagnum, char **outstr) const char *s = NULL; *outstr = NULL; - if ((unsigned int)flagnum < NOUTFLAGS) { + if ((unsigned int)flagnum < NOUTFLAGS) s = outflags[flagnum]; - } - if (s == NULL) + if (s == NULL) { /* Assume that krb5_flags are 32 bits long. */ - asprintf(outstr, "0x%08lx", 1UL<