From patchwork Fri Jul 5 00:15:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 11031953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 26230912 for ; Fri, 5 Jul 2019 00:16:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 14DEE2887A for ; Fri, 5 Jul 2019 00:16:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08D6E28900; Fri, 5 Jul 2019 00:16:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id B5D282887A for ; Fri, 5 Jul 2019 00:16:15 +0000 (UTC) Received: (qmail 12048 invoked by uid 550); 5 Jul 2019 00:16:13 -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: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 12016 invoked from network); 5 Jul 2019 00:16:12 -0000 X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: lace17_6e747616f8753 X-Filterd-Recvd-Size: 2600 Message-ID: Subject: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().) From: Joe Perches To: Nitin Gote , akpm@linux-foundation.org Cc: corbet@lwn.net, apw@canonical.com, keescook@chromium.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Thu, 04 Jul 2019 17:15:57 -0700 In-Reply-To: References: <1562219683-15474-1-git-send-email-nitin.r.gote@intel.com> User-Agent: Evolution 3.30.5-0ubuntu0.18.10.1 MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP 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