Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5477205ybi; Tue, 28 May 2019 13:51:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqzdkTsGLvEE1WMGRtk8ga9yZ6m/aLVjyXZq6vE0P17VU41jiJw4J3aucVwuOqVfw5kN9HhY X-Received: by 2002:a63:a84c:: with SMTP id i12mr137083352pgp.115.1559076697725; Tue, 28 May 2019 13:51:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559076697; cv=none; d=google.com; s=arc-20160816; b=xtptZihKKGrqi6dJDnMCiMs2GFOFSVaLPVLbHn4Yc5hNcBVCEpHlBd3yDWWQw4HCkq qWqQLWjws16Y4P+EzkGZTQM2I3tA5h3jumgc0PZFmqwBmjQ3e7C9aResM+M35pbfw57S zIjgUHzeRPRsq0b7NCCltS3PuDxO9IQGUU9WW9qvDM7fG+c3eVf4W/kNwj40O/Xue+9Q R6Z4llhh9uN9gmxQXHHCziCJFUtcVkEyL3VX14e/SVt4LmBpSBk1tBzhRZf6ou//2ic7 AZj+nvoQV+XL43mDBzdFwi1LTTQALckkmc7SC/NctDPWig/gR92IDwRrkwIZqk5/34yb M27w== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject :dkim-signature; bh=k3l55ECOd6G49lDcW/Ub9navCGchmZ/ngyO7S9XQJ6c=; b=gwULC0z6hRKD/jj8+T/uqRlHwLh5pFCQTXqOBj8853s/wGSnqUXyxrtLyH/J4+PVnk DoqYfhh10xBr5DN2nUXyPbGmMreHUE1qlCNwT0kIopXCvefk9pA209nRxJvOSFBxIvGR LumsD/0dBmuwmoJW9NgLtrD3kbXVl8Mq9bj5BPlV4eYZDVyovizyLSwqDBI2wavvlWsA I9DqP2Iwwa5I51wnh36UwbXlEwnK1v7kVU+HJ1SVLcBjY6WfRjxIkATD0ROhrxQtEB1/ LqorgWGxK/7nfx7j8XOoKUhF9IrUeOTgEOW490WNlazqqoBGX7A3RclJILXAsoAE7/C4 eODg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PZ9ELxRT; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v65si27124850pfb.77.2019.05.28.13.51.21; Tue, 28 May 2019 13:51:37 -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=@gmail.com header.s=20161025 header.b=PZ9ELxRT; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727710AbfE1Usv (ORCPT + 99 others); Tue, 28 May 2019 16:48:51 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:34597 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726683AbfE1Usv (ORCPT ); Tue, 28 May 2019 16:48:51 -0400 Received: by mail-lj1-f194.google.com with SMTP id j24so258364ljg.1 for ; Tue, 28 May 2019 13:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=k3l55ECOd6G49lDcW/Ub9navCGchmZ/ngyO7S9XQJ6c=; b=PZ9ELxRTP+rOjmudehGgkfrpzwB+tYb4J+9NuSIsIIjunFAET6NcKYWgV5I6CLjV7N VPaDjyoROvcKhuB5n8kymbXQ5WTHYJoFxONFsy/U9SrmTdwwfaF+E/gpvu8vd/TjhgZq tCFXX7774TErU6adjntnnasvvstKbpibjWPX+HbII1k39SvMa6hmGf1lxP2qpb5WdqC/ LmG6HoS/8Fpq/W5kt68w73bcOphbIluSIqmeUcQqlCMJ6sypSS/os04PKi22d9MDGCKv tnMiDBuLNybYX8rGbpJgulObucTNte2fk7nLGfCmyBqiVcEDO4IyzA8leAwoHWsYAjWT rCnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=k3l55ECOd6G49lDcW/Ub9navCGchmZ/ngyO7S9XQJ6c=; b=rxKaMG7w4FH3Jr+6h8oxzbM6lHS5Z5O2f122NNTZG5NErB8BaSRQQ4794qirJY3+AX 7NWhmIQxkyX82xqixLY+/cU1whVLSLqdEFnwB2nSnwdr33f6wFbFXWWTyBK3HHkjo8NV QSdZmPCaQs+PkZzHPZub4ryxmsG16j+kMZ9HwxwQeW2x4/WizulmTt4KmNdPnofUB4BI yuh6XitAmwthF4mKW8o5GNOtZksbso2KIINwzJufnkVjO4BD0c0QaeXNKPthYnFxIz+A Iu/yBHHM9Z+JudAd1PbxkjNoxALMA1XfTQwu7565phVWTljsG0ZLR0h2vAambWcKP3GL mcMA== X-Gm-Message-State: APjAAAWFT7smwy4g4iDqDfWhEK7m1HVlb1M15/vneipBOPqnigheL8/X HgJLE/ACDpzYOFwrcqNQq8g= X-Received: by 2002:a2e:9742:: with SMTP id f2mr32737091ljj.184.1559076528334; Tue, 28 May 2019 13:48:48 -0700 (PDT) Received: from [192.168.1.15] (2-111-15-75-dynamic.dk.customer.tdc.net. [2.111.15.75]) by smtp.gmail.com with ESMTPSA id d20sm3111879lja.32.2019.05.28.13.48.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 May 2019 13:48:47 -0700 (PDT) Subject: Re: [PATCH] trace: Avoid memory leak in predicate_parse() To: Steven Rostedt Cc: linux-kernel@vger.kernel.org, mingo@redhat.com References: <20190528104400.388e4c3f@gandalf.local.home> <20190528154338.29976-1-tomasbortoli@gmail.com> <20190528163104.67763762@gandalf.local.home> From: Tomas Bortoli Openpgp: preference=signencrypt Autocrypt: addr=tomasbortoli@gmail.com; prefer-encrypt=mutual; keydata= mQINBFpCTZMBEADNZ1+Ibh0Z4pgGRcd1aOUMbe/YfHktmajjcoTnKmZZunjoUVAl8waeLITd BC2c8i1wHzHcnthrmb1izs5XlG6PZnl8n5tjysSNbwggzS1NcEK1qgn5VjNlHQ5aRMUwCC51 kicBiNmlQk2UuzzWwdheRGnaf+O1MNhC0GBeEDKQAL5obOU92pzflv6wWNACr+lHxdnpyies mOnRMjH16NjuTkrGbEmJe+MKp0qbjvR3R/dmFC1wczniRMQmV5w3MZ/N9wRappE+Atc1fOM+ wP7AWNuPvrKg4bN5uqKZLDFH7OFpxvjgVdWM40n0cQfqElWY9as+228Sltdd1XyHtUWRF2VW O1l5L0kX0+7+B5k/fpLhXqD3Z7DK7wRXpXmY59pofk7aFdcN97ZK+r6R7mqrwX4W9IpsPhkT kUyg3/Dx/khBZlJKFoUP325/hoH684bSiPEBroel9alB7gTq2ueoFwy6R3q5CMUw3D+CZWHA 3xllu46TRQ/Vt2g0cIHQNPoye2OWYFJ6kSEvaLpymjNDJ9ph2EuHegonDfOaYSq34ic2BcdB JkCgXRLP5K7KtRNJqqR+DM8xByeGmQv9yp6S97el+SiM9R53RhHawJZGz0EPl+2Q6+5mgh3u wXOlkmGrrSrlB8lc567l34ECl6NFtUPIL7H5vppIXAFl7JZUdQARAQABtB50b21hcyA8dG9t YXNib3J0b2xpQGdt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MUGVHhQPZ0OzjHIKS6cTzN6ue8bB+CGOLCaQp1 C0NRT5Tn9zpLxtf5nBExFd/zVENY5vAV2ZbKQdemO54O7j6B9DSgVRrm83GCZxbL4d+qTYBF 3tSCWw/6SG1F3q9gR9QrSC2YRjCmhijUVEh6FhZwB58TNZ1sEEttrps8TDa5tUd9 Message-ID: <77f8591e-6230-7341-7bb3-0599c16bea92@gmail.com> Date: Tue, 28 May 2019 22:48:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190528163104.67763762@gandalf.local.home> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/28/19 10:31 PM, Steven Rostedt wrote: > On Tue, 28 May 2019 17:43:38 +0200 > Tomas Bortoli wrote: > >> @@ -578,6 +578,8 @@ predicate_parse(const char *str, int nr_parens, int nr_preds, >> out_free: >> kfree(op_stack); >> kfree(inverts); >> + for (i = 0; prog_stack[i].pred; i++) >> + kfree(prog_stack[i].pred); >> kfree(prog_stack); >> return ERR_PTR(ret); >> } > > I should have caught this, but thanks to the zero day bot, it found it > first: > > kernel/trace/trace_events_filter.c:582:27-31: ERROR: prog_stack is NULL but dereferenced. > > I changed the patch with the following: > > From dfb4a6f2191a80c8b790117d0ff592fd712d3296 Mon Sep 17 00:00:00 2001 > From: Tomas Bortoli > Date: Tue, 28 May 2019 17:43:38 +0200 > Subject: [PATCH] tracing: Avoid memory leak in predicate_parse() > > In case of errors, predicate_parse() goes to the out_free label > to free memory and to return an error code. > > However, predicate_parse() does not free the predicates of the > temporary prog_stack array, thence leaking them. > > Link: http://lkml.kernel.org/r/20190528154338.29976-1-tomasbortoli@gmail.com > > Cc: stable@vger.kernel.org > Fixes: 80765597bc587 ("tracing: Rewrite filter logic to be simpler and faster") > Reported-by: syzbot+6b8e0fb820e570c59e19@syzkaller.appspotmail.com > Signed-off-by: Tomas Bortoli > [ Added protection around freeing prog_stack[i].pred ] > Signed-off-by: Steven Rostedt (VMware) > --- > kernel/trace/trace_events_filter.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c > index d3e59312ef40..5079d1db3754 100644 > --- a/kernel/trace/trace_events_filter.c > +++ b/kernel/trace/trace_events_filter.c > @@ -428,7 +428,7 @@ predicate_parse(const char *str, int nr_parens, int nr_preds, > op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL); > if (!op_stack) > return ERR_PTR(-ENOMEM); > - prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL); > + prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL); > if (!prog_stack) { > parse_error(pe, -ENOMEM, 0); > goto out_free; > @@ -579,7 +579,11 @@ predicate_parse(const char *str, int nr_parens, int nr_preds, > out_free: > kfree(op_stack); > kfree(inverts); > - kfree(prog_stack); > + if (prog_stack) { > + for (i = 0; prog_stack[i].pred; i++) > + kfree(prog_stack[i].pred); > + kfree(prog_stack); > + } > return ERR_PTR(ret); > } > > Oops again, I should have been more careful. Thanks.