The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 06-20-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
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
#
Reply With Quote
Forum Sponsor
  #9  
Old 06-21-2006
Registered User
 

Join Date: Jul 2004
Location: New York State
Posts: 65
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;
}
Reply With Quote
  #10  
Old 06-21-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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;  
    }
  }
And. Consider using regcomp() and friends when you want to test complex character classes in a long string. In this case the ctype.h tests are easy to implement and understand. Most other times they are a nightmare.
Reply With Quote
  #11  
Old 06-21-2006
Registered User
 

Join Date: Jul 2004
Location: New York State
Posts: 65
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0