From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEF48C43460 for ; Fri, 2 Apr 2021 15:21:30 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 69AF861176 for ; Fri, 2 Apr 2021 15:21:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 69AF861176 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.104890.200910 (Exim 4.92) (envelope-from ) id 1lSLbl-0004Uz-GG; Fri, 02 Apr 2021 15:21:13 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 104890.200910; Fri, 02 Apr 2021 15:21:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lSLbl-0004Us-Cn; Fri, 02 Apr 2021 15:21:13 +0000 Received: by outflank-mailman (input) for mailman id 104890; Fri, 02 Apr 2021 15:21:12 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lSLbk-0004Ui-Nm for xen-devel@lists.xenproject.org; Fri, 02 Apr 2021 15:21:12 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lSLbj-00009o-PF; Fri, 02 Apr 2021 15:21:11 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSLbj-00050B-EP; Fri, 02 Apr 2021 15:21:11 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=jQfOL7sF0Ax2+OqBBQai2IFHdDIfjtAQ7KE5lYnqt0Y=; b=gv+DsuaLbFWMDN4dwFQLxy6t+ yuFXCq5ZQAMJkrwLTwuX0Mz/MJBvTbulaBmBt1Q993VWbR+PTA8mZvm0SPlKUUtkOsmBexrEYRcY1 V4KrLOu6zWgicNymonlWP8bQMNSCMfRcu2B0/nM/rq54VdUONKLKDjvhiUF7BpzaFui2k=; From: Julien Grall To: xen-devel@lists.xenproject.org Cc: bertrand.marquis@arm.com, Julien Grall , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH 2/2] xen/gunzip: Allow perform_gunzip() to be called multiple times Date: Fri, 2 Apr 2021 16:21:05 +0100 Message-Id: <20210402152105.29387-3-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210402152105.29387-1-julien@xen.org> References: <20210402152105.29387-1-julien@xen.org> From: Julien Grall Currently perform_gunzip() can only be called once because the the internal state (e.g allocate) is not fully re-initialized. This works fine if you are only booting dom0. But this will break when booting multiple using the dom0less that uses compressed kernel images. This can be resolved by re-initializing bytes_out, malloc_ptr, malloc_count every time perform_gunzip() is called. Note the latter is only re-initialized for hardening purpose as there is no guarantee that every malloc() are followed by free() (It should in theory!). Take the opportunity to check the return of alloc_heap_pages() to return an error rather than dereferencing a NULL pointer later on failure. Reported-by: Charles Chiou Signed-off-by: Julien Grall --- xen/common/gunzip.c | 5 +++++ xen/common/inflate.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/xen/common/gunzip.c b/xen/common/gunzip.c index db4efcd34b77..425d64e904d8 100644 --- a/xen/common/gunzip.c +++ b/xen/common/gunzip.c @@ -114,11 +114,16 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len) window = (unsigned char *)output; free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER, 0); + if ( !free_mem_ptr ) + return -ENOMEM; + free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER); + init_allocator(); inbuf = (unsigned char *)image; insize = image_len; inptr = 0; + bytes_out = 0; makecrc(); diff --git a/xen/common/inflate.c b/xen/common/inflate.c index f99c985d6135..d8c28a3e9593 100644 --- a/xen/common/inflate.c +++ b/xen/common/inflate.c @@ -238,6 +238,12 @@ STATIC const ush mask_bits[] = { static unsigned long INITDATA malloc_ptr; static int INITDATA malloc_count; +static void init_allocator(void) +{ + malloc_ptr = free_mem_ptr; + malloc_count = 0; +} + static void *INIT malloc(int size) { void *p; -- 2.17.1