Skip to contentSkip to conversation

telemetry: avoid truncation of strlcpy return before check

PATCH In review Tyler Retzlaff 4 participants 7 messages Aug 2, 2023 – Feb 1, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 telemetry: avoid truncation of strlcpy return before check

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/telemetry/telemetry_data.c +3 -2
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+	const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));
 	d->type = TEL_STRING;
-	d->data_len = strlcpy(d->data.str, str, sizeof(d->data.str));
-	if (d->data_len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
+	if (len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
 		d->data_len = RTE_TEL_MAX_SINGLE_STRING_LEN - 1;
 		return E2BIG; /* not necessarily and error, just truncation */
 	}
+	d->data_len = (unsigned int)len;
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---

This probably should be marked as a fix for backport.

Quoted 26 lines
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/telemetry/telemetry_data.c +3 -2
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+	const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));
 	d->type = TEL_STRING;
-	d->data_len = strlcpy(d->data.str, str, sizeof(d->data.str));
-	if (d->data_len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
+	if (len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
 		d->data_len = RTE_TEL_MAX_SINGLE_STRING_LEN - 1;
 		return E2BIG; /* not necessarily and error, just truncation */
 	}
+	d->data_len = (unsigned int)len;
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#2 lore raw
In reply to Tyler Retzlaff
Quoted 18 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
  lib/telemetry/telemetry_data.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
lib/telemetry/telemetry_data.c +0 -0
index 3b1a240..52307cb 100644

lib/telemetry/telemetry_data.c line 44

Quoted 4 lines
lib/telemetry/telemetry_data.c +1 -0
@@ -41,12 +41,13 @@
  int
  rte_tel_data_string(struct rte_tel_data *d, const char *str)
  {
+	const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));

sizeof(d->data.str) is equal to RTE_TEL_MAX_SINGLE_STRING_LEN(8192). So It seems that this truncation probably will not happen.

Quoted 11 lines
lib/telemetry/telemetry_data.c +2 -2
  	d->type = TEL_STRING;
-	d->data_len = strlcpy(d->data.str, str, sizeof(d->data.str));
-	if (d->data_len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
+	if (len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
  		d->data_len = RTE_TEL_MAX_SINGLE_STRING_LEN - 1;
  		return E2BIG; /* not necessarily and error, just truncation */
  	}
+	d->data_len = (unsigned int)len;
  	return 0;
  }
 
#3 lore raw
Quoted 15 lines · lihuisong (C)
Quoted 13 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 11 lines

lib/telemetry/telemetry_data.c line 44

Quoted 9 lines
lib/telemetry/telemetry_data.c +1 -0
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+	const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));

sizeof(d->data.str) is equal to RTE_TEL_MAX_SINGLE_STRING_LEN(8192). So It seems that this truncation probably will not happen.

agreed, regardless the data type choices permit a size that exceeds the range of the narrower type and the assignment results in a warning being generated on some targets. that's why the truncating cast is safe to add.

none of this would be necessary if data_len had been appropriately typed as size_t. Bruce should we be changing the type instead since we are in 23.11 merge window...?

#4 lore raw
Quoted 28 lines · Tyler Retzlaff
Quoted 15 lines · lihuisong (C)
Quoted 13 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 11 lines

lib/telemetry/telemetry_data.c line 44

Quoted 9 lines
lib/telemetry/telemetry_data.c +1 -0
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+	const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));

sizeof(d->data.str) is equal to RTE_TEL_MAX_SINGLE_STRING_LEN(8192). So It seems that this truncation probably will not happen.

Quoted 9 lines

agreed, regardless the data type choices permit a size that exceeds the range of the narrower type and the assignment results in a warning being generated on some targets. that's why the truncating cast is safe to add.

none of this would be necessary if data_len had been appropriately typed as size_t. Bruce should we be changing the type instead since we are in 23.11 merge window...?

I'm fine either way, to be honest.

#5 lore raw
Quoted 35 lines · Bruce Richardson
Quoted 33 lines · Tyler Retzlaff
Quoted 15 lines · lihuisong (C)
Quoted 13 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 11 lines

lib/telemetry/telemetry_data.c line 44

Quoted 9 lines
lib/telemetry/telemetry_data.c +1 -0
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+  const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));

sizeof(d->data.str) is equal to RTE_TEL_MAX_SINGLE_STRING_LEN(8192). So It seems that this truncation probably will not happen.

agreed, regardless the data type choices permit a size that exceeds the range of the narrower type and the assignment results in a warning being generated on some targets. that's why the truncating cast is safe to add.

Quoted 5 lines
Quoted 4 lines

none of this would be necessary if data_len had been appropriately typed as size_t. Bruce should we be changing the type instead since we are in 23.11 merge window...?

I'm fine either way, to be honest.

Can we conclude?
struct rte_tel_data seems internal (at least opaque from an application pov), so I suppose the option of changing data_len to size_t is still on the table.

And we are missing a Fixes: tag too.

Thanks.

Signature · 1 line
David Marchand
#6 lore raw
Quoted 41 lines · David Marchand
Quoted 39 lines · Bruce Richardson
Quoted 37 lines · Tyler Retzlaff
Quoted 15 lines · lihuisong (C)
Quoted 13 lines · Tyler Retzlaff

strlcpy returns type size_t when directly assigning to struct rte_tel_data data_len field it may be truncated leading to compromised length check that follows

Since the limit in the check is < UINT_MAX the value returned is safe to be cast to unsigned int (which may be narrower than size_t) but only after being checked against RTE_TEL_MAX_SINGLE_STRING_LEN

---
 lib/telemetry/telemetry_data.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 11 lines

lib/telemetry/telemetry_data.c line 44

Quoted 9 lines
lib/telemetry/telemetry_data.c +1 -0
index 3b1a240..52307cb 100644
@@ -41,12 +41,13 @@
 int
 rte_tel_data_string(struct rte_tel_data *d, const char *str)
 {
+  const size_t len = strlcpy(d->data.str, str, sizeof(d->data.str));

sizeof(d->data.str) is equal to RTE_TEL_MAX_SINGLE_STRING_LEN(8192). So It seems that this truncation probably will not happen.

agreed, regardless the data type choices permit a size that exceeds the range of the narrower type and the assignment results in a warning being generated on some targets. that's why the truncating cast is safe to add.

none of this would be necessary if data_len had been appropriately typed as size_t. Bruce should we be changing the type instead since we are in 23.11 merge window...?

Quoted 8 lines
Quoted 1 line

I'm fine either way, to be honest.

Can we conclude?
struct rte_tel_data seems internal (at least opaque from an application pov), so I suppose the option of changing data_len to size_t is still on the table.

And we are missing a Fixes: tag too.

there is actually a general pattern of this problem across dpdk tree and this fixes one instance.

i've marked the patch as rejected for now and hope to come back with a more comprehensive series after msvc work is merged.

ty

Quoted 5 lines

Thanks.

Signature · 1 line
David Marchand