我的應(yīng)用程序需要以非 root 用戶身份運(yùn)行,但需要切換到其他用戶來執(zhí)行一些命令。我嘗試過了:寫一個 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構(gòu)建它-rwsr-xr-x 1 root root 76155 Aug 7 16:56 libsetuid.so*在java中調(diào)用它api = new SetUIDJNI(); // invoke the native methodapi.setuid("slurm");但是如果以非root身份運(yùn)行它,它就不起作用/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: 0The effective user ID is :0pid=1002The real user ID is: 0The effective user ID is :1002這里有什么問題嗎?
無法通過調(diào)用 JNI 在 Java 中設(shè)置 seteuid
慕田峪9158850
2021-06-30 13:23:52