Showing posts with label unsafe. Show all posts
Showing posts with label unsafe. Show all posts

Tuesday, April 27, 2010

Perl programming: system() cmd


Link ref:
String matching




Problem:

You need to use a user's input as part of a command, but you don't want to allow the user to make the shell run other commands or look at other files. If you just blindly call the system function or backticks on a single string containing a command line, the shell might be used to run the command. This would be unsafe.

Solution:
Unlike its single-argument version, the list form of the system function is safe from shell escapes. When the command's arguments involve user input from a form, never use this:

system("command $input @files"); # UNSAFE

Write it this way instead:

system("command", $input, @files); # safer

Thursday, March 25, 2010

Avoid using unsafe string functions




Avoid using unsafe string functions (UsageOfStringFunctions.rule)


Description

This rule detects code that uses unsafe string functions from C library.

Benefits:

Prevents the use of functions which may cause buffer overflows.

According to David A. Wheeler (see reference below), "C functions users must avoid using dangerous functions that do not check bounds unless they've ensured that the bounds will never get exceed.

Functions to avoid in most cases (or ensure protection) include the functions strcpy(), strcat(), sprintf() (with cousin vsprintf()), and gets().

These should be replaced with functions such as strncpy(), strncat(), snprintf(), fgets(), respectively."

Example:

#include
void main( void )
{
char* str1 = "testcase";
char* str2 = "testcase";
char* str3=0;

str3 = strcat( str1, str2 ); // Violation
}

Repair:

#include
void main( void )
{
char* str1 = "testcase";
char* str2 = "testcase";
char* str3=0;

str3 = strncat( str1, str2, 16 ); // OK
}

References:
http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/dangers-c.html

Author
ParaSoft


Tags: Avoid, unsafe, string, function, unsafe string, C library, buffer overflows, dangerous functions, strncpy, strncat, snprintf, fgets


Labels