Skip to contentSkip to conversation

raw: test against runtime value of max_raw_minors

PATCH In review Paul Bolle 3 participants 4 messages Feb 4, 2014 – Feb 5, 2014 Char and misc drivers

lore
Share & tools
lore

Paul Bolle

author
#0 lore raw

PATCH 1/2 raw: test against runtime value of max_raw_minors

bind_get() checks the device number it is called with. It uses MAX_RAW_MINORS for the upper bound. But MAX_RAW_MINORS is set at compile time while the actual number of raw devices can be set at runtime. This means the test can either be too strict or too lenient. And if the test ends up being too lenient bind_get() might try to access memory beyond what was allocated for "raw_devices".

So check against the runtime value (max_raw_minors) in this function.

---
 drivers/char/raw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/raw.c +1 -1
index f3223aa..6e8d65e 100644
@@ -190,7 +190,7 @@ static int bind_get(int number, dev_t *dev)
 	struct raw_device_data *rawdev;
 	struct block_device *bdev;
 
-	if (number <= 0 || number >= MAX_RAW_MINORS)
+	if (number <= 0 || number >= max_raw_minors)
 		return -EINVAL;
 
 	rawdev = &raw_devices[number];
Signature · 1 line
1.8.5.3
#1 lore raw
Quoted 19 lines · Paul Bolle

bind_get() checks the device number it is called with. It uses MAX_RAW_MINORS for the upper bound. But MAX_RAW_MINORS is set at compile time while the actual number of raw devices can be set at runtime. This means the test can either be too strict or too lenient. And if the test ends up being too lenient bind_get() might try to access memory beyond what was allocated for "raw_devices".

So check against the runtime value (max_raw_minors) in this function.

---
 drivers/char/raw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/raw.c +0 -0
index f3223aa..6e8d65e 100644

drivers/char/raw.c static int bind_get(int number, dev_t *dev) · line 193

Quoted 5 lines
drivers/char/raw.c +1 -1
@@ -190,7 +190,7 @@ static int bind_get(int number, dev_t *dev)
 	struct raw_device_data *rawdev;
 	struct block_device *bdev;
 
-	if (number <= 0 || number >= MAX_RAW_MINORS)
+	if (number <= 0 || number >= max_raw_minors)

Are you sure? For some reason, I thought this was changed to be this way a long time ago, can you please dig through the git archives, and even the history.git tree, to verify that this is correct and you aren't just making this be as it was before?

thanks,

greg k-h

author
#2 lore raw

[Added Jan Kara.]

Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Paul Bolle

bind_get() checks the device number it is called with. It uses MAX_RAW_MINORS for the upper bound. But MAX_RAW_MINORS is set at compile time while the actual number of raw devices can be set at runtime. This means the test can either be too strict or too lenient. And if the test ends up being too lenient bind_get() might try to access memory beyond what was allocated for "raw_devices".

So check against the runtime value (max_raw_minors) in this function.

---
 drivers/char/raw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/raw.c +0 -0
index f3223aa..6e8d65e 100644
@@ -190,7 +190,7 @@ static int bind_get(int number, dev_t *dev)
 	struct raw_device_data *rawdev;
 	struct block_device *bdev;
 
Quoted 7 lines

drivers/char/raw.c static int bind_get(int number, dev_t *dev) · line 193

Quoted 2 lines
drivers/char/raw.c +1 -1
-	if (number <= 0 || number >= MAX_RAW_MINORS)
+	if (number <= 0 || number >= max_raw_minors)

Are you sure? For some reason, I thought this was changed to be this way a long time ago, can you please dig through the git archives, and even the history.git tree, to verify that this is correct and you aren't just making this be as it was before?

What apparently happened was that in v3.0, through commit 0078bff5283d ("Allow setting of number of raw devices as a module parameter"), the test in bind_set() was updated but the test in bind_get() not.

You can - sort of - see this by comparing

    git grep -nwi max_raw_minors 0078bff5283d^

and

    git grep -nwi max_raw_minors 0078bff5283d

Paul Bolle

#3 lore raw
Quoted 38 lines · Paul Bolle

[Added Jan Kara.]

Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Paul Bolle

bind_get() checks the device number it is called with. It uses MAX_RAW_MINORS for the upper bound. But MAX_RAW_MINORS is set at compile time while the actual number of raw devices can be set at runtime. This means the test can either be too strict or too lenient. And if the test ends up being too lenient bind_get() might try to access memory beyond what was allocated for "raw_devices".

So check against the runtime value (max_raw_minors) in this function.

---
 drivers/char/raw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/raw.c +0 -0
index f3223aa..6e8d65e 100644
@@ -190,7 +190,7 @@ static int bind_get(int number, dev_t *dev)
 	struct raw_device_data *rawdev;
 	struct block_device *bdev;
 
Quoted 7 lines

drivers/char/raw.c static int bind_get(int number, dev_t *dev) · line 193

Quoted 2 lines
drivers/char/raw.c +1 -1
-	if (number <= 0 || number >= MAX_RAW_MINORS)
+	if (number <= 0 || number >= max_raw_minors)

Are you sure? For some reason, I thought this was changed to be this way a long time ago, can you please dig through the git archives, and even the history.git tree, to verify that this is correct and you aren't just making this be as it was before?

What apparently happened was that in v3.0, through commit 0078bff5283d ("Allow setting of number of raw devices as a module parameter"), the test in bind_set() was updated but the test in bind_get() not.

Quoted 5 lines

You can - sort of - see this by comparing

    git grep -nwi max_raw_minors 0078bff5283d^

and

    git grep -nwi max_raw_minors 0078bff5283d

Yeah, that was clearly an ommision on my side. Thanks for the fix!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR