Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1429859ybi; Sat, 8 Jun 2019 09:31:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqwWdCizSne/dfartKKE2+9eYx0rZTtDbVltuQXHOg+0sWH6yCv5REwZ24ixX4GxeI6ixYgJ X-Received: by 2002:aa7:9203:: with SMTP id 3mr66345435pfo.123.1560011517720; Sat, 08 Jun 2019 09:31:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560011517; cv=none; d=google.com; s=arc-20160816; b=Cm3q2U8j/UqGx2Jzi8E44uWhzHPcIV4LWSTXiPBag8G4VX8mHXqx7hc7gOS+wbonHr 1hSOpeies6N6dpnkRdzp+3ZnRh2sJufknaDq4ls6K297t3lOOKVIrYWBBercu/B3YO4G cl9ok9xHciVC7Kt1+ezqANgGhq5ys3WCSNXfgIWK1GUE/38ZXm++r+4gtgjiIPEKGavm ile/zcVME+n+y9KhcjSsAqTXwGj5tSKHwmyWO1hoNAxTiN8gnw/QWHaJaMBggaBkIeBP C0XZtBHJs2g53zcNd3kLaXtjCspCZkjVVZYUpzmU9Xj+q9PWvqvDxFcmPxKGg03xaiqd J+cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=quef0F8aYAG9qAFtSauA9W3OmlRfJN6ds/RIeMlxias=; b=hjuQ8xxYFVtsdeWmqPuPMG+Bdnjye75LPB95kaxSvFazzJ+/OBd4+fy585DUmr7Qy3 F/HncuiYjF0vkzo4xq1rCmYUt3uVoip+9WMX1n4RuNe9BhzK9i57y9RQgXDfR46Jsm1Q lBkEmjNlAia3dXyi5ms0UsXu/WC0fimDI1e6Rq3P1hk54oTYX/x4qSD7BwwYqbbk+CV0 deutGZOz4XW5wZh8LbX9kVCmCh1LUWZiyrk02qI6L5xali7Y6MSdg5OUSALiUWgdfR2b 36/SOVtwFzurlndodIVT7Yh7nMbjOmUjMA1AAFr+xKaNW9MPP3QoS95JAZ5L5m2UFs7S //Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WekMWgr9; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f17si4779143pgv.338.2019.06.08.09.31.41; Sat, 08 Jun 2019 09:31:57 -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=@gmail.com header.s=20161025 header.b=WekMWgr9; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727274AbfFHQaT (ORCPT + 99 others); Sat, 8 Jun 2019 12:30:19 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:38288 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727160AbfFHQaT (ORCPT ); Sat, 8 Jun 2019 12:30:19 -0400 Received: by mail-wr1-f67.google.com with SMTP id d18so5075008wrs.5 for ; Sat, 08 Jun 2019 09:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=quef0F8aYAG9qAFtSauA9W3OmlRfJN6ds/RIeMlxias=; b=WekMWgr9afHZFY7MNYUSdPYDetpcsmnQ/USAhexWt9lVitfqi2Oj7eXCd6Vc0Ty4rp idtj+AKzJXxQPoEvmnzoLBre3I1jvPEJeFRajvxi/kTXTWu7C5zF5ENh+iMMys8WvDQV QeFTGKDiX0SgyPRI0d9263ErcyxL+jsWu+ZLHRUTWyIfzfLIWb6ugjSAEN9ihJPC3VCq ZW41zVrdqid2wQL3Cp7Nxw487FFFKtzh+M6+Kc8kMIpNmKWdheaNcf9ejv91mAPbbMcy n5Iuba1/SpmvhD4wZuXBDAUnzPTVVCTdN+IztemmS/sxgIP0CdecgzW/GXMUE7QtZ/BX i6oQ== 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:user-agent; bh=quef0F8aYAG9qAFtSauA9W3OmlRfJN6ds/RIeMlxias=; b=grHAM8sAZ17AmTnbxZ3Ct/I5ygyOqsipPoTEnj2fti4wpWv2tFmPAIAsUd5/FDmKKy 4+uWql+yyg4QnNUe3028AdfaqA7i7s2ODpV9tof5QluUxhxEkuxdEZHKuT5HNeYM+17X ScX+JWcs/hgpeEP8yGy54odGNjfBNcef+vE1zqMDFtNqceJI+IVaAEU7C1JgrOsSBS8D jUGM2kmUDQNmJ462rnpRvxdEl3dw3rlSTrnHnesLhWFmOpfWVdlxfR4t0Wy5L2zPC3sG ub6SXyhMyhI0PxMTRyGXuV9n5nSyIlX5UnMx64UKQHdESfZMqYslU3rgg32k1iukBx1Z iazg== X-Gm-Message-State: APjAAAVdisSVKnKLUZgjKSSdazPhceQMxR2kd/H8DGhAth4eFMJR54aq sWZS29MvqiPLj+sGFI8t8Kc= X-Received: by 2002:adf:8183:: with SMTP id 3mr24236839wra.181.1560011417538; Sat, 08 Jun 2019 09:30:17 -0700 (PDT) Received: from zhanggen-UX430UQ ([108.61.173.19]) by smtp.gmail.com with ESMTPSA id t63sm9558269wmt.6.2019.06.08.09.30.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 08 Jun 2019 09:30:16 -0700 (PDT) Date: Sun, 9 Jun 2019 00:30:07 +0800 From: Gen Zhang To: Greg KH Cc: jslaby@suse.com, nico@fluxnic.net, kilobyte@angband.pl, textshell@uchuujin.de, mpatocka@redhat.com, daniel.vetter@ffwll.ch, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] vt: Fix a missing-check bug in con_init() Message-ID: <20190608163007.GA2492@zhanggen-UX430UQ> References: <20190528004529.GA12388@zhanggen-UX430UQ> <20190608160138.GA3840@zhanggen-UX430UQ> <20190608162219.GB11699@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190608162219.GB11699@kroah.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 08, 2019 at 06:22:19PM +0200, Greg KH wrote: > On Sun, Jun 09, 2019 at 12:01:38AM +0800, Gen Zhang wrote: > > On Tue, May 28, 2019 at 08:45:29AM +0800, Gen Zhang wrote: > > > In function con_init(), the pointer variable vc_cons[currcons].d, vc and > > > vc->vc_screenbuf is allocated by kzalloc(). And they are used in the > > > following codes. However, kzalloc() returns NULL when fails, and null > > > pointer dereference may happen. And it will cause the kernel to crash. > > > Therefore, we should check the return value and handle the error. > > > > > > Further, since the allcoation is in a loop, we should free all the > > > allocated memory in a loop. > > > > > > Signed-off-by: Gen Zhang > > > Reviewed-by: Nicolas Pitre > > > --- > > > diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c > > > index fdd12f8..d50f68f 100644 > > > --- a/drivers/tty/vt/vt.c > > > +++ b/drivers/tty/vt/vt.c > > > @@ -3350,10 +3350,14 @@ static int __init con_init(void) > > > > > > for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { > > > vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT); > > > + if (!vc) > > > + goto fail1; > > > INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); > > > tty_port_init(&vc->port); > > > visual_init(vc, currcons, 1); > > > vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); > > > + if (!vc->vc_screenbuf) > > > + goto fail2; > > > vc_init(vc, vc->vc_rows, vc->vc_cols, > > > currcons || !vc->vc_sw->con_save_screen); > > > } > > > @@ -3375,6 +3379,16 @@ static int __init con_init(void) > > > register_console(&vt_console_driver); > > > #endif > > > return 0; > > > +fail1: > > > + while (currcons > 0) { > > > + currcons--; > > > + kfree(vc_cons[currcons].d->vc_screenbuf); > > > +fail2: > > > + kfree(vc_cons[currcons].d); > > > + vc_cons[currcons].d = NULL; > > > + } > > Wait, will that even work? You can jump into the middle of a while > loop? I felt like the same when I saw reviewer's advice to write in this way. But I tested, and it did work. > > Ugh, that's beyond ugly. And please provide "real" names for the > labels, "fail1" and "fail2" do not tell anything here. Sure, I can revise that if needed. Thanks Gen > > Also, have you actually be able to trigger this? > > thanks, > > greg k-h