Solved

Whats wrong with this line? Creating sig 11

Posted on 2008-11-02
11
475 Views
Last Modified: 2012-05-05
Hi All,

I use this code that reads a cfg file in and it works fine on windows and linux but not solaris which seems strange to me. I have put loads of printf's in and its down to one line. Can anyone help  please. I really can not understand why.

The cfg file looks like this

Revision: 2
MT-IP:127.0.0.1

The code that is crashing is

        /* remove trailing whitespace */
        ptr = Buffer + strlen(Buffer) - 1;

thanks for your time guys, hope you can help
Danny



/*****************************************************************************/

/*                                                                           */

/*                             jss_getConfigData                             */

/*                                                                           */

/*****************************************************************************/

/* Description: This function is called to get the config information        */

/*              from a cfg file and populate the ConfigInfo.                 */

/*                                                                           */

/*****************************************************************************/

int             jss_getConfigData(ConfigInfo * configInfo)

{
 

    FILE           *in;

    char            Buffer[LINELENGTH];

    char           *Command;

    char           *Parameter;

    char           *ptr;

    char            confFormatStr[AR_MAX_FULL_FILENAME];

    char            confFormatInt[AR_MAX_FULL_FILENAME];

    char            configPath[AR_MAX_FULL_FILENAME + 1] = "";

    char            tmpConfigPath[AR_MAX_FULL_FILENAME + 1] = "";

    int             gotnewMidTierSharedKey = FALSE;

		int				gotnewARUSERSharedKey = FALSE;

    char            dirSep[2] = "";

    int             i;
 

    /* provide diagnosis information */

    jss_writeToARLog(AR_PLUGIN_LOG_FINEST, "+ jss_getConfigData");
 

    sprintf(confFormatStr, "%%-%ds %%s", 30);

    sprintf(confFormatInt, "%%-%ds %%d", 30);
 

    if (*configInfo->arInstallDir == 0) {

        jss_writeToARLog(AR_PLUGIN_LOG_SEVERE,

                         "[80820] Could not determine install directory");

        return -1;

    }
 
 

    /* Set the showPasswords to false as default */

    configInfo->showPasswords = FALSE;
 

    /* Depending on the OS, the directory seperator will be / or \ This

     * function uses a macro to find out. */

    getDirSeperator(dirSep);
 

    /* Now build the arconfig directory to find where the .cfg files are */

    strcpy(configInfo->arConfigDir, configInfo->arInstallDir);

    strcat(configInfo->arConfigDir, dirSep);

    strcat(configInfo->arConfigDir, "conf");

    strcat(configInfo->arConfigDir, dirSep);
 

    /* Output the config directory for verification */

    jss_writeToARLog(AR_PLUGIN_LOG_CONFIG, confFormatStr, "\tConfiguration Directory Found", configInfo->arConfigDir);
 

    /* Now set the full path names for the config and tmp config files. */

    strcpy(configPath, configInfo->arConfigDir);

    strcat(configPath, CONFIGFILE);
 

    strcpy(tmpConfigPath, configInfo->arConfigDir);

    strcat(tmpConfigPath, TMPCONFIGFILE);
 

    strcpy(configInfo->jssConfigWithFullPath, configPath);

    strcpy(configInfo->tmpjssConfigWithFullPath, tmpConfigPath);
 

    /* open cfg file */

    if ((in = fopen(configPath, "rb")) == NULL) {

        jss_writeToARLog(AR_PLUGIN_LOG_SEVERE, "[80820] jss_getConfigData - Unable to open %s %d", CONFIGFILE, errno);

        return -1;

    }

    while (fgets(Buffer, sizeof(Buffer), in) != NULL) {

        /* skip leading spaces  */

        Command = Buffer;

        while (isspace(*Command))

            Command++;
 

        /* skip comment & blank lines */

        if (*Command == 0 || *Command == '#')

            continue;
 

        /* remove trailing whitespace */

        ptr = Buffer + strlen(Buffer) - 1;

        while (ptr > Buffer && isspace(*ptr))

            *ptr--;
 

        if (ptr <= Buffer)

            continue;
 

        ptr++;

        *ptr = 0;
 

        ptr = strchr(Command, ':');

        if (ptr == NULL) {

            jss_writeToARLog(AR_PLUGIN_LOG_FINEST,

                             "\tInvalid configuration line '%s'", Command);

            continue;

        }

        /* terminate command and find start of parameter */

        *ptr++ = 0;
 

        while (*ptr == ' ')

            ptr++;
 

        if (*ptr == 0)

            /* No trailing parameter  */

            continue;
 

        Parameter = ptr;

        /* remove trailing whitespace */

        ptr = Parameter + strlen(Parameter) - 1;

        while (ptr > Parameter && isspace(*ptr))

            *ptr--;

        ptr++;
 

        if (ptr <= Parameter) {

            jss_writeToARLog(AR_PLUGIN_LOG_FINEST,

                    "\tConfiguration file has no parameter for option '%s'",

                             Command);

            continue;

        }

        *ptr = 0;
 

        /* Command = the command name Parameter = the first parameter */
 

        /* jss-sso.cfg MidTier-Shared-Key

		 *

		 */

        if (strcmp(Command, "MidTier-Shared-Key") == 0) {

			configInfo->MidTierSharedKey = strdup(Parameter);
 

        } 
 

	     else if (strcmp(Command, "MT-IP") == 0) {

            configInfo->mtIP = strdup(Parameter);

        } 
 

       else if (strcmp(Command, "Revision") == 0) {

            configInfo->Revision = strdup(Parameter);

        }

		

		else if (strcmp(Command, "showPasswords") == 0) {

            if (Parameter[0] == 'T' || Parameter[0] == 't')

                configInfo->showPasswords = TRUE;

            else

                configInfo->showPasswords = FALSE;
 

        } else {

            jss_writeToARLog(AR_PLUGIN_LOG_SEVERE,

                             "\tjss_getConfigData %s - unknown setting '%s'",

                             CONFIGFILE, Command);

        }

    }
 

    /* Close the cfg file */

    if (fclose(in) == EOF) {

        jss_writeToARLog(AR_PLUGIN_LOG_SEVERE,

                         "[80823] jss_getConfigData - Unable to close %s %d",

                         CONFIGFILE, errno);

        return (1);

    }

    /* output the globalconf struct just to check the values */
 

    if (configInfo->newMidTierSharedKey != NULL) {

        jss_writeToARLog(AR_PLUGIN_LOG_CONFIG, confFormatStr,

                         "\tNew MidTier-Shared-Key", configInfo->newMidTierSharedKey);

    }
 

	/* ARUSER Win32 SSO Key */ 

    jss_writeToARLog(AR_PLUGIN_LOG_CONFIG, confFormatStr,

                     "\tARUSER-Shared-Key", configInfo->ARUSERSharedKey);
 

    jss_writeToARLog(AR_PLUGIN_LOG_CONFIG, confFormatStr,

                     "\tMidTier-Shared-Key", configInfo->MidTierSharedKey);
 

    if (configInfo->newARUSERSharedKey != NULL) {

        jss_writeToARLog(AR_PLUGIN_LOG_CONFIG, confFormatStr,

                         "\tNew ARUSER-Shared-Key", configInfo->newARUSERSharedKey);

    }

    

    jss_writeToARLog(AR_PLUGIN_LOG_FINEST, "- jss_getConfigData");
 

    return 0;

}

