Skip to contentSkip to conversation

mm/writeback: fix possible divide-by-zero in wb_dirty_limits(), again

PATCH Applied Zach O'Keefe 2 participants 5 messages Jan 18, 2024 – Apr 19, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/writeback: fix possible divide-by-zero in wb_dirty_limits(), again

(struct dirty_throttle_control *)->thresh is an unsigned long, but is passed as the u32 divisor argument to div_u64(). On architectures where unsigned long is 64 bytes, the argument will be implicitly truncated.

Use div64_u64() instead of div_u64() so that the value used in the "is this a safe division" check is the same as the divisor.

Also, remove redundant cast of the numerator to u64, as that should happen implicitly.

This would be difficult to exploit in memcg domain, given the ratio-based arithmetic domain_drity_limits() uses, but is much easier in global writeback domain with a BDI_CAP_STRICTLIMIT-backing device, using e.g. vm.dirty_bytes=(1<<32)*PAGE_SIZE so that dtc->thresh == (1<<32)

Fixes f6789593d5ce ("mm/page-writeback.c: fix divide by zero in bdi_dirty_limits()")
Cc Maxim Patlasov <[email protected]>
---
 mm/page-writeback.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page-writeback.c +1 -1
index cd4e4ae77c40a..02147b61712bc 100644
@@ -1638,7 +1638,7 @@ static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
 	 */
 	dtc->wb_thresh = __wb_calc_thresh(dtc);
 	dtc->wb_bg_thresh = dtc->thresh ?
-		div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+		div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
 
 	/*
 	 * In order to avoid the stacked BDI deadlock we need
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
maintainer
#1 lore raw
Quoted 15 lines · Zach O'Keefe

(struct dirty_throttle_control *)->thresh is an unsigned long, but is passed as the u32 divisor argument to div_u64(). On architectures where unsigned long is 64 bytes, the argument will be implicitly truncated.

Use div64_u64() instead of div_u64() so that the value used in the "is this a safe division" check is the same as the divisor.

Also, remove redundant cast of the numerator to u64, as that should happen implicitly.

This would be difficult to exploit in memcg domain, given the ratio-based arithmetic domain_drity_limits() uses, but is much easier in global writeback domain with a BDI_CAP_STRICTLIMIT-backing device, using e.g. vm.dirty_bytes=(1<<32)*PAGE_SIZE so that dtc->thresh == (1<<32)

Quoted 4 lines
Fixes f6789593d5ce ("mm/page-writeback.c: fix divide by zero in bdi_dirty_limits()")
Cc Maxim Patlasov <[email protected]>

I've come across this change today and it is broken in several ways:

mm/page-writeback.c static inline void wb_dirty_limits(struct dirty_throttle_control *dtc) · line 1641

Quoted 10 lines
mm/page-writeback.c +1 -1
index cd4e4ae77c40a..02147b61712bc 100644
@@ -1638,7 +1638,7 @@ static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
 	 */
 	dtc->wb_thresh = __wb_calc_thresh(dtc);
 	dtc->wb_bg_thresh = dtc->thresh ?
-		div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+		div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

Firstly, the removed (u64) cast from the multiplication will introduce a multiplication overflow on 32-bit archs if wb_thresh * bg_thresh >= 1<<32 (which is actually common - the default settings with 4GB of RAM will trigger this). Secondly, the div64_u64() is unnecessarily expensive on 32-bit archs. We have div64_ul() in case we want to be safe & cheap. Thirdly, if thresholds are larger than 1<<32 pages, then dirty balancing is going to blow up in many other spectacular ways - consider only the multiplication on this line - it will not necessarily fit into u64 anymore. The whole dirty limiting code is interspersed with assumptions that limits are actually within u32 and we do our calculations in unsigned longs to avoid worrying about overflows (with occasional typing to u64 to make it more interesting because people expected those entities to overflow 32 bits even on 32-bit archs). Which is lame I agree but so far people don't seem to be setting limits to 16TB or more. And I'm not really worried about security here since this is global-root-only tunable and that has much better ways to DoS the system.

So overall I'm all for cleaning up this code but in a sensible way please. E.g. for these overflow issues at least do it one function at a time so that we can sensibly review it.

Andrew, can you please revert this patch until we have a better fix? So far it does more harm than good... Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
Quoted 17 lines · Jan Kara
Quoted 15 lines · Zach O'Keefe

(struct dirty_throttle_control *)->thresh is an unsigned long, but is passed as the u32 divisor argument to div_u64(). On architectures where unsigned long is 64 bytes, the argument will be implicitly truncated.

Use div64_u64() instead of div_u64() so that the value used in the "is this a safe division" check is the same as the divisor.

Also, remove redundant cast of the numerator to u64, as that should happen implicitly.

