Kill all process by UID


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Kill all process by UID
# 1  
Old 10-02-2011
Question Kill all process by UID

Is there any secure way to kill all processes with specified UID ?
Traditional way like

setuid(WANTED_UID);
kill(-1,SIGKILL);

is not secure, because this programm will receive signals between calling setuid and calling kill (so, any programm with WANTED_UID can kill this "killer-program", because we cannot catch SIGKILL from process we try to kill).
Scripts like ps axu|sed ...|xargs kill ... are not good, because programms with WANTED_UID can call fork() after calling "ps" and before calling "kill", so child process will not be killed.

Is there any secure way to kill processes by UID which guarantees dying of all processes with specified UID?

Last edited by DendyGamer; 10-02-2011 at 10:06 AM..
# 2  
Old 10-02-2011
"killall" command has an option to kill by username. For example:

Code:
killall -9 -u mysql

However, you need to do some extra works to convert the UID to username.
# 3  
Old 10-02-2011
Quote:
Originally Posted by MacMonster
"killall" command has an option to kill by username. For example:

Code:
 killall -9 -u mysql

However, you need to do some extra works to convert the UID to username.
Yes, but i do not know is it safe. I mean killall gets process list from kernel. While getting process list from kernel process_we_want_to_kill can call fork and some processes may be stay alive (will not be killed, because new process will be created after/during getting process list in kill). I have not read source code of killall (and there are no killall in OpenBSD), but is it safe ? Will it kill really all processes without any "bugs", or there are any "features" with killing new processes which starts during killing process?
# 4  
Old 10-02-2011
Quote:
Originally Posted by DendyGamer
Yes, but i do not know is it safe. I mean killall gets process list from kernel. While getting process list from kernel process_we_want_to_kill can call fork and some processes may be stay alive (will not be killed, because new process will be created after/during getting process list in kill). I have not read source code of killall (and there are no killall in OpenBSD), but is it safe ? Will it kill really all processes without any "bugs", or there are any "features" with killing new processes which starts during killing process?
Yes, you're right. Your mentioned situation may still exist as "killall" can't block the processes from forking. Smilie
# 5  
Old 10-02-2011
Quote:
Originally Posted by DendyGamer
Yes, but i do not know is it safe. I mean killall gets process list from kernel. While getting process list from kernel process_we_want_to_kill can call fork and some processes may be stay alive (will not be killed, because new process will be created after/during getting process list in kill). I have not read source code of killall (and there are no killall in OpenBSD), but is it safe ? Will it kill really all processes without any "bugs", or there are any "features" with killing new processes which starts during killing process?
What are you trying to accomplish or prevent?

As far as killing all processes with a given euid or ruid, for that OpenBSD provides pkill.

Yes, there's a race between the time the list of matching processes is retrieved (using the kernel virtual memory library's kvm_getprocs() interface) and the time pkill sends the processes a signal, during which a process can fork or change uid.

I suppose you can run pkill multiple times until it returns an exit status of 1 (no processes match the criteria).

Again, more info about the situation will probably help us help you.

Regards,
Alister

Last edited by alister; 10-02-2011 at 06:25 PM.. Reason: typo
# 6  
Old 10-02-2011
Quote:
Originally Posted by alister
What are you trying to accomplish or prevent?
I'm writting programm to test students programms (user sends source code via web, server compiles it, run it with some tests). I'm trying to prevent unkillable programms when user's programm forks a lot of times (forks will be limited by OS, but user limit of processes > 1), theoreticaly programm can fork in "bad moment" and can be unkillable. Starting pkill lots of time - it is not a beautiful solution (i can write kernel patch to deny fork() - it guarantees that all processes will be killed), i'm trying to find "beautiful" solution without patching kernel and it will be better if it will be cross-platform solution. Running pkill multiple times - practicaly normal solution, but for me it looks like Achilles' heel.
# 7  
Old 10-02-2011
Quote:
Originally Posted by DendyGamer
Is there any secure way to kill all processes with specified UID ?
Traditional way like

setuid(WANTED_UID);
kill(-1,SIGKILL);

is not secure, because this programm will receive signals between calling setuid and calling kill (so, any programm with WANTED_UID can kill this "killer-program", because we cannot catch SIGKILL from process we try to kill).
You haven't provided any information regarding the ruid, euid, and suid of killer-program nor of its victims. However, it's possible that the only reason that killer-program is vulnerable is because of the setuid() call you're using.

A process p cannot send a signal to a process q unless p's real uid or effective uid matches either q's real uid or saved set uid.

Assuming that killer-program is privileged and starts with ruid==euid==suid==0, setuid(WANTED_GUID) will set them all to WANTED_GUID.

Assuming that the victims are running with ruid==euid==suid==WANTED_GUID, the victims can now kill killer-program because killer-programs ruid and/or suid matches victims' ruid and/or euid.

However, if instead you only modified killer-program's credentials so that ruid==suid==0 and euid==WANTED_GUID, the victims could not kill killer-program, since the victims' ruid and/or euid does not match killer-program's ruid and/or suid.

In short, if the assumptions are correct, all you need is to use seteuid instead of setuid.

If the uid assumptions are incorrect, then please be more specific.

Regards,
Alister

Last edited by alister; 10-02-2011 at 06:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

2. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

3. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

4. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

5. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

6. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

7. UNIX for Dummies Questions & Answers

UID & GID of the running process

Hi, out of curosity this question just popped in my mind. Is there any way to find out the uid and gid of the running process ? If i do a ls -l of a program then it shows the uid/gid bit (if its set). I want to see as which user/group the program is running ..... is there any way to know this... (2 Replies)
Discussion started by: ankurjain
2 Replies

8. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

9. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

10. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies
Login or Register to Ask a Question