gpt4 book ai didi

c - 向需要 root 权限的 Linux 添加新的系统调用

转载 作者:太空狗 更新时间:2023-10-29 11:18:17 24 4
gpt4 key购买 nike

我正在尝试向需要 root 权限才能运行的 linux 内核(版本:3.10.91)添加系统调用。

你可以在下面看到我的尝试:

#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <asm/errno.h>

asmlinkage long sys_set_casper(pid_t pid, int value)
{
struct task_struct *process;

if (!capable(CAP_SYS_ADMIN))
return -EPERM;

//valid values are 0,1,2,3
if (value != 0 && value != 1 && value != 2 && value != 3 )
return -EINVAL;

process = find_task_by_vpid(pid);
if (process == NULL)
return -ESRCH;

//modify the casper field accordingly
process->casper = value;
return 0;
}

Casper 只是我添加的任务描述符。基本上,当 casper 值为 1 时,我希望进程被隐藏(ps、pstree、top 等不可见)。内核重新编译很好(我还在 base.c 等中做了必要的更改)。

我尝试使用以下代码测试我的系统调用,test.c:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NR_set_casper 351

int main(int argc, char **argv)
{
long y;
printf("PID of current process: %d\n\n", getpid());

printf("Call set_casper system call to set flag 1\n");
y = syscall(NR_set_casper, getpid(), 0);
printf("Return value of set_casper system call: %ld\n", y);
if (y < 0)
{
printf("set_casper system call failed. Run with sudo\n");
return EXIT_FAILURE;
}

return 0;
}

我编译运行如下:

gcc test.c

sudo ./a.out

输出是:

PID of current process: 3852

Call set_casper system call to set flag 1
Return value of set_casper system call: -1
set_casper system call failed. Run with sudo

奇怪的是,即使在删除 sudo 控制线之后:

 if (!capable(CAP_SYS_ADMIN))
return -EPERM;

重新编译内核,还是报同样的错误。

基本上,为什么我的 sys_set_casper 函数返回 -1?

编辑

我添加了这个: 351 i386 set_casper sys_set_casper 到arch/x86/syscalls$ gedit syscall_32.tbl

但是,我的系统是 64 位的。这可能是问题所在吗?

最佳答案

正如其他人在评论中所说,问题是我根本没有调用系统调用。我只是将我的调用添加到 64 位系统调用表中,然后再次尝试一切,它成功了。

关于c - 向需要 root 权限的 Linux 添加新的系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33335827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com