Skip to contentSkip to conversation

crypto: hisilicon/qm - annotate cap_tables with __counted_by_ptr

PATCH In review Bill Wendling 4 participants 4 messages Sep 23 – Oct 2

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: hisilicon/qm - annotate cap_tables with __counted_by_ptr

Annotate the 'qm_cap_table' and 'dev_cap_table' pointer fields in 'struct hisi_qm_cap_tables' with the '__counted_by_ptr' attribute. This improves bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE, allowing KASAN and compiler diagnostics to detect out-of-bounds accesses.

The 'qm_cap_table' pointer is associated with 'qm_cap_size' which specifies its element count. Similarly, 'dev_cap_table' is associated with 'dev_cap_size'.

To ensure safety under compiler instrumentation, the assignments in the initialization paths have been updated to set the 'size'/'count' fields prior to setting the pointer fields. This prevents any transient state where the pointer is valid but the count is 0 (or uninitialized), ensuring that subsequent accesses to these tables do not trigger false-positive bounds check panics.

---
 drivers/crypto/hisilicon/hpre/hpre_main.c | 2 +-
 drivers/crypto/hisilicon/qm.c             | 2 +-
 drivers/crypto/hisilicon/sec2/sec_main.c  | 2 +-
 drivers/crypto/hisilicon/zip/zip_main.c   | 2 +-
 include/linux/hisi_acc_qm.h               | 4 ++--
 5 files changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_main.c +1 -1
index b6903fce6071..4a1b2de476f5 100644
@@ -1234,8 +1234,8 @@ static int hpre_pre_store_cap_reg(struct hisi_qm *qm)
 		return -EINVAL;
 	}
 
-	qm->cap_tables.dev_cap_table = hpre_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = hpre_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/qm.c +1 -1
index c01966a4a33f..7d13476451e3 100644
@@ -5743,8 +5743,8 @@ static int qm_pre_store_caps(struct hisi_qm *qm)
 							i, qm->cap_ver);
 	}
 
-	qm->cap_tables.qm_cap_table = qm_cap;
 	qm->cap_tables.qm_cap_size = size;
+	qm->cap_tables.qm_cap_table = qm_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/sec2/sec_main.c +1 -1
index 752565200c16..ac423d016998 100644
@@ -1285,8 +1285,8 @@ static int sec_pre_store_cap_reg(struct hisi_qm *qm)
 				     i, qm->cap_ver);
 	}
 
-	qm->cap_tables.dev_cap_table = sec_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = sec_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/zip/zip_main.c +1 -1
index 706a73656977..10091ad4a6a8 100644
@@ -1400,8 +1400,8 @@ static int zip_pre_store_cap_reg(struct hisi_qm *qm)
 				     i, qm->cap_ver);
 	}
 
-	qm->cap_tables.dev_cap_table = zip_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = zip_cap;
 
 	return 0;
 }
include/linux/hisi_acc_qm.h +2 -2
index f7570a409905..8dc49fa2123b 100644
@@ -332,9 +332,9 @@ struct hisi_qm_cap_record {
 
 struct hisi_qm_cap_tables {
 	u32 qm_cap_size;
-	struct hisi_qm_cap_record *qm_cap_table;
+	struct hisi_qm_cap_record *qm_cap_table __counted_by_ptr(qm_cap_size);
 	u32 dev_cap_size;
-	struct hisi_qm_cap_record *dev_cap_table;
+	struct hisi_qm_cap_record *dev_cap_table __counted_by_ptr(dev_cap_size);
 };
 
 struct hisi_qm_list {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Bill Wendling

Annotate the 'qm_cap_table' and 'dev_cap_table' pointer fields in 'struct hisi_qm_cap_tables' with the '__counted_by_ptr' attribute. This improves bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE, allowing KASAN and compiler diagnostics to detect out-of-bounds accesses.

The 'qm_cap_table' pointer is associated with 'qm_cap_size' which specifies its element count. Similarly, 'dev_cap_table' is associated with 'dev_cap_size'.

To ensure safety under compiler instrumentation, the assignments in the initialization paths have been updated to set the 'size'/'count' fields prior to setting the pointer fields. This prevents any transient state where the pointer is valid but the count is 0 (or uninitialized), ensuring that subsequent accesses to these tables do not trigger false-positive bounds check panics.

Quoted 3 lines

Thanks
-Gustavo

Quoted 80 lines
---
  drivers/crypto/hisilicon/hpre/hpre_main.c | 2 +-
  drivers/crypto/hisilicon/qm.c             | 2 +-
  drivers/crypto/hisilicon/sec2/sec_main.c  | 2 +-
  drivers/crypto/hisilicon/zip/zip_main.c   | 2 +-
  include/linux/hisi_acc_qm.h               | 4 ++--
  5 files changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_main.c +1 -1
index b6903fce6071..4a1b2de476f5 100644
@@ -1234,8 +1234,8 @@ static int hpre_pre_store_cap_reg(struct hisi_qm *qm)
  		return -EINVAL;
  	}
 
-	qm->cap_tables.dev_cap_table = hpre_cap;
  	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = hpre_cap;
 
  	return 0;
  }
