LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 12-11-2007, 03:25 PM   #1
SimoneB
LQ Newbie
 
Registered: Dec 2007
Location: Rome, Italy
Distribution: Gentoo
Posts: 2

Rep: Reputation: 0
Question Problem reading procfs


Hi, I'm developing an application that access the procfs and then
read/write /proc/<PID>/mem of a process to change the content of a file
owned by that process.

The sequential steps performed by the application are the followings:

1) catch, by using ptrace, of the "read" system call
2) read the file content directly on /proc/PID/mem
3) modify this content
4) write back the modified content on /proc/PD/mem

The problem is that it returns "Error 22 Invalid argument" every time
it tries to read (or write) on procfs.
(I've already patched the kernel to allow read/write operation on
/proc/<pid>/mem and I've tried the pread/pwrite functions, but they
have the same behavior).

This is an extract of my code:

Quote:
...
int eax_reg, ecx_reg, ret;
char * buffer;
char orig_buf[eax_reg+1];
char proc_path[MAX_FILE_NAME+1];

// traced_proc is the pid_t of the traced process
// get number of bytes to read
eax_reg = ptrace(PTRACE_PEEKUSER, traced_proc, 4*EAX, NULL);
// get traced process space buffer
ecx_reg = ptrace(PTRACE_PEEKUSER, traced_proc, 4*ECX, NULL);

buffer = malloc(sizeof(char)*eax_reg);
memset(orig_buf,0,eax_reg+1);
memset(proc_path, 0, sizeof(proc_path));

sprintf(proc_path, "/proc/%d/mem", traced_proc);
fd_proc = open(proc_path, O_RDWR);

lseek(fd_proc, ecx_reg, SEEK_SET);
int ret = read(fd_proc, buffer, eax_reg);
if (ret < 0) {
perror("read");
fprintf(stderr, "Failed to read: %d - %s\n", errno, strerror(errno));
}
...
if I run the application with strace, the output for the code shown above is:

Quote:
open("/proc/13271/mem", O_RDWR) = 3
lseek(3, 3086368768, SEEK_SET) = 3086368768
read(3, 0xbfca0940, 280) = -1 EINVAL (Invalid argument)
Can someone tell me where I'm wrong?
Thanks in advance for any help...
 
Old 12-11-2007, 04:28 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,285
Blog Entries: 1

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by SimoneB View Post
Code:
char orig_buf[eax_reg+1];
At this point, eax_reg is unintialized. Compile with "-Wall -Werror" to convert warnings about this into errors.

Quote:
Originally Posted by SimoneB View Post
Code:
eax_reg = ptrace(PTRACE_PEEKUSER, traced_proc, 4*EAX, NULL);
You initialize eax_reg here to a different value than whatever random garbage it originally had.

Quote:
Originally Posted by SimoneB View Post
Code:
memset(orig_buf,0,eax_reg+1);
You probably trash your stack here. You can easily check this by printing out the value of "buffer" before and after this call.
 
Old 12-12-2007, 01:55 AM   #3
SimoneB
LQ Newbie
 
Registered: Dec 2007
Location: Rome, Italy
Distribution: Gentoo
Posts: 2

Original Poster
Rep: Reputation: 0
First of all, thank you for your reply.

eax_reg is initialized to 0 before the "char orig_buf[eax_reg+1];" line (the code that I reported here is only an extract of the actual application, sorry for the misunderstanding...).
 
Old 12-12-2007, 07:56 AM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,285
Blog Entries: 1

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by SimoneB View Post
eax_reg is initialized to 0 before the "char orig_buf[eax_reg+1];" line (the code that I reported here is only an extract of the actual application, sorry for the misunderstanding...).
You are still probably smashing your stack variables with the memset, because the value of eax_reg is non-zero in the memset.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Monitoring procfs krizzz Programming 9 05-10-2007 06:29 PM
what is and why use PROCFS donoh Programming 1 05-30-2005 01:31 AM
HostAP and procfs shuuhen Linux - Wireless Networking 1 04-11-2005 01:22 AM
about procfs serji Linux - Software 1 12-04-2003 11:43 PM
procfs nullpt *BSD 1 10-25-2003 03:30 PM


All times are GMT -5. The time now is 02:49 PM.

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
Open Source Consulting | Domain Registration