Skip to contentSkip to conversation

"reachable by inconsistent control-flow paths"

cocci Jason A. Donenfeld 3 participants 10 messages Oct 9, 2022 – Oct 10, 2022

lore
Share & tools
lore
#0 lore raw

Hi,

I've got a fairly simple rule that works on easy contrived examples:

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16;
identifier v;
@@
u16 v;
... when exists
- v = get_random_u32();
+ v = get_random_u16();

However, when I try to apply it to a real function, afixed below, I get the error:

rule starting on line 1: node 172: off = ... ;[1,2,67,68,70] in nf_nat_l4proto_unique_tuple reachable by inconsistent control-flow paths

I tried using the option "--allow-inconsistent-paths", and it works, but that also seems potentially scary. I figure if it's not on by default, it must be for a good reason. Any idea what's up here?

Thanks,
Jason

---------------------------------------------------------------------

static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
const struct nf_nat_range2 *range, enum nf_nat_manip_type maniptype, const struct nf_conn *ct)

{
	unsigned int range_size, min, max, i, attempts;
	__be16 *keyptr;
	u16 off;
	static const unsigned int max_attempts = 128;

	switch (tuple->dst.protonum) {
	case IPPROTO_ICMP:
	case IPPROTO_ICMPV6:
		/* id is same for either direction... */
		keyptr = &tuple->src.u.icmp.id;
		if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
			min = 0;
			range_size = 65536;
		} else {
			min = ntohs(range->min_proto.icmp.id);
			range_size = ntohs(range->max_proto.icmp.id) -
				     ntohs(range->min_proto.icmp.id) + 1;
		}
		goto find_free_id;
#if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE)
	case IPPROTO_GRE:
		/* If there is no master conntrack we are not PPTP,
		   do not change tuples */
		if (!ct->master)
			return;

		if (maniptype == NF_NAT_MANIP_SRC)
			keyptr = &tuple->src.u.gre.key;
		else
			keyptr = &tuple->dst.u.gre.key;

		if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
			min = 1;
			range_size = 65535;
		} else {
			min = ntohs(range->min_proto.gre.key);
			range_size = ntohs(range->max_proto.gre.key) - min + 1;
		}
		goto find_free_id;
#endif
	case IPPROTO_UDP:
	case IPPROTO_UDPLITE:
	case IPPROTO_TCP:
	case IPPROTO_SCTP:
	case IPPROTO_DCCP:
		if (maniptype == NF_NAT_MANIP_SRC)
			keyptr = &tuple->src.u.all;
		else
			keyptr = &tuple->dst.u.all;

		break;
	default:
		return;
	}

	/* If no range specified... */
	if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
		/* If it's dst rewrite, can't change port */
		if (maniptype == NF_NAT_MANIP_DST)
			return;

		if (ntohs(*keyptr) < 1024) {
			/* Loose convention: >> 512 is credential passing */
			if (ntohs(*keyptr) < 512) {
				min = 1;
				range_size = 511 - min + 1;
			} else {
				min = 600;
				range_size = 1023 - min + 1;
			}
		} else {
			min = 1024;
			range_size = 65535 - 1024 + 1;
		}
	} else {
		min = ntohs(range->min_proto.all);
		max = ntohs(range->max_proto.all);
		if (unlikely(max < min))
			swap(max, min);
		range_size = max - min + 1;
	}

find_free_id:

	if (range->flags & NF_NAT_RANGE_PROTO_OFFSET)
		off = (ntohs(*keyptr) - ntohs(range->base_proto.all));
	else
		off = prandom_u32();

	attempts = range_size;
	if (attempts > max_attempts)
		attempts = max_attempts;

	/* We are in softirq; doing a search of the entire range risks
	 * soft lockup when all tuples are already used.
	 *
	 * If we can't find any free port from first offset, pick a new
	 * one and try again, with ever smaller search window.
	 */
another_round:
	for (i = 0; i < attempts; i++, off++) {
		*keyptr = htons(min + off % range_size);
		if (!nf_nat_used_tuple(tuple, ct))
			return;
	}

	if (attempts >= range_size || attempts < 16)
		return;
	attempts /= 2;
	off = prandom_u32();
	goto another_round;
}
#1 lore raw
Quoted 14 lines · Jason A. Donenfeld

Hi,

I've got a fairly simple rule that works on easy contrived examples:

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16;
identifier v;
@@
u16 v;
... when exists

(quoted diff) +1 -1
-  v = get_random_u32();
+  v = get_random_u16();
Quoted 8 lines

However, when I try to apply it to a real function, afixed below, I get the error:

