From patchwork Wed Oct 19 08:24:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp327932wrs; Wed, 19 Oct 2022 06:30:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7mxnBXxFy2n+eMPYW1Ao8TlZF5Xp32JqSI5+lrsi8cV+ScXJ1SkGbBXF2cvLSIR/JwmvaZ X-Received: by 2002:a05:6a00:1491:b0:563:8844:3f31 with SMTP id v17-20020a056a00149100b0056388443f31mr8440030pfu.31.1666186201972; Wed, 19 Oct 2022 06:30:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666186201; cv=none; d=google.com; s=arc-20160816; b=wevgno8yf4G/v+TTzhcGfDvI5HkExyJonD3fMHgTSwlD59Spp2fgBs/Hb28q2cuADm iof9JSSil071A/QBZy2qhBgxujrA43dUCsZg2wEJDBZemcoDnyfsb+u2X5oEzYiZSKcH a7H8b8MLeP6AD4O69ZRWSAsfc9xTQaF10hFzBprGv+/KJmNCdq9grmyhNX6A3VsAOlZ1 7GSwVDnqOaw6wirUmzHWSysexMp+PhBlgQsG7PTDJO92RsoMlOsnXeGN42Xl3x/zE+S8 lLjcjXFzAhyJ8yvMCjW2kEbhJmaIWS8V/Q2eH4/mJjbZabwhgtsgjU7J8RhCbgwEFoFr HC2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r8jefCc5omYODzIdGSyaJkR+hbn0RbXEHSNFppjjWcE=; b=DpzUaPkVKXUgALkGpiwEVLISvb6hpz0bR9B07fIzyLkjE3sPo4mKlZvYDz/jj3SerW xoFOxPWNp9UH7b8Ta8b9kgCzFDsIropk6hBInJeSFGM5tE0vXp+VGYD0aSVRBVlxKk5C oLIqiH8QxY8yiq/fCiBjT9oPWpQkIwzw0diRNM46i2i5M8GE3SF3GdnfxPsFuCYUQhYl G7NUiR4mE7jtkV1XpLxjMJewBFp68ntSa+ADPQF9pO+5vgb/rbI5sxfwmbaQqZyM+Rwh ld9IuK1OIqgKJgi1EP02rhuynx2BWOONH+xw0ME2Fi3Z1f0L4+voctRUsqbnJmcErFz6 146w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="q3zdOc/o"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 136-20020a63028e000000b00438762a8c81si18885154pgc.780.2022.10.19.06.29.49; Wed, 19 Oct 2022 06:30:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="q3zdOc/o"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230248AbiJSNQe (ORCPT + 99 others); Wed, 19 Oct 2022 09:16:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232558AbiJSNPt (ORCPT ); Wed, 19 Oct 2022 09:15:49 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1E7F1DC814; Wed, 19 Oct 2022 06:01:30 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9C7E4B82303; Wed, 19 Oct 2022 08:45:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC648C433D6; Wed, 19 Oct 2022 08:45:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169119; bh=YDvdxWfV1FcnbZUuL8IYRGcSN1JzkaRyWuTjHwTihzY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q3zdOc/oeAMkiNXNuBRt8wu0nIentd5Y+EjLd7sjODSVaMWSHlydyr+WVcIZvc8Ft P9dHQFqDwGmE35++L9RJbVdG6+SnzdyTBv2fdpWprMdSU7UDNkwos4JAN6KTSC8e3t F5M20smpUfSof4cZWmaByfq+5xP27VZD7Csbgvcg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrew Morton , Tom Zanussi , "Masami Hiramatsu (Google)" , "Steven Rostedt (Google)" Subject: [PATCH 6.0 165/862] tracing: Fix reading strings from synthetic events Date: Wed, 19 Oct 2022 10:24:12 +0200 Message-Id: <20221019083257.263418277@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747122862782113829?= X-GMAIL-MSGID: =?utf-8?q?1747122862782113829?= From: Steven Rostedt (Google) commit 0934ae9977c27133449b6dd8c6213970e7eece38 upstream. The follow commands caused a crash: # cd /sys/kernel/tracing # echo 's:open char file[]' > dynamic_events # echo 'hist:keys=common_pid:file=filename:onchange($file).trace(open,$file)' > events/syscalls/sys_enter_openat/trigger' # echo 1 > events/synthetic/open/enable BOOM! The problem is that the synthetic event field "char file[]" will read the value given to it as a string without any memory checks to make sure the address is valid. The above example will pass in the user space address and the sythetic event code will happily call strlen() on it and then strscpy() where either one will cause an oops when accessing user space addresses. Use the helper functions from trace_kprobe and trace_eprobe that can read strings safely (and actually succeed when the address is from user space and the memory is mapped in). Now the above can show: packagekitd-1721 [000] ...2. 104.597170: open: file=/usr/lib/rpm/fileattrs/cmake.attr in:imjournal-978 [006] ...2. 104.599642: open: file=/var/lib/rsyslog/imjournal.state.tmp packagekitd-1721 [000] ...2. 104.626308: open: file=/usr/lib/rpm/fileattrs/debuginfo.attr Link: https://lkml.kernel.org/r/20221012104534.826549315@goodmis.org Cc: stable@vger.kernel.org Cc: Andrew Morton Cc: Tom Zanussi Acked-by: Masami Hiramatsu (Google) Reviewed-by: Tom Zanussi Fixes: bd82631d7ccdc ("tracing: Add support for dynamic strings to synthetic events") Signed-off-by: Steven Rostedt (Google) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace_events_synth.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) --- a/kernel/trace/trace_events_synth.c +++ b/kernel/trace/trace_events_synth.c @@ -17,6 +17,8 @@ /* for gfp flag names */ #include #include +#include "trace_probe.h" +#include "trace_probe_kernel.h" #include "trace_synth.h" @@ -409,6 +411,7 @@ static unsigned int trace_string(struct { unsigned int len = 0; char *str_field; + int ret; if (is_dynamic) { u32 data_offset; @@ -417,19 +420,27 @@ static unsigned int trace_string(struct data_offset += event->n_u64 * sizeof(u64); data_offset += data_size; - str_field = (char *)entry + data_offset; - - len = strlen(str_val) + 1; - strscpy(str_field, str_val, len); + len = kern_fetch_store_strlen((unsigned long)str_val); data_offset |= len << 16; *(u32 *)&entry->fields[*n_u64] = data_offset; + ret = kern_fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry); + (*n_u64)++; } else { str_field = (char *)&entry->fields[*n_u64]; - strscpy(str_field, str_val, STR_VAR_LEN_MAX); +#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE + if ((unsigned long)str_val < TASK_SIZE) + ret = strncpy_from_user_nofault(str_field, str_val, STR_VAR_LEN_MAX); + else +#endif + ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX); + + if (ret < 0) + strcpy(str_field, FAULT_STRING); + (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64); } @@ -462,7 +473,7 @@ static notrace void trace_event_raw_even val_idx = var_ref_idx[field_pos]; str_val = (char *)(long)var_ref_vals[val_idx]; - len = strlen(str_val) + 1; + len = kern_fetch_store_strlen((unsigned long)str_val); fields_size += len; }