Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2049195pxk; Sat, 5 Sep 2020 06:55:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJ1m8M8yq/y/K2g03C4ew6zO5EK2r0lREDf/jZ6et387phHuFgSfw2e7fwCUEgnVJjxbe+ X-Received: by 2002:a50:fc0b:: with SMTP id i11mr10180247edr.164.1599314151330; Sat, 05 Sep 2020 06:55:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599314151; cv=none; d=google.com; s=arc-20160816; b=rFrtSdJe/b5XdCHlxGxOL500J0Hny8nbl89nQ25WMkcCk6XFjr4AF315E5wqKhbY5g E+8Z3FzuPwjB0DDszOWfzSLIaoeSPXIqgwabNd0wlfzwksbEc4WQcDnI3lcoC/aR6Zz3 zxTPoUeLpgICA6QV1lWiH2vS9pq3YY+u9S9BFmC5FznaH2nftzx2txgLG8QlEkOo3Jwn EWy0aiDNTAQs3JprXnXu+BpSWSf3x37GT4fyfQLngy1utbtL1bOuLzBq/ZQD+8sSru4/ 0wrKP8MyoaMUxISaBMLKCRGTBWJk7kO0kKsYDWEi+r/ymQ+Wu1kPdTJ/peANn3BeIPwI NpdQ== 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=RcaJJxPUWfTCsa1mpxbpca/AYe7cJ1n1R+MIiQxItQI=; b=JS0qNcnRXYkJX5EmYx6JKKZbeACgQ7Py83xaVCYlSCgtJDYvS2l598uvpw/OLIxXt5 67SlFcUkdFEbitGZ2kPiuZYAYo1lQ/pnpzPHhWgdu9NUJVi0PaWnBOIKcRuzj6ypf4mQ JPb4KHGlK23qO66TOdlhXUj7f3vkHEVJPHUjyG72HIFi5u/JC51W5jHERbgtoNB25DAV Uf8bAu34NNNOFlUPqcwBai4cS66I1afQni6EZz3tmn5m6giZVOcAT9IGJBv+rWJ7CXhR cZpO7vNJC3Am0Z/qLkjXAagzn1UCIhWo1Y1eGCK+2HjfRrVcq6HP0SudcFxSDg4EgPVQ msAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MxwYv0IT; 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 d90si6388593edd.489.2020.09.05.06.55.28; Sat, 05 Sep 2020 06:55:51 -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=MxwYv0IT; 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 S1728687AbgIENwk (ORCPT + 99 others); Sat, 5 Sep 2020 09:52:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728608AbgIENgv (ORCPT ); Sat, 5 Sep 2020 09:36:51 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 586DBC061247; Sat, 5 Sep 2020 06:36:49 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id m8so6166073pfh.3; Sat, 05 Sep 2020 06:36:49 -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=RcaJJxPUWfTCsa1mpxbpca/AYe7cJ1n1R+MIiQxItQI=; b=MxwYv0ITKeCA6AbvehPS3/EpH1B+J3WBt3JiGsQmv0zOFjRlJUVuy2UsNZqx/G/NmL vwgTraIMLPWp0T6h9/TEeaLt0o04wzz8nUYcOUvLy5GcY77nd64GNS9H4F9/+X88dwd+ 5/HGua9nQHa45JzbY70HzJzLjtLqQ4IXMDHiEs1qyhC7g861cDVl+uY8DOxYp3UpOSpU OLEFkyGzXwC4Q6wQ66a1PW/guANKAVjGdXo2I1KVyMeGGlaJhNyqtXE3SvoGyoQAQ5co Zis68Uh+mGLooYDOstIO8h+FKKKDK/M4O7fG6J3FNRF3z8esT1BAUKDjf/oGa88pq5Sn o+dA== 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=RcaJJxPUWfTCsa1mpxbpca/AYe7cJ1n1R+MIiQxItQI=; b=g483qH6mfSZtnSgZ0k1eGl25g2bM5ARl4NPV0vU8cNlbN+bJ444jJXQ+MY35hH0JKJ VMW6fI2B2cCrJKDhNKBq6jmF7OCiFkukrlh0pIikxhxZXoR7F9J3kYkf7dFmnYKyQJTq OVqHTIONzXOQ3lKoXbtpV0Xh9XfE07VH3tf0gCb46TGgLN3U+OFySyxBW93MdClygKHn /vV8LaNPNHTm8/2g/mbVDgmyIxu1W1yWPe5u8li8kIF2I6pcZie00Gm2wvNkeWiK7fA+ CCkqyZ55CW0CZBXXbDTDq7pIvE4v+8ahA6bmV5HhXJXG6axMp+8nXuYLnK2m+ZJC52Ok nbBQ== X-Gm-Message-State: AOAM532Nme+4xw5nXOkCShKD7P8W+V/prdqv+YyABWLr1KRgNXzJQ5k+ NI+rl/vN9TB1WMrvSbTyfzH0RgP4JSo= X-Received: by 2002:a62:6845:0:b029:13e:dcd:75bd with SMTP id d66-20020a6268450000b029013e0dcd75bdmr275980pfc.12.1599313008475; Sat, 05 Sep 2020 06:36:48 -0700 (PDT) Received: from sol.lan (106-69-184-100.dyn.iinet.net.au. [106.69.184.100]) by smtp.gmail.com with ESMTPSA id u3sm7556717pjn.29.2020.09.05.06.36.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Sep 2020 06:36:47 -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 v7 02/20] gpiolib: cdev: replace strncpy with strscpy Date: Sat, 5 Sep 2020 21:35:31 +0800 Message-Id: <20200905133549.24606-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200905133549.24606-1-warthog618@gmail.com> References: <20200905133549.24606-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 zero pads the remainder of the destination. It also guarantees that the info cannot leak kernel stack 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 also 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