26288 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
SoftBank gives
Washington veto
over Sprint board
job
STROKE this mouse
to make apps POP,
says Microsoft
Oz shared services
collapse looks bad
for NetApp
Googlerola loses
bid to ban US Xbox
sales after ITC
slapdown
Samsung, carriers
tout first Tizen
mobes for late 2013
Google to double
encryption key
lengths for SSL
certs by year"s end
Facebook Home phone
plans canned in the
UK
Joyent cuts prices
on cloudy
infrastructure
Yahoo! continues
quest for youth
with yet another
acquisition
Internet2 superfast
boffin network
peers with Azure
cloud
Slashdot
Australian Police
Move To Make 3D
Printed Guns
Illegal
Cockroaches
Evolving To Avoid
Roach Motels
Meet the 23-Ton
X-Wing, the World"s
Largest Lego Model
Android Malware
Intercepts Text
Messages, Forwards
To Criminals
Scientists Growing
New Crystals To
Make LED Lights
Better
Google Takes Street
View To the
Galapagos Islands
Bitcoin"s Success
With Investors
Alienates Earliest
Adopters
WIPO Panel Says Ron
Paul Guilty of
Reverse Domain Name
Hijacking
Red Hat"s Diane
Mueller Talks About
OpenShift (Video)
5-Pound UAV Flies
For 50 Minutes,
Streams HD From
Over 3 Miles
Article viewer

Secure coding 2



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:C
Search OSI about C.More articles by dimport.
 viewed 8278 times send this article printer friendly

Digg this!
    Rate this article :
Continuation of the Secure coding article that was published earlier.

system() vs exec()It is always more secure to use the exec command insted of the system call, becuse system spawns a shell that executes the whole string while exec only executes the first argument. Lets look at some source code:

int main(int argc, char *argv[]) {
 
  char buffer[30];
 

 
  snprintf(buffer, sizeof(buffer), "cat %s", argv[1]);
 
  system(buffer); }
 


Now, think about what would happen if they input "/etc/hosts ; rm -rf /etc/passwd" as argv[1] that would fuck things up a little right ? lets look at this:

...
 
execl("cat", "cat", argv[1], NULL);
 


Now whatever string we get it end up as an argument to cat, handy.Check the PATH/IFSConsider the code from the above example with a modified PATH and a modified cat, that could get kinda messy. So therefore you should always check the path before doing anything.The IFS is not very much better, here take a look at this example:

sol# cat > /tmp/bin
 
#!/bin/sh
 

 
IFS=" "
 
export IFS
 
echo "gotcha! i'm actually in /tmp"
 

 
sol# IFS="/"
 
sol# export IFS
 
sol# /bin/csh
 
gotcha! i'm actually in /tmp
 
sol#
 


I think you see the danger now.Keep it randomIf your program ever generates something random that will be used for something important, make sure it's really random and that you use a really good seed. Most def. random generations isn't really random numbers so, be carefull and BE paranoid it's never wrong ;)A great article of random number can be found

here

KEEP THE SETUID AWAYAnd if you really have to make sure you run as little code as necessary in setuid mode.

This article was originally written by shab

Did you like this article? There are hundreds more.

Comments:
nomzz
2009-06-06 10:21:22
Thx for providing all that!
u may get some information from hdi certification
That data may help many people!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006