From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2621842A141 for ; Wed, 23 Sep 2026 19:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790191495; cv=none; b=asupaplPGNizXTrqfy+2moC6HAZuREz1PX67qeKAAuDralhV3HqIvzggbJy4aSa+7BLG9rc+04qN0MyUoy8pkHV+fJ5iA5C+XHa8Jp7gJALTcnSV9NBTNAzvtXiTx1caToVGeHEk34GHqSIgyF5kO1N2m73+pNAIfH/M2Fr+SZI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790191495; c=relaxed/simple; bh=9c9OuAocaCCgtCpHxNZG42iIBMJ13yG9pQl/dWI+rDk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gWqDqknbxrz1xVJCFLN7BLN/Q53vBoiJv2ayXPYkTHBi7t8sb6eKqSv7nYZ6pekGSQ/cjEOI3VA/eFH6rTX0HsEkBF9samDpzXxe9to+D9RGvtVf3MYyp4/fgyuPh8PI0nRUm6K736z9s4NKFOSFr6HtMgtEcCxcdterX7aWTIQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qRMfJTg2; arc=none smtp.client-ip=209.85.210.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--morbo.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qRMfJTg2" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-8632fdf67dfso129841b3a.1 for ; Wed, 23 Sep 2026 12:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790191493; x=1790796293; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+WX8GeFlTKInVKU4q2TclqJUuJDXCWold0qvMqcWSxY=; b=qRMfJTg23fyUgmU7jxY7+RjTO4gWojpWqgx03hsLc8qf/Ia2nz1jyOG9AYm/HKAscN F8BbUx0G+xNHA13Wk/qLZ6j3t31kCMw+yBQ1BhME7Uds56HV3OXwe01tqFMKye2c7G0D +8g3dcRnlwdDg5OY3L+T6BLRs5AwHfHErIMfU8tDl8ghXN4NWHCoSkprZ+MovvxwKHuC v+64e5lEvcgtL/rd2f04tC9tY5NqdEwEzBcPTb+YDFvYeeZCUlEVdjWi4g6Rq/5dirK7 VRjpjWYDMNAzRhYvkX9Aimu4fCViJh13JV2r8I/uQR4qkfuCzMjpNcRSqdLAqy+wlgKH jckg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790191493; x=1790796293; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+WX8GeFlTKInVKU4q2TclqJUuJDXCWold0qvMqcWSxY=; b=Y9Y49c+Tf9grcmiBCz9HPwngZ+qyUlG0bBWpEpBqyfgxIYAFnZcn0vx5vg+RRwdb8w pGV4vc0YLg/cIPiHziwAlPBAqnVIQvSLSl0lCTyW+pJMChryvHt4fkCPLFXSAgA8d46O stYTbC4ALYjlPseUJ0xPPvcfTyMcVN2hwgTUPs9HOq0KmqMYh9AevqVxWErDo9j+Obfa UoY3jy5+HWxd4NcVZeuL8hsCgXXxB4yOWPL6P+/ooII/ecu4bo0XM3c68na0TS7mbA1z a7PXtIhTGsBFDSBc5WD/MMs75oGJdeTQS3PU6aat852wyGJOLmghGiVPFzX2NN00qAYA 5SJw== X-Forwarded-Encrypted: i=1; AKwUvByZFcPv88SLFFuDAA0rGqUSWCzHOe8osYAi8OlpN9p5S00upIjK2c+iJrVZMTc5SEcJ5UjB5QifzSXCSI0OzG4=@vger.kernel.org X-Gm-Message-State: AFuF++nKHCIUCWm3F9PXFQ95/dZKc898RTxaA5405+Wk1XZ6rPdMmENO mt/xwwZyIc5V4TNQWg4unIXRQ+AmTMYuTe5zP9oQdlsLjIV+tGCt7XVeCaJ3dwLFQH7BfZvl7As Q X-Received: from pfbdl8.prod.google.com ([2002:a05:6a00:4908:b0:87d:d8c6:60e8]) (user=morbo job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:ab03:b0:869:6440:74ba with SMTP id d2e1a72fcca58-87e9e3936f5mr83242b3a.18.1790191493219; Wed, 23 Sep 2026 12:24:53 -0700 (PDT) Date: Wed, 23 Sep 2026 19:24:50 +0000 In-Reply-To: <20260923041630.2553973-1-morbo@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260923041630.2553973-1-morbo@google.com> X-Mailer: git-send-email 2.56.0.rc1.310.g51773c2048-goog Message-ID: <20260923192450.3008671-1-morbo@google.com> Subject: [PATCH v2] iio: gts-helper: add __counted_by_ptr to struct iio_gts From: Bill Wendling To: Matti Vaittinen , Jonathan Cameron Cc: David Lechner , "=?UTF-8?q?Nuno=20S=C3=A1?=" , Andy Shevchenko , Kees Cook , "Gustavo A. R. Silva" , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Bill Wendling , codemender-patching+linux@google.com Content-Type: text/plain; charset="UTF-8" The compiler attribute __counted_by_ptr can be used by KASAN and UBSAN to detect out-of-bounds accesses to pointer fields in structs where a corresponding element count field is available. In "struct iio_gts", there are multiple pointer fields associated with an element count. This patch annotates these pointer fields with "__counted_by_ptr" to improve runtime safety: - 'hwgain_table': counted by 'num_hwgain' - 'itime_table': counted by 'num_itime' - 'per_time_avail_scale_tables': counted by 'num_itime' - 'avail_all_scales_table': counted by 'num_avail_all_scales' - 'avail_time_tables': counted by 'num_avail_time_tables' To ensure that the count is set correctly before any pointer is accessed or assigned, we update "iio_init_iio_gts()" to set the counts prior to assigning the pointers. Cc: codemender-patching+linux@google.com Assisted-by: LLM Signed-off-by: Bill Wendling Acked-by: Matti Vaittinen Reviewed-by: Gustavo A. R. Silva --- v2: Remove reordering of assignments. It's not necessary as an assignment of the pointer isn't an access that's checked by KASAN. --- include/linux/iio/iio-gts-helper.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/linux/iio/iio-gts-helper.h b/include/linux/iio/iio-gts-helper.h index 66f830ab9b49..7e7d3396ff1a 100644 --- a/include/linux/iio/iio-gts-helper.h +++ b/include/linux/iio/iio-gts-helper.h @@ -58,14 +58,14 @@ struct iio_itime_sel_mul { struct iio_gts { u64 max_scale; - const struct iio_gain_sel_pair *hwgain_table; + const struct iio_gain_sel_pair *hwgain_table __counted_by_ptr(num_hwgain); int num_hwgain; - const struct iio_itime_sel_mul *itime_table; + const struct iio_itime_sel_mul *itime_table __counted_by_ptr(num_itime); int num_itime; - int **per_time_avail_scale_tables; - int *avail_all_scales_table; + int **per_time_avail_scale_tables __counted_by_ptr(num_itime); + int *avail_all_scales_table __counted_by_ptr(num_avail_all_scales); int num_avail_all_scales; - int *avail_time_tables; + int *avail_time_tables __counted_by_ptr(num_avail_time_tables); int num_avail_time_tables; }; -- 2.56.0.rc1.310.g51773c2048-goog