我的代码(如下)失败:11:资源暂时不可用代码以root用户身份运行(在abrt钩子中),但将seteuid设置给正在运行相关pid的用户。从进程中写入/proc/self/coredump_filter可以正常工作。如何从abrt钩子写入coredump_filter?
void SetDumpFlags(pid_t pid, int dumpflags){
std::string c_filter_name = "/proc/" + std::to_string( pid ) + "/coredump_filter";
int f = open( c_filter_name.c_str(), O_WRONLY );
if (f < 0) {
fprintf( log, "Couldn't open %s\n", c_filter_name.c_str());
bail_out(1);
}
int wsz = write( f, &dumpflags, sizeof dumpflags);
if (wsz != sizeof dumpflags){
fprintf( log, "Couldn't write to %s, %d:%s\n", c_filter_name.c_str(),errno, strerror(errno));
close( f );
bail_out(1);
}
close( f );
fprintf( log, "Pid %d, dump filter set to 0x%x\n", pid, dumpflags);
}发布于 2018-06-17 06:26:39
我试着用一个C语言例子来重现你的问题(我会用C++11,但我使用的是没有C++11的老式上网本,很难在这里找到它并使用它)。
我在open上得到了一个EACCESS (我猜你可能也得到了它,但是这个错误可能会在其他地方被覆盖?)
看起来coredump_filter (至少在Linux3.2上)是以根用户身份启动的,seteuid不会改变它。
我在setuid之前尝试过chown,但没有用。
真正起作用的(正如预期的)是在您仍然是root用户时打开fd,并在seteuid调用期间保持打开状态。然后,即使在euid更改之后,我也可以成功地再次写入该文件。
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define FLAGS "0x11"
#define FLAGSSZ (sizeof(FLAGS)-1)
int main()
{
pid_t pid = getpid();
char buf[sizeof("/proc/XXXXXXXXXXXXXX/coredump_filter")];
sprintf(buf,"/proc/%ld/coredump_filter",(long)pid);
int f;
if(0>(f=open(buf,O_WRONLY|O_TRUNC))) {perror("open");exit(1);}
if(FLAGSSZ != write(f,FLAGS,FLAGSSZ)){perror("write");exit(1);}
puts("ok");
char cat[sizeof("cat /proc/XXXXXXXXXXXXXX/coredump_filter")];
sprintf(cat,"cat /proc/%ld/coredump_filter", (long)pid);
system(cat);
char ls[sizeof("ls -l /proc/XXXXXXXXXXXXXX/coredump_filter")];
sprintf(ls,"ls -l /proc/%ld/coredump_filter", (long)pid);
system(ls); //owned by root, not writable by others
if(0>chown(buf,getuid(),getgid())){perror("chown"); exit(1); }
//chown returns success but ls -l doesn't agree
system(ls); //still owned by root
if(0>seteuid(getuid())){
perror("seteuid");
exit(1);
}
//can't reopen because of the perms but can still
//use the old fd if we kept it open
if(0>lseek(f,SEEK_SET,0)){perror("lseek"); exit(1);}
#define ALTFLAGS "0x22"
#define ALTFLAGSSZ (sizeof(ALTFLAGS)-1)
if(ALTFLAGSSZ != write(f,ALTFLAGS,ALTFLAGSSZ)){perror("write");exit(1);}
puts("ok");
system(cat);
}我用gcc c.c编译,并在运行它之前用sudo sh -c 'chown 0 $1 && chmod u+s $1' - a.out创建了a.out的setuid根目录。
发布于 2018-06-18 18:39:59
我正在尝试将数据写入coredump_filter,而我应该写一个字符串!使用文字(例如,PSkocik给出的答案中的#define标志"0x11“)可以修复该问题。
proc/ nnnnn /coredump_filter文件归运行nnnnn进程的用户所有。在我的例子中,对于某些进程,这是根用户,对于其他进程,这是另一个用户。在尝试编写coredump_filter之前,将用户(在abrt钩子中)切换到适当的用户可以正常工作。
https://stackoverflow.com/questions/50879298
复制相似问题