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

tty: rfcomm: 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 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.

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
7d2c7ddb c61e4112

+5 -8
+1 -1
include/net/bluetooth/rfcomm.h
··· 355 355 356 356 struct rfcomm_dev_list_req { 357 357 u16 dev_num; 358 - struct rfcomm_dev_info dev_info[]; 358 + struct rfcomm_dev_info dev_info[] __counted_by(dev_num); 359 359 }; 360 360 361 361 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
+4 -7
net/bluetooth/rfcomm/tty.c
··· 504 504 struct rfcomm_dev *dev; 505 505 struct rfcomm_dev_list_req *dl; 506 506 struct rfcomm_dev_info *di; 507 - int n = 0, size, err; 507 + int n = 0, err; 508 508 u16 dev_num; 509 509 510 510 BT_DBG(""); ··· 515 515 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di)) 516 516 return -EINVAL; 517 517 518 - size = sizeof(*dl) + dev_num * sizeof(*di); 519 - 520 - dl = kzalloc(size, GFP_KERNEL); 518 + dl = kzalloc(struct_size(dl, dev_info, dev_num), GFP_KERNEL); 521 519 if (!dl) 522 520 return -ENOMEM; 523 521 522 + dl->dev_num = dev_num; 524 523 di = dl->dev_info; 525 524 526 525 mutex_lock(&rfcomm_dev_lock); ··· 541 542 mutex_unlock(&rfcomm_dev_lock); 542 543 543 544 dl->dev_num = n; 544 - size = sizeof(*dl) + n * sizeof(*di); 545 - 546 - err = copy_to_user(arg, dl, size); 545 + err = copy_to_user(arg, dl, struct_size(dl, dev_info, n)); 547 546 kfree(dl); 548 547 549 548 return err ? -EFAULT : 0;