From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A2D5223339 for ; Thu, 12 Jun 2025 10:48:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749725304; cv=none; b=R27pa3CPPbdaAotD7h06tzqZIFY2jNn5Cj/nn1IsdORaNsQmJygbzZZEOJ8EKOknCFSWLoANWAVwGOsepYf/Dkkt2rsY7/WiOjfU75RGhAJwm9pOp4pwgsx35xLbn+dYl9qe2j2VJzoWgwKPgcixXOpZVzGv9umK3Q0wkKK7ucs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749725304; c=relaxed/simple; bh=HNDqJvbrM3YoJTFiv4uqxRukmDC/kioQTcqiuAA2cac=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=E++7//TjKGmdoHkKnCclSpd9S47Th6ps5RIJnJVWzGf5K7gGnBgSMIRwiZu8YiTFEpHLKCPl27OpNICxIhzKDAp8DWwhEirOi5FodmmGBSlza8DLW3ZB3aiHw0lHvK7WJZabFcIjAy9rpg9gYKf0DUMszTAOVJP/+7dKCseNHb0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=VKGzRxSh; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="VKGzRxSh" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-3a54700a463so486515f8f.1 for ; Thu, 12 Jun 2025 03:48:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1749725301; x=1750330101; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=O9eVd27nL1eqHZbw4F9ds6pBpZa05pK5xdu04RGTqP0=; b=VKGzRxSh71/QF8p8wnUQ7PEGwC0EUpZkw7hv4LYxPDa7j4XDSOth5nk828JYDek1HH gyVQZfml7PkpMgWORHIN6L5J4O0dbGuWYWf3SdM6mHoN69ENGQC3uHa1nBqkq0siUJi3 5Hs6IsYvlv41WztZIOdtoOof0W+m9AvXIw20bVXixzeqjj04ZJIbyem3HTHuXpFUoJnh kocp75cCpvXWbWxRNgYl83NQdPsX8kN1A8iBNUh6QUK+C0M4Q+1QUx+csaDXMRxRAGKl RdBq+F4t5VAEuNsA2bNtnC3/F5zoQpcYX/F46KVzSg2QMSF0mA59uuKAnBlfYLBe2uMc tbUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749725301; x=1750330101; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O9eVd27nL1eqHZbw4F9ds6pBpZa05pK5xdu04RGTqP0=; b=FcBlBibvuGtLhPDaKdcx6J+YkpJsZyzdigGdL6ZoWeqJgdSYEZgul58j+c3tyomW8S jyRRyPn1dnJU+GerGSWtkM573aliSsziDmIvyTYjXc1NrNQSOa4vzrPQnp+PYJPs4ysU hHShi9+GKVt9SBq5N199u6qbvj8/97VKEY8PwSxhd4Nb2jiwiKkSgd9ZZCT9KWHYZxZ+ MGmPRMcco58T3fwNcL/E6byQ4mZZBjFxaRSR6hhNpsnDTzi7QBYYpaBewFhmPNw4tk/p zTpodRfQwB3Qf9Kd3ukNrrjidDoP8Rajv2qNycXb0AohIaC5AynW/d+Mx/NjQg9Fc47z 7/iw== X-Forwarded-Encrypted: i=1; AJvYcCXmWmaX5t9HdC8nOMSZhGRRHbUUqGDeeknaFKjvfzZcAP1a+he67dADeMAmFExPRmMRzWLAdaftFR0xnPo=@vger.kernel.org X-Gm-Message-State: AOJu0YwGl2z+1YQ87KCI6bFmJyR18o2yUIdQSUY2GHRyNT6KXT1CTV/C psp130HJGSiuVbwoMq20k+dkZFXuX3GSAEcoCCyrFrrvi9Oe6r7vt+HxYZyLk0mz/F0= X-Gm-Gg: ASbGncuMibqtL+q0emK6H9fCkBeMIw3w5gGBnI+sGDWwvErNEk8LyItyQw8VUfItGA2 uLz6AvdAg/+BqoaSgZUNFF82VCcX2HXXHr2RfpjnfkZj6Z+HensLipBLNj7hss43GSdh4ZmJxcm xYmQ36bGcwdXyo+uUwGyTOzFFAGAo9MUM4MvoWoJKM+dlqCAXGVN/1uRJo01PZ9JFl+V5UyVH2W Puan9JulWWseQlSnsZ9Nvc5qQXCdD6t+z9tfKBj+w8U81QhtsYnKVxwIx6WW49zrNckYPoH0KCP x7gTa9FPOeoCaozpRMuCeouis1wcWJRuw2IM8ygHSRW06Hc5HZX/TzPKf+PsIiLzJrRsBvxHNHo jXmXs X-Google-Smtp-Source: AGHT+IEaAyozngycLd0mG65hkJuHuQ/++UMGDBEwmcvUNmlN9HEgHszswErmF9JjhOS7OFHmrHQ5zA== X-Received: by 2002:a05:6000:3107:b0:3a4:ed9a:7016 with SMTP id ffacd0b85a97d-3a560827df3mr1956811f8f.26.1749725300756; Thu, 12 Jun 2025 03:48:20 -0700 (PDT) Received: from [10.0.1.22] (109-81-1-248.rct.o2.cz. [109.81.1.248]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a561976a20sm1655934f8f.19.2025.06.12.03.48.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Jun 2025 03:48:20 -0700 (PDT) Message-ID: Date: Thu, 12 Jun 2025 12:48:19 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] module: Fix memory deallocation on error path in move_module() To: Daniel Gomez Cc: Luis Chamberlain , Sami Tolvanen , Daniel Gomez , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org References: <20250607161823.409691-1-petr.pavlu@suse.com> <20250607161823.409691-2-petr.pavlu@suse.com> Content-Language: en-US From: Petr Pavlu In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 6/10/25 8:51 PM, Daniel Gomez wrote: > On 07/06/2025 18.16, Petr Pavlu wrote: >> The function move_module() uses the variable t to track how many memory >> types it has allocated and consequently how many should be freed if an >> error occurs. >> >> The variable is initially set to 0 and is updated when a call to >> module_memory_alloc() fails. However, move_module() can fail for other >> reasons as well, in which case t remains set to 0 and no memory is freed. > > Do you have a way to reproduce the leak? I was only able to test it by directly inserting errors in move_module(). > >> >> Fix the problem by setting t to MOD_MEM_NUM_TYPES after all memory types >> have been allocated. Additionally, make the deallocation loop more robust >> by not relying on the mod_mem_type_t enum having a signed integer as its >> underlying type. >> >> Fixes: c7ee8aebf6c0 ("module: add stop-grap sanity check on module memcpy()") >> Signed-off-by: Petr Pavlu >> --- >> kernel/module/main.c | 7 ++++--- >> 1 file changed, 4 insertions(+), 3 deletions(-) >> >> diff --git a/kernel/module/main.c b/kernel/module/main.c >> index 08b59c37735e..322b38c0a782 100644 >> --- a/kernel/module/main.c >> +++ b/kernel/module/main.c >> @@ -2614,7 +2614,7 @@ static int find_module_sections(struct module *mod, struct load_info *info) >> static int move_module(struct module *mod, struct load_info *info) >> { >> int i; >> - enum mod_mem_type t = 0; >> + enum mod_mem_type t; >> int ret = -ENOMEM; >> bool codetag_section_found = false; >> >> @@ -2630,6 +2630,7 @@ static int move_module(struct module *mod, struct load_info *info) >> goto out_err; >> } >> } >> + t = MOD_MEM_NUM_TYPES; > > Why forcing to this? I think we want to loop from the last type found, in case > move_module() fails after this point. Here's my suggestion: > > diff --git a/kernel/module/main.c b/kernel/module/main.c > index ada44860a868..c66881d2fb62 100644 > --- a/kernel/module/main.c > +++ b/kernel/module/main.c > @@ -2697,7 +2697,7 @@ static int find_module_sections(struct module *mod, struct load_info *info) > static int move_module(struct module *mod, struct load_info *info) > { > int i; > - enum mod_mem_type t; > + enum mod_mem_type t = MOD_TEXT; > int ret; > bool codetag_section_found = false; > > @@ -2708,12 +2708,10 @@ static int move_module(struct module *mod, struct load_info *info) > } > > ret = module_memory_alloc(mod, type); > - if (ret) { > - t = type; > + t = type; > + if (ret) > goto out_err; > - } > } > - t = MOD_MEM_NUM_TYPES; > > /* Transfer each section which specifies SHF_ALLOC */ > pr_debug("Final section addresses for %s:\n", mod->name) This seems to be off by one. For instance, if the loop reaches the last valid type in mod_mem_type, MOD_INIT_RODATA, and successfully allocates its memory, the variable t gets set to MOD_INIT_RODATA. Subsequently, if an error occurs later in move_module() and control is transferred to out_err, the deallocation starts from t-1, and therefore MOD_INIT_RODATA doesn't get freed. If we want to always start from the last type found, the code would need to be: [...] ret = module_memory_alloc(mod, type); if (ret) goto out_err; t = type + 1; } I can adjust it in this way if it is preferred. -- Thanks, Petr