* [PATCH] ipc/shm: check shm_lock() in do_shmat cleanup
@ 2026-07-14 3:17 Yi Xie
2026-07-14 9:03 ` Lorenzo Stoakes (ARM)
2026-07-14 9:22 ` [PATCH v2] " Yi Xie
0 siblings, 2 replies; 4+ messages in thread
From: Yi Xie @ 2026-07-14 3:17 UTC (permalink / raw)
To: akpm, ljs; +Cc: linux-kernel, Yi Xie
shm_lock() can fail; don't dereference the error pointer.
Signed-off-by: Yi Xie <xieyi@kylinos.cn>
---
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;
+ }
shp->shm_nattch--;
if (shm_may_destroy(shp))
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] ipc/shm: check shm_lock() in do_shmat cleanup
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)
2026-07-14 9:22 ` [PATCH v2] " Yi Xie
1 sibling, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-07-14 9:03 UTC (permalink / raw)
To: Yi Xie; +Cc: akpm, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2] ipc/shm: check shm_lock() in do_shmat cleanup
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)
@ 2026-07-14 9:22 ` Yi Xie
2026-07-14 9:30 ` Lorenzo Stoakes (ARM)
1 sibling, 1 reply; 4+ messages in thread
From: Yi Xie @ 2026-07-14 9:22 UTC (permalink / raw)
To: akpm, ljs; +Cc: linux-kernel, Yi Xie
shm_lock() can fail; don't dereference the error pointer.
Signed-off-by: Yi Xie <xieyi@kylinos.cn>
---
v2: keep a single up_write(), as suggested by Lorenzo
ipc/shm.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/ipc/shm.c b/ipc/shm.c
index b3e8a58e177d..d243137c4dbd 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1677,12 +1677,16 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
out_nattch:
down_write(&shm_ids(ns).rwsem);
shp = shm_lock(ns, shmid);
- 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);
+ }
up_write(&shm_ids(ns).rwsem);
return err;
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] ipc/shm: check shm_lock() in do_shmat cleanup
2026-07-14 9:22 ` [PATCH v2] " Yi Xie
@ 2026-07-14 9:30 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-07-14 9:30 UTC (permalink / raw)
To: Yi Xie; +Cc: akpm, linux-kernel
Please don't send v2 in-reply-to v1 :) it makes it really hard to keep
track of things.
Please wait _at least_ 24 hours before sending a respin. We have far too
much review load to have an immediate turnaround.
Also sending out so quick doesn't make me think you've tested this :)
On Tue, Jul 14, 2026 at 05:22:37PM +0800, Yi Xie wrote:
> shm_lock() can fail; don't dereference the error pointer.
Maybe something a bit longer, e.g.:
do_shmat() calls shm_lock() in the out_nattch branch and
immediately dereferences it, however shm_lock() can return an
error.
Check for an error and handle it if there is one.
>
> Signed-off-by: Yi Xie <xieyi@kylinos.cn>
> ---
> v2: keep a single up_write(), as suggested by Lorenzo
>
> ipc/shm.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/ipc/shm.c b/ipc/shm.c
> index b3e8a58e177d..d243137c4dbd 100644
> --- a/ipc/shm.c
> +++ b/ipc/shm.c
> @@ -1677,12 +1677,16 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
> out_nattch:
> down_write(&shm_ids(ns).rwsem);
> shp = shm_lock(ns, shmid);
> - 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);
> + }
> up_write(&shm_ids(ns).rwsem);
> return err;
>
> --
> 2.25.1
>
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-14 9:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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)
2026-07-14 9:22 ` [PATCH v2] " Yi Xie
2026-07-14 9:30 ` Lorenzo Stoakes (ARM)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox