Question

In: Computer Science

Working with strings can lead to various security flaws and errors in software development using the...

Working with strings can lead to various security flaws and errors in software development using the C++ language. What are the common string manipulation errors that can be encountered? How can these errors be resolved and/or limited? What tips can be utilized to identify security vulnerabilities related to strings in C++? Be sure to provide an appropriate source code example to illustrate your points.

Solutions

Expert Solution

Working with strings in C++ may lead to manipulation errors and needs to be resolved. Some of the common errors include :

  • String Truncation errors - String truncation may occur while reading user input or copying a string when a destination character array is not large enough to hold the contents of a string. The following code, for example, will truncate user input exceeding 11 characters.
     #include <iostream.h>
     int main() {
     char buff[12]; 
     cin.width(12); 
     cin >> buff; 
     cout << "echo: " << buff << endl; 
     }
    In some cases, String Truncation errors may lead to loss of data, or even software vulnerabilities too.
    
  • String Errors without invoking a string function - Strings in C++ are character arrays, so it is possible to perform an insecure string operation without invoking functions that are highly susceptible to error, like strcpy(), streadd(), strecpy() strcat(), gets(), and strtrns(). Consider the following code, for example , accepts a string argument, copies it to the buff character array, and prints the contents of the buff. If the first argument to the program equals or exceeds 128 characters (remember the trailing null character), the program writes outside the bounds of the fixed-size array.
     int main(int argc, char *argv[]) {
        int j = 0;
        char buff[128];
        char *arg1 = argv[1];
        while (arg1[j] != '\0' ) {
        buff[j] = arg1[j]; 
       i++;
        }
        buff[j] = '\0'; 
       printf("buff = %s\n", buff); 
     }
  • Errors of Null-termination - Consider the following code, for example , the static declarations for the three character arrays (x[], y[], and z[]) fail to allocate storage for the null-termination character. As a result, the strcpy() to x writes a null character beyond the end of the array. Depending on how the compiler allocates storage, this null byte may be overwritten by the strcpy() on . If this occurs, x now points to an array of 20 characters, while y points to an array of 10 characters. The strcpy() to z fills z, causing the strcat() on it to write well beyond the bounds of the array (particularly because the terminating null byte for y is overwritten by the strcpy() to z).
     int main(int argc, char* argv[]) {
       char x[16];
       char y[16];
       char z[32];
       strcpy(x, "0123456789abcdef");
       strcpy(y, "0123456789abcdef");
       strcpy(z, x);
       strcat(z, y);
        printf("x = %s\n", x); 
       return 0; 
     }
  • Unbound String copies - Unbounded string copies occur when data is copied from an unbounded source to a fixed length character array (for example, when reading from standard input into a fixed length buffer). For example, the code below reads characters from standard input using the gets() function into a fixed-length character array until a newline character is read or an end-of-file condition is encountered.
     void main(void) {
     char Pass[80]; 
     puts("Enter 8 character password:"); 
      gets(Pass);
     }
  • String Vulnerabilities - Let us have a look at the Get password code section( that shows how strings are misused that pose security threats).
     bool IsPasswordOk(void) {
     char Pass[12];
     gets(Pass); 
    if (!strcmp(Pass, "good password")) 
     return(true); 
    else return(false);
      }
      void main(void) {
     bool PswrdStatus; 
     puts("Enter password:"); 
     PswrdStatus = IsPasswordOk(); 
     if (PswrdStatus == false) { 
     puts("Access denied"); 
     exit(-1); 
     } 
     else puts("Access granted"); 
     }
    The get password program starts in the main() routine. The first line executed is the puts() call that prints out a string literal . The puts() function is defined in C++ as a character i/o function, is declared in stdio.h, and writes a string to the input stream pointed to by stdin followed by a newline character ('\n'). The IsPasswordOk() function is called to retrieve a password from the user . The function returns a boolean value: true if the password is valid, false if it is not. The value of PswrdStatus is tested and access is allowed or denied.The IsPasswordOk() function uses the gets() function to read characters from the input stream (pointed to by stdin) into the array pointed to by Password until an EOF is encountered or a newline character is read. Any newline character is discarded, and a null character is written immediately after the last character read into the array. The strcmp() function defined in string.h compares the string pointed to by Password to the string literal "good password" and returns an integer value of zero if the strings are equal and a nonzero integer value if they are not. The IsPasswordOk() function returns true if the password is "good password" and the main() function consequently grants access.
    

Related Solutions

Building security into the design phase of the software development lifecycle (SDLC) is important to be...
Building security into the design phase of the software development lifecycle (SDLC) is important to be successful in securing software. In this forum, you will research the best secure software design methodologies to prevent vulnerabilities and share your findings with the class. While a security method may work well for one organization, the same approach may not work well for other organizations. However, there should be some best practices we should follow to be successful in the SDLC phases Considering...
What factors can lead to the development of a goiter?
What factors can lead to the development of a goiter?What are treatmen options for Hypo and Hyperthyroidism?What are treatment options for Cushings vs Addisons?
You are the lead trainer for the software development team at a large telecommunications company. You...
You are the lead trainer for the software development team at a large telecommunications company. You have been tasked with preparing a training document that explains the principles of polymorphism, inheritance, and encapsulation. Research these principles and provide examples for each principle, showing how they would be used in software development. Be sure to answer the question of how each principle would be employed in the software development process. Java programmers use class hierarchies for the purposes of inheritance. For...
As the CISO, you are responsible for development and implementation of various security policies to ensure...
As the CISO, you are responsible for development and implementation of various security policies to ensure the protection of company sensitive information and systems. There are different levels of policy from executive level to issue specific and system level policies. Discuss how the program policy leads to the other types of policies. Provide at least two examples of issue specific policies and two system specific (codified) policies. Include at least one research reference and associated in-text citation using APA standards....
What are three software design and development factors which contribute to system errors and failures? What...
What are three software design and development factors which contribute to system errors and failures? What are three management and use problems which contribute to system errors and failures?
Scenario You are the manager of a software development team working on new applications for your...
Scenario You are the manager of a software development team working on new applications for your company, Optimum Way Development, Inc. Your director has called for all development teams to submit product briefs detailing their current projects. The director plans to share the most promising product briefs with clients at an upcoming meeting. You have software design documents for two potential projects. Directions You must choose one of the potential products and use the information contained in the technical specification...
how the MYOB software can contribute to the various kinds of business management decision-making through using...
how the MYOB software can contribute to the various kinds of business management decision-making through using some of the specific special-purpose-reports generated by its software.
explain the security,legal and ethical issues that may arise from development and use of computer software?
explain the security,legal and ethical issues that may arise from development and use of computer software?
It has been argued that training can lead to turnover but career development can reduce it....
It has been argued that training can lead to turnover but career development can reduce it. Differentiate between training and career development. Why might training lead to turnover while career development might improve retention? Explain
Describe a mechanism that can lead to the development of antibiotic-resistant bacterial strains?
Describe a mechanism that can lead to the development of antibiotic-resistant bacterial strains?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT