Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp423591ybm; Mon, 20 May 2019 19:32:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqwdEP455LVs6m9EcEp6EzcbmE6gIc/H6rYEJ4po3Wv120mEQk9dRRu++589r8KxSHGjzJT8 X-Received: by 2002:a62:e803:: with SMTP id c3mr38289070pfi.58.1558405940038; Mon, 20 May 2019 19:32:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558405940; cv=none; d=google.com; s=arc-20160816; b=tPB8cvn0Ex9lA1lozMYiPK491ZhmSHDe34pEoGQSLdqKFbjoDJpwweTZe8zPgvL4YA OI8QcNikwkmFd+U4aK0vlzGsq1NM3sX7E4clLYmxcgtK6fFXj3MfJBYWMk2kTJdOovYK +a7rex4SHGoGnw2DPHAQFVUOAB5cpTOAgIlUrlOBnUu68KQNA/ALQZW67x6btzdNU5tG uhAtmqKaS8eI2aOSTXWtORGFSpSdHVuGiGftXBqcOA7WBZxPMfTQPGnuZAO0zk+AF/vW /+tErIaQr5YqmqFKPlDyeBjr56v2afI/SRCthESQp6C5JmfKuddYhuF/OY8QvpzkzYCk yJcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=qMYs7A9DUfxjSa4/BdCbURvdQcnbXXndFxXlWbC2VcI=; b=RH/xISYWPhatdUfpuB8d7O2F5DDDh3OeiItvGbOaTazuemB6kwJBBfWOonw3pzbASA 8knLZbJFE98ejlteRvYsHFlTA75vRJEN1/cReK385BuNy8DfwbKxo+ywz2ccK2oGIJBb BzDzQinEAP7QPWtqnbI+TaPODOzz2H220FQLvHSSqDmYe0WphDCbeg9ANnyM9vFtWPep F80add2h/P744cnVa5Ko0K4s37lCGVHNWxz+5y7r/mLG7dy1BN2eOK821kIew56raIxD 8dg65LaXXKouoXO/3pYwKaRgV0+h1vqTN4wPq7U5keC58rCTNCr38UsquqUDB7jJtX8p bEcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tcPEysMj; 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 p19si8749920plo.212.2019.05.20.19.32.04; Mon, 20 May 2019 19:32:20 -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=tcPEysMj; 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 S1727666AbfEUC3w (ORCPT + 99 others); Mon, 20 May 2019 22:29:52 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:43253 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726548AbfEUC3w (ORCPT ); Mon, 20 May 2019 22:29:52 -0400 Received: by mail-pf1-f193.google.com with SMTP id c6so8189816pfa.10 for ; Mon, 20 May 2019 19:29:52 -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:mime-version:content-disposition :user-agent; bh=qMYs7A9DUfxjSa4/BdCbURvdQcnbXXndFxXlWbC2VcI=; b=tcPEysMj7VmRxPCDgaAzoSuELPARd6JUs8hsKo1MTtc3tStq78LVwpZjjR6+i+VSL7 UDfztBuPyp/2KhEsKRPYyxdufxS+SVnHCsynpO9W467mc9k6P7EwnMMeJUi+0X3m28xE 8U2iiBU9fEEIo86O+lZEp70vy75VI7Bo7qRXkd74mffOMmOeI267Em/Dr70zoKF+cu/x xWghgNYbxXoxHWCG+yqz1KoxYfRHE701e8iyz+zJNS9hxuWti+j6SkMTfRVaamEcC85f xiTM1+or4kRTorDa6fJpS+umMG8+hu7O4f73IK+T1gPeF7iWDLGkygWNqnPnd2b7oUt2 Jjzg== 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:mime-version :content-disposition:user-agent; bh=qMYs7A9DUfxjSa4/BdCbURvdQcnbXXndFxXlWbC2VcI=; b=IW4TFV13NiXhBNoyV9/FPELC2kzyt4OqGlulzNmI5S9W9Xihfo2oH4cR6ThF25fOyE +nFN7Ba7+bJssEJD+JdwV2zsieSuKfcrgLyO6zsV3Fm2au6icYDcnpHpP9SWXN6jIJ3D qoM6lDJhYTxkzskXoBvd+ek17r+paEbOwSPZTiL5DLBr7FK+X3E+MyoTAmskvsk6vagD OBriYML1rGKqiPa1G4uF7XpZSzMFge+xQQNaur1A3zve97AEaRB5sl6L2GR5y8aBt7mc 2PKp4op/OHjuzeYihoAkNE5IkHhaIE1/rgg5/rKsjukw9xnEOIE3kCtVPBDi/su4atjS /+mQ== X-Gm-Message-State: APjAAAUu8IfDXoFOLLjkBGZnZ4dumxQ6Osx0ZkdgN9JQfd7nDTcMI210 CuoIcfkD/kMvSTw8za2qdrmO/IM66tM= X-Received: by 2002:a62:5201:: with SMTP id g1mr10135981pfb.152.1558405791767; Mon, 20 May 2019 19:29:51 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id 135sm34848161pfb.97.2019.05.20.19.29.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 May 2019 19:29:51 -0700 (PDT) Date: Tue, 21 May 2019 10:29:40 +0800 From: Gen Zhang To: nico@fluxnic.net Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2] vt: Fix a missing-check bug in drivers/tty/vt/vt.c Message-ID: <20190521022940.GA4858@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. 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..b756609 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_cons[currcons].d || !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,14 @@ static int __init con_init(void) register_console(&vt_console_driver); #endif return 0; +err_vc: + console_unlock(); + return -ENOMEM; +err_vc_screenbuf: + console_unlock(); + kfree(vc); + vc_cons[currcons].d = NULL; + return -ENOMEM; } console_initcall(con_init); ---