Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2708157ybi; Thu, 4 Jul 2019 17:42:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqw5mn6BYcnefGh3Gj4p72lDOQls253XEk/WnbZikG+S3jJHTxbe3/A5ZqpyillPoknzPlsw X-Received: by 2002:a17:90a:e397:: with SMTP id b23mr821841pjz.117.1562287359791; Thu, 04 Jul 2019 17:42:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562287359; cv=none; d=google.com; s=arc-20160816; b=ps3VS2zVhcm1leWevT/ImOBepYT0Y6/686FJSlZIGQhqPNGiwv7M/kNDLuvgtv0g/6 KchcUq0O5rVb3H3g5dtWrPczd36e68PDWPfG78PiCyRNFFO+U91WW7HFz4TNj4mGJZQ5 se3wHNTThjpyd8JLD2ZBnOZEE13GGajnmfXqdXTndXJALEfRubcLr39WB5EVQ8CHbjMy PH0PlcnZfM2AgXzw0mLdzLO6uplyTjGFMEFaoMmnCHHR3qUicAGP3wrxuprhmXre8SKt T+XW5UtqT4vDGm8fFtQ33vRaXx2kP0iCYDsJEMWcL10BE16h0KGnqw4A9LL3N8Dpa8Bw kH1Q== 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=tU06T5DaHVXJW1phJ7YQDw8v1avd4X6PLNKbZbV6ThQ=; b=wkpa56DLkUOUy0Moahz4Ulz9ISwSQI6uogn+ijUnzkExp/qxqbp2IRBf2C4Xl9DKja pi2+hKvqna49qpjEDGDI9tULHxbrs7AWKLYkARpBDwuU2xQanKslvVdXxp3uZBbjANTm 0nBQ6kq6m5vLNkhj6qglqEOcYsGNlh/sSWnvcDLKVCDvMvlLiXAO6qyJVuj8ScPX5XsM aepBeYpDWdXyJPHb4S4vB7fPIBwP/xjvZeLG8bjQ6n2+a+OJpvdALdF+C1I1ViST1SnR cnUc4P9cXN5bHf29817OOXr0k1q2XgG1lOL0dMtVxAob64Nz5T3IZOjfivihpOBa3MzJ Gi8g== 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 h11si7153884pfn.40.2019.07.04.17.42.12; Thu, 04 Jul 2019 17:42:39 -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 S1727320AbfGEAQC (ORCPT + 99 others); Thu, 4 Jul 2019 20:16:02 -0400 Received: from smtprelay0248.hostedemail.com ([216.40.44.248]:38772 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726404AbfGEAQC (ORCPT ); Thu, 4 Jul 2019 20:16:02 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay07.hostedemail.com (Postfix) with ESMTP id 6C2E7181D3377; Fri, 5 Jul 2019 00:16:00 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::,RULES_HIT:41:355:379:982:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:2828:2895:3138:3139:3140:3141:3142:3353:3622:3865:3866:3867:3870:4321:5007:6119:6120:8603:10004:10400:10471:10848:11026:11232:11473:11658:11914:12043:12297:12555:12740:12760:12895:13069:13141:13230:13255:13311:13357:13439:14181:14659:14721:21080:21451:21627:30012:30054: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: lace17_6e747616f8753 X-Filterd-Recvd-Size: 2600 Received: from XPS-9350.home (cpe-23-242-196-136.socal.res.rr.com [23.242.196.136]) (Authenticated sender: joe@perches.com) by omf20.hostedemail.com (Postfix) with ESMTPA; Fri, 5 Jul 2019 00:15:58 +0000 (UTC) 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> 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 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