Re: [PATCH v3] kernel/module: Fix mem leak in module_add_modinfo_attrs

From: Greg KH
Date: Tue Jun 11 2019 - 11:43:05 EST


On Tue, Jun 11, 2019 at 10:30:56PM +0800, Yuehaibing wrote:
> On 2019/6/11 21:33, Jessica Yu wrote:
> > +++ YueHaibing [03/06/19 22:45 +0800]:
> >> In module_add_modinfo_attrs if sysfs_create_file
> >> fails, we forget to free allocated modinfo_attrs
> >> and roll back the sysfs files.
> >>
> >> Fixes: 03e88ae1b13d ("[PATCH] fix module sysfs files reference counting")
> >> Signed-off-by: YueHaibing <yuehaibing@xxxxxxxxxx>
> >> ---
> >> v3: reuse module_remove_modinfo_attrs
> >> v2: free from '--i' instead of 'i--'
> >> ---
> >> kernel/module.c | 21 ++++++++++++++++-----
> >> 1 file changed, 16 insertions(+), 5 deletions(-)
> >>
> >> diff --git a/kernel/module.c b/kernel/module.c
> >> index 80c7c09..c6b8912 100644
> >> --- a/kernel/module.c
> >> +++ b/kernel/module.c
> >> @@ -1697,6 +1697,8 @@ static int add_usage_links(struct module *mod)
> >> return ret;
> >> }
> >>
> >> +static void module_remove_modinfo_attrs(struct module *mod, int end);
> >> +
> >> static int module_add_modinfo_attrs(struct module *mod)
> >> {
> >> struct module_attribute *attr;
> >> @@ -1711,24 +1713,33 @@ static int module_add_modinfo_attrs(struct module *mod)
> >> return -ENOMEM;
> >>
> >> temp_attr = mod->modinfo_attrs;
> >> - for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
> >> + for (i = 0; (attr = modinfo_attrs[i]); i++) {
> >> if (!attr->test || attr->test(mod)) {
> >> memcpy(temp_attr, attr, sizeof(*temp_attr));
> >> sysfs_attr_init(&temp_attr->attr);
> >> error = sysfs_create_file(&mod->mkobj.kobj,
> >> &temp_attr->attr);
> >> + if (error)
> >> + goto error_out;
> >> ++temp_attr;
> >> }
> >> }
> >> +
> >> + return 0;
> >> +
> >> +error_out:
> >> + module_remove_modinfo_attrs(mod, --i);
> >
> > Gah, I think there is another issue here - if sysfs_create_file()
> > fails on the first iteration of the loop (so i = 0), then we will go
> > to error_out and end up calling module_remove_modinfo_attrs(mod, -1),
> > which, for i = 0, will call sysfs_remove_file() since attr->attr.name
> > had already been set before the failed call to sysfs_create_file().
> > Perhaps we need to check that i > 0 before calling
> > module_remove_modinfo_attrs() under error_out?
>
> Indeed, this should be checked, thanks!

There is a simple sysfs core function that does the whole "add a whole
group of files at once" in a simple manner. Perhaps you should be
calling that one instead?

thanks,

greg k-h