Timed action after fork() in parent process


 
Thread Tools Search this Thread
Top Forums Programming Timed action after fork() in parent process
# 1  
Old 04-05-2011
Timed action after fork() in parent process

Assume you have such a piece of (more or less pseudo-)code:

Code:
if(fork() == 0) {// childprocess
     chmod(someProgram, 00777);
     exec(someProgram);
} else { // assume it never fails and this is the parent
     chmod(someProgram, 00000); // should be executed as soon as possible after the exec call of the child
}

What do I want to achieve: Assume a non executable program someProgram. I want to execute this program in such a way, that it gets executable rights, execute it in a child process via fork, and then take away the executable rights as soon as possible after the exec call.
Is there any way to do this?
# 2  
Old 04-05-2011
Why would you want to do this? First-off, you could always have the program itself remove its own executable flag (see readlink(2) and /proc/self/exe). Secondly, a program doesn't need to have the executable flag set to be executed (on a Linux system, anyway) since you can always invoke the interpreter directly:

Code:
[john]$ echo 'int main(){printf("hi\n");}' | gcc -x c -o program -
<stdin>: In function ‘main':
<stdin>:1: warning: incompatible implicit declaration of built-in function ‘printf'

[john]$ ls
total 8.0K
-rwxr-xr-x 1 john john 7.0K 2011-04-05 10:47 program*

[john]$ ./program 
hi

[john]$ chmod -x program 

[john]$ ./program
bash: ./program: Permission denied

[john]$ readelf -a program | grep interpreter
      [Requesting program interpreter: /lib/ld-linux.so.2]

[john]$ /lib/ld-linux.so.2 ./program 
hi

# 3  
Old 04-05-2011
Well, looks like I learned more than I intended to... didn't know that this was possible for binaries either... Anyways: If you take away the read flag, this way won't work anymore:
Code:
[chris@myhost Test]$ ./hw
Hallo welt
[chris@myhost Test]$ ls -l hw
-rwxr-xr-x 1 chris users 5432 Apr  1 14:44 hw
[chris@myhost Test]$ chmod -x hw
[chris@myhost Test]$ ./hw
bash: ./hw: Permission denied
[chris@myhost Test]$ /lib/ld-linux.so.2 ./hw
Hallo welt
[chris@myhost Test]$ chmod -r hw
[chris@myhost Test]$ /lib/ld-linux.so.2 ./hw
./hw: error while loading shared libraries: ./hw: cannot open shared object file: Permission denied
[chris@myhost Test]$ chmod +x hw
[chris@myhost Test]$ ./hw
Hallo welt
[chris@myhost Test]$ /lib/ld-linux.so.2 ./hw
./hw: error while loading shared libraries: ./hw: cannot open shared object file: Permission denied


My intention is to make one program available only over one call within another program. So I would take away its read and exec permissions, so it cannot be executed from anywhere else (assume chmod isn't available to the user). Just with my call as above. The program is obvious: If I wait in the parent process until the child exits there is a time gap in which the permissions are not so strict as I actually want them to be...
# 4  
Old 04-05-2011
Quote:
Originally Posted by disaster
My intention is to make one program available only over one call within another program.
Ah, sounds like what you're best off doing is creating a new group (e.g. 'proggrp') and making the calling program be owned by proggrp and setgid, and then making the called program owned by proggrp with the executable bit set only for the group. That way the user won't be able to chmod it (unless they own it - best make it owned by root, or use this scheme with a special user and setuid instead) and there will be no "gap" during which they can exploit the program's "executableness".

Edit: Also, by doing it this way you give control over who can run what to the system administrator (where it should be) as opposed to the programmer (who might not anticipate a need for more flexibility somewhere down the line...).
# 5  
Old 04-05-2011
By removing the executable bit from the executable you may end up removing the executable bit from the loaded executable code in memory!

Why not just delete the executable instead of chmod? It'll stay resident in your program but nothing else will be able to load it.
# 6  
Old 04-05-2011
Bug

Quote:
Originally Posted by disaster
Assume you have such a piece of (more or less pseudo-)code:

Code:
if(fork() == 0) {// childprocess
     chmod(someProgram, 00777);
     exec(someProgram);
} else { // assume it never fails and this is the parent
     chmod(someProgram, 00000); // should be executed as soon as possible after the exec call of the child
}

What do I want to achieve: Assume a non executable program someProgram. I want to execute this program in such a way, that it gets executable rights, execute it in a child process via fork, and then take away the executable rights as soon as possible after the exec call.
Is there any way to do this?
If you want the child process to execute first, the parent process should wait() for the child before changing the permission like this ->

Code:
switch(pid = fork())
{
case -1: perror("fork error");
break;

case 0 : chmod(); //do the process with the file
break;

default :wait(); //wait for the child to finish. claim child's              // resources
             chmod() //change back the permission

break;
}
return 0;
}

But this has a security hole as when the child is executing, another malicious process may try to modify the file so you can employ a locking mechanism so that its only accessed by the particular process i.e the child .

Thanks,
Gaurav.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One parent, multiple children pipe with fork()

The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print". The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that... (2 Replies)
Discussion started by: Ildiko
2 Replies

2. UNIX for Dummies Questions & Answers

Timed loop to scan for a process

Hi all, Looking for some help, I have written a very simple script to pass to PowerHA to act as an indicator to activate failover required. Where i get completely lost is I have to create a wait and carry out the grep for the process once every 10 seconds this works but need to embed this... (1 Reply)
Discussion started by: Gary Hay
1 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

5. Programming

parent called more times - fork - C language

Hi gurus can you explain following lines of code ? #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: ... (7 Replies)
Discussion started by: wakatana
7 Replies

6. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. UNIX for Advanced & Expert Users

Fork() 1 Parent 3 Children

Hi, as I understand fork(), it makes a copy of the parent which becomes a child. But is there anyway to make three children for that one parent. So in other words, if I look up the getppid() of the children, I want them to have the same value?? Thanks in advance to any help! (1 Reply)
Discussion started by: MS_CC
1 Replies

9. Programming

Problems with child comunicating with parent on fork()

Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks. The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing. 1)create a... (8 Replies)
Discussion started by: Zarnick
8 Replies

10. Shell Programming and Scripting

how to find the chid process id from given parent process id

how to find the chid process id from given parent process id.... (the chid process doesnot have sub processes inturn) (3 Replies)
Discussion started by: guhas
3 Replies
Login or Register to Ask a Question