This would be difficult to exploit in memcg domain, given the ratio-based arithmetic domain_drity_limits() uses, but is much easier in global writeback domain with a BDI_CAP_STRICTLIMIT-backing device, using e.g. vm.dirty_bytes=(1<<32)*PAGE_SIZE so that dtc->thresh == (1<<32)

Quoted 6 lines
Quoted 4 lines
Fixes f6789593d5ce ("mm/page-writeback.c: fix divide by zero in bdi_dirty_limits()")
Cc Maxim Patlasov <[email protected]>

I've come across this change today and it is broken in several ways:

Thanks for picking up on this, Jan.

Quoted 7 lines
Quoted 7 lines
mm/page-writeback.c +0 -0
index cd4e4ae77c40a..02147b61712bc 100644
@@ -1638,7 +1638,7 @@ static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
       */
      dtc->wb_thresh = __wb_calc_thresh(dtc);
Quoted 8 lines

mm/page-writeback.c static inline void wb_dirty_limits(struct dirty_throttle_control *dtc) · line 1641

Quoted 3 lines
mm/page-writeback.c +1 -1
      dtc->wb_bg_thresh = dtc->thresh ?
-             div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+             div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

Firstly, the removed (u64) cast from the multiplication will introduce a multiplication overflow on 32-bit archs if wb_thresh * bg_thresh >= 1<<32 (which is actually common - the default settings with 4GB of RAM will trigger this). [..]

True, and embarrassing given I was looking at this code with a 32-bit focus. Well spotted.

Quoted 2 lines

[..] Secondly, the div64_u64() is unnecessarily expensive on 32-bit archs. We have div64_ul() in case we want to be safe & cheap.

A last-minute change vs just casting the initial "dtc->thresh ?" check. It did look expensive, but figured its existence implied it should be used. I must have missed div64_ul().

Quoted 12 lines

Thirdly, if thresholds are larger than 1<<32 pages, then dirty balancing is going to blow up in many other spectacular ways - consider only the multiplication on this line - it will not necessarily fit into u64 anymore. The whole dirty limiting code is interspersed with assumptions that limits are actually within u32 and we do our calculations in unsigned longs to avoid worrying about overflows (with occasional typing to u64 to make it more interesting because people expected those entities to overflow 32 bits even on 32-bit archs). Which is lame I agree but so far people don't seem to be setting limits to 16TB or more. And I'm not really worried about security here since this is global-root-only tunable and that has much better ways to DoS the system.

Quoted 6 lines

So overall I'm all for cleaning up this code but in a sensible way please. E.g. for these overflow issues at least do it one function at a time so that we can sensibly review it.

Andrew, can you please revert this patch until we have a better fix? So far it does more harm than good... Thanks!

Shall we just roll-forward with a suitable fix? I think all the original code actually "needed" was to cast the ternary predicate, like:

---8<---

mm/page-writeback.c +0 -0
index fba324e1a010..ca1bfc0c9bdd 100644
@@ -1637,8 +1637,8 @@ static inline void wb_dirty_limits(struct

dirty_throttle_control *dtc)

         *   at some rate <= (write_bw / 2) for bringing down wb_dirty.
         */
        dtc->wb_thresh = __wb_calc_thresh(dtc);

- dtc->wb_bg_thresh = dtc->thresh ?
- div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+ dtc->wb_bg_thresh = (u32)dtc->thresh ?
+ div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

/*
* In order to avoid the stacked BDI deadlock we need
---8<---

Thanks, and apologize for the inconvenience

Zach

Quoted 4 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

maintainer
#3 lore raw
Quoted 11 lines · Zach O'Keefe
Quoted 10 lines · Jan Kara
Quoted 10 lines
mm/page-writeback.c +1 -1
index cd4e4ae77c40a..02147b61712bc 100644
@@ -1638,7 +1638,7 @@ static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
       */
      dtc->wb_thresh = __wb_calc_thresh(dtc);
      dtc->wb_bg_thresh = dtc->thresh ?
-             div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+             div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

...

Quoted 29 lines
Quoted 12 lines

Thirdly, if thresholds are larger than 1<<32 pages, then dirty balancing is going to blow up in many other spectacular ways - consider only the multiplication on this line - it will not necessarily fit into u64 anymore. The whole dirty limiting code is interspersed with assumptions that limits are actually within u32 and we do our calculations in unsigned longs to avoid worrying about overflows (with occasional typing to u64 to make it more interesting because people expected those entities to overflow 32 bits even on 32-bit archs). Which is lame I agree but so far people don't seem to be setting limits to 16TB or more. And I'm not really worried about security here since this is global-root-only tunable and that has much better ways to DoS the system.

Quoted 6 lines

So overall I'm all for cleaning up this code but in a sensible way please. E.g. for these overflow issues at least do it one function at a time so that we can sensibly review it.

Andrew, can you please revert this patch until we have a better fix? So far it does more harm than good... Thanks!

