Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1746089pxy; Mon, 2 Aug 2021 09:14:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmUHQ7AqQqtkyu/F43cLENTqSTD1KD+mocx+ieugLXHHUJ66ehq0wgyH+hoedaELqSXBmv X-Received: by 2002:a05:6402:1c83:: with SMTP id cy3mr20721967edb.231.1627920894451; Mon, 02 Aug 2021 09:14:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627920894; cv=none; d=google.com; s=arc-20160816; b=AevB98b5/1Nggq18hFRjOBsPeUFrBu7v4lOLkLRR1JTkfoiNMqJI6ulByYovP4Zcji /MNdVxtdG/DVzfu1pvKsdboqRLOlxd9RhqCEO+PfgMXs407UHsP9XwvORViDfYtkpFKL kcCE0/icqP85MV3d7DqQ3JIHfXeSaSmkQX9kvM4zI+XwSCKZ+yHN4ZlzmQaB4NUc+B0m VMdeAoItbDJIPMtk4x8X2L3eLwhQ0xpMeAvyiUE5k5eIP3kpy9Ec0MW3ALJzaq8EZglJ 0T3jZaNXj2Kq/caBz+LrpBtdu4zP6p3/EfcTfr3jckjKcl653OXtOASxokeUf8xw6EL1 0BQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=N9riSg2fac+VoTKhkzLfWrBDE+0l5wS73gV2Grz1oJo=; b=sAJs2HRTRR0e862segnasByodapkr8zzl9kd+ErhE/D5KocJqYivDXANHySDd6gOF2 K9R99AfayToa2J3TC7o4qrB1/gF2QV6prDD8KK+muWr+vrcoR/mpl1D9iSyob4rTv2cT brS84iqbovNNYvQeSr+kLziWpkwHeVm/MmYjrNAc3T59HR4N9Ci1AkL2+V0JE7hB/5OL 4hrdNST5tGxbWjEH/dfWQEd11jKoRQX1lRGbWXZmjbWRFo6jIAOUMb/f213dRjV8YvtF ythJon/xcxbFNViBD46hQITxAaNSz4/BLX0Ee4tyzCOLo/s6EIR/p4WJ9sVdYi6w3ox/ DcwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=DZtWqGU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id m21si8639454edc.147.2021.08.02.09.14.31; Mon, 02 Aug 2021 09:14:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=DZtWqGU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S232240AbhHBQNU (ORCPT + 99 others); Mon, 2 Aug 2021 12:13:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229780AbhHBQNT (ORCPT ); Mon, 2 Aug 2021 12:13:19 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6757DC061760 for ; Mon, 2 Aug 2021 09:13:10 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id c16so20175501plh.7 for ; Mon, 02 Aug 2021 09:13:10 -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=N9riSg2fac+VoTKhkzLfWrBDE+0l5wS73gV2Grz1oJo=; b=DZtWqGU4zIj2q2wG2EX5uEXZEA37v3g9XZl91m6pb2VzC3ia/H4tD9JI9GNfFao4oJ ux+Po8ZNy3yzxrxivaS1i9Y9Iedzp6zoD6awc7A9RtIUdPF3ByWUG7ysQqyOixB+OFvl JTV2EJdKVt+T2yv3QT+4JLrXz/EOknP/1IMDg= 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=N9riSg2fac+VoTKhkzLfWrBDE+0l5wS73gV2Grz1oJo=; b=UcE+VocNRd1nJkwAx1BMflTIfLr/V1+aHW7So7WPpYLKt/ZLsNcFJcOs3AfIhMfKbX X5WW5/bnKUlpBOKYji2kRic5SiIu2pYdzFrBUIW/yU2R4k5ibk5Shi8aBTqVKqp/TSeb cSeP0D6Pbu+qE0ws6x72crJ3THnI1AtZdQfCHjTSY+XpWSaCpCB+FfVhxbfoB4f/zkO3 Pn+gMYh1yv1hwkoJ127ghEBbqj7NSsFGEnQjaTchaQlgM5WZWi71lUWsHMkC/pt11q6p DviQCICCzpml6qhTasP1g7dpL8u6HuCjzxVkWZHEkt1e8K3hX6FHjRxgApOjI3FgR2BS ngcA== X-Gm-Message-State: AOAM530Bupyjp/+mxO+naE74r2fjHZIf4kAwjIyBMu+7saaNYVE2yjF+ Eg2s4K3FO7j5FN+lELSCi+9FPQ== X-Received: by 2002:a17:90a:ab0b:: with SMTP id m11mr17527651pjq.221.1627920789933; Mon, 02 Aug 2021 09:13:09 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id v30sm10717478pgk.25.2021.08.02.09.13.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Aug 2021 09:13:09 -0700 (PDT) Date: Mon, 2 Aug 2021 09:13:08 -0700 From: Kees Cook To: Joe Perches Cc: "Russell King (Oracle)" , Len Baker , Dmitry Torokhov , Lee Jones , Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= , linux-hardening@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] drivers/input: Remove all strcpy() uses in favor of strscpy() Message-ID: <202108020912.3807510B4B@keescook> References: <20210801144316.12841-1-len.baker@gmx.com> <20210801145959.GI22278@shell.armlinux.org.uk> <922b0d99b6397adc44761abaed12c019dc0b9e88.camel@perches.com> <4962ac72a94bc5826960dab855b5e2f47a4d1b9a.camel@perches.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4962ac72a94bc5826960dab855b5e2f47a4d1b9a.camel@perches.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Aug 01, 2021 at 09:55:28AM -0700, Joe Perches wrote: > On Sun, 2021-08-01 at 09:39 -0700, Joe Perches wrote: > > On Sun, 2021-08-01 at 16:00 +0100, Russell King (Oracle) wrote: > > > On Sun, Aug 01, 2021 at 04:43:16PM +0200, Len Baker wrote: > > > > strcpy() performs no bounds checking on the destination buffer. This > > > > could result in linear overflows beyond the end of the buffer, leading > > > > to all kinds of misbehaviors. The safe replacement is strscpy(). > [] > > > So if the string doesn't fit, it's fine to silently truncate it? > > > > > > Rather than converting every single strcpy() in the kernel to > > > strscpy(), maybe there should be some consideration given to how the > > > issue of a strcpy() that overflows the buffer should be handled. > > > E.g. in the case of a known string such as the above, if it's longer > > > than the destination, should we find a way to make the compiler issue > > > a warning at compile time? > > (apologies for the earlier blank reply, sometimes I dislike my email client) > > stracpy could do that with a trivial addition like below: > > Old lkml references: > > https://lore.kernel.org/lkml/cover.1563889130.git.joe@perches.com/ > and > https://lore.kernel.org/lkml/56dc4de7e0db153cb10954ac251cb6c27c33da4a.camel@perches.com/ > > But Linus T wants a copy_string mechanism instead: > https://lore.kernel.org/lkml/CAHk-=wgqQKoAnhmhGE-2PBFt7oQs9LLAATKbYa573UO=DPBE0Q@mail.gmail.com/ > > /** > * stracpy - Copy a C-string into an array of char/u8/s8 or equivalent > * @dest: Where to copy the string, must be an array of char and not a pointer > * @src: String to copy, may be a pointer or const char array > * > * Helper for strscpy(). > * Copies a maximum of sizeof(@dest) bytes of @src with %NUL termination. > * > * A BUILD_BUG_ON is used for cases where @dest is not a char array or > * @src is a char array and is larger than @dest. > * > * Returns: > * * The number of characters copied (not including the trailing %NUL) > * * -E2BIG if @dest is a zero size array or @src was truncated. > */ > #define stracpy(dest, src) \ > ({ \ > BUILD_BUG_ON(!(__same_type(dest, char[]) || \ > __same_type(dest, unsigned char[]) || \ > __same_type(dest, signed char[]))); \ > BUILD_BUG_ON((__same_type(src, char[]) || \ > __same_type(src, unsigned char[]) || \ > __same_type(src, signed char[])) && \ > ARRAY_SIZE(src) > ARRAY_SIZE(dest)); \ > \ > strscpy(dest, src, ARRAY_SIZE(dest)); \ > }) I'm wondering, instead, if we could convert strcpy() into this instead of adding another API? I.e. convert all the places that warn (if this were strcpy), and then land the conversion. -- Kees Cook