From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60451) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adUJs-00015z-1w for qemu-devel@nongnu.org; Tue, 08 Mar 2016 21:57:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1adUJo-0006dj-Q1 for qemu-devel@nongnu.org; Tue, 08 Mar 2016 21:57:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36039) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adUJo-0006dT-JW for qemu-devel@nongnu.org; Tue, 08 Mar 2016 21:57:48 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 059C2C0003F5 for ; Wed, 9 Mar 2016 02:57:48 +0000 (UTC) Date: Wed, 9 Mar 2016 10:57:38 +0800 From: Peter Xu Message-ID: <20160309025738.GF2377@pxdev.xzpeter.org> References: <1457420446-25276-1-git-send-email-peterx@redhat.com> <1457420446-25276-2-git-send-email-peterx@redhat.com> <87pov5e5w3.fsf@blackfin.pond.sub.org> <20160308101944.GD5807@noname.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20160308101944.GD5807@noname.str.redhat.com> Subject: Re: [Qemu-devel] [PATCH 1/8] qdict: fix unbounded stack for qdict_array_entries List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: pbonzini@redhat.com, Luiz Capitulino , Markus Armbruster , qemu-devel@nongnu.org On Tue, Mar 08, 2016 at 11:19:44AM +0100, Kevin Wolf wrote: > Am 08.03.2016 um 09:22 hat Markus Armbruster geschrieben: > > Same arguments as for PATCH 2, except here an argument on the maximum > > length of subqdict would probably be easier. > > Yes, these are constant string literals in all callers, including the > one non-test case in quorum. > > Let's simply assert a reasonable maximum for subqdict_length. The > minimum we need to allow with the existing callers is 9, and I expect > we'll never get keys longer than 16 characters. Hi, Kevin, Markus, The patch should be trying to do as mentioned above. To make it clearer, how about the following one: diff --git a/qobject/qdict.c b/qobject/qdict.c index 9833bd0..dde99e0 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -704,17 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict) for (i = 0; i < INT_MAX; i++) { QObject *subqobj; int subqdict_entries; - size_t slen = 32 + subqdict_len; - char indexstr[slen], prefix[slen]; + char indexstr[128], prefix[128]; size_t snprintf_ret; - snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i); - assert(snprintf_ret < slen); + snprintf_ret = snprintf(indexstr, ARRAY_SIZE(indexstr), "%s%u", subqdict, i); + assert(snprintf_ret < ARRAY_SIZE(indexstr)); subqobj = qdict_get(src, indexstr); - snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i); - assert(snprintf_ret < slen); + snprintf_ret = snprintf(prefix, ARRAY_SIZE(prefix), "%s%u.", subqdict, i); + assert(snprintf_ret < ARRAY_SIZE(prefix)); subqdict_entries = qdict_count_prefixed_entries(src, prefix); if (subqdict_entries < 0) { Two assertions on the snprintf_ret should make sure we are safe, right? Thanks. Peter