From patchwork Wed Feb 15 11:18:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 9573897 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 54B14600F6 for ; Wed, 15 Feb 2017 11:18:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 454522843F for ; Wed, 15 Feb 2017 11:18:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A03528464; Wed, 15 Feb 2017 11:18:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3523B2843F for ; Wed, 15 Feb 2017 11:18:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751443AbdBOLSM (ORCPT ); Wed, 15 Feb 2017 06:18:12 -0500 Received: from mail-wm0-f47.google.com ([74.125.82.47]:35026 "EHLO mail-wm0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751359AbdBOLSL (ORCPT ); Wed, 15 Feb 2017 06:18:11 -0500 Received: by mail-wm0-f47.google.com with SMTP id v186so39236148wmd.0 for ; Wed, 15 Feb 2017 03:18:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=RIVeaH+bpk6ijz2+rHsIW4zPAl+QnqVRJ5LpF/cFRpA=; b=dBSnupBLgdQCIaYZLaJgtZ2Ip+H6q1vcV2gApJFcIdOApEcoHv1260LD1uJxrazYHH jNaYppaYZ38dK6IJQgK14GfjdVYGnewHljjOsmc+1yadVzAZCUA14TgwBrYMkCtfbtM2 sjogiRHCJ7k9ZcCZNKK3HwF8Bi3zbuHR8ZLrdc1M5hbx97+xuMt3IcrFT8PixUejdyPT GVofy6UMKPEoHbgfRaM7uE3ndw+k8tIj0peqvmybsq91V/diicWloJmvedBLAf2tdso2 F9hgdEsfFksU27W6EiC8gEQ0aEpRMnP5ScDH/kz7n4Ib68z5xXZw9KCq9l7W8VTB4MnV 9ODQ== 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=RIVeaH+bpk6ijz2+rHsIW4zPAl+QnqVRJ5LpF/cFRpA=; b=k8+dTgadu1wGzhm5hvSA6BqfHa+oNFbE24O1M5yXUjsoYOpZO/orG/LZOHqrGA9wSs tHnBAusoH47339tgsOs2eMDHO0fE+KSv/ekdzORdSBzkhbqNpi5akPHMxcYq5+Woz8/r ZRvCTS+2oDt81NvyOakVqDlimFykv5ZWML3z0LJUtYwPWak570JK1KGrwtCA5vPIHbLn /OrMHWc8wWNO0u8cHzDuiSBY0XM3PtK/JxYeyKyKmwulXBSxDNhPgtMdjpm06y363s81 h43bZxEV+nRWtCyOwwi0MMd2k8ktsdaaKs8m/X0IyBioxGqvdWvVQf+nu4RbIDzZvj4T 0t6Q== X-Gm-Message-State: AMke39ncH0mHc2GfORYTO3o+OUYyiyKLeHam7oQpgwrD/EyyKXmAMv4AgtwwjBWTHIDr5J03 X-Received: by 10.28.16.70 with SMTP id 67mr7936576wmq.142.1487157490029; Wed, 15 Feb 2017 03:18:10 -0800 (PST) Received: from localhost.localdomain ([90.63.244.31]) by smtp.gmail.com with ESMTPSA id 17sm4509034wru.16.2017.02.15.03.18.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 15 Feb 2017 03:18:09 -0800 (PST) From: Bartosz Golaszewski To: linux-modules@vger.kernel.org Cc: Bartosz Golaszewski Subject: [PATCH] module: fix a memory leak Date: Wed, 15 Feb 2017 12:18:03 +0100 Message-Id: <1487157483-12848-1-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP When a module is removed and re-inserted without unrefing, the kmod_file is unconditionally re-opened. This results in a memory and file descriptor leak. Fix it by checking if the file is already open in kmod_module_insert_module(). Signed-off-by: Bartosz Golaszewski --- libkmod/libkmod-module.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index bf6a8d6..57da0a2 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -833,10 +833,12 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, return -ENOENT; } - mod->file = kmod_file_open(mod->ctx, path); - if (mod->file == NULL) { - err = -errno; - return err; + if (!mod->file) { + mod->file = kmod_file_open(mod->ctx, path); + if (mod->file == NULL) { + err = -errno; + return err; + } } if (kmod_file_get_direct(mod->file)) {