Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4357453pxv; Tue, 20 Jul 2021 01:38:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzp4uxLsN0cNrAj8aFRJ5BQ2q5utroTbtxe5Vgyer/F50rid2CUe6oCRh0VhVEti/0VlNqy X-Received: by 2002:a5e:c109:: with SMTP id v9mr10480302iol.76.1626770318268; Tue, 20 Jul 2021 01:38:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626770318; cv=none; d=google.com; s=arc-20160816; b=nP8WK1YRYHd2no6W9rFm8IQIhzLL0X+oKvlfBRdnBrZrXVXrJMsE1FKDTA6Ju7Ewwk UPAd100fXxraja/70CQj1erkJ0q7qGrSdJ66e/nVyku++2GEOz0cP9kPOgrfyM1lC/Ng 8PSXFuwQ05JK5lZuE/s7OplTKkqhTrlsDYdpl9WLhI2BqFYhDJXuKJuaaCoJuOZXqoyg MOFgwmQCPcr4ol4Dm30+P2R51omnFW/884AYW6+3S2CFKgn2n6Wp37/ekrKJAqkVmW1c tEG+O+g+/8EOJ0UC+tTuv0s6JGaCDWeMyyT5Av9e6xWr4cREyvcTOs0L4u3vBvuO79/0 +rxg== 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=Qe6v+j+gwpyLkSOCL4a327U+Pch1W2v5U1bch0lG4Jg=; b=ZuQ+/0jgm3OoPD+nRRTA+7Wzr5Llimsm3BlFTLwQtLo75KBAAWOOxoPa58wujrInbc L33ldolcoBMtq8138NAIWDlnbJhbAp9vgdacwj5Zehjuh1CapfG6MWF9GGR3F9RuDvJm KEF7e6C7BeHTInZNfAxqOg2s2n0x2ieMHebntJ6P013Gbje4qutZhnxstKkNoBj4De3E BDn6PzlXqIUfb0y75HIthHQZrBIRdN/IYIxwAR6bFnKFfNDanivyoYlDclMiWRDaahYM wCreuJ+KVZTVMp256HU5fVZUYJ3VMXnkMyuma1BOegOwjDip7XKybBsiQ1CWCe4WpYkk jDTA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si23176464iol.30.2021.07.20.01.38.27; Tue, 20 Jul 2021 01:38:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234725AbhGTHzN (ORCPT + 99 others); Tue, 20 Jul 2021 03:55:13 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:12279 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234547AbhGTHzB (ORCPT ); Tue, 20 Jul 2021 03:55:01 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4GTX3d0Qf8z7wDb; Tue, 20 Jul 2021 16:30:53 +0800 (CST) Received: from dggpeml500017.china.huawei.com (7.185.36.243) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 20 Jul 2021 16:35:28 +0800 Received: from huawei.com (10.175.103.91) by dggpeml500017.china.huawei.com (7.185.36.243) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 20 Jul 2021 16:35:27 +0800 From: Yang Yingliang To: , CC: , Subject: [PATCH] io_uring: fix memleak in io_init_wq_offload() Date: Tue, 20 Jul 2021 16:38:05 +0800 Message-ID: <20210720083805.3030730-1-yangyingliang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500017.china.huawei.com (7.185.36.243) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I got memory leak report when doing fuzz test: BUG: memory leak unreferenced object 0xffff888107310a80 (size 96): comm "syz-executor.6", pid 4610, jiffies 4295140240 (age 20.135s) hex dump (first 32 bytes): 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00 .....N.......... backtrace: [<000000001974933b>] kmalloc include/linux/slab.h:591 [inline] [<000000001974933b>] kzalloc include/linux/slab.h:721 [inline] [<000000001974933b>] io_init_wq_offload fs/io_uring.c:7920 [inline] [<000000001974933b>] io_uring_alloc_task_context+0x466/0x640 fs/io_uring.c:7955 [<0000000039d0800d>] __io_uring_add_tctx_node+0x256/0x360 fs/io_uring.c:9016 [<000000008482e78c>] io_uring_add_tctx_node fs/io_uring.c:9052 [inline] [<000000008482e78c>] __do_sys_io_uring_enter fs/io_uring.c:9354 [inline] [<000000008482e78c>] __se_sys_io_uring_enter fs/io_uring.c:9301 [inline] [<000000008482e78c>] __x64_sys_io_uring_enter+0xabc/0xc20 fs/io_uring.c:9301 [<00000000b875f18f>] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [<00000000b875f18f>] do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80 [<000000006b0a8484>] entry_SYSCALL_64_after_hwframe+0x44/0xae CPU0 CPU1 io_uring_enter io_uring_enter io_uring_add_tctx_node io_uring_add_tctx_node __io_uring_add_tctx_node __io_uring_add_tctx_node io_uring_alloc_task_context io_uring_alloc_task_context io_init_wq_offload io_init_wq_offload hash = kzalloc hash = kzalloc ctx->hash_map = hash ctx->hash_map = hash <- one of the hash is leaked When calling io_uring_enter() in parallel, the 'hash_map' will be leaked, add uring_lock to protect 'hash_map'. Fixes: e941894eae31 ("io-wq: make buffered file write hashed work map per-ctx") Reported-by: Hulk Robot Signed-off-by: Yang Yingliang --- fs/io_uring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 0cac361bf6b8..63d3a9c2a2a6 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -7899,15 +7899,19 @@ static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, struct io_wq_data data; unsigned int concurrency; + mutex_lock(&ctx->uring_lock); hash = ctx->hash_map; if (!hash) { hash = kzalloc(sizeof(*hash), GFP_KERNEL); - if (!hash) + if (!hash) { + mutex_unlock(&ctx->uring_lock); return ERR_PTR(-ENOMEM); + } refcount_set(&hash->refs, 1); init_waitqueue_head(&hash->wait); ctx->hash_map = hash; } + mutex_unlock(&ctx->uring_lock); data.hash = hash; data.task = task; -- 2.25.1