phy: sunplus: fix two issues in update_disc_vol()
Subsystems (4)
v2 cover phy: sunplus: fix two issues in update_disc_vol()
---
Changes in v2:
- Fix Vinod's comments.
- Split the commit into two patches, one for memory leak, one for error pointer deference.
- Link to v1: https://patch.msgid.link/[email protected]
To: Vincent Shih <[email protected]>
To: Vinod Koul <[email protected]>
To: Neil Armstrong <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Felix Gu (2):
phy: sunplus: fix memory leak in update_disc_vol()
phy: sunplus: fix ERR_PTR deref in update_disc_vol()
drivers/phy/sunplus/phy-sunplus-usb2.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
---
base-commit: 9a4cdc958dd79fc6c3b20b51a10debec6ca09fec
change-id: 20260804-sunplus-53a7bb1bcd72
Best regards,
Signature · 7 lines
Felix Gu <[email protected]> -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
PATCH v2 1/2 phy: sunplus: fix memory leak in update_disc_vol()
nvmem_cell_read() returns a kmalloc'd buffer that update_disc_vol() never freed. Free it after extracting the disconnect-voltage value.
--- drivers/phy/sunplus/phy-sunplus-usb2.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/phy/sunplus/phy-sunplus-usb2.c +4 -4
@@ -81,7 +81,7 @@ static int update_disc_vol(struct sp_usbphy *usbphy)char *disc_name ="disc_vol" ;ssize_t otp_l =0 ;char *otp_v; -u32 val, set; +u32 val, set =OTP_DISC_LEVEL_DEFAULT ; cell =nvmem_cell_get (usbphy->dev, disc_name);if (IS_ERR_OR_NULL (cell)) {
@@ -96,11 +96,11 @@ static int update_disc_vol(struct sp_usbphy *usbphy) set = *(otp_v +1 ); set = (set << (sizeof (char ) *8 )) | *otp_v; set = (set >> usbphy->disc_vol_addr_off) &J_DISC ; +if (set ==0 ) + set =OTP_DISC_LEVEL_DEFAULT ; +kfree (otp_v); } -if (IS_ERR (otp_v) || set ==0 ) - set =OTP_DISC_LEVEL_DEFAULT ; - val =readl (usbphy->phy_regs +CONFIG7 ); val = (val & ~J_DISC ) | set;writel (val, usbphy->phy_regs +CONFIG7 );
Signature · 7 lines
2.43.0 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
[email protected]
AI reviewRe: [PATCH v2 1/2] phy: sunplus: fix memory leak in update_disc_vol()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing bounds check on the buffer returned by nvmem_cell_read.
- [High] Clock and reset control resources leak on the error path of sp_uphy_init. --
--- Patch [1]: [PATCH v2 1/2] phy: sunplus: fix memory leak in update_disc_vol() --- commit 3e3f886e3032055dfbdc001346d3637c0a80b19d
Author: Felix Gu <[email protected]>
phy: sunplus: fix memory leak in update_disc_vol()
nvmem_cell_read() returns a kmalloc'd buffer that update_disc_vol() never freed. Free it after extracting the disconnect-voltage value.
Quoted 5 lines
drivers/phy/sunplus/phy-sunplus-usb2.c +0
-0@@ -81,7 +81,7 @@ static int update_disc_vol(struct sp_usbphy *usbphy)
[ ... ]
drivers/phy/sunplus/phy-sunplus-usb2.c static int update_disc_vol(struct sp_usbphy *usbphy) · line 96
Quoted 2 lines
drivers/phy/sunplus/phy-sunplus-usb2.c +0
-0@@ -96,11 +96,11 @@ static int update_disc_vol(struct sp_usbphy *usbphy) set = *(otp_v +1 );
[Severity: High]
This is a pre-existing issue, but does this code need a bounds check on the buffer returned by nvmem_cell_read()?
If the NVMEM cell defined in the device tree has a size of 1 byte, nvmem_cell_read() will allocate 1 byte and set otp_l = 1. This code unconditionally reads *(otp_v + 1), which could result in an out-of-bounds read.
Additionally, if the cell size is 0 bytes, nvmem_cell_read() will allocate 0 bytes, returning ZERO_SIZE_PTR. Since ZERO_SIZE_PTR passes the IS_ERR() check, dereferencing this pointer would immediately panic the kernel.
drivers/phy/sunplus/phy-sunplus-usb2.c static int update_disc_vol(struct sp_usbphy *usbphy) · line 103
Quoted 10 lines
drivers/phy/sunplus/phy-sunplus-usb2.c +3
-3set = (set << (sizeof (char ) *8 )) | *otp_v; set = (set >> usbphy->disc_vol_addr_off) &J_DISC ; +if (set ==0 ) + set =OTP_DISC_LEVEL_DEFAULT ; +kfree (otp_v); } -if (IS_ERR (otp_v) || set ==0 ) - set =OTP_DISC_LEVEL_DEFAULT ; -
[Severity: High]
This isn't a bug introduced by this patch, but does the error path in sp_uphy_init() leak clock and reset control resources?
In sp_uphy_init(), if update_disc_vol() returns -EPROBE_DEFER, the code appears to return directly instead of jumping to the err_reset cleanup label:
drivers/phy/sunplus/phy-sunplus-usb2.c:sp_uphy_init() {
...
ret = update_disc_vol(usbphy);
if (ret < 0)
return ret;
...
}Upon probe deferral retry, wouldn't the enable counts increment again without being decreased, permanently leaking the clock and reset control?
Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
Re: [PATCH v2 1/2] phy: sunplus: fix memory leak in update_disc_vol()
In reply to Felix GuQuoted 2 lines
nvmem_cell_read()returns a kmalloc'd buffer thatupdate_disc_vol()never freed. Free it after extracting the disconnect-voltage value.
How do you think about to apply the attribute “__free(kfree)” also in such a function implementation?
https://elixir.bootlin.com/linux/v7.2/source/include/linux/slab.h#L596
Regards,
Markus
PATCH v2 2/2 phy: sunplus: fix ERR_PTR deref in update_disc_vol()
In reply to Felix Gunvmem_cell_get() returns ERR_PTR on failure. For any error other than -EPROBE_DEFER the code fell through and passed the invalid pointer to nvmem_cell_read(), which dereferences it.
Skip the read and write the default disconnect voltage when the lookup fails.
--- drivers/phy/sunplus/phy-sunplus-usb2.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/phy/sunplus/phy-sunplus-usb2.c +3 -1
@@ -84,9 +84,10 @@ static int update_disc_vol(struct sp_usbphy *usbphy)u32 val, set =OTP_DISC_LEVEL_DEFAULT ; cell =nvmem_cell_get (usbphy->dev, disc_name); -if (IS_ERR_OR_NULL (cell)) { +if (IS_ERR (cell)) {if (PTR_ERR (cell) == -EPROBE_DEFER )return -EPROBE_DEFER ; + goto skip_nvmem_read; } otp_v =nvmem_cell_read (cell, &otp_l);
@@ -101,6 +102,7 @@ static int update_disc_vol(struct sp_usbphy *usbphy)kfree (otp_v); } +skip_nvmem_read: val =readl (usbphy->phy_regs +CONFIG7 ); val = (val & ~J_DISC ) | set;writel (val, usbphy->phy_regs +CONFIG7 );
Signature · 7 lines
2.43.0 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
[email protected]
AI reviewRe: [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref in update_disc_vol()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing validation of nvmem cell length can lead to an out-of-bounds read or ZERO_SIZE_PTR dereference.
- [High] Clock and reset control resources are leaked due to unbalanced operations in sp_uphy_init() error paths. --
--- Patch [2]: [PATCH v2 2/2] phy: sunplus: fix ERR_PTR deref in update_disc_vol() --- commit ec8ef9e1698074ff5a592bf46bc32ad0bdd396db
Author: Felix Gu <[email protected]>
phy: sunplus: fix ERR_PTR deref in update_disc_vol()
nvmem_cell_get() returns ERR_PTR on failure. The code previously fell through to nvmem_cell_read(), dereferencing the invalid pointer. This patch skips the read and writes the default disconnect voltage when the lookup fails.
drivers/phy/sunplus/phy-sunplus-usb2.c static int update_disc_vol(struct sp_usbphy *usbphy) · line 89
Quoted 12 lines
drivers/phy/sunplus/phy-sunplus-usb2.c +1
-1@@ -84,9 +84,10 @@ static int update_disc_vol(struct sp_usbphy *usbphy)u32 val, set =OTP_DISC_LEVEL_DEFAULT ; cell =nvmem_cell_get (usbphy->dev, disc_name); -if (IS_ERR_OR_NULL (cell)) { +if (IS_ERR (cell)) {if (PTR_ERR (cell) == -EPROBE_DEFER )return -EPROBE_DEFER ;
[Severity: High]
This is a pre-existing issue, but does this return path leak the clock and reset control references?
When update_disc_vol() returns -EPROBE_DEFER, the caller sp_uphy_init() returns the error directly without performing cleanup:
sp_uphy_init() {
...
ret = clk_prepare_enable(usbphy->phy_clk);
if (ret)
goto err_clk;
ret = reset_control_deassert(usbphy->rstc);
if (ret)
goto err_reset;
...
ret = update_disc_vol(usbphy);
if (ret < 0)
return ret;
...
}This early return bypasses the err_reset and err_clk cleanup labels, leading to unbounded incrementing of reference counts on subsequent retries if the nvmem provider is not yet ready.
drivers/phy/sunplus/phy-sunplus-usb2.c static int update_disc_vol(struct sp_usbphy *usbphy) · line 93
Quoted 4 lines
drivers/phy/sunplus/phy-sunplus-usb2.c +1
-0+ goto skip_nvmem_read; } otp_v =nvmem_cell_read (cell, &otp_l);
[Severity: High]
This isn't a bug introduced by this patch, but should the length of the nvmem cell returned in otp_l be validated?
If nvmem_cell_read() returns a cell with a length of 0, the allocator returns ZERO_SIZE_PTR which passes the !IS_ERR() check. Dereferencing this pointer would trigger a page fault:
if (!IS_ERR(otp_v)) {
set = *(otp_v + 1);
set = (set << (sizeof(char) * 8)) | *otp_v;If the cell length is 1, *(otp_v + 1) would cause an out-of-bounds read.
[ ... ]
Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2 -- linux-phy mailing list [email protected] https://lists.infradead.org/mailman/listinfo/linux-phy
On Fri, Aug 07, 2026 at 09:16:58PM +0800, Felix Gu wrote:
You should really add description to the cover letter describing what the series does.
Quoted 1 line
Signed-off-by Felix Gu <[email protected]>
For the patches,
- Mani
Signature · 1 line
மணிவண்ணன் சதாசிவம்