Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以root和非交互身份运行exec()函数?_C_Permissions_Exec_Root_Daemon - Fatal编程技术网

如何以root和非交互身份运行exec()函数?

如何以root和非交互身份运行exec()函数?,c,permissions,exec,root,daemon,C,Permissions,Exec,Root,Daemon,我正在寻找fork()和具有root权限的exec。调用exec函数后,似乎不会从主线程传递特权 现在我看到了一篇描述如何以root身份运行进程的帖子,但是当我尝试他们的解决方案时 char sudo[]="/usr/bin/sudo"; char pbin[]="/usr/local/bin/puppet"; execl(sudo,sudo,pbin,(char *)NULL); sudo命令提示输入守护程序的密码。我正在寻找以root身份运行进程的非交互式方式。除了删除守护程序的密码之外,

我正在寻找
fork()
和具有root权限的exec。调用exec函数后,似乎不会从主线程传递特权

现在我看到了一篇描述如何以root身份运行进程的帖子,但是当我尝试他们的解决方案时

char sudo[]="/usr/bin/sudo";
char pbin[]="/usr/local/bin/puppet";
execl(sudo,sudo,pbin,(char *)NULL);

sudo
命令提示输入守护程序的密码。我正在寻找以root身份运行进程的非交互式方式。除了删除守护程序的密码之外,还有其他方法吗?

要测试您的问题的前提是

“调用exec函数后,似乎不会从主线程传递权限。”

我编写了以下测试代码

#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {
//    printf("starting");
    char sudo[]="/usr/bin/sudo";
    char pbin[]="mkdir";

//    printf("running test: %s %s",sudo,pbin);
    errno=0;

    if (fork() == 0) {
        int res = execl(sudo,sudo,pbin,"/bin/child",(char *)NULL);
//        printf("res:%d", res);
    }
    else {
        sleep(2);
        int res = execl(sudo,sudo,pbin,"/bin/parent",(char *)NULL);
//        printf("res:%d", res);
    }
}
如您所见,有一个由父进程以及具有根权限的子进程创建的目录


这让我想到你的问题其实是别的,正如你所说:

“sudo命令提示输入守护程序的密码。我正在寻找以root用户身份运行进程的非交互式方式。除了删除守护程序的密码外,还有其他方法吗?”

您真正想要的是一款无密码sudo,可以通过运行

sudo visudo
然后添加行:

ALL     ALL=(ALL) NOPASSWD: ALL
让你的sudoers文件看起来像这样

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

ALL ALL=(ALL) NOPASSWD: ALL
# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

如果您可以编写一个C程序,在没有任何安全验证的情况下启动根进程,那么拥有根权限就没有多大意义了,对吗?如果程序需要root权限才能正常工作,请记录该事实,并让用户
sudo
浏览peogram。调用
fork()
的初始进程和线程使用
sudo
运行。调用exec函数时,似乎没有传递这些权限。您能将程序设置为SUID
root
?如果是这样的话,那就很好了。@benjamin:
execve
不会删除特权。你有什么证据证明这是真的?如果以root用户身份启动可执行文件,则所有子进程也将以root用户身份运行,因此您不需要在exec中使用
sudo
。主进程和我要创建的子进程都需要对
/dev/mem
的读取权限。似乎主进程能够很好地阅读,但子进程不能。我将获得一个访问被拒绝/dev/mem错误
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

ALL ALL=(ALL) NOPASSWD: ALL
# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d