Skip to contentSkip to conversation

tracing/probes: fix error check in parse_btf_field()

PATCH Superseded Carlos López 2 participants 4 messages May 25, 2024 – May 26, 2024 Tracing

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing/probes: fix error check in parse_btf_field()

btf_find_struct_member() might return NULL or an error via the ERR_PTR() macro. However, its caller in parse_btf_field() only checks for the NULL condition. Fix this by using IS_ERR() and returning the error up the stack.

Fixes c440adfbe3025 ("tracing/probes: Support BTF based data structure field access")
---
 kernel/trace/trace_probe.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/trace_probe.c +2 -0
index 5e263c141574..5417e9712157 100644
@@ -554,6 +554,8 @@ static int parse_btf_field(char *fieldname, const struct btf_type *type,
 			anon_offs = 0;
 			field = btf_find_struct_member(ctx->btf, type, fieldname,
 						       &anon_offs);
+			if (IS_ERR(field))
+				return PTR_ERR(field);
 			if (!field) {
 				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
 				return -ENOENT;
Signature · 1 line
2.35.3
#1 lore raw
Quoted 5 lines · On Sat, 25 May 2024 20:21:32 +0200 Carlos López <[email protected]> wrote:

btf_find_struct_member() might return NULL or an error via the ERR_PTR() macro. However, its caller in parse_btf_field() only checks for the NULL condition. Fix this by using IS_ERR() and returning the error up the stack.

Thanks for finding it!
I think this requires new error message for error_log file. Can you add the log as

trace_probe_log_err(ctx->offset, BTF_ERROR);

And define BTF_ERROR in ERRORS@kernel/trace/trace_probe.h ?

Thank you,

Quoted 22 lines
Fixes c440adfbe3025 ("tracing/probes: Support BTF based data structure field access")
---
 kernel/trace/trace_probe.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/trace_probe.c +2 -0
index 5e263c141574..5417e9712157 100644
@@ -554,6 +554,8 @@ static int parse_btf_field(char *fieldname, const struct btf_type *type,
 			anon_offs = 0;
 			field = btf_find_struct_member(ctx->btf, type, fieldname,
 						       &anon_offs);
+			if (IS_ERR(field))
+				return PTR_ERR(field);
 			if (!field) {
 				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
 				return -ENOENT;
Signature · 1 line
2.35.3
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#2 lore raw

Hi,

Quoted 9 lines · Masami Hiramatsu (Google)
Quoted 5 lines · On Sat, 25 May 2024 20:21:32 +0200 Carlos López <[email protected]> wrote:

btf_find_struct_member() might return NULL or an error via the ERR_PTR() macro. However, its caller in parse_btf_field() only checks for the NULL condition. Fix this by using IS_ERR() and returning the error up the stack.

Quoted 7 lines

Thanks for finding it!
I think this requires new error message for error_log file. Can you add the log as

trace_probe_log_err(ctx->offset, BTF_ERROR);

And define BTF_ERROR in ERRORS@kernel/trace/trace_probe.h ?

Sounds good, but should we perhaps reuse BAD_BTF_TID?

```
C(BAD_BTF_TID,		"Failed to get BTF type info."),\
```

btf_find_struct_member() fails if type is not a struct or if it runs OOM while allocating the anon stack, so it seems appropriate.

Best,
Carlos

Quoted 26 lines

Thank you,

Quoted 22 lines
Fixes c440adfbe3025 ("tracing/probes: Support BTF based data structure field access")
---
  kernel/trace/trace_probe.c | 2 ++
  1 file changed, 2 insertions(+)
kernel/trace/trace_probe.c +2 -0
index 5e263c141574..5417e9712157 100644
@@ -554,6 +554,8 @@ static int parse_btf_field(char *fieldname, const struct btf_type *type,
  			anon_offs = 0;
  			field = btf_find_struct_member(ctx->btf, type, fieldname,
  						       &anon_offs);
+			if (IS_ERR(field))
+				return PTR_ERR(field);
  			if (!field) {
  				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
  				return -ENOENT;
Signature · 1 line
2.35.3
Signature · 3 lines
Carlos López
Security Engineer
SUSE Software Solutions
#3 lore raw
Quoted 21 lines · On Sun, 26 May 2024 14:27:56 +0200 Carlos López <[email protected]> wrote:

Hi,

Quoted 9 lines · Masami Hiramatsu (Google)
Quoted 5 lines · On Sat, 25 May 2024 20:21:32 +0200 Carlos López <[email protected]> wrote:

btf_find_struct_member() might return NULL or an error via the ERR_PTR() macro. However, its caller in parse_btf_field() only checks for the NULL condition. Fix this by using IS_ERR() and returning the error up the stack.

Quoted 7 lines

Thanks for finding it!
I think this requires new error message for error_log file. Can you add the log as

trace_probe_log_err(ctx->offset, BTF_ERROR);

And define BTF_ERROR in ERRORS@kernel/trace/trace_probe.h ?

Quoted 8 lines

Sounds good, but should we perhaps reuse BAD_BTF_TID?

```
C(BAD_BTF_TID,		"Failed to get BTF type info."),\
```

btf_find_struct_member() fails if type is not a struct or if it runs OOM while allocating the anon stack, so it seems appropriate.

Good point, it sounds reasonable.

Thanks!

Quoted 35 lines

Best,
Carlos

Quoted 26 lines

Thank you,

Quoted 22 lines
Fixes c440adfbe3025 ("tracing/probes: Support BTF based data structure field access")
---
  kernel/trace/trace_probe.c | 2 ++
  1 file changed, 2 insertions(+)
kernel/trace/trace_probe.c +2 -0
index 5e263c141574..5417e9712157 100644
@@ -554,6 +554,8 @@ static int parse_btf_field(char *fieldname, const struct btf_type *type,
  			anon_offs = 0;
  			field = btf_find_struct_member(ctx->btf, type, fieldname,
  						       &anon_offs);
+			if (IS_ERR(field))
+				return PTR_ERR(field);
  			if (!field) {
  				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
  				return -ENOENT;
Signature · 1 line
2.35.3
Signature · 3 lines
Carlos López
Security Engineer
SUSE Software Solutions
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>