Open in new window

0
Comment
Question by:smurff
  • 6
  • 4
11 Comments
 
LVL 10

Expert Comment

by:peetm
ID: 22860898


        if (*Command == 0 || *Command == '#')

Do you mean to check for zero here?
 

        while (ptr > Buffer && isspace(*ptr))
            *ptr--;

*ptr--;   ??
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22860907
A signal 11 is a segmentation fault. It means that the code is trying to access memory that it isn't allowed to.

The line you indicate should not cause such a problem. I've gone over your parsing code, and it looks ok to me (except that you only treat spaces as whitespace, and not newlines, tabs, etc. - this is an issue since fgets leaves the newline character in the string).

How did you identify that line as the cause of the problem ?


Note that you sue several strcpy's and strcat's which are unsafe - ie. they might write past the end of the buffer. Consider using strncpy and strncat instead. That might be the cause of your problem. Is AR_MAX_FULL_FILENAME big enough to hold the data ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22860909
>> Do you mean to check for zero here?

It's a check for the trailing '\0' character. It might have been clearer to use '\0' instead of 0, but it's not incorrect.


>> *ptr--;   ??

The * is not needed - just ptr-- was enough. But it doesn't cause a problem either.
0
 
LVL 3

Author Comment

by:smurff
ID: 22861122
Wow cheers for the replies guys :) Happy days!

I found out by putting printf's in between everyline and the last one was just before and the one after was not printed.

#define AR_MAX_FULL_FILENAME 255

The output is this

+ jss_getConfigData
        Configuration Directory Found /opt/ars/cgitsmdev01/conf/
        Config File (cfg/conf) Found  /opt/ars/cgitsmdev01/conf/jss-sso.cfg
 1                              
 2                              
 1                              
 2                              
 3


