我的应用程序需要以非 root 用户身份运行,但需要切换到其他用户来执行一些命令。
我尝试过了:
写一个 JNI,
JNIEXPORT void JNICALL Java_SetUIDJNI_setuid(JNIEnv *env, jobject thisObj,jstring uname) {
const char *name = jstringTostring(env,uname);
int pid;
struct passwd *p;
show_ids();
if ((p = getpwnam(name)) == NULL) {
perror(name);
exit(EXIT_FAILURE);
}
pid = (int) p->pw_uid;
printf("pid=%d\n",pid);
if (seteuid (pid) < 0) {
perror ("setuid");
exit (EXIT_FAILURE);
}
show_ids();
}
以root和chmod u+s构建它
-rwsr-xr-x 1 root root 76155 Aug 7 16:56 libsetuid.so*
在java中调用它
api = new SetUIDJNI(); // invoke the native method
api.setuid("slurm");
但是如果以非root身份运行它,它就不起作用
/opt/jdk1.8/jre/bin/java -Djava.library.path=../jni HelloJNI
The real user ID is: 1000
The effective user ID is :1000 <= which expected is 0
setuid: Operation not permitted
但如果 runner 是 root 则它有效
The real user ID is: 0
The effective user ID is :0
pid=1002The real user ID is: 0
The effective user ID is :1002
这里有什么问题吗?
相关分类