On Thu, Mar 05, 2026 at 02:59:48PM +0800, Leon Hwang wrote:

SNIP

> > +static int tracing_multi_mod_fd(struct bpf_program *prog, int *btf_obj_fd)
> > +{
> > +   const char *attach_name, *sep, *mod_name = NULL;
> > +   int i, err, mod_len = 0;
> > +
> > +   *btf_obj_fd = 0;
> > +   attach_name = strchr(prog->sec_name, '/');
> > +
> > +   /* Program with no details in spec, using kernel btf. */
> > +   if (!attach_name)
> > +           return 0;
> > +
> > +   attach_name++;
> > +   sep = strchr(attach_name, ':');
> > +   if (sep) {
> > +           mod_name = attach_name;
> > +           mod_len = sep - mod_name;
> > +   }
> > +
> > +   /* Program with no module section, using kernel btf. */
> > +   if (!mod_name)
> > +           return 0;
> > +
> > +   err = load_module_btfs(prog->obj);
> > +   if (err)
> > +           return err;
> > +
> > +   for (i = 0; i < prog->obj->btf_module_cnt; i++) {
> > +           const struct module_btf *mod = &prog->obj->btf_modules[i];
> > +
> > +           if (mod_name && strncmp(mod->name, mod_name, mod_len) == 0) {
> 
> strncmp() is not enough to exactly compare two module names, as
> strncmp() only compares the first 'mod_len' bytes.
> 
> So, mod_name "foo" could match mod "foobar", since 'obj->btf_modules'
> are not sorted by name after loading from kernel.
> 
> To find the exact module via module name, "strncmp() == 0 &&
> mod->name[mod_len] == '\0'" would be better.

yes, good catch, will fix

SNIP

> > +static int collect_func_ids_by_glob(struct bpf_object *obj, const char 
> > *pattern, __u32 **ids)
> > +{
> > +   const char *mod_name = NULL, *sep;
> > +   int i, err, mod_len = 0;
> > +   struct btf *btf = NULL;
> > +
> > +   err = bpf_object__load_vmlinux_btf(obj, true);
> > +   if (err)
> > +           return err;
> > +
> > +   /* In case we have module specified, we will find its btf and use that. 
> > */
> > +   sep = strchr(pattern, ':');
> > +   if (sep) {
> > +           mod_name = pattern;
> > +           mod_len = sep - pattern;
> > +           pattern = sep + 1;
> > +
> > +           err = load_module_btfs(obj);
> > +           if (err)
> > +                   goto cleanup;
> > +
> > +           for (i = 0; i < obj->btf_module_cnt; i++) {
> > +                   const struct module_btf *mod = &obj->btf_modules[i];
> > +
> > +                   if (!strncmp(mod->name, mod_name, mod_len)) {
> 
> Same concern here.
> 
> Would it be better to factor out a helper to find module via name?

we have similar issue in find_kernel_btf_id, but we get away with
that with just continuing to loop modules if the lookup fails

I agree some helper would be good hear, will try to add it

thanks,
jirka

Reply via email to