0

I am trying to execute a bash script in the user space using the call_usermodehelper function like this.

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */                                
#include <linux/init.h>         /* Needed for the macros */
#include<linux/kmod.h>
#include <linux/moduleparam.h> 
   
MODULE_LICENSE( "GPL" );
   
static char *mystring = "blah"; 
   
module_param(mystring, charp, 0000); 
MODULE_PARM_DESC(mystring, "A character string");

static int __init hello_start(void) {
    printk(KERN_INFO "Loading rooted module...\n");
    pr_info("mystring is a string: %s\n", mystring); 
    char* argv[] = {"/bin/bash", "home/grim-reaper/kernel/FocusModule/focusmyman.sh", NULL};
    static char* envp[] = { "HOME=/",  "TERM=linux",   "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
    call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
    return 0;
}

static void __exit hello_end(void){
    printk(KERN_INFO "exit.\n");
    char* argv[] = {"/bin/bash", "home/grim-reaper/script-remove.sh", NULL};

    static char* envp[] = { "HOME=/",  "TERM=linux",   "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
    call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
}

module_init(hello_start);
module_exit(hello_end);

The shell script is as follows:

#!/bin/bash

if [ -f ~/kernel/FocusModule/focus.txt ]; then
    echo "File exists and is a regular file"
else
    touch focus.txt
fi


while true; do
    active_window=$(xdotool getwindowfocus getwindowname)
    
    echo "$active_window" >> ~/kernel/FocusModule/focus.txt    
    sleep 5
done

This script works perfectly when executed in the terminal, but it simply doesn't start execution when called from the module. The module compilation and dmesg show no errors. I'm at a loss at what's wrong. Please help. I've used an identical module for the execution of another script, which has just a chmod command, and it works perfectly.

  • I would make the path to `home/grim-reaper/kernel/FocusModule/focusmyman.sh` a full path starting with `/`. – GoinOff Apr 24 '23 at 22:00
  • Yeah I had tried that and path starting with ~/ but to no avail. – Aniket Raj Apr 24 '23 at 22:02
  • 1
    What about trying `sh -c "home/grim-reaper/kernel/FocusModule/focusmyman.sh` from the module to see if it works? `sh -c` should spawn a new shell to run in. Maybe ENV problem?? – GoinOff Apr 24 '23 at 22:07
  • Here is an example using `sh -c`: https://tech.feedyourhead.at/content/kernel-programming-execute-call-usermodehelper-within-a-systemcall – GoinOff Apr 24 '23 at 22:13
  • Note, that using `UMH_WAIT_EXEC` means that `call_usermodehelper` call doesn't wait that the process will *complete* its execution. It is better to use `UMH_WAIT_PROC` instead: https://stackoverflow.com/questions/40385836/why-does-call-usermodehelper-fail-most-of-the-times. – Tsyvarev Apr 24 '23 at 22:17

0 Answers0