mm/writeback: fix possible divide-by-zero in wb_dirty_limits(), again
Subsystems (3)
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)
--- mm/page-writeback.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page-writeback.c +1 -1
@@ -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
Jan Kara
maintainerQuoted 15 lines · Zach O'Keefe
(struct
dirty_throttle_control*)->thresh is an unsigned long, but is passed as the u32 divisor argument todiv_u64(). On architectures where unsigned long is 64 bytes, the argument will be implicitly truncated.Use
div64_u64()instead ofdiv_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 thatdtc->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]>Cc <[email protected]>Signed-off-by Zach O'Keefe <[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@@ -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
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 todiv_u64(). On architectures where unsigned long is 64 bytes, the argument will be implicitly truncated.Use
div64_u64()instead ofdiv_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 thatdtc->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]>Cc <[email protected]>Signed-off-by Zach O'Keefe <[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@@ -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
-1dtc->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 havediv64_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
@@ -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
Jan Kara
maintainerQuoted 11 lines · Zach O'Keefe
Quoted 10 lines · Jan Kara
Quoted 10 lines
mm/page-writeback.c +1
-1@@ -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
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(structdirty_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
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@@ -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@@ -1637,8 +1637,8 @@ static inline void wb_dirty_limits(structdirty_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
-2dtc->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 passUINT_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 modifymm/page-writeback.cso 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