- 提到的案例正在使用内核(5.4.0-91-generic)在 ubuntu 上工作,但它不适用于内核(4.19.71)
- 在更改用户 ID 和组 ID 后,我正在使用 launcher.cpp 启动应用程序,
- 但在那之后 test_app.cpp 无法使用 syslog 进行记录。(没有日志记录)
- 验证日志日志的命令:journalctl -f
- 我能在日志中看到的所有 launcher.cpp 日志只有启动 test_app.cpp 日志没有出现。
启动测试应用程序的命令:
- $sudo ./launcher test_server ./test_app
下面是启动器和测试应用程序代码:
启动器.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <grp.h>
#include<syslog.h>
typedef struct AppID{
unsigned int userID;
unsigned int groupID;
char appName[100];
unsigned int appGroupSize;
gid_t groups[64];
}AppID;
const struct AppID appList[]={
{3070 ,3070 ,"test_server" ,4 ,{ 100, 110, 3010, 3020 }},
};
static inline void settingAppID(const char* nameExec){
gid_t oldgid = getegid();
uid_t olduid = geteuid();
gid_t newgid = 0;
uid_t newuid = 0;
for(unsigned int i=0;i<sizeof(appList)/sizeof(AppID);++i)
{
if(strlen(nameExec) < strlen(appList[i].appName))
continue;
if(memcmp(nameExec , appList[i].appName , strlen(appList[i].appName))==0){
if(appList[i].appGroupSize > 0){
setgroups( appList[i].appGroupSize, appList[i].groups );
}
newuid = appList[i].userID;
newgid = appList[i].groupID;
if (newgid != oldgid && setregid(newgid, newgid) == -1)
{
exit(1);
}
if (newuid != olduid && setreuid(newuid, newuid) == -1)
{
exit(1);
}
// verify that the changes are successful
if (newgid != oldgid && (setegid(oldgid) != -1 || getegid() != newgid))
{
exit(1);
}
if (newuid != olduid && (seteuid(olduid) != -1 || geteuid() != newuid))
{
exit(1);
}
break;
}
}
return;
}
int main(int argc, char* argv[]){
if(argc < 3){
exit(1);
}
syslog (LOG_NOTICE, "launcher : Program started by User ");
settingAppID(argv[1]);
syslog (LOG_NOTICE, "launcher : Program started by User ");
closelog();
execvp (argv[2],&argv[2]);
return 0;
}
test_app.cpp:
#include<syslog.h>
int main()
{
openlog("launcher", LOG_PID|LOG_CONS, LOG_USER);
syslog (LOG_NOTICE, "test : Program started by User ");
syslog (LOG_INFO, "test : A tree falls in a forest");
syslog (LOG_NOTICE, "test : Program started by User ");
syslog (LOG_INFO, "test : A tree falls in a forest");
closelog();
return 0;
}