在C中运行setuid程序的正确方法
发布时间:2020-12-16 03:24:29 所属栏目:百科 来源:网络整理
导读:我有一个权限为4750的进程.我的 Linux系统中存在两个用户. root用户和appz用户.该进程继承了以“appz”用户身份运行的进程管理器的权限. 我有两个基本的例程: void do_root (void){ int status; status = seteuid (euid); if (status 0) { exit (status); }
我有一个权限为4750的进程.我的
Linux系统中存在两个用户. root用户和appz用户.该进程继承了以“appz”用户身份运行的进程管理器的权限.
我有两个基本的例程: void do_root (void) { int status; status = seteuid (euid); if (status < 0) { exit (status); } } /* undo root permissions */ void undo_root (void) { int status; status = seteuid (ruid); if (status < 0) { exit (status); } status = setuid(ruid); if (status < 0) { exit (status); } } 我的流程如下: int main() { undo_root(); do some stuff; do_root(); bind( port 80); //needs root perm undo_root(); while(1) { accept commads() if ( commands needs root user access) { do_root(); execute(); undo_root(); } } 如您所见,我想以root身份执行一些命令.我试图临时删除权限,如果任务需要root访问权限,我在do_root和undo_root调用之间包装命令. 然而,似乎我的程序不起作用. 做这个规范的方法是什么? 解决方法
老派的方法是在do_root和undo_root中使用setreuid()来交换ruid和euid:
setreuid(geteuid(),getuid()); 如果程序足够小,可以进行完整的安全审计,那么这是完全可以接受的. 新学校的方式要复杂得多,并且涉及fork()关闭一个孩子,该子女接受以root身份做什么的指令,然后做setuid(getuid())以永久地在父母中删除root.孩子负责验证它收到的所有指令.对于足够大的程序,这会丢弃必须进行安全审计的代码量,并允许用户使用作业控制来管理进程或将其终止等. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |