Skip to contentSkip to conversation

erec: Sanitize erec location indesc

PATCH In review Phil Sutter 2 participants 10 messages Jan 26, 2021 – Feb 9, 2021 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 erec: Sanitize erec location indesc

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

---
 src/erec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/erec.c +2 -1
index c550a596b38c8..5c3351a512464 100644
@@ -38,7 +38,8 @@ void erec_add_location(struct error_record *erec, const struct location *loc)
 {
 	assert(erec->num_locations < EREC_LOCATIONS_MAX);
 	erec->locations[erec->num_locations] = *loc;
-	erec->locations[erec->num_locations].indesc = loc->indesc;
+	erec->locations[erec->num_locations].indesc = loc->indesc ?
+						    : &internal_indesc;
 	erec->num_locations++;
 }
 
Signature · 1 line
2.28.0
#1 lore raw

Hi Phil,

Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Quoted 22 lines
---
 src/erec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/erec.c +2 -1
index c550a596b38c8..5c3351a512464 100644
@@ -38,7 +38,8 @@ void erec_add_location(struct error_record *erec, const struct location *loc)
 {
 	assert(erec->num_locations < EREC_LOCATIONS_MAX);
 	erec->locations[erec->num_locations] = *loc;
-	erec->locations[erec->num_locations].indesc = loc->indesc;
+	erec->locations[erec->num_locations].indesc = loc->indesc ?
+						    : &internal_indesc;
 	erec->num_locations++;
 }
 
Signature · 1 line
2.28.0
author reviewer
#2 lore raw

Hi Pablo,

Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");

and this ruleset:

| table netdev t {
| 	chain c {
| 		reject
| 	}
| }

Cheers, Phil

#3 lore raw

Hi Phil,

Quoted 14 lines · Phil Sutter

Hi Pablo,

Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Quoted 9 lines

and this ruleset:

| table netdev t {
| 	chain c {
| 		reject
| 	}
| }

Cheers, Phil

src/evaluate.c +2 -2
index 030bbde4ab2c..771b71c83d01 100644
@@ -2729,9 +2729,9 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
 
 	desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
 	if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