rule starting on line 1: node 172: off = ... ;[1,2,67,68,70] in nf_nat_l4proto_unique_tuple reachable by inconsistent control-flow paths

I tried using the option "--allow-inconsistent-paths", and it works, but that also seems potentially scary. I figure if it's not on by default, it must be for a good reason. Any idea what's up here?

The meaning of the message is that Coccinelle thinks that it is possible to get to the call that you have changed without going through the u16 v declaration.

I'll check on your function and try to figure out why the message is getting triggered. It is indeed often not a good idea to use the --allow-inconsistent-paths flag when making changes.

julia

Quoted 123 lines

Thanks,
Jason

---------------------------------------------------------------------

static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
const struct nf_nat_range2 *range, enum nf_nat_manip_type maniptype, const struct nf_conn *ct)

{
	unsigned int range_size, min, max, i, attempts;
	__be16 *keyptr;
	u16 off;
	static const unsigned int max_attempts = 128;

	switch (tuple->dst.protonum) {
	case IPPROTO_ICMP:
	case IPPROTO_ICMPV6:
		/* id is same for either direction... */
		keyptr = &tuple->src.u.icmp.id;
		if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
			min = 0;
			range_size = 65536;
		} else {
			min = ntohs(range->min_proto.icmp.id);
			range_size = ntohs(range->max_proto.icmp.id) -
				     ntohs(range->min_proto.icmp.id) + 1;
		}
		goto find_free_id;
#if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE)
	case IPPROTO_GRE:
		/* If there is no master conntrack we are not PPTP,
		   do not change tuples */
		if (!ct->master)
			return;

		if (maniptype == NF_NAT_MANIP_SRC)
			keyptr = &tuple->src.u.gre.key;
		else
			keyptr = &tuple->dst.u.gre.key;

		if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
			min = 1;
			range_size = 65535;
		} else {
			min = ntohs(range->min_proto.gre.key);
			range_size = ntohs(range->max_proto.gre.key) - min + 1;
		}
		goto find_free_id;
#endif
	case IPPROTO_UDP:
	case IPPROTO_UDPLITE:
	case IPPROTO_TCP:
	case IPPROTO_SCTP:
	case IPPROTO_DCCP:
		if (maniptype == NF_NAT_MANIP_SRC)
			keyptr = &tuple->src.u.all;
		else
			keyptr = &tuple->dst.u.all;

		break;
	default:
		return;
	}

	/* If no range specified... */
	if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
		/* If it's dst rewrite, can't change port */
		if (maniptype == NF_NAT_MANIP_DST)
			return;

		if (ntohs(*keyptr) < 1024) {
			/* Loose convention: >> 512 is credential passing */
			if (ntohs(*keyptr) < 512) {
				min = 1;
				range_size = 511 - min + 1;
			} else {
				min = 600;
				range_size = 1023 - min + 1;
			}
		} else {
			min = 1024;
			range_size = 65535 - 1024 + 1;
		}
	} else {
		min = ntohs(range->min_proto.all);
		max = ntohs(range->max_proto.all);
		if (unlikely(max < min))
			swap(max, min);
		range_size = max - min + 1;
	}

find_free_id:

	if (range->flags & NF_NAT_RANGE_PROTO_OFFSET)
		off = (ntohs(*keyptr) - ntohs(range->base_proto.all));
	else
		off = prandom_u32();

	attempts = range_size;
	if (attempts > max_attempts)
		attempts = max_attempts;

	/* We are in softirq; doing a search of the entire range risks
	 * soft lockup when all tuples are already used.
	 *
	 * If we can't find any free port from first offset, pick a new
	 * one and try again, with ever smaller search window.
	 */
another_round:
	for (i = 0; i < attempts; i++, off++) {
		*keyptr = htons(min + off % range_size);
		if (!nf_nat_used_tuple(tuple, ct))
			return;
	}

	if (attempts >= range_size || attempts < 16)
		return;
	attempts /= 2;
	off = prandom_u32();
	goto another_round;
}
#2 lore raw

Hi Julia,

Quoted 3 lines · Julia Lawall

The meaning of the message is that Coccinelle thinks that it is possible to get to the call that you have changed without going through the u16 v declaration.

Huh, okay. That seems like a bug then?

