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 1A583C433E0 for ; Thu, 25 Feb 2021 17:42:26 +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 BC96064F1B for ; Thu, 25 Feb 2021 17:42:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BC96064F1B 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.89893.169808 (Exim 4.92) (envelope-from ) id 1lFKeX-0006Cz-4Q; Thu, 25 Feb 2021 17:42:17 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 89893.169808; Thu, 25 Feb 2021 17:42:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lFKeW-0006CT-UQ; Thu, 25 Feb 2021 17:42:16 +0000 Received: by outflank-mailman (input) for mailman id 89893; Thu, 25 Feb 2021 17:42:16 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lFKeW-0006Bm-Aw for xen-devel@lists.xenproject.org; Thu, 25 Feb 2021 17:42:16 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lFKeV-0005qz-Js; Thu, 25 Feb 2021 17:42:15 +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 1lFKeV-00032g-B9; Thu, 25 Feb 2021 17:42:15 +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=Ouy6xY5aZ/aq/+pbqmrj55EhVefIirlCz92nwNMOsZA=; b=x+8bIRwILAv/nmOo1cc9xwPdm 7qBOBZtcexmHlyNMvcxQsWnqcX9OqY/8HPnwk3AP1njlyD940IhnTQ7kfbGv7Sao2Ml4OJVzbK5Zt Uqi/WT+drUEonBHHtA1VUbCaEwWvKrEv2Iz5qTmpa6DZRSt17NnvoUaHxWuZF++9BLGnE=; From: Julien Grall To: xen-devel@lists.xenproject.org Cc: raphning@amazon.co.uk, iwj@xenproject.org, Julien Grall , Wei Liu , Juergen Gross Subject: [PATCH for-4.15 3/5] tools/xenstored: control: Store the save filename in lu_dump_state Date: Thu, 25 Feb 2021 17:41:29 +0000 Message-Id: <20210225174131.10115-4-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210225174131.10115-1-julien@xen.org> References: <20210225174131.10115-1-julien@xen.org> From: Julien Grall The function lu_close_dump_state() will use talloc_asprintf() without checking whether the allocation succeeded. In the unlikely case we are out of memory, we would dereference a NULL pointer. As we already computed the filename in lu_get_dump_state(), we can store the name in the lu_dump_state. This is avoiding to deal with memory file in the close path and also reduce the risk to use the different filename. This bug was discovered and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc. Fixes: c0dc6a3e7c41 ("tools/xenstore: read internal state when doing live upgrade") Signed-off-by: Julien Grall --- tools/xenstore/xenstored_control.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/xenstore/xenstored_control.c b/tools/xenstore/xenstored_control.c index 8eb57827765c..653890f2d9e0 100644 --- a/tools/xenstore/xenstored_control.c +++ b/tools/xenstore/xenstored_control.c @@ -16,6 +16,7 @@ Interactive commands for Xen Store Daemon. along with this program; If not, see . */ +#include #include #include #include @@ -74,6 +75,7 @@ struct lu_dump_state { unsigned int size; #ifndef __MINIOS__ int fd; + char *filename; #endif }; @@ -399,17 +401,16 @@ static void lu_dump_close(FILE *fp) static void lu_get_dump_state(struct lu_dump_state *state) { - char *filename; struct stat statbuf; state->size = 0; - filename = talloc_asprintf(NULL, "%s/state_dump", xs_daemon_rootdir()); - if (!filename) + state->filename = talloc_asprintf(NULL, "%s/state_dump", + xs_daemon_rootdir()); + if (!state->filename) barf("Allocation failure"); - state->fd = open(filename, O_RDONLY); - talloc_free(filename); + state->fd = open(state->filename, O_RDONLY); if (state->fd < 0) return; if (fstat(state->fd, &statbuf) != 0) @@ -431,14 +432,13 @@ static void lu_get_dump_state(struct lu_dump_state *state) static void lu_close_dump_state(struct lu_dump_state *state) { - char *filename; + assert(state->filename != NULL); munmap(state->buf, state->size); close(state->fd); - filename = talloc_asprintf(NULL, "%s/state_dump", xs_daemon_rootdir()); - unlink(filename); - talloc_free(filename); + unlink(state->filename); + talloc_free(state->filename); } static char *lu_exec(const void *ctx, int argc, char **argv) -- 2.17.1