-		return stmt_binary_error(ctx,
+		return stmt_binary_error(ctx, stmt,
 					 &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
-					 stmt, "unsupported link layer protocol");
+					 "unsupported link layer protocol");
 
 	desc = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
 	if (desc != NULL &&
#4 lore raw
Quoted 29 lines · Pablo Neira Ayuso

Hi Phil,

Quoted 14 lines · Phil Sutter

Hi Pablo,

Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

New patch fix, just do not use ctx->pctx.protocol[PROTO_BASE_LL_HDR] since it's an internal generated dependency, it is not visible from the rule, so stmt_binary_error() cannot really help with the error printing.

src/evaluate.c +2 -3
index 030bbde4ab2c..782a5bca98bb 100644
@@ -2729,9 +2729,8 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
 
 	desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
 	if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
-		return stmt_binary_error(ctx,
-					 &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
-					 stmt, "unsupported link layer protocol");
+		return __stmt_binary_error(ctx, &stmt->location, NULL,
+					   "cannot reject from this link layer protocol");
 
 	desc = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
 	if (desc != NULL &&
author reviewer
#5 lore raw
In reply to Pablo Neira Ayuso

Hi Pablo,

Quoted 25 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Thanks for addressing the root problem!

Quoted 2 lines

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Is it better than just sanitizing input to error functions? After all we just want to make sure users see the error message, right? Catching the programming mistake (wrong args passed to __stmt_binary_error()) IMHO is useful only if we can compile-time assert it. Otherwise we risk hiding error info from user.

Cheers, Phil

#6 lore raw
Quoted 35 lines · Phil Sutter

Hi Pablo,

Quoted 25 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Thanks for addressing the root problem!

Quoted 8 lines
Quoted 2 lines

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Is it better than just sanitizing input to error functions? After all we just want to make sure users see the error message, right? Catching the programming mistake (wrong args passed to __stmt_binary_error()) IMHO is useful only if we can compile-time assert it. Otherwise we risk hiding error info from user.

I see. I don't see a way to catch this at compile time.

Push out your patch and I'll push mine too for correctness.

#7 lore raw
Quoted 45 lines · Pablo Neira Ayuso
Quoted 35 lines · Phil Sutter

Hi Pablo,

Quoted 25 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Thanks for addressing the root problem!

Quoted 8 lines
Quoted 2 lines

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Is it better than just sanitizing input to error functions? After all we just want to make sure users see the error message, right? Catching the programming mistake (wrong args passed to __stmt_binary_error()) IMHO is useful only if we can compile-time assert it. Otherwise we risk hiding error info from user.

Quoted 3 lines

I see. I don't see a way to catch this at compile time.

Push out your patch and I'll push mine too for correctness.

Hm, one second: Probably set internal_indesc for autogenerated dependencies?

#8 lore raw
Quoted 46 lines · Pablo Neira Ayuso
Quoted 45 lines · Pablo Neira Ayuso
Quoted 43 lines · Phil Sutter

Hi Pablo,

Quoted 25 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Thanks for addressing the root problem!

Quoted 2 lines

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Is it better than just sanitizing input to error functions? After all we just want to make sure users see the error message, right? Catching the programming mistake (wrong args passed to __stmt_binary_error()) IMHO is useful only if we can compile-time assert it. Otherwise we risk hiding error info from user.

Quoted 6 lines
Quoted 3 lines

I see. I don't see a way to catch this at compile time.

Push out your patch and I'll push mine too for correctness.

Hm, one second: Probably set internal_indesc for autogenerated dependencies?

Either way, it's just changing where internal_indesc is set.

Probably not worth spending more cycles on this issue.

author reviewer
#9 lore raw
Quoted 47 lines · Pablo Neira Ayuso
Quoted 46 lines · Pablo Neira Ayuso
Quoted 45 lines · Pablo Neira Ayuso
Quoted 43 lines · Phil Sutter

Hi Pablo,

Quoted 25 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Phil Sutter

erec_print() unconditionally dereferences erec->locations->indesc, so make sure it is valid when either creating an erec or adding a location.

I guess your're trigger a bug where erec is indesc is NULL, thing is that indesc should be always set on. Is there a reproducer for this bug?

Yes, exactly. I hit it when trying to clean up the netdev family reject support, while just "hacking around". You can trigger it with the following change:

Quoted 11 lines
| --- a/src/evaluate.c
| +++ b/src/evaluate.c
| @@ -2718,7 +2718,7 @@ static int stmt_evaluate_reject_bridge(struct eval_ctx *ctx, struct stmt *stmt,
|         const struct proto_desc *desc;
|  
|         desc = ctx->pctx.protocol[PROTO_BASE_LL_HDR].desc;
| -       if (desc != &proto_eth && desc != &proto_vlan && desc != &proto_netdev)
| +       if (desc != &proto_eth && desc != &proto_vlan)
|                 return stmt_binary_error(ctx,
|                                          &ctx->pctx.protocol[PROTO_BASE_LL_HDR],
|                                          stmt, "unsupported link layer protocol");
Quoted 4 lines

I'm attaching fix.

Looks like call to stmt_binary_error() parameters are not in the right order, &ctx->pctx.protocol[PROTO_BASE_LL_HDR] has indesc.

Thanks for addressing the root problem!

Quoted 2 lines

Probably add a bugtrap to erec to check that indesc is always set on accordingly instead?

Is it better than just sanitizing input to error functions? After all we just want to make sure users see the error message, right? Catching the programming mistake (wrong args passed to __stmt_binary_error()) IMHO is useful only if we can compile-time assert it. Otherwise we risk hiding error info from user.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I see. I don't see a way to catch this at compile time.

Push out your patch and I'll push mine too for correctness.

DONE.

Quoted 6 lines
Quoted 2 lines

Hm, one second: Probably set internal_indesc for autogenerated dependencies?

Either way, it's just changing where internal_indesc is set.

Probably not worth spending more cycles on this issue.

ACK. :)

Cheers, Phil