Shall we just roll-forward with a suitable fix? I think all the original code actually "needed" was to cast the ternary predicate, like:

---8<---

mm/page-writeback.c +0 -0
index fba324e1a010..ca1bfc0c9bdd 100644

mm/page-writeback.c static inline void wb_dirty_limits(struct · line 1642

Quoted 8 lines
mm/page-writeback.c +2 -2
@@ -1637,8 +1637,8 @@ static inline void wb_dirty_limits(struct
 dirty_throttle_control *dtc)
         *   at some rate <= (write_bw / 2) for bringing down wb_dirty.
         */
        dtc->wb_thresh = __wb_calc_thresh(dtc);
-       dtc->wb_bg_thresh = dtc->thresh ?
-               div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+       dtc->wb_bg_thresh = (u32)dtc->thresh ?
+               div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

Well, this would fix the division by 0 but when you read the code you really start wondering what's going on :) And as I wrote above when thresholds pass UINT_MAX, the dirty limitting code breaks down anyway so I don't think the machine will be more usable after your fix. Would you be up for a challenge to modify mm/page-writeback.c so that such huge limits cannot be set instead? That would be actually a useful fix...

Honza

Quoted 14 lines
mm/page-writeback.c +0 -1
        /*
         * In order to avoid the stacked BDI deadlock we need
---8<---

Thanks, and apologize for the inconvenience

Zach

Quoted 4 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 46 lines · Jan Kara
Quoted 11 lines · Zach O'Keefe
Quoted 10 lines · Jan Kara
Quoted 10 lines
mm/page-writeback.c +1 -1
index cd4e4ae77c40a..02147b61712bc 100644
@@ -1638,7 +1638,7 @@ static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
       */
      dtc->wb_thresh = __wb_calc_thresh(dtc);
      dtc->wb_bg_thresh = dtc->thresh ?
-             div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+             div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

...

Quoted 32 lines
Quoted 12 lines

Thirdly, if thresholds are larger than 1<<32 pages, then dirty balancing is going to blow up in many other spectacular ways - consider only the multiplication on this line - it will not necessarily fit into u64 anymore. The whole dirty limiting code is interspersed with assumptions that limits are actually within u32 and we do our calculations in unsigned longs to avoid worrying about overflows (with occasional typing to u64 to make it more interesting because people expected those entities to overflow 32 bits even on 32-bit archs). Which is lame I agree but so far people don't seem to be setting limits to 16TB or more. And I'm not really worried about security here since this is global-root-only tunable and that has much better ways to DoS the system.

Quoted 6 lines

So overall I'm all for cleaning up this code but in a sensible way please. E.g. for these overflow issues at least do it one function at a time so that we can sensibly review it.

Andrew, can you please revert this patch until we have a better fix? So far it does more harm than good... Thanks!

Shall we just roll-forward with a suitable fix? I think all the original code actually "needed" was to cast the ternary predicate, like:

---8<---

mm/page-writeback.c +0 -0
index fba324e1a010..ca1bfc0c9bdd 100644
@@ -1637,8 +1637,8 @@ static inline void wb_dirty_limits(struct
 dirty_throttle_control *dtc)
         *   at some rate <= (write_bw / 2) for bringing down wb_dirty.
         */
Quoted 8 lines

mm/page-writeback.c static inline void wb_dirty_limits(struct · line 1642

Quoted 5 lines
mm/page-writeback.c +2 -2
        dtc->wb_thresh = __wb_calc_thresh(dtc);
-       dtc->wb_bg_thresh = dtc->thresh ?
-               div64_u64(dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
+       dtc->wb_bg_thresh = (u32)dtc->thresh ?
+               div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;

Well, this would fix the division by 0 but when you read the code you really start wondering what's going on :) [..]

Ya, this was definitely a local fix in an area of code I know very little abit. I stumbled across it in a rather contrived way -- made easier by internal patches -- and felt its existence still warranted a local fix.

Quoted 5 lines

[..] And as I wrote above when
thresholds pass UINT_MAX, the dirty limitting code breaks down anyway so I don't think the machine will be more usable after your fix. Would you be up for a challenge to modify mm/page-writeback.c so that such huge limits cannot be set instead? That would be actually a useful fix...

:) I can't say my schedule affords me much time to take on any significant unplanned work. Perhaps as a Friday afternoon exercise I'll come back to scope this out, driven by some sense of responsibility garnered from starting down this path ; but ... my TODO list is long.

Have a great rest of your day / weekend,
Zach

Quoted 19 lines

Honza

Quoted 5 lines
mm/page-writeback.c +0 -1
        /*
         * In order to avoid the stacked BDI deadlock we need
---8<---
Quoted 9 lines

Thanks, and apologize for the inconvenience

Zach

Quoted 4 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

--
Jan Kara <[email protected]>
SUSE Labs, CR