Skip to contentSkip to conversation

fix potential memory loss and exit codes

PATCH Applied Donald Yandt 2 participants 7 messages Mar 2, 2024 – Mar 4, 2024 netfilter

lore
Share & tools
lore
#0 lore raw

v2 cover fix potential memory loss and exit codes

Vector data will be lost if reallocation fails, leading to undefined behaviour. Additionally, the indices of the allocated vector data can be represented more precisely by using size_t as the index type.

If no configuration file or an invalid parameter is provided, the daemon should exit with a failure status.

v2:
- Moved variable declaration and described usage of size_t as suggested by Pablo Neira Ayuso <[email protected]>

Donald Yandt (3):
conntrackd: prevent memory loss if reallocation fails
conntrackd: use size_t for element indices
conntrackd: exit with failure status

 src/main.c   |  5 ++---
 src/vector.c | 11 ++++++-----
 2 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v2 1/3 conntrackd: prevent memory loss if reallocation fails

---
 src/vector.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
src/vector.c +5 -2
index c81e7ce..0af8db7 100644
@@ -60,13 +60,16 @@ void vector_destroy(struct vector *v)
 
 int vector_add(struct vector *v, void *data)
 {
+	void *ptr;
+
 	if (v->cur_elems >= v->max_elems) {
 		v->max_elems += DEFAULT_VECTOR_GROWTH;
-		v->data = realloc(v->data, v->max_elems * v->size);
-		if (v->data == NULL) {
+		ptr = realloc(v->data, v->max_elems * v->size);
+		if (ptr == NULL) {
 			v->max_elems -= DEFAULT_VECTOR_GROWTH;
 			return -1;
 		}
+		v->data = ptr;
 	}
 	memcpy(v->data + (v->size * v->cur_elems), data, v->size);
 	v->cur_elems++;
Signature · 1 line
2.44.0
#2 lore raw

PATCH v2 2/3 conntrackd: use size_t for element indices

In reply to Donald Yandt

The indices of the allocated vector data can be represented more precisely by using size_t as the index type. The size_t type integer is used in memory allocation routines and is capable of handling any allocated object size or index.

---
 src/vector.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
src/vector.c +1 -3
index 0af8db7..fb1014f 100644
@@ -23,9 +23,7 @@
 
 struct vector {
 	char *data;
-	unsigned int cur_elems;
-	unsigned int max_elems;
-	size_t size;
+	size_t cur_elems, max_elems, size;
 };
 
 #define DEFAULT_VECTOR_MEMBERS	8
Signature · 1 line
2.44.0
#3 lore raw

PATCH v2 3/3 conntrackd: exit with failure status

In reply to Donald Yandt
---
 src/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/main.c +2 -3
index de4773d..c6b2600 100644
@@ -175,7 +175,7 @@ int main(int argc, char *argv[])
 			}
 			show_usage(argv[0]);
 			dlog(LOG_ERR, "Missing config filename");
-			break;
+			exit(EXIT_FAILURE);
 		case 'F':
 			set_operation_mode(&type, REQUEST, argv);
 			i = set_action_by_table(i, argc, argv,
@@ -309,8 +309,7 @@ int main(int argc, char *argv[])
 		default:
 			show_usage(argv[0]);
 			dlog(LOG_ERR, "Unknown option: %s", argv[i]);
-			return 0;
-			break;
+			exit(EXIT_FAILURE);
 		}
 	}
 
Signature · 1 line
2.44.0
maintainer Applied
#4 lore raw
In reply to Donald Yandt
Quoted 6 lines · Donald Yandt

Vector data will be lost if reallocation fails, leading to undefined behaviour. Additionally, the indices of the allocated vector data can be represented more precisely by using size_t as the index type.

If no configuration file or an invalid parameter is provided, the daemon should exit with a failure status.

Applied.

I move this description chunks where they belong to

#5 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines · Donald Yandt

Vector data will be lost if reallocation fails, leading to undefined behaviour. Additionally, the indices of the allocated vector data can be represented more precisely by using size_t as the index type.

If no configuration file or an invalid parameter is provided, the daemon should exit with a failure status.

Applied.

BTW, I skipped 2/3, I am not convinced this gives us much.

Quoted 1 line

I move this description chunks where they belong to

#6 lore raw
Quoted 3 lines · Pablo Neira Ayuso

BTW, I skipped 2/3, I am not convinced this gives us much.

Hi Pablo,

Thank you, I greatly appreciate you accepting patches 1/3 and 3/3. In regards to 2/3, since we're using max_elems in allocation calls that take size_t as a parameter,
such as in realloc(..., v->max_elems * v->size), and cur_elems as an
offset of allocated memory, we should utilize that type. In the event that you concur,
it would also be necessary to modify the index type on line 83, which was missed.

Donald