Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2518226pxt; Mon, 9 Aug 2021 02:23:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxb4BF9y4gTR0VXq8WP6b79/sHkCh5hwEIJ4CXPlE/lHhTK+N+JmAgb32rFW6iQ4PfR9E3o X-Received: by 2002:a5d:9906:: with SMTP id x6mr389315iol.23.1628500981573; Mon, 09 Aug 2021 02:23:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628500981; cv=none; d=google.com; s=arc-20160816; b=jomREFJgrT3ITcf1ilQxWr8wdDHVCMH1Qq8hwXNLyGBrLKmEzy5+PmQiuCz6VoqbBA XEtfizCHFeZoLkKVnukD0pq47xOr05/X+EvtRCXQAH7IhjIpztals8KJ7G6lDDDqYdfe z6q+TUTi6XSmgimbuasyc+9kIjEX2y7e7tld8mOyXJih+S10a6TZ2BOcH79zItNkewBD eno9QG8e8p71AqcX+FCkc87T7NSulautZ5UQ6XYTBr4rTB5PBSAGh5oQ7A5J2lhdZmqi L8zMcXVQRJAH65H1nFQaPfqs2S8ae5d638xVww5Xwx4FL4lMBv5xfVwwP65ij1OUKcWs Pydg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=m2f3ivJjOerWpmhkRhLKWfHrGz9CAxIatZA2NVXZM5s=; b=kM3CiVV1qN88i5W2D3cgDsQSQE8TljTxiiup6xF+dweOwhiiBe2CqW8t9O6IUm/Rk6 rED3EVGgiTPg0egpWXwGBy/dthf4saDKBJTw3IahlxBXQJI620RB6DvKDpZ3SNMSOWc0 e7IgUcGDLvshAfrAmsdBeHNqfCk1X9dFxlBr7JCX6BaZoE8v1kjNdlP50OsefTf/HwUB ryeckp7lr11loU524osMVCn9KY5o7IHClEKplsEWptcMJIqy/ZOHPDd8sQng+Ab2bebq A4k3MivGUCdB9GaeEwFF78qwbs3ipmI7IWkdGDs5le9LElW4TssL8LZS6zLc1GBG0G8G Ptsw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q7si14032550ilt.27.2021.08.09.02.22.48; Mon, 09 Aug 2021 02:23:01 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234353AbhHIJWY (ORCPT + 99 others); Mon, 9 Aug 2021 05:22:24 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:3605 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234257AbhHIJWX (ORCPT ); Mon, 9 Aug 2021 05:22:23 -0400 Received: from fraeml705-chm.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4GjrDt6yWfz6D9D7; Mon, 9 Aug 2021 17:21:34 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml705-chm.china.huawei.com (10.206.15.54) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Mon, 9 Aug 2021 11:22:01 +0200 Received: from localhost (10.52.123.61) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2176.2; Mon, 9 Aug 2021 10:22:00 +0100 Date: Mon, 9 Aug 2021 10:21:31 +0100 From: Jonathan Cameron To: Andy Shevchenko CC: Jonathan Cameron , Len Baker , Lars-Peter Clausen , Kees Cook , , linux-iio , Linux Kernel Mailing List Subject: Re: [PATCH v2] drivers/iio: Remove all strcpy() uses in favor of strscpy() Message-ID: <20210809102131.000021eb@Huawei.com> In-Reply-To: References: <20210807152225.9403-1-len.baker@gmx.com> <20210808172503.5187cd24@jic23-huawei> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; i686-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.52.123.61] X-ClientProxiedBy: lhreml702-chm.china.huawei.com (10.201.108.51) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 8 Aug 2021 22:00:34 +0300 Andy Shevchenko wrote: > On Sun, Aug 8, 2021 at 7:25 PM Jonathan Cameron wrote: > > > > On Sat, 7 Aug 2021 17:22:25 +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(). > > > > > > This patch is an effort to clean up the proliferation of str*() > > > functions in the kernel and a previous step in the path to remove > > > the strcpy function from the kernel entirely [1]. > > > > > > [1] https://github.com/KSPP/linux/issues/88 > > > > > > Signed-off-by: Len Baker > > Applied to the togreg branch of iio.git and pushed out as testing > > so 0-day can poke at it and see if we missed anything. > > Isn't it too early? Or am I missing something (see below)? > > ... > > > > /* use length + 2 for adding minus sign if needed */ > > > - str = devm_kzalloc(regmap_get_device(st->map), > > > - strlen(orient) + 2, GFP_KERNEL); > > > + n = strlen(orient) + 2; > > > + str = devm_kzalloc(regmap_get_device(st->map), n, > > > + GFP_KERNEL); > > > if (str == NULL) > > > return -ENOMEM; > > > if (strcmp(orient, "0") == 0) { > > > - strcpy(str, orient); > > > + strscpy(str, orient, n); > > > } else if (orient[0] == '-') { > > > - strcpy(str, &orient[1]); > > > + strscpy(str, &orient[1], n); > > > } else { > > > str[0] = '-'; > > > - strcpy(&str[1], orient); > > > + strscpy(&str[1], orient, n - 1); > > Why n-1? n is the total length and this is printing from [1], so n - 1 is remaining space. > > > > } > > As far as I understood the logic, it inverts the sign except the case > when it equals 0. > > I have a question here, why can't we always use +/-? > Why can't 0 be prefixed with a sign? Technically a userspace ABI change, but I agree it should be fairly harmless unless someone is rolling their own string handling routines. > > If the above can be used, we may simplify this code. > > Len, I think this task may be considered simple, but I recommend > thinking about each case and finding a way to simplify it more. > It could be a little simpler doing this, so 'maybe' worth doing. Jonathan