drivers/crypto/hisilicon/qm.c +1 -1
index c01966a4a33f..7d13476451e3 100644
@@ -5743,8 +5743,8 @@ static int qm_pre_store_caps(struct hisi_qm *qm)
  							i, qm->cap_ver);
  	}
 
-	qm->cap_tables.qm_cap_table = qm_cap;
  	qm->cap_tables.qm_cap_size = size;
+	qm->cap_tables.qm_cap_table = qm_cap;
 
  	return 0;
  }
drivers/crypto/hisilicon/sec2/sec_main.c +1 -1
index 752565200c16..ac423d016998 100644
@@ -1285,8 +1285,8 @@ static int sec_pre_store_cap_reg(struct hisi_qm *qm)
  				     i, qm->cap_ver);
  	}
 
-	qm->cap_tables.dev_cap_table = sec_cap;
  	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = sec_cap;
 
  	return 0;
  }
drivers/crypto/hisilicon/zip/zip_main.c +1 -1
index 706a73656977..10091ad4a6a8 100644
@@ -1400,8 +1400,8 @@ static int zip_pre_store_cap_reg(struct hisi_qm *qm)
  				     i, qm->cap_ver);
  	}
 
-	qm->cap_tables.dev_cap_table = zip_cap;
  	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = zip_cap;
 
  	return 0;
  }
include/linux/hisi_acc_qm.h +2 -2
index f7570a409905..8dc49fa2123b 100644
@@ -332,9 +332,9 @@ struct hisi_qm_cap_record {
 
  struct hisi_qm_cap_tables {
  	u32 qm_cap_size;
-	struct hisi_qm_cap_record *qm_cap_table;
+	struct hisi_qm_cap_record *qm_cap_table __counted_by_ptr(qm_cap_size);
  	u32 dev_cap_size;
-	struct hisi_qm_cap_record *dev_cap_table;
+	struct hisi_qm_cap_record *dev_cap_table __counted_by_ptr(dev_cap_size);
  };
 
  struct hisi_qm_list {
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Bill Wendling
Quoted 20 lines · Bill Wendling

Annotate the 'qm_cap_table' and 'dev_cap_table' pointer fields in 'struct hisi_qm_cap_tables' with the '__counted_by_ptr' attribute. This improves bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE, allowing KASAN and compiler diagnostics to detect out-of-bounds accesses.

The 'qm_cap_table' pointer is associated with 'qm_cap_size' which specifies its element count. Similarly, 'dev_cap_table' is associated with 'dev_cap_size'.

To ensure safety under compiler instrumentation, the assignments in the initialization paths have been updated to set the 'size'/'count' fields prior to setting the pointer fields. This prevents any transient state where the pointer is valid but the count is 0 (or uninitialized), ensuring that subsequent accesses to these tables do not trigger false-positive bounds check panics.

Quoted 8 lines
---
 drivers/crypto/hisilicon/hpre/hpre_main.c | 2 +-
 drivers/crypto/hisilicon/qm.c             | 2 +-
 drivers/crypto/hisilicon/sec2/sec_main.c  | 2 +-
 drivers/crypto/hisilicon/zip/zip_main.c   | 2 +-
 include/linux/hisi_acc_qm.h               | 4 ++--
 5 files changed, 6 insertions(+), 6 deletions(-)

Thanks.
Longfang.

Quoted 73 lines
drivers/crypto/hisilicon/hpre/hpre_main.c +1 -1
index b6903fce6071..4a1b2de476f5 100644
@@ -1234,8 +1234,8 @@ static int hpre_pre_store_cap_reg(struct hisi_qm *qm)
 		return -EINVAL;
 	}
 
-	qm->cap_tables.dev_cap_table = hpre_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = hpre_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/qm.c +1 -1
index c01966a4a33f..7d13476451e3 100644
@@ -5743,8 +5743,8 @@ static int qm_pre_store_caps(struct hisi_qm *qm)
 							i, qm->cap_ver);
 	}
 
