All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Yi Xie <xieyi@kylinos.cn>
Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] ipc/shm: check shm_lock() in do_shmat cleanup
Date: Tue, 14 Jul 2026 10:03:15 +0100	[thread overview]
Message-ID: <alX5Q9fLo-GHszaB@lucifer> (raw)
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 <xieyi@kylinos.cn>

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

  reply	other threads:[~2026-07-14  9:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-14  3:17 [PATCH] ipc/shm: check shm_lock() in do_shmat cleanup Yi Xie
2026-07-14  9:03 ` Lorenzo Stoakes (ARM) [this message]
2026-07-14  9:22 ` [PATCH v2] " Yi Xie
2026-07-14  9:30   ` Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alX5Q9fLo-GHszaB@lucifer \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xieyi@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.