From patchwork Wed Jun 21 16:29:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 111185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp4501362vqr; Wed, 21 Jun 2023 09:47:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5M6KVqmzi461mytrxjxrsNbtPT6yOENC16yGolrk+mXILISPuddiLGkVMNHyIaR0QUt+fO X-Received: by 2002:a17:902:cec7:b0:1b3:db5d:e8a0 with SMTP id d7-20020a170902cec700b001b3db5de8a0mr22450693plg.28.1687366031573; Wed, 21 Jun 2023 09:47:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687366031; cv=none; d=google.com; s=arc-20160816; b=uyz7iG+NJ1qoBUAKTt3ld4BbM4QXIdaE7Escjc1duMofaTnDZzenWxxfKxPepI5sey mAOB/Ng0eqJZ+gVVFvKHf4q4BEjwzPEyKVkxHX/nBcuSLpLavVmIgfp0eC1DAMXxXKM/ ea2snh7m4eSLlJT5AdJFXOc8zYX9S2AMLW3tFZ4Xcaug8bQSw0ZDhwYwF+WVxu/cidr2 S1tbS2ve5cjfImXj8JRL/eB4JyYBaPThHey4MTF2eLcU74K+B79NiuXCI7JZFvWoWH3/ 5CmN6yfMAkrJYY+RFljlh6OcvZ7H1U2wTUo/ID3vhtmQiYqRcQXIw1xamxXdYRkzCDI5 ZpWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id; bh=WUy6SgmhyIIIBaa8mcWqsa5RZ5RWY21tK6imfJsSqbI=; b=J14P1WGrw/J4Y4GqHW1c7O4nerPxHlVQVUpSRfbk6aScQJlcLL4YhkDa1NRD48tSaF 544hUzOgsK0octYa9I3+bZZeLJ7Bfeeysc6742r+qBAGbronSQ7kKHSJegRw0Ef9VLfq 9jURAS/CnDiycuUz2BqZp2OjHhqx9PzmtvZIOSzwhO8Q+RayH9iboAhhj8FR3F1htY38 1A7+fexMvs4mzLPq3Izhi/GLjLZY8AIYByzyeRwcYmlmCXwo/btSOJWoB/1RzrbWONbJ PtGXiglQFk1eexM7GzNGftue/GJWJuqSbNJZGi4S9QB7JokKxDJ59bb3lcHR+tW2ttq+ vg5Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x10-20020a1709027c0a00b001b5430d9397si4333702pll.39.2023.06.21.09.46.58; Wed, 21 Jun 2023 09:47:11 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232386AbjFUQar (ORCPT + 99 others); Wed, 21 Jun 2023 12:30:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232470AbjFUQ35 (ORCPT ); Wed, 21 Jun 2023 12:29:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D74B1733 for ; Wed, 21 Jun 2023 09:29:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6AA536162A for ; Wed, 21 Jun 2023 16:29:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1F12C433CD; Wed, 21 Jun 2023 16:29:47 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1qC0iI-001yoz-2x; Wed, 21 Jun 2023 12:29:46 -0400 Message-ID: <20230621162946.729796944@goodmis.org> User-Agent: quilt/0.66 Date: Wed, 21 Jun 2023 12:29:35 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Azeem Shaikh , Kees Cook Subject: [for-next][PATCH 12/13] tracing/boot: Replace strlcpy with strscpy References: <20230621162923.953123395@goodmis.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769331523741432650?= X-GMAIL-MSGID: =?utf-8?q?1769331523741432650?= From: Azeem Shaikh strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy(). Direct replacement is safe here since return value of -E2BIG is used to check for truncation instead of sizeof(dest). [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [2] https://github.com/KSPP/linux/issues/89 Link: https://lore.kernel.org/linux-trace-kernel/20230613004125.3539934-1-azeemshaikh38@gmail.com Cc: Masami Hiramatsu Signed-off-by: Azeem Shaikh Reviewed-by: Kees Cook Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace_boot.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c index 778200dd8ede..5fe525f1b8cc 100644 --- a/kernel/trace/trace_boot.c +++ b/kernel/trace/trace_boot.c @@ -31,7 +31,7 @@ trace_boot_set_instance_options(struct trace_array *tr, struct xbc_node *node) /* Common ftrace options */ xbc_node_for_each_array_value(node, "options", anode, p) { - if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) { + if (strscpy(buf, p, ARRAY_SIZE(buf)) == -E2BIG) { pr_err("String is too long: %s\n", p); continue; } @@ -87,7 +87,7 @@ trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node) const char *p; xbc_node_for_each_array_value(node, "events", anode, p) { - if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) { + if (strscpy(buf, p, ARRAY_SIZE(buf)) == -E2BIG) { pr_err("String is too long: %s\n", p); continue; } @@ -486,7 +486,7 @@ trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode, p = xbc_node_find_value(enode, "filter", NULL); if (p && *p != '\0') { - if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) + if (strscpy(buf, p, ARRAY_SIZE(buf)) == -E2BIG) pr_err("filter string is too long: %s\n", p); else if (apply_event_filter(file, buf) < 0) pr_err("Failed to apply filter: %s\n", buf); @@ -494,7 +494,7 @@ trace_boot_init_one_event(struct trace_array *tr, struct xbc_node *gnode, if (IS_ENABLED(CONFIG_HIST_TRIGGERS)) { xbc_node_for_each_array_value(enode, "actions", anode, p) { - if (strlcpy(buf, p, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) + if (strscpy(buf, p, ARRAY_SIZE(buf)) == -E2BIG) pr_err("action string is too long: %s\n", p); else if (trigger_process_regex(file, buf) < 0) pr_err("Failed to apply an action: %s\n", p);