Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1330362ybi; Fri, 24 May 2019 23:08:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqyvZjx98XEt8YXFLsR8mGntPFlg7ZmbbZxdKrmYV0FMR9IRRNj5aI4cvv4psZayi/03jYG2 X-Received: by 2002:a17:902:6948:: with SMTP id k8mr111891151plt.81.1558764519048; Fri, 24 May 2019 23:08:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558764519; cv=none; d=google.com; s=arc-20160816; b=q/NXYWIRXQANZCcHdu+ooReC59g9C9xr+DsxHAUKzflKeTfi67tVY0N+yIduXkUlMY pUCoA69w6htCgG/o5XCsT5T5JPvMAXC4dKRZL0JSJF+UjDF+KgEvspaKcIc7I6nsOP6Q oWHrm0rFU7UNppwLZEpysKny2PoLoe+Jl+R0hcgLcRaHigsblGvFw064fKJmizonNoan 3Jrc2LNipLC/Ww4NxfEXr4Ib18NZ6TR0SYq9v+TadC8Vh+6F4HtUfJHrUUGz78JP/ENM ZuhyVf5z/8wHsOZi09tQM9O8WeNt01pGYcZnYp0R+/qf+MH9tDSaBtHXzP8hIsXp7OMU lYAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=MM6OOzM4T+V4zslooatuonhXlfRCNs+JVKlRCY2343A=; b=tG/oBorREOna/f5JHPKbylYbs9k137yqjp7j1/QJ7ELEXTk+3c3pfhkv565sLsvEFi qPl+DHKyPq/jjedH1/lNKvEYNEJt16iDVMdTDubyuS48gdF3dDprCUVtTubh6MzccWZM u5F0omWdIXAUQq3NMReU5v92NAEbkyx+MLFNpuoT57UCMoraBi6Ntumukx0/QNsDYvfL 5XG4XcbLOipyHyTL71eQp9R/PJH+NelboYCGki9XA1MfNTMVdt3st6QdzglVrpp+cxxc Ps87djuwB6vfXyZzqufs2HnDITRFnIWczISqZdv2kAiVSYQ/MI8txVMWfsQvJ7REG7jN bXFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WsnbJVNy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h62si7012740pgc.585.2019.05.24.23.08.11; Fri, 24 May 2019 23:08:39 -0700 (PDT) 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=@chromium.org header.s=google header.b=WsnbJVNy; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726368AbfEYGE2 (ORCPT + 99 others); Sat, 25 May 2019 02:04:28 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:40733 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726145AbfEYGE2 (ORCPT ); Sat, 25 May 2019 02:04:28 -0400 Received: by mail-pl1-f195.google.com with SMTP id g69so4996193plb.7 for ; Fri, 24 May 2019 23:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=MM6OOzM4T+V4zslooatuonhXlfRCNs+JVKlRCY2343A=; b=WsnbJVNyUx6si4w8yZ0Coji5M1mNT8dklbTY1i/pqVceKyQ0JePsbV/qSYPbBPVIxC bLSmCxtYp+tJmRKdL1lKgG1ltIrK+cT59yyGaR1u/F2UfFv32+ce7+PxsQFsxb37WrjQ RAeh9YCIJeyVVI9ssqly5jzmOd0n4s8EcdFlo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=MM6OOzM4T+V4zslooatuonhXlfRCNs+JVKlRCY2343A=; b=A/OaNYlbfe9SZd2Ra125Z88jWrLHJxUp9uaSUuESpzsME3fSEMwMEamL1REeatlu1j F+Kzdlhhzobttg0eUwSLdY3INpGvZv0AeegGWwtx1U+U5AzfFCWa/+u2Z+XgWvl9YY5d FA/a+rkmWcgWviAAXtyWv6Cl7Iuu+XJEA2UeQT2PKEFFIFrFUf/GbSN7SVQpGPbYH+pZ iNEsKjIi/uRPre6dIic3qD2T0oiDEWoLM7wgOeImo91XOZsUZkKWwje0TiR4LVskpAXK aNzrl0UHQepzkfYuPtxQMK5kMSjmL4a0w+ynieKztwNmH2yyo6vsYl93mF+H9SslPu9N MUjw== X-Gm-Message-State: APjAAAV6Ajiiye4JJeoKr3+8FWrVaIix4smInGQno0QJsABJrGdsTZZn 0czpZOXAfXJ2cezC4s+HlgW/U9LAqU4= X-Received: by 2002:a17:902:2be8:: with SMTP id l95mr6541998plb.231.1558764267844; Fri, 24 May 2019 23:04:27 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id e184sm6631260pfa.169.2019.05.24.23.04.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 24 May 2019 23:04:26 -0700 (PDT) Date: Fri, 24 May 2019 23:04:25 -0700 From: Kees Cook To: Jon Hunter , Greg KH Cc: Gen Zhang , jslaby@suse.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] consolemap: Fix a memory leaking bug in drivers/tty/vt/consolemap.c Message-ID: <201905242302.139A912@keescook> References: <20190521092935.GA2297@zhanggen-UX430UQ> <201905211342.DE554F0D@keescook> <20190522015055.GC4093@zhanggen-UX430UQ> <201905221353.AD8E585E6D@keescook> <20190523003452.GB14060@zhanggen-UX430UQ> <201905230952.B47ADA17A@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 24, 2019 at 10:37:59AM +0100, Jon Hunter wrote: > Kees, > > On 23/05/2019 17:54, Kees Cook wrote: > > On Thu, May 23, 2019 at 08:34:52AM +0800, Gen Zhang wrote: > >> In function con_insert_unipair(), when allocation for p2 and p1[n] > >> fails, ENOMEM is returned, but previously allocated p1 is not freed, > >> remains as leaking memory. Thus we should free p1 as well when this > >> allocation fails. > >> > >> Signed-off-by: Gen Zhang > > > > As far as I can see this is correct, as it's just restoring the prior > > state before the p1 allocation. > > Are you sure this is correct? It looks like p1 is only allocated if > p->uni_pgdir[n = unicode >> 11] == NULL. Thanks, yes, I looked more closely, and this is wrong. It's probably fine to leave it as it was (since it appears to just be allocating on demand). If we really want to restore the state on failure, we also can't re-use "n", which is no longer valid. Here, I think, would be a complete patch to check for allocation and use a separate index for the other array: diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c index b28aa0d289f8..5f77cffc53b8 100644 --- a/drivers/tty/vt/consolemap.c +++ b/drivers/tty/vt/consolemap.c @@ -474,7 +474,8 @@ static int con_unify_unimap(struct vc_data *conp, struct uni_pagedir *p) static int con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos) { - int i, n; + int i, n, high; + bool p1_alloced = false; u16 **p1, *p2; p1 = p->uni_pgdir[n = unicode >> 11]; @@ -482,14 +483,22 @@ con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos) p1 = p->uni_pgdir[n] = kmalloc_array(32, sizeof(u16 *), GFP_KERNEL); if (!p1) return -ENOMEM; + p1_alloced = true; for (i = 0; i < 32; i++) p1[i] = NULL; } - p2 = p1[n = (unicode >> 6) & 0x1f]; + p2 = p1[high = (unicode >> 6) & 0x1f]; if (!p2) { - p2 = p1[n] = kmalloc_array(64, sizeof(u16), GFP_KERNEL); - if (!p2) return -ENOMEM; + p2 = p1[high] = kmalloc_array(64, sizeof(u16), GFP_KERNEL); + if (!p2) { + if (p1_alloced) { + kfree(p1); + p->uni_pgdir[n] = NULL; + } + return -ENOMEM; + } + memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */ } But, frankly, probably the patch should just be removed... -Kees > > I only mention this because I have seen a few patches from Gen today > regarding memory leaks and devm_kzalloc() that are not correct. > > > Reviewed-by: Kees Cook > > > >> --- > >> diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c > >> index b28aa0d..79fcc96 100644 > >> --- a/drivers/tty/vt/consolemap.c > >> +++ b/drivers/tty/vt/consolemap.c > >> @@ -489,7 +489,11 @@ con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos) > >> p2 = p1[n = (unicode >> 6) & 0x1f]; > >> if (!p2) { > >> p2 = p1[n] = kmalloc_array(64, sizeof(u16), GFP_KERNEL); > >> - if (!p2) return -ENOMEM; > >> + if (!p2) { > >> + kfree(p1); > >> + p->uni_pgdir[n] = NULL; > >> + return -ENOMEM; > >> + } > >> memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */ > >> } > >> > >> --- > > Cheers > Jon > > -- > nvpublic -- Kees Cook