Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp73926pxa; Thu, 13 Aug 2020 20:05:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfs0N0F+qMIL1p5mYrjDoKNEEYZ0QmVB3bHdVzWogyCzMMhOkd09tHSq2EnMABFvIwIDMj X-Received: by 2002:a05:6402:304b:: with SMTP id bu11mr373129edb.106.1597374308347; Thu, 13 Aug 2020 20:05:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597374308; cv=none; d=google.com; s=arc-20160816; b=TQ9jrNxYaPZcWa5nGooAShBN9oZy0Cc9izvQhqaJoRnUGs7zl6+ERokN6CBnRTY52X m4DVGjZQcTsMhGoidramBqKpYMCKHaLR6Rb0o5SyMaODwrtJQ3ayjcXtd4TQ8zj1sR5C ys1p5cxGFxrtlHwlMDsY3sqoYLd7NK9+v6hh6GJFklSOBR/9luVm57AOPCJ4z84rlbrR SfcKk7y4+quZ5Eh/n1Xqg4XQ8htDZzZ3xPdWWpy8PK0Fo2057YlDN/LhwYYUEiXLWF4e 0yeIPD1KSvFM49WZ8AUWLsOVoc1pB7xCDNylBR5H7Y4x+U1j0hMe3tnDYuU0tqFPuFUC FU0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wJ2LgLWLDnEe7sclGO7e9++McxVbFk4TMB6wGucpVAo=; b=ZEUfFLnLmsQl7vm2DYHFcehHMZnrY3MwdLewOQQ+ZuOIczrQKF48zMnQhdzGCNU4ox 37LUZvsSwLUpPFXSYnG8f/no/sK4vVcva1o59A7DL6nXWBMYrqJUI7DqMprFoR7uhNYF Z4BMMYMU7PqpC4h5n9BMY/8vPPrKpDsn1jbBdw13YBqzxed9NxplGoBlN1tJcIOAhKXN gc7nRN1yJwby0HkmyCxt+vj+cLrZXP2Bgrttyr9z2EkarzuwfjvQW1UD2sUXDO2rCPo4 VMDYPxKekIHnDzj0uO+L0Cj1f1LtC5jVzfoYvocUJwWpN2IiwsX60BU1L4WeSsE5GSFo FQaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AxFm7VzZ; 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 n1si4403922eju.121.2020.08.13.20.04.38; Thu, 13 Aug 2020 20:05:08 -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=AxFm7VzZ; 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 S1726709AbgHNDDd (ORCPT + 99 others); Thu, 13 Aug 2020 23:03:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726564AbgHNDDd (ORCPT ); Thu, 13 Aug 2020 23:03:33 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AD72C061757; Thu, 13 Aug 2020 20:03:33 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id y206so3845867pfb.10; Thu, 13 Aug 2020 20:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wJ2LgLWLDnEe7sclGO7e9++McxVbFk4TMB6wGucpVAo=; b=AxFm7VzZAY2MabY2l36y+a/zeZrVntbmos/mHJsl9xmGIcvXXid1mMlDXiCe9MZRP+ gjZP+rXhYS7vGF+7cl2cNsAap3RD4LmauIoNrZ5nPbWUgqeRjPlWC2TAxoTG4zdX8f6D 22UnQp18bFESURChAnwpQrO0JVd8vjS0nM4EMNzWKBiB07CZylAXXUr/fpG6VcL9g9FX id9hu6ye6/QcA6wMu/QdVg28twbxFrKIkvkzLq369uB9cXlFM7nrtW9+QQAyoMGSHDOB 12H7SmtRQY8GjDkSEdbqYZooHQRj8afQLz4vzAb9BxkKHoiFb/qNXRhQ7l1Mgh+CHhzF gbMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wJ2LgLWLDnEe7sclGO7e9++McxVbFk4TMB6wGucpVAo=; b=mPxMLHpi1SPFl1RCkN90Rmaz6Dyppa6ZazAhkXzEO54Eb8Rs6YhyQxX1HQcrvqW7V+ Hl4lOeIYhSfvoKJ595TBQuo6IG4YfU9x79Fvz+oDeHtKndc+pOa3KhsC8hgUhMeMyq/G 37Z7DaM7UmbcWAAPPPBIJksxyusCjgHCWXAchKYoyizuk0hVlWl5/bSdXTpUkp/BRK25 NRI6qUo1qfLS8IJvIiEu4LA0G1HnNuEtN4KfH5NEOi4Win4TmDqx0bCjfvbsswmZ0yde y9EiLETDfYT1SnZvmyfS3+TFdyDBvCuUI0kCqKRmybSBlw6ydM+QK/CgEwmtD6x2LUtV odGA== X-Gm-Message-State: AOAM530IKKCDrf47bfrbzqm/efHjLWz5gdqEQ8AVA20QImW2DkzQNavh jmEV9N1ndVPf11JcJWoj6In5Dl0L X-Received: by 2002:a63:1d23:: with SMTP id d35mr401029pgd.291.1597374212597; Thu, 13 Aug 2020 20:03:32 -0700 (PDT) Received: from sol.lan (106-69-161-64.dyn.iinet.net.au. [106.69.161.64]) by smtp.gmail.com with ESMTPSA id z3sm6522231pgk.49.2020.08.13.20.03.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:03:31 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org Cc: Kent Gibson , kernel test robot Subject: [PATCH v4 02/20] gpiolib: cdev: replace strncpy with strscpy Date: Fri, 14 Aug 2020 11:02:39 +0800 Message-Id: <20200814030257.135463-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200814030257.135463-1-warthog618@gmail.com> References: <20200814030257.135463-1-warthog618@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Replace usage of strncpy with strscpy to remove -Wstringop-truncation warnings. The structs being populated are zeroed, to prevent stack leakage as they are returned to userspace, so strscpy performs the equivalent function without the warnings. Reported-by: kernel test robot Signed-off-by: Kent Gibson --- The memset in gpio_desc_to_lineinfo(), in conjunction with the strscpy, is necessary as strncpy zeroed pads the remainder of the destination. It is also guarantees that the info cannot contain kernel stack that could get leaked to userspace. This is useful here, but is even more important for the v2 info that this function is changed to generate in a subsequent patch as that struct contains padding and attribute arrays that need to be initialised. drivers/gpio/gpiolib-cdev.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index e95e3eab9867..8b012879fe3f 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -752,6 +752,7 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, bool ok_for_pinctrl; unsigned long flags; + memset(info, 0, sizeof(*info)); info->line_offset = gpio_chip_hwgpio(desc); /* * This function takes a mutex so we must check this before taking @@ -765,19 +766,11 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc, spin_lock_irqsave(&gpio_lock, flags); - if (desc->name) { - strncpy(info->name, desc->name, sizeof(info->name)); - info->name[sizeof(info->name) - 1] = '\0'; - } else { - info->name[0] = '\0'; - } + if (desc->name) + strscpy(info->name, desc->name, sizeof(info->name)); - if (desc->label) { - strncpy(info->consumer, desc->label, sizeof(info->consumer)); - info->consumer[sizeof(info->consumer) - 1] = '\0'; - } else { - info->consumer[0] = '\0'; - } + if (desc->label) + strscpy(info->consumer, desc->label, sizeof(info->consumer)); /* * Userspace only need to know that the kernel is using this GPIO so @@ -841,12 +834,10 @@ static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) memset(&chipinfo, 0, sizeof(chipinfo)); - strncpy(chipinfo.name, dev_name(&gdev->dev), + strscpy(chipinfo.name, dev_name(&gdev->dev), sizeof(chipinfo.name)); - chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; - strncpy(chipinfo.label, gdev->label, + strscpy(chipinfo.label, gdev->label, sizeof(chipinfo.label)); - chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; chipinfo.lines = gdev->ngpio; if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) return -EFAULT; -- 2.28.0