Skip to contentSkip to conversation

dspbridge: proc: double free on error path

PATCH Applied Phil Carmody 2 participants 8 messages Feb 12, 2010 – Feb 18, 2010

lore
Share & tools
lore
#0 lore raw

cover dspbridge: proc: double free on error path

First a bit of a cleanup of what was clearly some legacy 'handle' usage, and then a new quicker escape route for an error path to avoid a double free.

It's tempting to get a bit more invasive, as there's a lot of quite dodgy looking code (e.g. casting away const) in the areas around my changes, but they'll have to wait for another time.

#1 lore raw

PATCH 1/2 dspbridge: proc: it's a pointer, not a handle

From: Phil Carmody <[email protected]>

If there's one thing worse than systems hungarian notation, it's misleading systems hungarian notation. And you don't need 2 copies of the pointer.

---
 drivers/dsp/bridge/rmgr/proc.c |   25 +++++++++++--------------
 1 files changed, 11 insertions(+), 14 deletions(-)
drivers/dsp/bridge/rmgr/proc.c +11 -14
index 2ccbc9b..7bc1bcd 100644
@@ -313,7 +313,6 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 	DSP_STATUS status = DSP_EFAIL;
 	u32 dwAutoStart = 0;	/* autostart flag */
 	struct PROC_OBJECT *pProcObject;
-	struct PROC_OBJECT *hProcObject;
 	char szExecFile[MAXCMDLINELEN];
 	char *argv[2];
 	struct MGR_OBJECT *hMgrObject = NULL;
@@ -345,19 +344,18 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 	GT_0trace(PROC_DebugMask, GT_1CLASS, "NTFY Created \n");
 	pProcObject->hDevObject = hDevObject;
 	pProcObject->hMgrObject = hMgrObject;
-	hProcObject = pProcObject;
 	status = DEV_GetIntfFxns(hDevObject, &pProcObject->pIntfFxns);
 	if (DSP_SUCCEEDED(status)) {
 		status = DEV_GetWMDContext(hDevObject,
 					&pProcObject->hWmdContext);
 		if (DSP_FAILED(status)) {
-			MEM_FreeObject(hProcObject);
+			MEM_FreeObject(pProcObject);
 			GT_0trace(PROC_DebugMask, GT_7CLASS,
 				 "PROC_AutoStart: Failed "
 				 "to get WMD Context \n");
 		}
 	} else {
-		MEM_FreeObject(hProcObject);
+		MEM_FreeObject(pProcObject);
 		GT_0trace(PROC_DebugMask, GT_7CLASS,
 			 "PROC_AutoStart: Failed to "
 			 "get IntFxns \n");
@@ -366,7 +364,7 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 		goto func_cont;
 
 	/* Stop the Device, put it into standby mode */
-	status = PROC_Stop(hProcObject);
+	status = PROC_Stop(pProcObject);
 
 	if (DSP_FAILED(status))
 		goto func_cont;
@@ -388,9 +386,9 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 		argv[0] = szExecFile;
 		argv[1] = NULL;
 		/* ...and try to load it: */
-		status = PROC_Load(hProcObject, 1, (CONST char **)argv, NULL);
+		status = PROC_Load(pProcObject, 1, (CONST char **)argv, NULL);
 		if (DSP_SUCCEEDED(status)) {
-			status = PROC_Start(hProcObject);
+			status = PROC_Start(pProcObject);
 			if (DSP_SUCCEEDED(status)) {
 				GT_0trace(PROC_DebugMask, GT_1CLASS,
 					  "PROC_AutoStart: Processor started "
@@ -409,9 +407,9 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 			 "No Exec file found \n");
 	}
 func_cont:
-	kfree(hProcObject->g_pszLastCoff);
-	hProcObject->g_pszLastCoff = NULL;
-	MEM_FreeObject(hProcObject);
+	kfree(pProcObject->g_pszLastCoff);
+	pProcObject->g_pszLastCoff = NULL;
+	MEM_FreeObject(pProcObject);
 func_end:
 	GT_1trace(PROC_DebugMask, GT_ENTER,
 		 "Exiting PROC_AutoStart, status:0x%x\n", status);
@@ -1742,7 +1740,7 @@ func_end:
  *      This does a WMD_BRD_Stop, DEV_Destroy2 and WMD_BRD_Monitor.
  *      In DEV_Destroy2 we delete the node manager.
  *  Parameters:
- *      hProcObject:    Handle to Processor Object
+ *      pProcObject:    Pointer to Processor Object
  *  Returns:
  *      DSP_SOK:	Processor placed in monitor mode.
  *      !DSP_SOK:       Failed to place processor in monitor mode.
@@ -1751,10 +1749,9 @@ func_end:
  *  Ensures:
  *      Success:	ProcObject state is PROC_IDLE
  */
-static DSP_STATUS PROC_Monitor(struct PROC_OBJECT *hProcObject)
+static DSP_STATUS PROC_Monitor(struct PROC_OBJECT *pProcObject)
 {
 	DSP_STATUS status = DSP_EFAIL;
-	struct PROC_OBJECT *pProcObject = (struct PROC_OBJECT *)hProcObject;
 	struct MSG_MGR *hMsgMgr;
 #ifdef CONFIG_BRIDGE_DEBUG
 	BRD_STATUS uBrdState;
@@ -1764,7 +1761,7 @@ static DSP_STATUS PROC_Monitor(struct PROC_OBJECT *hProcObject)
 	DBC_Require(MEM_IsValidHandle(pProcObject, PROC_SIGNATURE));
 
 	GT_1trace(PROC_DebugMask, GT_ENTER, "Entered PROC_Monitor, args:\n\t"
-		 "hProcessor: 0x%x\n", hProcObject);
+		 "hProcessor: 0x%x\n", pProcObject);
 	/* This is needed only when Device is loaded when it is
 	 * already 'ACTIVE' */
 	/* Destory the Node Manager, MSG Manager */
Signature · 1 line
1.6.0.4
#2 lore raw

PATCH 2/2 dspbridge: proc: fix a double-free on 2 error paths

From: Phil Carmody <[email protected]>

We free in the tail cleanup, so don't free before jumping there.

---
 drivers/dsp/bridge/rmgr/proc.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
drivers/dsp/bridge/rmgr/proc.c +5 -3
index 7bc1bcd..e89077b 100644
@@ -349,13 +349,11 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 		status = DEV_GetWMDContext(hDevObject,
 					&pProcObject->hWmdContext);
 		if (DSP_FAILED(status)) {
-			MEM_FreeObject(pProcObject);
 			GT_0trace(PROC_DebugMask, GT_7CLASS,
 				 "PROC_AutoStart: Failed "
 				 "to get WMD Context \n");
 		}
 	} else {
-		MEM_FreeObject(pProcObject);
 		GT_0trace(PROC_DebugMask, GT_7CLASS,
 			 "PROC_AutoStart: Failed to "
 			 "get IntFxns \n");
@@ -377,6 +375,10 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 			 "CFG_GetAutoStart DSP_FAILED \n");
 		goto func_cont;
 	}
