Skip to contentSkip to conversation

bcache: fix 0day error of setting writeback_rate by sysfs interface

PATCH In review Coly Li 3 participants 7 messages Aug 10, 2018 – Aug 12, 2018

Subsystems (3)
lore
Share & tools
lore

Coly Li

author
#0 lore raw

PATCH 1/1 bcache: fix 0day error of setting writeback_rate by sysfs interface

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>
---
 drivers/md/bcache/sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/bcache/sysfs.c +10 -3
index 543b06408321..150cf4f4cf74 100644
@@ -267,10 +267,17 @@ STORE(__cached_dev)
 	sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
 
 	if (attr == &sysfs_writeback_rate) {
-		int v;
+		ssize_t ret;
+		long int v = atomic_long_read(&dc->writeback_rate.rate);
+
+		ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
 
-		sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
-		atomic_long_set(&dc->writeback_rate.rate, v);
+		if (!ret) {
+			atomic_long_set(&dc->writeback_rate.rate, v);
+			ret = size;
+		}
+
+		return ret;
 	}
 
 	sysfs_strtoul_clamp(writeback_rate_update_seconds,
Signature · 1 line
2.18.0
#1 lore raw

Thanks for cc. How is this exploitable? I mean only root can write to sysfs? Or do you mean by allowing a user via sudo to write to that entry?

Stefan

Am 10.08.2018 um 17:45 schrieb Coly Li:

Quoted 55 lines

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>
---
 drivers/md/bcache/sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/bcache/sysfs.c +10 -3
index 543b06408321..150cf4f4cf74 100644
@@ -267,10 +267,17 @@ STORE(__cached_dev)
 	sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
 
 	if (attr == &sysfs_writeback_rate) {
-		int v;
+		ssize_t ret;
+		long int v = atomic_long_read(&dc->writeback_rate.rate);
+
+		ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
 
-		sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
-		atomic_long_set(&dc->writeback_rate.rate, v);
+		if (!ret) {
+			atomic_long_set(&dc->writeback_rate.rate, v);
+			ret = size;
+		}
+
+		return ret;
 	}
 
 	sysfs_strtoul_clamp(writeback_rate_update_seconds,
maintainer
#3 lore raw
In reply to Coly Li
Quoted 17 lines · Coly Li

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

Quoted 8 lines

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>

You don't need to CC stable for this, the commit isn't in a released kernel yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.

Also, the fixes line should be:

Fixes ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")

(no Commit). I missed that on some of your earlier commits.

Applied with those edits, and some wording changes.

Signature · 1 line
Jens Axboe
author
#2 lore raw
In reply to Stefan Priebe - Profihost AG
Quoted 2 lines · Stefan Priebe - Profihost AG

Thanks for cc. How is this exploitable? I mean only root can write to sysfs? Or do you mean by allowing a user via sudo to write to that entry?

Hi Stefan,

This is not a security 0day bug, this is an error reported by Linux kernel 0day test service
(https://01.org/zh/lkp/documentation/0-day-test-service). My development tree is registered and monitored by 0day testing service, so if there is any static code error or boot failure, I can be noticed in very early stage.

The bug in previous patch is, writeback_rate cannot be set by sysfs interface, because sysfs_strtoul_clamp() directly returns. This patch fixes this and allows writeback_rate can be manually set again.

Coly Li

Quoted 57 lines

Am 10.08.2018 um 17:45 schrieb Coly Li:

Quoted 55 lines

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>
---
 drivers/md/bcache/sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/bcache/sysfs.c +10 -3
index 543b06408321..150cf4f4cf74 100644
@@ -267,10 +267,17 @@ STORE(__cached_dev)
 	sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
 
 	if (attr == &sysfs_writeback_rate) {
-		int v;
+		ssize_t ret;
+		long int v = atomic_long_read(&dc->writeback_rate.rate);
+
+		ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
 
-		sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
-		atomic_long_set(&dc->writeback_rate.rate, v);
+		if (!ret) {
+			atomic_long_set(&dc->writeback_rate.rate, v);
+			ret = size;
+		}
+
+		return ret;
 	}
 
 	sysfs_strtoul_clamp(writeback_rate_update_seconds,
author
#4 lore raw
In reply to Jens Axboe
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Coly Li

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

Quoted 9 lines
Quoted 8 lines

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>

Hi Jens,

Quoted 3 lines

You don't need to CC stable for this, the commit isn't in a released kernel yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.

What I thought was, Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") is a stable fix, so this patch should go into stable as well. Otherwise the stable kernel cannot manually set writeback_rate via sysfs interface.

Quoted 5 lines

Also, the fixes line should be:

Fixes ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")

(no Commit). I missed that on some of your earlier commits.

Copied, keep in mind for next time.

Quoted 3 lines

Applied with those edits, and some wording changes.

Thanks for your help !

Coly Li

maintainer
#5 lore raw
Quoted 31 lines · Coly Li
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Coly Li

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

Quoted 9 lines
Quoted 8 lines

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>

Hi Jens,

Quoted 8 lines
Quoted 3 lines

You don't need to CC stable for this, the commit isn't in a released kernel yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.

What I thought was, Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") is a stable fix, so this patch should go into stable as well. Otherwise the stable kernel cannot manually set writeback_rate via sysfs interface.

It's not necessary when new patch is correctly marked as fixing one that is going to stable.

Signature · 1 line
Jens Axboe
author
#6 lore raw
Quoted 36 lines · Jens Axboe
Quoted 35 lines · Coly Li
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Coly Li

Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") changes struct bch_ratelimit member rate from uint32_t to atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c to set new writeback rate, after the input is converted from memory buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return inside sysfs_strtoul_clamp() so the following atomic_long_set() won't be called. This error is detected by 0day system with following snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized symbol 'v'.

270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@271 atomic_long_set(&dc->writeback_rate.rate, v);

Quoted 9 lines
Quoted 8 lines

This patch fixes the above error by using strtoul_safe_clamp() to convert the input buffer into a long int type result.

Fixes Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle")
Cc Kai Krakow <[email protected]>
Cc Stefan Priebe <[email protected]>

Hi Jens,

Quoted 3 lines

You don't need to CC stable for this, the commit isn't in a released kernel yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.

Quoted 8 lines
Quoted 4 lines

What I thought was, Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is idle") is a stable fix, so this patch should go into stable as well. Otherwise the stable kernel cannot manually set writeback_rate via sysfs interface.

It's not necessary when new patch is correctly marked as fixing one that is going to stable.

Copied. Thanks for the hint.

Coly Li