Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp1377522ybj; Fri, 20 Sep 2019 09:28:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwNQeIk17K/Wk4XcXJ8Ug0T0ln4p9nkBAWvUAizkLxbrZKQn26/RsRsGrlpbdbT6QNTIRdN X-Received: by 2002:a50:dac2:: with SMTP id s2mr22804113edj.26.1568996914384; Fri, 20 Sep 2019 09:28:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568996914; cv=none; d=google.com; s=arc-20160816; b=ZNKYe5xZarrhIw3RaA+rfwYTg3IL0HOUHmaox3tm2NtUQjqOa589WMfJ11Yq6h4mqY iUuD5VkmqQzyRidPILD0ecJ5420R7fjYM1k6HVgpK1kuFjprzDHbL54LubjKmilSuoxX /NcEAWt9XDsxw/iv5KSqDaGb2SF1jW5n66nyLEelqu7Jb4b+LNCKkJjdIbXdfHN6kgYP sPJKdz8PdZMY7FKJCD9789BdgdSf7iu2EdKWmGjvHkhSY5te52yyyua4EQCVETQeItPo Xe29oyVxI5xP9BC9lXkE4szB+I1KKk8tNQwPPBdoffHmCFQRVdx4X5QHjBJovDoSNEuP kXgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=AqFr4m219z/+p4Nf1vVkLsvIMzJGJCozzfw64Qt1VCM=; b=o9DXFuZ6EnXdfWE5i889l6GAyXesEvrfpaEiY0dT7CRQKanIBPALVPyjWKvuDNNGoX vLYh6LKvBsshEbL+8z5b55G1ET1VewFt642EENGdPjYcFjz9GBFXqunsJ3/07mK7P9H4 ZGESUNTNf2UW2GRKCOkDUtW2W5f/g7uoW38HFUFZCRF1B8+zRT4siW/Ir1W0dlUk0Ebc hNSz902pSqqyt6AQqofIkNAklgW/Zvaby40reD3S6U2YON8BoHf+84peVYF4k3XgiSt5 1qjsAEJrCK+yZ1dWFRX5UTNU4/mUSGJ2KknM9S033CupssAfxk1be2CL7EWcEA7Rj95J 6RVg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h15si1639810eda.284.2019.09.20.09.28.10; Fri, 20 Sep 2019 09:28:34 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405478AbfITCaB convert rfc822-to-8bit (ORCPT + 99 others); Thu, 19 Sep 2019 22:30:01 -0400 Received: from szxga08-in.huawei.com ([45.249.212.255]:58006 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2405325AbfITCaA (ORCPT ); Thu, 19 Sep 2019 22:30:00 -0400 Received: from DGGEMM404-HUB.china.huawei.com (unknown [172.30.72.54]) by Forcepoint Email with ESMTP id 70EB3FC551F96FA08BA5; Fri, 20 Sep 2019 10:29:58 +0800 (CST) Received: from DGGEMM507-MBX.china.huawei.com ([169.254.1.207]) by DGGEMM404-HUB.china.huawei.com ([10.3.20.212]) with mapi id 14.03.0439.000; Fri, 20 Sep 2019 10:29:49 +0800 From: Nixiaoming To: Greg KH CC: "penberg@cs.helsinki.fi" , "jslaby@suse.com" , "nico@fluxnic.net" , "textshell@uchuujin.de" , "sam@ravnborg.org" , "daniel.vetter@ffwll.ch" , "mpatocka@redhat.com" , "ghalat@redhat.com" , "linux-kernel@vger.kernel.org" , Yangyingliang , yuehaibing , Zengweilin Subject: RE: [PATCH] tty:vt: Add check the return value of kzalloc to avoid oops Thread-Topic: [PATCH] tty:vt: Add check the return value of kzalloc to avoid oops Thread-Index: AQHVbss9p4oaa6BB+U6wTTWJjS3rk6cyNcqAgAGignA= Date: Fri, 20 Sep 2019 02:29:49 +0000 Message-ID: References: <1568884695-56789-1-git-send-email-nixiaoming@huawei.com> <20190919092933.GA2684163@kroah.com> In-Reply-To: <20190919092933.GA2684163@kroah.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.57.88.168] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/9/19 17:30, Greg KH wrote: > On Thu, Sep 19, 2019 at 05:18:15PM +0800, Xiaoming Ni wrote: >> Using kzalloc() to allocate memory in function con_init(), but not >> checking the return value, there is a risk of null pointer references >> oops. >> >> Signed-off-by: Xiaoming Ni > > We keep having this be "reported" > >> --- >> drivers/tty/vt/vt.c | 18 ++++++++++++++++++ >> 1 file changed, 18 insertions(+) >> >> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c >> index 34aa39d..db83e52 100644 >> --- a/drivers/tty/vt/vt.c >> +++ b/drivers/tty/vt/vt.c >> @@ -3357,15 +3357,33 @@ 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 (unlikely(!vc)) { >> + pr_warn("%s:failed to allocate memory for the %u vc\n", >> + __func__, currcons); >> + break; >> + } > > At init, this really can not happen. Have you see it ever happen? I did not actually observe the null pointer here. I am confused when I see the code allocated here without check the return value. Small memory allocation failures are difficult to occur during system initialization But is it not safe enough if the code is not judged? Also if the memory allocation failure is not allowed here, is it better to add the __GFP_NOFAIL flags? >> 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 (unlikely(!vc->vc_screenbuf)) { > > Never use likely/unlikely unless you can actually measure the speed > difference. For something like this, the compiler will always get it > right without you having to do anything. > Thank you for your guidance. I misuse it. > And again, how can this fail? Have you seen it fail? > >> + pr_warn("%s:failed to allocate memory for the %u vc_screenbuf\n", >> + __func__, currcons); >> + visual_deinit(vc); >> + tty_port_destroy(&vc->port); >> + kfree(vc); >> + vc_cons[currcons].d = NULL; >> + break; >> + } >> vc_init(vc, vc->vc_rows, vc->vc_cols, >> currcons || !vc->vc_sw->con_save_screen); >> } >> currcons = fg_console = 0; >> master_display_fg = vc = vc_cons[currcons].d; >> + if (unlikely(!vc)) { > > Again, never use likely/unlikely unless you can measure it. > > thanks, > > greg k-h > > . > thanks, Xiaoming Ni