Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp241598ybl; Tue, 28 Jan 2020 02:21:20 -0800 (PST) X-Google-Smtp-Source: APXvYqx6I8lcvg0u4lbgiGyIEhRuFiN5J969URj/mNHA+0X+bWREBwQcrWNmr/Cd4lUddynCHBLn X-Received: by 2002:a05:6808:9a4:: with SMTP id e4mr2367204oig.127.1580206880532; Tue, 28 Jan 2020 02:21:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580206880; cv=none; d=google.com; s=arc-20160816; b=QJ32JRFe2djf2vQ8Myg0ViBr9qHXUr7cV4v2Ah1Qobhz5qDheNgKK5fKMUUcyI/pLV KjCvwExQJ9vsVeBWfDW35BjAQMUMlLxruIAiWGnTpCfqb6PNrOrkqdL0sKIhwZ357yTQ E+mpqs8hDvWRcTB75g+DeYT4auKN2xGh+wFgZmbaevdDSNRwyw5hAH40MRwVKBA1TGbx O7A16njmskSehjrqKT3LRNcbvUS1u454Y2pNYPRrYySsygyCzR/pKUg4/My/pfNw19R4 mlzW5eLy9yICbDHWOh2CkS3yJcPYG2LUmM7tFj6BigSKPQzqx0qzjkyKtQSAK6xa06oZ i8UQ== 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=ZIMdOJlyY6citiHF9J1CKJwyQ+4WiSxR+2fHrrPOHvM=; b=j9WZcAIxqP2a9ICJNd2fBIZXU97TvZxrlUTY0jnlm4ySjYDk+VEih4y6rUNI+BOPSe 3FCqfbflddp2jxRopd9HYTEVHlYnQxa3QHa208nnE1FTHoJtWg8mvI9vg277wur2dVJG uf8Pjg3I/ix4KOPT9SCCQTEcqA/hnwpZikMUHI+sYwcuFPaQWxXKB9w5FEbxsrC6gcGy C35p0RRubnAs6E3pTZBbxcg/oVeA56yF+WaywUmCzmxvSCIlYDeJBne4uSm3oAXSHVPN vRgr+IOLT7TeEmy3oeOBBS4l3u9MLKUCoVjIIAvQAefdcv2oydK5Rc0h3Irpa89dtSKd I1RA== 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 l5si2508563otn.35.2020.01.28.02.21.06; Tue, 28 Jan 2020 02:21:20 -0800 (PST) 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 S1725995AbgA1KTs (ORCPT + 99 others); Tue, 28 Jan 2020 05:19:48 -0500 Received: from mx2.suse.de ([195.135.220.15]:36582 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725941AbgA1KTs (ORCPT ); Tue, 28 Jan 2020 05:19:48 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 917F8B25F; Tue, 28 Jan 2020 10:19:46 +0000 (UTC) Date: Tue, 28 Jan 2020 11:19:45 +0100 From: Libor Pechacek To: Nathan Lynch Cc: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Michal Suchanek , Thomas Gleixner , Allison Randal , Leonardo Bras , David Hildenbrand , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: Re: [PATCH] powerpc: drmem: avoid NULL pointer dereference when drmem is unavailable Message-ID: <20200128101945.GA20336@fm.suse.cz> References: <20200116102758.GC25138@fm.suse.cz> <87o8uudv51.fsf@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87o8uudv51.fsf@linux.ibm.com> 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 Hello Nathan, On Thu 23-01-20 09:56:10, Nathan Lynch wrote: > Libor Pechacek writes: > > In KVM guests drmem structure is only zero initialized. Trying to > > manipulate DLPAR parameters results in a crash in this environment. > > I think this statement needs qualification. Unless I'm mistaken, this > happens only when you boot a guest without any hotpluggable memory > configured, and then try to add or remove memory. Thanks for the review. The introductory statement can indeed be clearer. [...] > > diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c > > index c126b94d1943..4ea6af002e27 100644 > > --- a/arch/powerpc/platforms/pseries/hotplug-memory.c > > +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c > > @@ -236,9 +236,9 @@ static int get_lmb_range(u32 drc_index, int n_lmbs, > > if (!start) > > return -EINVAL; > > > > - end = &start[n_lmbs - 1]; > > + end = &start[n_lmbs]; > > > > - last_lmb = &drmem_info->lmbs[drmem_info->n_lmbs - 1]; > > + last_lmb = &drmem_info->lmbs[drmem_info->n_lmbs]; > > if (end > last_lmb) > > return -EINVAL; > > Is this not undefined behavior? I'd rather do this in a way that does > not involve forming out-of-bounds pointers. Well, this is a tough question for the case when drmem_info->lmbs was not allocated. Given that the array does not exist, what bounds are we talking about? My patch builds on the fact that NULL[0] is NULL and NULL < NULL is false. Talking about a pointer to one past the last element of an non-existent array is too much philosophy for me. For the case when drmem_info->lmbs is allocated, last_lmb is a pointer to one past the last element of the array as Michal mentioned. > Even if it's safe, naming that pointer "last_lmb" now actively hinders > understanding of the code; it should be named "limit" or something. Good catch. [...] > 1 file changed, 36 insertions(+), 7 deletions(-) > arch/powerpc/include/asm/drmem.h | 43 +++++++++++++++++++++++++++++++++------- > > modified arch/powerpc/include/asm/drmem.h > @@ -20,19 +20,48 @@ struct drmem_lmb { > > struct drmem_lmb_info { > struct drmem_lmb *lmbs; > - int n_lmbs; > + unsigned int n_lmbs; > u32 lmb_size; > }; > > extern struct drmem_lmb_info *drmem_info; > > -#define for_each_drmem_lmb_in_range(lmb, start, end) \ > - for ((lmb) = (start); (lmb) <= (end); (lmb)++) > +static inline bool drmem_present(void) > +{ > + return drmem_info->lmbs != NULL; > +} Yes, use of this test was also my first idea about the fix. > +static inline struct drmem_lmb *drmem_lmb_index(unsigned int index) > +{ > + if (!drmem_present()) > + return NULL; > > -#define for_each_drmem_lmb(lmb) \ > - for_each_drmem_lmb_in_range((lmb), \ > - &drmem_info->lmbs[0], \ > - &drmem_info->lmbs[drmem_info->n_lmbs - 1]) > + if (WARN_ON(index >= drmem_info->n_lmbs)) > + return NULL; Why is this WARN_ON needed? > + > + return &drmem_info->lmbs[index]; > +} > + > +static inline struct drmem_lmb *drmem_first_lmb(void) > +{ > + return drmem_lmb_index(0); > +} > + > +static inline struct drmem_lmb *drmem_last_lmb(void) > +{ > + if (!drmem_present()) > + return NULL; > + > + return drmem_lmb_index(drmem_info->n_lmbs - 1); Is the unsigned integer wraparound intended in drmem_info->n_lmbs == 0 case? > +} > + > +#define for_each_drmem_lmb(lmb) \ > + for ((lmb) = drmem_first_lmb(); \ drmem_first_lmb() is essentially a call to drmem_info->lmbs(0). What happens if drmem_info->n_lmbs is zero and drmem_info->lmbs is not NULL? > + (lmb) != NULL && (lmb) <= drmem_last_lmb(); \ > + (lmb)++) > + > +#define for_each_drmem_lmb_in_range(lmb, start, end) \ > + for ((lmb) = (start); (lmb) <= (end); (lmb)++) > > /* > * The of_drconf_cell_v1 struct defines the layout of the LMB data > After all, I don't mind how the bug will be fixed. As you can see, my preference is towards simpler solutions. In my opinion your solution special-cased drmem_info->lmbs == NULL and opened the doorway to the combination of drmem_info->lmbs != NULL && !drmem_info->n_lmbs. Maybe the condition can never become true but the code should IMHO be robust enough to handle it. Thanks! Libor -- Libor Pechacek SUSE Labs Remember to have fun...