Is the buffer overflow still a problem? C ++/CLI Security Code

Source: Internet
Author: User

++/CLI is an extension of C ++. It applies to all types, including standard C ++ classes, added support for attributes, events, garbage collection, and generics.

Visual c ++ 2005 extends the Control and Application for developing and running on a virtual machine with garbage collection using C ++/CLI (general language infrastructure)ProgramWhile C ++/CLI is for C ++Programming LanguageAll types, including standard C ++ classes, are added with features such as attributes, events, garbage collection, and generics.

Visual c ++ 2005 supports the. NET Framework Universal Language Runtime Library (CLR), which is the implementation of the garbage collection Virtual Machine Microsoft. Visual c ++ 2005 pairs.. NET programming C ++ syntax is supported from Visual C ++. the managed extension C ++ introduced in net 2003 evolved, and managed extension C ++ is still supported, but it is no longer in favor of new syntax. Visual c ++ 2005 also adds new features to local programming, including 64-bit processor architecture support and new library functions that improve security.

In this article, we will mainly explain the problems faced when migrating existing old systems to the new environment using CLR at the minimum cost, the objective is to determine whether these programs are still vulnerable to the impact of buffer overflow of C/C ++ programs for many years.

Example 1 requires the user to enter the user name and password, except the user name, the program only accept "NCC-1701" as a valid password. If the user enters the wrong password, the program will exit. (This program is only used as C ++/CLICodeInstead of demonstrating how to handle passwords .) Example 1:

1. # include <stdlib. h>
2. # include <stdio. h>
3. # include <windows. h>
4. Char buff [1028];
5. struct user {
6. char * Name;
7. size_t Len;
8. Int uid;
9 .};
10. bool checkpassword (){
11. Char password [10];
12. Puts ("Enter 8 character password :");
13. Gets (password );
14. If (strcmp (password, "NCC-1701") = 0 ){
15. Return true;
16 .}
17. else {
18. Return false;
19 .}
20 .}
21. Int main (INT argc, char * argv []) {
22. struct user * userp = (struct user *) 0 xcdcdcd;
23. size_t usernamelen = 0 xdeadbeef;
24. userp = (struct user *) malloc (sizeof (User ));
25. Puts ("Enter User name :");
26. Gets (buff );
27. If (! Checkpassword ()){
28. usernamelen = strlen (buff) + 1;
29. userp-> Len = usernamelen;
30. userp-> name = (char *) malloc (usernamelen );
31. strcpy (userp-> name, buff); // log Failed Login Attempt
32. Exit (-1 );
33 .}
34 .}

The program starts to execute main () In line 21, and uses a pair of puts () and gets () in lines 25 and 26 to prompt the user name, as a result, an uncontrolled string replication from standard input to the buffer character array (declared in line 1) may result in a buffer overflow vulnerability in both parts of the program. The checkpassword () function is called by line 27 of main () and prompts the user to enter the password in lines 12 and 13. This also uses a pair of puts ()/gets (). The second call to gets () also results in a buffer overflow of the Password character array defined on the stack.

The program uses Microsoft Visual C ++ 2005 for compilation, closes the buffer security check option (/GS-), and opens the managed extension (/CLR ). By default, the buffer security check is enabled. disabling it is not a good practice (as shown in this example ), the/CLR option allows both hosted and unmanaged code to generate a hybrid assembly.
For more information, go

Http://dev.yesky.com/msdn/252/3045752_1.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.