From mboxrd@z Thu Jan 1 00:00:00 1970 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=-21.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 9EA19C432BE for ; Tue, 27 Jul 2021 12:38:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8652561A4F for ; Tue, 27 Jul 2021 12:38:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236545AbhG0Mix (ORCPT ); Tue, 27 Jul 2021 08:38:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:43700 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231971AbhG0Miw (ORCPT ); Tue, 27 Jul 2021 08:38:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7FAC360F51; Tue, 27 Jul 2021 12:38:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627389532; bh=AD5ouT9mvfsgZly5z4aHDSb6lt2hRORpdoAEEVzrNJI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=X9Az5WbEXmubiisPgGnKHk6YCaXrl7wTE5leoHtHKsDEDVi/+frp1wTvRDwTGZJYo mWldMMacFsfG72w/YqxNgd96jO02R7xUY1Vnzi2VPrRTRU4gkCs9mCirOmRStAA95j Z/CXrkZxXF+vsLwKDjVX9EkwbdFjr4kCI8raU7EjmARXsNjZtqHaRaYh+MhKNhVuVq 4eFRa0hbNMvlPjItvWCzsQnV7SqxpDizc1ORTkMx9C3ByuuUGCiGdAnOrpniivsQty LeWL/6ORtbfOHQrL5B8EtS74pPv2gZOUkR1C6M/QbjSrcCmT9uuUF92XD0joSVpY7R S4/eQ1mgWeR+w== Date: Tue, 27 Jul 2021 14:38:47 +0200 From: Robert Richter To: Len Baker Cc: Kees Cook , Borislav Petkov , Mauro Carvalho Chehab , Tony Luck , James Morse , linux-hardening@vger.kernel.org, linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] drivers/edac/edac_mc: Remove all strcpy() uses Message-ID: References: <20210725162954.9861-1-len.baker@gmx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210725162954.9861-1-len.baker@gmx.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 25.07.21 18:29:54, Len Baker wrote: > strcpy() performs no bounds checking on the destination buffer. This > could result in linear overflows beyond the end of the buffer, leading > to all kinds of misbehaviors. The safe replacement is strscpy(). > > However, to add labels is better to use the scnprintf to simplify the > arithmetic. > > This is a previous step in the path to remove the strcpy() function > entirely from the kernel. > > Signed-off-by: Len Baker > --- > This is a task of the KSPP [1] > > [1] https://github.com/KSPP/linux/issues/88 > > drivers/edac/edac_mc.c | 16 ++++++++-------- > 1 file changed, 8 insertions(+), 8 deletions(-) > > diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c > index f6d462d0be2d..1286364f0e48 100644 > --- a/drivers/edac/edac_mc.c > +++ b/drivers/edac/edac_mc.c > @@ -1027,6 +1027,7 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, > { > struct dimm_info *dimm; > char *p; > + size_t p_size = 0; I would rather use a 'left' variable which is initialized with sizeof(e->label) close to there p = e->label is. > int row = -1, chan = -1; > int pos[EDAC_MAX_LAYERS] = { top_layer, mid_layer, low_layer }; > int i, n_labels = 0; > @@ -1113,12 +1114,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, > p = e->label; > *p = '\0'; > } else { > - if (p != e->label) { > - strcpy(p, OTHER_LABEL); > - p += strlen(OTHER_LABEL); > - } > - strcpy(p, dimm->label); > - p += strlen(p); > + const char *or = (p != e->label) ? OTHER_LABEL : ""; > + > + p_size += scnprintf(p + p_size, > + sizeof(e->label) - p_size, > + "%s%s", or, dimm->label); My preference is to advance p here (and decrement 'left'). This is the pattern how p is used throughout the code. I also don't see a benefit of using scnprintf() here compared to the previous implementation. Thanks, -Robert > } > > /* > @@ -1140,9 +1140,9 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, > } > > if (any_memory) > - strcpy(e->label, "any memory"); > + strscpy(e->label, "any memory", sizeof(e->label)); > else if (!*e->label) > - strcpy(e->label, "unknown memory"); > + strscpy(e->label, "unknown memory", sizeof(e->label)); > > edac_inc_csrow(e, row, chan); > > -- > 2.25.1 >