5

I developed a custom system call to log killed processes. A C program kills the process and it invokes the custom system call, passes the process ID of the killed process and then the system call will print out the killed process's ID to the kernel's log. Here I'm just passing a dummy to test if the system call writes to the kernel log. The system call's number in the system call table is 329.

Below is my system call

#include <linux/kernel.h> asmlinkage long sys_killa(char* proc_id) { printk("The process %s has been killed", proc_id); return 0; } 

This is my C program to call my custom system call.

#include <stdio.h> #include <linux/kernel.h> #include <sys/syscall.h> #include <unistd.h> int main() { char proc_id[5] = "3219"; long int sys = syscall(329, proc_id); printf("System call sys_killa returned %ld\n", sys); return 0; } 

Running the C program simply prints "Killed" in the terminal. Running the program again crashes my virtual machine. Nothing is printed out in the kernel log when I check using dmesg utility. What am I doing wrong?

2
  • 2
    You are using a pointer obtained from userspace as-is in kernel space. Oops. You should definitely use copy_from_user() etc.; see existing syscalls for proper approach. Better yet, don't pass a string, pass a process ID (a pid_t). Commented Nov 4, 2016 at 17:12
  • This solved my issue. Thanks a bunch! Commented Nov 4, 2016 at 20:36

1 Answer 1

2

Need to use pid_t variable instead of String.This is the modified system call:

#include <linux/kernel.h> asmlinkage long sys_killa(pid_t pid) { long pid_l = (long) pid; printk("The process %ld has been killed", pid_l); return 0; } 

This is the modified C code to use the system call:

#include <stdio.h> #include <linux/kernel.h> #include <sys/syscall.h> #include <unistd.h> int main() { pid_t pid = 3249; long int sys = syscall(329, pid); printf("System call sys_killa returned %ld\n", sys); return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.