Skip to contentSkip to conversation

set: buffer overflow in NFTNL_SET_DESC_CONCAT setter

PATCH In review Pablo Neira Ayuso 2 participants 4 messages Jan 11, 2024 – Jan 12, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 set: buffer overflow in NFTNL_SET_DESC_CONCAT setter

Allow to set a maximum limit of sizeof(s->desc.field_len) which is 16 bytes, otherwise, bail out. Ensure s->desc.field_count does not go over the array boundary.

Fixes 7cd41b5387ac ("set: Add support for NFTA_SET_DESC_CONCAT attributes")
---
 src/set.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/set.c +7 -1
index 719e59616e97..b51ff9e0ba64 100644
@@ -194,8 +194,14 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
 		memcpy(&s->desc.size, data, sizeof(s->desc.size));
 		break;
 	case NFTNL_SET_DESC_CONCAT:
+		if (data_len > sizeof(s->desc.field_len))
+			return -1;
+
 		memcpy(&s->desc.field_len, data, data_len);
-		while (s->desc.field_len[++s->desc.field_count]);
+		while (s->desc.field_len[++s->desc.field_count]) {
+			if (s->desc.field_count >= NFT_REG32_COUNT)
+				break;
+		}
 		break;
 	case NFTNL_SET_TIMEOUT:
 		memcpy(&s->timeout, data, sizeof(s->timeout));
Signature · 1 line
2.30.2
reviewer
#1 lore raw
Quoted 17 lines · Pablo Neira Ayuso

Allow to set a maximum limit of sizeof(s->desc.field_len) which is 16 bytes, otherwise, bail out. Ensure s->desc.field_count does not go over the array boundary.

Fixes 7cd41b5387ac ("set: Add support for NFTA_SET_DESC_CONCAT attributes")
---
 src/set.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/set.c +0 -0
index 719e59616e97..b51ff9e0ba64 100644
@@ -194,8 +194,14 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
 		memcpy(&s->desc.size, data, sizeof(s->desc.size));
 		break;

src/set.c int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data, · line 204

Quoted 10 lines
src/set.c +7 -1
 	case NFTNL_SET_DESC_CONCAT:
+		if (data_len > sizeof(s->desc.field_len))
+			return -1;
+
 		memcpy(&s->desc.field_len, data, data_len);
-		while (s->desc.field_len[++s->desc.field_count]);
+		while (s->desc.field_len[++s->desc.field_count]) {
+			if (s->desc.field_count >= NFT_REG32_COUNT)
+				break;
+		}

Isn't the second check redundant if you adjust the first one like so:

| if (data_len >= sizeof(s->desc.field_len))

Or more explicit:

| if (data_len > sizeof(s->desc.field_len) -
| sizeof(s->desc.field_len[0]))

Cheers, Phil

author maintainer
#2 lore raw
Quoted 31 lines · Phil Sutter
Quoted 17 lines · Pablo Neira Ayuso

Allow to set a maximum limit of sizeof(s->desc.field_len) which is 16 bytes, otherwise, bail out. Ensure s->desc.field_count does not go over the array boundary.

Fixes 7cd41b5387ac ("set: Add support for NFTA_SET_DESC_CONCAT attributes")
---
 src/set.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/set.c +0 -0
index 719e59616e97..b51ff9e0ba64 100644
@@ -194,8 +194,14 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
 		memcpy(&s->desc.size, data, sizeof(s->desc.size));
 		break;

src/set.c int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data, · line 204

Quoted 10 lines
src/set.c +7 -1
 	case NFTNL_SET_DESC_CONCAT:
+		if (data_len > sizeof(s->desc.field_len))
+			return -1;
+
 		memcpy(&s->desc.field_len, data, data_len);
-		while (s->desc.field_len[++s->desc.field_count]);
+		while (s->desc.field_len[++s->desc.field_count]) {
+			if (s->desc.field_count >= NFT_REG32_COUNT)
+				break;
+		}

Isn't the second check redundant if you adjust the first one like so:

Quoted 6 lines

| if (data_len >= sizeof(s->desc.field_len))

Or more explicit:

| if (data_len > sizeof(s->desc.field_len) -
| sizeof(s->desc.field_len[0]))

I see, you suggest to ensure last item in the array is always zero.

reviewer
#3 lore raw
Quoted 32 lines · Pablo Neira Ayuso
Quoted 31 lines · Phil Sutter
Quoted 17 lines · Pablo Neira Ayuso

Allow to set a maximum limit of sizeof(s->desc.field_len) which is 16 bytes, otherwise, bail out. Ensure s->desc.field_count does not go over the array boundary.

Fixes 7cd41b5387ac ("set: Add support for NFTA_SET_DESC_CONCAT attributes")
---
 src/set.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
src/set.c +0 -0
index 719e59616e97..b51ff9e0ba64 100644
@@ -194,8 +194,14 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
 		memcpy(&s->desc.size, data, sizeof(s->desc.size));
 		break;

src/set.c int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data, · line 204

Quoted 10 lines
src/set.c +7 -1
 	case NFTNL_SET_DESC_CONCAT:
+		if (data_len > sizeof(s->desc.field_len))
+			return -1;
+
 		memcpy(&s->desc.field_len, data, data_len);
-		while (s->desc.field_len[++s->desc.field_count]);
+		while (s->desc.field_len[++s->desc.field_count]) {
+			if (s->desc.field_count >= NFT_REG32_COUNT)
+				break;
+		}

Isn't the second check redundant if you adjust the first one like so:

Quoted 8 lines
Quoted 6 lines

| if (data_len >= sizeof(s->desc.field_len))

Or more explicit:

| if (data_len > sizeof(s->desc.field_len) -
| sizeof(s->desc.field_len[0]))

I see, you suggest to ensure last item in the array is always zero.

This is what's required by the while loop in the original form, I don't see a real reason for it, though.

Cheers, Phil