Skip to contentSkip to conversation

iproute2/m_ipt fix, opts was not set properly

PATCH Denys Fedoryshchenko 2 participants 7 messages Aug 5, 2008 – Aug 6, 2008

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

Since opts global variable, if it is set to original_opts, it was done in local context of function, and global variable in result was pointing to freed memory. Sure result of that - segfault, it is possible to trigger it only while processing batch, cause only on next iteration opts was wrong.

There is two ways to fix that, both patches attached.
Please send me note, which way correct (at least i will learn something :-) )

tc/m_ipt.c +4 -4
diff -Naur iproute2-original/tc/m_ipt.c iproute2-patched/tc/m_ipt.c
@@ -162,7 +162,7 @@
 	return result;
 }
 
-static void free_opts(struct option *opts)
+static void free_opts(void)
 {
 	if (opts != original_opts) {
 		free(opts);
@@ -455,7 +455,7 @@
 		if (matches(argv[optind], "index") == 0) {
 			if (get_u32(&index, argv[optind + 1], 10)) {
 				fprintf(stderr, "Illegal \"index\"\n");
-				free_opts(opts);
+				free_opts();
 				return -1;
 			}
 			iok++;
@@ -513,7 +513,7 @@
 	*argv_p = argv;
 
 	optind = 1;
-	free_opts(opts);
+	free_opts();
 
 	return 0;
 
@@ -594,7 +594,7 @@
 		fprintf(f, " \n");
 
 	}
-	free_opts(opts);
+	free_opts();
 
 	return 0;
 }
tc/m_ipt.c +3 -3
diff -Naur iproute2-original/tc/m_ipt.c iproute2-patched2/tc/m_ipt.c
@@ -162,10 +162,10 @@
 	return result;
 }
 
-static void free_opts(struct option *opts)
+static void free_opts(struct option *opts_local)
 {
-	if (opts != original_opts) {
-		free(opts);
+	if (opts_local != original_opts) {
+		free(opts_local);
 		opts = original_opts;
 		global_option_offset = 0;
 	}
#1 lore raw

Denys,

Quoted 5 lines · Denys Fedoryshchenko

Since opts global variable, if it is set to original_opts, it was done in local context of function, and global variable in result was pointing to freed memory.
Sure result of that - segfault, it is possible to trigger it only while processing batch, cause only on next iteration opts was wrong.

Are you sure this fixes any segfault? example, the variant you have below just changes a variable name:

-----
-static void free_opts(struct option *opts)
+static void free_opts(struct option *opts_local)
{
- if (opts != original_opts) {
- free(opts);
+ if (opts_local != original_opts) {

+               free(opts_local);
                opts = original_opts;
                global_option_offset = 0;
        }

-------

You could respin the patch with comment "change bad smelling name of a variable" - and that would be fine by me.

Please CC me on m_ipt or any of the other actions (as i have asked you before).

cheers,
jamal

#2 lore raw
Quoted 16 lines · jamal

Are you sure this fixes any segfault? example, the variant you have below just changes a variable name:

-----

tc/m_ipt.c +3 -4
-static void free_opts(struct option *opts)
+static void free_opts(struct option *opts_local)
 {
-       if (opts != original_opts) {
-               free(opts);
+       if (opts_local != original_opts) {
+               free(opts_local);
                opts = original_opts;
                global_option_offset = 0;
        }
-------
Quoted 2 lines

You could respin the patch with comment "change bad smelling name of a variable" - and that would be fine by me.

Make attention - before static void free_opts(struct option *opts) was shadowing global variable with same name *opts, after this patch - it will not shadow anymore.

#3 lore raw
Quoted 3 lines · Denys Fedoryshchenko

Make attention - before static void free_opts(struct option *opts) was shadowing global variable with same name *opts, after this patch - it will not shadow anymore.

Sorry Denys, unless my memory has lost the C scoping rules, i dont think the shadowing you describe above is accurate. Let me take a closer look.

[BTW, this code was all copied from iptables - so if this bug exists here, it likely still exists in iptables. The xtables changes will make this code a lot more maintainable].

cheers,
jamal

#4 lore raw
Quoted 2 lines · jamal

Sorry Denys, unless my memory has lost the C scoping rules, i dont think the shadowing you describe above is accurate. Let me take a closer look.

Grrr. I see the bug; until you tested with batching, it probably hasnt mattered.
I still dont see how your patch fixes it. But heres a fix that I think will work:

1. change free_opts to be:

static void free_opts(struct option **opts)

{
        if (*opts != original_opts) {
                free(*opts);
                *opts = original_opts;
        }
}

2. Change invocations of free_opts to be:

free_opts(&opts);

cheers,
jamal

#5 lore raw
Quoted 10 lines · jamal

1. change free_opts to be:

static void free_opts(struct option **opts)

{
        if (*opts != original_opts) {
                free(*opts);
                *opts = original_opts;
        }
}
Quoted 6 lines

2. Change invocations of free_opts to be:

free_opts(&opts);

cheers,
jamal

Does it work on my test batch?
Your way looks much more clean :-)
(I cannot test it properly, since i have modified iproute2 now)

#6 lore raw
Quoted 1 line · Denys Fedoryshchenko

Does it work on my test batch?

I havent tested with your scenario as i am staring at your script and trying to construct a simple case. It was just eyeballing. The original code was not resetting the contents "pointed to" by opts. So on return from free_opts, nothing has changed really.

Quoted 2 lines

Your way looks much more clean :-)
(I cannot test it properly, since i have modified iproute2 now)

It should work but i will test it. The simple test case i can think of is to have two ipt invocations with different parameters (eg mark and set tos).
If you test it, please resubmit with new approach. I will let you know my results (as i have a few vacation hours to kill this morning).

cheers,
jamal