0

I am implementing a custom syscall in Linux kernel and need to check if a process is running as superuser. I am using getuid() function like this:

#include <linux/unistd.h>

if (getuid() == 0) {
...
}

However, when I compile I'm getting the error error: implicit declaration of function 'getuid' [-Werror=implicit-function-declaration].

I thought this would only be an issue if it can't find the header file, but I am including it, so I'm not sure where to go. Any ideas?

Baker
  • 43
  • 10
  • 1
    [getuid()](http://man7.org/linux/man-pages/man2/getuid.2.html) is a user-model call you should *NOT* be making from a kernel module! Look here for a solution: [finding the username in a linux system call](https://stackoverflow.com/questions/31149539/finding-the-username-in-a-linux-system-call) – FoggyDay Mar 08 '20 at 03:26
  • @FoggyDay Not sure I understand. Is there no way to call from kernel space? In that post, it says many times that you can only get user id from the kernel (not username because that's what the post is about). Couldn't I just do that and check if uid == 0 in kernel space? – Baker Mar 08 '20 at 16:38
  • When you make a system call (from a Linux "process" running in "user space"), it triggers a "trap" that transfers control to the kernel. The kernel module (running in "kernel space") is *independent* of any given user or process. Look here for more details: https://blog.packagecloud.io/eng/2016/04/05/the-definitive-guide-to-linux-system-calls/ SUGGESTION: If you *really* want/need to check if "uid == 0" ... then why not pass "uid" as one of your syscall's parameters? – FoggyDay Mar 08 '20 at 19:47

0 Answers0