Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Bluetooth: hci_core: Prefer struct_size over open coded arithmetic

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 hci_dev_list_req" and this
structure ends in a flexible array:

struct hci_dev_list_req {
[...]
struct hci_dev_req dev_req[]; /* hci_dev_req structures */
};

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.

This way, the code is more readable 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]
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Erick Archer <erick.archer@outlook.com>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

authored by

Erick Archer and committed by
Luiz Augusto von Dentz
8f7dfe17 041677e7

+5 -8
+1 -1
include/net/bluetooth/hci_sock.h
··· 144 144 145 145 struct hci_dev_list_req { 146 146 __u16 dev_num; 147 - struct hci_dev_req dev_req[]; /* hci_dev_req structures */ 147 + struct hci_dev_req dev_req[] __counted_by(dev_num); 148 148 }; 149 149 150 150 struct hci_conn_list_req {
+4 -7
net/bluetooth/hci_core.c
··· 801 801 struct hci_dev *hdev; 802 802 struct hci_dev_list_req *dl; 803 803 struct hci_dev_req *dr; 804 - int n = 0, size, err; 804 + int n = 0, err; 805 805 __u16 dev_num; 806 806 807 807 if (get_user(dev_num, (__u16 __user *) arg)) ··· 810 810 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) 811 811 return -EINVAL; 812 812 813 - size = sizeof(*dl) + dev_num * sizeof(*dr); 814 - 815 - dl = kzalloc(size, GFP_KERNEL); 813 + dl = kzalloc(struct_size(dl, dev_req, dev_num), GFP_KERNEL); 816 814 if (!dl) 817 815 return -ENOMEM; 818 816 817 + dl->dev_num = dev_num; 819 818 dr = dl->dev_req; 820 819 821 820 read_lock(&hci_dev_list_lock); ··· 837 838 read_unlock(&hci_dev_list_lock); 838 839 839 840 dl->dev_num = n; 840 - size = sizeof(*dl) + n * sizeof(*dr); 841 - 842 - err = copy_to_user(arg, dl, size); 841 + err = copy_to_user(arg, dl, struct_size(dl, dev_req, n)); 843 842 kfree(dl); 844 843 845 844 return err ? -EFAULT : 0;