In the Linux kernel, the following vulnerability has been resolved:
net: dsa: Fix possible memory leaks in dsaloopinit()
kmemleak reported memory leaks in dsaloopinit():
kmemleak: 12 new suspected memory leaks
unreferenced object 0xffff8880138ce000 (size 2048): comm "modprobe", pid 390, jiffies 4295040478 (age 238.976s) backtrace: [<000000006a94f1d5>] kmalloctrace+0x26/0x60 [<00000000a9c44622>] phydevicecreate+0x5d/0x970 [<00000000d0ee2afc>] getphydevice+0xf3/0x2b0 [<00000000dca0c71f>] _fixedphyregister.part.0+0x92/0x4e0 [<000000008a834798>] fixedphyregister+0x84/0xb0 [<0000000055223fcb>] dsaloopinit+0xa9/0x116 [dsa_loop] ...
There are two reasons for memleak in dsaloopinit().
First, fixedphyregister() create and register phy_device:
fixedphyregister() getphydevice() phydevicecreate() # freed by phydevicefree() phydeviceregister() # freed by phydeviceremove()
But fixedphyunregister() only calls phydeviceremove(). So the memory allocated in phydevicecreate() is leaked.
Second, when mdiodriverregister() fail in dsaloopinit(), it just returns and there is no cleanup for phydevs.
Fix the problems by catching the error of mdiodriverregister() in dsaloopinit(), then calling both fixedphyunregister() and phydevicefree() to release phydevs. Also add a function for phydevs cleanup to avoid duplacate.