Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6020287rwb; Tue, 22 Nov 2022 07:49:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf5O0u8CeJBmTuHHQI46hqnVpWgGUn7XqAQJ/E/OAOLYjEWQr0UCq44S3nkAgAusVWrSTvjX X-Received: by 2002:a17:90a:f112:b0:218:c296:b617 with SMTP id cc18-20020a17090af11200b00218c296b617mr7605441pjb.132.1669132162742; Tue, 22 Nov 2022 07:49:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669132162; cv=none; d=google.com; s=arc-20160816; b=vOu86zrT0sj58L4Z920vNTZ2nzoYAQwJgIc3F1iraqg1ICmIKzzacMY5AkI3hLWyuT ta2M+nQWzIHj8ZQMd7NYl3oNlvce83Fw26fZIh4qoGNW+R7EoWBq3q/3Hz3nOLAB8Jae SCDTc+PE+3l5fQGF6LWwe573tL98B9oIjtw2+FRYhYkEjmhEGC2AXupxmBZ2TZifYJtF Uy/pnSrNwwMa1V4lB4w7TYmsFHEW2Ry9q2OpKf6qxwCwovpVuPh6Vmp4A74u2JbjP4nB he6RDUVNhR6cfRoCAom7skpoIYFEH0viZlGS0P0byhmMufMHTsI6R3/289mVtpcP6qRV ILdw== 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; bh=QLwI3J83ElUlZ56rt6GpFbYxJ/Gv3efqN7qp4lDumtg=; b=OEA+/NjqVhc8YNcORNm1hyHiDwYJ2i6dKGOhLHodag6huAxgdwdT5hyBEErEONtkEl CJKafa8QlVDymo34lrCAOQlEOMZsUkiQsZWSaoEweTlZd4TXYT3PgTgdyMzvDCsrosGs 3HMmBvmWzP+h0cGdDWhklDQ5TtAvoi4h3p1tYFU15bApRlCk69pqJsV0LGpnEL4qX6JC uP/F2I4JjNKnjSOAFvNuEKBMV6DkbQ9ieAdUnV+J3ygAGf+eyVJ2cVaNAAkn98ESfI+q vbFMAuQbtwWa8Om+I9PjdZGZewC2FXxsNGdOYnJ6S4gYVhIfGNj8JsvplfiAcCTXeVUM ATpg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n15-20020a17090ade8f00b00213566a9fa9si16010581pjv.25.2022.11.22.07.49.09; Tue, 22 Nov 2022 07:49:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233499AbiKVOv5 (ORCPT + 90 others); Tue, 22 Nov 2022 09:51:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232762AbiKVOvx (ORCPT ); Tue, 22 Nov 2022 09:51:53 -0500 Received: from gw.red-soft.ru (red-soft.ru [188.246.186.2]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8D9B9663D2; Tue, 22 Nov 2022 06:51:51 -0800 (PST) Received: from localhost.biz (unknown [10.81.81.211]) by gw.red-soft.ru (Postfix) with ESMTPA id 427563E1A6E; Tue, 22 Nov 2022 17:51:49 +0300 (MSK) From: Artem Chernyshev To: Chris Mason , David Sterba Cc: Artem Chernyshev , Josef Bacik , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org Subject: [PATCH v3] btrfs: Replace strncpy() with strscpy() Date: Tue, 22 Nov 2022 17:51:08 +0300 Message-Id: <20221122145108.3710710-1-artem.chernyshev@red-soft.ru> X-Mailer: git-send-email 2.30.3 In-Reply-To: <20221121185427.GB5824@twin.jikos.cz> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 173685 [Nov 22 2022] X-KLMS-AntiSpam-Version: 5.9.59.0 X-KLMS-AntiSpam-Envelope-From: artem.chernyshev@red-soft.ru X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Auth: dkim=none X-KLMS-AntiSpam-Info: LuaCore: 502 502 69dee8ef46717dd3cb3eeb129cb7cc8dab9e30f6, {Tracking_from_domain_doesnt_match_to}, d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2;red-soft.ru:7.1.1;localhost.biz:7.1.1 X-MS-Exchange-Organization-SCL: -1 X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, bases: 2022/11/22 09:40:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2022/11/22 11:32:00 #20598255 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Using strncpy() on NUL-terminated strings are deprecated. To avoid possible forming of non-terminated string strscpy() could be used. Found by Linux Verification Center (linuxtesting.org) with SVACE. Fixes: 475f63874d73 ("btrfs: new ioctls for scrub") Fixes: 606686eeac45 ("Btrfs: use rcu to protect device->name") Signed-off-by: Artem Chernyshev --- V1->V2 Fixed typo in subject V2->V3 Added fix for ioctl.c fs/btrfs/ioctl.c | 5 ++--- fs/btrfs/rcu-string.h | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index d5dd8bed1488..fdf62d514662 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3748,9 +3748,8 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, di_args->total_bytes = btrfs_device_get_total_bytes(dev); memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); if (dev->name) { - strncpy(di_args->path, rcu_str_deref(dev->name), - sizeof(di_args->path) - 1); - di_args->path[sizeof(di_args->path) - 1] = 0; + strscpy(di_args->path, rcu_str_deref(dev->name), + sizeof(di_args->path)); } else { di_args->path[0] = '\0'; } diff --git a/fs/btrfs/rcu-string.h b/fs/btrfs/rcu-string.h index 5c1a617eb25d..d9894da7a05a 100644 --- a/fs/btrfs/rcu-string.h +++ b/fs/btrfs/rcu-string.h @@ -18,7 +18,10 @@ static inline struct rcu_string *rcu_string_strdup(const char *src, gfp_t mask) (len * sizeof(char)), mask); if (!ret) return ret; - strncpy(ret->str, src, len); + if (WARN_ON(strscpy(ret->str, src, len) < 0)) { + kfree(ret); + return NULL; + } return ret; } -- 2.30.3