Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp407179ybm; Wed, 22 May 2019 05:22:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3GreEKo3Tvz415m4D1ACbEgFqIvrJl90EO54vhPDjlR2NI/xuMhXzhVqZTdWorsk0PeQo X-Received: by 2002:aa7:8652:: with SMTP id a18mr93385246pfo.167.1558527761478; Wed, 22 May 2019 05:22:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558527761; cv=none; d=google.com; s=arc-20160816; b=b/s9HG+GlO1kxmAtY7GLZFBJxfdPV4uLzNkTuwFsPWJK/yF+eTr4wuDezFmPT+TAb9 vqNspdJionUFJA7hTonBdPSw8dLpmroKO3aUVLVP7SJfFYb4paJEslIKXer8Hht3Rudv quTvGam7cpXN+9ffFI1UYgJwtHoW/+weO/6k9fHuyvXRBa/oFZHRAH/uo+N2lchyacYk /i51k8WzmNEydlc2y+5owPI4apHEua7IfY6XTVtPCECC75PD/MnutLRC4DTHA3nw0Kty RTIfOuGjPsxe/a7PVVPrMWHaTYCaiZs2AzmRLnHEahYlqLfgy8QgfPYfXB72SdWfKGwI PeZA== 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=jWsOvlcYOxIXtOpraIA5T9PctmV/EptPwztKFrzGvOo=; b=FdLF1TXDkQ1TgmOv7KtBCVzt4fuoWSsHBTnhJvjjppaclvDbnOqdVu5e9qFl75x4qy xsvI/DJtmBvGiaCuvDIzMEoosqvn2FGnErH5Pu3ZBVBEOsM3Za1b+lp/WKDSIUqWYUcV fSxX0VvAcGcBYOp/waRs+Lv1LJv2pYdyzBudU4F8eR2fcEVXZorgWdItleDzf11scWe5 e2HnBWz4/UAI7nthuYTumZ2AKdEVzaYMCDwuM2OXGaFbi9+5X/uwAbXj0Bm99CF4bkry Dz0y0B1h96oNNn1ij3my/kGkSx9ViqbQtrRPS8vVr5Xckqvc9II658ZMU+UhEy+3A5nC BsBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Dfnrzugz; 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 s21si21077930pgm.154.2019.05.22.05.22.26; Wed, 22 May 2019 05:22:41 -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=Dfnrzugz; 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 S1729371AbfEVMT2 (ORCPT + 99 others); Wed, 22 May 2019 08:19:28 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39367 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728438AbfEVMT2 (ORCPT ); Wed, 22 May 2019 08:19:28 -0400 Received: by mail-pf1-f196.google.com with SMTP id z26so1240593pfg.6 for ; Wed, 22 May 2019 05:19:28 -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=jWsOvlcYOxIXtOpraIA5T9PctmV/EptPwztKFrzGvOo=; b=DfnrzugzDyXh/iZid72Iz9u1Xl4oN9sE8R33qPGtvV7cHxMsGly8x1uuU1r+HX8DOE Hz1H5LSs4X+g9e5wUixU1Zx0ijMoeFC8/SIyd3CNbZpgSvBMXUMKJiIHTSyYQ8Wh7csd iIk06QwmoYcdL92po2NOdoXm1iIhzQrEpmYf+FVB3kGYK95o3kysJm4OpW89GdnGqwS9 InWVtpXJEjgpcsdiesL07B0rWyGcqZOUwE3Zmf7VvZOl0VCwRjMRvb90yRwVKw4D3fBV pDTXDEo1Q2YkCQOAFX4PQg1kwvAlCoTs+UTxFWEGtfSXZivYqmeAn6nXtdz2j8sw5BGL Oshg== 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=jWsOvlcYOxIXtOpraIA5T9PctmV/EptPwztKFrzGvOo=; b=JPuQbT+nQeQv8WPIfcTGqhLspRKDmIwkqEFet8YLWzKUmMlDdXdBHtcGelhBgz7x/i oTiEcZwNUfurPgc9XwBEatYTgEbLYVf1mJNrI0/LhOzaxcpEtV/O1X81Ru0vQDjX2rU2 DXLMuseTtvo4G4lQv83jcblJmMgU94LSE/DCkQS0mup30WqlvTOpwp7gS0DngFYV+Bh2 d0SGf1+XerBViTxRJ1CC3aNcbWScAStvcQEyay+ALBmvREDSLUTNyqlKmDmHWd9XJpLK nn3x1tlYpP973/WmYrH4J1f3rF+16jcOAOX+b/tt5jNlxr6KbmLc5En83EfDTC9PPdOA YZHw== X-Gm-Message-State: APjAAAXGOalpRj8qXBvP32vWrWs1ZSezoypyYJwaI5QiZ3CCYbDfoqDC VK7J32bdYFFeQlD/QDEovlsSyiBn01LrXQ== X-Received: by 2002:a62:6341:: with SMTP id x62mr94524316pfb.63.1558527568070; Wed, 22 May 2019 05:19:28 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id v23sm31403926pff.185.2019.05.22.05.19.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2019 05:19:27 -0700 (PDT) Date: Wed, 22 May 2019 20:19:08 +0800 From: Gen Zhang To: Nicolas Pitre Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3] vt: Fix a missing-check bug in drivers/tty/vt/vt.c Message-ID: <20190522121908.GA6772@zhanggen-UX430UQ> References: <20190521022940.GA4858@zhanggen-UX430UQ> <20190521030905.GB5263@zhanggen-UX430UQ> <20190521040019.GD5263@zhanggen-UX430UQ> <20190521073901.GF5263@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 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, since the allcoation is in a loop, we should free all the allocated memory in a loop. Signed-off-by: Gen Zhang --- 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; + } + console_unlock(); + return -ENOMEM; } console_initcall(con_init); ---