From patchwork Tue Nov 22 14:51:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Chernyshev X-Patchwork-Id: 24413 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2251522wrr; Tue, 22 Nov 2022 06:53:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf4jUmH1rmVZSPBKvB4eJjLdcG5yn3boDga0snuCYTKqwdeJBuP3RrHBzHfCqi4uCyifQFjs X-Received: by 2002:a17:907:8c05:b0:7b2:bb8e:702d with SMTP id ta5-20020a1709078c0500b007b2bb8e702dmr7094211ejc.432.1669128836196; Tue, 22 Nov 2022 06:53:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669128836; cv=none; d=google.com; s=arc-20160816; b=s6v/d9lRH9MF4y9Yk7TFvBAMrnJSUPM16SKtSDkDJc8tctB9HEZHPNFrs8WO0SuoSL V0FivJdK7FD4pMHB1dSP26g0JBdTfqSZpnzK/oI1Av71AF2V+tBI0OnftT+Ahifx0dIt 1XXb19k2eI13aQZ+Y/iAJMY+kPrtTV9pGTC3tsgfOp9M1VzV30RII/AG/bonFkKC/1// 8xUVcxJJ7A5bqF3BYVpMv64T0hhJ5emJK2MCcDATzkcQe3qwOqxuavyc5Gy1emX760ve BhR1drfzDUWllbRwhE4HpdrYyz+MhDkRLIQsinyZPwCn7+ug7n65Pm1KmSVQMQTjFNM+ jD9g== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vq3-20020a170907a4c300b007882926848bsi11570785ejc.818.2022.11.22.06.53.32; Tue, 22 Nov 2022 06:53:56 -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 + 99 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750208438526603262?= X-GMAIL-MSGID: =?utf-8?q?1750208438526603262?= 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; }