Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4361095pxv; Tue, 27 Jul 2021 05:41:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzG5tQ9ei4gdzyRLFsitLGoNtuxT/O4/Vxlz22W2S3flpPdkT8zMmyViAs+pBQ6wXy/PMKr X-Received: by 2002:a17:906:9251:: with SMTP id c17mr9442390ejx.516.1627389683130; Tue, 27 Jul 2021 05:41:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627389683; cv=none; d=google.com; s=arc-20160816; b=Kc+VMUyvEcOadxYkZjPWbSTF2W5FrTf+7zZXavM0gngwK4AVYIE/9WkfdR9vqOun6m +/QALJqbDc64LkzL545Q7YwvQqbh/1OmC5c8yyZdActVYXPd1xm3Dp2QEk2w8q8LosWt QP74gCrcalFHYksTDdlkH0NYxQvnGjsquGdRqoqXqLfqtizhagslgK31wCS5XuibG4qJ RafEUYCO3hnoUWxkQSWlFqGrLwIMmBJXnlepOS2uoCzpff2aPiuFeuujHSZQC8OKs2uV OQCGwl9fKuHpFEH6OWZcMELazaK5UO5su5JLu6i2FYQBYbSUUqM81gS3zig0yIlyT1hn jFfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=HIwO6ba640/V1Tl/N07G/t08sQt++szYrkfK1L84wSk=; b=u15o/wW9IyOkR0aKP7Ox5FY/Gt4p8zdQh1Usm06T0E/zzQZrQhYusvKh4Ibdsycvca y3yweaHMzt1FlSMIssN4mvtPvoJWpJgPGukkTc3jKfjMv9ZSssZfR1yGHEVitBZZfXXb +7f4OswGe65hez6bA3MHgIafSlEbFjSW9bM18CvZma+OI/uzFaD3KMW2WU0Xg1JEKMe9 SEYd4nOiihrEQ66AL5F2UHA/R3VTo1ogkzdQf6BD6+a6tIG53IoAygktg7UqhOGkjPkE TRV/D23Lkjl3oXswA6twKWN3EFFowSk9WR2ufT4ZgELEMk9AW9eco63faoc7iBhWGb+r 1Cug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=X9Az5WbE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce21si2739791ejc.23.2021.07.27.05.40.54; Tue, 27 Jul 2021 05:41:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=X9Az5WbE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236508AbhG0Miw (ORCPT + 99 others); Tue, 27 Jul 2021 08:38:52 -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 >