Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7555885ybi; Mon, 22 Jul 2019 15:37:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxdRaB1tNYsNo8oYoHOoW2Zex77Vehv8zl4RZ1LsAnu6tF9XSZ0gWZ2arl20XKGnP5s5Y1M X-Received: by 2002:a17:902:b43:: with SMTP id 61mr79310660plq.322.1563835076653; Mon, 22 Jul 2019 15:37:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563835076; cv=none; d=google.com; s=arc-20160816; b=AKyXjewpjGp32RmvZzYTfSi2bZWVeGLAYVLj6KpZRr2ntbvo/qbK+eBZlkFAH3XBVb 9k2pn5FN5cfo0qdqyq/lTSLmJN2aqCU+hT1xWSbhRHLnhp/uYH3WY2ntDYk/EY7NeXiB hOAgLSOO3h3OyGEaqmNv6KYR2cIGR3qy1KRZKqIQoSQE4fBoWtZcPjtptbhp7MzRli2q dH1Pjg8Onu1fq8EowSGVig06X2qM0/Jvjif023O9w9/V1K90rqdv2emiQDaEDRtN223v Agb8xPpP01j6qebu/U6qiKMqspMV3dZ1rD0JT4XC2B1P/QDja2M9ThvSSmPUVBNbOGGE ArAw== 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=vwLeKQrsP8MixxdM04qDJlJyEK06pOeWZRtTwqanu6k=; b=dxHkoFHQTv8Lc5zZ1zDKoTtIBpsvjWC3eDT0LHIPdlZ/sGsQCmKNqXfB/yqIM7wSSJ jvBD7pqc0EFJDnNk1A10yUi4p0uw989IHO6Agtc+gR5S98jqPZxX+F9nW6GrSAqC0Ff5 nRnc8VbpGHbV/33FsGWaxHMWNnoulRTHD4opMYrVLmEeQYVGdQTnJb+HyBii7e+rKXVb C97TcVd8C2t5p33se+kmsaQf6/POF3NiWbYisTP8jaLVCvXNHnKNRIUh708PLKfBnHaH 5SLijZmH5dTALVLe4dwfBwcN2jYxRFWg0xo+9HqOXL+mBp7JC6icujrciTreMOgzUqoM IHhw== 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 q15si9594293pgt.150.2019.07.22.15.37.40; Mon, 22 Jul 2019 15:37:56 -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 S1729644AbfGVRn4 (ORCPT + 99 others); Mon, 22 Jul 2019 13:43:56 -0400 Received: from smtprelay0231.hostedemail.com ([216.40.44.231]:60993 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727510AbfGVRnz (ORCPT ); Mon, 22 Jul 2019 13:43:55 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay03.hostedemail.com (Postfix) with ESMTP id 25AE1837F24D; Mon, 22 Jul 2019 17:43:54 +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:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:2828:2895:3138:3139:3140:3141:3142:3355: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:12740:12760:12895:13019:13141:13200:13229: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:26,LUA_SUMMARY:none X-HE-Tag: cart38_3ee779eddd660 X-Filterd-Recvd-Size: 3679 Received: from XPS-9350.home (cpe-23-242-196-136.socal.res.rr.com [23.242.196.136]) (Authenticated sender: joe@perches.com) by omf14.hostedemail.com (Postfix) with ESMTPA; Mon, 22 Jul 2019 17:43:52 +0000 (UTC) Message-ID: 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:43:50 -0700 In-Reply-To: <201907221031.8B87A9DE@keescook> 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: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.