Skip to contentSkip to conversation

Avoid out of bounds read from data

PATCH In review Maya Rashish 2 participants 4 messages Feb 17, 2021 – Feb 19, 2021 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Avoid out of bounds read from data

This might introduce some issues since we're now not filling the rest of the memory, but filling out with uninitialized garbage is probably as bad as leaving it as garbage.

---
 include/utils.h    | 2 ++
 src/expr/counter.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/utils.h +2 -0
index 8af5a8e..7413534 100644
@@ -67,6 +67,8 @@ void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
 
 #define array_size(arr)		(sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 
+#define	MIN(a,b)		(a>b ? (b) : (a))
+
 const char *nftnl_family2str(uint32_t family);
 int nftnl_str2family(const char *family);
 
src/expr/counter.c +2 -2
index 89a602e..fb036dd 100644
@@ -35,10 +35,10 @@ nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type,
 
 	switch(type) {
 	case NFTNL_EXPR_CTR_BYTES:
-		memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
+		memcpy(&ctr->bytes, data, MIN(data_len, sizeof(ctr->bytes)));
 		break;
 	case NFTNL_EXPR_CTR_PACKETS:
-		memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
+		memcpy(&ctr->pkts, data, MIN(data_len, sizeof(ctr->pkts)));
 		break;
 	default:
 		return -1;
Signature · 1 line
2.29.2
#1 lore raw
Quoted 4 lines · Maya Rashish

This might introduce some issues since we're now not filling the rest of the memory, but filling out with uninitialized garbage is probably as bad as leaving it as garbage.

What are you trying to fix?

Caller sends pass data as parameter that is smaller than sizeof(ctr->byte)?

Thanks.

Quoted 39 lines
---
 include/utils.h    | 2 ++
 src/expr/counter.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/utils.h +2 -0
index 8af5a8e..7413534 100644
@@ -67,6 +67,8 @@ void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
 
 #define array_size(arr)		(sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 
+#define	MIN(a,b)		(a>b ? (b) : (a))
+
 const char *nftnl_family2str(uint32_t family);
 int nftnl_str2family(const char *family);
 
src/expr/counter.c +2 -2
index 89a602e..fb036dd 100644
@@ -35,10 +35,10 @@ nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type,
 
 	switch(type) {
 	case NFTNL_EXPR_CTR_BYTES:
-		memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
+		memcpy(&ctr->bytes, data, MIN(data_len, sizeof(ctr->bytes)));
 		break;
 	case NFTNL_EXPR_CTR_PACKETS:
-		memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
+		memcpy(&ctr->pkts, data, MIN(data_len, sizeof(ctr->pkts)));
 		break;
 	default:
 		return -1;
Signature · 1 line
2.29.2
#2 lore raw

PATCH v2 2/2 Avoid out of bounds read from data

When data is smaller than the destination, &ctr->pkts.

This might introduce some issues since we're now not filling the rest of the memory, but filling out with uninitialized garbage is probably as bad as leaving it as garbage.

---
 include/utils.h    | 2 ++
 src/expr/counter.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/utils.h +2 -0
index 8af5a8e..6b22e46 100644
@@ -67,6 +67,8 @@ void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
 
 #define array_size(arr)		(sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 
+#define	MIN(a,b)		((a) > (b) ? (b) : (a))
+
 const char *nftnl_family2str(uint32_t family);
 int nftnl_str2family(const char *family);
 
src/expr/counter.c +2 -2
index 89a602e..fb036dd 100644
@@ -35,10 +35,10 @@ nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type,
 
 	switch(type) {
 	case NFTNL_EXPR_CTR_BYTES:
-		memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
+		memcpy(&ctr->bytes, data, MIN(data_len, sizeof(ctr->bytes)));
 		break;
 	case NFTNL_EXPR_CTR_PACKETS:
-		memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
+		memcpy(&ctr->pkts, data, MIN(data_len, sizeof(ctr->pkts)));
 		break;
 	default:
 		return -1;
Signature · 1 line
2.29.2
#3 lore raw

Hi Maya,

Quoted 6 lines · Maya Rashish

When data is smaller than the destination, &ctr->pkts.

This might introduce some issues since we're now not filling the rest of the memory, but filling out with uninitialized garbage is probably as bad as leaving it as garbage.

Probably you could update src/expr/ to use nftnl_assert_validate() to sanity check the input data length?

Please, have a look at nftnl_assert_attr_exists() and nftnl_assert_validate().

Quoted 39 lines
---
 include/utils.h    | 2 ++
 src/expr/counter.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/utils.h +2 -0
index 8af5a8e..6b22e46 100644
@@ -67,6 +67,8 @@ void __nftnl_assert_attr_exists(uint16_t attr, uint16_t attr_max,
 
 #define array_size(arr)		(sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
 
+#define	MIN(a,b)		((a) > (b) ? (b) : (a))
+
 const char *nftnl_family2str(uint32_t family);
 int nftnl_str2family(const char *family);
 
src/expr/counter.c +2 -2
index 89a602e..fb036dd 100644
@@ -35,10 +35,10 @@ nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type,
 
 	switch(type) {
 	case NFTNL_EXPR_CTR_BYTES:
-		memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
+		memcpy(&ctr->bytes, data, MIN(data_len, sizeof(ctr->bytes)));
 		break;
 	case NFTNL_EXPR_CTR_PACKETS:
-		memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
+		memcpy(&ctr->pkts, data, MIN(data_len, sizeof(ctr->pkts)));
 		break;
 	default:
 		return -1;
Signature · 1 line
2.29.2