Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp895251ybe; Fri, 13 Sep 2019 07:59:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqyhbyOLQON1eJBL35OvlNeejyA5GtsWFMnTqkqHvexPyUNQFKWbzVZlHdZw7voETT+1F+sS X-Received: by 2002:a50:f0db:: with SMTP id a27mr3177955edm.17.1568386795758; Fri, 13 Sep 2019 07:59:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568386795; cv=none; d=google.com; s=arc-20160816; b=IgTfwYaDP6Hjq38g428HGFHdq8lMir5MJe5Xp06rFrW0nws/5K6RdomoMiIp0yx1xa OhCm0nOJYaUN/qzcvA2qnn4AisCNstw+1jXI/+ixR69KpYSv0kyo4rrcDP5bGFm31nvx bq5KUAgZcs4AMOchSbAuP4Bra5jkXeowYZe+zgMoG0/hX/ZWpykgfyIRh5+yQ1ct5kLt cFba0wk5mNDGvoT5GCxENpqI0XIefIThwDVt0auXiui1HcyAJM5+6q+sD9VrKQRBjhTj MjxlOIF0Y6pgf74XUhfh6RSW5k8QabukjrqibAlroUc7Y4mzF04236p17gJ864wKs3uU dK/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DY7HFEAXz7W+rMMGJvY9bqbJO0ihPIWdLIHx9xTiyJA=; b=n9eBYFvi7b2BDGhXPNTS5enrpx7nRQvRUKQ9z9Y1bkM8HvR5i/Y4bwGIwYUkUcpQAQ gUAp5zyIajpOY6uy1oONzztipRphCAR/NtAHvstZAcjafyh0rXGQsooB2QvNQWbEHLeo vj45r8FP9hER7jeU6oyAMFrcVSIcbdtMh+RLAUlXqiwqPCROoWUsrS6YGJwRAnaPDVsW L6wxjYhqLEI5ErOx+p9Ieu/p+Ls2omoA94TgmJ1LgoVNt6kN+6qyMwKOE5zbdZdzKT+5 eenX8ODLjhZBiludcaq4iPuFybQ5jQqzH60XKTYDVlsxZzKxvcNJu3NAL3Ujz5sAunc+ vj1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="aHOY/RLC"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k15si1056629eds.398.2019.09.13.07.59.31; Fri, 13 Sep 2019 07:59:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="aHOY/RLC"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390824AbfIMNV1 (ORCPT + 99 others); Fri, 13 Sep 2019 09:21:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:51072 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389107AbfIMNV0 (ORCPT ); Fri, 13 Sep 2019 09:21:26 -0400 Received: from localhost (unknown [104.132.45.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 499C0206A5; Fri, 13 Sep 2019 13:21:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568380885; bh=VD0f0+ab+C+4GW+EAuXTuKN+g/TJz9sfwB1E3e6vQIs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aHOY/RLCAI2ySYFA9EHhOELGDTnTn37caVnxMqAuWc1pgXFDZFu0MSNoG0IZxP9xw fQYUxG8Os5l9NQ53jCLYig/Qf8IFnQo+TjxkEpob14KHoVT9BuavNoFaypX0zMnU0Y /lfugZBaiSJPC4UJGZTZ2/dpUOG1hZDuxuAdq5p0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Thomas Hellstrom Subject: [PATCH 5.2 09/37] drm/vmwgfx: Fix double free in vmw_recv_msg() Date: Fri, 13 Sep 2019 14:07:14 +0100 Message-Id: <20190913130513.448614886@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190913130510.727515099@linuxfoundation.org> References: <20190913130510.727515099@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Carpenter commit 08b0c891605acf727e43e3e03a25857d3e789b61 upstream. We recently added a kfree() after the end of the loop: if (retries == RETRIES) { kfree(reply); return -EINVAL; } There are two problems. First the test is wrong and because retries equals RETRIES if we succeed on the last iteration through the loop. Second if we fail on the last iteration through the loop then the kfree is a double free. When you're reading this code, please note the break statement at the end of the while loop. This patch changes the loop so that if it's not successful then "reply" is NULL and we can test for that afterward. Cc: Fixes: 6b7c3b86f0b6 ("drm/vmwgfx: fix memory leak when too many retries have occurred") Signed-off-by: Dan Carpenter Reviewed-by: Thomas Hellstrom Signed-off-by: Thomas Hellstrom Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/vmwgfx/vmwgfx_msg.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) --- a/drivers/gpu/drm/vmwgfx/vmwgfx_msg.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_msg.c @@ -353,7 +353,7 @@ static int vmw_recv_msg(struct rpc_chann !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB)); if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) { kfree(reply); - + reply = NULL; if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) { /* A checkpoint occurred. Retry. */ continue; @@ -377,7 +377,7 @@ static int vmw_recv_msg(struct rpc_chann if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) { kfree(reply); - + reply = NULL; if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) { /* A checkpoint occurred. Retry. */ continue; @@ -389,10 +389,8 @@ static int vmw_recv_msg(struct rpc_chann break; } - if (retries == RETRIES) { - kfree(reply); + if (!reply) return -EINVAL; - } *msg_len = reply_len; *msg = reply;