stm class: Fix out of bound access from bitmap allocation
Subsystems (5)
PATCH 1/1 stm class: Fix out of bound access from bitmap allocation
From: Mulu He <[email protected]>
Bitmap allocation works on array of unsigned longs and for stm master allocation when the number of software channels is 32, 4 bytes are allocated and there is a out of bound access at the first 8 bytes access of bitmap region.
--- drivers/hwtracing/stm/core.c | 2 +- drivers/hwtracing/stm/stm.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/hwtracing/stm/core.c +1 -1
@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx)struct stp_master *master;size_t size; - size =ALIGN (stm->data->sw_nchannels,8 ) /8 ; + size =ALIGN (stm->data->sw_nchannels,STM_MASTER_SZ ) /STM_MASTER_SZ ; size +=sizeof (struct stp_master ); master =kzalloc (size,GFP_ATOMIC );if (!master)
drivers/hwtracing/stm/stm.h +2 -0
@@ -12,6 +12,8 @@#include <linux/configfs.h> +#define STM_MASTER_SZ sizeof(unsigned long) +struct stp_policy ;struct stp_policy_node ;struct stm_protocol_driver ;
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
From: Sai Prakash Ranjan
Quoted 23 lines
Sent: 05 April 2019 13:23
From: Mulu He <[email protected]>
Bitmap allocation works on array of unsigned longs and for stm master allocation when the number of software channels is 32, 4 bytes are allocated and there is a out of bound access at the first 8 bytes access of bitmap region.
Fixes 7bd1d4093c2f ("stm class: Introduce an abstraction for System Trace Module devices")Signed-off-by Mulu He <[email protected]>Signed-off-by Sai Prakash Ranjan <[email protected]>--- drivers/hwtracing/stm/core.c | 2 +- drivers/hwtracing/stm/stm.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)drivers/hwtracing/stm/core.c +0
-0
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device *stm, unsigned int idx) · line 171
Quoted 5 lines
drivers/hwtracing/stm/core.c +1
-1@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx)struct stp_master *master;size_t size; - size =ALIGN (stm->data->sw_nchannels,8 ) /8 ; + size =ALIGN (stm->data->sw_nchannels,STM_MASTER_SZ ) /STM_MASTER_SZ ;
I'm not sure that using STP_MASTER_SZ improves readability at all.
Is there something that gives the size of a bitmap for 'n' items?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 10 lines · David Laight
From: Sai Prakash Ranjan
Quoted 9 lines
drivers/hwtracing/stm/core.c +0
-0@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx)struct stp_master *master;size_t size;
Quoted 5 lines
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device *stm, unsigned int idx) · line 171
Quoted 2 lines
drivers/hwtracing/stm/core.c +1
-1- size =ALIGN (stm->data->sw_nchannels,8 ) /8 ; + size =ALIGN (stm->data->sw_nchannels,STM_MASTER_SZ ) /STM_MASTER_SZ ;I'm not sure that using
STP_MASTER_SZimproves readability at all.
I thought it was better to have a macro than directly specifying sizeof(unsigned long), anyways I can change it.
Quoted 2 lines
Is there something that gives the size of a bitmap for 'n' items?
Not sure if there is something.
Thanks,
Sai
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
Quoted 19 lines · Sai Prakash Ranjan
Quoted 11 lines · David Laight
From: Sai Prakash Ranjan
Quoted 10 lines
drivers/hwtracing/stm/core.c +0
-0@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx) struct stp_master *master; size_t size;Quoted 6 lines
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device · line 173
Quoted 3 lines
drivers/hwtracing/stm/core.c +1
-1- size = ALIGN(stm->data->sw_nchannels, 8) / 8; + size = ALIGN(stm->data->sw_nchannels, STM_MASTER_SZ) /STM_MASTER_SZ ;I'm not sure that using
STP_MASTER_SZimproves readability at all.
Quoted 7 lines
I thought it was better to have a macro than directly specifying sizeof(unsigned long), anyways I can change it.
Quoted 2 lines
Is there something that gives the size of a bitmap for 'n' items?
Not sure if there is something.
If you were to ask the question "how does the bitmap code itself know what the total size of a bitmap is?", that would quickly lead you towards BITS_TO_LONGS() ;)
And given that stp_master::chan_map is already an appropriate type, that suggests simplifying the entire calculation down to something neat and tidy like:
size = offsetof(struct stp_master, chan_map[BITS_TO_LONGS(stm->data->sw_nchannels)]);
Robin.
From: Robin Murphy
Quoted 33 lines
Sent: 08 April 2019 11:24
Quoted 19 lines · Sai Prakash Ranjan
Quoted 11 lines · David Laight
From: Sai Prakash Ranjan
Quoted 10 lines
drivers/hwtracing/stm/core.c +0
-0@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx) struct stp_master *master; size_t size;Quoted 6 lines
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device · line 173
Quoted 3 lines
drivers/hwtracing/stm/core.c +1
-1- size = ALIGN(stm->data->sw_nchannels, 8) / 8; + size = ALIGN(stm->data->sw_nchannels, STM_MASTER_SZ) /STM_MASTER_SZ ;I'm not sure that using
STP_MASTER_SZimproves readability at all.Quoted 7 lines
I thought it was better to have a macro than directly specifying sizeof(unsigned long), anyways I can change it.
Quoted 2 lines
Is there something that gives the size of a bitmap for 'n' items?
Not sure if there is something.
If you were to ask the question "how does the bitmap code itself know what the total size of a bitmap is?", that would quickly lead you towards
BITS_TO_LONGS();)
Quoted 5 lines
And given that
stp_master::chan_mapis already an appropriate type, that suggests simplifying the entire calculation down to something neat and tidy like:size =
offsetof(struct stp_master, chan_map[BITS_TO_LONGS(stm->data->sw_nchannels)]);
Except that is invalid.
You can't use offsetof() with something that isn't a compile time constant.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 34 lines · David Laight
From: Robin Murphy
Quoted 33 lines
Sent: 08 April 2019 11:24
Quoted 19 lines · Sai Prakash Ranjan
Quoted 11 lines · David Laight
From: Sai Prakash Ranjan
Quoted 10 lines
drivers/hwtracing/stm/core.c +0
-0@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx) struct stp_master *master; size_t size;Quoted 6 lines
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device · line 173
Quoted 3 lines
drivers/hwtracing/stm/core.c +1
-1- size = ALIGN(stm->data->sw_nchannels, 8) / 8; + size = ALIGN(stm->data->sw_nchannels, STM_MASTER_SZ) /STM_MASTER_SZ ;I'm not sure that using
STP_MASTER_SZimproves readability at all.Quoted 7 lines
I thought it was better to have a macro than directly specifying sizeof(unsigned long), anyways I can change it.
Quoted 2 lines
Is there something that gives the size of a bitmap for 'n' items?
Not sure if there is something.
If you were to ask the question "how does the bitmap code itself know what the total size of a bitmap is?", that would quickly lead you towards
BITS_TO_LONGS();)
Quoted 8 lines
Quoted 5 lines
And given that
stp_master::chan_mapis already an appropriate type, that suggests simplifying the entire calculation down to something neat and tidy like:size =
offsetof(struct stp_master, chan_map[BITS_TO_LONGS(stm->data->sw_nchannels)]);Except that is invalid.
You can't useoffsetof()with something that isn't a compile time constant.
Oh, I see the standard does actually say that, although there seem to be enough non-constant uses in the kernel to suggest that it still works in practice.
However, while writing the above I was still trying to remember the other thing I'd seen for handling precisely this variable-sized-struct situation, which I've now found again, namely struct_size(). I guess now I understand why that isn't implemented in terms of offsetof(), thanks for the nudge :)
Robin.
From: Robin Murphy
Quoted 40 lines
Sent: 08 April 2019 11:52
Quoted 38 lines · David Laight
From: Robin Murphy
Quoted 37 lines
Sent: 08 April 2019 11:24
Quoted 19 lines · Sai Prakash Ranjan
Quoted 11 lines · David Laight
From: Sai Prakash Ranjan
Quoted 10 lines
drivers/hwtracing/stm/core.c +0
-0@@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device *stm, unsigned int idx) struct stp_master *master; size_t size;Quoted 6 lines
drivers/hwtracing/stm/core.c static int stp_master_alloc(struct stm_device · line 173
Quoted 3 lines
drivers/hwtracing/stm/core.c +1
-1- size = ALIGN(stm->data->sw_nchannels, 8) / 8; + size = ALIGN(stm->data->sw_nchannels, STM_MASTER_SZ) /STM_MASTER_SZ ;I'm not sure that using
STP_MASTER_SZimproves readability at all.Quoted 7 lines
I thought it was better to have a macro than directly specifying sizeof(unsigned long), anyways I can change it.
Quoted 2 lines
Is there something that gives the size of a bitmap for 'n' items?
Not sure if there is something.
If you were to ask the question "how does the bitmap code itself know what the total size of a bitmap is?", that would quickly lead you towards
BITS_TO_LONGS();)And given that
stp_master::chan_mapis already an appropriate type, that suggests simplifying the entire calculation down to something neat and tidy like:
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
size =
offsetof(struct stp_master, chan_map[BITS_TO_LONGS(stm->data->sw_nchannels)]);Except that is invalid.
You can't useoffsetof()with something that isn't a compile time constant.Oh, I see the standard does actually say that, although there seem to be enough non-constant uses in the kernel to suggest that it still works in practice.
One of the compilers we use here complains about it - won't be a linux one. It also fails to generate compile time constants (for static initialisers) if you multiply or divide the result of anything that looks like offsetof() applied to an array element (which is a bug).
I'd guess that the requirement in the standard was added because some compilers were generating a result that wasn't a compile-time constant and no one thought you'd want to pass a non-constant argument.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Alexander Shishkin
maintainerQuoted 6 lines · Sai Prakash Ranjan
From: Mulu He <[email protected]>
Bitmap allocation works on array of unsigned longs and for stm master allocation when the number of software channels is 32, 4 bytes are allocated and there is a out of bound access at the first 8 bytes access of bitmap region.
Does the below fix the problem for you?
Quoted 1 line
From
fb22b9ab109b332e58d72df13563e270befbd0e3Mon Sep 17 00:00:00 2001
From: Alexander Shishkin <[email protected]>
Date: Tue, 16 Apr 2019 17:47:02 +0300
Subject: [PATCH] stm class: Fix channel bitmap on 32-bit systems
Commit 7bd1d4093c2f ("stm class: Introduce an abstraction for System Trace Module devices") naively calculates the channel bitmap size in 64-bit chunks regardless of the size of underlying unsigned long, making the bitmap half as big on a 32-bit system. This leads to an out of bounds access with the upper half of the bitmap.
Fix this by using BITS_TO_LONGS. While at it, convert to using struct_size() for the total size calculation of the master struct.
--- drivers/hwtracing/stm/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/hwtracing/stm/core.c +2 -4
@@ -166,11 +166,9 @@ stm_master(struct stm_device *stm, unsigned int idx)static int stp_master_alloc (struct stm_device *stm,unsigned int idx) {struct stp_master *master; -size_t size; - size =ALIGN (stm->data->sw_nchannels,8 ) /8 ; - size +=sizeof (struct stp_master ); - master =kzalloc (size,GFP_ATOMIC ); + master =kzalloc (struct_size (master, chan_map,BITS_TO_LONGS (stm->data->sw_nchannels)), +GFP_ATOMIC );if (!master)return -ENOMEM ;
Signature · 1 line
2.20.1
Quoted 42 lines · Alexander Shishkin
Quoted 6 lines · Sai Prakash Ranjan
From: Mulu He <[email protected]>
Bitmap allocation works on array of unsigned longs and for stm master allocation when the number of software channels is 32, 4 bytes are allocated and there is a out of bound access at the first 8 bytes access of bitmap region.
Does the below fix the problem for you?
From
fb22b9ab109b332e58d72df13563e270befbd0e3Mon Sep 17 00:00:00 2001
From: Alexander Shishkin <[email protected]>
Date: Tue, 16 Apr 2019 17:47:02 +0300
Subject: [PATCH] stm class: Fix channel bitmap on 32-bit systemsCommit
7bd1d4093c2f("stm class: Introduce an abstraction for System Trace Module devices") naively calculates the channel bitmap size in 64-bit chunks regardless of the size of underlying unsigned long, making the bitmap half as big on a 32-bit system. This leads to an out of bounds access with the upper half of the bitmap.Fix this by using
BITS_TO_LONGS. While at it, convert to usingstruct_size()for the total size calculation of the master struct.Signed-off-by Alexander Shishkin <[email protected]>Fixes 7bd1d4093c2f ("stm class: Introduce an abstraction for System Trace Module devices")Reported-by Mulu He <[email protected]>Cc [email protected] # v4.4+--- drivers/hwtracing/stm/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)drivers/hwtracing/stm/core.c +0
-0@@ -166,11 +166,9 @@ stm_master(struct stm_device *stm, unsigned int idx)static int stp_master_alloc (struct stm_device *stm,unsigned int idx) {struct stp_master *master;
drivers/hwtracing/stm/core.c stm_master(struct stm_device *stm, unsigned int idx) · line 174
Quoted 10 lines
drivers/hwtracing/stm/core.c +2
-4-size_t size; - size =ALIGN (stm->data->sw_nchannels,8 ) /8 ; - size +=sizeof (struct stp_master ); - master =kzalloc (size,GFP_ATOMIC ); + master =kzalloc (struct_size (master, chan_map,BITS_TO_LONGS (stm->data->sw_nchannels)), +GFP_ATOMIC );if (!master)return -ENOMEM ;
++ David
Yes it does fix the issue. Actually initial fix internally was using BITS_TO_LONGS, don't no why they deferred from it.
Anyways thanks for the patch.
- Sai
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation