LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 11-05-2012, 03:42 PM   #1
abcde597
Member
 
Registered: Oct 2012
Location: Alabama, US
Distribution: Several Debian Based Distros
Posts: 57

Rep: Reputation: Disabled
Question I have a partially theoretical question.


Would it be possible to write a program that writes/changes its own source code?
I mean, virus writers can do it, can't we?
 
Old 11-05-2012, 04:08 PM   #2
hunternet93
Member
 
Registered: Jun 2009
Posts: 87

Rep: Reputation: 31
Yup. If the program knows where it's own source files are, it can edit them and recompile.
 
Old 11-05-2012, 04:48 PM   #3
abcde597
Member
 
Registered: Oct 2012
Location: Alabama, US
Distribution: Several Debian Based Distros
Posts: 57

Original Poster
Rep: Reputation: Disabled
Can you help me find source code showing this?
 
Old 11-05-2012, 10:45 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,425

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
Yes, you can compile a program if you have its source-code, but it's far more difficult to swap the image of a running process on-the-fly. Very often the executable file is locked to prevent such modification. Almost always, it's simply ignored if you try.

When a program is executing, its code-segment is almost always protected against modification, for a variety of reasons not just this one.

(And all of the foregoing comments are "not-at-all Linux-specific.")
 
Old 11-05-2012, 11:14 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,889

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
A famous problem: write a program that prints its source.
http://en.wikipedia.org/wiki/Quine_%28computing%29
 
Old 11-06-2012, 04:52 AM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,916

Rep: Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777Reputation: 777
Donald Knuth, in the Art of Computer Programming (one of the early parts, if I remember correctly) considers the problem of self-modifying code and is prepared to do it and so has an example. Of course, this is in assembler for the hypothetical MIX machine, but you should be able to extrapolate from there.
 
Old 11-06-2012, 07:47 AM   #7
hunternet93
Member
 
Registered: Jun 2009
Posts: 87

Rep: Reputation: 31
I think the program would just compile itself, start the modified copy, pass the copy whatever data/parameters it needs, and exit. I'm not a C programmer, though, so I can't be sure.
 
Old 11-06-2012, 05:13 PM   #8
abcde597
Member
 
Registered: Oct 2012
Location: Alabama, US
Distribution: Several Debian Based Distros
Posts: 57

Original Poster
Rep: Reputation: Disabled
Thanks, all!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Theoretical question: Install linux via serial port? linuxlicious Linux - General 10 12-05-2011 06:46 PM
INIT and the boot scripts, a theoretical question. stf92 Linux - Newbie 4 09-19-2011 06:09 AM
[SOLVED] wiping HDD using /dev/urandom versus /dev/zero, a theoretical question H_TeXMeX_H Linux - General 6 06-29-2009 07:55 AM
abi file in the /boot directory -- possibly theoretical question only mpts Ubuntu 0 09-04-2007 11:31 AM
Theoretical Question jgr220 Linux - Security 3 03-29-2003 06:40 AM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration