Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2979908ybt; Mon, 29 Jun 2020 12:01:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSh/lI1Ri2DURWakyrFYa8NFQ9Ws+DUkta6bSPRbhVfgrur0MOAKC+eWd4YZjBvHurbNOI X-Received: by 2002:a50:ee01:: with SMTP id g1mr11978194eds.264.1593457316294; Mon, 29 Jun 2020 12:01:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593457316; cv=none; d=google.com; s=arc-20160816; b=ZlFQHTOm0XiEHJo1/PweebZWNyppI9h6LUk3oJJ/L/2UE9gCCBtSc3Ir0AzL94iHI6 xMj7xEwgWGx46mk6d40di6p12axxD+lalU7q2mEnEgIpagDys888NJj3gDMIfj46lV4l yF10e1F3GJLzew9aFw8TKjy4zHhL6xHYY5fULEosj9zqZc4B3uFQKsAmRCqvWsFMdEQQ 6BwWC8bMUVJL2aNgp7bjXUT8T83RlgZk2Ypk1bfHlB4e2ea/7ajQdU3mUlAk6QLFr668 FYJj6JYIfOCvmW5bLXwjKsQdMMgfEfDcZGTPKojuLhSeRgLhdy22T5TUhn+abzpKjYLQ 4V0g== 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; bh=Cx6MpL+arezCqElY9vexcse6LM5rAdU3E/qBC2dYoVc=; b=VcVuRFyaXzMeCdgb6iRM1f7JuzPLWMHKWVgyZk3vVcfEZGDxJ/U2/vKus2kYV6Mzfz Q65ZjtEZ5W6u8lIIaUPnjaDSVEJcMheHmHjUtkT3iaCfJ1ucFYFzbYLGfSahn7tzDtH8 Y5XhuqtagheFbRKwOoHvu5ohjdFBk1C6Vg6sCB/pz8irLLRB4Zj7DB041Q+9ljZiB9kX zIwdzzRbU9MDt6MEe+shVdb5RZW0/DTR4RJoVJnxxizL4B4Qd/nGVQ82b1UsZQ67LDl7 CVCtZp88Dlv+Fl2f2PrIPH4tDFtOc/eewW6trd+/VyZRzVDLi9DDeeq9Zsf19578wUDv 4Tqw== 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 n6si272268ejs.278.2020.06.29.12.01.32; Mon, 29 Jun 2020 12:01:56 -0700 (PDT) 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 S1729580AbgF2TBa (ORCPT + 99 others); Mon, 29 Jun 2020 15:01:30 -0400 Received: from mx2.suse.de ([195.135.220.15]:48442 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729397AbgF2TBY (ORCPT ); Mon, 29 Jun 2020 15:01:24 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id F1A96ACF1; Mon, 29 Jun 2020 14:50:20 +0000 (UTC) Date: Mon, 29 Jun 2020 16:50:20 +0200 From: Petr Mladek To: Cengiz Can , Daniel Thompson , Sumit Garg Cc: Jason Wessel , Douglas Anderson , kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, Andy Shevchenko Subject: Re: [PATCH] kdb: prevent possible null deref in kdb_msg_write Message-ID: <20200629145020.GL6156@alley> References: <20200629135923.14912-1-cengiz@kernel.wtf> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200629135923.14912-1-cengiz@kernel.wtf> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 2020-06-29 16:59:24, Cengiz Can wrote: > `kdb_msg_write` operates on a global `struct kgdb_io *` called > `dbg_io_ops`. > > Although it is initialized in `debug_core.c`, there's a null check in > `kdb_msg_write` which implies that it can be null whenever we dereference > it in this function call. > > Coverity scanner caught this as CID 1465042. > > I have modified the function to bail out if `dbg_io_ops` is not properly > initialized. > > Signed-off-by: Cengiz Can > --- > kernel/debug/kdb/kdb_io.c | 15 ++++++++------- > 1 file changed, 8 insertions(+), 7 deletions(-) > > diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c > index 683a799618ad..85e579812458 100644 > --- a/kernel/debug/kdb/kdb_io.c > +++ b/kernel/debug/kdb/kdb_io.c > @@ -549,14 +549,15 @@ static void kdb_msg_write(const char *msg, int msg_len) > if (msg_len == 0) > return; > > - if (dbg_io_ops) { > - const char *cp = msg; > - int len = msg_len; > + if (!dbg_io_ops) > + return; This looks wrong. The message should be printed to the consoles even when dbg_io_ops is NULL. I mean that the for_each_console(c) cycle should always get called. Well, the code really looks racy. dbg_io_ops is set under kgdb_registration_lock. IMHO, it should also get accessed under this lock. It seems that the race is possible. kdb_msg_write() is called from vkdb_printf(). This function is serialized on more CPUs using kdb_printf_cpu lock. But it is not serialized with kgdb_register_io_module() and kgdb_unregister_io_module() calls. But I might miss something. dbg_io_ops is dereferenced on many other locations without any check. > > - while (len--) { > - dbg_io_ops->write_char(*cp); > - cp++; > - } > + const char *cp = msg; > + int len = msg_len; > + > + while (len--) { > + dbg_io_ops->write_char(*cp); > + cp++; > } > > for_each_console(c) { You probably got confused by this new code: if (c == dbg_io_ops->cons) continue; It dereferences dbg_io_ops without NULL check. It should probably get replaced by: if (dbg_io_ops && c == dbg_io_ops->cons) continue; Daniel, Sumit, could you please put some light on this? Best Regards, Petr