From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751821AbdJZGVe (ORCPT ); Thu, 26 Oct 2017 02:21:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:48840 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751651AbdJZGVd (ORCPT ); Thu, 26 Oct 2017 02:21:33 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 65B96218B5 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=mhiramat@kernel.org Date: Thu, 26 Oct 2017 15:21:30 +0900 From: Masami Hiramatsu To: chenjiankang Cc: , , , , , Subject: Re: [PATCH] kernel/kprobes: add check to avoid kprobe memory leak Message-Id: <20171026152130.119473592483b3106b2ac4df@kernel.org> In-Reply-To: References: <1508847422-63641-1-git-send-email-chenjiankang1@huawei.com> <20171026015453.ba08699319ffaa9a18bbd8e3@kernel.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 26 Oct 2017 09:22:23 +0800 chenjiankang wrote: > > On Tue, 24 Oct 2017 20:17:02 +0800 > > JianKang Chen wrote: > > > >> The function register_kretprobe is used to initialize a struct > >> kretprobe and allocate a list table for kprobe instance. > >> However,in this function, there is a memory leak. > >> > >> The test case: > >> > >> static struct kretprobe rp; > >> struct kretprobe *rps[10]={&rp ,&rp ,&rp , > >> &rp ,&rp ,&rp ,&rp ,&rp ,&rp,&rp}; > > > > What ? this is buggy code. you must not list same kretprobe. > > But, year, since register_kprobe() already has similar protection against > > reusing, register_kretprobe() should do so. > > > > [..] > >> raw_spin_lock_init(&rp->lock); > >> + > >> + if (!hlist_empty(&rp->free_instances)) > >> + return -EBUSY; > >> + > > > > Hmm, but can you use check_kprobe_rereg() before raw_spin_lock_init()? > > If user reuses rp after it starts, rp->lock can already be used. > > Hmm, your advice is very good, we can use check_kprobe_rereg() at > the beginning of the register_kretprobe(); > > For example: > > int register_kretprobe(struct kretprobe *rp) > { > int ret = 0; > struct kretprobe_instance *inst; > int i; > void *addr; > > ret = check_kprobe_rereg(&rp->kp); > if (ret) > return ret; Yeah, this looks much better for me :) Thanks, > > Thank you! > > -- Masami Hiramatsu