Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3617059pxk; Mon, 21 Sep 2020 20:12:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPeg7AKEti3f0DjY61O1E6Nkhg+1F8UVegslK6Z7tCH5HipgbMF6waxHxFztj2Mc8GYWB9 X-Received: by 2002:a17:906:364b:: with SMTP id r11mr2628117ejb.48.1600744325304; Mon, 21 Sep 2020 20:12:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600744325; cv=none; d=google.com; s=arc-20160816; b=mC3R2b94SzwmbjAqDYwDy/kN5S7F99587WlnzaArO62r3AbJ80aKD+Ty+jE4EqlxBi HloBjLA6xqa8Ih41iDzDDIOcpSLePHi0FJumbEift5IndzKBGWAV5PX0534+NHG2jCsc P/09mx50QQSL3Wub8gZG9AgS+zwLWb+3GSHfbrdVcjchlRZ9xc9Y1Px7JRAY1a8Ug9SV 5UBummA/lTS26GEnZobYVylEDB2HMRmXtCovXeYzqvMefNp7+lQKecxAOqpmEOYqtzog HmbIG9h6ZbZVmcl6YQHWR7cxbpI3uawj7/TwO2HlAnF+7CV5OxbEQerFB9cNHXF2tz/Y wcxA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Eem0c+jPGt4h5O0FB7VOY7qfs4N3yUmzBZn+jIhhYvU=; b=SE2nssguXupoZ4EMvRnUoSr6pYyoJqlMl8mRFAwsD1XOhygJdXNdegr4ZZx9HJw/pb zuQvxYFUP0ndAAgB9s3sPL3+6rnbFQpcD9HfkjOEzmgtadVXKzztiaQnqXnxgjO8CnnV PJfEvkjCTb1lM1yMLYLoDRuHSWJjNjZUyVXw51cX4TIedtcgN8s3jaT7BsOHdoBk4dm/ x5nrN0LGMkso/ZB1fPu8hTBFG62Z1kZFQQL+Rcpu2IqH91F8GXfDVAYQl1RdHFPzPErl ap+KpSDmSvq5U0MRI2/hDaJ5AnjxPbSBH44L2sMSuDN5hAyZlxt0YEGSSPuHsxeHg8Ka Dx0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=eXqr5EpE; 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 l3si10011564ejd.163.2020.09.21.20.11.42; Mon, 21 Sep 2020 20:12:05 -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=eXqr5EpE; 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 S1729808AbgIVCe2 (ORCPT + 99 others); Mon, 21 Sep 2020 22:34:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729379AbgIVCe1 (ORCPT ); Mon, 21 Sep 2020 22:34:27 -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 C3CBAC061755; Mon, 21 Sep 2020 19:34:27 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id l126so11073830pfd.5; Mon, 21 Sep 2020 19:34:27 -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=Eem0c+jPGt4h5O0FB7VOY7qfs4N3yUmzBZn+jIhhYvU=; b=eXqr5EpE5nujDhsP0bx57l/Z9UKevEA53ZX87cschPeuX67tKhGnjikc+geuJkKot0 pFLgRgLPqm7aRHmBVcgS1uFEqKaraH9gnJljdJAX7ZGBmkJZqJ4Huid6glgxrs/kC1Pv SYqarqaXyumfZ2dU5e7q3hXZZZEQR/tDBcHTjjcGzU3zcl4Nf0KTUaBbZn4e0KNfZ9+P z4J7oAao4thTQnmKzQ6bsG+Sbf2b2MLGDjWfhNNah1h8aF1j6G2oe871vP2xIvX+suw+ PwTvLNRgyRjEpoh2EoNNSUpyJkFTDA6M1fKF8+MVcQNbeNQnWknIcuxYrrHybMdDvde7 fJSQ== 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=Eem0c+jPGt4h5O0FB7VOY7qfs4N3yUmzBZn+jIhhYvU=; b=FaXVPPIIVjk/x/4uLF55Q2U6iGUvGV738iPs3crKYhrXnmX0dNy+OeL2SsxrVPzCtV fTpQYd/fgRduvmW8fW+pwmOs4urYgV8JjtRo9aYQD1Uebx/0REKUhJIncy53GSwAdpso X+NHEUxTfIuwhiqnO1oHI2tJerd2ppRfve4tL+sOpgqGSe9n0NU7cdo4/srllMjO3g5x 0qb+kkTRf2zIZ+uP7e8/MHDKwPK31iwBgRim3LTADGRIvTPsvRDe8f/QEV7uhTJWG7o4 GImlk3FGnl3X0v/q7XGNt4CApr0WF5BGZ/VUOOC28+5t9rYpsA/nzsSo+gwxrhBtJifQ XT6w== X-Gm-Message-State: AOAM5307EWNRbmom3bWqkwb+kh+uL0vcu4LSEpmz/NnMdNiQGwxnnc/6 COJiJRpZVVzcb9WPkQ0NfxTaLfOTx5orCA== X-Received: by 2002:a63:184b:: with SMTP id 11mr1773903pgy.298.1600742066897; Mon, 21 Sep 2020 19:34:26 -0700 (PDT) Received: from sol.lan (106-69-189-59.dyn.iinet.net.au. [106.69.189.59]) by smtp.gmail.com with ESMTPSA id j19sm13321390pfi.51.2020.09.21.19.34.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2020 19:34:26 -0700 (PDT) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, bgolaszewski@baylibre.com, linus.walleij@linaro.org, andy.shevchenko@gmail.com Cc: arnd@arndb.de, Kent Gibson , kernel test robot Subject: [PATCH v9 02/20] gpiolib: cdev: replace strncpy() with strscpy() Date: Tue, 22 Sep 2020 10:31:33 +0800 Message-Id: <20200922023151.387447-3-warthog618@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200922023151.387447-1-warthog618@gmail.com> References: <20200922023151.387447-1-warthog618@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Replace usage of strncpy() with strscpy() to remove -Wstringop-truncation warnings. The structures 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 Reviewed-by: Andy Shevchenko --- 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 81ce2020f17b..86679397d09c 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); /* @@ -766,19 +767,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 @@ -842,12 +835,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