From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CDFB3F871E for ; Tue, 14 Jul 2026 09:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784019808; cv=none; b=O7HBpPJiU6x4TVQrpa3fT3prPV4Jlvk8/TJq0sE77kki0GP0BPgXDkXEhwRSFRpQxdafDCiFgcbp2y6b0WxJvbGMaUIjHnty25aeaf8YHlCXYCOXoGaAmAuF+sGULfOCUb3KrVXCMSJgR6s2YgT55bzNr3UxCEYM17CFIxlvzBI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784019808; c=relaxed/simple; bh=7psyalB6BwHtkh52OwIh5yWSlU2xpKCiQ7leuHpO0d8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Ld48Rip+cML/aePJrNduRekllUKkOXWnWrNNV+097PuBxJAAdcz2HJQQfpORSzEbZ1Xn+ijtcfi9qaHO0+4o53I26P/ijATgtjR46Whc9+n2hyaalJM302nctD8AjVofve+d1+ABnyeZmGypEfLAeTaPMI26IsXaMuMYvxNKKeM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hl1JMkr6; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hl1JMkr6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7B631F000E9; Tue, 14 Jul 2026 09:03:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784019806; bh=d5R2MDERhanvlvg6+P817FqUn3z6e1x6O68CAAAhHpg=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Hl1JMkr6TkNgna2lJgxaKtBDnrHOMIg7YUCyXsbOAOHpvra1tNPintmwYLlzpKrXs 1FmuXNwLvM/GMIqGbu663XlCGpyqjW7HVMluIIjp8JDeHRJ4n/h8qfOm20ezAqOTWF Q5arK57HBo7dD9Cx+uu2oq/AHc6LQWKRlfFxCpTl4eEFoE/pe2UCzuu2SPqN8lxxYe mjdo6x4TAYFvLpeLzd/2NOrO2hjg4mfBgRECE5VhjHgGyrKAsp1X8faD2IsYk/9fR8 VVXn5Hq2EOttyIJ8+mqYWB1EjaLYTaAbATyJQCawYb1b287nTbERUVAEAR4XW5c3Ds ZIiUw2C9woP4A== Date: Tue, 14 Jul 2026 10:03:15 +0100 From: "Lorenzo Stoakes (ARM)" To: Yi Xie Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ipc/shm: check shm_lock() in do_shmat cleanup Message-ID: References: <20260714031713.72859-1-xieyi@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260714031713.72859-1-xieyi@kylinos.cn> On Tue, Jul 14, 2026 at 11:17:13AM +0800, Yi Xie wrote: > shm_lock() can fail; don't dereference the error pointer. > > Signed-off-by: Yi Xie Yikes yeah. Presumably this is pretty hard to hit, but seems legit. > --- > ipc/shm.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/ipc/shm.c b/ipc/shm.c > index b3e8a58e177d..bdcf1c0f221d 100644 > --- a/ipc/shm.c > +++ b/ipc/shm.c > @@ -1677,6 +1677,10 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, > out_nattch: > down_write(&shm_ids(ns).rwsem); > shp = shm_lock(ns, shmid); > + if (IS_ERR(shp)) { > + up_write(&shm_ids(ns).rwsem); > + return err; > + } You're duplicating the ugly up_write() here. Probably better to do something like: - shp->shm_nattch--; + if (IS_ERR(shp)) { + err = PTR_ERR(shp); + } else { + shp->shm_nattch--; - if (shm_may_destroy(shp)) - shm_destroy(ns, shp); - else - shm_unlock(shp); + if (shm_may_destroy(shp)) + shm_destroy(ns, shp); + else + shm_unlock(shp); + } And keep the up_write() shared. > shp->shm_nattch--; > > if (shm_may_destroy(shp)) > -- > 2.25.1 > Thanks, Lorenzo