powerpc/kexec: Annotate umem_info members with __counted_by_ptr
PATCH 1/1 powerpc/kexec: Annotate umem_info members with __counted_by_ptr
Add __counted_by_ptr() to umem_info::buf and umem_info::ranges to improve access bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.
Set the count fields before assigning the corresponding pointers, return early on krealloc() failure, and use sizeof(*buf) when deriving max_entries from the allocation size.
--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
arch/powerpc/kexec/file_load_64.c +12 -10
@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };const struct kexec_file_ops *const kexec_file_loaders[] = {
@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) { - um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf) +return NULL ; + + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;return tbuf; }
@@ -288,13 +290,13 @@ static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)return -EINVAL ; } - um_info.buf =NULL ; um_info.size =0 ; um_info.max_entries =0 ; - um_info.idx =0 ; + um_info.buf =NULL ; + um_info.idx =0 ;/* Memory ranges to look up */ - um_info.ranges = &(usable_mem->ranges[0 ]); um_info.nr_ranges = usable_mem->nr_ranges; + um_info.ranges = usable_mem->ranges; dn =of_find_node_by_path ("/ibm,dynamic-reconfiguration-memory" );if (dn) {
Quoted 33 lines · Thorsten Blum
Add
__counted_by_ptr()toumem_info::bufandumem_info::rangesto improve access bounds checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Set the count fields before assigning the corresponding pointers, return early on
krealloc()failure, and usesizeof(*buf)when derivingmax_entriesfrom the allocation size.Signed-off-by Thorsten Blum <[email protected]>--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)arch/powerpc/kexec/file_load_64.c +3
-2@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };
[...]
Quoted 7 lines
const struct kexec_file_ops * const kexec_file_loaders[] = {arch/powerpc/kexec/file_load_64.c +0
-1@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) {
arch/powerpc/kexec/file_load_64.c static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) · line 92
Quoted 10 lines
arch/powerpc/kexec/file_load_64.c +6
-4- um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf) +return NULL ; + + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;
Could you please explain why size and max_entries are updated before the buffer itself?
- Sourabh Jain
Quoted 21 lines
arch/powerpc/kexec/file_load_64.c +3
-3return tbuf; }@@ -288,13 +290,13 @@ static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)return -EINVAL ; } - um_info.buf =NULL ; um_info.size =0 ; um_info.max_entries =0 ; - um_info.idx =0 ; + um_info.buf =NULL ; + um_info.idx =0 ;/* Memory ranges to look up */ - um_info.ranges = &(usable_mem->ranges[0 ]); um_info.nr_ranges = usable_mem->nr_ranges; + um_info.ranges = usable_mem->ranges; dn =of_find_node_by_path ("/ibm,dynamic-reconfiguration-memory" );if (dn) {
Quoted 44 lines · Sourabh Jain
Quoted 31 lines · Thorsten Blum
Add
__counted_by_ptr()toumem_info::bufandumem_info::rangesto improve access bounds checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Set the count fields before assigning the corresponding pointers, return early on
krealloc()failure, and usesizeof(*buf)when derivingmax_entriesfrom the allocation size.Signed-off-by Thorsten Blum <[email protected]>--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)arch/powerpc/kexec/file_load_64.c +3
-2@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };[...]
Quoted 11 lines
const struct kexec_file_ops * const kexec_file_loaders[] = {arch/powerpc/kexec/file_load_64.c +2
-5@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) { - um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf) +return NULL ;
Quoted 8 lines
arch/powerpc/kexec/file_load_64.c static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) · line 92
Quoted 4 lines
arch/powerpc/kexec/file_load_64.c +4
-0+ + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;Could you please explain why size and
max_entriesare updated before the buffer itself?
__counted_by_ptr() requires the counter ->max_entries to be set before the ->buf pointer is assigned; otherwise you have an inconsistent state where the counter doesn't match the pointer.
And ->size is moved up because ->max_entries depends on it.
Quoted 52 lines · Thorsten Blum
Quoted 43 lines · Sourabh Jain
Quoted 31 lines · Thorsten Blum
Add
__counted_by_ptr()toumem_info::bufandumem_info::rangesto improve access bounds checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Set the count fields before assigning the corresponding pointers, return early on
krealloc()failure, and usesizeof(*buf)when derivingmax_entriesfrom the allocation size.Signed-off-by Thorsten Blum <[email protected]>--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)arch/powerpc/kexec/file_load_64.c +3
-2@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };[...]
Quoted 10 lines
const struct kexec_file_ops * const kexec_file_loaders[] = {arch/powerpc/kexec/file_load_64.c +1
-5@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) { - um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf)Quoted 8 lines
arch/powerpc/kexec/file_load_64.c static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) · line 92
Quoted 5 lines
arch/powerpc/kexec/file_load_64.c +5
-0+return NULL ; + + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;Could you please explain why size and
max_entriesare updated before the buffer itself?
Quoted 3 lines
__counted_by_ptr()requires the counter ->max_entries to be set before the ->buf pointer is assigned; otherwise you have an inconsistent state where the counter doesn't match the pointer.
But isn't updating the counter holding the buffer size before the actual buffer can cause problems?
Can you share the document link of __counted_by_ptr() which says that size counter to be updated before the buffer pointer.
Here is an example in fs/coredump.c file where size counter is updated after the buffer with __counter_by_ptr():
https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L98
https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L115
- Sourabh Jain
Quoted 1 line
And ->size is moved up because ->max_entries depends on it.
Quoted 65 lines · Sourabh Jain
Quoted 52 lines · Thorsten Blum
Quoted 43 lines · Sourabh Jain
Quoted 31 lines · Thorsten Blum
Add
__counted_by_ptr()toumem_info::bufandumem_info::rangesto improve access bounds checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Set the count fields before assigning the corresponding pointers, return early on
krealloc()failure, and usesizeof(*buf)when derivingmax_entriesfrom the allocation size.Signed-off-by Thorsten Blum <[email protected]>--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)arch/powerpc/kexec/file_load_64.c +3
-2@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };[...]
Quoted 10 lines
const struct kexec_file_ops * const kexec_file_loaders[] = {arch/powerpc/kexec/file_load_64.c +1
-5@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) { - um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf)Quoted 8 lines
arch/powerpc/kexec/file_load_64.c static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) · line 92
Quoted 5 lines
arch/powerpc/kexec/file_load_64.c +5
-0+return NULL ; + + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;Could you please explain why size and
max_entriesare updated before the buffer itself?Quoted 3 lines
__counted_by_ptr()requires the counter ->max_entries to be set before the ->buf pointer is assigned; otherwise you have an inconsistent state where the counter doesn't match the pointer.But isn't updating the counter holding the buffer size before the actual buffer
can cause problems?Can you share the document link of
__counted_by_ptr()which says that size counter to be
updated before the buffer pointer.
Quoted 6 lines
Here is an example in
fs/coredump.cfile where size counter is updated after the buffer
with__counter_by_ptr():https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L98
https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L115
Maybe the order only matters for flexible arrays and __counted_by(), but not for __counted_by_ptr(). I reordered it mostly out of habit from __counted_by() annotations and assumed the same rules apply for __counted_by_ptr(). Happy to restore the old order if it's not required.
Kees or Gustavo, what's your take on this?
Thanks,
Thorsten
Quoted 73 lines · Thorsten Blum
Quoted 65 lines · Sourabh Jain
Quoted 52 lines · Thorsten Blum
Quoted 43 lines · Sourabh Jain
Quoted 31 lines · Thorsten Blum
Add
__counted_by_ptr()toumem_info::bufandumem_info::rangesto improve access bounds checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Set the count fields before assigning the corresponding pointers, return early on
krealloc()failure, and usesizeof(*buf)when derivingmax_entriesfrom the allocation size.Signed-off-by Thorsten Blum <[email protected]>--- arch/powerpc/kexec/file_load_64.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)arch/powerpc/kexec/file_load_64.c +3
-2@@ -34,14 +34,15 @@#include <asm/cputhreads.h> struct umem_info { -__be64 *buf;/* data buffer for usable-memory property */ +/* data buffer for usable-memory property */ + __be64 *buf __counted_by_ptr(max_entries);u32 size;/* size allocated for the data buffer */ u32 max_entries;/* maximum no. of entries */ u32 idx;/* index of current entry */ /* usable memory ranges to look up */ unsigned int nr_ranges; -const struct range *ranges; + const struct range *ranges __counted_by_ptr(nr_ranges); };[...]
Quoted 10 lines
const struct kexec_file_ops * const kexec_file_loaders[] = {arch/powerpc/kexec/file_load_64.c +1
-5@@ -83,11 +84,12 @@ static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) new_size = um_info->size +MEM_RANGE_CHUNK_SZ ; tbuf =krealloc (um_info->buf, new_size,GFP_KERNEL ); -if (tbuf) { - um_info->buf = tbuf; - um_info->size = new_size; - um_info->max_entries = (um_info->size /sizeof (u64)); - } +if (!tbuf)Quoted 8 lines
arch/powerpc/kexec/file_load_64.c static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) · line 92
Quoted 5 lines
arch/powerpc/kexec/file_load_64.c +5
-0+return NULL ; + + um_info->size = new_size; + um_info->max_entries = um_info->size /sizeof (*um_info->buf); + um_info->buf = tbuf;Could you please explain why size and
max_entriesare updated before the buffer itself?Quoted 3 lines
__counted_by_ptr()requires the counter ->max_entries to be set before the ->buf pointer is assigned; otherwise you have an inconsistent state where the counter doesn't match the pointer.But isn't updating the counter holding the buffer size before the actual buffer
can cause problems?Can you share the document link of
__counted_by_ptr()which says that size counter to be
updated before the buffer pointer.Quoted 6 lines
Here is an example in
fs/coredump.cfile where size counter is updated after the buffer
with__counter_by_ptr():https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L98
https://github.com/torvalds/linux/blob/a90ee4305c4a5df72c11b31dacfdc76e00fcf78a/fs/coredump.c#L115
Quoted 6 lines
Maybe the order only matters for flexible arrays and
__counted_by(), but not for__counted_by_ptr(). I reordered it mostly out of habit from__counted_by()annotations and assumed the same rules apply for__counted_by_ptr(). Happy to restore the old order if it's not required.Kees or Gustavo, what's your take on this?
The order matters when dereferencing the pointer. In this case, it's just an assignment.
Thanks
-Gustavo