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=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED 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 A7311C43381 for ; Thu, 14 Feb 2019 14:30:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 73B3E222D7 for ; Thu, 14 Feb 2019 14:30:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550154609; bh=+5TF4U7oiLkzoaX1mB953U+VsYFJXzDGOfx5giYCOkM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-ID:From; b=O5RZTegqQrzhvDf1owakdm4jR8g+sYAemJUv22LZTSIvoDsZ6+GVGJS8n/zgCxc0Z rESmJJdwaf41v/TPSfX1KiKfFzVlZJpFhlgTKhNqjvzTGQDGjtWB+ZlnIDo9OWvazt 6yyRtUXaLLtxcaF7vXGslG2rQvBm/8vScp03/IPE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393617AbfBNOaJ (ORCPT ); Thu, 14 Feb 2019 09:30:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:55426 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387633AbfBNOaI (ORCPT ); Thu, 14 Feb 2019 09:30:08 -0500 Received: from devbox (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (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 85EE22229F; Thu, 14 Feb 2019 14:30:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550154607; bh=+5TF4U7oiLkzoaX1mB953U+VsYFJXzDGOfx5giYCOkM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Ymru1oo5tMW1GaFeuMCVx47GLPsM8+c8OS9iT1wtzjxj/amC8cL3tYli41XWlhn6v DnmKPkV9FoTfuN0Zj3N095vHyrZ0b9rbpmoYicyXCr1ypZSTjJU3K5zNS8a/I6a2HP oNEg7/MxY1GxXliH/r2jWMfNjZ8qogtgCVkzY/Rw= Date: Thu, 14 Feb 2019 23:30:04 +0900 From: Masami Hiramatsu To: Andreas Ziegler Cc: stable@vger.kernel.org, andreas.ziegler@fau.de, mingo@redhat.com, rostedt@goodmis.org, Masami Hiramatsu Subject: Re: [PATCH stable-4.19 v2] tracing/uprobes: Fix output for multiple string arguments Message-Id: <20190214233004.a5ccf93e1b026c49d75be777@kernel.org> In-Reply-To: <155015426911.16940.12496678038569055827.stgit@devbox> References: <20190214223923.3b068eafda0114870580e48d@kernel.org> <155015426911.16940.12496678038569055827.stgit@devbox> 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: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Hi Andreas, I've update this patch to include "ret == maxlen" case fix as similar to the latest kernel code. Please review it. If it is OK, I'll update patches for other stable trees. Thank you, On Thu, 14 Feb 2019 23:24:29 +0900 Masami Hiramatsu wrote: > From: Andreas Ziegler > > commit 0722069a5374b904ec1a67f91249f90e1cfae259 upstream. > > When printing multiple uprobe arguments as strings the output for the > earlier arguments would also include all later string arguments. > > This is best explained in an example: > > Consider adding a uprobe to a function receiving two strings as > parameters which is at offset 0xa0 in strlib.so and we want to print > both parameters when the uprobe is hit (on x86_64): > > $ echo 'p:func /lib/strlib.so:0xa0 +0(%di):string +0(%si):string' > \ > /sys/kernel/debug/tracing/uprobe_events > > When the function is called as func("foo", "bar") and we hit the probe, > the trace file shows a line like the following: > > [...] func: (0x7f7e683706a0) arg1="foobar" arg2="bar" > > Note the extra "bar" printed as part of arg1. This behaviour stacks up > for additional string arguments. > > The strings are stored in a dynamically growing part of the uprobe > buffer by fetch_store_string() after copying them from userspace via > strncpy_from_user(). The return value of strncpy_from_user() is then > directly used as the required size for the string. However, this does > not take the terminating null byte into account as the documentation > for strncpy_from_user() cleary states that it "[...] returns the > length of the string (not including the trailing NUL)" even though the > null byte will be copied to the destination. > > Therefore, subsequent calls to fetch_store_string() will overwrite > the terminating null byte of the most recently fetched string with > the first character of the current string, leading to the > "accumulation" of strings in earlier arguments in the output. > > Fix this by incrementing the return value of strncpy_from_user() by > one if we did not hit the maximum buffer size. > > Link: http://lkml.kernel.org/r/20190116141629.5752-1-andreas.ziegler@fau.de > > Cc: Ingo Molnar > Cc: stable@vger.kernel.org > Fixes: 5baaa59ef09e ("tracing/probes: Implement 'memory' fetch method for uprobes") > Acked-by: Masami Hiramatsu > Signed-off-by: Andreas Ziegler > Signed-off-by: Steven Rostedt (VMware) > Signed-off-by: Masami Hiramatsu > --- > kernel/trace/trace_uprobe.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c > index e696667da29a..bf93ae152c22 100644 > --- a/kernel/trace/trace_uprobe.c > +++ b/kernel/trace/trace_uprobe.c > @@ -141,7 +141,14 @@ static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, > > ret = strncpy_from_user(dst, src, maxlen); > if (ret == maxlen) > - dst[--ret] = '\0'; > + dst[ret - 1] = '\0'; > + else if (ret >= 0) > + /* > + * Include the terminating null byte. In this case it > + * was copied by strncpy_from_user but not accounted > + * for in ret. > + */ > + ret++; > > if (ret < 0) { /* Failed to fetch string */ > ((u8 *)get_rloc_data(dest))[0] = '\0'; > -- Masami Hiramatsu