stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources
@ 2022-11-04 18:41 Luiz Capitulino
  2022-11-07  9:14 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Luiz Capitulino @ 2022-11-04 18:41 UTC (permalink / raw)
  To: stable
  Cc: lcapitulino, Vasily Averin, Shakeel Butt, Alexander Viro,
	Alexey Dobriyan, Andrei Vagin, Borislav Petkov, Borislav Petkov,
	Christian Brauner, Dmitry Safonov, Eric W. Biederman,
	Greg Kroah-Hartman, H. Peter Anvin, Ingo Molnar, J. Bruce Fields,
	Jeff Layton, Jens Axboe, Jiri Slaby, Johannes Weiner,
	Kirill Tkhai, Michal Hocko, Oleg Nesterov, Roman Gushchin,
	Serge Hallyn, Tejun Heo, Thomas Gleixner, Vladimir Davydov,
	Yutian Yang, Zefan Li, Andrew Morton, Linus Torvalds

From: Vasily Averin <vvs@virtuozzo.com>

Commit 18319498fdd4cdf8c1c2c48cd432863b1f915d6f upstream.

[ This backport fixes CVE-2021-3759 for 5.10 and 5.4. Please, note that
  it caused conflicts in all files being changed because upstream
  changed ipc object allocation to and from kvmalloc() & friends (eg.
  commits bc8136a543aa and fc37a3b8b4388e). However, I decided to keep
  this backport about the memcg accounting fix only. ]

When user creates IPC objects it forces kernel to allocate memory for
these long-living objects.

It makes sense to account them to restrict the host's memory consumption
from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages
semaphores and semaphore's undo lists.

Link: https://lkml.kernel.org/r/d6507b06-4df6-78f8-6c54-3ae86e3b5339@virtuozzo.com
Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrei Vagin <avagin@gmail.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Christian Brauner <christian.brauner@ubuntu.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "J. Bruce Fields" <bfields@fieldses.org>
Cc: Jeff Layton <jlayton@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Jiri Slaby <jirislaby@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Yutian Yang <nglaive@gmail.com>
Cc: Zefan Li <lizefan.x@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Luiz Capitulino <luizcap@amazon.com>
---
 ipc/msg.c | 2 +-
 ipc/sem.c | 9 +++++----
 ipc/shm.c | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)

Reviewers,

Some important details:

o While doing this backport I realized that Vasily worked on a large accounting
overhaul which may include more instances of this problem (and possibly more
unfixed CVEs). This brings the question whether we should only fix concrete/reproducible
accounting issues or bring Vasily's entire overhaul. I'm choosing to fix
only concrete cases

o 4.19 and 4.9 should also have this issue, but I haven't tried the backport
there yet

o For testing, I did two things:

  1. Reproduced the issue as described in the link below, with and
     without this patch. Without the patch I can pretty clearly see
     the kernel allocating several gigas of memory that are not
     accounted for by memcg. With the patch the memory is accounted
     correctly

     Reproducer: https://lore.kernel.org/linux-mm/1626333284-1404-1-git-send-email-nglaive@gmail.com/

 2. I ran LTP's ipc test-cases (which simple, but hopefully good enough)

