From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9049 Path: news.gmane.org!not-for-mail From: Alexander Monakov Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] fix use of pointer after free in unsetenv Date: Mon, 4 Jan 2016 18:47:36 +0300 (MSK) Message-ID: References: <5689AA38.60108@openwall.com> <20160104030558.GT238@brightrain.aerifal.cx> <568A4ED2.9020609@openwall.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Trace: ger.gmane.org 1451922473 18160 80.91.229.3 (4 Jan 2016 15:47:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 4 Jan 2016 15:47:53 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9062-gllmg-musl=m.gmane.org@lists.openwall.com Mon Jan 04 16:47:52 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aG7MM-0004X4-8d for gllmg-musl@m.gmane.org; Mon, 04 Jan 2016 16:47:50 +0100 Original-Received: (qmail 5253 invoked by uid 550); 4 Jan 2016 15:47:48 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 5232 invoked from network); 4 Jan 2016 15:47:48 -0000 In-Reply-To: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) Xref: news.gmane.org gmane.linux.lib.musl.general:9049 Archived-At: On Mon, 4 Jan 2016, Alexander Monakov wrote: > To me the implementation looks weird due to how it restarts scanning __environ > with 'goto again' from position 0 instead of current position. I can propose > the following rewrite (untested): > > for (i=0; __environ[i]; i++) { > char *e = __environ[i]; > if (!memcmp(name, e, l) && e[l] == '=') { > for (j=i--; __environ[j]; j++) > __environ[j] = __environ[j+1]; > if (__env_map) { > for (j=0; __env_map[j] && __env_map[j] != e; j++); > if (__env_map[j]) { > free(__env_map[j]); > do __env_map[j] = __env_map[j+1]; > while (__env_map[++j]); > } > } > } > } Hm, there's no need to preserve relative order of env entries, is there? for (i=0; __environ[i]; i++); for (int im=i-1; i-->0; ) if (!memcmp(name, __environ[i], l) && __environ[i][l] == '=') { if (__env_map) { for (j=0; __env_map[j]; j++); for (int jm=j-1; j-->0; ) if (__env_map[j] == __environ[i]) { __env_map[j] = __env_map[jm]; __env_map[jm] = 0; free(__environ[i]); break; } } if (i != im) __environ[i] = __environ[im]; __environ[im--] = 0; } (in practice I'd rather spell i-->0 as --i>=0 above) Alexander