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=-6.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,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 64138C43219 for ; Tue, 30 Apr 2019 11:42:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3761E2184C for ; Tue, 30 Apr 2019 11:42:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556624542; bh=77D6z18oj1v3jnBnn8XBJEpwVJSyPUUId9lJz6jmG7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=dJJKw/W6UZC2x5jll9sYtlP7hFfhJ7vF5TKBRVhWdeZS571rHX5utVg2eFOLXPtsd zdfpCDPeRp3oWUihkRr/FvTgo73SDg0LNx0sjeHdLt9ve3shWsNhE3bWg3TpedmR/M MGwz57qdFP6N9ldZqeLn13VAS/ysTyCngqfftHwk= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729279AbfD3LmV (ORCPT ); Tue, 30 Apr 2019 07:42:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:51040 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727770AbfD3LmR (ORCPT ); Tue, 30 Apr 2019 07:42:17 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 E16A821734; Tue, 30 Apr 2019 11:42:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556624536; bh=77D6z18oj1v3jnBnn8XBJEpwVJSyPUUId9lJz6jmG7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K560cWkVkcjBI3AJklte0DMLHIhMRnh36Wj6j2rh/IhZ+WunUrpjyCEcJS9fTA/bF 3XVSS9HfwQMoQ2mKQt7CcRCofSeDUnX2FT8OuiS79wT7opyLg3c8q3cGVpL015cCfL Xru4Ij18+ydC0NevXcGt4VRO9QOSJ5f9AExvX5r4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wenwen Wang , "Steven Rostedt (VMware)" Subject: [PATCH 4.14 03/53] tracing: Fix a memory leak by early error exit in trace_pid_write() Date: Tue, 30 Apr 2019 13:38:10 +0200 Message-Id: <20190430113550.171594651@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190430113549.400132183@linuxfoundation.org> References: <20190430113549.400132183@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: Wenwen Wang commit 91862cc7867bba4ee5c8fcf0ca2f1d30427b6129 upstream. In trace_pid_write(), the buffer for trace parser is allocated through kmalloc() in trace_parser_get_init(). Later on, after the buffer is used, it is then freed through kfree() in trace_parser_put(). However, it is possible that trace_pid_write() is terminated due to unexpected errors, e.g., ENOMEM. In that case, the allocated buffer will not be freed, which is a memory leak bug. To fix this issue, free the allocated buffer when an error is encountered. Link: http://lkml.kernel.org/r/1555726979-15633-1-git-send-email-wang6495@umn.edu Fixes: f4d34a87e9c10 ("tracing: Use pid bitmap instead of a pid array for set_event_pid") Cc: stable@vger.kernel.org Signed-off-by: Wenwen Wang Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -494,8 +494,10 @@ int trace_pid_write(struct trace_pid_lis * not modified. */ pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); - if (!pid_list) + if (!pid_list) { + trace_parser_put(&parser); return -ENOMEM; + } pid_list->pid_max = READ_ONCE(pid_max); @@ -505,6 +507,7 @@ int trace_pid_write(struct trace_pid_lis pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); if (!pid_list->pids) { + trace_parser_put(&parser); kfree(pid_list); return -ENOMEM; }