LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2016, 11:24 AM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
command line args buffer overflow


I'm a novice c programmer and have read about buffer overflows in programs. This may sound like a dumb question but what's the prevent a buffer overflow from command line args? What prevents this if my assumption is incorrect?
 
Old 03-29-2016, 11:43 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by doughyi8u View Post
I'm a novice c programmer and have read about buffer overflows in programs. This may sound like a dumb question but what's the prevent a buffer overflow from command line args? What prevents this if my assumption is incorrect?
You "prevent" buffer overflow by not allowing them to happen. In other words; make d*mn sure that you don't allow things to be written where it should not.

But, if you're wondering if you can have too many arguments, as in:
Code:
int main (int argc, char *argv[])   //  command line arguments
the short answer is no. It all boils down to the programmer, as in what he/she does with the arguments.

Best regards,
HMW
 
Old 03-29-2016, 11:46 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Command line arguments are 'dealt with' by the time your program starts. Of course you can still do stupid things in your program, eg:
Code:
    char copy0buff [64];
    strcpy (copy0buff, argv[0]);
fix:
Code:
    char *copy0buff;
    copy0buff= strdup (argv[0]);
 
Old 03-29-2016, 12:32 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by doughyi8u View Post
I'm a novice c programmer and have read about buffer overflows in programs. This may sound like a dumb question but what's the prevent a buffer overflow from command line args? What prevents this if my assumption is incorrect?
Well, what is your assumption?

How do you think "a buffer overflow from command line args" would be done?

Last edited by dugan; 03-29-2016 at 12:36 PM.
 
Old 03-29-2016, 01:40 PM   #5
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
My assumption is that you could cause a buffer overflow by using command line args. Is that not possible?
 
Old 03-29-2016, 01:48 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
If you're talking about the command line arguments that your program receives, that is not space that your program allocates or has any control over. The space for your program's arguments and environment is set up by the execve() system call in the program that invokes yours, and a limit on the total size is enforced at that time.

From /usr/include/linux/limits.h:
Code:
#define ARG_MAX       131072    /* # bytes of args + environ for exec() */
Of course if your program modifies its argument strings, it is up to you not to exceed the space that they originally used.
 
1 members found this post helpful.
Old 03-29-2016, 02:08 PM   #7
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
thanks rknichols. that answered my question
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pipeline when the file requires two command line args???? lemon09 Programming 4 01-19-2010 09:24 PM
Cant pass command line args in flwm.desktop deep_inda_jungl Ubuntu 1 05-02-2008 03:05 PM
file contents to command line args hawkmauk Linux - General 3 11-21-2006 01:27 PM
C command line args in linux YankeeFan Linux - Newbie 5 06-11-2006 08:22 AM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration