public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup/misc: Fix an overflow
@ 2023-07-17 18:47 Haitao Huang
       [not found] ` <20230717184719.85523-1-haitao.huang-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
  2023-07-17 18:55 ` Jarkko Sakkinen
  0 siblings, 2 replies; 15+ messages in thread
From: Haitao Huang @ 2023-07-17 18:47 UTC (permalink / raw)
  To: jarkko, dave.hansen, tj, linux-kernel, linux-sgx, cgroups,
	Zefan Li, Johannes Weiner
  Cc: vipinsh, kai.huang, reinette.chatre, zhiquan1.li, kristen

The variable 'new_usage' in misc_cg_try_charge() may overflow if it
becomes above INT_MAX. This was observed when I implement the new SGX
EPC cgroup[1] as a misc cgroup and test on a platform with large SGX EPC
sizes.

Change type of new_usage to long from int and check overflow.

Fixes: a72232eabdfcf ("cgroup: Add misc cgroup controller")
Signed-off-by: Haitao Huang <haitao.huang@linux.intel.com>

[1] https://lore.kernel.org/linux-sgx/20230712230202.47929-1-haitao.huang@linux.intel.com/
---
 kernel/cgroup/misc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c
index fe3e8a0eb7ed..ff9f900981a3 100644
--- a/kernel/cgroup/misc.c
+++ b/kernel/cgroup/misc.c
@@ -143,7 +143,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
 	struct misc_cg *i, *j;
 	int ret;
 	struct misc_res *res;
-	int new_usage;
+	long new_usage;
 
 	if (!(valid_type(type) && cg && READ_ONCE(misc_res_capacity[type])))
 		return -EINVAL;
@@ -153,10 +153,10 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
 
 	for (i = cg; i; i = parent_misc(i)) {
 		res = &i->res[type];
-
 		new_usage = atomic_long_add_return(amount, &res->usage);
 		if (new_usage > READ_ONCE(res->max) ||
-		    new_usage > READ_ONCE(misc_res_capacity[type])) {
+		    new_usage > READ_ONCE(misc_res_capacity[type]) ||
+		    new_usage < 0) {
 			ret = -EBUSY;
 			goto err_charge;
 		}
-- 
2.25.1


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

end of thread, other threads:[~2023-07-21 18:10 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-17 18:47 [PATCH] cgroup/misc: Fix an overflow Haitao Huang
     [not found] ` <20230717184719.85523-1-haitao.huang-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2023-07-17 18:51   ` Tejun Heo
2023-07-17 18:55 ` Jarkko Sakkinen
2023-07-17 18:57   ` Tejun Heo
     [not found]     ` <ZLWPN_xyGFrqqJkV-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>
2023-07-17 19:01       ` Haitao Huang
     [not found]         ` <op.178pr1qewjvjmi-yDQzE4XY+yVaPPhiJ6yCxLKMmGWinSIL2HeeBUIffwg@public.gmane.org>
2023-07-17 20:19           ` Haitao Huang
2023-07-17 20:37             ` Tejun Heo
2023-07-18  1:08               ` [PATCH 1/2] " Haitao Huang
     [not found]                 ` <20230718010845.35197-1-haitao.huang-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2023-07-18  1:08                   ` [PATCH 2/2] cgroup/misc: Change counters to be explicit 64bit types Haitao Huang
     [not found]                     ` <20230718010845.35197-2-haitao.huang-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2023-07-18 22:52                       ` Tejun Heo
     [not found]                         ` <ZLcXmvDKheCRYOjG-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>
2023-07-21  2:48                           ` Haitao Huang
2023-07-21 12:02                           ` [PATCH] cgroup/misc: Store atomic64_t reads to u64 Haitao Huang
     [not found]                             ` <20230721120231.13916-1-haitao.huang-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2023-07-21 18:10                               ` Tejun Heo
     [not found]               ` <ZLWmdBfcuPUBtk1K-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>
2023-07-18  1:11                 ` [PATCH] cgroup/misc: Fix an overflow Haitao Huang
2023-07-18 15:41                 ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox