Bug 172273

Summary: unsetenv(3) issue according to autoconf
Product: Base System Reporter: Ed Maste <emaste>
Component: standardsAssignee: Andrey A. Chernov <ache>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 9.1-PRERELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
getenv.patch
none
getenv.patch none

Description Ed Maste freebsd_committer freebsd_triage 2012-10-02 15:50:10 UTC

How-To-Repeat: 
The autoconf test is reproduced below - the 'return 3' is the failing line


#include <stdlib.h>
#include <errno.h>
extern char **environ;

int main(int argc, char *argv[])
{
       char entry1[] = "a=1";
       char entry2[] = "b=2";
       char *env[] = { entry1, entry2, NULL };
       if (putenv ((char *) "a=1")) return 1;
       if (putenv (entry2)) return 2;
       entry2[0] = 'a';
       unsetenv ("a");
       if (getenv ("a")) return 3;
       if (!unsetenv ("") || errno != EINVAL) return 4;
       entry2[0] = 'b';
       environ = env;
       if (!getenv ("a")) return 5;
       entry2[0] = 'a';
       unsetenv ("a");
       if (getenv ("a")) return 6;
       return 0;
}
Comment 1 ache 2012-10-02 17:08:31 UTC
Attached simple patch should fix this issue (failing on test 3).
Comment 2 Ed Maste freebsd_committer freebsd_triage 2012-10-02 17:29:24 UTC
> Attached simple patch should fix this issue (failing on test 3).

Thanks, that fixes it.

Your comment is missing a . and occurrences has two 'r's though.

-Ed
Comment 3 ache 2012-10-02 17:29:33 UTC
On 02.10.2012 20:08, Andrey Chernov wrote:
> Attached simple patch should fix this issue (failing on test 3).

Oops, see more correct version attached.
Comment 4 dfilter service freebsd_committer freebsd_triage 2012-10-02 18:44:22 UTC
Author: ache
Date: Tue Oct  2 17:44:08 2012
New Revision: 241137
URL: http://svn.freebsd.org/changeset/base/241137

Log:
  Using putenv() and later direct pointer contents modification it is possibe
  to craft environment variables with similar names like that:
  a=1
  a=2
  ...
  unsetenv("a") should remove them all to make later getenv("a") impossible.
  Fix it to do so (this is GNU autoconf test #3 failure too).
  
  PR:             172273
  MFC after:      1 week

Modified:
  head/lib/libc/stdlib/getenv.c

Modified: head/lib/libc/stdlib/getenv.c
==============================================================================
--- head/lib/libc/stdlib/getenv.c	Tue Oct  2 17:05:20 2012	(r241136)
+++ head/lib/libc/stdlib/getenv.c	Tue Oct  2 17:44:08 2012	(r241137)
@@ -675,11 +675,13 @@ unsetenv(const char *name)
 
 	/* Deactivate specified variable. */
 	envNdx = envVarsTotal - 1;
-	if (__findenv(name, nameLen, &envNdx, true) != NULL) {
+	/* Remove all occurrences. */
+	while (__findenv(name, nameLen, &envNdx, true) != NULL) {
 		envVars[envNdx].active = false;
 		if (envVars[envNdx].putenv)
 			__remove_putenv(envNdx);
 		__rebuild_environ(envActive - 1);
+		envNdx = envVarsTotal - 1;
 	}
 
 	return (0);
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 5 Mark Linimon freebsd_committer freebsd_triage 2012-10-08 04:48:51 UTC
State Changed
From-To: open->patched

assign and set as MFC reminder. 


Comment 6 Mark Linimon freebsd_committer freebsd_triage 2012-10-08 04:48:51 UTC
Responsible Changed
From-To: freebsd-standards->ache
Comment 7 Andrey A. Chernov freebsd_committer freebsd_triage 2012-10-12 16:20:32 UTC
State Changed
From-To: patched->closed

Committed into stable