From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7953 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] Implement glibc *_chk interfaces for ABI compatibility. Date: Wed, 17 Jun 2015 20:40:41 -0400 Message-ID: <20150618004040.GK1173@brightrain.aerifal.cx> References: <1434509291-28997-1-git-send-email-josiahw@gmail.com> <20150617093056.GD22285@port70.net> 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 1434588062 5271 80.91.229.3 (18 Jun 2015 00:41:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 18 Jun 2015 00:41:02 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7966-gllmg-musl=m.gmane.org@lists.openwall.com Thu Jun 18 02:41:00 2015 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 1Z5Nt2-0004w5-25 for gllmg-musl@m.gmane.org; Thu, 18 Jun 2015 02:40:56 +0200 Original-Received: (qmail 18091 invoked by uid 550); 18 Jun 2015 00:40:54 -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 18073 invoked from network); 18 Jun 2015 00:40:53 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:7953 Archived-At: On Wed, Jun 17, 2015 at 02:07:08PM -0500, Josiah Worcester wrote: > >> +size_t __wcrtomb_chk(char *restrict s, wchar_t wc, mbstate_t *restrict st, size_t slen) > >> +{ > >> + if (slen < MB_CUR_MAX) a_crash(); > >> + return wcrtomb(s, wc, st); > >> +} > > > > i think this can cause a false positive crash > > but glibc seems to do the same.. > > > > (eg some api passes wchar_t* and the exact mb encoded length of > > a string so the output s can be safely shorter than max mb length) > > > I'm not sure if we should match glibc here or do a better check. > > Thanks for the input. I'm going to wait a bit before posting a new > patch, though, in hopes that more comments are forthcoming. This code is definitely wrong as-is; for example: char c; wcrtomb(&c, L'0', st) is perfectly valid (albeit useless) code. Other examples are possible too. I think a decent check would be: size_t r = wcrtomb(s, wc, st); if (r+1 > slen+1) a_crash(); return r; This allows an overflow of up to 3 bytes to occur before the trap, but there's no way a valid dest buffer could be within 3 bytes of this function's or wcrtomb's stack, so you should safely reach the a_crash even on overflow. The alternative would be using a temp buffer of size MB_LEN_MAX and then performing memcpy to the caller's buffer if the result fits or a_crash if not. Rich