Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp1936587rdb; Sun, 19 Nov 2023 17:34:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHAdiqVPbkTJVxBTROPZllAcvyuIc7DEcwGsXkFC1db/Mjq2Xq3TlcsaEt8TyoCZomqyFKp X-Received: by 2002:a05:6a21:1a5:b0:15c:b7ba:1671 with SMTP id le37-20020a056a2101a500b0015cb7ba1671mr8750319pzb.2.1700444055863; Sun, 19 Nov 2023 17:34:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700444055; cv=none; d=google.com; s=arc-20160816; b=cVma+TkFtj7I/OTzJ36lXNjDFiSr6V8JljYqX+VCcX+sOOL2oZfUiaeGoMSLiHExFr RYs3L84BnHXAynSd8Tl+/LBx8pLV1S4o8TJkQ4XdWWDOtVRZePcVFwOBkXlNP/DE6X+a jALn7z+6Q806bjYNi7aGblEIjeLyI1D1Evz3zZDPbbT3MltYOtPPBhXRLOxbvF7bTEpP ntDRuPC0vyJhT2X2VK34/6FqixxU9uCrcbO0oWo5jS7jk1JPoIPmvJc8W8eEyyxuzNi0 arSWn7dnSVmg1TFDdNXoDhkiXzv+2RYExQdtthcWk7a/o09TBP8V8nxHObBa3I+L9kSq Rp+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=dp3F0jZpK4Xy4+0DQhm52CttZBKAjwyOmhBp6iPkJAI=; fh=1BqR1afEoK0SnZAeAhqiuk/g/kpnkNS0aIqdp0Y7Rpk=; b=k5PNRNisTj3O7ufSB8tUXm/7OahJZdIZ6Eq0YrzCEO/XnEz0lAGfQEyITJ0+pOpEDl U5AQQCTyl78Z92l1PEnPhSbxwpCE/r9+MHlv9ntozy0Og18aarkRX82mvST8s1/cQfoz e4JlYkQ6ut7W/CxXm6fZQ9gzcqRATaaGo4eIG5dI9G63WF8Ny+ohNFgbaM5nt1wNcgsT EX37M52c9Yku4dq89oqlwhAJ2+RNdY5GcKfIbuRbUp6oK4RnDVjqiQVdjwYewEfCF+Su yNr1mYwXplSySmGT0cAz7CeDyhveXN38JYwaOIFYfAHnqgn8ApPRLUo5iJ2KexjBeg0Q RQAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id v6-20020a63f206000000b005b95ccd1b4dsi6302697pgh.82.2023.11.19.17.34.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 17:34:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 3CFB580560E7; Sun, 19 Nov 2023 17:34:13 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231716AbjKTBd7 convert rfc822-to-8bit (ORCPT + 99 others); Sun, 19 Nov 2023 20:33:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231615AbjKTBd6 (ORCPT ); Sun, 19 Nov 2023 20:33:58 -0500 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0283FAF; Sun, 19 Nov 2023 17:33:51 -0800 (PST) X-UUID: a415da84ec33465490b3bf93b867497a-20231120 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.32,REQID:971e500b-8345-482f-863e-990189d2dcff,IP:20, URL:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:5 X-CID-INFO: VERSION:1.1.32,REQID:971e500b-8345-482f-863e-990189d2dcff,IP:20,UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:5 X-CID-META: VersionHash:5f78ec9,CLOUDID:87f5b5fc-4a48-46e2-b946-12f04f20af8c,B ulkID:23111917141437RVKK6Q,BulkQuantity:4,Recheck:0,SF:38|24|17|19|44|64|6 6|102,TC:nil,Content:0,EDM:-3,IP:-2,URL:1,File:nil,Bulk:40,QS:nil,BEC:nil, COL: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_FSI,TF_CID_SPAM_ULS,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS, TF_CID_SPAM_FSD X-UUID: a415da84ec33465490b3bf93b867497a-20231120 Received: from mail.kylinos.cn [(39.156.73.10)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 1847431844; Mon, 20 Nov 2023 09:33:37 +0800 Received: from mail.kylinos.cn (localhost [127.0.0.1]) by mail.kylinos.cn (NSMail) with SMTP id 867A9E0080FF; Mon, 20 Nov 2023 09:33:37 +0800 (CST) X-ns-mid: postfix-655AB771-470563140 Received: from [172.20.12.156] (unknown [172.20.12.156]) by mail.kylinos.cn (NSMail) with ESMTPA id 891A4E0080FF; Mon, 20 Nov 2023 09:33:35 +0800 (CST) Message-ID: <313c7290-1f8c-4944-a420-be23d28e59fa@kylinos.cn> Date: Mon, 20 Nov 2023 09:33:35 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] ksmbd: prevent memory leak on error return To: Namjae Jeon , Pierre Mariani Cc: Zongmin Zhou , linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org, senozhatsky@chromium.org, sfrench@samba.org, tom@talpey.com, kernel test robot , Dan Carpenter References: <20231109011725.1798784-1-min_halo@163.com> Content-Language: en-US From: Zongmin Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT 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 agentk.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 (agentk.vger.email [0.0.0.0]); Sun, 19 Nov 2023 17:34:13 -0800 (PST) On 2023/11/19 22:17, Namjae Jeon wrote: > 2023-11-19 18:14 GMT+09:00, Pierre Mariani : >> On 11/8/2023 5:17 PM, Zongmin Zhou wrote: >>> When allocated memory for 'new' failed,just return >>> will cause memory leak of 'ar'. >>> >>> v2: rollback iov_alloc_cnt when allocate memory failed. >>> >>> Fixes: 1819a9042999 ("ksmbd: reorganize ksmbd_iov_pin_rsp()") >>> >>> Reported-by: kernel test robot >>> Reported-by: Dan Carpenter >>> Closes: https://lore.kernel.org/r/202311031837.H3yo7JVl-lkp@intel.com/ >>> Signed-off-by: Zongmin Zhou >>> --- >>> fs/smb/server/ksmbd_work.c | 5 ++++- >>> 1 file changed, 4 insertions(+), 1 deletion(-) >>> >>> diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c >>> index a2ed441e837a..44bce4c56daf 100644 >>> --- a/fs/smb/server/ksmbd_work.c >>> +++ b/fs/smb/server/ksmbd_work.c >>> @@ -123,8 +123,11 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work >>> *work, void *ib, int len, >>> new = krealloc(work->iov, >>> sizeof(struct kvec) * work->iov_alloc_cnt, >>> GFP_KERNEL | __GFP_ZERO); >>> - if (!new) >>> + if (!new) { >>> + kfree(ar); >>> + work->iov_alloc_cnt -= 4; >>> return -ENOMEM; >>> + } >>> work->iov = new; >>> } >>> >> A few lines above, ar is allocated inside the 'if (aux_size)' block. >> If aux_size is falsy, isn't it possible that ar will be NULL hence >> we should have 'if (ar) kfree(ar);'? > We need to initialize ar to NULL on that case. And Passing a NULL > pointer to kfree is safe, So NULL check before kfree() is not needed. Yes, ar should be initialized to NULL to avoid the case of  aux_size will be false. Since kfree(NULL) is safe. Should I  send another patch for this? Best regards! > Thanks.