From patchwork Fri Jun 28 16:58:13 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 255479 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 814522C0091 for ; Sat, 29 Jun 2013 03:00:21 +1000 (EST) Received: from localhost ([::1]:37264 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usc1z-0005jG-Fv for incoming@patchwork.ozlabs.org; Fri, 28 Jun 2013 13:00:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33079) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usc0Q-0003Yo-8E for qemu-devel@nongnu.org; Fri, 28 Jun 2013 12:58:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Usc0O-0001i2-WB for qemu-devel@nongnu.org; Fri, 28 Jun 2013 12:58:42 -0400 Received: from mail-ee0-x229.google.com ([2a00:1450:4013:c00::229]:60304) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Usc0O-0001hw-Ps for qemu-devel@nongnu.org; Fri, 28 Jun 2013 12:58:40 -0400 Received: by mail-ee0-f41.google.com with SMTP id d17so1169983eek.28 for ; Fri, 28 Jun 2013 09:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=q3TtfLoFmdvpfwgajBUnKXTlXPdSQNyceUokvQU/IkI=; b=huaho0PqlzE7ZZgWx3vC2Ux/eoQbGSnBqefYRQ/Uujqtuo6nVfPVKFkSfFhJQlsYls H8IX3lZkfFgAP1HruYeg9yyiOHwTboGkn7unXVMYwHq2nl3B8lD9FNPrAMTGr8ckxPam ihauSJyVbWY1UmGnmtutVq9B/fcM26p2BMJAGJDvkmBjLZg+wqyoXZrdf8cJ0nRooyqy hbiCtTMNHVLkG3k6SzVk/dqRphLvfrMHn9udK2WtdTJQCFJf0stQi2QXWwZpEC2GoNif 85y5R5yo1edyiP285703sJjAMudesuIS5rMZjT0VdWTYOV0PxtOI4hjooVTehCxITnO3 iWpA== X-Received: by 10.15.24.13 with SMTP id i13mr14671918eeu.23.1372438719836; Fri, 28 Jun 2013 09:58:39 -0700 (PDT) Received: from playground.lan (net-37-116-217-184.cust.dsl.vodafone.it. [37.116.217.184]) by mx.google.com with ESMTPSA id w43sm11609000eez.6.2013.06.28.09.58.37 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 28 Jun 2013 09:58:39 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2013 18:58:13 +0200 Message-Id: <1372438702-20491-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1372438702-20491-1-git-send-email-pbonzini@redhat.com> References: <1372438702-20491-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c00::229 Cc: jan.kiszka@siemens.com, peter.maydell@linaro.org Subject: [Qemu-devel] [PATCH v2 02/11] memory: destroy phys_sections one by one X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org phys_sections_clear is invoked after the dispatch tree has been destroyed. This leaves a window where phys_sections_nb > 0 but the subpages are not valid anymore, which is a recipe for use-after-free bugs. Move the destruction of subpages in phys_sections_clear. Signed-off-by: Paolo Bonzini --- exec.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/exec.c b/exec.c index 1118587..e7eadf5 100644 --- a/exec.c +++ b/exec.c @@ -762,17 +762,6 @@ hwaddr memory_region_section_get_iotlb(CPUArchState *env, static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, uint16_t section); static subpage_t *subpage_init(AddressSpace *as, hwaddr base); -static void destroy_page_desc(uint16_t section_index) -{ - MemoryRegionSection *section = &phys_sections[section_index]; - MemoryRegion *mr = section->mr; - - if (mr->subpage) { - subpage_t *subpage = container_of(mr, subpage_t, iomem); - memory_region_destroy(&subpage->iomem); - g_free(subpage); - } -} static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level) { @@ -787,8 +776,6 @@ static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level) for (i = 0; i < L2_SIZE; ++i) { if (!p[i].is_leaf) { destroy_l2_mapping(&p[i], level - 1); - } else { - destroy_page_desc(p[i].ptr); } } lp->is_leaf = 0; @@ -818,9 +805,21 @@ static uint16_t phys_section_add(MemoryRegionSection *section) return phys_sections_nb++; } +static void phys_section_destroy(MemoryRegion *mr) +{ + if (mr->subpage) { + subpage_t *subpage = container_of(mr, subpage_t, iomem); + memory_region_destroy(&subpage->iomem); + g_free(subpage); + } +} + static void phys_sections_clear(void) { - phys_sections_nb = 0; + while (phys_sections_nb > 0) { + MemoryRegionSection *section = &phys_sections[--phys_sections_nb]; + phys_section_destroy(section->mr); + } } static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)