Message ID | AS8PR02MB7237262C62B054FABD7229168BE12@AS8PR02MB7237.eurprd02.prod.outlook.com |
---|---|
State | New |
Headers | show |
Series | [v2] tty: rfcomm: prefer struct_size over open coded arithmetic | expand |
This is automated email and please do not reply to this email! Dear submitter, Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series: PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=852638 ---Test result--- Test Summary: CheckPatch PASS 0.83 seconds GitLint FAIL 0.47 seconds SubjectPrefix FAIL 0.31 seconds BuildKernel PASS 29.53 seconds CheckAllWarning PASS 32.27 seconds CheckSparse PASS 38.11 seconds CheckSmatch FAIL 35.80 seconds BuildKernel32 PASS 28.62 seconds TestRunnerSetup PASS 517.15 seconds TestRunner_l2cap-tester PASS 18.47 seconds TestRunner_iso-tester FAIL 31.31 seconds TestRunner_bnep-tester PASS 4.69 seconds TestRunner_mgmt-tester PASS 111.49 seconds TestRunner_rfcomm-tester PASS 7.21 seconds TestRunner_sco-tester PASS 15.40 seconds TestRunner_ioctl-tester PASS 7.58 seconds TestRunner_mesh-tester PASS 5.75 seconds TestRunner_smp-tester PASS 6.70 seconds TestRunner_userchan-tester PASS 4.87 seconds IncrementalBuild PASS 27.48 seconds Details ############################## Test: GitLint - FAIL Desc: Run gitlint Output: [v2] tty: rfcomm: prefer struct_size over open coded arithmetic WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search 10: B3 Line contains hard tab characters (\t): " [...]" 11: B3 Line contains hard tab characters (\t): " struct rfcomm_dev_info dev_info[];" 37: B1 Line exceeds max length (117>80): "Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]" 47: B1 Line exceeds max length (125>80): "v1 -> https://lore.kernel.org/linux-hardening/AS8PR02MB723725E0069F7AE8F64E876E8B142@AS8PR02MB7237.eurprd02.prod.outlook.com/" ############################## Test: SubjectPrefix - FAIL Desc: Check subject contains "Bluetooth" prefix Output: "Bluetooth: " prefix is not specified in the subject ############################## Test: CheckSmatch - FAIL Desc: Run smatch tool with source Output: Segmentation fault (core dumped) make[4]: *** [scripts/Makefile.build:244: net/bluetooth/hci_core.o] Error 139 make[4]: *** Deleting file 'net/bluetooth/hci_core.o' make[3]: *** [scripts/Makefile.build:485: net/bluetooth] Error 2 make[2]: *** [scripts/Makefile.build:485: net] Error 2 make[2]: *** Waiting for unfinished jobs.... Segmentation fault (core dumped) make[4]: *** [scripts/Makefile.build:244: drivers/bluetooth/bcm203x.o] Error 139 make[4]: *** Deleting file 'drivers/bluetooth/bcm203x.o' make[4]: *** Waiting for unfinished jobs.... Segmentation fault (core dumped) make[4]: *** [scripts/Makefile.build:244: drivers/bluetooth/bpa10x.o] Error 139 make[4]: *** Deleting file 'drivers/bluetooth/bpa10x.o' make[3]: *** [scripts/Makefile.build:485: drivers/bluetooth] Error 2 make[2]: *** [scripts/Makefile.build:485: drivers] Error 2 make[1]: *** [/github/workspace/src/src/Makefile:1919: .] Error 2 make: *** [Makefile:240: __sub-make] Error 2 ############################## Test: TestRunner_iso-tester - FAIL Desc: Run iso-tester with test-runner Output: Total: 122, Passed: 121 (99.2%), Failed: 1, Not Run: 0 Failed Test Cases ISO Connect Suspend - Success Failed 4.172 seconds --- Regards, Linux Bluetooth
On Sun, May 12, 2024 at 01:17:24PM +0200, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > this structure ends in a flexible array: > > struct rfcomm_dev_list_req { > [...] > struct rfcomm_dev_info dev_info[]; > }; > > the preferred way in the kernel is to use the struct_size() helper to > do the arithmetic instead of the calculation "size + count * size" in > the kzalloc() and copy_to_user() functions. > > At the same time, prepare for the coming implementation by GCC and Clang > of the __counted_by attribute. Flexible array members annotated with > __counted_by can have their accesses bounds-checked at run-time via > CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for > strcpy/memcpy-family functions). > > In this case, it is important to note that the logic needs a little > refactoring to ensure that the "dev_num" member is initialized before > the first access to the flex array. Specifically, add the assignment > before the list_for_each_entry() loop. > > Also remove the "size" variable as it is no longer needed and refactor > the list_for_each_entry() loop to use di[n] instead of (di + n). > > This way, the code is more readable, idiomatic and safer. > > This code was detected with the help of Coccinelle, and audited and > modified manually. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/160 [2] > Signed-off-by: Erick Archer <erick.archer@outlook.com> Looks good! Reviewed-by: Kees Cook <keescook@chromium.org> > [...] > - bacpy(&(di + n)->src, &dev->src); > - bacpy(&(di + n)->dst, &dev->dst); > + bacpy(&di[n].src, &dev->src); > + bacpy(&di[n].dst, &dev->dst); Not an issue with your patch, but this helper is really pointless in the Bluetooth tree: static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) { memcpy(dst, src, sizeof(bdaddr_t)); } So the above could just be: di[n].src = dev->src; di[n].dst = dev->dst; :P -Kees
On 12. 05. 24, 13:17, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > this structure ends in a flexible array: ... > --- a/include/net/bluetooth/rfcomm.h > +++ b/include/net/bluetooth/rfcomm.h ... > @@ -528,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) > list_for_each_entry(dev, &rfcomm_dev_list, list) { > if (!tty_port_get(&dev->port)) > continue; > - (di + n)->id = dev->id; > - (di + n)->flags = dev->flags; > - (di + n)->state = dev->dlc->state; > - (di + n)->channel = dev->channel; > - bacpy(&(di + n)->src, &dev->src); > - bacpy(&(di + n)->dst, &dev->dst); > + di[n].id = dev->id; > + di[n].flags = dev->flags; > + di[n].state = dev->dlc->state; > + di[n].channel = dev->channel; > + bacpy(&di[n].src, &dev->src); > + bacpy(&di[n].dst, &dev->dst); This does not relate much to "prefer struct_size over open coded arithmetic". It should have been in a separate patch. Other than that, LGTM. thanks,
Hi Jiri, Eric, On Mon, May 13, 2024 at 1:07 AM Jiri Slaby <jirislaby@kernel.org> wrote: > > On 12. 05. 24, 13:17, Erick Archer wrote: > > This is an effort to get rid of all multiplications from allocation > > functions in order to prevent integer overflows [1][2]. > > > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > > this structure ends in a flexible array: > ... > > --- a/include/net/bluetooth/rfcomm.h > > +++ b/include/net/bluetooth/rfcomm.h > ... > > @@ -528,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) > > list_for_each_entry(dev, &rfcomm_dev_list, list) { > > if (!tty_port_get(&dev->port)) > > continue; > > - (di + n)->id = dev->id; > > - (di + n)->flags = dev->flags; > > - (di + n)->state = dev->dlc->state; > > - (di + n)->channel = dev->channel; > > - bacpy(&(di + n)->src, &dev->src); > > - bacpy(&(di + n)->dst, &dev->dst); > > + di[n].id = dev->id; > > + di[n].flags = dev->flags; > > + di[n].state = dev->dlc->state; > > + di[n].channel = dev->channel; > > + bacpy(&di[n].src, &dev->src); > > + bacpy(&di[n].dst, &dev->dst); > > This does not relate much to "prefer struct_size over open coded > arithmetic". It should have been in a separate patch. +1, please split these changes into its own patch so we can apply it separately. > Other than that, LGTM. > > thanks, > -- > js > suse labs >
Hi Kees, Jiri and Luiz, First of all, thanks for the reviews. On Mon, May 13, 2024 at 12:29:04PM -0400, Luiz Augusto von Dentz wrote: > Hi Jiri, Eric, > > On Mon, May 13, 2024 at 1:07 AM Jiri Slaby <jirislaby@kernel.org> wrote: > > > > On 12. 05. 24, 13:17, Erick Archer wrote: > > > This is an effort to get rid of all multiplications from allocation > > > functions in order to prevent integer overflows [1][2]. > > > > > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > > > this structure ends in a flexible array: > > ... > > > --- a/include/net/bluetooth/rfcomm.h > > > +++ b/include/net/bluetooth/rfcomm.h > > ... > > > @@ -528,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) > > > list_for_each_entry(dev, &rfcomm_dev_list, list) { > > > if (!tty_port_get(&dev->port)) > > > continue; > > > - (di + n)->id = dev->id; > > > - (di + n)->flags = dev->flags; > > > - (di + n)->state = dev->dlc->state; > > > - (di + n)->channel = dev->channel; > > > - bacpy(&(di + n)->src, &dev->src); > > > - bacpy(&(di + n)->dst, &dev->dst); > > > + di[n].id = dev->id; > > > + di[n].flags = dev->flags; > > > + di[n].state = dev->dlc->state; > > > + di[n].channel = dev->channel; > > > + bacpy(&di[n].src, &dev->src); > > > + bacpy(&di[n].dst, &dev->dst); > > > > This does not relate much to "prefer struct_size over open coded > > arithmetic". It should have been in a separate patch. > > +1, please split these changes into its own patch so we can apply it separately. Ok, no problem. Also, I will simplify the "bacpy" lines with direct assignments as Kees suggested: di[n].src = dev->src; di[n].dst = dev->dst; instead of: bacpy(&di[n].src, &dev->src); bacpy(&di[n].dst, &dev->dst); Regards, Erick > > Other than that, LGTM. > > > > thanks, > > -- > > js > > suse labs > > > > > -- > Luiz Augusto von Dentz
On Mon, May 13, 2024 at 07:12:57PM +0200, Erick Archer wrote: > Hi Kees, Jiri and Luiz, > First of all, thanks for the reviews. > > On Mon, May 13, 2024 at 12:29:04PM -0400, Luiz Augusto von Dentz wrote: > > Hi Jiri, Eric, > > > > On Mon, May 13, 2024 at 1:07 AM Jiri Slaby <jirislaby@kernel.org> wrote: > > > > > > On 12. 05. 24, 13:17, Erick Archer wrote: > > > > This is an effort to get rid of all multiplications from allocation > > > > functions in order to prevent integer overflows [1][2]. > > > > > > > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > > > > this structure ends in a flexible array: > > > ... > > > > --- a/include/net/bluetooth/rfcomm.h > > > > +++ b/include/net/bluetooth/rfcomm.h > > > ... > > > > @@ -528,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) > > > > list_for_each_entry(dev, &rfcomm_dev_list, list) { > > > > if (!tty_port_get(&dev->port)) > > > > continue; > > > > - (di + n)->id = dev->id; > > > > - (di + n)->flags = dev->flags; > > > > - (di + n)->state = dev->dlc->state; > > > > - (di + n)->channel = dev->channel; > > > > - bacpy(&(di + n)->src, &dev->src); > > > > - bacpy(&(di + n)->dst, &dev->dst); > > > > + di[n].id = dev->id; > > > > + di[n].flags = dev->flags; > > > > + di[n].state = dev->dlc->state; > > > > + di[n].channel = dev->channel; > > > > + bacpy(&di[n].src, &dev->src); > > > > + bacpy(&di[n].dst, &dev->dst); > > > > > > This does not relate much to "prefer struct_size over open coded > > > arithmetic". It should have been in a separate patch. > > > > +1, please split these changes into its own patch so we can apply it separately. > > Ok, no problem. Also, I will simplify the "bacpy" lines with direct > assignments as Kees suggested: > > di[n].src = dev->src; > di[n].dst = dev->dst; > > instead of: > > bacpy(&di[n].src, &dev->src); > bacpy(&di[n].dst, &dev->dst); I think that's a separate thing and you can leave bacpy() as-is for now.
diff --git a/include/net/bluetooth/rfcomm.h b/include/net/bluetooth/rfcomm.h index 99d26879b02a..c05882476900 100644 --- a/include/net/bluetooth/rfcomm.h +++ b/include/net/bluetooth/rfcomm.h @@ -355,7 +355,7 @@ struct rfcomm_dev_info { struct rfcomm_dev_list_req { u16 dev_num; - struct rfcomm_dev_info dev_info[]; + struct rfcomm_dev_info dev_info[] __counted_by(dev_num); }; int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg); diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index 69c75c041fe1..af80d599c337 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -504,7 +504,7 @@ static int rfcomm_get_dev_list(void __user *arg) struct rfcomm_dev *dev; struct rfcomm_dev_list_req *dl; struct rfcomm_dev_info *di; - int n = 0, size, err; + int n = 0, err; u16 dev_num; BT_DBG(""); @@ -515,12 +515,11 @@ static int rfcomm_get_dev_list(void __user *arg) if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di)) return -EINVAL; - size = sizeof(*dl) + dev_num * sizeof(*di); - - dl = kzalloc(size, GFP_KERNEL); + dl = kzalloc(struct_size(dl, dev_info, dev_num), GFP_KERNEL); if (!dl) return -ENOMEM; + dl->dev_num = dev_num; di = dl->dev_info; mutex_lock(&rfcomm_dev_lock); @@ -528,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) list_for_each_entry(dev, &rfcomm_dev_list, list) { if (!tty_port_get(&dev->port)) continue; - (di + n)->id = dev->id; - (di + n)->flags = dev->flags; - (di + n)->state = dev->dlc->state; - (di + n)->channel = dev->channel; - bacpy(&(di + n)->src, &dev->src); - bacpy(&(di + n)->dst, &dev->dst); + di[n].id = dev->id; + di[n].flags = dev->flags; + di[n].state = dev->dlc->state; + di[n].channel = dev->channel; + bacpy(&di[n].src, &dev->src); + bacpy(&di[n].dst, &dev->dst); tty_port_put(&dev->port); if (++n >= dev_num) break; @@ -542,9 +541,7 @@ static int rfcomm_get_dev_list(void __user *arg) mutex_unlock(&rfcomm_dev_lock); dl->dev_num = n; - size = sizeof(*dl) + n * sizeof(*di); - - err = copy_to_user(arg, dl, size); + err = copy_to_user(arg, dl, struct_size(dl, dev_info, n)); kfree(dl); return err ? -EFAULT : 0;
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and this structure ends in a flexible array: struct rfcomm_dev_list_req { [...] struct rfcomm_dev_info dev_info[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc() and copy_to_user() functions. At the same time, prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). In this case, it is important to note that the logic needs a little refactoring to ensure that the "dev_num" member is initialized before the first access to the flex array. Specifically, add the assignment before the list_for_each_entry() loop. Also remove the "size" variable as it is no longer needed and refactor the list_for_each_entry() loop to use di[n] instead of (di + n). This way, the code is more readable, idiomatic and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] Signed-off-by: Erick Archer <erick.archer@outlook.com> --- Changes in v2: - Add the __counted_by() attribute (Kees Cook). - Refactor the list_for_each_entry() loop to use di[n] instead of (di + n) (Kees Cook). Previous versions: v1 -> https://lore.kernel.org/linux-hardening/AS8PR02MB723725E0069F7AE8F64E876E8B142@AS8PR02MB7237.eurprd02.prod.outlook.com/ --- include/net/bluetooth/rfcomm.h | 2 +- net/bluetooth/rfcomm/tty.c | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-)