Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp697923pxk; Sun, 30 Aug 2020 20:24:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysCjBlStLKUHndXUrMY/FAfG8w2+uK6l8K9MwxH9Rdkvrw04BkZovRuAE772PobqqiilTO X-Received: by 2002:a17:906:380d:: with SMTP id v13mr5642162ejc.152.1598844298965; Sun, 30 Aug 2020 20:24:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598844298; cv=none; d=google.com; s=arc-20160816; b=DC6e1WxM52xycGVYDYZr2vvhl6h9wDXmwAlmpwFNILORQ+hxjOn5G2pN8dnxTqTWlW 1CGjyW1JKTR+v76UgfkYaY585GmPOSEr4YVASZjBc3zaVsbmnoPI/5/IO3d9JjLr92RT 1p2vVtpJoXseomrbUTVlTjtafdxyWLAJwFgWzrZy37UPBofc6gwCJezseGOufw+0BcKD qWM28d9kpT0nftc23J2zfIOpnLnnTdqphZUQtawQwUm9qApbvdmtqysH81O4VbItFPQY eMtECbR4Kv9eDzlst/Rco7y8gua6pc1kdlXISST7AuojQxdwH6s6/bd7GG3pY5Tqu9lZ kxWg== 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=N8f5GQnedoJxKHy2XbfhSaCNp5bq0GZo3txVV503oZ+qeJ1fiXvGuyNKEjbcCq1wH0 +lzKUmEZHfkUTYCpU+dWcun8X3ImADV+IKubH72vhPH8ZUFLV0BjMx/yuyjMyuep4uqn LiKp3x5xGJ9xMybRBVbM7pJ4tFPuUDeVZKek9Ypm8fdwRzg2gL5tPlNbYezT3UOdVxWB Dfxjya7DxDEQgFBIEW26PNjtPwdfE1So6TMZe5ZC6U7nKUPfEgYQ71+3XOPZO9LGy0pZ q3TzCeX5bwfgCSf5c7ynUuMep0E6b+YmQOSr1uC9H3URaYQhLwpOOYDjCj1C/7NFTqjh DnMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=aTI7a2AU; 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 t23si2055543ejf.457.2020.08.30.20.24.36; Sun, 30 Aug 2020 20:24:58 -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=aTI7a2AU; 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 S1727834AbgHaDVo (ORCPT + 99 others); Sun, 30 Aug 2020 23:21:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726838AbgHaDVi (ORCPT ); Sun, 30 Aug 2020 23:21:38 -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 A2937C061573; Sun, 30 Aug 2020 20:21:37 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id c142so3726129pfb.7; Sun, 30 Aug 2020 20:21:37 -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=aTI7a2AU4hxZdi+i7/z9kbwoB9GJNpqWiuKDO7BeND95OxdHew4eUuOlqpacA7Ol1E 4K9+mnRroycBM2xnBlGXu35ltuY4K0nh8+G8L56a56SfUfdvFv2U8YRZ2iQT9ZRF2I+4 69N/EdEGMc6yGWzdZAGnci9ASNMM3iNuq5EGEKrq9YDtuk6jN/mDGjw43tWfGaCJySHH RPFUyZAM3D4o1LH+eLBMSvpN6bNpFXhJDLXsYB4Dw0YbtR1vpxkOgog2L7F3TnBnfikp MfCPRSOPyGuaZL9+egRF9uxak365/EAwPIr/IFkyDtnFWpxS7s7RT6rGF+EiJC030umk kYCQ== 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=JPDROGSRm1Saa438RnXAmeWQzphOzhZ+d8j0VxOFW+jZSePpDrPkUNaPkNePObiG2o ZcIyEEgdLPY5E/1HTd28/pO+2xsqCvxWShFnzoxTZ4hCAKjH0yESQKU9Vgg/3drCsdir ZGBdNu098S3n2qSfP1mVPOm37cxrDZ7qkzScxe4++YnTU5BThY/u9oakt0QhFGGjMww9 GM3Afmz/r+zq3o9cYCAKwFjOLXbP59JJQ+ou+1xXL6NodaawDd6hdILchUeC6W7/8gRY Lv/vHONs+egUqgg9Y8nfecdnk/8eNqp4YoIahSSsSr5+4zv+fWTplrcHuhBgnEEpCcVI Y8YQ== X-Gm-Message-State: AOAM5332agqIWZORhv2050xINEmfyePqljQyrUomTenLeTkw3IToEkQU 8gLECswHajKeA4mMLDlJYYKXI8Zv2j0= X-Received: by 2002:a63:6c0a:: with SMTP id h10mr6864869pgc.11.1598844096832; Sun, 30 Aug 2020 20:21:36 -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 z17sm6095733pfq.38.2020.08.30.20.21.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Aug 2020 20:21:36 -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 v6 02/20] gpiolib: cdev: replace strncpy with strscpy Date: Mon, 31 Aug 2020 11:19:48 +0800 Message-Id: <20200831032006.1019978-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200831032006.1019978-1-warthog618@gmail.com> References: <20200831032006.1019978-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