From patchwork Tue Dec 5 09:58:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kunwu Chan X-Patchwork-Id: 173873 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3320877vqy; Tue, 5 Dec 2023 01:59:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHSoAYLztXSv8igfa8MWVhkORO/pOhASEpQVT4fx8hFDEG3V/i/lfx/6InOAWViDRSe89z/ X-Received: by 2002:a05:6a20:7f95:b0:18f:97c:6162 with SMTP id d21-20020a056a207f9500b0018f097c6162mr7067012pzj.95.1701770347392; Tue, 05 Dec 2023 01:59:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701770347; cv=none; d=google.com; s=arc-20160816; b=BB4bXVpO0fId07G3Uj4TkkPgbomhltISRXVQ6A/QiX1byZPkrX7LwVNUtzE+DuBfGo XU70QpFmvODthDuihbgraHCyEjbklay0QhwMMafmg80948WeWZnu5qIWlyQfgl/ZHdtT 8oDfa23iV7Oy1+KesNdGi8fFV1Z2l4YFdrT1wLurVpf1dBNd7PcPwlnlmu4ZSMBM919X 49UBK0d0o6rnVTQpMMCLtgjuRiotND2WMHnjdfuNCauV9emGfi0isnVPWl3hqRLjsbeD KI3WPxFR3ZAPscxOuNfWT/H4SJSX0XtF5aeCD90IVT8iylwYu/ieIUU512+zGNO2B5TL WtXQ== 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 :message-id:date:subject:cc:to:from; bh=z5CEn1UZpXzPPFcZD5Eecl4xpnDEIRsJ97D7SZ47HjY=; fh=FIUPk6LiDkIWEdRVHzXIZ0JUDW318ec8J3jqNlf7u0o=; b=oysRFeNOiqyfeJEg71Rfarnr0sPhsB3z5kwPIJsefvD30fGupl25UEZfWz0AR+pQNU QDoeKuooEh0tjsMiu2C9ds0S2H6+15+3XmrN2C2OJ63vpT2Vv+GMu07ATNAn77hxzZs6 BVT6Rnjk2KKY4rnd4lJicDg2d1CQtpaJw3F8H3Billx/EJffMT9xVPdNO2AkK8wDv2VQ cz78BGHeyxLL0KHiPESMclFKr88tcw8Konosfc/D38oqmKTaEN5yT8O5TxWMS1K0v/EQ d4jToVSYyxkQ90Zmjl3R02vwaeFkuCc1WExd8vmtqmYHaIpyi/ab000+VIXuHO990Tdd efTg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id u4-20020a634704000000b005c1b5a76098si9120852pga.771.2023.12.05.01.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 01:59:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id ED63280436A1; Tue, 5 Dec 2023 01:59:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231772AbjLEJ6s (ORCPT + 99 others); Tue, 5 Dec 2023 04:58:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229643AbjLEJ6s (ORCPT ); Tue, 5 Dec 2023 04:58:48 -0500 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA544A0; Tue, 5 Dec 2023 01:58:53 -0800 (PST) X-UUID: 9e0e00b194d04b3db395bf7e6e040e34-20231205 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.33,REQID:aef0a0c6-7cde-48a6-ad98-77de04a783a8,IP:5,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-35 X-CID-INFO: VERSION:1.1.33,REQID:aef0a0c6-7cde-48a6-ad98-77de04a783a8,IP:5,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-35 X-CID-META: VersionHash:364b77b,CLOUDID:d75dd660-c89d-4129-91cb-8ebfae4653fc,B ulkID:231205175850BNX1BCP2,BulkQuantity:0,Recheck:0,SF:38|24|17|19|44|66|1 02,TC:nil,Content:0,EDM:-3,IP:-2,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD,TF_CID_SPAM_FSI X-UUID: 9e0e00b194d04b3db395bf7e6e040e34-20231205 X-User: chentao@kylinos.cn Received: from vt.. [(116.128.244.169)] by mailgw (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1910631065; Tue, 05 Dec 2023 17:58:47 +0800 From: Kunwu Chan To: jesse.brandeburg@intel.com, anthony.l.nguyen@intel.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, jeffrey.t.kirsher@intel.com, shannon.nelson@amd.com Cc: kunwu.chan@hotmail.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Kunwu Chan , Simon Horman , Alexander Lobakin Subject: [PATCH v4 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read Date: Tue, 5 Dec 2023 17:58:44 +0800 Message-Id: <20231205095844.2532859-1-chentao@kylinos.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 05 Dec 2023 01:59:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784313917039779645 X-GMAIL-MSGID: 1784435543478442399 The size of "i40e_dbg_command_buf" is 256, the size of "name" depends on "IFNAMSIZ", plus a null character and format size, the total size is more than 256. Improve readability and maintainability by replacing a hardcoded string allocation and formatting by the use of the kasprintf() helper. Fixes: 02e9c290814c ("i40e: debugfs interface") Signed-off-by: Kunwu Chan Suggested-by: Simon Horman Suggested-by: Alexander Lobakin Reviewed-by: Alexander Lobakin --- v2 - Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf) v3 - Use kasprintf to improve readability and maintainability v4 - Fix memory leak in error path --- .../net/ethernet/intel/i40e/i40e_debugfs.c | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c index 88240571721a..78a7200211b2 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c +++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c @@ -72,29 +72,31 @@ static ssize_t i40e_dbg_command_read(struct file *filp, char __user *buffer, { struct i40e_pf *pf = filp->private_data; int bytes_not_copied; - int buf_size = 256; char *buf; int len; /* don't allow partial reads */ if (*ppos != 0) return 0; - if (count < buf_size) - return -ENOSPC; - buf = kzalloc(buf_size, GFP_KERNEL); + buf = kasprintf(GFP_KERNEL, "%s: %s\n", + pf->vsi[pf->lan_vsi]->netdev->name, + i40e_dbg_command_buf); if (!buf) return -ENOSPC; - len = snprintf(buf, buf_size, "%s: %s\n", - pf->vsi[pf->lan_vsi]->netdev->name, - i40e_dbg_command_buf); + len = strlen(buf) + 1; + if (count < len) + bytes_not_copied = -ENOSPC; + else if (copy_to_user(buffer, buf, len)) + bytes_not_copied = -EFAULT; + else + bytes_not_copied = 0; - bytes_not_copied = copy_to_user(buffer, buf, len); kfree(buf); if (bytes_not_copied) - return -EFAULT; + return bytes_not_copied; *ppos = len; return len;