+
+	/* paranoid - must be able to kfree this on remaining error paths */
+	pProcObject->g_pszLastCoff = NULL;
+
 	/* Get the default executable for this board... */
 	DEV_GetDevType(hDevObject, (u32 *)&devType);
 	pProcObject->uProcessor = devType;
@@ -406,9 +408,9 @@ DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode,
 		GT_0trace(PROC_DebugMask, GT_7CLASS, "PROC_AutoStart: "
 			 "No Exec file found \n");
 	}
-func_cont:
 	kfree(pProcObject->g_pszLastCoff);
 	pProcObject->g_pszLastCoff = NULL;
+func_cont:
 	MEM_FreeObject(pProcObject);
 func_end:
 	GT_1trace(PROC_DebugMask, GT_ENTER,
Signature · 1 line
1.6.0.4
#3 lore raw

Re: [PATCH 2/2] dspbridge: proc: fix a double-free on 2 error paths

Quoted 6 lines · Phil Carmody

From: Phil Carmody<[email protected]>

We free in the tail cleanup, so don't free before jumping there.

---

[...]

drivers/dsp/bridge/rmgr/proc.c DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode, · line 381

Quoted 5 lines
  	}
drivers/dsp/bridge/rmgr/proc.c +4 -0
+
+	/* paranoid - must be able to kfree this on remaining error paths */
+	pProcObject->g_pszLastCoff = NULL;
+

do we need this? afaik it should be NULL at this point, because of kmalloc + memset (which btw should be replaced by kzalloc).

i.e:
MEM_AllocObject(pProcObject, struct PROC_OBJECT, PROC_SIGNATURE); -> MEM_Calloc(sizeof(Obj), MEM_NONPAGED);
-> pMem = kmalloc(...)

	if (pMem)
		memset(pMem, 0, cBytes);
Quoted 3 lines
  	/* Get the default executable for this board... */
  	DEV_GetDevType(hDevObject, (u32 *)&devType);
  	pProcObject->uProcessor = devType;

[...]

Regards,

Omar

#4 lore raw

Re: [PATCH 2/2] dspbridge: proc: fix a double-free on 2 error paths

Quoted 9 lines · ext Omar Ramirez Luna
Quoted 6 lines · Phil Carmody

From: Phil Carmody<[email protected]>

We free in the tail cleanup, so don't free before jumping there.

---

[...]

Quoted 8 lines

drivers/dsp/bridge/rmgr/proc.c DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode, · line 381

Quoted 5 lines
  	}
drivers/dsp/bridge/rmgr/proc.c +4 -0
+
+	/* paranoid - must be able to kfree this on remaining error paths */
+	pProcObject->g_pszLastCoff = NULL;
+

do we need this? afaik it should be NULL at this point, because of kmalloc + memset (which btw should be replaced by kzalloc).

I suspected it wasn't. I confess I was a little bit rushed when looking at that.

Quoted 6 lines

i.e:
MEM_AllocObject(pProcObject, struct PROC_OBJECT, PROC_SIGNATURE); -> MEM_Calloc(sizeof(Obj), MEM_NONPAGED);
-> pMem = kmalloc(...)

	if (pMem)
		memset(pMem, 0, cBytes);

i.e. kzalloc().

Quoted 6 lines
Quoted 3 lines
  	/* Get the default executable for this board... */
  	DEV_GetDevType(hDevObject, (u32 *)&devType);
  	pProcObject->uProcessor = devType;

[...]

Regards,

I can resend without, if you like, whatever's simplest for you.

Phil

#5 lore raw

Re: [PATCH 2/2] dspbridge: proc: fix a double-free on 2 error paths

Quoted 31 lines · Phil Carmody
Quoted 9 lines · ext Omar Ramirez Luna
Quoted 6 lines · Phil Carmody

From: Phil Carmody<[email protected]>

We free in the tail cleanup, so don't free before jumping there.

---

[...]

Quoted 8 lines

drivers/dsp/bridge/rmgr/proc.c DSP_STATUS PROC_AutoStart(struct CFG_DEVNODE *hDevNode, · line 381

Quoted 5 lines
   	}
drivers/dsp/bridge/rmgr/proc.c +4 -0
+
+	/* paranoid - must be able to kfree this on remaining error paths */
+	pProcObject->g_pszLastCoff = NULL;
+

do we need this? afaik it should be NULL at this point, because of kmalloc + memset (which btw should be replaced by kzalloc).

I suspected it wasn't. I confess I was a little bit rushed when looking at that.

Quoted 6 lines

i.e:
MEM_AllocObject(pProcObject, struct PROC_OBJECT, PROC_SIGNATURE); -> MEM_Calloc(sizeof(Obj), MEM_NONPAGED);
-> pMem = kmalloc(...)

	if (pMem)
		memset(pMem, 0, cBytes);

i.e. kzalloc().

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
   	/* Get the default executable for this board... */
   	DEV_GetDevType(hDevObject, (u32 *)&devType);
   	pProcObject->uProcessor = devType;

[...]

Regards,

I can resend without, if you like, whatever's simplest for you.

No worries, I'll remove from original.

Regards,

Omar

Acked-by on v1 2/2 Applied
#6 lore raw

Re: [PATCH 2/2] dspbridge: proc: fix a double-free on 2 error paths

In reply to Phil Carmody
Quoted 9 lines · Phil Carmody

From: Phil Carmody<[email protected]>

We free in the tail cleanup, so don't free before jumping there.

---
  drivers/dsp/bridge/rmgr/proc.c |    8 +++++---
  1 files changed, 5 insertions(+), 3 deletions(-)

Removed NULL assignment as agreed.

Pushed to dspbridge.

- omar

Acked-by on v1 1/2 Applied
#7 lore raw

Re: [PATCH 1/2] dspbridge: proc: it's a pointer, not a handle

In reply to Phil Carmody
Quoted 11 lines · Phil Carmody

From: Phil Carmody<[email protected]>

If there's one thing worse than systems hungarian notation, it's misleading systems hungarian notation. And you don't need 2 copies of the pointer.

---
  drivers/dsp/bridge/rmgr/proc.c |   25 +++++++++++--------------
  1 files changed, 11 insertions(+), 14 deletions(-)

Pushed to dspbridge.

- omar