rds: Prevent kernel-infoleak in rds_notify_queue_get()
Subsystems (4)
PATCH 1/1 rds: Prevent kernel-infoleak in rds_notify_queue_get()
rds_notify_queue_get() is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end of cmsg.
In 2016 we tried to fix this issue by doing = { 0 }; on cmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by using memset() instead.
---
Note: the "real" copy_to_user() happens in put_cmsg(), where cmlen - sizeof(*cm) equals to sizeof(cmsg).
Reference: https://lwn.net/Articles/417989/
$ pahole -C "rds_rdma_notify" net/rds/recv.o
struct rds_rdma_notify {
__u64 user_token; /* 0 8 */
__s32 status; /* 8 4 *//* size: 16, cachelines: 1, members: 2 */ /* padding: 4 */ /* last cacheline: 16 bytes */ };
net/rds/recv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/rds/recv.c +2 -1
@@ -450,12 +450,13 @@ static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc,int rds_notify_queue_get (struct rds_sock *rs,struct msghdr *msghdr) {struct rds_notifier *notifier; -struct rds_rdma_notify cmsg = {0 };/* fill holes with zero */ +struct rds_rdma_notify cmsg;unsigned int count =0 , max_messages = ~0U ;unsigned long flags;LIST_HEAD (copy);int err =0 ; +memset (&cmsg,0 ,sizeof (cmsg));/* fill holes with zero */ /* put_cmsg copies to user space and thus may sleep. We can't do this * with rs_lock held, so first grab as many notifications as we can stuff
Signature · 1 line
2.25.1
Quoted 24 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Fixes f037590fff30 ("rds: fix a leak of kernel memory")Fixes bdbe6fbc6a2f ("RDS: recv.c")Suggested-by Dan Carpenter <[email protected]>Signed-off-by Peilin Ye <[email protected]>---
Note: the "real"copy_to_user()happens input_cmsg(), wherecmlen - sizeof(*cm)equals tosizeof(cmsg).Reference: https://lwn.net/Articles/417989/
$ pahole -C "rds_rdma_notify" net/rds/recv.o struct rds_rdma_notify { __u64 user_token; /* 0 8 */ __s32 status; /* 8 4 */
Quoted 8 lines
/* size: 16, cachelines: 1, members: 2 */ /* padding: 4 */ /* last cacheline: 16 bytes */ };net/rds/recv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Looks good.
FWIW,
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.
Of course, this is the difference between "{ 0 }" and "{}" initializations.
Quoted 31 lines
Fixes f037590fff30 ("rds: fix a leak of kernel memory")Fixes bdbe6fbc6a2f ("RDS: recv.c")Suggested-by Dan Carpenter <[email protected]>Signed-off-by Peilin Ye <[email protected]>---
Note: the "real"copy_to_user()happens input_cmsg(), wherecmlen - sizeof(*cm)equals tosizeof(cmsg).Reference: https://lwn.net/Articles/417989/
$ pahole -C "rds_rdma_notify" net/rds/recv.o struct rds_rdma_notify { __u64 user_token; /* 0 8 */ __s32 status; /* 8 4 *//* size: 16, cachelines: 1, members: 2 */ /* padding: 4 */ /* last cacheline: 16 bytes */ };net/rds/recv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)net/rds/recv.c +0
-0@@ -450,12 +450,13 @@ static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc,int rds_notify_queue_get (struct rds_sock *rs,struct msghdr *msghdr)
net/rds/recv.c static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc, · line 459
Quoted 10 lines
net/rds/recv.c +2
-1{struct rds_notifier *notifier; -struct rds_rdma_notify cmsg = {0 };/* fill holes with zero */ +struct rds_rdma_notify cmsg;unsigned int count =0 , max_messages = ~0U ;unsigned long flags;LIST_HEAD (copy);int err =0 ; +memset (&cmsg,0 ,sizeof (cmsg));/* fill holes with zero */
It works, but the right solution is to drop 0 from cmsg initialization and write "struct rds_rdma_notify cmsg = {};" without any memset.
Thanks
Quoted 10 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Really? Neither will handle structures with holes in it, try it and see.
thanks,
greg k-h
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines · Leon Romanovsky
Quoted 4 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Really? Neither will handle structures with holes in it, try it and see.
And if true, where in the C spec does it say that?
thanks,
greg k-h
Quoted 9 lines · Greg Kroah-Hartman
Quoted 3 lines · Greg Kroah-Hartman
Quoted 2 lines · Leon Romanovsky
Quoted 1 line · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernelstack
Quoted 1 line
Quoted 1 line
Quoted 1 line
memory to userspace since the compiler may leave a 4-byte hole at
the end
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
of
cmsg.
Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
In 2016 we tried to fix this issue by doing
= { 0 };oncmsg,which
Quoted 1 line
Quoted 1 line
Quoted 1 line
unfortunately does not always initialize that 4-byte hole. Fix it by
using
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
memset()instead.Of course, this is the difference between "{ 0 }" and "{}"
initializations.
Quoted 3 lines
Really? Neither will handle structures with holes in it, try it and see.
{} is a GCC extension, but I never thought it works differently.
Quoted 7 lines
And if true, where in the C spec does it say that?
thanks,
greg k-h
Signature · 2 lines
With Best Regards, Andy Shevchenko
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 18 lines · Andy Shevchenko
Quoted 15 lines · Greg Kroah-Hartman
Quoted 3 lines · Greg Kroah-Hartman
Quoted 2 lines · Leon Romanovsky
Quoted 1 line · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernelstack
Quoted 1 line
Quoted 1 line
Quoted 1 line
memory to userspace since the compiler may leave a 4-byte hole at
the end
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
of
cmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg,which
Quoted 1 line
Quoted 1 line
Quoted 1 line
unfortunately does not always initialize that 4-byte hole. Fix it by
using
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
memset()instead.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Of course, this is the difference between "{ 0 }" and "{}"
initializations.
Quoted 3 lines
Really? Neither will handle structures with holes in it, try it and see.
{} is a GCC extension, but I never thought it works differently.
Yes, this is GCC extension and kernel relies on them very heavily.
Thanks
Quoted 8 lines
Quoted 8 lines
And if true, where in the C spec does it say that?
thanks,
Quoted 7 lines
Quoted 2 lines
greg k-h
--
With Best Regards,
Andy Shevchenko
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Sakari, JFYI. I remember during some reviews we have a discussion about {0} vs {} and surprisingly they are not an equivalent.
Quoted 6 lines · Leon Romanovsky
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Greg Kroah-Hartman
Quoted 2 lines · Greg Kroah-Hartman
Quoted 1 line · Leon Romanovsky
On Thu, Jul 30, 2020 at 03:20:26PM -0400, Peilin Ye wrote:
...
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Of course, this is the difference between "{ 0 }" and "{}"
initializations.
Quoted 3 lines
Really? Neither will handle structures with holes in it, try it and see.
{} is a GCC extension, but I never thought it works differently.
Yes, this is GCC extension and kernel relies on them very heavily.
I guess simple people who contribute to the kernel just haven't realized (yet) that it's an extension and that's why we have plenty of {} and {0} in the kernel.
Quoted 1 line
Quoted 1 line
Quoted 1 line
And if true, where in the C spec does it say that?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 11 lines · Greg Kroah-Hartman
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Of course, this is the difference between "{ 0 }" and "{}" initializations.
Really? Neither will handle structures with holes in it, try it and see.
And if true, where in the C spec does it say that?
The spec was updated in C11 to require zero'ing padding when doing partial initialization of aggregates (eg = {})
"""if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;"""
The difference between {0} and the {} extension is only that {} reliably triggers partial initialization for all kinds of aggregates, while {0} has a number of edge cases where it can fail to compile.
IIRC gcc has cleared the padding during aggregate initialization for a long time. Considering we have thousands of aggregate initializers it seems likely to me Linux also requires a compiler with this C11 behavior to operate correctly.
Does this patch actually fix anything? My compiler generates identical assembly code in either case.
Jason
Quoted 17 lines · Jason Gunthorpe
Quoted 16 lines · Greg Kroah-Hartman
Quoted 14 lines · Greg Kroah-Hartman
Quoted 10 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Really? Neither will handle structures with holes in it, try it and see.
Quoted 8 lines
Quoted 1 line
And if true, where in the C spec does it say that?
The spec was updated in C11 to require zero'ing padding when doing partial initialization of aggregates (eg = {})
"""if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;"""
But then why does the compilers not do this?
Quoted 6 lines
The difference between {0} and the {} extension is only that {} reliably triggers partial initialization for all kinds of aggregates, while {0} has a number of edge cases where it can fail to compile.
IIRC gcc has cleared the padding during aggregate initialization for a long time.
Huh? Last we checked a few months ago, no, it did not do that.
Quoted 3 lines
Considering we have thousands of aggregate initializers it seems likely to me Linux also requires a compiler with this C11 behavior to operate correctly.
Note that this is not an "operate correctly" thing, it is a "zero out stale data in structure paddings so that data will not leak to userspace" thing.
Quoted 2 lines
Does this patch actually fix anything? My compiler generates identical assembly code in either case.
What compiler version?
thanks,
greg k-h
Quoted 8 lines · Greg Kroah-Hartman
Quoted 6 lines
The spec was updated in C11 to require zero'ing padding when doing partial initialization of aggregates (eg = {})
"""if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;"""
But then why does the compilers not do this?
Do you have an example?
Quoted 7 lines
Quoted 3 lines
Considering we have thousands of aggregate initializers it seems likely to me Linux also requires a compiler with this C11 behavior to operate correctly.
Note that this is not an "operate correctly" thing, it is a "zero out stale data in structure paddings so that data will not leak to userspace" thing.
Yes, not being insecure is "operate correctly", IMHO :)
Quoted 4 lines
Quoted 2 lines
Does this patch actually fix anything? My compiler generates identical assembly code in either case.
What compiler version?
I tried clang 10 and gcc 9.3 for x86-64.
#include <string.h>
void test(void *out)
{
struct rds_rdma_notify {
unsigned long user_token;
unsigned int status;
} foo = {};
memcpy(out, &foo, sizeof(foo));
}$ gcc -mno-sse2 -O2 -Wall -std=c99 t.c -S
test:
endbr64 movq $0, (%rdi) movq $0, 8(%rdi) ret
Just did this same test with gcc 4.4 and it also gave the same output..
Made it more complex with this:
struct rds_rdma_notify {
unsigned long user_token;
unsigned char status;
unsigned long user_token1;
unsigned char status1;
unsigned long user_token2;
unsigned char status2;
unsigned long user_token3;
unsigned char status3;
unsigned long user_token4;
unsigned char status4;
} foo;And still got the same assembly vs memset on gcc 4.4.
I tried for a bit and didn't find a way to get even old gcc 4.4 to not initialize the holes.
Jason
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Greg Kroah-Hartman
Quoted 3 lines
The spec was updated in C11 to require zero'ing padding when doing partial initialization of aggregates (eg = {})
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
"""if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;"""
But then why does the compilers not do this?
Do you have an example?
At the moment, no, but we have had them in the past due to security issues we have had to fix for this.
Quoted 53 lines
Quoted 7 lines
Quoted 3 lines
Considering we have thousands of aggregate initializers it seems likely to me Linux also requires a compiler with this C11 behavior to operate correctly.
Note that this is not an "operate correctly" thing, it is a "zero out stale data in structure paddings so that data will not leak to userspace" thing.
Yes, not being insecure is "operate correctly", IMHO :)
Quoted 4 lines
Quoted 2 lines
Does this patch actually fix anything? My compiler generates identical assembly code in either case.
What compiler version?
I tried clang 10 and gcc 9.3 for x86-64.
#include <
string.h>void
test(void *out){ struct rds_rdma_notify { unsigned long user_token; unsigned int status; } foo = {}; memcpy(out, &foo, sizeof(foo)); }$ gcc -mno-sse2 -O2 -Wall -std=c99 t.c -Stest:
endbr64 movq $0, (%rdi) movq $0, 8(%rdi) retJust did this same test with gcc 4.4 and it also gave the same output..
Made it more complex with this:
struct rds_rdma_notify { unsigned long user_token; unsigned char status; unsigned long user_token1; unsigned char status1; unsigned long user_token2; unsigned char status2; unsigned long user_token3; unsigned char status3; unsigned long user_token4; unsigned char status4; } foo;
Quoted 4 lines
And still got the same assembly vs memset on gcc 4.4.
I tried for a bit and didn't find a way to get even old gcc 4.4 to not initialize the holes.
Odd, so it is just the "= {0};" that does not zero out the holes?
thanks,
greg k-h
Quoted 4 lines · Greg Kroah-Hartman
Quoted 2 lines
I tried for a bit and didn't find a way to get even old gcc 4.4 to not initialize the holes.
Odd, so it is just the "= {0};" that does not zero out the holes?
Nope, it seems to work fine too. I tried a number of situations and I could not get the compiler to not zero holes, even back to gcc 4.4
It is not just accidental either, take this:
struct rds_rdma_notify {
unsigned long user_token;
unsigned char status;
unsigned long user_token1 __attribute__((aligned(32)));
} foo = {0};Which has quite a big hole, clang generates:
movq $0, 56(%rdi) movq $0, 48(%rdi) movq $0, 40(%rdi) movq $0, 32(%rdi) movq $0, 24(%rdi) movq $0, 16(%rdi) movq $0, 8(%rdi) movq $0, (%rdi)
Deliberate extra instructions to fill both holes. gcc 10 does the same, older gcc's do create a rep stosq over the whole thing.
Some fiddling with godbolt shows quite a variety of output, but I didn't see anything that looks like a compiler not filling padding. Even godbolt's gcc 4.1 filled the padding, which is super old.
In several cases it seems the aggregate initializer produced better code than memset, in other cases it didn't
Without an actual example where this doesn't work right it is hard to say anything more..
Jason
Quoted 36 lines · Jason Gunthorpe
Quoted 4 lines · Greg Kroah-Hartman
Quoted 2 lines
I tried for a bit and didn't find a way to get even old gcc 4.4 to not initialize the holes.
Odd, so it is just the "= {0};" that does not zero out the holes?
Nope, it seems to work fine too. I tried a number of situations and I could not get the compiler to not zero holes, even back to gcc 4.4
It is not just accidental either, take this:
struct rds_rdma_notify { unsigned long user_token; unsigned char status; unsigned long user_token1 __attribute__((aligned(32))); } foo = {0};Which has quite a big hole, clang generates:
movq $0, 56(%rdi) movq $0, 48(%rdi) movq $0, 40(%rdi) movq $0, 32(%rdi) movq $0, 24(%rdi) movq $0, 16(%rdi) movq $0, 8(%rdi) movq $0, (%rdi)Deliberate extra instructions to fill both holes. gcc 10 does the same, older gcc's do create a rep stosq over the whole thing.
Some fiddling with godbolt shows quite a variety of output, but I didn't see anything that looks like a compiler not filling padding. Even godbolt's gcc 4.1 filled the padding, which is super old.
Quoted 5 lines
In several cases it seems the aggregate initializer produced better code than memset, in other cases it didn't
Without an actual example where this doesn't work right it is hard to say anything more..
Here is the example that set off the recent patches:
https://lkml.org/lkml/2020/7/27/199
Another example is commit 5ff223e86f5a ("net: Zeroing the structure ethtool_wolinfo in ethtool_get_wol()"). I tested this one with GCC 7.4 at the time and it was a real life bug.
The rest of these patches were based on static analysis from Smatch. They're all "theoretical" bugs based on the C standard but it's impossible to know if and when they'll turn into real life bugs.
It's not a super long list of code that's affected because we've known that the bug was possible for a few years. It was only last year when I saw that it had become a real life bug.
regards,
dan carpenter
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 6 lines · Dan Carpenter
Quoted 2 lines
Without an actual example where this doesn't work right it is hard to say anything more..
Here is the example that set off the recent patches:
Oh, that is something completely different. This thread was talking about '= {}'.
From a C11 perspective the above link is complete initialization of an aggregate and does not trigger the rule requiring that padding be zero'd.
C11 only zeros padding during *partial* initialization of an aggregate.
ie this does not zero padding:
void test(void)
{
extern void copy(const void *ptr, size_t len);
struct rds_rdma_notify {
unsigned long user_token;
unsigned char status __attribute__((aligned(32)));
} foo = {1, 1};
// Padding NOT zeroed
copy(&foo, sizeof(foo));
}While the addition of a xxx member to make it partial initialization does zero:
void test(void)
{
extern void copy(const void *ptr, size_t len);
struct rds_rdma_notify {
unsigned long user_token;
unsigned char status __attribute__((aligned(32)));
unsigned long xx;
} foo = {1, 1};
// Padding NOT zeroed
copy(&foo, sizeof(foo));
}(and godbolt confirms this on a wide range of compilers)
Quoted 3 lines
The rest of these patches were based on static analysis from Smatch. They're all "theoretical" bugs based on the C standard but it's impossible to know if and when they'll turn into real life bugs.
Any patches replaing '= {}' (and usually '= {0}') with memset are not fixing anything.
The C11 standard requires zeroing padding in these case. It is just useless churn and in some cases results in worse codegen.
smatch should only warn about this if the aggregate initialization is not partial.
Jason
Ah, thanks. We've had a bunch of discussions about these leaks but I wasn't aware of this.
regards,
dan carpenter
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Greg Kroah-Hartman
Quoted 6 lines
The spec was updated in C11 to require zero'ing padding when doing partial initialization of aggregates (eg = {})
"""if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;"""
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
But then why does the compilers not do this?
Do you have an example?
At the moment, no, but we have had them in the past due to security issues we have had to fix for this.
Is it still relevant after bump of required GCC version to build kernel?
I afraid that without solid example such changes will start to be treated with cargo cult.
Jason,
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Thanks
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
This is why the {} extension exists..
Jason
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
Quoted 10 lines · Joe Perches
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
We just went over this. Yes there is, C11 requires it.
Jason
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Joe Perches
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
We just went over this. Yes there is, C11 requires it.
c11 is not c90. The kernel uses c90.
Quoted 8 lines · Joe Perches
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Joe Perches
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
We just went over this. Yes there is, C11 requires it.
c11 is not c90. The kernel uses c90.
It is not accurate, kernel uses gnu89 dialect, which is C90 with some C99 features [1]. In our case, we rely on GCC extension {} that doesn't contradict standart [2] and fills holes with zeros too.
[1] Makefile:500
496 KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
497 -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \ 498 -Werror=implicit-function-declaration -Werror=implicit-int \ 499 -Wno-format-security \ 500 -std=gnu89
[2] From GCC:
https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
"When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it."
Thanks
Quoted 3 lines
Quoted 8 lines · Joe Perches
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Joe Perches
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
We just went over this. Yes there is, C11 requires it.
c11 is not c90. The kernel uses c90.
The kernel already relies on a lot of C11/C99 features and behaviors. For instance Linus just bumped the minimum compiler version so that C11's _Generic is usable.
Why do you think this particular part of C11 shouldn't be relied on?
Jason
Hello!
Thanks to Jason for getting this conversation back on track.
Yes: in general, {} or a partial initializer /will/ zero padding bits.
However, there is a bug in some versions of GCC where {} will /not/ zero
padding bits; actually, Jason's test program in this mail https://lore.kernel.org/lkml/[email protected]/
has the right ingredients to trigger the bug, but the GCC
versions used are outside of the bug window. :)
For more details on these cases and more (including said GCC bug), see my paper at:
Hopefully this paper can serve as a helpful reference when these cases are encountered in the kernel.
Thank you.
Jack Leadford
Quoted 27 lines · Jason Gunthorpe
Quoted 8 lines · Joe Perches
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Joe Perches
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Leon Romanovsky
I'm using {} instead of {0} because of this GCC bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
This is why the {} extension exists..
There is no guarantee that the gcc struct initialization {} extension also zeros padding.
We just went over this. Yes there is, C11 requires it.
c11 is not c90. The kernel uses c90.
The kernel already relies on a lot of C11/C99 features and behaviors. For instance Linus just bumped the minimum compiler version so that C11's
_Genericis usable.Why do you think this particular part of C11 shouldn't be relied on?
Jason
Quoted 12 lines · Jack Leadford
Hello!
Thanks to Jason for getting this conversation back on track.
Yes: in general, {} or a partial initializer /will/ zero padding bits.
However, there is a bug in some versions of GCC where {} will /not/ zero padding bits; actually, Jason's test program in this mail
https://lore.kernel.org/lkml/[email protected]/
has the right ingredients to trigger the bug, but the GCC versions used are outside of the bug window. :)
Quoted 7 lines
For more details on these cases and more (including said GCC bug), see my paper at:
Hopefully this paper can serve as a helpful reference when these cases are encountered in the kernel.
I read the paper and didn't find exact GCC version, only remark that it was before GCC 7.
So my question, why is this case different from any other GCC bugs? AFAIK, we don't add kernel code to overcome GCC bugs which exist in specific versions, which already were fixed.
More on that, this paper talks about specific flow which doesn't exist in the discussed patch.
Thanks
Quoted 11 lines · Jack Leadford
Hello!
Thanks to Jason for getting this conversation back on track.
Yes: in general, {} or a partial initializer /will/ zero padding bits.
However, there is a bug in some versions of GCC where {} will /not/ zero padding bits; actually, Jason's test program in this mail
https://lore.kernel.org/lkml/[email protected]/
has the right ingredients to trigger the bug, but the GCC versions used are outside of the bug window. :)
It seems fine, at least Godbolt doesn't show a bug with that code.
Can you share the test that does fail?
This seems like the sort of security sensitive bug that should be addressed in gcc, not worked around in the kernel code :\
Jason
Quoted 53 lines · Leon Romanovsky
Quoted 6 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it byusing
Quoted 1 line
memset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
net/rds/recv.c static int rds_still_queued(struct rds_sock *rs,
Quoted 30 lines
Fixes f037590fff30 ("rds: fix a leak of kernel memory")Fixes bdbe6fbc6a2f ("RDS: recv.c")Suggested-by Dan Carpenter <[email protected]>Signed-off-by Peilin Ye <[email protected]>---
Note: the "real"copy_to_user()happens input_cmsg(), wherecmlen - sizeof(*cm)equals tosizeof(cmsg).Reference: https://lwn.net/Articles/417989/
$ pahole -C "rds_rdma_notify" net/rds/recv.o struct rds_rdma_notify { __u64 user_token; /* 0 8 */ __s32 status; /* 8 4 *//* size: 16, cachelines: 1, members: 2 */ /* padding: 4 */ /* last cacheline: 16 bytes */ };net/rds/recv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)net/rds/recv.c +0
-0@@ -450,12 +450,13 @@ static int rds_still_queued(struct rds_sock *rs,struct
rds_incoming*inc,Quoted 10 lines
net/rds/recv.c +1
-1int rds_notify_queue_get (struct rds_sock *rs,struct msghdr *msghdr) {struct rds_notifier *notifier; -struct rds_rdma_notify cmsg = {0 };/* fill holes with zero */ +struct rds_rdma_notify cmsg;unsigned int count =0 , max_messages = ~0U ;unsigned long flags;LIST_HEAD (copy);int err =0 ;
Quoted 4 lines
net/rds/recv.c static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc, · line 459
Quoted 1 line
net/rds/recv.c +1
-0+memset (&cmsg,0 ,sizeof (cmsg));/* fill holes with zero */ It works, but the right solution is to drop 0 from cmsg initialization and write "struct
rds_rdma_notifycmsg = {};" without any memset.
No, the right solution is to use memset() over gcc extension.
Quoted 3 lines
Thanks
Signature · 2 lines
With Best Regards, Andy Shevchenko
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 11 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
No, there is no difference. Even struct assignments like:
foo = *bar;
can leave struct holes uninitialized. Depending on the compiler the assignment can be implemented as a memset() or as a series of struct member assignments.
regards,
dan carpenter
Quoted 17 lines · Dan Carpenter
Quoted 11 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Quoted 7 lines
No, there is no difference. Even struct assignments like:
foo = *bar;can leave struct holes uninitialized. Depending on the compiler the assignment can be implemented as a
memset()or as a series of struct member assignments.
What about:
struct rds_rdma_notify {
__u64 user_token;
__s32 status;
} __attribute__((packed));Thxs, Håkon
Quoted 3 lines
regards,
dan carpenter
Quoted 25 lines · Håkon Bugge
Quoted 17 lines · Dan Carpenter
Quoted 11 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Quoted 7 lines
No, there is no difference. Even struct assignments like:
foo = *bar;can leave struct holes uninitialized. Depending on the compiler the assignment can be implemented as a
memset()or as a series of struct member assignments.
Quoted 6 lines
What about:
struct rds_rdma_notify { __u64 user_token; __s32 status; } __attribute__((packed));
Why is this still a discussion at all?
Try it and see, run pahole and see if there are holes in this structure (odds are no), you don't need us to say what is happening here...
thanks,
greg k-h
Quoted 37 lines · Greg Kroah-Hartman
Quoted 25 lines · Håkon Bugge
Quoted 17 lines · Dan Carpenter
Quoted 11 lines · Leon Romanovsky
Quoted 7 lines · Peilin Ye
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.Of course, this is the difference between "{ 0 }" and "{}" initializations.
Quoted 7 lines
No, there is no difference. Even struct assignments like:
foo = *bar;can leave struct holes uninitialized. Depending on the compiler the assignment can be implemented as a
memset()or as a series of struct member assignments.Quoted 6 lines
What about:
struct rds_rdma_notify { __u64 user_token; __s32 status; } __attribute__((packed));
Quoted 4 lines
Why is this still a discussion at all?
Try it and see, run pahole and see if there are holes in this structure (odds are no), you don't need us to say what is happening here...
An older posting had this:
$ pahole -C "rds_rdma_notify" net/rds/recv.o
struct rds_rdma_notify {
__u64 user_token; /* 0 8 */
__s32 status; /* 8 4 *//* size: 16, cachelines: 1, members: 2 */ /* padding: 4 */ /* last cacheline: 16 bytes */ };
Thxs, Håkon
David Miller
maintainerFrom: Peilin Ye <[email protected]>
Date: Thu, 30 Jul 2020 15:20:26 -0400
Quoted 8 lines
rds_notify_queue_get()is potentially copying uninitialized kernel stack memory to userspace since the compiler may leave a 4-byte hole at the end ofcmsg.In 2016 we tried to fix this issue by doing
= { 0 };oncmsg, which unfortunately does not always initialize that 4-byte hole. Fix it by usingmemset()instead.
Quoted 5 lines
Fixes f037590fff30 ("rds: fix a leak of kernel memory")Fixes bdbe6fbc6a2f ("RDS: recv.c")Suggested-by Dan Carpenter <[email protected]>Signed-off-by Peilin Ye <[email protected]>
Applied and queued up for -stable, thanks.
I saw a suggestion to use __packed but that breaks UAPI and is definitely not an option to solve this problem.