Received: by 10.192.165.156 with SMTP id m28csp798561imm; Thu, 19 Apr 2018 07:45:24 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+VyUm8FqdK+6UPos5m5dLKwwhAUypdSAyc9j9+zzz3emoDpkAO7UrgMIwEIjVozSRt34oN X-Received: by 10.99.178.3 with SMTP id x3mr5366368pge.266.1524149124857; Thu, 19 Apr 2018 07:45:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524149124; cv=none; d=google.com; s=arc-20160816; b=anHLhYtzTCKtsNQcBLjB5X2dIc0FlYhet+22mvF3o2iRG82veU5MYSvA6k3ThbhxvD lrRNNszRi6neXBuMdPBTNdtnzgxxIfLdneWzCfo1Lvn5fHTwwH4Q+/igcsjylIRiIPqh ELZYkBx1NXJSECrPO0hhhIcW0Xj8+S5C35/xj6qr+2sc0nNk7l5uezOHYxKdF4n/MsX2 Ob2Vbf1ixy1cgDS6/CzzDZk/8uAtnVjiGbsGmV3d88UvqAW4sAkwYXSMmtMCm/N4BT02 DtO+sirmOWKuKs+uzEztKsC2oweeR2RVxlspqKlSna65RgzDrRV17Au5IsYFE49fp9XA BpSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=bNgT0WVV5qAYrXgvFAJMy+2+pX+svM9QtuuCPRVt4Rk=; b=inyIwRXaaan4+PTdGG6x8unk+PMvw/iRFFbrl+4EFNK2prbvtHmD/X5r9/eMlh2CT6 jI7RFgO/2RzyYuRrGtNQJ61yL3lfUZdwLt5WKZSlB49MgJCXmCLomP31K7Un0QzPkwkM X5pPyKew6P1Ao0zltI9YQVPsqooQXjmKxGw7QgPGyvRUpswuTAjp8xkC/sm1j86LvzUw RoumnSFZOCE5ZK/zXSfC44GmShl2aGzE0LDVxXNE6+Z+8G46XySUzBCvHyf3+vq1x9Tn Q0Rv8M6tx/z2HTW+MDIsLqNgD9bMw1x+zkwYLPkPsndjwW+4leK52bFLKssLdn9QRB6d cJFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=TtzpspR8; 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 q7si3224803pga.86.2018.04.19.07.45.11; Thu, 19 Apr 2018 07:45:24 -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=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=TtzpspR8; 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 S1753445AbeDSOoP (ORCPT + 99 others); Thu, 19 Apr 2018 10:44:15 -0400 Received: from mail-yb0-f195.google.com ([209.85.213.195]:45743 "EHLO mail-yb0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752086AbeDSOoL (ORCPT ); Thu, 19 Apr 2018 10:44:11 -0400 Received: by mail-yb0-f195.google.com with SMTP id r13-v6so777540ybm.12 for ; Thu, 19 Apr 2018 07:44:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=szeredi.hu; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=bNgT0WVV5qAYrXgvFAJMy+2+pX+svM9QtuuCPRVt4Rk=; b=TtzpspR8+pusL7CxwAWo66BnQMVYtNA1iU/MstvWCid7bcfpFO8u7+M1Xauc9Nn/Yj g+c6hDd6KTuj82Z07IY74r7/IMgCCFH//4ojfI55dJCW11wlI8Uscmo0c+EQdOIy2K1u smkiTjD0uNnWavKWNBHcb8r8WDB/hCc7RgHF8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=bNgT0WVV5qAYrXgvFAJMy+2+pX+svM9QtuuCPRVt4Rk=; b=qnNNUFeFoTYJS6SUxtfd+Tr+ssmHA7WD6JdZBfdsZ6iK8wzb1CjeHuKDKj1HM3sFIY wlan4Y6q08VQHIsSL3u6uDyFmIjUG5SjjXxX+UgYHdbVTN+EvV9gJIUtrL1gASAfQCPh E6Ou74pfWkRNESO4imbqKP8E/0LfQTlR0danrTsrofUyznHZ5Lsabj3LIEFwDdlcmQDE a+vMzlXIR+sEZWgQZh5VfonhmPl1sPpSXfnrQLh7ohKblyMaVV2hbFNCsn9tvT3dQIGJ 0phHrpvoKprqje08ojZeiYXWr9Np+X1+wQDc3FslE5JuMPbHTVlr1UA7evDEjwm4j932 KH6w== X-Gm-Message-State: ALQs6tCeEZN6QeQZXGEfvhMreWuT53YjOhRU99IsiBBh5MEwvasB4/S0 VkgYc7uT7cPlNsQbM5tAVKUlfnoVz+k= X-Received: by 2002:a25:a1a4:: with SMTP id a33-v6mr3998274ybi.10.1524149050178; Thu, 19 Apr 2018 07:44:10 -0700 (PDT) Received: from mail-yw0-f182.google.com (mail-yw0-f182.google.com. [209.85.161.182]) by smtp.gmail.com with ESMTPSA id l14-v6sm1561386ywh.24.2018.04.19.07.44.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Apr 2018 07:44:09 -0700 (PDT) Received: by mail-yw0-f182.google.com with SMTP id g9-v6so1724985ywb.12 for ; Thu, 19 Apr 2018 07:44:09 -0700 (PDT) X-Received: by 2002:a81:5605:: with SMTP id k5-v6mr3678388ywb.345.1524149048872; Thu, 19 Apr 2018 07:44:08 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:ca52:0:0:0:0:0 with HTTP; Thu, 19 Apr 2018 07:44:08 -0700 (PDT) In-Reply-To: References: <20180418174014.1592871-1-songliubraving@fb.com> From: Miklos Szeredi Date: Thu, 19 Apr 2018 16:44:08 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] tracing: fix bad use of igrab in trace_uprobe.c To: Song Liu Cc: LKML , kernel-team , Steven Rostedt , Ingo Molnar , Howard McLauchlan , Josef Bacik , Srikar Dronamraju Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 19, 2018 at 10:58 AM, Miklos Szeredi wrote: > On Wed, Apr 18, 2018 at 7:40 PM, Song Liu wrote: >> As Miklos reported and suggested: >> >> This pattern repeats two times in trace_uprobe.c and in >> kernel/events/core.c as well: >> >> ret = kern_path(filename, LOOKUP_FOLLOW, &path); >> if (ret) >> goto fail_address_parse; >> >> inode = igrab(d_inode(path.dentry)); >> path_put(&path); >> >> And it's wrong. You can only hold a reference to the inode if you >> have an active ref to the superblock as well (which is normally >> through path.mnt) or holding s_umount. >> >> This way unmounting the containing filesystem while the tracepoint is >> active will give you the "VFS: Busy inodes after unmount..." message >> and a crash when the inode is finally put. >> >> Solution: store path instead of inode. >> >> This patch fixes two instances in trace_uprobe.c. >> >> Fixes: f3f096cfedf8 ("tracing: Provide trace events interface for uprobes") >> Fixes: 33ea4b24277b ("perf/core: Implement the 'perf_uprobe' PMU") >> Cc: Steven Rostedt >> Cc: Ingo Molnar >> Cc: Howard McLauchlan >> Cc: Josef Bacik >> Cc: Srikar Dronamraju >> Reported-by: Miklos Szeredi >> Signed-off-by: Song Liu >> --- >> kernel/trace/trace_uprobe.c | 49 +++++++++++++++------------------------------ >> 1 file changed, 16 insertions(+), 33 deletions(-) >> >> diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c >> index 34fd0e0..d9ee522c 100644 >> --- a/kernel/trace/trace_uprobe.c >> +++ b/kernel/trace/trace_uprobe.c >> @@ -55,7 +55,7 @@ struct trace_uprobe { >> struct list_head list; >> struct trace_uprobe_filter filter; >> struct uprobe_consumer consumer; >> - struct inode *inode; >> + struct path path; >> char *filename; >> unsigned long offset; >> unsigned long nhit; >> @@ -289,7 +289,7 @@ static void free_trace_uprobe(struct trace_uprobe *tu) >> for (i = 0; i < tu->tp.nr_args; i++) >> traceprobe_free_probe_arg(&tu->tp.args[i]); >> >> - iput(tu->inode); >> + path_put(&tu->path); >> kfree(tu->tp.call.class->system); >> kfree(tu->tp.call.name); >> kfree(tu->filename); >> @@ -363,7 +363,6 @@ static int register_trace_uprobe(struct trace_uprobe *tu) >> static int create_trace_uprobe(int argc, char **argv) >> { >> struct trace_uprobe *tu; >> - struct inode *inode; >> char *arg, *event, *group, *filename; >> char buf[MAX_EVENT_NAME_LEN]; >> struct path path; >> @@ -371,7 +370,6 @@ static int create_trace_uprobe(int argc, char **argv) >> bool is_delete, is_return; >> int i, ret; >> >> - inode = NULL; >> ret = 0; >> is_delete = false; >> is_return = false; >> @@ -437,24 +435,14 @@ static int create_trace_uprobe(int argc, char **argv) >> } >> /* Find the last occurrence, in case the path contains ':' too. */ >> arg = strrchr(argv[1], ':'); >> - if (!arg) { >> - ret = -EINVAL; >> - goto fail_address_parse; >> - } >> + if (!arg) >> + return -EINVAL; >> >> *arg++ = '\0'; >> filename = argv[1]; >> ret = kern_path(filename, LOOKUP_FOLLOW, &path); >> if (ret) >> - goto fail_address_parse; >> - >> - inode = igrab(d_real_inode(path.dentry)); Also, where has the d_real_inode() gone? Looks like we need tu->inode back, since the return value of d_real_inode() may change over time. I'd do the "tu->inode = d_real_inode(tu->path.dentry)" just before first use (i.e. when enabling the tracepoint). Thanks, Miklos >> - path_put(&path); >> - >> - if (!inode || !S_ISREG(inode->i_mode)) { > > Where has the S_ISREG check gone? > >> - ret = -EINVAL; >> - goto fail_address_parse; >> - } >> + return ret; >> >> ret = kstrtoul(arg, 0, &offset); >> if (ret) >> @@ -490,7 +478,7 @@ static int create_trace_uprobe(int argc, char **argv) >> goto fail_address_parse; >> } >> tu->offset = offset; >> - tu->inode = inode; >> + tu->path = path; >> tu->filename = kstrdup(filename, GFP_KERNEL); >> >> if (!tu->filename) { >> @@ -558,7 +546,7 @@ static int create_trace_uprobe(int argc, char **argv) >> return ret; >> >> fail_address_parse: >> - iput(inode); >> + path_put(&path); >> >> pr_info("Failed to parse address or file.\n"); >> >> @@ -922,7 +910,8 @@ probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file, >> goto err_flags; >> >> tu->consumer.filter = filter; >> - ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); >> + ret = uprobe_register(d_inode(tu->path.dentry), tu->offset, >> + &tu->consumer); >> if (ret) >> goto err_buffer; >> >> @@ -966,7 +955,7 @@ probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file) >> >> WARN_ON(!uprobe_filter_is_empty(&tu->filter)); >> >> - uprobe_unregister(tu->inode, tu->offset, &tu->consumer); >> + uprobe_unregister(d_inode(tu->path.dentry), tu->offset, &tu->consumer); >> tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE; >> >> uprobe_buffer_disable(); >> @@ -1041,7 +1030,8 @@ static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) >> write_unlock(&tu->filter.rwlock); >> >> if (!done) >> - return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); >> + return uprobe_apply(d_inode(tu->path.dentry), tu->offset, >> + &tu->consumer, false); >> >> return 0; >> } >> @@ -1073,7 +1063,8 @@ static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) >> >> err = 0; >> if (!done) { >> - err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); >> + err = uprobe_apply(d_inode(tu->path.dentry), >> + tu->offset, &tu->consumer, true); >> if (err) >> uprobe_perf_close(tu, event); >> } >> @@ -1337,7 +1328,6 @@ struct trace_event_call * >> create_local_trace_uprobe(char *name, unsigned long offs, bool is_return) >> { >> struct trace_uprobe *tu; >> - struct inode *inode; >> struct path path; >> int ret; >> >> @@ -1345,14 +1335,6 @@ create_local_trace_uprobe(char *name, unsigned long offs, bool is_return) >> if (ret) >> return ERR_PTR(ret); >> >> - inode = igrab(d_inode(path.dentry)); >> - path_put(&path); >> - >> - if (!inode || !S_ISREG(inode->i_mode)) { > > And here, again. > > Otherwise ACK. > > Also please create a separate patch that removes igrab/iput calls from > kernel/events/uprobes.c and adds a comment to the effect that the > caller is required to keep the inode (and the containing mount) > referenced. > > Thanks, > Miklos