Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7519826ybi; Mon, 22 Jul 2019 14:56:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxn00WyNIOgliHMnHA6XwyQaUX3N51joecrGC2qlBAKIVDHTF224j+w3H+zYgSxJvyxhkY1 X-Received: by 2002:a65:464d:: with SMTP id k13mr66301024pgr.99.1563832593109; Mon, 22 Jul 2019 14:56:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563832593; cv=none; d=google.com; s=arc-20160816; b=ieHUl23a0cVRK0lqAH7bkh5fD8AU0hECuawzNXn0Rl1hJQtd4yjplsx8YFvir9Hy1t JwqVT47fbzHAyoPDh+fyZoCogme7iAFKx+m1OHC82ooKEC76Ct5EN8hVJVYVCvL7vk7h MKtaAwkn8cVU/NyLAgYx9s3G6jBdFDfZMwgVMUDZO/u38fud6Q1O1LdVEsAyDMRyX2qw 2MOnanlyZzCeDDnNQmKgxNnjpQjYfrliFWBs0N/Ikd6L7+p7g2Bh/XbxVDRAvrR0O6vQ qLkCh6dBo7HheE2G7UFKOwgGzIixrZIKjWFCrwP9vQ4vRABzJylQ02fMzPcdm0/3NsNM kDbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ax8I1mb7UwIFxfCmMJfuzkJQZA4WbZ7Ek1Ds9PvMQYU=; b=qwghwJMDF1WYT80SEgJunM/kdPAuFNq8+l9ONLX+vN129OENhQuoRCZYlPlTXo78eT 477ceUcx96XkxrOfEFl9tYFB4miQ081RbZ5yUsQM2RoPvHXIGf6y5lbvY5oI21gcjaQt NUC8iUg1Sov3LZqkFBd10h4dVFaD2wfH1T1XP6kZrcprfewjkruHpJOTnK6hge649D06 wGeHfLngisVTI4U21a4zG4vRwMA1Tz6ZS9PH5wIkxkzYiVwYjpl/hdiYSihM+L6rYfyz zUb9R7d8aa4nf+gJrRbVW+HX/m+6YX6h6KEQKfrEI107X22CXb4axI+rQEC0AOD35faR ZUpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZowcOjJM; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p14si8620069plo.119.2019.07.22.14.56.17; Mon, 22 Jul 2019 14:56:33 -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; dkim=pass header.i=@chromium.org header.s=google header.b=ZowcOjJM; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727391AbfGVRdm (ORCPT + 99 others); Mon, 22 Jul 2019 13:33:42 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:43712 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726590AbfGVRdl (ORCPT ); Mon, 22 Jul 2019 13:33:41 -0400 Received: by mail-pl1-f193.google.com with SMTP id 4so12498909pld.10 for ; Mon, 22 Jul 2019 10:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ax8I1mb7UwIFxfCmMJfuzkJQZA4WbZ7Ek1Ds9PvMQYU=; b=ZowcOjJMdxosv2QSzqf2eI08qVemvBx1XFUBmEfS4sU/PraJBWF671A5zObKnz4TfD zk/CQmr2CoQ/bq7hFwV/jk7C4Zl+sLHE/NGWoR8qwxKIwHpxQQ4RjoUJThjmRHvGEZgo CUZMefdwNypT9GNU3PI/XXImbp4GU9RlYE3TQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ax8I1mb7UwIFxfCmMJfuzkJQZA4WbZ7Ek1Ds9PvMQYU=; b=KVo+nXE+hp5FPOScIxMKTnRSVG73khxbi/wVvjfMEBRR9D4Lr+Mpv9PfKHRlUNKZ2I zxQ48c0hjrislQJ8ae153p92TUj3BTUSyYtJhmKB/Ar8PC/vH2zXUj4Vpc/lDfTBecPw T0Ljz75HDmV8f6st7NAXUQdvvo5Kq9HPPoBnjZ7Oo6bWZK3fDgeg66xzOLshG/C73BSU +e2Zq/fHHW+vumHA3eYElKKoe60VZh2IoRiDFQ39DUh3Kt6vUyljRkOvdfM8PbJ0e/yb 2zz8ldQt5c8ATV+jiTF/1t3sfPbQv220htnxXZUuCWNDrPC96g7xWSAy7zMeXfN+iIbP LXLw== X-Gm-Message-State: APjAAAVJBR/EZ8lrFhP+T8JMS+5SzuVQegD6nP11YnQ8+TD3tZOiNPII FSqjlvW0bFTyel4xaQ1s458Z2g== X-Received: by 2002:a17:902:7043:: with SMTP id h3mr48081374plt.10.1563816821309; Mon, 22 Jul 2019 10:33:41 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n98sm41097264pjc.26.2019.07.22.10.33.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 Jul 2019 10:33:40 -0700 (PDT) Date: Mon, 22 Jul 2019 10:33:39 -0700 From: Kees Cook To: Joe Perches 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 Subject: Re: [RFC PATCH] string.h: Add stracpy/stracpy_pad (was: Re: [PATCH] checkpatch: Added warnings in favor of strscpy().) Message-ID: <201907221031.8B87A9DE@keescook> References: <1562219683-15474-1-git-send-email-nitin.r.gote@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Devil's advocate: this adds yet more string handling functions... will this cause even more confusion? -- Kees Cook