Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp2531626ybm; Thu, 23 May 2019 19:29:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqwEb2DQJY7Oxbef82kdPhZ/OLlUaOlX31KsHY3Yc4lba2RTm8BR+CE94icsDEvVh6dg0LqS X-Received: by 2002:a17:902:9348:: with SMTP id g8mr60328459plp.174.1558664987398; Thu, 23 May 2019 19:29:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558664987; cv=none; d=google.com; s=arc-20160816; b=ix9IQF4eJRhVXKbQsMCbQvzXz2j8hkrDfIOfjflqGifOx2U6Apk35ExcWLFhjAQ3AX qM+Io92ThAQ6tU4lSHUiJbdYAPDi148SfSThLJhzpDeZiP8HBDqzesFyr4PPtNDrw7Xy LfT3LyelKZpqBFE8/zWVD6zkm+0Qt1JtsNq2NT32Jm+vkiJ1wutrGfapRNayedhiQemj oGkd+EqOnyvE1Nq59gy3h8dH+kIEURugywS9gMUT7svg0Hkw9GxfAIeTy4SX4/T6LHVj bSszedGTk60B4SfD0PFV4HjUf+WMRD6YSYEp9nunLDqpcnOVdKfODsb5yzmjFxs62B30 svvw== 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=3cpZrS1gQYWA804XDvn99we6TYNemMw9nD17Rft+xGU=; b=N/Fx7SuIIjesM3mv7wjwzSEz1GdHtHoTLx7YR329CYI3SyXPmv1n8NVBzGFwALn091 j5vy+IJynmm36aRizdCTuIHwRSO5spHWRjwxlzCmh2SSOH7oURJWvZ9wbGWDUsY0nX95 Kpp+EPj3KljvYBbR71XPUkYADYnSdpbR5JwyR0VZ1E4FTp9mSfU6Kp9F4EULs1w+vInm jUnhOH0rR9H+wTyoaXLq3TCNYKWkOjoLfMwibYWf8XuUMdCXHrhpe43NQuy4woEEhsLw rZn1IU80l/9x6aC1K4FW/U1iTDgD90Zj+rJTRJH4bEuuXu79AH3s8al9UbCe8lc2yXMu Ckog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=aPhzGHIa; 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 d124si2350029pfd.29.2019.05.23.19.29.27; Thu, 23 May 2019 19:29:47 -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=aPhzGHIa; 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 S1731772AbfEXC2E (ORCPT + 99 others); Thu, 23 May 2019 22:28:04 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:35684 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727037AbfEXC2D (ORCPT ); Thu, 23 May 2019 22:28:03 -0400 Received: by mail-pf1-f195.google.com with SMTP id d126so2148109pfd.2 for ; Thu, 23 May 2019 19:28:03 -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=3cpZrS1gQYWA804XDvn99we6TYNemMw9nD17Rft+xGU=; b=aPhzGHIavDeQPOiPuA74tRP5GhRo9/O+P6OtPK2EnC2Qyc7iRFzCGz0y71oUjDVW7v ESw/sLodd8OA/sQzQOgDfWFyvHdtNhz08u2+Seg4IXI0yjhjID3KoGaAKC6VvO/P4G1j gZU+K62a/h/qE6vaPTpGNMr2sXi4HEyGdtCpOBhWMQprL0ltZWYqAi0usZ63EduLYjZp /RsNyzf3Ah3jR351dqPoVeOyHlUZEbXdgHqOaXzcoaaZSDhLWLsI0xgZGRxDuO+KnFYk lcnaKqpyHExmecXmFzXRgscIu92Fob0nHmY7t3dRfUm9yumqa8jcc8ncrhxyb2tqF487 NIfg== 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=3cpZrS1gQYWA804XDvn99we6TYNemMw9nD17Rft+xGU=; b=bgqzAd4wuU/izg7tniGCaz5HFfTmyn7P5Agsc4KxVCI6W1nEPae/jktz15/mqSSLia U10KbuJlT2uqsur+T2Vx3t+0cP0NmyKSVQQeo/l4d+FGWcjF7CCtX/P/uv0tHKwGPcZa C5rpL3b8jebOGZdJMsx9gYmRzi9XMKFTBkPqRLqab2RIWxMRBz0y8Ck1XO+OBoF5byYc UMSH4QwHF22x0TI9CNdUsKxOeV5A4s8LTDqhJRm2AGvxIeHwuftTmXi78rQrOK7j2FWX tNV/N7lgfcogbmUq9xcpAadHhFMZHsQKKKJoHCYhNYoBIZnaCQOV0i+GBnLcx4JQWmkO QoOw== X-Gm-Message-State: APjAAAVpc6hJSRxU8kRjSMsk1yAlXfzUgv5uNDwVeaBbRoYXhF6QPrdG 54i4u4zpnVO4H+bsr2bWSacxXxsm1jQ= X-Received: by 2002:a62:87c6:: with SMTP id i189mr110854542pfe.65.1558664882724; Thu, 23 May 2019 19:28:02 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id k9sm777238pfa.180.2019.05.23.19.28.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 May 2019 19:28:02 -0700 (PDT) Date: Fri, 24 May 2019 10:27:52 +0800 From: Gen Zhang To: jslaby@suse.com Cc: mpatocka@redhat.com, linux-kernel@vger.kernel.org Subject: [PATCH v3] vt: Fix a missing-check bug in con_init() Message-ID: <20190524022752.GB4866@zhanggen-UX430UQ> References: <20190521022940.GA4858@zhanggen-UX430UQ> <20190521030905.GB5263@zhanggen-UX430UQ> <20190521040019.GD5263@zhanggen-UX430UQ> <20190521073901.GF5263@zhanggen-UX430UQ> <20190522121908.GA6772@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 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; + } + console_unlock(); + return -ENOMEM; } console_initcall(con_init); ---