-	qm->cap_tables.qm_cap_table = qm_cap;
 	qm->cap_tables.qm_cap_size = size;
+	qm->cap_tables.qm_cap_table = qm_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/sec2/sec_main.c +1 -1
index 752565200c16..ac423d016998 100644
@@ -1285,8 +1285,8 @@ static int sec_pre_store_cap_reg(struct hisi_qm *qm)
 				     i, qm->cap_ver);
 	}
 
-	qm->cap_tables.dev_cap_table = sec_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = sec_cap;
 
 	return 0;
 }
drivers/crypto/hisilicon/zip/zip_main.c +1 -1
index 706a73656977..10091ad4a6a8 100644
@@ -1400,8 +1400,8 @@ static int zip_pre_store_cap_reg(struct hisi_qm *qm)
 				     i, qm->cap_ver);
 	}
 
-	qm->cap_tables.dev_cap_table = zip_cap;
 	qm->cap_tables.dev_cap_size = size;
+	qm->cap_tables.dev_cap_table = zip_cap;
 
 	return 0;
 }
include/linux/hisi_acc_qm.h +2 -2
index f7570a409905..8dc49fa2123b 100644
@@ -332,9 +332,9 @@ struct hisi_qm_cap_record {
 
 struct hisi_qm_cap_tables {
 	u32 qm_cap_size;
-	struct hisi_qm_cap_record *qm_cap_table;
+	struct hisi_qm_cap_record *qm_cap_table __counted_by_ptr(qm_cap_size);
 	u32 dev_cap_size;
-	struct hisi_qm_cap_record *dev_cap_table;
+	struct hisi_qm_cap_record *dev_cap_table __counted_by_ptr(dev_cap_size);
 };
 
 struct hisi_qm_list {
maintainer
#3 lore raw
In reply to Bill Wendling
Quoted 20 lines · Bill Wendling

Annotate the 'qm_cap_table' and 'dev_cap_table' pointer fields in 'struct hisi_qm_cap_tables' with the '__counted_by_ptr' attribute. This improves bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE, allowing KASAN and compiler diagnostics to detect out-of-bounds accesses.

The 'qm_cap_table' pointer is associated with 'qm_cap_size' which specifies its element count. Similarly, 'dev_cap_table' is associated with 'dev_cap_size'.

To ensure safety under compiler instrumentation, the assignments in the initialization paths have been updated to set the 'size'/'count' fields prior to setting the pointer fields. This prevents any transient state where the pointer is valid but the count is 0 (or uninitialized), ensuring that subsequent accesses to these tables do not trigger false-positive bounds check panics.

Quoted 7 lines
---
 drivers/crypto/hisilicon/hpre/hpre_main.c | 2 +-
 drivers/crypto/hisilicon/qm.c             | 2 +-
 drivers/crypto/hisilicon/sec2/sec_main.c  | 2 +-
 drivers/crypto/hisilicon/zip/zip_main.c   | 2 +-
 include/linux/hisi_acc_qm.h               | 4 ++--
 5 files changed, 6 insertions(+), 6 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt