From patchwork Wed Jul 15 14:56:25 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ameya Palande X-Patchwork-Id: 35697 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n6FEufud024812 for ; Wed, 15 Jul 2009 14:56:45 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755208AbZGOO4n (ORCPT ); Wed, 15 Jul 2009 10:56:43 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755210AbZGOO4n (ORCPT ); Wed, 15 Jul 2009 10:56:43 -0400 Received: from smtp.nokia.com ([192.100.122.233]:35436 "EHLO mgw-mx06.nokia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755208AbZGOO4m (ORCPT ); Wed, 15 Jul 2009 10:56:42 -0400 Received: from esebh105.NOE.Nokia.com (esebh105.ntc.nokia.com [172.21.138.211]) by mgw-mx06.nokia.com (Switch-3.3.3/Switch-3.3.3) with ESMTP id n6FEu5JC007004; Wed, 15 Jul 2009 17:56:24 +0300 Received: from esebh102.NOE.Nokia.com ([172.21.138.183]) by esebh105.NOE.Nokia.com with Microsoft SMTPSVC(6.0.3790.3959); Wed, 15 Jul 2009 17:56:32 +0300 Received: from mgw-sa01.ext.nokia.com ([147.243.1.47]) by esebh102.NOE.Nokia.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Wed, 15 Jul 2009 17:56:31 +0300 Received: from localhost.localdomain (esdhcp04048.research.nokia.com [172.21.40.48]) by mgw-sa01.ext.nokia.com (Switch-3.2.6/Switch-3.2.6) with ESMTP id n6FEuQeV018779; Wed, 15 Jul 2009 17:56:29 +0300 From: Ameya Palande To: linux-omap@vger.kernel.org Cc: omar.ramirez@ti.com, x0095840@ti.com, nm@ti.com, hiroshi.doyu@nokia.com Subject: [PATCH 02/13] DSPBRIDGE: PROCWRAP_Load function cleanup in a complete mess Date: Wed, 15 Jul 2009 17:56:25 +0300 Message-Id: <1247669795-23895-3-git-send-email-ameya.palande@nokia.com> X-Mailer: git-send-email 1.6.2.4 In-Reply-To: <1247669795-23895-2-git-send-email-ameya.palande@nokia.com> References: <1247669795-23895-1-git-send-email-ameya.palande@nokia.com> <1247669795-23895-2-git-send-email-ameya.palande@nokia.com> X-OriginalArrivalTime: 15 Jul 2009 14:56:31.0995 (UTC) FILETIME=[70BB8CB0:01CA055C] X-Nokia-AV: Clean Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org If you followed some failure paths, it was entirely possible that you'd attempt to MEM_Free a user-space pointer, because it wouldn't have been replaced with a kernel-space copy yet. Now ensure there's a NULL pointer to stop the cleanup at the position of the first error. Signed-off-by: Phil Carmody Signed-off-by: Ameya Palande Acked-by: Fernando Guzman Lugo --- drivers/dsp/bridge/pmgr/wcd.c | 114 +++++++++++++++++++++++----------------- 1 files changed, 65 insertions(+), 49 deletions(-) diff --git a/drivers/dsp/bridge/pmgr/wcd.c b/drivers/dsp/bridge/pmgr/wcd.c index b8adb41..8111b60 100644 --- a/drivers/dsp/bridge/pmgr/wcd.c +++ b/drivers/dsp/bridge/pmgr/wcd.c @@ -880,99 +880,115 @@ u32 PROCWRAP_Load(union Trapped_Args *args) { s32 i, len; DSP_STATUS status = DSP_SOK; - char *temp; - s32 argc = args->ARGS_PROC_LOAD.iArgc; + char *temp; + s32 count = args->ARGS_PROC_LOAD.iArgc; u8 **argv, **envp = NULL; + DBC_Require(count > 0); + DBC_Require(count <= MAX_LOADARGS); - DBC_Require(argc > 0); - DBC_Require(argc <= MAX_LOADARGS); - - argv = MEM_Alloc(argc * sizeof(u8 *), MEM_NONPAGED); - if (argv == NULL) + argv = MEM_Alloc(count * sizeof(u8 *), MEM_NONPAGED); + if (!argv) { status = DSP_EMEMORY; + goto func_cont; + } - cp_fm_usr(argv, args->ARGS_PROC_LOAD.aArgv, status, argc); - if (DSP_FAILED(status)) + cp_fm_usr(argv, args->ARGS_PROC_LOAD.aArgv, status, count); + if (DSP_FAILED(status)) { + MEM_Free(argv); + argv = NULL; goto func_cont; + } - for (i = 0; DSP_SUCCEEDED(status) && (i < argc); i++) { - if (argv[i] != NULL) { - /* User space pointer to argument */ - temp = (char *) argv[i]; + for (i = 0; DSP_SUCCEEDED(status) && (i < count); i++) { + if (argv[i]) { + /* User space pointer to argument */ + temp = (char *) argv[i]; /* len is increased by 1 to accommodate NULL */ len = strlen_user((char *)temp) + 1; /* Kernel space pointer to argument */ argv[i] = MEM_Alloc(len, MEM_NONPAGED); - if (argv[i] == NULL) { + if (argv[i]) { + cp_fm_usr(argv[i], temp, status, len); + if (DSP_FAILED(status)) { + MEM_Free(argv[i]); + argv[i] = NULL; + goto func_cont; + } + } else { status = DSP_EMEMORY; - break; - } - cp_fm_usr(argv[i], temp, status, len); - if (DSP_FAILED(status)) goto func_cont; + } } } /* TODO: validate this */ - if (args->ARGS_PROC_LOAD.aEnvp != NULL) { + if (args->ARGS_PROC_LOAD.aEnvp) { /* number of elements in the envp array including NULL */ - len = 0; + count = 0; do { - len++; - get_user(temp, args->ARGS_PROC_LOAD.aEnvp); - } while (temp); - envp = MEM_Alloc(len * sizeof(u8 *), MEM_NONPAGED); - if (envp == NULL) { + get_user(temp, args->ARGS_PROC_LOAD.aEnvp + count); + count++; + } while (temp); + envp = MEM_Alloc(count * sizeof(u8 *), MEM_NONPAGED); + if (!envp) { status = DSP_EMEMORY; goto func_cont; } - cp_fm_usr(envp, args->ARGS_PROC_LOAD.aEnvp, status, len); - if (DSP_FAILED(status)) + cp_fm_usr(envp, args->ARGS_PROC_LOAD.aEnvp, status, count); + if (DSP_FAILED(status)) { + MEM_Free(envp); + envp = NULL; goto func_cont; - for (i = 0; DSP_SUCCEEDED(status) && (envp[i] != NULL); i++) { - /* User space pointer to argument */ - temp = (char *)envp[i]; + } + for (i = 0; DSP_SUCCEEDED(status) && envp[i]; i++) { + /* User space pointer to argument */ + temp = (char *)envp[i]; /* len is increased by 1 to accommodate NULL */ len = strlen_user((char *)temp) + 1; /* Kernel space pointer to argument */ envp[i] = MEM_Alloc(len, MEM_NONPAGED); - if (envp[i] == NULL) { + if (envp[i]) { + cp_fm_usr(envp[i], temp, status, len); + if (DSP_FAILED(status)) { + MEM_Free(envp[i]); + envp[i] = NULL; + goto func_cont; + } + } else { status = DSP_EMEMORY; - break; - } - cp_fm_usr(envp[i], temp, status, len); - if (DSP_FAILED(status)) goto func_cont; + } } } GT_5trace(WCD_debugMask, GT_ENTER, - "PROCWRAP_Load, hProcessor: 0x%x\n\tiArgc:" - "0x%x\n\taArgv: 0x%x\n\taArgv[0]: %s\n\taEnvp: 0x%0x\n", - args->ARGS_PROC_LOAD.hProcessor, - args->ARGS_PROC_LOAD.iArgc, args->ARGS_PROC_LOAD.aArgv, - argv[0], args->ARGS_PROC_LOAD.aEnvp); + "PROCWRAP_Load, hProcessor: 0x%x\n\tiArgc:" + "0x%x\n\taArgv: 0x%x\n\taArgv[0]: %s\n\taEnvp: 0x%0x\n", + args->ARGS_PROC_LOAD.hProcessor, + args->ARGS_PROC_LOAD.iArgc, args->ARGS_PROC_LOAD.aArgv, + argv[0], args->ARGS_PROC_LOAD.aEnvp); if (DSP_SUCCEEDED(status)) { status = PROC_Load(args->ARGS_PROC_LOAD.hProcessor, - args->ARGS_PROC_LOAD.iArgc, - (CONST char **)argv, (CONST char **)envp); + args->ARGS_PROC_LOAD.iArgc, + (CONST char **)argv, (CONST char **)envp); } func_cont: - if (envp != NULL) { + if (envp) { i = 0; - while (envp[i] != NULL) + while (envp[i]) MEM_Free(envp[i++]); MEM_Free(envp); } - if (argv != NULL) { - for (i = 0; i < argc; i++) { - if (argv[i] != NULL) - MEM_Free(argv[i]); - } + if (argv) { + count = args->ARGS_PROC_LOAD.iArgc; + for (i = 0; (i < count) && argv[i]; i++) + MEM_Free(argv[i]); + MEM_Free(argv); } + return status; }