LinuxQuestions.org
Help answer threads with 0 replies.
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 10-06-2003, 09:12 PM   #1
darkseed2g3
Member
 
Registered: Sep 2003
Location: Philadelphia ,Pa
Distribution: Fedora Core 1 BABY !!! YEA
Posts: 67

Rep: Reputation: 19
memory address and c for linux


Basicly this is what i wanna do

lets say i have this code


main (int x )
{

x = 1024;

printf("%p is the memory addy " , &x);

}

of course its going to print the memory address is there any way i can change the value of it
like is there a way i can write an other program and run them both at the same time like when i run the 1st program it puts the variable x into memory with the value of x as 1024 but when i run the second one it will overide the value of x to something else basicly a buffer exploit i guess
 
Old 10-06-2003, 09:20 PM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
im still getting the hang of C (as can be seen from previous posts), but the address spaces for the variables will be allocated and freed once the code block is completed. so once u ran this program, by the end of it, it would have freed up the memory (as it is a local var)?
i would use pointers and threads for this - to pass the location of the var u got here to the other code block which runs as the thread...

thats my ...
 
Old 10-06-2003, 09:30 PM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
I am not sure I understanded your meaning exactly, if so please excuse me.
Code:
#include<iostream>
using namespace std;
int main ()
{
   int x=1024;
   cout<<"x="<<x<<endl;
   cout<<"&x="<<&x<<endl;
   int* p=&x;
   cout<<"p="<<p<<endl;
   cout<<"*p="<<*p<<endl;
   *p=5;
   cout<<"p="<<p<<endl;
   cout<<"x="<<x<<endl;
}
[root@localhost linux_question]# g++ pa.cpp -o pa
[root@localhost linux_question]# ./pa
x=1024
&x=0xbfffdc94
p=0xbfffdc94
*p=1024
p=0xbfffdc94
x=5
[root@localhost linux_question]#
 
Old 10-06-2003, 09:33 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
i thought he wanted to run 2 different processes/programs - one operating on the other? no?
 
Old 10-06-2003, 09:38 PM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
perhaps the standard c system() function can do this.

edit: sorry for my mistake.

Last edited by Xiangbuilder; 10-07-2003 at 12:45 AM.
 
Old 10-06-2003, 10:18 PM   #6
zju_zhangxm
LQ Newbie
 
Registered: Aug 2003
Location: Hangzhou zhejiang, China
Distribution: RedHat
Posts: 7

Rep: Reputation: 0
I think there is no way to do that.

Every process has his own stack memory, and of course they are protected by Kernel. So the other process can not "see" this process's stack.

Only kernel module( kernel program) can "see" this stack memory. But indeed it does not know exactly which is "x" value.
 
Old 10-07-2003, 12:23 AM   #7
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
No, you cannot do that. Simple reason, because both the program will have different address space and hence cannot interfere with each others address space. The address which will be printed on the screen will be the logical address and not the actual physical address. Use IPC if you want to setup communication between the two programs.
 
Old 10-07-2003, 07:20 AM   #8
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Yes, as it have been mentioned above, every process has its own memory address space, so it cannot simply change address space which belongs to other process. The case for U is some kind of IPC. The best in this case is shared memory.
See man shm_open, mmap, ... for POSIX standard IPC,
man shmget, ... for UNIX98 IPC.
 
Old 10-07-2003, 11:52 AM   #9
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
actually....

You're kindof right....I'm going to take a stab at explaining this in detail (please excuse errors)

In the world of execution, there are two major modes of operation under the x86 architecture: Real mode and Protected Mode. They each deal with the way memory becomes segmented for the execution of the program. In the case of protected mode (the "right" mode as far as OS developers are concerned), when a piece of code tries to access new memory, it throws the appropriate Page Fault exception which causes the kernel to look at the process and handle the exception properly. IE: a malloc() will throw a request for new pages page fault exception and the kernel will either return a new block of memory, or a NULL if no memory is available.
In protected, a piece of code entering another process' address space throws a Segmentation Page Fault exception. This is why that process dies with a SYSSEGV error code. Under this system, what he's trying to accomplish will never happen.

Under real mode, however, the system doesn't use a paged approach to memory (which is why there is a significantly smaller amount of memory available to the process running) and a program can fairly easily encraoch on another's address space (corewars without an emulator ). This is why DOS had some crazy issues with multi-tasking programs. Sometimes, even, a process under real mode would accidentally overwrite the kernel routines and cause the OS to do funky things. Under that system declaring a pointer to memory like char *p = (char *)<address> would surely get you access to any arbitrary point in memory.

Even under protected mode, if you're executing in kernel space, you can access raw points of memory (how do you think the video driver works?? char *vid = (char *)0xB80000; for vga and char *vid = 0x080000; for monochrome ) but doing such things is risky and should be avoided at all costs.

This is, I believe, the information relevant for the x86 platform. This all translates into, under a proper OS, you can't accomplish this.

Aaron
 
Old 10-07-2003, 04:30 PM   #10
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
nice explanation
 
Old 10-09-2003, 09:21 PM   #11
darkseed2g3
Member
 
Registered: Sep 2003
Location: Philadelphia ,Pa
Distribution: Fedora Core 1 BABY !!! YEA
Posts: 67

Original Poster
Rep: Reputation: 19
basiclly, i was talking about a buffer overrun
when you have a buffer defined for a program i was trying to overrun the buffer and have it point to another instruction

i was just wondering how to do it easier or if anyone has anything that would explian it better . I have read phrack 49 - smashing the stack but i need more. you can all understand that right.
 
Old 10-09-2003, 09:50 PM   #12
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Well, you probebly not going to understand smashing the stack unless you are familiar with assembly language.

Do you want me to post a sample program that executes a buffer overrun?
 
Old 10-09-2003, 10:57 PM   #13
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
"Do you want me to post a sample program that executes a buffer overrun?"
I want to learn from you.
Thank you.
 
Old 10-10-2003, 03:25 PM   #14
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
i would like to see some code that does it too, so if you could post it, it owuld be great. umm, it wouldnt be some code that does a gets() in a loop. would it?
 
Old 10-10-2003, 03:57 PM   #15
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Ill post a simple buffer overrun program when i get home,
about 5:15 CDT
 
  


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
36 bit memory address Schmidt Programming 2 03-03-2005 01:45 AM
get the actual hardware memory address of kernel oops edman007 Linux - General 0 02-24-2005 10:26 PM
assigning memory address to pointer irfanhab Programming 4 04-24-2004 03:35 AM
ni52 can't find memory, but address correct Wim_Woittiez Linux - Hardware 0 12-01-2003 01:14 AM
Help!?! RH 8 Memory Mapping -High Memory-Virtural Memory issues.. Merlin53 Linux - Hardware 2 06-18-2003 04:48 PM

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

All times are GMT -5. The time now is 02:36 AM.

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