Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1650327rwr; Thu, 27 Apr 2023 23:03:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4/1ZjBzRxPoPydP+XAq+pwSFGa+FByIJz+fC+wzq1ET+EK8ikpvWVcQg/f1VurWfp05NDT X-Received: by 2002:a05:6a00:2d1b:b0:63a:8533:d6a7 with SMTP id fa27-20020a056a002d1b00b0063a8533d6a7mr6635411pfb.9.1682661788461; Thu, 27 Apr 2023 23:03:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682661788; cv=none; d=google.com; s=arc-20160816; b=t0fj69VLh+W3vBFeorawiHyAYntOrpG8Uvc1nmZ82UqUbybZpi3DzHlrK5VDD1kbwc pxhYUc5M44QeTZZ1TlZgcAavFaxa/TdorS1M/HrmbqgWEVT04kkFH8owT98y48WvhPw5 LAdnEO1wyB9WsuU6KT6z+EZn8KGUWvj4nMu9jms/Jq6R6kMopQpKqDYUHsWaYdmONLpq 5i690hWsKOB7UuhNhbu72jevCrL5m7HR07FnoSUQ8SFUQVDkPMlLNjywarsn7fzhAIIy 7jZPEBhlvHmdfmUXk3333Zi8uckY2+qE41vfecqBf/vD39pSNkq2AbxVlSTQ8ngQhSxl aduw== 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:dkim-signature; bh=fUeCGzF2E/MJpwwWsgeCABFqdUqiBLxFGm1C7ZyiMnU=; b=G2UUWfzFCY4EOOFkwPRXsYYb2jOypItjkJs5pcIGXFuAM0hrFrPtiqqmEvjUjqoXaZ A7esVzku7rjFwvLFzu4Pg2t+ObXlh15CHsCP1erPefAHZ2UEwWZiOeu5kEhJKEutxDsH MmeCTW7+QGje5vGz4SswWwcTsQb4/xhTW+UaG1MOL3o8U3hSp/nVSvgfvGXiaxJASgh1 NeS9mx9IY55KIyfTy3dFPhcg4YAhQBdasv4wviOuNR+jzcEgbPCDdp9MNUy5r/5yo121 DhjOmsvvzyn2kNF0F5+Qbxdxo8mMFfZiIHw1bnHurtpodlU0xYZ0nuxnvFWqCJYvSHfM fs6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-03-30 header.b=CRVuBVEL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g191-20020a636bc8000000b0051a650b8f62si20827207pgc.836.2023.04.27.23.02.53; Thu, 27 Apr 2023 23:03:08 -0700 (PDT) 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; dkim=pass header.i=@oracle.com header.s=corp-2023-03-30 header.b=CRVuBVEL; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345257AbjD1F76 (ORCPT + 99 others); Fri, 28 Apr 2023 01:59:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229645AbjD1F75 (ORCPT ); Fri, 28 Apr 2023 01:59:57 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDCA62689; Thu, 27 Apr 2023 22:59:55 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33RKPCrD031856; Fri, 28 Apr 2023 05:59:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2023-03-30; bh=fUeCGzF2E/MJpwwWsgeCABFqdUqiBLxFGm1C7ZyiMnU=; b=CRVuBVELEk0nDpCg8ePKt3xe+lEoLxsuSnoHdkmKLnsG7OkE0+zM85dghSwtfjeH8Shq s/QonyhCHF4I+XCh9MJjYUHl2UEQ/iB7/zifzVvaS3mcBosBS2hdObzRZxlk07QNbD8r gPfwVAAiqzSW67NalW8304EbrjkbKnLiVK/10D3Grkl3zMzNcly7lLyVepyfWJjYw9Pk kDkA3F+/5mqSLo/hNUpCGcnmiFkvEgNmULzuIXfJXNmOy7qm3N2gqxVLpya7wsDFCntF ALRalrrV7eZ/w8VExUBJGZLb60N/UhCjF1F32MXAcxLZfYfcDKw1teNoslohauPMUXWD HA== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3q476u5e9b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Apr 2023 05:59:52 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 33S4XU2E008644; Fri, 28 Apr 2023 05:59:51 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3q461ahpm8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Apr 2023 05:59:51 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 33S5v0aN014799; Fri, 28 Apr 2023 05:59:50 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3q461ahpm4-1; Fri, 28 Apr 2023 05:59:50 +0000 From: Harshit Mogalapalli To: mcgrof@kernel.org, linux-modules@vger.kernel.org Cc: linux-kernel@vger.kernel.org, error27@gmail.com, kernel-janitors@vger.kernel.org, Harshit Mogalapalli Subject: [PATCH] module: Fix use-after-free bug in read_file_mod_stats() Date: Thu, 27 Apr 2023 22:59:33 -0700 Message-Id: <20230428055933.2699308-1-harshit.m.mogalapalli@oracle.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-28_02,2023-04-27_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 malwarescore=0 mlxscore=0 adultscore=0 suspectscore=0 phishscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304280048 X-Proofpoint-GUID: _VFUwZeukZMWnvtc5VMW4LkaK160HGLs X-Proofpoint-ORIG-GUID: _VFUwZeukZMWnvtc5VMW4LkaK160HGLs X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Smatch warns: kernel/module/stats.c:394 read_file_mod_stats() warn: passing freed memory 'buf' We are passing 'buf' to simple_read_from_buffer() after freeing it. Fix this by changing the order of 'simple_read_from_buffer' and 'kfree'. Fixes: df3e764d8e5c ("module: add debug stats to help identify memory pressure") Signed-off-by: Harshit Mogalapalli --- Found with statis analysis, only compile tested. --- kernel/module/stats.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/module/stats.c b/kernel/module/stats.c index ad7b6ada29f2..6ab2c94d6bc3 100644 --- a/kernel/module/stats.c +++ b/kernel/module/stats.c @@ -276,6 +276,7 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf, struct mod_fail_load *mod_fail; unsigned int len, size, count_failed = 0; char *buf; + int ret; u32 live_mod_count, fkreads, fdecompress, fbecoming, floads; unsigned long total_size, text_size, ikread_bytes, ibecoming_bytes, idecompress_bytes, imod_bytes, total_virtual_lost; @@ -390,8 +391,9 @@ static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf, out_unlock: mutex_unlock(&module_mutex); out: + ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); kfree(buf); - return simple_read_from_buffer(user_buf, count, ppos, buf, len); + return ret; } #undef MAX_PREAMBLE #undef MAX_FAILED_MOD_PRINT -- 2.40.0