Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8037238ybi; Tue, 23 Jul 2019 01:44:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjRChZ+UoUYCMw4ZFc1oqM0nV4YVQ5p9Udhe/zUusMNCwlIJ0jDri8mhQT2cXafJDLewY0 X-Received: by 2002:a17:902:a50d:: with SMTP id s13mr80177702plq.12.1563871463457; Tue, 23 Jul 2019 01:44:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563871463; cv=none; d=google.com; s=arc-20160816; b=bDOz/bv9TQIwIAJ5QAftv0weK62qqLtXiwAzQWRprUb8tRE8uMqDRgBy61MTVfYo7r egG8zjyMDutl4UxqlBxXr52X4ZaxWmPHg62vYA3JsXb5OyMb4DuPgzrBTVU9lZFZ4u09 AZlgG8FxzD7kD7F0lh8Z7PsqmYBAYrgkTlI5GCWxHz2BxYs9Le296e+6u1s9obw2eyW+ LPxb80l2+enzAGnqdgJL0r7bmYlxvF1sBpTkuAON7uZWe8XHMsjn18uvYC9xNERMP3RX VUgQJ01QRHyzI0jloJj6OK07nrS0lP5AttT7If5H3cgNZfbr+nLuf/0iXR9FDAf46fTw ebYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=BP3rQh0pTxqG1nk/zKUtfV8yqD2/oY5CYWMuTrwnKA8=; b=ZntueIix7EIY+NiLdJBmRxgf7l8k+fPcTbnvmoKAJsh3gmE4AQHdMymaqCTXLrXgWG 1PrGyVXd91osRFVhlRpaMEw75wwfgHeBZXBSPD8tnW6i4TBS+/2iIpXcdWbMhFZD8Fxi R/5PE2fbSbHM9ws27wkhIylZPV7vwY14Y8MFK1JbbdhhLgve7o/gBgfnjioXfb7huXNT ZQZMPtTZSZhmnpajIP3n+yl5+swdJ2DN3ErAr24QLvvYeAW5rwRuYYUaPm+GFHUoGI0M wJGbk3OGwaHbjuN2NB3+3mr3MQmzye+DF1mgsl0X25tzVdQniZnjldXOY8VYlJP2i2Wm s44Q== 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 i10si13526983pfr.130.2019.07.23.01.44.07; Tue, 23 Jul 2019 01:44:23 -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 S1732031AbfGWAiZ (ORCPT + 99 others); Mon, 22 Jul 2019 20:38:25 -0400 Received: from smtprelay0192.hostedemail.com ([216.40.44.192]:50395 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728573AbfGWAiY (ORCPT ); Mon, 22 Jul 2019 20:38:24 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay03.hostedemail.com (Postfix) with ESMTP id 33C9A83777ED; Tue, 23 Jul 2019 00:38:23 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::::,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1345:1359:1437:1534:1542:1711:1730:1747:1777:1792:2198:2199:2393:2553:2559:2562:2915:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3871:3874:4321:5007:6261:7875:8603:10004:10848:11026:11473:11658:11914:12043:12291:12296:12297:12555:12683:12895:13141:13161:13229:13230:14181:14394:14721:21080:21451:21627:30012:30034:30054:30069:30079:30090,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:17,LUA_SUMMARY:none X-HE-Tag: wheel67_2bd70514adf25 X-Filterd-Recvd-Size: 3459 Received: from joe-laptop.perches.com (cpe-23-242-196-136.socal.res.rr.com [23.242.196.136]) (Authenticated sender: joe@perches.com) by omf13.hostedemail.com (Postfix) with ESMTPA; Tue, 23 Jul 2019 00:38:21 +0000 (UTC) From: Joe Perches To: Linus Torvalds , linux-kernel@vger.kernel.org Cc: Jonathan Corbet , Stephen Kitt , Kees Cook , Nitin Gote , jannh@google.com, kernel-hardening@lists.openwall.com, Rasmus Villemoes , Andrew Morton Subject: [PATCH 1/2] string: Add stracpy and stracpy_pad mechanisms Date: Mon, 22 Jul 2019 17:38:15 -0700 Message-Id: <7ab8957eaf9b0931a59eff6e2bd8c5169f2f6c41.1563841972.git.joe@perches.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Several uses of strlcpy and strscpy have had defects because the last argument of each function is misused or typoed. Add macro mechanisms to avoid this defect. stracpy (copy a string to a string array) must have a string array as the first argument (to) and uses sizeof(to) as the size. These mechanisms verify that the to argument is an array of char or other compatible types like u8 or unsigned char. A BUILD_BUG is emitted when the type of to is not compatible. Signed-off-by: Joe Perches --- include/linux/string.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 4deb11f7976b..f80b0973f0e5 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -35,6 +35,47 @@ 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); +/** + * stracpy - Copy a C-string into an array of char + * @to: Where to copy the string, must be an array of char and not a pointer + * @from: String to copy, may be a pointer or const char array + * + * Helper for strscpy. + * Copies a maximum of sizeof(@to) bytes of @from with %NUL termination. + * + * Returns: + * * The number of characters copied (not including the trailing %NUL) + * * -E2BIG if @to is a zero size array. + */ +#define stracpy(to, from) \ +({ \ + size_t size = ARRAY_SIZE(to); \ + BUILD_BUG_ON(!__same_type(typeof(*to), char)); \ + \ + strscpy(to, from, size); \ +}) + +/** + * stracpy_pad - Copy a C-string into an array of char with %NUL padding + * @to: Where to copy the string, must be an array of char and not a pointer + * @from: String to copy, may be a pointer or const char array + * + * Helper for strscpy_pad. + * Copies a maximum of sizeof(@to) bytes of @from with %NUL termination + * and zero-pads the remaining size of @to + * + * Returns: + * * The number of characters copied (not including the trailing %NUL) + * * -E2BIG if @to is a zero size array. + */ +#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 -- 2.15.0