Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2992341pxb; Sun, 29 Aug 2021 09:40:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyo69JwiJb2J6Oz9eN0tDITJEZaHxYPwtYfuM5UUWRGYQpnLYYP3OSiGXokn5iLBB5JyOM5 X-Received: by 2002:a17:907:1b06:: with SMTP id mp6mr20979685ejc.188.1630255246977; Sun, 29 Aug 2021 09:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630255246; cv=none; d=google.com; s=arc-20160816; b=OT1JLUEnMOGUBoBFJnXL1dNDQrT22T2xPeKeoxJ/hqFg6cNYwAX1y/lFQtrHqKF1l6 u2LRoMYaPkT0TZ/vR11AybmXoFiMONPetqf0KDLnapdEkbGR46pn8z0a+t+BUdt8Qdoe M40SrgqiglllbpRrrFyW6eipJAzDojeXLP38sHwE2XuHr5ikVnpqu/t1vi4NLWZFCcIF U3nMKRTVKhnNfnm1TdMyVIFB/eMybbI/s7S93B7yOBGbhwYAp64CpwTsa3A1p19lCDHj bQld9k5MgPQ8F9JntxFoogdVs+OlTdFzna+V+/kK14DreyuhdUjMOQqCjxqYPveNZe5Y IBiQ== 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:date:cc:to:from:subject :message-id; bh=RlId/HSWZvWkUMlJzKfJnvv7pFAIuvzQYefxqwQBI3s=; b=BXwXCSRLXbgYpzPMbLjs5yh1YJuPPIYMUm+25jaJDT6Bg3ffSYlo0Mv//Ei6ScF+we IDqtX/vkth6DvyZcTH2ooath+/irSxnusCyD47QisTDqMS/fCq4to9L+ywXGDI6zyrli qOU+FswZxYDzdrGJ5RbHBVnjCkLTXi1LLHgSTLC0Ze4RTFq3JWWLXCgCYOtHwAzyEAK9 /8kw1p12+tfGs3OtnKzlArkLAGxYl5GTar5WLirzHvllp6PxHT24fcOnZPy8DJDiV5rp wzF8bs8M25R02ef9RZlzfcNQ/5XXGpUcy0aVgz1iJcQk8ac7tkiCKyoCiqV0j0i2Xcap DjDw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m26si2247029ejn.618.2021.08.29.09.40.16; Sun, 29 Aug 2021 09:40:46 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235680AbhH2Qje (ORCPT + 99 others); Sun, 29 Aug 2021 12:39:34 -0400 Received: from smtprelay0107.hostedemail.com ([216.40.44.107]:58348 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235595AbhH2Qjd (ORCPT ); Sun, 29 Aug 2021 12:39:33 -0400 Received: from omf19.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id 2902D180286D2; Sun, 29 Aug 2021 16:38:40 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf19.hostedemail.com (Postfix) with ESMTPA id 8230820D751; Sun, 29 Aug 2021 16:38:38 +0000 (UTC) Message-ID: <7b6dc45194f28db52740c2a604550f6879dafe36.camel@perches.com> Subject: Re: [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy From: Joe Perches To: Len Baker , Borislav Petkov , Mauro Carvalho Chehab , Tony Luck , James Morse , Robert Richter Cc: David Laight , Kees Cook , linux-hardening@vger.kernel.org, linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Date: Sun, 29 Aug 2021 09:38:37 -0700 In-Reply-To: <20210829161547.6069-1-len.baker@gmx.com> References: <20210829161547.6069-1-len.baker@gmx.com> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.40.0-1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.10 X-Stat-Signature: kjeub665hd3eemhd18azphncp68gyjyo X-Rspamd-Server: rspamout02 X-Rspamd-Queue-Id: 8230820D751 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1+6+/+VJDwyfpXsLyBJWe/JoPn23k2/juA= X-HE-Tag: 1630255118-696944 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 2021-08-29 at 18:15 +0200, 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() [1]. > > However, to simplify and clarify the code, to concatenate labels use > the scnprintf() function. This way it is not necessary to check the > return value of strscpy (-E2BIG if the parameter count is 0 or the src > was truncated) since the scnprintf returns always the number of chars > written into the buffer. This function returns always a nul-terminated > string even if it needs to be truncated. [] > diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c [] > @@ -1113,12 +1116,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 : ""; > + > + n = scnprintf(p, len, "%s%s", or, dimm->label); > + len -= n; > + p += n; A very common and intelligible mechanism for this is: const char *prefix = ""; int n = 0; ... n += scnprintf(e->label + n, sizeof(e->label) - n, "%s%s", prefix, dimm->label); prefix = " or ";