From nobody Sun Dec 22 02:10:07 2024 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 980F93F9D2 for ; Sat, 21 Dec 2024 09:49:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734774552; cv=none; b=sIgpczCe4fQ/AYYGX5yYyveBMGjPiTCV1BQviKJGbD1i7S9xRh1AB+8fCKM44Ki5bOJMSwMxX2f58Rr9UJ7vuHBkkeoy7LeQeceZrsK/MAA3R25bLIrm8EIv4HZEKzp1EFcO6AMtFa/+2UdiSb6BDc98XUVMnH/d4cmrcsLuFlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734774552; c=relaxed/simple; bh=cmVb76fr41h+B8X6CVyACIA8cA6pC9xqC9/Perks9Tw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=cyIl4MdLeiBg6E/J0GJa4LNIkPJRdfOXs4nrnJJktHiQxewJKuP858PS+hnsi0HFUFjrpB70BdAfrIj4bNjwIkUB8oqn3qP3Fdba3ulo04s6HYwrMGQl1U9wZ1IHtMJao20eD5IEKTOOW/GsxQx2jUyQ3oIEzDKi2uvjzWSALVM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=NCL8/xdQ; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="NCL8/xdQ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1734774538; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=/krp34iAvyquzhaabUgMh39+FlbVT/TCY2R0VKeKZU4=; b=NCL8/xdQq5s8ufIkJYtQj4L5aSCVne4wH0qkElLDMqjKcQuhRUJfmcjY/TcWF1v2OTXZUu IIeg+59Yx20/l563oVyjpo09UB47KVO0pnkhz8qQjOwS6GluzCuuvugyGb+ZKbJMgpiuVe QeuKf7ZQvSbmenZuTejxE9wjyBhcogE= From: Thorsten Blum To: Kees Cook , "Gustavo A. R. Silva" Cc: andriy.shevchenko@linux.intel.com, mcgrof@kernel.org, Thorsten Blum , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [RESEND PATCH v2] params: Annotate struct module_param_attrs with __counted_by() Date: Sat, 21 Dec 2024 10:48:02 +0100 Message-ID: <20241221094803.5329-2-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add the __counted_by compiler attribute to the flexible array member attrs to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE. Increment num before adding a new param_attribute to the attrs array and adjust the array index accordingly. Increment num immediately after the first reallocation such that the reallocation for the NULL terminator only needs to add 1 (instead of 2) to mk->mp->num. Use struct_size() instead of manually calculating the size for the reallocation. Use krealloc_array() for the additional NULL terminator. Signed-off-by: Thorsten Blum --- Changes in v2: - Use krealloc_array() as suggested by Andy Shevchenko - Link to v1: https://lore.kernel.org/r/20240823123300.37574-1-thorsten.blu= m@toblux.com/ --- kernel/params.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/kernel/params.c b/kernel/params.c index 2e447f8ae183..5f6643676697 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -551,7 +551,7 @@ struct module_param_attrs { unsigned int num; struct attribute_group grp; - struct param_attribute attrs[]; + struct param_attribute attrs[] __counted_by(num); }; =20 #ifdef CONFIG_SYSFS @@ -651,35 +651,32 @@ static __modinit int add_sysfs_param(struct module_ko= bject *mk, } =20 /* Enlarge allocations. */ - new_mp =3D krealloc(mk->mp, - sizeof(*mk->mp) + - sizeof(mk->mp->attrs[0]) * (mk->mp->num + 1), + new_mp =3D krealloc(mk->mp, struct_size(mk->mp, attrs, mk->mp->num + 1), GFP_KERNEL); if (!new_mp) return -ENOMEM; mk->mp =3D new_mp; + mk->mp->num++; =20 /* Extra pointer for NULL terminator */ - new_attrs =3D krealloc(mk->mp->grp.attrs, - sizeof(mk->mp->grp.attrs[0]) * (mk->mp->num + 2), - GFP_KERNEL); + new_attrs =3D krealloc_array(mk->mp->grp.attrs, mk->mp->num + 1, + sizeof(mk->mp->grp.attrs[0]), GFP_KERNEL); if (!new_attrs) return -ENOMEM; mk->mp->grp.attrs =3D new_attrs; =20 /* Tack new one on the end. */ - memset(&mk->mp->attrs[mk->mp->num], 0, sizeof(mk->mp->attrs[0])); - sysfs_attr_init(&mk->mp->attrs[mk->mp->num].mattr.attr); - mk->mp->attrs[mk->mp->num].param =3D kp; - mk->mp->attrs[mk->mp->num].mattr.show =3D param_attr_show; + memset(&mk->mp->attrs[mk->mp->num - 1], 0, sizeof(mk->mp->attrs[0])); + sysfs_attr_init(&mk->mp->attrs[mk->mp->num - 1].mattr.attr); + mk->mp->attrs[mk->mp->num - 1].param =3D kp; + mk->mp->attrs[mk->mp->num - 1].mattr.show =3D param_attr_show; /* Do not allow runtime DAC changes to make param writable. */ if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) !=3D 0) - mk->mp->attrs[mk->mp->num].mattr.store =3D param_attr_store; + mk->mp->attrs[mk->mp->num - 1].mattr.store =3D param_attr_store; else - mk->mp->attrs[mk->mp->num].mattr.store =3D NULL; - mk->mp->attrs[mk->mp->num].mattr.attr.name =3D (char *)name; - mk->mp->attrs[mk->mp->num].mattr.attr.mode =3D kp->perm; - mk->mp->num++; + mk->mp->attrs[mk->mp->num - 1].mattr.store =3D NULL; + mk->mp->attrs[mk->mp->num - 1].mattr.attr.name =3D (char *)name; + mk->mp->attrs[mk->mp->num - 1].mattr.attr.mode =3D kp->perm; =20 /* Fix up all the pointers, since krealloc can move us */ for (i =3D 0; i < mk->mp->num; i++) --=20 2.47.1