Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp602174ybm; Mon, 20 May 2019 23:48:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqyxp6WT5Wta2YpcsTx7vMQstg+/CF/gdx+JTBquUB0cIUgN3iiB+GauSW9aiO8LToVxDbrk X-Received: by 2002:a17:902:b58a:: with SMTP id a10mr50932371pls.83.1558421296865; Mon, 20 May 2019 23:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558421296; cv=none; d=google.com; s=arc-20160816; b=Fom6s74TzZ5DqWSGQHVK30WYFEg3zW4PG5zA//V6v0XFrBoZbyA8YujyuR9VoBVBSH b0pYCjKV29iHc6XqUFkQIPt0cuS04RHCprOsXJBdM2ZkaB+wK9S2jNHZLr/pV1gMVg95 CPpreTQhG1gb9Scehf/k6exUQJwRXSPU89V9bAaX8kAcpRbwiUx0V97tqTQOEqphP+C2 MVb8E1nvG0Hr2YDdSuEXHoSftHLUl8KGYIirCbDLooHj/ELksBT2oIcifovgIcY4MpfA hVfgqSPAkTd+D77Xnl7ELwij74fUdSjCYdfJKKjDR0WTw3hKwiREWfj7r05Ffbq7166H AhAw== 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; bh=hgpTLFoEBK0qAp/AMPVLKVpFtjZQiXWSFLACx+mQKDU=; b=b2izPP5J2DY73yabsmH9KNuvJT09IaIL+UoInjnlyR8O8RAuF/mX5twXNWcTpF4PBK xQgsvE4Kjy8riuPLxMH2zkCYhu+uZqEVQW2poSsPM2M/c+BioGth/g4ClWngZXFNz2+1 AUFUwW31FMIfht46LpLtOKe5LiB4JJhM3KOhKgw2NdnC4DBWhr2Z8euXOAMgQjk8mW3B /8fT7LODmb/dZ5G7Z9Xem2cFjaIq1mDG5dGjekR3JaewD+th7iIAAD7h/TbtG57rHBHV SfNlOvqNpwygzHGcmuD7grkBxbcateGRAUI5g0J90LOgWU3GjdhlkEgbVZoN0WT1Uhdw d7JQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w190si18621979pgd.577.2019.05.20.23.48.01; Mon, 20 May 2019 23:48:16 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726633AbfEUGqL (ORCPT + 99 others); Tue, 21 May 2019 02:46:11 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:37579 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726318AbfEUGqK (ORCPT ); Tue, 21 May 2019 02:46:10 -0400 Received: by mail-wm1-f66.google.com with SMTP id 7so1585385wmo.2 for ; Mon, 20 May 2019 23:46:09 -0700 (PDT) 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=hgpTLFoEBK0qAp/AMPVLKVpFtjZQiXWSFLACx+mQKDU=; b=Qv8TMK38a13yFG8UTm8Zd/Obcwp9Pn6/wD/hZrsvNkuQb5GTQXT/wuVYIsMp2vKRft I50LWTxPHs6mT9OCJF6z0Vko1VhE91VazOv0/feJfqdzdXuwYEW+aaB7+IltKnHXU/nz gGGSS6V1Fal3UZ//hJWlOnfp/4pkSAsw8Qp6Ctp/VqCeC1odN6bKiN0i2FTWbsx5WoFC otcbrtIF1ZFPsbYZ+OFtTYSQBFA2lCqXc2T/uCF3nEyQyotuaUcOrQdLCuTxgU2oSFj1 lb05mj/nyVs8HwvMUIAzOvaUW6fGKgp9PjaUHEwMj99LKoNS76Q62aS6ZTM5Tjll9ArX wlxw== X-Gm-Message-State: APjAAAVjxKNSst8edBBavPhjLPFoTEuT/lkFZWROAkwE8KZ0MORjfLlj FHHvkqx44Jw/8gUGYPoDcEjJzA== X-Received: by 2002:a1c:20d7:: with SMTP id g206mr1996089wmg.136.1558421169069; Mon, 20 May 2019 23:46:09 -0700 (PDT) Received: from localhost (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id a11sm20586018wrx.31.2019.05.20.23.46.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 20 May 2019 23:46:08 -0700 (PDT) Date: Tue, 21 May 2019 08:46:07 +0200 From: Oleksandr Natalenko To: Gen Zhang Cc: Nicolas Pitre , linux-kernel@vger.kernel.org, Grzegorz Halat Subject: Re: [PATCH v3] vt: Fix a missing-check bug in drivers/tty/vt/vt.c Message-ID: <20190521064605.ho7tdwaagt4wow4v@butterfly.localdomain> References: <20190521022940.GA4858@zhanggen-UX430UQ> <20190521032118.GC5263@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190521032118.GC5263@zhanggen-UX430UQ> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Cc'ing Grzegorz. On Tue, May 21, 2019 at 11:21:18AM +0800, Gen Zhang wrote: > On Mon, May 20, 2019 at 10:55:40PM -0400, Nicolas Pitre wrote: > > As soon as you release the lock, another thread could come along and > > start using the memory pointed by vc_cons[currcons].d you're about to > > free here. This is unlikely for an initcall, but still. > > > > You should consider this ordering instead: > > > > err_vc_screenbuf: > > kfree(vc); > > vc_cons[currcons].d = NULL; > > err_vc: > > console_unlock(); > > return -ENOMEM; > In function con_init(), the pointer variable vc_cons[currcons].d, vc and > vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are > used in the following codes. > However, when there is a memory allocation error, kzalloc() can fail. > Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) > dereference may happen. And it will cause the kernel to crash. Therefore, > we should check return value and handle the error. > Further,the loop condition MIN_NR_CONSOLES is defined as 1 in > include/uapi/linux/vt.h and it is not changed. So there is no need to > unwind the loop. > > Signed-off-by: Gen Zhang > > --- > diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c > index fdd12f8..ea47eb3 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 err_vc; > 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 err_vc_screenbuf; > vc_init(vc, vc->vc_rows, vc->vc_cols, > currcons || !vc->vc_sw->con_save_screen); > } > @@ -3375,6 +3379,13 @@ static int __init con_init(void) > register_console(&vt_console_driver); > #endif > return 0; > +err_vc_screenbuf: > + kfree(vc); > + vc_cons[currcons].d = NULL; > +err_vc: > + console_unlock(); > + return -ENOMEM; > + > } > console_initcall(con_init); > --- -- Best regards, Oleksandr Natalenko (post-factum) Senior Software Maintenance Engineer