![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| "unexpected end of file" when Iīm use EOF inside block if | ricardo.ludwig | Shell Programming and Scripting | 4 | 03-28-2008 12:45 PM |
| Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" | Lokesha | UNIX for Dummies Questions & Answers | 4 | 12-19-2007 10:52 PM |
| check input = "empty" and "numeric" | geoffry | Shell Programming and Scripting | 6 | 12-13-2007 02:12 AM |
| a weird issue with "while" block | sleepy_11 | Shell Programming and Scripting | 7 | 08-06-2007 08:33 PM |
| Maximum input file size in "Diff" Command | Neeraja | UNIX for Dummies Questions & Answers | 1 | 01-17-2007 07:09 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#8
|
|||
|
|||
|
It's a bad sign when your programming strategy shows up on The Daily WTF. But then, half of the important things I learned, I learned on TDWTF.
How about escaping things instead? Code:
// bashescape.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int safe_system(const char *strin)
{
int m,pos=0;
char bufout[512];
for(m=0; (pos<511)&&(strin[m] != '\0'); m++)
{
char c=strin[m];
if(!(isalnum(c) || isspace(c)))
{
bufout[pos++]='\\';
if(pos >= 510) break;
}
bufout[pos++]=c;
}
fprintf(stderr,"system(\"%s\")\n",bufout);
return(system(bufout));
}
int main(int argc, char *argv[])
{
return(safe_system(argv[1]));
}
Code:
# cc bashescape.c -o bashescape
# ./bashescape "echo hello ; world"
system("echo hello \; world")
hello ; world
#
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
Jim,
I really like the way you handled my problem. After reading your code, I reliezed that what I needed to do was to not only check for semi-colons, but to only allow A-Z, 0-9, and space. Since this is what your code did, I rewrote my routine and would like you to throw rocks at it. I don't want to output any errors, only return to the calling routine. Code:
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
int sysrun(char *command) {
int num;
int m,pos;
char str[80];
char process[39] = "/gers/test/adhoc/syscr/wpleca2unix.sh ";
num=0;
for(m=0; (m<36)&&(command[m] != '\0'); m++)
{
char c=command[m];
if(!(isalnum(c) || isspace(c)))
{
return 0;
}
}
strcpy(str,process);
strncat(str,command,35);
num = system(str);
return num;
}
|
|
#10
|
|||
|
|||
|
Assuming the arguments are never more than 34 chars long then that will work.
Corona actually gave a better solution - ie., let wpleca2unix.sh fend for itself. What if that code is invoked by some other means than your program - i.e., another programmer decides to let it run on it's own? In general, you should not depend on security with only one secure code layer. IMO. FWIW: Code:
for(m=0; command[m]; m++) /* check the whole thing */
{
if(!(isalnum(command[m]) || isspace(command[m])))
{
return 0;
}
}
|
|
#11
|
|||
|
|||
|
Because I use the strncat function to append the command onto the script invokation and I have a maximum length of 35 (or the actual length of command if less then 35), it should work fine. The wpleca2unix.sh script can only be accessed through the oracle database external procedure, which can only be accessed via the C library that I am building.
Thank you for the review, I appreciate it. |
|||
| Google The UNIX and Linux Forums |