Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp711504ybe; Thu, 19 Sep 2019 02:33:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqw9AAdCwe6Yq5Xh30fKk/TW0U64No5GANOAkqx5nqoBKyTP5kb5r3mFFcos7bKW2t+lV34F X-Received: by 2002:a50:ce53:: with SMTP id k19mr14840115edj.2.1568885580440; Thu, 19 Sep 2019 02:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568885580; cv=none; d=google.com; s=arc-20160816; b=jwtGQ+PIKT1S3kEG7TiJp73LtnJwpc8aGXdxGPQQm1OG1pEViW/1KWdpWEP1vA1cuV w1BodpJcBVm/dKY1jg198+VkqzGi3pCVN8xDXdX3vEHEK16WO+miFO9EnSUu0+Olh6g6 Sy4ki4tG2TdvIqLhn62OzEzd+HY3MrNMpiCVDcXfXhxku+SZhOVUFVl7qiPGKSCMRp3G WBXJ1KRoqFArzMXiOgPgMXqzvK22Ha2eELc3lz7qNdGHL8WELna5/WOn6yiyeJKCpsdV a8AsAWSFriRr8x2cski89HUoa5x/J7dJK2ULHg9vylYB1LXoYlYWIpqFSexOxhB9GzFF smnQ== 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=Hi7j3Rm+W6Sz5AuFmd9fLPui6HdIqPpvPNxss2oy8uU=; b=byp1F5qg0ti15DmvEAQeDQNkBe1JqN7i63AxR+rYaazkLWRL5OZW0Twr9hwwRezGfq lYouWu6SaYhzhpnWhLHeHnUKNvMLyFbJPw2RDqcY2sn2la6o1ja8AeZENRhyCNBMuHvr m+ix80XhKdLHSc85OwEUhXp3vQ8a5R78vRd7qhwdYvqBFhnwsVlEk8omPhj3e9N26ake yxdoULP8qzKw4RlafQCEl3fgY99V9qqPtFsNtX1OooPUuyCKh/j4ctS7DSuFFakloAWp V/+nFP31uTeY7sVJbgwNn960WRMRvg73JsnwfnTxkoD1WQ0SLeK9pXvSd40L8tp367ik 9JdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XVwcLDwJ; 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 h17si4978077eda.54.2019.09.19.02.32.34; Thu, 19 Sep 2019 02:33:00 -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=@kernel.org header.s=default header.b=XVwcLDwJ; 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 S2388650AbfISJ3g (ORCPT + 99 others); Thu, 19 Sep 2019 05:29:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:60284 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387767AbfISJ3g (ORCPT ); Thu, 19 Sep 2019 05:29:36 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F321721924; Thu, 19 Sep 2019 09:29:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568885375; bh=KrLnWU/9gwbq35alpkpIodhigbzAcDozcpZYhH1dTL0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XVwcLDwJ3Hk953YT1U2lwnhjYSvxCDSm3xvYHSVOCFgXTLW+WenSOPm+g2wfU193Q rnqLwW+6a+NGWTIgll6+45eoh+a8xhtUia0XurNzMPRbsTwZnxvoGB6r0VeUN6GD72 OXYDsjHleJnaiAL1zChT4IAXZq+BU96wbGHeImHc= Date: Thu, 19 Sep 2019 11:29:33 +0200 From: Greg KH To: Xiaoming Ni 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@huawei.com, yuehaibing@huawei.com, zengweilin@huawei.com Subject: Re: [PATCH] tty:vt: Add check the return value of kzalloc to avoid oops Message-ID: <20190919092933.GA2684163@kroah.com> References: <1568884695-56789-1-git-send-email-nixiaoming@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1568884695-56789-1-git-send-email-nixiaoming@huawei.com> User-Agent: Mutt/1.12.1 (2019-06-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? > 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. 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