VirtualBox

Changeset 2860 in kBuild


Ignore:
Timestamp:
Sep 1, 2016 4:55:07 PM (9 years ago)
Author:
bird
Message:

kWorker: cmdline quoting fix.

Location:
trunk/src/kWorker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kWorker/Makefile.kmk

    r2858 r2860  
    6464       nt/ntstat.c \
    6565       nt/ntunlink.c \
    66        nt/kFsCache.c
     66       nt/kFsCache.c \
     67       quote_argv.c
    6768kbuild_version.c_DEFS = KBUILD_SVN_REV=$(KBUILD_SVN_REV)
    6869
  • trunk/src/kWorker/kWorker.c

    r2859 r2860  
    4747
    4848#include "nt/kFsCache.h"
     49#include "quote_argv.h"
    4950
    5051
     
    43084309
    43094310
     4311/**
     4312 * Creates a correctly quoted ANSI command line string from the given argv.
     4313 *
     4314 * @returns Pointer to the command line.
     4315 * @param   cArgs               Number of arguments.
     4316 * @param   papszArgs           The argument vector.
     4317 * @param   fWatcomBrainDamange Whether to apply watcom rules while quoting.
     4318 * @param   pcbCmdLine          Where to return the command line length,
     4319 *                              including one terminator.
     4320 */
     4321static char *kwSandboxInitCmdLineFromArgv(KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange, KSIZE *pcbCmdLine)
     4322{
     4323    KU32    i;
     4324    KSIZE   cbCmdLine;
     4325    char   *pszCmdLine;
     4326
     4327    /* Make a copy of the argument vector that we'll be quoting. */
     4328    char **papszQuotedArgs = alloca(sizeof(papszArgs[0]) * (cArgs + 1));
     4329    kHlpMemCopy(papszQuotedArgs, papszArgs, sizeof(papszArgs[0]) * (cArgs + 1));
     4330
     4331    /* Quote the arguments that need it. */
     4332    quote_argv(cArgs, papszQuotedArgs, fWatcomBrainDamange, 0 /*leak*/);
     4333
     4334    /* figure out cmd line length. */
     4335    cbCmdLine = 0;
     4336    for (i = 0; i < cArgs; i++)
     4337        cbCmdLine += strlen(papszQuotedArgs[i]) + 1;
     4338    *pcbCmdLine = cbCmdLine;
     4339
     4340    pszCmdLine = (char *)kHlpAlloc(cbCmdLine + 1);
     4341    if (pszCmdLine)
     4342    {
     4343        char *psz = kHlpStrPCopy(pszCmdLine, papszQuotedArgs[0]);
     4344        if (papszQuotedArgs[0] != papszArgs[0])
     4345            free(papszQuotedArgs[0]);
     4346
     4347        for (i = 1; i < cArgs; i++)
     4348        {
     4349            *psz++ = ' ';
     4350            psz = kHlpStrPCopy(psz, papszQuotedArgs[i]);
     4351            if (papszQuotedArgs[i] != papszArgs[i])
     4352                free(papszQuotedArgs[i]);
     4353        }
     4354        kHlpAssert((KSIZE)(&psz[1] - pszCmdLine) == cbCmdLine);
     4355
     4356        *psz++ = '\0';
     4357        *psz++ = '\0';
     4358    }
     4359
     4360    return pszCmdLine;
     4361}
     4362
     4363
    43104364
    43114365static int kwSandboxInit(PKWSANDBOX pSandbox, PKWTOOL pTool,
    4312                          KU32 cArgs, const char **papszArgs, KU32 cbArgs,
     4366                         KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
    43134367                         KU32 cEnvVars, const char **papszEnvVars)
    43144368{
    43154369    PPEB pPeb = kwSandboxGetProcessEnvironmentBlock();
    4316     char *psz;
    43174370    wchar_t *pwcPool;
    43184371    KSIZE cbStrings;
    43194372    KSIZE cwc;
     4373    KSIZE cbCmdLine;
    43204374    KU32 i;
    43214375
    43224376    /* Simple stuff. */
    4323     g_Sandbox.rcExitCode    = 256;
    4324     g_Sandbox.pTool         = pTool;
    4325     g_Sandbox.idMainThread  = GetCurrentThreadId();
    4326     g_Sandbox.TibMainThread = *(PNT_TIB)NtCurrentTeb();
    4327     g_Sandbox.pgmptr        = (char *)pTool->pszPath;
    4328     g_Sandbox.wpgmptr       = (wchar_t *)pTool->pwszPath;
    4329     g_Sandbox.cArgs         = cArgs;
    4330     g_Sandbox.papszArgs     = (char **)papszArgs;
    4331 
    4332     /*
    4333      * Create a command line from the given argv.
    4334      * ASSUME that it's correctly quoted by quote_argv.c already.
    4335      */
    4336     pSandbox->pszCmdLine = psz = (char *)kHlpAlloc(cbArgs + 1);
    4337     if (!psz)
     4377    pSandbox->rcExitCode    = 256;
     4378    pSandbox->pTool         = pTool;
     4379    pSandbox->idMainThread  = GetCurrentThreadId();
     4380    pSandbox->TibMainThread = *(PNT_TIB)NtCurrentTeb();
     4381    pSandbox->pgmptr        = (char *)pTool->pszPath;
     4382    pSandbox->wpgmptr       = (wchar_t *)pTool->pwszPath;
     4383    pSandbox->cArgs         = cArgs;
     4384    pSandbox->papszArgs     = (char **)papszArgs;
     4385    pSandbox->pszCmdLine    = kwSandboxInitCmdLineFromArgv(cArgs, papszArgs, fWatcomBrainDamange, &cbCmdLine);
     4386    if (!pSandbox->pszCmdLine)
    43384387        return KERR_NO_MEMORY;
    4339     psz = kHlpStrPCopy(psz, papszArgs[0]);
    4340     for (i = 1; i < cArgs; i++)
    4341     {
    4342         *psz++ = ' ';
    4343         psz = kHlpStrPCopy(psz, papszArgs[i]);
    4344     }
    4345     kHlpAssert((KSIZE)(&psz[1] - pSandbox->pszCmdLine) == cbArgs);
    4346     psz[0] = '\0';
    4347     psz[1] = '\0';
    43484388
    43494389    /*
     
    43514391     * We assume each ANSI char requires a surrogate pair in the UTF-16 variant.
    43524392     */
    4353     pSandbox->papwszArgs = (wchar_t **)kHlpAlloc(sizeof(wchar_t *) * (pSandbox->cArgs + 2) + cbArgs * 2 * sizeof(wchar_t));
     4393    pSandbox->papwszArgs = (wchar_t **)kHlpAlloc(sizeof(wchar_t *) * (pSandbox->cArgs + 2) + cbCmdLine * 2 * sizeof(wchar_t));
    43544394    if (!pSandbox->papwszArgs)
    43554395        return KERR_NO_MEMORY;
     
    43684408     * Convert the commandline string to UTF-16, same pessimistic approach as above.
    43694409     */
    4370     cbStrings = (cbArgs + 1) * 2 * sizeof(wchar_t);
     4410    cbStrings = (cbCmdLine + 1) * 2 * sizeof(wchar_t);
    43714411    pSandbox->pwszCmdLine = kHlpAlloc(cbStrings);
    43724412    if (!pSandbox->pwszCmdLine)
     
    44154455
    44164456
    4417 static int kwSandboxExec(PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KU32 cbArgs,
     4457static int kwSandboxExec(PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
    44184458                         KU32 cEnvVars, const char **papszEnvVars)
    44194459{
     
    44244464     * Initialize the sandbox environment.
    44254465     */
    4426     rc = kwSandboxInit(&g_Sandbox, pTool, cArgs, papszArgs, cbArgs, cEnvVars, papszEnvVars);
     4466    rc = kwSandboxInit(&g_Sandbox, pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
    44274467    if (rc == 0)
    44284468    {
     
    45314571 * @param   cArgs           The number of arguments.
    45324572 * @param   papszArgs       The argument vector.
    4533  * @param   cbArgs          The size of the argument strings and terminators.
     4573 * @param   fWatcomBrainDamange Whether to apply watcom rules while quoting.
    45344574 * @param   cEnvVars        The number of environment variables.
    45354575 * @param   papszEnvVars    The enviornment vector.
    45364576 */
    45374577static int kSubmitHandleJobUnpacked(const char *pszExecutable, const char *pszCwd,
    4538                                     KU32 cArgs, const char **papszArgs, KU32 cbArgs,
     4578                                    KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
    45394579                                    KU32 cEnvVars, const char **papszEnvVars)
    45404580{
     
    45804620                {
    45814621                    KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));
    4582                     rcExit = kwSandboxExec(pTool, cArgs, papszArgs, cbArgs, cEnvVars, papszEnvVars);
     4622                    rcExit = kwSandboxExec(pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
    45834623                }
    45844624                else
     
    46624702                if (papszArgs)
    46634703                {
    4664                     KU32 cbArgs;
    46654704                    KU32 i;
    46664705                    for (i = 0; i < cArgs; i++)
     
    46794718                    }
    46804719                    papszArgs[cArgs] = 0;
    4681                     cbArgs = (KU32)(pszMsg - papszArgs[0]) - cArgs + 1;
    46824720
    46834721                    /* Environment variable count. */
     
    47184756                                    if (cbMsg == 0)
    47194757                                    {
     4758                                        KBOOL fWatcomBrainDamange = K_FALSE; /** @todo fix fWatcomBrainDamange */
    47204759                                        /*
    47214760                                         * The next step.
    47224761                                         */
    47234762                                        rcExit = kSubmitHandleJobUnpacked(pszExecutable, pszCwd,
    4724                                                                           cArgs, papszArgs, cbArgs,
     4763                                                                          cArgs, papszArgs, fWatcomBrainDamange,
    47254764                                                                          cEnvVars, papszEnvVars);
    47264765                                    }
     
    48534892    char        szCwd[MAX_PATH];
    48544893    const char *pszCwd = getcwd(szCwd, sizeof(szCwd));
    4855     KU32        cbArgs;
    48564894    KU32        cEnvVars;
    48574895
     
    48984936     * Do the job.
    48994937     */
    4900     cbArgs = 0;
    4901     for (j = i; j < argc; j++)
    4902         cbArgs += (KU32)strlen(argv[j]) + 1;
    4903 
    49044938    cEnvVars = 0;
    49054939    while (environ[cEnvVars] != NULL)
     
    49084942    for (j = 0; j < cRepeats; j++)
    49094943    {
    4910         rcExit = kSubmitHandleJobUnpacked(argv[i], pszCwd, argc - i, &argv[i], cbArgs, cEnvVars, environ);
     4944        rcExit = kSubmitHandleJobUnpacked(argv[i], pszCwd,
     4945                                          argc - i, &argv[i], K_FALSE /* fWatcomBrainDamange*/,
     4946                                          cEnvVars, environ);
    49114947    }
    49124948
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette