Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7582590ybi; Mon, 22 Jul 2019 16:11:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqxHwSJhs6WZ9Bq2qze6fwK9k9Y3vWGjIB3h2kkTBlcQzniNE5+Fs+ZAhBhE2H2t6m8MdWDX X-Received: by 2002:a17:902:20c8:: with SMTP id v8mr77953238plg.284.1563837085183; Mon, 22 Jul 2019 16:11:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563837085; cv=none; d=google.com; s=arc-20160816; b=gKhq32SBwrtv0fGO/mA5mOxawZtp4fNrZKUTmMFAmnO6983Q+gN3jd/VOSCXYm8h7K sv4QPoWhj7XJvotJkgOElLVrVkZ1Qablyk7XXHDuiY6E0tU7W9sK/e5lJeIRXcI7gI44 Z6UpGgyk5lLTpGEHggFQY3dHcdW3lLfB4yLag00oETD/oOd3+4YF2EG+WcQf1GnYrdwH 3q31XhvCcekZkrX86afhMF8Ha6W8+B7+VY9NAqz6bv7VM1RCeO6YfsUKgl+lCvaPNryQ 8cNESUqKYow98PItj9G/1QCh6pP2wVvqmtTG8gBU2NWN2VWU69fzUuQGt9qwEb8aMhG+ 3t7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=Z+pRJf/dIF3L3ezM477tkQujdJQVLXwifz9xLjPEaL8=; b=cD7tlmRWG4YO/QvUo+k22ghR2IpnlXgxXCv6CEKJ9mSR9sh+NS/zDbv2I+cpqwPnrg crhtAvfjyD42hclZwUjPIJFAJu/qLJEgPtJdu/nUGE+rBMvpqtqym7QjFu7gGtK9V/Y7 5galfPulfnLVLnUy3IBxkaIYWMlSj9IbITQ08/S3W9tELN3+B1Wm8JDH1arkTgRuh8vc JCvPwvZkSq/wmqvxRtaWkeps/BsduNmz1XjrW1F5i2REh6AP10XTZcsLqTnjT011bro1 3gFjyIZ/AjMw3XjvE69A3iAbt3fzXphBHUPPGsWZjLXEIPOtue4AILnWYISZqXjMNAtH dh8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n129si11287932pgn.254.2019.07.22.16.11.09; Mon, 22 Jul 2019 16:11:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730962AbfGVR6T (ORCPT + 99 others); Mon, 22 Jul 2019 13:58:19 -0400 Received: from smtprelay0011.hostedemail.com ([216.40.44.11]:56978 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726272AbfGVR6T (ORCPT ); Mon, 22 Jul 2019 13:58:19 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay02.hostedemail.com (Postfix) with ESMTP id 57E742C88; Mon, 22 Jul 2019 17:58:18 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::,RULES_HIT:41:355:379:599:982:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1542:1593:1594:1605:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:2828:2895:3138:3139:3140:3141:3142:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:6119:6120:7875:8603:10004:10400:10471:10848:11026:11232:11473:11658:11914:12043:12297:12555:12679:12740:12760:12895:13141:13230:13255:13439:14096:14097:14181:14659:14721:21080:21324:21433:21451:21627:21740:30012:30054:30090:30091,0,RBL:23.242.196.136:@perches.com:.lbl8.mailshell.net-62.8.0.180 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: leg66_2b31318223726 X-Filterd-Recvd-Size: 4181 Received: from XPS-9350.home (cpe-23-242-196-136.socal.res.rr.com [23.242.196.136]) (Authenticated sender: joe@perches.com) by omf19.hostedemail.com (Postfix) with ESMTPA; Mon, 22 Jul 2019 17:58:16 +0000 (UTC) Message-ID: <2c959c56c23d0052e5c35ecfa2f6051b17fb2798.camel@perches.com> Subject: Re: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().) From: Joe Perches To: Kees Cook Cc: Nitin Gote , akpm@linux-foundation.org, corbet@lwn.net, apw@canonical.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Rasmus Villemoes Date: Mon, 22 Jul 2019 10:58:15 -0700 In-Reply-To: References: <1562219683-15474-1-git-send-email-nitin.r.gote@intel.com> <201907221031.8B87A9DE@keescook> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.30.5-0ubuntu0.18.10.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2019-07-22 at 10:43 -0700, Joe Perches wrote: > On Mon, 2019-07-22 at 10:33 -0700, Kees Cook wrote: > > On Thu, Jul 04, 2019 at 05:15:57PM -0700, Joe Perches wrote: > > > On Thu, 2019-07-04 at 13:46 -0700, Joe Perches wrote: > > > > On Thu, 2019-07-04 at 11:24 +0530, Nitin Gote wrote: > > > > > Added warnings in checkpatch.pl script to : > > > > > > > > > > 1. Deprecate strcpy() in favor of strscpy(). > > > > > 2. Deprecate strlcpy() in favor of strscpy(). > > > > > 3. Deprecate strncpy() in favor of strscpy() or strscpy_pad(). > > > > > > > > > > Updated strncpy() section in Documentation/process/deprecated.rst > > > > > to cover strscpy_pad() case. > > > > > > [] > > > > > > I sent a patch series for some strscpy/strlcpy misuses. > > > > > > How about adding a macro helper to avoid the misuses like: > > > --- > > > include/linux/string.h | 16 ++++++++++++++++ > > > 1 file changed, 16 insertions(+) > > > > > > diff --git a/include/linux/string.h b/include/linux/string.h > > > index 4deb11f7976b..ef01bd6f19df 100644 > > > --- a/include/linux/string.h > > > +++ b/include/linux/string.h > > > @@ -35,6 +35,22 @@ ssize_t strscpy(char *, const char *, size_t); > > > /* Wraps calls to strscpy()/memset(), no arch specific code required */ > > > ssize_t strscpy_pad(char *dest, const char *src, size_t count); > > > > > > +#define stracpy(to, from) \ > > > +({ \ > > > + size_t size = ARRAY_SIZE(to); \ > > > + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ > > > + \ > > > + strscpy(to, from, size); \ > > > +}) > > > + > > > +#define stracpy_pad(to, from) \ > > > +({ \ > > > + size_t size = ARRAY_SIZE(to); \ > > > + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ > > > + \ > > > + strscpy_pad(to, from, size); \ > > > +}) > > > + > > > #ifndef __HAVE_ARCH_STRCAT > > > extern char * strcat(char *, const char *); > > > #endif > > > > This seems like a reasonable addition, yes. I think Coccinelle might > > actually be able to find all the existing strscpy(dst, src, sizeof(dst)) > > cases to jump-start this conversion. > > I did that. It works. It's a lot of conversions. > > $ cat str.cpy.cocci > @@ > expression e1; > expression e2; > @@ > > - strscpy(e1, e2, sizeof(e1)) > + stracpy(e1, e2) > > @@ > expression e1; > expression e2; > @@ > > - strlcpy(e1, e2, sizeof(e1)) > + stracpy(e1, e2) > > > Devil's advocate: this adds yet more string handling functions... will > > this cause even more confusion? > > Documentation is good. > Actual in-kernel use and examples better. btw: I just ran this again and it produces: $ spatch --in-place -sp-file str.cpy.cocci . $ git checkout tools/ $ git diff --shortstat 958 files changed, 2179 insertions(+), 2655 deletions(-)