Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4985254ybc; Tue, 26 Nov 2019 18:50:52 -0800 (PST) X-Google-Smtp-Source: APXvYqxqUK65Z3Ehc6+CkWhx3q9ZVI13lMRVTWGjiQukR54pnHnamsKp+GqKceEwsoJyjflg6Wxk X-Received: by 2002:a17:906:4019:: with SMTP id v25mr47645456ejj.11.1574823052415; Tue, 26 Nov 2019 18:50:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574823052; cv=none; d=google.com; s=arc-20160816; b=cv6siVFwceqymP2wn6rvWJGlKZ2A2xJ7Q38/nK9n9RZpS5kiN1DgxSCn0q+JsCZjrG 8RZ3R1uL5ygDeZmZKfOeH2LdOcu/jsrr/7NpFVrsX6o4yJl8/ZzHqev0m2WXtgXMD57i oFoMJrP8JmT+lsEbX4uEpT+/Cud9UhU/U19/ofNAF6g5NdPEe9EPdRWsk0Vi7Xecbe+S Yfzlt2zYWJ7YDDktzXRKWy5rQtQg8BAUrlVSGpCybnHcgllMjd8JoiU3gg9Q3gvgFAaT sXXQshhYpFgLBFpxqXgS+7ffJ2mJg9qhiu1YJIYbQ5jIikT1jFLra0udU8sHInk7GGRI nbSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=745A+eCsEg1muKCYSg2p/wtTP4Y+wsxmjId5exHGtfo=; b=ltVv6On5vYBaPB3aaqGZ+JaEx77OWhjGWHncr8mYXI/Xjiz55ot5/pPB37qXCwer5J 0VAGvQU+huwX8nD6LmzsNozm2/zlEDbnQptI/bkxp0i5Fg2vFAQHfYxwKJpB9LMK6phh Q5tSZBz3rbi/g6Gd1WELt3ahF4VSyEJONj8csNcqTIvh2HB7tvrVT5HMgc5MdkRrNKOu ta21aLRbG8TVwmp/Qz4wQARGSarhVtgSOFkjkjwat5a7HL19n3JQoJSSGNW/CnMSF9JC 6iZ95rgRZ7olkHOmwnaeNz0hxu7u7AipOlA1wWilaCSeox9uIz6mFVDosw7K/LLAqpGN aIAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sslab.ics.keio.ac.jp header.s=google header.b=SMgYIVyN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j1si3371706edh.399.2019.11.26.18.50.28; Tue, 26 Nov 2019 18:50:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sslab.ics.keio.ac.jp header.s=google header.b=SMgYIVyN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726975AbfK0CrM (ORCPT + 99 others); Tue, 26 Nov 2019 21:47:12 -0500 Received: from mail-pj1-f65.google.com ([209.85.216.65]:45591 "EHLO mail-pj1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726304AbfK0CrM (ORCPT ); Tue, 26 Nov 2019 21:47:12 -0500 Received: by mail-pj1-f65.google.com with SMTP id r11so1380342pjp.12 for ; Tue, 26 Nov 2019 18:47:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sslab.ics.keio.ac.jp; s=google; h=from:to:cc:subject:date:message-id; bh=745A+eCsEg1muKCYSg2p/wtTP4Y+wsxmjId5exHGtfo=; b=SMgYIVyNSzhZH4ald3Xy3UtcVJF+AiXdjJnAZ68iotYqDWhZho5ZFo2BF//Sj/1kEO 0RjE3/EpMLC4qRPsA8j+HZ06MCtN6+2it3lpenCl61EnBaYMN8m+tOv7eoQlej63dudM N4F8n0IZIw9bQrXXBOzXzxtxptLLvBD1IlGVo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=745A+eCsEg1muKCYSg2p/wtTP4Y+wsxmjId5exHGtfo=; b=KPVqTzz5TGC5OouTj5oMPs+uEmk8NCaQ/DGcVbHShdBFaowgPyOVy7NFVkPzEg1blL m3clY2Tn1TrdPpTr0NlmSTsOavVS7LkwoZhY41mD605fxDNn+RQyAVDTeboeVP13QFt1 Xh6JHRV+lYghqP9I/IMJLfi066sMh0UrAthz2RlvDQN+NbER7W9ugqk/XFtOEC3m6hta P8okMgbcqRCiFrC+N/Nl1t+T99HjTjBnaQCAIDwx7mgnq7FNBjrstM96hkvaL7rrRZU/ wPXCZipxQBuKNFKjqDZtbLdNpTTrdooQdinTBwccV1YqZGHPFY5/lCAnYhge402P/Rz5 QkQA== X-Gm-Message-State: APjAAAVaCc3YtTn5kwOo6h591s1B0inif+YVU9bw8y8k+aULw11QRw0T 4k17NPYnf+vagID+HEkvlposFQUHKy1OsXAm X-Received: by 2002:a17:902:d205:: with SMTP id t5mr1546245ply.31.1574822829825; Tue, 26 Nov 2019 18:47:09 -0800 (PST) Received: from brooklyn.i.sslab.ics.keio.ac.jp (sslab-relay.ics.keio.ac.jp. [131.113.126.173]) by smtp.googlemail.com with ESMTPSA id w15sm13416137pfi.168.2019.11.26.18.47.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Nov 2019 18:47:09 -0800 (PST) From: Keita Suzuki To: Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, keitasuzuki.park@sslab.ics.keio.ac.jp, takafumi.kubota1012@sslab.ics.keio.ac.jp Subject: [PATCH] block/genhd: Fix memory leak in error path of __alloc_disk_node() Date: Wed, 27 Nov 2019 02:40:57 +0000 Message-Id: <20191127024057.5827-1-keitasuzuki.park@sslab.ics.keio.ac.jp> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 'disk->part_tbl' is malloced in disk_expand_part_tbl() and should be freed before leaving from the error handling cases. However, current code does not free this, causing a memory leak. Add disk_replace_part_tbl() before freeing 'disk'. I have tested this by randomly causing failures to the target code, and verified on kmemleak that this memory leak does occur. unreferenced object 0xffff888006dad500 (size 64): comm "systemd-udevd", pid 116, jiffies 4294895558 (age 121.716s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<00000000eec79bf3>] disk_expand_part_tbl+0xab/0x170 [<00000000624e7d03>] __alloc_disk_node+0xb1/0x1c0 [<00000000ca3f4185>] 0xffffffffc01b8584 [<000000006f88a6ee>] do_one_initcall+0x8b/0x2a4 [<0000000016058199>] do_init_module+0xfd/0x380 [<00000000b6fde336>] load_module+0x3fae/0x4240 [<00000000c523d013>] __do_sys_finit_module+0x11a/0x1b0 [<00000000f07bba26>] do_syscall_64+0x6d/0x1e0 [<00000000979467fd>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Signed-off-by: Keita Suzuki --- block/genhd.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/genhd.c b/block/genhd.c index ff6268970ddc..8c4b63d7f507 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1504,6 +1504,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) */ seqcount_init(&disk->part0.nr_sects_seq); if (hd_ref_init(&disk->part0)) { + disk_replace_part_tbl(disk, NULL); hd_free_part(&disk->part0); kfree(disk); return NULL; -- 2.17.1