BPF List
 help / color / mirror / Atom feed
* [PATCH] samples/bpf: hbm: check the return value of fopen()
@ 2026-11-04  3:53 zhangchujun
  2026-11-04  4:15 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: zhangchujun @ 2026-11-04  3:53 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, zhangchujun

run_bpf_prog() passes the FILE pointer returned by fopen() directly
to fscanf() without checking it for NULL.  When the file cannot be
opened (e.g. the network interface is not named eth0), fscanf()
dereferences NULL and the program crashes.

Add a NULL check on the return value of fopen() at both call sites
and exit with an error message on failure.

Signed-off-by: zhangchujun <zhangchujun@cmss.chinamobile.com>
---
 samples/bpf/hbm.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/samples/bpf/hbm.c b/samples/bpf/hbm.c
index fc88d4dbdf48..23304b988a45 100644
--- a/samples/bpf/hbm.c
+++ b/samples/bpf/hbm.c
@@ -226,6 +226,8 @@ static int run_bpf_prog(char *prog, int cg_id)
 			do_error("gettimeofday failed", true);
 		t_last = t0;
 		fin = fopen("/sys/class/net/eth0/statistics/tx_bytes", "r");
+		if (!fin)
+			do_error("fopen fails", true);
 		if (fscanf(fin, "%llu", &last_eth_tx_bytes) != 1)
 			do_error("fscanf fails", false);
 		fclose(fin);
@@ -245,6 +247,8 @@ static int run_bpf_prog(char *prog, int cg_id)
 			t_last = t_new;
 			fin = fopen("/sys/class/net/eth0/statistics/tx_bytes",
 				    "r");
+			if (!fin)
+				do_error("fopen fails", true);
 			if (fscanf(fin, "%llu", &new_eth_tx_bytes) != 1)
 				do_error("fscanf fails", false);
 			fclose(fin);




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] samples/bpf: hbm: check the return value of fopen()
  2026-11-04  3:53 [PATCH] samples/bpf: hbm: check the return value of fopen() zhangchujun
@ 2026-11-04  4:15 ` Alexei Starovoitov
  0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2026-11-04  4:15 UTC (permalink / raw)
  To: zhangchujun, bpf; +Cc: daniel, andrii

On Tue, Nov 03, 2026 at 07:53 PM zhangchujun <zhangchujun@cmss.chinamobile.com> wrote:
> run_bpf_prog() passes the FILE pointer returned by fopen() directly
> to fscanf() without checking it for NULL.  When the file cannot be
> opened (e.g. the network interface is not named eth0), fscanf()
> dereferences NULL and the program crashes.

samples/bpf/ are dead.
'hbm -w' works with eth0 only. Usage() says so.
Without eth0 it fails with and without this patch.
I don't think we need this patch.

pw-bot: cr

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-04  4:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-04  3:53 [PATCH] samples/bpf: hbm: check the return value of fopen() zhangchujun
2026-11-04  4:15 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox