Bug 31673

Summary: lack of bounds check on string functions after getenv() call.
Product: Base System Reporter: Andrew R. Reiter <arr>
Component: binAssignee: arr
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   

Description Andrew R. Reiter 2001-10-31 22:30:00 UTC
	Essentially, after a call to getenv() in which the code wishes to 
	receive the data for the TMPDIR key, it either will do a sprintf
	or strcpy depending on whether or not a NULL was returned from 
	getenv().  The sprintf() could be overflowed.. the strcpy more than 
	likely not.  

	More specifically the problem is:

        char path[MAXPATHLEN];

        if (!first && !envtmp) {
                envtmp = getenv("TMPDIR");
                first = 1;
        }

        if (envtmp)
                (void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
        else
                strcpy(path, _PATH_ARTMP);

Fix: 

if (envtmp)
-               (void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
+               (void)snprintf(path, sizeof(path), "%s/%s", envtmp,
+                   _NAME_ARTMP);
        else
-               strcpy(path, _PATH_ARTMP);
+               strlcpy(path, _PATH_ARTMP, sizeof(path));

        sigfillset(&set);
        (void)sigprocmask(SIG_BLOCK, &set, &oset);--Wbzru9s1sOdvfkGdEa7ox8u3e0ppj8ufDMhuhEafU2q8mLbi
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

Index: misc.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/ar/misc.c,v
retrieving revision 1.7
diff -u -r1.7 misc.c
--- misc.c      24 Jul 2001 14:04:20 -0000      1.7
+++ misc.c      31 Oct 2001 14:11:17 -0000
@@ -73,9 +73,10 @@
        }
Comment 1 ru freebsd_committer freebsd_triage 2001-11-20 11:24:08 UTC
Responsible Changed
From-To: freebsd-bugs->arr

Originator was punished to become a committer.
Comment 2 arr freebsd_committer freebsd_triage 2001-11-20 16:48:09 UTC
State Changed
From-To: open->closed

Patch committed.  Thanks (to self).