Closed Bug 1192020 Opened 9 years ago Closed 9 years ago

MSan: use-of-uninitialized-value in nssutil_DupnCat()

Categories

(NSS :: Libraries, defect, P1)

defect

Tracking

(firefox40 wontfix, firefox41 wontfix, firefox42 affected, firefox43 affected, firefox-esr38 wontfix, b2g-v2.0 wontfix, b2g-v2.0M wontfix, b2g-v2.1 wontfix, b2g-v2.1S wontfix, b2g-v2.2 affected, b2g-v2.2r affected, b2g-master affected)

RESOLVED FIXED
Tracking Status
firefox40 --- wontfix
firefox41 --- wontfix
firefox42 --- affected
firefox43 --- affected
firefox-esr38 --- wontfix
b2g-v2.0 --- wontfix
b2g-v2.0M --- wontfix
b2g-v2.1 --- wontfix
b2g-v2.1S --- wontfix
b2g-v2.2 --- affected
b2g-v2.2r --- affected
b2g-master --- affected

People

(Reporter: tsmith, Assigned: wtc)

References

(Blocks 1 open bug)

Details

(Keywords: csectype-uninitialized, sec-audit)

Attachments

(2 files, 2 obsolete files)

Attached file call_stack
This seems to be triggered by a call to NSS_NoDB_Init(NULL).

I'm not sure about the security implications here so I'm making this as a sec bug. Feel free to change it if need be.
Mass cc to get some NSS eyes on these bugs.
--- a/lib/util/utilmod.c
+++ b/lib/util/utilmod.c
@@ -87,17 +87,19 @@ nssutil_DupnCat(char *baseString, const 
 
     len += str_len;
     newString = (char *) PORT_Realloc(baseString,len);
     if (newString == NULL) {
 	PORT_Free(baseString);
 	return NULL;
     }
     if (baseString == NULL) *newString = 0;
-    return PORT_Strncat(newString,str, str_len);
+    newString[len]=0; /* Strncat will not copy the NULL in str. Make sure the
+		       * resulting string terminates */
+    return PORT_Strncat(newString, str, str_len);
 }
 
 /* Same as nssutil_DupnCat except it concatenates the full string, not a
  * partial one */
 static char *
 nssutil_DupCat(char *baseString, const char *str)
 {


I don't think this has a security exploit unless the user has access to pkcs11.txt, which is the source of the module strings, but it should get fixed.
Comment on attachment 8647748 [details] [diff] [review]
Make sure the string is null terminated on return from nssutil_DupnCat().

Review of attachment 8647748 [details] [diff] [review]:
-----------------------------------------------------------------

::: lib/util/utilmod.c
@@ +91,5 @@
>  	PORT_Free(baseString);
>  	return NULL;
>      }
>      if (baseString == NULL) *newString = 0;
> +    newString[len]=0; /* Strncat will not copy the NULL in str. Make sure the

1. Nit: add spaces around the '=' sign.

2. The comment on line 78 is wrong:

   * The first parameter is the source string. If it's null, we

"source" should be changed to "destination".

3. Nit: This function can use memcpy instead of strncat so that it
doesn't scan baseString twice. This should work:

static char *
nssutil_DupnCat(char *baseString, const char *str, int str_len)
{
    int baseStringLen = baseString ? PORT_Strlen(baseString) : 0;
    int len = baseStringLen + 1;
    char *newString;

    len += str_len;
    newString = (char *) PORT_Realloc(baseString,len);
    if (newString == NULL) {
        PORT_Free(baseString);
        return NULL;
    }
    PORT_Memcpy(&newString[baseStringLen], str, str_len);
    newString[len] = 0;
    return newString;
}
I would r+ wtc's patch. It is actually clearer to read that the existing function, and removes the hackish test to set the first byte of newString to zero if baseStringLen is zero.
Bob: thank you for the support! Here is the patch.
Attachment #8647748 - Attachment is obsolete: true
Attachment #8647748 - Flags: review?(dkeeler)
Attachment #8647800 - Flags: review?(rrelyea)
Comment on attachment 8647800 [details] [diff] [review]
Make sure the string is null terminated on return from nssutil_DupnCat(). v2

Review of attachment 8647800 [details] [diff] [review]:
-----------------------------------------------------------------

r+ rrelyea
Attachment #8647800 - Flags: review?(rrelyea) → review+
actually the patch should be:

newString[len-1] = 0;

newString[len] is beyond the array.

Alternately we can skip adding 1 to the len and add it only in the malloc.

bob
Bob: I made the fix you described and checked in the patch. Thanks!

https://hg.mozilla.org/projects/nss/rev/49af81a7869b
Assignee: nobody → wtc
Attachment #8647800 - Attachment is obsolete: true
Status: NEW → ASSIGNED
Attachment #8652635 - Flags: checked-in+
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
OS: Unspecified → All
Priority: -- → P1
Hardware: Unspecified → All
Resolution: --- → FIXED
Target Milestone: --- → 3.21
Group: core-security → core-security-release
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: