Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1364518pxb; Fri, 21 Jan 2022 16:36:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwOX36PGs8GBW8COvdJLjAf4IDQTLtJ8yzQYV4dU9ukwEjWekTWKSTy3XqZi9Xwkt/xvJbU X-Received: by 2002:a65:5a8c:: with SMTP id c12mr4668177pgt.54.1642811775827; Fri, 21 Jan 2022 16:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642811775; cv=none; d=google.com; s=arc-20160816; b=wHWSuAqmkhhiEf+gJq8k3QKFMlU6ScJ80S9KYzZmYe+X5afHhMB83d9p8b7s4UgD8B M9bWkLyMI4q1igD7a3pDvSTcseZju1D5b+PuksQ4a2XiRa/cZAApTMA0kIm3M8DP1Q9c 4+byntCEHfeVgEF6pzymes04pAyT/SLmlXf9HicQ2fHYAEK3KyB8Zhwc5KBTVvmlkroF B/W++7TNlmeO+YeKWzJMYs8eUFk/ChgVvBWy87Qrj1CPuwB7/zlpyFVWMRchrbC6sJor gGyfFR/mpugzQ1c/tWbXn+908qMQ3Dg/GJlW4qS3AsWzmMIILjCEEM9UNoWbIWZz6KV0 Ek0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=/DvEpmtgBHJ3rKRXjcZCSZO6AUgZ5F71daPw/VxTEfI=; b=WxIWISAji8PIXrhwK6YApxbBkSlIRbXwYtKwEZ8kbZpZ9ugXLyK6G5hG3XGIvcUsp2 ejRMh1/waQc84wLw+xVswvnE88hgQUPU3RcqN/LvAj0hToEmbIGCuIYHRDuhXCdSUTX3 O6EYVeqiYtDvLznooGhyfg+bamWWH++uxm6TBF+YF/JkDEGi1ADwW9obeeS4tUoNBVg5 1uO+UmFmzKYu+LICDHTav7tHuVhqQ47TuWmsMjEIsQBgSFw9GdpqzSmWmIztj1ZeZoXn 79imMsgd7bsyq+1P5rswPdEkE/pMG8Kf1vYegc2UoMX7MyymwdQjrMTkWIgoy4FDIQ6Q sSsw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k15si10171607pga.245.2022.01.21.16.36.04; Fri, 21 Jan 2022 16:36:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378787AbiAUGxv (ORCPT + 99 others); Fri, 21 Jan 2022 01:53:51 -0500 Received: from smtp21.cstnet.cn ([159.226.251.21]:45462 "EHLO cstnet.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1378777AbiAUGxu (ORCPT ); Fri, 21 Jan 2022 01:53:50 -0500 Received: from localhost.localdomain (unknown [124.16.138.126]) by APP-01 (Coremail) with SMTP id qwCowAB3fZ1hWOphqQW0Bg--.17811S2; Fri, 21 Jan 2022 14:53:21 +0800 (CST) From: Jiasheng Jiang To: gregkh@linuxfoundation.org, jirislaby@kernel.org, jcmvbkbc@gmail.com, dsterba@suse.com, johan@kernel.org, dankamongmen@gmail.com, penguin-kernel@I-love.SAKURA.ne.jp, igormtorrente@gmail.com Cc: linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH] tty: vt: Check for NULL pointer after calling kzalloc Date: Fri, 21 Jan 2022 14:53:20 +0800 Message-Id: <20220121065320.2283544-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Enc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I0E14v26r1j6r4UYxBIdaVFxh VjvjDU0xZFpf9x0JU-J5rUUUUU= X-Originating-IP: [124.16.138.126] X-CM-SenderInfo: pmld2xxhqjqxpvfd2hldfou0/ Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As the potential failure of the allocation, the kzalloc() will return NULL pointer. Therefore, it should be better to check it in order to avoid the dereference of the NULL pointer. When it fails, we should free all the allocated memory and return error number. To make the code more clear, I use the 'err_free', like how vc_allocate() deals with the allocation failure. Fixes: a5f4f52e8211 ("vt: use kzalloc() instead of the bootmem allocator") Signed-off-by: Jiasheng Jiang --- drivers/tty/vt/vt.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index f8c87c4d7399..343fa6fffc18 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -3519,11 +3519,17 @@ 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_free; + INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); tty_port_init(&vc->port); visual_init(vc, currcons, 1); /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */ vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); + if (!vc->vc_screenbuf) + goto err_free_vc; + vc_init(vc, vc->vc_rows, vc->vc_cols, currcons || !vc->vc_sw->con_save_screen); } @@ -3545,6 +3551,19 @@ static int __init con_init(void) register_console(&vt_console_driver); #endif return 0; +err_free_vc: + visual_deinit(vc); + kfree(vc); + vc_cons[currcons].d = NULL; +err_free: + for (i = 0; i < currcons; i++) { + kfree(vc_cons[currcons].d->vc_screenbuf); + visual_deinit(vc_cons[currcons].d); + kfree(vc_cons[currcons].d); + vc_cons[currcons].d = NULL; + } + console_unlock(); + return -ENOMEM; } console_initcall(con_init); -- 2.25.1