Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2115837pxt; Sun, 8 Aug 2021 12:06:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywjHs5jPIoMTwNXSqnxs/ExfvBfPjIhyVV6ngwYdD8yDrFz9lJpU0Wp1rbzs6KND4/mA70 X-Received: by 2002:a17:906:150c:: with SMTP id b12mr19008365ejd.275.1628449594765; Sun, 08 Aug 2021 12:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628449594; cv=none; d=google.com; s=arc-20160816; b=AL887ZNf3+TGoQ7dRWAPt1HK00BftYA9foudPCdIKfQWkKmOX63ClZjMmi/N1HnhcG D3WBtTqo45E+UBPXgBOl57l1kyoD+ygkua5DBj48+u24OQWrZGpDKrY9qnCVe7rzAdt9 E+Gg8RvcXazWFCmfTkM+Zxu+q0QUKxkNtH2AcayzBXL7TwemjCe80X7oIJNUh1QWKcRu gC4CkK1r0Ir0B72BCt69spNAJOp7XhXPISqTcXG9zaQC/aDHza+iVqw102+TmG7xPlO1 CDhitWqd6NzdGfNXFwXRAkf0JeFrsRSTAwVtgpx91vJ8WeOSM/Br2TJKWG2cjDdB4El3 JXZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qBu5iUaSAxxZpgJfxNDAcC/Wp2BLXG1BK3fHE7DMi70=; b=NNTkNTM2cSgH3TqDpOlyFs3ZKVLt9THgL9Fvs4JJWJ3QxuC1ZpGFi/AmyblkryPKOL 6Q7LvIe6xLDZmpAxbtsRiGgQaEzBNG2SKZBpNNC3FH87FC/CWmCjdj3moDCJ9dHL9AUF m7crusjPw7Uhhps04EepupHBcXu1q9NoRNKA/5bT68J+0KBwoTjcrdPg1ZMKuwXphYm+ O9jiVqRUxrToeb5GfFVSHGnaceO4lIchtV30lneXrr4sk7GL3v5vWapp4k6AfEWgm4jI Fkq4jggoZSSjcyv5AtbwQk9sP8k9R4wAif/R0DTr/SkCvZMGVN+LnstlMkpijahJ6BHv qSWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tUpEbBFw; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y12si14655921edp.290.2021.08.08.12.06.11; Sun, 08 Aug 2021 12:06:34 -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=@gmail.com header.s=20161025 header.b=tUpEbBFw; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232435AbhHHTBc (ORCPT + 99 others); Sun, 8 Aug 2021 15:01:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232284AbhHHTBc (ORCPT ); Sun, 8 Aug 2021 15:01:32 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9EA5C061760; Sun, 8 Aug 2021 12:01:11 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d1so14077924pll.1; Sun, 08 Aug 2021 12:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qBu5iUaSAxxZpgJfxNDAcC/Wp2BLXG1BK3fHE7DMi70=; b=tUpEbBFwt/yi6jWl+lS2T/oX8Km8JAD2DIZeIPuoQE+MF/+EOx8nBPFbudEB0WXd29 JEZaNCk960Jpgqn6/b1alTqcbP/Cfx3WAdeksY4clk0CWQMgyFwrkhAAkIrP/QpVJ3hY VqyppylTuz1BsMOSUcvSeEoE6g5hgcMOsFT1d2nMtMmqV68DFBwNX+5UJEls1B7h2LB2 eJhNoiw3WpqgrUTb2gk7k4/Qx9+fuVaUh/hLmSnzC2gkjREh83ugtKKBIPJJqP/ytFIN 4t1/mGZn51/qpeqpFy7Kv9c78nazmY16RyKg22OTxUtC3Qz8gkpwOi98Ka5JwVe4uV/z TuGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qBu5iUaSAxxZpgJfxNDAcC/Wp2BLXG1BK3fHE7DMi70=; b=EMlynxhvMrcii04RBgzrQbQ0J52GqmfscOEhRVo3voLJH1Zg0w5oIMfq4U7wJvF9nx toj5XNunx7hCl+mN5E2qpt5+FR2mb5FpH5/ZtoC48tEsIVlvo+i3q9bVq2B4CdgN9Fcs +45JJYTAZbI6J7k79NnX+XsP0EmMnK4NBFfoEervxtohgSzDT0kI12LrHPX/XUBPZ5kb vUSKrc8/kIsv8h8fvB9q7BvM82mOAsvCnd6/scdDkzG/+B4+PtSgF3ceueGvVF1IdpZn z1tsEYJDxkRyJoOjV995M7FUYmiSd+NwxjJ4cJ5Rc/tgj1BdcdlPrTuYfJXq6xcVX0AR pDYA== X-Gm-Message-State: AOAM5319Dq+hi4zXVQqJGj+zS6jhs/YoBKJvOks4mnHNZVI93WMZ5Hjm c6vh+QSbg27PthF6ML0w08i/BqD8FQRfoG0pbUI= X-Received: by 2002:a17:90a:b10b:: with SMTP id z11mr32121963pjq.181.1628449270967; Sun, 08 Aug 2021 12:01:10 -0700 (PDT) MIME-Version: 1.0 References: <20210807152225.9403-1-len.baker@gmx.com> <20210808172503.5187cd24@jic23-huawei> In-Reply-To: <20210808172503.5187cd24@jic23-huawei> From: Andy Shevchenko Date: Sun, 8 Aug 2021 22:00:34 +0300 Message-ID: Subject: Re: [PATCH v2] drivers/iio: Remove all strcpy() uses in favor of strscpy() To: Jonathan Cameron Cc: Len Baker , Lars-Peter Clausen , Kees Cook , linux-hardening@vger.kernel.org, linux-iio , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? > > } 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? 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. -- With Best Regards, Andy Shevchenko