diff --git a/ipc/msg.c b/ipc/msg.c
index 6e6c8e0c9380..8ded6b8f10a2 100644
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
diff --git a/ipc/sem.c b/ipc/sem.c
index 7d9c06b0ad6e..d3b9b73cd9ca 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -511,7 +511,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1852,7 +1852,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1937,7 +1937,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
+	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -2001,7 +2001,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
diff --git a/ipc/shm.c b/ipc/shm.c
index 471ac3e7498d..b418731d66e8 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -711,7 +711,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
-- 
2.24.4.AMZN


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources
  2022-11-04 18:41 [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources Luiz Capitulino
@ 2022-11-07  9:14 ` Greg Kroah-Hartman
  2022-11-08 12:45   ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-07  9:14 UTC (permalink / raw)
  To: Luiz Capitulino
  Cc: stable, lcapitulino, Vasily Averin, Shakeel Butt, Alexander Viro,
	Alexey Dobriyan, Andrei Vagin, Borislav Petkov, Borislav Petkov,
	Christian Brauner, Dmitry Safonov, Eric W. Biederman,
	H. Peter Anvin, Ingo Molnar, J. Bruce Fields, Jeff Layton,
	Jens Axboe, Jiri Slaby, Johannes Weiner, Kirill Tkhai,
	Michal Hocko, Oleg Nesterov, Roman Gushchin, Serge Hallyn,
	Tejun Heo, Thomas Gleixner, Vladimir Davydov, Yutian Yang,
	Zefan Li, Andrew Morton, Linus Torvalds

On Fri, Nov 04, 2022 at 06:41:31PM +0000, Luiz Capitulino wrote:
> From: Vasily Averin <vvs@virtuozzo.com>
> 
> Commit 18319498fdd4cdf8c1c2c48cd432863b1f915d6f upstream.
> 
> [ This backport fixes CVE-2021-3759 for 5.10 and 5.4. Please, note that
>   it caused conflicts in all files being changed because upstream
>   changed ipc object allocation to and from kvmalloc() & friends (eg.
>   commits bc8136a543aa and fc37a3b8b4388e). However, I decided to keep
>   this backport about the memcg accounting fix only. ]

Looks good, now queued up, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources
  2022-11-07  9:14 ` Greg Kroah-Hartman
@ 2022-11-08 12:45   ` Greg Kroah-Hartman
  2022-11-08 16:51     ` Luiz Capitulino
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-08 12:45 UTC (permalink / raw)
  To: Luiz Capitulino
  Cc: stable, lcapitulino, Vasily Averin, Shakeel Butt, Alexander Viro,
	Alexey Dobriyan, Andrei Vagin, Borislav Petkov, Borislav Petkov,
	Christian Brauner, Dmitry Safonov, Eric W. Biederman,
	H. Peter Anvin, Ingo Molnar, J. Bruce Fields, Jeff Layton,
	Jens Axboe, Jiri Slaby, Johannes Weiner, Kirill Tkhai,
	Michal Hocko, Oleg Nesterov, Roman Gushchin, Serge Hallyn,
	Tejun Heo, Thomas Gleixner, Vladimir Davydov, Yutian Yang,
	Zefan Li, Andrew Morton, Linus Torvalds

On Mon, Nov 07, 2022 at 10:14:05AM +0100, Greg Kroah-Hartman wrote:
> On Fri, Nov 04, 2022 at 06:41:31PM +0000, Luiz Capitulino wrote:
> > From: Vasily Averin <vvs@virtuozzo.com>
> > 
> > Commit 18319498fdd4cdf8c1c2c48cd432863b1f915d6f upstream.
> > 
> > [ This backport fixes CVE-2021-3759 for 5.10 and 5.4. Please, note that
> >   it caused conflicts in all files being changed because upstream
> >   changed ipc object allocation to and from kvmalloc() & friends (eg.
> >   commits bc8136a543aa and fc37a3b8b4388e). However, I decided to keep
> >   this backport about the memcg accounting fix only. ]
> 
> Looks good, now queued up, thanks.

Ah, but you missed a fix-up patch for this one {sigh}

I've now queued up 18319498fdd4 ("memcg: enable accounting of ipc
resources") as well.  Please be more careful in the future when
backporting changes that you also include the fixes for those changes.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources
  2022-11-08 12:45   ` Greg Kroah-Hartman
@ 2022-11-08 16:51     ` Luiz Capitulino
  2022-11-08 17:10       ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Luiz Capitulino @ 2022-11-08 16:51 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: stable, lcapitulino, Vasily Averin, Shakeel Butt, Alexander Viro,
	Alexey Dobriyan, Andrei Vagin, Borislav Petkov, Borislav Petkov,
	Christian Brauner, Dmitry Safonov, Eric W. Biederman,
	H. Peter Anvin, Ingo Molnar, J. Bruce Fields, Jeff Layton,
	Jens Axboe, Jiri Slaby, Johannes Weiner, Kirill Tkhai,
	Michal Hocko, Oleg Nesterov, Roman Gushchin, Serge Hallyn,
	Tejun Heo, Thomas Gleixner, Vladimir Davydov, Yutian Yang,
	Zefan Li, Andrew Morton, Linus Torvalds

On Tue, Nov 08, 2022 at 01:45:56PM +0100, Greg Kroah-Hartman wrote:

> 
> 
> On Mon, Nov 07, 2022 at 10:14:05AM +0100, Greg Kroah-Hartman wrote:
> > On Fri, Nov 04, 2022 at 06:41:31PM +0000, Luiz Capitulino wrote:
> > > From: Vasily Averin <vvs@virtuozzo.com>
> > >
> > > Commit 18319498fdd4cdf8c1c2c48cd432863b1f915d6f upstream.
> > >
> > > [ This backport fixes CVE-2021-3759 for 5.10 and 5.4. Please, note that
> > >   it caused conflicts in all files being changed because upstream
> > >   changed ipc object allocation to and from kvmalloc() & friends (eg.
> > >   commits bc8136a543aa and fc37a3b8b4388e). However, I decided to keep
> > >   this backport about the memcg accounting fix only. ]
> >
> > Looks good, now queued up, thanks.
> 
> Ah, but you missed a fix-up patch for this one {sigh}
> 
> I've now queued up 18319498fdd4 ("memcg: enable accounting of ipc
> resources") as well.  Please be more careful in the future when
> backporting changes that you also include the fixes for those changes.

Good catch, the fixup is actually 6a4746ba06191e23d30230738e94334b26590a8a

Will try to be more careful next time.

- Luiz

> 
> thanks,
> 
> greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources
  2022-11-08 16:51     ` Luiz Capitulino
@ 2022-11-08 17:10       ` Greg Kroah-Hartman
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-08 17:10 UTC (permalink / raw)
  To: Luiz Capitulino
  Cc: stable, lcapitulino, Vasily Averin, Shakeel Butt, Alexander Viro,
	Alexey Dobriyan, Andrei Vagin, Borislav Petkov, Borislav Petkov,
	Christian Brauner, Dmitry Safonov, Eric W. Biederman,
	H. Peter Anvin, Ingo Molnar, J. Bruce Fields, Jeff Layton,
	Jens Axboe, Jiri Slaby, Johannes Weiner, Kirill Tkhai,
	Michal Hocko, Oleg Nesterov, Roman Gushchin, Serge Hallyn,
	Tejun Heo, Thomas Gleixner, Vladimir Davydov, Yutian Yang,
	Zefan Li, Andrew Morton, Linus Torvalds

On Tue, Nov 08, 2022 at 04:51:16PM +0000, Luiz Capitulino wrote:
> On Tue, Nov 08, 2022 at 01:45:56PM +0100, Greg Kroah-Hartman wrote:
> 
> > 
> > 
> > On Mon, Nov 07, 2022 at 10:14:05AM +0100, Greg Kroah-Hartman wrote:
> > > On Fri, Nov 04, 2022 at 06:41:31PM +0000, Luiz Capitulino wrote:
> > > > From: Vasily Averin <vvs@virtuozzo.com>
> > > >
> > > > Commit 18319498fdd4cdf8c1c2c48cd432863b1f915d6f upstream.
> > > >
> > > > [ This backport fixes CVE-2021-3759 for 5.10 and 5.4. Please, note that
> > > >   it caused conflicts in all files being changed because upstream
> > > >   changed ipc object allocation to and from kvmalloc() & friends (eg.
> > > >   commits bc8136a543aa and fc37a3b8b4388e). However, I decided to keep
> > > >   this backport about the memcg accounting fix only. ]
> > >
> > > Looks good, now queued up, thanks.
> > 
> > Ah, but you missed a fix-up patch for this one {sigh}
> > 
> > I've now queued up 18319498fdd4 ("memcg: enable accounting of ipc
> > resources") as well.  Please be more careful in the future when
> > backporting changes that you also include the fixes for those changes.
> 
> Good catch, the fixup is actually 6a4746ba06191e23d30230738e94334b26590a8a

Oops, yes, wrong git id in my clipboard buffer.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-08 17:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-04 18:41 [PATCH 5.10, 5.4] memcg: enable accounting of ipc resources Luiz Capitulino
2022-11-07  9:14 ` Greg Kroah-Hartman
2022-11-08 12:45   ` Greg Kroah-Hartman
2022-11-08 16:51     ` Luiz Capitulino
2022-11-08 17:10       ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).