Thanks again for your time guys
Danny

    while (fgets(Buffer, sizeof(Buffer), in) != NULL) {

    printf("1\n");

        /* skip leading spaces  */

        Command = Buffer;

        while (isspace(*Command))

            Command++;

		printf("2\n");

        /* skip comment & blank lines */

        if (*Command == 0 || *Command == '#')

            continue;

		printf("3\n");

        /* remove trailing whitespace */

        ptr = Buffer + strlen(Buffer) - 1;

		printf("4\n");

        while (ptr > Buffer && isspace(*ptr))

        {

		printf("5\n");

            *ptr--;

				}

        if (ptr <= Buffer)

            continue;
 

        ptr++;

        *ptr = 0;

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22861137
>> The output is this

And what was the input given For that output ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 53

Expert Comment

by:Infinity08
ID: 22861154
>> I found out by putting printf's in between everyline and the last one was just before and the one after was not printed.

It's better to run the code in a debugger and/or analyze the generated core file with a debugger. That will give you more information about what is going wrong.

Can you do that ? Start with the core file, and do a simple pstack on it :

        pstack core

(where core is the generated core file of course)
0
 
LVL 3

Author Comment

by:smurff
ID: 22861211
Hi again,

the code is in a .so that I have coded but the binary is from a vendor that I dont have the source code to. it did create a core and this is the output
Thanks again

bash-3.00# pstack core
core 'core' of 22670:   /opt/ars/cgitsmdev01/bin/arplugin -s cgitsmdev01 -i /opt/ars/cgitsmdev
 00103aa8 WriteLogFile (ffbfbb7c, ffbf7a7c, 76, 2441, ff00, 23) + 40
 00104048 PopulateTraceLogHeader (ffbfbb7c, ffbf7a7c, 0, 0, ff000000, 80808080) + 2d0
 00105230 CloseWorkflowTraceLog (1d5770, ffbfbff1, 7ffffc00, 154ee0, 0, 2) + f8
 000ff3b4 CleanupProcess (15, 19cedc, ffbfc160, 0, ff1c2000, ffbfc160) + c
 000ff5ac ShutdownProcess (1, ffbfbff1, 7ffffc00, 154ee0, 0, 2) + a4
 00154ff8 SignalTrapProc (b, 0, ffbfc160, 0, ff1c2000, ffbfc160) + 118
 ff340620 __sighndlr (b, 0, ffbfc160, 154ee0, 0, 1) + c
 ff335718 call_user_handler (b, 0, 0, 0, ff1c2000, ffbfc160) + 3b8
 ff2b0db0 strlen   (ffbff510, ffbff1e4, fe05b63b, 80808080, 0, 0) + 50
 ff318b48 vsprintf (ffbfd180, 7fffffff, 7ffffc00, fffffff8, ffbfd180, ffffdffc) + 30
 fe05190c jss_writeToARLog (2bc, ffbff508, fe05b638, 80808080, ff00, 80808080) + 8c
 fe053d68 jss_getConfigData (fe06d5e0, ffbffa74, fe06d5e0, fffffff8, 0, fe06da08) + 504
 fe051b04 ARPluginInitialization (5, ffbffa74, ffbff810, fffffffc, 0, 1d59a4) + ac
 00102dc4 Initialization (5, ffbffa74, 1, 9, 18c, feeaf76c) + 89c
 000778bc RPCmain  (5, ffbffa74, ff1c0540, 6, ff36c1dc, ff34fcf8) + 7c
 000fd9d4 main     (5, ffbffa74, ffbffa8c, 1af800, feec0880, feec08c0) + d4
 000756f0 _start   (0, 0, 0, 0, 0, 0) + 108
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 22861234
This indicates that the crash occurred inside jss_writeToARLog (called from jss_getConfigData). There is a vsprintf call in there that causes a segmentation fault because the passed string data did not contain a '\0' terminator.

The next step is to find out which of these jss_writeToARLog calls causes the problem. For that, you can run your code in a debugger.

Or if that's not feasible, go with your printf solution, but rather than printing to stdout, print to stderr, and make sure to fflush the stderr stream after every print.

        http://www.cplusplus.com/reference/clibrary/cstdio/fflush.html
0
 
LVL 3

Author Comment

by:smurff
ID: 22863294
Cheers infinity08

I have been doing some reading thanks. For some reason the printf's were not actually showing past a certain point but yes you were right it was another call by the jss_writeToARLog when the parameter was null. What is strange is that this code works fine on windows and red hat.

Can I be cheeky please? When you say
"(except that you only treat spaces as whitespace, and not newlines, tabs, etc. - this is an issue since fgets leaves the newline character in the string).
"

What would you change to allow for these then?

Thanks again for all your help
Kind regards
Danny
0
 
LVL 3

Author Closing Comment

by:smurff
ID: 31512461
Thanks again Infinity08
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22863316
>> For some reason the printf's were not actually showing past a certain point

That's because there is an output buffer, and at the moment of the crash, it could be that not the whole buffer has been written to the screen yet. The fflush I suggested earlier takes care of that by explicitly flushing the buffer.


>> What is strange is that this code works fine on windows and red hat.

Some implementations have protections against null. Others don't. These kind of protections are non-standard, and thus you shouldn't rely on them.


>> What would you change to allow for these then?

Take a look at isspace :

        http://www.cplusplus.com/reference/clibrary/cctype/isspace.html

It's used to check if a character is whitespace or not.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now