Message ID | Y5mMWEtHWKOiPVU+@mail.google.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [next] pcmcia: synclink_cs: replace 1-element array with flex-array member | expand |
On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida wrote: > One-element arrays are deprecated, and we are replacing them with > flexible array members instead. So, replace one-element array with > flexible-array member in struct RXBUF and refactor the rest of the code > accordingly. > > It's worth mentioning that doing a build before/after this patch > results in no binary output differences. > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > routines on memcpy() and help us make progress towards globally > enabling -fstrict-flex-arrays=3 [1]. > > Link: https://github.com/KSPP/linux/issues/79 > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > --- > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > index b2735be81ab2..1ab2d552f498 100644 > --- a/drivers/char/pcmcia/synclink_cs.c > +++ b/drivers/char/pcmcia/synclink_cs.c > @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { > typedef struct { > int count; > unsigned char status; > - char data[1]; > + char data[]; > } RXBUF; > > /* The queue of BH actions to be performed */ > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > static int rx_alloc_buffers(MGSLPC_INFO *info) > { > /* each buffer has header and data */ > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > + + info->max_frame_size; It seems like there is an existing size bug here, and likely should be fixed separately? i.e. this was already allocating 1 byte "too much". I'd expect this first: - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; and then the next patch: - char data[1]; + char data[]; ... - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; The above would induce a binary output change, and the second would not. Though this results in what you had for the v2 patch (but I can't believe it had no binary changes...)
On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida wrote: > > One-element arrays are deprecated, and we are replacing them with > > flexible array members instead. So, replace one-element array with > > flexible-array member in struct RXBUF and refactor the rest of the code > > accordingly. > > > > It's worth mentioning that doing a build before/after this patch > > results in no binary output differences. > > > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > > routines on memcpy() and help us make progress towards globally > > enabling -fstrict-flex-arrays=3 [1]. > > > > Link: https://github.com/KSPP/linux/issues/79 > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > > > > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > > --- > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > index b2735be81ab2..1ab2d552f498 100644 > > --- a/drivers/char/pcmcia/synclink_cs.c > > +++ b/drivers/char/pcmcia/synclink_cs.c > > @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { > > typedef struct { > > int count; > > unsigned char status; > > - char data[1]; > > + char data[]; > > } RXBUF; > > > > /* The queue of BH actions to be performed */ > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > { > > /* each buffer has header and data */ > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > + + info->max_frame_size; > > It seems like there is an existing size bug here, and likely should be > fixed separately? > > i.e. this was already allocating 1 byte "too much". I'd expect this > first: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > and then the next patch: > > - char data[1]; > + char data[]; > ... > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > The above would induce a binary output change, and the second would not. > > Though this results in what you had for the v2 patch (but I can't > believe it had no binary changes...) > > -- > Kees Cook Hi Kees, Hi Andy, Thanks for taking the time to review this patch. As both of you had similar points, I will reply them here. The reasons why it had no binary changes was because of the combination of this 2 things: 1) Existing padding - so sizeof(RXBUF) returned 8 bytes in both cases. pahole -C RXBUF gcc/before/drivers/char/pcmcia/synclink_cs.ko typedef struct { int count; /* 0 4 */ unsigned char status; /* 4 1 */ char data[1]; /* 5 1 */ /* size: 8, cachelines: 1, members: 3 */ /* padding: 2 */ /* last cacheline: 8 bytes */ } RXBUF; pahole -C RXBUF gcc/after/drivers/char/pcmcia/synclink_cs.ko typedef struct { int count; /* 0 4 */ unsigned char status; /* 4 1 */ char data[]; /* 5 0 */ /* size: 8, cachelines: 1, members: 3 */ /* padding: 3 */ /* last cacheline: 8 bytes */ } RXBUF; 2) RXBUF (as implemented now) is just like a pair of lenses from which a developer can have access to one of the circular buffers in MGSLPC_INFO struct called 'rx_buf'. 2611 static int rx_alloc_buffers(MGSLPC_INFO *info) 2612 { 2613 /* each buffer has header and data */ 2614 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; 2615 2616 /* calculate total allocation size for 8 buffers */ 2617 info->rx_buf_total_size = info->rx_buf_size * 8; 2618 2619 /* limit total allocated memory */ 2620 if (info->rx_buf_total_size > 0x10000) 2621 info->rx_buf_total_size = 0x10000; 2622 2623 /* calculate number of buffers */ 2624 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size; 2625 2626 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL); To be honest, char data[_1_] in RXBUF was never required to be there. The code base seems to make sure that it doesn't run past its limits by keeping track of size buffer on MGSLPC_INFO->rx_buf_size (and sometimes RXBUF->count) (Addressing one point made by Andy about using of of the macros in overflow.h) struct_size(buf, data, 1) would return 9 bytes which could potentially break the existing driver as it produces binary changes. Let me know your thoughts thanks! - Paulo A.
On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida wrote: > > One-element arrays are deprecated, and we are replacing them with > > flexible array members instead. So, replace one-element array with > > flexible-array member in struct RXBUF and refactor the rest of the code > > accordingly. > > > > It's worth mentioning that doing a build before/after this patch > > results in no binary output differences. > > > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > > routines on memcpy() and help us make progress towards globally > > enabling -fstrict-flex-arrays=3 [1]. > > > > Link: https://github.com/KSPP/linux/issues/79 > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > > > > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > > --- > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > index b2735be81ab2..1ab2d552f498 100644 > > --- a/drivers/char/pcmcia/synclink_cs.c > > +++ b/drivers/char/pcmcia/synclink_cs.c > > @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { > > typedef struct { > > int count; > > unsigned char status; > > - char data[1]; > > + char data[]; > > } RXBUF; > > > > /* The queue of BH actions to be performed */ > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > { > > /* each buffer has header and data */ > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > + + info->max_frame_size; > > It seems like there is an existing size bug here, and likely should be > fixed separately? > > i.e. this was already allocating 1 byte "too much". I'd expect this > first: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > and then the next patch: > > - char data[1]; > + char data[]; > ... > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > The above would induce a binary output change, and the second would not. > > Though this results in what you had for the v2 patch (but I can't > believe it had no binary changes...) > > -- > Kees Cook Just realised that you made a comment on PATCH v1 and Andy made a comment on PATCH v2. Please conside my answer for PATCH v2 as I have abandoned the v1. Apologies for the confusion. thanks! - Paulo A.
On Thu, Dec 15, 2022 at 09:09:46AM +1300, Paulo Miguel Almeida wrote: > On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida wrote: > > > One-element arrays are deprecated, and we are replacing them with > > > flexible array members instead. So, replace one-element array with > > > flexible-array member in struct RXBUF and refactor the rest of the code > > > accordingly. > > > > > > It's worth mentioning that doing a build before/after this patch > > > results in no binary output differences. > > > > > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > > > routines on memcpy() and help us make progress towards globally > > > enabling -fstrict-flex-arrays=3 [1]. > > > > > > Link: https://github.com/KSPP/linux/issues/79 > > > Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] > > > > > > Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> > > > --- > > > drivers/char/pcmcia/synclink_cs.c | 5 +++-- > > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c > > > index b2735be81ab2..1ab2d552f498 100644 > > > --- a/drivers/char/pcmcia/synclink_cs.c > > > +++ b/drivers/char/pcmcia/synclink_cs.c > > > @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { > > > typedef struct { > > > int count; > > > unsigned char status; > > > - char data[1]; > > > + char data[]; > > > } RXBUF; > > > > > > /* The queue of BH actions to be performed */ > > > @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) > > > static int rx_alloc_buffers(MGSLPC_INFO *info) > > > { > > > /* each buffer has header and data */ > > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > > + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) > > > + + info->max_frame_size; > > > > It seems like there is an existing size bug here, and likely should be > > fixed separately? > > > > i.e. this was already allocating 1 byte "too much". I'd expect this > > first: > > > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > > > and then the next patch: > > > > - char data[1]; > > + char data[]; > > ... > > - info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > + info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > > > > The above would induce a binary output change, and the second would not. > > > > Though this results in what you had for the v2 patch (but I can't > > believe it had no binary changes...) > > > > -- > > Kees Cook > > Hi Kees, Hi Andy, Thanks for taking the time to review this patch. > > As both of you had similar points, I will reply them here. > > The reasons why it had no binary changes was because of the combination > of this 2 things: > > 1) Existing padding - so sizeof(RXBUF) returned 8 bytes in both cases. > > pahole -C RXBUF gcc/before/drivers/char/pcmcia/synclink_cs.ko > typedef struct { > int count; /* 0 4 */ > unsigned char status; /* 4 1 */ > char data[1]; /* 5 1 */ > > /* size: 8, cachelines: 1, members: 3 */ > /* padding: 2 */ > /* last cacheline: 8 bytes */ > } RXBUF; > > pahole -C RXBUF gcc/after/drivers/char/pcmcia/synclink_cs.ko > typedef struct { > int count; /* 0 4 */ > unsigned char status; /* 4 1 */ > char data[]; /* 5 0 */ > > /* size: 8, cachelines: 1, members: 3 */ > /* padding: 3 */ > /* last cacheline: 8 bytes */ > } RXBUF; Ah-ha, now I see. > > 2) RXBUF (as implemented now) is just like a pair of lenses from which a > developer can have access to one of the circular buffers in MGSLPC_INFO > struct called 'rx_buf'. > > 2611 static int rx_alloc_buffers(MGSLPC_INFO *info) > 2612 { > 2613 /* each buffer has header and data */ > 2614 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > 2615 > 2616 /* calculate total allocation size for 8 buffers */ > 2617 info->rx_buf_total_size = info->rx_buf_size * 8; > 2618 > 2619 /* limit total allocated memory */ > 2620 if (info->rx_buf_total_size > 0x10000) > 2621 info->rx_buf_total_size = 0x10000; > 2622 > 2623 /* calculate number of buffers */ > 2624 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size; > 2625 > 2626 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL); > > To be honest, char data[_1_] in RXBUF was never required to be there. > The code base seems to make sure that it doesn't run past its limits by > keeping track of size buffer on MGSLPC_INFO->rx_buf_size (and sometimes > RXBUF->count) > > (Addressing one point made by Andy about using of of the macros in > overflow.h) > struct_size(buf, data, 1) would return 9 bytes which could > potentially break the existing driver as it produces binary > changes. Yeah, I think your v2 is fine. Perhaps explicitly repeat the notes about struct size padding in a v3 commit log?
On Wed, Dec 14, 2022 at 10:09 PM Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> wrote: > On Wed, Dec 14, 2022 at 11:29:37AM -0800, Kees Cook wrote: > > On Wed, Dec 14, 2022 at 09:42:00PM +1300, Paulo Miguel Almeida wrote: > > > One-element arrays are deprecated, and we are replacing them with > > > flexible array members instead. So, replace one-element array with > > > flexible-array member in struct RXBUF and refactor the rest of the code > > > accordingly. > > > > > > It's worth mentioning that doing a build before/after this patch > > > results in no binary output differences. > > > > > > This helps with the ongoing efforts to tighten the FORTIFY_SOURCE > > > routines on memcpy() and help us make progress towards globally > > > enabling -fstrict-flex-arrays=3 [1]. ... > > > typedef struct { > > > int count; > > > unsigned char status; > > > - char data[1]; > > > + char data[]; > > > } RXBUF; ... > As both of you had similar points, I will reply them here. > > The reasons why it had no binary changes was because of the combination > of this 2 things: > > 1) Existing padding - so sizeof(RXBUF) returned 8 bytes in both cases. > > pahole -C RXBUF gcc/before/drivers/char/pcmcia/synclink_cs.ko > typedef struct { > int count; /* 0 4 */ > unsigned char status; /* 4 1 */ > char data[1]; /* 5 1 */ > > /* size: 8, cachelines: 1, members: 3 */ > /* padding: 2 */ > /* last cacheline: 8 bytes */ > } RXBUF; > > pahole -C RXBUF gcc/after/drivers/char/pcmcia/synclink_cs.ko > typedef struct { > int count; /* 0 4 */ > unsigned char status; /* 4 1 */ > char data[]; /* 5 0 */ > > /* size: 8, cachelines: 1, members: 3 */ > /* padding: 3 */ > /* last cacheline: 8 bytes */ > } RXBUF; Yes, and Try to make it work with __packed. As I said, the problem is that the code is relying on something which is architecture dependent strictly speaking. And hence I disagree with Kees that v2 is okay to go. > 2) RXBUF (as implemented now) is just like a pair of lenses from which a > developer can have access to one of the circular buffers in MGSLPC_INFO > struct called 'rx_buf'. > 2611 static int rx_alloc_buffers(MGSLPC_INFO *info) > 2612 { > 2613 /* each buffer has header and data */ > 2614 info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > 2615 > 2616 /* calculate total allocation size for 8 buffers */ > 2617 info->rx_buf_total_size = info->rx_buf_size * 8; > 2618 > 2619 /* limit total allocated memory */ > 2620 if (info->rx_buf_total_size > 0x10000) > 2621 info->rx_buf_total_size = 0x10000; > 2622 > 2623 /* calculate number of buffers */ > 2624 info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size; > 2625 > 2626 info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL); > > To be honest, char data[_1_] in RXBUF was never required to be there. > The code base seems to make sure that it doesn't run past its limits by > keeping track of size buffer on MGSLPC_INFO->rx_buf_size (and sometimes > RXBUF->count) > > (Addressing one point made by Andy about using of of the macros in > overflow.h) > struct_size(buf, data, 1) would return 9 bytes which could > potentially break the existing driver as it produces binary > changes. You got it incorrectly. I believe you should use something different than 1. In previous lines in the function it multiplies sizeof + max_frame_size by 8. The full change should be something like check_add(sizeof(), max_frame_size) kcalloc(8, size) Think about it. > Let me know your thoughts
On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > Yes, and Try to make it work with __packed. As I said, the problem is > that the code is relying on something which is architecture dependent > strictly speaking. And hence I disagree with Kees that v2 is okay to > go. I meant that v2 is functionally identical to the existing code. > The full change should be something like > > check_add(sizeof(), max_frame_size) > kcalloc(8, size) Right -- this would fix the existing mistakes in size calculation (and is certainly better).
On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > Yes, and Try to make it work with __packed. As I said, the problem is > > that the code is relying on something which is architecture dependent > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > go. > > I meant that v2 is functionally identical to the existing code. Ah, sorry for misunderstanding. > > The full change should be something like > > > > check_add(sizeof(), max_frame_size) > > kcalloc(8, size) > > Right -- this would fix the existing mistakes in size calculation (and > is certainly better). Glad to hear that we are on the same page.
On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > that the code is relying on something which is architecture dependent > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > go. > > > > I meant that v2 is functionally identical to the existing code. > > Ah, sorry for misunderstanding. > I agree with using __packed attribute to remove the extra padding (and for the reasons you mentioned before). That would reduce the sizeof(RXBUF) from 8 to 5 (which is good) but that is still 1 byte "too much". Piggying back on a suggestion Kees gave before: - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; That way RXBUF->data will point to the first byte of the frame_size (MGSLPC_INFO->max_frame_size) which is what is actually needed. > > > The full change should be something like > > > > > > check_add(sizeof(), max_frame_size) > > > kcalloc(8, size) > > > > Right -- this would fix the existing mistakes in size calculation (and > > is certainly better). > > Glad to hear that we are on the same page. > That makes sense to me. thanks! - Paulo A.
On Thu, Dec 15, 2022 at 05:29:15PM +1300, Paulo Miguel Almeida wrote: > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > that the code is relying on something which is architecture dependent > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > go. > > > > > > I meant that v2 is functionally identical to the existing code. > > > > Ah, sorry for misunderstanding. > > > > I agree with using __packed attribute to remove the extra padding (and > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > from 8 to 5 (which is good) but that is still 1 byte "too much". > > Piggying back on a suggestion Kees gave before: > > - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; > + info->rx_buf_size = sizeof(RXBUF) - 1 + info->max_frame_size; > > That way RXBUF->data will point to the first byte of the frame_size > (MGSLPC_INFO->max_frame_size) which is what is actually needed. > I chose my words poorly here... sorry my brain is a bit fried today. Let me rephrase that last sentence. After that change (or similar change), RXBUF->data will point to the first byte of the buffer allocated during the initialisation process. (which is limited/controlled by the size of MGSLPC_INFO->max_frame_size)... so no 'extra byte/padding' will be there. - Paulo A. > > > > The full change should be something like > > > > > > > > check_add(sizeof(), max_frame_size) > > > > kcalloc(8, size) > > > > > > Right -- this would fix the existing mistakes in size calculation (and > > > is certainly better). > > > > Glad to hear that we are on the same page. > > > > That makes sense to me. > > thanks! > > - Paulo A.
On Thu, Dec 15, 2022 at 6:29 AM Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> wrote: > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: ... > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > that the code is relying on something which is architecture dependent > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > go. > > > > > > I meant that v2 is functionally identical to the existing code. > > > > Ah, sorry for misunderstanding. > > I agree with using __packed attribute to remove the extra padding (and > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > from 8 to 5 (which is good) but that is still 1 byte "too much". What I meant with the above is that the code has to work properly with or without __packed. It's just to show you that this code has flaws if it relies on the padding.
On Thu, Dec 15, 2022 at 10:57:57AM +0200, Andy Shevchenko wrote: > On Thu, Dec 15, 2022 at 6:29 AM Paulo Miguel Almeida > <paulo.miguel.almeida.rodenas@gmail.com> wrote: > > On Thu, Dec 15, 2022 at 12:06:46AM +0200, Andy Shevchenko wrote: > > > On Wed, Dec 14, 2022 at 11:49 PM Kees Cook <keescook@chromium.org> wrote: > > > > On Wed, Dec 14, 2022 at 10:39:52PM +0200, Andy Shevchenko wrote: > > ... > > > > > > Yes, and Try to make it work with __packed. As I said, the problem is > > > > > that the code is relying on something which is architecture dependent > > > > > strictly speaking. And hence I disagree with Kees that v2 is okay to > > > > > go. > > > > > > > > I meant that v2 is functionally identical to the existing code. > > > > > > Ah, sorry for misunderstanding. > > > > I agree with using __packed attribute to remove the extra padding (and > > for the reasons you mentioned before). That would reduce the sizeof(RXBUF) > > from 8 to 5 (which is good) but that is still 1 byte "too much". > > What I meant with the above is that the code has to work properly with > or without __packed. It's just to show you that this code has flaws if > it relies on the padding. > Right - that would work just as well. I will work on v3 with the suggestions given by you (sizing calculation amendments using overflow.h macros) and kees (adding the notes regarding the padding) then. - Paulo A.
diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c index b2735be81ab2..1ab2d552f498 100644 --- a/drivers/char/pcmcia/synclink_cs.c +++ b/drivers/char/pcmcia/synclink_cs.c @@ -105,7 +105,7 @@ static MGSL_PARAMS default_params = { typedef struct { int count; unsigned char status; - char data[1]; + char data[]; } RXBUF; /* The queue of BH actions to be performed */ @@ -2611,7 +2611,8 @@ static int mgslpc_proc_show(struct seq_file *m, void *v) static int rx_alloc_buffers(MGSLPC_INFO *info) { /* each buffer has header and data */ - info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size; + info->rx_buf_size = max(offsetof(typeof(RXBUF), data) + 1, sizeof(RXBUF)) + + info->max_frame_size; /* calculate total allocation size for 8 buffers */ info->rx_buf_total_size = info->rx_buf_size * 8;
One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element array with flexible-array member in struct RXBUF and refactor the rest of the code accordingly. It's worth mentioning that doing a build before/after this patch results in no binary output differences. This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1]. Link: https://github.com/KSPP/linux/issues/79 Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 [1] Signed-off-by: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com> --- drivers/char/pcmcia/synclink_cs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)