Quoted 4 lines
Quoted 4 lines
{
      unsigned int range_size, min, max, i, attempts;
      __be16 *keyptr;
      u16 off;

The declaration is the third line of the function and there's no label above the declaration.

Jason

#3 lore raw
Quoted 9 lines · Jason A. Donenfeld

Hi Julia,

Quoted 3 lines · Julia Lawall

The meaning of the message is that Coccinelle thinks that it is possible to get to the call that you have changed without going through the u16 v declaration.

Huh, okay. That seems like a bug then?

If I remove the #if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE) and the #endif, then it works!

Jason

#4 lore raw
Quoted 5 lines · Jason A. Donenfeld
Quoted 4 lines · Jason A. Donenfeld

Hi Julia,

On Sun, Oct 9, 2022 at 9:15 AM Julia Lawall <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The meaning of the message is that Coccinelle thinks that it is possible to get to the call that you have changed without going through the u16 v declaration.

Huh, okay. That seems like a bug then?

If I remove the #if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE) and the #endif, then it works!

Ah. There is indeed probably a bug somewhere.

Would the following work for you?

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16;
local idexpression u16 v;
@@
- v = get_random_u32();
+ v = get_random_u16();

It would be faster and completely avoid the problem.

You only need "local" is you want to ensure that v is a local variable.

If you don't care at all about what form v has, you can put

u16 v;

julia

#5 lore raw
Quoted 8 lines · Julia Lawall

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16;
local idexpression u16 v;
@@

(quoted diff) +1 -1
-  v = get_random_u32();
+  v = get_random_u16();
Quoted 7 lines

It would be faster and completely avoid the problem.

You only need "local" is you want to ensure that v is a local variable.

If you don't care at all about what form v has, you can put

u16 v;

Oh that's terrific. Much simpler. Thank you.

Jason

#7 lore raw

Re: [cocci] Replacement of random functions for Linux

In reply to Jason A. Donenfeld
Quoted 1 line

I've got a fairly simple rule that works on easy contrived examples:

You published that you are working on the topic “treewide cleanup of random integer usage”.
https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32"; typedef u16;
identifier v;

Your clarification request triggered that Julia Lawall pointed another possibility out for such metavariable declarations an hour ago.

Review

Quoted 5 lines

@@
u16 v;
... when exists

(quoted diff) +1 -1
-  v = get_random_u32();
+  v = get_random_u16();

I would like to show further SmPL code variants so that you can choose better from corresponding run time characteristics of the Coccinelle software.

@replacement1@
identifier to_be_replaced =~ "^(?:get_random_(?:int|u32)|prandom_u32)$"; u16 target;
@@
 target =
-         to_be_replaced
+         get_random_u16
          ()

@replacement2@
u16 target;
@@
 target =
(
-         get_random_int
|
-         get_random_u32
|
-         prandom_u32
)
+         get_random_u16
          ()

Regards,
Markus

#8 lore raw

Re: [cocci] Replacement of random functions for Linux

Hi Markus,

Quoted 3 lines · Markus Elfring

Your clarification request triggered that Julia Lawall pointed another possibility out for such metavariable declarations an hour ago.

Thanks for the pointers.

Review

Quoted 8 lines

@replacement1@
identifier to_be_replaced =~ "^(?:get_random_(?:int|u32)|prandom_u32)$"; u16 target;
@@
target =

(quoted diff) +1 -1
-         to_be_replaced
+         get_random_u16
          ()

Any reason why you leave the () on the next line?

Quoted 14 lines

@replacement2@
u16 target;
@@
target =
(
- get_random_int
|
- get_random_u32
|
- prandom_u32
)
+ get_random_u16

Quoted 1 line
          ()

Comparing the two, the regex one is definitely easier to write multiple rules for. Is there a difference in speed? Will the regex one filter --dir, or do I have to use the latter one for that?

Jason

#9 lore raw

Re: [cocci] Replacement of random functions for Linux

Quoted 9 lines

Review

Quoted 8 lines

@replacement1@
identifier to_be_replaced =~ "^(?:get_random_(?:int|u32)|prandom_u32)$"; u16 target;
@@
target =

(quoted diff) +1 -1
-         to_be_replaced
+         get_random_u16
          ()

Any reason why you leave the () on the next line?

Yes, of course.

The parentheses should indicate as another context information that a macro/function call should be adjusted, shouldn't they?

Quoted 13 lines
Quoted 12 lines

@replacement2@
u16 target;
@@
target =
(
- get_random_int
|
- get_random_u32
|
- prandom_u32
)
+ get_random_u16

Quoted 1 line
          ()
Quoted 2 lines

Comparing the two, the regex one is definitely easier to write multiple rules for.

The application of advanced regular expression can occasionally become more appealing than other design options for SmPL constraints.

It seems that you do not need to use SmPL inheritance here.

Quoted 1 line

Is there a difference in speed?

Probably, yes.

Quoted 1 line

Will the regex one filter --dir,

I suggest to take another look at the documentation for spatch program parameters.

Quoted 1 line

or do I have to use the latter one for that?

I find your development concern unclear.

Regards,
Markus