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=-3.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no 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 47757C7618F for ; Mon, 22 Jul 2019 17:43:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2667921901 for ; Mon, 22 Jul 2019 17:43:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730031AbfGVRn4 (ORCPT ); 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-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. 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=-3.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no 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 23C7FC7618F for ; Mon, 22 Jul 2019 17:44:14 +0000 (UTC) Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.kernel.org (Postfix) with SMTP id BE7FD21903 for ; Mon, 22 Jul 2019 17:44:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BE7FD21903 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kernel-hardening-return-16527-kernel-hardening=archiver.kernel.org@lists.openwall.com Received: (qmail 16298 invoked by uid 550); 22 Jul 2019 17:44:07 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Received: (qmail 16257 invoked from network); 22 Jul 2019 17:44:06 -0000 X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: cart38_3ee779eddd660 X-Filterd-Recvd-Size: 3679 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 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.